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