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