1 /* $NetBSD: main.c,v 1.9 2004/09/01 01:46:56 chs Exp $ */
4 * Copyright (c) 1988 Mark Nudelman
5 * Copyright (c) 1988, 1993
6 * Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
33 #include <sys/cdefs.h>
36 "@(#) Copyright (c) 1988 Mark Nudelman.\
37 @(#) Copyright (c) 1988, 1993\
38 Regents of the University of California. All rights reserved.");
43 static char sccsid
[] = "@(#)main.c 8.1 (Berkeley) 6/7/93";
45 __RCSID("$NetBSD: main.c,v 1.9 2004/09/01 01:46:56 chs Exp $");
50 * Entry point, initialization, miscellaneous routines.
53 #include <sys/types.h>
67 char *current_file
, *previous_file
, *current_name
, *next_name
;
76 static void cat_file
__P((void));
79 * Filename "-" means standard input.
80 * No filename means the "current" file, from the command line.
90 char message
[100], *p
;
92 initial_pos
= NULL_POSITION
;
93 if (filename
== NULL
|| *filename
== '\0') {
95 error("No current file");
98 filename
= save(av
[curr_ac
]);
100 else if (strcmp(filename
, "#") == 0) {
101 if (*previous_file
== '\0') {
102 error("no previous file");
105 filename
= save(previous_file
);
106 initial_pos
= prev_pos
;
108 filename
= save(filename
);
110 /* use standard input. */
111 if (!strcmp(filename
, "-")) {
113 error("Can view standard input only once");
118 else if ((m
= bad_file(filename
, message
, sizeof(message
))) != NULL
) {
123 else if ((f
= open(filename
, O_RDONLY
, 0)) < 0) {
124 (void)snprintf(message
, sizeof(message
), "%s: %s", filename
,
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.
138 error("Can't take input from a terminal");
141 (void)free(filename
);
146 * We are now committed to using the new file.
147 * Close the current input file and set up to use the new one.
152 if (previous_file
!= NULL
)
154 previous_file
= current_file
;
155 current_file
= filename
;
157 prev_pos
= position(TOP
);
161 current_name
= "stdin";
163 current_name
= (p
= rindex(filename
, '/')) ? p
+ 1 : filename
;
167 next_name
= av
[curr_ac
+ 1];
173 int no_display
= !any_display
;
175 if (no_display
&& errmsgs
> 0) {
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.
185 * Indicate there is nothing displayed yet.
187 if (initial_pos
!= NULL_POSITION
)
188 jump_loc(initial_pos
);
195 * Edit the next file in the command line list.
201 if (curr_ac
+ n
>= ac
) {
202 if (quit_at_eof
|| position(TOP
) == NULL_POSITION
)
204 error("No (N-th) next file");
207 (void)edit(av
[curr_ac
+= n
]);
211 * Edit the previous file in the command line list.
218 error("No (N-th) previous file");
220 (void)edit(av
[curr_ac
-= n
]);
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.
234 while ((c
= ch_forw_get()) != EOI
)
239 else if (empty
< 2) {
244 else while ((c
= ch_forw_get()) != EOI
)
258 * Process command line arguments and MORE environment arguments.
259 * Command line arguments override environment arguments.
261 if ((envargv
[1] = getenv("MORE")) != NULL
) {
265 (void)option(envargc
, envargv
);
267 argcnt
= option(argc
, argv
);
272 * Set up list of files to be examined.
279 * Set up terminal, etc.
284 * Output is not a tty.
285 * Just copy the input file(s) to output.
292 (void)edit((char *)NULL
);
295 } while (++curr_ac
< ac
);
306 /* select the first file to examine. */
308 (void)edit("-"); /* Standard input */
311 * Try all the files named as command arguments.
312 * We are simply looking for one which can be
313 * opened without error.
316 (void)edit((char *)NULL
);
317 } while (file
< 0 && ++curr_ac
< ac
);
326 * Copy a string to a "safe" place
327 * (that is, to a buffer allocated by malloc).
338 error("cannot allocate memory");
351 * Put cursor at bottom left corner, clear the line,
352 * reset the terminal modes, and exit.