3 * Copyright (C) 1984-2000 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)
135 scan_option(save(s));
137 #define isoptstring(s) (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
138 while (argc > 0 && (isoptstring(*argv) || isoptpending()))
142 if (strcmp(s, "--") == 0)
151 * Last command line option was a flag requiring a
152 * following string, but there was no following string.
159 editor = lgetenv("VISUAL");
160 if (editor == NULL || *editor == '\0')
162 editor = lgetenv("EDITOR");
163 if (editor == NULL || *editor == '\0')
166 editproto = lgetenv("LESSEDIT");
167 if (editproto == NULL || *editproto == '\0')
168 editproto = "%E ?lm+%lm. %f";
172 * Call get_ifile with all the command line filenames
173 * to "register" them with the ifile system.
177 ifile = get_ifile(FAKE_HELPFILE, ifile);
180 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
182 * Because the "shell" doesn't expand filename patterns,
183 * treat each argument as a filename pattern rather than
185 * Expand the pattern and iterate over the expanded list.
187 struct textlist tlist;
191 gfilename = lglob(*argv++);
192 init_textlist(&tlist, gfilename);
194 while ((filename = forw_textlist(&tlist, filename)) != NULL)
195 ifile = get_ifile(filename, ifile);
198 ifile = get_ifile(*argv++, ifile);
202 * Set up terminal, etc.
207 * Output is not a tty.
208 * Just copy the input file(s) to output.
213 if (edit_stdin() == 0)
215 } else if (edit_first() == 0)
219 } while (edit_next(1) == 0);
224 if (missing_cap && !know_dumb && !more_mode)
225 error("WARNING: terminal is not fully functional", NULL_PARG);
233 * Select the first file to examine.
236 if (tagoption != NULL || strcmp(tags, "-") == 0)
239 * A -t option was given.
240 * Verify that no filenames were also given.
241 * Edit the file selected by the "tags" search,
242 * and search for the proper line in the file.
246 error("No filenames allowed with -t option", NULL_PARG);
250 if (edit_tagfile()) /* Edit file which contains the tag */
253 * Search for the line which contains the tag.
254 * Set up initial_scrpos so we display that line.
256 initial_scrpos.pos = tagsearch();
257 if (initial_scrpos.pos == NULL_POSITION)
259 initial_scrpos.ln = jump_sline;
264 if (edit_stdin()) /* Edit standard input */
268 if (edit_first()) /* Edit first valid file in cmd line */
279 * Copy a string to a "safe" place
280 * (that is, to a buffer allocated by calloc).
288 p = (char *) ecalloc(strlen(s)+1, sizeof(char));
295 * Like calloc(), but never returns an error (NULL).
302 register VOID_POINTER p;
304 p = (VOID_POINTER) calloc(count, size);
307 error("Cannot allocate memory", NULL_PARG);
313 * Skip leading spaces in a string.
319 while (*s == ' ' || *s == '\t')
325 * See how many characters of two strings are identical.
326 * If uppercase is true, the first string must begin with an uppercase
327 * character; the remainder of the first string may be either case.
330 sprefix(ps, s, uppercase)
337 register int len = 0;
339 for ( ; *s != '\0'; s++, ps++)
344 if (len == 0 && SIMPLE_IS_LOWER(c))
346 if (SIMPLE_IS_UPPER(c))
347 c = SIMPLE_TO_LOWER(c);
350 if (len > 0 && SIMPLE_IS_UPPER(sc))
351 sc = SIMPLE_TO_LOWER(sc);
366 static int save_status;
369 * Put cursor at bottom left corner, clear the line,
370 * reset the terminal modes, and exit.
373 status = save_status;
375 save_status = status;
378 if (any_display && is_tty)
383 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
385 * If we don't close 2, we get some garbage from
386 * 2's buffer when it flushes automatically.
387 * I cannot track this one down RB
388 * The same bug shows up if we use ^C^C to abort.
393 SetConsoleTitle(consoleTitle);