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*31125Smckusick static char sccsid[] = "@(#)more.c 5.12 (Berkeley) 05/13/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); 383*31125Smckusick if (ftell(f) < sizeof magic) 384*31125Smckusick rewind(f); /* reset file position */ 385*31125Smckusick else 386*31125Smckusick fseek(f, -sizeof (magic), L_INCR); /* reset file position */ 38729906Smckusick return (magic == 0405 || magic == OMAGIC || magic == NMAGIC || 38829906Smckusick magic == 0411 || magic == ZMAGIC || magic == 0177545); 38929906Smckusick } 39029906Smckusick 39129906Smckusick /* 3921500Serics ** A real function, for the tputs routine in termlib 3931500Serics */ 3941500Serics 3951500Serics putch (ch) 3961500Serics char ch; 3971500Serics { 3981500Serics putchar (ch); 3991500Serics } 4001500Serics 4011500Serics /* 4021500Serics ** Print out the contents of the file f, one screenful at a time. 4031500Serics */ 4041500Serics 4051500Serics #define STOP -10 4061500Serics 4071500Serics screen (f, num_lines) 4081500Serics register FILE *f; 4091500Serics register int num_lines; 4101500Serics { 4111500Serics register int c; 4121500Serics register int nchars; 4133594Sroot int length; /* length of current line */ 4143594Sroot static int prev_len = 1; /* length of previous line */ 4151500Serics 4161500Serics for (;;) { 4171500Serics while (num_lines > 0 && !Pause) { 4181500Serics if ((nchars = getline (f, &length)) == EOF) 4193455Sroot { 4203594Sroot if (clreol) 4213594Sroot clreos(); 4221500Serics return; 4233455Sroot } 4243594Sroot if (ssp_opt && length == 0 && prev_len == 0) 4253594Sroot continue; 4263594Sroot prev_len = length; 4271500Serics if (bad_so || (Senter && *Senter == ' ') && promptlen > 0) 4281500Serics erase (0); 4293594Sroot /* must clear before drawing line since tabs on some terminals 4303594Sroot * do not erase what they tab over. 4313594Sroot */ 4323594Sroot if (clreol) 4333594Sroot cleareol (); 4341500Serics prbuf (Line, length); 4351500Serics if (nchars < promptlen) 4361500Serics erase (nchars); /* erase () sets promptlen to 0 */ 4371500Serics else promptlen = 0; 4383594Sroot /* is this needed? 4393594Sroot * if (clreol) 4403594Sroot * cleareol(); /* must clear again in case we wrapped * 4413594Sroot */ 4421500Serics if (nchars < Mcol || !fold_opt) 44316710Sjak prbuf("\n", 1); /* will turn off UL if necessary */ 4441500Serics if (nchars == STOP) 4451500Serics break; 4461500Serics num_lines--; 4471500Serics } 44816710Sjak if (pstate) { 44916710Sjak tputs(ULexit, 1, putch); 45016710Sjak pstate = 0; 45116710Sjak } 4521500Serics fflush(stdout); 4531500Serics if ((c = Getc(f)) == EOF) 4543455Sroot { 4553594Sroot if (clreol) 4563594Sroot clreos (); 4571500Serics return; 4583455Sroot } 4593455Sroot 4603594Sroot if (Pause && clreol) 4613594Sroot clreos (); 4621500Serics Ungetc (c, f); 4631500Serics setjmp (restore); 4641500Serics Pause = 0; startup = 0; 4651500Serics if ((num_lines = command (NULL, f)) == 0) 4661500Serics return; 4671500Serics if (hard && promptlen > 0) 4681500Serics erase (0); 46911123Slayer if (noscroll && num_lines >= dlines) 47016582Sleres { 4713594Sroot if (clreol) 4723594Sroot home(); 4733594Sroot else 4743594Sroot doclear (); 4753455Sroot } 4761500Serics screen_start.line = Currline; 4771500Serics screen_start.chrctr = Ftell (f); 4781500Serics } 4791500Serics } 4801500Serics 4811500Serics /* 4821500Serics ** Come here if a quit signal is received 4831500Serics */ 4841500Serics 4851500Serics onquit() 4861500Serics { 4871500Serics signal(SIGQUIT, SIG_IGN); 4881500Serics if (!inwait) { 4891500Serics putchar ('\n'); 4901500Serics if (!startup) { 4911500Serics signal(SIGQUIT, onquit); 4921500Serics longjmp (restore, 1); 4931500Serics } 4941500Serics else 4951500Serics Pause++; 4961500Serics } 4971500Serics else if (!dum_opt && notell) { 4981500Serics write (2, "[Use q or Q to quit]", 20); 4991500Serics promptlen += 20; 5001500Serics notell = 0; 5011500Serics } 5021500Serics signal(SIGQUIT, onquit); 5031500Serics } 5041500Serics 5051500Serics /* 50627006Sdonn ** Come here if a signal for a window size change is received 50727006Sdonn */ 50827006Sdonn 50927006Sdonn chgwinsz() 51027006Sdonn { 51127006Sdonn struct winsize win; 51227006Sdonn 51327006Sdonn (void) signal(SIGWINCH, SIG_IGN); 51427006Sdonn if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) { 51527006Sdonn if (win.ws_row != 0) { 51627006Sdonn Lpp = win.ws_row; 51727006Sdonn nscroll = Lpp/2 - 1; 51827006Sdonn if (nscroll <= 0) 51927006Sdonn nscroll = 1; 52027006Sdonn dlines = Lpp - (noscroll ? 1 : 2); 52127006Sdonn } 52227006Sdonn if (win.ws_col != 0) 52327006Sdonn Mcol = win.ws_col; 52427006Sdonn } 52527006Sdonn (void) signal(SIGWINCH, chgwinsz); 52627006Sdonn } 52727006Sdonn 52827006Sdonn /* 5291500Serics ** Clean up terminal state and exit. Also come here if interrupt signal received 5301500Serics */ 5311500Serics 5321500Serics end_it () 5331500Serics { 5341500Serics 5351500Serics reset_tty (); 5363594Sroot if (clreol) { 5373594Sroot putchar ('\r'); 5383594Sroot clreos (); 5393594Sroot fflush (stdout); 5403594Sroot } 5413455Sroot else if (!clreol && (promptlen > 0)) { 5421500Serics kill_line (); 5431500Serics fflush (stdout); 5441500Serics } 5451500Serics else 5461500Serics write (2, "\n", 1); 5471500Serics _exit(0); 5481500Serics } 5491500Serics 5501500Serics copy_file(f) 5511500Serics register FILE *f; 5521500Serics { 5531500Serics register int c; 5541500Serics 5551500Serics while ((c = getc(f)) != EOF) 5561500Serics putchar(c); 5571500Serics } 5581500Serics 5591500Serics /* Simplified printf function */ 5601500Serics 5611500Serics printf (fmt, args) 5621500Serics register char *fmt; 5631500Serics int args; 5641500Serics { 5651500Serics register int *argp; 5661500Serics register char ch; 5671500Serics register int ccount; 5681500Serics 5691500Serics ccount = 0; 5701500Serics argp = &args; 5711500Serics while (*fmt) { 5721500Serics while ((ch = *fmt++) != '%') { 5731500Serics if (ch == '\0') 5741500Serics return (ccount); 5751500Serics ccount++; 5761500Serics putchar (ch); 5771500Serics } 5781500Serics switch (*fmt++) { 5791500Serics case 'd': 5801500Serics ccount += printd (*argp); 5811500Serics break; 5821500Serics case 's': 5831500Serics ccount += pr ((char *)*argp); 5841500Serics break; 5851500Serics case '%': 5861500Serics ccount++; 5871500Serics argp--; 5881500Serics putchar ('%'); 5891500Serics break; 5901500Serics case '0': 5911500Serics return (ccount); 5921500Serics default: 5931500Serics break; 5941500Serics } 5951500Serics ++argp; 5961500Serics } 5971500Serics return (ccount); 5981500Serics 5991500Serics } 6001500Serics 6011500Serics /* 6021500Serics ** Print an integer as a string of decimal digits, 6031500Serics ** returning the length of the print representation. 6041500Serics */ 6051500Serics 6061500Serics printd (n) 6071500Serics int n; 6081500Serics { 6091500Serics int a, nchars; 6101500Serics 6111500Serics if (a = n/10) 6121500Serics nchars = 1 + printd(a); 6131500Serics else 6141500Serics nchars = 1; 6151500Serics putchar (n % 10 + '0'); 6161500Serics return (nchars); 6171500Serics } 6181500Serics 6191500Serics /* Put the print representation of an integer into a string */ 6201500Serics static char *sptr; 6211500Serics 6221500Serics scanstr (n, str) 6231500Serics int n; 6241500Serics char *str; 6251500Serics { 6261500Serics sptr = str; 62711604Slayer Sprintf (n); 6281500Serics *sptr = '\0'; 6291500Serics } 6301500Serics 63111604Slayer Sprintf (n) 6321500Serics { 6331500Serics int a; 6341500Serics 6351500Serics if (a = n/10) 63611604Slayer Sprintf (a); 6371500Serics *sptr++ = n % 10 + '0'; 6381500Serics } 6391500Serics 6401500Serics static char bell = ctrl(G); 6411500Serics 6421500Serics strlen (s) 6431500Serics char *s; 6441500Serics { 6451500Serics register char *p; 6461500Serics 6471500Serics p = s; 6481500Serics while (*p++) 6491500Serics ; 6501500Serics return (p - s - 1); 6511500Serics } 6521500Serics 6531500Serics /* See whether the last component of the path name "path" is equal to the 6541500Serics ** string "string" 6551500Serics */ 6561500Serics 6571500Serics tailequ (path, string) 6581500Serics char *path; 6591500Serics register char *string; 6601500Serics { 6611500Serics register char *tail; 6621500Serics 6631500Serics tail = path + strlen(path); 6641500Serics while (tail >= path) 6651500Serics if (*(--tail) == '/') 6661500Serics break; 6671500Serics ++tail; 6681500Serics while (*tail++ == *string++) 6691500Serics if (*tail == '\0') 6701500Serics return(1); 6711500Serics return(0); 6721500Serics } 6731500Serics 6741500Serics prompt (filename) 6751500Serics char *filename; 6761500Serics { 6773594Sroot if (clreol) 6783594Sroot cleareol (); 6793455Sroot else if (promptlen > 0) 6801500Serics kill_line (); 6811500Serics if (!hard) { 6821500Serics promptlen = 8; 68316710Sjak if (Senter && Sexit) { 6841500Serics tputs (Senter, 1, putch); 68516710Sjak promptlen += (2 * soglitch); 68616710Sjak } 6873594Sroot if (clreol) 6883594Sroot cleareol (); 6891500Serics pr("--More--"); 6901500Serics if (filename != NULL) { 6911500Serics promptlen += printf ("(Next file: %s)", filename); 6921500Serics } 6931500Serics else if (!no_intty) { 6941500Serics promptlen += printf ("(%d%%)", (int)((file_pos * 100) / file_size)); 6951500Serics } 6961500Serics if (dum_opt) { 69716710Sjak promptlen += pr("[Press space to continue, 'q' to quit.]"); 6981500Serics } 6991500Serics if (Senter && Sexit) 7001500Serics tputs (Sexit, 1, putch); 7013594Sroot if (clreol) 7023594Sroot clreos (); 7031500Serics fflush(stdout); 7041500Serics } 7051500Serics else 7061500Serics write (2, &bell, 1); 7071500Serics inwait++; 7081500Serics } 7091500Serics 7101500Serics /* 7111500Serics ** Get a logical line 7121500Serics */ 7131500Serics 7141500Serics getline(f, length) 7151500Serics register FILE *f; 7161500Serics int *length; 7171500Serics { 7181500Serics register int c; 7191500Serics register char *p; 7201500Serics register int column; 7211500Serics static int colflg; 7221500Serics 7231500Serics p = Line; 7241500Serics column = 0; 7251500Serics c = Getc (f); 7261500Serics if (colflg && c == '\n') { 7271500Serics Currline++; 7281500Serics c = Getc (f); 7291500Serics } 7301500Serics while (p < &Line[LINSIZ - 1]) { 7311500Serics if (c == EOF) { 7321500Serics if (p > Line) { 7331500Serics *p = '\0'; 7341500Serics *length = p - Line; 7351500Serics return (column); 7361500Serics } 7371500Serics *length = p - Line; 7381500Serics return (EOF); 7391500Serics } 7401500Serics if (c == '\n') { 7411500Serics Currline++; 7421500Serics break; 7431500Serics } 7441500Serics *p++ = c; 7451500Serics if (c == '\t') 74629907Smckusick if (!hardtabs || column < promptlen && !hard) { 74729907Smckusick if (hardtabs && eraseln && !dumb) { 7481500Serics column = 1 + (column | 7); 7491500Serics tputs (eraseln, 1, putch); 7501500Serics promptlen = 0; 7511500Serics } 7521500Serics else { 75329907Smckusick for (--p; p < &Line[LINSIZ - 1];) { 7541500Serics *p++ = ' '; 75529907Smckusick if ((++column & 7) == 0) 75629907Smckusick break; 7571500Serics } 7581500Serics if (column >= promptlen) promptlen = 0; 7591500Serics } 7601500Serics } 7611500Serics else 7621500Serics column = 1 + (column | 7); 7639627Ssklower else if (c == '\b' && column > 0) 7641500Serics column--; 7651500Serics else if (c == '\r') 7661500Serics column = 0; 7671500Serics else if (c == '\f' && stop_opt) { 7681500Serics p[-1] = '^'; 7691500Serics *p++ = 'L'; 7701500Serics column += 2; 7711500Serics Pause++; 7721500Serics } 7731500Serics else if (c == EOF) { 7741500Serics *length = p - Line; 7751500Serics return (column); 7761500Serics } 7771500Serics else if (c >= ' ' && c != RUBOUT) 7781500Serics column++; 7791500Serics if (column >= Mcol && fold_opt) break; 7801500Serics c = Getc (f); 7811500Serics } 7821500Serics if (column >= Mcol && Mcol > 0) { 7831500Serics if (!Wrap) { 7841500Serics *p++ = '\n'; 7851500Serics } 7861500Serics } 7871500Serics colflg = column == Mcol && fold_opt; 78829907Smckusick if (colflg && eatnl && Wrap) { 78929907Smckusick *p++ = '\n'; /* simulate normal wrap */ 79029907Smckusick } 7911500Serics *length = p - Line; 7921500Serics *p = 0; 7931500Serics return (column); 7941500Serics } 7951500Serics 7961500Serics /* 7971500Serics ** Erase the rest of the prompt, assuming we are starting at column col. 7981500Serics */ 7991500Serics 8001500Serics erase (col) 8011500Serics register int col; 8021500Serics { 8031500Serics 8041500Serics if (promptlen == 0) 8051500Serics return; 8061500Serics if (hard) { 8071500Serics putchar ('\n'); 8081500Serics } 8091500Serics else { 8101500Serics if (col == 0) 8111500Serics putchar ('\r'); 8121500Serics if (!dumb && eraseln) 8131500Serics tputs (eraseln, 1, putch); 8141500Serics else 8151500Serics for (col = promptlen - col; col > 0; col--) 8161500Serics putchar (' '); 8171500Serics } 8181500Serics promptlen = 0; 8191500Serics } 8201500Serics 8211500Serics /* 8221500Serics ** Erase the current line entirely 8231500Serics */ 8241500Serics 8251500Serics kill_line () 8261500Serics { 8271500Serics erase (0); 8281500Serics if (!eraseln || dumb) putchar ('\r'); 8291500Serics } 8301500Serics 8311500Serics /* 8323455Sroot * force clear to end of line 8333455Sroot */ 8343455Sroot cleareol() 8353455Sroot { 8363594Sroot tputs(eraseln, 1, putch); 8373455Sroot } 8383455Sroot 8393594Sroot clreos() 8403455Sroot { 8413594Sroot tputs(EodClr, 1, putch); 8423455Sroot } 8433455Sroot 8443455Sroot /* 8451500Serics ** Print string and return number of characters 8461500Serics */ 8471500Serics 8481500Serics pr(s1) 8491500Serics char *s1; 8501500Serics { 8511500Serics register char *s; 8521500Serics register char c; 8531500Serics 8541500Serics for (s = s1; c = *s++; ) 8551500Serics putchar(c); 8561500Serics return (s - s1 - 1); 8571500Serics } 8581500Serics 8591500Serics 8601500Serics /* Print a buffer of n characters */ 8611500Serics 8621500Serics prbuf (s, n) 8631500Serics register char *s; 8641500Serics register int n; 8651500Serics { 86616710Sjak register char c; /* next output character */ 8673594Sroot register int state; /* next output char's UL state */ 86816710Sjak #define wouldul(s,n) ((n) >= 2 && (((s)[0] == '_' && (s)[1] == '\b') || ((s)[1] == '\b' && (s)[2] == '_'))) 8693594Sroot 8703594Sroot while (--n >= 0) 8713594Sroot if (!ul_opt) 8723594Sroot putchar (*s++); 8733594Sroot else { 87416710Sjak if (*s == ' ' && pstate == 0 && ulglitch && wouldul(s+1, n-1)) { 87516710Sjak s++; 87616710Sjak continue; 87716710Sjak } 87816710Sjak if (state = wouldul(s, n)) { 87916710Sjak c = (*s == '_')? s[2] : *s ; 8803594Sroot n -= 2; 88116710Sjak s += 3; 88216710Sjak } else 8833594Sroot c = *s++; 88416710Sjak if (state != pstate) { 88516710Sjak if (c == ' ' && state == 0 && ulglitch && wouldul(s, n-1)) 88616710Sjak state = 1; 88716710Sjak else 88816710Sjak tputs(state ? ULenter : ULexit, 1, putch); 8893594Sroot } 89016710Sjak if (c != ' ' || pstate == 0 || state != 0 || ulglitch == 0) 89116710Sjak putchar(c); 8923594Sroot if (state && *chUL) { 8933594Sroot pr(chBS); 8943594Sroot tputs(chUL, 1, putch); 8953594Sroot } 89616710Sjak pstate = state; 8973594Sroot } 8981500Serics } 8991500Serics 9001500Serics /* 9011500Serics ** Clear the screen 9021500Serics */ 9031500Serics 9041500Serics doclear() 9051500Serics { 9061500Serics if (Clear && !hard) { 9071500Serics tputs(Clear, 1, putch); 9081500Serics 9091500Serics /* Put out carriage return so that system doesn't 9101500Serics ** get confused by escape sequences when expanding tabs 9111500Serics */ 9121500Serics putchar ('\r'); 9131500Serics promptlen = 0; 9141500Serics } 9151500Serics } 9161500Serics 9173455Sroot /* 9183455Sroot * Go to home position 9193455Sroot */ 9203455Sroot home() 9213455Sroot { 9223455Sroot tputs(Home,1,putch); 9233455Sroot } 9243455Sroot 9251500Serics static int lastcmd, lastarg, lastp; 9261500Serics static int lastcolon; 9271500Serics char shell_line[132]; 9281500Serics 9291500Serics /* 9301500Serics ** Read a command and do it. A command consists of an optional integer 9311500Serics ** argument followed by the command character. Return the number of lines 9321500Serics ** to display in the next screenful. If there is nothing more to display 9331500Serics ** in the current file, zero is returned. 9341500Serics */ 9351500Serics 9361500Serics command (filename, f) 9371500Serics char *filename; 9381500Serics register FILE *f; 9391500Serics { 9401500Serics register int nlines; 9411500Serics register int retval; 9421500Serics register char c; 9431500Serics char colonch; 9441500Serics FILE *helpf; 9451500Serics int done; 9461500Serics char comchar, cmdbuf[80], *p; 9471500Serics 9481500Serics #define ret(val) retval=val;done++;break 9491500Serics 9501500Serics done = 0; 9511500Serics if (!errors) 9521500Serics prompt (filename); 9531500Serics else 9541500Serics errors = 0; 9551500Serics if (MBIT == RAW && slow_tty) { 9561500Serics otty.sg_flags |= MBIT; 95716582Sleres stty(fileno(stderr), &otty); 9581500Serics } 9591500Serics for (;;) { 9601500Serics nlines = number (&comchar); 9611500Serics lastp = colonch = 0; 9621500Serics if (comchar == '.') { /* Repeat last command */ 9631500Serics lastp++; 9641500Serics comchar = lastcmd; 9651500Serics nlines = lastarg; 9661500Serics if (lastcmd == ':') 9671500Serics colonch = lastcolon; 9681500Serics } 9691500Serics lastcmd = comchar; 9701500Serics lastarg = nlines; 9711500Serics if (comchar == otty.sg_erase) { 9721500Serics kill_line (); 9731500Serics prompt (filename); 9741500Serics continue; 9751500Serics } 9761500Serics switch (comchar) { 9771500Serics case ':': 9781500Serics retval = colon (filename, colonch, nlines); 9791500Serics if (retval >= 0) 9801500Serics done++; 9811500Serics break; 98224490Sbloom case 'b': 98324490Sbloom case ctrl(B): 98424490Sbloom { 98524490Sbloom register int initline; 98624490Sbloom 98724490Sbloom if (no_intty) { 98824490Sbloom write(2, &bell, 1); 98924490Sbloom return (-1); 99024490Sbloom } 99124490Sbloom 99224490Sbloom if (nlines == 0) nlines++; 99324490Sbloom 99424490Sbloom putchar ('\r'); 99524490Sbloom erase (0); 99624490Sbloom printf ("\n"); 99724490Sbloom if (clreol) 99824490Sbloom cleareol (); 99924490Sbloom printf ("...back %d page", nlines); 100024490Sbloom if (nlines > 1) 100124490Sbloom pr ("s\n"); 100224490Sbloom else 100324490Sbloom pr ("\n"); 100424490Sbloom 100524490Sbloom if (clreol) 100624490Sbloom cleareol (); 100724490Sbloom pr ("\n"); 100824490Sbloom 100924490Sbloom initline = Currline - dlines * (nlines + 1); 101024490Sbloom if (! noscroll) 101124490Sbloom --initline; 101224490Sbloom if (initline < 0) initline = 0; 101324490Sbloom Fseek(f, 0L); 101424490Sbloom Currline = 0; /* skiplns() will make Currline correct */ 101524490Sbloom skiplns(initline, f); 101624490Sbloom if (! noscroll) { 101724490Sbloom ret(dlines + 1); 101824490Sbloom } 101924490Sbloom else { 102024490Sbloom ret(dlines); 102124490Sbloom } 102224490Sbloom } 10231500Serics case ' ': 10241500Serics case 'z': 10251500Serics if (nlines == 0) nlines = dlines; 10261500Serics else if (comchar == 'z') dlines = nlines; 10271500Serics ret (nlines); 10281500Serics case 'd': 10291500Serics case ctrl(D): 10301500Serics if (nlines != 0) nscroll = nlines; 10311500Serics ret (nscroll); 10321500Serics case 'q': 10331500Serics case 'Q': 10341500Serics end_it (); 10351500Serics case 's': 10361500Serics case 'f': 10371500Serics if (nlines == 0) nlines++; 10381500Serics if (comchar == 'f') 10391500Serics nlines *= dlines; 10401500Serics putchar ('\r'); 10411500Serics erase (0); 10423594Sroot printf ("\n"); 10433594Sroot if (clreol) 10443594Sroot cleareol (); 10453594Sroot printf ("...skipping %d line", nlines); 10461500Serics if (nlines > 1) 10473594Sroot pr ("s\n"); 10481500Serics else 10493594Sroot pr ("\n"); 10503594Sroot 10513594Sroot if (clreol) 10523594Sroot cleareol (); 10533594Sroot pr ("\n"); 10543594Sroot 10551500Serics while (nlines > 0) { 10561500Serics while ((c = Getc (f)) != '\n') 10571500Serics if (c == EOF) { 10581500Serics retval = 0; 10591500Serics done++; 10601500Serics goto endsw; 10611500Serics } 10621500Serics Currline++; 10631500Serics nlines--; 10641500Serics } 10651500Serics ret (dlines); 10661500Serics case '\n': 10671500Serics if (nlines != 0) 10681500Serics dlines = nlines; 10691500Serics else 10701500Serics nlines = 1; 10711500Serics ret (nlines); 10721500Serics case '\f': 10731500Serics if (!no_intty) { 10741500Serics doclear (); 10751500Serics Fseek (f, screen_start.chrctr); 10761500Serics Currline = screen_start.line; 10771500Serics ret (dlines); 10781500Serics } 10791500Serics else { 10801500Serics write (2, &bell, 1); 10811500Serics break; 10821500Serics } 10831500Serics case '\'': 10841500Serics if (!no_intty) { 10851500Serics kill_line (); 10861500Serics pr ("\n***Back***\n\n"); 10871500Serics Fseek (f, context.chrctr); 10881500Serics Currline = context.line; 10891500Serics ret (dlines); 10901500Serics } 10911500Serics else { 10921500Serics write (2, &bell, 1); 10931500Serics break; 10941500Serics } 10951500Serics case '=': 10961500Serics kill_line (); 10971500Serics promptlen = printd (Currline); 10981500Serics fflush (stdout); 10991500Serics break; 11001500Serics case 'n': 11011500Serics lastp++; 11021500Serics case '/': 11031500Serics if (nlines == 0) nlines++; 11041500Serics kill_line (); 11051500Serics pr ("/"); 11061500Serics promptlen = 1; 11071500Serics fflush (stdout); 11081500Serics if (lastp) { 11091500Serics write (2,"\r", 1); 11101500Serics search (NULL, f, nlines); /* Use previous r.e. */ 11111500Serics } 11121500Serics else { 11131500Serics ttyin (cmdbuf, 78, '/'); 11141500Serics write (2, "\r", 1); 11151500Serics search (cmdbuf, f, nlines); 11161500Serics } 11173455Sroot ret (dlines-1); 11181500Serics case '!': 11191500Serics do_shell (filename); 11201500Serics break; 112124490Sbloom case '?': 11221500Serics case 'h': 11231500Serics if ((helpf = fopen (HELPFILE, "r")) == NULL) 11241500Serics error ("Can't open help file"); 11251500Serics if (noscroll) doclear (); 11261500Serics copy_file (helpf); 112727006Sdonn fclose (helpf); 11281500Serics prompt (filename); 11291500Serics break; 11301500Serics case 'v': /* This case should go right before default */ 11311500Serics if (!no_intty) { 11321500Serics kill_line (); 11331500Serics cmdbuf[0] = '+'; 113424490Sbloom scanstr (Currline - dlines < 0 ? 0 113524490Sbloom : Currline - (dlines + 1) / 2, &cmdbuf[1]); 11361500Serics pr ("vi "); pr (cmdbuf); putchar (' '); pr (fnames[fnum]); 11371500Serics execute (filename, VI, "vi", cmdbuf, fnames[fnum], 0); 11381500Serics break; 11391500Serics } 11401500Serics default: 114116710Sjak if (dum_opt) { 114216710Sjak kill_line (); 114316710Sjak if (Senter && Sexit) { 114416710Sjak tputs (Senter, 1, putch); 114516710Sjak promptlen = pr ("[Press 'h' for instructions.]") + (2 * soglitch); 114616710Sjak tputs (Sexit, 1, putch); 114716710Sjak } 114816710Sjak else 114916710Sjak promptlen = pr ("[Press 'h' for instructions.]"); 115016710Sjak fflush (stdout); 115116710Sjak } 115216710Sjak else 115316710Sjak write (2, &bell, 1); 11541500Serics break; 11551500Serics } 11561500Serics if (done) break; 11571500Serics } 11581500Serics putchar ('\r'); 11591500Serics endsw: 11601500Serics inwait = 0; 11611500Serics notell++; 11621500Serics if (MBIT == RAW && slow_tty) { 11631500Serics otty.sg_flags &= ~MBIT; 116416582Sleres stty(fileno(stderr), &otty); 11651500Serics } 11661500Serics return (retval); 11671500Serics } 11681500Serics 11691500Serics char ch; 11701500Serics 11711500Serics /* 11721500Serics * Execute a colon-prefixed command. 11731500Serics * Returns <0 if not a command that should cause 11741500Serics * more of the file to be printed. 11751500Serics */ 11761500Serics 11771500Serics colon (filename, cmd, nlines) 11781500Serics char *filename; 11791500Serics int cmd; 11801500Serics int nlines; 11811500Serics { 11821500Serics if (cmd == 0) 11831500Serics ch = readch (); 11841500Serics else 11851500Serics ch = cmd; 11861500Serics lastcolon = ch; 11871500Serics switch (ch) { 11881500Serics case 'f': 11891500Serics kill_line (); 11901500Serics if (!no_intty) 11911500Serics promptlen = printf ("\"%s\" line %d", fnames[fnum], Currline); 11921500Serics else 11931500Serics promptlen = printf ("[Not a file] line %d", Currline); 11941500Serics fflush (stdout); 11951500Serics return (-1); 11961500Serics case 'n': 11971500Serics if (nlines == 0) { 11981500Serics if (fnum >= nfiles - 1) 11991500Serics end_it (); 12001500Serics nlines++; 12011500Serics } 12021500Serics putchar ('\r'); 12031500Serics erase (0); 12041500Serics skipf (nlines); 12051500Serics return (0); 12061500Serics case 'p': 12071500Serics if (no_intty) { 12081500Serics write (2, &bell, 1); 12091500Serics return (-1); 12101500Serics } 12111500Serics putchar ('\r'); 12121500Serics erase (0); 12131500Serics if (nlines == 0) 12141500Serics nlines++; 12151500Serics skipf (-nlines); 12161500Serics return (0); 12171500Serics case '!': 12181500Serics do_shell (filename); 12191500Serics return (-1); 12201500Serics case 'q': 12211500Serics case 'Q': 12221500Serics end_it (); 12231500Serics default: 12241500Serics write (2, &bell, 1); 12251500Serics return (-1); 12261500Serics } 12271500Serics } 12281500Serics 12291500Serics /* 12301500Serics ** Read a decimal number from the terminal. Set cmd to the non-digit which 12311500Serics ** terminates the number. 12321500Serics */ 12331500Serics 12341500Serics number(cmd) 12351500Serics char *cmd; 12361500Serics { 12371500Serics register int i; 12381500Serics 12391500Serics i = 0; ch = otty.sg_kill; 12401500Serics for (;;) { 12411500Serics ch = readch (); 12421500Serics if (ch >= '0' && ch <= '9') 12431500Serics i = i*10 + ch - '0'; 12441500Serics else if (ch == otty.sg_kill) 12451500Serics i = 0; 12461500Serics else { 12471500Serics *cmd = ch; 12481500Serics break; 12491500Serics } 12501500Serics } 12511500Serics return (i); 12521500Serics } 12531500Serics 12541500Serics do_shell (filename) 12551500Serics char *filename; 12561500Serics { 12571500Serics char cmdbuf[80]; 12581500Serics 12591500Serics kill_line (); 12601500Serics pr ("!"); 12611500Serics fflush (stdout); 12621500Serics promptlen = 1; 12631500Serics if (lastp) 12641500Serics pr (shell_line); 12651500Serics else { 12661500Serics ttyin (cmdbuf, 78, '!'); 12671500Serics if (expand (shell_line, cmdbuf)) { 12681500Serics kill_line (); 12691500Serics promptlen = printf ("!%s", shell_line); 12701500Serics } 12711500Serics } 12721500Serics fflush (stdout); 12731500Serics write (2, "\n", 1); 12741500Serics promptlen = 0; 12751500Serics shellp = 1; 12761500Serics execute (filename, shell, shell, "-c", shell_line, 0); 12771500Serics } 12781500Serics 12791500Serics /* 12801500Serics ** Search for nth ocurrence of regular expression contained in buf in the file 12811500Serics */ 12821500Serics 12831500Serics search (buf, file, n) 12841500Serics char buf[]; 12851500Serics FILE *file; 12861500Serics register int n; 12871500Serics { 12881500Serics long startline = Ftell (file); 12891500Serics register long line1 = startline; 12901500Serics register long line2 = startline; 12911500Serics register long line3 = startline; 12921500Serics register int lncount; 12931500Serics int saveln, rv, re_exec(); 12941500Serics char *s, *re_comp(); 12951500Serics 12961500Serics context.line = saveln = Currline; 12971500Serics context.chrctr = startline; 12981500Serics lncount = 0; 12991500Serics if ((s = re_comp (buf)) != 0) 13001500Serics error (s); 13011500Serics while (!feof (file)) { 13021500Serics line3 = line2; 13031500Serics line2 = line1; 13041500Serics line1 = Ftell (file); 13051500Serics rdline (file); 13061500Serics lncount++; 13071500Serics if ((rv = re_exec (Line)) == 1) 13081500Serics if (--n == 0) { 13091500Serics if (lncount > 3 || (lncount > 1 && no_intty)) 13103455Sroot { 13113455Sroot pr ("\n"); 13123594Sroot if (clreol) 13133594Sroot cleareol (); 13143455Sroot pr("...skipping\n"); 13153455Sroot } 13161500Serics if (!no_intty) { 13171500Serics Currline -= (lncount >= 3 ? 3 : lncount); 13181500Serics Fseek (file, line3); 13193594Sroot if (noscroll) 13203594Sroot if (clreol) { 13213594Sroot home (); 13223594Sroot cleareol (); 132316582Sleres } 13243594Sroot else 13253594Sroot doclear (); 13261500Serics } 13271500Serics else { 13281500Serics kill_line (); 13293594Sroot if (noscroll) 13303594Sroot if (clreol) { 133116582Sleres home (); 13323594Sroot cleareol (); 133316582Sleres } 13343594Sroot else 13353594Sroot doclear (); 13361500Serics pr (Line); 13371500Serics putchar ('\n'); 13381500Serics } 13391500Serics break; 13401500Serics } 13411500Serics else if (rv == -1) 13421500Serics error ("Regular expression botch"); 13431500Serics } 13441500Serics if (feof (file)) { 13451500Serics if (!no_intty) { 13461500Serics file->_flag &= ~_IOEOF; /* why doesn't fseek do this ??!!??! */ 13471500Serics Currline = saveln; 13481500Serics Fseek (file, startline); 13491500Serics } 13501500Serics else { 13511500Serics pr ("\nPattern not found\n"); 13521500Serics end_it (); 13531500Serics } 13541500Serics error ("Pattern not found"); 13551500Serics } 13561500Serics } 13571500Serics 13581500Serics execute (filename, cmd, args) 13591500Serics char *filename; 13601500Serics char *cmd, *args; 13611500Serics { 13621500Serics int id; 136316710Sjak int n; 13641500Serics 13651500Serics fflush (stdout); 13661500Serics reset_tty (); 136716710Sjak for (n = 10; (id = fork ()) < 0 && n > 0; n--) 13681500Serics sleep (5); 13691500Serics if (id == 0) { 137016710Sjak if (!isatty(0)) { 137116710Sjak close(0); 137216710Sjak open("/dev/tty", 0); 137316710Sjak } 13741500Serics execv (cmd, &args); 13751500Serics write (2, "exec failed\n", 12); 13761500Serics exit (1); 13771500Serics } 137816710Sjak if (id > 0) { 137916710Sjak signal (SIGINT, SIG_IGN); 138016710Sjak signal (SIGQUIT, SIG_IGN); 138116710Sjak if (catch_susp) 138216710Sjak signal(SIGTSTP, SIG_DFL); 138316710Sjak while (wait(0) > 0); 138416710Sjak signal (SIGINT, end_it); 138516710Sjak signal (SIGQUIT, onquit); 138616710Sjak if (catch_susp) 138716710Sjak signal(SIGTSTP, onsusp); 138816710Sjak } else 138916710Sjak write(2, "can't fork\n", 11); 13901500Serics set_tty (); 13911500Serics pr ("------------------------\n"); 13921500Serics prompt (filename); 13931500Serics } 13941500Serics /* 13951500Serics ** Skip n lines in the file f 13961500Serics */ 13971500Serics 13981500Serics skiplns (n, f) 13991500Serics register int n; 14001500Serics register FILE *f; 14011500Serics { 14021500Serics register char c; 14031500Serics 14041500Serics while (n > 0) { 14051500Serics while ((c = Getc (f)) != '\n') 14061500Serics if (c == EOF) 14071500Serics return; 14081500Serics n--; 14091500Serics Currline++; 14101500Serics } 14111500Serics } 14121500Serics 14131500Serics /* 14141500Serics ** Skip nskip files in the file list (from the command line). Nskip may be 14151500Serics ** negative. 14161500Serics */ 14171500Serics 14181500Serics skipf (nskip) 14191500Serics register int nskip; 14201500Serics { 14211500Serics if (nskip == 0) return; 14221500Serics if (nskip > 0) { 14231500Serics if (fnum + nskip > nfiles - 1) 14241500Serics nskip = nfiles - fnum - 1; 14251500Serics } 14261500Serics else if (within) 14271500Serics ++fnum; 14281500Serics fnum += nskip; 14291500Serics if (fnum < 0) 14301500Serics fnum = 0; 14313594Sroot pr ("\n...Skipping "); 14323455Sroot pr ("\n"); 14333594Sroot if (clreol) 14343594Sroot cleareol (); 14353455Sroot pr ("...Skipping "); 14361500Serics pr (nskip > 0 ? "to file " : "back to file "); 14371500Serics pr (fnames[fnum]); 14383455Sroot pr ("\n"); 14393594Sroot if (clreol) 14403594Sroot cleareol (); 14413455Sroot pr ("\n"); 14421500Serics --fnum; 14431500Serics } 14441500Serics 14451500Serics /*----------------------------- Terminal I/O -------------------------------*/ 14461500Serics 14471500Serics initterm () 14481500Serics { 14491500Serics char buf[TBUFSIZ]; 145017195Sralph static char clearbuf[TBUFSIZ]; 14511500Serics char *clearptr, *padstr; 14521500Serics int ldisc; 145317592Sleres int lmode; 145410823Ssam char *term; 145516582Sleres int tgrp; 145618030Sbloom struct winsize win; 14571500Serics 145816582Sleres retry: 145916582Sleres if (!(no_tty = gtty(fileno(stdout), &otty))) { 146017592Sleres if (ioctl(fileno(stdout), TIOCLGET, &lmode) < 0) { 146117592Sleres perror("TIOCLGET"); 146217592Sleres exit(1); 146317592Sleres } 146417592Sleres docrterase = ((lmode & LCRTERA) != 0); 146517592Sleres docrtkill = ((lmode & LCRTKIL) != 0); 146616582Sleres /* 146717592Sleres * Wait until we're in the foreground before we save the 146817592Sleres * the terminal modes. 146916582Sleres */ 147016582Sleres if (ioctl(fileno(stdout), TIOCGPGRP, &tgrp) < 0) { 147117592Sleres perror("TIOCGPGRP"); 147216582Sleres exit(1); 147316582Sleres } 147416582Sleres if (tgrp != getpgrp(0)) { 147516582Sleres kill(0, SIGTTOU); 147616582Sleres goto retry; 147716582Sleres } 147813830Skre if ((term = getenv("TERM")) == 0 || tgetent(buf, term) <= 0) { 14793594Sroot dumb++; ul_opt = 0; 14801500Serics } 14811500Serics else { 148218030Sbloom if (ioctl(fileno(stdout), TIOCGWINSZ, &win) < 0) { 148318030Sbloom Lpp = tgetnum("li"); 148418030Sbloom Mcol = tgetnum("co"); 148518030Sbloom } else { 148618030Sbloom if ((Lpp = win.ws_row) == 0) 148718030Sbloom Lpp = tgetnum("li"); 148818030Sbloom if ((Mcol = win.ws_col) == 0) 148918030Sbloom Mcol = tgetnum("co"); 149018030Sbloom } 149118030Sbloom if ((Lpp <= 0) || tgetflag("hc")) { 14921500Serics hard++; /* Hard copy terminal */ 14931500Serics Lpp = 24; 14941500Serics } 149529907Smckusick if (tgetflag("xn")) 149629907Smckusick eatnl++; /* Eat newline at last column + 1; dec, concept */ 149718030Sbloom if (Mcol <= 0) 149818030Sbloom Mcol = 80; 149918030Sbloom 15001500Serics if (tailequ (fnames[0], "page") || !hard && tgetflag("ns")) 15011500Serics noscroll++; 15021500Serics Wrap = tgetflag("am"); 15031500Serics bad_so = tgetflag ("xs"); 15041500Serics clearptr = clearbuf; 15051500Serics eraseln = tgetstr("ce",&clearptr); 15061500Serics Clear = tgetstr("cl", &clearptr); 15071500Serics Senter = tgetstr("so", &clearptr); 15081500Serics Sexit = tgetstr("se", &clearptr); 150916710Sjak if ((soglitch = tgetnum("sg")) < 0) 151016710Sjak soglitch = 0; 15113594Sroot 15123594Sroot /* 15133594Sroot * Set up for underlining: some terminals don't need it; 15143594Sroot * others have start/stop sequences, still others have an 15153594Sroot * underline char sequence which is assumed to move the 15163594Sroot * cursor forward one character. If underline sequence 15173594Sroot * isn't available, settle for standout sequence. 15183594Sroot */ 15193594Sroot 15203594Sroot if (tgetflag("ul") || tgetflag("os")) 15213594Sroot ul_opt = 0; 15223594Sroot if ((chUL = tgetstr("uc", &clearptr)) == NULL ) 15233594Sroot chUL = ""; 152416710Sjak if (((ULenter = tgetstr("us", &clearptr)) == NULL || 152516710Sjak (ULexit = tgetstr("ue", &clearptr)) == NULL) && !*chUL) { 152616710Sjak if ((ULenter = Senter) == NULL || (ULexit = Sexit) == NULL) { 152716710Sjak ULenter = ""; 152816710Sjak ULexit = ""; 152916710Sjak } else 153016710Sjak ulglitch = soglitch; 153116710Sjak } else { 153216710Sjak if ((ulglitch = tgetnum("ug")) < 0) 153316710Sjak ulglitch = 0; 153416710Sjak } 153516582Sleres 15361500Serics if (padstr = tgetstr("pc", &clearptr)) 15371500Serics PC = *padstr; 15383455Sroot Home = tgetstr("ho",&clearptr); 153913536Ssam if (Home == 0 || *Home == '\0') 15403455Sroot { 15413594Sroot if ((cursorm = tgetstr("cm", &clearptr)) != NULL) { 15423594Sroot strcpy(cursorhome, tgoto(cursorm, 0, 0)); 15433455Sroot Home = cursorhome; 15443455Sroot } 15453455Sroot } 15463594Sroot EodClr = tgetstr("cd", &clearptr); 154725540Smckusick if ((chBS = tgetstr("bc", &clearptr)) == NULL) 154825540Smckusick chBS = "\b"; 154925540Smckusick 15501500Serics } 15511500Serics if ((shell = getenv("SHELL")) == NULL) 15521500Serics shell = "/bin/sh"; 15531500Serics } 155416582Sleres no_intty = gtty(fileno(stdin), &otty); 155516582Sleres gtty(fileno(stderr), &otty); 155613830Skre savetty = otty; 15571500Serics ospeed = otty.sg_ospeed; 15581500Serics slow_tty = ospeed < B1200; 155927006Sdonn hardtabs = (otty.sg_flags & TBDELAY) != XTABS; 15601500Serics if (!no_tty) { 15611500Serics otty.sg_flags &= ~ECHO; 15621500Serics if (MBIT == CBREAK || !slow_tty) 15631500Serics otty.sg_flags |= MBIT; 15641500Serics } 15651500Serics } 15661500Serics 15671500Serics readch () 15681500Serics { 15691500Serics char ch; 15701500Serics extern int errno; 15711500Serics 157231089Skarels errno = 0; 15731500Serics if (read (2, &ch, 1) <= 0) 15741500Serics if (errno != EINTR) 157531089Skarels end_it(); 15761500Serics else 15771500Serics ch = otty.sg_kill; 15781500Serics return (ch); 15791500Serics } 15801500Serics 15811500Serics static char BS = '\b'; 158217592Sleres static char *BSB = "\b \b"; 15831500Serics static char CARAT = '^'; 158417592Sleres #define ERASEONECHAR \ 158517592Sleres if (docrterase) \ 158617592Sleres write (2, BSB, sizeof(BSB)); \ 158717592Sleres else \ 158817592Sleres write (2, &BS, sizeof(BS)); 15891500Serics 15901500Serics ttyin (buf, nmax, pchar) 15911500Serics char buf[]; 15921500Serics register int nmax; 15931500Serics char pchar; 15941500Serics { 15951500Serics register char *sptr; 15961500Serics register char ch; 15971500Serics register int slash = 0; 15981500Serics int maxlen; 15991500Serics char cbuf; 16001500Serics 16011500Serics sptr = buf; 16021500Serics maxlen = 0; 16031500Serics while (sptr - buf < nmax) { 16041500Serics if (promptlen > maxlen) maxlen = promptlen; 16051500Serics ch = readch (); 16061500Serics if (ch == '\\') { 16071500Serics slash++; 16081500Serics } 16091500Serics else if ((ch == otty.sg_erase) && !slash) { 16101500Serics if (sptr > buf) { 16111500Serics --promptlen; 161217592Sleres ERASEONECHAR 16131500Serics --sptr; 16141500Serics if ((*sptr < ' ' && *sptr != '\n') || *sptr == RUBOUT) { 16151500Serics --promptlen; 161617592Sleres ERASEONECHAR 16171500Serics } 16181500Serics continue; 16191500Serics } 16201500Serics else { 16211500Serics if (!eraseln) promptlen = maxlen; 16221500Serics longjmp (restore, 1); 16231500Serics } 16241500Serics } 16251500Serics else if ((ch == otty.sg_kill) && !slash) { 16261500Serics if (hard) { 16271500Serics show (ch); 16281500Serics putchar ('\n'); 16291500Serics putchar (pchar); 16301500Serics } 16311500Serics else { 16321500Serics putchar ('\r'); 16331500Serics putchar (pchar); 16341500Serics if (eraseln) 16351500Serics erase (1); 163617592Sleres else if (docrtkill) 163717592Sleres while (promptlen-- > 1) 163817592Sleres write (2, BSB, sizeof(BSB)); 16391500Serics promptlen = 1; 16401500Serics } 16411500Serics sptr = buf; 16421500Serics fflush (stdout); 16431500Serics continue; 16441500Serics } 16451500Serics if (slash && (ch == otty.sg_kill || ch == otty.sg_erase)) { 164617592Sleres ERASEONECHAR 16471500Serics --sptr; 16481500Serics } 16491500Serics if (ch != '\\') 16501500Serics slash = 0; 16511500Serics *sptr++ = ch; 16521500Serics if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) { 16531500Serics ch += ch == RUBOUT ? -0100 : 0100; 16541500Serics write (2, &CARAT, 1); 16551500Serics promptlen++; 16561500Serics } 16571500Serics cbuf = ch; 16581500Serics if (ch != '\n' && ch != ESC) { 16591500Serics write (2, &cbuf, 1); 16601500Serics promptlen++; 16611500Serics } 16621500Serics else 16631500Serics break; 16641500Serics } 16651500Serics *--sptr = '\0'; 16661500Serics if (!eraseln) promptlen = maxlen; 16671500Serics if (sptr - buf >= nmax - 1) 16681500Serics error ("Line too long"); 16691500Serics } 16701500Serics 16711500Serics expand (outbuf, inbuf) 16721500Serics char *outbuf; 16731500Serics char *inbuf; 16741500Serics { 16751500Serics register char *instr; 16761500Serics register char *outstr; 16771500Serics register char ch; 16781500Serics char temp[200]; 16791500Serics int changed = 0; 16801500Serics 16811500Serics instr = inbuf; 16821500Serics outstr = temp; 16831500Serics while ((ch = *instr++) != '\0') 16841500Serics switch (ch) { 16851500Serics case '%': 16861500Serics if (!no_intty) { 16871500Serics strcpy (outstr, fnames[fnum]); 16881500Serics outstr += strlen (fnames[fnum]); 16891500Serics changed++; 16901500Serics } 16911500Serics else 16921500Serics *outstr++ = ch; 16931500Serics break; 16941500Serics case '!': 16951500Serics if (!shellp) 16961500Serics error ("No previous command to substitute for"); 16971500Serics strcpy (outstr, shell_line); 16981500Serics outstr += strlen (shell_line); 16991500Serics changed++; 17001500Serics break; 17011500Serics case '\\': 17021500Serics if (*instr == '%' || *instr == '!') { 17031500Serics *outstr++ = *instr++; 17041500Serics break; 17051500Serics } 17061500Serics default: 17071500Serics *outstr++ = ch; 17081500Serics } 17091500Serics *outstr++ = '\0'; 17101500Serics strcpy (outbuf, temp); 17111500Serics return (changed); 17121500Serics } 17131500Serics 17141500Serics show (ch) 17151500Serics register char ch; 17161500Serics { 17171500Serics char cbuf; 17181500Serics 17191500Serics if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) { 17201500Serics ch += ch == RUBOUT ? -0100 : 0100; 17211500Serics write (2, &CARAT, 1); 17221500Serics promptlen++; 17231500Serics } 17241500Serics cbuf = ch; 17251500Serics write (2, &cbuf, 1); 17261500Serics promptlen++; 17271500Serics } 17281500Serics 17291500Serics error (mess) 17301500Serics char *mess; 17311500Serics { 17323594Sroot if (clreol) 17333594Sroot cleareol (); 17343594Sroot else 17353594Sroot kill_line (); 17361500Serics promptlen += strlen (mess); 17371500Serics if (Senter && Sexit) { 17381500Serics tputs (Senter, 1, putch); 17391500Serics pr(mess); 17401500Serics tputs (Sexit, 1, putch); 17411500Serics } 17421500Serics else 17431500Serics pr (mess); 17441500Serics fflush(stdout); 17451500Serics errors++; 17461500Serics longjmp (restore, 1); 17471500Serics } 17481500Serics 17491500Serics 17501500Serics set_tty () 17511500Serics { 17521500Serics otty.sg_flags |= MBIT; 17531500Serics otty.sg_flags &= ~ECHO; 175416582Sleres stty(fileno(stderr), &otty); 17551500Serics } 17561500Serics 17571500Serics reset_tty () 17581500Serics { 175931033Sbostic if (no_tty) 176031033Sbostic return; 176116710Sjak if (pstate) { 176216710Sjak tputs(ULexit, 1, putch); 176316710Sjak fflush(stdout); 176416710Sjak pstate = 0; 176516710Sjak } 17661500Serics otty.sg_flags |= ECHO; 17671500Serics otty.sg_flags &= ~MBIT; 176816582Sleres stty(fileno(stderr), &savetty); 17691500Serics } 17701500Serics 17711500Serics rdline (f) 17721500Serics register FILE *f; 17731500Serics { 17741500Serics register char c; 17751500Serics register char *p; 17761500Serics 17771500Serics p = Line; 17781500Serics while ((c = Getc (f)) != '\n' && c != EOF && p - Line < LINSIZ - 1) 17791500Serics *p++ = c; 17801500Serics if (c == '\n') 17811500Serics Currline++; 17821500Serics *p = '\0'; 17831500Serics } 17841500Serics 17851500Serics /* Come here when we get a suspend signal from the terminal */ 17861500Serics 17871500Serics onsusp () 17881500Serics { 178914861Skarels /* ignore SIGTTOU so we don't get stopped if csh grabs the tty */ 179014861Skarels signal(SIGTTOU, SIG_IGN); 17911500Serics reset_tty (); 17921500Serics fflush (stdout); 179314861Skarels signal(SIGTTOU, SIG_DFL); 17941500Serics /* Send the TSTP signal to suspend our process group */ 179513289Ssam signal(SIGTSTP, SIG_DFL); 179613289Ssam sigsetmask(0); 17971500Serics kill (0, SIGTSTP); 17981500Serics /* Pause for station break */ 17991500Serics 18001500Serics /* We're back */ 18011500Serics signal (SIGTSTP, onsusp); 18021500Serics set_tty (); 18031500Serics if (inwait) 18041500Serics longjmp (restore); 18051500Serics } 1806