148284Sbostic /*-
248284Sbostic * Copyright (c) 1980 The Regents of the University of California.
332736Sbostic * All rights reserved.
432736Sbostic *
548284Sbostic * %sccs.include.redist.c%
621981Sdist */
721981Sdist
813615Ssam #ifndef lint
921981Sdist char copyright[] =
1048284Sbostic "@(#) Copyright (c) 1980 The Regents of the University of California.\n\
1121981Sdist All rights reserved.\n";
1232736Sbostic #endif /* not lint */
133594Sroot
1421981Sdist #ifndef lint
15*58341Storek static char sccsid[] = "@(#)more.c 5.28 (Berkeley) 03/01/93";
1632736Sbostic #endif /* not lint */
1721981Sdist
181500Serics /*
191500Serics ** more.c - General purpose tty output filter and file perusal program
201500Serics **
211500Serics ** by Eric Shienbrood, UC Berkeley
223594Sroot **
233594Sroot ** modified by Geoff Peck, UCB to add underlining, single spacing
243594Sroot ** modified by John Foderaro, UCB to add -c and MORE environment variable
251500Serics */
261500Serics
2732736Sbostic #include <sys/param.h>
2846188Sbostic #include <sys/stat.h>
2946188Sbostic #include <sys/file.h>
301500Serics #include <signal.h>
311500Serics #include <errno.h>
321500Serics #include <sgtty.h>
331500Serics #include <setjmp.h>
3432736Sbostic #include <a.out.h>
3532736Sbostic #include <varargs.h>
3646188Sbostic #include <stdio.h>
37*58341Storek #include <string.h>
3846188Sbostic #include <ctype.h>
3937943Sbostic #include "pathnames.h"
401500Serics
411500Serics #define Fopen(s,m) (Currline = 0,file_pos=0,fopen(s,m))
421500Serics #define Ftell(f) file_pos
431500Serics #define Fseek(f,off) (file_pos=off,fseek(f,off,0))
441500Serics #define Getc(f) (++file_pos, getc(f))
451500Serics #define Ungetc(c,f) (--file_pos, ungetc(c,f))
461500Serics
471500Serics #define MBIT CBREAK
481500Serics #define stty(fd,argp) ioctl(fd,TIOCSETN,argp)
491500Serics
501500Serics #define TBUFSIZ 1024
511500Serics #define LINSIZ 256
5233232Sbostic #define ctrl(letter) (letter & 077)
531500Serics #define RUBOUT '\177'
541500Serics #define ESC '\033'
551500Serics #define QUIT '\034'
561500Serics
5716582Sleres struct sgttyb otty, savetty;
581500Serics long file_pos, file_size;
591500Serics int fnum, no_intty, no_tty, slow_tty;
6046779Sbostic int dum_opt, dlines;
6146779Sbostic void chgwinsz(), end_it(), onquit(), onsusp();
621500Serics int nscroll = 11; /* Number of lines scrolled by 'd' */
631500Serics int fold_opt = 1; /* Fold long lines */
641500Serics int stop_opt = 1; /* Stop after form feeds */
653594Sroot int ssp_opt = 0; /* Suppress white space */
663594Sroot int ul_opt = 1; /* Underline as best we can */
671500Serics int promptlen;
681500Serics int Currline; /* Line we are currently at */
691500Serics int startup = 1;
701500Serics int firstf = 1;
711500Serics int notell = 1;
7217592Sleres int docrterase = 0;
7317592Sleres int docrtkill = 0;
741500Serics int bad_so; /* True if overwriting does not turn off standout */
751500Serics int inwait, Pause, errors;
761500Serics int within; /* true if we are within a file,
771500Serics false if we are between files */
7829907Smckusick int hard, dumb, noscroll, hardtabs, clreol, eatnl;
791500Serics int catch_susp; /* We should catch the SIGTSTP signal */
801500Serics char **fnames; /* The list of file names */
811500Serics int nfiles; /* Number of files left to process */
821500Serics char *shell; /* The name of the shell to use */
831500Serics int shellp; /* A previous shell command exists */
841500Serics char ch;
851500Serics jmp_buf restore;
861500Serics char Line[LINSIZ]; /* Line buffer */
871500Serics int Lpp = 24; /* lines per page */
881500Serics char *Clear; /* clear screen */
891500Serics char *eraseln; /* erase line */
901500Serics char *Senter, *Sexit;/* enter and exit standout mode */
913594Sroot char *ULenter, *ULexit; /* enter and exit underline mode */
923594Sroot char *chUL; /* underline character */
933594Sroot char *chBS; /* backspace character */
943455Sroot char *Home; /* go to home */
953455Sroot char *cursorm; /* cursor movement */
963455Sroot char cursorhome[40]; /* contains cursor movement to home */
973594Sroot char *EodClr; /* clear rest of screen */
981500Serics char *tgetstr();
991500Serics int Mcol = 80; /* number of columns */
1001500Serics int Wrap = 1; /* set if automargins */
10116710Sjak int soglitch; /* terminal has standout mode glitch */
10216710Sjak int ulglitch; /* terminal has underline mode glitch */
10316710Sjak int pstate = 0; /* current UL state */
1043455Sroot char *getenv();
1051500Serics struct {
1061500Serics long chrctr, line;
1071500Serics } context, screen_start;
1081500Serics extern char PC; /* pad character */
1091500Serics extern short ospeed;
1101500Serics
1111500Serics
main(argc,argv)1121500Serics main(argc, argv)
1131500Serics int argc;
1141500Serics char *argv[];
1151500Serics {
1161500Serics register FILE *f;
1171500Serics register char *s;
1181500Serics register char *p;
1191500Serics register char ch;
1201500Serics register int left;
12116582Sleres int prnames = 0;
1221500Serics int initopt = 0;
1231500Serics int srchopt = 0;
1241500Serics int clearit = 0;
1251500Serics int initline;
1261500Serics char initbuf[80];
1271500Serics FILE *checkf();
1281500Serics
1291500Serics nfiles = argc;
1301500Serics fnames = argv;
1311500Serics initterm ();
13215813Sralph nscroll = Lpp/2 - 1;
13315813Sralph if (nscroll <= 0)
13415813Sralph nscroll = 1;
1353455Sroot if(s = getenv("MORE")) argscan(s);
1361500Serics while (--nfiles > 0) {
1371500Serics if ((ch = (*++fnames)[0]) == '-') {
1383455Sroot argscan(*fnames+1);
1391500Serics }
1401500Serics else if (ch == '+') {
1411500Serics s = *fnames;
1421500Serics if (*++s == '/') {
1431500Serics srchopt++;
1441500Serics for (++s, p = initbuf; p < initbuf + 79 && *s != '\0';)
1451500Serics *p++ = *s++;
1461500Serics *p = '\0';
1471500Serics }
1481500Serics else {
1491500Serics initopt++;
1501500Serics for (initline = 0; *s != '\0'; s++)
1511500Serics if (isdigit (*s))
1521500Serics initline = initline*10 + *s -'0';
1531500Serics --initline;
1541500Serics }
1551500Serics }
1561500Serics else break;
1571500Serics }
1583594Sroot /* allow clreol only if Home and eraseln and EodClr strings are
1593455Sroot * defined, and in that case, make sure we are in noscroll mode
1603455Sroot */
1613455Sroot if(clreol)
1623455Sroot {
16318608Sralph if((Home == NULL) || (*Home == '\0') ||
16418608Sralph (eraseln == NULL) || (*eraseln == '\0') ||
16518608Sralph (EodClr == NULL) || (*EodClr == '\0') )
16618608Sralph clreol = 0;
1673455Sroot else noscroll = 1;
1683455Sroot }
1691500Serics if (dlines == 0)
1701500Serics dlines = Lpp - (noscroll ? 1 : 2);
1711500Serics left = dlines;
1721500Serics if (nfiles > 1)
1731500Serics prnames++;
1741500Serics if (!no_intty && nfiles == 0) {
17534104Sbostic char *rindex();
17634104Sbostic
17734104Sbostic p = rindex(argv[0], '/');
17834104Sbostic fputs("usage: ",stderr);
17934104Sbostic fputs(p ? p + 1 : argv[0],stderr);
1801500Serics fputs(" [-dfln] [+linenum | +/pattern] name1 name2 ...\n",stderr);
1811500Serics exit(1);
1821500Serics }
1831500Serics else
1841500Serics f = stdin;
1851500Serics if (!no_tty) {
1861500Serics signal(SIGQUIT, onquit);
1871500Serics signal(SIGINT, end_it);
18827006Sdonn signal(SIGWINCH, chgwinsz);
1891500Serics if (signal (SIGTSTP, SIG_IGN) == SIG_DFL) {
1901500Serics signal(SIGTSTP, onsusp);
1911500Serics catch_susp++;
1921500Serics }
19316582Sleres stty (fileno(stderr), &otty);
1941500Serics }
1951500Serics if (no_intty) {
1961500Serics if (no_tty)
1971500Serics copy_file (stdin);
1981500Serics else {
1991500Serics if ((ch = Getc (f)) == '\f')
2003594Sroot doclear();
2011500Serics else {
2021500Serics Ungetc (ch, f);
2033594Sroot if (noscroll && (ch != EOF)) {
2043594Sroot if (clreol)
2053594Sroot home ();
2063594Sroot else
2073594Sroot doclear ();
2083455Sroot }
2091500Serics }
2101500Serics if (srchopt)
2113455Sroot {
2121500Serics search (initbuf, stdin, 1);
2133594Sroot if (noscroll)
2143594Sroot left--;
2153455Sroot }
2161500Serics else if (initopt)
2171500Serics skiplns (initline, stdin);
2181500Serics screen (stdin, left);
2191500Serics }
2201500Serics no_intty = 0;
2211500Serics prnames++;
2221500Serics firstf = 0;
2231500Serics }
2241500Serics
2251500Serics while (fnum < nfiles) {
2261500Serics if ((f = checkf (fnames[fnum], &clearit)) != NULL) {
2271500Serics context.line = context.chrctr = 0;
2281500Serics Currline = 0;
2291500Serics if (firstf) setjmp (restore);
2301500Serics if (firstf) {
2311500Serics firstf = 0;
2321500Serics if (srchopt)
2333455Sroot {
2341500Serics search (initbuf, f, 1);
2353594Sroot if (noscroll)
2363594Sroot left--;
2373455Sroot }
2381500Serics else if (initopt)
2391500Serics skiplns (initline, f);
2401500Serics }
2411500Serics else if (fnum < nfiles && !no_tty) {
2421500Serics setjmp (restore);
2431500Serics left = command (fnames[fnum], f);
2441500Serics }
2451500Serics if (left != 0) {
24632736Sbostic if ((noscroll || clearit) && (file_size != LONG_MAX))
2473594Sroot if (clreol)
2483594Sroot home ();
2493594Sroot else
2503594Sroot doclear ();
2511500Serics if (prnames) {
2521500Serics if (bad_so)
2531500Serics erase (0);
2543594Sroot if (clreol)
2553594Sroot cleareol ();
2561500Serics pr("::::::::::::::");
2571500Serics if (promptlen > 14)
2581500Serics erase (14);
25946779Sbostic prtf ("\n");
2603455Sroot if(clreol) cleareol();
26146779Sbostic prtf("%s\n", fnames[fnum]);
2623455Sroot if(clreol) cleareol();
26346779Sbostic prtf("::::::::::::::\n");
2641500Serics if (left > Lpp - 4)
2651500Serics left = Lpp - 4;
2661500Serics }
2671500Serics if (no_tty)
2681500Serics copy_file (f);
2691500Serics else {
2701500Serics within++;
2711500Serics screen(f, left);
2721500Serics within = 0;
2731500Serics }
2741500Serics }
2751500Serics setjmp (restore);
2761500Serics fflush(stdout);
2771500Serics fclose(f);
2781500Serics screen_start.line = screen_start.chrctr = 0L;
2791500Serics context.line = context.chrctr = 0L;
2801500Serics }
2811500Serics fnum++;
2821500Serics firstf = 0;
2831500Serics }
2841500Serics reset_tty ();
2851500Serics exit(0);
2861500Serics }
2871500Serics
argscan(s)2883455Sroot argscan(s)
2893455Sroot char *s;
2903455Sroot {
29132273Sbostic int seen_num = 0;
29232273Sbostic
29332273Sbostic while (*s != '\0') {
29432273Sbostic switch (*s) {
29511604Slayer case '0': case '1': case '2':
29611604Slayer case '3': case '4': case '5':
29711604Slayer case '6': case '7': case '8':
29811604Slayer case '9':
29932273Sbostic if (!seen_num) {
30032273Sbostic dlines = 0;
30132273Sbostic seen_num = 1;
30232273Sbostic }
30311604Slayer dlines = dlines*10 + *s - '0';
30411604Slayer break;
30511604Slayer case 'd':
30611604Slayer dum_opt = 1;
30711604Slayer break;
30811604Slayer case 'l':
30911604Slayer stop_opt = 0;
31011604Slayer break;
31111604Slayer case 'f':
31211604Slayer fold_opt = 0;
31311604Slayer break;
31411604Slayer case 'p':
31511604Slayer noscroll++;
31611604Slayer break;
31711604Slayer case 'c':
31811604Slayer clreol++;
31911604Slayer break;
32011604Slayer case 's':
32111604Slayer ssp_opt = 1;
32211604Slayer break;
32311604Slayer case 'u':
32411604Slayer ul_opt = 0;
32511604Slayer break;
32611604Slayer }
32732273Sbostic s++;
32811604Slayer }
3293455Sroot }
3303455Sroot
3313455Sroot
3321500Serics /*
3331500Serics ** Check whether the file named by fs is an ASCII file which the user may
3341500Serics ** access. If it is, return the opened file. Otherwise return NULL.
3351500Serics */
3361500Serics
3371500Serics FILE *
checkf(fs,clearfirst)3381500Serics checkf (fs, clearfirst)
33932736Sbostic register char *fs;
34032736Sbostic int *clearfirst;
3411500Serics {
34232736Sbostic struct stat stbuf;
34332736Sbostic register FILE *f;
34432736Sbostic char c;
3451500Serics
34632736Sbostic if (stat (fs, &stbuf) == -1) {
34732736Sbostic (void)fflush(stdout);
34832736Sbostic if (clreol)
34932736Sbostic cleareol ();
35032736Sbostic perror(fs);
35132736Sbostic return((FILE *)NULL);
35232736Sbostic }
35332736Sbostic if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
35446779Sbostic prtf("\n*** %s: directory ***\n\n", fs);
35532736Sbostic return((FILE *)NULL);
35632736Sbostic }
35732736Sbostic if ((f = Fopen(fs, "r")) == NULL) {
35832736Sbostic (void)fflush(stdout);
35932736Sbostic perror(fs);
36032736Sbostic return((FILE *)NULL);
36132736Sbostic }
36232736Sbostic if (magic(f, fs))
36332736Sbostic return((FILE *)NULL);
36432736Sbostic c = Getc(f);
36532736Sbostic *clearfirst = c == '\f';
3661500Serics Ungetc (c, f);
36732736Sbostic if ((file_size = stbuf.st_size) == 0)
36832736Sbostic file_size = LONG_MAX;
36932736Sbostic return(f);
3701500Serics }
3711500Serics
3721500Serics /*
37332736Sbostic * magic --
37432736Sbostic * check for file magic numbers. This code would best be shared with
37532736Sbostic * the file(1) program or, perhaps, more should not try and be so smart?
37629906Smckusick */
magic(f,fs)37732736Sbostic magic(f, fs)
37832736Sbostic FILE *f;
37932736Sbostic char *fs;
38029906Smckusick {
38132736Sbostic struct exec ex;
38229906Smckusick
38332736Sbostic if (fread(&ex, sizeof(ex), 1, f) == 1)
38432736Sbostic switch(ex.a_magic) {
38532736Sbostic case OMAGIC:
38632736Sbostic case NMAGIC:
38732736Sbostic case ZMAGIC:
38832736Sbostic case 0405:
38932736Sbostic case 0411:
39032736Sbostic case 0177545:
39146779Sbostic prtf("\n******** %s: Not a text file ********\n\n", fs);
39232736Sbostic (void)fclose(f);
39332736Sbostic return(1);
39432736Sbostic }
39532736Sbostic (void)fseek(f, 0L, L_SET); /* rewind() not necessary */
39633087Sbostic return(0);
39729906Smckusick }
39829906Smckusick
39929906Smckusick /*
4001500Serics ** A real function, for the tputs routine in termlib
4011500Serics */
4021500Serics
putch(ch)4031500Serics putch (ch)
4041500Serics char ch;
4051500Serics {
4061500Serics putchar (ch);
4071500Serics }
4081500Serics
4091500Serics /*
4101500Serics ** Print out the contents of the file f, one screenful at a time.
4111500Serics */
4121500Serics
4131500Serics #define STOP -10
4141500Serics
screen(f,num_lines)4151500Serics screen (f, num_lines)
4161500Serics register FILE *f;
4171500Serics register int num_lines;
4181500Serics {
4191500Serics register int c;
4201500Serics register int nchars;
4213594Sroot int length; /* length of current line */
4223594Sroot static int prev_len = 1; /* length of previous line */
4231500Serics
4241500Serics for (;;) {
4251500Serics while (num_lines > 0 && !Pause) {
4261500Serics if ((nchars = getline (f, &length)) == EOF)
4273455Sroot {
4283594Sroot if (clreol)
4293594Sroot clreos();
4301500Serics return;
4313455Sroot }
4323594Sroot if (ssp_opt && length == 0 && prev_len == 0)
4333594Sroot continue;
4343594Sroot prev_len = length;
4351500Serics if (bad_so || (Senter && *Senter == ' ') && promptlen > 0)
4361500Serics erase (0);
4373594Sroot /* must clear before drawing line since tabs on some terminals
4383594Sroot * do not erase what they tab over.
4393594Sroot */
4403594Sroot if (clreol)
4413594Sroot cleareol ();
4421500Serics prbuf (Line, length);
4431500Serics if (nchars < promptlen)
4441500Serics erase (nchars); /* erase () sets promptlen to 0 */
4451500Serics else promptlen = 0;
4463594Sroot /* is this needed?
4473594Sroot * if (clreol)
4483594Sroot * cleareol(); /* must clear again in case we wrapped *
4493594Sroot */
4501500Serics if (nchars < Mcol || !fold_opt)
45116710Sjak prbuf("\n", 1); /* will turn off UL if necessary */
4521500Serics if (nchars == STOP)
4531500Serics break;
4541500Serics num_lines--;
4551500Serics }
45616710Sjak if (pstate) {
45716710Sjak tputs(ULexit, 1, putch);
45816710Sjak pstate = 0;
45916710Sjak }
4601500Serics fflush(stdout);
4611500Serics if ((c = Getc(f)) == EOF)
4623455Sroot {
4633594Sroot if (clreol)
4643594Sroot clreos ();
4651500Serics return;
4663455Sroot }
4673455Sroot
4683594Sroot if (Pause && clreol)
4693594Sroot clreos ();
4701500Serics Ungetc (c, f);
4711500Serics setjmp (restore);
4721500Serics Pause = 0; startup = 0;
4731500Serics if ((num_lines = command (NULL, f)) == 0)
4741500Serics return;
4751500Serics if (hard && promptlen > 0)
4761500Serics erase (0);
47711123Slayer if (noscroll && num_lines >= dlines)
47816582Sleres {
4793594Sroot if (clreol)
4803594Sroot home();
4813594Sroot else
4823594Sroot doclear ();
4833455Sroot }
4841500Serics screen_start.line = Currline;
4851500Serics screen_start.chrctr = Ftell (f);
4861500Serics }
4871500Serics }
4881500Serics
4891500Serics /*
4901500Serics ** Come here if a quit signal is received
4911500Serics */
4921500Serics
49346779Sbostic void
onquit()4941500Serics onquit()
4951500Serics {
4961500Serics signal(SIGQUIT, SIG_IGN);
4971500Serics if (!inwait) {
4981500Serics putchar ('\n');
4991500Serics if (!startup) {
5001500Serics signal(SIGQUIT, onquit);
5011500Serics longjmp (restore, 1);
5021500Serics }
5031500Serics else
5041500Serics Pause++;
5051500Serics }
5061500Serics else if (!dum_opt && notell) {
5071500Serics write (2, "[Use q or Q to quit]", 20);
5081500Serics promptlen += 20;
5091500Serics notell = 0;
5101500Serics }
5111500Serics signal(SIGQUIT, onquit);
5121500Serics }
5131500Serics
5141500Serics /*
51527006Sdonn ** Come here if a signal for a window size change is received
51627006Sdonn */
51727006Sdonn
51846779Sbostic void
chgwinsz()51927006Sdonn chgwinsz()
52027006Sdonn {
52127006Sdonn struct winsize win;
52227006Sdonn
52327006Sdonn (void) signal(SIGWINCH, SIG_IGN);
52427006Sdonn if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) {
52527006Sdonn if (win.ws_row != 0) {
52627006Sdonn Lpp = win.ws_row;
52727006Sdonn nscroll = Lpp/2 - 1;
52827006Sdonn if (nscroll <= 0)
52927006Sdonn nscroll = 1;
53027006Sdonn dlines = Lpp - (noscroll ? 1 : 2);
53127006Sdonn }
53227006Sdonn if (win.ws_col != 0)
53327006Sdonn Mcol = win.ws_col;
53427006Sdonn }
53527006Sdonn (void) signal(SIGWINCH, chgwinsz);
53627006Sdonn }
53727006Sdonn
53827006Sdonn /*
5391500Serics ** Clean up terminal state and exit. Also come here if interrupt signal received
5401500Serics */
5411500Serics
54246779Sbostic void
end_it()5431500Serics end_it ()
5441500Serics {
5451500Serics
5461500Serics reset_tty ();
5473594Sroot if (clreol) {
5483594Sroot putchar ('\r');
5493594Sroot clreos ();
5503594Sroot fflush (stdout);
5513594Sroot }
5523455Sroot else if (!clreol && (promptlen > 0)) {
5531500Serics kill_line ();
5541500Serics fflush (stdout);
5551500Serics }
5561500Serics else
5571500Serics write (2, "\n", 1);
5581500Serics _exit(0);
5591500Serics }
5601500Serics
copy_file(f)5611500Serics copy_file(f)
5621500Serics register FILE *f;
5631500Serics {
5641500Serics register int c;
5651500Serics
5661500Serics while ((c = getc(f)) != EOF)
5671500Serics putchar(c);
5681500Serics }
5691500Serics
5701500Serics /* Simplified printf function */
5711500Serics
prtf(fmt,va_alist)57246779Sbostic prtf (fmt, va_alist)
5731500Serics register char *fmt;
57432737Sbostic va_dcl
5751500Serics {
57632737Sbostic va_list ap;
5771500Serics register char ch;
5781500Serics register int ccount;
5791500Serics
5801500Serics ccount = 0;
58132737Sbostic va_start(ap);
5821500Serics while (*fmt) {
5831500Serics while ((ch = *fmt++) != '%') {
5841500Serics if (ch == '\0')
5851500Serics return (ccount);
5861500Serics ccount++;
5871500Serics putchar (ch);
5881500Serics }
5891500Serics switch (*fmt++) {
5901500Serics case 'd':
59132737Sbostic ccount += printd (va_arg(ap, int));
5921500Serics break;
5931500Serics case 's':
59432737Sbostic ccount += pr (va_arg(ap, char *));
5951500Serics break;
5961500Serics case '%':
5971500Serics ccount++;
5981500Serics putchar ('%');
5991500Serics break;
6001500Serics case '0':
6011500Serics return (ccount);
6021500Serics default:
6031500Serics break;
6041500Serics }
6051500Serics }
60632737Sbostic va_end(ap);
6071500Serics return (ccount);
6081500Serics
6091500Serics }
6101500Serics
6111500Serics /*
6121500Serics ** Print an integer as a string of decimal digits,
6131500Serics ** returning the length of the print representation.
6141500Serics */
6151500Serics
printd(n)6161500Serics printd (n)
6171500Serics int n;
6181500Serics {
6191500Serics int a, nchars;
6201500Serics
6211500Serics if (a = n/10)
6221500Serics nchars = 1 + printd(a);
6231500Serics else
6241500Serics nchars = 1;
6251500Serics putchar (n % 10 + '0');
6261500Serics return (nchars);
6271500Serics }
6281500Serics
6291500Serics /* Put the print representation of an integer into a string */
6301500Serics static char *sptr;
6311500Serics
scanstr(n,str)6321500Serics scanstr (n, str)
6331500Serics int n;
6341500Serics char *str;
6351500Serics {
6361500Serics sptr = str;
63711604Slayer Sprintf (n);
6381500Serics *sptr = '\0';
6391500Serics }
6401500Serics
Sprintf(n)64111604Slayer Sprintf (n)
6421500Serics {
6431500Serics int a;
6441500Serics
6451500Serics if (a = n/10)
64611604Slayer Sprintf (a);
6471500Serics *sptr++ = n % 10 + '0';
6481500Serics }
6491500Serics
65033232Sbostic static char bell = ctrl('G');
6511500Serics
6521500Serics /* See whether the last component of the path name "path" is equal to the
6531500Serics ** string "string"
6541500Serics */
6551500Serics
tailequ(path,string)6561500Serics tailequ (path, string)
6571500Serics char *path;
6581500Serics register char *string;
6591500Serics {
6601500Serics register char *tail;
6611500Serics
6621500Serics tail = path + strlen(path);
6631500Serics while (tail >= path)
6641500Serics if (*(--tail) == '/')
6651500Serics break;
6661500Serics ++tail;
6671500Serics while (*tail++ == *string++)
6681500Serics if (*tail == '\0')
6691500Serics return(1);
6701500Serics return(0);
6711500Serics }
6721500Serics
prompt(filename)6731500Serics prompt (filename)
6741500Serics char *filename;
6751500Serics {
6763594Sroot if (clreol)
6773594Sroot cleareol ();
6783455Sroot else if (promptlen > 0)
6791500Serics kill_line ();
6801500Serics if (!hard) {
6811500Serics promptlen = 8;
68216710Sjak if (Senter && Sexit) {
6831500Serics tputs (Senter, 1, putch);
68416710Sjak promptlen += (2 * soglitch);
68516710Sjak }
6863594Sroot if (clreol)
6873594Sroot cleareol ();
6881500Serics pr("--More--");
6891500Serics if (filename != NULL) {
69046779Sbostic promptlen += prtf ("(Next file: %s)", filename);
6911500Serics }
6921500Serics else if (!no_intty) {
69346779Sbostic promptlen += prtf ("(%d%%)", (int)((file_pos * 100) / file_size));
6941500Serics }
6951500Serics if (dum_opt) {
69616710Sjak promptlen += pr("[Press space to continue, 'q' to quit.]");
6971500Serics }
6981500Serics if (Senter && Sexit)
6991500Serics tputs (Sexit, 1, putch);
7003594Sroot if (clreol)
7013594Sroot clreos ();
7021500Serics fflush(stdout);
7031500Serics }
7041500Serics else
7051500Serics write (2, &bell, 1);
7061500Serics inwait++;
7071500Serics }
7081500Serics
7091500Serics /*
7101500Serics ** Get a logical line
7111500Serics */
7121500Serics
getline(f,length)7131500Serics getline(f, length)
7141500Serics register FILE *f;
7151500Serics int *length;
7161500Serics {
7171500Serics register int c;
7181500Serics register char *p;
7191500Serics register int column;
7201500Serics static int colflg;
7211500Serics
7221500Serics p = Line;
7231500Serics column = 0;
7241500Serics c = Getc (f);
7251500Serics if (colflg && c == '\n') {
7261500Serics Currline++;
7271500Serics c = Getc (f);
7281500Serics }
7291500Serics while (p < &Line[LINSIZ - 1]) {
7301500Serics if (c == EOF) {
7311500Serics if (p > Line) {
7321500Serics *p = '\0';
7331500Serics *length = p - Line;
7341500Serics return (column);
7351500Serics }
7361500Serics *length = p - Line;
7371500Serics return (EOF);
7381500Serics }
7391500Serics if (c == '\n') {
7401500Serics Currline++;
7411500Serics break;
7421500Serics }
7431500Serics *p++ = c;
7441500Serics if (c == '\t')
74529907Smckusick if (!hardtabs || column < promptlen && !hard) {
74629907Smckusick if (hardtabs && eraseln && !dumb) {
7471500Serics column = 1 + (column | 7);
7481500Serics tputs (eraseln, 1, putch);
7491500Serics promptlen = 0;
7501500Serics }
7511500Serics else {
75229907Smckusick for (--p; p < &Line[LINSIZ - 1];) {
7531500Serics *p++ = ' ';
75429907Smckusick if ((++column & 7) == 0)
75529907Smckusick break;
7561500Serics }
7571500Serics if (column >= promptlen) promptlen = 0;
7581500Serics }
7591500Serics }
7601500Serics else
7611500Serics column = 1 + (column | 7);
7629627Ssklower else if (c == '\b' && column > 0)
7631500Serics column--;
7641500Serics else if (c == '\r')
7651500Serics column = 0;
7661500Serics else if (c == '\f' && stop_opt) {
7671500Serics p[-1] = '^';
7681500Serics *p++ = 'L';
7691500Serics column += 2;
7701500Serics Pause++;
7711500Serics }
7721500Serics else if (c == EOF) {
7731500Serics *length = p - Line;
7741500Serics return (column);
7751500Serics }
7761500Serics else if (c >= ' ' && c != RUBOUT)
7771500Serics column++;
7781500Serics if (column >= Mcol && fold_opt) break;
7791500Serics c = Getc (f);
7801500Serics }
7811500Serics if (column >= Mcol && Mcol > 0) {
7821500Serics if (!Wrap) {
7831500Serics *p++ = '\n';
7841500Serics }
7851500Serics }
7861500Serics colflg = column == Mcol && fold_opt;
78729907Smckusick if (colflg && eatnl && Wrap) {
78829907Smckusick *p++ = '\n'; /* simulate normal wrap */
78929907Smckusick }
7901500Serics *length = p - Line;
7911500Serics *p = 0;
7921500Serics return (column);
7931500Serics }
7941500Serics
7951500Serics /*
7961500Serics ** Erase the rest of the prompt, assuming we are starting at column col.
7971500Serics */
7981500Serics
erase(col)7991500Serics erase (col)
8001500Serics register int col;
8011500Serics {
8021500Serics
8031500Serics if (promptlen == 0)
8041500Serics return;
8051500Serics if (hard) {
8061500Serics putchar ('\n');
8071500Serics }
8081500Serics else {
8091500Serics if (col == 0)
8101500Serics putchar ('\r');
8111500Serics if (!dumb && eraseln)
8121500Serics tputs (eraseln, 1, putch);
8131500Serics else
8141500Serics for (col = promptlen - col; col > 0; col--)
8151500Serics putchar (' ');
8161500Serics }
8171500Serics promptlen = 0;
8181500Serics }
8191500Serics
8201500Serics /*
8211500Serics ** Erase the current line entirely
8221500Serics */
8231500Serics
kill_line()8241500Serics kill_line ()
8251500Serics {
8261500Serics erase (0);
8271500Serics if (!eraseln || dumb) putchar ('\r');
8281500Serics }
8291500Serics
8301500Serics /*
8313455Sroot * force clear to end of line
8323455Sroot */
cleareol()8333455Sroot cleareol()
8343455Sroot {
8353594Sroot tputs(eraseln, 1, putch);
8363455Sroot }
8373455Sroot
clreos()8383594Sroot clreos()
8393455Sroot {
8403594Sroot tputs(EodClr, 1, putch);
8413455Sroot }
8423455Sroot
8433455Sroot /*
8441500Serics ** Print string and return number of characters
8451500Serics */
8461500Serics
pr(s1)8471500Serics pr(s1)
8481500Serics char *s1;
8491500Serics {
8501500Serics register char *s;
8511500Serics register char c;
8521500Serics
8531500Serics for (s = s1; c = *s++; )
8541500Serics putchar(c);
8551500Serics return (s - s1 - 1);
8561500Serics }
8571500Serics
8581500Serics
8591500Serics /* Print a buffer of n characters */
8601500Serics
prbuf(s,n)8611500Serics prbuf (s, n)
8621500Serics register char *s;
8631500Serics register int n;
8641500Serics {
86516710Sjak register char c; /* next output character */
8663594Sroot register int state; /* next output char's UL state */
86716710Sjak #define wouldul(s,n) ((n) >= 2 && (((s)[0] == '_' && (s)[1] == '\b') || ((s)[1] == '\b' && (s)[2] == '_')))
8683594Sroot
8693594Sroot while (--n >= 0)
8703594Sroot if (!ul_opt)
8713594Sroot putchar (*s++);
8723594Sroot else {
87316710Sjak if (*s == ' ' && pstate == 0 && ulglitch && wouldul(s+1, n-1)) {
87416710Sjak s++;
87516710Sjak continue;
87616710Sjak }
87716710Sjak if (state = wouldul(s, n)) {
87816710Sjak c = (*s == '_')? s[2] : *s ;
8793594Sroot n -= 2;
88016710Sjak s += 3;
88116710Sjak } else
8823594Sroot c = *s++;
88316710Sjak if (state != pstate) {
88416710Sjak if (c == ' ' && state == 0 && ulglitch && wouldul(s, n-1))
88516710Sjak state = 1;
88616710Sjak else
88716710Sjak tputs(state ? ULenter : ULexit, 1, putch);
8883594Sroot }
88916710Sjak if (c != ' ' || pstate == 0 || state != 0 || ulglitch == 0)
89016710Sjak putchar(c);
8913594Sroot if (state && *chUL) {
8923594Sroot pr(chBS);
8933594Sroot tputs(chUL, 1, putch);
8943594Sroot }
89516710Sjak pstate = state;
8963594Sroot }
8971500Serics }
8981500Serics
8991500Serics /*
9001500Serics ** Clear the screen
9011500Serics */
9021500Serics
doclear()9031500Serics doclear()
9041500Serics {
9051500Serics if (Clear && !hard) {
9061500Serics tputs(Clear, 1, putch);
9071500Serics
9081500Serics /* Put out carriage return so that system doesn't
9091500Serics ** get confused by escape sequences when expanding tabs
9101500Serics */
9111500Serics putchar ('\r');
9121500Serics promptlen = 0;
9131500Serics }
9141500Serics }
9151500Serics
9163455Sroot /*
9173455Sroot * Go to home position
9183455Sroot */
home()9193455Sroot home()
9203455Sroot {
9213455Sroot tputs(Home,1,putch);
9223455Sroot }
9233455Sroot
9241500Serics static int lastcmd, lastarg, lastp;
9251500Serics static int lastcolon;
9261500Serics char shell_line[132];
9271500Serics
9281500Serics /*
9291500Serics ** Read a command and do it. A command consists of an optional integer
9301500Serics ** argument followed by the command character. Return the number of lines
9311500Serics ** to display in the next screenful. If there is nothing more to display
9321500Serics ** in the current file, zero is returned.
9331500Serics */
9341500Serics
command(filename,f)9351500Serics command (filename, f)
9361500Serics char *filename;
9371500Serics register FILE *f;
9381500Serics {
9391500Serics register int nlines;
9401500Serics register int retval;
9411500Serics register char c;
9421500Serics char colonch;
9431500Serics FILE *helpf;
9441500Serics int done;
9451500Serics char comchar, cmdbuf[80], *p;
9461500Serics
9471500Serics #define ret(val) retval=val;done++;break
9481500Serics
9491500Serics done = 0;
9501500Serics if (!errors)
9511500Serics prompt (filename);
9521500Serics else
9531500Serics errors = 0;
9541500Serics if (MBIT == RAW && slow_tty) {
9551500Serics otty.sg_flags |= MBIT;
95616582Sleres stty(fileno(stderr), &otty);
9571500Serics }
9581500Serics for (;;) {
9591500Serics nlines = number (&comchar);
9601500Serics lastp = colonch = 0;
9611500Serics if (comchar == '.') { /* Repeat last command */
9621500Serics lastp++;
9631500Serics comchar = lastcmd;
9641500Serics nlines = lastarg;
9651500Serics if (lastcmd == ':')
9661500Serics colonch = lastcolon;
9671500Serics }
9681500Serics lastcmd = comchar;
9691500Serics lastarg = nlines;
9701500Serics if (comchar == otty.sg_erase) {
9711500Serics kill_line ();
9721500Serics prompt (filename);
9731500Serics continue;
9741500Serics }
9751500Serics switch (comchar) {
9761500Serics case ':':
9771500Serics retval = colon (filename, colonch, nlines);
9781500Serics if (retval >= 0)
9791500Serics done++;
9801500Serics break;
98124490Sbloom case 'b':
98233232Sbostic case ctrl('B'):
98324490Sbloom {
98424490Sbloom register int initline;
98524490Sbloom
98624490Sbloom if (no_intty) {
98724490Sbloom write(2, &bell, 1);
98824490Sbloom return (-1);
98924490Sbloom }
99024490Sbloom
99124490Sbloom if (nlines == 0) nlines++;
99224490Sbloom
99324490Sbloom putchar ('\r');
99424490Sbloom erase (0);
99546779Sbostic prtf ("\n");
99624490Sbloom if (clreol)
99724490Sbloom cleareol ();
99846779Sbostic prtf ("...back %d page", nlines);
99924490Sbloom if (nlines > 1)
100024490Sbloom pr ("s\n");
100124490Sbloom else
100224490Sbloom pr ("\n");
100324490Sbloom
100424490Sbloom if (clreol)
100524490Sbloom cleareol ();
100624490Sbloom pr ("\n");
100724490Sbloom
100824490Sbloom initline = Currline - dlines * (nlines + 1);
100924490Sbloom if (! noscroll)
101024490Sbloom --initline;
101124490Sbloom if (initline < 0) initline = 0;
101224490Sbloom Fseek(f, 0L);
101324490Sbloom Currline = 0; /* skiplns() will make Currline correct */
101424490Sbloom skiplns(initline, f);
101524490Sbloom if (! noscroll) {
101624490Sbloom ret(dlines + 1);
101724490Sbloom }
101824490Sbloom else {
101924490Sbloom ret(dlines);
102024490Sbloom }
102124490Sbloom }
10221500Serics case ' ':
10231500Serics case 'z':
10241500Serics if (nlines == 0) nlines = dlines;
10251500Serics else if (comchar == 'z') dlines = nlines;
10261500Serics ret (nlines);
10271500Serics case 'd':
102833232Sbostic case ctrl('D'):
10291500Serics if (nlines != 0) nscroll = nlines;
10301500Serics ret (nscroll);
10311500Serics case 'q':
10321500Serics case 'Q':
10331500Serics end_it ();
10341500Serics case 's':
10351500Serics case 'f':
10361500Serics if (nlines == 0) nlines++;
10371500Serics if (comchar == 'f')
10381500Serics nlines *= dlines;
10391500Serics putchar ('\r');
10401500Serics erase (0);
104146779Sbostic prtf ("\n");
10423594Sroot if (clreol)
10433594Sroot cleareol ();
104446779Sbostic prtf ("...skipping %d line", nlines);
10451500Serics if (nlines > 1)
10463594Sroot pr ("s\n");
10471500Serics else
10483594Sroot pr ("\n");
10493594Sroot
10503594Sroot if (clreol)
10513594Sroot cleareol ();
10523594Sroot pr ("\n");
10533594Sroot
10541500Serics while (nlines > 0) {
10551500Serics while ((c = Getc (f)) != '\n')
10561500Serics if (c == EOF) {
10571500Serics retval = 0;
10581500Serics done++;
10591500Serics goto endsw;
10601500Serics }
10611500Serics Currline++;
10621500Serics nlines--;
10631500Serics }
10641500Serics ret (dlines);
10651500Serics case '\n':
10661500Serics if (nlines != 0)
10671500Serics dlines = nlines;
10681500Serics else
10691500Serics nlines = 1;
10701500Serics ret (nlines);
10711500Serics case '\f':
10721500Serics if (!no_intty) {
10731500Serics doclear ();
10741500Serics Fseek (f, screen_start.chrctr);
10751500Serics Currline = screen_start.line;
10761500Serics ret (dlines);
10771500Serics }
10781500Serics else {
10791500Serics write (2, &bell, 1);
10801500Serics break;
10811500Serics }
10821500Serics case '\'':
10831500Serics if (!no_intty) {
10841500Serics kill_line ();
10851500Serics pr ("\n***Back***\n\n");
10861500Serics Fseek (f, context.chrctr);
10871500Serics Currline = context.line;
10881500Serics ret (dlines);
10891500Serics }
10901500Serics else {
10911500Serics write (2, &bell, 1);
10921500Serics break;
10931500Serics }
10941500Serics case '=':
10951500Serics kill_line ();
10961500Serics promptlen = printd (Currline);
10971500Serics fflush (stdout);
10981500Serics break;
10991500Serics case 'n':
11001500Serics lastp++;
11011500Serics case '/':
11021500Serics if (nlines == 0) nlines++;
11031500Serics kill_line ();
11041500Serics pr ("/");
11051500Serics promptlen = 1;
11061500Serics fflush (stdout);
11071500Serics if (lastp) {
11081500Serics write (2,"\r", 1);
11091500Serics search (NULL, f, nlines); /* Use previous r.e. */
11101500Serics }
11111500Serics else {
11121500Serics ttyin (cmdbuf, 78, '/');
11131500Serics write (2, "\r", 1);
11141500Serics search (cmdbuf, f, nlines);
11151500Serics }
11163455Sroot ret (dlines-1);
11171500Serics case '!':
11181500Serics do_shell (filename);
11191500Serics break;
112024490Sbloom case '?':
11211500Serics case 'h':
11221500Serics if ((helpf = fopen (HELPFILE, "r")) == NULL)
11231500Serics error ("Can't open help file");
11241500Serics if (noscroll) doclear ();
11251500Serics copy_file (helpf);
112627006Sdonn fclose (helpf);
11271500Serics prompt (filename);
11281500Serics break;
11291500Serics case 'v': /* This case should go right before default */
11301500Serics if (!no_intty) {
11311500Serics kill_line ();
11321500Serics cmdbuf[0] = '+';
113324490Sbloom scanstr (Currline - dlines < 0 ? 0
113424490Sbloom : Currline - (dlines + 1) / 2, &cmdbuf[1]);
11351500Serics pr ("vi "); pr (cmdbuf); putchar (' '); pr (fnames[fnum]);
113638522Sbostic execute (filename, _PATH_VI, "vi", cmdbuf, fnames[fnum], 0);
11371500Serics break;
11381500Serics }
11391500Serics default:
114016710Sjak if (dum_opt) {
114116710Sjak kill_line ();
114216710Sjak if (Senter && Sexit) {
114316710Sjak tputs (Senter, 1, putch);
114416710Sjak promptlen = pr ("[Press 'h' for instructions.]") + (2 * soglitch);
114516710Sjak tputs (Sexit, 1, putch);
114616710Sjak }
114716710Sjak else
114816710Sjak promptlen = pr ("[Press 'h' for instructions.]");
114916710Sjak fflush (stdout);
115016710Sjak }
115116710Sjak else
115216710Sjak write (2, &bell, 1);
11531500Serics break;
11541500Serics }
11551500Serics if (done) break;
11561500Serics }
11571500Serics putchar ('\r');
11581500Serics endsw:
11591500Serics inwait = 0;
11601500Serics notell++;
11611500Serics if (MBIT == RAW && slow_tty) {
11621500Serics otty.sg_flags &= ~MBIT;
116316582Sleres stty(fileno(stderr), &otty);
11641500Serics }
11651500Serics return (retval);
11661500Serics }
11671500Serics
11681500Serics char ch;
11691500Serics
11701500Serics /*
11711500Serics * Execute a colon-prefixed command.
11721500Serics * Returns <0 if not a command that should cause
11731500Serics * more of the file to be printed.
11741500Serics */
11751500Serics
colon(filename,cmd,nlines)11761500Serics colon (filename, cmd, nlines)
11771500Serics char *filename;
11781500Serics int cmd;
11791500Serics int nlines;
11801500Serics {
11811500Serics if (cmd == 0)
11821500Serics ch = readch ();
11831500Serics else
11841500Serics ch = cmd;
11851500Serics lastcolon = ch;
11861500Serics switch (ch) {
11871500Serics case 'f':
11881500Serics kill_line ();
11891500Serics if (!no_intty)
119046779Sbostic promptlen = prtf ("\"%s\" line %d", fnames[fnum], Currline);
11911500Serics else
119246779Sbostic promptlen = prtf ("[Not a file] line %d", Currline);
11931500Serics fflush (stdout);
11941500Serics return (-1);
11951500Serics case 'n':
11961500Serics if (nlines == 0) {
11971500Serics if (fnum >= nfiles - 1)
11981500Serics end_it ();
11991500Serics nlines++;
12001500Serics }
12011500Serics putchar ('\r');
12021500Serics erase (0);
12031500Serics skipf (nlines);
12041500Serics return (0);
12051500Serics case 'p':
12061500Serics if (no_intty) {
12071500Serics write (2, &bell, 1);
12081500Serics return (-1);
12091500Serics }
12101500Serics putchar ('\r');
12111500Serics erase (0);
12121500Serics if (nlines == 0)
12131500Serics nlines++;
12141500Serics skipf (-nlines);
12151500Serics return (0);
12161500Serics case '!':
12171500Serics do_shell (filename);
12181500Serics return (-1);
12191500Serics case 'q':
12201500Serics case 'Q':
12211500Serics end_it ();
12221500Serics default:
12231500Serics write (2, &bell, 1);
12241500Serics return (-1);
12251500Serics }
12261500Serics }
12271500Serics
12281500Serics /*
12291500Serics ** Read a decimal number from the terminal. Set cmd to the non-digit which
12301500Serics ** terminates the number.
12311500Serics */
12321500Serics
number(cmd)12331500Serics number(cmd)
12341500Serics char *cmd;
12351500Serics {
12361500Serics register int i;
12371500Serics
12381500Serics i = 0; ch = otty.sg_kill;
12391500Serics for (;;) {
12401500Serics ch = readch ();
12411500Serics if (ch >= '0' && ch <= '9')
12421500Serics i = i*10 + ch - '0';
12431500Serics else if (ch == otty.sg_kill)
12441500Serics i = 0;
12451500Serics else {
12461500Serics *cmd = ch;
12471500Serics break;
12481500Serics }
12491500Serics }
12501500Serics return (i);
12511500Serics }
12521500Serics
do_shell(filename)12531500Serics do_shell (filename)
12541500Serics char *filename;
12551500Serics {
12561500Serics char cmdbuf[80];
12571500Serics
12581500Serics kill_line ();
12591500Serics pr ("!");
12601500Serics fflush (stdout);
12611500Serics promptlen = 1;
12621500Serics if (lastp)
12631500Serics pr (shell_line);
12641500Serics else {
12651500Serics ttyin (cmdbuf, 78, '!');
12661500Serics if (expand (shell_line, cmdbuf)) {
12671500Serics kill_line ();
126846779Sbostic promptlen = prtf ("!%s", shell_line);
12691500Serics }
12701500Serics }
12711500Serics fflush (stdout);
12721500Serics write (2, "\n", 1);
12731500Serics promptlen = 0;
12741500Serics shellp = 1;
12751500Serics execute (filename, shell, shell, "-c", shell_line, 0);
12761500Serics }
12771500Serics
12781500Serics /*
12791500Serics ** Search for nth ocurrence of regular expression contained in buf in the file
12801500Serics */
12811500Serics
search(buf,file,n)12821500Serics search (buf, file, n)
12831500Serics char buf[];
12841500Serics FILE *file;
12851500Serics register int n;
12861500Serics {
12871500Serics long startline = Ftell (file);
12881500Serics register long line1 = startline;
12891500Serics register long line2 = startline;
12901500Serics register long line3 = startline;
12911500Serics register int lncount;
12921500Serics int saveln, rv, re_exec();
12931500Serics char *s, *re_comp();
12941500Serics
12951500Serics context.line = saveln = Currline;
12961500Serics context.chrctr = startline;
12971500Serics lncount = 0;
12981500Serics if ((s = re_comp (buf)) != 0)
12991500Serics error (s);
13001500Serics while (!feof (file)) {
13011500Serics line3 = line2;
13021500Serics line2 = line1;
13031500Serics line1 = Ftell (file);
13041500Serics rdline (file);
13051500Serics lncount++;
13061500Serics if ((rv = re_exec (Line)) == 1)
13071500Serics if (--n == 0) {
13081500Serics if (lncount > 3 || (lncount > 1 && no_intty))
13093455Sroot {
13103455Sroot pr ("\n");
13113594Sroot if (clreol)
13123594Sroot cleareol ();
13133455Sroot pr("...skipping\n");
13143455Sroot }
13151500Serics if (!no_intty) {
13161500Serics Currline -= (lncount >= 3 ? 3 : lncount);
13171500Serics Fseek (file, line3);
13183594Sroot if (noscroll)
13193594Sroot if (clreol) {
13203594Sroot home ();
13213594Sroot cleareol ();
132216582Sleres }
13233594Sroot else
13243594Sroot doclear ();
13251500Serics }
13261500Serics else {
13271500Serics kill_line ();
13283594Sroot if (noscroll)
13293594Sroot if (clreol) {
133016582Sleres home ();
13313594Sroot cleareol ();
133216582Sleres }
13333594Sroot else
13343594Sroot doclear ();
13351500Serics pr (Line);
13361500Serics putchar ('\n');
13371500Serics }
13381500Serics break;
13391500Serics }
13401500Serics else if (rv == -1)
13411500Serics error ("Regular expression botch");
13421500Serics }
13431500Serics if (feof (file)) {
13441500Serics if (!no_intty) {
134546188Sbostic /* file->_flag &= ~_IOEOF; /* why doesn't fseek do this ??!!??! */
13461500Serics Currline = saveln;
13471500Serics Fseek (file, startline);
13481500Serics }
13491500Serics else {
13501500Serics pr ("\nPattern not found\n");
13511500Serics end_it ();
13521500Serics }
13531500Serics error ("Pattern not found");
13541500Serics }
13551500Serics }
13561500Serics
135732737Sbostic /*VARARGS2*/
execute(filename,cmd,va_alist)135832737Sbostic execute (filename, cmd, va_alist)
13591500Serics char *filename;
136032737Sbostic char *cmd;
136132737Sbostic va_dcl
13621500Serics {
13631500Serics int id;
136416710Sjak int n;
136532737Sbostic va_list argp;
13661500Serics
13671500Serics fflush (stdout);
13681500Serics reset_tty ();
136916710Sjak for (n = 10; (id = fork ()) < 0 && n > 0; n--)
13701500Serics sleep (5);
13711500Serics if (id == 0) {
137216710Sjak if (!isatty(0)) {
137316710Sjak close(0);
137416710Sjak open("/dev/tty", 0);
137516710Sjak }
137632737Sbostic va_start(argp);
137732737Sbostic execv (cmd, argp);
13781500Serics write (2, "exec failed\n", 12);
13791500Serics exit (1);
138033087Sbostic va_end(argp); /* balance {}'s for some UNIX's */
13811500Serics }
138216710Sjak if (id > 0) {
138316710Sjak signal (SIGINT, SIG_IGN);
138416710Sjak signal (SIGQUIT, SIG_IGN);
138516710Sjak if (catch_susp)
138616710Sjak signal(SIGTSTP, SIG_DFL);
138716710Sjak while (wait(0) > 0);
138816710Sjak signal (SIGINT, end_it);
138916710Sjak signal (SIGQUIT, onquit);
139016710Sjak if (catch_susp)
139116710Sjak signal(SIGTSTP, onsusp);
139216710Sjak } else
139316710Sjak write(2, "can't fork\n", 11);
13941500Serics set_tty ();
13951500Serics pr ("------------------------\n");
13961500Serics prompt (filename);
13971500Serics }
13981500Serics /*
13991500Serics ** Skip n lines in the file f
14001500Serics */
14011500Serics
skiplns(n,f)14021500Serics skiplns (n, f)
14031500Serics register int n;
14041500Serics register FILE *f;
14051500Serics {
14061500Serics register char c;
14071500Serics
14081500Serics while (n > 0) {
14091500Serics while ((c = Getc (f)) != '\n')
14101500Serics if (c == EOF)
14111500Serics return;
14121500Serics n--;
14131500Serics Currline++;
14141500Serics }
14151500Serics }
14161500Serics
14171500Serics /*
14181500Serics ** Skip nskip files in the file list (from the command line). Nskip may be
14191500Serics ** negative.
14201500Serics */
14211500Serics
skipf(nskip)14221500Serics skipf (nskip)
14231500Serics register int nskip;
14241500Serics {
14251500Serics if (nskip == 0) return;
14261500Serics if (nskip > 0) {
14271500Serics if (fnum + nskip > nfiles - 1)
14281500Serics nskip = nfiles - fnum - 1;
14291500Serics }
14301500Serics else if (within)
14311500Serics ++fnum;
14321500Serics fnum += nskip;
14331500Serics if (fnum < 0)
14341500Serics fnum = 0;
14353594Sroot pr ("\n...Skipping ");
14363455Sroot pr ("\n");
14373594Sroot if (clreol)
14383594Sroot cleareol ();
14393455Sroot pr ("...Skipping ");
14401500Serics pr (nskip > 0 ? "to file " : "back to file ");
14411500Serics pr (fnames[fnum]);
14423455Sroot pr ("\n");
14433594Sroot if (clreol)
14443594Sroot cleareol ();
14453455Sroot pr ("\n");
14461500Serics --fnum;
14471500Serics }
14481500Serics
14491500Serics /*----------------------------- Terminal I/O -------------------------------*/
14501500Serics
initterm()14511500Serics initterm ()
14521500Serics {
14531500Serics char buf[TBUFSIZ];
145417195Sralph static char clearbuf[TBUFSIZ];
14551500Serics char *clearptr, *padstr;
14561500Serics int ldisc;
145717592Sleres int lmode;
145810823Ssam char *term;
145916582Sleres int tgrp;
146018030Sbloom struct winsize win;
1461*58341Storek char *tgoto();
14621500Serics
146316582Sleres retry:
146453073Sbostic if (!(no_tty = ioctl(fileno(stdout), TIOCGETP, &otty))) {
146517592Sleres if (ioctl(fileno(stdout), TIOCLGET, &lmode) < 0) {
146617592Sleres perror("TIOCLGET");
146717592Sleres exit(1);
146817592Sleres }
146917592Sleres docrterase = ((lmode & LCRTERA) != 0);
147017592Sleres docrtkill = ((lmode & LCRTKIL) != 0);
147116582Sleres /*
147217592Sleres * Wait until we're in the foreground before we save the
147317592Sleres * the terminal modes.
147416582Sleres */
147516582Sleres if (ioctl(fileno(stdout), TIOCGPGRP, &tgrp) < 0) {
147617592Sleres perror("TIOCGPGRP");
147716582Sleres exit(1);
147816582Sleres }
147916582Sleres if (tgrp != getpgrp(0)) {
148016582Sleres kill(0, SIGTTOU);
148116582Sleres goto retry;
148216582Sleres }
148313830Skre if ((term = getenv("TERM")) == 0 || tgetent(buf, term) <= 0) {
14843594Sroot dumb++; ul_opt = 0;
14851500Serics }
14861500Serics else {
148718030Sbloom if (ioctl(fileno(stdout), TIOCGWINSZ, &win) < 0) {
148818030Sbloom Lpp = tgetnum("li");
148918030Sbloom Mcol = tgetnum("co");
149018030Sbloom } else {
149118030Sbloom if ((Lpp = win.ws_row) == 0)
149218030Sbloom Lpp = tgetnum("li");
149318030Sbloom if ((Mcol = win.ws_col) == 0)
149418030Sbloom Mcol = tgetnum("co");
149518030Sbloom }
149618030Sbloom if ((Lpp <= 0) || tgetflag("hc")) {
14971500Serics hard++; /* Hard copy terminal */
14981500Serics Lpp = 24;
14991500Serics }
150029907Smckusick if (tgetflag("xn"))
150129907Smckusick eatnl++; /* Eat newline at last column + 1; dec, concept */
150218030Sbloom if (Mcol <= 0)
150318030Sbloom Mcol = 80;
150418030Sbloom
15051500Serics if (tailequ (fnames[0], "page") || !hard && tgetflag("ns"))
15061500Serics noscroll++;
15071500Serics Wrap = tgetflag("am");
15081500Serics bad_so = tgetflag ("xs");
15091500Serics clearptr = clearbuf;
15101500Serics eraseln = tgetstr("ce",&clearptr);
15111500Serics Clear = tgetstr("cl", &clearptr);
15121500Serics Senter = tgetstr("so", &clearptr);
15131500Serics Sexit = tgetstr("se", &clearptr);
151416710Sjak if ((soglitch = tgetnum("sg")) < 0)
151516710Sjak soglitch = 0;
15163594Sroot
15173594Sroot /*
15183594Sroot * Set up for underlining: some terminals don't need it;
15193594Sroot * others have start/stop sequences, still others have an
15203594Sroot * underline char sequence which is assumed to move the
15213594Sroot * cursor forward one character. If underline sequence
15223594Sroot * isn't available, settle for standout sequence.
15233594Sroot */
15243594Sroot
15253594Sroot if (tgetflag("ul") || tgetflag("os"))
15263594Sroot ul_opt = 0;
15273594Sroot if ((chUL = tgetstr("uc", &clearptr)) == NULL )
15283594Sroot chUL = "";
152916710Sjak if (((ULenter = tgetstr("us", &clearptr)) == NULL ||
153016710Sjak (ULexit = tgetstr("ue", &clearptr)) == NULL) && !*chUL) {
153116710Sjak if ((ULenter = Senter) == NULL || (ULexit = Sexit) == NULL) {
153216710Sjak ULenter = "";
153316710Sjak ULexit = "";
153416710Sjak } else
153516710Sjak ulglitch = soglitch;
153616710Sjak } else {
153716710Sjak if ((ulglitch = tgetnum("ug")) < 0)
153816710Sjak ulglitch = 0;
153916710Sjak }
154016582Sleres
15411500Serics if (padstr = tgetstr("pc", &clearptr))
15421500Serics PC = *padstr;
15433455Sroot Home = tgetstr("ho",&clearptr);
154413536Ssam if (Home == 0 || *Home == '\0')
15453455Sroot {
15463594Sroot if ((cursorm = tgetstr("cm", &clearptr)) != NULL) {
15473594Sroot strcpy(cursorhome, tgoto(cursorm, 0, 0));
15483455Sroot Home = cursorhome;
15493455Sroot }
15503455Sroot }
15513594Sroot EodClr = tgetstr("cd", &clearptr);
155225540Smckusick if ((chBS = tgetstr("bc", &clearptr)) == NULL)
155325540Smckusick chBS = "\b";
155425540Smckusick
15551500Serics }
15561500Serics if ((shell = getenv("SHELL")) == NULL)
15571500Serics shell = "/bin/sh";
15581500Serics }
155953073Sbostic no_intty = ioctl(fileno(stdin), TIOCGETP, &otty);
156053073Sbostic (void)ioctl(fileno(stderr), TIOCGETP, &otty);
156113830Skre savetty = otty;
15621500Serics ospeed = otty.sg_ospeed;
15631500Serics slow_tty = ospeed < B1200;
156427006Sdonn hardtabs = (otty.sg_flags & TBDELAY) != XTABS;
15651500Serics if (!no_tty) {
15661500Serics otty.sg_flags &= ~ECHO;
15671500Serics if (MBIT == CBREAK || !slow_tty)
15681500Serics otty.sg_flags |= MBIT;
15691500Serics }
15701500Serics }
15711500Serics
readch()15721500Serics readch ()
15731500Serics {
15741500Serics char ch;
15751500Serics extern int errno;
15761500Serics
157731089Skarels errno = 0;
15781500Serics if (read (2, &ch, 1) <= 0)
15791500Serics if (errno != EINTR)
158031089Skarels end_it();
15811500Serics else
15821500Serics ch = otty.sg_kill;
15831500Serics return (ch);
15841500Serics }
15851500Serics
15861500Serics static char BS = '\b';
158717592Sleres static char *BSB = "\b \b";
15881500Serics static char CARAT = '^';
158917592Sleres #define ERASEONECHAR \
159017592Sleres if (docrterase) \
159117592Sleres write (2, BSB, sizeof(BSB)); \
159217592Sleres else \
159317592Sleres write (2, &BS, sizeof(BS));
15941500Serics
ttyin(buf,nmax,pchar)15951500Serics ttyin (buf, nmax, pchar)
15961500Serics char buf[];
15971500Serics register int nmax;
15981500Serics char pchar;
15991500Serics {
16001500Serics register char *sptr;
16011500Serics register char ch;
16021500Serics register int slash = 0;
16031500Serics int maxlen;
16041500Serics char cbuf;
16051500Serics
16061500Serics sptr = buf;
16071500Serics maxlen = 0;
16081500Serics while (sptr - buf < nmax) {
16091500Serics if (promptlen > maxlen) maxlen = promptlen;
16101500Serics ch = readch ();
16111500Serics if (ch == '\\') {
16121500Serics slash++;
16131500Serics }
16141500Serics else if ((ch == otty.sg_erase) && !slash) {
16151500Serics if (sptr > buf) {
16161500Serics --promptlen;
161717592Sleres ERASEONECHAR
16181500Serics --sptr;
16191500Serics if ((*sptr < ' ' && *sptr != '\n') || *sptr == RUBOUT) {
16201500Serics --promptlen;
162117592Sleres ERASEONECHAR
16221500Serics }
16231500Serics continue;
16241500Serics }
16251500Serics else {
16261500Serics if (!eraseln) promptlen = maxlen;
16271500Serics longjmp (restore, 1);
16281500Serics }
16291500Serics }
16301500Serics else if ((ch == otty.sg_kill) && !slash) {
16311500Serics if (hard) {
16321500Serics show (ch);
16331500Serics putchar ('\n');
16341500Serics putchar (pchar);
16351500Serics }
16361500Serics else {
16371500Serics putchar ('\r');
16381500Serics putchar (pchar);
16391500Serics if (eraseln)
16401500Serics erase (1);
164117592Sleres else if (docrtkill)
164217592Sleres while (promptlen-- > 1)
164317592Sleres write (2, BSB, sizeof(BSB));
16441500Serics promptlen = 1;
16451500Serics }
16461500Serics sptr = buf;
16471500Serics fflush (stdout);
16481500Serics continue;
16491500Serics }
16501500Serics if (slash && (ch == otty.sg_kill || ch == otty.sg_erase)) {
165117592Sleres ERASEONECHAR
16521500Serics --sptr;
16531500Serics }
16541500Serics if (ch != '\\')
16551500Serics slash = 0;
16561500Serics *sptr++ = ch;
16571500Serics if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) {
16581500Serics ch += ch == RUBOUT ? -0100 : 0100;
16591500Serics write (2, &CARAT, 1);
16601500Serics promptlen++;
16611500Serics }
16621500Serics cbuf = ch;
16631500Serics if (ch != '\n' && ch != ESC) {
16641500Serics write (2, &cbuf, 1);
16651500Serics promptlen++;
16661500Serics }
16671500Serics else
16681500Serics break;
16691500Serics }
16701500Serics *--sptr = '\0';
16711500Serics if (!eraseln) promptlen = maxlen;
16721500Serics if (sptr - buf >= nmax - 1)
16731500Serics error ("Line too long");
16741500Serics }
16751500Serics
expand(outbuf,inbuf)16761500Serics expand (outbuf, inbuf)
16771500Serics char *outbuf;
16781500Serics char *inbuf;
16791500Serics {
16801500Serics register char *instr;
16811500Serics register char *outstr;
16821500Serics register char ch;
16831500Serics char temp[200];
16841500Serics int changed = 0;
16851500Serics
16861500Serics instr = inbuf;
16871500Serics outstr = temp;
16881500Serics while ((ch = *instr++) != '\0')
16891500Serics switch (ch) {
16901500Serics case '%':
16911500Serics if (!no_intty) {
16921500Serics strcpy (outstr, fnames[fnum]);
16931500Serics outstr += strlen (fnames[fnum]);
16941500Serics changed++;
16951500Serics }
16961500Serics else
16971500Serics *outstr++ = ch;
16981500Serics break;
16991500Serics case '!':
17001500Serics if (!shellp)
17011500Serics error ("No previous command to substitute for");
17021500Serics strcpy (outstr, shell_line);
17031500Serics outstr += strlen (shell_line);
17041500Serics changed++;
17051500Serics break;
17061500Serics case '\\':
17071500Serics if (*instr == '%' || *instr == '!') {
17081500Serics *outstr++ = *instr++;
17091500Serics break;
17101500Serics }
17111500Serics default:
17121500Serics *outstr++ = ch;
17131500Serics }
17141500Serics *outstr++ = '\0';
17151500Serics strcpy (outbuf, temp);
17161500Serics return (changed);
17171500Serics }
17181500Serics
show(ch)17191500Serics show (ch)
17201500Serics register char ch;
17211500Serics {
17221500Serics char cbuf;
17231500Serics
17241500Serics if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) {
17251500Serics ch += ch == RUBOUT ? -0100 : 0100;
17261500Serics write (2, &CARAT, 1);
17271500Serics promptlen++;
17281500Serics }
17291500Serics cbuf = ch;
17301500Serics write (2, &cbuf, 1);
17311500Serics promptlen++;
17321500Serics }
17331500Serics
error(mess)17341500Serics error (mess)
17351500Serics char *mess;
17361500Serics {
17373594Sroot if (clreol)
17383594Sroot cleareol ();
17393594Sroot else
17403594Sroot kill_line ();
17411500Serics promptlen += strlen (mess);
17421500Serics if (Senter && Sexit) {
17431500Serics tputs (Senter, 1, putch);
17441500Serics pr(mess);
17451500Serics tputs (Sexit, 1, putch);
17461500Serics }
17471500Serics else
17481500Serics pr (mess);
17491500Serics fflush(stdout);
17501500Serics errors++;
17511500Serics longjmp (restore, 1);
17521500Serics }
17531500Serics
17541500Serics
set_tty()17551500Serics set_tty ()
17561500Serics {
17571500Serics otty.sg_flags |= MBIT;
17581500Serics otty.sg_flags &= ~ECHO;
175916582Sleres stty(fileno(stderr), &otty);
17601500Serics }
17611500Serics
reset_tty()17621500Serics reset_tty ()
17631500Serics {
176431033Sbostic if (no_tty)
176531033Sbostic return;
176616710Sjak if (pstate) {
176716710Sjak tputs(ULexit, 1, putch);
176816710Sjak fflush(stdout);
176916710Sjak pstate = 0;
177016710Sjak }
17711500Serics otty.sg_flags |= ECHO;
17721500Serics otty.sg_flags &= ~MBIT;
177316582Sleres stty(fileno(stderr), &savetty);
17741500Serics }
17751500Serics
rdline(f)17761500Serics rdline (f)
17771500Serics register FILE *f;
17781500Serics {
17791500Serics register char c;
17801500Serics register char *p;
17811500Serics
17821500Serics p = Line;
17831500Serics while ((c = Getc (f)) != '\n' && c != EOF && p - Line < LINSIZ - 1)
17841500Serics *p++ = c;
17851500Serics if (c == '\n')
17861500Serics Currline++;
17871500Serics *p = '\0';
17881500Serics }
17891500Serics
17901500Serics /* Come here when we get a suspend signal from the terminal */
17911500Serics
179246779Sbostic void
onsusp()17931500Serics onsusp ()
17941500Serics {
179514861Skarels /* ignore SIGTTOU so we don't get stopped if csh grabs the tty */
179614861Skarels signal(SIGTTOU, SIG_IGN);
17971500Serics reset_tty ();
17981500Serics fflush (stdout);
179914861Skarels signal(SIGTTOU, SIG_DFL);
18001500Serics /* Send the TSTP signal to suspend our process group */
180113289Ssam signal(SIGTSTP, SIG_DFL);
180213289Ssam sigsetmask(0);
18031500Serics kill (0, SIGTSTP);
18041500Serics /* Pause for station break */
18051500Serics
18061500Serics /* We're back */
18071500Serics signal (SIGTSTP, onsusp);
18081500Serics set_tty ();
18091500Serics if (inwait)
181046779Sbostic longjmp (restore, 1);
18111500Serics }
1812