1/*	$NetBSD: main.c,v 1.3 2011/07/03 20:14:13 tron Exp $	*/
2
3/*
4 * Copyright (C) 1984-2011  Mark Nudelman
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Less License, as specified in the README file.
8 *
9 * For more information about less, or for information on how to
10 * contact the author, see the README file.
11 */
12
13
14/*
15 * Entry point, initialization, miscellaneous routines.
16 */
17
18#include "less.h"
19#if MSDOS_COMPILER==WIN32C
20#include <windows.h>
21#endif
22
23public char *	every_first_cmd = NULL;
24public int	new_file;
25public int	is_tty;
26public IFILE	curr_ifile = NULL_IFILE;
27public IFILE	old_ifile = NULL_IFILE;
28public struct scrpos initial_scrpos;
29public int	any_display = FALSE;
30public POSITION	start_attnpos = NULL_POSITION;
31public POSITION	end_attnpos = NULL_POSITION;
32public int	wscroll;
33public char *	progname;
34public int	quitting;
35public int	secure;
36public int	dohelp;
37public int	more_mode = 0;
38
39#if LOGFILE
40public int	logfile = -1;
41public int	force_logfile = FALSE;
42public char *	namelogfile = NULL;
43#endif
44
45#if EDITOR
46public char *	editor;
47public char *	editproto;
48#endif
49
50#if TAGS
51extern char *	tags;
52extern char *	tagoption;
53extern int	jump_sline;
54#endif
55
56#ifdef WIN32
57static char consoleTitle[256];
58#endif
59
60extern int	less_is_more;
61extern int	missing_cap;
62extern int	know_dumb;
63extern int	quit_if_one_screen;
64extern int	pr_type;
65
66
67/*
68 * Entry point.
69 */
70int
71main(argc, argv)
72	int argc;
73	char *argv[];
74{
75	IFILE ifile;
76	char *s;
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	if (strcmp(getprogname(), "more") == 0)
118		more_mode = 1;
119
120	is_tty = isatty(1);
121	get_term();
122	init_cmds();
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	for (s = progname + strlen(progname);  s > progname;  s--)
134	{
135		if (s[-1] == PATHNAME_SEP[0])
136			break;
137	}
138	if (strcmp(s, "more") == 0)
139		less_is_more = 1;
140
141	init_prompt();
142
143	s = lgetenv(less_is_more ? "MORE" : "LESS");
144	if (s != NULL)
145		scan_option(save(s));
146
147#define	isoptstring(s)	(((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 && get_quit_at_eof())
169		quit_if_one_screen = TRUE;
170
171#if EDITOR
172	editor = lgetenv("VISUAL");
173	if (editor == NULL || *editor == '\0')
174	{
175		editor = lgetenv("EDITOR");
176		if (editor == NULL || *editor == '\0')
177			editor = EDIT_PGM;
178	}
179	editproto = lgetenv("LESSEDIT");
180	if (editproto == NULL || *editproto == '\0')
181		editproto = "%E ?lm+%lm. %f";
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		char *filename;
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 *gfilename;
203
204		gfilename = lglob(*argv++);
205		init_textlist(&tlist, gfilename);
206		filename = NULL;
207		while ((filename = forw_textlist(&tlist, filename)) != NULL)
208		{
209			(void) get_ifile(filename, ifile);
210			ifile = prev_ifile(NULL_IFILE);
211		}
212		free(gfilename);
213#else
214		filename = shell_quote(*argv);
215		if (filename == NULL)
216			filename = *argv;
217		argv++;
218		(void) get_ifile(filename, 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 && !more_mode)
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	} else
287	{
288		if (edit_first())  /* Edit first valid file in cmd line */
289			quit(QUIT_ERROR);
290	}
291
292	init();
293	commands();
294	quit(QUIT_OK);
295	/*NOTREACHED*/
296	return (0);
297}
298
299/*
300 * Copy a string to a "safe" place
301 * (that is, to a buffer allocated by calloc).
302 */
303	public char *
304save(s)
305	char *s;
306{
307	register char *p;
308
309	p = (char *) ecalloc(strlen(s)+1, sizeof(char));
310	strcpy(p, s);
311	return (p);
312}
313
314/*
315 * Allocate memory.
316 * Like calloc(), but never returns an error (NULL).
317 */
318	public VOID_POINTER
319ecalloc(count, size)
320	int count;
321	unsigned int size;
322{
323	register VOID_POINTER p;
324
325	p = (VOID_POINTER) calloc(count, size);
326	if (p != NULL)
327		return (p);
328	error("Cannot allocate memory", NULL_PARG);
329	quit(QUIT_ERROR);
330	/*NOTREACHED*/
331	return (NULL);
332}
333
334/*
335 * Skip leading spaces in a string.
336 */
337	public char *
338skipsp(s)
339	register char *s;
340{
341	while (*s == ' ' || *s == '\t')
342		s++;
343	return (s);
344}
345
346/*
347 * See how many characters of two strings are identical.
348 * If uppercase is true, the first string must begin with an uppercase
349 * character; the remainder of the first string may be either case.
350 */
351	public int
352sprefix(ps, s, uppercase)
353	char *ps;
354	char *s;
355	int uppercase;
356{
357	register int c;
358	register int sc;
359	register int len = 0;
360
361	for ( ;  *s != '\0';  s++, ps++)
362	{
363		c = *ps;
364		if (uppercase)
365		{
366			if (len == 0 && ASCII_IS_LOWER(c))
367				return (-1);
368			if (ASCII_IS_UPPER(c))
369				c = ASCII_TO_LOWER(c);
370		}
371		sc = *s;
372		if (len > 0 && ASCII_IS_UPPER(sc))
373			sc = ASCII_TO_LOWER(sc);
374		if (c != sc)
375			break;
376		len++;
377	}
378	return (len);
379}
380
381/*
382 * Exit the program.
383 */
384	public void
385quit(status)
386	int status;
387{
388	static int save_status;
389
390	/*
391	 * Put cursor at bottom left corner, clear the line,
392	 * reset the terminal modes, and exit.
393	 */
394	if (status < 0)
395		status = save_status;
396	else
397		save_status = status;
398	quitting = 1;
399	edit((char*)NULL);
400	save_cmdhist();
401	if (any_display && is_tty)
402		clear_bot();
403	deinit();
404	flush();
405	raw_mode(0);
406#if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
407	/*
408	 * If we don't close 2, we get some garbage from
409	 * 2's buffer when it flushes automatically.
410	 * I cannot track this one down  RB
411	 * The same bug shows up if we use ^C^C to abort.
412	 */
413	close(2);
414#endif
415#ifdef WIN32
416	SetConsoleTitle(consoleTitle);
417#endif
418	close_getchr();
419	exit(status);
420}
421