xref: /freebsd/contrib/less/main.c (revision f173c2b77e0287807852bb7ba982643f33bdf8a0)
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 	extern char *__progname;
77 
78 #ifdef __EMX__
79 	_response(&argc, &argv);
80 	_wildcard(&argc, &argv);
81 #endif
82 
83 	progname = *argv++;
84 	argc--;
85 
86 	secure = 0;
87 	s = lgetenv("LESSSECURE");
88 	if (s != NULL && *s != '\0')
89 		secure = 1;
90 
91 #ifdef WIN32
92 	if (getenv("HOME") == NULL)
93 	{
94 		/*
95 		 * If there is no HOME environment variable,
96 		 * try the concatenation of HOMEDRIVE + HOMEPATH.
97 		 */
98 		char *drive = getenv("HOMEDRIVE");
99 		char *path  = getenv("HOMEPATH");
100 		if (drive != NULL && path != NULL)
101 		{
102 			char *env = (char *) ecalloc(strlen(drive) +
103 					strlen(path) + 6, sizeof(char));
104 			strcpy(env, "HOME=");
105 			strcat(env, drive);
106 			strcat(env, path);
107 			putenv(env);
108 		}
109 	}
110 	GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
111 #endif /* WIN32 */
112 
113 	/*
114 	 * Process command line arguments and LESS environment arguments.
115 	 * Command line arguments override environment arguments.
116 	 */
117 	is_tty = isatty(1);
118 	get_term();
119 	init_cmds();
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 		char *filename;
195 #if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
196 		/*
197 		 * Because the "shell" doesn't expand filename patterns,
198 		 * treat each argument as a filename pattern rather than
199 		 * a single filename.
200 		 * Expand the pattern and iterate over the expanded list.
201 		 */
202 		struct textlist tlist;
203 		char *gfilename;
204 
205 		gfilename = lglob(*argv++);
206 		init_textlist(&tlist, gfilename);
207 		filename = NULL;
208 		while ((filename = forw_textlist(&tlist, filename)) != NULL)
209 		{
210 			(void) get_ifile(filename, ifile);
211 			ifile = prev_ifile(NULL_IFILE);
212 		}
213 		free(gfilename);
214 #else
215 		filename = shell_quote(*argv);
216 		if (filename == NULL)
217 			filename = *argv;
218 		argv++;
219 		(void) get_ifile(filename, ifile);
220 		ifile = prev_ifile(NULL_IFILE);
221 		free(filename);
222 #endif
223 	}
224 	/*
225 	 * Set up terminal, etc.
226 	 */
227 	if (!is_tty)
228 	{
229 		/*
230 		 * Output is not a tty.
231 		 * Just copy the input file(s) to output.
232 		 */
233 		SET_BINARY(1);
234 		if (nifile() == 0)
235 		{
236 			if (edit_stdin() == 0)
237 				cat_file();
238 		} else if (edit_first() == 0)
239 		{
240 			do {
241 				cat_file();
242 			} while (edit_next(1) == 0);
243 		}
244 		quit(QUIT_OK);
245 	}
246 
247 	if (missing_cap && !know_dumb && !less_is_more)
248 		error("WARNING: terminal is not fully functional", NULL_PARG);
249 	init_mark();
250 	open_getchr();
251 	raw_mode(1);
252 	init_signals(1);
253 
254 	/*
255 	 * Select the first file to examine.
256 	 */
257 #if TAGS
258 	if (tagoption != NULL || strcmp(tags, "-") == 0)
259 	{
260 		/*
261 		 * A -t option was given.
262 		 * Verify that no filenames were also given.
263 		 * Edit the file selected by the "tags" search,
264 		 * and search for the proper line in the file.
265 		 */
266 		if (nifile() > 0)
267 		{
268 			error("No filenames allowed with -t option", NULL_PARG);
269 			quit(QUIT_ERROR);
270 		}
271 		findtag(tagoption);
272 		if (edit_tagfile())  /* Edit file which contains the tag */
273 			quit(QUIT_ERROR);
274 		/*
275 		 * Search for the line which contains the tag.
276 		 * Set up initial_scrpos so we display that line.
277 		 */
278 		initial_scrpos.pos = tagsearch();
279 		if (initial_scrpos.pos == NULL_POSITION)
280 			quit(QUIT_ERROR);
281 		initial_scrpos.ln = jump_sline;
282 	} else
283 #endif
284 	if (nifile() == 0)
285 	{
286 		if (edit_stdin())  /* Edit standard input */
287 			quit(QUIT_ERROR);
288 		if (quit_if_one_screen)
289 			line_count = get_line_count();
290 	} else
291 	{
292 		if (edit_first())  /* Edit first valid file in cmd line */
293 			quit(QUIT_ERROR);
294 		/*
295 		 * In case that we have only one file and -F, have to get a line
296 		 * count fot init(). If the line count is less then a height of a term,
297 		 * the content of the file is printed out and then less quits. Otherwise
298 		 * -F can not be used
299 		 */
300 		if (quit_if_one_screen)
301 		{
302 			if (nifile() == 1)
303 				line_count = get_line_count();
304 			else /* In case more than one file, -F can not be used */
305 				quit_if_one_screen = FALSE;
306 		}
307 	}
308 
309 	init();
310 	commands();
311 	quit(QUIT_OK);
312 	/*NOTREACHED*/
313 	return (0);
314 }
315 
316 /*
317  * Copy a string to a "safe" place
318  * (that is, to a buffer allocated by calloc).
319  */
320 	public char *
321 save(s)
322 	constant char *s;
323 {
324 	char *p;
325 
326 	p = (char *) ecalloc(strlen(s)+1, sizeof(char));
327 	strcpy(p, s);
328 	return (p);
329 }
330 
331 /*
332  * Allocate memory.
333  * Like calloc(), but never returns an error (NULL).
334  */
335 	public VOID_POINTER
336 ecalloc(count, size)
337 	int count;
338 	unsigned int size;
339 {
340 	VOID_POINTER p;
341 
342 	p = (VOID_POINTER) calloc(count, size);
343 	if (p != NULL)
344 		return (p);
345 	error("Cannot allocate memory", NULL_PARG);
346 	quit(QUIT_ERROR);
347 	/*NOTREACHED*/
348 	return (NULL);
349 }
350 
351 /*
352  * Skip leading spaces in a string.
353  */
354 	public char *
355 skipsp(s)
356 	char *s;
357 {
358 	while (*s == ' ' || *s == '\t')
359 		s++;
360 	return (s);
361 }
362 
363 /*
364  * See how many characters of two strings are identical.
365  * If uppercase is true, the first string must begin with an uppercase
366  * character; the remainder of the first string may be either case.
367  */
368 	public int
369 sprefix(ps, s, uppercase)
370 	char *ps;
371 	char *s;
372 	int uppercase;
373 {
374 	int c;
375 	int sc;
376 	int len = 0;
377 
378 	for ( ;  *s != '\0';  s++, ps++)
379 	{
380 		c = *ps;
381 		if (uppercase)
382 		{
383 			if (len == 0 && ASCII_IS_LOWER(c))
384 				return (-1);
385 			if (ASCII_IS_UPPER(c))
386 				c = ASCII_TO_LOWER(c);
387 		}
388 		sc = *s;
389 		if (len > 0 && ASCII_IS_UPPER(sc))
390 			sc = ASCII_TO_LOWER(sc);
391 		if (c != sc)
392 			break;
393 		len++;
394 	}
395 	return (len);
396 }
397 
398 /*
399  * Exit the program.
400  */
401 	public void
402 quit(status)
403 	int status;
404 {
405 	static int save_status;
406 
407 	/*
408 	 * Put cursor at bottom left corner, clear the line,
409 	 * reset the terminal modes, and exit.
410 	 */
411 	if (status < 0)
412 		status = save_status;
413 	else
414 		save_status = status;
415 	quitting = 1;
416 	edit((char*)NULL);
417 	save_cmdhist();
418 	if (any_display && is_tty)
419 		clear_bot();
420 	deinit();
421 	flush();
422 	raw_mode(0);
423 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
424 	/*
425 	 * If we don't close 2, we get some garbage from
426 	 * 2's buffer when it flushes automatically.
427 	 * I cannot track this one down  RB
428 	 * The same bug shows up if we use ^C^C to abort.
429 	 */
430 	close(2);
431 #endif
432 #ifdef WIN32
433 	SetConsoleTitle(consoleTitle);
434 #endif
435 	close_getchr();
436 	exit(status);
437 }
438