3 * Copyright (C) 1984-2004 Mark Nudelman
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Less License, as specified in the README file.
8 * For more information about less, or for information on how to
9 * contact the author, see the README file.
14 * Entry point, initialization, miscellaneous routines.
18 #if MSDOS_COMPILER==WIN32C
22 public char * every_first_cmd = NULL;
25 public IFILE curr_ifile = NULL_IFILE;
26 public IFILE old_ifile = NULL_IFILE;
27 public struct scrpos initial_scrpos;
28 public int any_display = FALSE;
29 public POSITION start_attnpos = NULL_POSITION;
30 public POSITION end_attnpos = NULL_POSITION;
32 public char * progname;
36 public int more_mode = 0;
39 public int logfile = -1;
40 public int force_logfile = FALSE;
41 public char * namelogfile = NULL;
46 public char * editproto;
51 extern char * tagoption;
52 extern int jump_sline;
56 static char consoleTitle[256];
59 extern int missing_cap;
73 extern char *__progname;
76 _response(&argc, &argv);
77 _wildcard(&argc, &argv);
84 s = lgetenv("LESSSECURE");
85 if (s != NULL && *s != '\0')
89 if (getenv("HOME") == NULL)
92 * If there is no HOME environment variable,
93 * try the concatenation of HOMEDRIVE + HOMEPATH.
95 char *drive = getenv("HOMEDRIVE");
96 char *path = getenv("HOMEPATH");
97 if (drive != NULL && path != NULL)
99 char *env = (char *) ecalloc(strlen(drive) +
100 strlen(path) + 6, sizeof(char));
101 strcpy(env, "HOME=");
107 GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
111 * Process command line arguments and LESS environment arguments.
112 * Command line arguments override environment arguments.
114 if (strcmp(__progname, "more") == 0)
136 scan_option(save(s));
138 #define isoptstring(s) (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
139 while (argc > 0 && (isoptstring(*argv) || isoptpending()))
143 if (strcmp(s, "--") == 0)
152 * Last command line option was a flag requiring a
153 * following string, but there was no following string.
160 editor = lgetenv("VISUAL");
161 if (editor == NULL || *editor == '\0')
163 editor = lgetenv("EDITOR");
164 if (editor == NULL || *editor == '\0')
167 editproto = lgetenv("LESSEDIT");
168 if (editproto == NULL || *editproto == '\0')
169 editproto = "%E ?lm+%lm. %f";
173 * Call get_ifile with all the command line filenames
174 * to "register" them with the ifile system.
178 ifile = get_ifile(FAKE_HELPFILE, ifile);
182 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
184 * Because the "shell" doesn't expand filename patterns,
185 * treat each argument as a filename pattern rather than
187 * Expand the pattern and iterate over the expanded list.
189 struct textlist tlist;
192 gfilename = lglob(*argv++);
193 init_textlist(&tlist, gfilename);
195 while ((filename = forw_textlist(&tlist, filename)) != NULL)
197 (void) get_ifile(filename, ifile);
198 ifile = prev_ifile(NULL_IFILE);
202 filename = shell_quote(*argv);
203 if (filename == NULL)
206 (void) get_ifile(filename, ifile);
207 ifile = prev_ifile(NULL_IFILE);
211 * Set up terminal, etc.
216 * Output is not a tty.
217 * Just copy the input file(s) to output.
222 if (edit_stdin() == 0)
224 } else if (edit_first() == 0)
228 } while (edit_next(1) == 0);
233 if (missing_cap && !know_dumb && !more_mode)
234 error("WARNING: terminal is not fully functional", NULL_PARG);
241 * Select the first file to examine.
244 if (tagoption != NULL || strcmp(tags, "-") == 0)
247 * A -t option was given.
248 * Verify that no filenames were also given.
249 * Edit the file selected by the "tags" search,
250 * and search for the proper line in the file.
254 error("No filenames allowed with -t option", NULL_PARG);
258 if (edit_tagfile()) /* Edit file which contains the tag */
261 * Search for the line which contains the tag.
262 * Set up initial_scrpos so we display that line.
264 initial_scrpos.pos = tagsearch();
265 if (initial_scrpos.pos == NULL_POSITION)
267 initial_scrpos.ln = jump_sline;
272 if (edit_stdin()) /* Edit standard input */
276 if (edit_first()) /* Edit first valid file in cmd line */
288 * Copy a string to a "safe" place
289 * (that is, to a buffer allocated by calloc).
297 p = (char *) ecalloc(strlen(s)+1, sizeof(char));
304 * Like calloc(), but never returns an error (NULL).
311 register VOID_POINTER p;
313 p = (VOID_POINTER) calloc(count, size);
316 error("Cannot allocate memory", NULL_PARG);
323 * Skip leading spaces in a string.
329 while (*s == ' ' || *s == '\t')
335 * See how many characters of two strings are identical.
336 * If uppercase is true, the first string must begin with an uppercase
337 * character; the remainder of the first string may be either case.
340 sprefix(ps, s, uppercase)
347 register int len = 0;
349 for ( ; *s != '\0'; s++, ps++)
354 if (len == 0 && ASCII_IS_LOWER(c))
356 if (ASCII_IS_UPPER(c))
357 c = ASCII_TO_LOWER(c);
360 if (len > 0 && ASCII_IS_UPPER(sc))
361 sc = ASCII_TO_LOWER(sc);
376 static int save_status;
379 * Put cursor at bottom left corner, clear the line,
380 * reset the terminal modes, and exit.
383 status = save_status;
385 save_status = status;
389 if (any_display && is_tty)
394 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
396 * If we don't close 2, we get some garbage from
397 * 2's buffer when it flushes automatically.
398 * I cannot track this one down RB
399 * The same bug shows up if we use ^C^C to abort.
404 SetConsoleTitle(consoleTitle);