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