Annotation of researchv9/jtools/src/tek/x11.c, revision 1.1.1.1

1.1       root        1: #include <sys/types.h>
                      2: #ifdef BSD
                      3: #include <sgtty.h>
                      4: #include <setjmp.h>
                      5: #include <errno.h>
                      6: #else
                      7: #include <sys/stream.h>
                      8: #include <sys/ioctl.h>
                      9: 
                     10: extern int tty_ld, mesg_ld;
                     11: static struct ttydevb  devmodes;
                     12: #endif BSD
                     13: #include <signal.h>
                     14: #include "jerq.h"
                     15: 
                     16: static char            umesgf[]=       "/dev/pt/pt109";
                     17: static char            *shell;
                     18: static struct sgttyb   sttymodes;
                     19: static struct tchars   tcharmodes;
                     20: int    childid;
                     21: 
                     22: extern char    *getenv();
                     23: 
                     24: dsegment(b, p, q, f, linetype)
                     25: Bitmap *b;
                     26:        Point p, q;
                     27:        char *linetype;
                     28: {
                     29: #ifdef X11
                     30:        if (linetype) {
                     31:                XSetLineAttributes(dpy,gc,0,LineDoubleDash,CapButt,JoinMiter);
                     32:                XSetDashes(dpy,gc,0, linetype, strlen(linetype));
                     33:        }
                     34: #endif X11
                     35:        segment(b,p,q,f);
                     36: #ifdef X11
                     37:        if (linetype)
                     38:                XSetLineAttributes(dpy,gc,0,LineSolid,CapButt,JoinMiter);
                     39: #endif X11
                     40: }
                     41: 
                     42: #ifdef BSD
                     43: startshell()
                     44: {
                     45:        if((shell=getenv("SHELL")) == 0)
                     46:                shell="sh";
                     47:        if (doshell() < 0)
                     48:                exit(1);
                     49: }
                     50: #else
                     51: startshell()
                     52: {
                     53:        if((shell=getenv("SHELL")) == 0)
                     54:                shell="sh";
                     55:        if (ioctl(3, TIOCGETP, &sttymodes) == -1)
                     56:                readmodes();
                     57:        else {
                     58:                ioctl(3, TIOCGDEV, &devmodes);
                     59:                ioctl(3, TIOCGETC, &tcharmodes);
                     60:        }
                     61:        if (doshell() < 0)
                     62:                exit(1);
                     63: }
                     64: 
                     65: readmodes()
                     66: {
                     67:        char filename[256], *p;
                     68:        int fd;
                     69: 
                     70:        if((p=getenv("HOME")) == 0)
                     71:                exit(1);
                     72:        strcpy(filename, p);
                     73:        strcat(filename, "/.sttymodes");
                     74:        if ((fd = open(filename, 0)) < 0)
                     75:                exit(1);
                     76:        read(fd, &sttymodes, sizeof(struct sgttyb));
                     77:        read(fd, &devmodes, sizeof(struct ttydevb));
                     78:        read(fd, &tcharmodes, sizeof(struct tchars));
                     79:        close(fd);
                     80: }
                     81: #endif BSD
                     82: 
                     83: #ifndef BSD
                     84: /*
                     85:  *     Unpack a message
                     86:  */
                     87: rcvfill()
                     88: {
                     89:        register struct mesg *mp;
                     90:        register int size;
                     91:        char buf[1024];
                     92:        int i, n;
                     93:        register char *bp;
                     94: 
                     95:        n = read(0, buf, sizeof(buf));
                     96:        bp = buf;
                     97:        mp = (struct mesg *)bp;
                     98:        size = mp->losize + (mp->hisize<<8);
                     99:        if(n<=0)
                    100:                mp->type=M_HANGUP;
                    101:        switch (mp->type) {
                    102:        case M_HANGUP:
                    103:                exit(1);
                    104:        case M_DELAY:
                    105:        default:
                    106:                return;
                    107:        case M_DELIM:
                    108:        case M_DATA:
                    109:                if(size==0)
                    110:                        return;
                    111:                break;
                    112:        case M_IOCTL:
                    113:                mp->type = M_IOCACK;
                    114:                switch (*(int *)(bp+MSGHLEN)) {
                    115:                case TIOCSETP:
                    116:                case TIOCSETN:
                    117:                        sttymodes = *(struct sgttyb *)(bp+MSGHLEN+sizeof(int));                                 size = 0;
                    118:                        break;
                    119:                case TIOCGETP:
                    120:                        *(struct sgttyb *)(bp+MSGHLEN+sizeof(int)) = sttymodes;
                    121:                        size=sizeof(struct sgttyb)+sizeof(int);
                    122:                        break;
                    123:                case TIOCSETC:
                    124:                        tcharmodes = *(struct tchars *)(bp+MSGHLEN+sizeof(int));
                    125:                        size=0;
                    126:                        break;
                    127:                case TIOCGETC:
                    128:                        *(struct tchars *)(bp+MSGHLEN+sizeof(int)) = tcharmodes;
                    129:                        size=sizeof (struct tchars) + sizeof (int);
                    130:                        break;
                    131:                case TIOCSDEV:
                    132:                        size=0;
                    133:                        break;
                    134:                case TIOCGDEV:
                    135:                        size=sizeof(struct ttydevb)+sizeof(int);
                    136:                        *(struct ttydevb *)(bp+MSGHLEN+sizeof(int))=devmodes;
                    137:                        break;
                    138:                default:
                    139:                        mp->type = M_IOCNAK;
                    140:                        size = 0;
                    141:                }
                    142:                mp->magic = MSGMAGIC;           /* safety net */
                    143:                mp->losize = size;
                    144:                mp->hisize = size>>8;
                    145:                write(1, bp, MSGHLEN+size);
                    146:                return;
                    147:        }
                    148:        bp += MSGHLEN;
                    149:        rcvbfill(bp, size);
                    150: }
                    151: 
                    152: sendchar(c)
                    153: {
                    154:        unsigned char uc = c;
                    155: 
                    156:        wrmesgb(&uc, 1);
                    157:        delim();
                    158: }
                    159: 
                    160: sendnchars(n, cp)
                    161: char *cp;
                    162: {
                    163:        wrmesgb(cp, n);
                    164: }
                    165: 
                    166: wrmesgb(cp, n)
                    167:        register char *cp;
                    168:        int n;
                    169: {
                    170:        char wrbuf[128+MSGHLEN];
                    171:        register char *bp;
                    172:        register struct mesg *mp;
                    173:        register int i;
                    174: 
                    175:        mp = (struct mesg *)wrbuf;
                    176:        mp->type=M_DATA;
                    177:        mp->magic=MSGMAGIC;
                    178:        mp->losize=n;
                    179:        mp->hisize=n>>8;
                    180:        if (n <= 128) {
                    181:                bp=wrbuf+MSGHLEN;
                    182:                i=n;
                    183:                while(i--)
                    184:                        *bp++= *cp++;
                    185:                write(1, wrbuf, MSGHLEN+n);
                    186:        } else {
                    187:                write(1, wrbuf, MSGHLEN);
                    188:                write(1, cp, n);
                    189:        }
                    190: }
                    191: 
                    192: delim()
                    193: {
                    194:        struct mesg delbuf;
                    195: 
                    196:        delbuf.type=M_DELIM;
                    197:        delbuf.magic=MSGMAGIC;
                    198:        delbuf.losize=0;
                    199:        delbuf.hisize=0;
                    200:        write(1, (char *)&delbuf, MSGHLEN);
                    201: }
                    202: 
                    203: doshell()
                    204: {
                    205:        register fd, slave;
                    206:        if((fd=ptopen(umesgf))<0){
                    207:                return -1;
                    208:        }
                    209:        if((slave=open(umesgf, 2))==-1){
                    210:                close(fd);
                    211:                return -1;
                    212:        }
                    213:        if(ioctl(fd, FIOPUSHLD, &mesg_ld) == -1){
                    214:                close(slave);
                    215:                close(fd);
                    216:                return -1;
                    217:        }
                    218:        if(ioctl(slave, FIOPUSHLD, &tty_ld) == -1){
                    219:                close(slave);
                    220:                close(fd);
                    221:                return -1;
                    222:        }
                    223: 
                    224:        switch(childid = fork()){
                    225:        case 0:
                    226:                /* close every file descriptor in sight, and then some */
                    227:                for(fd=0; fd<10; fd++)
                    228:                        if(fd!=slave)
                    229:                                close(fd);
                    230:                dup(slave); dup(slave); dup(slave); dup(slave);
                    231:                close(slave);
                    232:                ioctl (0, TIOCSETP, (char *)&sttymodes);
                    233:                ioctl (0, TIOCSETC, (char *)&tcharmodes);
                    234:                ioctl(0, TIOCSPGRP, 0);
                    235:                signal(SIGPIPE, (int (*)())0);
                    236:                execlp(shell, shell, 0);
                    237:                perror(shell);
                    238:                exit(1);
                    239:                break;
                    240:        case -1:
                    241:                close(fd);
                    242:                return -1;
                    243:        }
                    244:        close(slave);
                    245:        close(0); close(1);
                    246:        dup(fd); dup(fd);
                    247:        return(fd);
                    248: }
                    249: #endif BSD
                    250: 
                    251: #ifdef BSD
                    252: static jmp_buf env;
                    253: hungtty()
                    254: {
                    255:        longjmp(env, 1);
                    256: }
                    257: 
                    258: doshell()
                    259: {
                    260:        static struct  ltchars ltc;
                    261:        int discipline;
                    262:        long lmode;
                    263:        int tty, pgrp, slave;
                    264:        register fd, i;
                    265: 
                    266:        for(i = 0; i < NSIG; i++)
                    267:                signal(i, SIG_DFL);
                    268: 
                    269:        tty = open ("/dev/tty", 2);
                    270:        ioctl(tty, TIOCGETP, (char *)&sttymodes);
                    271:        ioctl(tty, TIOCGETC, (char *)&tcharmodes);
                    272:        ioctl(tty, TIOCGETD, (char *)&discipline);
                    273:        ioctl(tty, TIOCGLTC, (char *)&ltc);
                    274:        ioctl(tty, TIOCLGET, (char *)&lmode);
                    275:        ioctl(tty, TIOCNOTTY, (char *)0);
                    276:        close (tty);
                    277:        if((fd=ptopen(&slave))<0)
                    278:                return -1;
                    279:        switch(childid = fork()){
                    280:        case 0:
                    281:                childid = getpid();
                    282:                ioctl(slave, TIOCSPGRP, &childid);
                    283:                setpgrp (childid, childid);
                    284:                dup2(slave, 0); dup2(slave, 1); dup2(slave, 2);
                    285:                /* close every file descriptor in sight, and then some */
                    286:                for(fd=3; fd<10; fd++)
                    287:                        close(fd);
                    288:                sttymodes.sg_flags &= ~(ALLDELAY | XTABS | CBREAK | RAW);
                    289:                sttymodes.sg_flags |= ECHO | CRMOD;
                    290:                sttymodes.sg_ispeed = B9600;
                    291:                sttymodes.sg_ospeed = B9600;
                    292:                tcharmodes.t_brkc = -1;
                    293:                ioctl (0, TIOCSETD, (char *)&discipline);
                    294:                ioctl (0, TIOCSETP, (char *)&sttymodes);
                    295:                ioctl (0, TIOCSETC, (char *)&tcharmodes);
                    296:                ioctl (0, TIOCSLTC, (char *)&ltc);
                    297:                ioctl (0, TIOCLSET, (char *)&lmode);
                    298:                execlp(shell, shell, 0);
                    299:                execlp(shell, shell, 0);
                    300:                perror(shell);
                    301:                exit(1);
                    302:                break;
                    303:        case -1:
                    304:                close(fd);
                    305:                return -1;
                    306:        }
                    307:        close(slave);
                    308:        dup2(fd, 0);
                    309:        dup2(fd, 1);
                    310:        return(fd);
                    311: }
                    312: 
                    313: ptopen (tty)
                    314: int *tty;
                    315: {
                    316:        int devindex, letter = 0;
                    317:        static char ttydev[] = "/dev/ttyxx";
                    318:        static char ptydev[] = "/dev/ptyxx";
                    319:        int masterfd;
                    320: 
                    321:        while (letter < 11) {
                    322:            ttydev [8] = ptydev [8] = "pqrstuvwxyz" [letter++];
                    323:            devindex = 0;
                    324: 
                    325:            while (devindex < 16) {
                    326:                ttydev [9] = ptydev [9] = "0123456789abcdef" [devindex++];
                    327:                if ((masterfd = open (ptydev, 2)) < 0)
                    328:                        continue;
                    329:                if ((*tty = open (ttydev, 2)) < 0) {
                    330:                        close(masterfd);
                    331:                        continue;
                    332:                }
                    333:                return masterfd;
                    334:            }
                    335:        }
                    336:        return -1;
                    337: }
                    338: #endif BSD

unix.superglobalmegacorp.com

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