Annotation of researchv8dc/cmd/p/p.c, revision 1.1.1.1

1.1       root        1: #define        JERQ    No home should be without one
                      2: /*%cc p.c pad.o spname.o
                      3:  */
                      4: #include <stdio.h>
                      5: #include "pad.h"
                      6: #include <signal.h>
                      7: #ifdef JERQ
                      8: #include <sgtty.h>
                      9: #include "/usr/jerq/include/jioctl.h"
                     10: int    ismpx=0;
                     11: #endif
                     12: #define        DEF     22      /* 3*DEF == 66, #lines per nroff page */
                     13: #define        WIDTH   79      /* some goddam terminals get confused at the margin */
                     14: #define        STDIN   (char *)0
                     15: int nprintfiles = 0;
                     16: int lineno = 0;
                     17: int pglen = DEF;
                     18: int width = WIDTH;
                     19: FILE *tty;
                     20: char buf[BUFSIZ];
                     21: 
                     22: main(argc, argv)
                     23:        char *argv[];
                     24: {
                     25: #ifdef JERQ
                     26:        struct sgttyb sttybuf;
                     27: #endif
                     28:        if((tty=fopen("/dev/tty", "r")) == NULL) {
                     29:                printf("p: no /dev/tty\n");
                     30:                exit(1);
                     31:        }
                     32:        setbuf(stdout, buf);
                     33: #ifdef JERQ
                     34:        if(ioctl(1, JWINSIZE, &sttybuf) == 0){
                     35:                pglen = sttybuf.sg_ospeed-1;    /* ick */
                     36:                if(pglen<0)
                     37:                        pglen = 5;      /* why not 5? */
                     38:                width = sttybuf.sg_ispeed-1;
                     39:                if(width<10)
                     40:                        width=10;
                     41:                ismpx=1;
                     42:        }
                     43: #endif
                     44:        while(argc > 1) {
                     45:                --argc; argv++;
                     46:                if(*argv[0] == '-') {
                     47:                        pglen = atoi(&argv[0][1]);
                     48:                        if(pglen < 0)
                     49:                                pglen = DEF;
                     50:                } else
                     51:                        printfile(argv[0], argc==1);
                     52:        }
                     53:        if(nprintfiles == 0)
                     54:                printfile(STDIN, 1);
                     55:        return 0;
                     56: }
                     57: 
                     58: PAD *
                     59: spopen(file, mode)
                     60:        register char *file;
                     61:        register char *mode;
                     62: {
                     63:        register FILE *f;
                     64:        register char c;
                     65:        extern char *spname();
                     66: 
                     67:        if((f=fopen(file, mode)) == NULL) {
                     68:                file = spname(file);
                     69:                if(file != (char *)0){
                     70:                        printf("\"p %s\"? ", file);
                     71:                        fflush(stdout);
                     72:                        c = getc(tty);
                     73:                        if(c != 'n')
                     74:                                f=fopen(file, mode);
                     75:                        while(c != '\n')
                     76:                                c = getc(tty);
                     77:                }
                     78:        }
                     79:        return(Pfopen(f));
                     80: }
                     81: int peek;
                     82: int cheat;
                     83: int col;
                     84: get(pad)
                     85:        PAD *pad;
                     86: {
                     87:        register c;
                     88:        if(c=peek){
                     89:                peek=0;
                     90:                if(cheat && (peek=Pgetc(pad))=='\n')
                     91:                        peek=0; /* don't insert '\n' if it's coming up anyway */
                     92:                cheat=0;
                     93:        }else
                     94:                c=Pgetc(pad);
                     95:        return c;
                     96: }
                     97: put(c)
                     98:        register c;
                     99: {
                    100:        putchar(c);
                    101:        if(c=='\t')
                    102:                col=(col|7)+1;
                    103:        else if(c=='\n')
                    104:                col=0;
                    105:        else if(c=='\b' && col>0)
                    106:                col--;
                    107:        else if(c>=' ')
                    108:                col++;
                    109:        if(col>=width)
                    110:                cheat=peek='\n';
                    111: }
                    112: printfile(file, last)
                    113:        register char *file;
                    114: {
                    115:        register PAD *pad;
                    116:        register c;
                    117: 
                    118:        nprintfiles++;
                    119:        peek = cheat = 0;
                    120:        col = 0;
                    121:        if(file == STDIN)
                    122:                pad=Pfopen(stdin);
                    123:        else if((pad=spopen(file, "r")) == NULL) {
                    124:                /*
                    125:                 * no need to use stderr in p!
                    126:                 */
                    127:                printf("p: can't open %s\n", file);
                    128:                return;
                    129:        }
                    130:        while((c=get(pad))!=EOF || (!last && newline(pad, 1)=='-')) {
                    131:                if(c == '\n' && ++lineno==pglen) {
                    132:                        if(newline(pad, 0)==EOF)
                    133:                                goto Return;
                    134:                } else
                    135:                        put(c);
                    136:        }
                    137:        fflush(stdout);
                    138:  Return:
                    139:        Pclose(pad);
                    140: }
                    141: newline(p, eof)
                    142:        register PAD *p;
                    143: {
                    144:        register c, i;
                    145: 
                    146:        lineno = 0;
                    147:        col = 0;
                    148:        fflush(stdout);
                    149:   loop:
                    150:        switch(getc(tty)){
                    151:        case '\n':              /* Continue */
                    152: #ifdef JERQ
                    153:                if(ismpx && !eof)
                    154:                        putchar('\f');
                    155: #endif
                    156:                return '\n';
                    157:        case '!':
                    158:                callunix();
                    159:                goto loop;
                    160:        case 'q':
                    161:                waitnl(1);
                    162:        case EOF:
                    163:                return EOF;
                    164:        case '-':
                    165:                for(i=1; (c=getc(tty))=='-'; i++)
                    166:                        ;
                    167:                ungetc(c, tty);
                    168:                peek=cheat=0;
                    169:                backpage(p, i);
                    170:                waitnl(0);
                    171:                return '-';
                    172:                /* Fall through */
                    173:        default:
                    174:                waitnl(eof);
                    175:                return '\n';
                    176:        }
                    177: }
                    178: 
                    179: backpage(p, pg)
                    180:        register PAD *p;
                    181:        register pg;
                    182: {
                    183:        register i;
                    184:        while(pg--)
                    185:                for(i=0; i<pglen; i++)
                    186:                        if(backline(p)==EOF)
                    187:                                return;
                    188:        /* Now at end of new first line; back up to beginning */
                    189:        if(backline(p)=='\n')
                    190:                (void)Pgetc(p);         /* Eat that newline */
                    191: }
                    192: 
                    193: backline(p)
                    194:        register PAD *p;
                    195: {
                    196:        register c;
                    197:        while((c=Pbackc(p))!='\n')
                    198:                if(c==EOF)
                    199:                        return(EOF);
                    200:        return('\n');
                    201: }
                    202: 
                    203: waitnl(eof){
                    204:        do; while(getc(tty) != '\n');
                    205: #ifdef JERQ
                    206:        if(ismpx && !eof)
                    207:                putchar('\f');
                    208: #endif
                    209: }
                    210: 
                    211: callunix()
                    212: {
                    213:        int rc, status, unixpid;
                    214:        char buf[256];
                    215:        register char *p;
                    216:        for(p=buf; (*p++=getc(tty))!='\n'; )
                    217:                ;
                    218:        *--p=0;
                    219:        if( (unixpid=fork())==0 ) {
                    220:                close(0); dup(2);
                    221:                execl("/bin/sh", "sh", "-c", buf, 0);
                    222:                exit(255);
                    223:        }
                    224:        else if(unixpid == -1){
                    225:                printf("p: can't fork\n");
                    226:                return;
                    227:        }else{  signal(SIGINT, SIG_IGN); signal(SIGQUIT, SIG_IGN);
                    228:                while( (rc = wait(&status)) != unixpid && rc != -1 ) ;
                    229:                signal(SIGINT, SIG_DFL); signal(SIGQUIT, SIG_DFL);
                    230:                printf("!\n");
                    231:                fflush(stdout);
                    232:        }
                    233: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.