3 * Copyright (C) 1984-2020 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.
129 * If the name of the executable program is "more",
130 * act like LESS_IS_MORE is set.
132 s = last_component(progname);
133 if (strcmp(s, "more") == 0)
141 s = lgetenv(less_is_more ? "MORE" : "LESS");
143 scan_option(save(s));
145 #define isoptstring(s) less_is_more ? (((s)[0] == '-') && (s)[1] != '\0') : \
146 (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
147 while (argc > 0 && (isoptstring(*argv) || isoptpending()))
151 if (strcmp(s, "--") == 0)
160 * Last command line option was a flag requiring a
161 * following string, but there was no following string.
173 editor = lgetenv("VISUAL");
174 if (editor == NULL || *editor == '\0')
176 editor = lgetenv("EDITOR");
177 if (isnullenv(editor))
180 editproto = lgetenv("LESSEDIT");
181 if (isnullenv(editproto))
182 editproto = "%E ?lm+%lm. %g";
186 * Call get_ifile with all the command line filenames
187 * to "register" them with the ifile system.
191 ifile = get_ifile(FAKE_HELPFILE, ifile);
194 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
196 * Because the "shell" doesn't expand filename patterns,
197 * treat each argument as a filename pattern rather than
199 * Expand the pattern and iterate over the expanded list.
201 struct textlist tlist;
206 gfilename = lglob(*argv++);
207 init_textlist(&tlist, gfilename);
209 while ((filename = forw_textlist(&tlist, filename)) != NULL)
211 qfilename = shell_unquote(filename);
212 (void) get_ifile(qfilename, ifile);
214 ifile = prev_ifile(NULL_IFILE);
218 (void) get_ifile(*argv++, ifile);
219 ifile = prev_ifile(NULL_IFILE);
223 * Set up terminal, etc.
228 * Output is not a tty.
229 * Just copy the input file(s) to output.
232 if (edit_first() == 0)
236 } while (edit_next(1) == 0);
241 if (missing_cap && !know_dumb && !less_is_more)
242 error("WARNING: terminal is not fully functional", NULL_PARG);
248 * Select the first file to examine.
251 if (tagoption != NULL || strcmp(tags, "-") == 0)
254 * A -t option was given.
255 * Verify that no filenames were also given.
256 * Edit the file selected by the "tags" search,
257 * and search for the proper line in the file.
261 error("No filenames allowed with -t option", NULL_PARG);
265 if (edit_tagfile()) /* Edit file which contains the tag */
268 * Search for the line which contains the tag.
269 * Set up initial_scrpos so we display that line.
271 initial_scrpos.pos = tagsearch();
272 if (initial_scrpos.pos == NULL_POSITION)
274 initial_scrpos.ln = jump_sline;
281 * See if file fits on one screen to decide whether
282 * to send terminal init. But don't need this
283 * if -X (no_init) overrides this (see init()).
285 if (quit_if_one_screen)
287 if (nifile() > 1) /* If more than one file, -F cannot be used */
288 quit_if_one_screen = FALSE;
290 one_screen = get_one_screen();
302 * Copy a string to a "safe" place
303 * (that is, to a buffer allocated by calloc).
311 p = (char *) ecalloc(strlen(s)+1, sizeof(char));
318 * Like calloc(), but never returns an error (NULL).
327 p = (VOID_POINTER) calloc(count, size);
330 error("Cannot allocate memory", NULL_PARG);
337 * Skip leading spaces in a string.
343 while (*s == ' ' || *s == '\t')
349 * See how many characters of two strings are identical.
350 * If uppercase is true, the first string must begin with an uppercase
351 * character; the remainder of the first string may be either case.
354 sprefix(ps, s, uppercase)
363 for ( ; *s != '\0'; s++, ps++)
368 if (len == 0 && ASCII_IS_LOWER(c))
370 if (ASCII_IS_UPPER(c))
371 c = ASCII_TO_LOWER(c);
374 if (len > 0 && ASCII_IS_UPPER(sc))
375 sc = ASCII_TO_LOWER(sc);
390 static int save_status;
393 * Put cursor at bottom left corner, clear the line,
394 * reset the terminal modes, and exit.
397 status = save_status;
399 save_status = status;
403 if (any_display && is_tty)
408 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
410 * If we don't close 2, we get some garbage from
411 * 2's buffer when it flushes automatically.
412 * I cannot track this one down RB
413 * The same bug shows up if we use ^C^C to abort.
418 SetConsoleTitle(consoleTitle);