xref: /csrg-svn/old/more/more.c (revision 13615)
1*13615Ssam #ifndef lint
2*13615Ssam static	char *sccsid = "@(#)more.c	4.14 (Berkeley) 83/07/02";
3*13615Ssam #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>
151500Serics #include <ctype.h>
161500Serics #include <signal.h>
171500Serics #include <errno.h>
181500Serics #include <sgtty.h>
191500Serics #include <setjmp.h>
201500Serics #include <sys/types.h>
211500Serics #include <sys/stat.h>
221500Serics 
23*13615Ssam #define HELPFILE	"/usr/lib/more.help"
24*13615Ssam #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 
421500Serics struct sgttyb 	otty;
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;
571500Serics int		bad_so;	/* True if overwriting does not turn off standout */
581500Serics int		inwait, Pause, errors;
591500Serics int		within;	/* true if we are within a file,
601500Serics 			false if we are between files */
613594Sroot int		hard, dumb, noscroll, hardtabs, clreol;
621500Serics int		catch_susp;	/* We should catch the SIGTSTP signal */
631500Serics char		**fnames;	/* The list of file names */
641500Serics int		nfiles;		/* Number of files left to process */
651500Serics char		*shell;		/* The name of the shell to use */
661500Serics int		shellp;		/* A previous shell command exists */
671500Serics char		ch;
681500Serics jmp_buf		restore;
691500Serics char		obuf[BUFSIZ];	/* stdout buffer */
701500Serics char		Line[LINSIZ];	/* Line buffer */
711500Serics int		Lpp = 24;	/* lines per page */
721500Serics char		*Clear;		/* clear screen */
731500Serics char		*eraseln;	/* erase line */
741500Serics char		*Senter, *Sexit;/* enter and exit standout mode */
753594Sroot char		*ULenter, *ULexit;	/* enter and exit underline mode */
763594Sroot char		*chUL;		/* underline character */
773594Sroot char		*chBS;		/* backspace character */
783455Sroot char		*Home;		/* go to home */
793455Sroot char		*cursorm;	/* cursor movement */
803455Sroot char		cursorhome[40];	/* contains cursor movement to home */
813594Sroot char		*EodClr;	/* clear rest of screen */
821500Serics char		*tgetstr();
831500Serics int		Mcol = 80;	/* number of columns */
841500Serics int		Wrap = 1;	/* set if automargins */
851500Serics long		fseek();
863455Sroot char		*getenv();
871500Serics struct {
881500Serics     long chrctr, line;
891500Serics } context, screen_start;
901500Serics extern char	PC;		/* pad character */
911500Serics extern short	ospeed;
921500Serics 
931500Serics 
941500Serics main(argc, argv)
951500Serics int argc;
961500Serics char *argv[];
971500Serics {
981500Serics     register FILE	*f;
991500Serics     register char	*s;
1001500Serics     register char	*p;
1011500Serics     register char	ch;
1021500Serics     register int	left;
1031500Serics     int			prnames = 0;
1041500Serics     int			initopt = 0;
1051500Serics     int			srchopt = 0;
1061500Serics     int			clearit = 0;
1071500Serics     int			initline;
1081500Serics     char		initbuf[80];
1091500Serics     FILE		*checkf();
1101500Serics 
1111500Serics     nfiles = argc;
1121500Serics     fnames = argv;
1131500Serics     initterm ();
1143455Sroot     if(s = getenv("MORE")) argscan(s);
1151500Serics     while (--nfiles > 0) {
1161500Serics 	if ((ch = (*++fnames)[0]) == '-') {
1173455Sroot 	    argscan(*fnames+1);
1181500Serics 	}
1191500Serics 	else if (ch == '+') {
1201500Serics 	    s = *fnames;
1211500Serics 	    if (*++s == '/') {
1221500Serics 		srchopt++;
1231500Serics 		for (++s, p = initbuf; p < initbuf + 79 && *s != '\0';)
1241500Serics 		    *p++ = *s++;
1251500Serics 		*p = '\0';
1261500Serics 	    }
1271500Serics 	    else {
1281500Serics 		initopt++;
1291500Serics 		for (initline = 0; *s != '\0'; s++)
1301500Serics 		    if (isdigit (*s))
1311500Serics 			initline = initline*10 + *s -'0';
1321500Serics 		--initline;
1331500Serics 	    }
1341500Serics 	}
1351500Serics 	else break;
1361500Serics     }
1373594Sroot     /* allow clreol only if Home and eraseln and EodClr strings are
1383455Sroot      *  defined, and in that case, make sure we are in noscroll mode
1393455Sroot      */
1403455Sroot     if(clreol)
1413455Sroot     {
1423594Sroot 	if ((*Home == '\0') || (*eraseln == '\0') || (*EodClr == '\0'))
1433594Sroot 	    clreol = 0;
1443455Sroot 	else noscroll = 1;
1453455Sroot     }
1463455Sroot 
1471500Serics     if (dlines == 0)
1481500Serics 	dlines = Lpp - (noscroll ? 1 : 2);
1491500Serics     left = dlines;
1501500Serics     if (nfiles > 1)
1511500Serics 	prnames++;
1521500Serics     if (!no_intty && nfiles == 0) {
1531500Serics 	fputs("Usage: ",stderr);
1541500Serics 	fputs(argv[0],stderr);
1551500Serics 	fputs(" [-dfln] [+linenum | +/pattern] name1 name2 ...\n",stderr);
1561500Serics 	exit(1);
1571500Serics     }
1581500Serics     else
1591500Serics 	f = stdin;
1601500Serics     if (!no_tty) {
1611500Serics 	signal(SIGQUIT, onquit);
1621500Serics 	signal(SIGINT, end_it);
1631500Serics 	if (signal (SIGTSTP, SIG_IGN) == SIG_DFL) {
1641500Serics 	    signal(SIGTSTP, onsusp);
1651500Serics 	    catch_susp++;
1661500Serics 	}
1671500Serics 	stty (2, &otty);
1681500Serics     }
1691500Serics     if (no_intty) {
1701500Serics 	if (no_tty)
1711500Serics 	    copy_file (stdin);
1721500Serics 	else {
1731500Serics 	    if ((ch = Getc (f)) == '\f')
1743594Sroot 		doclear();
1751500Serics 	    else {
1761500Serics 		Ungetc (ch, f);
1773594Sroot 		if (noscroll && (ch != EOF)) {
1783594Sroot 		    if (clreol)
1793594Sroot 			home ();
1803594Sroot 		    else
1813594Sroot 			doclear ();
1823455Sroot 		}
1831500Serics 	    }
1841500Serics 	    if (srchopt)
1853455Sroot 	    {
1861500Serics 		search (initbuf, stdin, 1);
1873594Sroot 		if (noscroll)
1883594Sroot 		    left--;
1893455Sroot 	    }
1901500Serics 	    else if (initopt)
1911500Serics 		skiplns (initline, stdin);
1921500Serics 	    screen (stdin, left);
1931500Serics 	}
1941500Serics 	no_intty = 0;
1951500Serics 	prnames++;
1961500Serics 	firstf = 0;
1971500Serics     }
1981500Serics 
1991500Serics     while (fnum < nfiles) {
2001500Serics 	if ((f = checkf (fnames[fnum], &clearit)) != NULL) {
2011500Serics 	    context.line = context.chrctr = 0;
2021500Serics 	    Currline = 0;
2031500Serics 	    if (firstf) setjmp (restore);
2041500Serics 	    if (firstf) {
2051500Serics 		firstf = 0;
2061500Serics 		if (srchopt)
2073455Sroot 		{
2081500Serics 		    search (initbuf, f, 1);
2093594Sroot 		    if (noscroll)
2103594Sroot 			left--;
2113455Sroot 		}
2121500Serics 		else if (initopt)
2131500Serics 		    skiplns (initline, f);
2141500Serics 	    }
2151500Serics 	    else if (fnum < nfiles && !no_tty) {
2161500Serics 		setjmp (restore);
2171500Serics 		left = command (fnames[fnum], f);
2181500Serics 	    }
2191500Serics 	    if (left != 0) {
2203594Sroot 		if ((noscroll || clearit) && (file_size != 0x7fffffffffffffffL))
2213594Sroot 		    if (clreol)
2223594Sroot 			home ();
2233594Sroot 		    else
2243594Sroot 			doclear ();
2251500Serics 		if (prnames) {
2261500Serics 		    if (bad_so)
2271500Serics 			erase (0);
2283594Sroot 		    if (clreol)
2293594Sroot 			cleareol ();
2301500Serics 		    pr("::::::::::::::");
2311500Serics 		    if (promptlen > 14)
2321500Serics 			erase (14);
2333455Sroot 		    printf ("\n");
2343455Sroot 		    if(clreol) cleareol();
2353455Sroot 		    printf("%s\n", fnames[fnum]);
2363455Sroot 		    if(clreol) cleareol();
2373455Sroot 		    printf("::::::::::::::\n", fnames[fnum]);
2381500Serics 		    if (left > Lpp - 4)
2391500Serics 			left = Lpp - 4;
2401500Serics 		}
2411500Serics 		if (no_tty)
2421500Serics 		    copy_file (f);
2431500Serics 		else {
2441500Serics 		    within++;
2451500Serics 		    screen(f, left);
2461500Serics 		    within = 0;
2471500Serics 		}
2481500Serics 	    }
2491500Serics 	    setjmp (restore);
2501500Serics 	    fflush(stdout);
2511500Serics 	    fclose(f);
2521500Serics 	    screen_start.line = screen_start.chrctr = 0L;
2531500Serics 	    context.line = context.chrctr = 0L;
2541500Serics 	}
2551500Serics 	fnum++;
2561500Serics 	firstf = 0;
2571500Serics     }
2581500Serics     reset_tty ();
2591500Serics     exit(0);
2601500Serics }
2611500Serics 
2623455Sroot argscan(s)
2633455Sroot char *s;
2643455Sroot {
26511604Slayer 	for (dlines = 0; *s != '\0'; s++)
26611604Slayer 	{
26711604Slayer 		switch (*s)
26811604Slayer 		{
26911604Slayer 		  case '0': case '1': case '2':
27011604Slayer 		  case '3': case '4': case '5':
27111604Slayer 		  case '6': case '7': case '8':
27211604Slayer 		  case '9':
27311604Slayer 			dlines = dlines*10 + *s - '0';
27411604Slayer 			break;
27511604Slayer 		  case 'd':
27611604Slayer 			dum_opt = 1;
27711604Slayer 			break;
27811604Slayer 		  case 'l':
27911604Slayer 			stop_opt = 0;
28011604Slayer 			break;
28111604Slayer 		  case 'f':
28211604Slayer 			fold_opt = 0;
28311604Slayer 			break;
28411604Slayer 		  case 'p':
28511604Slayer 			noscroll++;
28611604Slayer 			break;
28711604Slayer 		  case 'c':
28811604Slayer 			clreol++;
28911604Slayer 			break;
29011604Slayer 		  case 's':
29111604Slayer 			ssp_opt = 1;
29211604Slayer 			break;
29311604Slayer 		  case 'u':
29411604Slayer 			ul_opt = 0;
29511604Slayer 			break;
29611604Slayer 		}
29711604Slayer 	}
2983455Sroot }
2993455Sroot 
3003455Sroot 
3011500Serics /*
3021500Serics ** Check whether the file named by fs is an ASCII file which the user may
3031500Serics ** access.  If it is, return the opened file. Otherwise return NULL.
3041500Serics */
3051500Serics 
3061500Serics FILE *
3071500Serics checkf (fs, clearfirst)
3081500Serics register char *fs;
3091500Serics int *clearfirst;
3101500Serics {
3111500Serics     struct stat stbuf;
3121500Serics     register FILE *f;
3131500Serics     char c;
3141500Serics 
3151500Serics     if (stat (fs, &stbuf) == -1) {
3161500Serics 	fflush(stdout);
3173594Sroot 	if (clreol)
3183594Sroot 	    cleareol ();
3191500Serics 	perror(fs);
3201500Serics 	return (NULL);
3211500Serics     }
3221500Serics     if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
3231500Serics 	printf("\n*** %s: directory ***\n\n", fs);
3241500Serics 	return (NULL);
3251500Serics     }
3261500Serics     if ((f=Fopen(fs, "r")) == NULL) {
3271500Serics 	fflush(stdout);
3281500Serics 	perror(fs);
3291500Serics 	return (NULL);
3301500Serics     }
3311500Serics     c = Getc(f);
3321500Serics 
3331500Serics     /* Try to see whether it is an ASCII file */
3341500Serics 
3351500Serics     switch ((c | *f->_ptr << 8) & 0177777) {
3361500Serics     case 0405:
3371500Serics     case 0407:
3381500Serics     case 0410:
3391500Serics     case 0411:
3401500Serics     case 0413:
3411500Serics     case 0177545:
3421500Serics 	printf("\n******** %s: Not a text file ********\n\n", fs);
3431500Serics 	fclose (f);
3441500Serics 	return (NULL);
3451500Serics     default:
3461500Serics 	break;
3471500Serics     }
3481500Serics     if (c == '\f')
3491500Serics 	*clearfirst = 1;
3501500Serics     else {
3511500Serics 	*clearfirst = 0;
3521500Serics 	Ungetc (c, f);
3531500Serics     }
3541500Serics     if ((file_size = stbuf.st_size) == 0)
3551500Serics 	file_size = 0x7fffffffffffffffL;
3561500Serics     return (f);
3571500Serics }
3581500Serics 
3591500Serics /*
3601500Serics ** A real function, for the tputs routine in termlib
3611500Serics */
3621500Serics 
3631500Serics putch (ch)
3641500Serics char ch;
3651500Serics {
3661500Serics     putchar (ch);
3671500Serics }
3681500Serics 
3691500Serics /*
3701500Serics ** Print out the contents of the file f, one screenful at a time.
3711500Serics */
3721500Serics 
3731500Serics #define STOP -10
3741500Serics 
3751500Serics screen (f, num_lines)
3761500Serics register FILE *f;
3771500Serics register int num_lines;
3781500Serics {
3791500Serics     register int c;
3801500Serics     register int nchars;
3813594Sroot     int length;			/* length of current line */
3823594Sroot     static int prev_len = 1;	/* length of previous line */
3831500Serics 
3841500Serics     for (;;) {
3851500Serics 	while (num_lines > 0 && !Pause) {
3861500Serics 	    if ((nchars = getline (f, &length)) == EOF)
3873455Sroot 	    {
3883594Sroot 		if (clreol)
3893594Sroot 		    clreos();
3901500Serics 		return;
3913455Sroot 	    }
3923594Sroot 	    if (ssp_opt && length == 0 && prev_len == 0)
3933594Sroot 		continue;
3943594Sroot 	    prev_len = length;
3951500Serics 	    if (bad_so || (Senter && *Senter == ' ') && promptlen > 0)
3961500Serics 		erase (0);
3973594Sroot 	    /* must clear before drawing line since tabs on some terminals
3983594Sroot 	     * do not erase what they tab over.
3993594Sroot 	     */
4003594Sroot 	    if (clreol)
4013594Sroot 		cleareol ();
4021500Serics 	    prbuf (Line, length);
4031500Serics 	    if (nchars < promptlen)
4041500Serics 		erase (nchars);	/* erase () sets promptlen to 0 */
4051500Serics 	    else promptlen = 0;
4063594Sroot 	    /* is this needed?
4073594Sroot 	     * if (clreol)
4083594Sroot 	     *	cleareol();	/* must clear again in case we wrapped *
4093594Sroot 	     */
4101500Serics 	    if (nchars < Mcol || !fold_opt)
4111500Serics 		putchar('\n');
4121500Serics 	    if (nchars == STOP)
4131500Serics 		break;
4141500Serics 	    num_lines--;
4151500Serics 	}
4161500Serics 	fflush(stdout);
4171500Serics 	if ((c = Getc(f)) == EOF)
4183455Sroot 	{
4193594Sroot 	    if (clreol)
4203594Sroot 		clreos ();
4211500Serics 	    return;
4223455Sroot 	}
4233455Sroot 
4243594Sroot 	if (Pause && clreol)
4253594Sroot 	    clreos ();
4261500Serics 	Ungetc (c, f);
4271500Serics 	setjmp (restore);
4281500Serics 	Pause = 0; startup = 0;
4291500Serics 	if ((num_lines = command (NULL, f)) == 0)
4301500Serics 	    return;
4311500Serics 	if (hard && promptlen > 0)
4321500Serics 		erase (0);
43311123Slayer 	if (noscroll && num_lines >= dlines)
4343455Sroot 	{
4353594Sroot 	    if (clreol)
4363594Sroot 		home();
4373594Sroot 	    else
4383594Sroot 		doclear ();
4393455Sroot 	}
4401500Serics 	screen_start.line = Currline;
4411500Serics 	screen_start.chrctr = Ftell (f);
4421500Serics     }
4431500Serics }
4441500Serics 
4451500Serics /*
4461500Serics ** Come here if a quit signal is received
4471500Serics */
4481500Serics 
4491500Serics onquit()
4501500Serics {
4511500Serics     signal(SIGQUIT, SIG_IGN);
4521500Serics     if (!inwait) {
4531500Serics 	putchar ('\n');
4541500Serics 	if (!startup) {
4551500Serics 	    signal(SIGQUIT, onquit);
4561500Serics 	    longjmp (restore, 1);
4571500Serics 	}
4581500Serics 	else
4591500Serics 	    Pause++;
4601500Serics     }
4611500Serics     else if (!dum_opt && notell) {
4621500Serics 	write (2, "[Use q or Q to quit]", 20);
4631500Serics 	promptlen += 20;
4641500Serics 	notell = 0;
4651500Serics     }
4661500Serics     signal(SIGQUIT, onquit);
4671500Serics }
4681500Serics 
4691500Serics /*
4701500Serics ** Clean up terminal state and exit. Also come here if interrupt signal received
4711500Serics */
4721500Serics 
4731500Serics end_it ()
4741500Serics {
4751500Serics 
4761500Serics     reset_tty ();
4773594Sroot     if (clreol) {
4783594Sroot 	putchar ('\r');
4793594Sroot 	clreos ();
4803594Sroot 	fflush (stdout);
4813594Sroot     }
4823455Sroot     else if (!clreol && (promptlen > 0)) {
4831500Serics 	kill_line ();
4841500Serics 	fflush (stdout);
4851500Serics     }
4861500Serics     else
4871500Serics 	write (2, "\n", 1);
4881500Serics     _exit(0);
4891500Serics }
4901500Serics 
4911500Serics copy_file(f)
4921500Serics register FILE *f;
4931500Serics {
4941500Serics     register int c;
4951500Serics 
4961500Serics     while ((c = getc(f)) != EOF)
4971500Serics 	putchar(c);
4981500Serics }
4991500Serics 
5001500Serics /* Simplified printf function */
5011500Serics 
5021500Serics printf (fmt, args)
5031500Serics register char *fmt;
5041500Serics int args;
5051500Serics {
5061500Serics 	register int *argp;
5071500Serics 	register char ch;
5081500Serics 	register int ccount;
5091500Serics 
5101500Serics 	ccount = 0;
5111500Serics 	argp = &args;
5121500Serics 	while (*fmt) {
5131500Serics 		while ((ch = *fmt++) != '%') {
5141500Serics 			if (ch == '\0')
5151500Serics 				return (ccount);
5161500Serics 			ccount++;
5171500Serics 			putchar (ch);
5181500Serics 		}
5191500Serics 		switch (*fmt++) {
5201500Serics 		case 'd':
5211500Serics 			ccount += printd (*argp);
5221500Serics 			break;
5231500Serics 		case 's':
5241500Serics 			ccount += pr ((char *)*argp);
5251500Serics 			break;
5261500Serics 		case '%':
5271500Serics 			ccount++;
5281500Serics 			argp--;
5291500Serics 			putchar ('%');
5301500Serics 			break;
5311500Serics 		case '0':
5321500Serics 			return (ccount);
5331500Serics 		default:
5341500Serics 			break;
5351500Serics 		}
5361500Serics 		++argp;
5371500Serics 	}
5381500Serics 	return (ccount);
5391500Serics 
5401500Serics }
5411500Serics 
5421500Serics /*
5431500Serics ** Print an integer as a string of decimal digits,
5441500Serics ** returning the length of the print representation.
5451500Serics */
5461500Serics 
5471500Serics printd (n)
5481500Serics int n;
5491500Serics {
5501500Serics     int a, nchars;
5511500Serics 
5521500Serics     if (a = n/10)
5531500Serics 	nchars = 1 + printd(a);
5541500Serics     else
5551500Serics 	nchars = 1;
5561500Serics     putchar (n % 10 + '0');
5571500Serics     return (nchars);
5581500Serics }
5591500Serics 
5601500Serics /* Put the print representation of an integer into a string */
5611500Serics static char *sptr;
5621500Serics 
5631500Serics scanstr (n, str)
5641500Serics int n;
5651500Serics char *str;
5661500Serics {
5671500Serics     sptr = str;
56811604Slayer     Sprintf (n);
5691500Serics     *sptr = '\0';
5701500Serics }
5711500Serics 
57211604Slayer Sprintf (n)
5731500Serics {
5741500Serics     int a;
5751500Serics 
5761500Serics     if (a = n/10)
57711604Slayer 	Sprintf (a);
5781500Serics     *sptr++ = n % 10 + '0';
5791500Serics }
5801500Serics 
5811500Serics static char bell = ctrl(G);
5821500Serics 
5831500Serics strlen (s)
5841500Serics char *s;
5851500Serics {
5861500Serics     register char *p;
5871500Serics 
5881500Serics     p = s;
5891500Serics     while (*p++)
5901500Serics 	;
5911500Serics     return (p - s - 1);
5921500Serics }
5931500Serics 
5941500Serics /* See whether the last component of the path name "path" is equal to the
5951500Serics ** string "string"
5961500Serics */
5971500Serics 
5981500Serics tailequ (path, string)
5991500Serics char *path;
6001500Serics register char *string;
6011500Serics {
6021500Serics 	register char *tail;
6031500Serics 
6041500Serics 	tail = path + strlen(path);
6051500Serics 	while (tail >= path)
6061500Serics 		if (*(--tail) == '/')
6071500Serics 			break;
6081500Serics 	++tail;
6091500Serics 	while (*tail++ == *string++)
6101500Serics 		if (*tail == '\0')
6111500Serics 			return(1);
6121500Serics 	return(0);
6131500Serics }
6141500Serics 
6151500Serics prompt (filename)
6161500Serics char *filename;
6171500Serics {
6183594Sroot     if (clreol)
6193594Sroot 	cleareol ();
6203455Sroot     else if (promptlen > 0)
6211500Serics 	kill_line ();
6221500Serics     if (!hard) {
6231500Serics 	promptlen = 8;
6241500Serics 	if (Senter && Sexit)
6251500Serics 	    tputs (Senter, 1, putch);
6263594Sroot 	if (clreol)
6273594Sroot 	    cleareol ();
6281500Serics 	pr("--More--");
6291500Serics 	if (filename != NULL) {
6301500Serics 	    promptlen += printf ("(Next file: %s)", filename);
6311500Serics 	}
6321500Serics 	else if (!no_intty) {
6331500Serics 	    promptlen += printf ("(%d%%)", (int)((file_pos * 100) / file_size));
6341500Serics 	}
6351500Serics 	if (dum_opt) {
6361500Serics 	    promptlen += pr("[Hit space to continue, Rubout to abort]");
6371500Serics 	}
6381500Serics 	if (Senter && Sexit)
6391500Serics 	    tputs (Sexit, 1, putch);
6403594Sroot 	if (clreol)
6413594Sroot 	    clreos ();
6421500Serics 	fflush(stdout);
6431500Serics     }
6441500Serics     else
6451500Serics 	write (2, &bell, 1);
6461500Serics     inwait++;
6471500Serics }
6481500Serics 
6491500Serics /*
6501500Serics ** Get a logical line
6511500Serics */
6521500Serics 
6531500Serics getline(f, length)
6541500Serics register FILE *f;
6551500Serics int *length;
6561500Serics {
6571500Serics     register int	c;
6581500Serics     register char	*p;
6591500Serics     register int	column;
6601500Serics     static int		colflg;
6611500Serics 
6621500Serics     p = Line;
6631500Serics     column = 0;
6641500Serics     c = Getc (f);
6651500Serics     if (colflg && c == '\n') {
6661500Serics 	Currline++;
6671500Serics 	c = Getc (f);
6681500Serics     }
6691500Serics     while (p < &Line[LINSIZ - 1]) {
6701500Serics 	if (c == EOF) {
6711500Serics 	    if (p > Line) {
6721500Serics 		*p = '\0';
6731500Serics 		*length = p - Line;
6741500Serics 		return (column);
6751500Serics 	    }
6761500Serics 	    *length = p - Line;
6771500Serics 	    return (EOF);
6781500Serics 	}
6791500Serics 	if (c == '\n') {
6801500Serics 	    Currline++;
6811500Serics 	    break;
6821500Serics 	}
6831500Serics 	*p++ = c;
6841500Serics 	if (c == '\t')
6851500Serics 	    if (hardtabs && column < promptlen && !hard) {
6861500Serics 		if (eraseln && !dumb) {
6871500Serics 		    column = 1 + (column | 7);
6881500Serics 		    tputs (eraseln, 1, putch);
6891500Serics 		    promptlen = 0;
6901500Serics 		}
6911500Serics 		else {
6921500Serics 		    for (--p; column & 7 && p < &Line[LINSIZ - 1]; column++) {
6931500Serics 			*p++ = ' ';
6941500Serics 		    }
6951500Serics 		    if (column >= promptlen) promptlen = 0;
6961500Serics 		}
6971500Serics 	    }
6981500Serics 	    else
6991500Serics 		column = 1 + (column | 7);
7009627Ssklower 	else if (c == '\b' && column > 0)
7011500Serics 	    column--;
7021500Serics 	else if (c == '\r')
7031500Serics 	    column = 0;
7041500Serics 	else if (c == '\f' && stop_opt) {
7051500Serics 		p[-1] = '^';
7061500Serics 		*p++ = 'L';
7071500Serics 		column += 2;
7081500Serics 		Pause++;
7091500Serics 	}
7101500Serics 	else if (c == EOF) {
7111500Serics 	    *length = p - Line;
7121500Serics 	    return (column);
7131500Serics 	}
7141500Serics 	else if (c >= ' ' && c != RUBOUT)
7151500Serics 	    column++;
7161500Serics 	if (column >= Mcol && fold_opt) break;
7171500Serics 	c = Getc (f);
7181500Serics     }
7191500Serics     if (column >= Mcol && Mcol > 0) {
7201500Serics 	if (!Wrap) {
7211500Serics 	    *p++ = '\n';
7221500Serics 	}
7231500Serics     }
7241500Serics     colflg = column == Mcol && fold_opt;
7251500Serics     *length = p - Line;
7261500Serics     *p = 0;
7271500Serics     return (column);
7281500Serics }
7291500Serics 
7301500Serics /*
7311500Serics ** Erase the rest of the prompt, assuming we are starting at column col.
7321500Serics */
7331500Serics 
7341500Serics erase (col)
7351500Serics register int col;
7361500Serics {
7371500Serics 
7381500Serics     if (promptlen == 0)
7391500Serics 	return;
7401500Serics     if (hard) {
7411500Serics 	putchar ('\n');
7421500Serics     }
7431500Serics     else {
7441500Serics 	if (col == 0)
7451500Serics 	    putchar ('\r');
7461500Serics 	if (!dumb && eraseln)
7471500Serics 	    tputs (eraseln, 1, putch);
7481500Serics 	else
7491500Serics 	    for (col = promptlen - col; col > 0; col--)
7501500Serics 		putchar (' ');
7511500Serics     }
7521500Serics     promptlen = 0;
7531500Serics }
7541500Serics 
7551500Serics /*
7561500Serics ** Erase the current line entirely
7571500Serics */
7581500Serics 
7591500Serics kill_line ()
7601500Serics {
7611500Serics     erase (0);
7621500Serics     if (!eraseln || dumb) putchar ('\r');
7631500Serics }
7641500Serics 
7651500Serics /*
7663455Sroot  * force clear to end of line
7673455Sroot  */
7683455Sroot cleareol()
7693455Sroot {
7703594Sroot     tputs(eraseln, 1, putch);
7713455Sroot }
7723455Sroot 
7733594Sroot clreos()
7743455Sroot {
7753594Sroot     tputs(EodClr, 1, putch);
7763455Sroot }
7773455Sroot 
7783455Sroot /*
7791500Serics **  Print string and return number of characters
7801500Serics */
7811500Serics 
7821500Serics pr(s1)
7831500Serics char	*s1;
7841500Serics {
7851500Serics     register char	*s;
7861500Serics     register char	c;
7871500Serics 
7881500Serics     for (s = s1; c = *s++; )
7891500Serics 	putchar(c);
7901500Serics     return (s - s1 - 1);
7911500Serics }
7921500Serics 
7931500Serics 
7941500Serics /* Print a buffer of n characters */
7951500Serics 
7961500Serics prbuf (s, n)
7971500Serics register char *s;
7981500Serics register int n;
7991500Serics {
8003594Sroot     char c;				/* next ouput character */
8013594Sroot     register int state;			/* next output char's UL state */
8023594Sroot     static int pstate = 0;		/* current terminal UL state (off) */
8033594Sroot 
8043594Sroot     while (--n >= 0)
8053594Sroot 	if (!ul_opt)
8063594Sroot 	    putchar (*s++);
8073594Sroot 	else {
8083594Sroot 	    if (n >= 2 && s[0] == '_' && s[1] == '\b') {
8093594Sroot 		n -= 2;
8103594Sroot 	        s += 2;
8113594Sroot 		c = *s++;
8123594Sroot 		state = 1;
8133594Sroot 	    } else if (n >= 2 && s[1] == '\b' && s[2] == '_') {
8143594Sroot 		n -= 2;
8153594Sroot 		c = *s++;
8163594Sroot 		s += 2;
8173594Sroot 		state = 1;
8183594Sroot 	    } else {
8193594Sroot 		c = *s++;
8203594Sroot 		state = 0;
8213594Sroot 	    }
8223594Sroot 	    if (state != pstate)
8233594Sroot 		tputs(state ? ULenter : ULexit, 1, putch);
8243594Sroot 	    pstate = state;
8253594Sroot 	    putchar(c);
8263594Sroot 	    if (state && *chUL) {
8273594Sroot 		pr(chBS);
8283594Sroot 		tputs(chUL, 1, putch);
8293594Sroot 	    }
8303594Sroot 	}
8311500Serics }
8321500Serics 
8331500Serics /*
8341500Serics **  Clear the screen
8351500Serics */
8361500Serics 
8371500Serics doclear()
8381500Serics {
8391500Serics     if (Clear && !hard) {
8401500Serics 	tputs(Clear, 1, putch);
8411500Serics 
8421500Serics 	/* Put out carriage return so that system doesn't
8431500Serics 	** get confused by escape sequences when expanding tabs
8441500Serics 	*/
8451500Serics 	putchar ('\r');
8461500Serics 	promptlen = 0;
8471500Serics     }
8481500Serics }
8491500Serics 
8503455Sroot /*
8513455Sroot  * Go to home position
8523455Sroot  */
8533455Sroot home()
8543455Sroot {
8553455Sroot     tputs(Home,1,putch);
8563455Sroot }
8573455Sroot 
8581500Serics static int lastcmd, lastarg, lastp;
8591500Serics static int lastcolon;
8601500Serics char shell_line[132];
8611500Serics 
8621500Serics /*
8631500Serics ** Read a command and do it. A command consists of an optional integer
8641500Serics ** argument followed by the command character.  Return the number of lines
8651500Serics ** to display in the next screenful.  If there is nothing more to display
8661500Serics ** in the current file, zero is returned.
8671500Serics */
8681500Serics 
8691500Serics command (filename, f)
8701500Serics char *filename;
8711500Serics register FILE *f;
8721500Serics {
8731500Serics     register int nlines;
8741500Serics     register int retval;
8751500Serics     register char c;
8761500Serics     char colonch;
8771500Serics     FILE *helpf;
8781500Serics     int done;
8791500Serics     char comchar, cmdbuf[80], *p;
8801500Serics 
8811500Serics #define ret(val) retval=val;done++;break
8821500Serics 
8831500Serics     done = 0;
8841500Serics     if (!errors)
8851500Serics 	prompt (filename);
8861500Serics     else
8871500Serics 	errors = 0;
8881500Serics     if (MBIT == RAW && slow_tty) {
8891500Serics 	otty.sg_flags |= MBIT;
8901500Serics 	stty(2, &otty);
8911500Serics     }
8921500Serics     for (;;) {
8931500Serics 	nlines = number (&comchar);
8941500Serics 	lastp = colonch = 0;
8951500Serics 	if (comchar == '.') {	/* Repeat last command */
8961500Serics 		lastp++;
8971500Serics 		comchar = lastcmd;
8981500Serics 		nlines = lastarg;
8991500Serics 		if (lastcmd == ':')
9001500Serics 			colonch = lastcolon;
9011500Serics 	}
9021500Serics 	lastcmd = comchar;
9031500Serics 	lastarg = nlines;
9041500Serics 	if (comchar == otty.sg_erase) {
9051500Serics 	    kill_line ();
9061500Serics 	    prompt (filename);
9071500Serics 	    continue;
9081500Serics 	}
9091500Serics 	switch (comchar) {
9101500Serics 	case ':':
9111500Serics 	    retval = colon (filename, colonch, nlines);
9121500Serics 	    if (retval >= 0)
9131500Serics 		done++;
9141500Serics 	    break;
9151500Serics 	case ' ':
9161500Serics 	case 'z':
9171500Serics 	    if (nlines == 0) nlines = dlines;
9181500Serics 	    else if (comchar == 'z') dlines = nlines;
9191500Serics 	    ret (nlines);
9201500Serics 	case 'd':
9211500Serics 	case ctrl(D):
9221500Serics 	    if (nlines != 0) nscroll = nlines;
9231500Serics 	    ret (nscroll);
9241500Serics 	case RUBOUT:
9251500Serics 	case 'q':
9261500Serics 	case 'Q':
9271500Serics 	    end_it ();
9281500Serics 	case 's':
9291500Serics 	case 'f':
9301500Serics 	    if (nlines == 0) nlines++;
9311500Serics 	    if (comchar == 'f')
9321500Serics 		nlines *= dlines;
9331500Serics 	    putchar ('\r');
9341500Serics 	    erase (0);
9353594Sroot 	    printf ("\n");
9363594Sroot 	    if (clreol)
9373594Sroot 		cleareol ();
9383594Sroot 	    printf ("...skipping %d line", nlines);
9391500Serics 	    if (nlines > 1)
9403594Sroot 		pr ("s\n");
9411500Serics 	    else
9423594Sroot 		pr ("\n");
9433594Sroot 
9443594Sroot 	    if (clreol)
9453594Sroot 		cleareol ();
9463594Sroot 	    pr ("\n");
9473594Sroot 
9481500Serics 	    while (nlines > 0) {
9491500Serics 		while ((c = Getc (f)) != '\n')
9501500Serics 		    if (c == EOF) {
9511500Serics 			retval = 0;
9521500Serics 			done++;
9531500Serics 			goto endsw;
9541500Serics 		    }
9551500Serics 		    Currline++;
9561500Serics 		    nlines--;
9571500Serics 	    }
9581500Serics 	    ret (dlines);
9591500Serics 	case '\n':
9601500Serics 	    if (nlines != 0)
9611500Serics 		dlines = nlines;
9621500Serics 	    else
9631500Serics 		nlines = 1;
9641500Serics 	    ret (nlines);
9651500Serics 	case '\f':
9661500Serics 	    if (!no_intty) {
9671500Serics 		doclear ();
9681500Serics 		Fseek (f, screen_start.chrctr);
9691500Serics 		Currline = screen_start.line;
9701500Serics 		ret (dlines);
9711500Serics 	    }
9721500Serics 	    else {
9731500Serics 		write (2, &bell, 1);
9741500Serics 		break;
9751500Serics 	    }
9761500Serics 	case '\'':
9771500Serics 	    if (!no_intty) {
9781500Serics 		kill_line ();
9791500Serics 		pr ("\n***Back***\n\n");
9801500Serics 		Fseek (f, context.chrctr);
9811500Serics 		Currline = context.line;
9821500Serics 		ret (dlines);
9831500Serics 	    }
9841500Serics 	    else {
9851500Serics 		write (2, &bell, 1);
9861500Serics 		break;
9871500Serics 	    }
9881500Serics 	case '=':
9891500Serics 	    kill_line ();
9901500Serics 	    promptlen = printd (Currline);
9911500Serics 	    fflush (stdout);
9921500Serics 	    break;
9931500Serics 	case 'n':
9941500Serics 	    lastp++;
9951500Serics 	case '/':
9961500Serics 	    if (nlines == 0) nlines++;
9971500Serics 	    kill_line ();
9981500Serics 	    pr ("/");
9991500Serics 	    promptlen = 1;
10001500Serics 	    fflush (stdout);
10011500Serics 	    if (lastp) {
10021500Serics 		write (2,"\r", 1);
10031500Serics 		search (NULL, f, nlines);	/* Use previous r.e. */
10041500Serics 	    }
10051500Serics 	    else {
10061500Serics 		ttyin (cmdbuf, 78, '/');
10071500Serics 		write (2, "\r", 1);
10081500Serics 		search (cmdbuf, f, nlines);
10091500Serics 	    }
10103455Sroot 	    ret (dlines-1);
10111500Serics 	case '!':
10121500Serics 	    do_shell (filename);
10131500Serics 	    break;
10141500Serics 	case 'h':
10151500Serics 	    if ((helpf = fopen (HELPFILE, "r")) == NULL)
10161500Serics 		error ("Can't open help file");
10171500Serics 	    if (noscroll) doclear ();
10181500Serics 	    copy_file (helpf);
10191500Serics 	    close (helpf);
10201500Serics 	    prompt (filename);
10211500Serics 	    break;
10221500Serics 	case 'v':	/* This case should go right before default */
10231500Serics 	    if (!no_intty) {
10241500Serics 		kill_line ();
10251500Serics 		cmdbuf[0] = '+';
10261500Serics 		scanstr (Currline, &cmdbuf[1]);
10271500Serics 		pr ("vi "); pr (cmdbuf); putchar (' '); pr (fnames[fnum]);
10281500Serics 		execute (filename, VI, "vi", cmdbuf, fnames[fnum], 0);
10291500Serics 		break;
10301500Serics 	    }
10311500Serics 	default:
10321500Serics 	    write (2, &bell, 1);
10331500Serics 	    break;
10341500Serics 	}
10351500Serics 	if (done) break;
10361500Serics     }
10371500Serics     putchar ('\r');
10381500Serics endsw:
10391500Serics     inwait = 0;
10401500Serics     notell++;
10411500Serics     if (MBIT == RAW && slow_tty) {
10421500Serics 	otty.sg_flags &= ~MBIT;
10431500Serics 	stty(2, &otty);
10441500Serics     }
10451500Serics     return (retval);
10461500Serics }
10471500Serics 
10481500Serics char ch;
10491500Serics 
10501500Serics /*
10511500Serics  * Execute a colon-prefixed command.
10521500Serics  * Returns <0 if not a command that should cause
10531500Serics  * more of the file to be printed.
10541500Serics  */
10551500Serics 
10561500Serics colon (filename, cmd, nlines)
10571500Serics char *filename;
10581500Serics int cmd;
10591500Serics int nlines;
10601500Serics {
10611500Serics 	if (cmd == 0)
10621500Serics 		ch = readch ();
10631500Serics 	else
10641500Serics 		ch = cmd;
10651500Serics 	lastcolon = ch;
10661500Serics 	switch (ch) {
10671500Serics 	case 'f':
10681500Serics 		kill_line ();
10691500Serics 		if (!no_intty)
10701500Serics 			promptlen = printf ("\"%s\" line %d", fnames[fnum], Currline);
10711500Serics 		else
10721500Serics 			promptlen = printf ("[Not a file] line %d", Currline);
10731500Serics 		fflush (stdout);
10741500Serics 		return (-1);
10751500Serics 	case 'n':
10761500Serics 		if (nlines == 0) {
10771500Serics 			if (fnum >= nfiles - 1)
10781500Serics 				end_it ();
10791500Serics 			nlines++;
10801500Serics 		}
10811500Serics 		putchar ('\r');
10821500Serics 		erase (0);
10831500Serics 		skipf (nlines);
10841500Serics 		return (0);
10851500Serics 	case 'p':
10861500Serics 		if (no_intty) {
10871500Serics 			write (2, &bell, 1);
10881500Serics 			return (-1);
10891500Serics 		}
10901500Serics 		putchar ('\r');
10911500Serics 		erase (0);
10921500Serics 		if (nlines == 0)
10931500Serics 			nlines++;
10941500Serics 		skipf (-nlines);
10951500Serics 		return (0);
10961500Serics 	case '!':
10971500Serics 		do_shell (filename);
10981500Serics 		return (-1);
10991500Serics 	case 'q':
11001500Serics 	case 'Q':
11011500Serics 		end_it ();
11021500Serics 	default:
11031500Serics 		write (2, &bell, 1);
11041500Serics 		return (-1);
11051500Serics 	}
11061500Serics }
11071500Serics 
11081500Serics /*
11091500Serics ** Read a decimal number from the terminal. Set cmd to the non-digit which
11101500Serics ** terminates the number.
11111500Serics */
11121500Serics 
11131500Serics number(cmd)
11141500Serics char *cmd;
11151500Serics {
11161500Serics 	register int i;
11171500Serics 
11181500Serics 	i = 0; ch = otty.sg_kill;
11191500Serics 	for (;;) {
11201500Serics 		ch = readch ();
11211500Serics 		if (ch >= '0' && ch <= '9')
11221500Serics 			i = i*10 + ch - '0';
11231500Serics 		else if (ch == otty.sg_kill)
11241500Serics 			i = 0;
11251500Serics 		else {
11261500Serics 			*cmd = ch;
11271500Serics 			break;
11281500Serics 		}
11291500Serics 	}
11301500Serics 	return (i);
11311500Serics }
11321500Serics 
11331500Serics do_shell (filename)
11341500Serics char *filename;
11351500Serics {
11361500Serics 	char cmdbuf[80];
11371500Serics 
11381500Serics 	kill_line ();
11391500Serics 	pr ("!");
11401500Serics 	fflush (stdout);
11411500Serics 	promptlen = 1;
11421500Serics 	if (lastp)
11431500Serics 		pr (shell_line);
11441500Serics 	else {
11451500Serics 		ttyin (cmdbuf, 78, '!');
11461500Serics 		if (expand (shell_line, cmdbuf)) {
11471500Serics 			kill_line ();
11481500Serics 			promptlen = printf ("!%s", shell_line);
11491500Serics 		}
11501500Serics 	}
11511500Serics 	fflush (stdout);
11521500Serics 	write (2, "\n", 1);
11531500Serics 	promptlen = 0;
11541500Serics 	shellp = 1;
11551500Serics 	execute (filename, shell, shell, "-c", shell_line, 0);
11561500Serics }
11571500Serics 
11581500Serics /*
11591500Serics ** Search for nth ocurrence of regular expression contained in buf in the file
11601500Serics */
11611500Serics 
11621500Serics search (buf, file, n)
11631500Serics char buf[];
11641500Serics FILE *file;
11651500Serics register int n;
11661500Serics {
11671500Serics     long startline = Ftell (file);
11681500Serics     register long line1 = startline;
11691500Serics     register long line2 = startline;
11701500Serics     register long line3 = startline;
11711500Serics     register int lncount;
11721500Serics     int saveln, rv, re_exec();
11731500Serics     char *s, *re_comp();
11741500Serics 
11751500Serics     context.line = saveln = Currline;
11761500Serics     context.chrctr = startline;
11771500Serics     lncount = 0;
11781500Serics     if ((s = re_comp (buf)) != 0)
11791500Serics 	error (s);
11801500Serics     while (!feof (file)) {
11811500Serics 	line3 = line2;
11821500Serics 	line2 = line1;
11831500Serics 	line1 = Ftell (file);
11841500Serics 	rdline (file);
11851500Serics 	lncount++;
11861500Serics 	if ((rv = re_exec (Line)) == 1)
11871500Serics 		if (--n == 0) {
11881500Serics 		    if (lncount > 3 || (lncount > 1 && no_intty))
11893455Sroot 		    {
11903455Sroot 			pr ("\n");
11913594Sroot 			if (clreol)
11923594Sroot 			    cleareol ();
11933455Sroot 			pr("...skipping\n");
11943455Sroot 		    }
11951500Serics 		    if (!no_intty) {
11961500Serics 			Currline -= (lncount >= 3 ? 3 : lncount);
11971500Serics 			Fseek (file, line3);
11983594Sroot 			if (noscroll)
11993594Sroot 			    if (clreol) {
12003594Sroot 				home ();
12013594Sroot 				cleareol ();
12023594Sroot 			    }
12033594Sroot 			    else
12043594Sroot 				doclear ();
12051500Serics 		    }
12061500Serics 		    else {
12071500Serics 			kill_line ();
12083594Sroot 			if (noscroll)
12093594Sroot 			    if (clreol) {
12103594Sroot 			        home ();
12113594Sroot 			        cleareol ();
12123594Sroot 			    }
12133594Sroot 			    else
12143594Sroot 				doclear ();
12151500Serics 			pr (Line);
12161500Serics 			putchar ('\n');
12171500Serics 		    }
12181500Serics 		    break;
12191500Serics 		}
12201500Serics 	else if (rv == -1)
12211500Serics 	    error ("Regular expression botch");
12221500Serics     }
12231500Serics     if (feof (file)) {
12241500Serics 	if (!no_intty) {
12251500Serics 	    file->_flag &= ~_IOEOF; /* why doesn't fseek do this ??!!??! */
12261500Serics 	    Currline = saveln;
12271500Serics 	    Fseek (file, startline);
12281500Serics 	}
12291500Serics 	else {
12301500Serics 	    pr ("\nPattern not found\n");
12311500Serics 	    end_it ();
12321500Serics 	}
12331500Serics 	error ("Pattern not found");
12341500Serics     }
12351500Serics }
12361500Serics 
12371500Serics execute (filename, cmd, args)
12381500Serics char *filename;
12391500Serics char *cmd, *args;
12401500Serics {
12411500Serics 	int id;
12421500Serics 
12431500Serics 	fflush (stdout);
12441500Serics 	reset_tty ();
12451500Serics 	while ((id = fork ()) < 0)
12461500Serics 	    sleep (5);
12471500Serics 	if (id == 0) {
12481500Serics 	    execv (cmd, &args);
12491500Serics 	    write (2, "exec failed\n", 12);
12501500Serics 	    exit (1);
12511500Serics 	}
12521500Serics 	signal (SIGINT, SIG_IGN);
12531500Serics 	signal (SIGQUIT, SIG_IGN);
12541500Serics 	if (catch_susp)
12551500Serics 	    signal(SIGTSTP, SIG_DFL);
12561500Serics 	wait (0);
12571500Serics 	signal (SIGINT, end_it);
12581500Serics 	signal (SIGQUIT, onquit);
12591500Serics 	if (catch_susp)
12601500Serics 	    signal(SIGTSTP, onsusp);
12611500Serics 	set_tty ();
12621500Serics 	pr ("------------------------\n");
12631500Serics 	prompt (filename);
12641500Serics }
12651500Serics /*
12661500Serics ** Skip n lines in the file f
12671500Serics */
12681500Serics 
12691500Serics skiplns (n, f)
12701500Serics register int n;
12711500Serics register FILE *f;
12721500Serics {
12731500Serics     register char c;
12741500Serics 
12751500Serics     while (n > 0) {
12761500Serics 	while ((c = Getc (f)) != '\n')
12771500Serics 	    if (c == EOF)
12781500Serics 		return;
12791500Serics 	    n--;
12801500Serics 	    Currline++;
12811500Serics     }
12821500Serics }
12831500Serics 
12841500Serics /*
12851500Serics ** Skip nskip files in the file list (from the command line). Nskip may be
12861500Serics ** negative.
12871500Serics */
12881500Serics 
12891500Serics skipf (nskip)
12901500Serics register int nskip;
12911500Serics {
12921500Serics     if (nskip == 0) return;
12931500Serics     if (nskip > 0) {
12941500Serics 	if (fnum + nskip > nfiles - 1)
12951500Serics 	    nskip = nfiles - fnum - 1;
12961500Serics     }
12971500Serics     else if (within)
12981500Serics 	++fnum;
12991500Serics     fnum += nskip;
13001500Serics     if (fnum < 0)
13011500Serics 	fnum = 0;
13023594Sroot     pr ("\n...Skipping ");
13033455Sroot     pr ("\n");
13043594Sroot     if (clreol)
13053594Sroot 	cleareol ();
13063455Sroot     pr ("...Skipping ");
13071500Serics     pr (nskip > 0 ? "to file " : "back to file ");
13081500Serics     pr (fnames[fnum]);
13093455Sroot     pr ("\n");
13103594Sroot     if (clreol)
13113594Sroot 	cleareol ();
13123455Sroot     pr ("\n");
13131500Serics     --fnum;
13141500Serics }
13151500Serics 
13161500Serics /*----------------------------- Terminal I/O -------------------------------*/
13171500Serics 
13181500Serics initterm ()
13191500Serics {
13201500Serics     char	buf[TBUFSIZ];
13211500Serics     char	clearbuf[100];
13221500Serics     char	*clearptr, *padstr;
13231500Serics     int		ldisc;
132410823Ssam     char	*term;
13251500Serics 
13261500Serics     setbuf(stdout, obuf);
13271500Serics     if (!(no_tty = gtty(1, &otty))) {
132813536Ssam 	if ((term = getenv("TERM")) || tgetent(buf, term) <= 0) {
13293594Sroot 	    dumb++; ul_opt = 0;
13301500Serics 	}
13311500Serics 	else {
13321500Serics 	    if (((Lpp = tgetnum("li")) < 0) || tgetflag("hc")) {
13331500Serics 		hard++;	/* Hard copy terminal */
13341500Serics 		Lpp = 24;
13351500Serics 	    }
13361500Serics 	    if (tailequ (fnames[0], "page") || !hard && tgetflag("ns"))
13371500Serics 		noscroll++;
13381500Serics 	    if ((Mcol = tgetnum("co")) < 0)
13391500Serics 		Mcol = 80;
13401500Serics 	    Wrap = tgetflag("am");
13411500Serics 	    bad_so = tgetflag ("xs");
13421500Serics 	    clearptr = clearbuf;
13431500Serics 	    eraseln = tgetstr("ce",&clearptr);
13441500Serics 	    Clear = tgetstr("cl", &clearptr);
13451500Serics 	    Senter = tgetstr("so", &clearptr);
13461500Serics 	    Sexit = tgetstr("se", &clearptr);
13473594Sroot 
13483594Sroot 	    /*
13493594Sroot 	     *  Set up for underlining:  some terminals don't need it;
13503594Sroot 	     *  others have start/stop sequences, still others have an
13513594Sroot 	     *  underline char sequence which is assumed to move the
13523594Sroot 	     *  cursor forward one character.  If underline sequence
13533594Sroot 	     *  isn't available, settle for standout sequence.
13543594Sroot 	     */
13553594Sroot 
13563594Sroot 	    if (tgetflag("ul") || tgetflag("os"))
13573594Sroot 		ul_opt = 0;
13583594Sroot 	    if ((chUL = tgetstr("uc", &clearptr)) == NULL )
13593594Sroot 		chUL = "";
13603594Sroot 	    if ((ULenter = tgetstr("us", &clearptr)) == NULL &&
13613594Sroot 		(!*chUL) && (ULenter = tgetstr("so", &clearptr)) == NULL)
13623594Sroot 		ULenter = "";
13633594Sroot 	    if ((ULexit = tgetstr("ue", &clearptr)) == NULL &&
13643594Sroot 		(!*chUL) && (ULexit = tgetstr("se", &clearptr)) == NULL)
13653594Sroot 		ULexit = "";
13663594Sroot 
13671500Serics 	    if (padstr = tgetstr("pc", &clearptr))
13681500Serics 		PC = *padstr;
13693455Sroot 	    Home = tgetstr("ho",&clearptr);
137013536Ssam 	    if (Home == 0 || *Home == '\0')
13713455Sroot 	    {
13723594Sroot 		if ((cursorm = tgetstr("cm", &clearptr)) != NULL) {
13733594Sroot 		    strcpy(cursorhome, tgoto(cursorm, 0, 0));
13743455Sroot 		    Home = cursorhome;
13753455Sroot 	       }
13763455Sroot 	    }
13773594Sroot 	    EodClr = tgetstr("cd", &clearptr);
13781500Serics 	}
13791500Serics 	if ((shell = getenv("SHELL")) == NULL)
13801500Serics 	    shell = "/bin/sh";
13811500Serics     }
13821500Serics     no_intty = gtty(0, &otty);
13831500Serics     gtty(2, &otty);
13841500Serics     ospeed = otty.sg_ospeed;
13851500Serics     slow_tty = ospeed < B1200;
13861500Serics     hardtabs =  !(otty.sg_flags & XTABS);
13871500Serics     if (!no_tty) {
13881500Serics 	otty.sg_flags &= ~ECHO;
13891500Serics 	if (MBIT == CBREAK || !slow_tty)
13901500Serics 	    otty.sg_flags |= MBIT;
13911500Serics     }
13921500Serics }
13931500Serics 
13941500Serics readch ()
13951500Serics {
13961500Serics 	char ch;
13971500Serics 	extern int errno;
13981500Serics 
13991500Serics 	if (read (2, &ch, 1) <= 0)
14001500Serics 		if (errno != EINTR)
14011500Serics 			exit(0);
14021500Serics 		else
14031500Serics 			ch = otty.sg_kill;
14041500Serics 	return (ch);
14051500Serics }
14061500Serics 
14071500Serics static char BS = '\b';
14081500Serics static char CARAT = '^';
14091500Serics 
14101500Serics ttyin (buf, nmax, pchar)
14111500Serics char buf[];
14121500Serics register int nmax;
14131500Serics char pchar;
14141500Serics {
14151500Serics     register char *sptr;
14161500Serics     register char ch;
14171500Serics     register int slash = 0;
14181500Serics     int	maxlen;
14191500Serics     char cbuf;
14201500Serics 
14211500Serics     sptr = buf;
14221500Serics     maxlen = 0;
14231500Serics     while (sptr - buf < nmax) {
14241500Serics 	if (promptlen > maxlen) maxlen = promptlen;
14251500Serics 	ch = readch ();
14261500Serics 	if (ch == '\\') {
14271500Serics 	    slash++;
14281500Serics 	}
14291500Serics 	else if ((ch == otty.sg_erase) && !slash) {
14301500Serics 	    if (sptr > buf) {
14311500Serics 		--promptlen;
14321500Serics 		write (2, &BS, 1);
14331500Serics 		--sptr;
14341500Serics 		if ((*sptr < ' ' && *sptr != '\n') || *sptr == RUBOUT) {
14351500Serics 		    --promptlen;
14361500Serics 		    write (2, &BS, 1);
14371500Serics 		}
14381500Serics 		continue;
14391500Serics 	    }
14401500Serics 	    else {
14411500Serics 		if (!eraseln) promptlen = maxlen;
14421500Serics 		longjmp (restore, 1);
14431500Serics 	    }
14441500Serics 	}
14451500Serics 	else if ((ch == otty.sg_kill) && !slash) {
14461500Serics 	    if (hard) {
14471500Serics 		show (ch);
14481500Serics 		putchar ('\n');
14491500Serics 		putchar (pchar);
14501500Serics 	    }
14511500Serics 	    else {
14521500Serics 		putchar ('\r');
14531500Serics 		putchar (pchar);
14541500Serics 		if (eraseln)
14551500Serics 		    erase (1);
14561500Serics 		promptlen = 1;
14571500Serics 	    }
14581500Serics 	    sptr = buf;
14591500Serics 	    fflush (stdout);
14601500Serics 	    continue;
14611500Serics 	}
14621500Serics 	if (slash && (ch == otty.sg_kill || ch == otty.sg_erase)) {
14631500Serics 	    write (2, &BS, 1);
14641500Serics 	    --sptr;
14651500Serics 	}
14661500Serics 	if (ch != '\\')
14671500Serics 	    slash = 0;
14681500Serics 	*sptr++ = ch;
14691500Serics 	if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) {
14701500Serics 	    ch += ch == RUBOUT ? -0100 : 0100;
14711500Serics 	    write (2, &CARAT, 1);
14721500Serics 	    promptlen++;
14731500Serics 	}
14741500Serics 	cbuf = ch;
14751500Serics 	if (ch != '\n' && ch != ESC) {
14761500Serics 	    write (2, &cbuf, 1);
14771500Serics 	    promptlen++;
14781500Serics 	}
14791500Serics 	else
14801500Serics 	    break;
14811500Serics     }
14821500Serics     *--sptr = '\0';
14831500Serics     if (!eraseln) promptlen = maxlen;
14841500Serics     if (sptr - buf >= nmax - 1)
14851500Serics 	error ("Line too long");
14861500Serics }
14871500Serics 
14881500Serics expand (outbuf, inbuf)
14891500Serics char *outbuf;
14901500Serics char *inbuf;
14911500Serics {
14921500Serics     register char *instr;
14931500Serics     register char *outstr;
14941500Serics     register char ch;
14951500Serics     char temp[200];
14961500Serics     int changed = 0;
14971500Serics 
14981500Serics     instr = inbuf;
14991500Serics     outstr = temp;
15001500Serics     while ((ch = *instr++) != '\0')
15011500Serics 	switch (ch) {
15021500Serics 	case '%':
15031500Serics 	    if (!no_intty) {
15041500Serics 		strcpy (outstr, fnames[fnum]);
15051500Serics 		outstr += strlen (fnames[fnum]);
15061500Serics 		changed++;
15071500Serics 	    }
15081500Serics 	    else
15091500Serics 		*outstr++ = ch;
15101500Serics 	    break;
15111500Serics 	case '!':
15121500Serics 	    if (!shellp)
15131500Serics 		error ("No previous command to substitute for");
15141500Serics 	    strcpy (outstr, shell_line);
15151500Serics 	    outstr += strlen (shell_line);
15161500Serics 	    changed++;
15171500Serics 	    break;
15181500Serics 	case '\\':
15191500Serics 	    if (*instr == '%' || *instr == '!') {
15201500Serics 		*outstr++ = *instr++;
15211500Serics 		break;
15221500Serics 	    }
15231500Serics 	default:
15241500Serics 	    *outstr++ = ch;
15251500Serics 	}
15261500Serics     *outstr++ = '\0';
15271500Serics     strcpy (outbuf, temp);
15281500Serics     return (changed);
15291500Serics }
15301500Serics 
15311500Serics show (ch)
15321500Serics register char ch;
15331500Serics {
15341500Serics     char cbuf;
15351500Serics 
15361500Serics     if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) {
15371500Serics 	ch += ch == RUBOUT ? -0100 : 0100;
15381500Serics 	write (2, &CARAT, 1);
15391500Serics 	promptlen++;
15401500Serics     }
15411500Serics     cbuf = ch;
15421500Serics     write (2, &cbuf, 1);
15431500Serics     promptlen++;
15441500Serics }
15451500Serics 
15461500Serics error (mess)
15471500Serics char *mess;
15481500Serics {
15493594Sroot     if (clreol)
15503594Sroot 	cleareol ();
15513594Sroot     else
15523594Sroot 	kill_line ();
15531500Serics     promptlen += strlen (mess);
15541500Serics     if (Senter && Sexit) {
15551500Serics 	tputs (Senter, 1, putch);
15561500Serics 	pr(mess);
15571500Serics 	tputs (Sexit, 1, putch);
15581500Serics     }
15591500Serics     else
15601500Serics 	pr (mess);
15611500Serics     fflush(stdout);
15621500Serics     errors++;
15631500Serics     longjmp (restore, 1);
15641500Serics }
15651500Serics 
15661500Serics 
15671500Serics set_tty ()
15681500Serics {
15691500Serics 	otty.sg_flags |= MBIT;
15701500Serics 	otty.sg_flags &= ~ECHO;
15711500Serics 	stty(2, &otty);
15721500Serics }
15731500Serics 
15741500Serics reset_tty ()
15751500Serics {
15761500Serics     otty.sg_flags |= ECHO;
15771500Serics     otty.sg_flags &= ~MBIT;
15781500Serics     stty(2, &otty);
15791500Serics }
15801500Serics 
15811500Serics rdline (f)
15821500Serics register FILE *f;
15831500Serics {
15841500Serics     register char c;
15851500Serics     register char *p;
15861500Serics 
15871500Serics     p = Line;
15881500Serics     while ((c = Getc (f)) != '\n' && c != EOF && p - Line < LINSIZ - 1)
15891500Serics 	*p++ = c;
15901500Serics     if (c == '\n')
15911500Serics 	Currline++;
15921500Serics     *p = '\0';
15931500Serics }
15941500Serics 
15951500Serics /* Come here when we get a suspend signal from the terminal */
15961500Serics 
15971500Serics onsusp ()
15981500Serics {
15991500Serics     reset_tty ();
16001500Serics     fflush (stdout);
16011500Serics     /* Send the TSTP signal to suspend our process group */
160213289Ssam     signal(SIGTSTP, SIG_DFL);
160313289Ssam     sigsetmask(0);
16041500Serics     kill (0, SIGTSTP);
16051500Serics     /* Pause for station break */
16061500Serics 
16071500Serics     /* We're back */
16081500Serics     signal (SIGTSTP, onsusp);
16091500Serics     set_tty ();
16101500Serics     if (inwait)
16111500Serics 	    longjmp (restore);
16121500Serics }
1613