121981Sdist /* 221981Sdist * Copyright (c) 1980 Regents of the University of California. 321981Sdist * All rights reserved. The Berkeley software License Agreement 421981Sdist * specifies the terms and conditions for redistribution. 521981Sdist */ 621981Sdist 713615Ssam #ifndef lint 821981Sdist char copyright[] = 921981Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 1021981Sdist All rights reserved.\n"; 1121981Sdist #endif not lint 123594Sroot 1321981Sdist #ifndef lint 14*31033Sbostic static char sccsid[] = "@(#)more.c 5.10 (Berkeley) 05/06/87"; 1521981Sdist #endif not lint 1621981Sdist 171500Serics /* 181500Serics ** more.c - General purpose tty output filter and file perusal program 191500Serics ** 201500Serics ** by Eric Shienbrood, UC Berkeley 213594Sroot ** 223594Sroot ** modified by Geoff Peck, UCB to add underlining, single spacing 233594Sroot ** modified by John Foderaro, UCB to add -c and MORE environment variable 241500Serics */ 251500Serics 261500Serics #include <stdio.h> 2717592Sleres #include <sys/types.h> 281500Serics #include <ctype.h> 291500Serics #include <signal.h> 301500Serics #include <errno.h> 311500Serics #include <sgtty.h> 321500Serics #include <setjmp.h> 331500Serics #include <sys/stat.h> 3429906Smckusick #include <sys/file.h> 3529906Smckusick #include <sys/exec.h> 361500Serics 3713615Ssam #define HELPFILE "/usr/lib/more.help" 3813615Ssam #define VI "/usr/ucb/vi" 391500Serics 401500Serics #define Fopen(s,m) (Currline = 0,file_pos=0,fopen(s,m)) 411500Serics #define Ftell(f) file_pos 421500Serics #define Fseek(f,off) (file_pos=off,fseek(f,off,0)) 431500Serics #define Getc(f) (++file_pos, getc(f)) 441500Serics #define Ungetc(c,f) (--file_pos, ungetc(c,f)) 451500Serics 461500Serics #define MBIT CBREAK 471500Serics #define stty(fd,argp) ioctl(fd,TIOCSETN,argp) 481500Serics 491500Serics #define TBUFSIZ 1024 501500Serics #define LINSIZ 256 511500Serics #define ctrl(letter) ('letter' & 077) 521500Serics #define RUBOUT '\177' 531500Serics #define ESC '\033' 541500Serics #define QUIT '\034' 551500Serics 5616582Sleres struct sgttyb otty, savetty; 571500Serics long file_pos, file_size; 581500Serics int fnum, no_intty, no_tty, slow_tty; 5927006Sdonn int dum_opt, dlines, onquit(), end_it(), chgwinsz(); 601500Serics int onsusp(); 611500Serics int nscroll = 11; /* Number of lines scrolled by 'd' */ 621500Serics int fold_opt = 1; /* Fold long lines */ 631500Serics int stop_opt = 1; /* Stop after form feeds */ 643594Sroot int ssp_opt = 0; /* Suppress white space */ 653594Sroot int ul_opt = 1; /* Underline as best we can */ 661500Serics int promptlen; 671500Serics int Currline; /* Line we are currently at */ 681500Serics int startup = 1; 691500Serics int firstf = 1; 701500Serics int notell = 1; 7117592Sleres int docrterase = 0; 7217592Sleres int docrtkill = 0; 731500Serics int bad_so; /* True if overwriting does not turn off standout */ 741500Serics int inwait, Pause, errors; 751500Serics int within; /* true if we are within a file, 761500Serics false if we are between files */ 7729907Smckusick int hard, dumb, noscroll, hardtabs, clreol, eatnl; 781500Serics int catch_susp; /* We should catch the SIGTSTP signal */ 791500Serics char **fnames; /* The list of file names */ 801500Serics int nfiles; /* Number of files left to process */ 811500Serics char *shell; /* The name of the shell to use */ 821500Serics int shellp; /* A previous shell command exists */ 831500Serics char ch; 841500Serics jmp_buf restore; 851500Serics char Line[LINSIZ]; /* Line buffer */ 861500Serics int Lpp = 24; /* lines per page */ 871500Serics char *Clear; /* clear screen */ 881500Serics char *eraseln; /* erase line */ 891500Serics char *Senter, *Sexit;/* enter and exit standout mode */ 903594Sroot char *ULenter, *ULexit; /* enter and exit underline mode */ 913594Sroot char *chUL; /* underline character */ 923594Sroot char *chBS; /* backspace character */ 933455Sroot char *Home; /* go to home */ 943455Sroot char *cursorm; /* cursor movement */ 953455Sroot char cursorhome[40]; /* contains cursor movement to home */ 963594Sroot char *EodClr; /* clear rest of screen */ 971500Serics char *tgetstr(); 981500Serics int Mcol = 80; /* number of columns */ 991500Serics int Wrap = 1; /* set if automargins */ 10016710Sjak int soglitch; /* terminal has standout mode glitch */ 10116710Sjak int ulglitch; /* terminal has underline mode glitch */ 10216710Sjak int pstate = 0; /* current UL state */ 1031500Serics long fseek(); 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 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) { 1751500Serics fputs("Usage: ",stderr); 1761500Serics fputs(argv[0],stderr); 1771500Serics fputs(" [-dfln] [+linenum | +/pattern] name1 name2 ...\n",stderr); 1781500Serics exit(1); 1791500Serics } 1801500Serics else 1811500Serics f = stdin; 1821500Serics if (!no_tty) { 1831500Serics signal(SIGQUIT, onquit); 1841500Serics signal(SIGINT, end_it); 18527006Sdonn signal(SIGWINCH, chgwinsz); 1861500Serics if (signal (SIGTSTP, SIG_IGN) == SIG_DFL) { 1871500Serics signal(SIGTSTP, onsusp); 1881500Serics catch_susp++; 1891500Serics } 19016582Sleres stty (fileno(stderr), &otty); 1911500Serics } 1921500Serics if (no_intty) { 1931500Serics if (no_tty) 1941500Serics copy_file (stdin); 1951500Serics else { 1961500Serics if ((ch = Getc (f)) == '\f') 1973594Sroot doclear(); 1981500Serics else { 1991500Serics Ungetc (ch, f); 2003594Sroot if (noscroll && (ch != EOF)) { 2013594Sroot if (clreol) 2023594Sroot home (); 2033594Sroot else 2043594Sroot doclear (); 2053455Sroot } 2061500Serics } 2071500Serics if (srchopt) 2083455Sroot { 2091500Serics search (initbuf, stdin, 1); 2103594Sroot if (noscroll) 2113594Sroot left--; 2123455Sroot } 2131500Serics else if (initopt) 2141500Serics skiplns (initline, stdin); 2151500Serics screen (stdin, left); 2161500Serics } 2171500Serics no_intty = 0; 2181500Serics prnames++; 2191500Serics firstf = 0; 2201500Serics } 2211500Serics 2221500Serics while (fnum < nfiles) { 2231500Serics if ((f = checkf (fnames[fnum], &clearit)) != NULL) { 2241500Serics context.line = context.chrctr = 0; 2251500Serics Currline = 0; 2261500Serics if (firstf) setjmp (restore); 2271500Serics if (firstf) { 2281500Serics firstf = 0; 2291500Serics if (srchopt) 2303455Sroot { 2311500Serics search (initbuf, f, 1); 2323594Sroot if (noscroll) 2333594Sroot left--; 2343455Sroot } 2351500Serics else if (initopt) 2361500Serics skiplns (initline, f); 2371500Serics } 2381500Serics else if (fnum < nfiles && !no_tty) { 2391500Serics setjmp (restore); 2401500Serics left = command (fnames[fnum], f); 2411500Serics } 2421500Serics if (left != 0) { 2433594Sroot if ((noscroll || clearit) && (file_size != 0x7fffffffffffffffL)) 2443594Sroot if (clreol) 2453594Sroot home (); 2463594Sroot else 2473594Sroot doclear (); 2481500Serics if (prnames) { 2491500Serics if (bad_so) 2501500Serics erase (0); 2513594Sroot if (clreol) 2523594Sroot cleareol (); 2531500Serics pr("::::::::::::::"); 2541500Serics if (promptlen > 14) 2551500Serics erase (14); 2563455Sroot printf ("\n"); 2573455Sroot if(clreol) cleareol(); 2583455Sroot printf("%s\n", fnames[fnum]); 2593455Sroot if(clreol) cleareol(); 26030931Sbostic printf("::::::::::::::\n"); 2611500Serics if (left > Lpp - 4) 2621500Serics left = Lpp - 4; 2631500Serics } 2641500Serics if (no_tty) 2651500Serics copy_file (f); 2661500Serics else { 2671500Serics within++; 2681500Serics screen(f, left); 2691500Serics within = 0; 2701500Serics } 2711500Serics } 2721500Serics setjmp (restore); 2731500Serics fflush(stdout); 2741500Serics fclose(f); 2751500Serics screen_start.line = screen_start.chrctr = 0L; 2761500Serics context.line = context.chrctr = 0L; 2771500Serics } 2781500Serics fnum++; 2791500Serics firstf = 0; 2801500Serics } 2811500Serics reset_tty (); 2821500Serics exit(0); 2831500Serics } 2841500Serics 2853455Sroot argscan(s) 2863455Sroot char *s; 2873455Sroot { 28811604Slayer for (dlines = 0; *s != '\0'; s++) 28911604Slayer { 29011604Slayer switch (*s) 29111604Slayer { 29211604Slayer case '0': case '1': case '2': 29311604Slayer case '3': case '4': case '5': 29411604Slayer case '6': case '7': case '8': 29511604Slayer case '9': 29611604Slayer dlines = dlines*10 + *s - '0'; 29711604Slayer break; 29811604Slayer case 'd': 29911604Slayer dum_opt = 1; 30011604Slayer break; 30111604Slayer case 'l': 30211604Slayer stop_opt = 0; 30311604Slayer break; 30411604Slayer case 'f': 30511604Slayer fold_opt = 0; 30611604Slayer break; 30711604Slayer case 'p': 30811604Slayer noscroll++; 30911604Slayer break; 31011604Slayer case 'c': 31111604Slayer clreol++; 31211604Slayer break; 31311604Slayer case 's': 31411604Slayer ssp_opt = 1; 31511604Slayer break; 31611604Slayer case 'u': 31711604Slayer ul_opt = 0; 31811604Slayer break; 31911604Slayer } 32011604Slayer } 3213455Sroot } 3223455Sroot 3233455Sroot 3241500Serics /* 3251500Serics ** Check whether the file named by fs is an ASCII file which the user may 3261500Serics ** access. If it is, return the opened file. Otherwise return NULL. 3271500Serics */ 3281500Serics 3291500Serics FILE * 3301500Serics checkf (fs, clearfirst) 3311500Serics register char *fs; 3321500Serics int *clearfirst; 3331500Serics { 3341500Serics struct stat stbuf; 3351500Serics register FILE *f; 3361500Serics char c; 3371500Serics 3381500Serics if (stat (fs, &stbuf) == -1) { 3391500Serics fflush(stdout); 3403594Sroot if (clreol) 3413594Sroot cleareol (); 3421500Serics perror(fs); 3431500Serics return (NULL); 3441500Serics } 3451500Serics if ((stbuf.st_mode & S_IFMT) == S_IFDIR) { 3461500Serics printf("\n*** %s: directory ***\n\n", fs); 3471500Serics return (NULL); 3481500Serics } 3491500Serics if ((f=Fopen(fs, "r")) == NULL) { 3501500Serics fflush(stdout); 3511500Serics perror(fs); 3521500Serics return (NULL); 3531500Serics } 3541500Serics /* Try to see whether it is an ASCII file */ 35529906Smckusick if (magic(f)) { 3561500Serics printf("\n******** %s: Not a text file ********\n\n", fs); 3571500Serics fclose (f); 3581500Serics return (NULL); 3591500Serics } 36029906Smckusick c = Getc(f); 3611500Serics if (c == '\f') 3621500Serics *clearfirst = 1; 3631500Serics else { 3641500Serics *clearfirst = 0; 3651500Serics Ungetc (c, f); 3661500Serics } 3671500Serics if ((file_size = stbuf.st_size) == 0) 3681500Serics file_size = 0x7fffffffffffffffL; 3691500Serics return (f); 3701500Serics } 3711500Serics 3721500Serics /* 37329906Smckusick * Check for file magic numbers. This code would best 37429906Smckusick * be shared with the file(1) program or, perhaps, more 37529906Smckusick * should not try and be so smart? 37629906Smckusick */ 37729906Smckusick magic(f) 37829906Smckusick FILE *f; 37929906Smckusick { 38029906Smckusick long magic; 38129906Smckusick 38229906Smckusick magic = getw(f); 38329906Smckusick fseek(f, -sizeof (magic), L_INCR); /* reset file position */ 38429906Smckusick return (magic == 0405 || magic == OMAGIC || magic == NMAGIC || 38529906Smckusick magic == 0411 || magic == ZMAGIC || magic == 0177545); 38629906Smckusick } 38729906Smckusick 38829906Smckusick /* 3891500Serics ** A real function, for the tputs routine in termlib 3901500Serics */ 3911500Serics 3921500Serics putch (ch) 3931500Serics char ch; 3941500Serics { 3951500Serics putchar (ch); 3961500Serics } 3971500Serics 3981500Serics /* 3991500Serics ** Print out the contents of the file f, one screenful at a time. 4001500Serics */ 4011500Serics 4021500Serics #define STOP -10 4031500Serics 4041500Serics screen (f, num_lines) 4051500Serics register FILE *f; 4061500Serics register int num_lines; 4071500Serics { 4081500Serics register int c; 4091500Serics register int nchars; 4103594Sroot int length; /* length of current line */ 4113594Sroot static int prev_len = 1; /* length of previous line */ 4121500Serics 4131500Serics for (;;) { 4141500Serics while (num_lines > 0 && !Pause) { 4151500Serics if ((nchars = getline (f, &length)) == EOF) 4163455Sroot { 4173594Sroot if (clreol) 4183594Sroot clreos(); 4191500Serics return; 4203455Sroot } 4213594Sroot if (ssp_opt && length == 0 && prev_len == 0) 4223594Sroot continue; 4233594Sroot prev_len = length; 4241500Serics if (bad_so || (Senter && *Senter == ' ') && promptlen > 0) 4251500Serics erase (0); 4263594Sroot /* must clear before drawing line since tabs on some terminals 4273594Sroot * do not erase what they tab over. 4283594Sroot */ 4293594Sroot if (clreol) 4303594Sroot cleareol (); 4311500Serics prbuf (Line, length); 4321500Serics if (nchars < promptlen) 4331500Serics erase (nchars); /* erase () sets promptlen to 0 */ 4341500Serics else promptlen = 0; 4353594Sroot /* is this needed? 4363594Sroot * if (clreol) 4373594Sroot * cleareol(); /* must clear again in case we wrapped * 4383594Sroot */ 4391500Serics if (nchars < Mcol || !fold_opt) 44016710Sjak prbuf("\n", 1); /* will turn off UL if necessary */ 4411500Serics if (nchars == STOP) 4421500Serics break; 4431500Serics num_lines--; 4441500Serics } 44516710Sjak if (pstate) { 44616710Sjak tputs(ULexit, 1, putch); 44716710Sjak pstate = 0; 44816710Sjak } 4491500Serics fflush(stdout); 4501500Serics if ((c = Getc(f)) == EOF) 4513455Sroot { 4523594Sroot if (clreol) 4533594Sroot clreos (); 4541500Serics return; 4553455Sroot } 4563455Sroot 4573594Sroot if (Pause && clreol) 4583594Sroot clreos (); 4591500Serics Ungetc (c, f); 4601500Serics setjmp (restore); 4611500Serics Pause = 0; startup = 0; 4621500Serics if ((num_lines = command (NULL, f)) == 0) 4631500Serics return; 4641500Serics if (hard && promptlen > 0) 4651500Serics erase (0); 46611123Slayer if (noscroll && num_lines >= dlines) 46716582Sleres { 4683594Sroot if (clreol) 4693594Sroot home(); 4703594Sroot else 4713594Sroot doclear (); 4723455Sroot } 4731500Serics screen_start.line = Currline; 4741500Serics screen_start.chrctr = Ftell (f); 4751500Serics } 4761500Serics } 4771500Serics 4781500Serics /* 4791500Serics ** Come here if a quit signal is received 4801500Serics */ 4811500Serics 4821500Serics onquit() 4831500Serics { 4841500Serics signal(SIGQUIT, SIG_IGN); 4851500Serics if (!inwait) { 4861500Serics putchar ('\n'); 4871500Serics if (!startup) { 4881500Serics signal(SIGQUIT, onquit); 4891500Serics longjmp (restore, 1); 4901500Serics } 4911500Serics else 4921500Serics Pause++; 4931500Serics } 4941500Serics else if (!dum_opt && notell) { 4951500Serics write (2, "[Use q or Q to quit]", 20); 4961500Serics promptlen += 20; 4971500Serics notell = 0; 4981500Serics } 4991500Serics signal(SIGQUIT, onquit); 5001500Serics } 5011500Serics 5021500Serics /* 50327006Sdonn ** Come here if a signal for a window size change is received 50427006Sdonn */ 50527006Sdonn 50627006Sdonn chgwinsz() 50727006Sdonn { 50827006Sdonn struct winsize win; 50927006Sdonn 51027006Sdonn (void) signal(SIGWINCH, SIG_IGN); 51127006Sdonn if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) { 51227006Sdonn if (win.ws_row != 0) { 51327006Sdonn Lpp = win.ws_row; 51427006Sdonn nscroll = Lpp/2 - 1; 51527006Sdonn if (nscroll <= 0) 51627006Sdonn nscroll = 1; 51727006Sdonn dlines = Lpp - (noscroll ? 1 : 2); 51827006Sdonn } 51927006Sdonn if (win.ws_col != 0) 52027006Sdonn Mcol = win.ws_col; 52127006Sdonn } 52227006Sdonn (void) signal(SIGWINCH, chgwinsz); 52327006Sdonn } 52427006Sdonn 52527006Sdonn /* 5261500Serics ** Clean up terminal state and exit. Also come here if interrupt signal received 5271500Serics */ 5281500Serics 5291500Serics end_it () 5301500Serics { 5311500Serics 5321500Serics reset_tty (); 5333594Sroot if (clreol) { 5343594Sroot putchar ('\r'); 5353594Sroot clreos (); 5363594Sroot fflush (stdout); 5373594Sroot } 5383455Sroot else if (!clreol && (promptlen > 0)) { 5391500Serics kill_line (); 5401500Serics fflush (stdout); 5411500Serics } 5421500Serics else 5431500Serics write (2, "\n", 1); 5441500Serics _exit(0); 5451500Serics } 5461500Serics 5471500Serics copy_file(f) 5481500Serics register FILE *f; 5491500Serics { 5501500Serics register int c; 5511500Serics 5521500Serics while ((c = getc(f)) != EOF) 5531500Serics putchar(c); 5541500Serics } 5551500Serics 5561500Serics /* Simplified printf function */ 5571500Serics 5581500Serics printf (fmt, args) 5591500Serics register char *fmt; 5601500Serics int args; 5611500Serics { 5621500Serics register int *argp; 5631500Serics register char ch; 5641500Serics register int ccount; 5651500Serics 5661500Serics ccount = 0; 5671500Serics argp = &args; 5681500Serics while (*fmt) { 5691500Serics while ((ch = *fmt++) != '%') { 5701500Serics if (ch == '\0') 5711500Serics return (ccount); 5721500Serics ccount++; 5731500Serics putchar (ch); 5741500Serics } 5751500Serics switch (*fmt++) { 5761500Serics case 'd': 5771500Serics ccount += printd (*argp); 5781500Serics break; 5791500Serics case 's': 5801500Serics ccount += pr ((char *)*argp); 5811500Serics break; 5821500Serics case '%': 5831500Serics ccount++; 5841500Serics argp--; 5851500Serics putchar ('%'); 5861500Serics break; 5871500Serics case '0': 5881500Serics return (ccount); 5891500Serics default: 5901500Serics break; 5911500Serics } 5921500Serics ++argp; 5931500Serics } 5941500Serics return (ccount); 5951500Serics 5961500Serics } 5971500Serics 5981500Serics /* 5991500Serics ** Print an integer as a string of decimal digits, 6001500Serics ** returning the length of the print representation. 6011500Serics */ 6021500Serics 6031500Serics printd (n) 6041500Serics int n; 6051500Serics { 6061500Serics int a, nchars; 6071500Serics 6081500Serics if (a = n/10) 6091500Serics nchars = 1 + printd(a); 6101500Serics else 6111500Serics nchars = 1; 6121500Serics putchar (n % 10 + '0'); 6131500Serics return (nchars); 6141500Serics } 6151500Serics 6161500Serics /* Put the print representation of an integer into a string */ 6171500Serics static char *sptr; 6181500Serics 6191500Serics scanstr (n, str) 6201500Serics int n; 6211500Serics char *str; 6221500Serics { 6231500Serics sptr = str; 62411604Slayer Sprintf (n); 6251500Serics *sptr = '\0'; 6261500Serics } 6271500Serics 62811604Slayer Sprintf (n) 6291500Serics { 6301500Serics int a; 6311500Serics 6321500Serics if (a = n/10) 63311604Slayer Sprintf (a); 6341500Serics *sptr++ = n % 10 + '0'; 6351500Serics } 6361500Serics 6371500Serics static char bell = ctrl(G); 6381500Serics 6391500Serics strlen (s) 6401500Serics char *s; 6411500Serics { 6421500Serics register char *p; 6431500Serics 6441500Serics p = s; 6451500Serics while (*p++) 6461500Serics ; 6471500Serics return (p - s - 1); 6481500Serics } 6491500Serics 6501500Serics /* See whether the last component of the path name "path" is equal to the 6511500Serics ** string "string" 6521500Serics */ 6531500Serics 6541500Serics tailequ (path, string) 6551500Serics char *path; 6561500Serics register char *string; 6571500Serics { 6581500Serics register char *tail; 6591500Serics 6601500Serics tail = path + strlen(path); 6611500Serics while (tail >= path) 6621500Serics if (*(--tail) == '/') 6631500Serics break; 6641500Serics ++tail; 6651500Serics while (*tail++ == *string++) 6661500Serics if (*tail == '\0') 6671500Serics return(1); 6681500Serics return(0); 6691500Serics } 6701500Serics 6711500Serics prompt (filename) 6721500Serics char *filename; 6731500Serics { 6743594Sroot if (clreol) 6753594Sroot cleareol (); 6763455Sroot else if (promptlen > 0) 6771500Serics kill_line (); 6781500Serics if (!hard) { 6791500Serics promptlen = 8; 68016710Sjak if (Senter && Sexit) { 6811500Serics tputs (Senter, 1, putch); 68216710Sjak promptlen += (2 * soglitch); 68316710Sjak } 6843594Sroot if (clreol) 6853594Sroot cleareol (); 6861500Serics pr("--More--"); 6871500Serics if (filename != NULL) { 6881500Serics promptlen += printf ("(Next file: %s)", filename); 6891500Serics } 6901500Serics else if (!no_intty) { 6911500Serics promptlen += printf ("(%d%%)", (int)((file_pos * 100) / file_size)); 6921500Serics } 6931500Serics if (dum_opt) { 69416710Sjak promptlen += pr("[Press space to continue, 'q' to quit.]"); 6951500Serics } 6961500Serics if (Senter && Sexit) 6971500Serics tputs (Sexit, 1, putch); 6983594Sroot if (clreol) 6993594Sroot clreos (); 7001500Serics fflush(stdout); 7011500Serics } 7021500Serics else 7031500Serics write (2, &bell, 1); 7041500Serics inwait++; 7051500Serics } 7061500Serics 7071500Serics /* 7081500Serics ** Get a logical line 7091500Serics */ 7101500Serics 7111500Serics getline(f, length) 7121500Serics register FILE *f; 7131500Serics int *length; 7141500Serics { 7151500Serics register int c; 7161500Serics register char *p; 7171500Serics register int column; 7181500Serics static int colflg; 7191500Serics 7201500Serics p = Line; 7211500Serics column = 0; 7221500Serics c = Getc (f); 7231500Serics if (colflg && c == '\n') { 7241500Serics Currline++; 7251500Serics c = Getc (f); 7261500Serics } 7271500Serics while (p < &Line[LINSIZ - 1]) { 7281500Serics if (c == EOF) { 7291500Serics if (p > Line) { 7301500Serics *p = '\0'; 7311500Serics *length = p - Line; 7321500Serics return (column); 7331500Serics } 7341500Serics *length = p - Line; 7351500Serics return (EOF); 7361500Serics } 7371500Serics if (c == '\n') { 7381500Serics Currline++; 7391500Serics break; 7401500Serics } 7411500Serics *p++ = c; 7421500Serics if (c == '\t') 74329907Smckusick if (!hardtabs || column < promptlen && !hard) { 74429907Smckusick if (hardtabs && eraseln && !dumb) { 7451500Serics column = 1 + (column | 7); 7461500Serics tputs (eraseln, 1, putch); 7471500Serics promptlen = 0; 7481500Serics } 7491500Serics else { 75029907Smckusick for (--p; p < &Line[LINSIZ - 1];) { 7511500Serics *p++ = ' '; 75229907Smckusick if ((++column & 7) == 0) 75329907Smckusick break; 7541500Serics } 7551500Serics if (column >= promptlen) promptlen = 0; 7561500Serics } 7571500Serics } 7581500Serics else 7591500Serics column = 1 + (column | 7); 7609627Ssklower else if (c == '\b' && column > 0) 7611500Serics column--; 7621500Serics else if (c == '\r') 7631500Serics column = 0; 7641500Serics else if (c == '\f' && stop_opt) { 7651500Serics p[-1] = '^'; 7661500Serics *p++ = 'L'; 7671500Serics column += 2; 7681500Serics Pause++; 7691500Serics } 7701500Serics else if (c == EOF) { 7711500Serics *length = p - Line; 7721500Serics return (column); 7731500Serics } 7741500Serics else if (c >= ' ' && c != RUBOUT) 7751500Serics column++; 7761500Serics if (column >= Mcol && fold_opt) break; 7771500Serics c = Getc (f); 7781500Serics } 7791500Serics if (column >= Mcol && Mcol > 0) { 7801500Serics if (!Wrap) { 7811500Serics *p++ = '\n'; 7821500Serics } 7831500Serics } 7841500Serics colflg = column == Mcol && fold_opt; 78529907Smckusick if (colflg && eatnl && Wrap) { 78629907Smckusick *p++ = '\n'; /* simulate normal wrap */ 78729907Smckusick } 7881500Serics *length = p - Line; 7891500Serics *p = 0; 7901500Serics return (column); 7911500Serics } 7921500Serics 7931500Serics /* 7941500Serics ** Erase the rest of the prompt, assuming we are starting at column col. 7951500Serics */ 7961500Serics 7971500Serics erase (col) 7981500Serics register int col; 7991500Serics { 8001500Serics 8011500Serics if (promptlen == 0) 8021500Serics return; 8031500Serics if (hard) { 8041500Serics putchar ('\n'); 8051500Serics } 8061500Serics else { 8071500Serics if (col == 0) 8081500Serics putchar ('\r'); 8091500Serics if (!dumb && eraseln) 8101500Serics tputs (eraseln, 1, putch); 8111500Serics else 8121500Serics for (col = promptlen - col; col > 0; col--) 8131500Serics putchar (' '); 8141500Serics } 8151500Serics promptlen = 0; 8161500Serics } 8171500Serics 8181500Serics /* 8191500Serics ** Erase the current line entirely 8201500Serics */ 8211500Serics 8221500Serics kill_line () 8231500Serics { 8241500Serics erase (0); 8251500Serics if (!eraseln || dumb) putchar ('\r'); 8261500Serics } 8271500Serics 8281500Serics /* 8293455Sroot * force clear to end of line 8303455Sroot */ 8313455Sroot cleareol() 8323455Sroot { 8333594Sroot tputs(eraseln, 1, putch); 8343455Sroot } 8353455Sroot 8363594Sroot clreos() 8373455Sroot { 8383594Sroot tputs(EodClr, 1, putch); 8393455Sroot } 8403455Sroot 8413455Sroot /* 8421500Serics ** Print string and return number of characters 8431500Serics */ 8441500Serics 8451500Serics pr(s1) 8461500Serics char *s1; 8471500Serics { 8481500Serics register char *s; 8491500Serics register char c; 8501500Serics 8511500Serics for (s = s1; c = *s++; ) 8521500Serics putchar(c); 8531500Serics return (s - s1 - 1); 8541500Serics } 8551500Serics 8561500Serics 8571500Serics /* Print a buffer of n characters */ 8581500Serics 8591500Serics prbuf (s, n) 8601500Serics register char *s; 8611500Serics register int n; 8621500Serics { 86316710Sjak register char c; /* next output character */ 8643594Sroot register int state; /* next output char's UL state */ 86516710Sjak #define wouldul(s,n) ((n) >= 2 && (((s)[0] == '_' && (s)[1] == '\b') || ((s)[1] == '\b' && (s)[2] == '_'))) 8663594Sroot 8673594Sroot while (--n >= 0) 8683594Sroot if (!ul_opt) 8693594Sroot putchar (*s++); 8703594Sroot else { 87116710Sjak if (*s == ' ' && pstate == 0 && ulglitch && wouldul(s+1, n-1)) { 87216710Sjak s++; 87316710Sjak continue; 87416710Sjak } 87516710Sjak if (state = wouldul(s, n)) { 87616710Sjak c = (*s == '_')? s[2] : *s ; 8773594Sroot n -= 2; 87816710Sjak s += 3; 87916710Sjak } else 8803594Sroot c = *s++; 88116710Sjak if (state != pstate) { 88216710Sjak if (c == ' ' && state == 0 && ulglitch && wouldul(s, n-1)) 88316710Sjak state = 1; 88416710Sjak else 88516710Sjak tputs(state ? ULenter : ULexit, 1, putch); 8863594Sroot } 88716710Sjak if (c != ' ' || pstate == 0 || state != 0 || ulglitch == 0) 88816710Sjak putchar(c); 8893594Sroot if (state && *chUL) { 8903594Sroot pr(chBS); 8913594Sroot tputs(chUL, 1, putch); 8923594Sroot } 89316710Sjak pstate = state; 8943594Sroot } 8951500Serics } 8961500Serics 8971500Serics /* 8981500Serics ** Clear the screen 8991500Serics */ 9001500Serics 9011500Serics doclear() 9021500Serics { 9031500Serics if (Clear && !hard) { 9041500Serics tputs(Clear, 1, putch); 9051500Serics 9061500Serics /* Put out carriage return so that system doesn't 9071500Serics ** get confused by escape sequences when expanding tabs 9081500Serics */ 9091500Serics putchar ('\r'); 9101500Serics promptlen = 0; 9111500Serics } 9121500Serics } 9131500Serics 9143455Sroot /* 9153455Sroot * Go to home position 9163455Sroot */ 9173455Sroot home() 9183455Sroot { 9193455Sroot tputs(Home,1,putch); 9203455Sroot } 9213455Sroot 9221500Serics static int lastcmd, lastarg, lastp; 9231500Serics static int lastcolon; 9241500Serics char shell_line[132]; 9251500Serics 9261500Serics /* 9271500Serics ** Read a command and do it. A command consists of an optional integer 9281500Serics ** argument followed by the command character. Return the number of lines 9291500Serics ** to display in the next screenful. If there is nothing more to display 9301500Serics ** in the current file, zero is returned. 9311500Serics */ 9321500Serics 9331500Serics command (filename, f) 9341500Serics char *filename; 9351500Serics register FILE *f; 9361500Serics { 9371500Serics register int nlines; 9381500Serics register int retval; 9391500Serics register char c; 9401500Serics char colonch; 9411500Serics FILE *helpf; 9421500Serics int done; 9431500Serics char comchar, cmdbuf[80], *p; 9441500Serics 9451500Serics #define ret(val) retval=val;done++;break 9461500Serics 9471500Serics done = 0; 9481500Serics if (!errors) 9491500Serics prompt (filename); 9501500Serics else 9511500Serics errors = 0; 9521500Serics if (MBIT == RAW && slow_tty) { 9531500Serics otty.sg_flags |= MBIT; 95416582Sleres stty(fileno(stderr), &otty); 9551500Serics } 9561500Serics for (;;) { 9571500Serics nlines = number (&comchar); 9581500Serics lastp = colonch = 0; 9591500Serics if (comchar == '.') { /* Repeat last command */ 9601500Serics lastp++; 9611500Serics comchar = lastcmd; 9621500Serics nlines = lastarg; 9631500Serics if (lastcmd == ':') 9641500Serics colonch = lastcolon; 9651500Serics } 9661500Serics lastcmd = comchar; 9671500Serics lastarg = nlines; 9681500Serics if (comchar == otty.sg_erase) { 9691500Serics kill_line (); 9701500Serics prompt (filename); 9711500Serics continue; 9721500Serics } 9731500Serics switch (comchar) { 9741500Serics case ':': 9751500Serics retval = colon (filename, colonch, nlines); 9761500Serics if (retval >= 0) 9771500Serics done++; 9781500Serics break; 97924490Sbloom case 'b': 98024490Sbloom case ctrl(B): 98124490Sbloom { 98224490Sbloom register int initline; 98324490Sbloom 98424490Sbloom if (no_intty) { 98524490Sbloom write(2, &bell, 1); 98624490Sbloom return (-1); 98724490Sbloom } 98824490Sbloom 98924490Sbloom if (nlines == 0) nlines++; 99024490Sbloom 99124490Sbloom putchar ('\r'); 99224490Sbloom erase (0); 99324490Sbloom printf ("\n"); 99424490Sbloom if (clreol) 99524490Sbloom cleareol (); 99624490Sbloom printf ("...back %d page", nlines); 99724490Sbloom if (nlines > 1) 99824490Sbloom pr ("s\n"); 99924490Sbloom else 100024490Sbloom pr ("\n"); 100124490Sbloom 100224490Sbloom if (clreol) 100324490Sbloom cleareol (); 100424490Sbloom pr ("\n"); 100524490Sbloom 100624490Sbloom initline = Currline - dlines * (nlines + 1); 100724490Sbloom if (! noscroll) 100824490Sbloom --initline; 100924490Sbloom if (initline < 0) initline = 0; 101024490Sbloom Fseek(f, 0L); 101124490Sbloom Currline = 0; /* skiplns() will make Currline correct */ 101224490Sbloom skiplns(initline, f); 101324490Sbloom if (! noscroll) { 101424490Sbloom ret(dlines + 1); 101524490Sbloom } 101624490Sbloom else { 101724490Sbloom ret(dlines); 101824490Sbloom } 101924490Sbloom } 10201500Serics case ' ': 10211500Serics case 'z': 10221500Serics if (nlines == 0) nlines = dlines; 10231500Serics else if (comchar == 'z') dlines = nlines; 10241500Serics ret (nlines); 10251500Serics case 'd': 10261500Serics case ctrl(D): 10271500Serics if (nlines != 0) nscroll = nlines; 10281500Serics ret (nscroll); 10291500Serics case 'q': 10301500Serics case 'Q': 10311500Serics end_it (); 10321500Serics case 's': 10331500Serics case 'f': 10341500Serics if (nlines == 0) nlines++; 10351500Serics if (comchar == 'f') 10361500Serics nlines *= dlines; 10371500Serics putchar ('\r'); 10381500Serics erase (0); 10393594Sroot printf ("\n"); 10403594Sroot if (clreol) 10413594Sroot cleareol (); 10423594Sroot printf ("...skipping %d line", nlines); 10431500Serics if (nlines > 1) 10443594Sroot pr ("s\n"); 10451500Serics else 10463594Sroot pr ("\n"); 10473594Sroot 10483594Sroot if (clreol) 10493594Sroot cleareol (); 10503594Sroot pr ("\n"); 10513594Sroot 10521500Serics while (nlines > 0) { 10531500Serics while ((c = Getc (f)) != '\n') 10541500Serics if (c == EOF) { 10551500Serics retval = 0; 10561500Serics done++; 10571500Serics goto endsw; 10581500Serics } 10591500Serics Currline++; 10601500Serics nlines--; 10611500Serics } 10621500Serics ret (dlines); 10631500Serics case '\n': 10641500Serics if (nlines != 0) 10651500Serics dlines = nlines; 10661500Serics else 10671500Serics nlines = 1; 10681500Serics ret (nlines); 10691500Serics case '\f': 10701500Serics if (!no_intty) { 10711500Serics doclear (); 10721500Serics Fseek (f, screen_start.chrctr); 10731500Serics Currline = screen_start.line; 10741500Serics ret (dlines); 10751500Serics } 10761500Serics else { 10771500Serics write (2, &bell, 1); 10781500Serics break; 10791500Serics } 10801500Serics case '\'': 10811500Serics if (!no_intty) { 10821500Serics kill_line (); 10831500Serics pr ("\n***Back***\n\n"); 10841500Serics Fseek (f, context.chrctr); 10851500Serics Currline = context.line; 10861500Serics ret (dlines); 10871500Serics } 10881500Serics else { 10891500Serics write (2, &bell, 1); 10901500Serics break; 10911500Serics } 10921500Serics case '=': 10931500Serics kill_line (); 10941500Serics promptlen = printd (Currline); 10951500Serics fflush (stdout); 10961500Serics break; 10971500Serics case 'n': 10981500Serics lastp++; 10991500Serics case '/': 11001500Serics if (nlines == 0) nlines++; 11011500Serics kill_line (); 11021500Serics pr ("/"); 11031500Serics promptlen = 1; 11041500Serics fflush (stdout); 11051500Serics if (lastp) { 11061500Serics write (2,"\r", 1); 11071500Serics search (NULL, f, nlines); /* Use previous r.e. */ 11081500Serics } 11091500Serics else { 11101500Serics ttyin (cmdbuf, 78, '/'); 11111500Serics write (2, "\r", 1); 11121500Serics search (cmdbuf, f, nlines); 11131500Serics } 11143455Sroot ret (dlines-1); 11151500Serics case '!': 11161500Serics do_shell (filename); 11171500Serics break; 111824490Sbloom case '?': 11191500Serics case 'h': 11201500Serics if ((helpf = fopen (HELPFILE, "r")) == NULL) 11211500Serics error ("Can't open help file"); 11221500Serics if (noscroll) doclear (); 11231500Serics copy_file (helpf); 112427006Sdonn fclose (helpf); 11251500Serics prompt (filename); 11261500Serics break; 11271500Serics case 'v': /* This case should go right before default */ 11281500Serics if (!no_intty) { 11291500Serics kill_line (); 11301500Serics cmdbuf[0] = '+'; 113124490Sbloom scanstr (Currline - dlines < 0 ? 0 113224490Sbloom : Currline - (dlines + 1) / 2, &cmdbuf[1]); 11331500Serics pr ("vi "); pr (cmdbuf); putchar (' '); pr (fnames[fnum]); 11341500Serics execute (filename, VI, "vi", cmdbuf, fnames[fnum], 0); 11351500Serics break; 11361500Serics } 11371500Serics default: 113816710Sjak if (dum_opt) { 113916710Sjak kill_line (); 114016710Sjak if (Senter && Sexit) { 114116710Sjak tputs (Senter, 1, putch); 114216710Sjak promptlen = pr ("[Press 'h' for instructions.]") + (2 * soglitch); 114316710Sjak tputs (Sexit, 1, putch); 114416710Sjak } 114516710Sjak else 114616710Sjak promptlen = pr ("[Press 'h' for instructions.]"); 114716710Sjak fflush (stdout); 114816710Sjak } 114916710Sjak else 115016710Sjak write (2, &bell, 1); 11511500Serics break; 11521500Serics } 11531500Serics if (done) break; 11541500Serics } 11551500Serics putchar ('\r'); 11561500Serics endsw: 11571500Serics inwait = 0; 11581500Serics notell++; 11591500Serics if (MBIT == RAW && slow_tty) { 11601500Serics otty.sg_flags &= ~MBIT; 116116582Sleres stty(fileno(stderr), &otty); 11621500Serics } 11631500Serics return (retval); 11641500Serics } 11651500Serics 11661500Serics char ch; 11671500Serics 11681500Serics /* 11691500Serics * Execute a colon-prefixed command. 11701500Serics * Returns <0 if not a command that should cause 11711500Serics * more of the file to be printed. 11721500Serics */ 11731500Serics 11741500Serics colon (filename, cmd, nlines) 11751500Serics char *filename; 11761500Serics int cmd; 11771500Serics int nlines; 11781500Serics { 11791500Serics if (cmd == 0) 11801500Serics ch = readch (); 11811500Serics else 11821500Serics ch = cmd; 11831500Serics lastcolon = ch; 11841500Serics switch (ch) { 11851500Serics case 'f': 11861500Serics kill_line (); 11871500Serics if (!no_intty) 11881500Serics promptlen = printf ("\"%s\" line %d", fnames[fnum], Currline); 11891500Serics else 11901500Serics promptlen = printf ("[Not a file] line %d", Currline); 11911500Serics fflush (stdout); 11921500Serics return (-1); 11931500Serics case 'n': 11941500Serics if (nlines == 0) { 11951500Serics if (fnum >= nfiles - 1) 11961500Serics end_it (); 11971500Serics nlines++; 11981500Serics } 11991500Serics putchar ('\r'); 12001500Serics erase (0); 12011500Serics skipf (nlines); 12021500Serics return (0); 12031500Serics case 'p': 12041500Serics if (no_intty) { 12051500Serics write (2, &bell, 1); 12061500Serics return (-1); 12071500Serics } 12081500Serics putchar ('\r'); 12091500Serics erase (0); 12101500Serics if (nlines == 0) 12111500Serics nlines++; 12121500Serics skipf (-nlines); 12131500Serics return (0); 12141500Serics case '!': 12151500Serics do_shell (filename); 12161500Serics return (-1); 12171500Serics case 'q': 12181500Serics case 'Q': 12191500Serics end_it (); 12201500Serics default: 12211500Serics write (2, &bell, 1); 12221500Serics return (-1); 12231500Serics } 12241500Serics } 12251500Serics 12261500Serics /* 12271500Serics ** Read a decimal number from the terminal. Set cmd to the non-digit which 12281500Serics ** terminates the number. 12291500Serics */ 12301500Serics 12311500Serics number(cmd) 12321500Serics char *cmd; 12331500Serics { 12341500Serics register int i; 12351500Serics 12361500Serics i = 0; ch = otty.sg_kill; 12371500Serics for (;;) { 12381500Serics ch = readch (); 12391500Serics if (ch >= '0' && ch <= '9') 12401500Serics i = i*10 + ch - '0'; 12411500Serics else if (ch == otty.sg_kill) 12421500Serics i = 0; 12431500Serics else { 12441500Serics *cmd = ch; 12451500Serics break; 12461500Serics } 12471500Serics } 12481500Serics return (i); 12491500Serics } 12501500Serics 12511500Serics do_shell (filename) 12521500Serics char *filename; 12531500Serics { 12541500Serics char cmdbuf[80]; 12551500Serics 12561500Serics kill_line (); 12571500Serics pr ("!"); 12581500Serics fflush (stdout); 12591500Serics promptlen = 1; 12601500Serics if (lastp) 12611500Serics pr (shell_line); 12621500Serics else { 12631500Serics ttyin (cmdbuf, 78, '!'); 12641500Serics if (expand (shell_line, cmdbuf)) { 12651500Serics kill_line (); 12661500Serics promptlen = printf ("!%s", shell_line); 12671500Serics } 12681500Serics } 12691500Serics fflush (stdout); 12701500Serics write (2, "\n", 1); 12711500Serics promptlen = 0; 12721500Serics shellp = 1; 12731500Serics execute (filename, shell, shell, "-c", shell_line, 0); 12741500Serics } 12751500Serics 12761500Serics /* 12771500Serics ** Search for nth ocurrence of regular expression contained in buf in the file 12781500Serics */ 12791500Serics 12801500Serics search (buf, file, n) 12811500Serics char buf[]; 12821500Serics FILE *file; 12831500Serics register int n; 12841500Serics { 12851500Serics long startline = Ftell (file); 12861500Serics register long line1 = startline; 12871500Serics register long line2 = startline; 12881500Serics register long line3 = startline; 12891500Serics register int lncount; 12901500Serics int saveln, rv, re_exec(); 12911500Serics char *s, *re_comp(); 12921500Serics 12931500Serics context.line = saveln = Currline; 12941500Serics context.chrctr = startline; 12951500Serics lncount = 0; 12961500Serics if ((s = re_comp (buf)) != 0) 12971500Serics error (s); 12981500Serics while (!feof (file)) { 12991500Serics line3 = line2; 13001500Serics line2 = line1; 13011500Serics line1 = Ftell (file); 13021500Serics rdline (file); 13031500Serics lncount++; 13041500Serics if ((rv = re_exec (Line)) == 1) 13051500Serics if (--n == 0) { 13061500Serics if (lncount > 3 || (lncount > 1 && no_intty)) 13073455Sroot { 13083455Sroot pr ("\n"); 13093594Sroot if (clreol) 13103594Sroot cleareol (); 13113455Sroot pr("...skipping\n"); 13123455Sroot } 13131500Serics if (!no_intty) { 13141500Serics Currline -= (lncount >= 3 ? 3 : lncount); 13151500Serics Fseek (file, line3); 13163594Sroot if (noscroll) 13173594Sroot if (clreol) { 13183594Sroot home (); 13193594Sroot cleareol (); 132016582Sleres } 13213594Sroot else 13223594Sroot doclear (); 13231500Serics } 13241500Serics else { 13251500Serics kill_line (); 13263594Sroot if (noscroll) 13273594Sroot if (clreol) { 132816582Sleres home (); 13293594Sroot cleareol (); 133016582Sleres } 13313594Sroot else 13323594Sroot doclear (); 13331500Serics pr (Line); 13341500Serics putchar ('\n'); 13351500Serics } 13361500Serics break; 13371500Serics } 13381500Serics else if (rv == -1) 13391500Serics error ("Regular expression botch"); 13401500Serics } 13411500Serics if (feof (file)) { 13421500Serics if (!no_intty) { 13431500Serics file->_flag &= ~_IOEOF; /* why doesn't fseek do this ??!!??! */ 13441500Serics Currline = saveln; 13451500Serics Fseek (file, startline); 13461500Serics } 13471500Serics else { 13481500Serics pr ("\nPattern not found\n"); 13491500Serics end_it (); 13501500Serics } 13511500Serics error ("Pattern not found"); 13521500Serics } 13531500Serics } 13541500Serics 13551500Serics execute (filename, cmd, args) 13561500Serics char *filename; 13571500Serics char *cmd, *args; 13581500Serics { 13591500Serics int id; 136016710Sjak int n; 13611500Serics 13621500Serics fflush (stdout); 13631500Serics reset_tty (); 136416710Sjak for (n = 10; (id = fork ()) < 0 && n > 0; n--) 13651500Serics sleep (5); 13661500Serics if (id == 0) { 136716710Sjak if (!isatty(0)) { 136816710Sjak close(0); 136916710Sjak open("/dev/tty", 0); 137016710Sjak } 13711500Serics execv (cmd, &args); 13721500Serics write (2, "exec failed\n", 12); 13731500Serics exit (1); 13741500Serics } 137516710Sjak if (id > 0) { 137616710Sjak signal (SIGINT, SIG_IGN); 137716710Sjak signal (SIGQUIT, SIG_IGN); 137816710Sjak if (catch_susp) 137916710Sjak signal(SIGTSTP, SIG_DFL); 138016710Sjak while (wait(0) > 0); 138116710Sjak signal (SIGINT, end_it); 138216710Sjak signal (SIGQUIT, onquit); 138316710Sjak if (catch_susp) 138416710Sjak signal(SIGTSTP, onsusp); 138516710Sjak } else 138616710Sjak write(2, "can't fork\n", 11); 13871500Serics set_tty (); 13881500Serics pr ("------------------------\n"); 13891500Serics prompt (filename); 13901500Serics } 13911500Serics /* 13921500Serics ** Skip n lines in the file f 13931500Serics */ 13941500Serics 13951500Serics skiplns (n, f) 13961500Serics register int n; 13971500Serics register FILE *f; 13981500Serics { 13991500Serics register char c; 14001500Serics 14011500Serics while (n > 0) { 14021500Serics while ((c = Getc (f)) != '\n') 14031500Serics if (c == EOF) 14041500Serics return; 14051500Serics n--; 14061500Serics Currline++; 14071500Serics } 14081500Serics } 14091500Serics 14101500Serics /* 14111500Serics ** Skip nskip files in the file list (from the command line). Nskip may be 14121500Serics ** negative. 14131500Serics */ 14141500Serics 14151500Serics skipf (nskip) 14161500Serics register int nskip; 14171500Serics { 14181500Serics if (nskip == 0) return; 14191500Serics if (nskip > 0) { 14201500Serics if (fnum + nskip > nfiles - 1) 14211500Serics nskip = nfiles - fnum - 1; 14221500Serics } 14231500Serics else if (within) 14241500Serics ++fnum; 14251500Serics fnum += nskip; 14261500Serics if (fnum < 0) 14271500Serics fnum = 0; 14283594Sroot pr ("\n...Skipping "); 14293455Sroot pr ("\n"); 14303594Sroot if (clreol) 14313594Sroot cleareol (); 14323455Sroot pr ("...Skipping "); 14331500Serics pr (nskip > 0 ? "to file " : "back to file "); 14341500Serics pr (fnames[fnum]); 14353455Sroot pr ("\n"); 14363594Sroot if (clreol) 14373594Sroot cleareol (); 14383455Sroot pr ("\n"); 14391500Serics --fnum; 14401500Serics } 14411500Serics 14421500Serics /*----------------------------- Terminal I/O -------------------------------*/ 14431500Serics 14441500Serics initterm () 14451500Serics { 14461500Serics char buf[TBUFSIZ]; 144717195Sralph static char clearbuf[TBUFSIZ]; 14481500Serics char *clearptr, *padstr; 14491500Serics int ldisc; 145017592Sleres int lmode; 145110823Ssam char *term; 145216582Sleres int tgrp; 145318030Sbloom struct winsize win; 14541500Serics 145516582Sleres retry: 145616582Sleres if (!(no_tty = gtty(fileno(stdout), &otty))) { 145717592Sleres if (ioctl(fileno(stdout), TIOCLGET, &lmode) < 0) { 145817592Sleres perror("TIOCLGET"); 145917592Sleres exit(1); 146017592Sleres } 146117592Sleres docrterase = ((lmode & LCRTERA) != 0); 146217592Sleres docrtkill = ((lmode & LCRTKIL) != 0); 146316582Sleres /* 146417592Sleres * Wait until we're in the foreground before we save the 146517592Sleres * the terminal modes. 146616582Sleres */ 146716582Sleres if (ioctl(fileno(stdout), TIOCGPGRP, &tgrp) < 0) { 146817592Sleres perror("TIOCGPGRP"); 146916582Sleres exit(1); 147016582Sleres } 147116582Sleres if (tgrp != getpgrp(0)) { 147216582Sleres kill(0, SIGTTOU); 147316582Sleres goto retry; 147416582Sleres } 147513830Skre if ((term = getenv("TERM")) == 0 || tgetent(buf, term) <= 0) { 14763594Sroot dumb++; ul_opt = 0; 14771500Serics } 14781500Serics else { 147918030Sbloom if (ioctl(fileno(stdout), TIOCGWINSZ, &win) < 0) { 148018030Sbloom Lpp = tgetnum("li"); 148118030Sbloom Mcol = tgetnum("co"); 148218030Sbloom } else { 148318030Sbloom if ((Lpp = win.ws_row) == 0) 148418030Sbloom Lpp = tgetnum("li"); 148518030Sbloom if ((Mcol = win.ws_col) == 0) 148618030Sbloom Mcol = tgetnum("co"); 148718030Sbloom } 148818030Sbloom if ((Lpp <= 0) || tgetflag("hc")) { 14891500Serics hard++; /* Hard copy terminal */ 14901500Serics Lpp = 24; 14911500Serics } 149229907Smckusick if (tgetflag("xn")) 149329907Smckusick eatnl++; /* Eat newline at last column + 1; dec, concept */ 149418030Sbloom if (Mcol <= 0) 149518030Sbloom Mcol = 80; 149618030Sbloom 14971500Serics if (tailequ (fnames[0], "page") || !hard && tgetflag("ns")) 14981500Serics noscroll++; 14991500Serics Wrap = tgetflag("am"); 15001500Serics bad_so = tgetflag ("xs"); 15011500Serics clearptr = clearbuf; 15021500Serics eraseln = tgetstr("ce",&clearptr); 15031500Serics Clear = tgetstr("cl", &clearptr); 15041500Serics Senter = tgetstr("so", &clearptr); 15051500Serics Sexit = tgetstr("se", &clearptr); 150616710Sjak if ((soglitch = tgetnum("sg")) < 0) 150716710Sjak soglitch = 0; 15083594Sroot 15093594Sroot /* 15103594Sroot * Set up for underlining: some terminals don't need it; 15113594Sroot * others have start/stop sequences, still others have an 15123594Sroot * underline char sequence which is assumed to move the 15133594Sroot * cursor forward one character. If underline sequence 15143594Sroot * isn't available, settle for standout sequence. 15153594Sroot */ 15163594Sroot 15173594Sroot if (tgetflag("ul") || tgetflag("os")) 15183594Sroot ul_opt = 0; 15193594Sroot if ((chUL = tgetstr("uc", &clearptr)) == NULL ) 15203594Sroot chUL = ""; 152116710Sjak if (((ULenter = tgetstr("us", &clearptr)) == NULL || 152216710Sjak (ULexit = tgetstr("ue", &clearptr)) == NULL) && !*chUL) { 152316710Sjak if ((ULenter = Senter) == NULL || (ULexit = Sexit) == NULL) { 152416710Sjak ULenter = ""; 152516710Sjak ULexit = ""; 152616710Sjak } else 152716710Sjak ulglitch = soglitch; 152816710Sjak } else { 152916710Sjak if ((ulglitch = tgetnum("ug")) < 0) 153016710Sjak ulglitch = 0; 153116710Sjak } 153216582Sleres 15331500Serics if (padstr = tgetstr("pc", &clearptr)) 15341500Serics PC = *padstr; 15353455Sroot Home = tgetstr("ho",&clearptr); 153613536Ssam if (Home == 0 || *Home == '\0') 15373455Sroot { 15383594Sroot if ((cursorm = tgetstr("cm", &clearptr)) != NULL) { 15393594Sroot strcpy(cursorhome, tgoto(cursorm, 0, 0)); 15403455Sroot Home = cursorhome; 15413455Sroot } 15423455Sroot } 15433594Sroot EodClr = tgetstr("cd", &clearptr); 154425540Smckusick if ((chBS = tgetstr("bc", &clearptr)) == NULL) 154525540Smckusick chBS = "\b"; 154625540Smckusick 15471500Serics } 15481500Serics if ((shell = getenv("SHELL")) == NULL) 15491500Serics shell = "/bin/sh"; 15501500Serics } 155116582Sleres no_intty = gtty(fileno(stdin), &otty); 155216582Sleres gtty(fileno(stderr), &otty); 155313830Skre savetty = otty; 15541500Serics ospeed = otty.sg_ospeed; 15551500Serics slow_tty = ospeed < B1200; 155627006Sdonn hardtabs = (otty.sg_flags & TBDELAY) != XTABS; 15571500Serics if (!no_tty) { 15581500Serics otty.sg_flags &= ~ECHO; 15591500Serics if (MBIT == CBREAK || !slow_tty) 15601500Serics otty.sg_flags |= MBIT; 15611500Serics } 15621500Serics } 15631500Serics 15641500Serics readch () 15651500Serics { 15661500Serics char ch; 15671500Serics extern int errno; 15681500Serics 15691500Serics if (read (2, &ch, 1) <= 0) 15701500Serics if (errno != EINTR) 15711500Serics exit(0); 15721500Serics else 15731500Serics ch = otty.sg_kill; 15741500Serics return (ch); 15751500Serics } 15761500Serics 15771500Serics static char BS = '\b'; 157817592Sleres static char *BSB = "\b \b"; 15791500Serics static char CARAT = '^'; 158017592Sleres #define ERASEONECHAR \ 158117592Sleres if (docrterase) \ 158217592Sleres write (2, BSB, sizeof(BSB)); \ 158317592Sleres else \ 158417592Sleres write (2, &BS, sizeof(BS)); 15851500Serics 15861500Serics ttyin (buf, nmax, pchar) 15871500Serics char buf[]; 15881500Serics register int nmax; 15891500Serics char pchar; 15901500Serics { 15911500Serics register char *sptr; 15921500Serics register char ch; 15931500Serics register int slash = 0; 15941500Serics int maxlen; 15951500Serics char cbuf; 15961500Serics 15971500Serics sptr = buf; 15981500Serics maxlen = 0; 15991500Serics while (sptr - buf < nmax) { 16001500Serics if (promptlen > maxlen) maxlen = promptlen; 16011500Serics ch = readch (); 16021500Serics if (ch == '\\') { 16031500Serics slash++; 16041500Serics } 16051500Serics else if ((ch == otty.sg_erase) && !slash) { 16061500Serics if (sptr > buf) { 16071500Serics --promptlen; 160817592Sleres ERASEONECHAR 16091500Serics --sptr; 16101500Serics if ((*sptr < ' ' && *sptr != '\n') || *sptr == RUBOUT) { 16111500Serics --promptlen; 161217592Sleres ERASEONECHAR 16131500Serics } 16141500Serics continue; 16151500Serics } 16161500Serics else { 16171500Serics if (!eraseln) promptlen = maxlen; 16181500Serics longjmp (restore, 1); 16191500Serics } 16201500Serics } 16211500Serics else if ((ch == otty.sg_kill) && !slash) { 16221500Serics if (hard) { 16231500Serics show (ch); 16241500Serics putchar ('\n'); 16251500Serics putchar (pchar); 16261500Serics } 16271500Serics else { 16281500Serics putchar ('\r'); 16291500Serics putchar (pchar); 16301500Serics if (eraseln) 16311500Serics erase (1); 163217592Sleres else if (docrtkill) 163317592Sleres while (promptlen-- > 1) 163417592Sleres write (2, BSB, sizeof(BSB)); 16351500Serics promptlen = 1; 16361500Serics } 16371500Serics sptr = buf; 16381500Serics fflush (stdout); 16391500Serics continue; 16401500Serics } 16411500Serics if (slash && (ch == otty.sg_kill || ch == otty.sg_erase)) { 164217592Sleres ERASEONECHAR 16431500Serics --sptr; 16441500Serics } 16451500Serics if (ch != '\\') 16461500Serics slash = 0; 16471500Serics *sptr++ = ch; 16481500Serics if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) { 16491500Serics ch += ch == RUBOUT ? -0100 : 0100; 16501500Serics write (2, &CARAT, 1); 16511500Serics promptlen++; 16521500Serics } 16531500Serics cbuf = ch; 16541500Serics if (ch != '\n' && ch != ESC) { 16551500Serics write (2, &cbuf, 1); 16561500Serics promptlen++; 16571500Serics } 16581500Serics else 16591500Serics break; 16601500Serics } 16611500Serics *--sptr = '\0'; 16621500Serics if (!eraseln) promptlen = maxlen; 16631500Serics if (sptr - buf >= nmax - 1) 16641500Serics error ("Line too long"); 16651500Serics } 16661500Serics 16671500Serics expand (outbuf, inbuf) 16681500Serics char *outbuf; 16691500Serics char *inbuf; 16701500Serics { 16711500Serics register char *instr; 16721500Serics register char *outstr; 16731500Serics register char ch; 16741500Serics char temp[200]; 16751500Serics int changed = 0; 16761500Serics 16771500Serics instr = inbuf; 16781500Serics outstr = temp; 16791500Serics while ((ch = *instr++) != '\0') 16801500Serics switch (ch) { 16811500Serics case '%': 16821500Serics if (!no_intty) { 16831500Serics strcpy (outstr, fnames[fnum]); 16841500Serics outstr += strlen (fnames[fnum]); 16851500Serics changed++; 16861500Serics } 16871500Serics else 16881500Serics *outstr++ = ch; 16891500Serics break; 16901500Serics case '!': 16911500Serics if (!shellp) 16921500Serics error ("No previous command to substitute for"); 16931500Serics strcpy (outstr, shell_line); 16941500Serics outstr += strlen (shell_line); 16951500Serics changed++; 16961500Serics break; 16971500Serics case '\\': 16981500Serics if (*instr == '%' || *instr == '!') { 16991500Serics *outstr++ = *instr++; 17001500Serics break; 17011500Serics } 17021500Serics default: 17031500Serics *outstr++ = ch; 17041500Serics } 17051500Serics *outstr++ = '\0'; 17061500Serics strcpy (outbuf, temp); 17071500Serics return (changed); 17081500Serics } 17091500Serics 17101500Serics show (ch) 17111500Serics register char ch; 17121500Serics { 17131500Serics char cbuf; 17141500Serics 17151500Serics if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) { 17161500Serics ch += ch == RUBOUT ? -0100 : 0100; 17171500Serics write (2, &CARAT, 1); 17181500Serics promptlen++; 17191500Serics } 17201500Serics cbuf = ch; 17211500Serics write (2, &cbuf, 1); 17221500Serics promptlen++; 17231500Serics } 17241500Serics 17251500Serics error (mess) 17261500Serics char *mess; 17271500Serics { 17283594Sroot if (clreol) 17293594Sroot cleareol (); 17303594Sroot else 17313594Sroot kill_line (); 17321500Serics promptlen += strlen (mess); 17331500Serics if (Senter && Sexit) { 17341500Serics tputs (Senter, 1, putch); 17351500Serics pr(mess); 17361500Serics tputs (Sexit, 1, putch); 17371500Serics } 17381500Serics else 17391500Serics pr (mess); 17401500Serics fflush(stdout); 17411500Serics errors++; 17421500Serics longjmp (restore, 1); 17431500Serics } 17441500Serics 17451500Serics 17461500Serics set_tty () 17471500Serics { 17481500Serics otty.sg_flags |= MBIT; 17491500Serics otty.sg_flags &= ~ECHO; 175016582Sleres stty(fileno(stderr), &otty); 17511500Serics } 17521500Serics 17531500Serics reset_tty () 17541500Serics { 1755*31033Sbostic if (no_tty) 1756*31033Sbostic return; 175716710Sjak if (pstate) { 175816710Sjak tputs(ULexit, 1, putch); 175916710Sjak fflush(stdout); 176016710Sjak pstate = 0; 176116710Sjak } 17621500Serics otty.sg_flags |= ECHO; 17631500Serics otty.sg_flags &= ~MBIT; 176416582Sleres stty(fileno(stderr), &savetty); 17651500Serics } 17661500Serics 17671500Serics rdline (f) 17681500Serics register FILE *f; 17691500Serics { 17701500Serics register char c; 17711500Serics register char *p; 17721500Serics 17731500Serics p = Line; 17741500Serics while ((c = Getc (f)) != '\n' && c != EOF && p - Line < LINSIZ - 1) 17751500Serics *p++ = c; 17761500Serics if (c == '\n') 17771500Serics Currline++; 17781500Serics *p = '\0'; 17791500Serics } 17801500Serics 17811500Serics /* Come here when we get a suspend signal from the terminal */ 17821500Serics 17831500Serics onsusp () 17841500Serics { 178514861Skarels /* ignore SIGTTOU so we don't get stopped if csh grabs the tty */ 178614861Skarels signal(SIGTTOU, SIG_IGN); 17871500Serics reset_tty (); 17881500Serics fflush (stdout); 178914861Skarels signal(SIGTTOU, SIG_DFL); 17901500Serics /* Send the TSTP signal to suspend our process group */ 179113289Ssam signal(SIGTSTP, SIG_DFL); 179213289Ssam sigsetmask(0); 17931500Serics kill (0, SIGTSTP); 17941500Serics /* Pause for station break */ 17951500Serics 17961500Serics /* We're back */ 17971500Serics signal (SIGTSTP, onsusp); 17981500Serics set_tty (); 17991500Serics if (inwait) 18001500Serics longjmp (restore); 18011500Serics } 1802