3 * Copyright (C) 1984-2015 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 extern int less_is_more;
58 extern int missing_cap;
74 extern char *__progname;
77 _response(&argc, &argv);
78 _wildcard(&argc, &argv);
85 s = lgetenv("LESSSECURE");
86 if (s != NULL && *s != '\0')
90 if (getenv("HOME") == NULL)
93 * If there is no HOME environment variable,
94 * try the concatenation of HOMEDRIVE + HOMEPATH.
96 char *drive = getenv("HOMEDRIVE");
97 char *path = getenv("HOMEPATH");
98 if (drive != NULL && path != NULL)
100 char *env = (char *) ecalloc(strlen(drive) +
101 strlen(path) + 6, sizeof(char));
102 strcpy(env, "HOME=");
108 GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
112 * Process command line arguments and LESS environment arguments.
113 * 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)
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.
171 editor = lgetenv("VISUAL");
172 if (editor == NULL || *editor == '\0')
174 editor = lgetenv("EDITOR");
175 if (editor == NULL || *editor == '\0')
178 editproto = lgetenv("LESSEDIT");
179 if (editproto == NULL || *editproto == '\0')
180 editproto = "%E ?lm+%lm. %f";
184 * Call get_ifile with all the command line filenames
185 * to "register" them with the ifile system.
189 ifile = get_ifile(FAKE_HELPFILE, ifile);
193 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
195 * Because the "shell" doesn't expand filename patterns,
196 * treat each argument as a filename pattern rather than
198 * Expand the pattern and iterate over the expanded list.
200 struct textlist tlist;
203 gfilename = lglob(*argv++);
204 init_textlist(&tlist, gfilename);
206 while ((filename = forw_textlist(&tlist, filename)) != NULL)
208 (void) get_ifile(filename, ifile);
209 ifile = prev_ifile(NULL_IFILE);
213 filename = shell_quote(*argv);
214 if (filename == NULL)
217 (void) get_ifile(filename, ifile);
218 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.
234 if (edit_stdin() == 0)
236 } else if (edit_first() == 0)
240 } while (edit_next(1) == 0);
245 if (missing_cap && !know_dumb && !less_is_more)
246 error("WARNING: terminal is not fully functional", NULL_PARG);
253 * Select the first file to examine.
256 if (tagoption != NULL || strcmp(tags, "-") == 0)
259 * A -t option was given.
260 * Verify that no filenames were also given.
261 * Edit the file selected by the "tags" search,
262 * and search for the proper line in the file.
266 error("No filenames allowed with -t option", NULL_PARG);
270 if (edit_tagfile()) /* Edit file which contains the tag */
273 * Search for the line which contains the tag.
274 * Set up initial_scrpos so we display that line.
276 initial_scrpos.pos = tagsearch();
277 if (initial_scrpos.pos == NULL_POSITION)
279 initial_scrpos.ln = jump_sline;
284 if (edit_stdin()) /* Edit standard input */
288 if (edit_first()) /* Edit first valid file in cmd line */
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).
323 register VOID_POINTER p;
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)
359 register int len = 0;
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);