2 * Copyright (C) 1984-2007 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
;
35 public int less_is_more
;
38 public int logfile
= -1;
39 public int force_logfile
= FALSE
;
40 public char * namelogfile
= NULL
;
45 public char * editproto
;
50 extern char * tagoption
;
51 extern int jump_sline
;
55 static char consoleTitle
[256];
58 extern int missing_cap
;
60 extern int quit_if_one_screen
;
76 _response(&argc
, &argv
);
77 _wildcard(&argc
, &argv
);
84 s
= lgetenv("LESSSECURE");
85 if (s
!= NULL
&& *s
!= '\0')
89 if (getenv("HOME") == NULL
)
92 * If there is no HOME environment variable,
93 * try the concatenation of HOMEDRIVE + HOMEPATH.
95 char *drive
= getenv("HOMEDRIVE");
96 char *path
= getenv("HOMEPATH");
97 if (drive
!= NULL
&& path
!= NULL
)
99 char *env
= (char *) ecalloc(strlen(drive
) +
100 strlen(path
) + 6, sizeof(char));
101 strcpy(env
, "HOME=");
107 GetConsoleTitle(consoleTitle
, sizeof(consoleTitle
)/sizeof(char));
111 * Process command line arguments and LESS environment arguments.
112 * Command line arguments override environment arguments.
123 * If the name of the executable program is "more",
124 * act like LESS_IS_MORE is set.
126 for (s
= progname
+ strlen(progname
); s
> progname
; s
--)
128 if (s
[-1] == PATHNAME_SEP
[0])
131 if (strcmp(s
, "more") == 0)
136 s
= lgetenv(less_is_more
? "MORE" : "LESS");
138 scan_option(save(s
));
140 #define isoptstring(s) (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
141 while (argc
> 0 && (isoptstring(*argv
) || isoptpending()))
145 if (strcmp(s
, "--") == 0)
154 * Last command line option was a flag requiring a
155 * following string, but there was no following string.
161 if (less_is_more
&& get_quit_at_eof())
162 quit_if_one_screen
= TRUE
;
165 editor
= lgetenv("VISUAL");
166 if (editor
== NULL
|| *editor
== '\0')
168 editor
= lgetenv("EDITOR");
169 if (editor
== NULL
|| *editor
== '\0')
172 editproto
= lgetenv("LESSEDIT");
173 if (editproto
== NULL
|| *editproto
== '\0')
174 editproto
= "%E ?lm+%lm. %f";
178 * Call get_ifile with all the command line filenames
179 * to "register" them with the ifile system.
183 ifile
= get_ifile(FAKE_HELPFILE
, ifile
);
187 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
189 * Because the "shell" doesn't expand filename patterns,
190 * treat each argument as a filename pattern rather than
192 * Expand the pattern and iterate over the expanded list.
194 struct textlist tlist
;
197 gfilename
= lglob(*argv
++);
198 init_textlist(&tlist
, gfilename
);
200 while ((filename
= forw_textlist(&tlist
, filename
)) != NULL
)
202 (void) get_ifile(filename
, ifile
);
203 ifile
= prev_ifile(NULL_IFILE
);
207 filename
= shell_quote(*argv
);
208 if (filename
== NULL
)
211 (void) get_ifile(filename
, ifile
);
212 ifile
= prev_ifile(NULL_IFILE
);
216 * Set up terminal, etc.
221 * Output is not a tty.
222 * Just copy the input file(s) to output.
227 if (edit_stdin() == 0)
229 } else if (edit_first() == 0)
233 } while (edit_next(1) == 0);
238 if (missing_cap
&& !know_dumb
)
239 error("WARNING: terminal is not fully functional", NULL_PARG
);
246 * Select the first file to examine.
249 if (tagoption
!= NULL
|| strcmp(tags
, "-") == 0)
252 * A -t option was given.
253 * Verify that no filenames were also given.
254 * Edit the file selected by the "tags" search,
255 * and search for the proper line in the file.
259 error("No filenames allowed with -t option", NULL_PARG
);
263 if (edit_tagfile()) /* Edit file which contains the tag */
266 * Search for the line which contains the tag.
267 * Set up initial_scrpos so we display that line.
269 initial_scrpos
.pos
= tagsearch();
270 if (initial_scrpos
.pos
== NULL_POSITION
)
272 initial_scrpos
.ln
= jump_sline
;
277 if (edit_stdin()) /* Edit standard input */
281 if (edit_first()) /* Edit first valid file in cmd line */
293 * Copy a string to a "safe" place
294 * (that is, to a buffer allocated by calloc).
302 p
= (char *) ecalloc(strlen(s
)+1, sizeof(char));
309 * Like calloc(), but never returns an error (NULL).
316 register VOID_POINTER p
;
318 p
= (VOID_POINTER
) calloc(count
, size
);
321 error("Cannot allocate memory", NULL_PARG
);
328 * Skip leading spaces in a string.
334 while (*s
== ' ' || *s
== '\t')
340 * See how many characters of two strings are identical.
341 * If uppercase is true, the first string must begin with an uppercase
342 * character; the remainder of the first string may be either case.
345 sprefix(ps
, s
, uppercase
)
352 register int len
= 0;
354 for ( ; *s
!= '\0'; s
++, ps
++)
359 if (len
== 0 && ASCII_IS_LOWER(c
))
361 if (ASCII_IS_UPPER(c
))
362 c
= ASCII_TO_LOWER(c
);
365 if (len
> 0 && ASCII_IS_UPPER(sc
))
366 sc
= ASCII_TO_LOWER(sc
);
381 static int save_status
;
384 * Put cursor at bottom left corner, clear the line,
385 * reset the terminal modes, and exit.
388 status
= save_status
;
390 save_status
= status
;
394 if (any_display
&& is_tty
)
399 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
401 * If we don't close 2, we get some garbage from
402 * 2's buffer when it flushes automatically.
403 * I cannot track this one down RB
404 * The same bug shows up if we use ^C^C to abort.
409 SetConsoleTitle(consoleTitle
);