3 * Copyright (C) 1984-2022 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;
65 extern int redraw_on_quit;
66 extern int term_init_done;
67 extern int first_time;
81 _response(&argc, &argv);
82 _wildcard(&argc, &argv);
92 s = lgetenv("LESSSECURE");
98 if (getenv("HOME") == NULL)
101 * If there is no HOME environment variable,
102 * try the concatenation of HOMEDRIVE + HOMEPATH.
104 char *drive = getenv("HOMEDRIVE");
105 char *path = getenv("HOMEPATH");
106 if (drive != NULL && path != NULL)
108 char *env = (char *) ecalloc(strlen(drive) +
109 strlen(path) + 6, sizeof(char));
110 strcpy(env, "HOME=");
116 GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
120 * Process command line arguments and LESS environment arguments.
121 * Command line arguments override environment arguments.
134 * If the name of the executable program is "more",
135 * act like LESS_IS_MORE is set.
137 s = last_component(progname);
138 if (strcmp(s, "more") == 0)
146 s = lgetenv(less_is_more ? "MORE" : "LESS");
150 #define isoptstring(s) less_is_more ? (((s)[0] == '-') && (s)[1] != '\0') : \
151 (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
152 while (argc > 0 && (isoptstring(*argv) || isoptpending()))
156 if (strcmp(s, "--") == 0)
165 * Last command line option was a flag requiring a
166 * following string, but there was no following string.
178 editor = lgetenv("VISUAL");
179 if (editor == NULL || *editor == '\0')
181 editor = lgetenv("EDITOR");
182 if (isnullenv(editor))
185 editproto = lgetenv("LESSEDIT");
186 if (isnullenv(editproto))
187 editproto = "%E ?lm+%lm. %g";
191 * Call get_ifile with all the command line filenames
192 * to "register" them with the ifile system.
196 ifile = get_ifile(FAKE_HELPFILE, ifile);
199 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
201 * Because the "shell" doesn't expand filename patterns,
202 * treat each argument as a filename pattern rather than
204 * Expand the pattern and iterate over the expanded list.
206 struct textlist tlist;
211 gfilename = lglob(*argv++);
212 init_textlist(&tlist, gfilename);
214 while ((filename = forw_textlist(&tlist, filename)) != NULL)
216 qfilename = shell_unquote(filename);
217 (void) get_ifile(qfilename, ifile);
219 ifile = prev_ifile(NULL_IFILE);
223 (void) get_ifile(*argv++, ifile);
224 ifile = prev_ifile(NULL_IFILE);
228 * Set up terminal, etc.
233 * Output is not a tty.
234 * Just copy the input file(s) to output.
236 set_output(1); /* write to stdout */
238 if (edit_first() == 0)
242 } while (edit_next(1) == 0);
247 if (missing_cap && !know_dumb && !less_is_more)
248 error("WARNING: terminal is not fully functional", NULL_PARG);
254 * Select the first file to examine.
257 if (tagoption != NULL || strcmp(tags, "-") == 0)
260 * A -t option was given.
261 * Verify that no filenames were also given.
262 * Edit the file selected by the "tags" search,
263 * and search for the proper line in the file.
267 error("No filenames allowed with -t option", NULL_PARG);
271 if (edit_tagfile()) /* Edit file which contains the tag */
274 * Search for the line which contains the tag.
275 * Set up initial_scrpos so we display that line.
277 initial_scrpos.pos = tagsearch();
278 if (initial_scrpos.pos == NULL_POSITION)
280 initial_scrpos.ln = jump_sline;
287 * See if file fits on one screen to decide whether
288 * to send terminal init. But don't need this
289 * if -X (no_init) overrides this (see init()).
291 if (quit_if_one_screen)
293 if (nifile() > 1) /* If more than one file, -F cannot be used */
294 quit_if_one_screen = FALSE;
296 one_screen = get_one_screen();
303 * We displayed some messages on error output
304 * (file descriptor 2; see flush()).
305 * Before erasing the screen contents, wait for a keystroke.
307 less_printf("Press RETURN to continue ", NULL_PARG);
320 * Copy a string to a "safe" place
321 * (that is, to a buffer allocated by calloc).
329 p = (char *) ecalloc(strlen(s)+1, sizeof(char));
336 * Like calloc(), but never returns an error (NULL).
345 p = (VOID_POINTER) calloc(count, size);
348 error("Cannot allocate memory", NULL_PARG);
355 * Skip leading spaces in a string.
361 while (*s == ' ' || *s == '\t')
367 * See how many characters of two strings are identical.
368 * If uppercase is true, the first string must begin with an uppercase
369 * character; the remainder of the first string may be either case.
372 sprefix(ps, s, uppercase)
381 for ( ; *s != '\0'; s++, ps++)
386 if (len == 0 && ASCII_IS_LOWER(c))
388 if (ASCII_IS_UPPER(c))
389 c = ASCII_TO_LOWER(c);
392 if (len > 0 && ASCII_IS_UPPER(sc))
393 sc = ASCII_TO_LOWER(sc);
408 static int save_status;
411 * Put cursor at bottom left corner, clear the line,
412 * reset the terminal modes, and exit.
415 status = save_status;
417 save_status = status;
426 if (redraw_on_quit && term_init_done)
429 * The last file text displayed might have been on an
430 * alternate screen, which now (since deinit) cannot be seen.
431 * redraw_on_quit tells us to redraw it on the main screen.
433 first_time = 1; /* Don't print "skipping" or tildes */
440 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
442 * If we don't close 2, we get some garbage from
443 * 2's buffer when it flushes automatically.
444 * I cannot track this one down RB
445 * The same bug shows up if we use ^C^C to abort.
450 SetConsoleTitle(consoleTitle);