Annotation of src/usr.bin/more/more.c, Revision 1.13
1.13 ! millert 1: /* $OpenBSD: more.c,v 1.12 2001/07/18 17:17:39 pvalchev Exp $ */
1.1 etheisen 2: /*-
3: * Copyright (c) 1980 The Regents of the University of California.
4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: * 2. Redistributions in binary form must reproduce the above copyright
12: * notice, this list of conditions and the following disclaimer in the
13: * documentation and/or other materials provided with the distribution.
14: * 3. All advertising materials mentioning features or use of this software
15: * must display the following acknowledgement:
16: * This product includes software developed by the University of
17: * California, Berkeley and its contributors.
18: * 4. Neither the name of the University nor the names of its contributors
19: * may be used to endorse or promote products derived from this software
20: * without specific prior written permission.
21: *
22: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32: * SUCH DAMAGE.
33: */
34:
35: #ifndef lint
36: char copyright[] =
37: "@(#) Copyright (c) 1980 The Regents of the University of California.\n\
38: All rights reserved.\n";
39: #endif /* not lint */
40:
41: #ifndef lint
42: static char sccsid[] = "@(#)more.c 5.28 (Berkeley) 3/1/93";
43: #endif /* not lint */
44:
45: /*
46: ** more.c - General purpose tty output filter and file perusal program
47: **
48: ** by Eric Shienbrood, UC Berkeley
49: **
50: ** modified by Geoff Peck, UCB to add underlining, single spacing
51: ** modified by John Foderaro, UCB to add -c and MORE environment variable
52: */
53:
54: #include <sys/param.h>
55: #include <sys/stat.h>
56: #include <signal.h>
57: #include <errno.h>
1.10 millert 58: #include <fcntl.h>
1.1 etheisen 59: #include <sgtty.h>
60: #include <setjmp.h>
61: #include <a.out.h>
62: #include <varargs.h>
63: #include <stdio.h>
64: #include <string.h>
65: #include <ctype.h>
66: #include "pathnames.h"
67:
68: #define Fopen(s,m) (Currline = 0,file_pos=0,fopen(s,m))
69: #define Ftell(f) file_pos
70: #define Fseek(f,off) (file_pos=off,fseek(f,off,0))
71: #define Getc(f) (++file_pos, getc(f))
72: #define Ungetc(c,f) (--file_pos, ungetc(c,f))
73:
74: #define MBIT CBREAK
75: #define stty(fd,argp) ioctl(fd,TIOCSETN,argp)
76:
77: #define TBUFSIZ 1024
78: #define LINSIZ 256
79: #define ctrl(letter) (letter & 077)
80: #define RUBOUT '\177'
81: #define ESC '\033'
82: #define QUIT '\034'
83:
84: struct sgttyb otty, savetty;
85: long file_pos, file_size;
86: int fnum, no_intty, no_tty, slow_tty;
87: int dum_opt, dlines;
88: void chgwinsz(), end_it(), onquit(), onsusp();
89: int nscroll = 11; /* Number of lines scrolled by 'd' */
90: int fold_opt = 1; /* Fold long lines */
91: int stop_opt = 1; /* Stop after form feeds */
92: int ssp_opt = 0; /* Suppress white space */
93: int ul_opt = 1; /* Underline as best we can */
94: int promptlen;
95: int Currline; /* Line we are currently at */
96: int startup = 1;
97: int firstf = 1;
98: int notell = 1;
99: int docrterase = 0;
100: int docrtkill = 0;
101: int bad_so; /* True if overwriting does not turn off standout */
102: int inwait, Pause, errors;
103: int within; /* true if we are within a file,
104: false if we are between files */
105: int hard, dumb, noscroll, hardtabs, clreol, eatnl;
106: int catch_susp; /* We should catch the SIGTSTP signal */
107: char **fnames; /* The list of file names */
108: int nfiles; /* Number of files left to process */
109: char *shell; /* The name of the shell to use */
110: int shellp; /* A previous shell command exists */
111: char ch;
112: jmp_buf restore;
113: char Line[LINSIZ]; /* Line buffer */
114: int Lpp = 24; /* lines per page */
115: char *Clear; /* clear screen */
116: char *eraseln; /* erase line */
117: char *Senter, *Sexit;/* enter and exit standout mode */
118: char *ULenter, *ULexit; /* enter and exit underline mode */
119: char *chUL; /* underline character */
120: char *chBS; /* backspace character */
121: char *Home; /* go to home */
122: char *cursorm; /* cursor movement */
123: char cursorhome[40]; /* contains cursor movement to home */
124: char *EodClr; /* clear rest of screen */
125: char *tgetstr();
126: int Mcol = 80; /* number of columns */
127: int Wrap = 1; /* set if automargins */
128: int soglitch; /* terminal has standout mode glitch */
129: int ulglitch; /* terminal has underline mode glitch */
130: int pstate = 0; /* current UL state */
131: char *getenv();
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)
1.3 etheisen 197: // dlines = Lpp - (noscroll ? 1 : 2);
198: dlines = Lpp - 1; /* XXX - maybe broken on dumb
199: terminals. */
1.1 etheisen 200: left = dlines;
201: if (nfiles > 1)
202: prnames++;
203: if (!no_intty && nfiles == 0) {
1.7 millert 204: p = strrchr(argv[0], '/');
1.1 etheisen 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);
1.8 kstailey 378: return(NULL);
1.1 etheisen 379: }
380: if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
381: prtf("\n*** %s: directory ***\n\n", fs);
1.8 kstailey 382: return(NULL);
1.1 etheisen 383: }
384: if ((f = Fopen(fs, "r")) == NULL) {
385: (void)fflush(stdout);
386: perror(fs);
1.8 kstailey 387: return(NULL);
1.1 etheisen 388: }
389: if (magic(f, fs))
1.8 kstailey 390: return(NULL);
1.1 etheisen 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)
1.2 etheisen 411: switch(N_GETMAGIC(ex)) {
1.1 etheisen 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: }
1.9 millert 422: (void)fseek(f, 0L, SEEK_SET); /* rewind() not necessary */
1.1 etheisen 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: /* See whether the last component of the path name "path" is equal to the
680: ** string "string"
681: */
682:
683: tailequ (path, string)
684: char *path;
685: register char *string;
686: {
687: register char *tail;
688:
689: tail = path + strlen(path);
690: while (tail >= path)
691: if (*(--tail) == '/')
692: break;
693: ++tail;
694: while (*tail++ == *string++)
695: if (*tail == '\0')
696: return(1);
697: return(0);
698: }
699:
700: prompt (filename)
701: char *filename;
702: {
703: if (clreol)
704: cleareol ();
705: else if (promptlen > 0)
706: kill_line ();
707: if (!hard) {
708: promptlen = 8;
709: if (Senter && Sexit) {
710: tputs (Senter, 1, putch);
711: promptlen += (2 * soglitch);
712: }
713: if (clreol)
714: cleareol ();
1.4 etheisen 715: /* XXX - evil global vars */
716: if(fnames[fnum] != NULL) {
717: promptlen += prtf ("%s ", fnames[fnum]);
718: }
719: else
720: pr("--More--");
1.1 etheisen 721: if (filename != NULL) {
722: promptlen += prtf ("(Next file: %s)", filename);
723: }
724: else if (!no_intty) {
725: promptlen += prtf ("(%d%%)", (int)((file_pos * 100) / file_size));
726: }
727: if (dum_opt) {
728: promptlen += pr("[Press space to continue, 'q' to quit.]");
729: }
730: if (Senter && Sexit)
731: tputs (Sexit, 1, putch);
732: if (clreol)
733: clreos ();
734: fflush(stdout);
735: }
736: else
737: write (2, &bell, 1);
738: inwait++;
739: }
740:
741: /*
742: ** Get a logical line
743: */
744:
745: getline(f, length)
746: register FILE *f;
747: int *length;
748: {
749: register int c;
750: register char *p;
751: register int column;
752: static int colflg;
753:
754: p = Line;
755: column = 0;
756: c = Getc (f);
757: if (colflg && c == '\n') {
758: Currline++;
759: c = Getc (f);
760: }
761: while (p < &Line[LINSIZ - 1]) {
762: if (c == EOF) {
763: if (p > Line) {
764: *p = '\0';
765: *length = p - Line;
766: return (column);
767: }
768: *length = p - Line;
769: return (EOF);
770: }
771: if (c == '\n') {
772: Currline++;
773: break;
774: }
775: *p++ = c;
776: if (c == '\t')
777: if (!hardtabs || column < promptlen && !hard) {
778: if (hardtabs && eraseln && !dumb) {
779: column = 1 + (column | 7);
780: tputs (eraseln, 1, putch);
781: promptlen = 0;
782: }
783: else {
784: for (--p; p < &Line[LINSIZ - 1];) {
785: *p++ = ' ';
786: if ((++column & 7) == 0)
787: break;
788: }
789: if (column >= promptlen) promptlen = 0;
790: }
791: }
792: else
793: column = 1 + (column | 7);
794: else if (c == '\b' && column > 0)
795: column--;
796: else if (c == '\r')
797: column = 0;
798: else if (c == '\f' && stop_opt) {
799: p[-1] = '^';
800: *p++ = 'L';
801: column += 2;
802: Pause++;
803: }
804: else if (c == EOF) {
805: *length = p - Line;
806: return (column);
807: }
808: else if (c >= ' ' && c != RUBOUT)
809: column++;
810: if (column >= Mcol && fold_opt) break;
811: c = Getc (f);
812: }
813: if (column >= Mcol && Mcol > 0) {
814: if (!Wrap) {
815: *p++ = '\n';
816: }
817: }
818: colflg = column == Mcol && fold_opt;
819: if (colflg && eatnl && Wrap) {
820: *p++ = '\n'; /* simulate normal wrap */
821: }
822: *length = p - Line;
823: *p = 0;
824: return (column);
825: }
826:
827: /*
828: ** Erase the rest of the prompt, assuming we are starting at column col.
829: */
830:
831: erase (col)
832: register int col;
833: {
834:
835: if (promptlen == 0)
836: return;
837: if (hard) {
838: putchar ('\n');
839: }
840: else {
841: if (col == 0)
842: putchar ('\r');
843: if (!dumb && eraseln)
844: tputs (eraseln, 1, putch);
845: else
846: for (col = promptlen - col; col > 0; col--)
847: putchar (' ');
848: }
849: promptlen = 0;
850: }
851:
852: /*
853: ** Erase the current line entirely
854: */
855:
856: kill_line ()
857: {
858: erase (0);
859: if (!eraseln || dumb) putchar ('\r');
860: }
861:
862: /*
863: * force clear to end of line
864: */
865: cleareol()
866: {
867: tputs(eraseln, 1, putch);
868: }
869:
870: clreos()
871: {
872: tputs(EodClr, 1, putch);
873: }
874:
875: /*
876: ** Print string and return number of characters
877: */
878:
879: pr(s1)
880: char *s1;
881: {
882: register char *s;
883: register char c;
884:
885: for (s = s1; c = *s++; )
886: putchar(c);
887: return (s - s1 - 1);
888: }
889:
890:
891: /* Print a buffer of n characters */
892:
893: prbuf (s, n)
894: register char *s;
895: register int n;
896: {
897: register char c; /* next output character */
898: register int state; /* next output char's UL state */
899: #define wouldul(s,n) ((n) >= 2 && (((s)[0] == '_' && (s)[1] == '\b') || ((s)[1] == '\b' && (s)[2] == '_')))
900:
901: while (--n >= 0)
902: if (!ul_opt)
903: putchar (*s++);
904: else {
905: if (*s == ' ' && pstate == 0 && ulglitch && wouldul(s+1, n-1)) {
906: s++;
907: continue;
908: }
909: if (state = wouldul(s, n)) {
910: c = (*s == '_')? s[2] : *s ;
911: n -= 2;
912: s += 3;
913: } else
914: c = *s++;
915: if (state != pstate) {
916: if (c == ' ' && state == 0 && ulglitch && wouldul(s, n-1))
917: state = 1;
918: else
919: tputs(state ? ULenter : ULexit, 1, putch);
920: }
921: if (c != ' ' || pstate == 0 || state != 0 || ulglitch == 0)
922: putchar(c);
923: if (state && *chUL) {
924: pr(chBS);
925: tputs(chUL, 1, putch);
926: }
927: pstate = state;
928: }
929: }
930:
931: /*
932: ** Clear the screen
933: */
934:
935: doclear()
936: {
937: if (Clear && !hard) {
938: tputs(Clear, 1, putch);
939:
940: /* Put out carriage return so that system doesn't
941: ** get confused by escape sequences when expanding tabs
942: */
943: putchar ('\r');
944: promptlen = 0;
945: }
946: }
947:
948: /*
949: * Go to home position
950: */
951: home()
952: {
953: tputs(Home,1,putch);
954: }
955:
956: static int lastcmd, lastarg, lastp;
957: static int lastcolon;
958: char shell_line[132];
959:
960: /*
961: ** Read a command and do it. A command consists of an optional integer
962: ** argument followed by the command character. Return the number of lines
963: ** to display in the next screenful. If there is nothing more to display
964: ** in the current file, zero is returned.
965: */
966:
967: command (filename, f)
968: char *filename;
969: register FILE *f;
970: {
971: register int nlines;
972: register int retval;
973: register char c;
974: char colonch;
975: FILE *helpf;
976: int done;
1.12 pvalchev 977: char comchar, cmdbuf[80];
1.5 etheisen 978: char option[8];
979: char *EDITOR;
980: char *editor;
1.1 etheisen 981:
982: #define ret(val) retval=val;done++;break
983:
984: done = 0;
985: if (!errors)
986: prompt (filename);
987: else
988: errors = 0;
989: if (MBIT == RAW && slow_tty) {
990: otty.sg_flags |= MBIT;
991: stty(fileno(stderr), &otty);
992: }
993: for (;;) {
994: nlines = number (&comchar);
995: lastp = colonch = 0;
996: if (comchar == '.') { /* Repeat last command */
997: lastp++;
998: comchar = lastcmd;
999: nlines = lastarg;
1000: if (lastcmd == ':')
1001: colonch = lastcolon;
1002: }
1003: lastcmd = comchar;
1004: lastarg = nlines;
1005: if (comchar == otty.sg_erase) {
1006: kill_line ();
1007: prompt (filename);
1008: continue;
1009: }
1010: switch (comchar) {
1011: case ':':
1012: retval = colon (filename, colonch, nlines);
1013: if (retval >= 0)
1014: done++;
1015: break;
1016: case 'b':
1017: case ctrl('B'):
1018: {
1019: register int initline;
1020:
1021: if (no_intty) {
1022: write(2, &bell, 1);
1023: return (-1);
1024: }
1025:
1026: if (nlines == 0) nlines++;
1027:
1028: putchar ('\r');
1029: erase (0);
1030: prtf ("\n");
1031: if (clreol)
1032: cleareol ();
1033: prtf ("...back %d page", nlines);
1034: if (nlines > 1)
1035: pr ("s\n");
1036: else
1037: pr ("\n");
1038:
1039: if (clreol)
1040: cleareol ();
1041: pr ("\n");
1042:
1043: initline = Currline - dlines * (nlines + 1);
1044: if (! noscroll)
1045: --initline;
1046: if (initline < 0) initline = 0;
1047: Fseek(f, 0L);
1048: Currline = 0; /* skiplns() will make Currline correct */
1049: skiplns(initline, f);
1.3 etheisen 1050: /* if (! noscroll) {
1.1 etheisen 1051: ret(dlines + 1);
1.3 etheisen 1052: }
1053: else {
1.1 etheisen 1054: ret(dlines);
1.3 etheisen 1055: } */
1056: ret(dlines); /* XXX - Maybe broken on dumb
1057: terminals */
1.1 etheisen 1058: }
1.6 etheisen 1059: /* 4.3BSD more - Display next [count] lines of text */
1.1 etheisen 1060: case ' ':
1061: case 'z':
1062: if (nlines == 0) nlines = dlines;
1.6 etheisen 1063: else if (comchar == 'z')
1064: dlines = nlines;
1.1 etheisen 1065: ret (nlines);
1066: case 'd':
1067: case ctrl('D'):
1068: if (nlines != 0) nscroll = nlines;
1069: ret (nscroll);
1070: case 'q':
1071: case 'Q':
1072: end_it ();
1073: case 's':
1.6 etheisen 1074: /* POSIX.2 Move forward one screenfull */
1075: case 'f': /* POSIX.2 [count]f */
1076: case ctrl('F'): /* [count]control-F */
1.1 etheisen 1077: if (nlines == 0) nlines++;
1078: if (comchar == 'f')
1079: nlines *= dlines;
1080: putchar ('\r');
1081: erase (0);
1082: prtf ("\n");
1083: if (clreol)
1084: cleareol ();
1085: prtf ("...skipping %d line", nlines);
1086: if (nlines > 1)
1087: pr ("s\n");
1088: else
1089: pr ("\n");
1090:
1091: if (clreol)
1092: cleareol ();
1093: pr ("\n");
1094:
1095: while (nlines > 0) {
1096: while ((c = Getc (f)) != '\n')
1097: if (c == EOF) {
1098: retval = 0;
1099: done++;
1100: goto endsw;
1101: }
1102: Currline++;
1103: nlines--;
1104: }
1105: ret (dlines);
1106: case '\n':
1107: if (nlines != 0)
1108: dlines = nlines;
1109: else
1110: nlines = 1;
1111: ret (nlines);
1112: case '\f':
1113: if (!no_intty) {
1114: doclear ();
1115: Fseek (f, screen_start.chrctr);
1116: Currline = screen_start.line;
1117: ret (dlines);
1118: }
1119: else {
1120: write (2, &bell, 1);
1121: break;
1122: }
1123: case '\'':
1124: if (!no_intty) {
1125: kill_line ();
1126: pr ("\n***Back***\n\n");
1127: Fseek (f, context.chrctr);
1128: Currline = context.line;
1129: ret (dlines);
1130: }
1131: else {
1132: write (2, &bell, 1);
1133: break;
1134: }
1135: case '=':
1136: kill_line ();
1137: promptlen = printd (Currline);
1138: fflush (stdout);
1139: break;
1140: case 'n':
1141: lastp++;
1142: case '/':
1143: if (nlines == 0) nlines++;
1144: kill_line ();
1145: pr ("/");
1146: promptlen = 1;
1147: fflush (stdout);
1148: if (lastp) {
1149: write (2,"\r", 1);
1150: search (NULL, f, nlines); /* Use previous r.e. */
1151: }
1152: else {
1153: ttyin (cmdbuf, 78, '/');
1154: write (2, "\r", 1);
1155: search (cmdbuf, f, nlines);
1156: }
1157: ret (dlines-1);
1158: case '!':
1159: do_shell (filename);
1160: break;
1161: case '?':
1162: case 'h':
1163: if ((helpf = fopen (HELPFILE, "r")) == NULL)
1164: error ("Can't open help file");
1165: if (noscroll) doclear ();
1166: copy_file (helpf);
1167: fclose (helpf);
1168: prompt (filename);
1169: break;
1.5 etheisen 1170: /* Run Editor */
1.1 etheisen 1171: case 'v': /* This case should go right before default */
1172: if (!no_intty) {
1173: kill_line ();
1.5 etheisen 1174: strcpy(cmdbuf, "-c");
1.1 etheisen 1175: scanstr (Currline - dlines < 0 ? 0
1.5 etheisen 1176: : Currline - (dlines + 1) / 2, option);
1177:
1178: /* POSIX.2 more EDITOR env. var. behavior */
1179: if ((EDITOR = getenv("EDITOR")) != NULL) {
1180:
1181: /* Need to look for vi or ex as
1182: * we need to tell them the current
1183: * line number
1184: */
1185: if ((editor = strrchr(EDITOR, '/')) == NULL)
1186: editor = EDITOR;
1187: else
1188: editor++;
1189: if ((strcmp(editor, "vi") == 0) ||
1190: (strcmp(editor, "ex") == 0))
1191: execute (filename, EDITOR, EDITOR, cmdbuf, option, fnames[fnum], 0);
1192: else /* must be some other editor */
1193: execute (filename, EDITOR, EDITOR, fnames[fnum], 0);
1194: }
1195: else { /* default editor */
1196: execute (filename, _PATH_VI, _PATH_VI, cmdbuf, option, fnames[fnum], 0);
1197: }
1198:
1.1 etheisen 1199: break;
1200: }
1201: default:
1202: if (dum_opt) {
1203: kill_line ();
1204: if (Senter && Sexit) {
1205: tputs (Senter, 1, putch);
1206: promptlen = pr ("[Press 'h' for instructions.]") + (2 * soglitch);
1207: tputs (Sexit, 1, putch);
1208: }
1209: else
1210: promptlen = pr ("[Press 'h' for instructions.]");
1211: fflush (stdout);
1212: }
1213: else
1214: write (2, &bell, 1);
1215: break;
1216: }
1217: if (done) break;
1218: }
1219: putchar ('\r');
1220: endsw:
1221: inwait = 0;
1222: notell++;
1223: if (MBIT == RAW && slow_tty) {
1224: otty.sg_flags &= ~MBIT;
1225: stty(fileno(stderr), &otty);
1226: }
1227: return (retval);
1228: }
1229:
1230: char ch;
1231:
1232: /*
1233: * Execute a colon-prefixed command.
1234: * Returns <0 if not a command that should cause
1235: * more of the file to be printed.
1236: */
1237:
1238: colon (filename, cmd, nlines)
1239: char *filename;
1240: int cmd;
1241: int nlines;
1242: {
1243: if (cmd == 0)
1244: ch = readch ();
1245: else
1246: ch = cmd;
1247: lastcolon = ch;
1248: switch (ch) {
1249: case 'f':
1250: kill_line ();
1251: if (!no_intty)
1252: promptlen = prtf ("\"%s\" line %d", fnames[fnum], Currline);
1253: else
1254: promptlen = prtf ("[Not a file] line %d", Currline);
1255: fflush (stdout);
1256: return (-1);
1257: case 'n':
1258: if (nlines == 0) {
1259: if (fnum >= nfiles - 1)
1260: end_it ();
1261: nlines++;
1262: }
1263: putchar ('\r');
1264: erase (0);
1265: skipf (nlines);
1266: return (0);
1267: case 'p':
1268: if (no_intty) {
1269: write (2, &bell, 1);
1270: return (-1);
1271: }
1272: putchar ('\r');
1273: erase (0);
1274: if (nlines == 0)
1275: nlines++;
1276: skipf (-nlines);
1277: return (0);
1278: case '!':
1279: do_shell (filename);
1280: return (-1);
1281: case 'q':
1282: case 'Q':
1283: end_it ();
1284: default:
1285: write (2, &bell, 1);
1286: return (-1);
1287: }
1288: }
1289:
1290: /*
1291: ** Read a decimal number from the terminal. Set cmd to the non-digit which
1292: ** terminates the number.
1293: */
1294:
1295: number(cmd)
1296: char *cmd;
1297: {
1298: register int i;
1299:
1300: i = 0; ch = otty.sg_kill;
1301: for (;;) {
1302: ch = readch ();
1303: if (ch >= '0' && ch <= '9')
1304: i = i*10 + ch - '0';
1305: else if (ch == otty.sg_kill)
1306: i = 0;
1307: else {
1308: *cmd = ch;
1309: break;
1310: }
1311: }
1312: return (i);
1313: }
1314:
1315: do_shell (filename)
1316: char *filename;
1317: {
1318: char cmdbuf[80];
1319:
1320: kill_line ();
1321: pr ("!");
1322: fflush (stdout);
1323: promptlen = 1;
1324: if (lastp)
1325: pr (shell_line);
1326: else {
1327: ttyin (cmdbuf, 78, '!');
1328: if (expand (shell_line, cmdbuf)) {
1329: kill_line ();
1330: promptlen = prtf ("!%s", shell_line);
1331: }
1332: }
1333: fflush (stdout);
1334: write (2, "\n", 1);
1335: promptlen = 0;
1336: shellp = 1;
1337: execute (filename, shell, shell, "-c", shell_line, 0);
1338: }
1339:
1340: /*
1341: ** Search for nth ocurrence of regular expression contained in buf in the file
1342: */
1343:
1344: search (buf, file, n)
1345: char buf[];
1346: FILE *file;
1347: register int n;
1348: {
1349: long startline = Ftell (file);
1350: register long line1 = startline;
1351: register long line2 = startline;
1352: register long line3 = startline;
1353: register int lncount;
1354: int saveln, rv, re_exec();
1355: char *s, *re_comp();
1356:
1357: context.line = saveln = Currline;
1358: context.chrctr = startline;
1359: lncount = 0;
1360: if ((s = re_comp (buf)) != 0)
1361: error (s);
1362: while (!feof (file)) {
1363: line3 = line2;
1364: line2 = line1;
1365: line1 = Ftell (file);
1366: rdline (file);
1367: lncount++;
1368: if ((rv = re_exec (Line)) == 1)
1369: if (--n == 0) {
1370: if (lncount > 3 || (lncount > 1 && no_intty))
1371: {
1372: pr ("\n");
1373: if (clreol)
1374: cleareol ();
1375: pr("...skipping\n");
1376: }
1377: if (!no_intty) {
1378: Currline -= (lncount >= 3 ? 3 : lncount);
1379: Fseek (file, line3);
1380: if (noscroll)
1381: if (clreol) {
1382: home ();
1383: cleareol ();
1384: }
1385: else
1386: doclear ();
1387: }
1388: else {
1389: kill_line ();
1390: if (noscroll)
1391: if (clreol) {
1392: home ();
1393: cleareol ();
1394: }
1395: else
1396: doclear ();
1397: pr (Line);
1398: putchar ('\n');
1399: }
1400: break;
1401: }
1402: else if (rv == -1)
1403: error ("Regular expression botch");
1404: }
1405: if (feof (file)) {
1406: if (!no_intty) {
1407: /* file->_flag &= ~_IOEOF; /* why doesn't fseek do this ??!!??! */
1408: Currline = saveln;
1409: Fseek (file, startline);
1410: }
1411: else {
1412: pr ("\nPattern not found\n");
1413: end_it ();
1414: }
1415: error ("Pattern not found");
1416: }
1417: }
1418:
1419: /*VARARGS2*/
1420: execute (filename, cmd, va_alist)
1421: char *filename;
1422: char *cmd;
1423: va_dcl
1424: {
1425: int id;
1426: int n;
1427: va_list argp;
1428:
1429: fflush (stdout);
1430: reset_tty ();
1431: for (n = 10; (id = fork ()) < 0 && n > 0; n--)
1432: sleep (5);
1433: if (id == 0) {
1434: if (!isatty(0)) {
1435: close(0);
1.10 millert 1436: open(_PATH_TTY, O_RDONLY);
1.1 etheisen 1437: }
1438: va_start(argp);
1.5 etheisen 1439: execvp (cmd, argp);
1.1 etheisen 1440: write (2, "exec failed\n", 12);
1441: exit (1);
1442: va_end(argp); /* balance {}'s for some UNIX's */
1443: }
1444: if (id > 0) {
1445: signal (SIGINT, SIG_IGN);
1446: signal (SIGQUIT, SIG_IGN);
1447: if (catch_susp)
1448: signal(SIGTSTP, SIG_DFL);
1449: while (wait(0) > 0);
1450: signal (SIGINT, end_it);
1451: signal (SIGQUIT, onquit);
1452: if (catch_susp)
1453: signal(SIGTSTP, onsusp);
1454: } else
1455: write(2, "can't fork\n", 11);
1456: set_tty ();
1.5 etheisen 1457: /* pr ("------------------------\n"); */
1.1 etheisen 1458: prompt (filename);
1459: }
1460: /*
1461: ** Skip n lines in the file f
1462: */
1463:
1464: skiplns (n, f)
1465: register int n;
1466: register FILE *f;
1467: {
1468: register char c;
1469:
1470: while (n > 0) {
1471: while ((c = Getc (f)) != '\n')
1472: if (c == EOF)
1473: return;
1474: n--;
1475: Currline++;
1476: }
1477: }
1478:
1479: /*
1480: ** Skip nskip files in the file list (from the command line). Nskip may be
1481: ** negative.
1482: */
1483:
1484: skipf (nskip)
1485: register int nskip;
1486: {
1487: if (nskip == 0) return;
1488: if (nskip > 0) {
1489: if (fnum + nskip > nfiles - 1)
1490: nskip = nfiles - fnum - 1;
1491: }
1492: else if (within)
1493: ++fnum;
1494: fnum += nskip;
1495: if (fnum < 0)
1496: fnum = 0;
1497: pr ("\n...Skipping ");
1498: pr ("\n");
1499: if (clreol)
1500: cleareol ();
1501: pr ("...Skipping ");
1502: pr (nskip > 0 ? "to file " : "back to file ");
1503: pr (fnames[fnum]);
1504: pr ("\n");
1505: if (clreol)
1506: cleareol ();
1507: pr ("\n");
1508: --fnum;
1509: }
1510:
1511: /*----------------------------- Terminal I/O -------------------------------*/
1512:
1513: initterm ()
1514: {
1515: char buf[TBUFSIZ];
1516: static char clearbuf[TBUFSIZ];
1517: char *clearptr, *padstr;
1518: int ldisc;
1519: int lmode;
1520: char *term;
1521: int tgrp;
1522: struct winsize win;
1523: char *tgoto();
1524:
1525: retry:
1526: if (!(no_tty = ioctl(fileno(stdout), TIOCGETP, &otty))) {
1527: if (ioctl(fileno(stdout), TIOCLGET, &lmode) < 0) {
1528: perror("TIOCLGET");
1529: exit(1);
1530: }
1531: docrterase = ((lmode & LCRTERA) != 0);
1532: docrtkill = ((lmode & LCRTKIL) != 0);
1533: /*
1534: * Wait until we're in the foreground before we save the
1535: * the terminal modes.
1536: */
1537: if (ioctl(fileno(stdout), TIOCGPGRP, &tgrp) < 0) {
1538: perror("TIOCGPGRP");
1539: exit(1);
1540: }
1541: if (tgrp != getpgrp(0)) {
1542: kill(0, SIGTTOU);
1543: goto retry;
1544: }
1545: if ((term = getenv("TERM")) == 0 || tgetent(buf, term) <= 0) {
1546: dumb++; ul_opt = 0;
1547: }
1548: else {
1549: if (ioctl(fileno(stdout), TIOCGWINSZ, &win) < 0) {
1550: Lpp = tgetnum("li");
1551: Mcol = tgetnum("co");
1552: } else {
1553: if ((Lpp = win.ws_row) == 0)
1554: Lpp = tgetnum("li");
1555: if ((Mcol = win.ws_col) == 0)
1556: Mcol = tgetnum("co");
1557: }
1558: if ((Lpp <= 0) || tgetflag("hc")) {
1559: hard++; /* Hard copy terminal */
1560: Lpp = 24;
1561: }
1562: if (tgetflag("xn"))
1563: eatnl++; /* Eat newline at last column + 1; dec, concept */
1564: if (Mcol <= 0)
1565: Mcol = 80;
1566:
1567: if (tailequ (fnames[0], "page") || !hard && tgetflag("ns"))
1568: noscroll++;
1569: Wrap = tgetflag("am");
1570: bad_so = tgetflag ("xs");
1571: clearptr = clearbuf;
1572: eraseln = tgetstr("ce",&clearptr);
1573: Clear = tgetstr("cl", &clearptr);
1574: Senter = tgetstr("so", &clearptr);
1575: Sexit = tgetstr("se", &clearptr);
1576: if ((soglitch = tgetnum("sg")) < 0)
1577: soglitch = 0;
1578:
1579: /*
1580: * Set up for underlining: some terminals don't need it;
1581: * others have start/stop sequences, still others have an
1582: * underline char sequence which is assumed to move the
1583: * cursor forward one character. If underline sequence
1584: * isn't available, settle for standout sequence.
1585: */
1586:
1587: if (tgetflag("ul") || tgetflag("os"))
1588: ul_opt = 0;
1589: if ((chUL = tgetstr("uc", &clearptr)) == NULL )
1590: chUL = "";
1591: if (((ULenter = tgetstr("us", &clearptr)) == NULL ||
1592: (ULexit = tgetstr("ue", &clearptr)) == NULL) && !*chUL) {
1593: if ((ULenter = Senter) == NULL || (ULexit = Sexit) == NULL) {
1594: ULenter = "";
1595: ULexit = "";
1596: } else
1597: ulglitch = soglitch;
1598: } else {
1599: if ((ulglitch = tgetnum("ug")) < 0)
1600: ulglitch = 0;
1601: }
1602:
1603: if (padstr = tgetstr("pc", &clearptr))
1604: PC = *padstr;
1605: Home = tgetstr("ho",&clearptr);
1606: if (Home == 0 || *Home == '\0')
1607: {
1608: if ((cursorm = tgetstr("cm", &clearptr)) != NULL) {
1609: strcpy(cursorhome, tgoto(cursorm, 0, 0));
1610: Home = cursorhome;
1611: }
1612: }
1613: EodClr = tgetstr("cd", &clearptr);
1614: if ((chBS = tgetstr("bc", &clearptr)) == NULL)
1615: chBS = "\b";
1616:
1617: }
1618: if ((shell = getenv("SHELL")) == NULL)
1619: shell = "/bin/sh";
1620: }
1621: no_intty = ioctl(fileno(stdin), TIOCGETP, &otty);
1622: (void)ioctl(fileno(stderr), TIOCGETP, &otty);
1623: savetty = otty;
1624: ospeed = otty.sg_ospeed;
1625: slow_tty = ospeed < B1200;
1626: hardtabs = (otty.sg_flags & TBDELAY) != XTABS;
1627: if (!no_tty) {
1628: otty.sg_flags &= ~ECHO;
1629: if (MBIT == CBREAK || !slow_tty)
1630: otty.sg_flags |= MBIT;
1631: }
1632: }
1633:
1634: readch ()
1635: {
1636: char ch;
1637:
1638: errno = 0;
1639: if (read (2, &ch, 1) <= 0)
1640: if (errno != EINTR)
1641: end_it();
1642: else
1643: ch = otty.sg_kill;
1644: return (ch);
1645: }
1646:
1647: static char BS = '\b';
1648: static char *BSB = "\b \b";
1649: static char CARAT = '^';
1650: #define ERASEONECHAR \
1651: if (docrterase) \
1652: write (2, BSB, sizeof(BSB)); \
1653: else \
1654: write (2, &BS, sizeof(BS));
1655:
1656: ttyin (buf, nmax, pchar)
1657: char buf[];
1658: register int nmax;
1659: char pchar;
1660: {
1661: register char *sptr;
1662: register char ch;
1663: register int slash = 0;
1664: int maxlen;
1665: char cbuf;
1666:
1667: sptr = buf;
1668: maxlen = 0;
1669: while (sptr - buf < nmax) {
1670: if (promptlen > maxlen) maxlen = promptlen;
1671: ch = readch ();
1672: if (ch == '\\') {
1673: slash++;
1674: }
1675: else if ((ch == otty.sg_erase) && !slash) {
1676: if (sptr > buf) {
1677: --promptlen;
1678: ERASEONECHAR
1679: --sptr;
1680: if ((*sptr < ' ' && *sptr != '\n') || *sptr == RUBOUT) {
1681: --promptlen;
1682: ERASEONECHAR
1683: }
1684: continue;
1685: }
1686: else {
1687: if (!eraseln) promptlen = maxlen;
1688: longjmp (restore, 1);
1689: }
1690: }
1691: else if ((ch == otty.sg_kill) && !slash) {
1692: if (hard) {
1693: show (ch);
1694: putchar ('\n');
1695: putchar (pchar);
1696: }
1697: else {
1698: putchar ('\r');
1699: putchar (pchar);
1700: if (eraseln)
1701: erase (1);
1702: else if (docrtkill)
1703: while (promptlen-- > 1)
1704: write (2, BSB, sizeof(BSB));
1705: promptlen = 1;
1706: }
1707: sptr = buf;
1708: fflush (stdout);
1709: continue;
1710: }
1711: if (slash && (ch == otty.sg_kill || ch == otty.sg_erase)) {
1712: ERASEONECHAR
1713: --sptr;
1714: }
1715: if (ch != '\\')
1716: slash = 0;
1717: *sptr++ = ch;
1718: if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) {
1719: ch += ch == RUBOUT ? -0100 : 0100;
1720: write (2, &CARAT, 1);
1721: promptlen++;
1722: }
1723: cbuf = ch;
1724: if (ch != '\n' && ch != ESC) {
1725: write (2, &cbuf, 1);
1726: promptlen++;
1727: }
1728: else
1729: break;
1730: }
1731: *--sptr = '\0';
1732: if (!eraseln) promptlen = maxlen;
1733: if (sptr - buf >= nmax - 1)
1734: error ("Line too long");
1735: }
1736:
1737: expand (outbuf, inbuf)
1738: char *outbuf;
1739: char *inbuf;
1740: {
1741: register char *instr;
1742: register char *outstr;
1743: register char ch;
1744: char temp[200];
1745: int changed = 0;
1746:
1747: instr = inbuf;
1748: outstr = temp;
1749: while ((ch = *instr++) != '\0')
1750: switch (ch) {
1751: case '%':
1752: if (!no_intty) {
1753: strcpy (outstr, fnames[fnum]);
1754: outstr += strlen (fnames[fnum]);
1755: changed++;
1756: }
1757: else
1758: *outstr++ = ch;
1759: break;
1760: case '!':
1761: if (!shellp)
1762: error ("No previous command to substitute for");
1763: strcpy (outstr, shell_line);
1764: outstr += strlen (shell_line);
1765: changed++;
1766: break;
1767: case '\\':
1768: if (*instr == '%' || *instr == '!') {
1769: *outstr++ = *instr++;
1770: break;
1771: }
1772: default:
1773: *outstr++ = ch;
1774: }
1775: *outstr++ = '\0';
1776: strcpy (outbuf, temp);
1777: return (changed);
1778: }
1779:
1780: show (ch)
1781: register char ch;
1782: {
1783: char cbuf;
1784:
1785: if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) {
1786: ch += ch == RUBOUT ? -0100 : 0100;
1787: write (2, &CARAT, 1);
1788: promptlen++;
1789: }
1790: cbuf = ch;
1791: write (2, &cbuf, 1);
1792: promptlen++;
1793: }
1794:
1795: error (mess)
1796: char *mess;
1797: {
1798: if (clreol)
1799: cleareol ();
1800: else
1801: kill_line ();
1802: promptlen += strlen (mess);
1803: if (Senter && Sexit) {
1804: tputs (Senter, 1, putch);
1805: pr(mess);
1806: tputs (Sexit, 1, putch);
1807: }
1808: else
1809: pr (mess);
1810: fflush(stdout);
1811: errors++;
1812: longjmp (restore, 1);
1813: }
1814:
1815:
1816: set_tty ()
1817: {
1818: otty.sg_flags |= MBIT;
1819: otty.sg_flags &= ~ECHO;
1820: stty(fileno(stderr), &otty);
1821: }
1822:
1823: reset_tty ()
1824: {
1825: if (no_tty)
1826: return;
1827: if (pstate) {
1828: tputs(ULexit, 1, putch);
1829: fflush(stdout);
1830: pstate = 0;
1831: }
1832: otty.sg_flags |= ECHO;
1833: otty.sg_flags &= ~MBIT;
1834: stty(fileno(stderr), &savetty);
1835: }
1836:
1837: rdline (f)
1838: register FILE *f;
1839: {
1840: register char c;
1841: register char *p;
1842:
1843: p = Line;
1844: while ((c = Getc (f)) != '\n' && c != EOF && p - Line < LINSIZ - 1)
1845: *p++ = c;
1846: if (c == '\n')
1847: Currline++;
1848: *p = '\0';
1849: }
1850:
1851: /* Come here when we get a suspend signal from the terminal */
1852:
1853: void
1854: onsusp ()
1855: {
1.13 ! millert 1856: sigset_t mask;
! 1857:
1.1 etheisen 1858: /* ignore SIGTTOU so we don't get stopped if csh grabs the tty */
1859: signal(SIGTTOU, SIG_IGN);
1860: reset_tty ();
1861: fflush (stdout);
1862: signal(SIGTTOU, SIG_DFL);
1863: /* Send the TSTP signal to suspend our process group */
1864: signal(SIGTSTP, SIG_DFL);
1.13 ! millert 1865: sigemptyset(&mask);
! 1866: sigprocmask(SIG_SETMASK, &mask, NULL);
1.1 etheisen 1867: kill (0, SIGTSTP);
1868: /* Pause for station break */
1869:
1870: /* We're back */
1871: signal (SIGTSTP, onsusp);
1872: set_tty ();
1873: if (inwait)
1874: longjmp (restore, 1);
1875: }