3 * Copyright (C) 1984-2021 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 POSITION start_attnpos = NULL_POSITION;
29 public POSITION end_attnpos = NULL_POSITION;
31 public char * progname;
37 public int logfile = -1;
38 public int force_logfile = FALSE;
39 public char * namelogfile = NULL;
44 public char * editproto;
49 extern char * tagoption;
50 extern int jump_sline;
54 static char consoleTitle[256];
57 public int one_screen;
58 extern int less_is_more;
59 extern int missing_cap;
62 extern int quit_if_one_screen;
79 _response(&argc, &argv);
80 _wildcard(&argc, &argv);
90 s = lgetenv("LESSSECURE");
96 if (getenv("HOME") == NULL)
99 * If there is no HOME environment variable,
100 * try the concatenation of HOMEDRIVE + HOMEPATH.
102 char *drive = getenv("HOMEDRIVE");
103 char *path = getenv("HOMEPATH");
104 if (drive != NULL && path != NULL)
106 char *env = (char *) ecalloc(strlen(drive) +
107 strlen(path) + 6, sizeof(char));
108 strcpy(env, "HOME=");
114 GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
118 * Process command line arguments and LESS environment arguments.
119 * Command line arguments override environment arguments.
132 * If the name of the executable program is "more",
133 * act like LESS_IS_MORE is set.
135 s = last_component(progname);
136 if (strcmp(s, "more") == 0)
144 s = lgetenv(less_is_more ? "MORE" : "LESS");
146 scan_option(save(s));
148 #define isoptstring(s) less_is_more ? (((s)[0] == '-') && (s)[1] != '\0') : \
149 (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
150 while (argc > 0 && (isoptstring(*argv) || isoptpending()))
154 if (strcmp(s, "--") == 0)
163 * Last command line option was a flag requiring a
164 * following string, but there was no following string.
176 editor = lgetenv("VISUAL");
177 if (editor == NULL || *editor == '\0')
179 editor = lgetenv("EDITOR");
180 if (isnullenv(editor))
183 editproto = lgetenv("LESSEDIT");
184 if (isnullenv(editproto))
185 editproto = "%E ?lm+%lm. %g";
189 * Call get_ifile with all the command line filenames
190 * to "register" them with the ifile system.
194 ifile = get_ifile(FAKE_HELPFILE, ifile);
197 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
199 * Because the "shell" doesn't expand filename patterns,
200 * treat each argument as a filename pattern rather than
202 * Expand the pattern and iterate over the expanded list.
204 struct textlist tlist;
209 gfilename = lglob(*argv++);
210 init_textlist(&tlist, gfilename);
212 while ((filename = forw_textlist(&tlist, filename)) != NULL)
214 qfilename = shell_unquote(filename);
215 (void) get_ifile(qfilename, ifile);
217 ifile = prev_ifile(NULL_IFILE);
221 (void) get_ifile(*argv++, ifile);
222 ifile = prev_ifile(NULL_IFILE);
226 * Set up terminal, etc.
231 * Output is not a tty.
232 * Just copy the input file(s) to output.
234 set_output(1); /* write to stdout */
236 if (edit_first() == 0)
240 } while (edit_next(1) == 0);
245 if (missing_cap && !know_dumb && !less_is_more)
246 error("WARNING: terminal is not fully functional", NULL_PARG);
252 * Select the first file to examine.
255 if (tagoption != NULL || strcmp(tags, "-") == 0)
258 * A -t option was given.
259 * Verify that no filenames were also given.
260 * Edit the file selected by the "tags" search,
261 * and search for the proper line in the file.
265 error("No filenames allowed with -t option", NULL_PARG);
269 if (edit_tagfile()) /* Edit file which contains the tag */
272 * Search for the line which contains the tag.
273 * Set up initial_scrpos so we display that line.
275 initial_scrpos.pos = tagsearch();
276 if (initial_scrpos.pos == NULL_POSITION)
278 initial_scrpos.ln = jump_sline;
285 * See if file fits on one screen to decide whether
286 * to send terminal init. But don't need this
287 * if -X (no_init) overrides this (see init()).
289 if (quit_if_one_screen)
291 if (nifile() > 1) /* If more than one file, -F cannot be used */
292 quit_if_one_screen = FALSE;
294 one_screen = get_one_screen();
301 * We displayed some messages on error output
302 * (file descriptor 2; see flush()).
303 * Before erasing the screen contents, wait for a keystroke.
305 less_printf("Press RETURN to continue ", NULL_PARG);
318 * Copy a string to a "safe" place
319 * (that is, to a buffer allocated by calloc).
327 p = (char *) ecalloc(strlen(s)+1, sizeof(char));
334 * Like calloc(), but never returns an error (NULL).
343 p = (VOID_POINTER) calloc(count, size);
346 error("Cannot allocate memory", NULL_PARG);
353 * Skip leading spaces in a string.
359 while (*s == ' ' || *s == '\t')
365 * See how many characters of two strings are identical.
366 * If uppercase is true, the first string must begin with an uppercase
367 * character; the remainder of the first string may be either case.
370 sprefix(ps, s, uppercase)
379 for ( ; *s != '\0'; s++, ps++)
384 if (len == 0 && ASCII_IS_LOWER(c))
386 if (ASCII_IS_UPPER(c))
387 c = ASCII_TO_LOWER(c);
390 if (len > 0 && ASCII_IS_UPPER(sc))
391 sc = ASCII_TO_LOWER(sc);
406 static int save_status;
409 * Put cursor at bottom left corner, clear the line,
410 * reset the terminal modes, and exit.
413 status = save_status;
415 save_status = status;
427 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
429 * If we don't close 2, we get some garbage from
430 * 2's buffer when it flushes automatically.
431 * I cannot track this one down RB
432 * The same bug shows up if we use ^C^C to abort.
437 SetConsoleTitle(consoleTitle);