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