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