121981Sdist /* 221981Sdist * Copyright (c) 1980 Regents of the University of California. 332736Sbostic * All rights reserved. 432736Sbostic * 532736Sbostic * Redistribution and use in source and binary forms are permitted 632736Sbostic * provided that this notice is preserved and that due credit is given 732736Sbostic * to the University of California at Berkeley. The name of the University 832736Sbostic * may not be used to endorse or promote products derived from this 932736Sbostic * software without specific written prior permission. This software 1032736Sbostic * is provided ``as is'' without express or implied warranty. 1121981Sdist */ 1221981Sdist 1313615Ssam #ifndef lint 1421981Sdist char copyright[] = 1521981Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 1621981Sdist All rights reserved.\n"; 1732736Sbostic #endif /* not lint */ 183594Sroot 1921981Sdist #ifndef lint 20*33232Sbostic static char sccsid[] = "@(#)more.c 5.17 (Berkeley) 01/03/88"; 2132736Sbostic #endif /* not lint */ 2221981Sdist 231500Serics /* 241500Serics ** more.c - General purpose tty output filter and file perusal program 251500Serics ** 261500Serics ** by Eric Shienbrood, UC Berkeley 273594Sroot ** 283594Sroot ** modified by Geoff Peck, UCB to add underlining, single spacing 293594Sroot ** modified by John Foderaro, UCB to add -c and MORE environment variable 301500Serics */ 311500Serics 321500Serics #include <stdio.h> 3332736Sbostic #include <sys/param.h> 341500Serics #include <ctype.h> 351500Serics #include <signal.h> 361500Serics #include <errno.h> 371500Serics #include <sgtty.h> 381500Serics #include <setjmp.h> 391500Serics #include <sys/stat.h> 4029906Smckusick #include <sys/file.h> 4132736Sbostic #include <a.out.h> 4232736Sbostic #include <varargs.h> 431500Serics 4413615Ssam #define HELPFILE "/usr/lib/more.help" 4513615Ssam #define VI "/usr/ucb/vi" 461500Serics 471500Serics #define Fopen(s,m) (Currline = 0,file_pos=0,fopen(s,m)) 481500Serics #define Ftell(f) file_pos 491500Serics #define Fseek(f,off) (file_pos=off,fseek(f,off,0)) 501500Serics #define Getc(f) (++file_pos, getc(f)) 511500Serics #define Ungetc(c,f) (--file_pos, ungetc(c,f)) 521500Serics 531500Serics #define MBIT CBREAK 541500Serics #define stty(fd,argp) ioctl(fd,TIOCSETN,argp) 551500Serics 561500Serics #define TBUFSIZ 1024 571500Serics #define LINSIZ 256 58*33232Sbostic #define ctrl(letter) (letter & 077) 591500Serics #define RUBOUT '\177' 601500Serics #define ESC '\033' 611500Serics #define QUIT '\034' 621500Serics 6316582Sleres struct sgttyb otty, savetty; 641500Serics long file_pos, file_size; 651500Serics int fnum, no_intty, no_tty, slow_tty; 6627006Sdonn int dum_opt, dlines, onquit(), end_it(), chgwinsz(); 671500Serics int onsusp(); 681500Serics int nscroll = 11; /* Number of lines scrolled by 'd' */ 691500Serics int fold_opt = 1; /* Fold long lines */ 701500Serics int stop_opt = 1; /* Stop after form feeds */ 713594Sroot int ssp_opt = 0; /* Suppress white space */ 723594Sroot int ul_opt = 1; /* Underline as best we can */ 731500Serics int promptlen; 741500Serics int Currline; /* Line we are currently at */ 751500Serics int startup = 1; 761500Serics int firstf = 1; 771500Serics int notell = 1; 7817592Sleres int docrterase = 0; 7917592Sleres int docrtkill = 0; 801500Serics int bad_so; /* True if overwriting does not turn off standout */ 811500Serics int inwait, Pause, errors; 821500Serics int within; /* true if we are within a file, 831500Serics false if we are between files */ 8429907Smckusick int hard, dumb, noscroll, hardtabs, clreol, eatnl; 851500Serics int catch_susp; /* We should catch the SIGTSTP signal */ 861500Serics char **fnames; /* The list of file names */ 871500Serics int nfiles; /* Number of files left to process */ 881500Serics char *shell; /* The name of the shell to use */ 891500Serics int shellp; /* A previous shell command exists */ 901500Serics char ch; 911500Serics jmp_buf restore; 921500Serics char Line[LINSIZ]; /* Line buffer */ 931500Serics int Lpp = 24; /* lines per page */ 941500Serics char *Clear; /* clear screen */ 951500Serics char *eraseln; /* erase line */ 961500Serics char *Senter, *Sexit;/* enter and exit standout mode */ 973594Sroot char *ULenter, *ULexit; /* enter and exit underline mode */ 983594Sroot char *chUL; /* underline character */ 993594Sroot char *chBS; /* backspace character */ 1003455Sroot char *Home; /* go to home */ 1013455Sroot char *cursorm; /* cursor movement */ 1023455Sroot char cursorhome[40]; /* contains cursor movement to home */ 1033594Sroot char *EodClr; /* clear rest of screen */ 1041500Serics char *tgetstr(); 1051500Serics int Mcol = 80; /* number of columns */ 1061500Serics int Wrap = 1; /* set if automargins */ 10716710Sjak int soglitch; /* terminal has standout mode glitch */ 10816710Sjak int ulglitch; /* terminal has underline mode glitch */ 10916710Sjak int pstate = 0; /* current UL state */ 1101500Serics long fseek(); 1113455Sroot char *getenv(); 1121500Serics struct { 1131500Serics long chrctr, line; 1141500Serics } context, screen_start; 1151500Serics extern char PC; /* pad character */ 1161500Serics extern short ospeed; 1171500Serics 1181500Serics 1191500Serics main(argc, argv) 1201500Serics int argc; 1211500Serics char *argv[]; 1221500Serics { 1231500Serics register FILE *f; 1241500Serics register char *s; 1251500Serics register char *p; 1261500Serics register char ch; 1271500Serics register int left; 12816582Sleres int prnames = 0; 1291500Serics int initopt = 0; 1301500Serics int srchopt = 0; 1311500Serics int clearit = 0; 1321500Serics int initline; 1331500Serics char initbuf[80]; 1341500Serics FILE *checkf(); 1351500Serics 1361500Serics nfiles = argc; 1371500Serics fnames = argv; 1381500Serics initterm (); 13915813Sralph nscroll = Lpp/2 - 1; 14015813Sralph if (nscroll <= 0) 14115813Sralph nscroll = 1; 1423455Sroot if(s = getenv("MORE")) argscan(s); 1431500Serics while (--nfiles > 0) { 1441500Serics if ((ch = (*++fnames)[0]) == '-') { 1453455Sroot argscan(*fnames+1); 1461500Serics } 1471500Serics else if (ch == '+') { 1481500Serics s = *fnames; 1491500Serics if (*++s == '/') { 1501500Serics srchopt++; 1511500Serics for (++s, p = initbuf; p < initbuf + 79 && *s != '\0';) 1521500Serics *p++ = *s++; 1531500Serics *p = '\0'; 1541500Serics } 1551500Serics else { 1561500Serics initopt++; 1571500Serics for (initline = 0; *s != '\0'; s++) 1581500Serics if (isdigit (*s)) 1591500Serics initline = initline*10 + *s -'0'; 1601500Serics --initline; 1611500Serics } 1621500Serics } 1631500Serics else break; 1641500Serics } 1653594Sroot /* allow clreol only if Home and eraseln and EodClr strings are 1663455Sroot * defined, and in that case, make sure we are in noscroll mode 1673455Sroot */ 1683455Sroot if(clreol) 1693455Sroot { 17018608Sralph if((Home == NULL) || (*Home == '\0') || 17118608Sralph (eraseln == NULL) || (*eraseln == '\0') || 17218608Sralph (EodClr == NULL) || (*EodClr == '\0') ) 17318608Sralph clreol = 0; 1743455Sroot else noscroll = 1; 1753455Sroot } 1761500Serics if (dlines == 0) 1771500Serics dlines = Lpp - (noscroll ? 1 : 2); 1781500Serics left = dlines; 1791500Serics if (nfiles > 1) 1801500Serics prnames++; 1811500Serics if (!no_intty && nfiles == 0) { 1821500Serics fputs("Usage: ",stderr); 1831500Serics fputs(argv[0],stderr); 1841500Serics fputs(" [-dfln] [+linenum | +/pattern] name1 name2 ...\n",stderr); 1851500Serics exit(1); 1861500Serics } 1871500Serics else 1881500Serics f = stdin; 1891500Serics if (!no_tty) { 1901500Serics signal(SIGQUIT, onquit); 1911500Serics signal(SIGINT, end_it); 19227006Sdonn signal(SIGWINCH, chgwinsz); 1931500Serics if (signal (SIGTSTP, SIG_IGN) == SIG_DFL) { 1941500Serics signal(SIGTSTP, onsusp); 1951500Serics catch_susp++; 1961500Serics } 19716582Sleres stty (fileno(stderr), &otty); 1981500Serics } 1991500Serics if (no_intty) { 2001500Serics if (no_tty) 2011500Serics copy_file (stdin); 2021500Serics else { 2031500Serics if ((ch = Getc (f)) == '\f') 2043594Sroot doclear(); 2051500Serics else { 2061500Serics Ungetc (ch, f); 2073594Sroot if (noscroll && (ch != EOF)) { 2083594Sroot if (clreol) 2093594Sroot home (); 2103594Sroot else 2113594Sroot doclear (); 2123455Sroot } 2131500Serics } 2141500Serics if (srchopt) 2153455Sroot { 2161500Serics search (initbuf, stdin, 1); 2173594Sroot if (noscroll) 2183594Sroot left--; 2193455Sroot } 2201500Serics else if (initopt) 2211500Serics skiplns (initline, stdin); 2221500Serics screen (stdin, left); 2231500Serics } 2241500Serics no_intty = 0; 2251500Serics prnames++; 2261500Serics firstf = 0; 2271500Serics } 2281500Serics 2291500Serics while (fnum < nfiles) { 2301500Serics if ((f = checkf (fnames[fnum], &clearit)) != NULL) { 2311500Serics context.line = context.chrctr = 0; 2321500Serics Currline = 0; 2331500Serics if (firstf) setjmp (restore); 2341500Serics if (firstf) { 2351500Serics firstf = 0; 2361500Serics if (srchopt) 2373455Sroot { 2381500Serics search (initbuf, f, 1); 2393594Sroot if (noscroll) 2403594Sroot left--; 2413455Sroot } 2421500Serics else if (initopt) 2431500Serics skiplns (initline, f); 2441500Serics } 2451500Serics else if (fnum < nfiles && !no_tty) { 2461500Serics setjmp (restore); 2471500Serics left = command (fnames[fnum], f); 2481500Serics } 2491500Serics if (left != 0) { 25032736Sbostic if ((noscroll || clearit) && (file_size != LONG_MAX)) 2513594Sroot if (clreol) 2523594Sroot home (); 2533594Sroot else 2543594Sroot doclear (); 2551500Serics if (prnames) { 2561500Serics if (bad_so) 2571500Serics erase (0); 2583594Sroot if (clreol) 2593594Sroot cleareol (); 2601500Serics pr("::::::::::::::"); 2611500Serics if (promptlen > 14) 2621500Serics erase (14); 2633455Sroot printf ("\n"); 2643455Sroot if(clreol) cleareol(); 2653455Sroot printf("%s\n", fnames[fnum]); 2663455Sroot if(clreol) cleareol(); 26730931Sbostic printf("::::::::::::::\n"); 2681500Serics if (left > Lpp - 4) 2691500Serics left = Lpp - 4; 2701500Serics } 2711500Serics if (no_tty) 2721500Serics copy_file (f); 2731500Serics else { 2741500Serics within++; 2751500Serics screen(f, left); 2761500Serics within = 0; 2771500Serics } 2781500Serics } 2791500Serics setjmp (restore); 2801500Serics fflush(stdout); 2811500Serics fclose(f); 2821500Serics screen_start.line = screen_start.chrctr = 0L; 2831500Serics context.line = context.chrctr = 0L; 2841500Serics } 2851500Serics fnum++; 2861500Serics firstf = 0; 2871500Serics } 2881500Serics reset_tty (); 2891500Serics exit(0); 2901500Serics } 2911500Serics 2923455Sroot argscan(s) 2933455Sroot char *s; 2943455Sroot { 29532273Sbostic int seen_num = 0; 29632273Sbostic 29732273Sbostic while (*s != '\0') { 29832273Sbostic switch (*s) { 29911604Slayer case '0': case '1': case '2': 30011604Slayer case '3': case '4': case '5': 30111604Slayer case '6': case '7': case '8': 30211604Slayer case '9': 30332273Sbostic if (!seen_num) { 30432273Sbostic dlines = 0; 30532273Sbostic seen_num = 1; 30632273Sbostic } 30711604Slayer dlines = dlines*10 + *s - '0'; 30811604Slayer break; 30911604Slayer case 'd': 31011604Slayer dum_opt = 1; 31111604Slayer break; 31211604Slayer case 'l': 31311604Slayer stop_opt = 0; 31411604Slayer break; 31511604Slayer case 'f': 31611604Slayer fold_opt = 0; 31711604Slayer break; 31811604Slayer case 'p': 31911604Slayer noscroll++; 32011604Slayer break; 32111604Slayer case 'c': 32211604Slayer clreol++; 32311604Slayer break; 32411604Slayer case 's': 32511604Slayer ssp_opt = 1; 32611604Slayer break; 32711604Slayer case 'u': 32811604Slayer ul_opt = 0; 32911604Slayer break; 33011604Slayer } 33132273Sbostic s++; 33211604Slayer } 3333455Sroot } 3343455Sroot 3353455Sroot 3361500Serics /* 3371500Serics ** Check whether the file named by fs is an ASCII file which the user may 3381500Serics ** access. If it is, return the opened file. Otherwise return NULL. 3391500Serics */ 3401500Serics 3411500Serics FILE * 3421500Serics checkf (fs, clearfirst) 34332736Sbostic register char *fs; 34432736Sbostic int *clearfirst; 3451500Serics { 34632736Sbostic struct stat stbuf; 34732736Sbostic register FILE *f; 34832736Sbostic char c; 3491500Serics 35032736Sbostic if (stat (fs, &stbuf) == -1) { 35132736Sbostic (void)fflush(stdout); 35232736Sbostic if (clreol) 35332736Sbostic cleareol (); 35432736Sbostic perror(fs); 35532736Sbostic return((FILE *)NULL); 35632736Sbostic } 35732736Sbostic if ((stbuf.st_mode & S_IFMT) == S_IFDIR) { 35832736Sbostic printf("\n*** %s: directory ***\n\n", fs); 35932736Sbostic return((FILE *)NULL); 36032736Sbostic } 36132736Sbostic if ((f = Fopen(fs, "r")) == NULL) { 36232736Sbostic (void)fflush(stdout); 36332736Sbostic perror(fs); 36432736Sbostic return((FILE *)NULL); 36532736Sbostic } 36632736Sbostic if (magic(f, fs)) 36732736Sbostic return((FILE *)NULL); 36832736Sbostic c = Getc(f); 36932736Sbostic *clearfirst = c == '\f'; 3701500Serics Ungetc (c, f); 37132736Sbostic if ((file_size = stbuf.st_size) == 0) 37232736Sbostic file_size = LONG_MAX; 37332736Sbostic return(f); 3741500Serics } 3751500Serics 3761500Serics /* 37732736Sbostic * magic -- 37832736Sbostic * check for file magic numbers. This code would best be shared with 37932736Sbostic * the file(1) program or, perhaps, more should not try and be so smart? 38029906Smckusick */ 38132736Sbostic static 38232736Sbostic magic(f, fs) 38332736Sbostic FILE *f; 38432736Sbostic char *fs; 38529906Smckusick { 38632736Sbostic struct exec ex; 38729906Smckusick 38832736Sbostic if (fread(&ex, sizeof(ex), 1, f) == 1) 38932736Sbostic switch(ex.a_magic) { 39032736Sbostic case OMAGIC: 39132736Sbostic case NMAGIC: 39232736Sbostic case ZMAGIC: 39332736Sbostic case 0405: 39432736Sbostic case 0411: 39532736Sbostic case 0177545: 39632736Sbostic printf("\n******** %s: Not a text file ********\n\n", fs); 39732736Sbostic (void)fclose(f); 39832736Sbostic return(1); 39932736Sbostic } 40032736Sbostic (void)fseek(f, 0L, L_SET); /* rewind() not necessary */ 40133087Sbostic return(0); 40229906Smckusick } 40329906Smckusick 40429906Smckusick /* 4051500Serics ** A real function, for the tputs routine in termlib 4061500Serics */ 4071500Serics 4081500Serics putch (ch) 4091500Serics char ch; 4101500Serics { 4111500Serics putchar (ch); 4121500Serics } 4131500Serics 4141500Serics /* 4151500Serics ** Print out the contents of the file f, one screenful at a time. 4161500Serics */ 4171500Serics 4181500Serics #define STOP -10 4191500Serics 4201500Serics screen (f, num_lines) 4211500Serics register FILE *f; 4221500Serics register int num_lines; 4231500Serics { 4241500Serics register int c; 4251500Serics register int nchars; 4263594Sroot int length; /* length of current line */ 4273594Sroot static int prev_len = 1; /* length of previous line */ 4281500Serics 4291500Serics for (;;) { 4301500Serics while (num_lines > 0 && !Pause) { 4311500Serics if ((nchars = getline (f, &length)) == EOF) 4323455Sroot { 4333594Sroot if (clreol) 4343594Sroot clreos(); 4351500Serics return; 4363455Sroot } 4373594Sroot if (ssp_opt && length == 0 && prev_len == 0) 4383594Sroot continue; 4393594Sroot prev_len = length; 4401500Serics if (bad_so || (Senter && *Senter == ' ') && promptlen > 0) 4411500Serics erase (0); 4423594Sroot /* must clear before drawing line since tabs on some terminals 4433594Sroot * do not erase what they tab over. 4443594Sroot */ 4453594Sroot if (clreol) 4463594Sroot cleareol (); 4471500Serics prbuf (Line, length); 4481500Serics if (nchars < promptlen) 4491500Serics erase (nchars); /* erase () sets promptlen to 0 */ 4501500Serics else promptlen = 0; 4513594Sroot /* is this needed? 4523594Sroot * if (clreol) 4533594Sroot * cleareol(); /* must clear again in case we wrapped * 4543594Sroot */ 4551500Serics if (nchars < Mcol || !fold_opt) 45616710Sjak prbuf("\n", 1); /* will turn off UL if necessary */ 4571500Serics if (nchars == STOP) 4581500Serics break; 4591500Serics num_lines--; 4601500Serics } 46116710Sjak if (pstate) { 46216710Sjak tputs(ULexit, 1, putch); 46316710Sjak pstate = 0; 46416710Sjak } 4651500Serics fflush(stdout); 4661500Serics if ((c = Getc(f)) == EOF) 4673455Sroot { 4683594Sroot if (clreol) 4693594Sroot clreos (); 4701500Serics return; 4713455Sroot } 4723455Sroot 4733594Sroot if (Pause && clreol) 4743594Sroot clreos (); 4751500Serics Ungetc (c, f); 4761500Serics setjmp (restore); 4771500Serics Pause = 0; startup = 0; 4781500Serics if ((num_lines = command (NULL, f)) == 0) 4791500Serics return; 4801500Serics if (hard && promptlen > 0) 4811500Serics erase (0); 48211123Slayer if (noscroll && num_lines >= dlines) 48316582Sleres { 4843594Sroot if (clreol) 4853594Sroot home(); 4863594Sroot else 4873594Sroot doclear (); 4883455Sroot } 4891500Serics screen_start.line = Currline; 4901500Serics screen_start.chrctr = Ftell (f); 4911500Serics } 4921500Serics } 4931500Serics 4941500Serics /* 4951500Serics ** Come here if a quit signal is received 4961500Serics */ 4971500Serics 4981500Serics onquit() 4991500Serics { 5001500Serics signal(SIGQUIT, SIG_IGN); 5011500Serics if (!inwait) { 5021500Serics putchar ('\n'); 5031500Serics if (!startup) { 5041500Serics signal(SIGQUIT, onquit); 5051500Serics longjmp (restore, 1); 5061500Serics } 5071500Serics else 5081500Serics Pause++; 5091500Serics } 5101500Serics else if (!dum_opt && notell) { 5111500Serics write (2, "[Use q or Q to quit]", 20); 5121500Serics promptlen += 20; 5131500Serics notell = 0; 5141500Serics } 5151500Serics signal(SIGQUIT, onquit); 5161500Serics } 5171500Serics 5181500Serics /* 51927006Sdonn ** Come here if a signal for a window size change is received 52027006Sdonn */ 52127006Sdonn 52227006Sdonn chgwinsz() 52327006Sdonn { 52427006Sdonn struct winsize win; 52527006Sdonn 52627006Sdonn (void) signal(SIGWINCH, SIG_IGN); 52727006Sdonn if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) { 52827006Sdonn if (win.ws_row != 0) { 52927006Sdonn Lpp = win.ws_row; 53027006Sdonn nscroll = Lpp/2 - 1; 53127006Sdonn if (nscroll <= 0) 53227006Sdonn nscroll = 1; 53327006Sdonn dlines = Lpp - (noscroll ? 1 : 2); 53427006Sdonn } 53527006Sdonn if (win.ws_col != 0) 53627006Sdonn Mcol = win.ws_col; 53727006Sdonn } 53827006Sdonn (void) signal(SIGWINCH, chgwinsz); 53927006Sdonn } 54027006Sdonn 54127006Sdonn /* 5421500Serics ** Clean up terminal state and exit. Also come here if interrupt signal received 5431500Serics */ 5441500Serics 5451500Serics end_it () 5461500Serics { 5471500Serics 5481500Serics reset_tty (); 5493594Sroot if (clreol) { 5503594Sroot putchar ('\r'); 5513594Sroot clreos (); 5523594Sroot fflush (stdout); 5533594Sroot } 5543455Sroot else if (!clreol && (promptlen > 0)) { 5551500Serics kill_line (); 5561500Serics fflush (stdout); 5571500Serics } 5581500Serics else 5591500Serics write (2, "\n", 1); 5601500Serics _exit(0); 5611500Serics } 5621500Serics 5631500Serics copy_file(f) 5641500Serics register FILE *f; 5651500Serics { 5661500Serics register int c; 5671500Serics 5681500Serics while ((c = getc(f)) != EOF) 5691500Serics putchar(c); 5701500Serics } 5711500Serics 5721500Serics /* Simplified printf function */ 5731500Serics 57432737Sbostic printf (fmt, va_alist) 5751500Serics register char *fmt; 57632737Sbostic va_dcl 5771500Serics { 57832737Sbostic va_list ap; 5791500Serics register char ch; 5801500Serics register int ccount; 5811500Serics 5821500Serics ccount = 0; 58332737Sbostic va_start(ap); 5841500Serics while (*fmt) { 5851500Serics while ((ch = *fmt++) != '%') { 5861500Serics if (ch == '\0') 5871500Serics return (ccount); 5881500Serics ccount++; 5891500Serics putchar (ch); 5901500Serics } 5911500Serics switch (*fmt++) { 5921500Serics case 'd': 59332737Sbostic ccount += printd (va_arg(ap, int)); 5941500Serics break; 5951500Serics case 's': 59632737Sbostic ccount += pr (va_arg(ap, char *)); 5971500Serics break; 5981500Serics case '%': 5991500Serics ccount++; 6001500Serics putchar ('%'); 6011500Serics break; 6021500Serics case '0': 6031500Serics return (ccount); 6041500Serics default: 6051500Serics break; 6061500Serics } 6071500Serics } 60832737Sbostic va_end(ap); 6091500Serics return (ccount); 6101500Serics 6111500Serics } 6121500Serics 6131500Serics /* 6141500Serics ** Print an integer as a string of decimal digits, 6151500Serics ** returning the length of the print representation. 6161500Serics */ 6171500Serics 6181500Serics printd (n) 6191500Serics int n; 6201500Serics { 6211500Serics int a, nchars; 6221500Serics 6231500Serics if (a = n/10) 6241500Serics nchars = 1 + printd(a); 6251500Serics else 6261500Serics nchars = 1; 6271500Serics putchar (n % 10 + '0'); 6281500Serics return (nchars); 6291500Serics } 6301500Serics 6311500Serics /* Put the print representation of an integer into a string */ 6321500Serics static char *sptr; 6331500Serics 6341500Serics scanstr (n, str) 6351500Serics int n; 6361500Serics char *str; 6371500Serics { 6381500Serics sptr = str; 63911604Slayer Sprintf (n); 6401500Serics *sptr = '\0'; 6411500Serics } 6421500Serics 64311604Slayer Sprintf (n) 6441500Serics { 6451500Serics int a; 6461500Serics 6471500Serics if (a = n/10) 64811604Slayer Sprintf (a); 6491500Serics *sptr++ = n % 10 + '0'; 6501500Serics } 6511500Serics 652*33232Sbostic static char bell = ctrl('G'); 6531500Serics 6541500Serics strlen (s) 6551500Serics char *s; 6561500Serics { 6571500Serics register char *p; 6581500Serics 6591500Serics p = s; 6601500Serics while (*p++) 6611500Serics ; 6621500Serics return (p - s - 1); 6631500Serics } 6641500Serics 6651500Serics /* See whether the last component of the path name "path" is equal to the 6661500Serics ** string "string" 6671500Serics */ 6681500Serics 6691500Serics tailequ (path, string) 6701500Serics char *path; 6711500Serics register char *string; 6721500Serics { 6731500Serics register char *tail; 6741500Serics 6751500Serics tail = path + strlen(path); 6761500Serics while (tail >= path) 6771500Serics if (*(--tail) == '/') 6781500Serics break; 6791500Serics ++tail; 6801500Serics while (*tail++ == *string++) 6811500Serics if (*tail == '\0') 6821500Serics return(1); 6831500Serics return(0); 6841500Serics } 6851500Serics 6861500Serics prompt (filename) 6871500Serics char *filename; 6881500Serics { 6893594Sroot if (clreol) 6903594Sroot cleareol (); 6913455Sroot else if (promptlen > 0) 6921500Serics kill_line (); 6931500Serics if (!hard) { 6941500Serics promptlen = 8; 69516710Sjak if (Senter && Sexit) { 6961500Serics tputs (Senter, 1, putch); 69716710Sjak promptlen += (2 * soglitch); 69816710Sjak } 6993594Sroot if (clreol) 7003594Sroot cleareol (); 7011500Serics pr("--More--"); 7021500Serics if (filename != NULL) { 7031500Serics promptlen += printf ("(Next file: %s)", filename); 7041500Serics } 7051500Serics else if (!no_intty) { 7061500Serics promptlen += printf ("(%d%%)", (int)((file_pos * 100) / file_size)); 7071500Serics } 7081500Serics if (dum_opt) { 70916710Sjak promptlen += pr("[Press space to continue, 'q' to quit.]"); 7101500Serics } 7111500Serics if (Senter && Sexit) 7121500Serics tputs (Sexit, 1, putch); 7133594Sroot if (clreol) 7143594Sroot clreos (); 7151500Serics fflush(stdout); 7161500Serics } 7171500Serics else 7181500Serics write (2, &bell, 1); 7191500Serics inwait++; 7201500Serics } 7211500Serics 7221500Serics /* 7231500Serics ** Get a logical line 7241500Serics */ 7251500Serics 7261500Serics getline(f, length) 7271500Serics register FILE *f; 7281500Serics int *length; 7291500Serics { 7301500Serics register int c; 7311500Serics register char *p; 7321500Serics register int column; 7331500Serics static int colflg; 7341500Serics 7351500Serics p = Line; 7361500Serics column = 0; 7371500Serics c = Getc (f); 7381500Serics if (colflg && c == '\n') { 7391500Serics Currline++; 7401500Serics c = Getc (f); 7411500Serics } 7421500Serics while (p < &Line[LINSIZ - 1]) { 7431500Serics if (c == EOF) { 7441500Serics if (p > Line) { 7451500Serics *p = '\0'; 7461500Serics *length = p - Line; 7471500Serics return (column); 7481500Serics } 7491500Serics *length = p - Line; 7501500Serics return (EOF); 7511500Serics } 7521500Serics if (c == '\n') { 7531500Serics Currline++; 7541500Serics break; 7551500Serics } 7561500Serics *p++ = c; 7571500Serics if (c == '\t') 75829907Smckusick if (!hardtabs || column < promptlen && !hard) { 75929907Smckusick if (hardtabs && eraseln && !dumb) { 7601500Serics column = 1 + (column | 7); 7611500Serics tputs (eraseln, 1, putch); 7621500Serics promptlen = 0; 7631500Serics } 7641500Serics else { 76529907Smckusick for (--p; p < &Line[LINSIZ - 1];) { 7661500Serics *p++ = ' '; 76729907Smckusick if ((++column & 7) == 0) 76829907Smckusick break; 7691500Serics } 7701500Serics if (column >= promptlen) promptlen = 0; 7711500Serics } 7721500Serics } 7731500Serics else 7741500Serics column = 1 + (column | 7); 7759627Ssklower else if (c == '\b' && column > 0) 7761500Serics column--; 7771500Serics else if (c == '\r') 7781500Serics column = 0; 7791500Serics else if (c == '\f' && stop_opt) { 7801500Serics p[-1] = '^'; 7811500Serics *p++ = 'L'; 7821500Serics column += 2; 7831500Serics Pause++; 7841500Serics } 7851500Serics else if (c == EOF) { 7861500Serics *length = p - Line; 7871500Serics return (column); 7881500Serics } 7891500Serics else if (c >= ' ' && c != RUBOUT) 7901500Serics column++; 7911500Serics if (column >= Mcol && fold_opt) break; 7921500Serics c = Getc (f); 7931500Serics } 7941500Serics if (column >= Mcol && Mcol > 0) { 7951500Serics if (!Wrap) { 7961500Serics *p++ = '\n'; 7971500Serics } 7981500Serics } 7991500Serics colflg = column == Mcol && fold_opt; 80029907Smckusick if (colflg && eatnl && Wrap) { 80129907Smckusick *p++ = '\n'; /* simulate normal wrap */ 80229907Smckusick } 8031500Serics *length = p - Line; 8041500Serics *p = 0; 8051500Serics return (column); 8061500Serics } 8071500Serics 8081500Serics /* 8091500Serics ** Erase the rest of the prompt, assuming we are starting at column col. 8101500Serics */ 8111500Serics 8121500Serics erase (col) 8131500Serics register int col; 8141500Serics { 8151500Serics 8161500Serics if (promptlen == 0) 8171500Serics return; 8181500Serics if (hard) { 8191500Serics putchar ('\n'); 8201500Serics } 8211500Serics else { 8221500Serics if (col == 0) 8231500Serics putchar ('\r'); 8241500Serics if (!dumb && eraseln) 8251500Serics tputs (eraseln, 1, putch); 8261500Serics else 8271500Serics for (col = promptlen - col; col > 0; col--) 8281500Serics putchar (' '); 8291500Serics } 8301500Serics promptlen = 0; 8311500Serics } 8321500Serics 8331500Serics /* 8341500Serics ** Erase the current line entirely 8351500Serics */ 8361500Serics 8371500Serics kill_line () 8381500Serics { 8391500Serics erase (0); 8401500Serics if (!eraseln || dumb) putchar ('\r'); 8411500Serics } 8421500Serics 8431500Serics /* 8443455Sroot * force clear to end of line 8453455Sroot */ 8463455Sroot cleareol() 8473455Sroot { 8483594Sroot tputs(eraseln, 1, putch); 8493455Sroot } 8503455Sroot 8513594Sroot clreos() 8523455Sroot { 8533594Sroot tputs(EodClr, 1, putch); 8543455Sroot } 8553455Sroot 8563455Sroot /* 8571500Serics ** Print string and return number of characters 8581500Serics */ 8591500Serics 8601500Serics pr(s1) 8611500Serics char *s1; 8621500Serics { 8631500Serics register char *s; 8641500Serics register char c; 8651500Serics 8661500Serics for (s = s1; c = *s++; ) 8671500Serics putchar(c); 8681500Serics return (s - s1 - 1); 8691500Serics } 8701500Serics 8711500Serics 8721500Serics /* Print a buffer of n characters */ 8731500Serics 8741500Serics prbuf (s, n) 8751500Serics register char *s; 8761500Serics register int n; 8771500Serics { 87816710Sjak register char c; /* next output character */ 8793594Sroot register int state; /* next output char's UL state */ 88016710Sjak #define wouldul(s,n) ((n) >= 2 && (((s)[0] == '_' && (s)[1] == '\b') || ((s)[1] == '\b' && (s)[2] == '_'))) 8813594Sroot 8823594Sroot while (--n >= 0) 8833594Sroot if (!ul_opt) 8843594Sroot putchar (*s++); 8853594Sroot else { 88616710Sjak if (*s == ' ' && pstate == 0 && ulglitch && wouldul(s+1, n-1)) { 88716710Sjak s++; 88816710Sjak continue; 88916710Sjak } 89016710Sjak if (state = wouldul(s, n)) { 89116710Sjak c = (*s == '_')? s[2] : *s ; 8923594Sroot n -= 2; 89316710Sjak s += 3; 89416710Sjak } else 8953594Sroot c = *s++; 89616710Sjak if (state != pstate) { 89716710Sjak if (c == ' ' && state == 0 && ulglitch && wouldul(s, n-1)) 89816710Sjak state = 1; 89916710Sjak else 90016710Sjak tputs(state ? ULenter : ULexit, 1, putch); 9013594Sroot } 90216710Sjak if (c != ' ' || pstate == 0 || state != 0 || ulglitch == 0) 90316710Sjak putchar(c); 9043594Sroot if (state && *chUL) { 9053594Sroot pr(chBS); 9063594Sroot tputs(chUL, 1, putch); 9073594Sroot } 90816710Sjak pstate = state; 9093594Sroot } 9101500Serics } 9111500Serics 9121500Serics /* 9131500Serics ** Clear the screen 9141500Serics */ 9151500Serics 9161500Serics doclear() 9171500Serics { 9181500Serics if (Clear && !hard) { 9191500Serics tputs(Clear, 1, putch); 9201500Serics 9211500Serics /* Put out carriage return so that system doesn't 9221500Serics ** get confused by escape sequences when expanding tabs 9231500Serics */ 9241500Serics putchar ('\r'); 9251500Serics promptlen = 0; 9261500Serics } 9271500Serics } 9281500Serics 9293455Sroot /* 9303455Sroot * Go to home position 9313455Sroot */ 9323455Sroot home() 9333455Sroot { 9343455Sroot tputs(Home,1,putch); 9353455Sroot } 9363455Sroot 9371500Serics static int lastcmd, lastarg, lastp; 9381500Serics static int lastcolon; 9391500Serics char shell_line[132]; 9401500Serics 9411500Serics /* 9421500Serics ** Read a command and do it. A command consists of an optional integer 9431500Serics ** argument followed by the command character. Return the number of lines 9441500Serics ** to display in the next screenful. If there is nothing more to display 9451500Serics ** in the current file, zero is returned. 9461500Serics */ 9471500Serics 9481500Serics command (filename, f) 9491500Serics char *filename; 9501500Serics register FILE *f; 9511500Serics { 9521500Serics register int nlines; 9531500Serics register int retval; 9541500Serics register char c; 9551500Serics char colonch; 9561500Serics FILE *helpf; 9571500Serics int done; 9581500Serics char comchar, cmdbuf[80], *p; 9591500Serics 9601500Serics #define ret(val) retval=val;done++;break 9611500Serics 9621500Serics done = 0; 9631500Serics if (!errors) 9641500Serics prompt (filename); 9651500Serics else 9661500Serics errors = 0; 9671500Serics if (MBIT == RAW && slow_tty) { 9681500Serics otty.sg_flags |= MBIT; 96916582Sleres stty(fileno(stderr), &otty); 9701500Serics } 9711500Serics for (;;) { 9721500Serics nlines = number (&comchar); 9731500Serics lastp = colonch = 0; 9741500Serics if (comchar == '.') { /* Repeat last command */ 9751500Serics lastp++; 9761500Serics comchar = lastcmd; 9771500Serics nlines = lastarg; 9781500Serics if (lastcmd == ':') 9791500Serics colonch = lastcolon; 9801500Serics } 9811500Serics lastcmd = comchar; 9821500Serics lastarg = nlines; 9831500Serics if (comchar == otty.sg_erase) { 9841500Serics kill_line (); 9851500Serics prompt (filename); 9861500Serics continue; 9871500Serics } 9881500Serics switch (comchar) { 9891500Serics case ':': 9901500Serics retval = colon (filename, colonch, nlines); 9911500Serics if (retval >= 0) 9921500Serics done++; 9931500Serics break; 99424490Sbloom case 'b': 995*33232Sbostic case ctrl('B'): 99624490Sbloom { 99724490Sbloom register int initline; 99824490Sbloom 99924490Sbloom if (no_intty) { 100024490Sbloom write(2, &bell, 1); 100124490Sbloom return (-1); 100224490Sbloom } 100324490Sbloom 100424490Sbloom if (nlines == 0) nlines++; 100524490Sbloom 100624490Sbloom putchar ('\r'); 100724490Sbloom erase (0); 100824490Sbloom printf ("\n"); 100924490Sbloom if (clreol) 101024490Sbloom cleareol (); 101124490Sbloom printf ("...back %d page", nlines); 101224490Sbloom if (nlines > 1) 101324490Sbloom pr ("s\n"); 101424490Sbloom else 101524490Sbloom pr ("\n"); 101624490Sbloom 101724490Sbloom if (clreol) 101824490Sbloom cleareol (); 101924490Sbloom pr ("\n"); 102024490Sbloom 102124490Sbloom initline = Currline - dlines * (nlines + 1); 102224490Sbloom if (! noscroll) 102324490Sbloom --initline; 102424490Sbloom if (initline < 0) initline = 0; 102524490Sbloom Fseek(f, 0L); 102624490Sbloom Currline = 0; /* skiplns() will make Currline correct */ 102724490Sbloom skiplns(initline, f); 102824490Sbloom if (! noscroll) { 102924490Sbloom ret(dlines + 1); 103024490Sbloom } 103124490Sbloom else { 103224490Sbloom ret(dlines); 103324490Sbloom } 103424490Sbloom } 10351500Serics case ' ': 10361500Serics case 'z': 10371500Serics if (nlines == 0) nlines = dlines; 10381500Serics else if (comchar == 'z') dlines = nlines; 10391500Serics ret (nlines); 10401500Serics case 'd': 1041*33232Sbostic case ctrl('D'): 10421500Serics if (nlines != 0) nscroll = nlines; 10431500Serics ret (nscroll); 10441500Serics case 'q': 10451500Serics case 'Q': 10461500Serics end_it (); 10471500Serics case 's': 10481500Serics case 'f': 10491500Serics if (nlines == 0) nlines++; 10501500Serics if (comchar == 'f') 10511500Serics nlines *= dlines; 10521500Serics putchar ('\r'); 10531500Serics erase (0); 10543594Sroot printf ("\n"); 10553594Sroot if (clreol) 10563594Sroot cleareol (); 10573594Sroot printf ("...skipping %d line", nlines); 10581500Serics if (nlines > 1) 10593594Sroot pr ("s\n"); 10601500Serics else 10613594Sroot pr ("\n"); 10623594Sroot 10633594Sroot if (clreol) 10643594Sroot cleareol (); 10653594Sroot pr ("\n"); 10663594Sroot 10671500Serics while (nlines > 0) { 10681500Serics while ((c = Getc (f)) != '\n') 10691500Serics if (c == EOF) { 10701500Serics retval = 0; 10711500Serics done++; 10721500Serics goto endsw; 10731500Serics } 10741500Serics Currline++; 10751500Serics nlines--; 10761500Serics } 10771500Serics ret (dlines); 10781500Serics case '\n': 10791500Serics if (nlines != 0) 10801500Serics dlines = nlines; 10811500Serics else 10821500Serics nlines = 1; 10831500Serics ret (nlines); 10841500Serics case '\f': 10851500Serics if (!no_intty) { 10861500Serics doclear (); 10871500Serics Fseek (f, screen_start.chrctr); 10881500Serics Currline = screen_start.line; 10891500Serics ret (dlines); 10901500Serics } 10911500Serics else { 10921500Serics write (2, &bell, 1); 10931500Serics break; 10941500Serics } 10951500Serics case '\'': 10961500Serics if (!no_intty) { 10971500Serics kill_line (); 10981500Serics pr ("\n***Back***\n\n"); 10991500Serics Fseek (f, context.chrctr); 11001500Serics Currline = context.line; 11011500Serics ret (dlines); 11021500Serics } 11031500Serics else { 11041500Serics write (2, &bell, 1); 11051500Serics break; 11061500Serics } 11071500Serics case '=': 11081500Serics kill_line (); 11091500Serics promptlen = printd (Currline); 11101500Serics fflush (stdout); 11111500Serics break; 11121500Serics case 'n': 11131500Serics lastp++; 11141500Serics case '/': 11151500Serics if (nlines == 0) nlines++; 11161500Serics kill_line (); 11171500Serics pr ("/"); 11181500Serics promptlen = 1; 11191500Serics fflush (stdout); 11201500Serics if (lastp) { 11211500Serics write (2,"\r", 1); 11221500Serics search (NULL, f, nlines); /* Use previous r.e. */ 11231500Serics } 11241500Serics else { 11251500Serics ttyin (cmdbuf, 78, '/'); 11261500Serics write (2, "\r", 1); 11271500Serics search (cmdbuf, f, nlines); 11281500Serics } 11293455Sroot ret (dlines-1); 11301500Serics case '!': 11311500Serics do_shell (filename); 11321500Serics break; 113324490Sbloom case '?': 11341500Serics case 'h': 11351500Serics if ((helpf = fopen (HELPFILE, "r")) == NULL) 11361500Serics error ("Can't open help file"); 11371500Serics if (noscroll) doclear (); 11381500Serics copy_file (helpf); 113927006Sdonn fclose (helpf); 11401500Serics prompt (filename); 11411500Serics break; 11421500Serics case 'v': /* This case should go right before default */ 11431500Serics if (!no_intty) { 11441500Serics kill_line (); 11451500Serics cmdbuf[0] = '+'; 114624490Sbloom scanstr (Currline - dlines < 0 ? 0 114724490Sbloom : Currline - (dlines + 1) / 2, &cmdbuf[1]); 11481500Serics pr ("vi "); pr (cmdbuf); putchar (' '); pr (fnames[fnum]); 11491500Serics execute (filename, VI, "vi", cmdbuf, fnames[fnum], 0); 11501500Serics break; 11511500Serics } 11521500Serics default: 115316710Sjak if (dum_opt) { 115416710Sjak kill_line (); 115516710Sjak if (Senter && Sexit) { 115616710Sjak tputs (Senter, 1, putch); 115716710Sjak promptlen = pr ("[Press 'h' for instructions.]") + (2 * soglitch); 115816710Sjak tputs (Sexit, 1, putch); 115916710Sjak } 116016710Sjak else 116116710Sjak promptlen = pr ("[Press 'h' for instructions.]"); 116216710Sjak fflush (stdout); 116316710Sjak } 116416710Sjak else 116516710Sjak write (2, &bell, 1); 11661500Serics break; 11671500Serics } 11681500Serics if (done) break; 11691500Serics } 11701500Serics putchar ('\r'); 11711500Serics endsw: 11721500Serics inwait = 0; 11731500Serics notell++; 11741500Serics if (MBIT == RAW && slow_tty) { 11751500Serics otty.sg_flags &= ~MBIT; 117616582Sleres stty(fileno(stderr), &otty); 11771500Serics } 11781500Serics return (retval); 11791500Serics } 11801500Serics 11811500Serics char ch; 11821500Serics 11831500Serics /* 11841500Serics * Execute a colon-prefixed command. 11851500Serics * Returns <0 if not a command that should cause 11861500Serics * more of the file to be printed. 11871500Serics */ 11881500Serics 11891500Serics colon (filename, cmd, nlines) 11901500Serics char *filename; 11911500Serics int cmd; 11921500Serics int nlines; 11931500Serics { 11941500Serics if (cmd == 0) 11951500Serics ch = readch (); 11961500Serics else 11971500Serics ch = cmd; 11981500Serics lastcolon = ch; 11991500Serics switch (ch) { 12001500Serics case 'f': 12011500Serics kill_line (); 12021500Serics if (!no_intty) 12031500Serics promptlen = printf ("\"%s\" line %d", fnames[fnum], Currline); 12041500Serics else 12051500Serics promptlen = printf ("[Not a file] line %d", Currline); 12061500Serics fflush (stdout); 12071500Serics return (-1); 12081500Serics case 'n': 12091500Serics if (nlines == 0) { 12101500Serics if (fnum >= nfiles - 1) 12111500Serics end_it (); 12121500Serics nlines++; 12131500Serics } 12141500Serics putchar ('\r'); 12151500Serics erase (0); 12161500Serics skipf (nlines); 12171500Serics return (0); 12181500Serics case 'p': 12191500Serics if (no_intty) { 12201500Serics write (2, &bell, 1); 12211500Serics return (-1); 12221500Serics } 12231500Serics putchar ('\r'); 12241500Serics erase (0); 12251500Serics if (nlines == 0) 12261500Serics nlines++; 12271500Serics skipf (-nlines); 12281500Serics return (0); 12291500Serics case '!': 12301500Serics do_shell (filename); 12311500Serics return (-1); 12321500Serics case 'q': 12331500Serics case 'Q': 12341500Serics end_it (); 12351500Serics default: 12361500Serics write (2, &bell, 1); 12371500Serics return (-1); 12381500Serics } 12391500Serics } 12401500Serics 12411500Serics /* 12421500Serics ** Read a decimal number from the terminal. Set cmd to the non-digit which 12431500Serics ** terminates the number. 12441500Serics */ 12451500Serics 12461500Serics number(cmd) 12471500Serics char *cmd; 12481500Serics { 12491500Serics register int i; 12501500Serics 12511500Serics i = 0; ch = otty.sg_kill; 12521500Serics for (;;) { 12531500Serics ch = readch (); 12541500Serics if (ch >= '0' && ch <= '9') 12551500Serics i = i*10 + ch - '0'; 12561500Serics else if (ch == otty.sg_kill) 12571500Serics i = 0; 12581500Serics else { 12591500Serics *cmd = ch; 12601500Serics break; 12611500Serics } 12621500Serics } 12631500Serics return (i); 12641500Serics } 12651500Serics 12661500Serics do_shell (filename) 12671500Serics char *filename; 12681500Serics { 12691500Serics char cmdbuf[80]; 12701500Serics 12711500Serics kill_line (); 12721500Serics pr ("!"); 12731500Serics fflush (stdout); 12741500Serics promptlen = 1; 12751500Serics if (lastp) 12761500Serics pr (shell_line); 12771500Serics else { 12781500Serics ttyin (cmdbuf, 78, '!'); 12791500Serics if (expand (shell_line, cmdbuf)) { 12801500Serics kill_line (); 12811500Serics promptlen = printf ("!%s", shell_line); 12821500Serics } 12831500Serics } 12841500Serics fflush (stdout); 12851500Serics write (2, "\n", 1); 12861500Serics promptlen = 0; 12871500Serics shellp = 1; 12881500Serics execute (filename, shell, shell, "-c", shell_line, 0); 12891500Serics } 12901500Serics 12911500Serics /* 12921500Serics ** Search for nth ocurrence of regular expression contained in buf in the file 12931500Serics */ 12941500Serics 12951500Serics search (buf, file, n) 12961500Serics char buf[]; 12971500Serics FILE *file; 12981500Serics register int n; 12991500Serics { 13001500Serics long startline = Ftell (file); 13011500Serics register long line1 = startline; 13021500Serics register long line2 = startline; 13031500Serics register long line3 = startline; 13041500Serics register int lncount; 13051500Serics int saveln, rv, re_exec(); 13061500Serics char *s, *re_comp(); 13071500Serics 13081500Serics context.line = saveln = Currline; 13091500Serics context.chrctr = startline; 13101500Serics lncount = 0; 13111500Serics if ((s = re_comp (buf)) != 0) 13121500Serics error (s); 13131500Serics while (!feof (file)) { 13141500Serics line3 = line2; 13151500Serics line2 = line1; 13161500Serics line1 = Ftell (file); 13171500Serics rdline (file); 13181500Serics lncount++; 13191500Serics if ((rv = re_exec (Line)) == 1) 13201500Serics if (--n == 0) { 13211500Serics if (lncount > 3 || (lncount > 1 && no_intty)) 13223455Sroot { 13233455Sroot pr ("\n"); 13243594Sroot if (clreol) 13253594Sroot cleareol (); 13263455Sroot pr("...skipping\n"); 13273455Sroot } 13281500Serics if (!no_intty) { 13291500Serics Currline -= (lncount >= 3 ? 3 : lncount); 13301500Serics Fseek (file, line3); 13313594Sroot if (noscroll) 13323594Sroot if (clreol) { 13333594Sroot home (); 13343594Sroot cleareol (); 133516582Sleres } 13363594Sroot else 13373594Sroot doclear (); 13381500Serics } 13391500Serics else { 13401500Serics kill_line (); 13413594Sroot if (noscroll) 13423594Sroot if (clreol) { 134316582Sleres home (); 13443594Sroot cleareol (); 134516582Sleres } 13463594Sroot else 13473594Sroot doclear (); 13481500Serics pr (Line); 13491500Serics putchar ('\n'); 13501500Serics } 13511500Serics break; 13521500Serics } 13531500Serics else if (rv == -1) 13541500Serics error ("Regular expression botch"); 13551500Serics } 13561500Serics if (feof (file)) { 13571500Serics if (!no_intty) { 13581500Serics file->_flag &= ~_IOEOF; /* why doesn't fseek do this ??!!??! */ 13591500Serics Currline = saveln; 13601500Serics Fseek (file, startline); 13611500Serics } 13621500Serics else { 13631500Serics pr ("\nPattern not found\n"); 13641500Serics end_it (); 13651500Serics } 13661500Serics error ("Pattern not found"); 13671500Serics } 13681500Serics } 13691500Serics 137032737Sbostic /*VARARGS2*/ 137132737Sbostic execute (filename, cmd, va_alist) 13721500Serics char *filename; 137332737Sbostic char *cmd; 137432737Sbostic va_dcl 13751500Serics { 13761500Serics int id; 137716710Sjak int n; 137832737Sbostic va_list argp; 13791500Serics 13801500Serics fflush (stdout); 13811500Serics reset_tty (); 138216710Sjak for (n = 10; (id = fork ()) < 0 && n > 0; n--) 13831500Serics sleep (5); 13841500Serics if (id == 0) { 138516710Sjak if (!isatty(0)) { 138616710Sjak close(0); 138716710Sjak open("/dev/tty", 0); 138816710Sjak } 138932737Sbostic va_start(argp); 139032737Sbostic execv (cmd, argp); 13911500Serics write (2, "exec failed\n", 12); 13921500Serics exit (1); 139333087Sbostic va_end(argp); /* balance {}'s for some UNIX's */ 13941500Serics } 139516710Sjak if (id > 0) { 139616710Sjak signal (SIGINT, SIG_IGN); 139716710Sjak signal (SIGQUIT, SIG_IGN); 139816710Sjak if (catch_susp) 139916710Sjak signal(SIGTSTP, SIG_DFL); 140016710Sjak while (wait(0) > 0); 140116710Sjak signal (SIGINT, end_it); 140216710Sjak signal (SIGQUIT, onquit); 140316710Sjak if (catch_susp) 140416710Sjak signal(SIGTSTP, onsusp); 140516710Sjak } else 140616710Sjak write(2, "can't fork\n", 11); 14071500Serics set_tty (); 14081500Serics pr ("------------------------\n"); 14091500Serics prompt (filename); 14101500Serics } 14111500Serics /* 14121500Serics ** Skip n lines in the file f 14131500Serics */ 14141500Serics 14151500Serics skiplns (n, f) 14161500Serics register int n; 14171500Serics register FILE *f; 14181500Serics { 14191500Serics register char c; 14201500Serics 14211500Serics while (n > 0) { 14221500Serics while ((c = Getc (f)) != '\n') 14231500Serics if (c == EOF) 14241500Serics return; 14251500Serics n--; 14261500Serics Currline++; 14271500Serics } 14281500Serics } 14291500Serics 14301500Serics /* 14311500Serics ** Skip nskip files in the file list (from the command line). Nskip may be 14321500Serics ** negative. 14331500Serics */ 14341500Serics 14351500Serics skipf (nskip) 14361500Serics register int nskip; 14371500Serics { 14381500Serics if (nskip == 0) return; 14391500Serics if (nskip > 0) { 14401500Serics if (fnum + nskip > nfiles - 1) 14411500Serics nskip = nfiles - fnum - 1; 14421500Serics } 14431500Serics else if (within) 14441500Serics ++fnum; 14451500Serics fnum += nskip; 14461500Serics if (fnum < 0) 14471500Serics fnum = 0; 14483594Sroot pr ("\n...Skipping "); 14493455Sroot pr ("\n"); 14503594Sroot if (clreol) 14513594Sroot cleareol (); 14523455Sroot pr ("...Skipping "); 14531500Serics pr (nskip > 0 ? "to file " : "back to file "); 14541500Serics pr (fnames[fnum]); 14553455Sroot pr ("\n"); 14563594Sroot if (clreol) 14573594Sroot cleareol (); 14583455Sroot pr ("\n"); 14591500Serics --fnum; 14601500Serics } 14611500Serics 14621500Serics /*----------------------------- Terminal I/O -------------------------------*/ 14631500Serics 14641500Serics initterm () 14651500Serics { 14661500Serics char buf[TBUFSIZ]; 146717195Sralph static char clearbuf[TBUFSIZ]; 14681500Serics char *clearptr, *padstr; 14691500Serics int ldisc; 147017592Sleres int lmode; 147110823Ssam char *term; 147216582Sleres int tgrp; 147318030Sbloom struct winsize win; 14741500Serics 147516582Sleres retry: 147616582Sleres if (!(no_tty = gtty(fileno(stdout), &otty))) { 147717592Sleres if (ioctl(fileno(stdout), TIOCLGET, &lmode) < 0) { 147817592Sleres perror("TIOCLGET"); 147917592Sleres exit(1); 148017592Sleres } 148117592Sleres docrterase = ((lmode & LCRTERA) != 0); 148217592Sleres docrtkill = ((lmode & LCRTKIL) != 0); 148316582Sleres /* 148417592Sleres * Wait until we're in the foreground before we save the 148517592Sleres * the terminal modes. 148616582Sleres */ 148716582Sleres if (ioctl(fileno(stdout), TIOCGPGRP, &tgrp) < 0) { 148817592Sleres perror("TIOCGPGRP"); 148916582Sleres exit(1); 149016582Sleres } 149116582Sleres if (tgrp != getpgrp(0)) { 149216582Sleres kill(0, SIGTTOU); 149316582Sleres goto retry; 149416582Sleres } 149513830Skre if ((term = getenv("TERM")) == 0 || tgetent(buf, term) <= 0) { 14963594Sroot dumb++; ul_opt = 0; 14971500Serics } 14981500Serics else { 149918030Sbloom if (ioctl(fileno(stdout), TIOCGWINSZ, &win) < 0) { 150018030Sbloom Lpp = tgetnum("li"); 150118030Sbloom Mcol = tgetnum("co"); 150218030Sbloom } else { 150318030Sbloom if ((Lpp = win.ws_row) == 0) 150418030Sbloom Lpp = tgetnum("li"); 150518030Sbloom if ((Mcol = win.ws_col) == 0) 150618030Sbloom Mcol = tgetnum("co"); 150718030Sbloom } 150818030Sbloom if ((Lpp <= 0) || tgetflag("hc")) { 15091500Serics hard++; /* Hard copy terminal */ 15101500Serics Lpp = 24; 15111500Serics } 151229907Smckusick if (tgetflag("xn")) 151329907Smckusick eatnl++; /* Eat newline at last column + 1; dec, concept */ 151418030Sbloom if (Mcol <= 0) 151518030Sbloom Mcol = 80; 151618030Sbloom 15171500Serics if (tailequ (fnames[0], "page") || !hard && tgetflag("ns")) 15181500Serics noscroll++; 15191500Serics Wrap = tgetflag("am"); 15201500Serics bad_so = tgetflag ("xs"); 15211500Serics clearptr = clearbuf; 15221500Serics eraseln = tgetstr("ce",&clearptr); 15231500Serics Clear = tgetstr("cl", &clearptr); 15241500Serics Senter = tgetstr("so", &clearptr); 15251500Serics Sexit = tgetstr("se", &clearptr); 152616710Sjak if ((soglitch = tgetnum("sg")) < 0) 152716710Sjak soglitch = 0; 15283594Sroot 15293594Sroot /* 15303594Sroot * Set up for underlining: some terminals don't need it; 15313594Sroot * others have start/stop sequences, still others have an 15323594Sroot * underline char sequence which is assumed to move the 15333594Sroot * cursor forward one character. If underline sequence 15343594Sroot * isn't available, settle for standout sequence. 15353594Sroot */ 15363594Sroot 15373594Sroot if (tgetflag("ul") || tgetflag("os")) 15383594Sroot ul_opt = 0; 15393594Sroot if ((chUL = tgetstr("uc", &clearptr)) == NULL ) 15403594Sroot chUL = ""; 154116710Sjak if (((ULenter = tgetstr("us", &clearptr)) == NULL || 154216710Sjak (ULexit = tgetstr("ue", &clearptr)) == NULL) && !*chUL) { 154316710Sjak if ((ULenter = Senter) == NULL || (ULexit = Sexit) == NULL) { 154416710Sjak ULenter = ""; 154516710Sjak ULexit = ""; 154616710Sjak } else 154716710Sjak ulglitch = soglitch; 154816710Sjak } else { 154916710Sjak if ((ulglitch = tgetnum("ug")) < 0) 155016710Sjak ulglitch = 0; 155116710Sjak } 155216582Sleres 15531500Serics if (padstr = tgetstr("pc", &clearptr)) 15541500Serics PC = *padstr; 15553455Sroot Home = tgetstr("ho",&clearptr); 155613536Ssam if (Home == 0 || *Home == '\0') 15573455Sroot { 15583594Sroot if ((cursorm = tgetstr("cm", &clearptr)) != NULL) { 15593594Sroot strcpy(cursorhome, tgoto(cursorm, 0, 0)); 15603455Sroot Home = cursorhome; 15613455Sroot } 15623455Sroot } 15633594Sroot EodClr = tgetstr("cd", &clearptr); 156425540Smckusick if ((chBS = tgetstr("bc", &clearptr)) == NULL) 156525540Smckusick chBS = "\b"; 156625540Smckusick 15671500Serics } 15681500Serics if ((shell = getenv("SHELL")) == NULL) 15691500Serics shell = "/bin/sh"; 15701500Serics } 157116582Sleres no_intty = gtty(fileno(stdin), &otty); 157216582Sleres gtty(fileno(stderr), &otty); 157313830Skre savetty = otty; 15741500Serics ospeed = otty.sg_ospeed; 15751500Serics slow_tty = ospeed < B1200; 157627006Sdonn hardtabs = (otty.sg_flags & TBDELAY) != XTABS; 15771500Serics if (!no_tty) { 15781500Serics otty.sg_flags &= ~ECHO; 15791500Serics if (MBIT == CBREAK || !slow_tty) 15801500Serics otty.sg_flags |= MBIT; 15811500Serics } 15821500Serics } 15831500Serics 15841500Serics readch () 15851500Serics { 15861500Serics char ch; 15871500Serics extern int errno; 15881500Serics 158931089Skarels errno = 0; 15901500Serics if (read (2, &ch, 1) <= 0) 15911500Serics if (errno != EINTR) 159231089Skarels end_it(); 15931500Serics else 15941500Serics ch = otty.sg_kill; 15951500Serics return (ch); 15961500Serics } 15971500Serics 15981500Serics static char BS = '\b'; 159917592Sleres static char *BSB = "\b \b"; 16001500Serics static char CARAT = '^'; 160117592Sleres #define ERASEONECHAR \ 160217592Sleres if (docrterase) \ 160317592Sleres write (2, BSB, sizeof(BSB)); \ 160417592Sleres else \ 160517592Sleres write (2, &BS, sizeof(BS)); 16061500Serics 16071500Serics ttyin (buf, nmax, pchar) 16081500Serics char buf[]; 16091500Serics register int nmax; 16101500Serics char pchar; 16111500Serics { 16121500Serics register char *sptr; 16131500Serics register char ch; 16141500Serics register int slash = 0; 16151500Serics int maxlen; 16161500Serics char cbuf; 16171500Serics 16181500Serics sptr = buf; 16191500Serics maxlen = 0; 16201500Serics while (sptr - buf < nmax) { 16211500Serics if (promptlen > maxlen) maxlen = promptlen; 16221500Serics ch = readch (); 16231500Serics if (ch == '\\') { 16241500Serics slash++; 16251500Serics } 16261500Serics else if ((ch == otty.sg_erase) && !slash) { 16271500Serics if (sptr > buf) { 16281500Serics --promptlen; 162917592Sleres ERASEONECHAR 16301500Serics --sptr; 16311500Serics if ((*sptr < ' ' && *sptr != '\n') || *sptr == RUBOUT) { 16321500Serics --promptlen; 163317592Sleres ERASEONECHAR 16341500Serics } 16351500Serics continue; 16361500Serics } 16371500Serics else { 16381500Serics if (!eraseln) promptlen = maxlen; 16391500Serics longjmp (restore, 1); 16401500Serics } 16411500Serics } 16421500Serics else if ((ch == otty.sg_kill) && !slash) { 16431500Serics if (hard) { 16441500Serics show (ch); 16451500Serics putchar ('\n'); 16461500Serics putchar (pchar); 16471500Serics } 16481500Serics else { 16491500Serics putchar ('\r'); 16501500Serics putchar (pchar); 16511500Serics if (eraseln) 16521500Serics erase (1); 165317592Sleres else if (docrtkill) 165417592Sleres while (promptlen-- > 1) 165517592Sleres write (2, BSB, sizeof(BSB)); 16561500Serics promptlen = 1; 16571500Serics } 16581500Serics sptr = buf; 16591500Serics fflush (stdout); 16601500Serics continue; 16611500Serics } 16621500Serics if (slash && (ch == otty.sg_kill || ch == otty.sg_erase)) { 166317592Sleres ERASEONECHAR 16641500Serics --sptr; 16651500Serics } 16661500Serics if (ch != '\\') 16671500Serics slash = 0; 16681500Serics *sptr++ = ch; 16691500Serics if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) { 16701500Serics ch += ch == RUBOUT ? -0100 : 0100; 16711500Serics write (2, &CARAT, 1); 16721500Serics promptlen++; 16731500Serics } 16741500Serics cbuf = ch; 16751500Serics if (ch != '\n' && ch != ESC) { 16761500Serics write (2, &cbuf, 1); 16771500Serics promptlen++; 16781500Serics } 16791500Serics else 16801500Serics break; 16811500Serics } 16821500Serics *--sptr = '\0'; 16831500Serics if (!eraseln) promptlen = maxlen; 16841500Serics if (sptr - buf >= nmax - 1) 16851500Serics error ("Line too long"); 16861500Serics } 16871500Serics 16881500Serics expand (outbuf, inbuf) 16891500Serics char *outbuf; 16901500Serics char *inbuf; 16911500Serics { 16921500Serics register char *instr; 16931500Serics register char *outstr; 16941500Serics register char ch; 16951500Serics char temp[200]; 16961500Serics int changed = 0; 16971500Serics 16981500Serics instr = inbuf; 16991500Serics outstr = temp; 17001500Serics while ((ch = *instr++) != '\0') 17011500Serics switch (ch) { 17021500Serics case '%': 17031500Serics if (!no_intty) { 17041500Serics strcpy (outstr, fnames[fnum]); 17051500Serics outstr += strlen (fnames[fnum]); 17061500Serics changed++; 17071500Serics } 17081500Serics else 17091500Serics *outstr++ = ch; 17101500Serics break; 17111500Serics case '!': 17121500Serics if (!shellp) 17131500Serics error ("No previous command to substitute for"); 17141500Serics strcpy (outstr, shell_line); 17151500Serics outstr += strlen (shell_line); 17161500Serics changed++; 17171500Serics break; 17181500Serics case '\\': 17191500Serics if (*instr == '%' || *instr == '!') { 17201500Serics *outstr++ = *instr++; 17211500Serics break; 17221500Serics } 17231500Serics default: 17241500Serics *outstr++ = ch; 17251500Serics } 17261500Serics *outstr++ = '\0'; 17271500Serics strcpy (outbuf, temp); 17281500Serics return (changed); 17291500Serics } 17301500Serics 17311500Serics show (ch) 17321500Serics register char ch; 17331500Serics { 17341500Serics char cbuf; 17351500Serics 17361500Serics if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) { 17371500Serics ch += ch == RUBOUT ? -0100 : 0100; 17381500Serics write (2, &CARAT, 1); 17391500Serics promptlen++; 17401500Serics } 17411500Serics cbuf = ch; 17421500Serics write (2, &cbuf, 1); 17431500Serics promptlen++; 17441500Serics } 17451500Serics 17461500Serics error (mess) 17471500Serics char *mess; 17481500Serics { 17493594Sroot if (clreol) 17503594Sroot cleareol (); 17513594Sroot else 17523594Sroot kill_line (); 17531500Serics promptlen += strlen (mess); 17541500Serics if (Senter && Sexit) { 17551500Serics tputs (Senter, 1, putch); 17561500Serics pr(mess); 17571500Serics tputs (Sexit, 1, putch); 17581500Serics } 17591500Serics else 17601500Serics pr (mess); 17611500Serics fflush(stdout); 17621500Serics errors++; 17631500Serics longjmp (restore, 1); 17641500Serics } 17651500Serics 17661500Serics 17671500Serics set_tty () 17681500Serics { 17691500Serics otty.sg_flags |= MBIT; 17701500Serics otty.sg_flags &= ~ECHO; 177116582Sleres stty(fileno(stderr), &otty); 17721500Serics } 17731500Serics 17741500Serics reset_tty () 17751500Serics { 177631033Sbostic if (no_tty) 177731033Sbostic return; 177816710Sjak if (pstate) { 177916710Sjak tputs(ULexit, 1, putch); 178016710Sjak fflush(stdout); 178116710Sjak pstate = 0; 178216710Sjak } 17831500Serics otty.sg_flags |= ECHO; 17841500Serics otty.sg_flags &= ~MBIT; 178516582Sleres stty(fileno(stderr), &savetty); 17861500Serics } 17871500Serics 17881500Serics rdline (f) 17891500Serics register FILE *f; 17901500Serics { 17911500Serics register char c; 17921500Serics register char *p; 17931500Serics 17941500Serics p = Line; 17951500Serics while ((c = Getc (f)) != '\n' && c != EOF && p - Line < LINSIZ - 1) 17961500Serics *p++ = c; 17971500Serics if (c == '\n') 17981500Serics Currline++; 17991500Serics *p = '\0'; 18001500Serics } 18011500Serics 18021500Serics /* Come here when we get a suspend signal from the terminal */ 18031500Serics 18041500Serics onsusp () 18051500Serics { 180614861Skarels /* ignore SIGTTOU so we don't get stopped if csh grabs the tty */ 180714861Skarels signal(SIGTTOU, SIG_IGN); 18081500Serics reset_tty (); 18091500Serics fflush (stdout); 181014861Skarels signal(SIGTTOU, SIG_DFL); 18111500Serics /* Send the TSTP signal to suspend our process group */ 181213289Ssam signal(SIGTSTP, SIG_DFL); 181313289Ssam sigsetmask(0); 18141500Serics kill (0, SIGTSTP); 18151500Serics /* Pause for station break */ 18161500Serics 18171500Serics /* We're back */ 18181500Serics signal (SIGTSTP, onsusp); 18191500Serics set_tty (); 18201500Serics if (inwait) 18211500Serics longjmp (restore); 18221500Serics } 1823