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];
56 extern int less_is_more
;
57 extern int missing_cap
;
74 _response(&argc
, &argv
);
75 _wildcard(&argc
, &argv
);
82 s
= lgetenv("LESSSECURE");
83 if (s
!= NULL
&& *s
!= '\0')
87 if (getenv("HOME") == NULL
)
90 * If there is no HOME environment variable,
91 * try the concatenation of HOMEDRIVE + HOMEPATH.
93 char *drive
= getenv("HOMEDRIVE");
94 char *path
= getenv("HOMEPATH");
95 if (drive
!= NULL
&& path
!= NULL
)
97 char *env
= (char *) ecalloc(strlen(drive
) +
98 strlen(path
) + 6, sizeof(char));
105 GetConsoleTitle(consoleTitle
, sizeof(consoleTitle
)/sizeof(char));
109 * Process command line arguments and LESS environment arguments.
110 * Command line arguments override environment arguments.
122 * If the name of the executable program is "more",
123 * act like LESS_IS_MORE is set.
125 for (s
= progname
+ strlen(progname
); s
> progname
; s
--)
127 if (s
[-1] == PATHNAME_SEP
[0])
130 if (strcmp(s
, "more") == 0)
135 s
= lgetenv(less_is_more
? "MORE" : "LESS");
137 scan_option(save(s
));
139 #define isoptstring(s) (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
140 while (argc
> 0 && (isoptstring(*argv
) || isoptpending()))
144 if (strcmp(s
, "--") == 0)
153 * Last command line option was a flag requiring a
154 * following string, but there was no following string.
161 editor
= lgetenv("VISUAL");
162 if (editor
== NULL
|| *editor
== '\0')
164 editor
= lgetenv("EDITOR");
165 if (editor
== NULL
|| *editor
== '\0')
168 editproto
= lgetenv("LESSEDIT");
169 if (editproto
== NULL
|| *editproto
== '\0')
170 editproto
= "%E ?lm+%lm. %f";
174 * Call get_ifile with all the command line filenames
175 * to "register" them with the ifile system.
179 ifile
= get_ifile(FAKE_HELPFILE
, ifile
);
183 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
185 * Because the "shell" doesn't expand filename patterns,
186 * treat each argument as a filename pattern rather than
188 * Expand the pattern and iterate over the expanded list.
190 struct textlist tlist
;
193 gfilename
= lglob(*argv
++);
194 init_textlist(&tlist
, gfilename
);
196 while ((filename
= forw_textlist(&tlist
, filename
)) != NULL
)
198 (void) get_ifile(filename
, ifile
);
199 ifile
= prev_ifile(NULL_IFILE
);
203 filename
= shell_quote(*argv
);
204 if (filename
== NULL
)
207 (void) get_ifile(filename
, ifile
);
208 ifile
= prev_ifile(NULL_IFILE
);
213 * Set up terminal, etc.
218 * Output is not a tty.
219 * Just copy the input file(s) to output.
224 if (edit_stdin() == 0)
226 } else if (edit_first() == 0)
230 } while (edit_next(1) == 0);
235 if (missing_cap
&& !know_dumb
)
236 error("WARNING: terminal is not fully functional", NULL_PARG
);
243 * Select the first file to examine.
246 if (tagoption
!= NULL
|| strcmp(tags
, "-") == 0)
249 * A -t option was given.
250 * Verify that no filenames were also given.
251 * Edit the file selected by the "tags" search,
252 * and search for the proper line in the file.
256 error("No filenames allowed with -t option", NULL_PARG
);
260 if (edit_tagfile()) /* Edit file which contains the tag */
263 * Search for the line which contains the tag.
264 * Set up initial_scrpos so we display that line.
266 initial_scrpos
.pos
= tagsearch();
267 if (initial_scrpos
.pos
== NULL_POSITION
)
269 initial_scrpos
.ln
= jump_sline
;
274 if (edit_stdin()) /* Edit standard input */
278 if (edit_first()) /* Edit first valid file in cmd line */
290 * Copy a string to a "safe" place
291 * (that is, to a buffer allocated by calloc).
299 p
= (char *) ecalloc(strlen(s
)+1, sizeof(char));
306 * Like calloc(), but never returns an error (NULL).
313 register VOID_POINTER p
;
315 p
= (VOID_POINTER
) calloc(count
, size
);
318 error("Cannot allocate memory", NULL_PARG
);
325 * Skip leading spaces in a string.
331 while (*s
== ' ' || *s
== '\t')
337 * See how many characters of two strings are identical.
338 * If uppercase is true, the first string must begin with an uppercase
339 * character; the remainder of the first string may be either case.
342 sprefix(ps
, s
, uppercase
)
349 register int len
= 0;
351 for ( ; *s
!= '\0'; s
++, ps
++)
356 if (len
== 0 && ASCII_IS_LOWER(c
))
358 if (ASCII_IS_UPPER(c
))
359 c
= ASCII_TO_LOWER(c
);
362 if (len
> 0 && ASCII_IS_UPPER(sc
))
363 sc
= ASCII_TO_LOWER(sc
);
378 static int save_status
;
381 * Put cursor at bottom left corner, clear the line,
382 * reset the terminal modes, and exit.
385 status
= save_status
;
387 save_status
= status
;
391 if (any_display
&& is_tty
)
396 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
398 * If we don't close 2, we get some garbage from
399 * 2's buffer when it flushes automatically.
400 * I cannot track this one down RB
401 * The same bug shows up if we use ^C^C to abort.
406 SetConsoleTitle(consoleTitle
);