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