Annotation of researchv10dc/ipc/bin/scriptcon.c, revision 1.1.1.1

1.1       root        1: #include <sys/param.h>
                      2: #include <fio.h>
                      3: #include <ipc.h>
                      4: #include <sys/ttyio.h>
                      5: #include <signal.h>
                      6: #include <libc.h>
                      7: 
                      8: struct sgttyb old;
                      9: struct tchars chars;
                     10: int cfd;
                     11: 
                     12: done()
                     13: {
                     14:        cleanup();
                     15:        exit(0);
                     16: }
                     17: 
                     18: intr()
                     19: {
                     20:        write(cfd, &chars.t_intrc, 1);
                     21:        signal(SIGINT, intr);
                     22: }
                     23: 
                     24: dirty()
                     25: {
                     26:        struct sgttyb new;
                     27: 
                     28:        ioctl(0, TIOCGETP, &old);
                     29:        ioctl(0, TIOCGETC, &chars);
                     30:        new = old;
                     31:        new.sg_flags &= ~ECHO;
                     32:        new.sg_flags |= CBREAK;
                     33:        signal(SIGINT, intr);
                     34:        signal(SIGQUIT, done);
                     35:        signal(SIGHUP, done);
                     36:        signal(SIGPIPE, done);
                     37:        ioctl(0, TIOCSETP, &new);
                     38: }
                     39: 
                     40: cleanup()
                     41: {
                     42:        ioctl(0, TIOCSETP, &old);
                     43: }
                     44: 
                     45: usage()
                     46: {
                     47:        fprint(2, "usage: scriptcon dest script-file\n");
                     48:        exit(1);
                     49: }
                     50: 
                     51: main(ac, av)
                     52:        int ac;
                     53:        char *av[];
                     54: {
                     55:        int sfd;
                     56:        char *lookfor;
                     57:        char *sendback;
                     58:        fd_set fds;
                     59: 
                     60:        if (ac!=3)
                     61:                usage();
                     62:        sfd = open(av[2], 0);
                     63:        if (sfd<0){
                     64:                fprint(2, "can't open %s\n", av[2]);
                     65:                exit(1);
                     66:        }
                     67:        cfd = ipcopen(ipcpath(av[1], "dk", ""), "");
                     68:        if (cfd<0){
                     69:                fprint(2, "can't open %s\n", av[2]);
                     70:                exit(1);
                     71:        }
                     72: 
                     73:        /*
                     74:         *  do the script
                     75:         */
                     76:        while((lookfor=Frdline(sfd))!=NULL){
                     77:                await(cfd, lookfor);
                     78:                if((sendback=Frdline(sfd))!=NULL){
                     79:                        fprint(cfd, "%s\n", sendback);
                     80:                }
                     81:        }
                     82:        close(sfd);
                     83: 
                     84:        /*
                     85:         *  shuttle bytes back and forth
                     86:         */
                     87:        dirty();
                     88:        FD_ZERO(fds);
                     89:        for(;;) {
                     90:                FD_SET(cfd, fds);
                     91:                FD_SET(0, fds);
                     92:                switch(select(NOFILE, &fds, (struct fd_set *)0, 1000)) {
                     93:                case -1:
                     94:                        cleanup();
                     95:                        return(0);
                     96:                case 0:
                     97:                        continue;
                     98:                }
                     99:                if (FD_ISSET(cfd, fds))
                    100:                        if (pass(cfd, 1)<0)
                    101:                                break;
                    102:                if (FD_ISSET(0, fds))
                    103:                        if (pass(0, cfd)<0)
                    104:                                break;
                    105:        }
                    106: 
                    107:        cleanup();
                    108:        return(0);
                    109: }
                    110: 
                    111: /*
                    112:  *  wait for the specified string on the fd
                    113:  */
                    114: await(fd, str)
                    115:        int fd;
                    116:        char *str;
                    117: {
                    118:        char buf[128];
                    119:        int c;
                    120:        int len;
                    121: 
                    122:        len = strlen(str);
                    123:        if(len>sizeof(buf)-2)
                    124:                len = sizeof(buf)-2;
                    125:        for(c=0; c<len+2; c++)
                    126:                buf[c] = '\0';   
                    127:        while((c=Fgetc(fd))>=0){
                    128:                buf[len] = c&0x7f;
                    129:                memcpy(buf, buf+1, len+1);
                    130:                if(strcmp(buf, str)==0)
                    131:                        break;
                    132:        }
                    133: }
                    134: 
                    135: /*
                    136:  *  copy one record from one fd to another
                    137:  */
                    138: pass(from, to)
                    139:        int from, to;
                    140: {
                    141:        char buf[4096];
                    142:        int n;
                    143: 
                    144:        if ((n=read(from, buf, sizeof(buf)))<0)
                    145:                return -1;
                    146:        if (n == 0) {
                    147:                if (to == cfd) {
                    148:                        write(to, &chars.t_eofc, 1);
                    149:                        return (0);
                    150:                }
                    151:                return (-1);
                    152:        }
                    153:        if (write(to, buf, n)!=n)
                    154:                return -1;
                    155:        return 0;
                    156: }

unix.superglobalmegacorp.com

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