3 * Copyright (C) 1984-2017 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
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 line_count;
58 extern int less_is_more;
59 extern int missing_cap;
63 extern int quit_if_one_screen;
76 extern char *__progname;
79 _response(&argc, &argv);
80 _wildcard(&argc, &argv);
87 s = lgetenv("LESSSECURE");
88 if (s != NULL && *s != '\0')
92 if (getenv("HOME") == NULL)
95 * If there is no HOME environment variable,
96 * try the concatenation of HOMEDRIVE + HOMEPATH.
98 char *drive = getenv("HOMEDRIVE");
99 char *path = getenv("HOMEPATH");
100 if (drive != NULL && path != NULL)
102 char *env = (char *) ecalloc(strlen(drive) +
103 strlen(path) + 6, sizeof(char));
104 strcpy(env, "HOME=");
110 GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
114 * Process command line arguments and LESS environment arguments.
115 * Command line arguments override environment arguments.
127 * If the name of the executable program is "more",
128 * act like LESS_IS_MORE is set.
130 for (s = progname + strlen(progname); s > progname; s--)
132 if (s[-1] == PATHNAME_SEP[0])
135 if (strcmp(s, "more") == 0)
143 s = lgetenv(less_is_more ? "MORE" : "LESS");
145 scan_option(save(s));
147 #define isoptstring(s) less_is_more ? (((s)[0] == '-') && (s)[1] != '\0') : \
148 (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
149 while (argc > 0 && (isoptstring(*argv) || isoptpending()))
153 if (strcmp(s, "--") == 0)
162 * Last command line option was a flag requiring a
163 * following string, but there was no following string.
173 editor = lgetenv("VISUAL");
174 if (editor == NULL || *editor == '\0')
176 editor = lgetenv("EDITOR");
177 if (editor == NULL || *editor == '\0')
180 editproto = lgetenv("LESSEDIT");
181 if (editproto == NULL || *editproto == '\0')
182 editproto = "%E ?lm+%lm. %f";
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);
195 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
197 * Because the "shell" doesn't expand filename patterns,
198 * treat each argument as a filename pattern rather than
200 * Expand the pattern and iterate over the expanded list.
202 struct textlist tlist;
205 gfilename = lglob(*argv++);
206 init_textlist(&tlist, gfilename);
208 while ((filename = forw_textlist(&tlist, filename)) != NULL)
210 (void) get_ifile(filename, ifile);
211 ifile = prev_ifile(NULL_IFILE);
215 filename = shell_quote(*argv);
216 if (filename == NULL)
219 (void) get_ifile(filename, ifile);
220 ifile = prev_ifile(NULL_IFILE);
225 * Set up terminal, etc.
230 * Output is not a tty.
231 * Just copy the input file(s) to output.
236 if (edit_stdin() == 0)
238 } else 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);
255 * Select the first file to examine.
258 if (tagoption != NULL || strcmp(tags, "-") == 0)
261 * A -t option was given.
262 * Verify that no filenames were also given.
263 * Edit the file selected by the "tags" search,
264 * and search for the proper line in the file.
268 error("No filenames allowed with -t option", NULL_PARG);
272 if (edit_tagfile()) /* Edit file which contains the tag */
275 * Search for the line which contains the tag.
276 * Set up initial_scrpos so we display that line.
278 initial_scrpos.pos = tagsearch();
279 if (initial_scrpos.pos == NULL_POSITION)
281 initial_scrpos.ln = jump_sline;
286 if (edit_stdin()) /* Edit standard input */
288 if (quit_if_one_screen)
289 line_count = get_line_count();
292 if (edit_first()) /* Edit first valid file in cmd line */
295 * In case that we have only one file and -F, have to get a line
296 * count fot init(). If the line count is less then a height of a term,
297 * the content of the file is printed out and then less quits. Otherwise
300 if (quit_if_one_screen)
303 line_count = get_line_count();
304 else /* In case more than one file, -F can not be used */
305 quit_if_one_screen = FALSE;
317 * Copy a string to a "safe" place
318 * (that is, to a buffer allocated by calloc).
326 p = (char *) ecalloc(strlen(s)+1, sizeof(char));
333 * Like calloc(), but never returns an error (NULL).
342 p = (VOID_POINTER) calloc(count, size);
345 error("Cannot allocate memory", NULL_PARG);
352 * Skip leading spaces in a string.
358 while (*s == ' ' || *s == '\t')
364 * See how many characters of two strings are identical.
365 * If uppercase is true, the first string must begin with an uppercase
366 * character; the remainder of the first string may be either case.
369 sprefix(ps, s, uppercase)
378 for ( ; *s != '\0'; s++, ps++)
383 if (len == 0 && ASCII_IS_LOWER(c))
385 if (ASCII_IS_UPPER(c))
386 c = ASCII_TO_LOWER(c);
389 if (len > 0 && ASCII_IS_UPPER(sc))
390 sc = ASCII_TO_LOWER(sc);
405 static int save_status;
408 * Put cursor at bottom left corner, clear the line,
409 * reset the terminal modes, and exit.
412 status = save_status;
414 save_status = status;
418 if (any_display && is_tty)
423 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
425 * If we don't close 2, we get some garbage from
426 * 2's buffer when it flushes automatically.
427 * I cannot track this one down RB
428 * The same bug shows up if we use ^C^C to abort.
433 SetConsoleTitle(consoleTitle);