2 * Copyright (c) 1988 Mark Nudleman
3 * Copyright (c) 1988, 1993
4 * Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 "@(#) Copyright (c) 1988 Mark Nudleman.\n\
38 @(#) Copyright (c) 1988, 1993
39 Regents of the University of California. All rights reserved.\n";
43 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/7/93";
47 static const char rcsid[] =
52 * Entry point, initialization, miscellaneous routines.
56 #include <sys/param.h>
57 #include <sys/types.h>
68 char *current_file, *previous_file, *current_name, *next_name;
84 * Filename "-" means standard input.
85 * No filename means the "current" file, from the command line. If called
86 * with the same filename in succession, filename will be closed and reopened.
89 register char *filename;
94 off_t initial_pos, prev_pos, position();
96 char message[MAXPATHLEN + 50], *p;
97 char *rindex(), *strerror(), *save(), *bad_file();
99 initial_pos = NULL_POSITION;
100 if (filename == NULL || *filename == '\0') {
102 error("No current file");
105 filename = save(av[curr_ac]);
107 else if (strcmp(filename, "#") == 0) {
108 if (*previous_file == '\0') {
109 error("no previous file");
112 filename = save(previous_file);
113 initial_pos = prev_pos;
115 filename = save(filename);
117 /* use standard input. */
118 if (!strcmp(filename, "-")) {
120 error("can view standard input only once");
125 else if ((m = bad_file(filename, message, sizeof(message))) != NULL) {
130 else if ((f = open(filename, O_RDONLY, 0)) < 0) {
131 (void)snprintf(message, sizeof(message),
132 "%s: %s", filename, strerror(errno));
140 * Not really necessary to call this an error,
141 * but if the control terminal (for commands)
142 * and the input file (for data) are the same,
143 * we get weird results at best.
145 error("Can't take input from a terminal");
148 (void)free(filename);
153 * We are now committed to using the new file.
154 * Close the current input file and set up to use the new one.
158 if (previous_file != NULL)
160 previous_file = current_file;
161 current_file = filename;
163 prev_pos = position(TOP);
167 current_name = "stdin";
169 current_name = (p = rindex(filename, '/')) ? p + 1 : filename;
173 next_name = av[curr_ac + 1];
178 if (isatty(STDOUT_FILENO)) {
179 int no_display = !any_display;
181 if (no_display && errmsgs > 0) {
183 * We displayed some messages on error output
184 * (file descriptor 2; see error() function).
185 * Before erasing the screen contents,
186 * display the file name and wait for a keystroke.
191 * Indicate there is nothing displayed yet.
193 if (initial_pos != NULL_POSITION)
194 jump_loc(initial_pos);
201 * Edit the next file in the command line list.
206 extern int quit_at_eof;
209 if (curr_ac + n >= ac) {
210 if (quit_at_eof || position(TOP) == NULL_POSITION)
212 error("No (N-th) next file");
215 (void)edit(av[curr_ac += n]);
219 * Edit the previous file in the command line list.
225 error("No (N-th) previous file");
227 (void)edit(av[curr_ac -= n]);
231 * copy a file directly to standard output; used if stdout is not a tty.
232 * the only processing is to squeeze multiple blank input lines.
238 register int c, empty;
242 while ((c = ch_forw_get()) != EOI)
247 else if (empty < 2) {
252 else while ((c = ch_forw_get()) != EOI)
264 (void) setlocale(LC_ALL, "");
267 * Process command line arguments and MORE environment arguments.
268 * Command line arguments override environment arguments.
270 if (envargv[1] = getenv("MORE")) {
274 (void)option(envargc, envargv);
276 argcnt = option(argc, argv);
281 * Set up list of files to be examined.
288 * Set up terminal, etc.
290 if (!isatty(STDOUT_FILENO)) {
292 * Output is not a tty.
293 * Just copy the input file(s) to output.
301 (void)edit((char *)NULL);
304 } while (++curr_ac < ac);
315 /* select the first file to examine. */
318 * A -t option was given; edit the file selected by the
319 * "tags" search, and search for the proper line in the file.
321 if (!tagfile || !edit(tagfile) || tagsearch())
325 (void)edit("-"); /* Standard input */
328 * Try all the files named as command arguments.
329 * We are simply looking for one which can be
330 * opened without error.
333 (void)edit((char *)NULL);
334 } while (file < 0 && ++curr_ac < ac);
344 * Copy a string to a "safe" place
345 * (that is, to a buffer allocated by malloc).
353 p = malloc((u_int)strlen(s)+1);
356 error("cannot allocate memory");
359 return(strcpy(p, s));
368 * Put cursor at bottom left corner, clear the line,
369 * reset the terminal modes, and exit.