3 * Copyright (C) 1984-2019 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, see the README file.
13 * Entry point, initialization, miscellaneous routines.
17 #if MSDOS_COMPILER==WIN32C
18 #define WIN32_LEAN_AND_MEAN
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;
38 public int logfile = -1;
39 public int force_logfile = FALSE;
40 public char * namelogfile = NULL;
45 public char * editproto;
50 extern char * tagoption;
51 extern int jump_sline;
55 static char consoleTitle[256];
58 public int one_screen;
59 extern int less_is_more;
60 extern int missing_cap;
64 extern int quit_if_one_screen;
80 _response(&argc, &argv);
81 _wildcard(&argc, &argv);
88 s = lgetenv("LESSSECURE");
93 if (getenv("HOME") == NULL)
96 * If there is no HOME environment variable,
97 * try the concatenation of HOMEDRIVE + HOMEPATH.
99 char *drive = getenv("HOMEDRIVE");
100 char *path = getenv("HOMEPATH");
101 if (drive != NULL && path != NULL)
103 char *env = (char *) ecalloc(strlen(drive) +
104 strlen(path) + 6, sizeof(char));
105 strcpy(env, "HOME=");
111 GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
115 * Process command line arguments and LESS environment arguments.
116 * Command line arguments override environment arguments.
130 * If the name of the executable program is "more",
131 * act like LESS_IS_MORE is set.
133 s = last_component(progname);
134 if (strcmp(s, "more") == 0)
142 s = lgetenv(less_is_more ? "MORE" : "LESS");
144 scan_option(save(s));
146 #define isoptstring(s) less_is_more ? (((s)[0] == '-') && (s)[1] != '\0') : \
147 (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
148 while (argc > 0 && (isoptstring(*argv) || isoptpending()))
152 if (strcmp(s, "--") == 0)
161 * Last command line option was a flag requiring a
162 * following string, but there was no following string.
172 editor = lgetenv("VISUAL");
173 if (editor == NULL || *editor == '\0')
175 editor = lgetenv("EDITOR");
176 if (isnullenv(editor))
179 editproto = lgetenv("LESSEDIT");
180 if (isnullenv(editproto))
181 editproto = "%E ?lm+%lm. %g";
185 * Call get_ifile with all the command line filenames
186 * to "register" them with the ifile system.
190 ifile = get_ifile(FAKE_HELPFILE, ifile);
193 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
195 * Because the "shell" doesn't expand filename patterns,
196 * treat each argument as a filename pattern rather than
198 * Expand the pattern and iterate over the expanded list.
200 struct textlist tlist;
205 gfilename = lglob(*argv++);
206 init_textlist(&tlist, gfilename);
208 while ((filename = forw_textlist(&tlist, filename)) != NULL)
210 qfilename = shell_unquote(filename);
211 (void) get_ifile(qfilename, ifile);
213 ifile = prev_ifile(NULL_IFILE);
217 (void) get_ifile(*argv++, ifile);
218 ifile = prev_ifile(NULL_IFILE);
222 * Set up terminal, etc.
227 * Output is not a tty.
228 * Just copy the input file(s) to output.
231 if (edit_first() == 0)
235 } while (edit_next(1) == 0);
240 if (missing_cap && !know_dumb && !less_is_more)
241 error("WARNING: terminal is not fully functional", NULL_PARG);
247 * Select the first file to examine.
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.
260 error("No filenames allowed with -t option", NULL_PARG);
264 if (edit_tagfile()) /* Edit file which contains the tag */
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)
273 initial_scrpos.ln = jump_sline;
280 * See if file fits on one screen to decide whether
281 * to send terminal init. But don't need this
282 * if -X (no_init) overrides this (see init()).
284 if (quit_if_one_screen)
286 if (nifile() > 1) /* If more than one file, -F cannot be used */
287 quit_if_one_screen = FALSE;
289 one_screen = get_one_screen();
301 * Copy a string to a "safe" place
302 * (that is, to a buffer allocated by calloc).
310 p = (char *) ecalloc(strlen(s)+1, sizeof(char));
317 * Like calloc(), but never returns an error (NULL).
326 p = (VOID_POINTER) calloc(count, size);
329 error("Cannot allocate memory", NULL_PARG);
336 * Skip leading spaces in a string.
342 while (*s == ' ' || *s == '\t')
348 * See how many characters of two strings are identical.
349 * If uppercase is true, the first string must begin with an uppercase
350 * character; the remainder of the first string may be either case.
353 sprefix(ps, s, uppercase)
362 for ( ; *s != '\0'; s++, ps++)
367 if (len == 0 && ASCII_IS_LOWER(c))
369 if (ASCII_IS_UPPER(c))
370 c = ASCII_TO_LOWER(c);
373 if (len > 0 && ASCII_IS_UPPER(sc))
374 sc = ASCII_TO_LOWER(sc);
389 static int save_status;
392 * Put cursor at bottom left corner, clear the line,
393 * reset the terminal modes, and exit.
396 status = save_status;
398 save_status = status;
402 if (any_display && is_tty)
407 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
409 * If we don't close 2, we get some garbage from
410 * 2's buffer when it flushes automatically.
411 * I cannot track this one down RB
412 * The same bug shows up if we use ^C^C to abort.
417 SetConsoleTitle(consoleTitle);