Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /*- |
2 | * Copyright (c) 1980 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #ifndef lint | |
35 | char copyright[] = | |
36 | "@(#) Copyright (c) 1980 The Regents of the University of California.\n\ | |
37 | All rights reserved.\n"; | |
38 | #endif /* not lint */ | |
39 | ||
40 | #ifndef lint | |
41 | static char sccsid[] = "@(#)more.c 5.26 (Berkeley) 4/18/91"; | |
42 | #endif /* not lint */ | |
43 | ||
44 | /* | |
45 | ** more.c - General purpose tty output filter and file perusal program | |
46 | ** | |
47 | ** by Eric Shienbrood, UC Berkeley | |
48 | ** | |
49 | ** modified by Geoff Peck, UCB to add underlining, single spacing | |
50 | ** modified by John Foderaro, UCB to add -c and MORE environment variable | |
51 | */ | |
52 | ||
53 | #include <sys/param.h> | |
54 | #include <sys/stat.h> | |
55 | #include <sys/file.h> | |
56 | #include <signal.h> | |
57 | #include <errno.h> | |
58 | #include <sgtty.h> | |
59 | #include <setjmp.h> | |
60 | #include <a.out.h> | |
61 | #include <varargs.h> | |
62 | #include <stdio.h> | |
63 | #include <ctype.h> | |
64 | #include <regexp.h> | |
65 | #include "pathnames.h" | |
66 | ||
67 | #define Fopen(s,m) (Currline = 0,file_pos=0,fopen(s,m)) | |
68 | #define Ftell(f) file_pos | |
69 | #define Fseek(f,off) (file_pos=off,fseek(f,off,0)) | |
70 | #define Getc(f) (++file_pos, getc(f)) | |
71 | #define Ungetc(c,f) (--file_pos, ungetc(c,f)) | |
72 | ||
73 | #define MBIT CBREAK | |
74 | #define stty(fd,argp) ioctl(fd,TIOCSETN,argp) | |
75 | ||
76 | #define TBUFSIZ 1024 | |
77 | #define LINSIZ 256 | |
78 | #define ctrl(letter) (letter & 077) | |
79 | #define RUBOUT '\177' | |
80 | #define ESC '\033' | |
81 | #define QUIT '\034' | |
82 | ||
83 | struct sgttyb otty, savetty; | |
84 | long file_pos, file_size; | |
85 | int fnum, no_intty, no_tty, slow_tty; | |
86 | int dum_opt, dlines; | |
87 | void chgwinsz(), end_it(), onquit(), onsusp(); | |
88 | int nscroll = 11; /* Number of lines scrolled by 'd' */ | |
89 | int fold_opt = 1; /* Fold long lines */ | |
90 | int stop_opt = 1; /* Stop after form feeds */ | |
91 | int ssp_opt = 0; /* Suppress white space */ | |
92 | int ul_opt = 1; /* Underline as best we can */ | |
93 | int promptlen; | |
94 | int Currline; /* Line we are currently at */ | |
95 | int startup = 1; | |
96 | int firstf = 1; | |
97 | int notell = 1; | |
98 | int docrterase = 0; | |
99 | int docrtkill = 0; | |
100 | int bad_so; /* True if overwriting does not turn off standout */ | |
101 | int inwait, Pause, errors; | |
102 | int within; /* true if we are within a file, | |
103 | false if we are between files */ | |
104 | int hard, dumb, noscroll, hardtabs, clreol, eatnl; | |
105 | int catch_susp; /* We should catch the SIGTSTP signal */ | |
106 | char **fnames; /* The list of file names */ | |
107 | int nfiles; /* Number of files left to process */ | |
108 | char *shell; /* The name of the shell to use */ | |
109 | int shellp; /* A previous shell command exists */ | |
110 | char ch; | |
111 | jmp_buf restore; | |
112 | char Line[LINSIZ]; /* Line buffer */ | |
113 | int Lpp = 24; /* lines per page */ | |
114 | char *Clear; /* clear screen */ | |
115 | char *eraseln; /* erase line */ | |
116 | char *Senter, *Sexit;/* enter and exit standout mode */ | |
117 | char *ULenter, *ULexit; /* enter and exit underline mode */ | |
118 | char *chUL; /* underline character */ | |
119 | char *chBS; /* backspace character */ | |
120 | char *Home; /* go to home */ | |
121 | char *cursorm; /* cursor movement */ | |
122 | char cursorhome[40]; /* contains cursor movement to home */ | |
123 | char *EodClr; /* clear rest of screen */ | |
124 | char *tgetstr(); | |
125 | int Mcol = 80; /* number of columns */ | |
126 | int Wrap = 1; /* set if automargins */ | |
127 | int soglitch; /* terminal has standout mode glitch */ | |
128 | int ulglitch; /* terminal has underline mode glitch */ | |
129 | int pstate = 0; /* current UL state */ | |
130 | char *getenv(); | |
131 | regexp *previous; /* previous regular expression */ | |
132 | struct { | |
133 | long chrctr, line; | |
134 | } context, screen_start; | |
135 | extern char PC; /* pad character */ | |
136 | extern short ospeed; | |
137 | ||
138 | ||
139 | main(argc, argv) | |
140 | int argc; | |
141 | char *argv[]; | |
142 | { | |
143 | register FILE *f; | |
144 | register char *s; | |
145 | register char *p; | |
146 | register char ch; | |
147 | register int left; | |
148 | int prnames = 0; | |
149 | int initopt = 0; | |
150 | int srchopt = 0; | |
151 | int clearit = 0; | |
152 | int initline; | |
153 | char initbuf[80]; | |
154 | FILE *checkf(); | |
155 | ||
156 | nfiles = argc; | |
157 | fnames = argv; | |
158 | initterm (); | |
159 | nscroll = Lpp/2 - 1; | |
160 | if (nscroll <= 0) | |
161 | nscroll = 1; | |
162 | if(s = getenv("MORE")) argscan(s); | |
163 | while (--nfiles > 0) { | |
164 | if ((ch = (*++fnames)[0]) == '-') { | |
165 | argscan(*fnames+1); | |
166 | } | |
167 | else if (ch == '+') { | |
168 | s = *fnames; | |
169 | if (*++s == '/') { | |
170 | srchopt++; | |
171 | for (++s, p = initbuf; p < initbuf + 79 && *s != '\0';) | |
172 | *p++ = *s++; | |
173 | *p = '\0'; | |
174 | } | |
175 | else { | |
176 | initopt++; | |
177 | for (initline = 0; *s != '\0'; s++) | |
178 | if (isdigit (*s)) | |
179 | initline = initline*10 + *s -'0'; | |
180 | --initline; | |
181 | } | |
182 | } | |
183 | else break; | |
184 | } | |
185 | /* allow clreol only if Home and eraseln and EodClr strings are | |
186 | * defined, and in that case, make sure we are in noscroll mode | |
187 | */ | |
188 | if(clreol) | |
189 | { | |
190 | if((Home == NULL) || (*Home == '\0') || | |
191 | (eraseln == NULL) || (*eraseln == '\0') || | |
192 | (EodClr == NULL) || (*EodClr == '\0') ) | |
193 | clreol = 0; | |
194 | else noscroll = 1; | |
195 | } | |
196 | if (dlines == 0) | |
197 | dlines = Lpp - (noscroll ? 1 : 2); | |
198 | left = dlines; | |
199 | if (nfiles > 1) | |
200 | prnames++; | |
201 | if (!no_intty && nfiles == 0) { | |
202 | char *rindex(); | |
203 | ||
204 | p = rindex(argv[0], '/'); | |
205 | fputs("usage: ",stderr); | |
206 | fputs(p ? p + 1 : argv[0],stderr); | |
207 | fputs(" [-dfln] [+linenum | +/pattern] name1 name2 ...\n",stderr); | |
208 | exit(1); | |
209 | } | |
210 | else | |
211 | f = stdin; | |
212 | if (!no_tty) { | |
213 | signal(SIGQUIT, onquit); | |
214 | signal(SIGINT, end_it); | |
215 | signal(SIGWINCH, chgwinsz); | |
216 | if (signal (SIGTSTP, SIG_IGN) == SIG_DFL) { | |
217 | signal(SIGTSTP, onsusp); | |
218 | catch_susp++; | |
219 | } | |
220 | stty (fileno(stderr), &otty); | |
221 | } | |
222 | if (no_intty) { | |
223 | if (no_tty) | |
224 | copy_file (stdin); | |
225 | else { | |
226 | if ((ch = Getc (f)) == '\f') | |
227 | doclear(); | |
228 | else { | |
229 | Ungetc (ch, f); | |
230 | if (noscroll && (ch != EOF)) { | |
231 | if (clreol) | |
232 | home (); | |
233 | else | |
234 | doclear (); | |
235 | } | |
236 | } | |
237 | if (srchopt) | |
238 | { | |
239 | search (initbuf, stdin, 1); | |
240 | if (noscroll) | |
241 | left--; | |
242 | } | |
243 | else if (initopt) | |
244 | skiplns (initline, stdin); | |
245 | screen (stdin, left); | |
246 | } | |
247 | no_intty = 0; | |
248 | prnames++; | |
249 | firstf = 0; | |
250 | } | |
251 | ||
252 | while (fnum < nfiles) { | |
253 | if ((f = checkf (fnames[fnum], &clearit)) != NULL) { | |
254 | context.line = context.chrctr = 0; | |
255 | Currline = 0; | |
256 | if (firstf) setjmp (restore); | |
257 | if (firstf) { | |
258 | firstf = 0; | |
259 | if (srchopt) | |
260 | { | |
261 | search (initbuf, f, 1); | |
262 | if (noscroll) | |
263 | left--; | |
264 | } | |
265 | else if (initopt) | |
266 | skiplns (initline, f); | |
267 | } | |
268 | else if (fnum < nfiles && !no_tty) { | |
269 | setjmp (restore); | |
270 | left = command (fnames[fnum], f); | |
271 | } | |
272 | if (left != 0) { | |
273 | if ((noscroll || clearit) && (file_size != LONG_MAX)) | |
274 | if (clreol) | |
275 | home (); | |
276 | else | |
277 | doclear (); | |
278 | if (prnames) { | |
279 | if (bad_so) | |
280 | erase (0); | |
281 | if (clreol) | |
282 | cleareol (); | |
283 | pr("::::::::::::::"); | |
284 | if (promptlen > 14) | |
285 | erase (14); | |
286 | prtf ("\n"); | |
287 | if(clreol) cleareol(); | |
288 | prtf("%s\n", fnames[fnum]); | |
289 | if(clreol) cleareol(); | |
290 | prtf("::::::::::::::\n"); | |
291 | if (left > Lpp - 4) | |
292 | left = Lpp - 4; | |
293 | } | |
294 | if (no_tty) | |
295 | copy_file (f); | |
296 | else { | |
297 | within++; | |
298 | screen(f, left); | |
299 | within = 0; | |
300 | } | |
301 | } | |
302 | setjmp (restore); | |
303 | fflush(stdout); | |
304 | fclose(f); | |
305 | screen_start.line = screen_start.chrctr = 0L; | |
306 | context.line = context.chrctr = 0L; | |
307 | } | |
308 | fnum++; | |
309 | firstf = 0; | |
310 | } | |
311 | reset_tty (); | |
312 | exit(0); | |
313 | } | |
314 | ||
315 | argscan(s) | |
316 | char *s; | |
317 | { | |
318 | int seen_num = 0; | |
319 | ||
320 | while (*s != '\0') { | |
321 | switch (*s) { | |
322 | case '0': case '1': case '2': | |
323 | case '3': case '4': case '5': | |
324 | case '6': case '7': case '8': | |
325 | case '9': | |
326 | if (!seen_num) { | |
327 | dlines = 0; | |
328 | seen_num = 1; | |
329 | } | |
330 | dlines = dlines*10 + *s - '0'; | |
331 | break; | |
332 | case 'd': | |
333 | dum_opt = 1; | |
334 | break; | |
335 | case 'l': | |
336 | stop_opt = 0; | |
337 | break; | |
338 | case 'f': | |
339 | fold_opt = 0; | |
340 | break; | |
341 | case 'p': | |
342 | noscroll++; | |
343 | break; | |
344 | case 'c': | |
345 | clreol++; | |
346 | break; | |
347 | case 's': | |
348 | ssp_opt = 1; | |
349 | break; | |
350 | case 'u': | |
351 | ul_opt = 0; | |
352 | break; | |
353 | } | |
354 | s++; | |
355 | } | |
356 | } | |
357 | ||
358 | ||
359 | /* | |
360 | ** Check whether the file named by fs is an ASCII file which the user may | |
361 | ** access. If it is, return the opened file. Otherwise return NULL. | |
362 | */ | |
363 | ||
364 | FILE * | |
365 | checkf (fs, clearfirst) | |
366 | register char *fs; | |
367 | int *clearfirst; | |
368 | { | |
369 | struct stat stbuf; | |
370 | register FILE *f; | |
371 | char c; | |
372 | ||
373 | if (stat (fs, &stbuf) == -1) { | |
374 | (void)fflush(stdout); | |
375 | if (clreol) | |
376 | cleareol (); | |
377 | perror(fs); | |
378 | return((FILE *)NULL); | |
379 | } | |
380 | if ((stbuf.st_mode & S_IFMT) == S_IFDIR) { | |
381 | prtf("\n*** %s: directory ***\n\n", fs); | |
382 | return((FILE *)NULL); | |
383 | } | |
384 | if ((f = Fopen(fs, "r")) == NULL) { | |
385 | (void)fflush(stdout); | |
386 | perror(fs); | |
387 | return((FILE *)NULL); | |
388 | } | |
389 | if (magic(f, fs)) | |
390 | return((FILE *)NULL); | |
391 | c = Getc(f); | |
392 | *clearfirst = c == '\f'; | |
393 | Ungetc (c, f); | |
394 | if ((file_size = stbuf.st_size) == 0) | |
395 | file_size = LONG_MAX; | |
396 | return(f); | |
397 | } | |
398 | ||
399 | /* | |
400 | * magic -- | |
401 | * check for file magic numbers. This code would best be shared with | |
402 | * the file(1) program or, perhaps, more should not try and be so smart? | |
403 | */ | |
404 | magic(f, fs) | |
405 | FILE *f; | |
406 | char *fs; | |
407 | { | |
408 | struct exec ex; | |
409 | ||
410 | if (fread(&ex, sizeof(ex), 1, f) == 1) | |
411 | switch(ex.a_magic) { | |
412 | case OMAGIC: | |
413 | case NMAGIC: | |
414 | case ZMAGIC: | |
415 | case 0405: | |
416 | case 0411: | |
417 | case 0177545: | |
418 | prtf("\n******** %s: Not a text file ********\n\n", fs); | |
419 | (void)fclose(f); | |
420 | return(1); | |
421 | } | |
422 | (void)fseek(f, 0L, L_SET); /* rewind() not necessary */ | |
423 | return(0); | |
424 | } | |
425 | ||
426 | /* | |
427 | ** A real function, for the tputs routine in termlib | |
428 | */ | |
429 | ||
430 | putch (ch) | |
431 | char ch; | |
432 | { | |
433 | putchar (ch); | |
434 | } | |
435 | ||
436 | /* | |
437 | ** Print out the contents of the file f, one screenful at a time. | |
438 | */ | |
439 | ||
440 | #define STOP -10 | |
441 | ||
442 | screen (f, num_lines) | |
443 | register FILE *f; | |
444 | register int num_lines; | |
445 | { | |
446 | register int c; | |
447 | register int nchars; | |
448 | int length; /* length of current line */ | |
449 | static int prev_len = 1; /* length of previous line */ | |
450 | ||
451 | for (;;) { | |
452 | while (num_lines > 0 && !Pause) { | |
453 | if ((nchars = getline (f, &length)) == EOF) | |
454 | { | |
455 | if (clreol) | |
456 | clreos(); | |
457 | return; | |
458 | } | |
459 | if (ssp_opt && length == 0 && prev_len == 0) | |
460 | continue; | |
461 | prev_len = length; | |
462 | if (bad_so || (Senter && *Senter == ' ') && promptlen > 0) | |
463 | erase (0); | |
464 | /* must clear before drawing line since tabs on some terminals | |
465 | * do not erase what they tab over. | |
466 | */ | |
467 | if (clreol) | |
468 | cleareol (); | |
469 | prbuf (Line, length); | |
470 | if (nchars < promptlen) | |
471 | erase (nchars); /* erase () sets promptlen to 0 */ | |
472 | else promptlen = 0; | |
473 | /* is this needed? | |
474 | * if (clreol) | |
475 | * cleareol(); /* must clear again in case we wrapped * | |
476 | */ | |
477 | if (nchars < Mcol || !fold_opt) | |
478 | prbuf("\n", 1); /* will turn off UL if necessary */ | |
479 | if (nchars == STOP) | |
480 | break; | |
481 | num_lines--; | |
482 | } | |
483 | if (pstate) { | |
484 | tputs(ULexit, 1, putch); | |
485 | pstate = 0; | |
486 | } | |
487 | fflush(stdout); | |
488 | if ((c = Getc(f)) == EOF) | |
489 | { | |
490 | if (clreol) | |
491 | clreos (); | |
492 | return; | |
493 | } | |
494 | ||
495 | if (Pause && clreol) | |
496 | clreos (); | |
497 | Ungetc (c, f); | |
498 | setjmp (restore); | |
499 | Pause = 0; startup = 0; | |
500 | if ((num_lines = command (NULL, f)) == 0) | |
501 | return; | |
502 | if (hard && promptlen > 0) | |
503 | erase (0); | |
504 | if (noscroll && num_lines >= dlines) | |
505 | { | |
506 | if (clreol) | |
507 | home(); | |
508 | else | |
509 | doclear (); | |
510 | } | |
511 | screen_start.line = Currline; | |
512 | screen_start.chrctr = Ftell (f); | |
513 | } | |
514 | } | |
515 | ||
516 | /* | |
517 | ** Come here if a quit signal is received | |
518 | */ | |
519 | ||
520 | void | |
521 | onquit() | |
522 | { | |
523 | signal(SIGQUIT, SIG_IGN); | |
524 | if (!inwait) { | |
525 | putchar ('\n'); | |
526 | if (!startup) { | |
527 | signal(SIGQUIT, onquit); | |
528 | longjmp (restore, 1); | |
529 | } | |
530 | else | |
531 | Pause++; | |
532 | } | |
533 | else if (!dum_opt && notell) { | |
534 | write (2, "[Use q or Q to quit]", 20); | |
535 | promptlen += 20; | |
536 | notell = 0; | |
537 | } | |
538 | signal(SIGQUIT, onquit); | |
539 | } | |
540 | ||
541 | /* | |
542 | ** Come here if a signal for a window size change is received | |
543 | */ | |
544 | ||
545 | void | |
546 | chgwinsz() | |
547 | { | |
548 | struct winsize win; | |
549 | ||
550 | (void) signal(SIGWINCH, SIG_IGN); | |
551 | if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) { | |
552 | if (win.ws_row != 0) { | |
553 | Lpp = win.ws_row; | |
554 | nscroll = Lpp/2 - 1; | |
555 | if (nscroll <= 0) | |
556 | nscroll = 1; | |
557 | dlines = Lpp - (noscroll ? 1 : 2); | |
558 | } | |
559 | if (win.ws_col != 0) | |
560 | Mcol = win.ws_col; | |
561 | } | |
562 | (void) signal(SIGWINCH, chgwinsz); | |
563 | } | |
564 | ||
565 | /* | |
566 | ** Clean up terminal state and exit. Also come here if interrupt signal received | |
567 | */ | |
568 | ||
569 | void | |
570 | end_it () | |
571 | { | |
572 | ||
573 | reset_tty (); | |
574 | if (clreol) { | |
575 | putchar ('\r'); | |
576 | clreos (); | |
577 | fflush (stdout); | |
578 | } | |
579 | else if (!clreol && (promptlen > 0)) { | |
580 | kill_line (); | |
581 | fflush (stdout); | |
582 | } | |
583 | else | |
584 | write (2, "\n", 1); | |
585 | _exit(0); | |
586 | } | |
587 | ||
588 | copy_file(f) | |
589 | register FILE *f; | |
590 | { | |
591 | register int c; | |
592 | ||
593 | while ((c = getc(f)) != EOF) | |
594 | putchar(c); | |
595 | } | |
596 | ||
597 | /* Simplified printf function */ | |
598 | ||
599 | prtf (fmt, va_alist) | |
600 | register char *fmt; | |
601 | va_dcl | |
602 | { | |
603 | va_list ap; | |
604 | register char ch; | |
605 | register int ccount; | |
606 | ||
607 | ccount = 0; | |
608 | va_start(ap); | |
609 | while (*fmt) { | |
610 | while ((ch = *fmt++) != '%') { | |
611 | if (ch == '\0') | |
612 | return (ccount); | |
613 | ccount++; | |
614 | putchar (ch); | |
615 | } | |
616 | switch (*fmt++) { | |
617 | case 'd': | |
618 | ccount += printd (va_arg(ap, int)); | |
619 | break; | |
620 | case 's': | |
621 | ccount += pr (va_arg(ap, char *)); | |
622 | break; | |
623 | case '%': | |
624 | ccount++; | |
625 | putchar ('%'); | |
626 | break; | |
627 | case '0': | |
628 | return (ccount); | |
629 | default: | |
630 | break; | |
631 | } | |
632 | } | |
633 | va_end(ap); | |
634 | return (ccount); | |
635 | ||
636 | } | |
637 | ||
638 | /* | |
639 | ** Print an integer as a string of decimal digits, | |
640 | ** returning the length of the print representation. | |
641 | */ | |
642 | ||
643 | printd (n) | |
644 | int n; | |
645 | { | |
646 | int a, nchars; | |
647 | ||
648 | if (a = n/10) | |
649 | nchars = 1 + printd(a); | |
650 | else | |
651 | nchars = 1; | |
652 | putchar (n % 10 + '0'); | |
653 | return (nchars); | |
654 | } | |
655 | ||
656 | /* Put the print representation of an integer into a string */ | |
657 | static char *sptr; | |
658 | ||
659 | scanstr (n, str) | |
660 | int n; | |
661 | char *str; | |
662 | { | |
663 | sptr = str; | |
664 | Sprintf (n); | |
665 | *sptr = '\0'; | |
666 | } | |
667 | ||
668 | Sprintf (n) | |
669 | { | |
670 | int a; | |
671 | ||
672 | if (a = n/10) | |
673 | Sprintf (a); | |
674 | *sptr++ = n % 10 + '0'; | |
675 | } | |
676 | ||
677 | static char bell = ctrl('G'); | |
678 | ||
679 | strlen (s) | |
680 | char *s; | |
681 | { | |
682 | register char *p; | |
683 | ||
684 | p = s; | |
685 | while (*p++) | |
686 | ; | |
687 | return (p - s - 1); | |
688 | } | |
689 | ||
690 | /* See whether the last component of the path name "path" is equal to the | |
691 | ** string "string" | |
692 | */ | |
693 | ||
694 | tailequ (path, string) | |
695 | char *path; | |
696 | register char *string; | |
697 | { | |
698 | register char *tail; | |
699 | ||
700 | tail = path + strlen(path); | |
701 | while (tail >= path) | |
702 | if (*(--tail) == '/') | |
703 | break; | |
704 | ++tail; | |
705 | while (*tail++ == *string++) | |
706 | if (*tail == '\0') | |
707 | return(1); | |
708 | return(0); | |
709 | } | |
710 | ||
711 | prompt (filename) | |
712 | char *filename; | |
713 | { | |
714 | if (clreol) | |
715 | cleareol (); | |
716 | else if (promptlen > 0) | |
717 | kill_line (); | |
718 | if (!hard) { | |
719 | promptlen = 8; | |
720 | if (Senter && Sexit) { | |
721 | tputs (Senter, 1, putch); | |
722 | promptlen += (2 * soglitch); | |
723 | } | |
724 | if (clreol) | |
725 | cleareol (); | |
726 | pr("--More--"); | |
727 | if (filename != NULL) { | |
728 | promptlen += prtf ("(Next file: %s)", filename); | |
729 | } | |
730 | else if (!no_intty) { | |
731 | promptlen += prtf ("(%d%%)", (int)((file_pos * 100) / file_size)); | |
732 | } | |
733 | if (dum_opt) { | |
734 | promptlen += pr("[Press space to continue, 'q' to quit.]"); | |
735 | } | |
736 | if (Senter && Sexit) | |
737 | tputs (Sexit, 1, putch); | |
738 | if (clreol) | |
739 | clreos (); | |
740 | fflush(stdout); | |
741 | } | |
742 | else | |
743 | write (2, &bell, 1); | |
744 | inwait++; | |
745 | } | |
746 | ||
747 | /* | |
748 | ** Get a logical line | |
749 | */ | |
750 | ||
751 | getline(f, length) | |
752 | register FILE *f; | |
753 | int *length; | |
754 | { | |
755 | register int c; | |
756 | register char *p; | |
757 | register int column; | |
758 | static int colflg; | |
759 | ||
760 | p = Line; | |
761 | column = 0; | |
762 | c = Getc (f); | |
763 | if (colflg && c == '\n') { | |
764 | Currline++; | |
765 | c = Getc (f); | |
766 | } | |
767 | while (p < &Line[LINSIZ - 1]) { | |
768 | if (c == EOF) { | |
769 | if (p > Line) { | |
770 | *p = '\0'; | |
771 | *length = p - Line; | |
772 | return (column); | |
773 | } | |
774 | *length = p - Line; | |
775 | return (EOF); | |
776 | } | |
777 | if (c == '\n') { | |
778 | Currline++; | |
779 | break; | |
780 | } | |
781 | *p++ = c; | |
782 | if (c == '\t') | |
783 | if (!hardtabs || column < promptlen && !hard) { | |
784 | if (hardtabs && eraseln && !dumb) { | |
785 | column = 1 + (column | 7); | |
786 | tputs (eraseln, 1, putch); | |
787 | promptlen = 0; | |
788 | } | |
789 | else { | |
790 | for (--p; p < &Line[LINSIZ - 1];) { | |
791 | *p++ = ' '; | |
792 | if ((++column & 7) == 0) | |
793 | break; | |
794 | } | |
795 | if (column >= promptlen) promptlen = 0; | |
796 | } | |
797 | } | |
798 | else | |
799 | column = 1 + (column | 7); | |
800 | else if (c == '\b' && column > 0) | |
801 | column--; | |
802 | else if (c == '\r') | |
803 | column = 0; | |
804 | else if (c == '\f' && stop_opt) { | |
805 | p[-1] = '^'; | |
806 | *p++ = 'L'; | |
807 | column += 2; | |
808 | Pause++; | |
809 | } | |
810 | else if (c == EOF) { | |
811 | *length = p - Line; | |
812 | return (column); | |
813 | } | |
814 | else if (c >= ' ' && c != RUBOUT) | |
815 | column++; | |
816 | if (column >= Mcol && fold_opt) break; | |
817 | c = Getc (f); | |
818 | } | |
819 | if (column >= Mcol && Mcol > 0) { | |
820 | if (!Wrap) { | |
821 | *p++ = '\n'; | |
822 | } | |
823 | } | |
824 | colflg = column == Mcol && fold_opt; | |
825 | if (colflg && eatnl && Wrap) { | |
826 | *p++ = '\n'; /* simulate normal wrap */ | |
827 | } | |
828 | *length = p - Line; | |
829 | *p = 0; | |
830 | return (column); | |
831 | } | |
832 | ||
833 | /* | |
834 | ** Erase the rest of the prompt, assuming we are starting at column col. | |
835 | */ | |
836 | ||
837 | erase (col) | |
838 | register int col; | |
839 | { | |
840 | ||
841 | if (promptlen == 0) | |
842 | return; | |
843 | if (hard) { | |
844 | putchar ('\n'); | |
845 | } | |
846 | else { | |
847 | if (col == 0) | |
848 | putchar ('\r'); | |
849 | if (!dumb && eraseln) | |
850 | tputs (eraseln, 1, putch); | |
851 | else | |
852 | for (col = promptlen - col; col > 0; col--) | |
853 | putchar (' '); | |
854 | } | |
855 | promptlen = 0; | |
856 | } | |
857 | ||
858 | /* | |
859 | ** Erase the current line entirely | |
860 | */ | |
861 | ||
862 | kill_line () | |
863 | { | |
864 | erase (0); | |
865 | if (!eraseln || dumb) putchar ('\r'); | |
866 | } | |
867 | ||
868 | /* | |
869 | * force clear to end of line | |
870 | */ | |
871 | cleareol() | |
872 | { | |
873 | tputs(eraseln, 1, putch); | |
874 | } | |
875 | ||
876 | clreos() | |
877 | { | |
878 | tputs(EodClr, 1, putch); | |
879 | } | |
880 | ||
881 | /* | |
882 | ** Print string and return number of characters | |
883 | */ | |
884 | ||
885 | pr(s1) | |
886 | char *s1; | |
887 | { | |
888 | register char *s; | |
889 | register char c; | |
890 | ||
891 | for (s = s1; c = *s++; ) | |
892 | putchar(c); | |
893 | return (s - s1 - 1); | |
894 | } | |
895 | ||
896 | ||
897 | /* Print a buffer of n characters */ | |
898 | ||
899 | prbuf (s, n) | |
900 | register char *s; | |
901 | register int n; | |
902 | { | |
903 | register char c; /* next output character */ | |
904 | register int state; /* next output char's UL state */ | |
905 | #define wouldul(s,n) ((n) >= 2 && (((s)[0] == '_' && (s)[1] == '\b') || ((s)[1] == '\b' && (s)[2] == '_'))) | |
906 | ||
907 | while (--n >= 0) | |
908 | if (!ul_opt) | |
909 | putchar (*s++); | |
910 | else { | |
911 | if (*s == ' ' && pstate == 0 && ulglitch && wouldul(s+1, n-1)) { | |
912 | s++; | |
913 | continue; | |
914 | } | |
915 | if (state = wouldul(s, n)) { | |
916 | c = (*s == '_')? s[2] : *s ; | |
917 | n -= 2; | |
918 | s += 3; | |
919 | } else | |
920 | c = *s++; | |
921 | if (state != pstate) { | |
922 | if (c == ' ' && state == 0 && ulglitch && wouldul(s, n-1)) | |
923 | state = 1; | |
924 | else | |
925 | tputs(state ? ULenter : ULexit, 1, putch); | |
926 | } | |
927 | if (c != ' ' || pstate == 0 || state != 0 || ulglitch == 0) | |
928 | putchar(c); | |
929 | if (state && *chUL) { | |
930 | pr(chBS); | |
931 | tputs(chUL, 1, putch); | |
932 | } | |
933 | pstate = state; | |
934 | } | |
935 | } | |
936 | ||
937 | /* | |
938 | ** Clear the screen | |
939 | */ | |
940 | ||
941 | doclear() | |
942 | { | |
943 | if (Clear && !hard) { | |
944 | tputs(Clear, 1, putch); | |
945 | ||
946 | /* Put out carriage return so that system doesn't | |
947 | ** get confused by escape sequences when expanding tabs | |
948 | */ | |
949 | putchar ('\r'); | |
950 | promptlen = 0; | |
951 | } | |
952 | } | |
953 | ||
954 | /* | |
955 | * Go to home position | |
956 | */ | |
957 | home() | |
958 | { | |
959 | tputs(Home,1,putch); | |
960 | } | |
961 | ||
962 | static int lastcmd, lastarg, lastp; | |
963 | static int lastcolon; | |
964 | char shell_line[132]; | |
965 | ||
966 | /* | |
967 | ** Read a command and do it. A command consists of an optional integer | |
968 | ** argument followed by the command character. Return the number of lines | |
969 | ** to display in the next screenful. If there is nothing more to display | |
970 | ** in the current file, zero is returned. | |
971 | */ | |
972 | ||
973 | command (filename, f) | |
974 | char *filename; | |
975 | register FILE *f; | |
976 | { | |
977 | register int nlines; | |
978 | register int retval; | |
979 | register char c; | |
980 | char colonch; | |
981 | FILE *helpf; | |
982 | int done; | |
983 | char comchar, cmdbuf[80], *p; | |
984 | ||
985 | #define ret(val) retval=val;done++;break | |
986 | ||
987 | done = 0; | |
988 | if (!errors) | |
989 | prompt (filename); | |
990 | else | |
991 | errors = 0; | |
992 | if (MBIT == RAW && slow_tty) { | |
993 | otty.sg_flags |= MBIT; | |
994 | stty(fileno(stderr), &otty); | |
995 | } | |
996 | for (;;) { | |
997 | nlines = number (&comchar); | |
998 | lastp = colonch = 0; | |
999 | if (comchar == '.') { /* Repeat last command */ | |
1000 | lastp++; | |
1001 | comchar = lastcmd; | |
1002 | nlines = lastarg; | |
1003 | if (lastcmd == ':') | |
1004 | colonch = lastcolon; | |
1005 | } | |
1006 | lastcmd = comchar; | |
1007 | lastarg = nlines; | |
1008 | if (comchar == otty.sg_erase) { | |
1009 | kill_line (); | |
1010 | prompt (filename); | |
1011 | continue; | |
1012 | } | |
1013 | switch (comchar) { | |
1014 | case ':': | |
1015 | retval = colon (filename, colonch, nlines); | |
1016 | if (retval >= 0) | |
1017 | done++; | |
1018 | break; | |
1019 | case 'b': | |
1020 | case ctrl('B'): | |
1021 | { | |
1022 | register int initline; | |
1023 | ||
1024 | if (no_intty) { | |
1025 | write(2, &bell, 1); | |
1026 | return (-1); | |
1027 | } | |
1028 | ||
1029 | if (nlines == 0) nlines++; | |
1030 | ||
1031 | putchar ('\r'); | |
1032 | erase (0); | |
1033 | prtf ("\n"); | |
1034 | if (clreol) | |
1035 | cleareol (); | |
1036 | prtf ("...back %d page", nlines); | |
1037 | if (nlines > 1) | |
1038 | pr ("s\n"); | |
1039 | else | |
1040 | pr ("\n"); | |
1041 | ||
1042 | if (clreol) | |
1043 | cleareol (); | |
1044 | pr ("\n"); | |
1045 | ||
1046 | initline = Currline - dlines * (nlines + 1); | |
1047 | if (! noscroll) | |
1048 | --initline; | |
1049 | if (initline < 0) initline = 0; | |
1050 | Fseek(f, 0L); | |
1051 | Currline = 0; /* skiplns() will make Currline correct */ | |
1052 | skiplns(initline, f); | |
1053 | if (! noscroll) { | |
1054 | ret(dlines + 1); | |
1055 | } | |
1056 | else { | |
1057 | ret(dlines); | |
1058 | } | |
1059 | } | |
1060 | case ' ': | |
1061 | case 'z': | |
1062 | if (nlines == 0) nlines = dlines; | |
1063 | else if (comchar == 'z') dlines = nlines; | |
1064 | ret (nlines); | |
1065 | case 'd': | |
1066 | case ctrl('D'): | |
1067 | if (nlines != 0) nscroll = nlines; | |
1068 | ret (nscroll); | |
1069 | case 'q': | |
1070 | case 'Q': | |
1071 | end_it (); | |
1072 | case 's': | |
1073 | case 'f': | |
1074 | if (nlines == 0) nlines++; | |
1075 | if (comchar == 'f') | |
1076 | nlines *= dlines; | |
1077 | putchar ('\r'); | |
1078 | erase (0); | |
1079 | prtf ("\n"); | |
1080 | if (clreol) | |
1081 | cleareol (); | |
1082 | prtf ("...skipping %d line", nlines); | |
1083 | if (nlines > 1) | |
1084 | pr ("s\n"); | |
1085 | else | |
1086 | pr ("\n"); | |
1087 | ||
1088 | if (clreol) | |
1089 | cleareol (); | |
1090 | pr ("\n"); | |
1091 | ||
1092 | while (nlines > 0) { | |
1093 | while ((c = Getc (f)) != '\n') | |
1094 | if (c == EOF) { | |
1095 | retval = 0; | |
1096 | done++; | |
1097 | goto endsw; | |
1098 | } | |
1099 | Currline++; | |
1100 | nlines--; | |
1101 | } | |
1102 | ret (dlines); | |
1103 | case '\n': | |
1104 | if (nlines != 0) | |
1105 | dlines = nlines; | |
1106 | else | |
1107 | nlines = 1; | |
1108 | ret (nlines); | |
1109 | case '\f': | |
1110 | if (!no_intty) { | |
1111 | doclear (); | |
1112 | Fseek (f, screen_start.chrctr); | |
1113 | Currline = screen_start.line; | |
1114 | ret (dlines); | |
1115 | } | |
1116 | else { | |
1117 | write (2, &bell, 1); | |
1118 | break; | |
1119 | } | |
1120 | case '\'': | |
1121 | if (!no_intty) { | |
1122 | kill_line (); | |
1123 | pr ("\n***Back***\n\n"); | |
1124 | Fseek (f, context.chrctr); | |
1125 | Currline = context.line; | |
1126 | ret (dlines); | |
1127 | } | |
1128 | else { | |
1129 | write (2, &bell, 1); | |
1130 | break; | |
1131 | } | |
1132 | case '=': | |
1133 | kill_line (); | |
1134 | promptlen = printd (Currline); | |
1135 | fflush (stdout); | |
1136 | break; | |
1137 | case 'n': | |
1138 | lastp++; | |
1139 | case '/': | |
1140 | if (nlines == 0) nlines++; | |
1141 | kill_line (); | |
1142 | pr ("/"); | |
1143 | promptlen = 1; | |
1144 | fflush (stdout); | |
1145 | if (lastp) { | |
1146 | write (2,"\r", 1); | |
1147 | search (NULL, f, nlines); /* Use previous r.e. */ | |
1148 | } | |
1149 | else { | |
1150 | ttyin (cmdbuf, 78, '/'); | |
1151 | write (2, "\r", 1); | |
1152 | search (cmdbuf, f, nlines); | |
1153 | } | |
1154 | ret (dlines-1); | |
1155 | case '!': | |
1156 | do_shell (filename); | |
1157 | break; | |
1158 | case '?': | |
1159 | case 'h': | |
1160 | if ((helpf = fopen (HELPFILE, "r")) == NULL) | |
1161 | error ("Can't open help file"); | |
1162 | if (noscroll) doclear (); | |
1163 | copy_file (helpf); | |
1164 | fclose (helpf); | |
1165 | prompt (filename); | |
1166 | break; | |
1167 | case 'v': /* This case should go right before default */ | |
1168 | if (!no_intty) { | |
1169 | kill_line (); | |
1170 | cmdbuf[0] = '+'; | |
1171 | scanstr (Currline - dlines < 0 ? 0 | |
1172 | : Currline - (dlines + 1) / 2, &cmdbuf[1]); | |
1173 | pr ("vi "); pr (cmdbuf); putchar (' '); pr (fnames[fnum]); | |
1174 | execute (filename, _PATH_VI, "vi", cmdbuf, fnames[fnum], 0); | |
1175 | break; | |
1176 | } | |
1177 | default: | |
1178 | if (dum_opt) { | |
1179 | kill_line (); | |
1180 | if (Senter && Sexit) { | |
1181 | tputs (Senter, 1, putch); | |
1182 | promptlen = pr ("[Press 'h' for instructions.]") + (2 * soglitch); | |
1183 | tputs (Sexit, 1, putch); | |
1184 | } | |
1185 | else | |
1186 | promptlen = pr ("[Press 'h' for instructions.]"); | |
1187 | fflush (stdout); | |
1188 | } | |
1189 | else | |
1190 | write (2, &bell, 1); | |
1191 | break; | |
1192 | } | |
1193 | if (done) break; | |
1194 | } | |
1195 | putchar ('\r'); | |
1196 | endsw: | |
1197 | inwait = 0; | |
1198 | notell++; | |
1199 | if (MBIT == RAW && slow_tty) { | |
1200 | otty.sg_flags &= ~MBIT; | |
1201 | stty(fileno(stderr), &otty); | |
1202 | } | |
1203 | return (retval); | |
1204 | } | |
1205 | ||
1206 | char ch; | |
1207 | ||
1208 | /* | |
1209 | * Execute a colon-prefixed command. | |
1210 | * Returns <0 if not a command that should cause | |
1211 | * more of the file to be printed. | |
1212 | */ | |
1213 | ||
1214 | colon (filename, cmd, nlines) | |
1215 | char *filename; | |
1216 | int cmd; | |
1217 | int nlines; | |
1218 | { | |
1219 | if (cmd == 0) | |
1220 | ch = readch (); | |
1221 | else | |
1222 | ch = cmd; | |
1223 | lastcolon = ch; | |
1224 | switch (ch) { | |
1225 | case 'f': | |
1226 | kill_line (); | |
1227 | if (!no_intty) | |
1228 | promptlen = prtf ("\"%s\" line %d", fnames[fnum], Currline); | |
1229 | else | |
1230 | promptlen = prtf ("[Not a file] line %d", Currline); | |
1231 | fflush (stdout); | |
1232 | return (-1); | |
1233 | case 'n': | |
1234 | if (nlines == 0) { | |
1235 | if (fnum >= nfiles - 1) | |
1236 | end_it (); | |
1237 | nlines++; | |
1238 | } | |
1239 | putchar ('\r'); | |
1240 | erase (0); | |
1241 | skipf (nlines); | |
1242 | return (0); | |
1243 | case 'p': | |
1244 | if (no_intty) { | |
1245 | write (2, &bell, 1); | |
1246 | return (-1); | |
1247 | } | |
1248 | putchar ('\r'); | |
1249 | erase (0); | |
1250 | if (nlines == 0) | |
1251 | nlines++; | |
1252 | skipf (-nlines); | |
1253 | return (0); | |
1254 | case '!': | |
1255 | do_shell (filename); | |
1256 | return (-1); | |
1257 | case 'q': | |
1258 | case 'Q': | |
1259 | end_it (); | |
1260 | default: | |
1261 | write (2, &bell, 1); | |
1262 | return (-1); | |
1263 | } | |
1264 | } | |
1265 | ||
1266 | /* | |
1267 | ** Read a decimal number from the terminal. Set cmd to the non-digit which | |
1268 | ** terminates the number. | |
1269 | */ | |
1270 | ||
1271 | number(cmd) | |
1272 | char *cmd; | |
1273 | { | |
1274 | register int i; | |
1275 | ||
1276 | i = 0; ch = otty.sg_kill; | |
1277 | for (;;) { | |
1278 | ch = readch (); | |
1279 | if (ch >= '0' && ch <= '9') | |
1280 | i = i*10 + ch - '0'; | |
1281 | else if (ch == otty.sg_kill) | |
1282 | i = 0; | |
1283 | else { | |
1284 | *cmd = ch; | |
1285 | break; | |
1286 | } | |
1287 | } | |
1288 | return (i); | |
1289 | } | |
1290 | ||
1291 | do_shell (filename) | |
1292 | char *filename; | |
1293 | { | |
1294 | char cmdbuf[80]; | |
1295 | ||
1296 | kill_line (); | |
1297 | pr ("!"); | |
1298 | fflush (stdout); | |
1299 | promptlen = 1; | |
1300 | if (lastp) | |
1301 | pr (shell_line); | |
1302 | else { | |
1303 | ttyin (cmdbuf, 78, '!'); | |
1304 | if (expand (shell_line, cmdbuf)) { | |
1305 | kill_line (); | |
1306 | promptlen = prtf ("!%s", shell_line); | |
1307 | } | |
1308 | } | |
1309 | fflush (stdout); | |
1310 | write (2, "\n", 1); | |
1311 | promptlen = 0; | |
1312 | shellp = 1; | |
1313 | execute (filename, shell, shell, "-c", shell_line, 0); | |
1314 | } | |
1315 | ||
1316 | /* | |
1317 | ** Search for nth ocurrence of regular expression contained in buf in the file | |
1318 | */ | |
1319 | ||
1320 | search (buf, file, n) | |
1321 | char buf[]; | |
1322 | FILE *file; | |
1323 | register int n; | |
1324 | { | |
1325 | long startline = Ftell (file); | |
1326 | register long line1 = startline; | |
1327 | register long line2 = startline; | |
1328 | register long line3 = startline; | |
1329 | register int lncount; | |
1330 | int saveln, rv; | |
1331 | regexp *s; | |
1332 | ||
1333 | context.line = saveln = Currline; | |
1334 | context.chrctr = startline; | |
1335 | lncount = 0; | |
1336 | if (buf) | |
1337 | previous = s = regcomp (buf); | |
1338 | else | |
1339 | s = previous; | |
1340 | while (!feof (file)) { | |
1341 | line3 = line2; | |
1342 | line2 = line1; | |
1343 | line1 = Ftell (file); | |
1344 | rdline (file); | |
1345 | lncount++; | |
1346 | if ((rv = regexec (s, Line)) == 1) | |
1347 | if (--n == 0) { | |
1348 | if (lncount > 3 || (lncount > 1 && no_intty)) | |
1349 | { | |
1350 | pr ("\n"); | |
1351 | if (clreol) | |
1352 | cleareol (); | |
1353 | pr("...skipping\n"); | |
1354 | } | |
1355 | if (!no_intty) { | |
1356 | Currline -= (lncount >= 3 ? 3 : lncount); | |
1357 | Fseek (file, line3); | |
1358 | if (noscroll) | |
1359 | if (clreol) { | |
1360 | home (); | |
1361 | cleareol (); | |
1362 | } | |
1363 | else | |
1364 | doclear (); | |
1365 | } | |
1366 | else { | |
1367 | kill_line (); | |
1368 | if (noscroll) | |
1369 | if (clreol) { | |
1370 | home (); | |
1371 | cleareol (); | |
1372 | } | |
1373 | else | |
1374 | doclear (); | |
1375 | pr (Line); | |
1376 | putchar ('\n'); | |
1377 | } | |
1378 | break; | |
1379 | } | |
1380 | } | |
1381 | free(s); | |
1382 | if (feof (file)) { | |
1383 | if (!no_intty) { | |
1384 | /* file->_flag &= ~_IOEOF; /* why doesn't fseek do this ??!!??! */ | |
1385 | Currline = saveln; | |
1386 | Fseek (file, startline); | |
1387 | } | |
1388 | else { | |
1389 | pr ("\nPattern not found\n"); | |
1390 | end_it (); | |
1391 | } | |
1392 | error ("Pattern not found"); | |
1393 | } | |
1394 | } | |
1395 | ||
1396 | /*VARARGS2*/ | |
1397 | execute (filename, cmd, va_alist) | |
1398 | char *filename; | |
1399 | char *cmd; | |
1400 | va_dcl | |
1401 | { | |
1402 | int id; | |
1403 | int n; | |
1404 | va_list argp; | |
1405 | ||
1406 | fflush (stdout); | |
1407 | reset_tty (); | |
1408 | for (n = 10; (id = fork ()) < 0 && n > 0; n--) | |
1409 | sleep (5); | |
1410 | if (id == 0) { | |
1411 | if (!isatty(0)) { | |
1412 | close(0); | |
1413 | open("/dev/tty", 0); | |
1414 | } | |
1415 | va_start(argp); | |
1416 | execv (cmd, argp); | |
1417 | write (2, "exec failed\n", 12); | |
1418 | exit (1); | |
1419 | va_end(argp); /* balance {}'s for some UNIX's */ | |
1420 | } | |
1421 | if (id > 0) { | |
1422 | signal (SIGINT, SIG_IGN); | |
1423 | signal (SIGQUIT, SIG_IGN); | |
1424 | if (catch_susp) | |
1425 | signal(SIGTSTP, SIG_DFL); | |
1426 | while (wait(0) > 0); | |
1427 | signal (SIGINT, end_it); | |
1428 | signal (SIGQUIT, onquit); | |
1429 | if (catch_susp) | |
1430 | signal(SIGTSTP, onsusp); | |
1431 | } else | |
1432 | write(2, "can't fork\n", 11); | |
1433 | set_tty (); | |
1434 | pr ("------------------------\n"); | |
1435 | prompt (filename); | |
1436 | } | |
1437 | /* | |
1438 | ** Skip n lines in the file f | |
1439 | */ | |
1440 | ||
1441 | skiplns (n, f) | |
1442 | register int n; | |
1443 | register FILE *f; | |
1444 | { | |
1445 | register char c; | |
1446 | ||
1447 | while (n > 0) { | |
1448 | while ((c = Getc (f)) != '\n') | |
1449 | if (c == EOF) | |
1450 | return; | |
1451 | n--; | |
1452 | Currline++; | |
1453 | } | |
1454 | } | |
1455 | ||
1456 | /* | |
1457 | ** Skip nskip files in the file list (from the command line). Nskip may be | |
1458 | ** negative. | |
1459 | */ | |
1460 | ||
1461 | skipf (nskip) | |
1462 | register int nskip; | |
1463 | { | |
1464 | if (nskip == 0) return; | |
1465 | if (nskip > 0) { | |
1466 | if (fnum + nskip > nfiles - 1) | |
1467 | nskip = nfiles - fnum - 1; | |
1468 | } | |
1469 | else if (within) | |
1470 | ++fnum; | |
1471 | fnum += nskip; | |
1472 | if (fnum < 0) | |
1473 | fnum = 0; | |
1474 | pr ("\n...Skipping "); | |
1475 | pr ("\n"); | |
1476 | if (clreol) | |
1477 | cleareol (); | |
1478 | pr ("...Skipping "); | |
1479 | pr (nskip > 0 ? "to file " : "back to file "); | |
1480 | pr (fnames[fnum]); | |
1481 | pr ("\n"); | |
1482 | if (clreol) | |
1483 | cleareol (); | |
1484 | pr ("\n"); | |
1485 | --fnum; | |
1486 | } | |
1487 | ||
1488 | /*----------------------------- Terminal I/O -------------------------------*/ | |
1489 | ||
1490 | initterm () | |
1491 | { | |
1492 | char buf[TBUFSIZ]; | |
1493 | static char clearbuf[TBUFSIZ]; | |
1494 | char *clearptr, *padstr; | |
1495 | int ldisc; | |
1496 | int lmode; | |
1497 | char *term; | |
1498 | int tgrp; | |
1499 | struct winsize win; | |
1500 | ||
1501 | retry: | |
1502 | if (!(no_tty = ioctl(fileno(stdout), TIOCGETP, &otty))) { | |
1503 | if (ioctl(fileno(stdout), TIOCLGET, &lmode) < 0) { | |
1504 | perror("TIOCLGET"); | |
1505 | exit(1); | |
1506 | } | |
1507 | docrterase = ((lmode & LCRTERA) != 0); | |
1508 | docrtkill = ((lmode & LCRTKIL) != 0); | |
1509 | /* | |
1510 | * Wait until we're in the foreground before we save the | |
1511 | * the terminal modes. | |
1512 | */ | |
1513 | if (ioctl(fileno(stdout), TIOCGPGRP, &tgrp) < 0) { | |
1514 | perror("TIOCGPGRP"); | |
1515 | exit(1); | |
1516 | } | |
1517 | if (tgrp != getpgrp(0)) { | |
1518 | kill(0, SIGTTOU); | |
1519 | goto retry; | |
1520 | } | |
1521 | if ((term = getenv("TERM")) == 0 || tgetent(buf, term) <= 0) { | |
1522 | dumb++; ul_opt = 0; | |
1523 | } | |
1524 | else { | |
1525 | if (ioctl(fileno(stdout), TIOCGWINSZ, &win) < 0) { | |
1526 | Lpp = tgetnum("li"); | |
1527 | Mcol = tgetnum("co"); | |
1528 | } else { | |
1529 | if ((Lpp = win.ws_row) == 0) | |
1530 | Lpp = tgetnum("li"); | |
1531 | if ((Mcol = win.ws_col) == 0) | |
1532 | Mcol = tgetnum("co"); | |
1533 | } | |
1534 | if ((Lpp <= 0) || tgetflag("hc")) { | |
1535 | hard++; /* Hard copy terminal */ | |
1536 | Lpp = 24; | |
1537 | } | |
1538 | if (tgetflag("xn")) | |
1539 | eatnl++; /* Eat newline at last column + 1; dec, concept */ | |
1540 | if (Mcol <= 0) | |
1541 | Mcol = 80; | |
1542 | ||
1543 | if (tailequ (fnames[0], "page") || !hard && tgetflag("ns")) | |
1544 | noscroll++; | |
1545 | Wrap = tgetflag("am"); | |
1546 | bad_so = tgetflag ("xs"); | |
1547 | clearptr = clearbuf; | |
1548 | eraseln = tgetstr("ce",&clearptr); | |
1549 | Clear = tgetstr("cl", &clearptr); | |
1550 | Senter = tgetstr("so", &clearptr); | |
1551 | Sexit = tgetstr("se", &clearptr); | |
1552 | if ((soglitch = tgetnum("sg")) < 0) | |
1553 | soglitch = 0; | |
1554 | ||
1555 | /* | |
1556 | * Set up for underlining: some terminals don't need it; | |
1557 | * others have start/stop sequences, still others have an | |
1558 | * underline char sequence which is assumed to move the | |
1559 | * cursor forward one character. If underline sequence | |
1560 | * isn't available, settle for standout sequence. | |
1561 | */ | |
1562 | ||
1563 | if (tgetflag("ul") || tgetflag("os")) | |
1564 | ul_opt = 0; | |
1565 | if ((chUL = tgetstr("uc", &clearptr)) == NULL ) | |
1566 | chUL = ""; | |
1567 | if (((ULenter = tgetstr("us", &clearptr)) == NULL || | |
1568 | (ULexit = tgetstr("ue", &clearptr)) == NULL) && !*chUL) { | |
1569 | if ((ULenter = Senter) == NULL || (ULexit = Sexit) == NULL) { | |
1570 | ULenter = ""; | |
1571 | ULexit = ""; | |
1572 | } else | |
1573 | ulglitch = soglitch; | |
1574 | } else { | |
1575 | if ((ulglitch = tgetnum("ug")) < 0) | |
1576 | ulglitch = 0; | |
1577 | } | |
1578 | ||
1579 | if (padstr = tgetstr("pc", &clearptr)) | |
1580 | PC = *padstr; | |
1581 | Home = tgetstr("ho",&clearptr); | |
1582 | if (Home == 0 || *Home == '\0') | |
1583 | { | |
1584 | if ((cursorm = tgetstr("cm", &clearptr)) != NULL) { | |
1585 | strcpy(cursorhome, tgoto(cursorm, 0, 0)); | |
1586 | Home = cursorhome; | |
1587 | } | |
1588 | } | |
1589 | EodClr = tgetstr("cd", &clearptr); | |
1590 | if ((chBS = tgetstr("bc", &clearptr)) == NULL) | |
1591 | chBS = "\b"; | |
1592 | ||
1593 | } | |
1594 | if ((shell = getenv("SHELL")) == NULL) | |
1595 | shell = "/bin/sh"; | |
1596 | } | |
1597 | no_intty = ioctl(fileno(stdin), TIOCGETP, &otty); | |
1598 | ioctl(fileno(stderr), TIOCGETP, &otty); | |
1599 | savetty = otty; | |
1600 | ospeed = otty.sg_ospeed; | |
1601 | slow_tty = ospeed < B1200; | |
1602 | hardtabs = (otty.sg_flags & TBDELAY) != XTABS; | |
1603 | if (!no_tty) { | |
1604 | otty.sg_flags &= ~ECHO; | |
1605 | if (MBIT == CBREAK || !slow_tty) | |
1606 | otty.sg_flags |= MBIT; | |
1607 | } | |
1608 | } | |
1609 | ||
1610 | readch () | |
1611 | { | |
1612 | char ch; | |
1613 | extern int errno; | |
1614 | ||
1615 | errno = 0; | |
1616 | if (read (2, &ch, 1) <= 0) | |
1617 | if (errno != EINTR) | |
1618 | end_it(); | |
1619 | else | |
1620 | ch = otty.sg_kill; | |
1621 | return (ch); | |
1622 | } | |
1623 | ||
1624 | static char BS = '\b'; | |
1625 | static char *BSB = "\b \b"; | |
1626 | static char CARAT = '^'; | |
1627 | #define ERASEONECHAR \ | |
1628 | if (docrterase) \ | |
1629 | write (2, BSB, sizeof(BSB)); \ | |
1630 | else \ | |
1631 | write (2, &BS, sizeof(BS)); | |
1632 | ||
1633 | ttyin (buf, nmax, pchar) | |
1634 | char buf[]; | |
1635 | register int nmax; | |
1636 | char pchar; | |
1637 | { | |
1638 | register char *sptr; | |
1639 | register char ch; | |
1640 | register int slash = 0; | |
1641 | int maxlen; | |
1642 | char cbuf; | |
1643 | ||
1644 | sptr = buf; | |
1645 | maxlen = 0; | |
1646 | while (sptr - buf < nmax) { | |
1647 | if (promptlen > maxlen) maxlen = promptlen; | |
1648 | ch = readch (); | |
1649 | if (ch == '\\') { | |
1650 | slash++; | |
1651 | } | |
1652 | else if ((ch == otty.sg_erase) && !slash) { | |
1653 | if (sptr > buf) { | |
1654 | --promptlen; | |
1655 | ERASEONECHAR | |
1656 | --sptr; | |
1657 | if ((*sptr < ' ' && *sptr != '\n') || *sptr == RUBOUT) { | |
1658 | --promptlen; | |
1659 | ERASEONECHAR | |
1660 | } | |
1661 | continue; | |
1662 | } | |
1663 | else { | |
1664 | if (!eraseln) promptlen = maxlen; | |
1665 | longjmp (restore, 1); | |
1666 | } | |
1667 | } | |
1668 | else if ((ch == otty.sg_kill) && !slash) { | |
1669 | if (hard) { | |
1670 | show (ch); | |
1671 | putchar ('\n'); | |
1672 | putchar (pchar); | |
1673 | } | |
1674 | else { | |
1675 | putchar ('\r'); | |
1676 | putchar (pchar); | |
1677 | if (eraseln) | |
1678 | erase (1); | |
1679 | else if (docrtkill) | |
1680 | while (promptlen-- > 1) | |
1681 | write (2, BSB, sizeof(BSB)); | |
1682 | promptlen = 1; | |
1683 | } | |
1684 | sptr = buf; | |
1685 | fflush (stdout); | |
1686 | continue; | |
1687 | } | |
1688 | if (slash && (ch == otty.sg_kill || ch == otty.sg_erase)) { | |
1689 | ERASEONECHAR | |
1690 | --sptr; | |
1691 | } | |
1692 | if (ch != '\\') | |
1693 | slash = 0; | |
1694 | *sptr++ = ch; | |
1695 | if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) { | |
1696 | ch += ch == RUBOUT ? -0100 : 0100; | |
1697 | write (2, &CARAT, 1); | |
1698 | promptlen++; | |
1699 | } | |
1700 | cbuf = ch; | |
1701 | if (ch != '\n' && ch != ESC) { | |
1702 | write (2, &cbuf, 1); | |
1703 | promptlen++; | |
1704 | } | |
1705 | else | |
1706 | break; | |
1707 | } | |
1708 | *--sptr = '\0'; | |
1709 | if (!eraseln) promptlen = maxlen; | |
1710 | if (sptr - buf >= nmax - 1) | |
1711 | error ("Line too long"); | |
1712 | } | |
1713 | ||
1714 | expand (outbuf, inbuf) | |
1715 | char *outbuf; | |
1716 | char *inbuf; | |
1717 | { | |
1718 | register char *instr; | |
1719 | register char *outstr; | |
1720 | register char ch; | |
1721 | char temp[200]; | |
1722 | int changed = 0; | |
1723 | ||
1724 | instr = inbuf; | |
1725 | outstr = temp; | |
1726 | while ((ch = *instr++) != '\0') | |
1727 | switch (ch) { | |
1728 | case '%': | |
1729 | if (!no_intty) { | |
1730 | strcpy (outstr, fnames[fnum]); | |
1731 | outstr += strlen (fnames[fnum]); | |
1732 | changed++; | |
1733 | } | |
1734 | else | |
1735 | *outstr++ = ch; | |
1736 | break; | |
1737 | case '!': | |
1738 | if (!shellp) | |
1739 | error ("No previous command to substitute for"); | |
1740 | strcpy (outstr, shell_line); | |
1741 | outstr += strlen (shell_line); | |
1742 | changed++; | |
1743 | break; | |
1744 | case '\\': | |
1745 | if (*instr == '%' || *instr == '!') { | |
1746 | *outstr++ = *instr++; | |
1747 | break; | |
1748 | } | |
1749 | default: | |
1750 | *outstr++ = ch; | |
1751 | } | |
1752 | *outstr++ = '\0'; | |
1753 | strcpy (outbuf, temp); | |
1754 | return (changed); | |
1755 | } | |
1756 | ||
1757 | show (ch) | |
1758 | register char ch; | |
1759 | { | |
1760 | char cbuf; | |
1761 | ||
1762 | if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) { | |
1763 | ch += ch == RUBOUT ? -0100 : 0100; | |
1764 | write (2, &CARAT, 1); | |
1765 | promptlen++; | |
1766 | } | |
1767 | cbuf = ch; | |
1768 | write (2, &cbuf, 1); | |
1769 | promptlen++; | |
1770 | } | |
1771 | ||
1772 | error (mess) | |
1773 | char *mess; | |
1774 | { | |
1775 | if (clreol) | |
1776 | cleareol (); | |
1777 | else | |
1778 | kill_line (); | |
1779 | promptlen += strlen (mess); | |
1780 | if (Senter && Sexit) { | |
1781 | tputs (Senter, 1, putch); | |
1782 | pr(mess); | |
1783 | tputs (Sexit, 1, putch); | |
1784 | } | |
1785 | else | |
1786 | pr (mess); | |
1787 | fflush(stdout); | |
1788 | errors++; | |
1789 | longjmp (restore, 1); | |
1790 | } | |
1791 | ||
1792 | void | |
1793 | regerror (mess) | |
1794 | const char *mess; | |
1795 | { | |
1796 | if (clreol) | |
1797 | cleareol (); | |
1798 | else | |
1799 | kill_line (); | |
1800 | promptlen += strlen (mess); | |
1801 | if (Senter && Sexit) { | |
1802 | tputs (Senter, 1, putch); | |
1803 | pr(mess); | |
1804 | tputs (Sexit, 1, putch); | |
1805 | } | |
1806 | else | |
1807 | pr (mess); | |
1808 | fflush(stdout); | |
1809 | errors++; | |
1810 | longjmp (restore, 1); | |
1811 | } | |
1812 | ||
1813 | ||
1814 | set_tty () | |
1815 | { | |
1816 | otty.sg_flags |= MBIT; | |
1817 | otty.sg_flags &= ~ECHO; | |
1818 | stty(fileno(stderr), &otty); | |
1819 | } | |
1820 | ||
1821 | reset_tty () | |
1822 | { | |
1823 | if (no_tty) | |
1824 | return; | |
1825 | if (pstate) { | |
1826 | tputs(ULexit, 1, putch); | |
1827 | fflush(stdout); | |
1828 | pstate = 0; | |
1829 | } | |
1830 | otty.sg_flags |= ECHO; | |
1831 | otty.sg_flags &= ~MBIT; | |
1832 | stty(fileno(stderr), &savetty); | |
1833 | } | |
1834 | ||
1835 | rdline (f) | |
1836 | register FILE *f; | |
1837 | { | |
1838 | register char c; | |
1839 | register char *p; | |
1840 | ||
1841 | p = Line; | |
1842 | while ((c = Getc (f)) != '\n' && c != EOF && p - Line < LINSIZ - 1) | |
1843 | *p++ = c; | |
1844 | if (c == '\n') | |
1845 | Currline++; | |
1846 | *p = '\0'; | |
1847 | } | |
1848 | ||
1849 | /* Come here when we get a suspend signal from the terminal */ | |
1850 | ||
1851 | void | |
1852 | onsusp () | |
1853 | { | |
1854 | /* ignore SIGTTOU so we don't get stopped if csh grabs the tty */ | |
1855 | signal(SIGTTOU, SIG_IGN); | |
1856 | reset_tty (); | |
1857 | fflush (stdout); | |
1858 | signal(SIGTTOU, SIG_DFL); | |
1859 | /* Send the TSTP signal to suspend our process group */ | |
1860 | signal(SIGTSTP, SIG_DFL); | |
1861 | sigsetmask(0); | |
1862 | kill (0, SIGTSTP); | |
1863 | /* Pause for station break */ | |
1864 | ||
1865 | /* We're back */ | |
1866 | signal (SIGTSTP, onsusp); | |
1867 | set_tty (); | |
1868 | if (inwait) | |
1869 | longjmp (restore, 1); | |
1870 | } |