1*21981Sdist /* 2*21981Sdist * Copyright (c) 1980 Regents of the University of California. 3*21981Sdist * All rights reserved. The Berkeley software License Agreement 4*21981Sdist * specifies the terms and conditions for redistribution. 5*21981Sdist */ 6*21981Sdist 713615Ssam #ifndef lint 8*21981Sdist char copyright[] = 9*21981Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 10*21981Sdist All rights reserved.\n"; 11*21981Sdist #endif not lint 123594Sroot 13*21981Sdist #ifndef lint 14*21981Sdist static char sccsid[] = "@(#)more.c 5.1 (Berkeley) 06/04/85"; 15*21981Sdist #endif not lint 16*21981Sdist 171500Serics /* 181500Serics ** more.c - General purpose tty output filter and file perusal program 191500Serics ** 201500Serics ** by Eric Shienbrood, UC Berkeley 213594Sroot ** 223594Sroot ** modified by Geoff Peck, UCB to add underlining, single spacing 233594Sroot ** modified by John Foderaro, UCB to add -c and MORE environment variable 241500Serics */ 251500Serics 261500Serics #include <stdio.h> 2717592Sleres #include <sys/types.h> 281500Serics #include <ctype.h> 291500Serics #include <signal.h> 301500Serics #include <errno.h> 311500Serics #include <sgtty.h> 321500Serics #include <setjmp.h> 331500Serics #include <sys/stat.h> 341500Serics 3513615Ssam #define HELPFILE "/usr/lib/more.help" 3613615Ssam #define VI "/usr/ucb/vi" 371500Serics 381500Serics #define Fopen(s,m) (Currline = 0,file_pos=0,fopen(s,m)) 391500Serics #define Ftell(f) file_pos 401500Serics #define Fseek(f,off) (file_pos=off,fseek(f,off,0)) 411500Serics #define Getc(f) (++file_pos, getc(f)) 421500Serics #define Ungetc(c,f) (--file_pos, ungetc(c,f)) 431500Serics 441500Serics #define MBIT CBREAK 451500Serics #define stty(fd,argp) ioctl(fd,TIOCSETN,argp) 461500Serics 471500Serics #define TBUFSIZ 1024 481500Serics #define LINSIZ 256 491500Serics #define ctrl(letter) ('letter' & 077) 501500Serics #define RUBOUT '\177' 511500Serics #define ESC '\033' 521500Serics #define QUIT '\034' 531500Serics 5416582Sleres struct sgttyb otty, savetty; 551500Serics long file_pos, file_size; 561500Serics int fnum, no_intty, no_tty, slow_tty; 571500Serics int dum_opt, dlines, onquit(), end_it(); 581500Serics int onsusp(); 591500Serics int nscroll = 11; /* Number of lines scrolled by 'd' */ 601500Serics int fold_opt = 1; /* Fold long lines */ 611500Serics int stop_opt = 1; /* Stop after form feeds */ 623594Sroot int ssp_opt = 0; /* Suppress white space */ 633594Sroot int ul_opt = 1; /* Underline as best we can */ 641500Serics int promptlen; 651500Serics int Currline; /* Line we are currently at */ 661500Serics int startup = 1; 671500Serics int firstf = 1; 681500Serics int notell = 1; 6917592Sleres int docrterase = 0; 7017592Sleres int docrtkill = 0; 711500Serics int bad_so; /* True if overwriting does not turn off standout */ 721500Serics int inwait, Pause, errors; 731500Serics int within; /* true if we are within a file, 741500Serics false if we are between files */ 753594Sroot int hard, dumb, noscroll, hardtabs, clreol; 761500Serics int catch_susp; /* We should catch the SIGTSTP signal */ 771500Serics char **fnames; /* The list of file names */ 781500Serics int nfiles; /* Number of files left to process */ 791500Serics char *shell; /* The name of the shell to use */ 801500Serics int shellp; /* A previous shell command exists */ 811500Serics char ch; 821500Serics jmp_buf restore; 831500Serics char Line[LINSIZ]; /* Line buffer */ 841500Serics int Lpp = 24; /* lines per page */ 851500Serics char *Clear; /* clear screen */ 861500Serics char *eraseln; /* erase line */ 871500Serics char *Senter, *Sexit;/* enter and exit standout mode */ 883594Sroot char *ULenter, *ULexit; /* enter and exit underline mode */ 893594Sroot char *chUL; /* underline character */ 903594Sroot char *chBS; /* backspace character */ 913455Sroot char *Home; /* go to home */ 923455Sroot char *cursorm; /* cursor movement */ 933455Sroot char cursorhome[40]; /* contains cursor movement to home */ 943594Sroot char *EodClr; /* clear rest of screen */ 951500Serics char *tgetstr(); 961500Serics int Mcol = 80; /* number of columns */ 971500Serics int Wrap = 1; /* set if automargins */ 9816710Sjak int soglitch; /* terminal has standout mode glitch */ 9916710Sjak int ulglitch; /* terminal has underline mode glitch */ 10016710Sjak int pstate = 0; /* current UL state */ 1011500Serics long fseek(); 1023455Sroot char *getenv(); 1031500Serics struct { 1041500Serics long chrctr, line; 1051500Serics } context, screen_start; 1061500Serics extern char PC; /* pad character */ 1071500Serics extern short ospeed; 1081500Serics 1091500Serics 1101500Serics main(argc, argv) 1111500Serics int argc; 1121500Serics char *argv[]; 1131500Serics { 1141500Serics register FILE *f; 1151500Serics register char *s; 1161500Serics register char *p; 1171500Serics register char ch; 1181500Serics register int left; 11916582Sleres int prnames = 0; 1201500Serics int initopt = 0; 1211500Serics int srchopt = 0; 1221500Serics int clearit = 0; 1231500Serics int initline; 1241500Serics char initbuf[80]; 1251500Serics FILE *checkf(); 1261500Serics 1271500Serics nfiles = argc; 1281500Serics fnames = argv; 1291500Serics initterm (); 13015813Sralph nscroll = Lpp/2 - 1; 13115813Sralph if (nscroll <= 0) 13215813Sralph nscroll = 1; 1333455Sroot if(s = getenv("MORE")) argscan(s); 1341500Serics while (--nfiles > 0) { 1351500Serics if ((ch = (*++fnames)[0]) == '-') { 1363455Sroot argscan(*fnames+1); 1371500Serics } 1381500Serics else if (ch == '+') { 1391500Serics s = *fnames; 1401500Serics if (*++s == '/') { 1411500Serics srchopt++; 1421500Serics for (++s, p = initbuf; p < initbuf + 79 && *s != '\0';) 1431500Serics *p++ = *s++; 1441500Serics *p = '\0'; 1451500Serics } 1461500Serics else { 1471500Serics initopt++; 1481500Serics for (initline = 0; *s != '\0'; s++) 1491500Serics if (isdigit (*s)) 1501500Serics initline = initline*10 + *s -'0'; 1511500Serics --initline; 1521500Serics } 1531500Serics } 1541500Serics else break; 1551500Serics } 1563594Sroot /* allow clreol only if Home and eraseln and EodClr strings are 1573455Sroot * defined, and in that case, make sure we are in noscroll mode 1583455Sroot */ 1593455Sroot if(clreol) 1603455Sroot { 16118608Sralph if((Home == NULL) || (*Home == '\0') || 16218608Sralph (eraseln == NULL) || (*eraseln == '\0') || 16318608Sralph (EodClr == NULL) || (*EodClr == '\0') ) 16418608Sralph clreol = 0; 1653455Sroot else noscroll = 1; 1663455Sroot } 1671500Serics if (dlines == 0) 1681500Serics dlines = Lpp - (noscroll ? 1 : 2); 1691500Serics left = dlines; 1701500Serics if (nfiles > 1) 1711500Serics prnames++; 1721500Serics if (!no_intty && nfiles == 0) { 1731500Serics fputs("Usage: ",stderr); 1741500Serics fputs(argv[0],stderr); 1751500Serics fputs(" [-dfln] [+linenum | +/pattern] name1 name2 ...\n",stderr); 1761500Serics exit(1); 1771500Serics } 1781500Serics else 1791500Serics f = stdin; 1801500Serics if (!no_tty) { 1811500Serics signal(SIGQUIT, onquit); 1821500Serics signal(SIGINT, end_it); 1831500Serics if (signal (SIGTSTP, SIG_IGN) == SIG_DFL) { 1841500Serics signal(SIGTSTP, onsusp); 1851500Serics catch_susp++; 1861500Serics } 18716582Sleres stty (fileno(stderr), &otty); 1881500Serics } 1891500Serics if (no_intty) { 1901500Serics if (no_tty) 1911500Serics copy_file (stdin); 1921500Serics else { 1931500Serics if ((ch = Getc (f)) == '\f') 1943594Sroot doclear(); 1951500Serics else { 1961500Serics Ungetc (ch, f); 1973594Sroot if (noscroll && (ch != EOF)) { 1983594Sroot if (clreol) 1993594Sroot home (); 2003594Sroot else 2013594Sroot doclear (); 2023455Sroot } 2031500Serics } 2041500Serics if (srchopt) 2053455Sroot { 2061500Serics search (initbuf, stdin, 1); 2073594Sroot if (noscroll) 2083594Sroot left--; 2093455Sroot } 2101500Serics else if (initopt) 2111500Serics skiplns (initline, stdin); 2121500Serics screen (stdin, left); 2131500Serics } 2141500Serics no_intty = 0; 2151500Serics prnames++; 2161500Serics firstf = 0; 2171500Serics } 2181500Serics 2191500Serics while (fnum < nfiles) { 2201500Serics if ((f = checkf (fnames[fnum], &clearit)) != NULL) { 2211500Serics context.line = context.chrctr = 0; 2221500Serics Currline = 0; 2231500Serics if (firstf) setjmp (restore); 2241500Serics if (firstf) { 2251500Serics firstf = 0; 2261500Serics if (srchopt) 2273455Sroot { 2281500Serics search (initbuf, f, 1); 2293594Sroot if (noscroll) 2303594Sroot left--; 2313455Sroot } 2321500Serics else if (initopt) 2331500Serics skiplns (initline, f); 2341500Serics } 2351500Serics else if (fnum < nfiles && !no_tty) { 2361500Serics setjmp (restore); 2371500Serics left = command (fnames[fnum], f); 2381500Serics } 2391500Serics if (left != 0) { 2403594Sroot if ((noscroll || clearit) && (file_size != 0x7fffffffffffffffL)) 2413594Sroot if (clreol) 2423594Sroot home (); 2433594Sroot else 2443594Sroot doclear (); 2451500Serics if (prnames) { 2461500Serics if (bad_so) 2471500Serics erase (0); 2483594Sroot if (clreol) 2493594Sroot cleareol (); 2501500Serics pr("::::::::::::::"); 2511500Serics if (promptlen > 14) 2521500Serics erase (14); 2533455Sroot printf ("\n"); 2543455Sroot if(clreol) cleareol(); 2553455Sroot printf("%s\n", fnames[fnum]); 2563455Sroot if(clreol) cleareol(); 2573455Sroot printf("::::::::::::::\n", fnames[fnum]); 2581500Serics if (left > Lpp - 4) 2591500Serics left = Lpp - 4; 2601500Serics } 2611500Serics if (no_tty) 2621500Serics copy_file (f); 2631500Serics else { 2641500Serics within++; 2651500Serics screen(f, left); 2661500Serics within = 0; 2671500Serics } 2681500Serics } 2691500Serics setjmp (restore); 2701500Serics fflush(stdout); 2711500Serics fclose(f); 2721500Serics screen_start.line = screen_start.chrctr = 0L; 2731500Serics context.line = context.chrctr = 0L; 2741500Serics } 2751500Serics fnum++; 2761500Serics firstf = 0; 2771500Serics } 2781500Serics reset_tty (); 2791500Serics exit(0); 2801500Serics } 2811500Serics 2823455Sroot argscan(s) 2833455Sroot char *s; 2843455Sroot { 28511604Slayer for (dlines = 0; *s != '\0'; s++) 28611604Slayer { 28711604Slayer switch (*s) 28811604Slayer { 28911604Slayer case '0': case '1': case '2': 29011604Slayer case '3': case '4': case '5': 29111604Slayer case '6': case '7': case '8': 29211604Slayer case '9': 29311604Slayer dlines = dlines*10 + *s - '0'; 29411604Slayer break; 29511604Slayer case 'd': 29611604Slayer dum_opt = 1; 29711604Slayer break; 29811604Slayer case 'l': 29911604Slayer stop_opt = 0; 30011604Slayer break; 30111604Slayer case 'f': 30211604Slayer fold_opt = 0; 30311604Slayer break; 30411604Slayer case 'p': 30511604Slayer noscroll++; 30611604Slayer break; 30711604Slayer case 'c': 30811604Slayer clreol++; 30911604Slayer break; 31011604Slayer case 's': 31111604Slayer ssp_opt = 1; 31211604Slayer break; 31311604Slayer case 'u': 31411604Slayer ul_opt = 0; 31511604Slayer break; 31611604Slayer } 31711604Slayer } 3183455Sroot } 3193455Sroot 3203455Sroot 3211500Serics /* 3221500Serics ** Check whether the file named by fs is an ASCII file which the user may 3231500Serics ** access. If it is, return the opened file. Otherwise return NULL. 3241500Serics */ 3251500Serics 3261500Serics FILE * 3271500Serics checkf (fs, clearfirst) 3281500Serics register char *fs; 3291500Serics int *clearfirst; 3301500Serics { 3311500Serics struct stat stbuf; 3321500Serics register FILE *f; 3331500Serics char c; 3341500Serics 3351500Serics if (stat (fs, &stbuf) == -1) { 3361500Serics fflush(stdout); 3373594Sroot if (clreol) 3383594Sroot cleareol (); 3391500Serics perror(fs); 3401500Serics return (NULL); 3411500Serics } 3421500Serics if ((stbuf.st_mode & S_IFMT) == S_IFDIR) { 3431500Serics printf("\n*** %s: directory ***\n\n", fs); 3441500Serics return (NULL); 3451500Serics } 3461500Serics if ((f=Fopen(fs, "r")) == NULL) { 3471500Serics fflush(stdout); 3481500Serics perror(fs); 3491500Serics return (NULL); 3501500Serics } 3511500Serics c = Getc(f); 3521500Serics 3531500Serics /* Try to see whether it is an ASCII file */ 3541500Serics 3551500Serics switch ((c | *f->_ptr << 8) & 0177777) { 3561500Serics case 0405: 3571500Serics case 0407: 3581500Serics case 0410: 3591500Serics case 0411: 3601500Serics case 0413: 3611500Serics case 0177545: 3621500Serics printf("\n******** %s: Not a text file ********\n\n", fs); 3631500Serics fclose (f); 3641500Serics return (NULL); 3651500Serics default: 3661500Serics break; 3671500Serics } 3681500Serics if (c == '\f') 3691500Serics *clearfirst = 1; 3701500Serics else { 3711500Serics *clearfirst = 0; 3721500Serics Ungetc (c, f); 3731500Serics } 3741500Serics if ((file_size = stbuf.st_size) == 0) 3751500Serics file_size = 0x7fffffffffffffffL; 3761500Serics return (f); 3771500Serics } 3781500Serics 3791500Serics /* 3801500Serics ** A real function, for the tputs routine in termlib 3811500Serics */ 3821500Serics 3831500Serics putch (ch) 3841500Serics char ch; 3851500Serics { 3861500Serics putchar (ch); 3871500Serics } 3881500Serics 3891500Serics /* 3901500Serics ** Print out the contents of the file f, one screenful at a time. 3911500Serics */ 3921500Serics 3931500Serics #define STOP -10 3941500Serics 3951500Serics screen (f, num_lines) 3961500Serics register FILE *f; 3971500Serics register int num_lines; 3981500Serics { 3991500Serics register int c; 4001500Serics register int nchars; 4013594Sroot int length; /* length of current line */ 4023594Sroot static int prev_len = 1; /* length of previous line */ 4031500Serics 4041500Serics for (;;) { 4051500Serics while (num_lines > 0 && !Pause) { 4061500Serics if ((nchars = getline (f, &length)) == EOF) 4073455Sroot { 4083594Sroot if (clreol) 4093594Sroot clreos(); 4101500Serics return; 4113455Sroot } 4123594Sroot if (ssp_opt && length == 0 && prev_len == 0) 4133594Sroot continue; 4143594Sroot prev_len = length; 4151500Serics if (bad_so || (Senter && *Senter == ' ') && promptlen > 0) 4161500Serics erase (0); 4173594Sroot /* must clear before drawing line since tabs on some terminals 4183594Sroot * do not erase what they tab over. 4193594Sroot */ 4203594Sroot if (clreol) 4213594Sroot cleareol (); 4221500Serics prbuf (Line, length); 4231500Serics if (nchars < promptlen) 4241500Serics erase (nchars); /* erase () sets promptlen to 0 */ 4251500Serics else promptlen = 0; 4263594Sroot /* is this needed? 4273594Sroot * if (clreol) 4283594Sroot * cleareol(); /* must clear again in case we wrapped * 4293594Sroot */ 4301500Serics if (nchars < Mcol || !fold_opt) 43116710Sjak prbuf("\n", 1); /* will turn off UL if necessary */ 4321500Serics if (nchars == STOP) 4331500Serics break; 4341500Serics num_lines--; 4351500Serics } 43616710Sjak if (pstate) { 43716710Sjak tputs(ULexit, 1, putch); 43816710Sjak pstate = 0; 43916710Sjak } 4401500Serics fflush(stdout); 4411500Serics if ((c = Getc(f)) == EOF) 4423455Sroot { 4433594Sroot if (clreol) 4443594Sroot clreos (); 4451500Serics return; 4463455Sroot } 4473455Sroot 4483594Sroot if (Pause && clreol) 4493594Sroot clreos (); 4501500Serics Ungetc (c, f); 4511500Serics setjmp (restore); 4521500Serics Pause = 0; startup = 0; 4531500Serics if ((num_lines = command (NULL, f)) == 0) 4541500Serics return; 4551500Serics if (hard && promptlen > 0) 4561500Serics erase (0); 45711123Slayer if (noscroll && num_lines >= dlines) 45816582Sleres { 4593594Sroot if (clreol) 4603594Sroot home(); 4613594Sroot else 4623594Sroot doclear (); 4633455Sroot } 4641500Serics screen_start.line = Currline; 4651500Serics screen_start.chrctr = Ftell (f); 4661500Serics } 4671500Serics } 4681500Serics 4691500Serics /* 4701500Serics ** Come here if a quit signal is received 4711500Serics */ 4721500Serics 4731500Serics onquit() 4741500Serics { 4751500Serics signal(SIGQUIT, SIG_IGN); 4761500Serics if (!inwait) { 4771500Serics putchar ('\n'); 4781500Serics if (!startup) { 4791500Serics signal(SIGQUIT, onquit); 4801500Serics longjmp (restore, 1); 4811500Serics } 4821500Serics else 4831500Serics Pause++; 4841500Serics } 4851500Serics else if (!dum_opt && notell) { 4861500Serics write (2, "[Use q or Q to quit]", 20); 4871500Serics promptlen += 20; 4881500Serics notell = 0; 4891500Serics } 4901500Serics signal(SIGQUIT, onquit); 4911500Serics } 4921500Serics 4931500Serics /* 4941500Serics ** Clean up terminal state and exit. Also come here if interrupt signal received 4951500Serics */ 4961500Serics 4971500Serics end_it () 4981500Serics { 4991500Serics 5001500Serics reset_tty (); 5013594Sroot if (clreol) { 5023594Sroot putchar ('\r'); 5033594Sroot clreos (); 5043594Sroot fflush (stdout); 5053594Sroot } 5063455Sroot else if (!clreol && (promptlen > 0)) { 5071500Serics kill_line (); 5081500Serics fflush (stdout); 5091500Serics } 5101500Serics else 5111500Serics write (2, "\n", 1); 5121500Serics _exit(0); 5131500Serics } 5141500Serics 5151500Serics copy_file(f) 5161500Serics register FILE *f; 5171500Serics { 5181500Serics register int c; 5191500Serics 5201500Serics while ((c = getc(f)) != EOF) 5211500Serics putchar(c); 5221500Serics } 5231500Serics 5241500Serics /* Simplified printf function */ 5251500Serics 5261500Serics printf (fmt, args) 5271500Serics register char *fmt; 5281500Serics int args; 5291500Serics { 5301500Serics register int *argp; 5311500Serics register char ch; 5321500Serics register int ccount; 5331500Serics 5341500Serics ccount = 0; 5351500Serics argp = &args; 5361500Serics while (*fmt) { 5371500Serics while ((ch = *fmt++) != '%') { 5381500Serics if (ch == '\0') 5391500Serics return (ccount); 5401500Serics ccount++; 5411500Serics putchar (ch); 5421500Serics } 5431500Serics switch (*fmt++) { 5441500Serics case 'd': 5451500Serics ccount += printd (*argp); 5461500Serics break; 5471500Serics case 's': 5481500Serics ccount += pr ((char *)*argp); 5491500Serics break; 5501500Serics case '%': 5511500Serics ccount++; 5521500Serics argp--; 5531500Serics putchar ('%'); 5541500Serics break; 5551500Serics case '0': 5561500Serics return (ccount); 5571500Serics default: 5581500Serics break; 5591500Serics } 5601500Serics ++argp; 5611500Serics } 5621500Serics return (ccount); 5631500Serics 5641500Serics } 5651500Serics 5661500Serics /* 5671500Serics ** Print an integer as a string of decimal digits, 5681500Serics ** returning the length of the print representation. 5691500Serics */ 5701500Serics 5711500Serics printd (n) 5721500Serics int n; 5731500Serics { 5741500Serics int a, nchars; 5751500Serics 5761500Serics if (a = n/10) 5771500Serics nchars = 1 + printd(a); 5781500Serics else 5791500Serics nchars = 1; 5801500Serics putchar (n % 10 + '0'); 5811500Serics return (nchars); 5821500Serics } 5831500Serics 5841500Serics /* Put the print representation of an integer into a string */ 5851500Serics static char *sptr; 5861500Serics 5871500Serics scanstr (n, str) 5881500Serics int n; 5891500Serics char *str; 5901500Serics { 5911500Serics sptr = str; 59211604Slayer Sprintf (n); 5931500Serics *sptr = '\0'; 5941500Serics } 5951500Serics 59611604Slayer Sprintf (n) 5971500Serics { 5981500Serics int a; 5991500Serics 6001500Serics if (a = n/10) 60111604Slayer Sprintf (a); 6021500Serics *sptr++ = n % 10 + '0'; 6031500Serics } 6041500Serics 6051500Serics static char bell = ctrl(G); 6061500Serics 6071500Serics strlen (s) 6081500Serics char *s; 6091500Serics { 6101500Serics register char *p; 6111500Serics 6121500Serics p = s; 6131500Serics while (*p++) 6141500Serics ; 6151500Serics return (p - s - 1); 6161500Serics } 6171500Serics 6181500Serics /* See whether the last component of the path name "path" is equal to the 6191500Serics ** string "string" 6201500Serics */ 6211500Serics 6221500Serics tailequ (path, string) 6231500Serics char *path; 6241500Serics register char *string; 6251500Serics { 6261500Serics register char *tail; 6271500Serics 6281500Serics tail = path + strlen(path); 6291500Serics while (tail >= path) 6301500Serics if (*(--tail) == '/') 6311500Serics break; 6321500Serics ++tail; 6331500Serics while (*tail++ == *string++) 6341500Serics if (*tail == '\0') 6351500Serics return(1); 6361500Serics return(0); 6371500Serics } 6381500Serics 6391500Serics prompt (filename) 6401500Serics char *filename; 6411500Serics { 6423594Sroot if (clreol) 6433594Sroot cleareol (); 6443455Sroot else if (promptlen > 0) 6451500Serics kill_line (); 6461500Serics if (!hard) { 6471500Serics promptlen = 8; 64816710Sjak if (Senter && Sexit) { 6491500Serics tputs (Senter, 1, putch); 65016710Sjak promptlen += (2 * soglitch); 65116710Sjak } 6523594Sroot if (clreol) 6533594Sroot cleareol (); 6541500Serics pr("--More--"); 6551500Serics if (filename != NULL) { 6561500Serics promptlen += printf ("(Next file: %s)", filename); 6571500Serics } 6581500Serics else if (!no_intty) { 6591500Serics promptlen += printf ("(%d%%)", (int)((file_pos * 100) / file_size)); 6601500Serics } 6611500Serics if (dum_opt) { 66216710Sjak promptlen += pr("[Press space to continue, 'q' to quit.]"); 6631500Serics } 6641500Serics if (Senter && Sexit) 6651500Serics tputs (Sexit, 1, putch); 6663594Sroot if (clreol) 6673594Sroot clreos (); 6681500Serics fflush(stdout); 6691500Serics } 6701500Serics else 6711500Serics write (2, &bell, 1); 6721500Serics inwait++; 6731500Serics } 6741500Serics 6751500Serics /* 6761500Serics ** Get a logical line 6771500Serics */ 6781500Serics 6791500Serics getline(f, length) 6801500Serics register FILE *f; 6811500Serics int *length; 6821500Serics { 6831500Serics register int c; 6841500Serics register char *p; 6851500Serics register int column; 6861500Serics static int colflg; 6871500Serics 6881500Serics p = Line; 6891500Serics column = 0; 6901500Serics c = Getc (f); 6911500Serics if (colflg && c == '\n') { 6921500Serics Currline++; 6931500Serics c = Getc (f); 6941500Serics } 6951500Serics while (p < &Line[LINSIZ - 1]) { 6961500Serics if (c == EOF) { 6971500Serics if (p > Line) { 6981500Serics *p = '\0'; 6991500Serics *length = p - Line; 7001500Serics return (column); 7011500Serics } 7021500Serics *length = p - Line; 7031500Serics return (EOF); 7041500Serics } 7051500Serics if (c == '\n') { 7061500Serics Currline++; 7071500Serics break; 7081500Serics } 7091500Serics *p++ = c; 7101500Serics if (c == '\t') 7111500Serics if (hardtabs && column < promptlen && !hard) { 7121500Serics if (eraseln && !dumb) { 7131500Serics column = 1 + (column | 7); 7141500Serics tputs (eraseln, 1, putch); 7151500Serics promptlen = 0; 7161500Serics } 7171500Serics else { 7181500Serics for (--p; column & 7 && p < &Line[LINSIZ - 1]; column++) { 7191500Serics *p++ = ' '; 7201500Serics } 7211500Serics if (column >= promptlen) promptlen = 0; 7221500Serics } 7231500Serics } 7241500Serics else 7251500Serics column = 1 + (column | 7); 7269627Ssklower else if (c == '\b' && column > 0) 7271500Serics column--; 7281500Serics else if (c == '\r') 7291500Serics column = 0; 7301500Serics else if (c == '\f' && stop_opt) { 7311500Serics p[-1] = '^'; 7321500Serics *p++ = 'L'; 7331500Serics column += 2; 7341500Serics Pause++; 7351500Serics } 7361500Serics else if (c == EOF) { 7371500Serics *length = p - Line; 7381500Serics return (column); 7391500Serics } 7401500Serics else if (c >= ' ' && c != RUBOUT) 7411500Serics column++; 7421500Serics if (column >= Mcol && fold_opt) break; 7431500Serics c = Getc (f); 7441500Serics } 7451500Serics if (column >= Mcol && Mcol > 0) { 7461500Serics if (!Wrap) { 7471500Serics *p++ = '\n'; 7481500Serics } 7491500Serics } 7501500Serics colflg = column == Mcol && fold_opt; 7511500Serics *length = p - Line; 7521500Serics *p = 0; 7531500Serics return (column); 7541500Serics } 7551500Serics 7561500Serics /* 7571500Serics ** Erase the rest of the prompt, assuming we are starting at column col. 7581500Serics */ 7591500Serics 7601500Serics erase (col) 7611500Serics register int col; 7621500Serics { 7631500Serics 7641500Serics if (promptlen == 0) 7651500Serics return; 7661500Serics if (hard) { 7671500Serics putchar ('\n'); 7681500Serics } 7691500Serics else { 7701500Serics if (col == 0) 7711500Serics putchar ('\r'); 7721500Serics if (!dumb && eraseln) 7731500Serics tputs (eraseln, 1, putch); 7741500Serics else 7751500Serics for (col = promptlen - col; col > 0; col--) 7761500Serics putchar (' '); 7771500Serics } 7781500Serics promptlen = 0; 7791500Serics } 7801500Serics 7811500Serics /* 7821500Serics ** Erase the current line entirely 7831500Serics */ 7841500Serics 7851500Serics kill_line () 7861500Serics { 7871500Serics erase (0); 7881500Serics if (!eraseln || dumb) putchar ('\r'); 7891500Serics } 7901500Serics 7911500Serics /* 7923455Sroot * force clear to end of line 7933455Sroot */ 7943455Sroot cleareol() 7953455Sroot { 7963594Sroot tputs(eraseln, 1, putch); 7973455Sroot } 7983455Sroot 7993594Sroot clreos() 8003455Sroot { 8013594Sroot tputs(EodClr, 1, putch); 8023455Sroot } 8033455Sroot 8043455Sroot /* 8051500Serics ** Print string and return number of characters 8061500Serics */ 8071500Serics 8081500Serics pr(s1) 8091500Serics char *s1; 8101500Serics { 8111500Serics register char *s; 8121500Serics register char c; 8131500Serics 8141500Serics for (s = s1; c = *s++; ) 8151500Serics putchar(c); 8161500Serics return (s - s1 - 1); 8171500Serics } 8181500Serics 8191500Serics 8201500Serics /* Print a buffer of n characters */ 8211500Serics 8221500Serics prbuf (s, n) 8231500Serics register char *s; 8241500Serics register int n; 8251500Serics { 82616710Sjak register char c; /* next output character */ 8273594Sroot register int state; /* next output char's UL state */ 82816710Sjak #define wouldul(s,n) ((n) >= 2 && (((s)[0] == '_' && (s)[1] == '\b') || ((s)[1] == '\b' && (s)[2] == '_'))) 8293594Sroot 8303594Sroot while (--n >= 0) 8313594Sroot if (!ul_opt) 8323594Sroot putchar (*s++); 8333594Sroot else { 83416710Sjak if (*s == ' ' && pstate == 0 && ulglitch && wouldul(s+1, n-1)) { 83516710Sjak s++; 83616710Sjak continue; 83716710Sjak } 83816710Sjak if (state = wouldul(s, n)) { 83916710Sjak c = (*s == '_')? s[2] : *s ; 8403594Sroot n -= 2; 84116710Sjak s += 3; 84216710Sjak } else 8433594Sroot c = *s++; 84416710Sjak if (state != pstate) { 84516710Sjak if (c == ' ' && state == 0 && ulglitch && wouldul(s, n-1)) 84616710Sjak state = 1; 84716710Sjak else 84816710Sjak tputs(state ? ULenter : ULexit, 1, putch); 8493594Sroot } 85016710Sjak if (c != ' ' || pstate == 0 || state != 0 || ulglitch == 0) 85116710Sjak putchar(c); 8523594Sroot if (state && *chUL) { 8533594Sroot pr(chBS); 8543594Sroot tputs(chUL, 1, putch); 8553594Sroot } 85616710Sjak pstate = state; 8573594Sroot } 8581500Serics } 8591500Serics 8601500Serics /* 8611500Serics ** Clear the screen 8621500Serics */ 8631500Serics 8641500Serics doclear() 8651500Serics { 8661500Serics if (Clear && !hard) { 8671500Serics tputs(Clear, 1, putch); 8681500Serics 8691500Serics /* Put out carriage return so that system doesn't 8701500Serics ** get confused by escape sequences when expanding tabs 8711500Serics */ 8721500Serics putchar ('\r'); 8731500Serics promptlen = 0; 8741500Serics } 8751500Serics } 8761500Serics 8773455Sroot /* 8783455Sroot * Go to home position 8793455Sroot */ 8803455Sroot home() 8813455Sroot { 8823455Sroot tputs(Home,1,putch); 8833455Sroot } 8843455Sroot 8851500Serics static int lastcmd, lastarg, lastp; 8861500Serics static int lastcolon; 8871500Serics char shell_line[132]; 8881500Serics 8891500Serics /* 8901500Serics ** Read a command and do it. A command consists of an optional integer 8911500Serics ** argument followed by the command character. Return the number of lines 8921500Serics ** to display in the next screenful. If there is nothing more to display 8931500Serics ** in the current file, zero is returned. 8941500Serics */ 8951500Serics 8961500Serics command (filename, f) 8971500Serics char *filename; 8981500Serics register FILE *f; 8991500Serics { 9001500Serics register int nlines; 9011500Serics register int retval; 9021500Serics register char c; 9031500Serics char colonch; 9041500Serics FILE *helpf; 9051500Serics int done; 9061500Serics char comchar, cmdbuf[80], *p; 9071500Serics 9081500Serics #define ret(val) retval=val;done++;break 9091500Serics 9101500Serics done = 0; 9111500Serics if (!errors) 9121500Serics prompt (filename); 9131500Serics else 9141500Serics errors = 0; 9151500Serics if (MBIT == RAW && slow_tty) { 9161500Serics otty.sg_flags |= MBIT; 91716582Sleres stty(fileno(stderr), &otty); 9181500Serics } 9191500Serics for (;;) { 9201500Serics nlines = number (&comchar); 9211500Serics lastp = colonch = 0; 9221500Serics if (comchar == '.') { /* Repeat last command */ 9231500Serics lastp++; 9241500Serics comchar = lastcmd; 9251500Serics nlines = lastarg; 9261500Serics if (lastcmd == ':') 9271500Serics colonch = lastcolon; 9281500Serics } 9291500Serics lastcmd = comchar; 9301500Serics lastarg = nlines; 9311500Serics if (comchar == otty.sg_erase) { 9321500Serics kill_line (); 9331500Serics prompt (filename); 9341500Serics continue; 9351500Serics } 9361500Serics switch (comchar) { 9371500Serics case ':': 9381500Serics retval = colon (filename, colonch, nlines); 9391500Serics if (retval >= 0) 9401500Serics done++; 9411500Serics break; 9421500Serics case ' ': 9431500Serics case 'z': 9441500Serics if (nlines == 0) nlines = dlines; 9451500Serics else if (comchar == 'z') dlines = nlines; 9461500Serics ret (nlines); 9471500Serics case 'd': 9481500Serics case ctrl(D): 9491500Serics if (nlines != 0) nscroll = nlines; 9501500Serics ret (nscroll); 9511500Serics case RUBOUT: 9521500Serics case 'q': 9531500Serics case 'Q': 9541500Serics end_it (); 9551500Serics case 's': 9561500Serics case 'f': 9571500Serics if (nlines == 0) nlines++; 9581500Serics if (comchar == 'f') 9591500Serics nlines *= dlines; 9601500Serics putchar ('\r'); 9611500Serics erase (0); 9623594Sroot printf ("\n"); 9633594Sroot if (clreol) 9643594Sroot cleareol (); 9653594Sroot printf ("...skipping %d line", nlines); 9661500Serics if (nlines > 1) 9673594Sroot pr ("s\n"); 9681500Serics else 9693594Sroot pr ("\n"); 9703594Sroot 9713594Sroot if (clreol) 9723594Sroot cleareol (); 9733594Sroot pr ("\n"); 9743594Sroot 9751500Serics while (nlines > 0) { 9761500Serics while ((c = Getc (f)) != '\n') 9771500Serics if (c == EOF) { 9781500Serics retval = 0; 9791500Serics done++; 9801500Serics goto endsw; 9811500Serics } 9821500Serics Currline++; 9831500Serics nlines--; 9841500Serics } 9851500Serics ret (dlines); 9861500Serics case '\n': 9871500Serics if (nlines != 0) 9881500Serics dlines = nlines; 9891500Serics else 9901500Serics nlines = 1; 9911500Serics ret (nlines); 9921500Serics case '\f': 9931500Serics if (!no_intty) { 9941500Serics doclear (); 9951500Serics Fseek (f, screen_start.chrctr); 9961500Serics Currline = screen_start.line; 9971500Serics ret (dlines); 9981500Serics } 9991500Serics else { 10001500Serics write (2, &bell, 1); 10011500Serics break; 10021500Serics } 10031500Serics case '\'': 10041500Serics if (!no_intty) { 10051500Serics kill_line (); 10061500Serics pr ("\n***Back***\n\n"); 10071500Serics Fseek (f, context.chrctr); 10081500Serics Currline = context.line; 10091500Serics ret (dlines); 10101500Serics } 10111500Serics else { 10121500Serics write (2, &bell, 1); 10131500Serics break; 10141500Serics } 10151500Serics case '=': 10161500Serics kill_line (); 10171500Serics promptlen = printd (Currline); 10181500Serics fflush (stdout); 10191500Serics break; 10201500Serics case 'n': 10211500Serics lastp++; 10221500Serics case '/': 10231500Serics if (nlines == 0) nlines++; 10241500Serics kill_line (); 10251500Serics pr ("/"); 10261500Serics promptlen = 1; 10271500Serics fflush (stdout); 10281500Serics if (lastp) { 10291500Serics write (2,"\r", 1); 10301500Serics search (NULL, f, nlines); /* Use previous r.e. */ 10311500Serics } 10321500Serics else { 10331500Serics ttyin (cmdbuf, 78, '/'); 10341500Serics write (2, "\r", 1); 10351500Serics search (cmdbuf, f, nlines); 10361500Serics } 10373455Sroot ret (dlines-1); 10381500Serics case '!': 10391500Serics do_shell (filename); 10401500Serics break; 10411500Serics case 'h': 10421500Serics if ((helpf = fopen (HELPFILE, "r")) == NULL) 10431500Serics error ("Can't open help file"); 10441500Serics if (noscroll) doclear (); 10451500Serics copy_file (helpf); 10461500Serics close (helpf); 10471500Serics prompt (filename); 10481500Serics break; 10491500Serics case 'v': /* This case should go right before default */ 10501500Serics if (!no_intty) { 10511500Serics kill_line (); 10521500Serics cmdbuf[0] = '+'; 10531500Serics scanstr (Currline, &cmdbuf[1]); 10541500Serics pr ("vi "); pr (cmdbuf); putchar (' '); pr (fnames[fnum]); 10551500Serics execute (filename, VI, "vi", cmdbuf, fnames[fnum], 0); 10561500Serics break; 10571500Serics } 10581500Serics default: 105916710Sjak if (dum_opt) { 106016710Sjak kill_line (); 106116710Sjak if (Senter && Sexit) { 106216710Sjak tputs (Senter, 1, putch); 106316710Sjak promptlen = pr ("[Press 'h' for instructions.]") + (2 * soglitch); 106416710Sjak tputs (Sexit, 1, putch); 106516710Sjak } 106616710Sjak else 106716710Sjak promptlen = pr ("[Press 'h' for instructions.]"); 106816710Sjak fflush (stdout); 106916710Sjak } 107016710Sjak else 107116710Sjak write (2, &bell, 1); 10721500Serics break; 10731500Serics } 10741500Serics if (done) break; 10751500Serics } 10761500Serics putchar ('\r'); 10771500Serics endsw: 10781500Serics inwait = 0; 10791500Serics notell++; 10801500Serics if (MBIT == RAW && slow_tty) { 10811500Serics otty.sg_flags &= ~MBIT; 108216582Sleres stty(fileno(stderr), &otty); 10831500Serics } 10841500Serics return (retval); 10851500Serics } 10861500Serics 10871500Serics char ch; 10881500Serics 10891500Serics /* 10901500Serics * Execute a colon-prefixed command. 10911500Serics * Returns <0 if not a command that should cause 10921500Serics * more of the file to be printed. 10931500Serics */ 10941500Serics 10951500Serics colon (filename, cmd, nlines) 10961500Serics char *filename; 10971500Serics int cmd; 10981500Serics int nlines; 10991500Serics { 11001500Serics if (cmd == 0) 11011500Serics ch = readch (); 11021500Serics else 11031500Serics ch = cmd; 11041500Serics lastcolon = ch; 11051500Serics switch (ch) { 11061500Serics case 'f': 11071500Serics kill_line (); 11081500Serics if (!no_intty) 11091500Serics promptlen = printf ("\"%s\" line %d", fnames[fnum], Currline); 11101500Serics else 11111500Serics promptlen = printf ("[Not a file] line %d", Currline); 11121500Serics fflush (stdout); 11131500Serics return (-1); 11141500Serics case 'n': 11151500Serics if (nlines == 0) { 11161500Serics if (fnum >= nfiles - 1) 11171500Serics end_it (); 11181500Serics nlines++; 11191500Serics } 11201500Serics putchar ('\r'); 11211500Serics erase (0); 11221500Serics skipf (nlines); 11231500Serics return (0); 11241500Serics case 'p': 11251500Serics if (no_intty) { 11261500Serics write (2, &bell, 1); 11271500Serics return (-1); 11281500Serics } 11291500Serics putchar ('\r'); 11301500Serics erase (0); 11311500Serics if (nlines == 0) 11321500Serics nlines++; 11331500Serics skipf (-nlines); 11341500Serics return (0); 11351500Serics case '!': 11361500Serics do_shell (filename); 11371500Serics return (-1); 11381500Serics case 'q': 11391500Serics case 'Q': 11401500Serics end_it (); 11411500Serics default: 11421500Serics write (2, &bell, 1); 11431500Serics return (-1); 11441500Serics } 11451500Serics } 11461500Serics 11471500Serics /* 11481500Serics ** Read a decimal number from the terminal. Set cmd to the non-digit which 11491500Serics ** terminates the number. 11501500Serics */ 11511500Serics 11521500Serics number(cmd) 11531500Serics char *cmd; 11541500Serics { 11551500Serics register int i; 11561500Serics 11571500Serics i = 0; ch = otty.sg_kill; 11581500Serics for (;;) { 11591500Serics ch = readch (); 11601500Serics if (ch >= '0' && ch <= '9') 11611500Serics i = i*10 + ch - '0'; 11621500Serics else if (ch == otty.sg_kill) 11631500Serics i = 0; 11641500Serics else { 11651500Serics *cmd = ch; 11661500Serics break; 11671500Serics } 11681500Serics } 11691500Serics return (i); 11701500Serics } 11711500Serics 11721500Serics do_shell (filename) 11731500Serics char *filename; 11741500Serics { 11751500Serics char cmdbuf[80]; 11761500Serics 11771500Serics kill_line (); 11781500Serics pr ("!"); 11791500Serics fflush (stdout); 11801500Serics promptlen = 1; 11811500Serics if (lastp) 11821500Serics pr (shell_line); 11831500Serics else { 11841500Serics ttyin (cmdbuf, 78, '!'); 11851500Serics if (expand (shell_line, cmdbuf)) { 11861500Serics kill_line (); 11871500Serics promptlen = printf ("!%s", shell_line); 11881500Serics } 11891500Serics } 11901500Serics fflush (stdout); 11911500Serics write (2, "\n", 1); 11921500Serics promptlen = 0; 11931500Serics shellp = 1; 11941500Serics execute (filename, shell, shell, "-c", shell_line, 0); 11951500Serics } 11961500Serics 11971500Serics /* 11981500Serics ** Search for nth ocurrence of regular expression contained in buf in the file 11991500Serics */ 12001500Serics 12011500Serics search (buf, file, n) 12021500Serics char buf[]; 12031500Serics FILE *file; 12041500Serics register int n; 12051500Serics { 12061500Serics long startline = Ftell (file); 12071500Serics register long line1 = startline; 12081500Serics register long line2 = startline; 12091500Serics register long line3 = startline; 12101500Serics register int lncount; 12111500Serics int saveln, rv, re_exec(); 12121500Serics char *s, *re_comp(); 12131500Serics 12141500Serics context.line = saveln = Currline; 12151500Serics context.chrctr = startline; 12161500Serics lncount = 0; 12171500Serics if ((s = re_comp (buf)) != 0) 12181500Serics error (s); 12191500Serics while (!feof (file)) { 12201500Serics line3 = line2; 12211500Serics line2 = line1; 12221500Serics line1 = Ftell (file); 12231500Serics rdline (file); 12241500Serics lncount++; 12251500Serics if ((rv = re_exec (Line)) == 1) 12261500Serics if (--n == 0) { 12271500Serics if (lncount > 3 || (lncount > 1 && no_intty)) 12283455Sroot { 12293455Sroot pr ("\n"); 12303594Sroot if (clreol) 12313594Sroot cleareol (); 12323455Sroot pr("...skipping\n"); 12333455Sroot } 12341500Serics if (!no_intty) { 12351500Serics Currline -= (lncount >= 3 ? 3 : lncount); 12361500Serics Fseek (file, line3); 12373594Sroot if (noscroll) 12383594Sroot if (clreol) { 12393594Sroot home (); 12403594Sroot cleareol (); 124116582Sleres } 12423594Sroot else 12433594Sroot doclear (); 12441500Serics } 12451500Serics else { 12461500Serics kill_line (); 12473594Sroot if (noscroll) 12483594Sroot if (clreol) { 124916582Sleres home (); 12503594Sroot cleareol (); 125116582Sleres } 12523594Sroot else 12533594Sroot doclear (); 12541500Serics pr (Line); 12551500Serics putchar ('\n'); 12561500Serics } 12571500Serics break; 12581500Serics } 12591500Serics else if (rv == -1) 12601500Serics error ("Regular expression botch"); 12611500Serics } 12621500Serics if (feof (file)) { 12631500Serics if (!no_intty) { 12641500Serics file->_flag &= ~_IOEOF; /* why doesn't fseek do this ??!!??! */ 12651500Serics Currline = saveln; 12661500Serics Fseek (file, startline); 12671500Serics } 12681500Serics else { 12691500Serics pr ("\nPattern not found\n"); 12701500Serics end_it (); 12711500Serics } 12721500Serics error ("Pattern not found"); 12731500Serics } 12741500Serics } 12751500Serics 12761500Serics execute (filename, cmd, args) 12771500Serics char *filename; 12781500Serics char *cmd, *args; 12791500Serics { 12801500Serics int id; 128116710Sjak int n; 12821500Serics 12831500Serics fflush (stdout); 12841500Serics reset_tty (); 128516710Sjak for (n = 10; (id = fork ()) < 0 && n > 0; n--) 12861500Serics sleep (5); 12871500Serics if (id == 0) { 128816710Sjak if (!isatty(0)) { 128916710Sjak close(0); 129016710Sjak open("/dev/tty", 0); 129116710Sjak } 12921500Serics execv (cmd, &args); 12931500Serics write (2, "exec failed\n", 12); 12941500Serics exit (1); 12951500Serics } 129616710Sjak if (id > 0) { 129716710Sjak signal (SIGINT, SIG_IGN); 129816710Sjak signal (SIGQUIT, SIG_IGN); 129916710Sjak if (catch_susp) 130016710Sjak signal(SIGTSTP, SIG_DFL); 130116710Sjak while (wait(0) > 0); 130216710Sjak signal (SIGINT, end_it); 130316710Sjak signal (SIGQUIT, onquit); 130416710Sjak if (catch_susp) 130516710Sjak signal(SIGTSTP, onsusp); 130616710Sjak } else 130716710Sjak write(2, "can't fork\n", 11); 13081500Serics set_tty (); 13091500Serics pr ("------------------------\n"); 13101500Serics prompt (filename); 13111500Serics } 13121500Serics /* 13131500Serics ** Skip n lines in the file f 13141500Serics */ 13151500Serics 13161500Serics skiplns (n, f) 13171500Serics register int n; 13181500Serics register FILE *f; 13191500Serics { 13201500Serics register char c; 13211500Serics 13221500Serics while (n > 0) { 13231500Serics while ((c = Getc (f)) != '\n') 13241500Serics if (c == EOF) 13251500Serics return; 13261500Serics n--; 13271500Serics Currline++; 13281500Serics } 13291500Serics } 13301500Serics 13311500Serics /* 13321500Serics ** Skip nskip files in the file list (from the command line). Nskip may be 13331500Serics ** negative. 13341500Serics */ 13351500Serics 13361500Serics skipf (nskip) 13371500Serics register int nskip; 13381500Serics { 13391500Serics if (nskip == 0) return; 13401500Serics if (nskip > 0) { 13411500Serics if (fnum + nskip > nfiles - 1) 13421500Serics nskip = nfiles - fnum - 1; 13431500Serics } 13441500Serics else if (within) 13451500Serics ++fnum; 13461500Serics fnum += nskip; 13471500Serics if (fnum < 0) 13481500Serics fnum = 0; 13493594Sroot pr ("\n...Skipping "); 13503455Sroot pr ("\n"); 13513594Sroot if (clreol) 13523594Sroot cleareol (); 13533455Sroot pr ("...Skipping "); 13541500Serics pr (nskip > 0 ? "to file " : "back to file "); 13551500Serics pr (fnames[fnum]); 13563455Sroot pr ("\n"); 13573594Sroot if (clreol) 13583594Sroot cleareol (); 13593455Sroot pr ("\n"); 13601500Serics --fnum; 13611500Serics } 13621500Serics 13631500Serics /*----------------------------- Terminal I/O -------------------------------*/ 13641500Serics 13651500Serics initterm () 13661500Serics { 13671500Serics char buf[TBUFSIZ]; 136817195Sralph static char clearbuf[TBUFSIZ]; 13691500Serics char *clearptr, *padstr; 13701500Serics int ldisc; 137117592Sleres int lmode; 137210823Ssam char *term; 137316582Sleres int tgrp; 137418030Sbloom struct winsize win; 13751500Serics 137616582Sleres retry: 137716582Sleres if (!(no_tty = gtty(fileno(stdout), &otty))) { 137817592Sleres if (ioctl(fileno(stdout), TIOCLGET, &lmode) < 0) { 137917592Sleres perror("TIOCLGET"); 138017592Sleres exit(1); 138117592Sleres } 138217592Sleres docrterase = ((lmode & LCRTERA) != 0); 138317592Sleres docrtkill = ((lmode & LCRTKIL) != 0); 138416582Sleres /* 138517592Sleres * Wait until we're in the foreground before we save the 138617592Sleres * the terminal modes. 138716582Sleres */ 138816582Sleres if (ioctl(fileno(stdout), TIOCGPGRP, &tgrp) < 0) { 138917592Sleres perror("TIOCGPGRP"); 139016582Sleres exit(1); 139116582Sleres } 139216582Sleres if (tgrp != getpgrp(0)) { 139316582Sleres kill(0, SIGTTOU); 139416582Sleres goto retry; 139516582Sleres } 139613830Skre if ((term = getenv("TERM")) == 0 || tgetent(buf, term) <= 0) { 13973594Sroot dumb++; ul_opt = 0; 13981500Serics } 13991500Serics else { 140018030Sbloom if (ioctl(fileno(stdout), TIOCGWINSZ, &win) < 0) { 140118030Sbloom Lpp = tgetnum("li"); 140218030Sbloom Mcol = tgetnum("co"); 140318030Sbloom } else { 140418030Sbloom if ((Lpp = win.ws_row) == 0) 140518030Sbloom Lpp = tgetnum("li"); 140618030Sbloom if ((Mcol = win.ws_col) == 0) 140718030Sbloom Mcol = tgetnum("co"); 140818030Sbloom } 140918030Sbloom if ((Lpp <= 0) || tgetflag("hc")) { 14101500Serics hard++; /* Hard copy terminal */ 14111500Serics Lpp = 24; 14121500Serics } 141318030Sbloom if (Mcol <= 0) 141418030Sbloom Mcol = 80; 141518030Sbloom 14161500Serics if (tailequ (fnames[0], "page") || !hard && tgetflag("ns")) 14171500Serics noscroll++; 14181500Serics Wrap = tgetflag("am"); 14191500Serics bad_so = tgetflag ("xs"); 14201500Serics clearptr = clearbuf; 14211500Serics eraseln = tgetstr("ce",&clearptr); 14221500Serics Clear = tgetstr("cl", &clearptr); 14231500Serics Senter = tgetstr("so", &clearptr); 14241500Serics Sexit = tgetstr("se", &clearptr); 142516710Sjak if ((soglitch = tgetnum("sg")) < 0) 142616710Sjak soglitch = 0; 14273594Sroot 14283594Sroot /* 14293594Sroot * Set up for underlining: some terminals don't need it; 14303594Sroot * others have start/stop sequences, still others have an 14313594Sroot * underline char sequence which is assumed to move the 14323594Sroot * cursor forward one character. If underline sequence 14333594Sroot * isn't available, settle for standout sequence. 14343594Sroot */ 14353594Sroot 14363594Sroot if (tgetflag("ul") || tgetflag("os")) 14373594Sroot ul_opt = 0; 14383594Sroot if ((chUL = tgetstr("uc", &clearptr)) == NULL ) 14393594Sroot chUL = ""; 144016710Sjak if (((ULenter = tgetstr("us", &clearptr)) == NULL || 144116710Sjak (ULexit = tgetstr("ue", &clearptr)) == NULL) && !*chUL) { 144216710Sjak if ((ULenter = Senter) == NULL || (ULexit = Sexit) == NULL) { 144316710Sjak ULenter = ""; 144416710Sjak ULexit = ""; 144516710Sjak } else 144616710Sjak ulglitch = soglitch; 144716710Sjak } else { 144816710Sjak if ((ulglitch = tgetnum("ug")) < 0) 144916710Sjak ulglitch = 0; 145016710Sjak } 145116582Sleres 14521500Serics if (padstr = tgetstr("pc", &clearptr)) 14531500Serics PC = *padstr; 14543455Sroot Home = tgetstr("ho",&clearptr); 145513536Ssam if (Home == 0 || *Home == '\0') 14563455Sroot { 14573594Sroot if ((cursorm = tgetstr("cm", &clearptr)) != NULL) { 14583594Sroot strcpy(cursorhome, tgoto(cursorm, 0, 0)); 14593455Sroot Home = cursorhome; 14603455Sroot } 14613455Sroot } 14623594Sroot EodClr = tgetstr("cd", &clearptr); 14631500Serics } 14641500Serics if ((shell = getenv("SHELL")) == NULL) 14651500Serics shell = "/bin/sh"; 14661500Serics } 146716582Sleres no_intty = gtty(fileno(stdin), &otty); 146816582Sleres gtty(fileno(stderr), &otty); 146913830Skre savetty = otty; 14701500Serics ospeed = otty.sg_ospeed; 14711500Serics slow_tty = ospeed < B1200; 14721500Serics hardtabs = !(otty.sg_flags & XTABS); 14731500Serics if (!no_tty) { 14741500Serics otty.sg_flags &= ~ECHO; 14751500Serics if (MBIT == CBREAK || !slow_tty) 14761500Serics otty.sg_flags |= MBIT; 14771500Serics } 14781500Serics } 14791500Serics 14801500Serics readch () 14811500Serics { 14821500Serics char ch; 14831500Serics extern int errno; 14841500Serics 14851500Serics if (read (2, &ch, 1) <= 0) 14861500Serics if (errno != EINTR) 14871500Serics exit(0); 14881500Serics else 14891500Serics ch = otty.sg_kill; 14901500Serics return (ch); 14911500Serics } 14921500Serics 14931500Serics static char BS = '\b'; 149417592Sleres static char *BSB = "\b \b"; 14951500Serics static char CARAT = '^'; 149617592Sleres #define ERASEONECHAR \ 149717592Sleres if (docrterase) \ 149817592Sleres write (2, BSB, sizeof(BSB)); \ 149917592Sleres else \ 150017592Sleres write (2, &BS, sizeof(BS)); 15011500Serics 15021500Serics ttyin (buf, nmax, pchar) 15031500Serics char buf[]; 15041500Serics register int nmax; 15051500Serics char pchar; 15061500Serics { 15071500Serics register char *sptr; 15081500Serics register char ch; 15091500Serics register int slash = 0; 15101500Serics int maxlen; 15111500Serics char cbuf; 15121500Serics 15131500Serics sptr = buf; 15141500Serics maxlen = 0; 15151500Serics while (sptr - buf < nmax) { 15161500Serics if (promptlen > maxlen) maxlen = promptlen; 15171500Serics ch = readch (); 15181500Serics if (ch == '\\') { 15191500Serics slash++; 15201500Serics } 15211500Serics else if ((ch == otty.sg_erase) && !slash) { 15221500Serics if (sptr > buf) { 15231500Serics --promptlen; 152417592Sleres ERASEONECHAR 15251500Serics --sptr; 15261500Serics if ((*sptr < ' ' && *sptr != '\n') || *sptr == RUBOUT) { 15271500Serics --promptlen; 152817592Sleres ERASEONECHAR 15291500Serics } 15301500Serics continue; 15311500Serics } 15321500Serics else { 15331500Serics if (!eraseln) promptlen = maxlen; 15341500Serics longjmp (restore, 1); 15351500Serics } 15361500Serics } 15371500Serics else if ((ch == otty.sg_kill) && !slash) { 15381500Serics if (hard) { 15391500Serics show (ch); 15401500Serics putchar ('\n'); 15411500Serics putchar (pchar); 15421500Serics } 15431500Serics else { 15441500Serics putchar ('\r'); 15451500Serics putchar (pchar); 15461500Serics if (eraseln) 15471500Serics erase (1); 154817592Sleres else if (docrtkill) 154917592Sleres while (promptlen-- > 1) 155017592Sleres write (2, BSB, sizeof(BSB)); 15511500Serics promptlen = 1; 15521500Serics } 15531500Serics sptr = buf; 15541500Serics fflush (stdout); 15551500Serics continue; 15561500Serics } 15571500Serics if (slash && (ch == otty.sg_kill || ch == otty.sg_erase)) { 155817592Sleres ERASEONECHAR 15591500Serics --sptr; 15601500Serics } 15611500Serics if (ch != '\\') 15621500Serics slash = 0; 15631500Serics *sptr++ = ch; 15641500Serics if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) { 15651500Serics ch += ch == RUBOUT ? -0100 : 0100; 15661500Serics write (2, &CARAT, 1); 15671500Serics promptlen++; 15681500Serics } 15691500Serics cbuf = ch; 15701500Serics if (ch != '\n' && ch != ESC) { 15711500Serics write (2, &cbuf, 1); 15721500Serics promptlen++; 15731500Serics } 15741500Serics else 15751500Serics break; 15761500Serics } 15771500Serics *--sptr = '\0'; 15781500Serics if (!eraseln) promptlen = maxlen; 15791500Serics if (sptr - buf >= nmax - 1) 15801500Serics error ("Line too long"); 15811500Serics } 15821500Serics 15831500Serics expand (outbuf, inbuf) 15841500Serics char *outbuf; 15851500Serics char *inbuf; 15861500Serics { 15871500Serics register char *instr; 15881500Serics register char *outstr; 15891500Serics register char ch; 15901500Serics char temp[200]; 15911500Serics int changed = 0; 15921500Serics 15931500Serics instr = inbuf; 15941500Serics outstr = temp; 15951500Serics while ((ch = *instr++) != '\0') 15961500Serics switch (ch) { 15971500Serics case '%': 15981500Serics if (!no_intty) { 15991500Serics strcpy (outstr, fnames[fnum]); 16001500Serics outstr += strlen (fnames[fnum]); 16011500Serics changed++; 16021500Serics } 16031500Serics else 16041500Serics *outstr++ = ch; 16051500Serics break; 16061500Serics case '!': 16071500Serics if (!shellp) 16081500Serics error ("No previous command to substitute for"); 16091500Serics strcpy (outstr, shell_line); 16101500Serics outstr += strlen (shell_line); 16111500Serics changed++; 16121500Serics break; 16131500Serics case '\\': 16141500Serics if (*instr == '%' || *instr == '!') { 16151500Serics *outstr++ = *instr++; 16161500Serics break; 16171500Serics } 16181500Serics default: 16191500Serics *outstr++ = ch; 16201500Serics } 16211500Serics *outstr++ = '\0'; 16221500Serics strcpy (outbuf, temp); 16231500Serics return (changed); 16241500Serics } 16251500Serics 16261500Serics show (ch) 16271500Serics register char ch; 16281500Serics { 16291500Serics char cbuf; 16301500Serics 16311500Serics if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) { 16321500Serics ch += ch == RUBOUT ? -0100 : 0100; 16331500Serics write (2, &CARAT, 1); 16341500Serics promptlen++; 16351500Serics } 16361500Serics cbuf = ch; 16371500Serics write (2, &cbuf, 1); 16381500Serics promptlen++; 16391500Serics } 16401500Serics 16411500Serics error (mess) 16421500Serics char *mess; 16431500Serics { 16443594Sroot if (clreol) 16453594Sroot cleareol (); 16463594Sroot else 16473594Sroot kill_line (); 16481500Serics promptlen += strlen (mess); 16491500Serics if (Senter && Sexit) { 16501500Serics tputs (Senter, 1, putch); 16511500Serics pr(mess); 16521500Serics tputs (Sexit, 1, putch); 16531500Serics } 16541500Serics else 16551500Serics pr (mess); 16561500Serics fflush(stdout); 16571500Serics errors++; 16581500Serics longjmp (restore, 1); 16591500Serics } 16601500Serics 16611500Serics 16621500Serics set_tty () 16631500Serics { 16641500Serics otty.sg_flags |= MBIT; 16651500Serics otty.sg_flags &= ~ECHO; 166616582Sleres stty(fileno(stderr), &otty); 16671500Serics } 16681500Serics 16691500Serics reset_tty () 16701500Serics { 167116710Sjak if (pstate) { 167216710Sjak tputs(ULexit, 1, putch); 167316710Sjak fflush(stdout); 167416710Sjak pstate = 0; 167516710Sjak } 16761500Serics otty.sg_flags |= ECHO; 16771500Serics otty.sg_flags &= ~MBIT; 167816582Sleres stty(fileno(stderr), &savetty); 16791500Serics } 16801500Serics 16811500Serics rdline (f) 16821500Serics register FILE *f; 16831500Serics { 16841500Serics register char c; 16851500Serics register char *p; 16861500Serics 16871500Serics p = Line; 16881500Serics while ((c = Getc (f)) != '\n' && c != EOF && p - Line < LINSIZ - 1) 16891500Serics *p++ = c; 16901500Serics if (c == '\n') 16911500Serics Currline++; 16921500Serics *p = '\0'; 16931500Serics } 16941500Serics 16951500Serics /* Come here when we get a suspend signal from the terminal */ 16961500Serics 16971500Serics onsusp () 16981500Serics { 169914861Skarels /* ignore SIGTTOU so we don't get stopped if csh grabs the tty */ 170014861Skarels signal(SIGTTOU, SIG_IGN); 17011500Serics reset_tty (); 17021500Serics fflush (stdout); 170314861Skarels signal(SIGTTOU, SIG_DFL); 17041500Serics /* Send the TSTP signal to suspend our process group */ 170513289Ssam signal(SIGTSTP, SIG_DFL); 170613289Ssam sigsetmask(0); 17071500Serics kill (0, SIGTSTP); 17081500Serics /* Pause for station break */ 17091500Serics 17101500Serics /* We're back */ 17111500Serics signal (SIGTSTP, onsusp); 17121500Serics set_tty (); 17131500Serics if (inwait) 17141500Serics longjmp (restore); 17151500Serics } 1716