2 * Copyright (C) 1984-2004 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 about less, or for information on how to
8 * contact the author, 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 missing_cap;
73 _response(&argc, &argv);
74 _wildcard(&argc, &argv);
81 s = lgetenv("LESSSECURE");
82 if (s != NULL && *s != '\0')
86 if (getenv("HOME") == NULL)
89 * If there is no HOME environment variable,
90 * try the concatenation of HOMEDRIVE + HOMEPATH.
92 char *drive = getenv("HOMEDRIVE");
93 char *path = getenv("HOMEPATH");
94 if (drive != NULL && path != NULL)
96 char *env = (char *) ecalloc(strlen(drive) +
97 strlen(path) + 6, sizeof(char));
104 GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
108 * Process command line arguments and LESS environment arguments.
109 * Command line arguments override environment arguments.
121 scan_option(save(s));
123 #define isoptstring(s) (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
124 while (argc > 0 && (isoptstring(*argv) || isoptpending()))
128 if (strcmp(s, "--") == 0)
137 * Last command line option was a flag requiring a
138 * following string, but there was no following string.
145 editor = lgetenv("VISUAL");
146 if (editor == NULL || *editor == '\0')
148 editor = lgetenv("EDITOR");
149 if (editor == NULL || *editor == '\0')
152 editproto = lgetenv("LESSEDIT");
153 if (editproto == NULL || *editproto == '\0')
154 editproto = "%E ?lm+%lm. %f";
158 * Call get_ifile with all the command line filenames
159 * to "register" them with the ifile system.
163 ifile = get_ifile(FAKE_HELPFILE, ifile);
167 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
169 * Because the "shell" doesn't expand filename patterns,
170 * treat each argument as a filename pattern rather than
172 * Expand the pattern and iterate over the expanded list.
174 struct textlist tlist;
177 gfilename = lglob(*argv++);
178 init_textlist(&tlist, gfilename);
180 while ((filename = forw_textlist(&tlist, filename)) != NULL)
182 (void) get_ifile(filename, ifile);
183 ifile = prev_ifile(NULL_IFILE);
187 filename = shell_quote(*argv);
188 if (filename == NULL)
191 (void) get_ifile(filename, ifile);
192 ifile = prev_ifile(NULL_IFILE);
196 * Set up terminal, etc.
201 * Output is not a tty.
202 * Just copy the input file(s) to output.
207 if (edit_stdin() == 0)
209 } else if (edit_first() == 0)
213 } while (edit_next(1) == 0);
218 if (missing_cap && !know_dumb)
219 error("WARNING: terminal is not fully functional", NULL_PARG);
226 * Select the first file to examine.
229 if (tagoption != NULL || strcmp(tags, "-") == 0)
232 * A -t option was given.
233 * Verify that no filenames were also given.
234 * Edit the file selected by the "tags" search,
235 * and search for the proper line in the file.
239 error("No filenames allowed with -t option", NULL_PARG);
243 if (edit_tagfile()) /* Edit file which contains the tag */
246 * Search for the line which contains the tag.
247 * Set up initial_scrpos so we display that line.
249 initial_scrpos.pos = tagsearch();
250 if (initial_scrpos.pos == NULL_POSITION)
252 initial_scrpos.ln = jump_sline;
257 if (edit_stdin()) /* Edit standard input */
261 if (edit_first()) /* Edit first valid file in cmd line */
273 * Copy a string to a "safe" place
274 * (that is, to a buffer allocated by calloc).
282 p = (char *) ecalloc(strlen(s)+1, sizeof(char));
289 * Like calloc(), but never returns an error (NULL).
296 register VOID_POINTER p;
298 p = (VOID_POINTER) calloc(count, size);
301 error("Cannot allocate memory", NULL_PARG);
308 * Skip leading spaces in a string.
314 while (*s == ' ' || *s == '\t')
320 * See how many characters of two strings are identical.
321 * If uppercase is true, the first string must begin with an uppercase
322 * character; the remainder of the first string may be either case.
325 sprefix(ps, s, uppercase)
332 register int len = 0;
334 for ( ; *s != '\0'; s++, ps++)
339 if (len == 0 && ASCII_IS_LOWER(c))
341 if (ASCII_IS_UPPER(c))
342 c = ASCII_TO_LOWER(c);
345 if (len > 0 && ASCII_IS_UPPER(sc))
346 sc = ASCII_TO_LOWER(sc);
361 static int save_status;
364 * Put cursor at bottom left corner, clear the line,
365 * reset the terminal modes, and exit.
368 status = save_status;
370 save_status = status;
374 if (any_display && is_tty)
379 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
381 * If we don't close 2, we get some garbage from
382 * 2's buffer when it flushes automatically.
383 * I cannot track this one down RB
384 * The same bug shows up if we use ^C^C to abort.
389 SetConsoleTitle(consoleTitle);