Annotation of researchv10no/cmd/p/p.c, revision 1.1

1.1     ! root        1: /*%cc p.c pad.o spname.o
        !             2:  */
        !             3: #include <stdio.h>
        !             4: #include "pad.h"
        !             5: #include <signal.h>
        !             6: #define        DEF     22      /* 3*DEF == 66, #lines per nroff page */
        !             7: 
        !             8: #define        STDIN   (char *)0
        !             9: int nprintfiles = 0;
        !            10: int lineno = 0;
        !            11: int pglen = DEF;
        !            12: FILE *tty;
        !            13: static char obuf[BUFSIZ];
        !            14: 
        !            15: #ifndef SPNAME
        !            16: #define        spopen(f, m)    Pfopen(f, m)
        !            17: #endif
        !            18: 
        !            19: main(argc, argv)
        !            20:        char *argv[];
        !            21: {
        !            22:        if((tty=fopen("/dev/tty", "r")) == NULL) {
        !            23:                fprintf(stderr, "p: no /dev/tty\n");
        !            24:                exit(1);
        !            25:        }
        !            26:        setbuf(stdout, obuf);
        !            27:        while(argc > 1) {
        !            28:                --argc; argv++;
        !            29:                if(*argv[0] == '-') {
        !            30:                        pglen = atoi(&argv[0][1]);
        !            31:                        if(pglen < 0)
        !            32:                                pglen = DEF;
        !            33:                } else
        !            34:                        printfile(argv[0], argc==1);
        !            35:        }
        !            36:        if(nprintfiles == 0)
        !            37:                printfile(STDIN, 1);
        !            38:        return 0;
        !            39: }
        !            40: 
        !            41: #if SPNAME
        !            42: PAD *
        !            43: spopen(file, mode)
        !            44:        register char *file;
        !            45:        register char *mode;
        !            46: {
        !            47:        register PAD *f;
        !            48:        register int c;
        !            49:        extern char *spname();
        !            50: 
        !            51:        if ((f=Pfopen(file, mode)) != NULL)
        !            52:                return (f);
        !            53:        if ((file = spname(file)) == NULL)
        !            54:                return (f);
        !            55:        fprintf(stderr, "\"p %s\"? ", file);
        !            56:        fflush(stdout);
        !            57:        c = getc(tty);
        !            58:        if(c != 'n')
        !            59:                f=Pfopen(file, mode);
        !            60:        while(c != '\n' && c != EOF)
        !            61:                c = getc(tty);
        !            62:        return(f);
        !            63: }
        !            64: #endif
        !            65: 
        !            66: printfile(file, last)
        !            67:        register char *file;
        !            68: {
        !            69:        register PAD *pad;
        !            70:        register c;
        !            71: 
        !            72:        nprintfiles++;
        !            73:        if(file == STDIN)
        !            74:                pad=Pfdopen(0);
        !            75:        else if((pad=spopen(file, "r")) == NULL) {
        !            76:                fprintf(stderr, "p: can't open %s\n", file);
        !            77:                return;
        !            78:        }
        !            79:        while((c=Pgetc(pad))!=EOF || (!last && newline(pad)=='-')) {
        !            80:                if(c == '\n' && ++lineno==pglen) {
        !            81:                        if(newline(pad)==EOF)
        !            82:                                goto Return;
        !            83:                } else
        !            84:                        putchar(c);
        !            85:        }
        !            86:        fflush(stdout);
        !            87:  Return:
        !            88:        Pclose(pad);
        !            89: }
        !            90: 
        !            91: newline(p)
        !            92:        register PAD *p;
        !            93: {
        !            94:        register c, i;
        !            95: 
        !            96:        lineno = 0;
        !            97:        fflush(stdout);
        !            98:   loop:
        !            99:        switch(getc(tty)){
        !           100:        case '\n':              /* Continue */
        !           101:                return '\n';
        !           102:        case '!':
        !           103:                callunix();
        !           104:                goto loop;
        !           105:        case 'q':
        !           106:                waitnl();
        !           107:                /* Fall through */
        !           108:        case EOF:
        !           109:                return EOF;
        !           110:        case '-':
        !           111:                for(i=1; (c=getc(tty))=='-'; i++)
        !           112:                        ;
        !           113:                ungetc(c, tty);
        !           114:                backpage(p, i);
        !           115:                waitnl();
        !           116:                return '-';
        !           117:        default:
        !           118:                waitnl();
        !           119:                return '\n';
        !           120:        }
        !           121: }
        !           122: 
        !           123: backpage(p, pg)
        !           124:        register PAD *p;
        !           125:        register pg;
        !           126: {
        !           127:        register i;
        !           128:        while(pg--)
        !           129:                for(i=0; i<pglen; i++)
        !           130:                        if(backline(p)==EOF)
        !           131:                                return;
        !           132:        /* Now at end of new first line; back up to beginning */
        !           133:        if(backline(p)=='\n')
        !           134:                (void)Pgetc(p);         /* Eat that newline */
        !           135: }
        !           136: 
        !           137: backline(p)
        !           138:        register PAD *p;
        !           139: {
        !           140:        register c;
        !           141:        while((c=Pbackc(p))!='\n')
        !           142:                if(c==EOF)
        !           143:                        return(EOF);
        !           144:        return('\n');
        !           145: }
        !           146: 
        !           147: waitnl(){
        !           148:        do; while(getc(tty) != '\n');
        !           149: }
        !           150: 
        !           151: callunix()
        !           152: {
        !           153:        int rc, status, unixpid;
        !           154:        char buf[256];
        !           155:        register char *p;
        !           156:        for(p=buf; (*p++=getc(tty))!='\n'; )
        !           157:                ;
        !           158:        *--p=0;
        !           159:        if( (unixpid=fork())==0 ) {
        !           160:                close(0); dup(2);
        !           161:                execl("/bin/sh", "sh", "-c", buf, 0);
        !           162:                exit(255);
        !           163:        }
        !           164:        else if(unixpid == -1){
        !           165:                fprintf(stderr, "p: can't fork\n");
        !           166:                return;
        !           167:        }else{  signal(SIGINT, SIG_IGN); signal(SIGQUIT, SIG_IGN);
        !           168:                while( (rc = wait(&status)) != unixpid && rc != -1 ) ;
        !           169:                signal(SIGINT, SIG_DFL); signal(SIGQUIT, SIG_DFL);
        !           170:                fprintf(stderr, "!\n");
        !           171:                fflush(stdout);
        !           172:        }
        !           173: }

unix.superglobalmegacorp.com

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