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