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