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