2 * Copyright (C) 1984-2019 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information, see the README file.
12 * Entry point, initialization, miscellaneous routines.
16 #if MSDOS_COMPILER==WIN32C
17 #define WIN32_LEAN_AND_MEAN
21 public char * every_first_cmd = NULL;
24 public IFILE curr_ifile = NULL_IFILE;
25 public IFILE old_ifile = NULL_IFILE;
26 public struct scrpos initial_scrpos;
27 public int any_display = FALSE;
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;
78 _response(&argc, &argv);
79 _wildcard(&argc, &argv);
86 s = lgetenv("LESSSECURE");
91 if (getenv("HOME") == NULL)
94 * If there is no HOME environment variable,
95 * try the concatenation of HOMEDRIVE + HOMEPATH.
97 char *drive = getenv("HOMEDRIVE");
98 char *path = getenv("HOMEPATH");
99 if (drive != NULL && path != NULL)
101 char *env = (char *) ecalloc(strlen(drive) +
102 strlen(path) + 6, sizeof(char));
103 strcpy(env, "HOME=");
109 GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
113 * Process command line arguments and LESS environment arguments.
114 * Command line arguments override environment arguments.
128 * If the name of the executable program is "more",
129 * act like LESS_IS_MORE is set.
131 s = last_component(progname);
132 if (strcmp(s, "more") == 0)
137 s = lgetenv(less_is_more ? "MORE" : "LESS");
139 scan_option(save(s));
141 #define isoptstring(s) (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
142 while (argc > 0 && (isoptstring(*argv) || isoptpending()))
146 if (strcmp(s, "--") == 0)
155 * Last command line option was a flag requiring a
156 * following string, but there was no following string.
163 editor = lgetenv("VISUAL");
164 if (editor == NULL || *editor == '\0')
166 editor = lgetenv("EDITOR");
167 if (isnullenv(editor))
170 editproto = lgetenv("LESSEDIT");
171 if (isnullenv(editproto))
172 editproto = "%E ?lm+%lm. %g";
176 * Call get_ifile with all the command line filenames
177 * to "register" them with the ifile system.
181 ifile = get_ifile(FAKE_HELPFILE, ifile);
184 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
186 * Because the "shell" doesn't expand filename patterns,
187 * treat each argument as a filename pattern rather than
189 * Expand the pattern and iterate over the expanded list.
191 struct textlist tlist;
196 gfilename = lglob(*argv++);
197 init_textlist(&tlist, gfilename);
199 while ((filename = forw_textlist(&tlist, filename)) != NULL)
201 qfilename = shell_unquote(filename);
202 (void) get_ifile(qfilename, ifile);
204 ifile = prev_ifile(NULL_IFILE);
208 (void) get_ifile(*argv++, ifile);
209 ifile = prev_ifile(NULL_IFILE);
213 * Set up terminal, etc.
218 * Output is not a tty.
219 * Just copy the input file(s) to output.
222 if (edit_first() == 0)
226 } while (edit_next(1) == 0);
231 if (missing_cap && !know_dumb)
232 error("WARNING: terminal is not fully functional", NULL_PARG);
238 * Select the first file to examine.
241 if (tagoption != NULL || strcmp(tags, "-") == 0)
244 * A -t option was given.
245 * Verify that no filenames were also given.
246 * Edit the file selected by the "tags" search,
247 * and search for the proper line in the file.
251 error("No filenames allowed with -t option", NULL_PARG);
255 if (edit_tagfile()) /* Edit file which contains the tag */
258 * Search for the line which contains the tag.
259 * Set up initial_scrpos so we display that line.
261 initial_scrpos.pos = tagsearch();
262 if (initial_scrpos.pos == NULL_POSITION)
264 initial_scrpos.ln = jump_sline;
271 * See if file fits on one screen to decide whether
272 * to send terminal init. But don't need this
273 * if -X (no_init) overrides this (see init()).
275 if (quit_if_one_screen)
277 if (nifile() > 1) /* If more than one file, -F cannot be used */
278 quit_if_one_screen = FALSE;
280 one_screen = get_one_screen();
292 * Copy a string to a "safe" place
293 * (that is, to a buffer allocated by calloc).
301 p = (char *) ecalloc(strlen(s)+1, sizeof(char));
308 * Like calloc(), but never returns an error (NULL).
317 p = (VOID_POINTER) calloc(count, size);
320 error("Cannot allocate memory", NULL_PARG);
327 * Skip leading spaces in a string.
333 while (*s == ' ' || *s == '\t')
339 * See how many characters of two strings are identical.
340 * If uppercase is true, the first string must begin with an uppercase
341 * character; the remainder of the first string may be either case.
344 sprefix(ps, s, uppercase)
353 for ( ; *s != '\0'; s++, ps++)
358 if (len == 0 && ASCII_IS_LOWER(c))
360 if (ASCII_IS_UPPER(c))
361 c = ASCII_TO_LOWER(c);
364 if (len > 0 && ASCII_IS_UPPER(sc))
365 sc = ASCII_TO_LOWER(sc);
380 static int save_status;
383 * Put cursor at bottom left corner, clear the line,
384 * reset the terminal modes, and exit.
387 status = save_status;
389 save_status = status;
393 if (any_display && is_tty)
398 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
400 * If we don't close 2, we get some garbage from
401 * 2's buffer when it flushes automatically.
402 * I cannot track this one down RB
403 * The same bug shows up if we use ^C^C to abort.
408 SetConsoleTitle(consoleTitle);