1 /* $FreeBSD$ */
2 /*
3 * Copyright (C) 1984-2023 Mark Nudelman
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Less License, as specified in the README file.
7 *
8 * For more information, see the README file.
9 */
10
11
12 /*
13 * Entry point, initialization, miscellaneous routines.
14 */
15
16 #include "less.h"
17 #if MSDOS_COMPILER==WIN32C
18 #define WIN32_LEAN_AND_MEAN
19 #include <windows.h>
20 #endif
21
22 public char * every_first_cmd = NULL;
23 public int new_file;
24 public int is_tty;
25 public IFILE curr_ifile = NULL_IFILE;
26 public IFILE old_ifile = NULL_IFILE;
27 public struct scrpos initial_scrpos;
28 public POSITION start_attnpos = NULL_POSITION;
29 public POSITION end_attnpos = NULL_POSITION;
30 public int wscroll;
31 public char * progname;
32 public int quitting;
33 public int secure;
34 public int dohelp;
35
36 #if LOGFILE
37 public int logfile = -1;
38 public int force_logfile = FALSE;
39 public char * namelogfile = NULL;
40 #endif
41
42 #if EDITOR
43 public char * editor;
44 public char * editproto;
45 #endif
46
47 #if TAGS
48 extern char * tags;
49 extern char * tagoption;
50 extern int jump_sline;
51 #endif
52
53 #ifdef WIN32
54 static char consoleTitle[256];
55 #endif
56
57 public int one_screen;
58 extern int less_is_more;
59 extern int missing_cap;
60 extern int know_dumb;
61 extern int pr_type;
62 extern int quit_if_one_screen;
63 extern int no_init;
64 extern int errmsgs;
65 extern int redraw_on_quit;
66 extern int term_init_done;
67 extern int first_time;
68
69 /*
70 * Entry point.
71 */
main(int argc,char * argv[])72 int main(int argc, char *argv[])
73 {
74 IFILE ifile;
75 char *s;
76
77 #ifdef __EMX__
78 _response(&argc, &argv);
79 _wildcard(&argc, &argv);
80 #endif
81
82 progname = *argv++;
83 argc--;
84
85 #if SECURE
86 secure = 1;
87 #else
88 secure = 0;
89 s = lgetenv("LESSSECURE");
90 if (!isnullenv(s))
91 secure = 1;
92 #endif
93
94 #ifdef WIN32
95 if (getenv("HOME") == NULL)
96 {
97 /*
98 * If there is no HOME environment variable,
99 * try the concatenation of HOMEDRIVE + HOMEPATH.
100 */
101 char *drive = getenv("HOMEDRIVE");
102 char *path = getenv("HOMEPATH");
103 if (drive != NULL && path != NULL)
104 {
105 char *env = (char *) ecalloc(strlen(drive) +
106 strlen(path) + 6, sizeof(char));
107 strcpy(env, "HOME=");
108 strcat(env, drive);
109 strcat(env, path);
110 putenv(env);
111 }
112 }
113 GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
114 #endif /* WIN32 */
115
116 /*
117 * Process command line arguments and LESS environment arguments.
118 * Command line arguments override environment arguments.
119 */
120 is_tty = isatty(1);
121 init_mark();
122 init_cmds();
123 init_poll();
124 get_term();
125 init_charset();
126 init_line();
127 init_cmdhist();
128 init_option();
129 init_search();
130
131 /*
132 * If the name of the executable program is "more",
133 * act like LESS_IS_MORE is set.
134 */
135 s = last_component(progname);
136 if (strcmp(s, "more") == 0)
137 less_is_more = 1;
138
139 init_prompt();
140
141 if (less_is_more)
142 scan_option("-fG");
143
144 s = lgetenv(less_is_more ? "MORE" : "LESS");
145 if (s != NULL)
146 scan_option(s);
147
148 #define isoptstring(s) less_is_more ? (((s)[0] == '-') && (s)[1] != '\0') : \
149 (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
150 while (argc > 0 && (isoptstring(*argv) || isoptpending()))
151 {
152 s = *argv++;
153 argc--;
154 if (strcmp(s, "--") == 0)
155 break;
156 scan_option(s);
157 }
158 #undef isoptstring
159
160 if (isoptpending())
161 {
162 /*
163 * Last command line option was a flag requiring a
164 * following string, but there was no following string.
165 */
166 nopendopt();
167 quit(QUIT_OK);
168 }
169
170 if (less_is_more)
171 no_init = TRUE;
172
173 expand_cmd_tables();
174
175 #if EDITOR
176 editor = lgetenv("VISUAL");
177 if (editor == NULL || *editor == '\0')
178 {
179 editor = lgetenv("EDITOR");
180 if (isnullenv(editor))
181 editor = EDIT_PGM;
182 }
183 editproto = lgetenv("LESSEDIT");
184 if (isnullenv(editproto))
185 editproto = "%E ?lm+%lm. %g";
186 #endif
187
188 /*
189 * Call get_ifile with all the command line filenames
190 * to "register" them with the ifile system.
191 */
192 ifile = NULL_IFILE;
193 if (dohelp)
194 ifile = get_ifile(FAKE_HELPFILE, ifile);
195 while (argc-- > 0)
196 {
197 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
198 /*
199 * Because the "shell" doesn't expand filename patterns,
200 * treat each argument as a filename pattern rather than
201 * a single filename.
202 * Expand the pattern and iterate over the expanded list.
203 */
204 struct textlist tlist;
205 char *filename;
206 char *gfilename;
207 char *qfilename;
208
209 gfilename = lglob(*argv++);
210 init_textlist(&tlist, gfilename);
211 filename = NULL;
212 while ((filename = forw_textlist(&tlist, filename)) != NULL)
213 {
214 qfilename = shell_unquote(filename);
215 (void) get_ifile(qfilename, ifile);
216 free(qfilename);
217 ifile = prev_ifile(NULL_IFILE);
218 }
219 free(gfilename);
220 #else
221 (void) get_ifile(*argv++, ifile);
222 ifile = prev_ifile(NULL_IFILE);
223 #endif
224 }
225 /*
226 * Set up terminal, etc.
227 */
228 if (!is_tty)
229 {
230 /*
231 * Output is not a tty.
232 * Just copy the input file(s) to output.
233 */
234 set_output(1); /* write to stdout */
235 SET_BINARY(1);
236 if (edit_first() == 0)
237 {
238 do {
239 cat_file();
240 } while (edit_next(1) == 0);
241 }
242 quit(QUIT_OK);
243 }
244
245 if (missing_cap && !know_dumb && !less_is_more)
246 error("WARNING: terminal is not fully functional", NULL_PARG);
247 open_getchr();
248 raw_mode(1);
249 init_signals(1);
250
251 /*
252 * Select the first file to examine.
253 */
254 #if TAGS
255 if (tagoption != NULL || strcmp(tags, "-") == 0)
256 {
257 /*
258 * A -t option was given.
259 * Verify that no filenames were also given.
260 * Edit the file selected by the "tags" search,
261 * and search for the proper line in the file.
262 */
263 if (nifile() > 0)
264 {
265 error("No filenames allowed with -t option", NULL_PARG);
266 quit(QUIT_ERROR);
267 }
268 findtag(tagoption);
269 if (edit_tagfile()) /* Edit file which contains the tag */
270 quit(QUIT_ERROR);
271 /*
272 * Search for the line which contains the tag.
273 * Set up initial_scrpos so we display that line.
274 */
275 initial_scrpos.pos = tagsearch();
276 if (initial_scrpos.pos == NULL_POSITION)
277 quit(QUIT_ERROR);
278 initial_scrpos.ln = jump_sline;
279 } else
280 #endif
281 {
282 if (edit_first())
283 quit(QUIT_ERROR);
284 /*
285 * See if file fits on one screen to decide whether
286 * to send terminal init. But don't need this
287 * if -X (no_init) overrides this (see init()).
288 */
289 if (quit_if_one_screen)
290 {
291 if (nifile() > 1) /* If more than one file, -F cannot be used */
292 quit_if_one_screen = FALSE;
293 else if (!no_init)
294 one_screen = get_one_screen();
295 }
296 }
297
298 if (errmsgs > 0)
299 {
300 /*
301 * We displayed some messages on error output
302 * (file descriptor 2; see flush()).
303 * Before erasing the screen contents, wait for a keystroke.
304 */
305 less_printf("Press RETURN to continue ", NULL_PARG);
306 get_return();
307 putchr('\n');
308 }
309 set_output(1);
310 init();
311 commands();
312 quit(QUIT_OK);
313 /*NOTREACHED*/
314 return (0);
315 }
316
317 /*
318 * Copy a string to a "safe" place
319 * (that is, to a buffer allocated by calloc).
320 */
save(constant char * s)321 public char * save(constant char *s)
322 {
323 char *p;
324
325 p = (char *) ecalloc(strlen(s)+1, sizeof(char));
326 strcpy(p, s);
327 return (p);
328 }
329
out_of_memory(void)330 public void out_of_memory(void)
331 {
332 error("Cannot allocate memory", NULL_PARG);
333 quit(QUIT_ERROR);
334 }
335
336 /*
337 * Allocate memory.
338 * Like calloc(), but never returns an error (NULL).
339 */
ecalloc(int count,unsigned int size)340 public void * ecalloc(int count, unsigned int size)
341 {
342 void * p;
343
344 p = (void *) calloc(count, size);
345 if (p == NULL)
346 out_of_memory();
347 return p;
348 }
349
350 /*
351 * Skip leading spaces in a string.
352 */
skipsp(char * s)353 public char * skipsp(char *s)
354 {
355 while (*s == ' ' || *s == '\t')
356 s++;
357 return (s);
358 }
359
360 /*
361 * See how many characters of two strings are identical.
362 * If uppercase is true, the first string must begin with an uppercase
363 * character; the remainder of the first string may be either case.
364 */
sprefix(char * ps,char * s,int uppercase)365 public int sprefix(char *ps, char *s, int uppercase)
366 {
367 int c;
368 int sc;
369 int len = 0;
370
371 for ( ; *s != '\0'; s++, ps++)
372 {
373 c = *ps;
374 if (uppercase)
375 {
376 if (len == 0 && ASCII_IS_LOWER(c))
377 return (-1);
378 if (ASCII_IS_UPPER(c))
379 c = ASCII_TO_LOWER(c);
380 }
381 sc = *s;
382 if (len > 0 && ASCII_IS_UPPER(sc))
383 sc = ASCII_TO_LOWER(sc);
384 if (c != sc)
385 break;
386 len++;
387 }
388 return (len);
389 }
390
391 /*
392 * Exit the program.
393 */
quit(int status)394 public void quit(int status)
395 {
396 static int save_status;
397
398 /*
399 * Put cursor at bottom left corner, clear the line,
400 * reset the terminal modes, and exit.
401 */
402 if (status < 0)
403 status = save_status;
404 else
405 save_status = status;
406 quitting = 1;
407 check_altpipe_error();
408 if (interactive())
409 clear_bot();
410 deinit();
411 flush();
412 if (redraw_on_quit && term_init_done)
413 {
414 /*
415 * The last file text displayed might have been on an
416 * alternate screen, which now (since deinit) cannot be seen.
417 * redraw_on_quit tells us to redraw it on the main screen.
418 */
419 first_time = 1; /* Don't print "skipping" or tildes */
420 repaint();
421 flush();
422 }
423 edit((char*)NULL);
424 save_cmdhist();
425 raw_mode(0);
426 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
427 /*
428 * If we don't close 2, we get some garbage from
429 * 2's buffer when it flushes automatically.
430 * I cannot track this one down RB
431 * The same bug shows up if we use ^C^C to abort.
432 */
433 close(2);
434 #endif
435 #ifdef WIN32
436 SetConsoleTitle(consoleTitle);
437 #endif
438 close_getchr();
439 exit(status);
440 }
441