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