[BACK]Return to more.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / more

Annotation of src/usr.bin/more/more.c, Revision 1.6

1.6     ! etheisen    1: /*     $OpenBSD: more.c,v 1.5 1996/10/14 09:01:01 etheisen Exp $       */
1.1       etheisen    2: /*-
                      3:  * Copyright (c) 1980 The Regents of the University of California.
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  * 1. Redistributions of source code must retain the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer.
                     11:  * 2. Redistributions in binary form must reproduce the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer in the
                     13:  *    documentation and/or other materials provided with the distribution.
                     14:  * 3. All advertising materials mentioning features or use of this software
                     15:  *    must display the following acknowledgement:
                     16:  *     This product includes software developed by the University of
                     17:  *     California, Berkeley and its contributors.
                     18:  * 4. Neither the name of the University nor the names of its contributors
                     19:  *    may be used to endorse or promote products derived from this software
                     20:  *    without specific prior written permission.
                     21:  *
                     22:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     23:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     24:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     25:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     26:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     27:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     28:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     29:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     30:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     31:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     32:  * SUCH DAMAGE.
                     33:  */
                     34:
                     35: #ifndef lint
                     36: char copyright[] =
                     37: "@(#) Copyright (c) 1980 The Regents of the University of California.\n\
                     38:  All rights reserved.\n";
                     39: #endif /* not lint */
                     40:
                     41: #ifndef lint
                     42: static char sccsid[] = "@(#)more.c     5.28 (Berkeley) 3/1/93";
                     43: #endif /* not lint */
                     44:
                     45: /*
                     46: ** more.c - General purpose tty output filter and file perusal program
                     47: **
                     48: **     by Eric Shienbrood, UC Berkeley
                     49: **
                     50: **     modified by Geoff Peck, UCB to add underlining, single spacing
                     51: **     modified by John Foderaro, UCB to add -c and MORE environment variable
                     52: */
                     53:
                     54: #include <sys/param.h>
                     55: #include <sys/stat.h>
                     56: #include <sys/file.h>
                     57: #include <signal.h>
                     58: #include <errno.h>
                     59: #include <sgtty.h>
                     60: #include <setjmp.h>
                     61: #include <a.out.h>
                     62: #include <varargs.h>
                     63: #include <stdio.h>
                     64: #include <string.h>
                     65: #include <ctype.h>
                     66: #include "pathnames.h"
                     67:
                     68: #define Fopen(s,m)     (Currline = 0,file_pos=0,fopen(s,m))
                     69: #define Ftell(f)       file_pos
                     70: #define Fseek(f,off)   (file_pos=off,fseek(f,off,0))
                     71: #define Getc(f)                (++file_pos, getc(f))
                     72: #define Ungetc(c,f)    (--file_pos, ungetc(c,f))
                     73:
                     74: #define MBIT   CBREAK
                     75: #define stty(fd,argp)  ioctl(fd,TIOCSETN,argp)
                     76:
                     77: #define TBUFSIZ        1024
                     78: #define LINSIZ 256
                     79: #define ctrl(letter)   (letter & 077)
                     80: #define RUBOUT '\177'
                     81: #define ESC    '\033'
                     82: #define QUIT   '\034'
                     83:
                     84: struct sgttyb  otty, savetty;
                     85: long           file_pos, file_size;
                     86: int            fnum, no_intty, no_tty, slow_tty;
                     87: int            dum_opt, dlines;
                     88: void           chgwinsz(), end_it(), onquit(), onsusp();
                     89: int            nscroll = 11;   /* Number of lines scrolled by 'd' */
                     90: int            fold_opt = 1;   /* Fold long lines */
                     91: int            stop_opt = 1;   /* Stop after form feeds */
                     92: int            ssp_opt = 0;    /* Suppress white space */
                     93: int            ul_opt = 1;     /* Underline as best we can */
                     94: int            promptlen;
                     95: int            Currline;       /* Line we are currently at */
                     96: int            startup = 1;
                     97: int            firstf = 1;
                     98: int            notell = 1;
                     99: int            docrterase = 0;
                    100: int            docrtkill = 0;
                    101: int            bad_so; /* True if overwriting does not turn off standout */
                    102: int            inwait, Pause, errors;
                    103: int            within; /* true if we are within a file,
                    104:                        false if we are between files */
                    105: int            hard, dumb, noscroll, hardtabs, clreol, eatnl;
                    106: int            catch_susp;     /* We should catch the SIGTSTP signal */
                    107: char           **fnames;       /* The list of file names */
                    108: int            nfiles;         /* Number of files left to process */
                    109: char           *shell;         /* The name of the shell to use */
                    110: int            shellp;         /* A previous shell command exists */
                    111: char           ch;
                    112: jmp_buf                restore;
                    113: char           Line[LINSIZ];   /* Line buffer */
                    114: int            Lpp = 24;       /* lines per page */
                    115: char           *Clear;         /* clear screen */
                    116: char           *eraseln;       /* erase line */
                    117: char           *Senter, *Sexit;/* enter and exit standout mode */
                    118: char           *ULenter, *ULexit;      /* enter and exit underline mode */
                    119: char           *chUL;          /* underline character */
                    120: char           *chBS;          /* backspace character */
                    121: char           *Home;          /* go to home */
                    122: char           *cursorm;       /* cursor movement */
                    123: char           cursorhome[40]; /* contains cursor movement to home */
                    124: char           *EodClr;        /* clear rest of screen */
                    125: char           *tgetstr();
                    126: int            Mcol = 80;      /* number of columns */
                    127: int            Wrap = 1;       /* set if automargins */
                    128: int            soglitch;       /* terminal has standout mode glitch */
                    129: int            ulglitch;       /* terminal has underline mode glitch */
                    130: int            pstate = 0;     /* current UL state */
                    131: char           *getenv();
                    132: struct {
                    133:     long chrctr, line;
                    134: } context, screen_start;
                    135: extern char    PC;             /* pad character */
                    136: extern short   ospeed;
                    137:
                    138:
                    139: main(argc, argv)
                    140: int argc;
                    141: char *argv[];
                    142: {
                    143:     register FILE      *f;
                    144:     register char      *s;
                    145:     register char      *p;
                    146:     register char      ch;
                    147:     register int       left;
                    148:     int                        prnames = 0;
                    149:     int                        initopt = 0;
                    150:     int                        srchopt = 0;
                    151:     int                        clearit = 0;
                    152:     int                        initline;
                    153:     char               initbuf[80];
                    154:     FILE               *checkf();
                    155:
                    156:     nfiles = argc;
                    157:     fnames = argv;
                    158:     initterm ();
                    159:     nscroll = Lpp/2 - 1;
                    160:     if (nscroll <= 0)
                    161:        nscroll = 1;
                    162:     if(s = getenv("MORE")) argscan(s);
                    163:     while (--nfiles > 0) {
                    164:        if ((ch = (*++fnames)[0]) == '-') {
                    165:            argscan(*fnames+1);
                    166:        }
                    167:        else if (ch == '+') {
                    168:            s = *fnames;
                    169:            if (*++s == '/') {
                    170:                srchopt++;
                    171:                for (++s, p = initbuf; p < initbuf + 79 && *s != '\0';)
                    172:                    *p++ = *s++;
                    173:                *p = '\0';
                    174:            }
                    175:            else {
                    176:                initopt++;
                    177:                for (initline = 0; *s != '\0'; s++)
                    178:                    if (isdigit (*s))
                    179:                        initline = initline*10 + *s -'0';
                    180:                --initline;
                    181:            }
                    182:        }
                    183:        else break;
                    184:     }
                    185:     /* allow clreol only if Home and eraseln and EodClr strings are
                    186:      *  defined, and in that case, make sure we are in noscroll mode
                    187:      */
                    188:     if(clreol)
                    189:     {
                    190:         if((Home == NULL) || (*Home == '\0') ||
                    191:           (eraseln == NULL) || (*eraseln == '\0') ||
                    192:            (EodClr == NULL) || (*EodClr == '\0') )
                    193:              clreol = 0;
                    194:        else noscroll = 1;
                    195:     }
                    196:     if (dlines == 0)
1.3       etheisen  197: //     dlines = Lpp - (noscroll ? 1 : 2);
                    198:        dlines = Lpp - 1;                       /* XXX - maybe broken on dumb
                    199:                                                         terminals. */
1.1       etheisen  200:     left = dlines;
                    201:     if (nfiles > 1)
                    202:        prnames++;
                    203:     if (!no_intty && nfiles == 0) {
                    204:        char *rindex();
                    205:
                    206:        p = rindex(argv[0], '/');
                    207:        fputs("usage: ",stderr);
                    208:        fputs(p ? p + 1 : argv[0],stderr);
                    209:        fputs(" [-dfln] [+linenum | +/pattern] name1 name2 ...\n",stderr);
                    210:        exit(1);
                    211:     }
                    212:     else
                    213:        f = stdin;
                    214:     if (!no_tty) {
                    215:        signal(SIGQUIT, onquit);
                    216:        signal(SIGINT, end_it);
                    217:        signal(SIGWINCH, chgwinsz);
                    218:        if (signal (SIGTSTP, SIG_IGN) == SIG_DFL) {
                    219:            signal(SIGTSTP, onsusp);
                    220:            catch_susp++;
                    221:        }
                    222:        stty (fileno(stderr), &otty);
                    223:     }
                    224:     if (no_intty) {
                    225:        if (no_tty)
                    226:            copy_file (stdin);
                    227:        else {
                    228:            if ((ch = Getc (f)) == '\f')
                    229:                doclear();
                    230:            else {
                    231:                Ungetc (ch, f);
                    232:                if (noscroll && (ch != EOF)) {
                    233:                    if (clreol)
                    234:                        home ();
                    235:                    else
                    236:                        doclear ();
                    237:                }
                    238:            }
                    239:            if (srchopt)
                    240:            {
                    241:                search (initbuf, stdin, 1);
                    242:                if (noscroll)
                    243:                    left--;
                    244:            }
                    245:            else if (initopt)
                    246:                skiplns (initline, stdin);
                    247:            screen (stdin, left);
                    248:        }
                    249:        no_intty = 0;
                    250:        prnames++;
                    251:        firstf = 0;
                    252:     }
                    253:
                    254:     while (fnum < nfiles) {
                    255:        if ((f = checkf (fnames[fnum], &clearit)) != NULL) {
                    256:            context.line = context.chrctr = 0;
                    257:            Currline = 0;
                    258:            if (firstf) setjmp (restore);
                    259:            if (firstf) {
                    260:                firstf = 0;
                    261:                if (srchopt)
                    262:                {
                    263:                    search (initbuf, f, 1);
                    264:                    if (noscroll)
                    265:                        left--;
                    266:                }
                    267:                else if (initopt)
                    268:                    skiplns (initline, f);
                    269:            }
                    270:            else if (fnum < nfiles && !no_tty) {
                    271:                setjmp (restore);
                    272:                left = command (fnames[fnum], f);
                    273:            }
                    274:            if (left != 0) {
                    275:                if ((noscroll || clearit) && (file_size != LONG_MAX))
                    276:                    if (clreol)
                    277:                        home ();
                    278:                    else
                    279:                        doclear ();
                    280:                if (prnames) {
                    281:                    if (bad_so)
                    282:                        erase (0);
                    283:                    if (clreol)
                    284:                        cleareol ();
                    285:                    pr("::::::::::::::");
                    286:                    if (promptlen > 14)
                    287:                        erase (14);
                    288:                    prtf ("\n");
                    289:                    if(clreol) cleareol();
                    290:                    prtf("%s\n", fnames[fnum]);
                    291:                    if(clreol) cleareol();
                    292:                    prtf("::::::::::::::\n");
                    293:                    if (left > Lpp - 4)
                    294:                        left = Lpp - 4;
                    295:                }
                    296:                if (no_tty)
                    297:                    copy_file (f);
                    298:                else {
                    299:                    within++;
                    300:                    screen(f, left);
                    301:                    within = 0;
                    302:                }
                    303:            }
                    304:            setjmp (restore);
                    305:            fflush(stdout);
                    306:            fclose(f);
                    307:            screen_start.line = screen_start.chrctr = 0L;
                    308:            context.line = context.chrctr = 0L;
                    309:        }
                    310:        fnum++;
                    311:        firstf = 0;
                    312:     }
                    313:     reset_tty ();
                    314:     exit(0);
                    315: }
                    316:
                    317: argscan(s)
                    318: char *s;
                    319: {
                    320:        int seen_num = 0;
                    321:
                    322:        while (*s != '\0') {
                    323:                switch (*s) {
                    324:                  case '0': case '1': case '2':
                    325:                  case '3': case '4': case '5':
                    326:                  case '6': case '7': case '8':
                    327:                  case '9':
                    328:                        if (!seen_num) {
                    329:                                dlines = 0;
                    330:                                seen_num = 1;
                    331:                        }
                    332:                        dlines = dlines*10 + *s - '0';
                    333:                        break;
                    334:                  case 'd':
                    335:                        dum_opt = 1;
                    336:                        break;
                    337:                  case 'l':
                    338:                        stop_opt = 0;
                    339:                        break;
                    340:                  case 'f':
                    341:                        fold_opt = 0;
                    342:                        break;
                    343:                  case 'p':
                    344:                        noscroll++;
                    345:                        break;
                    346:                  case 'c':
                    347:                        clreol++;
                    348:                        break;
                    349:                  case 's':
                    350:                        ssp_opt = 1;
                    351:                        break;
                    352:                  case 'u':
                    353:                        ul_opt = 0;
                    354:                        break;
                    355:                }
                    356:                s++;
                    357:        }
                    358: }
                    359:
                    360:
                    361: /*
                    362: ** Check whether the file named by fs is an ASCII file which the user may
                    363: ** access.  If it is, return the opened file. Otherwise return NULL.
                    364: */
                    365:
                    366: FILE *
                    367: checkf (fs, clearfirst)
                    368:        register char *fs;
                    369:        int *clearfirst;
                    370: {
                    371:        struct stat stbuf;
                    372:        register FILE *f;
                    373:        char c;
                    374:
                    375:        if (stat (fs, &stbuf) == -1) {
                    376:                (void)fflush(stdout);
                    377:                if (clreol)
                    378:                        cleareol ();
                    379:                perror(fs);
                    380:                return((FILE *)NULL);
                    381:        }
                    382:        if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
                    383:                prtf("\n*** %s: directory ***\n\n", fs);
                    384:                return((FILE *)NULL);
                    385:        }
                    386:        if ((f = Fopen(fs, "r")) == NULL) {
                    387:                (void)fflush(stdout);
                    388:                perror(fs);
                    389:                return((FILE *)NULL);
                    390:        }
                    391:        if (magic(f, fs))
                    392:                return((FILE *)NULL);
                    393:        c = Getc(f);
                    394:        *clearfirst = c == '\f';
                    395:        Ungetc (c, f);
                    396:        if ((file_size = stbuf.st_size) == 0)
                    397:                file_size = LONG_MAX;
                    398:        return(f);
                    399: }
                    400:
                    401: /*
                    402:  * magic --
                    403:  *     check for file magic numbers.  This code would best be shared with
                    404:  *     the file(1) program or, perhaps, more should not try and be so smart?
                    405:  */
                    406: magic(f, fs)
                    407:        FILE *f;
                    408:        char *fs;
                    409: {
                    410:        struct exec ex;
                    411:
                    412:        if (fread(&ex, sizeof(ex), 1, f) == 1)
1.2       etheisen  413:                switch(N_GETMAGIC(ex)) {
1.1       etheisen  414:                case OMAGIC:
                    415:                case NMAGIC:
                    416:                case ZMAGIC:
                    417:                case 0405:
                    418:                case 0411:
                    419:                case 0177545:
                    420:                        prtf("\n******** %s: Not a text file ********\n\n", fs);
                    421:                        (void)fclose(f);
                    422:                        return(1);
                    423:                }
                    424:        (void)fseek(f, 0L, L_SET);              /* rewind() not necessary */
                    425:        return(0);
                    426: }
                    427:
                    428: /*
                    429: ** A real function, for the tputs routine in termlib
                    430: */
                    431:
                    432: putch (ch)
                    433: char ch;
                    434: {
                    435:     putchar (ch);
                    436: }
                    437:
                    438: /*
                    439: ** Print out the contents of the file f, one screenful at a time.
                    440: */
                    441:
                    442: #define STOP -10
                    443:
                    444: screen (f, num_lines)
                    445: register FILE *f;
                    446: register int num_lines;
                    447: {
                    448:     register int c;
                    449:     register int nchars;
                    450:     int length;                        /* length of current line */
                    451:     static int prev_len = 1;   /* length of previous line */
                    452:
                    453:     for (;;) {
                    454:        while (num_lines > 0 && !Pause) {
                    455:            if ((nchars = getline (f, &length)) == EOF)
                    456:            {
                    457:                if (clreol)
                    458:                    clreos();
                    459:                return;
                    460:            }
                    461:            if (ssp_opt && length == 0 && prev_len == 0)
                    462:                continue;
                    463:            prev_len = length;
                    464:            if (bad_so || (Senter && *Senter == ' ') && promptlen > 0)
                    465:                erase (0);
                    466:            /* must clear before drawing line since tabs on some terminals
                    467:             * do not erase what they tab over.
                    468:             */
                    469:            if (clreol)
                    470:                cleareol ();
                    471:            prbuf (Line, length);
                    472:            if (nchars < promptlen)
                    473:                erase (nchars); /* erase () sets promptlen to 0 */
                    474:            else promptlen = 0;
                    475:            /* is this needed?
                    476:             * if (clreol)
                    477:             *  cleareol();     /* must clear again in case we wrapped *
                    478:             */
                    479:            if (nchars < Mcol || !fold_opt)
                    480:                prbuf("\n", 1); /* will turn off UL if necessary */
                    481:            if (nchars == STOP)
                    482:                break;
                    483:            num_lines--;
                    484:        }
                    485:        if (pstate) {
                    486:                tputs(ULexit, 1, putch);
                    487:                pstate = 0;
                    488:        }
                    489:        fflush(stdout);
                    490:        if ((c = Getc(f)) == EOF)
                    491:        {
                    492:            if (clreol)
                    493:                clreos ();
                    494:            return;
                    495:        }
                    496:
                    497:        if (Pause && clreol)
                    498:            clreos ();
                    499:        Ungetc (c, f);
                    500:        setjmp (restore);
                    501:        Pause = 0; startup = 0;
                    502:        if ((num_lines = command (NULL, f)) == 0)
                    503:            return;
                    504:        if (hard && promptlen > 0)
                    505:                erase (0);
                    506:        if (noscroll && num_lines >= dlines)
                    507:        {
                    508:            if (clreol)
                    509:                home();
                    510:            else
                    511:                doclear ();
                    512:        }
                    513:        screen_start.line = Currline;
                    514:        screen_start.chrctr = Ftell (f);
                    515:     }
                    516: }
                    517:
                    518: /*
                    519: ** Come here if a quit signal is received
                    520: */
                    521:
                    522: void
                    523: onquit()
                    524: {
                    525:     signal(SIGQUIT, SIG_IGN);
                    526:     if (!inwait) {
                    527:        putchar ('\n');
                    528:        if (!startup) {
                    529:            signal(SIGQUIT, onquit);
                    530:            longjmp (restore, 1);
                    531:        }
                    532:        else
                    533:            Pause++;
                    534:     }
                    535:     else if (!dum_opt && notell) {
                    536:        write (2, "[Use q or Q to quit]", 20);
                    537:        promptlen += 20;
                    538:        notell = 0;
                    539:     }
                    540:     signal(SIGQUIT, onquit);
                    541: }
                    542:
                    543: /*
                    544: ** Come here if a signal for a window size change is received
                    545: */
                    546:
                    547: void
                    548: chgwinsz()
                    549: {
                    550:     struct winsize win;
                    551:
                    552:     (void) signal(SIGWINCH, SIG_IGN);
                    553:     if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) {
                    554:        if (win.ws_row != 0) {
                    555:            Lpp = win.ws_row;
                    556:            nscroll = Lpp/2 - 1;
                    557:            if (nscroll <= 0)
                    558:                nscroll = 1;
                    559:            dlines = Lpp - (noscroll ? 1 : 2);
                    560:        }
                    561:        if (win.ws_col != 0)
                    562:            Mcol = win.ws_col;
                    563:     }
                    564:     (void) signal(SIGWINCH, chgwinsz);
                    565: }
                    566:
                    567: /*
                    568: ** Clean up terminal state and exit. Also come here if interrupt signal received
                    569: */
                    570:
                    571: void
                    572: end_it ()
                    573: {
                    574:
                    575:     reset_tty ();
                    576:     if (clreol) {
                    577:        putchar ('\r');
                    578:        clreos ();
                    579:        fflush (stdout);
                    580:     }
                    581:     else if (!clreol && (promptlen > 0)) {
                    582:        kill_line ();
                    583:        fflush (stdout);
                    584:     }
                    585:     else
                    586:        write (2, "\n", 1);
                    587:     _exit(0);
                    588: }
                    589:
                    590: copy_file(f)
                    591: register FILE *f;
                    592: {
                    593:     register int c;
                    594:
                    595:     while ((c = getc(f)) != EOF)
                    596:        putchar(c);
                    597: }
                    598:
                    599: /* Simplified printf function */
                    600:
                    601: prtf (fmt, va_alist)
                    602: register char *fmt;
                    603: va_dcl
                    604: {
                    605:        va_list ap;
                    606:        register char ch;
                    607:        register int ccount;
                    608:
                    609:        ccount = 0;
                    610:        va_start(ap);
                    611:        while (*fmt) {
                    612:                while ((ch = *fmt++) != '%') {
                    613:                        if (ch == '\0')
                    614:                                return (ccount);
                    615:                        ccount++;
                    616:                        putchar (ch);
                    617:                }
                    618:                switch (*fmt++) {
                    619:                case 'd':
                    620:                        ccount += printd (va_arg(ap, int));
                    621:                        break;
                    622:                case 's':
                    623:                        ccount += pr (va_arg(ap, char *));
                    624:                        break;
                    625:                case '%':
                    626:                        ccount++;
                    627:                        putchar ('%');
                    628:                        break;
                    629:                case '0':
                    630:                        return (ccount);
                    631:                default:
                    632:                        break;
                    633:                }
                    634:        }
                    635:        va_end(ap);
                    636:        return (ccount);
                    637:
                    638: }
                    639:
                    640: /*
                    641: ** Print an integer as a string of decimal digits,
                    642: ** returning the length of the print representation.
                    643: */
                    644:
                    645: printd (n)
                    646: int n;
                    647: {
                    648:     int a, nchars;
                    649:
                    650:     if (a = n/10)
                    651:        nchars = 1 + printd(a);
                    652:     else
                    653:        nchars = 1;
                    654:     putchar (n % 10 + '0');
                    655:     return (nchars);
                    656: }
                    657:
                    658: /* Put the print representation of an integer into a string */
                    659: static char *sptr;
                    660:
                    661: scanstr (n, str)
                    662: int n;
                    663: char *str;
                    664: {
                    665:     sptr = str;
                    666:     Sprintf (n);
                    667:     *sptr = '\0';
                    668: }
                    669:
                    670: Sprintf (n)
                    671: {
                    672:     int a;
                    673:
                    674:     if (a = n/10)
                    675:        Sprintf (a);
                    676:     *sptr++ = n % 10 + '0';
                    677: }
                    678:
                    679: static char bell = ctrl('G');
                    680:
                    681: /* See whether the last component of the path name "path" is equal to the
                    682: ** string "string"
                    683: */
                    684:
                    685: tailequ (path, string)
                    686: char *path;
                    687: register char *string;
                    688: {
                    689:        register char *tail;
                    690:
                    691:        tail = path + strlen(path);
                    692:        while (tail >= path)
                    693:                if (*(--tail) == '/')
                    694:                        break;
                    695:        ++tail;
                    696:        while (*tail++ == *string++)
                    697:                if (*tail == '\0')
                    698:                        return(1);
                    699:        return(0);
                    700: }
                    701:
                    702: prompt (filename)
                    703: char *filename;
                    704: {
                    705:     if (clreol)
                    706:        cleareol ();
                    707:     else if (promptlen > 0)
                    708:        kill_line ();
                    709:     if (!hard) {
                    710:        promptlen = 8;
                    711:        if (Senter && Sexit) {
                    712:            tputs (Senter, 1, putch);
                    713:            promptlen += (2 * soglitch);
                    714:        }
                    715:        if (clreol)
                    716:            cleareol ();
1.4       etheisen  717:        /* XXX - evil global vars */
                    718:        if(fnames[fnum] != NULL) {
                    719:                 promptlen += prtf ("%s ", fnames[fnum]);
                    720:         }
                    721:         else
                    722:                 pr("--More--");
1.1       etheisen  723:        if (filename != NULL) {
                    724:            promptlen += prtf ("(Next file: %s)", filename);
                    725:        }
                    726:        else if (!no_intty) {
                    727:            promptlen += prtf ("(%d%%)", (int)((file_pos * 100) / file_size));
                    728:        }
                    729:        if (dum_opt) {
                    730:            promptlen += pr("[Press space to continue, 'q' to quit.]");
                    731:        }
                    732:        if (Senter && Sexit)
                    733:            tputs (Sexit, 1, putch);
                    734:        if (clreol)
                    735:            clreos ();
                    736:        fflush(stdout);
                    737:     }
                    738:     else
                    739:        write (2, &bell, 1);
                    740:     inwait++;
                    741: }
                    742:
                    743: /*
                    744: ** Get a logical line
                    745: */
                    746:
                    747: getline(f, length)
                    748: register FILE *f;
                    749: int *length;
                    750: {
                    751:     register int       c;
                    752:     register char      *p;
                    753:     register int       column;
                    754:     static int         colflg;
                    755:
                    756:     p = Line;
                    757:     column = 0;
                    758:     c = Getc (f);
                    759:     if (colflg && c == '\n') {
                    760:        Currline++;
                    761:        c = Getc (f);
                    762:     }
                    763:     while (p < &Line[LINSIZ - 1]) {
                    764:        if (c == EOF) {
                    765:            if (p > Line) {
                    766:                *p = '\0';
                    767:                *length = p - Line;
                    768:                return (column);
                    769:            }
                    770:            *length = p - Line;
                    771:            return (EOF);
                    772:        }
                    773:        if (c == '\n') {
                    774:            Currline++;
                    775:            break;
                    776:        }
                    777:        *p++ = c;
                    778:        if (c == '\t')
                    779:            if (!hardtabs || column < promptlen && !hard) {
                    780:                if (hardtabs && eraseln && !dumb) {
                    781:                    column = 1 + (column | 7);
                    782:                    tputs (eraseln, 1, putch);
                    783:                    promptlen = 0;
                    784:                }
                    785:                else {
                    786:                    for (--p; p < &Line[LINSIZ - 1];) {
                    787:                        *p++ = ' ';
                    788:                        if ((++column & 7) == 0)
                    789:                            break;
                    790:                    }
                    791:                    if (column >= promptlen) promptlen = 0;
                    792:                }
                    793:            }
                    794:            else
                    795:                column = 1 + (column | 7);
                    796:        else if (c == '\b' && column > 0)
                    797:            column--;
                    798:        else if (c == '\r')
                    799:            column = 0;
                    800:        else if (c == '\f' && stop_opt) {
                    801:                p[-1] = '^';
                    802:                *p++ = 'L';
                    803:                column += 2;
                    804:                Pause++;
                    805:        }
                    806:        else if (c == EOF) {
                    807:            *length = p - Line;
                    808:            return (column);
                    809:        }
                    810:        else if (c >= ' ' && c != RUBOUT)
                    811:            column++;
                    812:        if (column >= Mcol && fold_opt) break;
                    813:        c = Getc (f);
                    814:     }
                    815:     if (column >= Mcol && Mcol > 0) {
                    816:        if (!Wrap) {
                    817:            *p++ = '\n';
                    818:        }
                    819:     }
                    820:     colflg = column == Mcol && fold_opt;
                    821:     if (colflg && eatnl && Wrap) {
                    822:        *p++ = '\n'; /* simulate normal wrap */
                    823:     }
                    824:     *length = p - Line;
                    825:     *p = 0;
                    826:     return (column);
                    827: }
                    828:
                    829: /*
                    830: ** Erase the rest of the prompt, assuming we are starting at column col.
                    831: */
                    832:
                    833: erase (col)
                    834: register int col;
                    835: {
                    836:
                    837:     if (promptlen == 0)
                    838:        return;
                    839:     if (hard) {
                    840:        putchar ('\n');
                    841:     }
                    842:     else {
                    843:        if (col == 0)
                    844:            putchar ('\r');
                    845:        if (!dumb && eraseln)
                    846:            tputs (eraseln, 1, putch);
                    847:        else
                    848:            for (col = promptlen - col; col > 0; col--)
                    849:                putchar (' ');
                    850:     }
                    851:     promptlen = 0;
                    852: }
                    853:
                    854: /*
                    855: ** Erase the current line entirely
                    856: */
                    857:
                    858: kill_line ()
                    859: {
                    860:     erase (0);
                    861:     if (!eraseln || dumb) putchar ('\r');
                    862: }
                    863:
                    864: /*
                    865:  * force clear to end of line
                    866:  */
                    867: cleareol()
                    868: {
                    869:     tputs(eraseln, 1, putch);
                    870: }
                    871:
                    872: clreos()
                    873: {
                    874:     tputs(EodClr, 1, putch);
                    875: }
                    876:
                    877: /*
                    878: **  Print string and return number of characters
                    879: */
                    880:
                    881: pr(s1)
                    882: char   *s1;
                    883: {
                    884:     register char      *s;
                    885:     register char      c;
                    886:
                    887:     for (s = s1; c = *s++; )
                    888:        putchar(c);
                    889:     return (s - s1 - 1);
                    890: }
                    891:
                    892:
                    893: /* Print a buffer of n characters */
                    894:
                    895: prbuf (s, n)
                    896: register char *s;
                    897: register int n;
                    898: {
                    899:     register char c;                   /* next output character */
                    900:     register int state;                        /* next output char's UL state */
                    901: #define wouldul(s,n)   ((n) >= 2 && (((s)[0] == '_' && (s)[1] == '\b') || ((s)[1] == '\b' && (s)[2] == '_')))
                    902:
                    903:     while (--n >= 0)
                    904:        if (!ul_opt)
                    905:            putchar (*s++);
                    906:        else {
                    907:            if (*s == ' ' && pstate == 0 && ulglitch && wouldul(s+1, n-1)) {
                    908:                s++;
                    909:                continue;
                    910:            }
                    911:            if (state = wouldul(s, n)) {
                    912:                c = (*s == '_')? s[2] : *s ;
                    913:                n -= 2;
                    914:                s += 3;
                    915:            } else
                    916:                c = *s++;
                    917:            if (state != pstate) {
                    918:                if (c == ' ' && state == 0 && ulglitch && wouldul(s, n-1))
                    919:                    state = 1;
                    920:                else
                    921:                    tputs(state ? ULenter : ULexit, 1, putch);
                    922:            }
                    923:            if (c != ' ' || pstate == 0 || state != 0 || ulglitch == 0)
                    924:                putchar(c);
                    925:            if (state && *chUL) {
                    926:                pr(chBS);
                    927:                tputs(chUL, 1, putch);
                    928:            }
                    929:            pstate = state;
                    930:        }
                    931: }
                    932:
                    933: /*
                    934: **  Clear the screen
                    935: */
                    936:
                    937: doclear()
                    938: {
                    939:     if (Clear && !hard) {
                    940:        tputs(Clear, 1, putch);
                    941:
                    942:        /* Put out carriage return so that system doesn't
                    943:        ** get confused by escape sequences when expanding tabs
                    944:        */
                    945:        putchar ('\r');
                    946:        promptlen = 0;
                    947:     }
                    948: }
                    949:
                    950: /*
                    951:  * Go to home position
                    952:  */
                    953: home()
                    954: {
                    955:     tputs(Home,1,putch);
                    956: }
                    957:
                    958: static int lastcmd, lastarg, lastp;
                    959: static int lastcolon;
                    960: char shell_line[132];
                    961:
                    962: /*
                    963: ** Read a command and do it. A command consists of an optional integer
                    964: ** argument followed by the command character.  Return the number of lines
                    965: ** to display in the next screenful.  If there is nothing more to display
                    966: ** in the current file, zero is returned.
                    967: */
                    968:
                    969: command (filename, f)
                    970: char *filename;
                    971: register FILE *f;
                    972: {
                    973:     register int nlines;
                    974:     register int retval;
                    975:     register char c;
                    976:     char colonch;
                    977:     FILE *helpf;
                    978:     int done;
                    979:     char comchar, cmdbuf[80], *p;
1.5       etheisen  980:     char option[8];
                    981:     char *EDITOR;
                    982:     char *editor;
1.1       etheisen  983:
                    984: #define ret(val) retval=val;done++;break
                    985:
                    986:     done = 0;
                    987:     if (!errors)
                    988:        prompt (filename);
                    989:     else
                    990:        errors = 0;
                    991:     if (MBIT == RAW && slow_tty) {
                    992:        otty.sg_flags |= MBIT;
                    993:        stty(fileno(stderr), &otty);
                    994:     }
                    995:     for (;;) {
                    996:        nlines = number (&comchar);
                    997:        lastp = colonch = 0;
                    998:        if (comchar == '.') {   /* Repeat last command */
                    999:                lastp++;
                   1000:                comchar = lastcmd;
                   1001:                nlines = lastarg;
                   1002:                if (lastcmd == ':')
                   1003:                        colonch = lastcolon;
                   1004:        }
                   1005:        lastcmd = comchar;
                   1006:        lastarg = nlines;
                   1007:        if (comchar == otty.sg_erase) {
                   1008:            kill_line ();
                   1009:            prompt (filename);
                   1010:            continue;
                   1011:        }
                   1012:        switch (comchar) {
                   1013:        case ':':
                   1014:            retval = colon (filename, colonch, nlines);
                   1015:            if (retval >= 0)
                   1016:                done++;
                   1017:            break;
                   1018:        case 'b':
                   1019:        case ctrl('B'):
                   1020:            {
                   1021:                register int initline;
                   1022:
                   1023:                if (no_intty) {
                   1024:                    write(2, &bell, 1);
                   1025:                    return (-1);
                   1026:                }
                   1027:
                   1028:                if (nlines == 0) nlines++;
                   1029:
                   1030:                putchar ('\r');
                   1031:                erase (0);
                   1032:                prtf ("\n");
                   1033:                if (clreol)
                   1034:                        cleareol ();
                   1035:                prtf ("...back %d page", nlines);
                   1036:                if (nlines > 1)
                   1037:                        pr ("s\n");
                   1038:                else
                   1039:                        pr ("\n");
                   1040:
                   1041:                if (clreol)
                   1042:                        cleareol ();
                   1043:                pr ("\n");
                   1044:
                   1045:                initline = Currline - dlines * (nlines + 1);
                   1046:                if (! noscroll)
                   1047:                    --initline;
                   1048:                if (initline < 0) initline = 0;
                   1049:                Fseek(f, 0L);
                   1050:                Currline = 0;   /* skiplns() will make Currline correct */
                   1051:                skiplns(initline, f);
1.3       etheisen 1052:                /* if (! noscroll) {
1.1       etheisen 1053:                    ret(dlines + 1);
1.3       etheisen 1054:                 }
                   1055:                 else {
1.1       etheisen 1056:                    ret(dlines);
1.3       etheisen 1057:                 } */
                   1058:                ret(dlines);                    /* XXX - Maybe broken on dumb
                   1059:                                                         terminals */
1.1       etheisen 1060:            }
1.6     ! etheisen 1061:        /* 4.3BSD more - Display next [count] lines of text  */
1.1       etheisen 1062:        case ' ':
                   1063:        case 'z':
                   1064:            if (nlines == 0) nlines = dlines;
1.6     ! etheisen 1065:            else if (comchar == 'z')
        !          1066:                dlines = nlines;
1.1       etheisen 1067:            ret (nlines);
                   1068:        case 'd':
                   1069:        case ctrl('D'):
                   1070:            if (nlines != 0) nscroll = nlines;
                   1071:            ret (nscroll);
                   1072:        case 'q':
                   1073:        case 'Q':
                   1074:            end_it ();
                   1075:        case 's':
1.6     ! etheisen 1076:        /* POSIX.2 Move forward one screenfull */
        !          1077:        case 'f':                               /* POSIX.2 [count]f */
        !          1078:        case ctrl('F'):                         /*         [count]control-F */
1.1       etheisen 1079:            if (nlines == 0) nlines++;
                   1080:            if (comchar == 'f')
                   1081:                nlines *= dlines;
                   1082:            putchar ('\r');
                   1083:            erase (0);
                   1084:            prtf ("\n");
                   1085:            if (clreol)
                   1086:                cleareol ();
                   1087:            prtf ("...skipping %d line", nlines);
                   1088:            if (nlines > 1)
                   1089:                pr ("s\n");
                   1090:            else
                   1091:                pr ("\n");
                   1092:
                   1093:            if (clreol)
                   1094:                cleareol ();
                   1095:            pr ("\n");
                   1096:
                   1097:            while (nlines > 0) {
                   1098:                while ((c = Getc (f)) != '\n')
                   1099:                    if (c == EOF) {
                   1100:                        retval = 0;
                   1101:                        done++;
                   1102:                        goto endsw;
                   1103:                    }
                   1104:                    Currline++;
                   1105:                    nlines--;
                   1106:            }
                   1107:            ret (dlines);
                   1108:        case '\n':
                   1109:            if (nlines != 0)
                   1110:                dlines = nlines;
                   1111:            else
                   1112:                nlines = 1;
                   1113:            ret (nlines);
                   1114:        case '\f':
                   1115:            if (!no_intty) {
                   1116:                doclear ();
                   1117:                Fseek (f, screen_start.chrctr);
                   1118:                Currline = screen_start.line;
                   1119:                ret (dlines);
                   1120:            }
                   1121:            else {
                   1122:                write (2, &bell, 1);
                   1123:                break;
                   1124:            }
                   1125:        case '\'':
                   1126:            if (!no_intty) {
                   1127:                kill_line ();
                   1128:                pr ("\n***Back***\n\n");
                   1129:                Fseek (f, context.chrctr);
                   1130:                Currline = context.line;
                   1131:                ret (dlines);
                   1132:            }
                   1133:            else {
                   1134:                write (2, &bell, 1);
                   1135:                break;
                   1136:            }
                   1137:        case '=':
                   1138:            kill_line ();
                   1139:            promptlen = printd (Currline);
                   1140:            fflush (stdout);
                   1141:            break;
                   1142:        case 'n':
                   1143:            lastp++;
                   1144:        case '/':
                   1145:            if (nlines == 0) nlines++;
                   1146:            kill_line ();
                   1147:            pr ("/");
                   1148:            promptlen = 1;
                   1149:            fflush (stdout);
                   1150:            if (lastp) {
                   1151:                write (2,"\r", 1);
                   1152:                search (NULL, f, nlines);       /* Use previous r.e. */
                   1153:            }
                   1154:            else {
                   1155:                ttyin (cmdbuf, 78, '/');
                   1156:                write (2, "\r", 1);
                   1157:                search (cmdbuf, f, nlines);
                   1158:            }
                   1159:            ret (dlines-1);
                   1160:        case '!':
                   1161:            do_shell (filename);
                   1162:            break;
                   1163:        case '?':
                   1164:        case 'h':
                   1165:            if ((helpf = fopen (HELPFILE, "r")) == NULL)
                   1166:                error ("Can't open help file");
                   1167:            if (noscroll) doclear ();
                   1168:            copy_file (helpf);
                   1169:            fclose (helpf);
                   1170:            prompt (filename);
                   1171:            break;
1.5       etheisen 1172:        /* Run Editor */
1.1       etheisen 1173:        case 'v':       /* This case should go right before default */
                   1174:            if (!no_intty) {
                   1175:                kill_line ();
1.5       etheisen 1176:                strcpy(cmdbuf, "-c");
1.1       etheisen 1177:                scanstr (Currline - dlines < 0 ? 0
1.5       etheisen 1178:                                : Currline - (dlines + 1) / 2, option);
                   1179:
                   1180:                /* POSIX.2 more EDITOR env. var. behavior */
                   1181:                if ((EDITOR = getenv("EDITOR")) != NULL) {
                   1182:
                   1183:                    /* Need to look for vi or ex as
                   1184:                     * we need to tell them the current
                   1185:                     * line number
                   1186:                     */
                   1187:                    if ((editor = strrchr(EDITOR, '/')) == NULL)
                   1188:                        editor = EDITOR;
                   1189:                    else
                   1190:                        editor++;
                   1191:                    if ((strcmp(editor, "vi") == 0) ||
                   1192:                        (strcmp(editor, "ex") == 0))
                   1193:                        execute (filename, EDITOR, EDITOR, cmdbuf, option, fnames[fnum], 0);
                   1194:                    else /* must be some other editor */
                   1195:                        execute (filename, EDITOR, EDITOR, fnames[fnum], 0);
                   1196:                }
                   1197:                else { /* default editor */
                   1198:                        execute (filename, _PATH_VI, _PATH_VI, cmdbuf, option, fnames[fnum], 0);
                   1199:                }
                   1200:
1.1       etheisen 1201:                break;
                   1202:            }
                   1203:        default:
                   1204:            if (dum_opt) {
                   1205:                kill_line ();
                   1206:                if (Senter && Sexit) {
                   1207:                    tputs (Senter, 1, putch);
                   1208:                    promptlen = pr ("[Press 'h' for instructions.]") + (2 * soglitch);
                   1209:                    tputs (Sexit, 1, putch);
                   1210:                }
                   1211:                else
                   1212:                    promptlen = pr ("[Press 'h' for instructions.]");
                   1213:                fflush (stdout);
                   1214:            }
                   1215:            else
                   1216:                write (2, &bell, 1);
                   1217:            break;
                   1218:        }
                   1219:        if (done) break;
                   1220:     }
                   1221:     putchar ('\r');
                   1222: endsw:
                   1223:     inwait = 0;
                   1224:     notell++;
                   1225:     if (MBIT == RAW && slow_tty) {
                   1226:        otty.sg_flags &= ~MBIT;
                   1227:        stty(fileno(stderr), &otty);
                   1228:     }
                   1229:     return (retval);
                   1230: }
                   1231:
                   1232: char ch;
                   1233:
                   1234: /*
                   1235:  * Execute a colon-prefixed command.
                   1236:  * Returns <0 if not a command that should cause
                   1237:  * more of the file to be printed.
                   1238:  */
                   1239:
                   1240: colon (filename, cmd, nlines)
                   1241: char *filename;
                   1242: int cmd;
                   1243: int nlines;
                   1244: {
                   1245:        if (cmd == 0)
                   1246:                ch = readch ();
                   1247:        else
                   1248:                ch = cmd;
                   1249:        lastcolon = ch;
                   1250:        switch (ch) {
                   1251:        case 'f':
                   1252:                kill_line ();
                   1253:                if (!no_intty)
                   1254:                        promptlen = prtf ("\"%s\" line %d", fnames[fnum], Currline);
                   1255:                else
                   1256:                        promptlen = prtf ("[Not a file] line %d", Currline);
                   1257:                fflush (stdout);
                   1258:                return (-1);
                   1259:        case 'n':
                   1260:                if (nlines == 0) {
                   1261:                        if (fnum >= nfiles - 1)
                   1262:                                end_it ();
                   1263:                        nlines++;
                   1264:                }
                   1265:                putchar ('\r');
                   1266:                erase (0);
                   1267:                skipf (nlines);
                   1268:                return (0);
                   1269:        case 'p':
                   1270:                if (no_intty) {
                   1271:                        write (2, &bell, 1);
                   1272:                        return (-1);
                   1273:                }
                   1274:                putchar ('\r');
                   1275:                erase (0);
                   1276:                if (nlines == 0)
                   1277:                        nlines++;
                   1278:                skipf (-nlines);
                   1279:                return (0);
                   1280:        case '!':
                   1281:                do_shell (filename);
                   1282:                return (-1);
                   1283:        case 'q':
                   1284:        case 'Q':
                   1285:                end_it ();
                   1286:        default:
                   1287:                write (2, &bell, 1);
                   1288:                return (-1);
                   1289:        }
                   1290: }
                   1291:
                   1292: /*
                   1293: ** Read a decimal number from the terminal. Set cmd to the non-digit which
                   1294: ** terminates the number.
                   1295: */
                   1296:
                   1297: number(cmd)
                   1298: char *cmd;
                   1299: {
                   1300:        register int i;
                   1301:
                   1302:        i = 0; ch = otty.sg_kill;
                   1303:        for (;;) {
                   1304:                ch = readch ();
                   1305:                if (ch >= '0' && ch <= '9')
                   1306:                        i = i*10 + ch - '0';
                   1307:                else if (ch == otty.sg_kill)
                   1308:                        i = 0;
                   1309:                else {
                   1310:                        *cmd = ch;
                   1311:                        break;
                   1312:                }
                   1313:        }
                   1314:        return (i);
                   1315: }
                   1316:
                   1317: do_shell (filename)
                   1318: char *filename;
                   1319: {
                   1320:        char cmdbuf[80];
                   1321:
                   1322:        kill_line ();
                   1323:        pr ("!");
                   1324:        fflush (stdout);
                   1325:        promptlen = 1;
                   1326:        if (lastp)
                   1327:                pr (shell_line);
                   1328:        else {
                   1329:                ttyin (cmdbuf, 78, '!');
                   1330:                if (expand (shell_line, cmdbuf)) {
                   1331:                        kill_line ();
                   1332:                        promptlen = prtf ("!%s", shell_line);
                   1333:                }
                   1334:        }
                   1335:        fflush (stdout);
                   1336:        write (2, "\n", 1);
                   1337:        promptlen = 0;
                   1338:        shellp = 1;
                   1339:        execute (filename, shell, shell, "-c", shell_line, 0);
                   1340: }
                   1341:
                   1342: /*
                   1343: ** Search for nth ocurrence of regular expression contained in buf in the file
                   1344: */
                   1345:
                   1346: search (buf, file, n)
                   1347: char buf[];
                   1348: FILE *file;
                   1349: register int n;
                   1350: {
                   1351:     long startline = Ftell (file);
                   1352:     register long line1 = startline;
                   1353:     register long line2 = startline;
                   1354:     register long line3 = startline;
                   1355:     register int lncount;
                   1356:     int saveln, rv, re_exec();
                   1357:     char *s, *re_comp();
                   1358:
                   1359:     context.line = saveln = Currline;
                   1360:     context.chrctr = startline;
                   1361:     lncount = 0;
                   1362:     if ((s = re_comp (buf)) != 0)
                   1363:        error (s);
                   1364:     while (!feof (file)) {
                   1365:        line3 = line2;
                   1366:        line2 = line1;
                   1367:        line1 = Ftell (file);
                   1368:        rdline (file);
                   1369:        lncount++;
                   1370:        if ((rv = re_exec (Line)) == 1)
                   1371:                if (--n == 0) {
                   1372:                    if (lncount > 3 || (lncount > 1 && no_intty))
                   1373:                    {
                   1374:                        pr ("\n");
                   1375:                        if (clreol)
                   1376:                            cleareol ();
                   1377:                        pr("...skipping\n");
                   1378:                    }
                   1379:                    if (!no_intty) {
                   1380:                        Currline -= (lncount >= 3 ? 3 : lncount);
                   1381:                        Fseek (file, line3);
                   1382:                        if (noscroll)
                   1383:                            if (clreol) {
                   1384:                                home ();
                   1385:                                cleareol ();
                   1386:                            }
                   1387:                            else
                   1388:                                doclear ();
                   1389:                    }
                   1390:                    else {
                   1391:                        kill_line ();
                   1392:                        if (noscroll)
                   1393:                            if (clreol) {
                   1394:                                home ();
                   1395:                                cleareol ();
                   1396:                            }
                   1397:                            else
                   1398:                                doclear ();
                   1399:                        pr (Line);
                   1400:                        putchar ('\n');
                   1401:                    }
                   1402:                    break;
                   1403:                }
                   1404:        else if (rv == -1)
                   1405:            error ("Regular expression botch");
                   1406:     }
                   1407:     if (feof (file)) {
                   1408:        if (!no_intty) {
                   1409:        /*    file->_flag &= ~_IOEOF; /* why doesn't fseek do this ??!!??! */
                   1410:            Currline = saveln;
                   1411:            Fseek (file, startline);
                   1412:        }
                   1413:        else {
                   1414:            pr ("\nPattern not found\n");
                   1415:            end_it ();
                   1416:        }
                   1417:        error ("Pattern not found");
                   1418:     }
                   1419: }
                   1420:
                   1421: /*VARARGS2*/
                   1422: execute (filename, cmd, va_alist)
                   1423: char *filename;
                   1424: char *cmd;
                   1425: va_dcl
                   1426: {
                   1427:        int id;
                   1428:        int n;
                   1429:        va_list argp;
                   1430:
                   1431:        fflush (stdout);
                   1432:        reset_tty ();
                   1433:        for (n = 10; (id = fork ()) < 0 && n > 0; n--)
                   1434:            sleep (5);
                   1435:        if (id == 0) {
                   1436:            if (!isatty(0)) {
                   1437:                close(0);
                   1438:                open("/dev/tty", 0);
                   1439:            }
                   1440:            va_start(argp);
1.5       etheisen 1441:            execvp (cmd, argp);
1.1       etheisen 1442:            write (2, "exec failed\n", 12);
                   1443:            exit (1);
                   1444:            va_end(argp);       /* balance {}'s for some UNIX's */
                   1445:        }
                   1446:        if (id > 0) {
                   1447:            signal (SIGINT, SIG_IGN);
                   1448:            signal (SIGQUIT, SIG_IGN);
                   1449:            if (catch_susp)
                   1450:                signal(SIGTSTP, SIG_DFL);
                   1451:            while (wait(0) > 0);
                   1452:            signal (SIGINT, end_it);
                   1453:            signal (SIGQUIT, onquit);
                   1454:            if (catch_susp)
                   1455:                signal(SIGTSTP, onsusp);
                   1456:        } else
                   1457:            write(2, "can't fork\n", 11);
                   1458:        set_tty ();
1.5       etheisen 1459:        /* pr ("------------------------\n"); */
1.1       etheisen 1460:        prompt (filename);
                   1461: }
                   1462: /*
                   1463: ** Skip n lines in the file f
                   1464: */
                   1465:
                   1466: skiplns (n, f)
                   1467: register int n;
                   1468: register FILE *f;
                   1469: {
                   1470:     register char c;
                   1471:
                   1472:     while (n > 0) {
                   1473:        while ((c = Getc (f)) != '\n')
                   1474:            if (c == EOF)
                   1475:                return;
                   1476:            n--;
                   1477:            Currline++;
                   1478:     }
                   1479: }
                   1480:
                   1481: /*
                   1482: ** Skip nskip files in the file list (from the command line). Nskip may be
                   1483: ** negative.
                   1484: */
                   1485:
                   1486: skipf (nskip)
                   1487: register int nskip;
                   1488: {
                   1489:     if (nskip == 0) return;
                   1490:     if (nskip > 0) {
                   1491:        if (fnum + nskip > nfiles - 1)
                   1492:            nskip = nfiles - fnum - 1;
                   1493:     }
                   1494:     else if (within)
                   1495:        ++fnum;
                   1496:     fnum += nskip;
                   1497:     if (fnum < 0)
                   1498:        fnum = 0;
                   1499:     pr ("\n...Skipping ");
                   1500:     pr ("\n");
                   1501:     if (clreol)
                   1502:        cleareol ();
                   1503:     pr ("...Skipping ");
                   1504:     pr (nskip > 0 ? "to file " : "back to file ");
                   1505:     pr (fnames[fnum]);
                   1506:     pr ("\n");
                   1507:     if (clreol)
                   1508:        cleareol ();
                   1509:     pr ("\n");
                   1510:     --fnum;
                   1511: }
                   1512:
                   1513: /*----------------------------- Terminal I/O -------------------------------*/
                   1514:
                   1515: initterm ()
                   1516: {
                   1517:     char       buf[TBUFSIZ];
                   1518:     static char        clearbuf[TBUFSIZ];
                   1519:     char       *clearptr, *padstr;
                   1520:     int                ldisc;
                   1521:     int                lmode;
                   1522:     char       *term;
                   1523:     int                tgrp;
                   1524:     struct winsize win;
                   1525:     char       *tgoto();
                   1526:
                   1527: retry:
                   1528:     if (!(no_tty = ioctl(fileno(stdout), TIOCGETP, &otty))) {
                   1529:        if (ioctl(fileno(stdout), TIOCLGET, &lmode) < 0) {
                   1530:            perror("TIOCLGET");
                   1531:            exit(1);
                   1532:        }
                   1533:        docrterase = ((lmode & LCRTERA) != 0);
                   1534:        docrtkill = ((lmode & LCRTKIL) != 0);
                   1535:        /*
                   1536:         * Wait until we're in the foreground before we save the
                   1537:         * the terminal modes.
                   1538:         */
                   1539:        if (ioctl(fileno(stdout), TIOCGPGRP, &tgrp) < 0) {
                   1540:            perror("TIOCGPGRP");
                   1541:            exit(1);
                   1542:        }
                   1543:        if (tgrp != getpgrp(0)) {
                   1544:            kill(0, SIGTTOU);
                   1545:            goto retry;
                   1546:        }
                   1547:        if ((term = getenv("TERM")) == 0 || tgetent(buf, term) <= 0) {
                   1548:            dumb++; ul_opt = 0;
                   1549:        }
                   1550:        else {
                   1551:            if (ioctl(fileno(stdout), TIOCGWINSZ, &win) < 0) {
                   1552:                Lpp = tgetnum("li");
                   1553:                Mcol = tgetnum("co");
                   1554:            } else {
                   1555:                if ((Lpp = win.ws_row) == 0)
                   1556:                    Lpp = tgetnum("li");
                   1557:                if ((Mcol = win.ws_col) == 0)
                   1558:                    Mcol = tgetnum("co");
                   1559:            }
                   1560:            if ((Lpp <= 0) || tgetflag("hc")) {
                   1561:                hard++; /* Hard copy terminal */
                   1562:                Lpp = 24;
                   1563:            }
                   1564:            if (tgetflag("xn"))
                   1565:                eatnl++; /* Eat newline at last column + 1; dec, concept */
                   1566:            if (Mcol <= 0)
                   1567:                Mcol = 80;
                   1568:
                   1569:            if (tailequ (fnames[0], "page") || !hard && tgetflag("ns"))
                   1570:                noscroll++;
                   1571:            Wrap = tgetflag("am");
                   1572:            bad_so = tgetflag ("xs");
                   1573:            clearptr = clearbuf;
                   1574:            eraseln = tgetstr("ce",&clearptr);
                   1575:            Clear = tgetstr("cl", &clearptr);
                   1576:            Senter = tgetstr("so", &clearptr);
                   1577:            Sexit = tgetstr("se", &clearptr);
                   1578:            if ((soglitch = tgetnum("sg")) < 0)
                   1579:                soglitch = 0;
                   1580:
                   1581:            /*
                   1582:             *  Set up for underlining:  some terminals don't need it;
                   1583:             *  others have start/stop sequences, still others have an
                   1584:             *  underline char sequence which is assumed to move the
                   1585:             *  cursor forward one character.  If underline sequence
                   1586:             *  isn't available, settle for standout sequence.
                   1587:             */
                   1588:
                   1589:            if (tgetflag("ul") || tgetflag("os"))
                   1590:                ul_opt = 0;
                   1591:            if ((chUL = tgetstr("uc", &clearptr)) == NULL )
                   1592:                chUL = "";
                   1593:            if (((ULenter = tgetstr("us", &clearptr)) == NULL ||
                   1594:                 (ULexit = tgetstr("ue", &clearptr)) == NULL) && !*chUL) {
                   1595:                if ((ULenter = Senter) == NULL || (ULexit = Sexit) == NULL) {
                   1596:                        ULenter = "";
                   1597:                        ULexit = "";
                   1598:                } else
                   1599:                        ulglitch = soglitch;
                   1600:            } else {
                   1601:                if ((ulglitch = tgetnum("ug")) < 0)
                   1602:                    ulglitch = 0;
                   1603:            }
                   1604:
                   1605:            if (padstr = tgetstr("pc", &clearptr))
                   1606:                PC = *padstr;
                   1607:            Home = tgetstr("ho",&clearptr);
                   1608:            if (Home == 0 || *Home == '\0')
                   1609:            {
                   1610:                if ((cursorm = tgetstr("cm", &clearptr)) != NULL) {
                   1611:                    strcpy(cursorhome, tgoto(cursorm, 0, 0));
                   1612:                    Home = cursorhome;
                   1613:               }
                   1614:            }
                   1615:            EodClr = tgetstr("cd", &clearptr);
                   1616:            if ((chBS = tgetstr("bc", &clearptr)) == NULL)
                   1617:                chBS = "\b";
                   1618:
                   1619:        }
                   1620:        if ((shell = getenv("SHELL")) == NULL)
                   1621:            shell = "/bin/sh";
                   1622:     }
                   1623:     no_intty = ioctl(fileno(stdin), TIOCGETP, &otty);
                   1624:     (void)ioctl(fileno(stderr), TIOCGETP, &otty);
                   1625:     savetty = otty;
                   1626:     ospeed = otty.sg_ospeed;
                   1627:     slow_tty = ospeed < B1200;
                   1628:     hardtabs = (otty.sg_flags & TBDELAY) != XTABS;
                   1629:     if (!no_tty) {
                   1630:        otty.sg_flags &= ~ECHO;
                   1631:        if (MBIT == CBREAK || !slow_tty)
                   1632:            otty.sg_flags |= MBIT;
                   1633:     }
                   1634: }
                   1635:
                   1636: readch ()
                   1637: {
                   1638:        char ch;
                   1639:        extern int errno;
                   1640:
                   1641:        errno = 0;
                   1642:        if (read (2, &ch, 1) <= 0)
                   1643:                if (errno != EINTR)
                   1644:                        end_it();
                   1645:                else
                   1646:                        ch = otty.sg_kill;
                   1647:        return (ch);
                   1648: }
                   1649:
                   1650: static char BS = '\b';
                   1651: static char *BSB = "\b \b";
                   1652: static char CARAT = '^';
                   1653: #define ERASEONECHAR \
                   1654:     if (docrterase) \
                   1655:        write (2, BSB, sizeof(BSB)); \
                   1656:     else \
                   1657:        write (2, &BS, sizeof(BS));
                   1658:
                   1659: ttyin (buf, nmax, pchar)
                   1660: char buf[];
                   1661: register int nmax;
                   1662: char pchar;
                   1663: {
                   1664:     register char *sptr;
                   1665:     register char ch;
                   1666:     register int slash = 0;
                   1667:     int        maxlen;
                   1668:     char cbuf;
                   1669:
                   1670:     sptr = buf;
                   1671:     maxlen = 0;
                   1672:     while (sptr - buf < nmax) {
                   1673:        if (promptlen > maxlen) maxlen = promptlen;
                   1674:        ch = readch ();
                   1675:        if (ch == '\\') {
                   1676:            slash++;
                   1677:        }
                   1678:        else if ((ch == otty.sg_erase) && !slash) {
                   1679:            if (sptr > buf) {
                   1680:                --promptlen;
                   1681:                ERASEONECHAR
                   1682:                --sptr;
                   1683:                if ((*sptr < ' ' && *sptr != '\n') || *sptr == RUBOUT) {
                   1684:                    --promptlen;
                   1685:                    ERASEONECHAR
                   1686:                }
                   1687:                continue;
                   1688:            }
                   1689:            else {
                   1690:                if (!eraseln) promptlen = maxlen;
                   1691:                longjmp (restore, 1);
                   1692:            }
                   1693:        }
                   1694:        else if ((ch == otty.sg_kill) && !slash) {
                   1695:            if (hard) {
                   1696:                show (ch);
                   1697:                putchar ('\n');
                   1698:                putchar (pchar);
                   1699:            }
                   1700:            else {
                   1701:                putchar ('\r');
                   1702:                putchar (pchar);
                   1703:                if (eraseln)
                   1704:                    erase (1);
                   1705:                else if (docrtkill)
                   1706:                    while (promptlen-- > 1)
                   1707:                        write (2, BSB, sizeof(BSB));
                   1708:                promptlen = 1;
                   1709:            }
                   1710:            sptr = buf;
                   1711:            fflush (stdout);
                   1712:            continue;
                   1713:        }
                   1714:        if (slash && (ch == otty.sg_kill || ch == otty.sg_erase)) {
                   1715:            ERASEONECHAR
                   1716:            --sptr;
                   1717:        }
                   1718:        if (ch != '\\')
                   1719:            slash = 0;
                   1720:        *sptr++ = ch;
                   1721:        if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) {
                   1722:            ch += ch == RUBOUT ? -0100 : 0100;
                   1723:            write (2, &CARAT, 1);
                   1724:            promptlen++;
                   1725:        }
                   1726:        cbuf = ch;
                   1727:        if (ch != '\n' && ch != ESC) {
                   1728:            write (2, &cbuf, 1);
                   1729:            promptlen++;
                   1730:        }
                   1731:        else
                   1732:            break;
                   1733:     }
                   1734:     *--sptr = '\0';
                   1735:     if (!eraseln) promptlen = maxlen;
                   1736:     if (sptr - buf >= nmax - 1)
                   1737:        error ("Line too long");
                   1738: }
                   1739:
                   1740: expand (outbuf, inbuf)
                   1741: char *outbuf;
                   1742: char *inbuf;
                   1743: {
                   1744:     register char *instr;
                   1745:     register char *outstr;
                   1746:     register char ch;
                   1747:     char temp[200];
                   1748:     int changed = 0;
                   1749:
                   1750:     instr = inbuf;
                   1751:     outstr = temp;
                   1752:     while ((ch = *instr++) != '\0')
                   1753:        switch (ch) {
                   1754:        case '%':
                   1755:            if (!no_intty) {
                   1756:                strcpy (outstr, fnames[fnum]);
                   1757:                outstr += strlen (fnames[fnum]);
                   1758:                changed++;
                   1759:            }
                   1760:            else
                   1761:                *outstr++ = ch;
                   1762:            break;
                   1763:        case '!':
                   1764:            if (!shellp)
                   1765:                error ("No previous command to substitute for");
                   1766:            strcpy (outstr, shell_line);
                   1767:            outstr += strlen (shell_line);
                   1768:            changed++;
                   1769:            break;
                   1770:        case '\\':
                   1771:            if (*instr == '%' || *instr == '!') {
                   1772:                *outstr++ = *instr++;
                   1773:                break;
                   1774:            }
                   1775:        default:
                   1776:            *outstr++ = ch;
                   1777:        }
                   1778:     *outstr++ = '\0';
                   1779:     strcpy (outbuf, temp);
                   1780:     return (changed);
                   1781: }
                   1782:
                   1783: show (ch)
                   1784: register char ch;
                   1785: {
                   1786:     char cbuf;
                   1787:
                   1788:     if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) {
                   1789:        ch += ch == RUBOUT ? -0100 : 0100;
                   1790:        write (2, &CARAT, 1);
                   1791:        promptlen++;
                   1792:     }
                   1793:     cbuf = ch;
                   1794:     write (2, &cbuf, 1);
                   1795:     promptlen++;
                   1796: }
                   1797:
                   1798: error (mess)
                   1799: char *mess;
                   1800: {
                   1801:     if (clreol)
                   1802:        cleareol ();
                   1803:     else
                   1804:        kill_line ();
                   1805:     promptlen += strlen (mess);
                   1806:     if (Senter && Sexit) {
                   1807:        tputs (Senter, 1, putch);
                   1808:        pr(mess);
                   1809:        tputs (Sexit, 1, putch);
                   1810:     }
                   1811:     else
                   1812:        pr (mess);
                   1813:     fflush(stdout);
                   1814:     errors++;
                   1815:     longjmp (restore, 1);
                   1816: }
                   1817:
                   1818:
                   1819: set_tty ()
                   1820: {
                   1821:        otty.sg_flags |= MBIT;
                   1822:        otty.sg_flags &= ~ECHO;
                   1823:        stty(fileno(stderr), &otty);
                   1824: }
                   1825:
                   1826: reset_tty ()
                   1827: {
                   1828:     if (no_tty)
                   1829:        return;
                   1830:     if (pstate) {
                   1831:        tputs(ULexit, 1, putch);
                   1832:        fflush(stdout);
                   1833:        pstate = 0;
                   1834:     }
                   1835:     otty.sg_flags |= ECHO;
                   1836:     otty.sg_flags &= ~MBIT;
                   1837:     stty(fileno(stderr), &savetty);
                   1838: }
                   1839:
                   1840: rdline (f)
                   1841: register FILE *f;
                   1842: {
                   1843:     register char c;
                   1844:     register char *p;
                   1845:
                   1846:     p = Line;
                   1847:     while ((c = Getc (f)) != '\n' && c != EOF && p - Line < LINSIZ - 1)
                   1848:        *p++ = c;
                   1849:     if (c == '\n')
                   1850:        Currline++;
                   1851:     *p = '\0';
                   1852: }
                   1853:
                   1854: /* Come here when we get a suspend signal from the terminal */
                   1855:
                   1856: void
                   1857: onsusp ()
                   1858: {
                   1859:     /* ignore SIGTTOU so we don't get stopped if csh grabs the tty */
                   1860:     signal(SIGTTOU, SIG_IGN);
                   1861:     reset_tty ();
                   1862:     fflush (stdout);
                   1863:     signal(SIGTTOU, SIG_DFL);
                   1864:     /* Send the TSTP signal to suspend our process group */
                   1865:     signal(SIGTSTP, SIG_DFL);
                   1866:     sigsetmask(0);
                   1867:     kill (0, SIGTSTP);
                   1868:     /* Pause for station break */
                   1869:
                   1870:     /* We're back */
                   1871:     signal (SIGTSTP, onsusp);
                   1872:     set_tty ();
                   1873:     if (inwait)
                   1874:            longjmp (restore, 1);
                   1875: }