121981Sdist /* 221981Sdist * Copyright (c) 1980 Regents of the University of California. 3*32736Sbostic * All rights reserved. 4*32736Sbostic * 5*32736Sbostic * Redistribution and use in source and binary forms are permitted 6*32736Sbostic * provided that this notice is preserved and that due credit is given 7*32736Sbostic * to the University of California at Berkeley. The name of the University 8*32736Sbostic * may not be used to endorse or promote products derived from this 9*32736Sbostic * software without specific written prior permission. This software 10*32736Sbostic * 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"; 17*32736Sbostic #endif /* not lint */ 183594Sroot 1921981Sdist #ifndef lint 20*32736Sbostic static char sccsid[] = "@(#)more.c 5.14 (Berkeley) 12/02/87"; 21*32736Sbostic #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> 33*32736Sbostic #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> 41*32736Sbostic #include <a.out.h> 42*32736Sbostic #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 581500Serics #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) { 250*32736Sbostic 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) 343*32736Sbostic register char *fs; 344*32736Sbostic int *clearfirst; 3451500Serics { 346*32736Sbostic struct stat stbuf; 347*32736Sbostic register FILE *f; 348*32736Sbostic char c; 3491500Serics 350*32736Sbostic if (stat (fs, &stbuf) == -1) { 351*32736Sbostic (void)fflush(stdout); 352*32736Sbostic if (clreol) 353*32736Sbostic cleareol (); 354*32736Sbostic perror(fs); 355*32736Sbostic return((FILE *)NULL); 356*32736Sbostic } 357*32736Sbostic if ((stbuf.st_mode & S_IFMT) == S_IFDIR) { 358*32736Sbostic printf("\n*** %s: directory ***\n\n", fs); 359*32736Sbostic return((FILE *)NULL); 360*32736Sbostic } 361*32736Sbostic if ((f = Fopen(fs, "r")) == NULL) { 362*32736Sbostic (void)fflush(stdout); 363*32736Sbostic perror(fs); 364*32736Sbostic return((FILE *)NULL); 365*32736Sbostic } 366*32736Sbostic if (magic(f, fs)) 367*32736Sbostic return((FILE *)NULL); 368*32736Sbostic c = Getc(f); 369*32736Sbostic *clearfirst = c == '\f'; 3701500Serics Ungetc (c, f); 371*32736Sbostic if ((file_size = stbuf.st_size) == 0) 372*32736Sbostic file_size = LONG_MAX; 373*32736Sbostic return(f); 3741500Serics } 3751500Serics 3761500Serics /* 377*32736Sbostic * magic -- 378*32736Sbostic * check for file magic numbers. This code would best be shared with 379*32736Sbostic * the file(1) program or, perhaps, more should not try and be so smart? 38029906Smckusick */ 381*32736Sbostic static 382*32736Sbostic magic(f, fs) 383*32736Sbostic FILE *f; 384*32736Sbostic char *fs; 38529906Smckusick { 386*32736Sbostic struct exec ex; 38729906Smckusick 388*32736Sbostic if (fread(&ex, sizeof(ex), 1, f) == 1) 389*32736Sbostic switch(ex.a_magic) { 390*32736Sbostic case OMAGIC: 391*32736Sbostic case NMAGIC: 392*32736Sbostic case ZMAGIC: 393*32736Sbostic case 0405: 394*32736Sbostic case 0411: 395*32736Sbostic case 0177545: 396*32736Sbostic printf("\n******** %s: Not a text file ********\n\n", fs); 397*32736Sbostic (void)fclose(f); 398*32736Sbostic return(1); 399*32736Sbostic } 400*32736Sbostic (void)fseek(f, 0L, L_SET); /* rewind() not necessary */ 401*32736Sbostic return(NULL); 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 5741500Serics printf (fmt, args) 5751500Serics register char *fmt; 5761500Serics int args; 5771500Serics { 5781500Serics register int *argp; 5791500Serics register char ch; 5801500Serics register int ccount; 5811500Serics 5821500Serics ccount = 0; 5831500Serics argp = &args; 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': 5931500Serics ccount += printd (*argp); 5941500Serics break; 5951500Serics case 's': 5961500Serics ccount += pr ((char *)*argp); 5971500Serics break; 5981500Serics case '%': 5991500Serics ccount++; 6001500Serics argp--; 6011500Serics putchar ('%'); 6021500Serics break; 6031500Serics case '0': 6041500Serics return (ccount); 6051500Serics default: 6061500Serics break; 6071500Serics } 6081500Serics ++argp; 6091500Serics } 6101500Serics return (ccount); 6111500Serics 6121500Serics } 6131500Serics 6141500Serics /* 6151500Serics ** Print an integer as a string of decimal digits, 6161500Serics ** returning the length of the print representation. 6171500Serics */ 6181500Serics 6191500Serics printd (n) 6201500Serics int n; 6211500Serics { 6221500Serics int a, nchars; 6231500Serics 6241500Serics if (a = n/10) 6251500Serics nchars = 1 + printd(a); 6261500Serics else 6271500Serics nchars = 1; 6281500Serics putchar (n % 10 + '0'); 6291500Serics return (nchars); 6301500Serics } 6311500Serics 6321500Serics /* Put the print representation of an integer into a string */ 6331500Serics static char *sptr; 6341500Serics 6351500Serics scanstr (n, str) 6361500Serics int n; 6371500Serics char *str; 6381500Serics { 6391500Serics sptr = str; 64011604Slayer Sprintf (n); 6411500Serics *sptr = '\0'; 6421500Serics } 6431500Serics 64411604Slayer Sprintf (n) 6451500Serics { 6461500Serics int a; 6471500Serics 6481500Serics if (a = n/10) 64911604Slayer Sprintf (a); 6501500Serics *sptr++ = n % 10 + '0'; 6511500Serics } 6521500Serics 6531500Serics static char bell = ctrl(G); 6541500Serics 6551500Serics strlen (s) 6561500Serics char *s; 6571500Serics { 6581500Serics register char *p; 6591500Serics 6601500Serics p = s; 6611500Serics while (*p++) 6621500Serics ; 6631500Serics return (p - s - 1); 6641500Serics } 6651500Serics 6661500Serics /* See whether the last component of the path name "path" is equal to the 6671500Serics ** string "string" 6681500Serics */ 6691500Serics 6701500Serics tailequ (path, string) 6711500Serics char *path; 6721500Serics register char *string; 6731500Serics { 6741500Serics register char *tail; 6751500Serics 6761500Serics tail = path + strlen(path); 6771500Serics while (tail >= path) 6781500Serics if (*(--tail) == '/') 6791500Serics break; 6801500Serics ++tail; 6811500Serics while (*tail++ == *string++) 6821500Serics if (*tail == '\0') 6831500Serics return(1); 6841500Serics return(0); 6851500Serics } 6861500Serics 6871500Serics prompt (filename) 6881500Serics char *filename; 6891500Serics { 6903594Sroot if (clreol) 6913594Sroot cleareol (); 6923455Sroot else if (promptlen > 0) 6931500Serics kill_line (); 6941500Serics if (!hard) { 6951500Serics promptlen = 8; 69616710Sjak if (Senter && Sexit) { 6971500Serics tputs (Senter, 1, putch); 69816710Sjak promptlen += (2 * soglitch); 69916710Sjak } 7003594Sroot if (clreol) 7013594Sroot cleareol (); 7021500Serics pr("--More--"); 7031500Serics if (filename != NULL) { 7041500Serics promptlen += printf ("(Next file: %s)", filename); 7051500Serics } 7061500Serics else if (!no_intty) { 7071500Serics promptlen += printf ("(%d%%)", (int)((file_pos * 100) / file_size)); 7081500Serics } 7091500Serics if (dum_opt) { 71016710Sjak promptlen += pr("[Press space to continue, 'q' to quit.]"); 7111500Serics } 7121500Serics if (Senter && Sexit) 7131500Serics tputs (Sexit, 1, putch); 7143594Sroot if (clreol) 7153594Sroot clreos (); 7161500Serics fflush(stdout); 7171500Serics } 7181500Serics else 7191500Serics write (2, &bell, 1); 7201500Serics inwait++; 7211500Serics } 7221500Serics 7231500Serics /* 7241500Serics ** Get a logical line 7251500Serics */ 7261500Serics 7271500Serics getline(f, length) 7281500Serics register FILE *f; 7291500Serics int *length; 7301500Serics { 7311500Serics register int c; 7321500Serics register char *p; 7331500Serics register int column; 7341500Serics static int colflg; 7351500Serics 7361500Serics p = Line; 7371500Serics column = 0; 7381500Serics c = Getc (f); 7391500Serics if (colflg && c == '\n') { 7401500Serics Currline++; 7411500Serics c = Getc (f); 7421500Serics } 7431500Serics while (p < &Line[LINSIZ - 1]) { 7441500Serics if (c == EOF) { 7451500Serics if (p > Line) { 7461500Serics *p = '\0'; 7471500Serics *length = p - Line; 7481500Serics return (column); 7491500Serics } 7501500Serics *length = p - Line; 7511500Serics return (EOF); 7521500Serics } 7531500Serics if (c == '\n') { 7541500Serics Currline++; 7551500Serics break; 7561500Serics } 7571500Serics *p++ = c; 7581500Serics if (c == '\t') 75929907Smckusick if (!hardtabs || column < promptlen && !hard) { 76029907Smckusick if (hardtabs && eraseln && !dumb) { 7611500Serics column = 1 + (column | 7); 7621500Serics tputs (eraseln, 1, putch); 7631500Serics promptlen = 0; 7641500Serics } 7651500Serics else { 76629907Smckusick for (--p; p < &Line[LINSIZ - 1];) { 7671500Serics *p++ = ' '; 76829907Smckusick if ((++column & 7) == 0) 76929907Smckusick break; 7701500Serics } 7711500Serics if (column >= promptlen) promptlen = 0; 7721500Serics } 7731500Serics } 7741500Serics else 7751500Serics column = 1 + (column | 7); 7769627Ssklower else if (c == '\b' && column > 0) 7771500Serics column--; 7781500Serics else if (c == '\r') 7791500Serics column = 0; 7801500Serics else if (c == '\f' && stop_opt) { 7811500Serics p[-1] = '^'; 7821500Serics *p++ = 'L'; 7831500Serics column += 2; 7841500Serics Pause++; 7851500Serics } 7861500Serics else if (c == EOF) { 7871500Serics *length = p - Line; 7881500Serics return (column); 7891500Serics } 7901500Serics else if (c >= ' ' && c != RUBOUT) 7911500Serics column++; 7921500Serics if (column >= Mcol && fold_opt) break; 7931500Serics c = Getc (f); 7941500Serics } 7951500Serics if (column >= Mcol && Mcol > 0) { 7961500Serics if (!Wrap) { 7971500Serics *p++ = '\n'; 7981500Serics } 7991500Serics } 8001500Serics colflg = column == Mcol && fold_opt; 80129907Smckusick if (colflg && eatnl && Wrap) { 80229907Smckusick *p++ = '\n'; /* simulate normal wrap */ 80329907Smckusick } 8041500Serics *length = p - Line; 8051500Serics *p = 0; 8061500Serics return (column); 8071500Serics } 8081500Serics 8091500Serics /* 8101500Serics ** Erase the rest of the prompt, assuming we are starting at column col. 8111500Serics */ 8121500Serics 8131500Serics erase (col) 8141500Serics register int col; 8151500Serics { 8161500Serics 8171500Serics if (promptlen == 0) 8181500Serics return; 8191500Serics if (hard) { 8201500Serics putchar ('\n'); 8211500Serics } 8221500Serics else { 8231500Serics if (col == 0) 8241500Serics putchar ('\r'); 8251500Serics if (!dumb && eraseln) 8261500Serics tputs (eraseln, 1, putch); 8271500Serics else 8281500Serics for (col = promptlen - col; col > 0; col--) 8291500Serics putchar (' '); 8301500Serics } 8311500Serics promptlen = 0; 8321500Serics } 8331500Serics 8341500Serics /* 8351500Serics ** Erase the current line entirely 8361500Serics */ 8371500Serics 8381500Serics kill_line () 8391500Serics { 8401500Serics erase (0); 8411500Serics if (!eraseln || dumb) putchar ('\r'); 8421500Serics } 8431500Serics 8441500Serics /* 8453455Sroot * force clear to end of line 8463455Sroot */ 8473455Sroot cleareol() 8483455Sroot { 8493594Sroot tputs(eraseln, 1, putch); 8503455Sroot } 8513455Sroot 8523594Sroot clreos() 8533455Sroot { 8543594Sroot tputs(EodClr, 1, putch); 8553455Sroot } 8563455Sroot 8573455Sroot /* 8581500Serics ** Print string and return number of characters 8591500Serics */ 8601500Serics 8611500Serics pr(s1) 8621500Serics char *s1; 8631500Serics { 8641500Serics register char *s; 8651500Serics register char c; 8661500Serics 8671500Serics for (s = s1; c = *s++; ) 8681500Serics putchar(c); 8691500Serics return (s - s1 - 1); 8701500Serics } 8711500Serics 8721500Serics 8731500Serics /* Print a buffer of n characters */ 8741500Serics 8751500Serics prbuf (s, n) 8761500Serics register char *s; 8771500Serics register int n; 8781500Serics { 87916710Sjak register char c; /* next output character */ 8803594Sroot register int state; /* next output char's UL state */ 88116710Sjak #define wouldul(s,n) ((n) >= 2 && (((s)[0] == '_' && (s)[1] == '\b') || ((s)[1] == '\b' && (s)[2] == '_'))) 8823594Sroot 8833594Sroot while (--n >= 0) 8843594Sroot if (!ul_opt) 8853594Sroot putchar (*s++); 8863594Sroot else { 88716710Sjak if (*s == ' ' && pstate == 0 && ulglitch && wouldul(s+1, n-1)) { 88816710Sjak s++; 88916710Sjak continue; 89016710Sjak } 89116710Sjak if (state = wouldul(s, n)) { 89216710Sjak c = (*s == '_')? s[2] : *s ; 8933594Sroot n -= 2; 89416710Sjak s += 3; 89516710Sjak } else 8963594Sroot c = *s++; 89716710Sjak if (state != pstate) { 89816710Sjak if (c == ' ' && state == 0 && ulglitch && wouldul(s, n-1)) 89916710Sjak state = 1; 90016710Sjak else 90116710Sjak tputs(state ? ULenter : ULexit, 1, putch); 9023594Sroot } 90316710Sjak if (c != ' ' || pstate == 0 || state != 0 || ulglitch == 0) 90416710Sjak putchar(c); 9053594Sroot if (state && *chUL) { 9063594Sroot pr(chBS); 9073594Sroot tputs(chUL, 1, putch); 9083594Sroot } 90916710Sjak pstate = state; 9103594Sroot } 9111500Serics } 9121500Serics 9131500Serics /* 9141500Serics ** Clear the screen 9151500Serics */ 9161500Serics 9171500Serics doclear() 9181500Serics { 9191500Serics if (Clear && !hard) { 9201500Serics tputs(Clear, 1, putch); 9211500Serics 9221500Serics /* Put out carriage return so that system doesn't 9231500Serics ** get confused by escape sequences when expanding tabs 9241500Serics */ 9251500Serics putchar ('\r'); 9261500Serics promptlen = 0; 9271500Serics } 9281500Serics } 9291500Serics 9303455Sroot /* 9313455Sroot * Go to home position 9323455Sroot */ 9333455Sroot home() 9343455Sroot { 9353455Sroot tputs(Home,1,putch); 9363455Sroot } 9373455Sroot 9381500Serics static int lastcmd, lastarg, lastp; 9391500Serics static int lastcolon; 9401500Serics char shell_line[132]; 9411500Serics 9421500Serics /* 9431500Serics ** Read a command and do it. A command consists of an optional integer 9441500Serics ** argument followed by the command character. Return the number of lines 9451500Serics ** to display in the next screenful. If there is nothing more to display 9461500Serics ** in the current file, zero is returned. 9471500Serics */ 9481500Serics 9491500Serics command (filename, f) 9501500Serics char *filename; 9511500Serics register FILE *f; 9521500Serics { 9531500Serics register int nlines; 9541500Serics register int retval; 9551500Serics register char c; 9561500Serics char colonch; 9571500Serics FILE *helpf; 9581500Serics int done; 9591500Serics char comchar, cmdbuf[80], *p; 9601500Serics 9611500Serics #define ret(val) retval=val;done++;break 9621500Serics 9631500Serics done = 0; 9641500Serics if (!errors) 9651500Serics prompt (filename); 9661500Serics else 9671500Serics errors = 0; 9681500Serics if (MBIT == RAW && slow_tty) { 9691500Serics otty.sg_flags |= MBIT; 97016582Sleres stty(fileno(stderr), &otty); 9711500Serics } 9721500Serics for (;;) { 9731500Serics nlines = number (&comchar); 9741500Serics lastp = colonch = 0; 9751500Serics if (comchar == '.') { /* Repeat last command */ 9761500Serics lastp++; 9771500Serics comchar = lastcmd; 9781500Serics nlines = lastarg; 9791500Serics if (lastcmd == ':') 9801500Serics colonch = lastcolon; 9811500Serics } 9821500Serics lastcmd = comchar; 9831500Serics lastarg = nlines; 9841500Serics if (comchar == otty.sg_erase) { 9851500Serics kill_line (); 9861500Serics prompt (filename); 9871500Serics continue; 9881500Serics } 9891500Serics switch (comchar) { 9901500Serics case ':': 9911500Serics retval = colon (filename, colonch, nlines); 9921500Serics if (retval >= 0) 9931500Serics done++; 9941500Serics break; 99524490Sbloom case 'b': 99624490Sbloom case ctrl(B): 99724490Sbloom { 99824490Sbloom register int initline; 99924490Sbloom 100024490Sbloom if (no_intty) { 100124490Sbloom write(2, &bell, 1); 100224490Sbloom return (-1); 100324490Sbloom } 100424490Sbloom 100524490Sbloom if (nlines == 0) nlines++; 100624490Sbloom 100724490Sbloom putchar ('\r'); 100824490Sbloom erase (0); 100924490Sbloom printf ("\n"); 101024490Sbloom if (clreol) 101124490Sbloom cleareol (); 101224490Sbloom printf ("...back %d page", nlines); 101324490Sbloom if (nlines > 1) 101424490Sbloom pr ("s\n"); 101524490Sbloom else 101624490Sbloom pr ("\n"); 101724490Sbloom 101824490Sbloom if (clreol) 101924490Sbloom cleareol (); 102024490Sbloom pr ("\n"); 102124490Sbloom 102224490Sbloom initline = Currline - dlines * (nlines + 1); 102324490Sbloom if (! noscroll) 102424490Sbloom --initline; 102524490Sbloom if (initline < 0) initline = 0; 102624490Sbloom Fseek(f, 0L); 102724490Sbloom Currline = 0; /* skiplns() will make Currline correct */ 102824490Sbloom skiplns(initline, f); 102924490Sbloom if (! noscroll) { 103024490Sbloom ret(dlines + 1); 103124490Sbloom } 103224490Sbloom else { 103324490Sbloom ret(dlines); 103424490Sbloom } 103524490Sbloom } 10361500Serics case ' ': 10371500Serics case 'z': 10381500Serics if (nlines == 0) nlines = dlines; 10391500Serics else if (comchar == 'z') dlines = nlines; 10401500Serics ret (nlines); 10411500Serics case 'd': 10421500Serics case ctrl(D): 10431500Serics if (nlines != 0) nscroll = nlines; 10441500Serics ret (nscroll); 10451500Serics case 'q': 10461500Serics case 'Q': 10471500Serics end_it (); 10481500Serics case 's': 10491500Serics case 'f': 10501500Serics if (nlines == 0) nlines++; 10511500Serics if (comchar == 'f') 10521500Serics nlines *= dlines; 10531500Serics putchar ('\r'); 10541500Serics erase (0); 10553594Sroot printf ("\n"); 10563594Sroot if (clreol) 10573594Sroot cleareol (); 10583594Sroot printf ("...skipping %d line", nlines); 10591500Serics if (nlines > 1) 10603594Sroot pr ("s\n"); 10611500Serics else 10623594Sroot pr ("\n"); 10633594Sroot 10643594Sroot if (clreol) 10653594Sroot cleareol (); 10663594Sroot pr ("\n"); 10673594Sroot 10681500Serics while (nlines > 0) { 10691500Serics while ((c = Getc (f)) != '\n') 10701500Serics if (c == EOF) { 10711500Serics retval = 0; 10721500Serics done++; 10731500Serics goto endsw; 10741500Serics } 10751500Serics Currline++; 10761500Serics nlines--; 10771500Serics } 10781500Serics ret (dlines); 10791500Serics case '\n': 10801500Serics if (nlines != 0) 10811500Serics dlines = nlines; 10821500Serics else 10831500Serics nlines = 1; 10841500Serics ret (nlines); 10851500Serics case '\f': 10861500Serics if (!no_intty) { 10871500Serics doclear (); 10881500Serics Fseek (f, screen_start.chrctr); 10891500Serics Currline = screen_start.line; 10901500Serics ret (dlines); 10911500Serics } 10921500Serics else { 10931500Serics write (2, &bell, 1); 10941500Serics break; 10951500Serics } 10961500Serics case '\'': 10971500Serics if (!no_intty) { 10981500Serics kill_line (); 10991500Serics pr ("\n***Back***\n\n"); 11001500Serics Fseek (f, context.chrctr); 11011500Serics Currline = context.line; 11021500Serics ret (dlines); 11031500Serics } 11041500Serics else { 11051500Serics write (2, &bell, 1); 11061500Serics break; 11071500Serics } 11081500Serics case '=': 11091500Serics kill_line (); 11101500Serics promptlen = printd (Currline); 11111500Serics fflush (stdout); 11121500Serics break; 11131500Serics case 'n': 11141500Serics lastp++; 11151500Serics case '/': 11161500Serics if (nlines == 0) nlines++; 11171500Serics kill_line (); 11181500Serics pr ("/"); 11191500Serics promptlen = 1; 11201500Serics fflush (stdout); 11211500Serics if (lastp) { 11221500Serics write (2,"\r", 1); 11231500Serics search (NULL, f, nlines); /* Use previous r.e. */ 11241500Serics } 11251500Serics else { 11261500Serics ttyin (cmdbuf, 78, '/'); 11271500Serics write (2, "\r", 1); 11281500Serics search (cmdbuf, f, nlines); 11291500Serics } 11303455Sroot ret (dlines-1); 11311500Serics case '!': 11321500Serics do_shell (filename); 11331500Serics break; 113424490Sbloom case '?': 11351500Serics case 'h': 11361500Serics if ((helpf = fopen (HELPFILE, "r")) == NULL) 11371500Serics error ("Can't open help file"); 11381500Serics if (noscroll) doclear (); 11391500Serics copy_file (helpf); 114027006Sdonn fclose (helpf); 11411500Serics prompt (filename); 11421500Serics break; 11431500Serics case 'v': /* This case should go right before default */ 11441500Serics if (!no_intty) { 11451500Serics kill_line (); 11461500Serics cmdbuf[0] = '+'; 114724490Sbloom scanstr (Currline - dlines < 0 ? 0 114824490Sbloom : Currline - (dlines + 1) / 2, &cmdbuf[1]); 11491500Serics pr ("vi "); pr (cmdbuf); putchar (' '); pr (fnames[fnum]); 11501500Serics execute (filename, VI, "vi", cmdbuf, fnames[fnum], 0); 11511500Serics break; 11521500Serics } 11531500Serics default: 115416710Sjak if (dum_opt) { 115516710Sjak kill_line (); 115616710Sjak if (Senter && Sexit) { 115716710Sjak tputs (Senter, 1, putch); 115816710Sjak promptlen = pr ("[Press 'h' for instructions.]") + (2 * soglitch); 115916710Sjak tputs (Sexit, 1, putch); 116016710Sjak } 116116710Sjak else 116216710Sjak promptlen = pr ("[Press 'h' for instructions.]"); 116316710Sjak fflush (stdout); 116416710Sjak } 116516710Sjak else 116616710Sjak write (2, &bell, 1); 11671500Serics break; 11681500Serics } 11691500Serics if (done) break; 11701500Serics } 11711500Serics putchar ('\r'); 11721500Serics endsw: 11731500Serics inwait = 0; 11741500Serics notell++; 11751500Serics if (MBIT == RAW && slow_tty) { 11761500Serics otty.sg_flags &= ~MBIT; 117716582Sleres stty(fileno(stderr), &otty); 11781500Serics } 11791500Serics return (retval); 11801500Serics } 11811500Serics 11821500Serics char ch; 11831500Serics 11841500Serics /* 11851500Serics * Execute a colon-prefixed command. 11861500Serics * Returns <0 if not a command that should cause 11871500Serics * more of the file to be printed. 11881500Serics */ 11891500Serics 11901500Serics colon (filename, cmd, nlines) 11911500Serics char *filename; 11921500Serics int cmd; 11931500Serics int nlines; 11941500Serics { 11951500Serics if (cmd == 0) 11961500Serics ch = readch (); 11971500Serics else 11981500Serics ch = cmd; 11991500Serics lastcolon = ch; 12001500Serics switch (ch) { 12011500Serics case 'f': 12021500Serics kill_line (); 12031500Serics if (!no_intty) 12041500Serics promptlen = printf ("\"%s\" line %d", fnames[fnum], Currline); 12051500Serics else 12061500Serics promptlen = printf ("[Not a file] line %d", Currline); 12071500Serics fflush (stdout); 12081500Serics return (-1); 12091500Serics case 'n': 12101500Serics if (nlines == 0) { 12111500Serics if (fnum >= nfiles - 1) 12121500Serics end_it (); 12131500Serics nlines++; 12141500Serics } 12151500Serics putchar ('\r'); 12161500Serics erase (0); 12171500Serics skipf (nlines); 12181500Serics return (0); 12191500Serics case 'p': 12201500Serics if (no_intty) { 12211500Serics write (2, &bell, 1); 12221500Serics return (-1); 12231500Serics } 12241500Serics putchar ('\r'); 12251500Serics erase (0); 12261500Serics if (nlines == 0) 12271500Serics nlines++; 12281500Serics skipf (-nlines); 12291500Serics return (0); 12301500Serics case '!': 12311500Serics do_shell (filename); 12321500Serics return (-1); 12331500Serics case 'q': 12341500Serics case 'Q': 12351500Serics end_it (); 12361500Serics default: 12371500Serics write (2, &bell, 1); 12381500Serics return (-1); 12391500Serics } 12401500Serics } 12411500Serics 12421500Serics /* 12431500Serics ** Read a decimal number from the terminal. Set cmd to the non-digit which 12441500Serics ** terminates the number. 12451500Serics */ 12461500Serics 12471500Serics number(cmd) 12481500Serics char *cmd; 12491500Serics { 12501500Serics register int i; 12511500Serics 12521500Serics i = 0; ch = otty.sg_kill; 12531500Serics for (;;) { 12541500Serics ch = readch (); 12551500Serics if (ch >= '0' && ch <= '9') 12561500Serics i = i*10 + ch - '0'; 12571500Serics else if (ch == otty.sg_kill) 12581500Serics i = 0; 12591500Serics else { 12601500Serics *cmd = ch; 12611500Serics break; 12621500Serics } 12631500Serics } 12641500Serics return (i); 12651500Serics } 12661500Serics 12671500Serics do_shell (filename) 12681500Serics char *filename; 12691500Serics { 12701500Serics char cmdbuf[80]; 12711500Serics 12721500Serics kill_line (); 12731500Serics pr ("!"); 12741500Serics fflush (stdout); 12751500Serics promptlen = 1; 12761500Serics if (lastp) 12771500Serics pr (shell_line); 12781500Serics else { 12791500Serics ttyin (cmdbuf, 78, '!'); 12801500Serics if (expand (shell_line, cmdbuf)) { 12811500Serics kill_line (); 12821500Serics promptlen = printf ("!%s", shell_line); 12831500Serics } 12841500Serics } 12851500Serics fflush (stdout); 12861500Serics write (2, "\n", 1); 12871500Serics promptlen = 0; 12881500Serics shellp = 1; 12891500Serics execute (filename, shell, shell, "-c", shell_line, 0); 12901500Serics } 12911500Serics 12921500Serics /* 12931500Serics ** Search for nth ocurrence of regular expression contained in buf in the file 12941500Serics */ 12951500Serics 12961500Serics search (buf, file, n) 12971500Serics char buf[]; 12981500Serics FILE *file; 12991500Serics register int n; 13001500Serics { 13011500Serics long startline = Ftell (file); 13021500Serics register long line1 = startline; 13031500Serics register long line2 = startline; 13041500Serics register long line3 = startline; 13051500Serics register int lncount; 13061500Serics int saveln, rv, re_exec(); 13071500Serics char *s, *re_comp(); 13081500Serics 13091500Serics context.line = saveln = Currline; 13101500Serics context.chrctr = startline; 13111500Serics lncount = 0; 13121500Serics if ((s = re_comp (buf)) != 0) 13131500Serics error (s); 13141500Serics while (!feof (file)) { 13151500Serics line3 = line2; 13161500Serics line2 = line1; 13171500Serics line1 = Ftell (file); 13181500Serics rdline (file); 13191500Serics lncount++; 13201500Serics if ((rv = re_exec (Line)) == 1) 13211500Serics if (--n == 0) { 13221500Serics if (lncount > 3 || (lncount > 1 && no_intty)) 13233455Sroot { 13243455Sroot pr ("\n"); 13253594Sroot if (clreol) 13263594Sroot cleareol (); 13273455Sroot pr("...skipping\n"); 13283455Sroot } 13291500Serics if (!no_intty) { 13301500Serics Currline -= (lncount >= 3 ? 3 : lncount); 13311500Serics Fseek (file, line3); 13323594Sroot if (noscroll) 13333594Sroot if (clreol) { 13343594Sroot home (); 13353594Sroot cleareol (); 133616582Sleres } 13373594Sroot else 13383594Sroot doclear (); 13391500Serics } 13401500Serics else { 13411500Serics kill_line (); 13423594Sroot if (noscroll) 13433594Sroot if (clreol) { 134416582Sleres home (); 13453594Sroot cleareol (); 134616582Sleres } 13473594Sroot else 13483594Sroot doclear (); 13491500Serics pr (Line); 13501500Serics putchar ('\n'); 13511500Serics } 13521500Serics break; 13531500Serics } 13541500Serics else if (rv == -1) 13551500Serics error ("Regular expression botch"); 13561500Serics } 13571500Serics if (feof (file)) { 13581500Serics if (!no_intty) { 13591500Serics file->_flag &= ~_IOEOF; /* why doesn't fseek do this ??!!??! */ 13601500Serics Currline = saveln; 13611500Serics Fseek (file, startline); 13621500Serics } 13631500Serics else { 13641500Serics pr ("\nPattern not found\n"); 13651500Serics end_it (); 13661500Serics } 13671500Serics error ("Pattern not found"); 13681500Serics } 13691500Serics } 13701500Serics 13711500Serics execute (filename, cmd, args) 13721500Serics char *filename; 13731500Serics char *cmd, *args; 13741500Serics { 13751500Serics int id; 137616710Sjak int n; 13771500Serics 13781500Serics fflush (stdout); 13791500Serics reset_tty (); 138016710Sjak for (n = 10; (id = fork ()) < 0 && n > 0; n--) 13811500Serics sleep (5); 13821500Serics if (id == 0) { 138316710Sjak if (!isatty(0)) { 138416710Sjak close(0); 138516710Sjak open("/dev/tty", 0); 138616710Sjak } 13871500Serics execv (cmd, &args); 13881500Serics write (2, "exec failed\n", 12); 13891500Serics exit (1); 13901500Serics } 139116710Sjak if (id > 0) { 139216710Sjak signal (SIGINT, SIG_IGN); 139316710Sjak signal (SIGQUIT, SIG_IGN); 139416710Sjak if (catch_susp) 139516710Sjak signal(SIGTSTP, SIG_DFL); 139616710Sjak while (wait(0) > 0); 139716710Sjak signal (SIGINT, end_it); 139816710Sjak signal (SIGQUIT, onquit); 139916710Sjak if (catch_susp) 140016710Sjak signal(SIGTSTP, onsusp); 140116710Sjak } else 140216710Sjak write(2, "can't fork\n", 11); 14031500Serics set_tty (); 14041500Serics pr ("------------------------\n"); 14051500Serics prompt (filename); 14061500Serics } 14071500Serics /* 14081500Serics ** Skip n lines in the file f 14091500Serics */ 14101500Serics 14111500Serics skiplns (n, f) 14121500Serics register int n; 14131500Serics register FILE *f; 14141500Serics { 14151500Serics register char c; 14161500Serics 14171500Serics while (n > 0) { 14181500Serics while ((c = Getc (f)) != '\n') 14191500Serics if (c == EOF) 14201500Serics return; 14211500Serics n--; 14221500Serics Currline++; 14231500Serics } 14241500Serics } 14251500Serics 14261500Serics /* 14271500Serics ** Skip nskip files in the file list (from the command line). Nskip may be 14281500Serics ** negative. 14291500Serics */ 14301500Serics 14311500Serics skipf (nskip) 14321500Serics register int nskip; 14331500Serics { 14341500Serics if (nskip == 0) return; 14351500Serics if (nskip > 0) { 14361500Serics if (fnum + nskip > nfiles - 1) 14371500Serics nskip = nfiles - fnum - 1; 14381500Serics } 14391500Serics else if (within) 14401500Serics ++fnum; 14411500Serics fnum += nskip; 14421500Serics if (fnum < 0) 14431500Serics fnum = 0; 14443594Sroot pr ("\n...Skipping "); 14453455Sroot pr ("\n"); 14463594Sroot if (clreol) 14473594Sroot cleareol (); 14483455Sroot pr ("...Skipping "); 14491500Serics pr (nskip > 0 ? "to file " : "back to file "); 14501500Serics pr (fnames[fnum]); 14513455Sroot pr ("\n"); 14523594Sroot if (clreol) 14533594Sroot cleareol (); 14543455Sroot pr ("\n"); 14551500Serics --fnum; 14561500Serics } 14571500Serics 14581500Serics /*----------------------------- Terminal I/O -------------------------------*/ 14591500Serics 14601500Serics initterm () 14611500Serics { 14621500Serics char buf[TBUFSIZ]; 146317195Sralph static char clearbuf[TBUFSIZ]; 14641500Serics char *clearptr, *padstr; 14651500Serics int ldisc; 146617592Sleres int lmode; 146710823Ssam char *term; 146816582Sleres int tgrp; 146918030Sbloom struct winsize win; 14701500Serics 147116582Sleres retry: 147216582Sleres if (!(no_tty = gtty(fileno(stdout), &otty))) { 147317592Sleres if (ioctl(fileno(stdout), TIOCLGET, &lmode) < 0) { 147417592Sleres perror("TIOCLGET"); 147517592Sleres exit(1); 147617592Sleres } 147717592Sleres docrterase = ((lmode & LCRTERA) != 0); 147817592Sleres docrtkill = ((lmode & LCRTKIL) != 0); 147916582Sleres /* 148017592Sleres * Wait until we're in the foreground before we save the 148117592Sleres * the terminal modes. 148216582Sleres */ 148316582Sleres if (ioctl(fileno(stdout), TIOCGPGRP, &tgrp) < 0) { 148417592Sleres perror("TIOCGPGRP"); 148516582Sleres exit(1); 148616582Sleres } 148716582Sleres if (tgrp != getpgrp(0)) { 148816582Sleres kill(0, SIGTTOU); 148916582Sleres goto retry; 149016582Sleres } 149113830Skre if ((term = getenv("TERM")) == 0 || tgetent(buf, term) <= 0) { 14923594Sroot dumb++; ul_opt = 0; 14931500Serics } 14941500Serics else { 149518030Sbloom if (ioctl(fileno(stdout), TIOCGWINSZ, &win) < 0) { 149618030Sbloom Lpp = tgetnum("li"); 149718030Sbloom Mcol = tgetnum("co"); 149818030Sbloom } else { 149918030Sbloom if ((Lpp = win.ws_row) == 0) 150018030Sbloom Lpp = tgetnum("li"); 150118030Sbloom if ((Mcol = win.ws_col) == 0) 150218030Sbloom Mcol = tgetnum("co"); 150318030Sbloom } 150418030Sbloom if ((Lpp <= 0) || tgetflag("hc")) { 15051500Serics hard++; /* Hard copy terminal */ 15061500Serics Lpp = 24; 15071500Serics } 150829907Smckusick if (tgetflag("xn")) 150929907Smckusick eatnl++; /* Eat newline at last column + 1; dec, concept */ 151018030Sbloom if (Mcol <= 0) 151118030Sbloom Mcol = 80; 151218030Sbloom 15131500Serics if (tailequ (fnames[0], "page") || !hard && tgetflag("ns")) 15141500Serics noscroll++; 15151500Serics Wrap = tgetflag("am"); 15161500Serics bad_so = tgetflag ("xs"); 15171500Serics clearptr = clearbuf; 15181500Serics eraseln = tgetstr("ce",&clearptr); 15191500Serics Clear = tgetstr("cl", &clearptr); 15201500Serics Senter = tgetstr("so", &clearptr); 15211500Serics Sexit = tgetstr("se", &clearptr); 152216710Sjak if ((soglitch = tgetnum("sg")) < 0) 152316710Sjak soglitch = 0; 15243594Sroot 15253594Sroot /* 15263594Sroot * Set up for underlining: some terminals don't need it; 15273594Sroot * others have start/stop sequences, still others have an 15283594Sroot * underline char sequence which is assumed to move the 15293594Sroot * cursor forward one character. If underline sequence 15303594Sroot * isn't available, settle for standout sequence. 15313594Sroot */ 15323594Sroot 15333594Sroot if (tgetflag("ul") || tgetflag("os")) 15343594Sroot ul_opt = 0; 15353594Sroot if ((chUL = tgetstr("uc", &clearptr)) == NULL ) 15363594Sroot chUL = ""; 153716710Sjak if (((ULenter = tgetstr("us", &clearptr)) == NULL || 153816710Sjak (ULexit = tgetstr("ue", &clearptr)) == NULL) && !*chUL) { 153916710Sjak if ((ULenter = Senter) == NULL || (ULexit = Sexit) == NULL) { 154016710Sjak ULenter = ""; 154116710Sjak ULexit = ""; 154216710Sjak } else 154316710Sjak ulglitch = soglitch; 154416710Sjak } else { 154516710Sjak if ((ulglitch = tgetnum("ug")) < 0) 154616710Sjak ulglitch = 0; 154716710Sjak } 154816582Sleres 15491500Serics if (padstr = tgetstr("pc", &clearptr)) 15501500Serics PC = *padstr; 15513455Sroot Home = tgetstr("ho",&clearptr); 155213536Ssam if (Home == 0 || *Home == '\0') 15533455Sroot { 15543594Sroot if ((cursorm = tgetstr("cm", &clearptr)) != NULL) { 15553594Sroot strcpy(cursorhome, tgoto(cursorm, 0, 0)); 15563455Sroot Home = cursorhome; 15573455Sroot } 15583455Sroot } 15593594Sroot EodClr = tgetstr("cd", &clearptr); 156025540Smckusick if ((chBS = tgetstr("bc", &clearptr)) == NULL) 156125540Smckusick chBS = "\b"; 156225540Smckusick 15631500Serics } 15641500Serics if ((shell = getenv("SHELL")) == NULL) 15651500Serics shell = "/bin/sh"; 15661500Serics } 156716582Sleres no_intty = gtty(fileno(stdin), &otty); 156816582Sleres gtty(fileno(stderr), &otty); 156913830Skre savetty = otty; 15701500Serics ospeed = otty.sg_ospeed; 15711500Serics slow_tty = ospeed < B1200; 157227006Sdonn hardtabs = (otty.sg_flags & TBDELAY) != XTABS; 15731500Serics if (!no_tty) { 15741500Serics otty.sg_flags &= ~ECHO; 15751500Serics if (MBIT == CBREAK || !slow_tty) 15761500Serics otty.sg_flags |= MBIT; 15771500Serics } 15781500Serics } 15791500Serics 15801500Serics readch () 15811500Serics { 15821500Serics char ch; 15831500Serics extern int errno; 15841500Serics 158531089Skarels errno = 0; 15861500Serics if (read (2, &ch, 1) <= 0) 15871500Serics if (errno != EINTR) 158831089Skarels end_it(); 15891500Serics else 15901500Serics ch = otty.sg_kill; 15911500Serics return (ch); 15921500Serics } 15931500Serics 15941500Serics static char BS = '\b'; 159517592Sleres static char *BSB = "\b \b"; 15961500Serics static char CARAT = '^'; 159717592Sleres #define ERASEONECHAR \ 159817592Sleres if (docrterase) \ 159917592Sleres write (2, BSB, sizeof(BSB)); \ 160017592Sleres else \ 160117592Sleres write (2, &BS, sizeof(BS)); 16021500Serics 16031500Serics ttyin (buf, nmax, pchar) 16041500Serics char buf[]; 16051500Serics register int nmax; 16061500Serics char pchar; 16071500Serics { 16081500Serics register char *sptr; 16091500Serics register char ch; 16101500Serics register int slash = 0; 16111500Serics int maxlen; 16121500Serics char cbuf; 16131500Serics 16141500Serics sptr = buf; 16151500Serics maxlen = 0; 16161500Serics while (sptr - buf < nmax) { 16171500Serics if (promptlen > maxlen) maxlen = promptlen; 16181500Serics ch = readch (); 16191500Serics if (ch == '\\') { 16201500Serics slash++; 16211500Serics } 16221500Serics else if ((ch == otty.sg_erase) && !slash) { 16231500Serics if (sptr > buf) { 16241500Serics --promptlen; 162517592Sleres ERASEONECHAR 16261500Serics --sptr; 16271500Serics if ((*sptr < ' ' && *sptr != '\n') || *sptr == RUBOUT) { 16281500Serics --promptlen; 162917592Sleres ERASEONECHAR 16301500Serics } 16311500Serics continue; 16321500Serics } 16331500Serics else { 16341500Serics if (!eraseln) promptlen = maxlen; 16351500Serics longjmp (restore, 1); 16361500Serics } 16371500Serics } 16381500Serics else if ((ch == otty.sg_kill) && !slash) { 16391500Serics if (hard) { 16401500Serics show (ch); 16411500Serics putchar ('\n'); 16421500Serics putchar (pchar); 16431500Serics } 16441500Serics else { 16451500Serics putchar ('\r'); 16461500Serics putchar (pchar); 16471500Serics if (eraseln) 16481500Serics erase (1); 164917592Sleres else if (docrtkill) 165017592Sleres while (promptlen-- > 1) 165117592Sleres write (2, BSB, sizeof(BSB)); 16521500Serics promptlen = 1; 16531500Serics } 16541500Serics sptr = buf; 16551500Serics fflush (stdout); 16561500Serics continue; 16571500Serics } 16581500Serics if (slash && (ch == otty.sg_kill || ch == otty.sg_erase)) { 165917592Sleres ERASEONECHAR 16601500Serics --sptr; 16611500Serics } 16621500Serics if (ch != '\\') 16631500Serics slash = 0; 16641500Serics *sptr++ = ch; 16651500Serics if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) { 16661500Serics ch += ch == RUBOUT ? -0100 : 0100; 16671500Serics write (2, &CARAT, 1); 16681500Serics promptlen++; 16691500Serics } 16701500Serics cbuf = ch; 16711500Serics if (ch != '\n' && ch != ESC) { 16721500Serics write (2, &cbuf, 1); 16731500Serics promptlen++; 16741500Serics } 16751500Serics else 16761500Serics break; 16771500Serics } 16781500Serics *--sptr = '\0'; 16791500Serics if (!eraseln) promptlen = maxlen; 16801500Serics if (sptr - buf >= nmax - 1) 16811500Serics error ("Line too long"); 16821500Serics } 16831500Serics 16841500Serics expand (outbuf, inbuf) 16851500Serics char *outbuf; 16861500Serics char *inbuf; 16871500Serics { 16881500Serics register char *instr; 16891500Serics register char *outstr; 16901500Serics register char ch; 16911500Serics char temp[200]; 16921500Serics int changed = 0; 16931500Serics 16941500Serics instr = inbuf; 16951500Serics outstr = temp; 16961500Serics while ((ch = *instr++) != '\0') 16971500Serics switch (ch) { 16981500Serics case '%': 16991500Serics if (!no_intty) { 17001500Serics strcpy (outstr, fnames[fnum]); 17011500Serics outstr += strlen (fnames[fnum]); 17021500Serics changed++; 17031500Serics } 17041500Serics else 17051500Serics *outstr++ = ch; 17061500Serics break; 17071500Serics case '!': 17081500Serics if (!shellp) 17091500Serics error ("No previous command to substitute for"); 17101500Serics strcpy (outstr, shell_line); 17111500Serics outstr += strlen (shell_line); 17121500Serics changed++; 17131500Serics break; 17141500Serics case '\\': 17151500Serics if (*instr == '%' || *instr == '!') { 17161500Serics *outstr++ = *instr++; 17171500Serics break; 17181500Serics } 17191500Serics default: 17201500Serics *outstr++ = ch; 17211500Serics } 17221500Serics *outstr++ = '\0'; 17231500Serics strcpy (outbuf, temp); 17241500Serics return (changed); 17251500Serics } 17261500Serics 17271500Serics show (ch) 17281500Serics register char ch; 17291500Serics { 17301500Serics char cbuf; 17311500Serics 17321500Serics if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) { 17331500Serics ch += ch == RUBOUT ? -0100 : 0100; 17341500Serics write (2, &CARAT, 1); 17351500Serics promptlen++; 17361500Serics } 17371500Serics cbuf = ch; 17381500Serics write (2, &cbuf, 1); 17391500Serics promptlen++; 17401500Serics } 17411500Serics 17421500Serics error (mess) 17431500Serics char *mess; 17441500Serics { 17453594Sroot if (clreol) 17463594Sroot cleareol (); 17473594Sroot else 17483594Sroot kill_line (); 17491500Serics promptlen += strlen (mess); 17501500Serics if (Senter && Sexit) { 17511500Serics tputs (Senter, 1, putch); 17521500Serics pr(mess); 17531500Serics tputs (Sexit, 1, putch); 17541500Serics } 17551500Serics else 17561500Serics pr (mess); 17571500Serics fflush(stdout); 17581500Serics errors++; 17591500Serics longjmp (restore, 1); 17601500Serics } 17611500Serics 17621500Serics 17631500Serics set_tty () 17641500Serics { 17651500Serics otty.sg_flags |= MBIT; 17661500Serics otty.sg_flags &= ~ECHO; 176716582Sleres stty(fileno(stderr), &otty); 17681500Serics } 17691500Serics 17701500Serics reset_tty () 17711500Serics { 177231033Sbostic if (no_tty) 177331033Sbostic return; 177416710Sjak if (pstate) { 177516710Sjak tputs(ULexit, 1, putch); 177616710Sjak fflush(stdout); 177716710Sjak pstate = 0; 177816710Sjak } 17791500Serics otty.sg_flags |= ECHO; 17801500Serics otty.sg_flags &= ~MBIT; 178116582Sleres stty(fileno(stderr), &savetty); 17821500Serics } 17831500Serics 17841500Serics rdline (f) 17851500Serics register FILE *f; 17861500Serics { 17871500Serics register char c; 17881500Serics register char *p; 17891500Serics 17901500Serics p = Line; 17911500Serics while ((c = Getc (f)) != '\n' && c != EOF && p - Line < LINSIZ - 1) 17921500Serics *p++ = c; 17931500Serics if (c == '\n') 17941500Serics Currline++; 17951500Serics *p = '\0'; 17961500Serics } 17971500Serics 17981500Serics /* Come here when we get a suspend signal from the terminal */ 17991500Serics 18001500Serics onsusp () 18011500Serics { 180214861Skarels /* ignore SIGTTOU so we don't get stopped if csh grabs the tty */ 180314861Skarels signal(SIGTTOU, SIG_IGN); 18041500Serics reset_tty (); 18051500Serics fflush (stdout); 180614861Skarels signal(SIGTTOU, SIG_DFL); 18071500Serics /* Send the TSTP signal to suspend our process group */ 180813289Ssam signal(SIGTSTP, SIG_DFL); 180913289Ssam sigsetmask(0); 18101500Serics kill (0, SIGTSTP); 18111500Serics /* Pause for station break */ 18121500Serics 18131500Serics /* We're back */ 18141500Serics signal (SIGTSTP, onsusp); 18151500Serics set_tty (); 18161500Serics if (inwait) 18171500Serics longjmp (restore); 18181500Serics } 1819