2 * Copyright (C) 1984-2015 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];
54 static int savedConsoleCodePage;
57 extern int less_is_more;
58 extern int missing_cap;
75 _response(&argc, &argv);
76 _wildcard(&argc, &argv);
83 s = lgetenv("LESSSECURE");
84 if (s != NULL && *s != '\0')
88 if (getenv("HOME") == NULL)
91 * If there is no HOME environment variable,
92 * try the concatenation of HOMEDRIVE + HOMEPATH.
94 char *drive = getenv("HOMEDRIVE");
95 char *path = getenv("HOMEPATH");
96 if (drive != NULL && path != NULL)
98 char *env = (char *) ecalloc(strlen(drive) +
99 strlen(path) + 6, sizeof(char));
100 strcpy(env, "HOME=");
106 GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
107 savedConsoleCodePage = GetConsoleOutputCP();
111 * Process command line arguments and LESS environment arguments.
112 * Command line arguments override environment arguments.
124 * If the name of the executable program is "more",
125 * act like LESS_IS_MORE is set.
127 for (s = progname + strlen(progname); s > progname; s--)
129 if (s[-1] == PATHNAME_SEP[0])
132 if (strcmp(s, "more") == 0)
137 s = lgetenv(less_is_more ? "MORE" : "LESS");
139 scan_option(save(s));
141 #define isoptstring(s) (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
142 while (argc > 0 && (isoptstring(*argv) || isoptpending()))
146 if (strcmp(s, "--") == 0)
155 * Last command line option was a flag requiring a
156 * following string, but there was no following string.
163 editor = lgetenv("VISUAL");
164 if (editor == NULL || *editor == '\0')
166 editor = lgetenv("EDITOR");
167 if (editor == NULL || *editor == '\0')
170 editproto = lgetenv("LESSEDIT");
171 if (editproto == NULL || *editproto == '\0')
172 editproto = "%E ?lm+%lm. %f";
176 * Call get_ifile with all the command line filenames
177 * to "register" them with the ifile system.
181 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;
195 gfilename = lglob(*argv++);
196 init_textlist(&tlist, gfilename);
198 while ((filename = forw_textlist(&tlist, filename)) != NULL)
200 (void) get_ifile(filename, ifile);
201 ifile = prev_ifile(NULL_IFILE);
205 filename = shell_quote(*argv);
206 if (filename == NULL)
209 (void) get_ifile(filename, ifile);
210 ifile = prev_ifile(NULL_IFILE);
215 * Set up terminal, etc.
220 * Output is not a tty.
221 * Just copy the input file(s) to output.
226 if (edit_stdin() == 0)
228 } else if (edit_first() == 0)
232 } while (edit_next(1) == 0);
237 if (missing_cap && !know_dumb)
238 error("WARNING: terminal is not fully functional", NULL_PARG);
245 * Select the first file to examine.
248 if (tagoption != NULL || strcmp(tags, "-") == 0)
251 * A -t option was given.
252 * Verify that no filenames were also given.
253 * Edit the file selected by the "tags" search,
254 * and search for the proper line in the file.
258 error("No filenames allowed with -t option", NULL_PARG);
262 if (edit_tagfile()) /* Edit file which contains the tag */
265 * Search for the line which contains the tag.
266 * Set up initial_scrpos so we display that line.
268 initial_scrpos.pos = tagsearch();
269 if (initial_scrpos.pos == NULL_POSITION)
271 initial_scrpos.ln = jump_sline;
276 if (edit_stdin()) /* Edit standard input */
280 if (edit_first()) /* Edit first valid file in cmd line */
292 * Copy a string to a "safe" place
293 * (that is, to a buffer allocated by calloc).
301 p = (char *) ecalloc(strlen(s)+1, sizeof(char));
308 * Like calloc(), but never returns an error (NULL).
315 register VOID_POINTER p;
317 p = (VOID_POINTER) calloc(count, size);
320 error("Cannot allocate memory", NULL_PARG);
327 * Skip leading spaces in a string.
333 while (*s == ' ' || *s == '\t')
339 * See how many characters of two strings are identical.
340 * If uppercase is true, the first string must begin with an uppercase
341 * character; the remainder of the first string may be either case.
344 sprefix(ps, s, uppercase)
351 register int len = 0;
353 for ( ; *s != '\0'; s++, ps++)
358 if (len == 0 && ASCII_IS_LOWER(c))
360 if (ASCII_IS_UPPER(c))
361 c = ASCII_TO_LOWER(c);
364 if (len > 0 && ASCII_IS_UPPER(sc))
365 sc = ASCII_TO_LOWER(sc);
380 static int save_status;
383 * Put cursor at bottom left corner, clear the line,
384 * reset the terminal modes, and exit.
387 status = save_status;
389 save_status = status;
393 if (any_display && is_tty)
398 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
400 * If we don't close 2, we get some garbage from
401 * 2's buffer when it flushes automatically.
402 * I cannot track this one down RB
403 * The same bug shows up if we use ^C^C to abort.
408 SetConsoleTitle(consoleTitle);
409 SetConsoleOutputCP(savedConsoleCodePage);