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*29906Smckusick static char sccsid[] = "@(#)more.c 5.4.1.1 (Berkeley) 10/21/86"; 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> 34*29906Smckusick #include <sys/file.h> 35*29906Smckusick #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 */ 77*29906Smckusick int hard, dumb, noscroll, hardtabs, clreol; 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(); 2603455Sroot printf("::::::::::::::\n", fnames[fnum]); 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 */ 355*29906Smckusick if (magic(f)) { 3561500Serics printf("\n******** %s: Not a text file ********\n\n", fs); 3571500Serics fclose (f); 3581500Serics return (NULL); 3591500Serics } 360*29906Smckusick 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 /* 373*29906Smckusick * Check for file magic numbers. This code would best 374*29906Smckusick * be shared with the file(1) program or, perhaps, more 375*29906Smckusick * should not try and be so smart? 376*29906Smckusick */ 377*29906Smckusick magic(f) 378*29906Smckusick FILE *f; 379*29906Smckusick { 380*29906Smckusick long magic; 381*29906Smckusick 382*29906Smckusick magic = getw(f); 383*29906Smckusick fseek(f, -sizeof (magic), L_INCR); /* reset file position */ 384*29906Smckusick return (magic == 0405 || magic == OMAGIC || magic == NMAGIC || 385*29906Smckusick magic == 0411 || magic == ZMAGIC || magic == 0177545); 386*29906Smckusick } 387*29906Smckusick 388*29906Smckusick /* 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') 743*29906Smckusick if (hardtabs && column < promptlen && !hard) { 744*29906Smckusick if (eraseln && !dumb) { 7451500Serics column = 1 + (column | 7); 7461500Serics tputs (eraseln, 1, putch); 7471500Serics promptlen = 0; 7481500Serics } 7491500Serics else { 750*29906Smckusick for (--p; column & 7 && p < &Line[LINSIZ - 1]; column++) { 7511500Serics *p++ = ' '; 7521500Serics } 7531500Serics if (column >= promptlen) promptlen = 0; 7541500Serics } 7551500Serics } 7561500Serics else 7571500Serics column = 1 + (column | 7); 7589627Ssklower else if (c == '\b' && column > 0) 7591500Serics column--; 7601500Serics else if (c == '\r') 7611500Serics column = 0; 7621500Serics else if (c == '\f' && stop_opt) { 7631500Serics p[-1] = '^'; 7641500Serics *p++ = 'L'; 7651500Serics column += 2; 7661500Serics Pause++; 7671500Serics } 7681500Serics else if (c == EOF) { 7691500Serics *length = p - Line; 7701500Serics return (column); 7711500Serics } 7721500Serics else if (c >= ' ' && c != RUBOUT) 7731500Serics column++; 7741500Serics if (column >= Mcol && fold_opt) break; 7751500Serics c = Getc (f); 7761500Serics } 7771500Serics if (column >= Mcol && Mcol > 0) { 7781500Serics if (!Wrap) { 7791500Serics *p++ = '\n'; 7801500Serics } 7811500Serics } 7821500Serics colflg = column == Mcol && fold_opt; 7831500Serics *length = p - Line; 7841500Serics *p = 0; 7851500Serics return (column); 7861500Serics } 7871500Serics 7881500Serics /* 7891500Serics ** Erase the rest of the prompt, assuming we are starting at column col. 7901500Serics */ 7911500Serics 7921500Serics erase (col) 7931500Serics register int col; 7941500Serics { 7951500Serics 7961500Serics if (promptlen == 0) 7971500Serics return; 7981500Serics if (hard) { 7991500Serics putchar ('\n'); 8001500Serics } 8011500Serics else { 8021500Serics if (col == 0) 8031500Serics putchar ('\r'); 8041500Serics if (!dumb && eraseln) 8051500Serics tputs (eraseln, 1, putch); 8061500Serics else 8071500Serics for (col = promptlen - col; col > 0; col--) 8081500Serics putchar (' '); 8091500Serics } 8101500Serics promptlen = 0; 8111500Serics } 8121500Serics 8131500Serics /* 8141500Serics ** Erase the current line entirely 8151500Serics */ 8161500Serics 8171500Serics kill_line () 8181500Serics { 8191500Serics erase (0); 8201500Serics if (!eraseln || dumb) putchar ('\r'); 8211500Serics } 8221500Serics 8231500Serics /* 8243455Sroot * force clear to end of line 8253455Sroot */ 8263455Sroot cleareol() 8273455Sroot { 8283594Sroot tputs(eraseln, 1, putch); 8293455Sroot } 8303455Sroot 8313594Sroot clreos() 8323455Sroot { 8333594Sroot tputs(EodClr, 1, putch); 8343455Sroot } 8353455Sroot 8363455Sroot /* 8371500Serics ** Print string and return number of characters 8381500Serics */ 8391500Serics 8401500Serics pr(s1) 8411500Serics char *s1; 8421500Serics { 8431500Serics register char *s; 8441500Serics register char c; 8451500Serics 8461500Serics for (s = s1; c = *s++; ) 8471500Serics putchar(c); 8481500Serics return (s - s1 - 1); 8491500Serics } 8501500Serics 8511500Serics 8521500Serics /* Print a buffer of n characters */ 8531500Serics 8541500Serics prbuf (s, n) 8551500Serics register char *s; 8561500Serics register int n; 8571500Serics { 85816710Sjak register char c; /* next output character */ 8593594Sroot register int state; /* next output char's UL state */ 86016710Sjak #define wouldul(s,n) ((n) >= 2 && (((s)[0] == '_' && (s)[1] == '\b') || ((s)[1] == '\b' && (s)[2] == '_'))) 8613594Sroot 8623594Sroot while (--n >= 0) 8633594Sroot if (!ul_opt) 8643594Sroot putchar (*s++); 8653594Sroot else { 86616710Sjak if (*s == ' ' && pstate == 0 && ulglitch && wouldul(s+1, n-1)) { 86716710Sjak s++; 86816710Sjak continue; 86916710Sjak } 87016710Sjak if (state = wouldul(s, n)) { 87116710Sjak c = (*s == '_')? s[2] : *s ; 8723594Sroot n -= 2; 87316710Sjak s += 3; 87416710Sjak } else 8753594Sroot c = *s++; 87616710Sjak if (state != pstate) { 87716710Sjak if (c == ' ' && state == 0 && ulglitch && wouldul(s, n-1)) 87816710Sjak state = 1; 87916710Sjak else 88016710Sjak tputs(state ? ULenter : ULexit, 1, putch); 8813594Sroot } 88216710Sjak if (c != ' ' || pstate == 0 || state != 0 || ulglitch == 0) 88316710Sjak putchar(c); 8843594Sroot if (state && *chUL) { 8853594Sroot pr(chBS); 8863594Sroot tputs(chUL, 1, putch); 8873594Sroot } 88816710Sjak pstate = state; 8893594Sroot } 8901500Serics } 8911500Serics 8921500Serics /* 8931500Serics ** Clear the screen 8941500Serics */ 8951500Serics 8961500Serics doclear() 8971500Serics { 8981500Serics if (Clear && !hard) { 8991500Serics tputs(Clear, 1, putch); 9001500Serics 9011500Serics /* Put out carriage return so that system doesn't 9021500Serics ** get confused by escape sequences when expanding tabs 9031500Serics */ 9041500Serics putchar ('\r'); 9051500Serics promptlen = 0; 9061500Serics } 9071500Serics } 9081500Serics 9093455Sroot /* 9103455Sroot * Go to home position 9113455Sroot */ 9123455Sroot home() 9133455Sroot { 9143455Sroot tputs(Home,1,putch); 9153455Sroot } 9163455Sroot 9171500Serics static int lastcmd, lastarg, lastp; 9181500Serics static int lastcolon; 9191500Serics char shell_line[132]; 9201500Serics 9211500Serics /* 9221500Serics ** Read a command and do it. A command consists of an optional integer 9231500Serics ** argument followed by the command character. Return the number of lines 9241500Serics ** to display in the next screenful. If there is nothing more to display 9251500Serics ** in the current file, zero is returned. 9261500Serics */ 9271500Serics 9281500Serics command (filename, f) 9291500Serics char *filename; 9301500Serics register FILE *f; 9311500Serics { 9321500Serics register int nlines; 9331500Serics register int retval; 9341500Serics register char c; 9351500Serics char colonch; 9361500Serics FILE *helpf; 9371500Serics int done; 9381500Serics char comchar, cmdbuf[80], *p; 9391500Serics 9401500Serics #define ret(val) retval=val;done++;break 9411500Serics 9421500Serics done = 0; 9431500Serics if (!errors) 9441500Serics prompt (filename); 9451500Serics else 9461500Serics errors = 0; 9471500Serics if (MBIT == RAW && slow_tty) { 9481500Serics otty.sg_flags |= MBIT; 94916582Sleres stty(fileno(stderr), &otty); 9501500Serics } 9511500Serics for (;;) { 9521500Serics nlines = number (&comchar); 9531500Serics lastp = colonch = 0; 9541500Serics if (comchar == '.') { /* Repeat last command */ 9551500Serics lastp++; 9561500Serics comchar = lastcmd; 9571500Serics nlines = lastarg; 9581500Serics if (lastcmd == ':') 9591500Serics colonch = lastcolon; 9601500Serics } 9611500Serics lastcmd = comchar; 9621500Serics lastarg = nlines; 9631500Serics if (comchar == otty.sg_erase) { 9641500Serics kill_line (); 9651500Serics prompt (filename); 9661500Serics continue; 9671500Serics } 9681500Serics switch (comchar) { 9691500Serics case ':': 9701500Serics retval = colon (filename, colonch, nlines); 9711500Serics if (retval >= 0) 9721500Serics done++; 9731500Serics break; 97424490Sbloom case 'b': 97524490Sbloom case ctrl(B): 97624490Sbloom { 97724490Sbloom register int initline; 97824490Sbloom 97924490Sbloom if (no_intty) { 98024490Sbloom write(2, &bell, 1); 98124490Sbloom return (-1); 98224490Sbloom } 98324490Sbloom 98424490Sbloom if (nlines == 0) nlines++; 98524490Sbloom 98624490Sbloom putchar ('\r'); 98724490Sbloom erase (0); 98824490Sbloom printf ("\n"); 98924490Sbloom if (clreol) 99024490Sbloom cleareol (); 99124490Sbloom printf ("...back %d page", nlines); 99224490Sbloom if (nlines > 1) 99324490Sbloom pr ("s\n"); 99424490Sbloom else 99524490Sbloom pr ("\n"); 99624490Sbloom 99724490Sbloom if (clreol) 99824490Sbloom cleareol (); 99924490Sbloom pr ("\n"); 100024490Sbloom 100124490Sbloom initline = Currline - dlines * (nlines + 1); 100224490Sbloom if (! noscroll) 100324490Sbloom --initline; 100424490Sbloom if (initline < 0) initline = 0; 100524490Sbloom Fseek(f, 0L); 100624490Sbloom Currline = 0; /* skiplns() will make Currline correct */ 100724490Sbloom skiplns(initline, f); 100824490Sbloom if (! noscroll) { 100924490Sbloom ret(dlines + 1); 101024490Sbloom } 101124490Sbloom else { 101224490Sbloom ret(dlines); 101324490Sbloom } 101424490Sbloom } 10151500Serics case ' ': 10161500Serics case 'z': 10171500Serics if (nlines == 0) nlines = dlines; 10181500Serics else if (comchar == 'z') dlines = nlines; 10191500Serics ret (nlines); 10201500Serics case 'd': 10211500Serics case ctrl(D): 10221500Serics if (nlines != 0) nscroll = nlines; 10231500Serics ret (nscroll); 10241500Serics case 'q': 10251500Serics case 'Q': 10261500Serics end_it (); 10271500Serics case 's': 10281500Serics case 'f': 10291500Serics if (nlines == 0) nlines++; 10301500Serics if (comchar == 'f') 10311500Serics nlines *= dlines; 10321500Serics putchar ('\r'); 10331500Serics erase (0); 10343594Sroot printf ("\n"); 10353594Sroot if (clreol) 10363594Sroot cleareol (); 10373594Sroot printf ("...skipping %d line", nlines); 10381500Serics if (nlines > 1) 10393594Sroot pr ("s\n"); 10401500Serics else 10413594Sroot pr ("\n"); 10423594Sroot 10433594Sroot if (clreol) 10443594Sroot cleareol (); 10453594Sroot pr ("\n"); 10463594Sroot 10471500Serics while (nlines > 0) { 10481500Serics while ((c = Getc (f)) != '\n') 10491500Serics if (c == EOF) { 10501500Serics retval = 0; 10511500Serics done++; 10521500Serics goto endsw; 10531500Serics } 10541500Serics Currline++; 10551500Serics nlines--; 10561500Serics } 10571500Serics ret (dlines); 10581500Serics case '\n': 10591500Serics if (nlines != 0) 10601500Serics dlines = nlines; 10611500Serics else 10621500Serics nlines = 1; 10631500Serics ret (nlines); 10641500Serics case '\f': 10651500Serics if (!no_intty) { 10661500Serics doclear (); 10671500Serics Fseek (f, screen_start.chrctr); 10681500Serics Currline = screen_start.line; 10691500Serics ret (dlines); 10701500Serics } 10711500Serics else { 10721500Serics write (2, &bell, 1); 10731500Serics break; 10741500Serics } 10751500Serics case '\'': 10761500Serics if (!no_intty) { 10771500Serics kill_line (); 10781500Serics pr ("\n***Back***\n\n"); 10791500Serics Fseek (f, context.chrctr); 10801500Serics Currline = context.line; 10811500Serics ret (dlines); 10821500Serics } 10831500Serics else { 10841500Serics write (2, &bell, 1); 10851500Serics break; 10861500Serics } 10871500Serics case '=': 10881500Serics kill_line (); 10891500Serics promptlen = printd (Currline); 10901500Serics fflush (stdout); 10911500Serics break; 10921500Serics case 'n': 10931500Serics lastp++; 10941500Serics case '/': 10951500Serics if (nlines == 0) nlines++; 10961500Serics kill_line (); 10971500Serics pr ("/"); 10981500Serics promptlen = 1; 10991500Serics fflush (stdout); 11001500Serics if (lastp) { 11011500Serics write (2,"\r", 1); 11021500Serics search (NULL, f, nlines); /* Use previous r.e. */ 11031500Serics } 11041500Serics else { 11051500Serics ttyin (cmdbuf, 78, '/'); 11061500Serics write (2, "\r", 1); 11071500Serics search (cmdbuf, f, nlines); 11081500Serics } 11093455Sroot ret (dlines-1); 11101500Serics case '!': 11111500Serics do_shell (filename); 11121500Serics break; 111324490Sbloom case '?': 11141500Serics case 'h': 11151500Serics if ((helpf = fopen (HELPFILE, "r")) == NULL) 11161500Serics error ("Can't open help file"); 11171500Serics if (noscroll) doclear (); 11181500Serics copy_file (helpf); 111927006Sdonn fclose (helpf); 11201500Serics prompt (filename); 11211500Serics break; 11221500Serics case 'v': /* This case should go right before default */ 11231500Serics if (!no_intty) { 11241500Serics kill_line (); 11251500Serics cmdbuf[0] = '+'; 112624490Sbloom scanstr (Currline - dlines < 0 ? 0 112724490Sbloom : Currline - (dlines + 1) / 2, &cmdbuf[1]); 11281500Serics pr ("vi "); pr (cmdbuf); putchar (' '); pr (fnames[fnum]); 11291500Serics execute (filename, VI, "vi", cmdbuf, fnames[fnum], 0); 11301500Serics break; 11311500Serics } 11321500Serics default: 113316710Sjak if (dum_opt) { 113416710Sjak kill_line (); 113516710Sjak if (Senter && Sexit) { 113616710Sjak tputs (Senter, 1, putch); 113716710Sjak promptlen = pr ("[Press 'h' for instructions.]") + (2 * soglitch); 113816710Sjak tputs (Sexit, 1, putch); 113916710Sjak } 114016710Sjak else 114116710Sjak promptlen = pr ("[Press 'h' for instructions.]"); 114216710Sjak fflush (stdout); 114316710Sjak } 114416710Sjak else 114516710Sjak write (2, &bell, 1); 11461500Serics break; 11471500Serics } 11481500Serics if (done) break; 11491500Serics } 11501500Serics putchar ('\r'); 11511500Serics endsw: 11521500Serics inwait = 0; 11531500Serics notell++; 11541500Serics if (MBIT == RAW && slow_tty) { 11551500Serics otty.sg_flags &= ~MBIT; 115616582Sleres stty(fileno(stderr), &otty); 11571500Serics } 11581500Serics return (retval); 11591500Serics } 11601500Serics 11611500Serics char ch; 11621500Serics 11631500Serics /* 11641500Serics * Execute a colon-prefixed command. 11651500Serics * Returns <0 if not a command that should cause 11661500Serics * more of the file to be printed. 11671500Serics */ 11681500Serics 11691500Serics colon (filename, cmd, nlines) 11701500Serics char *filename; 11711500Serics int cmd; 11721500Serics int nlines; 11731500Serics { 11741500Serics if (cmd == 0) 11751500Serics ch = readch (); 11761500Serics else 11771500Serics ch = cmd; 11781500Serics lastcolon = ch; 11791500Serics switch (ch) { 11801500Serics case 'f': 11811500Serics kill_line (); 11821500Serics if (!no_intty) 11831500Serics promptlen = printf ("\"%s\" line %d", fnames[fnum], Currline); 11841500Serics else 11851500Serics promptlen = printf ("[Not a file] line %d", Currline); 11861500Serics fflush (stdout); 11871500Serics return (-1); 11881500Serics case 'n': 11891500Serics if (nlines == 0) { 11901500Serics if (fnum >= nfiles - 1) 11911500Serics end_it (); 11921500Serics nlines++; 11931500Serics } 11941500Serics putchar ('\r'); 11951500Serics erase (0); 11961500Serics skipf (nlines); 11971500Serics return (0); 11981500Serics case 'p': 11991500Serics if (no_intty) { 12001500Serics write (2, &bell, 1); 12011500Serics return (-1); 12021500Serics } 12031500Serics putchar ('\r'); 12041500Serics erase (0); 12051500Serics if (nlines == 0) 12061500Serics nlines++; 12071500Serics skipf (-nlines); 12081500Serics return (0); 12091500Serics case '!': 12101500Serics do_shell (filename); 12111500Serics return (-1); 12121500Serics case 'q': 12131500Serics case 'Q': 12141500Serics end_it (); 12151500Serics default: 12161500Serics write (2, &bell, 1); 12171500Serics return (-1); 12181500Serics } 12191500Serics } 12201500Serics 12211500Serics /* 12221500Serics ** Read a decimal number from the terminal. Set cmd to the non-digit which 12231500Serics ** terminates the number. 12241500Serics */ 12251500Serics 12261500Serics number(cmd) 12271500Serics char *cmd; 12281500Serics { 12291500Serics register int i; 12301500Serics 12311500Serics i = 0; ch = otty.sg_kill; 12321500Serics for (;;) { 12331500Serics ch = readch (); 12341500Serics if (ch >= '0' && ch <= '9') 12351500Serics i = i*10 + ch - '0'; 12361500Serics else if (ch == otty.sg_kill) 12371500Serics i = 0; 12381500Serics else { 12391500Serics *cmd = ch; 12401500Serics break; 12411500Serics } 12421500Serics } 12431500Serics return (i); 12441500Serics } 12451500Serics 12461500Serics do_shell (filename) 12471500Serics char *filename; 12481500Serics { 12491500Serics char cmdbuf[80]; 12501500Serics 12511500Serics kill_line (); 12521500Serics pr ("!"); 12531500Serics fflush (stdout); 12541500Serics promptlen = 1; 12551500Serics if (lastp) 12561500Serics pr (shell_line); 12571500Serics else { 12581500Serics ttyin (cmdbuf, 78, '!'); 12591500Serics if (expand (shell_line, cmdbuf)) { 12601500Serics kill_line (); 12611500Serics promptlen = printf ("!%s", shell_line); 12621500Serics } 12631500Serics } 12641500Serics fflush (stdout); 12651500Serics write (2, "\n", 1); 12661500Serics promptlen = 0; 12671500Serics shellp = 1; 12681500Serics execute (filename, shell, shell, "-c", shell_line, 0); 12691500Serics } 12701500Serics 12711500Serics /* 12721500Serics ** Search for nth ocurrence of regular expression contained in buf in the file 12731500Serics */ 12741500Serics 12751500Serics search (buf, file, n) 12761500Serics char buf[]; 12771500Serics FILE *file; 12781500Serics register int n; 12791500Serics { 12801500Serics long startline = Ftell (file); 12811500Serics register long line1 = startline; 12821500Serics register long line2 = startline; 12831500Serics register long line3 = startline; 12841500Serics register int lncount; 12851500Serics int saveln, rv, re_exec(); 12861500Serics char *s, *re_comp(); 12871500Serics 12881500Serics context.line = saveln = Currline; 12891500Serics context.chrctr = startline; 12901500Serics lncount = 0; 12911500Serics if ((s = re_comp (buf)) != 0) 12921500Serics error (s); 12931500Serics while (!feof (file)) { 12941500Serics line3 = line2; 12951500Serics line2 = line1; 12961500Serics line1 = Ftell (file); 12971500Serics rdline (file); 12981500Serics lncount++; 12991500Serics if ((rv = re_exec (Line)) == 1) 13001500Serics if (--n == 0) { 13011500Serics if (lncount > 3 || (lncount > 1 && no_intty)) 13023455Sroot { 13033455Sroot pr ("\n"); 13043594Sroot if (clreol) 13053594Sroot cleareol (); 13063455Sroot pr("...skipping\n"); 13073455Sroot } 13081500Serics if (!no_intty) { 13091500Serics Currline -= (lncount >= 3 ? 3 : lncount); 13101500Serics Fseek (file, line3); 13113594Sroot if (noscroll) 13123594Sroot if (clreol) { 13133594Sroot home (); 13143594Sroot cleareol (); 131516582Sleres } 13163594Sroot else 13173594Sroot doclear (); 13181500Serics } 13191500Serics else { 13201500Serics kill_line (); 13213594Sroot if (noscroll) 13223594Sroot if (clreol) { 132316582Sleres home (); 13243594Sroot cleareol (); 132516582Sleres } 13263594Sroot else 13273594Sroot doclear (); 13281500Serics pr (Line); 13291500Serics putchar ('\n'); 13301500Serics } 13311500Serics break; 13321500Serics } 13331500Serics else if (rv == -1) 13341500Serics error ("Regular expression botch"); 13351500Serics } 13361500Serics if (feof (file)) { 13371500Serics if (!no_intty) { 13381500Serics file->_flag &= ~_IOEOF; /* why doesn't fseek do this ??!!??! */ 13391500Serics Currline = saveln; 13401500Serics Fseek (file, startline); 13411500Serics } 13421500Serics else { 13431500Serics pr ("\nPattern not found\n"); 13441500Serics end_it (); 13451500Serics } 13461500Serics error ("Pattern not found"); 13471500Serics } 13481500Serics } 13491500Serics 13501500Serics execute (filename, cmd, args) 13511500Serics char *filename; 13521500Serics char *cmd, *args; 13531500Serics { 13541500Serics int id; 135516710Sjak int n; 13561500Serics 13571500Serics fflush (stdout); 13581500Serics reset_tty (); 135916710Sjak for (n = 10; (id = fork ()) < 0 && n > 0; n--) 13601500Serics sleep (5); 13611500Serics if (id == 0) { 136216710Sjak if (!isatty(0)) { 136316710Sjak close(0); 136416710Sjak open("/dev/tty", 0); 136516710Sjak } 13661500Serics execv (cmd, &args); 13671500Serics write (2, "exec failed\n", 12); 13681500Serics exit (1); 13691500Serics } 137016710Sjak if (id > 0) { 137116710Sjak signal (SIGINT, SIG_IGN); 137216710Sjak signal (SIGQUIT, SIG_IGN); 137316710Sjak if (catch_susp) 137416710Sjak signal(SIGTSTP, SIG_DFL); 137516710Sjak while (wait(0) > 0); 137616710Sjak signal (SIGINT, end_it); 137716710Sjak signal (SIGQUIT, onquit); 137816710Sjak if (catch_susp) 137916710Sjak signal(SIGTSTP, onsusp); 138016710Sjak } else 138116710Sjak write(2, "can't fork\n", 11); 13821500Serics set_tty (); 13831500Serics pr ("------------------------\n"); 13841500Serics prompt (filename); 13851500Serics } 13861500Serics /* 13871500Serics ** Skip n lines in the file f 13881500Serics */ 13891500Serics 13901500Serics skiplns (n, f) 13911500Serics register int n; 13921500Serics register FILE *f; 13931500Serics { 13941500Serics register char c; 13951500Serics 13961500Serics while (n > 0) { 13971500Serics while ((c = Getc (f)) != '\n') 13981500Serics if (c == EOF) 13991500Serics return; 14001500Serics n--; 14011500Serics Currline++; 14021500Serics } 14031500Serics } 14041500Serics 14051500Serics /* 14061500Serics ** Skip nskip files in the file list (from the command line). Nskip may be 14071500Serics ** negative. 14081500Serics */ 14091500Serics 14101500Serics skipf (nskip) 14111500Serics register int nskip; 14121500Serics { 14131500Serics if (nskip == 0) return; 14141500Serics if (nskip > 0) { 14151500Serics if (fnum + nskip > nfiles - 1) 14161500Serics nskip = nfiles - fnum - 1; 14171500Serics } 14181500Serics else if (within) 14191500Serics ++fnum; 14201500Serics fnum += nskip; 14211500Serics if (fnum < 0) 14221500Serics fnum = 0; 14233594Sroot pr ("\n...Skipping "); 14243455Sroot pr ("\n"); 14253594Sroot if (clreol) 14263594Sroot cleareol (); 14273455Sroot pr ("...Skipping "); 14281500Serics pr (nskip > 0 ? "to file " : "back to file "); 14291500Serics pr (fnames[fnum]); 14303455Sroot pr ("\n"); 14313594Sroot if (clreol) 14323594Sroot cleareol (); 14333455Sroot pr ("\n"); 14341500Serics --fnum; 14351500Serics } 14361500Serics 14371500Serics /*----------------------------- Terminal I/O -------------------------------*/ 14381500Serics 14391500Serics initterm () 14401500Serics { 14411500Serics char buf[TBUFSIZ]; 144217195Sralph static char clearbuf[TBUFSIZ]; 14431500Serics char *clearptr, *padstr; 14441500Serics int ldisc; 144517592Sleres int lmode; 144610823Ssam char *term; 144716582Sleres int tgrp; 144818030Sbloom struct winsize win; 14491500Serics 145016582Sleres retry: 145116582Sleres if (!(no_tty = gtty(fileno(stdout), &otty))) { 145217592Sleres if (ioctl(fileno(stdout), TIOCLGET, &lmode) < 0) { 145317592Sleres perror("TIOCLGET"); 145417592Sleres exit(1); 145517592Sleres } 145617592Sleres docrterase = ((lmode & LCRTERA) != 0); 145717592Sleres docrtkill = ((lmode & LCRTKIL) != 0); 145816582Sleres /* 145917592Sleres * Wait until we're in the foreground before we save the 146017592Sleres * the terminal modes. 146116582Sleres */ 146216582Sleres if (ioctl(fileno(stdout), TIOCGPGRP, &tgrp) < 0) { 146317592Sleres perror("TIOCGPGRP"); 146416582Sleres exit(1); 146516582Sleres } 146616582Sleres if (tgrp != getpgrp(0)) { 146716582Sleres kill(0, SIGTTOU); 146816582Sleres goto retry; 146916582Sleres } 147013830Skre if ((term = getenv("TERM")) == 0 || tgetent(buf, term) <= 0) { 14713594Sroot dumb++; ul_opt = 0; 14721500Serics } 14731500Serics else { 147418030Sbloom if (ioctl(fileno(stdout), TIOCGWINSZ, &win) < 0) { 147518030Sbloom Lpp = tgetnum("li"); 147618030Sbloom Mcol = tgetnum("co"); 147718030Sbloom } else { 147818030Sbloom if ((Lpp = win.ws_row) == 0) 147918030Sbloom Lpp = tgetnum("li"); 148018030Sbloom if ((Mcol = win.ws_col) == 0) 148118030Sbloom Mcol = tgetnum("co"); 148218030Sbloom } 148318030Sbloom if ((Lpp <= 0) || tgetflag("hc")) { 14841500Serics hard++; /* Hard copy terminal */ 14851500Serics Lpp = 24; 14861500Serics } 148718030Sbloom if (Mcol <= 0) 148818030Sbloom Mcol = 80; 148918030Sbloom 14901500Serics if (tailequ (fnames[0], "page") || !hard && tgetflag("ns")) 14911500Serics noscroll++; 14921500Serics Wrap = tgetflag("am"); 14931500Serics bad_so = tgetflag ("xs"); 14941500Serics clearptr = clearbuf; 14951500Serics eraseln = tgetstr("ce",&clearptr); 14961500Serics Clear = tgetstr("cl", &clearptr); 14971500Serics Senter = tgetstr("so", &clearptr); 14981500Serics Sexit = tgetstr("se", &clearptr); 149916710Sjak if ((soglitch = tgetnum("sg")) < 0) 150016710Sjak soglitch = 0; 15013594Sroot 15023594Sroot /* 15033594Sroot * Set up for underlining: some terminals don't need it; 15043594Sroot * others have start/stop sequences, still others have an 15053594Sroot * underline char sequence which is assumed to move the 15063594Sroot * cursor forward one character. If underline sequence 15073594Sroot * isn't available, settle for standout sequence. 15083594Sroot */ 15093594Sroot 15103594Sroot if (tgetflag("ul") || tgetflag("os")) 15113594Sroot ul_opt = 0; 15123594Sroot if ((chUL = tgetstr("uc", &clearptr)) == NULL ) 15133594Sroot chUL = ""; 151416710Sjak if (((ULenter = tgetstr("us", &clearptr)) == NULL || 151516710Sjak (ULexit = tgetstr("ue", &clearptr)) == NULL) && !*chUL) { 151616710Sjak if ((ULenter = Senter) == NULL || (ULexit = Sexit) == NULL) { 151716710Sjak ULenter = ""; 151816710Sjak ULexit = ""; 151916710Sjak } else 152016710Sjak ulglitch = soglitch; 152116710Sjak } else { 152216710Sjak if ((ulglitch = tgetnum("ug")) < 0) 152316710Sjak ulglitch = 0; 152416710Sjak } 152516582Sleres 15261500Serics if (padstr = tgetstr("pc", &clearptr)) 15271500Serics PC = *padstr; 15283455Sroot Home = tgetstr("ho",&clearptr); 152913536Ssam if (Home == 0 || *Home == '\0') 15303455Sroot { 15313594Sroot if ((cursorm = tgetstr("cm", &clearptr)) != NULL) { 15323594Sroot strcpy(cursorhome, tgoto(cursorm, 0, 0)); 15333455Sroot Home = cursorhome; 15343455Sroot } 15353455Sroot } 15363594Sroot EodClr = tgetstr("cd", &clearptr); 153725540Smckusick if ((chBS = tgetstr("bc", &clearptr)) == NULL) 153825540Smckusick chBS = "\b"; 153925540Smckusick 15401500Serics } 15411500Serics if ((shell = getenv("SHELL")) == NULL) 15421500Serics shell = "/bin/sh"; 15431500Serics } 154416582Sleres no_intty = gtty(fileno(stdin), &otty); 154516582Sleres gtty(fileno(stderr), &otty); 154613830Skre savetty = otty; 15471500Serics ospeed = otty.sg_ospeed; 15481500Serics slow_tty = ospeed < B1200; 154927006Sdonn hardtabs = (otty.sg_flags & TBDELAY) != XTABS; 15501500Serics if (!no_tty) { 15511500Serics otty.sg_flags &= ~ECHO; 15521500Serics if (MBIT == CBREAK || !slow_tty) 15531500Serics otty.sg_flags |= MBIT; 15541500Serics } 15551500Serics } 15561500Serics 15571500Serics readch () 15581500Serics { 15591500Serics char ch; 15601500Serics extern int errno; 15611500Serics 15621500Serics if (read (2, &ch, 1) <= 0) 15631500Serics if (errno != EINTR) 15641500Serics exit(0); 15651500Serics else 15661500Serics ch = otty.sg_kill; 15671500Serics return (ch); 15681500Serics } 15691500Serics 15701500Serics static char BS = '\b'; 157117592Sleres static char *BSB = "\b \b"; 15721500Serics static char CARAT = '^'; 157317592Sleres #define ERASEONECHAR \ 157417592Sleres if (docrterase) \ 157517592Sleres write (2, BSB, sizeof(BSB)); \ 157617592Sleres else \ 157717592Sleres write (2, &BS, sizeof(BS)); 15781500Serics 15791500Serics ttyin (buf, nmax, pchar) 15801500Serics char buf[]; 15811500Serics register int nmax; 15821500Serics char pchar; 15831500Serics { 15841500Serics register char *sptr; 15851500Serics register char ch; 15861500Serics register int slash = 0; 15871500Serics int maxlen; 15881500Serics char cbuf; 15891500Serics 15901500Serics sptr = buf; 15911500Serics maxlen = 0; 15921500Serics while (sptr - buf < nmax) { 15931500Serics if (promptlen > maxlen) maxlen = promptlen; 15941500Serics ch = readch (); 15951500Serics if (ch == '\\') { 15961500Serics slash++; 15971500Serics } 15981500Serics else if ((ch == otty.sg_erase) && !slash) { 15991500Serics if (sptr > buf) { 16001500Serics --promptlen; 160117592Sleres ERASEONECHAR 16021500Serics --sptr; 16031500Serics if ((*sptr < ' ' && *sptr != '\n') || *sptr == RUBOUT) { 16041500Serics --promptlen; 160517592Sleres ERASEONECHAR 16061500Serics } 16071500Serics continue; 16081500Serics } 16091500Serics else { 16101500Serics if (!eraseln) promptlen = maxlen; 16111500Serics longjmp (restore, 1); 16121500Serics } 16131500Serics } 16141500Serics else if ((ch == otty.sg_kill) && !slash) { 16151500Serics if (hard) { 16161500Serics show (ch); 16171500Serics putchar ('\n'); 16181500Serics putchar (pchar); 16191500Serics } 16201500Serics else { 16211500Serics putchar ('\r'); 16221500Serics putchar (pchar); 16231500Serics if (eraseln) 16241500Serics erase (1); 162517592Sleres else if (docrtkill) 162617592Sleres while (promptlen-- > 1) 162717592Sleres write (2, BSB, sizeof(BSB)); 16281500Serics promptlen = 1; 16291500Serics } 16301500Serics sptr = buf; 16311500Serics fflush (stdout); 16321500Serics continue; 16331500Serics } 16341500Serics if (slash && (ch == otty.sg_kill || ch == otty.sg_erase)) { 163517592Sleres ERASEONECHAR 16361500Serics --sptr; 16371500Serics } 16381500Serics if (ch != '\\') 16391500Serics slash = 0; 16401500Serics *sptr++ = ch; 16411500Serics if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) { 16421500Serics ch += ch == RUBOUT ? -0100 : 0100; 16431500Serics write (2, &CARAT, 1); 16441500Serics promptlen++; 16451500Serics } 16461500Serics cbuf = ch; 16471500Serics if (ch != '\n' && ch != ESC) { 16481500Serics write (2, &cbuf, 1); 16491500Serics promptlen++; 16501500Serics } 16511500Serics else 16521500Serics break; 16531500Serics } 16541500Serics *--sptr = '\0'; 16551500Serics if (!eraseln) promptlen = maxlen; 16561500Serics if (sptr - buf >= nmax - 1) 16571500Serics error ("Line too long"); 16581500Serics } 16591500Serics 16601500Serics expand (outbuf, inbuf) 16611500Serics char *outbuf; 16621500Serics char *inbuf; 16631500Serics { 16641500Serics register char *instr; 16651500Serics register char *outstr; 16661500Serics register char ch; 16671500Serics char temp[200]; 16681500Serics int changed = 0; 16691500Serics 16701500Serics instr = inbuf; 16711500Serics outstr = temp; 16721500Serics while ((ch = *instr++) != '\0') 16731500Serics switch (ch) { 16741500Serics case '%': 16751500Serics if (!no_intty) { 16761500Serics strcpy (outstr, fnames[fnum]); 16771500Serics outstr += strlen (fnames[fnum]); 16781500Serics changed++; 16791500Serics } 16801500Serics else 16811500Serics *outstr++ = ch; 16821500Serics break; 16831500Serics case '!': 16841500Serics if (!shellp) 16851500Serics error ("No previous command to substitute for"); 16861500Serics strcpy (outstr, shell_line); 16871500Serics outstr += strlen (shell_line); 16881500Serics changed++; 16891500Serics break; 16901500Serics case '\\': 16911500Serics if (*instr == '%' || *instr == '!') { 16921500Serics *outstr++ = *instr++; 16931500Serics break; 16941500Serics } 16951500Serics default: 16961500Serics *outstr++ = ch; 16971500Serics } 16981500Serics *outstr++ = '\0'; 16991500Serics strcpy (outbuf, temp); 17001500Serics return (changed); 17011500Serics } 17021500Serics 17031500Serics show (ch) 17041500Serics register char ch; 17051500Serics { 17061500Serics char cbuf; 17071500Serics 17081500Serics if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) { 17091500Serics ch += ch == RUBOUT ? -0100 : 0100; 17101500Serics write (2, &CARAT, 1); 17111500Serics promptlen++; 17121500Serics } 17131500Serics cbuf = ch; 17141500Serics write (2, &cbuf, 1); 17151500Serics promptlen++; 17161500Serics } 17171500Serics 17181500Serics error (mess) 17191500Serics char *mess; 17201500Serics { 17213594Sroot if (clreol) 17223594Sroot cleareol (); 17233594Sroot else 17243594Sroot kill_line (); 17251500Serics promptlen += strlen (mess); 17261500Serics if (Senter && Sexit) { 17271500Serics tputs (Senter, 1, putch); 17281500Serics pr(mess); 17291500Serics tputs (Sexit, 1, putch); 17301500Serics } 17311500Serics else 17321500Serics pr (mess); 17331500Serics fflush(stdout); 17341500Serics errors++; 17351500Serics longjmp (restore, 1); 17361500Serics } 17371500Serics 17381500Serics 17391500Serics set_tty () 17401500Serics { 17411500Serics otty.sg_flags |= MBIT; 17421500Serics otty.sg_flags &= ~ECHO; 174316582Sleres stty(fileno(stderr), &otty); 17441500Serics } 17451500Serics 17461500Serics reset_tty () 17471500Serics { 174816710Sjak if (pstate) { 174916710Sjak tputs(ULexit, 1, putch); 175016710Sjak fflush(stdout); 175116710Sjak pstate = 0; 175216710Sjak } 17531500Serics otty.sg_flags |= ECHO; 17541500Serics otty.sg_flags &= ~MBIT; 175516582Sleres stty(fileno(stderr), &savetty); 17561500Serics } 17571500Serics 17581500Serics rdline (f) 17591500Serics register FILE *f; 17601500Serics { 17611500Serics register char c; 17621500Serics register char *p; 17631500Serics 17641500Serics p = Line; 17651500Serics while ((c = Getc (f)) != '\n' && c != EOF && p - Line < LINSIZ - 1) 17661500Serics *p++ = c; 17671500Serics if (c == '\n') 17681500Serics Currline++; 17691500Serics *p = '\0'; 17701500Serics } 17711500Serics 17721500Serics /* Come here when we get a suspend signal from the terminal */ 17731500Serics 17741500Serics onsusp () 17751500Serics { 177614861Skarels /* ignore SIGTTOU so we don't get stopped if csh grabs the tty */ 177714861Skarels signal(SIGTTOU, SIG_IGN); 17781500Serics reset_tty (); 17791500Serics fflush (stdout); 178014861Skarels signal(SIGTTOU, SIG_DFL); 17811500Serics /* Send the TSTP signal to suspend our process group */ 178213289Ssam signal(SIGTSTP, SIG_DFL); 178313289Ssam sigsetmask(0); 17841500Serics kill (0, SIGTSTP); 17851500Serics /* Pause for station break */ 17861500Serics 17871500Serics /* We're back */ 17881500Serics signal (SIGTSTP, onsusp); 17891500Serics set_tty (); 17901500Serics if (inwait) 17911500Serics longjmp (restore); 17921500Serics } 1793