121981Sdist /* 221981Sdist * Copyright (c) 1980 Regents of the University of California. 332736Sbostic * All rights reserved. 432736Sbostic * 532736Sbostic * Redistribution and use in source and binary forms are permitted 634880Sbostic * provided that the above copyright notice and this paragraph are 734880Sbostic * duplicated in all such forms and that any documentation, 834880Sbostic * advertising materials, and other materials related to such 934880Sbostic * distribution and use acknowledge that the software was developed 1034880Sbostic * by the University of California, Berkeley. The name of the 1134880Sbostic * University may not be used to endorse or promote products derived 1234880Sbostic * from this software without specific prior written permission. 1334880Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1434880Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1534880Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1621981Sdist */ 1721981Sdist 1813615Ssam #ifndef lint 1921981Sdist char copyright[] = 2021981Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 2121981Sdist All rights reserved.\n"; 2232736Sbostic #endif /* not lint */ 233594Sroot 2421981Sdist #ifndef lint 25*37943Sbostic static char sccsid[] = "@(#)more.c 5.21 (Berkeley) 05/11/89"; 2632736Sbostic #endif /* not lint */ 2721981Sdist 281500Serics /* 291500Serics ** more.c - General purpose tty output filter and file perusal program 301500Serics ** 311500Serics ** by Eric Shienbrood, UC Berkeley 323594Sroot ** 333594Sroot ** modified by Geoff Peck, UCB to add underlining, single spacing 343594Sroot ** modified by John Foderaro, UCB to add -c and MORE environment variable 351500Serics */ 361500Serics 371500Serics #include <stdio.h> 3832736Sbostic #include <sys/param.h> 391500Serics #include <ctype.h> 401500Serics #include <signal.h> 411500Serics #include <errno.h> 421500Serics #include <sgtty.h> 431500Serics #include <setjmp.h> 441500Serics #include <sys/stat.h> 4529906Smckusick #include <sys/file.h> 4632736Sbostic #include <a.out.h> 4732736Sbostic #include <varargs.h> 48*37943Sbostic #include "pathnames.h" 491500Serics 501500Serics #define Fopen(s,m) (Currline = 0,file_pos=0,fopen(s,m)) 511500Serics #define Ftell(f) file_pos 521500Serics #define Fseek(f,off) (file_pos=off,fseek(f,off,0)) 531500Serics #define Getc(f) (++file_pos, getc(f)) 541500Serics #define Ungetc(c,f) (--file_pos, ungetc(c,f)) 551500Serics 561500Serics #define MBIT CBREAK 571500Serics #define stty(fd,argp) ioctl(fd,TIOCSETN,argp) 581500Serics 591500Serics #define TBUFSIZ 1024 601500Serics #define LINSIZ 256 6133232Sbostic #define ctrl(letter) (letter & 077) 621500Serics #define RUBOUT '\177' 631500Serics #define ESC '\033' 641500Serics #define QUIT '\034' 651500Serics 6616582Sleres struct sgttyb otty, savetty; 671500Serics long file_pos, file_size; 681500Serics int fnum, no_intty, no_tty, slow_tty; 6927006Sdonn int dum_opt, dlines, onquit(), end_it(), chgwinsz(); 701500Serics int onsusp(); 711500Serics int nscroll = 11; /* Number of lines scrolled by 'd' */ 721500Serics int fold_opt = 1; /* Fold long lines */ 731500Serics int stop_opt = 1; /* Stop after form feeds */ 743594Sroot int ssp_opt = 0; /* Suppress white space */ 753594Sroot int ul_opt = 1; /* Underline as best we can */ 761500Serics int promptlen; 771500Serics int Currline; /* Line we are currently at */ 781500Serics int startup = 1; 791500Serics int firstf = 1; 801500Serics int notell = 1; 8117592Sleres int docrterase = 0; 8217592Sleres int docrtkill = 0; 831500Serics int bad_so; /* True if overwriting does not turn off standout */ 841500Serics int inwait, Pause, errors; 851500Serics int within; /* true if we are within a file, 861500Serics false if we are between files */ 8729907Smckusick int hard, dumb, noscroll, hardtabs, clreol, eatnl; 881500Serics int catch_susp; /* We should catch the SIGTSTP signal */ 891500Serics char **fnames; /* The list of file names */ 901500Serics int nfiles; /* Number of files left to process */ 911500Serics char *shell; /* The name of the shell to use */ 921500Serics int shellp; /* A previous shell command exists */ 931500Serics char ch; 941500Serics jmp_buf restore; 951500Serics char Line[LINSIZ]; /* Line buffer */ 961500Serics int Lpp = 24; /* lines per page */ 971500Serics char *Clear; /* clear screen */ 981500Serics char *eraseln; /* erase line */ 991500Serics char *Senter, *Sexit;/* enter and exit standout mode */ 1003594Sroot char *ULenter, *ULexit; /* enter and exit underline mode */ 1013594Sroot char *chUL; /* underline character */ 1023594Sroot char *chBS; /* backspace character */ 1033455Sroot char *Home; /* go to home */ 1043455Sroot char *cursorm; /* cursor movement */ 1053455Sroot char cursorhome[40]; /* contains cursor movement to home */ 1063594Sroot char *EodClr; /* clear rest of screen */ 1071500Serics char *tgetstr(); 1081500Serics int Mcol = 80; /* number of columns */ 1091500Serics int Wrap = 1; /* set if automargins */ 11016710Sjak int soglitch; /* terminal has standout mode glitch */ 11116710Sjak int ulglitch; /* terminal has underline mode glitch */ 11216710Sjak int pstate = 0; /* current UL state */ 1131500Serics long fseek(); 1143455Sroot char *getenv(); 1151500Serics struct { 1161500Serics long chrctr, line; 1171500Serics } context, screen_start; 1181500Serics extern char PC; /* pad character */ 1191500Serics extern short ospeed; 1201500Serics 1211500Serics 1221500Serics main(argc, argv) 1231500Serics int argc; 1241500Serics char *argv[]; 1251500Serics { 1261500Serics register FILE *f; 1271500Serics register char *s; 1281500Serics register char *p; 1291500Serics register char ch; 1301500Serics register int left; 13116582Sleres int prnames = 0; 1321500Serics int initopt = 0; 1331500Serics int srchopt = 0; 1341500Serics int clearit = 0; 1351500Serics int initline; 1361500Serics char initbuf[80]; 1371500Serics FILE *checkf(); 1381500Serics 1391500Serics nfiles = argc; 1401500Serics fnames = argv; 1411500Serics initterm (); 14215813Sralph nscroll = Lpp/2 - 1; 14315813Sralph if (nscroll <= 0) 14415813Sralph nscroll = 1; 1453455Sroot if(s = getenv("MORE")) argscan(s); 1461500Serics while (--nfiles > 0) { 1471500Serics if ((ch = (*++fnames)[0]) == '-') { 1483455Sroot argscan(*fnames+1); 1491500Serics } 1501500Serics else if (ch == '+') { 1511500Serics s = *fnames; 1521500Serics if (*++s == '/') { 1531500Serics srchopt++; 1541500Serics for (++s, p = initbuf; p < initbuf + 79 && *s != '\0';) 1551500Serics *p++ = *s++; 1561500Serics *p = '\0'; 1571500Serics } 1581500Serics else { 1591500Serics initopt++; 1601500Serics for (initline = 0; *s != '\0'; s++) 1611500Serics if (isdigit (*s)) 1621500Serics initline = initline*10 + *s -'0'; 1631500Serics --initline; 1641500Serics } 1651500Serics } 1661500Serics else break; 1671500Serics } 1683594Sroot /* allow clreol only if Home and eraseln and EodClr strings are 1693455Sroot * defined, and in that case, make sure we are in noscroll mode 1703455Sroot */ 1713455Sroot if(clreol) 1723455Sroot { 17318608Sralph if((Home == NULL) || (*Home == '\0') || 17418608Sralph (eraseln == NULL) || (*eraseln == '\0') || 17518608Sralph (EodClr == NULL) || (*EodClr == '\0') ) 17618608Sralph clreol = 0; 1773455Sroot else noscroll = 1; 1783455Sroot } 1791500Serics if (dlines == 0) 1801500Serics dlines = Lpp - (noscroll ? 1 : 2); 1811500Serics left = dlines; 1821500Serics if (nfiles > 1) 1831500Serics prnames++; 1841500Serics if (!no_intty && nfiles == 0) { 18534104Sbostic char *rindex(); 18634104Sbostic 18734104Sbostic p = rindex(argv[0], '/'); 18834104Sbostic fputs("usage: ",stderr); 18934104Sbostic fputs(p ? p + 1 : argv[0],stderr); 1901500Serics fputs(" [-dfln] [+linenum | +/pattern] name1 name2 ...\n",stderr); 1911500Serics exit(1); 1921500Serics } 1931500Serics else 1941500Serics f = stdin; 1951500Serics if (!no_tty) { 1961500Serics signal(SIGQUIT, onquit); 1971500Serics signal(SIGINT, end_it); 19827006Sdonn signal(SIGWINCH, chgwinsz); 1991500Serics if (signal (SIGTSTP, SIG_IGN) == SIG_DFL) { 2001500Serics signal(SIGTSTP, onsusp); 2011500Serics catch_susp++; 2021500Serics } 20316582Sleres stty (fileno(stderr), &otty); 2041500Serics } 2051500Serics if (no_intty) { 2061500Serics if (no_tty) 2071500Serics copy_file (stdin); 2081500Serics else { 2091500Serics if ((ch = Getc (f)) == '\f') 2103594Sroot doclear(); 2111500Serics else { 2121500Serics Ungetc (ch, f); 2133594Sroot if (noscroll && (ch != EOF)) { 2143594Sroot if (clreol) 2153594Sroot home (); 2163594Sroot else 2173594Sroot doclear (); 2183455Sroot } 2191500Serics } 2201500Serics if (srchopt) 2213455Sroot { 2221500Serics search (initbuf, stdin, 1); 2233594Sroot if (noscroll) 2243594Sroot left--; 2253455Sroot } 2261500Serics else if (initopt) 2271500Serics skiplns (initline, stdin); 2281500Serics screen (stdin, left); 2291500Serics } 2301500Serics no_intty = 0; 2311500Serics prnames++; 2321500Serics firstf = 0; 2331500Serics } 2341500Serics 2351500Serics while (fnum < nfiles) { 2361500Serics if ((f = checkf (fnames[fnum], &clearit)) != NULL) { 2371500Serics context.line = context.chrctr = 0; 2381500Serics Currline = 0; 2391500Serics if (firstf) setjmp (restore); 2401500Serics if (firstf) { 2411500Serics firstf = 0; 2421500Serics if (srchopt) 2433455Sroot { 2441500Serics search (initbuf, f, 1); 2453594Sroot if (noscroll) 2463594Sroot left--; 2473455Sroot } 2481500Serics else if (initopt) 2491500Serics skiplns (initline, f); 2501500Serics } 2511500Serics else if (fnum < nfiles && !no_tty) { 2521500Serics setjmp (restore); 2531500Serics left = command (fnames[fnum], f); 2541500Serics } 2551500Serics if (left != 0) { 25632736Sbostic if ((noscroll || clearit) && (file_size != LONG_MAX)) 2573594Sroot if (clreol) 2583594Sroot home (); 2593594Sroot else 2603594Sroot doclear (); 2611500Serics if (prnames) { 2621500Serics if (bad_so) 2631500Serics erase (0); 2643594Sroot if (clreol) 2653594Sroot cleareol (); 2661500Serics pr("::::::::::::::"); 2671500Serics if (promptlen > 14) 2681500Serics erase (14); 2693455Sroot printf ("\n"); 2703455Sroot if(clreol) cleareol(); 2713455Sroot printf("%s\n", fnames[fnum]); 2723455Sroot if(clreol) cleareol(); 27330931Sbostic printf("::::::::::::::\n"); 2741500Serics if (left > Lpp - 4) 2751500Serics left = Lpp - 4; 2761500Serics } 2771500Serics if (no_tty) 2781500Serics copy_file (f); 2791500Serics else { 2801500Serics within++; 2811500Serics screen(f, left); 2821500Serics within = 0; 2831500Serics } 2841500Serics } 2851500Serics setjmp (restore); 2861500Serics fflush(stdout); 2871500Serics fclose(f); 2881500Serics screen_start.line = screen_start.chrctr = 0L; 2891500Serics context.line = context.chrctr = 0L; 2901500Serics } 2911500Serics fnum++; 2921500Serics firstf = 0; 2931500Serics } 2941500Serics reset_tty (); 2951500Serics exit(0); 2961500Serics } 2971500Serics 2983455Sroot argscan(s) 2993455Sroot char *s; 3003455Sroot { 30132273Sbostic int seen_num = 0; 30232273Sbostic 30332273Sbostic while (*s != '\0') { 30432273Sbostic switch (*s) { 30511604Slayer case '0': case '1': case '2': 30611604Slayer case '3': case '4': case '5': 30711604Slayer case '6': case '7': case '8': 30811604Slayer case '9': 30932273Sbostic if (!seen_num) { 31032273Sbostic dlines = 0; 31132273Sbostic seen_num = 1; 31232273Sbostic } 31311604Slayer dlines = dlines*10 + *s - '0'; 31411604Slayer break; 31511604Slayer case 'd': 31611604Slayer dum_opt = 1; 31711604Slayer break; 31811604Slayer case 'l': 31911604Slayer stop_opt = 0; 32011604Slayer break; 32111604Slayer case 'f': 32211604Slayer fold_opt = 0; 32311604Slayer break; 32411604Slayer case 'p': 32511604Slayer noscroll++; 32611604Slayer break; 32711604Slayer case 'c': 32811604Slayer clreol++; 32911604Slayer break; 33011604Slayer case 's': 33111604Slayer ssp_opt = 1; 33211604Slayer break; 33311604Slayer case 'u': 33411604Slayer ul_opt = 0; 33511604Slayer break; 33611604Slayer } 33732273Sbostic s++; 33811604Slayer } 3393455Sroot } 3403455Sroot 3413455Sroot 3421500Serics /* 3431500Serics ** Check whether the file named by fs is an ASCII file which the user may 3441500Serics ** access. If it is, return the opened file. Otherwise return NULL. 3451500Serics */ 3461500Serics 3471500Serics FILE * 3481500Serics checkf (fs, clearfirst) 34932736Sbostic register char *fs; 35032736Sbostic int *clearfirst; 3511500Serics { 35232736Sbostic struct stat stbuf; 35332736Sbostic register FILE *f; 35432736Sbostic char c; 3551500Serics 35632736Sbostic if (stat (fs, &stbuf) == -1) { 35732736Sbostic (void)fflush(stdout); 35832736Sbostic if (clreol) 35932736Sbostic cleareol (); 36032736Sbostic perror(fs); 36132736Sbostic return((FILE *)NULL); 36232736Sbostic } 36332736Sbostic if ((stbuf.st_mode & S_IFMT) == S_IFDIR) { 36432736Sbostic printf("\n*** %s: directory ***\n\n", fs); 36532736Sbostic return((FILE *)NULL); 36632736Sbostic } 36732736Sbostic if ((f = Fopen(fs, "r")) == NULL) { 36832736Sbostic (void)fflush(stdout); 36932736Sbostic perror(fs); 37032736Sbostic return((FILE *)NULL); 37132736Sbostic } 37232736Sbostic if (magic(f, fs)) 37332736Sbostic return((FILE *)NULL); 37432736Sbostic c = Getc(f); 37532736Sbostic *clearfirst = c == '\f'; 3761500Serics Ungetc (c, f); 37732736Sbostic if ((file_size = stbuf.st_size) == 0) 37832736Sbostic file_size = LONG_MAX; 37932736Sbostic return(f); 3801500Serics } 3811500Serics 3821500Serics /* 38332736Sbostic * magic -- 38432736Sbostic * check for file magic numbers. This code would best be shared with 38532736Sbostic * the file(1) program or, perhaps, more should not try and be so smart? 38629906Smckusick */ 38732736Sbostic static 38832736Sbostic magic(f, fs) 38932736Sbostic FILE *f; 39032736Sbostic char *fs; 39129906Smckusick { 39232736Sbostic struct exec ex; 39329906Smckusick 39432736Sbostic if (fread(&ex, sizeof(ex), 1, f) == 1) 39532736Sbostic switch(ex.a_magic) { 39632736Sbostic case OMAGIC: 39732736Sbostic case NMAGIC: 39832736Sbostic case ZMAGIC: 39932736Sbostic case 0405: 40032736Sbostic case 0411: 40132736Sbostic case 0177545: 40232736Sbostic printf("\n******** %s: Not a text file ********\n\n", fs); 40332736Sbostic (void)fclose(f); 40432736Sbostic return(1); 40532736Sbostic } 40632736Sbostic (void)fseek(f, 0L, L_SET); /* rewind() not necessary */ 40733087Sbostic return(0); 40829906Smckusick } 40929906Smckusick 41029906Smckusick /* 4111500Serics ** A real function, for the tputs routine in termlib 4121500Serics */ 4131500Serics 4141500Serics putch (ch) 4151500Serics char ch; 4161500Serics { 4171500Serics putchar (ch); 4181500Serics } 4191500Serics 4201500Serics /* 4211500Serics ** Print out the contents of the file f, one screenful at a time. 4221500Serics */ 4231500Serics 4241500Serics #define STOP -10 4251500Serics 4261500Serics screen (f, num_lines) 4271500Serics register FILE *f; 4281500Serics register int num_lines; 4291500Serics { 4301500Serics register int c; 4311500Serics register int nchars; 4323594Sroot int length; /* length of current line */ 4333594Sroot static int prev_len = 1; /* length of previous line */ 4341500Serics 4351500Serics for (;;) { 4361500Serics while (num_lines > 0 && !Pause) { 4371500Serics if ((nchars = getline (f, &length)) == EOF) 4383455Sroot { 4393594Sroot if (clreol) 4403594Sroot clreos(); 4411500Serics return; 4423455Sroot } 4433594Sroot if (ssp_opt && length == 0 && prev_len == 0) 4443594Sroot continue; 4453594Sroot prev_len = length; 4461500Serics if (bad_so || (Senter && *Senter == ' ') && promptlen > 0) 4471500Serics erase (0); 4483594Sroot /* must clear before drawing line since tabs on some terminals 4493594Sroot * do not erase what they tab over. 4503594Sroot */ 4513594Sroot if (clreol) 4523594Sroot cleareol (); 4531500Serics prbuf (Line, length); 4541500Serics if (nchars < promptlen) 4551500Serics erase (nchars); /* erase () sets promptlen to 0 */ 4561500Serics else promptlen = 0; 4573594Sroot /* is this needed? 4583594Sroot * if (clreol) 4593594Sroot * cleareol(); /* must clear again in case we wrapped * 4603594Sroot */ 4611500Serics if (nchars < Mcol || !fold_opt) 46216710Sjak prbuf("\n", 1); /* will turn off UL if necessary */ 4631500Serics if (nchars == STOP) 4641500Serics break; 4651500Serics num_lines--; 4661500Serics } 46716710Sjak if (pstate) { 46816710Sjak tputs(ULexit, 1, putch); 46916710Sjak pstate = 0; 47016710Sjak } 4711500Serics fflush(stdout); 4721500Serics if ((c = Getc(f)) == EOF) 4733455Sroot { 4743594Sroot if (clreol) 4753594Sroot clreos (); 4761500Serics return; 4773455Sroot } 4783455Sroot 4793594Sroot if (Pause && clreol) 4803594Sroot clreos (); 4811500Serics Ungetc (c, f); 4821500Serics setjmp (restore); 4831500Serics Pause = 0; startup = 0; 4841500Serics if ((num_lines = command (NULL, f)) == 0) 4851500Serics return; 4861500Serics if (hard && promptlen > 0) 4871500Serics erase (0); 48811123Slayer if (noscroll && num_lines >= dlines) 48916582Sleres { 4903594Sroot if (clreol) 4913594Sroot home(); 4923594Sroot else 4933594Sroot doclear (); 4943455Sroot } 4951500Serics screen_start.line = Currline; 4961500Serics screen_start.chrctr = Ftell (f); 4971500Serics } 4981500Serics } 4991500Serics 5001500Serics /* 5011500Serics ** Come here if a quit signal is received 5021500Serics */ 5031500Serics 5041500Serics onquit() 5051500Serics { 5061500Serics signal(SIGQUIT, SIG_IGN); 5071500Serics if (!inwait) { 5081500Serics putchar ('\n'); 5091500Serics if (!startup) { 5101500Serics signal(SIGQUIT, onquit); 5111500Serics longjmp (restore, 1); 5121500Serics } 5131500Serics else 5141500Serics Pause++; 5151500Serics } 5161500Serics else if (!dum_opt && notell) { 5171500Serics write (2, "[Use q or Q to quit]", 20); 5181500Serics promptlen += 20; 5191500Serics notell = 0; 5201500Serics } 5211500Serics signal(SIGQUIT, onquit); 5221500Serics } 5231500Serics 5241500Serics /* 52527006Sdonn ** Come here if a signal for a window size change is received 52627006Sdonn */ 52727006Sdonn 52827006Sdonn chgwinsz() 52927006Sdonn { 53027006Sdonn struct winsize win; 53127006Sdonn 53227006Sdonn (void) signal(SIGWINCH, SIG_IGN); 53327006Sdonn if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) { 53427006Sdonn if (win.ws_row != 0) { 53527006Sdonn Lpp = win.ws_row; 53627006Sdonn nscroll = Lpp/2 - 1; 53727006Sdonn if (nscroll <= 0) 53827006Sdonn nscroll = 1; 53927006Sdonn dlines = Lpp - (noscroll ? 1 : 2); 54027006Sdonn } 54127006Sdonn if (win.ws_col != 0) 54227006Sdonn Mcol = win.ws_col; 54327006Sdonn } 54427006Sdonn (void) signal(SIGWINCH, chgwinsz); 54527006Sdonn } 54627006Sdonn 54727006Sdonn /* 5481500Serics ** Clean up terminal state and exit. Also come here if interrupt signal received 5491500Serics */ 5501500Serics 5511500Serics end_it () 5521500Serics { 5531500Serics 5541500Serics reset_tty (); 5553594Sroot if (clreol) { 5563594Sroot putchar ('\r'); 5573594Sroot clreos (); 5583594Sroot fflush (stdout); 5593594Sroot } 5603455Sroot else if (!clreol && (promptlen > 0)) { 5611500Serics kill_line (); 5621500Serics fflush (stdout); 5631500Serics } 5641500Serics else 5651500Serics write (2, "\n", 1); 5661500Serics _exit(0); 5671500Serics } 5681500Serics 5691500Serics copy_file(f) 5701500Serics register FILE *f; 5711500Serics { 5721500Serics register int c; 5731500Serics 5741500Serics while ((c = getc(f)) != EOF) 5751500Serics putchar(c); 5761500Serics } 5771500Serics 5781500Serics /* Simplified printf function */ 5791500Serics 58032737Sbostic printf (fmt, va_alist) 5811500Serics register char *fmt; 58232737Sbostic va_dcl 5831500Serics { 58432737Sbostic va_list ap; 5851500Serics register char ch; 5861500Serics register int ccount; 5871500Serics 5881500Serics ccount = 0; 58932737Sbostic va_start(ap); 5901500Serics while (*fmt) { 5911500Serics while ((ch = *fmt++) != '%') { 5921500Serics if (ch == '\0') 5931500Serics return (ccount); 5941500Serics ccount++; 5951500Serics putchar (ch); 5961500Serics } 5971500Serics switch (*fmt++) { 5981500Serics case 'd': 59932737Sbostic ccount += printd (va_arg(ap, int)); 6001500Serics break; 6011500Serics case 's': 60232737Sbostic ccount += pr (va_arg(ap, char *)); 6031500Serics break; 6041500Serics case '%': 6051500Serics ccount++; 6061500Serics putchar ('%'); 6071500Serics break; 6081500Serics case '0': 6091500Serics return (ccount); 6101500Serics default: 6111500Serics break; 6121500Serics } 6131500Serics } 61432737Sbostic va_end(ap); 6151500Serics return (ccount); 6161500Serics 6171500Serics } 6181500Serics 6191500Serics /* 6201500Serics ** Print an integer as a string of decimal digits, 6211500Serics ** returning the length of the print representation. 6221500Serics */ 6231500Serics 6241500Serics printd (n) 6251500Serics int n; 6261500Serics { 6271500Serics int a, nchars; 6281500Serics 6291500Serics if (a = n/10) 6301500Serics nchars = 1 + printd(a); 6311500Serics else 6321500Serics nchars = 1; 6331500Serics putchar (n % 10 + '0'); 6341500Serics return (nchars); 6351500Serics } 6361500Serics 6371500Serics /* Put the print representation of an integer into a string */ 6381500Serics static char *sptr; 6391500Serics 6401500Serics scanstr (n, str) 6411500Serics int n; 6421500Serics char *str; 6431500Serics { 6441500Serics sptr = str; 64511604Slayer Sprintf (n); 6461500Serics *sptr = '\0'; 6471500Serics } 6481500Serics 64911604Slayer Sprintf (n) 6501500Serics { 6511500Serics int a; 6521500Serics 6531500Serics if (a = n/10) 65411604Slayer Sprintf (a); 6551500Serics *sptr++ = n % 10 + '0'; 6561500Serics } 6571500Serics 65833232Sbostic static char bell = ctrl('G'); 6591500Serics 6601500Serics strlen (s) 6611500Serics char *s; 6621500Serics { 6631500Serics register char *p; 6641500Serics 6651500Serics p = s; 6661500Serics while (*p++) 6671500Serics ; 6681500Serics return (p - s - 1); 6691500Serics } 6701500Serics 6711500Serics /* See whether the last component of the path name "path" is equal to the 6721500Serics ** string "string" 6731500Serics */ 6741500Serics 6751500Serics tailequ (path, string) 6761500Serics char *path; 6771500Serics register char *string; 6781500Serics { 6791500Serics register char *tail; 6801500Serics 6811500Serics tail = path + strlen(path); 6821500Serics while (tail >= path) 6831500Serics if (*(--tail) == '/') 6841500Serics break; 6851500Serics ++tail; 6861500Serics while (*tail++ == *string++) 6871500Serics if (*tail == '\0') 6881500Serics return(1); 6891500Serics return(0); 6901500Serics } 6911500Serics 6921500Serics prompt (filename) 6931500Serics char *filename; 6941500Serics { 6953594Sroot if (clreol) 6963594Sroot cleareol (); 6973455Sroot else if (promptlen > 0) 6981500Serics kill_line (); 6991500Serics if (!hard) { 7001500Serics promptlen = 8; 70116710Sjak if (Senter && Sexit) { 7021500Serics tputs (Senter, 1, putch); 70316710Sjak promptlen += (2 * soglitch); 70416710Sjak } 7053594Sroot if (clreol) 7063594Sroot cleareol (); 7071500Serics pr("--More--"); 7081500Serics if (filename != NULL) { 7091500Serics promptlen += printf ("(Next file: %s)", filename); 7101500Serics } 7111500Serics else if (!no_intty) { 7121500Serics promptlen += printf ("(%d%%)", (int)((file_pos * 100) / file_size)); 7131500Serics } 7141500Serics if (dum_opt) { 71516710Sjak promptlen += pr("[Press space to continue, 'q' to quit.]"); 7161500Serics } 7171500Serics if (Senter && Sexit) 7181500Serics tputs (Sexit, 1, putch); 7193594Sroot if (clreol) 7203594Sroot clreos (); 7211500Serics fflush(stdout); 7221500Serics } 7231500Serics else 7241500Serics write (2, &bell, 1); 7251500Serics inwait++; 7261500Serics } 7271500Serics 7281500Serics /* 7291500Serics ** Get a logical line 7301500Serics */ 7311500Serics 7321500Serics getline(f, length) 7331500Serics register FILE *f; 7341500Serics int *length; 7351500Serics { 7361500Serics register int c; 7371500Serics register char *p; 7381500Serics register int column; 7391500Serics static int colflg; 7401500Serics 7411500Serics p = Line; 7421500Serics column = 0; 7431500Serics c = Getc (f); 7441500Serics if (colflg && c == '\n') { 7451500Serics Currline++; 7461500Serics c = Getc (f); 7471500Serics } 7481500Serics while (p < &Line[LINSIZ - 1]) { 7491500Serics if (c == EOF) { 7501500Serics if (p > Line) { 7511500Serics *p = '\0'; 7521500Serics *length = p - Line; 7531500Serics return (column); 7541500Serics } 7551500Serics *length = p - Line; 7561500Serics return (EOF); 7571500Serics } 7581500Serics if (c == '\n') { 7591500Serics Currline++; 7601500Serics break; 7611500Serics } 7621500Serics *p++ = c; 7631500Serics if (c == '\t') 76429907Smckusick if (!hardtabs || column < promptlen && !hard) { 76529907Smckusick if (hardtabs && eraseln && !dumb) { 7661500Serics column = 1 + (column | 7); 7671500Serics tputs (eraseln, 1, putch); 7681500Serics promptlen = 0; 7691500Serics } 7701500Serics else { 77129907Smckusick for (--p; p < &Line[LINSIZ - 1];) { 7721500Serics *p++ = ' '; 77329907Smckusick if ((++column & 7) == 0) 77429907Smckusick break; 7751500Serics } 7761500Serics if (column >= promptlen) promptlen = 0; 7771500Serics } 7781500Serics } 7791500Serics else 7801500Serics column = 1 + (column | 7); 7819627Ssklower else if (c == '\b' && column > 0) 7821500Serics column--; 7831500Serics else if (c == '\r') 7841500Serics column = 0; 7851500Serics else if (c == '\f' && stop_opt) { 7861500Serics p[-1] = '^'; 7871500Serics *p++ = 'L'; 7881500Serics column += 2; 7891500Serics Pause++; 7901500Serics } 7911500Serics else if (c == EOF) { 7921500Serics *length = p - Line; 7931500Serics return (column); 7941500Serics } 7951500Serics else if (c >= ' ' && c != RUBOUT) 7961500Serics column++; 7971500Serics if (column >= Mcol && fold_opt) break; 7981500Serics c = Getc (f); 7991500Serics } 8001500Serics if (column >= Mcol && Mcol > 0) { 8011500Serics if (!Wrap) { 8021500Serics *p++ = '\n'; 8031500Serics } 8041500Serics } 8051500Serics colflg = column == Mcol && fold_opt; 80629907Smckusick if (colflg && eatnl && Wrap) { 80729907Smckusick *p++ = '\n'; /* simulate normal wrap */ 80829907Smckusick } 8091500Serics *length = p - Line; 8101500Serics *p = 0; 8111500Serics return (column); 8121500Serics } 8131500Serics 8141500Serics /* 8151500Serics ** Erase the rest of the prompt, assuming we are starting at column col. 8161500Serics */ 8171500Serics 8181500Serics erase (col) 8191500Serics register int col; 8201500Serics { 8211500Serics 8221500Serics if (promptlen == 0) 8231500Serics return; 8241500Serics if (hard) { 8251500Serics putchar ('\n'); 8261500Serics } 8271500Serics else { 8281500Serics if (col == 0) 8291500Serics putchar ('\r'); 8301500Serics if (!dumb && eraseln) 8311500Serics tputs (eraseln, 1, putch); 8321500Serics else 8331500Serics for (col = promptlen - col; col > 0; col--) 8341500Serics putchar (' '); 8351500Serics } 8361500Serics promptlen = 0; 8371500Serics } 8381500Serics 8391500Serics /* 8401500Serics ** Erase the current line entirely 8411500Serics */ 8421500Serics 8431500Serics kill_line () 8441500Serics { 8451500Serics erase (0); 8461500Serics if (!eraseln || dumb) putchar ('\r'); 8471500Serics } 8481500Serics 8491500Serics /* 8503455Sroot * force clear to end of line 8513455Sroot */ 8523455Sroot cleareol() 8533455Sroot { 8543594Sroot tputs(eraseln, 1, putch); 8553455Sroot } 8563455Sroot 8573594Sroot clreos() 8583455Sroot { 8593594Sroot tputs(EodClr, 1, putch); 8603455Sroot } 8613455Sroot 8623455Sroot /* 8631500Serics ** Print string and return number of characters 8641500Serics */ 8651500Serics 8661500Serics pr(s1) 8671500Serics char *s1; 8681500Serics { 8691500Serics register char *s; 8701500Serics register char c; 8711500Serics 8721500Serics for (s = s1; c = *s++; ) 8731500Serics putchar(c); 8741500Serics return (s - s1 - 1); 8751500Serics } 8761500Serics 8771500Serics 8781500Serics /* Print a buffer of n characters */ 8791500Serics 8801500Serics prbuf (s, n) 8811500Serics register char *s; 8821500Serics register int n; 8831500Serics { 88416710Sjak register char c; /* next output character */ 8853594Sroot register int state; /* next output char's UL state */ 88616710Sjak #define wouldul(s,n) ((n) >= 2 && (((s)[0] == '_' && (s)[1] == '\b') || ((s)[1] == '\b' && (s)[2] == '_'))) 8873594Sroot 8883594Sroot while (--n >= 0) 8893594Sroot if (!ul_opt) 8903594Sroot putchar (*s++); 8913594Sroot else { 89216710Sjak if (*s == ' ' && pstate == 0 && ulglitch && wouldul(s+1, n-1)) { 89316710Sjak s++; 89416710Sjak continue; 89516710Sjak } 89616710Sjak if (state = wouldul(s, n)) { 89716710Sjak c = (*s == '_')? s[2] : *s ; 8983594Sroot n -= 2; 89916710Sjak s += 3; 90016710Sjak } else 9013594Sroot c = *s++; 90216710Sjak if (state != pstate) { 90316710Sjak if (c == ' ' && state == 0 && ulglitch && wouldul(s, n-1)) 90416710Sjak state = 1; 90516710Sjak else 90616710Sjak tputs(state ? ULenter : ULexit, 1, putch); 9073594Sroot } 90816710Sjak if (c != ' ' || pstate == 0 || state != 0 || ulglitch == 0) 90916710Sjak putchar(c); 9103594Sroot if (state && *chUL) { 9113594Sroot pr(chBS); 9123594Sroot tputs(chUL, 1, putch); 9133594Sroot } 91416710Sjak pstate = state; 9153594Sroot } 9161500Serics } 9171500Serics 9181500Serics /* 9191500Serics ** Clear the screen 9201500Serics */ 9211500Serics 9221500Serics doclear() 9231500Serics { 9241500Serics if (Clear && !hard) { 9251500Serics tputs(Clear, 1, putch); 9261500Serics 9271500Serics /* Put out carriage return so that system doesn't 9281500Serics ** get confused by escape sequences when expanding tabs 9291500Serics */ 9301500Serics putchar ('\r'); 9311500Serics promptlen = 0; 9321500Serics } 9331500Serics } 9341500Serics 9353455Sroot /* 9363455Sroot * Go to home position 9373455Sroot */ 9383455Sroot home() 9393455Sroot { 9403455Sroot tputs(Home,1,putch); 9413455Sroot } 9423455Sroot 9431500Serics static int lastcmd, lastarg, lastp; 9441500Serics static int lastcolon; 9451500Serics char shell_line[132]; 9461500Serics 9471500Serics /* 9481500Serics ** Read a command and do it. A command consists of an optional integer 9491500Serics ** argument followed by the command character. Return the number of lines 9501500Serics ** to display in the next screenful. If there is nothing more to display 9511500Serics ** in the current file, zero is returned. 9521500Serics */ 9531500Serics 9541500Serics command (filename, f) 9551500Serics char *filename; 9561500Serics register FILE *f; 9571500Serics { 9581500Serics register int nlines; 9591500Serics register int retval; 9601500Serics register char c; 9611500Serics char colonch; 9621500Serics FILE *helpf; 9631500Serics int done; 9641500Serics char comchar, cmdbuf[80], *p; 9651500Serics 9661500Serics #define ret(val) retval=val;done++;break 9671500Serics 9681500Serics done = 0; 9691500Serics if (!errors) 9701500Serics prompt (filename); 9711500Serics else 9721500Serics errors = 0; 9731500Serics if (MBIT == RAW && slow_tty) { 9741500Serics otty.sg_flags |= MBIT; 97516582Sleres stty(fileno(stderr), &otty); 9761500Serics } 9771500Serics for (;;) { 9781500Serics nlines = number (&comchar); 9791500Serics lastp = colonch = 0; 9801500Serics if (comchar == '.') { /* Repeat last command */ 9811500Serics lastp++; 9821500Serics comchar = lastcmd; 9831500Serics nlines = lastarg; 9841500Serics if (lastcmd == ':') 9851500Serics colonch = lastcolon; 9861500Serics } 9871500Serics lastcmd = comchar; 9881500Serics lastarg = nlines; 9891500Serics if (comchar == otty.sg_erase) { 9901500Serics kill_line (); 9911500Serics prompt (filename); 9921500Serics continue; 9931500Serics } 9941500Serics switch (comchar) { 9951500Serics case ':': 9961500Serics retval = colon (filename, colonch, nlines); 9971500Serics if (retval >= 0) 9981500Serics done++; 9991500Serics break; 100024490Sbloom case 'b': 100133232Sbostic case ctrl('B'): 100224490Sbloom { 100324490Sbloom register int initline; 100424490Sbloom 100524490Sbloom if (no_intty) { 100624490Sbloom write(2, &bell, 1); 100724490Sbloom return (-1); 100824490Sbloom } 100924490Sbloom 101024490Sbloom if (nlines == 0) nlines++; 101124490Sbloom 101224490Sbloom putchar ('\r'); 101324490Sbloom erase (0); 101424490Sbloom printf ("\n"); 101524490Sbloom if (clreol) 101624490Sbloom cleareol (); 101724490Sbloom printf ("...back %d page", nlines); 101824490Sbloom if (nlines > 1) 101924490Sbloom pr ("s\n"); 102024490Sbloom else 102124490Sbloom pr ("\n"); 102224490Sbloom 102324490Sbloom if (clreol) 102424490Sbloom cleareol (); 102524490Sbloom pr ("\n"); 102624490Sbloom 102724490Sbloom initline = Currline - dlines * (nlines + 1); 102824490Sbloom if (! noscroll) 102924490Sbloom --initline; 103024490Sbloom if (initline < 0) initline = 0; 103124490Sbloom Fseek(f, 0L); 103224490Sbloom Currline = 0; /* skiplns() will make Currline correct */ 103324490Sbloom skiplns(initline, f); 103424490Sbloom if (! noscroll) { 103524490Sbloom ret(dlines + 1); 103624490Sbloom } 103724490Sbloom else { 103824490Sbloom ret(dlines); 103924490Sbloom } 104024490Sbloom } 10411500Serics case ' ': 10421500Serics case 'z': 10431500Serics if (nlines == 0) nlines = dlines; 10441500Serics else if (comchar == 'z') dlines = nlines; 10451500Serics ret (nlines); 10461500Serics case 'd': 104733232Sbostic case ctrl('D'): 10481500Serics if (nlines != 0) nscroll = nlines; 10491500Serics ret (nscroll); 10501500Serics case 'q': 10511500Serics case 'Q': 10521500Serics end_it (); 10531500Serics case 's': 10541500Serics case 'f': 10551500Serics if (nlines == 0) nlines++; 10561500Serics if (comchar == 'f') 10571500Serics nlines *= dlines; 10581500Serics putchar ('\r'); 10591500Serics erase (0); 10603594Sroot printf ("\n"); 10613594Sroot if (clreol) 10623594Sroot cleareol (); 10633594Sroot printf ("...skipping %d line", nlines); 10641500Serics if (nlines > 1) 10653594Sroot pr ("s\n"); 10661500Serics else 10673594Sroot pr ("\n"); 10683594Sroot 10693594Sroot if (clreol) 10703594Sroot cleareol (); 10713594Sroot pr ("\n"); 10723594Sroot 10731500Serics while (nlines > 0) { 10741500Serics while ((c = Getc (f)) != '\n') 10751500Serics if (c == EOF) { 10761500Serics retval = 0; 10771500Serics done++; 10781500Serics goto endsw; 10791500Serics } 10801500Serics Currline++; 10811500Serics nlines--; 10821500Serics } 10831500Serics ret (dlines); 10841500Serics case '\n': 10851500Serics if (nlines != 0) 10861500Serics dlines = nlines; 10871500Serics else 10881500Serics nlines = 1; 10891500Serics ret (nlines); 10901500Serics case '\f': 10911500Serics if (!no_intty) { 10921500Serics doclear (); 10931500Serics Fseek (f, screen_start.chrctr); 10941500Serics Currline = screen_start.line; 10951500Serics ret (dlines); 10961500Serics } 10971500Serics else { 10981500Serics write (2, &bell, 1); 10991500Serics break; 11001500Serics } 11011500Serics case '\'': 11021500Serics if (!no_intty) { 11031500Serics kill_line (); 11041500Serics pr ("\n***Back***\n\n"); 11051500Serics Fseek (f, context.chrctr); 11061500Serics Currline = context.line; 11071500Serics ret (dlines); 11081500Serics } 11091500Serics else { 11101500Serics write (2, &bell, 1); 11111500Serics break; 11121500Serics } 11131500Serics case '=': 11141500Serics kill_line (); 11151500Serics promptlen = printd (Currline); 11161500Serics fflush (stdout); 11171500Serics break; 11181500Serics case 'n': 11191500Serics lastp++; 11201500Serics case '/': 11211500Serics if (nlines == 0) nlines++; 11221500Serics kill_line (); 11231500Serics pr ("/"); 11241500Serics promptlen = 1; 11251500Serics fflush (stdout); 11261500Serics if (lastp) { 11271500Serics write (2,"\r", 1); 11281500Serics search (NULL, f, nlines); /* Use previous r.e. */ 11291500Serics } 11301500Serics else { 11311500Serics ttyin (cmdbuf, 78, '/'); 11321500Serics write (2, "\r", 1); 11331500Serics search (cmdbuf, f, nlines); 11341500Serics } 11353455Sroot ret (dlines-1); 11361500Serics case '!': 11371500Serics do_shell (filename); 11381500Serics break; 113924490Sbloom case '?': 11401500Serics case 'h': 11411500Serics if ((helpf = fopen (HELPFILE, "r")) == NULL) 11421500Serics error ("Can't open help file"); 11431500Serics if (noscroll) doclear (); 11441500Serics copy_file (helpf); 114527006Sdonn fclose (helpf); 11461500Serics prompt (filename); 11471500Serics break; 11481500Serics case 'v': /* This case should go right before default */ 11491500Serics if (!no_intty) { 11501500Serics kill_line (); 11511500Serics cmdbuf[0] = '+'; 115224490Sbloom scanstr (Currline - dlines < 0 ? 0 115324490Sbloom : Currline - (dlines + 1) / 2, &cmdbuf[1]); 11541500Serics pr ("vi "); pr (cmdbuf); putchar (' '); pr (fnames[fnum]); 11551500Serics execute (filename, VI, "vi", cmdbuf, fnames[fnum], 0); 11561500Serics break; 11571500Serics } 11581500Serics default: 115916710Sjak if (dum_opt) { 116016710Sjak kill_line (); 116116710Sjak if (Senter && Sexit) { 116216710Sjak tputs (Senter, 1, putch); 116316710Sjak promptlen = pr ("[Press 'h' for instructions.]") + (2 * soglitch); 116416710Sjak tputs (Sexit, 1, putch); 116516710Sjak } 116616710Sjak else 116716710Sjak promptlen = pr ("[Press 'h' for instructions.]"); 116816710Sjak fflush (stdout); 116916710Sjak } 117016710Sjak else 117116710Sjak write (2, &bell, 1); 11721500Serics break; 11731500Serics } 11741500Serics if (done) break; 11751500Serics } 11761500Serics putchar ('\r'); 11771500Serics endsw: 11781500Serics inwait = 0; 11791500Serics notell++; 11801500Serics if (MBIT == RAW && slow_tty) { 11811500Serics otty.sg_flags &= ~MBIT; 118216582Sleres stty(fileno(stderr), &otty); 11831500Serics } 11841500Serics return (retval); 11851500Serics } 11861500Serics 11871500Serics char ch; 11881500Serics 11891500Serics /* 11901500Serics * Execute a colon-prefixed command. 11911500Serics * Returns <0 if not a command that should cause 11921500Serics * more of the file to be printed. 11931500Serics */ 11941500Serics 11951500Serics colon (filename, cmd, nlines) 11961500Serics char *filename; 11971500Serics int cmd; 11981500Serics int nlines; 11991500Serics { 12001500Serics if (cmd == 0) 12011500Serics ch = readch (); 12021500Serics else 12031500Serics ch = cmd; 12041500Serics lastcolon = ch; 12051500Serics switch (ch) { 12061500Serics case 'f': 12071500Serics kill_line (); 12081500Serics if (!no_intty) 12091500Serics promptlen = printf ("\"%s\" line %d", fnames[fnum], Currline); 12101500Serics else 12111500Serics promptlen = printf ("[Not a file] line %d", Currline); 12121500Serics fflush (stdout); 12131500Serics return (-1); 12141500Serics case 'n': 12151500Serics if (nlines == 0) { 12161500Serics if (fnum >= nfiles - 1) 12171500Serics end_it (); 12181500Serics nlines++; 12191500Serics } 12201500Serics putchar ('\r'); 12211500Serics erase (0); 12221500Serics skipf (nlines); 12231500Serics return (0); 12241500Serics case 'p': 12251500Serics if (no_intty) { 12261500Serics write (2, &bell, 1); 12271500Serics return (-1); 12281500Serics } 12291500Serics putchar ('\r'); 12301500Serics erase (0); 12311500Serics if (nlines == 0) 12321500Serics nlines++; 12331500Serics skipf (-nlines); 12341500Serics return (0); 12351500Serics case '!': 12361500Serics do_shell (filename); 12371500Serics return (-1); 12381500Serics case 'q': 12391500Serics case 'Q': 12401500Serics end_it (); 12411500Serics default: 12421500Serics write (2, &bell, 1); 12431500Serics return (-1); 12441500Serics } 12451500Serics } 12461500Serics 12471500Serics /* 12481500Serics ** Read a decimal number from the terminal. Set cmd to the non-digit which 12491500Serics ** terminates the number. 12501500Serics */ 12511500Serics 12521500Serics number(cmd) 12531500Serics char *cmd; 12541500Serics { 12551500Serics register int i; 12561500Serics 12571500Serics i = 0; ch = otty.sg_kill; 12581500Serics for (;;) { 12591500Serics ch = readch (); 12601500Serics if (ch >= '0' && ch <= '9') 12611500Serics i = i*10 + ch - '0'; 12621500Serics else if (ch == otty.sg_kill) 12631500Serics i = 0; 12641500Serics else { 12651500Serics *cmd = ch; 12661500Serics break; 12671500Serics } 12681500Serics } 12691500Serics return (i); 12701500Serics } 12711500Serics 12721500Serics do_shell (filename) 12731500Serics char *filename; 12741500Serics { 12751500Serics char cmdbuf[80]; 12761500Serics 12771500Serics kill_line (); 12781500Serics pr ("!"); 12791500Serics fflush (stdout); 12801500Serics promptlen = 1; 12811500Serics if (lastp) 12821500Serics pr (shell_line); 12831500Serics else { 12841500Serics ttyin (cmdbuf, 78, '!'); 12851500Serics if (expand (shell_line, cmdbuf)) { 12861500Serics kill_line (); 12871500Serics promptlen = printf ("!%s", shell_line); 12881500Serics } 12891500Serics } 12901500Serics fflush (stdout); 12911500Serics write (2, "\n", 1); 12921500Serics promptlen = 0; 12931500Serics shellp = 1; 12941500Serics execute (filename, shell, shell, "-c", shell_line, 0); 12951500Serics } 12961500Serics 12971500Serics /* 12981500Serics ** Search for nth ocurrence of regular expression contained in buf in the file 12991500Serics */ 13001500Serics 13011500Serics search (buf, file, n) 13021500Serics char buf[]; 13031500Serics FILE *file; 13041500Serics register int n; 13051500Serics { 13061500Serics long startline = Ftell (file); 13071500Serics register long line1 = startline; 13081500Serics register long line2 = startline; 13091500Serics register long line3 = startline; 13101500Serics register int lncount; 13111500Serics int saveln, rv, re_exec(); 13121500Serics char *s, *re_comp(); 13131500Serics 13141500Serics context.line = saveln = Currline; 13151500Serics context.chrctr = startline; 13161500Serics lncount = 0; 13171500Serics if ((s = re_comp (buf)) != 0) 13181500Serics error (s); 13191500Serics while (!feof (file)) { 13201500Serics line3 = line2; 13211500Serics line2 = line1; 13221500Serics line1 = Ftell (file); 13231500Serics rdline (file); 13241500Serics lncount++; 13251500Serics if ((rv = re_exec (Line)) == 1) 13261500Serics if (--n == 0) { 13271500Serics if (lncount > 3 || (lncount > 1 && no_intty)) 13283455Sroot { 13293455Sroot pr ("\n"); 13303594Sroot if (clreol) 13313594Sroot cleareol (); 13323455Sroot pr("...skipping\n"); 13333455Sroot } 13341500Serics if (!no_intty) { 13351500Serics Currline -= (lncount >= 3 ? 3 : lncount); 13361500Serics Fseek (file, line3); 13373594Sroot if (noscroll) 13383594Sroot if (clreol) { 13393594Sroot home (); 13403594Sroot cleareol (); 134116582Sleres } 13423594Sroot else 13433594Sroot doclear (); 13441500Serics } 13451500Serics else { 13461500Serics kill_line (); 13473594Sroot if (noscroll) 13483594Sroot if (clreol) { 134916582Sleres home (); 13503594Sroot cleareol (); 135116582Sleres } 13523594Sroot else 13533594Sroot doclear (); 13541500Serics pr (Line); 13551500Serics putchar ('\n'); 13561500Serics } 13571500Serics break; 13581500Serics } 13591500Serics else if (rv == -1) 13601500Serics error ("Regular expression botch"); 13611500Serics } 13621500Serics if (feof (file)) { 13631500Serics if (!no_intty) { 13641500Serics file->_flag &= ~_IOEOF; /* why doesn't fseek do this ??!!??! */ 13651500Serics Currline = saveln; 13661500Serics Fseek (file, startline); 13671500Serics } 13681500Serics else { 13691500Serics pr ("\nPattern not found\n"); 13701500Serics end_it (); 13711500Serics } 13721500Serics error ("Pattern not found"); 13731500Serics } 13741500Serics } 13751500Serics 137632737Sbostic /*VARARGS2*/ 137732737Sbostic execute (filename, cmd, va_alist) 13781500Serics char *filename; 137932737Sbostic char *cmd; 138032737Sbostic va_dcl 13811500Serics { 13821500Serics int id; 138316710Sjak int n; 138432737Sbostic va_list argp; 13851500Serics 13861500Serics fflush (stdout); 13871500Serics reset_tty (); 138816710Sjak for (n = 10; (id = fork ()) < 0 && n > 0; n--) 13891500Serics sleep (5); 13901500Serics if (id == 0) { 139116710Sjak if (!isatty(0)) { 139216710Sjak close(0); 139316710Sjak open("/dev/tty", 0); 139416710Sjak } 139532737Sbostic va_start(argp); 139632737Sbostic execv (cmd, argp); 13971500Serics write (2, "exec failed\n", 12); 13981500Serics exit (1); 139933087Sbostic va_end(argp); /* balance {}'s for some UNIX's */ 14001500Serics } 140116710Sjak if (id > 0) { 140216710Sjak signal (SIGINT, SIG_IGN); 140316710Sjak signal (SIGQUIT, SIG_IGN); 140416710Sjak if (catch_susp) 140516710Sjak signal(SIGTSTP, SIG_DFL); 140616710Sjak while (wait(0) > 0); 140716710Sjak signal (SIGINT, end_it); 140816710Sjak signal (SIGQUIT, onquit); 140916710Sjak if (catch_susp) 141016710Sjak signal(SIGTSTP, onsusp); 141116710Sjak } else 141216710Sjak write(2, "can't fork\n", 11); 14131500Serics set_tty (); 14141500Serics pr ("------------------------\n"); 14151500Serics prompt (filename); 14161500Serics } 14171500Serics /* 14181500Serics ** Skip n lines in the file f 14191500Serics */ 14201500Serics 14211500Serics skiplns (n, f) 14221500Serics register int n; 14231500Serics register FILE *f; 14241500Serics { 14251500Serics register char c; 14261500Serics 14271500Serics while (n > 0) { 14281500Serics while ((c = Getc (f)) != '\n') 14291500Serics if (c == EOF) 14301500Serics return; 14311500Serics n--; 14321500Serics Currline++; 14331500Serics } 14341500Serics } 14351500Serics 14361500Serics /* 14371500Serics ** Skip nskip files in the file list (from the command line). Nskip may be 14381500Serics ** negative. 14391500Serics */ 14401500Serics 14411500Serics skipf (nskip) 14421500Serics register int nskip; 14431500Serics { 14441500Serics if (nskip == 0) return; 14451500Serics if (nskip > 0) { 14461500Serics if (fnum + nskip > nfiles - 1) 14471500Serics nskip = nfiles - fnum - 1; 14481500Serics } 14491500Serics else if (within) 14501500Serics ++fnum; 14511500Serics fnum += nskip; 14521500Serics if (fnum < 0) 14531500Serics fnum = 0; 14543594Sroot pr ("\n...Skipping "); 14553455Sroot pr ("\n"); 14563594Sroot if (clreol) 14573594Sroot cleareol (); 14583455Sroot pr ("...Skipping "); 14591500Serics pr (nskip > 0 ? "to file " : "back to file "); 14601500Serics pr (fnames[fnum]); 14613455Sroot pr ("\n"); 14623594Sroot if (clreol) 14633594Sroot cleareol (); 14643455Sroot pr ("\n"); 14651500Serics --fnum; 14661500Serics } 14671500Serics 14681500Serics /*----------------------------- Terminal I/O -------------------------------*/ 14691500Serics 14701500Serics initterm () 14711500Serics { 14721500Serics char buf[TBUFSIZ]; 147317195Sralph static char clearbuf[TBUFSIZ]; 14741500Serics char *clearptr, *padstr; 14751500Serics int ldisc; 147617592Sleres int lmode; 147710823Ssam char *term; 147816582Sleres int tgrp; 147918030Sbloom struct winsize win; 14801500Serics 148116582Sleres retry: 148216582Sleres if (!(no_tty = gtty(fileno(stdout), &otty))) { 148317592Sleres if (ioctl(fileno(stdout), TIOCLGET, &lmode) < 0) { 148417592Sleres perror("TIOCLGET"); 148517592Sleres exit(1); 148617592Sleres } 148717592Sleres docrterase = ((lmode & LCRTERA) != 0); 148817592Sleres docrtkill = ((lmode & LCRTKIL) != 0); 148916582Sleres /* 149017592Sleres * Wait until we're in the foreground before we save the 149117592Sleres * the terminal modes. 149216582Sleres */ 149316582Sleres if (ioctl(fileno(stdout), TIOCGPGRP, &tgrp) < 0) { 149417592Sleres perror("TIOCGPGRP"); 149516582Sleres exit(1); 149616582Sleres } 149716582Sleres if (tgrp != getpgrp(0)) { 149816582Sleres kill(0, SIGTTOU); 149916582Sleres goto retry; 150016582Sleres } 150113830Skre if ((term = getenv("TERM")) == 0 || tgetent(buf, term) <= 0) { 15023594Sroot dumb++; ul_opt = 0; 15031500Serics } 15041500Serics else { 150518030Sbloom if (ioctl(fileno(stdout), TIOCGWINSZ, &win) < 0) { 150618030Sbloom Lpp = tgetnum("li"); 150718030Sbloom Mcol = tgetnum("co"); 150818030Sbloom } else { 150918030Sbloom if ((Lpp = win.ws_row) == 0) 151018030Sbloom Lpp = tgetnum("li"); 151118030Sbloom if ((Mcol = win.ws_col) == 0) 151218030Sbloom Mcol = tgetnum("co"); 151318030Sbloom } 151418030Sbloom if ((Lpp <= 0) || tgetflag("hc")) { 15151500Serics hard++; /* Hard copy terminal */ 15161500Serics Lpp = 24; 15171500Serics } 151829907Smckusick if (tgetflag("xn")) 151929907Smckusick eatnl++; /* Eat newline at last column + 1; dec, concept */ 152018030Sbloom if (Mcol <= 0) 152118030Sbloom Mcol = 80; 152218030Sbloom 15231500Serics if (tailequ (fnames[0], "page") || !hard && tgetflag("ns")) 15241500Serics noscroll++; 15251500Serics Wrap = tgetflag("am"); 15261500Serics bad_so = tgetflag ("xs"); 15271500Serics clearptr = clearbuf; 15281500Serics eraseln = tgetstr("ce",&clearptr); 15291500Serics Clear = tgetstr("cl", &clearptr); 15301500Serics Senter = tgetstr("so", &clearptr); 15311500Serics Sexit = tgetstr("se", &clearptr); 153216710Sjak if ((soglitch = tgetnum("sg")) < 0) 153316710Sjak soglitch = 0; 15343594Sroot 15353594Sroot /* 15363594Sroot * Set up for underlining: some terminals don't need it; 15373594Sroot * others have start/stop sequences, still others have an 15383594Sroot * underline char sequence which is assumed to move the 15393594Sroot * cursor forward one character. If underline sequence 15403594Sroot * isn't available, settle for standout sequence. 15413594Sroot */ 15423594Sroot 15433594Sroot if (tgetflag("ul") || tgetflag("os")) 15443594Sroot ul_opt = 0; 15453594Sroot if ((chUL = tgetstr("uc", &clearptr)) == NULL ) 15463594Sroot chUL = ""; 154716710Sjak if (((ULenter = tgetstr("us", &clearptr)) == NULL || 154816710Sjak (ULexit = tgetstr("ue", &clearptr)) == NULL) && !*chUL) { 154916710Sjak if ((ULenter = Senter) == NULL || (ULexit = Sexit) == NULL) { 155016710Sjak ULenter = ""; 155116710Sjak ULexit = ""; 155216710Sjak } else 155316710Sjak ulglitch = soglitch; 155416710Sjak } else { 155516710Sjak if ((ulglitch = tgetnum("ug")) < 0) 155616710Sjak ulglitch = 0; 155716710Sjak } 155816582Sleres 15591500Serics if (padstr = tgetstr("pc", &clearptr)) 15601500Serics PC = *padstr; 15613455Sroot Home = tgetstr("ho",&clearptr); 156213536Ssam if (Home == 0 || *Home == '\0') 15633455Sroot { 15643594Sroot if ((cursorm = tgetstr("cm", &clearptr)) != NULL) { 15653594Sroot strcpy(cursorhome, tgoto(cursorm, 0, 0)); 15663455Sroot Home = cursorhome; 15673455Sroot } 15683455Sroot } 15693594Sroot EodClr = tgetstr("cd", &clearptr); 157025540Smckusick if ((chBS = tgetstr("bc", &clearptr)) == NULL) 157125540Smckusick chBS = "\b"; 157225540Smckusick 15731500Serics } 15741500Serics if ((shell = getenv("SHELL")) == NULL) 15751500Serics shell = "/bin/sh"; 15761500Serics } 157716582Sleres no_intty = gtty(fileno(stdin), &otty); 157816582Sleres gtty(fileno(stderr), &otty); 157913830Skre savetty = otty; 15801500Serics ospeed = otty.sg_ospeed; 15811500Serics slow_tty = ospeed < B1200; 158227006Sdonn hardtabs = (otty.sg_flags & TBDELAY) != XTABS; 15831500Serics if (!no_tty) { 15841500Serics otty.sg_flags &= ~ECHO; 15851500Serics if (MBIT == CBREAK || !slow_tty) 15861500Serics otty.sg_flags |= MBIT; 15871500Serics } 15881500Serics } 15891500Serics 15901500Serics readch () 15911500Serics { 15921500Serics char ch; 15931500Serics extern int errno; 15941500Serics 159531089Skarels errno = 0; 15961500Serics if (read (2, &ch, 1) <= 0) 15971500Serics if (errno != EINTR) 159831089Skarels end_it(); 15991500Serics else 16001500Serics ch = otty.sg_kill; 16011500Serics return (ch); 16021500Serics } 16031500Serics 16041500Serics static char BS = '\b'; 160517592Sleres static char *BSB = "\b \b"; 16061500Serics static char CARAT = '^'; 160717592Sleres #define ERASEONECHAR \ 160817592Sleres if (docrterase) \ 160917592Sleres write (2, BSB, sizeof(BSB)); \ 161017592Sleres else \ 161117592Sleres write (2, &BS, sizeof(BS)); 16121500Serics 16131500Serics ttyin (buf, nmax, pchar) 16141500Serics char buf[]; 16151500Serics register int nmax; 16161500Serics char pchar; 16171500Serics { 16181500Serics register char *sptr; 16191500Serics register char ch; 16201500Serics register int slash = 0; 16211500Serics int maxlen; 16221500Serics char cbuf; 16231500Serics 16241500Serics sptr = buf; 16251500Serics maxlen = 0; 16261500Serics while (sptr - buf < nmax) { 16271500Serics if (promptlen > maxlen) maxlen = promptlen; 16281500Serics ch = readch (); 16291500Serics if (ch == '\\') { 16301500Serics slash++; 16311500Serics } 16321500Serics else if ((ch == otty.sg_erase) && !slash) { 16331500Serics if (sptr > buf) { 16341500Serics --promptlen; 163517592Sleres ERASEONECHAR 16361500Serics --sptr; 16371500Serics if ((*sptr < ' ' && *sptr != '\n') || *sptr == RUBOUT) { 16381500Serics --promptlen; 163917592Sleres ERASEONECHAR 16401500Serics } 16411500Serics continue; 16421500Serics } 16431500Serics else { 16441500Serics if (!eraseln) promptlen = maxlen; 16451500Serics longjmp (restore, 1); 16461500Serics } 16471500Serics } 16481500Serics else if ((ch == otty.sg_kill) && !slash) { 16491500Serics if (hard) { 16501500Serics show (ch); 16511500Serics putchar ('\n'); 16521500Serics putchar (pchar); 16531500Serics } 16541500Serics else { 16551500Serics putchar ('\r'); 16561500Serics putchar (pchar); 16571500Serics if (eraseln) 16581500Serics erase (1); 165917592Sleres else if (docrtkill) 166017592Sleres while (promptlen-- > 1) 166117592Sleres write (2, BSB, sizeof(BSB)); 16621500Serics promptlen = 1; 16631500Serics } 16641500Serics sptr = buf; 16651500Serics fflush (stdout); 16661500Serics continue; 16671500Serics } 16681500Serics if (slash && (ch == otty.sg_kill || ch == otty.sg_erase)) { 166917592Sleres ERASEONECHAR 16701500Serics --sptr; 16711500Serics } 16721500Serics if (ch != '\\') 16731500Serics slash = 0; 16741500Serics *sptr++ = ch; 16751500Serics if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) { 16761500Serics ch += ch == RUBOUT ? -0100 : 0100; 16771500Serics write (2, &CARAT, 1); 16781500Serics promptlen++; 16791500Serics } 16801500Serics cbuf = ch; 16811500Serics if (ch != '\n' && ch != ESC) { 16821500Serics write (2, &cbuf, 1); 16831500Serics promptlen++; 16841500Serics } 16851500Serics else 16861500Serics break; 16871500Serics } 16881500Serics *--sptr = '\0'; 16891500Serics if (!eraseln) promptlen = maxlen; 16901500Serics if (sptr - buf >= nmax - 1) 16911500Serics error ("Line too long"); 16921500Serics } 16931500Serics 16941500Serics expand (outbuf, inbuf) 16951500Serics char *outbuf; 16961500Serics char *inbuf; 16971500Serics { 16981500Serics register char *instr; 16991500Serics register char *outstr; 17001500Serics register char ch; 17011500Serics char temp[200]; 17021500Serics int changed = 0; 17031500Serics 17041500Serics instr = inbuf; 17051500Serics outstr = temp; 17061500Serics while ((ch = *instr++) != '\0') 17071500Serics switch (ch) { 17081500Serics case '%': 17091500Serics if (!no_intty) { 17101500Serics strcpy (outstr, fnames[fnum]); 17111500Serics outstr += strlen (fnames[fnum]); 17121500Serics changed++; 17131500Serics } 17141500Serics else 17151500Serics *outstr++ = ch; 17161500Serics break; 17171500Serics case '!': 17181500Serics if (!shellp) 17191500Serics error ("No previous command to substitute for"); 17201500Serics strcpy (outstr, shell_line); 17211500Serics outstr += strlen (shell_line); 17221500Serics changed++; 17231500Serics break; 17241500Serics case '\\': 17251500Serics if (*instr == '%' || *instr == '!') { 17261500Serics *outstr++ = *instr++; 17271500Serics break; 17281500Serics } 17291500Serics default: 17301500Serics *outstr++ = ch; 17311500Serics } 17321500Serics *outstr++ = '\0'; 17331500Serics strcpy (outbuf, temp); 17341500Serics return (changed); 17351500Serics } 17361500Serics 17371500Serics show (ch) 17381500Serics register char ch; 17391500Serics { 17401500Serics char cbuf; 17411500Serics 17421500Serics if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) { 17431500Serics ch += ch == RUBOUT ? -0100 : 0100; 17441500Serics write (2, &CARAT, 1); 17451500Serics promptlen++; 17461500Serics } 17471500Serics cbuf = ch; 17481500Serics write (2, &cbuf, 1); 17491500Serics promptlen++; 17501500Serics } 17511500Serics 17521500Serics error (mess) 17531500Serics char *mess; 17541500Serics { 17553594Sroot if (clreol) 17563594Sroot cleareol (); 17573594Sroot else 17583594Sroot kill_line (); 17591500Serics promptlen += strlen (mess); 17601500Serics if (Senter && Sexit) { 17611500Serics tputs (Senter, 1, putch); 17621500Serics pr(mess); 17631500Serics tputs (Sexit, 1, putch); 17641500Serics } 17651500Serics else 17661500Serics pr (mess); 17671500Serics fflush(stdout); 17681500Serics errors++; 17691500Serics longjmp (restore, 1); 17701500Serics } 17711500Serics 17721500Serics 17731500Serics set_tty () 17741500Serics { 17751500Serics otty.sg_flags |= MBIT; 17761500Serics otty.sg_flags &= ~ECHO; 177716582Sleres stty(fileno(stderr), &otty); 17781500Serics } 17791500Serics 17801500Serics reset_tty () 17811500Serics { 178231033Sbostic if (no_tty) 178331033Sbostic return; 178416710Sjak if (pstate) { 178516710Sjak tputs(ULexit, 1, putch); 178616710Sjak fflush(stdout); 178716710Sjak pstate = 0; 178816710Sjak } 17891500Serics otty.sg_flags |= ECHO; 17901500Serics otty.sg_flags &= ~MBIT; 179116582Sleres stty(fileno(stderr), &savetty); 17921500Serics } 17931500Serics 17941500Serics rdline (f) 17951500Serics register FILE *f; 17961500Serics { 17971500Serics register char c; 17981500Serics register char *p; 17991500Serics 18001500Serics p = Line; 18011500Serics while ((c = Getc (f)) != '\n' && c != EOF && p - Line < LINSIZ - 1) 18021500Serics *p++ = c; 18031500Serics if (c == '\n') 18041500Serics Currline++; 18051500Serics *p = '\0'; 18061500Serics } 18071500Serics 18081500Serics /* Come here when we get a suspend signal from the terminal */ 18091500Serics 18101500Serics onsusp () 18111500Serics { 181214861Skarels /* ignore SIGTTOU so we don't get stopped if csh grabs the tty */ 181314861Skarels signal(SIGTTOU, SIG_IGN); 18141500Serics reset_tty (); 18151500Serics fflush (stdout); 181614861Skarels signal(SIGTTOU, SIG_DFL); 18171500Serics /* Send the TSTP signal to suspend our process group */ 181813289Ssam signal(SIGTSTP, SIG_DFL); 181913289Ssam sigsetmask(0); 18201500Serics kill (0, SIGTSTP); 18211500Serics /* Pause for station break */ 18221500Serics 18231500Serics /* We're back */ 18241500Serics signal (SIGTSTP, onsusp); 18251500Serics set_tty (); 18261500Serics if (inwait) 18271500Serics longjmp (restore); 18281500Serics } 1829