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