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