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