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