xref: /csrg-svn/old/more/more.c (revision 18030)
113615Ssam #ifndef lint
2*18030Sbloom static	char *sccsid = "@(#)more.c	4.22 (Berkeley) 85/02/21";
313615Ssam #endif
43594Sroot 
51500Serics /*
61500Serics ** more.c - General purpose tty output filter and file perusal program
71500Serics **
81500Serics **	by Eric Shienbrood, UC Berkeley
93594Sroot **
103594Sroot **	modified by Geoff Peck, UCB to add underlining, single spacing
113594Sroot **	modified by John Foderaro, UCB to add -c and MORE environment variable
121500Serics */
131500Serics 
141500Serics #include <stdio.h>
1517592Sleres #include <sys/types.h>
161500Serics #include <ctype.h>
171500Serics #include <signal.h>
181500Serics #include <errno.h>
191500Serics #include <sgtty.h>
201500Serics #include <setjmp.h>
211500Serics #include <sys/stat.h>
221500Serics 
2313615Ssam #define HELPFILE	"/usr/lib/more.help"
2413615Ssam #define VI		"/usr/ucb/vi"
251500Serics 
261500Serics #define Fopen(s,m)	(Currline = 0,file_pos=0,fopen(s,m))
271500Serics #define Ftell(f)	file_pos
281500Serics #define Fseek(f,off)	(file_pos=off,fseek(f,off,0))
291500Serics #define Getc(f)		(++file_pos, getc(f))
301500Serics #define Ungetc(c,f)	(--file_pos, ungetc(c,f))
311500Serics 
321500Serics #define MBIT	CBREAK
331500Serics #define stty(fd,argp)	ioctl(fd,TIOCSETN,argp)
341500Serics 
351500Serics #define TBUFSIZ	1024
361500Serics #define LINSIZ	256
371500Serics #define ctrl(letter)	('letter' & 077)
381500Serics #define RUBOUT	'\177'
391500Serics #define ESC	'\033'
401500Serics #define QUIT	'\034'
411500Serics 
4216582Sleres struct sgttyb	otty, savetty;
431500Serics long		file_pos, file_size;
441500Serics int		fnum, no_intty, no_tty, slow_tty;
451500Serics int		dum_opt, dlines, onquit(), end_it();
461500Serics int		onsusp();
471500Serics int		nscroll = 11;	/* Number of lines scrolled by 'd' */
481500Serics int		fold_opt = 1;	/* Fold long lines */
491500Serics int		stop_opt = 1;	/* Stop after form feeds */
503594Sroot int		ssp_opt = 0;	/* Suppress white space */
513594Sroot int		ul_opt = 1;	/* Underline as best we can */
521500Serics int		promptlen;
531500Serics int		Currline;	/* Line we are currently at */
541500Serics int		startup = 1;
551500Serics int		firstf = 1;
561500Serics int		notell = 1;
5717592Sleres int		docrterase = 0;
5817592Sleres int		docrtkill = 0;
591500Serics int		bad_so;	/* True if overwriting does not turn off standout */
601500Serics int		inwait, Pause, errors;
611500Serics int		within;	/* true if we are within a file,
621500Serics 			false if we are between files */
633594Sroot int		hard, dumb, noscroll, hardtabs, clreol;
641500Serics int		catch_susp;	/* We should catch the SIGTSTP signal */
651500Serics char		**fnames;	/* The list of file names */
661500Serics int		nfiles;		/* Number of files left to process */
671500Serics char		*shell;		/* The name of the shell to use */
681500Serics int		shellp;		/* A previous shell command exists */
691500Serics char		ch;
701500Serics jmp_buf		restore;
711500Serics char		Line[LINSIZ];	/* Line buffer */
721500Serics int		Lpp = 24;	/* lines per page */
731500Serics char		*Clear;		/* clear screen */
741500Serics char		*eraseln;	/* erase line */
751500Serics char		*Senter, *Sexit;/* enter and exit standout mode */
763594Sroot char		*ULenter, *ULexit;	/* enter and exit underline mode */
773594Sroot char		*chUL;		/* underline character */
783594Sroot char		*chBS;		/* backspace character */
793455Sroot char		*Home;		/* go to home */
803455Sroot char		*cursorm;	/* cursor movement */
813455Sroot char		cursorhome[40];	/* contains cursor movement to home */
823594Sroot char		*EodClr;	/* clear rest of screen */
831500Serics char		*tgetstr();
841500Serics int		Mcol = 80;	/* number of columns */
851500Serics int		Wrap = 1;	/* set if automargins */
8616710Sjak int		soglitch;	/* terminal has standout mode glitch */
8716710Sjak int		ulglitch;	/* terminal has underline mode glitch */
8816710Sjak int		pstate = 0;	/* current UL state */
891500Serics long		fseek();
903455Sroot char		*getenv();
911500Serics struct {
921500Serics     long chrctr, line;
931500Serics } context, screen_start;
941500Serics extern char	PC;		/* pad character */
951500Serics extern short	ospeed;
961500Serics 
971500Serics 
981500Serics main(argc, argv)
991500Serics int argc;
1001500Serics char *argv[];
1011500Serics {
1021500Serics     register FILE	*f;
1031500Serics     register char	*s;
1041500Serics     register char	*p;
1051500Serics     register char	ch;
1061500Serics     register int	left;
10716582Sleres     int			prnames = 0;
1081500Serics     int			initopt = 0;
1091500Serics     int			srchopt = 0;
1101500Serics     int			clearit = 0;
1111500Serics     int			initline;
1121500Serics     char		initbuf[80];
1131500Serics     FILE		*checkf();
1141500Serics 
1151500Serics     nfiles = argc;
1161500Serics     fnames = argv;
1171500Serics     initterm ();
11815813Sralph     nscroll = Lpp/2 - 1;
11915813Sralph     if (nscroll <= 0)
12015813Sralph 	nscroll = 1;
1213455Sroot     if(s = getenv("MORE")) argscan(s);
1221500Serics     while (--nfiles > 0) {
1231500Serics 	if ((ch = (*++fnames)[0]) == '-') {
1243455Sroot 	    argscan(*fnames+1);
1251500Serics 	}
1261500Serics 	else if (ch == '+') {
1271500Serics 	    s = *fnames;
1281500Serics 	    if (*++s == '/') {
1291500Serics 		srchopt++;
1301500Serics 		for (++s, p = initbuf; p < initbuf + 79 && *s != '\0';)
1311500Serics 		    *p++ = *s++;
1321500Serics 		*p = '\0';
1331500Serics 	    }
1341500Serics 	    else {
1351500Serics 		initopt++;
1361500Serics 		for (initline = 0; *s != '\0'; s++)
1371500Serics 		    if (isdigit (*s))
1381500Serics 			initline = initline*10 + *s -'0';
1391500Serics 		--initline;
1401500Serics 	    }
1411500Serics 	}
1421500Serics 	else break;
1431500Serics     }
1443594Sroot     /* allow clreol only if Home and eraseln and EodClr strings are
1453455Sroot      *  defined, and in that case, make sure we are in noscroll mode
1463455Sroot      */
1473455Sroot     if(clreol)
1483455Sroot     {
1493594Sroot 	if ((*Home == '\0') || (*eraseln == '\0') || (*EodClr == '\0'))
1503594Sroot 	    clreol = 0;
1513455Sroot 	else noscroll = 1;
1523455Sroot     }
1533455Sroot 
1541500Serics     if (dlines == 0)
1551500Serics 	dlines = Lpp - (noscroll ? 1 : 2);
1561500Serics     left = dlines;
1571500Serics     if (nfiles > 1)
1581500Serics 	prnames++;
1591500Serics     if (!no_intty && nfiles == 0) {
1601500Serics 	fputs("Usage: ",stderr);
1611500Serics 	fputs(argv[0],stderr);
1621500Serics 	fputs(" [-dfln] [+linenum | +/pattern] name1 name2 ...\n",stderr);
1631500Serics 	exit(1);
1641500Serics     }
1651500Serics     else
1661500Serics 	f = stdin;
1671500Serics     if (!no_tty) {
1681500Serics 	signal(SIGQUIT, onquit);
1691500Serics 	signal(SIGINT, end_it);
1701500Serics 	if (signal (SIGTSTP, SIG_IGN) == SIG_DFL) {
1711500Serics 	    signal(SIGTSTP, onsusp);
1721500Serics 	    catch_susp++;
1731500Serics 	}
17416582Sleres 	stty (fileno(stderr), &otty);
1751500Serics     }
1761500Serics     if (no_intty) {
1771500Serics 	if (no_tty)
1781500Serics 	    copy_file (stdin);
1791500Serics 	else {
1801500Serics 	    if ((ch = Getc (f)) == '\f')
1813594Sroot 		doclear();
1821500Serics 	    else {
1831500Serics 		Ungetc (ch, f);
1843594Sroot 		if (noscroll && (ch != EOF)) {
1853594Sroot 		    if (clreol)
1863594Sroot 			home ();
1873594Sroot 		    else
1883594Sroot 			doclear ();
1893455Sroot 		}
1901500Serics 	    }
1911500Serics 	    if (srchopt)
1923455Sroot 	    {
1931500Serics 		search (initbuf, stdin, 1);
1943594Sroot 		if (noscroll)
1953594Sroot 		    left--;
1963455Sroot 	    }
1971500Serics 	    else if (initopt)
1981500Serics 		skiplns (initline, stdin);
1991500Serics 	    screen (stdin, left);
2001500Serics 	}
2011500Serics 	no_intty = 0;
2021500Serics 	prnames++;
2031500Serics 	firstf = 0;
2041500Serics     }
2051500Serics 
2061500Serics     while (fnum < nfiles) {
2071500Serics 	if ((f = checkf (fnames[fnum], &clearit)) != NULL) {
2081500Serics 	    context.line = context.chrctr = 0;
2091500Serics 	    Currline = 0;
2101500Serics 	    if (firstf) setjmp (restore);
2111500Serics 	    if (firstf) {
2121500Serics 		firstf = 0;
2131500Serics 		if (srchopt)
2143455Sroot 		{
2151500Serics 		    search (initbuf, f, 1);
2163594Sroot 		    if (noscroll)
2173594Sroot 			left--;
2183455Sroot 		}
2191500Serics 		else if (initopt)
2201500Serics 		    skiplns (initline, f);
2211500Serics 	    }
2221500Serics 	    else if (fnum < nfiles && !no_tty) {
2231500Serics 		setjmp (restore);
2241500Serics 		left = command (fnames[fnum], f);
2251500Serics 	    }
2261500Serics 	    if (left != 0) {
2273594Sroot 		if ((noscroll || clearit) && (file_size != 0x7fffffffffffffffL))
2283594Sroot 		    if (clreol)
2293594Sroot 			home ();
2303594Sroot 		    else
2313594Sroot 			doclear ();
2321500Serics 		if (prnames) {
2331500Serics 		    if (bad_so)
2341500Serics 			erase (0);
2353594Sroot 		    if (clreol)
2363594Sroot 			cleareol ();
2371500Serics 		    pr("::::::::::::::");
2381500Serics 		    if (promptlen > 14)
2391500Serics 			erase (14);
2403455Sroot 		    printf ("\n");
2413455Sroot 		    if(clreol) cleareol();
2423455Sroot 		    printf("%s\n", fnames[fnum]);
2433455Sroot 		    if(clreol) cleareol();
2443455Sroot 		    printf("::::::::::::::\n", fnames[fnum]);
2451500Serics 		    if (left > Lpp - 4)
2461500Serics 			left = Lpp - 4;
2471500Serics 		}
2481500Serics 		if (no_tty)
2491500Serics 		    copy_file (f);
2501500Serics 		else {
2511500Serics 		    within++;
2521500Serics 		    screen(f, left);
2531500Serics 		    within = 0;
2541500Serics 		}
2551500Serics 	    }
2561500Serics 	    setjmp (restore);
2571500Serics 	    fflush(stdout);
2581500Serics 	    fclose(f);
2591500Serics 	    screen_start.line = screen_start.chrctr = 0L;
2601500Serics 	    context.line = context.chrctr = 0L;
2611500Serics 	}
2621500Serics 	fnum++;
2631500Serics 	firstf = 0;
2641500Serics     }
2651500Serics     reset_tty ();
2661500Serics     exit(0);
2671500Serics }
2681500Serics 
2693455Sroot argscan(s)
2703455Sroot char *s;
2713455Sroot {
27211604Slayer 	for (dlines = 0; *s != '\0'; s++)
27311604Slayer 	{
27411604Slayer 		switch (*s)
27511604Slayer 		{
27611604Slayer 		  case '0': case '1': case '2':
27711604Slayer 		  case '3': case '4': case '5':
27811604Slayer 		  case '6': case '7': case '8':
27911604Slayer 		  case '9':
28011604Slayer 			dlines = dlines*10 + *s - '0';
28111604Slayer 			break;
28211604Slayer 		  case 'd':
28311604Slayer 			dum_opt = 1;
28411604Slayer 			break;
28511604Slayer 		  case 'l':
28611604Slayer 			stop_opt = 0;
28711604Slayer 			break;
28811604Slayer 		  case 'f':
28911604Slayer 			fold_opt = 0;
29011604Slayer 			break;
29111604Slayer 		  case 'p':
29211604Slayer 			noscroll++;
29311604Slayer 			break;
29411604Slayer 		  case 'c':
29511604Slayer 			clreol++;
29611604Slayer 			break;
29711604Slayer 		  case 's':
29811604Slayer 			ssp_opt = 1;
29911604Slayer 			break;
30011604Slayer 		  case 'u':
30111604Slayer 			ul_opt = 0;
30211604Slayer 			break;
30311604Slayer 		}
30411604Slayer 	}
3053455Sroot }
3063455Sroot 
3073455Sroot 
3081500Serics /*
3091500Serics ** Check whether the file named by fs is an ASCII file which the user may
3101500Serics ** access.  If it is, return the opened file. Otherwise return NULL.
3111500Serics */
3121500Serics 
3131500Serics FILE *
3141500Serics checkf (fs, clearfirst)
3151500Serics register char *fs;
3161500Serics int *clearfirst;
3171500Serics {
3181500Serics     struct stat stbuf;
3191500Serics     register FILE *f;
3201500Serics     char c;
3211500Serics 
3221500Serics     if (stat (fs, &stbuf) == -1) {
3231500Serics 	fflush(stdout);
3243594Sroot 	if (clreol)
3253594Sroot 	    cleareol ();
3261500Serics 	perror(fs);
3271500Serics 	return (NULL);
3281500Serics     }
3291500Serics     if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
3301500Serics 	printf("\n*** %s: directory ***\n\n", fs);
3311500Serics 	return (NULL);
3321500Serics     }
3331500Serics     if ((f=Fopen(fs, "r")) == NULL) {
3341500Serics 	fflush(stdout);
3351500Serics 	perror(fs);
3361500Serics 	return (NULL);
3371500Serics     }
3381500Serics     c = Getc(f);
3391500Serics 
3401500Serics     /* Try to see whether it is an ASCII file */
3411500Serics 
3421500Serics     switch ((c | *f->_ptr << 8) & 0177777) {
3431500Serics     case 0405:
3441500Serics     case 0407:
3451500Serics     case 0410:
3461500Serics     case 0411:
3471500Serics     case 0413:
3481500Serics     case 0177545:
3491500Serics 	printf("\n******** %s: Not a text file ********\n\n", fs);
3501500Serics 	fclose (f);
3511500Serics 	return (NULL);
3521500Serics     default:
3531500Serics 	break;
3541500Serics     }
3551500Serics     if (c == '\f')
3561500Serics 	*clearfirst = 1;
3571500Serics     else {
3581500Serics 	*clearfirst = 0;
3591500Serics 	Ungetc (c, f);
3601500Serics     }
3611500Serics     if ((file_size = stbuf.st_size) == 0)
3621500Serics 	file_size = 0x7fffffffffffffffL;
3631500Serics     return (f);
3641500Serics }
3651500Serics 
3661500Serics /*
3671500Serics ** A real function, for the tputs routine in termlib
3681500Serics */
3691500Serics 
3701500Serics putch (ch)
3711500Serics char ch;
3721500Serics {
3731500Serics     putchar (ch);
3741500Serics }
3751500Serics 
3761500Serics /*
3771500Serics ** Print out the contents of the file f, one screenful at a time.
3781500Serics */
3791500Serics 
3801500Serics #define STOP -10
3811500Serics 
3821500Serics screen (f, num_lines)
3831500Serics register FILE *f;
3841500Serics register int num_lines;
3851500Serics {
3861500Serics     register int c;
3871500Serics     register int nchars;
3883594Sroot     int length;			/* length of current line */
3893594Sroot     static int prev_len = 1;	/* length of previous line */
3901500Serics 
3911500Serics     for (;;) {
3921500Serics 	while (num_lines > 0 && !Pause) {
3931500Serics 	    if ((nchars = getline (f, &length)) == EOF)
3943455Sroot 	    {
3953594Sroot 		if (clreol)
3963594Sroot 		    clreos();
3971500Serics 		return;
3983455Sroot 	    }
3993594Sroot 	    if (ssp_opt && length == 0 && prev_len == 0)
4003594Sroot 		continue;
4013594Sroot 	    prev_len = length;
4021500Serics 	    if (bad_so || (Senter && *Senter == ' ') && promptlen > 0)
4031500Serics 		erase (0);
4043594Sroot 	    /* must clear before drawing line since tabs on some terminals
4053594Sroot 	     * do not erase what they tab over.
4063594Sroot 	     */
4073594Sroot 	    if (clreol)
4083594Sroot 		cleareol ();
4091500Serics 	    prbuf (Line, length);
4101500Serics 	    if (nchars < promptlen)
4111500Serics 		erase (nchars);	/* erase () sets promptlen to 0 */
4121500Serics 	    else promptlen = 0;
4133594Sroot 	    /* is this needed?
4143594Sroot 	     * if (clreol)
4153594Sroot 	     *	cleareol();	/* must clear again in case we wrapped *
4163594Sroot 	     */
4171500Serics 	    if (nchars < Mcol || !fold_opt)
41816710Sjak 		prbuf("\n", 1);	/* will turn off UL if necessary */
4191500Serics 	    if (nchars == STOP)
4201500Serics 		break;
4211500Serics 	    num_lines--;
4221500Serics 	}
42316710Sjak 	if (pstate) {
42416710Sjak 		tputs(ULexit, 1, putch);
42516710Sjak 		pstate = 0;
42616710Sjak 	}
4271500Serics 	fflush(stdout);
4281500Serics 	if ((c = Getc(f)) == EOF)
4293455Sroot 	{
4303594Sroot 	    if (clreol)
4313594Sroot 		clreos ();
4321500Serics 	    return;
4333455Sroot 	}
4343455Sroot 
4353594Sroot 	if (Pause && clreol)
4363594Sroot 	    clreos ();
4371500Serics 	Ungetc (c, f);
4381500Serics 	setjmp (restore);
4391500Serics 	Pause = 0; startup = 0;
4401500Serics 	if ((num_lines = command (NULL, f)) == 0)
4411500Serics 	    return;
4421500Serics 	if (hard && promptlen > 0)
4431500Serics 		erase (0);
44411123Slayer 	if (noscroll && num_lines >= dlines)
44516582Sleres 	{
4463594Sroot 	    if (clreol)
4473594Sroot 		home();
4483594Sroot 	    else
4493594Sroot 		doclear ();
4503455Sroot 	}
4511500Serics 	screen_start.line = Currline;
4521500Serics 	screen_start.chrctr = Ftell (f);
4531500Serics     }
4541500Serics }
4551500Serics 
4561500Serics /*
4571500Serics ** Come here if a quit signal is received
4581500Serics */
4591500Serics 
4601500Serics onquit()
4611500Serics {
4621500Serics     signal(SIGQUIT, SIG_IGN);
4631500Serics     if (!inwait) {
4641500Serics 	putchar ('\n');
4651500Serics 	if (!startup) {
4661500Serics 	    signal(SIGQUIT, onquit);
4671500Serics 	    longjmp (restore, 1);
4681500Serics 	}
4691500Serics 	else
4701500Serics 	    Pause++;
4711500Serics     }
4721500Serics     else if (!dum_opt && notell) {
4731500Serics 	write (2, "[Use q or Q to quit]", 20);
4741500Serics 	promptlen += 20;
4751500Serics 	notell = 0;
4761500Serics     }
4771500Serics     signal(SIGQUIT, onquit);
4781500Serics }
4791500Serics 
4801500Serics /*
4811500Serics ** Clean up terminal state and exit. Also come here if interrupt signal received
4821500Serics */
4831500Serics 
4841500Serics end_it ()
4851500Serics {
4861500Serics 
4871500Serics     reset_tty ();
4883594Sroot     if (clreol) {
4893594Sroot 	putchar ('\r');
4903594Sroot 	clreos ();
4913594Sroot 	fflush (stdout);
4923594Sroot     }
4933455Sroot     else if (!clreol && (promptlen > 0)) {
4941500Serics 	kill_line ();
4951500Serics 	fflush (stdout);
4961500Serics     }
4971500Serics     else
4981500Serics 	write (2, "\n", 1);
4991500Serics     _exit(0);
5001500Serics }
5011500Serics 
5021500Serics copy_file(f)
5031500Serics register FILE *f;
5041500Serics {
5051500Serics     register int c;
5061500Serics 
5071500Serics     while ((c = getc(f)) != EOF)
5081500Serics 	putchar(c);
5091500Serics }
5101500Serics 
5111500Serics /* Simplified printf function */
5121500Serics 
5131500Serics printf (fmt, args)
5141500Serics register char *fmt;
5151500Serics int args;
5161500Serics {
5171500Serics 	register int *argp;
5181500Serics 	register char ch;
5191500Serics 	register int ccount;
5201500Serics 
5211500Serics 	ccount = 0;
5221500Serics 	argp = &args;
5231500Serics 	while (*fmt) {
5241500Serics 		while ((ch = *fmt++) != '%') {
5251500Serics 			if (ch == '\0')
5261500Serics 				return (ccount);
5271500Serics 			ccount++;
5281500Serics 			putchar (ch);
5291500Serics 		}
5301500Serics 		switch (*fmt++) {
5311500Serics 		case 'd':
5321500Serics 			ccount += printd (*argp);
5331500Serics 			break;
5341500Serics 		case 's':
5351500Serics 			ccount += pr ((char *)*argp);
5361500Serics 			break;
5371500Serics 		case '%':
5381500Serics 			ccount++;
5391500Serics 			argp--;
5401500Serics 			putchar ('%');
5411500Serics 			break;
5421500Serics 		case '0':
5431500Serics 			return (ccount);
5441500Serics 		default:
5451500Serics 			break;
5461500Serics 		}
5471500Serics 		++argp;
5481500Serics 	}
5491500Serics 	return (ccount);
5501500Serics 
5511500Serics }
5521500Serics 
5531500Serics /*
5541500Serics ** Print an integer as a string of decimal digits,
5551500Serics ** returning the length of the print representation.
5561500Serics */
5571500Serics 
5581500Serics printd (n)
5591500Serics int n;
5601500Serics {
5611500Serics     int a, nchars;
5621500Serics 
5631500Serics     if (a = n/10)
5641500Serics 	nchars = 1 + printd(a);
5651500Serics     else
5661500Serics 	nchars = 1;
5671500Serics     putchar (n % 10 + '0');
5681500Serics     return (nchars);
5691500Serics }
5701500Serics 
5711500Serics /* Put the print representation of an integer into a string */
5721500Serics static char *sptr;
5731500Serics 
5741500Serics scanstr (n, str)
5751500Serics int n;
5761500Serics char *str;
5771500Serics {
5781500Serics     sptr = str;
57911604Slayer     Sprintf (n);
5801500Serics     *sptr = '\0';
5811500Serics }
5821500Serics 
58311604Slayer Sprintf (n)
5841500Serics {
5851500Serics     int a;
5861500Serics 
5871500Serics     if (a = n/10)
58811604Slayer 	Sprintf (a);
5891500Serics     *sptr++ = n % 10 + '0';
5901500Serics }
5911500Serics 
5921500Serics static char bell = ctrl(G);
5931500Serics 
5941500Serics strlen (s)
5951500Serics char *s;
5961500Serics {
5971500Serics     register char *p;
5981500Serics 
5991500Serics     p = s;
6001500Serics     while (*p++)
6011500Serics 	;
6021500Serics     return (p - s - 1);
6031500Serics }
6041500Serics 
6051500Serics /* See whether the last component of the path name "path" is equal to the
6061500Serics ** string "string"
6071500Serics */
6081500Serics 
6091500Serics tailequ (path, string)
6101500Serics char *path;
6111500Serics register char *string;
6121500Serics {
6131500Serics 	register char *tail;
6141500Serics 
6151500Serics 	tail = path + strlen(path);
6161500Serics 	while (tail >= path)
6171500Serics 		if (*(--tail) == '/')
6181500Serics 			break;
6191500Serics 	++tail;
6201500Serics 	while (*tail++ == *string++)
6211500Serics 		if (*tail == '\0')
6221500Serics 			return(1);
6231500Serics 	return(0);
6241500Serics }
6251500Serics 
6261500Serics prompt (filename)
6271500Serics char *filename;
6281500Serics {
6293594Sroot     if (clreol)
6303594Sroot 	cleareol ();
6313455Sroot     else if (promptlen > 0)
6321500Serics 	kill_line ();
6331500Serics     if (!hard) {
6341500Serics 	promptlen = 8;
63516710Sjak 	if (Senter && Sexit) {
6361500Serics 	    tputs (Senter, 1, putch);
63716710Sjak 	    promptlen += (2 * soglitch);
63816710Sjak 	}
6393594Sroot 	if (clreol)
6403594Sroot 	    cleareol ();
6411500Serics 	pr("--More--");
6421500Serics 	if (filename != NULL) {
6431500Serics 	    promptlen += printf ("(Next file: %s)", filename);
6441500Serics 	}
6451500Serics 	else if (!no_intty) {
6461500Serics 	    promptlen += printf ("(%d%%)", (int)((file_pos * 100) / file_size));
6471500Serics 	}
6481500Serics 	if (dum_opt) {
64916710Sjak 	    promptlen += pr("[Press space to continue, 'q' to quit.]");
6501500Serics 	}
6511500Serics 	if (Senter && Sexit)
6521500Serics 	    tputs (Sexit, 1, putch);
6533594Sroot 	if (clreol)
6543594Sroot 	    clreos ();
6551500Serics 	fflush(stdout);
6561500Serics     }
6571500Serics     else
6581500Serics 	write (2, &bell, 1);
6591500Serics     inwait++;
6601500Serics }
6611500Serics 
6621500Serics /*
6631500Serics ** Get a logical line
6641500Serics */
6651500Serics 
6661500Serics getline(f, length)
6671500Serics register FILE *f;
6681500Serics int *length;
6691500Serics {
6701500Serics     register int	c;
6711500Serics     register char	*p;
6721500Serics     register int	column;
6731500Serics     static int		colflg;
6741500Serics 
6751500Serics     p = Line;
6761500Serics     column = 0;
6771500Serics     c = Getc (f);
6781500Serics     if (colflg && c == '\n') {
6791500Serics 	Currline++;
6801500Serics 	c = Getc (f);
6811500Serics     }
6821500Serics     while (p < &Line[LINSIZ - 1]) {
6831500Serics 	if (c == EOF) {
6841500Serics 	    if (p > Line) {
6851500Serics 		*p = '\0';
6861500Serics 		*length = p - Line;
6871500Serics 		return (column);
6881500Serics 	    }
6891500Serics 	    *length = p - Line;
6901500Serics 	    return (EOF);
6911500Serics 	}
6921500Serics 	if (c == '\n') {
6931500Serics 	    Currline++;
6941500Serics 	    break;
6951500Serics 	}
6961500Serics 	*p++ = c;
6971500Serics 	if (c == '\t')
6981500Serics 	    if (hardtabs && column < promptlen && !hard) {
6991500Serics 		if (eraseln && !dumb) {
7001500Serics 		    column = 1 + (column | 7);
7011500Serics 		    tputs (eraseln, 1, putch);
7021500Serics 		    promptlen = 0;
7031500Serics 		}
7041500Serics 		else {
7051500Serics 		    for (--p; column & 7 && p < &Line[LINSIZ - 1]; column++) {
7061500Serics 			*p++ = ' ';
7071500Serics 		    }
7081500Serics 		    if (column >= promptlen) promptlen = 0;
7091500Serics 		}
7101500Serics 	    }
7111500Serics 	    else
7121500Serics 		column = 1 + (column | 7);
7139627Ssklower 	else if (c == '\b' && column > 0)
7141500Serics 	    column--;
7151500Serics 	else if (c == '\r')
7161500Serics 	    column = 0;
7171500Serics 	else if (c == '\f' && stop_opt) {
7181500Serics 		p[-1] = '^';
7191500Serics 		*p++ = 'L';
7201500Serics 		column += 2;
7211500Serics 		Pause++;
7221500Serics 	}
7231500Serics 	else if (c == EOF) {
7241500Serics 	    *length = p - Line;
7251500Serics 	    return (column);
7261500Serics 	}
7271500Serics 	else if (c >= ' ' && c != RUBOUT)
7281500Serics 	    column++;
7291500Serics 	if (column >= Mcol && fold_opt) break;
7301500Serics 	c = Getc (f);
7311500Serics     }
7321500Serics     if (column >= Mcol && Mcol > 0) {
7331500Serics 	if (!Wrap) {
7341500Serics 	    *p++ = '\n';
7351500Serics 	}
7361500Serics     }
7371500Serics     colflg = column == Mcol && fold_opt;
7381500Serics     *length = p - Line;
7391500Serics     *p = 0;
7401500Serics     return (column);
7411500Serics }
7421500Serics 
7431500Serics /*
7441500Serics ** Erase the rest of the prompt, assuming we are starting at column col.
7451500Serics */
7461500Serics 
7471500Serics erase (col)
7481500Serics register int col;
7491500Serics {
7501500Serics 
7511500Serics     if (promptlen == 0)
7521500Serics 	return;
7531500Serics     if (hard) {
7541500Serics 	putchar ('\n');
7551500Serics     }
7561500Serics     else {
7571500Serics 	if (col == 0)
7581500Serics 	    putchar ('\r');
7591500Serics 	if (!dumb && eraseln)
7601500Serics 	    tputs (eraseln, 1, putch);
7611500Serics 	else
7621500Serics 	    for (col = promptlen - col; col > 0; col--)
7631500Serics 		putchar (' ');
7641500Serics     }
7651500Serics     promptlen = 0;
7661500Serics }
7671500Serics 
7681500Serics /*
7691500Serics ** Erase the current line entirely
7701500Serics */
7711500Serics 
7721500Serics kill_line ()
7731500Serics {
7741500Serics     erase (0);
7751500Serics     if (!eraseln || dumb) putchar ('\r');
7761500Serics }
7771500Serics 
7781500Serics /*
7793455Sroot  * force clear to end of line
7803455Sroot  */
7813455Sroot cleareol()
7823455Sroot {
7833594Sroot     tputs(eraseln, 1, putch);
7843455Sroot }
7853455Sroot 
7863594Sroot clreos()
7873455Sroot {
7883594Sroot     tputs(EodClr, 1, putch);
7893455Sroot }
7903455Sroot 
7913455Sroot /*
7921500Serics **  Print string and return number of characters
7931500Serics */
7941500Serics 
7951500Serics pr(s1)
7961500Serics char	*s1;
7971500Serics {
7981500Serics     register char	*s;
7991500Serics     register char	c;
8001500Serics 
8011500Serics     for (s = s1; c = *s++; )
8021500Serics 	putchar(c);
8031500Serics     return (s - s1 - 1);
8041500Serics }
8051500Serics 
8061500Serics 
8071500Serics /* Print a buffer of n characters */
8081500Serics 
8091500Serics prbuf (s, n)
8101500Serics register char *s;
8111500Serics register int n;
8121500Serics {
81316710Sjak     register char c;			/* next output character */
8143594Sroot     register int state;			/* next output char's UL state */
81516710Sjak #define wouldul(s,n)	((n) >= 2 && (((s)[0] == '_' && (s)[1] == '\b') || ((s)[1] == '\b' && (s)[2] == '_')))
8163594Sroot 
8173594Sroot     while (--n >= 0)
8183594Sroot 	if (!ul_opt)
8193594Sroot 	    putchar (*s++);
8203594Sroot 	else {
82116710Sjak 	    if (*s == ' ' && pstate == 0 && ulglitch && wouldul(s+1, n-1)) {
82216710Sjak 		s++;
82316710Sjak 		continue;
82416710Sjak 	    }
82516710Sjak 	    if (state = wouldul(s, n)) {
82616710Sjak 		c = (*s == '_')? s[2] : *s ;
8273594Sroot 		n -= 2;
82816710Sjak 		s += 3;
82916710Sjak 	    } else
8303594Sroot 		c = *s++;
83116710Sjak 	    if (state != pstate) {
83216710Sjak 		if (c == ' ' && state == 0 && ulglitch && wouldul(s, n-1))
83316710Sjak 		    state = 1;
83416710Sjak 		else
83516710Sjak 		    tputs(state ? ULenter : ULexit, 1, putch);
8363594Sroot 	    }
83716710Sjak 	    if (c != ' ' || pstate == 0 || state != 0 || ulglitch == 0)
83816710Sjak 	        putchar(c);
8393594Sroot 	    if (state && *chUL) {
8403594Sroot 		pr(chBS);
8413594Sroot 		tputs(chUL, 1, putch);
8423594Sroot 	    }
84316710Sjak 	    pstate = state;
8443594Sroot 	}
8451500Serics }
8461500Serics 
8471500Serics /*
8481500Serics **  Clear the screen
8491500Serics */
8501500Serics 
8511500Serics doclear()
8521500Serics {
8531500Serics     if (Clear && !hard) {
8541500Serics 	tputs(Clear, 1, putch);
8551500Serics 
8561500Serics 	/* Put out carriage return so that system doesn't
8571500Serics 	** get confused by escape sequences when expanding tabs
8581500Serics 	*/
8591500Serics 	putchar ('\r');
8601500Serics 	promptlen = 0;
8611500Serics     }
8621500Serics }
8631500Serics 
8643455Sroot /*
8653455Sroot  * Go to home position
8663455Sroot  */
8673455Sroot home()
8683455Sroot {
8693455Sroot     tputs(Home,1,putch);
8703455Sroot }
8713455Sroot 
8721500Serics static int lastcmd, lastarg, lastp;
8731500Serics static int lastcolon;
8741500Serics char shell_line[132];
8751500Serics 
8761500Serics /*
8771500Serics ** Read a command and do it. A command consists of an optional integer
8781500Serics ** argument followed by the command character.  Return the number of lines
8791500Serics ** to display in the next screenful.  If there is nothing more to display
8801500Serics ** in the current file, zero is returned.
8811500Serics */
8821500Serics 
8831500Serics command (filename, f)
8841500Serics char *filename;
8851500Serics register FILE *f;
8861500Serics {
8871500Serics     register int nlines;
8881500Serics     register int retval;
8891500Serics     register char c;
8901500Serics     char colonch;
8911500Serics     FILE *helpf;
8921500Serics     int done;
8931500Serics     char comchar, cmdbuf[80], *p;
8941500Serics 
8951500Serics #define ret(val) retval=val;done++;break
8961500Serics 
8971500Serics     done = 0;
8981500Serics     if (!errors)
8991500Serics 	prompt (filename);
9001500Serics     else
9011500Serics 	errors = 0;
9021500Serics     if (MBIT == RAW && slow_tty) {
9031500Serics 	otty.sg_flags |= MBIT;
90416582Sleres 	stty(fileno(stderr), &otty);
9051500Serics     }
9061500Serics     for (;;) {
9071500Serics 	nlines = number (&comchar);
9081500Serics 	lastp = colonch = 0;
9091500Serics 	if (comchar == '.') {	/* Repeat last command */
9101500Serics 		lastp++;
9111500Serics 		comchar = lastcmd;
9121500Serics 		nlines = lastarg;
9131500Serics 		if (lastcmd == ':')
9141500Serics 			colonch = lastcolon;
9151500Serics 	}
9161500Serics 	lastcmd = comchar;
9171500Serics 	lastarg = nlines;
9181500Serics 	if (comchar == otty.sg_erase) {
9191500Serics 	    kill_line ();
9201500Serics 	    prompt (filename);
9211500Serics 	    continue;
9221500Serics 	}
9231500Serics 	switch (comchar) {
9241500Serics 	case ':':
9251500Serics 	    retval = colon (filename, colonch, nlines);
9261500Serics 	    if (retval >= 0)
9271500Serics 		done++;
9281500Serics 	    break;
9291500Serics 	case ' ':
9301500Serics 	case 'z':
9311500Serics 	    if (nlines == 0) nlines = dlines;
9321500Serics 	    else if (comchar == 'z') dlines = nlines;
9331500Serics 	    ret (nlines);
9341500Serics 	case 'd':
9351500Serics 	case ctrl(D):
9361500Serics 	    if (nlines != 0) nscroll = nlines;
9371500Serics 	    ret (nscroll);
9381500Serics 	case RUBOUT:
9391500Serics 	case 'q':
9401500Serics 	case 'Q':
9411500Serics 	    end_it ();
9421500Serics 	case 's':
9431500Serics 	case 'f':
9441500Serics 	    if (nlines == 0) nlines++;
9451500Serics 	    if (comchar == 'f')
9461500Serics 		nlines *= dlines;
9471500Serics 	    putchar ('\r');
9481500Serics 	    erase (0);
9493594Sroot 	    printf ("\n");
9503594Sroot 	    if (clreol)
9513594Sroot 		cleareol ();
9523594Sroot 	    printf ("...skipping %d line", nlines);
9531500Serics 	    if (nlines > 1)
9543594Sroot 		pr ("s\n");
9551500Serics 	    else
9563594Sroot 		pr ("\n");
9573594Sroot 
9583594Sroot 	    if (clreol)
9593594Sroot 		cleareol ();
9603594Sroot 	    pr ("\n");
9613594Sroot 
9621500Serics 	    while (nlines > 0) {
9631500Serics 		while ((c = Getc (f)) != '\n')
9641500Serics 		    if (c == EOF) {
9651500Serics 			retval = 0;
9661500Serics 			done++;
9671500Serics 			goto endsw;
9681500Serics 		    }
9691500Serics 		    Currline++;
9701500Serics 		    nlines--;
9711500Serics 	    }
9721500Serics 	    ret (dlines);
9731500Serics 	case '\n':
9741500Serics 	    if (nlines != 0)
9751500Serics 		dlines = nlines;
9761500Serics 	    else
9771500Serics 		nlines = 1;
9781500Serics 	    ret (nlines);
9791500Serics 	case '\f':
9801500Serics 	    if (!no_intty) {
9811500Serics 		doclear ();
9821500Serics 		Fseek (f, screen_start.chrctr);
9831500Serics 		Currline = screen_start.line;
9841500Serics 		ret (dlines);
9851500Serics 	    }
9861500Serics 	    else {
9871500Serics 		write (2, &bell, 1);
9881500Serics 		break;
9891500Serics 	    }
9901500Serics 	case '\'':
9911500Serics 	    if (!no_intty) {
9921500Serics 		kill_line ();
9931500Serics 		pr ("\n***Back***\n\n");
9941500Serics 		Fseek (f, context.chrctr);
9951500Serics 		Currline = context.line;
9961500Serics 		ret (dlines);
9971500Serics 	    }
9981500Serics 	    else {
9991500Serics 		write (2, &bell, 1);
10001500Serics 		break;
10011500Serics 	    }
10021500Serics 	case '=':
10031500Serics 	    kill_line ();
10041500Serics 	    promptlen = printd (Currline);
10051500Serics 	    fflush (stdout);
10061500Serics 	    break;
10071500Serics 	case 'n':
10081500Serics 	    lastp++;
10091500Serics 	case '/':
10101500Serics 	    if (nlines == 0) nlines++;
10111500Serics 	    kill_line ();
10121500Serics 	    pr ("/");
10131500Serics 	    promptlen = 1;
10141500Serics 	    fflush (stdout);
10151500Serics 	    if (lastp) {
10161500Serics 		write (2,"\r", 1);
10171500Serics 		search (NULL, f, nlines);	/* Use previous r.e. */
10181500Serics 	    }
10191500Serics 	    else {
10201500Serics 		ttyin (cmdbuf, 78, '/');
10211500Serics 		write (2, "\r", 1);
10221500Serics 		search (cmdbuf, f, nlines);
10231500Serics 	    }
10243455Sroot 	    ret (dlines-1);
10251500Serics 	case '!':
10261500Serics 	    do_shell (filename);
10271500Serics 	    break;
10281500Serics 	case 'h':
10291500Serics 	    if ((helpf = fopen (HELPFILE, "r")) == NULL)
10301500Serics 		error ("Can't open help file");
10311500Serics 	    if (noscroll) doclear ();
10321500Serics 	    copy_file (helpf);
10331500Serics 	    close (helpf);
10341500Serics 	    prompt (filename);
10351500Serics 	    break;
10361500Serics 	case 'v':	/* This case should go right before default */
10371500Serics 	    if (!no_intty) {
10381500Serics 		kill_line ();
10391500Serics 		cmdbuf[0] = '+';
10401500Serics 		scanstr (Currline, &cmdbuf[1]);
10411500Serics 		pr ("vi "); pr (cmdbuf); putchar (' '); pr (fnames[fnum]);
10421500Serics 		execute (filename, VI, "vi", cmdbuf, fnames[fnum], 0);
10431500Serics 		break;
10441500Serics 	    }
10451500Serics 	default:
104616710Sjak 	    if (dum_opt) {
104716710Sjak    		kill_line ();
104816710Sjak 		if (Senter && Sexit) {
104916710Sjak 		    tputs (Senter, 1, putch);
105016710Sjak 		    promptlen = pr ("[Press 'h' for instructions.]") + (2 * soglitch);
105116710Sjak 		    tputs (Sexit, 1, putch);
105216710Sjak 		}
105316710Sjak 		else
105416710Sjak 		    promptlen = pr ("[Press 'h' for instructions.]");
105516710Sjak 		fflush (stdout);
105616710Sjak 	    }
105716710Sjak 	    else
105816710Sjak 		write (2, &bell, 1);
10591500Serics 	    break;
10601500Serics 	}
10611500Serics 	if (done) break;
10621500Serics     }
10631500Serics     putchar ('\r');
10641500Serics endsw:
10651500Serics     inwait = 0;
10661500Serics     notell++;
10671500Serics     if (MBIT == RAW && slow_tty) {
10681500Serics 	otty.sg_flags &= ~MBIT;
106916582Sleres 	stty(fileno(stderr), &otty);
10701500Serics     }
10711500Serics     return (retval);
10721500Serics }
10731500Serics 
10741500Serics char ch;
10751500Serics 
10761500Serics /*
10771500Serics  * Execute a colon-prefixed command.
10781500Serics  * Returns <0 if not a command that should cause
10791500Serics  * more of the file to be printed.
10801500Serics  */
10811500Serics 
10821500Serics colon (filename, cmd, nlines)
10831500Serics char *filename;
10841500Serics int cmd;
10851500Serics int nlines;
10861500Serics {
10871500Serics 	if (cmd == 0)
10881500Serics 		ch = readch ();
10891500Serics 	else
10901500Serics 		ch = cmd;
10911500Serics 	lastcolon = ch;
10921500Serics 	switch (ch) {
10931500Serics 	case 'f':
10941500Serics 		kill_line ();
10951500Serics 		if (!no_intty)
10961500Serics 			promptlen = printf ("\"%s\" line %d", fnames[fnum], Currline);
10971500Serics 		else
10981500Serics 			promptlen = printf ("[Not a file] line %d", Currline);
10991500Serics 		fflush (stdout);
11001500Serics 		return (-1);
11011500Serics 	case 'n':
11021500Serics 		if (nlines == 0) {
11031500Serics 			if (fnum >= nfiles - 1)
11041500Serics 				end_it ();
11051500Serics 			nlines++;
11061500Serics 		}
11071500Serics 		putchar ('\r');
11081500Serics 		erase (0);
11091500Serics 		skipf (nlines);
11101500Serics 		return (0);
11111500Serics 	case 'p':
11121500Serics 		if (no_intty) {
11131500Serics 			write (2, &bell, 1);
11141500Serics 			return (-1);
11151500Serics 		}
11161500Serics 		putchar ('\r');
11171500Serics 		erase (0);
11181500Serics 		if (nlines == 0)
11191500Serics 			nlines++;
11201500Serics 		skipf (-nlines);
11211500Serics 		return (0);
11221500Serics 	case '!':
11231500Serics 		do_shell (filename);
11241500Serics 		return (-1);
11251500Serics 	case 'q':
11261500Serics 	case 'Q':
11271500Serics 		end_it ();
11281500Serics 	default:
11291500Serics 		write (2, &bell, 1);
11301500Serics 		return (-1);
11311500Serics 	}
11321500Serics }
11331500Serics 
11341500Serics /*
11351500Serics ** Read a decimal number from the terminal. Set cmd to the non-digit which
11361500Serics ** terminates the number.
11371500Serics */
11381500Serics 
11391500Serics number(cmd)
11401500Serics char *cmd;
11411500Serics {
11421500Serics 	register int i;
11431500Serics 
11441500Serics 	i = 0; ch = otty.sg_kill;
11451500Serics 	for (;;) {
11461500Serics 		ch = readch ();
11471500Serics 		if (ch >= '0' && ch <= '9')
11481500Serics 			i = i*10 + ch - '0';
11491500Serics 		else if (ch == otty.sg_kill)
11501500Serics 			i = 0;
11511500Serics 		else {
11521500Serics 			*cmd = ch;
11531500Serics 			break;
11541500Serics 		}
11551500Serics 	}
11561500Serics 	return (i);
11571500Serics }
11581500Serics 
11591500Serics do_shell (filename)
11601500Serics char *filename;
11611500Serics {
11621500Serics 	char cmdbuf[80];
11631500Serics 
11641500Serics 	kill_line ();
11651500Serics 	pr ("!");
11661500Serics 	fflush (stdout);
11671500Serics 	promptlen = 1;
11681500Serics 	if (lastp)
11691500Serics 		pr (shell_line);
11701500Serics 	else {
11711500Serics 		ttyin (cmdbuf, 78, '!');
11721500Serics 		if (expand (shell_line, cmdbuf)) {
11731500Serics 			kill_line ();
11741500Serics 			promptlen = printf ("!%s", shell_line);
11751500Serics 		}
11761500Serics 	}
11771500Serics 	fflush (stdout);
11781500Serics 	write (2, "\n", 1);
11791500Serics 	promptlen = 0;
11801500Serics 	shellp = 1;
11811500Serics 	execute (filename, shell, shell, "-c", shell_line, 0);
11821500Serics }
11831500Serics 
11841500Serics /*
11851500Serics ** Search for nth ocurrence of regular expression contained in buf in the file
11861500Serics */
11871500Serics 
11881500Serics search (buf, file, n)
11891500Serics char buf[];
11901500Serics FILE *file;
11911500Serics register int n;
11921500Serics {
11931500Serics     long startline = Ftell (file);
11941500Serics     register long line1 = startline;
11951500Serics     register long line2 = startline;
11961500Serics     register long line3 = startline;
11971500Serics     register int lncount;
11981500Serics     int saveln, rv, re_exec();
11991500Serics     char *s, *re_comp();
12001500Serics 
12011500Serics     context.line = saveln = Currline;
12021500Serics     context.chrctr = startline;
12031500Serics     lncount = 0;
12041500Serics     if ((s = re_comp (buf)) != 0)
12051500Serics 	error (s);
12061500Serics     while (!feof (file)) {
12071500Serics 	line3 = line2;
12081500Serics 	line2 = line1;
12091500Serics 	line1 = Ftell (file);
12101500Serics 	rdline (file);
12111500Serics 	lncount++;
12121500Serics 	if ((rv = re_exec (Line)) == 1)
12131500Serics 		if (--n == 0) {
12141500Serics 		    if (lncount > 3 || (lncount > 1 && no_intty))
12153455Sroot 		    {
12163455Sroot 			pr ("\n");
12173594Sroot 			if (clreol)
12183594Sroot 			    cleareol ();
12193455Sroot 			pr("...skipping\n");
12203455Sroot 		    }
12211500Serics 		    if (!no_intty) {
12221500Serics 			Currline -= (lncount >= 3 ? 3 : lncount);
12231500Serics 			Fseek (file, line3);
12243594Sroot 			if (noscroll)
12253594Sroot 			    if (clreol) {
12263594Sroot 				home ();
12273594Sroot 				cleareol ();
122816582Sleres 			    }
12293594Sroot 			    else
12303594Sroot 				doclear ();
12311500Serics 		    }
12321500Serics 		    else {
12331500Serics 			kill_line ();
12343594Sroot 			if (noscroll)
12353594Sroot 			    if (clreol) {
123616582Sleres 			        home ();
12373594Sroot 			        cleareol ();
123816582Sleres 			    }
12393594Sroot 			    else
12403594Sroot 				doclear ();
12411500Serics 			pr (Line);
12421500Serics 			putchar ('\n');
12431500Serics 		    }
12441500Serics 		    break;
12451500Serics 		}
12461500Serics 	else if (rv == -1)
12471500Serics 	    error ("Regular expression botch");
12481500Serics     }
12491500Serics     if (feof (file)) {
12501500Serics 	if (!no_intty) {
12511500Serics 	    file->_flag &= ~_IOEOF; /* why doesn't fseek do this ??!!??! */
12521500Serics 	    Currline = saveln;
12531500Serics 	    Fseek (file, startline);
12541500Serics 	}
12551500Serics 	else {
12561500Serics 	    pr ("\nPattern not found\n");
12571500Serics 	    end_it ();
12581500Serics 	}
12591500Serics 	error ("Pattern not found");
12601500Serics     }
12611500Serics }
12621500Serics 
12631500Serics execute (filename, cmd, args)
12641500Serics char *filename;
12651500Serics char *cmd, *args;
12661500Serics {
12671500Serics 	int id;
126816710Sjak 	int n;
12691500Serics 
12701500Serics 	fflush (stdout);
12711500Serics 	reset_tty ();
127216710Sjak 	for (n = 10; (id = fork ()) < 0 && n > 0; n--)
12731500Serics 	    sleep (5);
12741500Serics 	if (id == 0) {
127516710Sjak 	    if (!isatty(0)) {
127616710Sjak 		close(0);
127716710Sjak 		open("/dev/tty", 0);
127816710Sjak 	    }
12791500Serics 	    execv (cmd, &args);
12801500Serics 	    write (2, "exec failed\n", 12);
12811500Serics 	    exit (1);
12821500Serics 	}
128316710Sjak 	if (id > 0) {
128416710Sjak 	    signal (SIGINT, SIG_IGN);
128516710Sjak 	    signal (SIGQUIT, SIG_IGN);
128616710Sjak 	    if (catch_susp)
128716710Sjak 		signal(SIGTSTP, SIG_DFL);
128816710Sjak 	    while (wait(0) > 0);
128916710Sjak 	    signal (SIGINT, end_it);
129016710Sjak 	    signal (SIGQUIT, onquit);
129116710Sjak 	    if (catch_susp)
129216710Sjak 		signal(SIGTSTP, onsusp);
129316710Sjak 	} else
129416710Sjak 	    write(2, "can't fork\n", 11);
12951500Serics 	set_tty ();
12961500Serics 	pr ("------------------------\n");
12971500Serics 	prompt (filename);
12981500Serics }
12991500Serics /*
13001500Serics ** Skip n lines in the file f
13011500Serics */
13021500Serics 
13031500Serics skiplns (n, f)
13041500Serics register int n;
13051500Serics register FILE *f;
13061500Serics {
13071500Serics     register char c;
13081500Serics 
13091500Serics     while (n > 0) {
13101500Serics 	while ((c = Getc (f)) != '\n')
13111500Serics 	    if (c == EOF)
13121500Serics 		return;
13131500Serics 	    n--;
13141500Serics 	    Currline++;
13151500Serics     }
13161500Serics }
13171500Serics 
13181500Serics /*
13191500Serics ** Skip nskip files in the file list (from the command line). Nskip may be
13201500Serics ** negative.
13211500Serics */
13221500Serics 
13231500Serics skipf (nskip)
13241500Serics register int nskip;
13251500Serics {
13261500Serics     if (nskip == 0) return;
13271500Serics     if (nskip > 0) {
13281500Serics 	if (fnum + nskip > nfiles - 1)
13291500Serics 	    nskip = nfiles - fnum - 1;
13301500Serics     }
13311500Serics     else if (within)
13321500Serics 	++fnum;
13331500Serics     fnum += nskip;
13341500Serics     if (fnum < 0)
13351500Serics 	fnum = 0;
13363594Sroot     pr ("\n...Skipping ");
13373455Sroot     pr ("\n");
13383594Sroot     if (clreol)
13393594Sroot 	cleareol ();
13403455Sroot     pr ("...Skipping ");
13411500Serics     pr (nskip > 0 ? "to file " : "back to file ");
13421500Serics     pr (fnames[fnum]);
13433455Sroot     pr ("\n");
13443594Sroot     if (clreol)
13453594Sroot 	cleareol ();
13463455Sroot     pr ("\n");
13471500Serics     --fnum;
13481500Serics }
13491500Serics 
13501500Serics /*----------------------------- Terminal I/O -------------------------------*/
13511500Serics 
13521500Serics initterm ()
13531500Serics {
13541500Serics     char	buf[TBUFSIZ];
135517195Sralph     static char	clearbuf[TBUFSIZ];
13561500Serics     char	*clearptr, *padstr;
13571500Serics     int		ldisc;
135817592Sleres     int		lmode;
135910823Ssam     char	*term;
136016582Sleres     int		tgrp;
1361*18030Sbloom     struct winsize win;
13621500Serics 
136316582Sleres retry:
136416582Sleres     if (!(no_tty = gtty(fileno(stdout), &otty))) {
136517592Sleres 	if (ioctl(fileno(stdout), TIOCLGET, &lmode) < 0) {
136617592Sleres 	    perror("TIOCLGET");
136717592Sleres 	    exit(1);
136817592Sleres 	}
136917592Sleres 	docrterase = ((lmode & LCRTERA) != 0);
137017592Sleres 	docrtkill = ((lmode & LCRTKIL) != 0);
137116582Sleres 	/*
137217592Sleres 	 * Wait until we're in the foreground before we save the
137317592Sleres 	 * the terminal modes.
137416582Sleres 	 */
137516582Sleres 	if (ioctl(fileno(stdout), TIOCGPGRP, &tgrp) < 0) {
137617592Sleres 	    perror("TIOCGPGRP");
137716582Sleres 	    exit(1);
137816582Sleres 	}
137916582Sleres 	if (tgrp != getpgrp(0)) {
138016582Sleres 	    kill(0, SIGTTOU);
138116582Sleres 	    goto retry;
138216582Sleres 	}
138313830Skre 	if ((term = getenv("TERM")) == 0 || tgetent(buf, term) <= 0) {
13843594Sroot 	    dumb++; ul_opt = 0;
13851500Serics 	}
13861500Serics 	else {
1387*18030Sbloom 	    if (ioctl(fileno(stdout), TIOCGWINSZ, &win) < 0) {
1388*18030Sbloom 		Lpp = tgetnum("li");
1389*18030Sbloom 		Mcol = tgetnum("co");
1390*18030Sbloom 	    } else {
1391*18030Sbloom 		if ((Lpp = win.ws_row) == 0)
1392*18030Sbloom 		    Lpp = tgetnum("li");
1393*18030Sbloom 		if ((Mcol = win.ws_col) == 0)
1394*18030Sbloom 		    Mcol = tgetnum("co");
1395*18030Sbloom 	    }
1396*18030Sbloom 	    if ((Lpp <= 0) || tgetflag("hc")) {
13971500Serics 		hard++;	/* Hard copy terminal */
13981500Serics 		Lpp = 24;
13991500Serics 	    }
1400*18030Sbloom 	    if (Mcol <= 0)
1401*18030Sbloom 		Mcol = 80;
1402*18030Sbloom 
14031500Serics 	    if (tailequ (fnames[0], "page") || !hard && tgetflag("ns"))
14041500Serics 		noscroll++;
14051500Serics 	    Wrap = tgetflag("am");
14061500Serics 	    bad_so = tgetflag ("xs");
14071500Serics 	    clearptr = clearbuf;
14081500Serics 	    eraseln = tgetstr("ce",&clearptr);
14091500Serics 	    Clear = tgetstr("cl", &clearptr);
14101500Serics 	    Senter = tgetstr("so", &clearptr);
14111500Serics 	    Sexit = tgetstr("se", &clearptr);
141216710Sjak 	    if ((soglitch = tgetnum("sg")) < 0)
141316710Sjak 		soglitch = 0;
14143594Sroot 
14153594Sroot 	    /*
14163594Sroot 	     *  Set up for underlining:  some terminals don't need it;
14173594Sroot 	     *  others have start/stop sequences, still others have an
14183594Sroot 	     *  underline char sequence which is assumed to move the
14193594Sroot 	     *  cursor forward one character.  If underline sequence
14203594Sroot 	     *  isn't available, settle for standout sequence.
14213594Sroot 	     */
14223594Sroot 
14233594Sroot 	    if (tgetflag("ul") || tgetflag("os"))
14243594Sroot 		ul_opt = 0;
14253594Sroot 	    if ((chUL = tgetstr("uc", &clearptr)) == NULL )
14263594Sroot 		chUL = "";
142716710Sjak 	    if (((ULenter = tgetstr("us", &clearptr)) == NULL ||
142816710Sjak 	         (ULexit = tgetstr("ue", &clearptr)) == NULL) && !*chUL) {
142916710Sjak 	        if ((ULenter = Senter) == NULL || (ULexit = Sexit) == NULL) {
143016710Sjak 			ULenter = "";
143116710Sjak 			ULexit = "";
143216710Sjak 		} else
143316710Sjak 			ulglitch = soglitch;
143416710Sjak 	    } else {
143516710Sjak 		if ((ulglitch = tgetnum("ug")) < 0)
143616710Sjak 		    ulglitch = 0;
143716710Sjak 	    }
143816582Sleres 
14391500Serics 	    if (padstr = tgetstr("pc", &clearptr))
14401500Serics 		PC = *padstr;
14413455Sroot 	    Home = tgetstr("ho",&clearptr);
144213536Ssam 	    if (Home == 0 || *Home == '\0')
14433455Sroot 	    {
14443594Sroot 		if ((cursorm = tgetstr("cm", &clearptr)) != NULL) {
14453594Sroot 		    strcpy(cursorhome, tgoto(cursorm, 0, 0));
14463455Sroot 		    Home = cursorhome;
14473455Sroot 	       }
14483455Sroot 	    }
14493594Sroot 	    EodClr = tgetstr("cd", &clearptr);
14501500Serics 	}
14511500Serics 	if ((shell = getenv("SHELL")) == NULL)
14521500Serics 	    shell = "/bin/sh";
14531500Serics     }
145416582Sleres     no_intty = gtty(fileno(stdin), &otty);
145516582Sleres     gtty(fileno(stderr), &otty);
145613830Skre     savetty = otty;
14571500Serics     ospeed = otty.sg_ospeed;
14581500Serics     slow_tty = ospeed < B1200;
14591500Serics     hardtabs =  !(otty.sg_flags & XTABS);
14601500Serics     if (!no_tty) {
14611500Serics 	otty.sg_flags &= ~ECHO;
14621500Serics 	if (MBIT == CBREAK || !slow_tty)
14631500Serics 	    otty.sg_flags |= MBIT;
14641500Serics     }
14651500Serics }
14661500Serics 
14671500Serics readch ()
14681500Serics {
14691500Serics 	char ch;
14701500Serics 	extern int errno;
14711500Serics 
14721500Serics 	if (read (2, &ch, 1) <= 0)
14731500Serics 		if (errno != EINTR)
14741500Serics 			exit(0);
14751500Serics 		else
14761500Serics 			ch = otty.sg_kill;
14771500Serics 	return (ch);
14781500Serics }
14791500Serics 
14801500Serics static char BS = '\b';
148117592Sleres static char *BSB = "\b \b";
14821500Serics static char CARAT = '^';
148317592Sleres #define ERASEONECHAR \
148417592Sleres     if (docrterase) \
148517592Sleres 	write (2, BSB, sizeof(BSB)); \
148617592Sleres     else \
148717592Sleres 	write (2, &BS, sizeof(BS));
14881500Serics 
14891500Serics ttyin (buf, nmax, pchar)
14901500Serics char buf[];
14911500Serics register int nmax;
14921500Serics char pchar;
14931500Serics {
14941500Serics     register char *sptr;
14951500Serics     register char ch;
14961500Serics     register int slash = 0;
14971500Serics     int	maxlen;
14981500Serics     char cbuf;
14991500Serics 
15001500Serics     sptr = buf;
15011500Serics     maxlen = 0;
15021500Serics     while (sptr - buf < nmax) {
15031500Serics 	if (promptlen > maxlen) maxlen = promptlen;
15041500Serics 	ch = readch ();
15051500Serics 	if (ch == '\\') {
15061500Serics 	    slash++;
15071500Serics 	}
15081500Serics 	else if ((ch == otty.sg_erase) && !slash) {
15091500Serics 	    if (sptr > buf) {
15101500Serics 		--promptlen;
151117592Sleres 		ERASEONECHAR
15121500Serics 		--sptr;
15131500Serics 		if ((*sptr < ' ' && *sptr != '\n') || *sptr == RUBOUT) {
15141500Serics 		    --promptlen;
151517592Sleres 		    ERASEONECHAR
15161500Serics 		}
15171500Serics 		continue;
15181500Serics 	    }
15191500Serics 	    else {
15201500Serics 		if (!eraseln) promptlen = maxlen;
15211500Serics 		longjmp (restore, 1);
15221500Serics 	    }
15231500Serics 	}
15241500Serics 	else if ((ch == otty.sg_kill) && !slash) {
15251500Serics 	    if (hard) {
15261500Serics 		show (ch);
15271500Serics 		putchar ('\n');
15281500Serics 		putchar (pchar);
15291500Serics 	    }
15301500Serics 	    else {
15311500Serics 		putchar ('\r');
15321500Serics 		putchar (pchar);
15331500Serics 		if (eraseln)
15341500Serics 		    erase (1);
153517592Sleres 		else if (docrtkill)
153617592Sleres 		    while (promptlen-- > 1)
153717592Sleres 			write (2, BSB, sizeof(BSB));
15381500Serics 		promptlen = 1;
15391500Serics 	    }
15401500Serics 	    sptr = buf;
15411500Serics 	    fflush (stdout);
15421500Serics 	    continue;
15431500Serics 	}
15441500Serics 	if (slash && (ch == otty.sg_kill || ch == otty.sg_erase)) {
154517592Sleres 	    ERASEONECHAR
15461500Serics 	    --sptr;
15471500Serics 	}
15481500Serics 	if (ch != '\\')
15491500Serics 	    slash = 0;
15501500Serics 	*sptr++ = ch;
15511500Serics 	if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) {
15521500Serics 	    ch += ch == RUBOUT ? -0100 : 0100;
15531500Serics 	    write (2, &CARAT, 1);
15541500Serics 	    promptlen++;
15551500Serics 	}
15561500Serics 	cbuf = ch;
15571500Serics 	if (ch != '\n' && ch != ESC) {
15581500Serics 	    write (2, &cbuf, 1);
15591500Serics 	    promptlen++;
15601500Serics 	}
15611500Serics 	else
15621500Serics 	    break;
15631500Serics     }
15641500Serics     *--sptr = '\0';
15651500Serics     if (!eraseln) promptlen = maxlen;
15661500Serics     if (sptr - buf >= nmax - 1)
15671500Serics 	error ("Line too long");
15681500Serics }
15691500Serics 
15701500Serics expand (outbuf, inbuf)
15711500Serics char *outbuf;
15721500Serics char *inbuf;
15731500Serics {
15741500Serics     register char *instr;
15751500Serics     register char *outstr;
15761500Serics     register char ch;
15771500Serics     char temp[200];
15781500Serics     int changed = 0;
15791500Serics 
15801500Serics     instr = inbuf;
15811500Serics     outstr = temp;
15821500Serics     while ((ch = *instr++) != '\0')
15831500Serics 	switch (ch) {
15841500Serics 	case '%':
15851500Serics 	    if (!no_intty) {
15861500Serics 		strcpy (outstr, fnames[fnum]);
15871500Serics 		outstr += strlen (fnames[fnum]);
15881500Serics 		changed++;
15891500Serics 	    }
15901500Serics 	    else
15911500Serics 		*outstr++ = ch;
15921500Serics 	    break;
15931500Serics 	case '!':
15941500Serics 	    if (!shellp)
15951500Serics 		error ("No previous command to substitute for");
15961500Serics 	    strcpy (outstr, shell_line);
15971500Serics 	    outstr += strlen (shell_line);
15981500Serics 	    changed++;
15991500Serics 	    break;
16001500Serics 	case '\\':
16011500Serics 	    if (*instr == '%' || *instr == '!') {
16021500Serics 		*outstr++ = *instr++;
16031500Serics 		break;
16041500Serics 	    }
16051500Serics 	default:
16061500Serics 	    *outstr++ = ch;
16071500Serics 	}
16081500Serics     *outstr++ = '\0';
16091500Serics     strcpy (outbuf, temp);
16101500Serics     return (changed);
16111500Serics }
16121500Serics 
16131500Serics show (ch)
16141500Serics register char ch;
16151500Serics {
16161500Serics     char cbuf;
16171500Serics 
16181500Serics     if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) {
16191500Serics 	ch += ch == RUBOUT ? -0100 : 0100;
16201500Serics 	write (2, &CARAT, 1);
16211500Serics 	promptlen++;
16221500Serics     }
16231500Serics     cbuf = ch;
16241500Serics     write (2, &cbuf, 1);
16251500Serics     promptlen++;
16261500Serics }
16271500Serics 
16281500Serics error (mess)
16291500Serics char *mess;
16301500Serics {
16313594Sroot     if (clreol)
16323594Sroot 	cleareol ();
16333594Sroot     else
16343594Sroot 	kill_line ();
16351500Serics     promptlen += strlen (mess);
16361500Serics     if (Senter && Sexit) {
16371500Serics 	tputs (Senter, 1, putch);
16381500Serics 	pr(mess);
16391500Serics 	tputs (Sexit, 1, putch);
16401500Serics     }
16411500Serics     else
16421500Serics 	pr (mess);
16431500Serics     fflush(stdout);
16441500Serics     errors++;
16451500Serics     longjmp (restore, 1);
16461500Serics }
16471500Serics 
16481500Serics 
16491500Serics set_tty ()
16501500Serics {
16511500Serics 	otty.sg_flags |= MBIT;
16521500Serics 	otty.sg_flags &= ~ECHO;
165316582Sleres 	stty(fileno(stderr), &otty);
16541500Serics }
16551500Serics 
16561500Serics reset_tty ()
16571500Serics {
165816710Sjak     if (pstate) {
165916710Sjak 	tputs(ULexit, 1, putch);
166016710Sjak 	fflush(stdout);
166116710Sjak 	pstate = 0;
166216710Sjak     }
16631500Serics     otty.sg_flags |= ECHO;
16641500Serics     otty.sg_flags &= ~MBIT;
166516582Sleres     stty(fileno(stderr), &savetty);
16661500Serics }
16671500Serics 
16681500Serics rdline (f)
16691500Serics register FILE *f;
16701500Serics {
16711500Serics     register char c;
16721500Serics     register char *p;
16731500Serics 
16741500Serics     p = Line;
16751500Serics     while ((c = Getc (f)) != '\n' && c != EOF && p - Line < LINSIZ - 1)
16761500Serics 	*p++ = c;
16771500Serics     if (c == '\n')
16781500Serics 	Currline++;
16791500Serics     *p = '\0';
16801500Serics }
16811500Serics 
16821500Serics /* Come here when we get a suspend signal from the terminal */
16831500Serics 
16841500Serics onsusp ()
16851500Serics {
168614861Skarels     /* ignore SIGTTOU so we don't get stopped if csh grabs the tty */
168714861Skarels     signal(SIGTTOU, SIG_IGN);
16881500Serics     reset_tty ();
16891500Serics     fflush (stdout);
169014861Skarels     signal(SIGTTOU, SIG_DFL);
16911500Serics     /* Send the TSTP signal to suspend our process group */
169213289Ssam     signal(SIGTSTP, SIG_DFL);
169313289Ssam     sigsetmask(0);
16941500Serics     kill (0, SIGTSTP);
16951500Serics     /* Pause for station break */
16961500Serics 
16971500Serics     /* We're back */
16981500Serics     signal (SIGTSTP, onsusp);
16991500Serics     set_tty ();
17001500Serics     if (inwait)
17011500Serics 	    longjmp (restore);
17021500Serics }
1703