Correct math and limerick.
[dragonfly.git] / contrib / less / main.c
blobda61b6bbeffaca2b2181d7a32320fba4ae94e044
1 /*
2 * Copyright (C) 1984-2012 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information, see the README file.
8 */
12 * Entry point, initialization, miscellaneous routines.
15 #include "less.h"
16 #if MSDOS_COMPILER==WIN32C
17 #include <windows.h>
18 #endif
20 public char * every_first_cmd = NULL;
21 public int new_file;
22 public int is_tty;
23 public IFILE curr_ifile = NULL_IFILE;
24 public IFILE old_ifile = NULL_IFILE;
25 public struct scrpos initial_scrpos;
26 public int any_display = FALSE;
27 public POSITION start_attnpos = NULL_POSITION;
28 public POSITION end_attnpos = NULL_POSITION;
29 public int wscroll;
30 public char * progname;
31 public int quitting;
32 public int secure;
33 public int dohelp;
35 #if LOGFILE
36 public int logfile = -1;
37 public int force_logfile = FALSE;
38 public char * namelogfile = NULL;
39 #endif
41 #if EDITOR
42 public char * editor;
43 public char * editproto;
44 #endif
46 #if TAGS
47 extern char * tags;
48 extern char * tagoption;
49 extern int jump_sline;
50 #endif
52 #ifdef WIN32
53 static char consoleTitle[256];
54 #endif
56 extern int less_is_more;
57 extern int missing_cap;
58 extern int know_dumb;
59 extern int quit_if_one_screen;
60 extern int pr_type;
64 * Entry point.
66 int
67 main(argc, argv)
68 int argc;
69 char *argv[];
71 IFILE ifile;
72 char *s;
74 #ifdef __EMX__
75 _response(&argc, &argv);
76 _wildcard(&argc, &argv);
77 #endif
79 progname = *argv++;
80 argc--;
82 secure = 0;
83 s = lgetenv("LESSSECURE");
84 if (s != NULL && *s != '\0')
85 secure = 1;
87 #ifdef WIN32
88 if (getenv("HOME") == NULL)
91 * If there is no HOME environment variable,
92 * try the concatenation of HOMEDRIVE + HOMEPATH.
94 char *drive = getenv("HOMEDRIVE");
95 char *path = getenv("HOMEPATH");
96 if (drive != NULL && path != NULL)
98 char *env = (char *) ecalloc(strlen(drive) +
99 strlen(path) + 6, sizeof(char));
100 strcpy(env, "HOME=");
101 strcat(env, drive);
102 strcat(env, path);
103 putenv(env);
106 GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
107 #endif /* WIN32 */
110 * Process command line arguments and LESS environment arguments.
111 * Command line arguments override environment arguments.
113 is_tty = isatty(1);
114 get_term();
115 init_cmds();
116 init_charset();
117 init_line();
118 init_cmdhist();
119 init_option();
120 init_search();
123 * If the name of the executable program is "more",
124 * act like LESS_IS_MORE is set.
126 for (s = progname + strlen(progname); s > progname; s--)
128 if (s[-1] == PATHNAME_SEP[0])
129 break;
131 if (strcmp(s, "more") == 0)
132 less_is_more = 1;
134 init_prompt();
136 s = lgetenv(less_is_more ? "MORE" : "LESS");
137 if (s != NULL)
138 scan_option(save(s));
140 #define isoptstring(s) (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
141 while (argc > 0 && (isoptstring(*argv) || isoptpending()))
143 s = *argv++;
144 argc--;
145 if (strcmp(s, "--") == 0)
146 break;
147 scan_option(s);
149 #undef isoptstring
151 if (isoptpending())
154 * Last command line option was a flag requiring a
155 * following string, but there was no following string.
157 nopendopt();
158 quit(QUIT_OK);
161 if (less_is_more && get_quit_at_eof())
162 quit_if_one_screen = TRUE;
164 #if EDITOR
165 editor = lgetenv("VISUAL");
166 if (editor == NULL || *editor == '\0')
168 editor = lgetenv("EDITOR");
169 if (editor == NULL || *editor == '\0')
170 editor = EDIT_PGM;
172 editproto = lgetenv("LESSEDIT");
173 if (editproto == NULL || *editproto == '\0')
174 editproto = "%E ?lm+%lm. %f";
175 #endif
178 * Call get_ifile with all the command line filenames
179 * to "register" them with the ifile system.
181 ifile = NULL_IFILE;
182 if (dohelp)
183 ifile = get_ifile(FAKE_HELPFILE, ifile);
184 while (argc-- > 0)
186 char *filename;
187 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
189 * Because the "shell" doesn't expand filename patterns,
190 * treat each argument as a filename pattern rather than
191 * a single filename.
192 * Expand the pattern and iterate over the expanded list.
194 struct textlist tlist;
195 char *gfilename;
197 gfilename = lglob(*argv++);
198 init_textlist(&tlist, gfilename);
199 filename = NULL;
200 while ((filename = forw_textlist(&tlist, filename)) != NULL)
202 (void) get_ifile(filename, ifile);
203 ifile = prev_ifile(NULL_IFILE);
205 free(gfilename);
206 #else
207 filename = shell_quote(*argv);
208 if (filename == NULL)
209 filename = *argv;
210 argv++;
211 (void) get_ifile(filename, ifile);
212 ifile = prev_ifile(NULL_IFILE);
213 free(filename);
214 #endif
217 * Set up terminal, etc.
219 if (!is_tty)
222 * Output is not a tty.
223 * Just copy the input file(s) to output.
225 SET_BINARY(1);
226 if (nifile() == 0)
228 if (edit_stdin() == 0)
229 cat_file();
230 } else if (edit_first() == 0)
232 do {
233 cat_file();
234 } while (edit_next(1) == 0);
236 quit(QUIT_OK);
239 if (missing_cap && !know_dumb)
240 error("WARNING: terminal is not fully functional", NULL_PARG);
241 init_mark();
242 open_getchr();
243 raw_mode(1);
244 init_signals(1);
247 * Select the first file to examine.
249 #if TAGS
250 if (tagoption != NULL || strcmp(tags, "-") == 0)
253 * A -t option was given.
254 * Verify that no filenames were also given.
255 * Edit the file selected by the "tags" search,
256 * and search for the proper line in the file.
258 if (nifile() > 0)
260 error("No filenames allowed with -t option", NULL_PARG);
261 quit(QUIT_ERROR);
263 findtag(tagoption);
264 if (edit_tagfile()) /* Edit file which contains the tag */
265 quit(QUIT_ERROR);
267 * Search for the line which contains the tag.
268 * Set up initial_scrpos so we display that line.
270 initial_scrpos.pos = tagsearch();
271 if (initial_scrpos.pos == NULL_POSITION)
272 quit(QUIT_ERROR);
273 initial_scrpos.ln = jump_sline;
274 } else
275 #endif
276 if (nifile() == 0)
278 if (edit_stdin()) /* Edit standard input */
279 quit(QUIT_ERROR);
280 } else
282 if (edit_first()) /* Edit first valid file in cmd line */
283 quit(QUIT_ERROR);
286 init();
287 commands();
288 quit(QUIT_OK);
289 /*NOTREACHED*/
290 return (0);
294 * Copy a string to a "safe" place
295 * (that is, to a buffer allocated by calloc).
297 public char *
298 save(s)
299 char *s;
301 register char *p;
303 p = (char *) ecalloc(strlen(s)+1, sizeof(char));
304 strcpy(p, s);
305 return (p);
309 * Allocate memory.
310 * Like calloc(), but never returns an error (NULL).
312 public VOID_POINTER
313 ecalloc(count, size)
314 int count;
315 unsigned int size;
317 register VOID_POINTER p;
319 p = (VOID_POINTER) calloc(count, size);
320 if (p != NULL)
321 return (p);
322 error("Cannot allocate memory", NULL_PARG);
323 quit(QUIT_ERROR);
324 /*NOTREACHED*/
325 return (NULL);
329 * Skip leading spaces in a string.
331 public char *
332 skipsp(s)
333 register char *s;
335 while (*s == ' ' || *s == '\t')
336 s++;
337 return (s);
341 * See how many characters of two strings are identical.
342 * If uppercase is true, the first string must begin with an uppercase
343 * character; the remainder of the first string may be either case.
345 public int
346 sprefix(ps, s, uppercase)
347 char *ps;
348 char *s;
349 int uppercase;
351 register int c;
352 register int sc;
353 register int len = 0;
355 for ( ; *s != '\0'; s++, ps++)
357 c = *ps;
358 if (uppercase)
360 if (len == 0 && ASCII_IS_LOWER(c))
361 return (-1);
362 if (ASCII_IS_UPPER(c))
363 c = ASCII_TO_LOWER(c);
365 sc = *s;
366 if (len > 0 && ASCII_IS_UPPER(sc))
367 sc = ASCII_TO_LOWER(sc);
368 if (c != sc)
369 break;
370 len++;
372 return (len);
376 * Exit the program.
378 public void
379 quit(status)
380 int status;
382 static int save_status;
385 * Put cursor at bottom left corner, clear the line,
386 * reset the terminal modes, and exit.
388 if (status < 0)
389 status = save_status;
390 else
391 save_status = status;
392 quitting = 1;
393 edit((char*)NULL);
394 save_cmdhist();
395 if (any_display && is_tty)
396 clear_bot();
397 deinit();
398 flush();
399 raw_mode(0);
400 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
402 * If we don't close 2, we get some garbage from
403 * 2's buffer when it flushes automatically.
404 * I cannot track this one down RB
405 * The same bug shows up if we use ^C^C to abort.
407 close(2);
408 #endif
409 #ifdef WIN32
410 SetConsoleTitle(consoleTitle);
411 #endif
412 close_getchr();
413 exit(status);