Annotation of researchv9/jtools/src/sux/sux.c, revision 1.1

1.1     ! root        1: #include <sys/types.h>
        !             2: #include <sys/ioctl.h>
        !             3: #include <signal.h>
        !             4: 
        !             5: #ifndef BSD
        !             6: #include <sys/stream.h>
        !             7: 
        !             8: char           umesgf[]=       "/dev/pt/pt109";
        !             9: extern int     mesg_ld;
        !            10: struct ttydevb devmodes;
        !            11: #endif BSD
        !            12: 
        !            13: #include "jerq.h"
        !            14: #include "frame.h"
        !            15: 
        !            16: char           *shell;
        !            17: struct sgttyb  sttymodes;
        !            18: struct tchars  tcharmodes;
        !            19: extern char    *getenv();
        !            20: 
        !            21: #ifdef BSD
        !            22: main(argc, argv)
        !            23: char *argv[];
        !            24: {
        !            25:        if((shell=getenv("SHELL")) == 0)
        !            26:                shell="sh";
        !            27:        if (doshell() < 0)
        !            28:                exit(1);
        !            29:        initdisplay(argc, argv);
        !            30:        initcursors();
        !            31:        windowproc();
        !            32: }
        !            33: #else
        !            34: main(argc, argv)
        !            35: char *argv[];
        !            36: {
        !            37:        if((shell=getenv("SHELL")) == 0)
        !            38:                shell="sh";
        !            39:        initdisplay(argc, argv);
        !            40:        if (ioctl(3, TIOCGETP, &sttymodes) == -1)
        !            41:                readmodes();
        !            42:        else {
        !            43:                ioctl(3, TIOCGDEV, &devmodes);
        !            44:                ioctl(3, TIOCGETC, &tcharmodes);
        !            45:        }
        !            46:        if (doshell() < 0)
        !            47:                exit(1);
        !            48:        initcursors();
        !            49:        windowproc();
        !            50: }
        !            51: 
        !            52: readmodes()
        !            53: {
        !            54:        char filename[256], *p;
        !            55:        int fd;
        !            56: 
        !            57:        if((p=getenv("HOME")) == 0)
        !            58:                exit(1);
        !            59:        strcpy(filename, p);
        !            60:        strcat(filename, "/.sttymodes");
        !            61:        if ((fd = open(filename, 0)) < 0)
        !            62:                exit(1);
        !            63:        read(fd, &sttymodes, sizeof(struct sgttyb));
        !            64:        read(fd, &devmodes, sizeof(struct ttydevb));
        !            65:        read(fd, &tcharmodes, sizeof(struct tchars));
        !            66:        close(fd);
        !            67: }
        !            68: #endif BSD
        !            69: 
        !            70: jerqsizehints()
        !            71: {
        !            72:        setsizehints (fontwidth(&defont) * 80 + 3*SCROLLWIDTH/2 + 5,
        !            73:                      fontheight(&defont) * 24 + 5, 0);
        !            74: }
        !            75: 
        !            76: #ifdef BSD
        !            77: void
        !            78: dosig(sig)
        !            79: {}
        !            80: 
        !            81: delim()
        !            82: {}
        !            83: #else
        !            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: void
        !           167: dosig(sig)             /* Interrupt shell */
        !           168: {
        !           169:        char sigbuf[MSGHLEN+1];
        !           170:        register struct mesg *mp;
        !           171: 
        !           172:        mp = (struct mesg *)sigbuf;
        !           173:        mp->type=M_SIGNAL;
        !           174:        mp->magic=MSGMAGIC;
        !           175:        mp->losize=sizeof(char);
        !           176:        mp->hisize=0;
        !           177:        sigbuf[MSGHLEN]=sig;
        !           178:        write(1, sigbuf, MSGHLEN+1);
        !           179:        mp->type=M_FLUSH;
        !           180:        mp->magic=MSGMAGIC;
        !           181:        mp->losize=0;
        !           182:        mp->hisize=0;
        !           183:        write(1, sigbuf, MSGHLEN);
        !           184: }
        !           185: 
        !           186: wrmesgb(cp, n)
        !           187:        register char *cp;
        !           188:        int n;
        !           189: {
        !           190:        char wrbuf[128+MSGHLEN];
        !           191:        register char *bp;
        !           192:        register struct mesg *mp;
        !           193:        register int i;
        !           194: 
        !           195:        mp = (struct mesg *)wrbuf;
        !           196:        mp->type=M_DATA;
        !           197:        mp->magic=MSGMAGIC;
        !           198:        mp->losize=n;
        !           199:        mp->hisize=n>>8;
        !           200:        if (n <= 128) {
        !           201:                bp=wrbuf+MSGHLEN;
        !           202:                i=n;
        !           203:                while(i--)
        !           204:                        *bp++= *cp++;
        !           205:                write(1, wrbuf, MSGHLEN+n);
        !           206:        } else {
        !           207:                write(1, wrbuf, MSGHLEN);
        !           208:                write(1, cp, n);
        !           209:        }
        !           210: }
        !           211: 
        !           212: delim()
        !           213: {
        !           214:        struct mesg delbuf;
        !           215: 
        !           216:        delbuf.type=M_DELIM;
        !           217:        delbuf.magic=MSGMAGIC;
        !           218:        delbuf.losize=0;
        !           219:        delbuf.hisize=0;
        !           220:        write(1, (char *)&delbuf, MSGHLEN);
        !           221: }
        !           222: 
        !           223: doshell()
        !           224: {
        !           225:        register fd, slave;
        !           226:        if((fd=ptopen(umesgf))<0){
        !           227:                return -1;
        !           228:        }
        !           229:        if((slave=open(umesgf, 2))==-1){
        !           230:                close(fd);
        !           231:                return -1;
        !           232:        }
        !           233:        if(ioctl(fd, FIOPUSHLD, &mesg_ld) == -1){
        !           234:                close(fd);
        !           235:                return -1;
        !           236:        }
        !           237:        switch(fork()){
        !           238:        case 0:
        !           239:                /* close every file descriptor in sight, and then some */
        !           240:                for(fd=0; fd<10; fd++)
        !           241:                        if(fd!=slave)
        !           242:                                close(fd);
        !           243:                dup(slave); dup(slave); dup(slave); dup(slave);
        !           244:                close(slave);
        !           245:                ioctl(0, TIOCSPGRP, 0);
        !           246:                signal(SIGPIPE, (int (*)())0);
        !           247:                execlp(shell, shell, 0);
        !           248:                perror(shell);
        !           249:                exit(1);
        !           250:                break;
        !           251:        case -1:
        !           252:                close(fd);
        !           253:                return -1;
        !           254:        }
        !           255:        close(slave);
        !           256:        close(0); close(1);
        !           257:        dup(fd); dup(fd);
        !           258:        return(fd);
        !           259: }
        !           260: #endif BSD
        !           261: 
        !           262: clear(r, inh)
        !           263:        Rectangle r;
        !           264: {
        !           265:        rectf(&display, r, F_CLR);
        !           266: }
        !           267: 
        !           268: whichbutton()
        !           269: {
        !           270:        static int which[]={0, 3, 2, 2, 1, 1, 2, 2, };
        !           271:        return which[mouse.buttons&7];
        !           272: }
        !           273: 
        !           274: #ifdef BSD
        !           275: doshell()
        !           276: {
        !           277:        static struct  sgttyb sg;
        !           278:        static struct  tchars tc;
        !           279:        static struct  ltchars ltc;
        !           280:        int discipline;
        !           281:        int pid;
        !           282:        long lmode;
        !           283:        int tty, pgrp, slave;
        !           284:        register fd, i;
        !           285: 
        !           286:        for(i = 0; i < NSIG; i++)
        !           287:                signal(i, SIG_DFL);
        !           288: 
        !           289:        tty = open ("/dev/tty", 2);
        !           290:        ioctl(tty, TIOCGETP, (char *)&sg);
        !           291:        ioctl(tty, TIOCGETC, (char *)&tc);
        !           292:        ioctl(tty, TIOCGETD, (char *)&discipline);
        !           293:        ioctl(tty, TIOCGLTC, (char *)&ltc);
        !           294:        ioctl(tty, TIOCLGET, (char *)&lmode);
        !           295:        ioctl(tty, TIOCNOTTY, (char *)0);
        !           296:        close (tty);
        !           297:        sttymodes = sg;
        !           298:        sttymodes.sg_flags = RAW;
        !           299:        tcharmodes = tc;
        !           300:        if((fd=ptopen(&slave))<0)
        !           301:                return -1;
        !           302:        switch(fork()){
        !           303:        case 0:
        !           304:                pid = getpid();
        !           305:                ioctl(slave, TIOCSPGRP, &pid);
        !           306:                setpgrp (pid, pid);
        !           307:                dup2(slave, 0); dup2(slave, 1); dup2(slave, 2);
        !           308:                /* close every file descriptor in sight, and then some */
        !           309:                for(fd=3; fd<10; fd++)
        !           310:                        close(fd);
        !           311:                sg.sg_flags &= ~(ALLDELAY | XTABS | CBREAK | RAW);
        !           312:                sg.sg_flags |= ECHO | CRMOD;
        !           313:                sg.sg_ispeed = B9600;
        !           314:                sg.sg_ospeed = B9600;
        !           315:                tc.t_brkc = -1;
        !           316:                ioctl (0, TIOCSETD, (char *)&discipline);
        !           317:                ioctl (0, TIOCSETP, (char *)&sg);
        !           318:                ioctl (0, TIOCSETC, (char *)&tc);
        !           319:                ioctl (0, TIOCSLTC, (char *)&ltc);
        !           320:                ioctl (0, TIOCLSET, (char *)&lmode);
        !           321:                execlp(shell, shell, 0);
        !           322:                perror(shell);
        !           323:                exit(1);
        !           324:                break;
        !           325:        case -1:
        !           326:                close(fd);
        !           327:                return -1;
        !           328:        }
        !           329:        signal(SIGINT, SIG_IGN);
        !           330:        signal(SIGQUIT, SIG_IGN);
        !           331:        close(slave);
        !           332:        dup2(fd, 0);
        !           333:        dup2(fd, 1);
        !           334:        return(fd);
        !           335: }
        !           336: 
        !           337: ptopen (tty)
        !           338: int *tty;
        !           339: {
        !           340:        int devindex, letter = 0;
        !           341:        int masterfd;
        !           342:        static char ttydev[] = "/dev/ttyxx";
        !           343:        static char ptydev[] = "/dev/ptyxx";
        !           344: 
        !           345:        while (letter < 11) {
        !           346:            ttydev [8] = ptydev [8] = "pqrstuvwxyz" [letter++];
        !           347:            devindex = 0;
        !           348: 
        !           349:            while (devindex < 16) {
        !           350:                ttydev [9] = ptydev [9] = "0123456789abcdef" [devindex++];
        !           351:                if ((masterfd = open (ptydev, 2)) < 0)
        !           352:                        continue;
        !           353:                if ((*tty = open (ttydev, 2)) < 0) {
        !           354:                        close(masterfd);
        !           355:                        continue;
        !           356:                }
        !           357:                return masterfd;
        !           358:            }
        !           359:        }
        !           360:        return -1;
        !           361: }
        !           362: #endif BSD

unix.superglobalmegacorp.com

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