Commit | Line | Data |
---|---|---|
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 | |
21 | char 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 | |
bc258617 | 28 | static char sccsid[] = "@(#)main.c 5.9 (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 |
40 | int ispipe; |
41 | int new_file; | |
42 | int is_tty; | |
43 | char *current_file, *previous_file, *current_name; | |
44 | off_t prev_pos; | |
45 | int any_display; | |
46 | int scroll; | |
47 | int ac; | |
48 | char **av; | |
49 | int curr_ac; | |
50 | int quitting; | |
bfe13c81 KB |
51 | |
52 | extern int file; | |
bfe13c81 KB |
53 | extern int cbufs; |
54 | extern int errmsgs; | |
55 | ||
bc258617 | 56 | extern char *tagfile; |
bfe13c81 | 57 | extern 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 |
64 | edit(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 |
175 | next_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 |
193 | prev_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 | /* | |
203 | * Copy a file directly to standard output. | |
204 | * Used if standard output is not a tty. | |
205 | */ | |
bc258617 | 206 | static |
bfe13c81 KB |
207 | cat_file() |
208 | { | |
209 | register int c; | |
210 | ||
211 | while ((c = ch_forw_get()) != EOI) | |
212 | putchr(c); | |
213 | flush(); | |
214 | } | |
215 | ||
bfe13c81 KB |
216 | main(argc, argv) |
217 | int argc; | |
bc258617 | 218 | char **argv; |
bfe13c81 | 219 | { |
bc258617 KB |
220 | int envargc, argcnt; |
221 | char *envargv[2], *getenv(); | |
bfe13c81 KB |
222 | |
223 | /* | |
bc258617 | 224 | * Process command line arguments and MORE environment arguments. |
bfe13c81 KB |
225 | * Command line arguments override environment arguments. |
226 | */ | |
bc258617 KB |
227 | if (envargv[1] = getenv("MORE")) { |
228 | envargc = 2; | |
229 | envargv[0] = "more"; | |
230 | envargv[2] = NULL; | |
231 | (void)option(envargc, envargv); | |
232 | } | |
233 | argcnt = option(argc, argv); | |
234 | argv += argcnt; | |
235 | argc -= argcnt; | |
bfe13c81 | 236 | |
bfe13c81 KB |
237 | /* |
238 | * Set up list of files to be examined. | |
239 | */ | |
240 | ac = argc; | |
241 | av = argv; | |
242 | curr_ac = 0; | |
243 | ||
244 | /* | |
245 | * Set up terminal, etc. | |
246 | */ | |
247 | is_tty = isatty(1); | |
bc258617 | 248 | if (!is_tty) { |
bfe13c81 KB |
249 | /* |
250 | * Output is not a tty. | |
251 | * Just copy the input file(s) to output. | |
252 | */ | |
bc258617 KB |
253 | if (ac < 1) { |
254 | (void)edit("-"); | |
bfe13c81 | 255 | cat_file(); |
bc258617 KB |
256 | } else { |
257 | do { | |
258 | (void)edit((char *)NULL); | |
bfe13c81 KB |
259 | if (file >= 0) |
260 | cat_file(); | |
261 | } while (++curr_ac < ac); | |
262 | } | |
263 | exit(0); | |
264 | } | |
265 | ||
266 | raw_mode(1); | |
267 | get_term(); | |
268 | open_getchr(); | |
269 | init(); | |
bfe13c81 KB |
270 | init_signals(1); |
271 | ||
bc258617 KB |
272 | /* select the first file to examine. */ |
273 | if (tagoption) { | |
bfe13c81 | 274 | /* |
bc258617 KB |
275 | * A -t option was given; edit the file selected by the |
276 | * "tags" search, and search for the proper line in the file. | |
bfe13c81 | 277 | */ |
bc258617 | 278 | if (!tagfile || !edit(tagfile) || tagsearch()) |
bfe13c81 | 279 | quit(); |
bc258617 KB |
280 | } |
281 | else if (ac < 1) | |
282 | (void)edit("-"); /* Standard input */ | |
283 | else { | |
bfe13c81 KB |
284 | /* |
285 | * Try all the files named as command arguments. | |
286 | * We are simply looking for one which can be | |
287 | * opened without error. | |
288 | */ | |
bc258617 KB |
289 | do { |
290 | (void)edit((char *)NULL); | |
bfe13c81 KB |
291 | } while (file < 0 && ++curr_ac < ac); |
292 | } | |
293 | ||
294 | if (file >= 0) | |
295 | commands(); | |
296 | quit(); | |
297 | /*NOTREACHED*/ | |
298 | } | |
299 | ||
bfe13c81 KB |
300 | /* |
301 | * Copy a string to a "safe" place | |
c48c82c8 | 302 | * (that is, to a buffer allocated by malloc). |
bfe13c81 | 303 | */ |
bc258617 | 304 | char * |
bfe13c81 KB |
305 | save(s) |
306 | char *s; | |
307 | { | |
c48c82c8 | 308 | char *p, *strcpy(), *malloc(); |
bfe13c81 | 309 | |
c48c82c8 | 310 | p = malloc((u_int)strlen(s)+1); |
bfe13c81 KB |
311 | if (p == NULL) |
312 | { | |
313 | error("cannot allocate memory"); | |
314 | quit(); | |
315 | } | |
c48c82c8 | 316 | return(strcpy(p, s)); |
bfe13c81 KB |
317 | } |
318 | ||
319 | /* | |
320 | * Exit the program. | |
321 | */ | |
bfe13c81 KB |
322 | quit() |
323 | { | |
324 | /* | |
325 | * Put cursor at bottom left corner, clear the line, | |
326 | * reset the terminal modes, and exit. | |
327 | */ | |
328 | quitting = 1; | |
bfe13c81 KB |
329 | lower_left(); |
330 | clear_eol(); | |
331 | deinit(); | |
332 | flush(); | |
333 | raw_mode(0); | |
334 | exit(0); | |
335 | } |