2 * Copyright (C) 1984-2002 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.
120 scan_option(save(s
));
122 #define isoptstring(s) (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
123 while (argc
> 0 && (isoptstring(*argv
) || isoptpending()))
127 if (strcmp(s
, "--") == 0)
136 * Last command line option was a flag requiring a
137 * following string, but there was no following string.
144 editor
= lgetenv("VISUAL");
145 if (editor
== NULL
|| *editor
== '\0')
147 editor
= lgetenv("EDITOR");
148 if (editor
== NULL
|| *editor
== '\0')
151 editproto
= lgetenv("LESSEDIT");
152 if (editproto
== NULL
|| *editproto
== '\0')
153 editproto
= "%E ?lm+%lm. %f";
157 * Call get_ifile with all the command line filenames
158 * to "register" them with the ifile system.
162 ifile
= get_ifile(FAKE_HELPFILE
, ifile
);
166 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
168 * Because the "shell" doesn't expand filename patterns,
169 * treat each argument as a filename pattern rather than
171 * Expand the pattern and iterate over the expanded list.
173 struct textlist tlist
;
176 gfilename
= lglob(*argv
++);
177 init_textlist(&tlist
, gfilename
);
179 while ((filename
= forw_textlist(&tlist
, filename
)) != NULL
)
181 (void) get_ifile(filename
, ifile
);
182 ifile
= prev_ifile(NULL_IFILE
);
186 filename
= shell_quote(*argv
);
187 if (filename
== NULL
)
190 (void) get_ifile(filename
, ifile
);
191 ifile
= prev_ifile(NULL_IFILE
);
195 * Set up terminal, etc.
200 * Output is not a tty.
201 * Just copy the input file(s) to output.
206 if (edit_stdin() == 0)
208 } else if (edit_first() == 0)
212 } while (edit_next(1) == 0);
217 if (missing_cap
&& !know_dumb
)
218 error("WARNING: terminal is not fully functional", NULL_PARG
);
225 * Select the first file to examine.
228 if (tagoption
!= NULL
|| strcmp(tags
, "-") == 0)
231 * A -t option was given.
232 * Verify that no filenames were also given.
233 * Edit the file selected by the "tags" search,
234 * and search for the proper line in the file.
238 error("No filenames allowed with -t option", NULL_PARG
);
242 if (edit_tagfile()) /* Edit file which contains the tag */
245 * Search for the line which contains the tag.
246 * Set up initial_scrpos so we display that line.
248 initial_scrpos
.pos
= tagsearch();
249 if (initial_scrpos
.pos
== NULL_POSITION
)
251 initial_scrpos
.ln
= jump_sline
;
256 if (edit_stdin()) /* Edit standard input */
260 if (edit_first()) /* Edit first valid file in cmd line */
272 * Copy a string to a "safe" place
273 * (that is, to a buffer allocated by calloc).
281 p
= (char *) ecalloc(strlen(s
)+1, sizeof(char));
288 * Like calloc(), but never returns an error (NULL).
295 register VOID_POINTER p
;
297 p
= (VOID_POINTER
) calloc(count
, size
);
300 error("Cannot allocate memory", NULL_PARG
);
307 * Skip leading spaces in a string.
313 while (*s
== ' ' || *s
== '\t')
319 * See how many characters of two strings are identical.
320 * If uppercase is true, the first string must begin with an uppercase
321 * character; the remainder of the first string may be either case.
324 sprefix(ps
, s
, uppercase
)
331 register int len
= 0;
333 for ( ; *s
!= '\0'; s
++, ps
++)
338 if (len
== 0 && SIMPLE_IS_LOWER(c
))
340 if (SIMPLE_IS_UPPER(c
))
341 c
= SIMPLE_TO_LOWER(c
);
344 if (len
> 0 && SIMPLE_IS_UPPER(sc
))
345 sc
= SIMPLE_TO_LOWER(sc
);
360 static int save_status
;
363 * Put cursor at bottom left corner, clear the line,
364 * reset the terminal modes, and exit.
367 status
= save_status
;
369 save_status
= status
;
372 if (any_display
&& is_tty
)
377 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
379 * If we don't close 2, we get some garbage from
380 * 2's buffer when it flushes automatically.
381 * I cannot track this one down RB
382 * The same bug shows up if we use ^C^C to abort.
387 SetConsoleTitle(consoleTitle
);