3 * Copyright (C) 1984-2012 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;
60 extern int quit_if_one_screen;
75 extern char *__progname;
78 _response(&argc, &argv);
79 _wildcard(&argc, &argv);
86 s = lgetenv("LESSSECURE");
87 if (s != NULL && *s != '\0')
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.
126 * If the name of the executable program is "more",
127 * act like LESS_IS_MORE is set.
129 for (s = progname + strlen(progname); s > progname; s--)
131 if (s[-1] == PATHNAME_SEP[0])
134 if (strcmp(s, "more") == 0)
142 s = lgetenv(less_is_more ? "MORE" : "LESS");
144 scan_option(save(s));
146 #define isoptstring(s) less_is_more ? (((s)[0] == '-') && (s)[1] != '\0') : \
147 (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
148 while (argc > 0 && (isoptstring(*argv) || isoptpending()))
152 if (strcmp(s, "--") == 0)
161 * Last command line option was a flag requiring a
162 * following string, but there was no following string.
170 if (less_is_more && get_quit_at_eof())
171 quit_if_one_screen = TRUE;
174 editor = lgetenv("VISUAL");
175 if (editor == NULL || *editor == '\0')
177 editor = lgetenv("EDITOR");
178 if (editor == NULL || *editor == '\0')
181 editproto = lgetenv("LESSEDIT");
182 if (editproto == NULL || *editproto == '\0')
183 editproto = "%E ?lm+%lm. %f";
187 * Call get_ifile with all the command line filenames
188 * to "register" them with the ifile system.
192 ifile = get_ifile(FAKE_HELPFILE, ifile);
196 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
198 * Because the "shell" doesn't expand filename patterns,
199 * treat each argument as a filename pattern rather than
201 * Expand the pattern and iterate over the expanded list.
203 struct textlist tlist;
206 gfilename = lglob(*argv++);
207 init_textlist(&tlist, gfilename);
209 while ((filename = forw_textlist(&tlist, filename)) != NULL)
211 (void) get_ifile(filename, ifile);
212 ifile = prev_ifile(NULL_IFILE);
216 filename = shell_quote(*argv);
217 if (filename == NULL)
220 (void) get_ifile(filename, ifile);
221 ifile = prev_ifile(NULL_IFILE);
226 * Set up terminal, etc.
231 * Output is not a tty.
232 * Just copy the input file(s) to output.
237 if (edit_stdin() == 0)
239 } else if (edit_first() == 0)
243 } while (edit_next(1) == 0);
248 if (missing_cap && !know_dumb && !less_is_more)
249 error("WARNING: terminal is not fully functional", NULL_PARG);
256 * Select the first file to examine.
259 if (tagoption != NULL || strcmp(tags, "-") == 0)
262 * A -t option was given.
263 * Verify that no filenames were also given.
264 * Edit the file selected by the "tags" search,
265 * and search for the proper line in the file.
269 error("No filenames allowed with -t option", NULL_PARG);
273 if (edit_tagfile()) /* Edit file which contains the tag */
276 * Search for the line which contains the tag.
277 * Set up initial_scrpos so we display that line.
279 initial_scrpos.pos = tagsearch();
280 if (initial_scrpos.pos == NULL_POSITION)
282 initial_scrpos.ln = jump_sline;
287 if (edit_stdin()) /* Edit standard input */
291 if (edit_first()) /* Edit first valid file in cmd line */
303 * Copy a string to a "safe" place
304 * (that is, to a buffer allocated by calloc).
312 p = (char *) ecalloc(strlen(s)+1, sizeof(char));
319 * Like calloc(), but never returns an error (NULL).
326 register VOID_POINTER p;
328 p = (VOID_POINTER) calloc(count, size);
331 error("Cannot allocate memory", NULL_PARG);
338 * Skip leading spaces in a string.
344 while (*s == ' ' || *s == '\t')
350 * See how many characters of two strings are identical.
351 * If uppercase is true, the first string must begin with an uppercase
352 * character; the remainder of the first string may be either case.
355 sprefix(ps, s, uppercase)
362 register int len = 0;
364 for ( ; *s != '\0'; s++, ps++)
369 if (len == 0 && ASCII_IS_LOWER(c))
371 if (ASCII_IS_UPPER(c))
372 c = ASCII_TO_LOWER(c);
375 if (len > 0 && ASCII_IS_UPPER(sc))
376 sc = ASCII_TO_LOWER(sc);
391 static int save_status;
394 * Put cursor at bottom left corner, clear the line,
395 * reset the terminal modes, and exit.
398 status = save_status;
400 save_status = status;
404 if (any_display && is_tty)
409 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
411 * If we don't close 2, we get some garbage from
412 * 2's buffer when it flushes automatically.
413 * I cannot track this one down RB
414 * The same bug shows up if we use ^C^C to abort.
419 SetConsoleTitle(consoleTitle);