xref: /netbsd-src/distrib/utils/more/main.c (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1 /*	$NetBSD: main.c,v 1.10 2009/01/24 13:43:21 tsutsui Exp $	*/
2 
3 /*
4  * Copyright (c) 1988 Mark Nudelman
5  * Copyright (c) 1988, 1993
6  *	Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 #ifndef lint
35 __COPYRIGHT(\
36 "@(#) Copyright (c) 1988 Mark Nudelman.\
37 @(#) Copyright (c) 1988, 1993\
38 Regents of the University of California.  All rights reserved.");
39 #endif /* not lint */
40 
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 6/7/93";
44 #else
45 __RCSID("$NetBSD: main.c,v 1.10 2009/01/24 13:43:21 tsutsui Exp $");
46 #endif
47 #endif /* not lint */
48 
49 /*
50  * Entry point, initialization, miscellaneous routines.
51  */
52 
53 #include <sys/types.h>
54 #include <sys/file.h>
55 #include <stdio.h>
56 #include <string.h>
57 #include <errno.h>
58 #include <stdlib.h>
59 #include <unistd.h>
60 
61 #include "less.h"
62 #include "extern.h"
63 
64 int	ispipe;
65 int	new_file;
66 int	is_tty;
67 char	*current_file, *previous_file, *current_name, *next_name;
68 off_t	prev_pos;
69 int	any_display;
70 int	scroll_lines;
71 int	ac;
72 char	**av;
73 int	curr_ac;
74 int	quitting;
75 
76 static void cat_file __P((void));
77 /*
78  * Edit a new file.
79  * Filename "-" means standard input.
80  * No filename means the "current" file, from the command line.
81  */
82 int
83 edit(filename)
84 	char *filename;
85 {
86 	int f;
87 	char *m;
88 	off_t initial_pos;
89 	static int didpipe;
90 	char message[100], *p;
91 
92 	initial_pos = NULL_POSITION;
93 	if (filename == NULL || *filename == '\0') {
94 		if (curr_ac >= ac) {
95 			error("No current file");
96 			return(0);
97 		}
98 		filename = save(av[curr_ac]);
99 	}
100 	else if (strcmp(filename, "#") == 0) {
101 		if (*previous_file == '\0') {
102 			error("no previous file");
103 			return(0);
104 		}
105 		filename = save(previous_file);
106 		initial_pos = prev_pos;
107 	} else
108 		filename = save(filename);
109 
110 	/* use standard input. */
111 	if (!strcmp(filename, "-")) {
112 		if (didpipe) {
113 			error("Can view standard input only once");
114 			return(0);
115 		}
116 		f = 0;
117 	}
118 	else if ((m = bad_file(filename, message, sizeof(message))) != NULL) {
119 		error(m);
120 		free(filename);
121 		return(0);
122 	}
123 	else if ((f = open(filename, O_RDONLY, 0)) < 0) {
124 		(void)snprintf(message, sizeof(message), "%s: %s", filename,
125 		    strerror(errno));
126 		error(message);
127 		free(filename);
128 		return(0);
129 	}
130 
131 	if (isatty(f)) {
132 		/*
133 		 * Not really necessary to call this an error,
134 		 * but if the control terminal (for commands)
135 		 * and the input file (for data) are the same,
136 		 * we get weird results at best.
137 		 */
138 		error("Can't take input from a terminal");
139 		if (f > 0)
140 			(void)close(f);
141 		(void)free(filename);
142 		return(0);
143 	}
144 
145 	/*
146 	 * We are now committed to using the new file.
147 	 * Close the current input file and set up to use the new one.
148 	 */
149 	if (file > 0)
150 		(void)close(file);
151 	new_file = 1;
152 	if (previous_file != NULL)
153 		free(previous_file);
154 	previous_file = current_file;
155 	current_file = filename;
156 	pos_clear();
157 	prev_pos = position(TOP);
158 	ispipe = (f == 0);
159 	if (ispipe) {
160 		didpipe = 1;
161 		current_name = "stdin";
162 	} else
163 		current_name = (p = rindex(filename, '/')) ? p + 1 : filename;
164 	if (curr_ac >= ac)
165 		next_name = NULL;
166 	else
167 		next_name = av[curr_ac + 1];
168 	file = f;
169 	ch_init(cbufs, 0);
170 	init_mark();
171 
172 	if (is_tty) {
173 		int no_display = !any_display;
174 		any_display = 1;
175 		if (no_display && errmsgs > 0) {
176 			/*
177 			 * We displayed some messages on error output
178 			 * (file descriptor 2; see error() function).
179 			 * Before erasing the screen contents,
180 			 * display the file name and wait for a keystroke.
181 			 */
182 			error(filename);
183 		}
184 		/*
185 		 * Indicate there is nothing displayed yet.
186 		 */
187 		if (initial_pos != NULL_POSITION)
188 			jump_loc(initial_pos);
189 		clr_linenum();
190 	}
191 	return(1);
192 }
193 
194 /*
195  * Edit the next file in the command line list.
196  */
197 void
198 next_file(n)
199 	int n;
200 {
201 	if (curr_ac + n >= ac) {
202 		if (quit_at_eof || position(TOP) == NULL_POSITION)
203 			quit();
204 		error("No (N-th) next file");
205 	}
206 	else
207 		(void)edit(av[curr_ac += n]);
208 }
209 
210 /*
211  * Edit the previous file in the command line list.
212  */
213 void
214 prev_file(n)
215 	int n;
216 {
217 	if (curr_ac - n < 0)
218 		error("No (N-th) previous file");
219 	else
220 		(void)edit(av[curr_ac -= n]);
221 }
222 
223 /*
224  * copy a file directly to standard output; used if stdout is not a tty.
225  * the only processing is to squeeze multiple blank input lines.
226  */
227 static void
228 cat_file()
229 {
230 	int c, empty;
231 
232 	if (squeeze) {
233 		empty = 0;
234 		while ((c = ch_forw_get()) != EOI)
235 			if (c != '\n') {
236 				putchr(c);
237 				empty = 0;
238 			}
239 			else if (empty < 2) {
240 				putchr(c);
241 				++empty;
242 			}
243 	}
244 	else while ((c = ch_forw_get()) != EOI)
245 		putchr(c);
246 	flush();
247 }
248 
249 int
250 main(argc, argv)
251 	int argc;
252 	char **argv;
253 {
254 	int envargc, argcnt;
255 	char *envargv[2];
256 
257 	/*
258 	 * Process command line arguments and MORE environment arguments.
259 	 * Command line arguments override environment arguments.
260 	 */
261 	if ((envargv[1] = getenv("MORE")) != NULL) {
262 		envargc = 2;
263 		envargv[0] = "more";
264 		envargv[2] = NULL;
265 		(void)option(envargc, envargv);
266 	}
267 	argcnt = option(argc, argv);
268 	argv += argcnt;
269 	argc -= argcnt;
270 
271 	/*
272 	 * Set up list of files to be examined.
273 	 */
274 	ac = argc;
275 	av = argv;
276 	curr_ac = 0;
277 
278 	/*
279 	 * Set up terminal, etc.
280 	 */
281 	is_tty = isatty(1);
282 	if (!is_tty) {
283 		/*
284 		 * Output is not a tty.
285 		 * Just copy the input file(s) to output.
286 		 */
287 		if (ac < 1) {
288 			(void)edit("-");
289 			cat_file();
290 		} else {
291 			do {
292 				(void)edit((char *)NULL);
293 				if (file >= 0)
294 					cat_file();
295 			} while (++curr_ac < ac);
296 		}
297 		exit(0);
298 	}
299 
300 	raw_mode(1);
301 	get_term();
302 	open_getchr();
303 	init();
304 	init_signals(1);
305 
306 	/* select the first file to examine. */
307 	if (ac < 1)
308 		(void)edit("-");	/* Standard input */
309 	else {
310 		/*
311 		 * Try all the files named as command arguments.
312 		 * We are simply looking for one which can be
313 		 * opened without error.
314 		 */
315 		do {
316 			(void)edit((char *)NULL);
317 		} while (file < 0 && ++curr_ac < ac);
318 	}
319 
320 	if (file >= 0)
321 		commands();
322 	quit();
323 }
324 
325 /*
326  * Copy a string to a "safe" place
327  * (that is, to a buffer allocated by malloc).
328  */
329 char *
330 save(s)
331 	char *s;
332 {
333 	char *p;
334 
335 	p = strdup(s);
336 	if (p == NULL)
337 	{
338 		error("cannot allocate memory");
339 		quit();
340 	}
341 	return(p);
342 }
343 
344 /*
345  * Exit the program.
346  */
347 void
348 quit()
349 {
350 	/*
351 	 * Put cursor at bottom left corner, clear the line,
352 	 * reset the terminal modes, and exit.
353 	 */
354 	quitting = 1;
355 	lower_left();
356 	clear_eol();
357 	deinit();
358 	flush();
359 	raw_mode(0);
360 	exit(0);
361 }
362