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