xref: /openbsd-src/usr.bin/less/main.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*
2  * Copyright (C) 1984-2011  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 about less, or for information on how to
8  * contact the author, 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 extern int	less_is_more;
58 extern int	missing_cap;
59 extern int	know_dumb;
60 extern int	quit_if_one_screen;
61 extern int	pr_type;
62 
63 
64 /*
65  * Entry point.
66  */
67 int
68 main(argc, argv)
69 	int argc;
70 	char *argv[];
71 {
72 	IFILE ifile;
73 	char *s;
74 
75 #ifdef __EMX__
76 	_response(&argc, &argv);
77 	_wildcard(&argc, &argv);
78 #endif
79 
80 	progname = *argv++;
81 	argc--;
82 
83 	secure = 0;
84 	s = lgetenv("LESSSECURE");
85 	if (s != NULL && *s != '\0')
86 		secure = 1;
87 
88 #ifdef WIN32
89 	if (getenv("HOME") == NULL)
90 	{
91 		/*
92 		 * If there is no HOME environment variable,
93 		 * try the concatenation of HOMEDRIVE + HOMEPATH.
94 		 */
95 		char *drive = getenv("HOMEDRIVE");
96 		char *path  = getenv("HOMEPATH");
97 		if (drive != NULL && path != NULL)
98 		{
99 			size_t len = strlen(drive) + strlen(path) + 6;
100 			char *env = (char *) ecalloc(len, sizeof(char));
101 			strlcpy(env, "HOME=", len);
102 			strlcat(env, drive, len);
103 			strlcat(env, path, len);
104 			putenv(env);
105 		}
106 	}
107 	GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
108 #endif /* WIN32 */
109 
110 	/*
111 	 * Process command line arguments and LESS environment arguments.
112 	 * Command line arguments override environment arguments.
113 	 */
114 	is_tty = isatty(1);
115 	get_term();
116 	init_cmds();
117 	init_charset();
118 	init_line();
119 	init_cmdhist();
120 	init_option();
121 	init_search();
122 
123 	/*
124 	 * If the name of the executable program is "more",
125 	 * act like LESS_IS_MORE is set.
126 	 */
127 	for (s = progname + strlen(progname);  s > progname;  s--)
128 	{
129 		if (s[-1] == PATHNAME_SEP[0])
130 			break;
131 	}
132 	if (strcmp(s, "more") == 0)
133 		less_is_more = 1;
134 
135 	init_prompt();
136 
137 	if (less_is_more) {
138 		scan_option("-G");
139 		scan_option("-L");
140 		scan_option("-X");
141 		scan_option("-c");
142 	}
143 
144 	s = lgetenv(less_is_more ? "MORE" : "LESS");
145 	if (s != NULL)
146 		scan_option(save(s));
147 
148 #define	isoptstring(s)	(((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 && get_quit_at_eof())
170 		quit_if_one_screen = 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 !SMALL
191 	if (dohelp)
192 		ifile = get_ifile(HELPFILE, ifile);
193 #endif /* !SMALL */
194 	while (argc-- > 0)
195 	{
196 		char *filename;
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 *gfilename;
206 
207 		gfilename = lglob(*argv++);
208 		init_textlist(&tlist, gfilename);
209 		filename = NULL;
210 		while ((filename = forw_textlist(&tlist, filename)) != NULL)
211 		{
212 			(void) get_ifile(filename, ifile);
213 			ifile = prev_ifile(NULL_IFILE);
214 		}
215 		free(gfilename);
216 #else
217 		filename = shell_quote(*argv);
218 		if (filename == NULL)
219 			filename = *argv;
220 		argv++;
221 		(void) get_ifile(filename, 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_BINARY(1);
235 		if (nifile() == 0)
236 		{
237 			if (edit_stdin() == 0)
238 				cat_file();
239 		} else if (edit_first() == 0)
240 		{
241 			do {
242 				cat_file();
243 			} while (edit_next(1) == 0);
244 		}
245 		quit(QUIT_OK);
246 	}
247 
248 	if (missing_cap && !know_dumb && !less_is_more)
249 		error("WARNING: terminal is not fully functional", NULL_PARG);
250 	init_mark();
251 	open_getchr();
252 	raw_mode(1);
253 	init_signals(1);
254 
255 	/*
256 	 * Select the first file to examine.
257 	 */
258 #if TAGS
259 	if (tagoption != NULL || strcmp(tags, "-") == 0)
260 	{
261 		/*
262 		 * A -t option was given.
263 		 * Verify that no filenames were also given.
264 		 * Edit the file selected by the "tags" search,
265 		 * and search for the proper line in the file.
266 		 */
267 		if (nifile() > 0)
268 		{
269 			error("No filenames allowed with -t option", NULL_PARG);
270 			quit(QUIT_ERROR);
271 		}
272 		findtag(tagoption);
273 		if (edit_tagfile())  /* Edit file which contains the tag */
274 			quit(QUIT_ERROR);
275 		/*
276 		 * Search for the line which contains the tag.
277 		 * Set up initial_scrpos so we display that line.
278 		 */
279 		initial_scrpos.pos = tagsearch();
280 		if (initial_scrpos.pos == NULL_POSITION)
281 			quit(QUIT_ERROR);
282 		initial_scrpos.ln = jump_sline;
283 	} else
284 #endif
285 	if (nifile() == 0)
286 	{
287 		if (edit_stdin())  /* Edit standard input */
288 			quit(QUIT_ERROR);
289 	} else
290 	{
291 		if (edit_first())  /* Edit first valid file in cmd line */
292 			quit(QUIT_ERROR);
293 	}
294 
295 	init();
296 	commands();
297 	quit(QUIT_OK);
298 	/*NOTREACHED*/
299 	return (0);
300 }
301 
302 /*
303  * Copy a string to a "safe" place
304  * (that is, to a buffer allocated by calloc).
305  */
306 	public char *
307 save(s)
308 	char *s;
309 {
310 	register char *p;
311 	size_t len = strlen(s) + 1;
312 
313 	p = (char *) ecalloc(len, sizeof(char));
314 	strlcpy(p, s, len);
315 	return (p);
316 }
317 
318 /*
319  * Allocate memory.
320  * Like calloc(), but never returns an error (NULL).
321  */
322 	public VOID_POINTER
323 ecalloc(count, size)
324 	int count;
325 	unsigned int size;
326 {
327 	register VOID_POINTER p;
328 
329 	p = (VOID_POINTER) calloc(count, size);
330 	if (p != NULL)
331 		return (p);
332 	error("Cannot allocate memory", NULL_PARG);
333 	quit(QUIT_ERROR);
334 	/*NOTREACHED*/
335 	return (NULL);
336 }
337 
338 /*
339  * Skip leading spaces in a string.
340  */
341 	public char *
342 skipsp(s)
343 	register char *s;
344 {
345 	while (*s == ' ' || *s == '\t')
346 		s++;
347 	return (s);
348 }
349 
350 #if GNU_OPTIONS
351 /*
352  * See how many characters of two strings are identical.
353  * If uppercase is true, the first string must begin with an uppercase
354  * character; the remainder of the first string may be either case.
355  */
356 	public int
357 sprefix(ps, s, uppercase)
358 	char *ps;
359 	char *s;
360 	int uppercase;
361 {
362 	register int c;
363 	register int sc;
364 	register int len = 0;
365 
366 	for ( ;  *s != '\0';  s++, ps++)
367 	{
368 		c = *ps;
369 		if (uppercase)
370 		{
371 			if (len == 0 && ASCII_IS_LOWER(c))
372 				return (-1);
373 			if (ASCII_IS_UPPER(c))
374 				c = ASCII_TO_LOWER(c);
375 		}
376 		sc = *s;
377 		if (len > 0 && ASCII_IS_UPPER(sc))
378 			sc = ASCII_TO_LOWER(sc);
379 		if (c != sc)
380 			break;
381 		len++;
382 	}
383 	return (len);
384 }
385 #endif /* GNU_OPTIONS */
386 
387 /*
388  * Exit the program.
389  */
390 	public void
391 quit(status)
392 	int status;
393 {
394 	static int save_status;
395 
396 	/*
397 	 * Put cursor at bottom left corner, clear the line,
398 	 * reset the terminal modes, and exit.
399 	 */
400 	if (status < 0)
401 		status = save_status;
402 	else
403 		save_status = status;
404 	quitting = 1;
405 	edit((char*)NULL);
406 	save_cmdhist();
407 	if (any_display && is_tty)
408 		clear_bot();
409 	deinit();
410 	flush();
411 	raw_mode(0);
412 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
413 	/*
414 	 * If we don't close 2, we get some garbage from
415 	 * 2's buffer when it flushes automatically.
416 	 * I cannot track this one down  RB
417 	 * The same bug shows up if we use ^C^C to abort.
418 	 */
419 	close(2);
420 #endif
421 #ifdef WIN32
422 	SetConsoleTitle(consoleTitle);
423 #endif
424 	close_getchr();
425 	exit(status);
426 }
427