2 * Copyright (C) 1984-2017 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
20 public char * every_first_cmd = NULL;
23 public IFILE curr_ifile = NULL_IFILE;
24 public IFILE old_ifile = NULL_IFILE;
25 public struct scrpos initial_scrpos;
26 public int any_display = FALSE;
27 public POSITION start_attnpos = NULL_POSITION;
28 public POSITION end_attnpos = NULL_POSITION;
30 public char * progname;
36 public int logfile = -1;
37 public int force_logfile = FALSE;
38 public char * namelogfile = NULL;
43 public char * editproto;
48 extern char * tagoption;
49 extern int jump_sline;
53 static char consoleTitle[256];
56 public int line_count;
57 extern int less_is_more;
58 extern int missing_cap;
61 extern int quit_if_one_screen;
76 _response(&argc, &argv);
77 _wildcard(&argc, &argv);
84 s = lgetenv("LESSSECURE");
85 if (s != NULL && *s != '\0')
89 if (getenv("HOME") == NULL)
92 * If there is no HOME environment variable,
93 * try the concatenation of HOMEDRIVE + HOMEPATH.
95 char *drive = getenv("HOMEDRIVE");
96 char *path = getenv("HOMEPATH");
97 if (drive != NULL && path != NULL)
99 char *env = (char *) ecalloc(strlen(drive) +
100 strlen(path) + 6, sizeof(char));
101 strcpy(env, "HOME=");
107 GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
111 * Process command line arguments and LESS environment arguments.
112 * Command line arguments override environment arguments.
125 * If the name of the executable program is "more",
126 * act like LESS_IS_MORE is set.
128 for (s = progname + strlen(progname); s > progname; s--)
130 if (s[-1] == PATHNAME_SEP[0])
133 if (strcmp(s, "more") == 0)
138 s = lgetenv(less_is_more ? "MORE" : "LESS");
140 scan_option(save(s));
142 #define isoptstring(s) (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
143 while (argc > 0 && (isoptstring(*argv) || isoptpending()))
147 if (strcmp(s, "--") == 0)
156 * Last command line option was a flag requiring a
157 * following string, but there was no following string.
164 editor = lgetenv("VISUAL");
165 if (editor == NULL || *editor == '\0')
167 editor = lgetenv("EDITOR");
168 if (editor == NULL || *editor == '\0')
171 editproto = lgetenv("LESSEDIT");
172 if (editproto == NULL || *editproto == '\0')
173 editproto = "%E ?lm+%lm. %f";
177 * Call get_ifile with all the command line filenames
178 * to "register" them with the ifile system.
182 ifile = get_ifile(FAKE_HELPFILE, ifile);
185 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
187 * Because the "shell" doesn't expand filename patterns,
188 * treat each argument as a filename pattern rather than
190 * Expand the pattern and iterate over the expanded list.
192 struct textlist tlist;
197 gfilename = lglob(*argv++);
198 init_textlist(&tlist, gfilename);
200 while ((filename = forw_textlist(&tlist, filename)) != NULL)
202 qfilename = shell_unquote(filename);
203 (void) get_ifile(qfilename, ifile);
205 ifile = prev_ifile(NULL_IFILE);
209 (void) get_ifile(*argv++, ifile);
210 ifile = prev_ifile(NULL_IFILE);
214 * Set up terminal, etc.
219 * Output is not a tty.
220 * Just copy the input file(s) to output.
225 if (edit_stdin() == 0)
227 } else if (edit_first() == 0)
231 } while (edit_next(1) == 0);
236 if (missing_cap && !know_dumb)
237 error("WARNING: terminal is not fully functional", NULL_PARG);
244 * Select the first file to examine.
247 if (tagoption != NULL || strcmp(tags, "-") == 0)
250 * A -t option was given.
251 * Verify that no filenames were also given.
252 * Edit the file selected by the "tags" search,
253 * and search for the proper line in the file.
257 error("No filenames allowed with -t option", NULL_PARG);
261 if (edit_tagfile()) /* Edit file which contains the tag */
264 * Search for the line which contains the tag.
265 * Set up initial_scrpos so we display that line.
267 initial_scrpos.pos = tagsearch();
268 if (initial_scrpos.pos == NULL_POSITION)
270 initial_scrpos.ln = jump_sline;
275 if (edit_stdin()) /* Edit standard input */
277 if (quit_if_one_screen)
278 line_count = get_line_count();
281 if (edit_first()) /* Edit first valid file in cmd line */
283 if (quit_if_one_screen)
286 line_count = get_line_count();
287 else /* If more than one file, -F can not be used */
288 quit_if_one_screen = FALSE;
300 * Copy a string to a "safe" place
301 * (that is, to a buffer allocated by calloc).
309 p = (char *) ecalloc(strlen(s)+1, sizeof(char));
316 * Like calloc(), but never returns an error (NULL).
325 p = (VOID_POINTER) calloc(count, size);
328 error("Cannot allocate memory", NULL_PARG);
335 * Skip leading spaces in a string.
341 while (*s == ' ' || *s == '\t')
347 * See how many characters of two strings are identical.
348 * If uppercase is true, the first string must begin with an uppercase
349 * character; the remainder of the first string may be either case.
352 sprefix(ps, s, uppercase)
361 for ( ; *s != '\0'; s++, ps++)
366 if (len == 0 && ASCII_IS_LOWER(c))
368 if (ASCII_IS_UPPER(c))
369 c = ASCII_TO_LOWER(c);
372 if (len > 0 && ASCII_IS_UPPER(sc))
373 sc = ASCII_TO_LOWER(sc);
388 static int save_status;
391 * Put cursor at bottom left corner, clear the line,
392 * reset the terminal modes, and exit.
395 status = save_status;
397 save_status = status;
401 if (any_display && is_tty)
406 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
408 * If we don't close 2, we get some garbage from
409 * 2's buffer when it flushes automatically.
410 * I cannot track this one down RB
411 * The same bug shows up if we use ^C^C to abort.
416 SetConsoleTitle(consoleTitle);