Annotation of researchv10no/cmd/basic/bas/cmds.c, revision 1.1

1.1     ! root        1: #include <stdio.h>
        !             2: #include "ctype.h"
        !             3: #include "typedef.h"
        !             4: #include "basic.h"
        !             5: #include "tokens.h"
        !             6: #include <signal.h>
        !             7: #include <sys/types.h>
        !             8: #include <sys/stat.h>
        !             9: 
        !            10: #define RENBASE        100
        !            11: #define RENINCR        10
        !            12: #define MAXWORD        16
        !            13: 
        !            14: char           *tempfile = "_basicedit";
        !            15: static char    *cmds[] = {
        !            16:        "old", "save", "bye", "dump", "edit", "vi", "!", "fp",
        !            17:        "int", "load", "renumber", "list", "del", "scr", "run",
        !            18:        "clear", "auto", "catalog", "unsave", "bsave", "ren", "new", 
        !            19:        "spool", 0
        !            20:        };
        !            21: 
        !            22: Linep          findline();
        !            23: Lnr            cvtlnr();
        !            24: 
        !            25: 
        !            26: /*
        !            27:  * moncmd --- determine if the current line contains a monitor command
        !            28:  */
        !            29: 
        !            30: moncmd()
        !            31: {
        !            32:        register int    i;
        !            33:        register char   *s;
        !            34:        char            *file, word[MAXWORD];
        !            35:        int             line1, line2, line3, line4;
        !            36: 
        !            37:        inptr = line;
        !            38:        skipbl();
        !            39:        getword(word, MAXWORD);
        !            40:        for (i = 0; s = cmds[i]; ++i) {
        !            41:                if (*word == *s && strcmp(word, s) == 0) {
        !            42:                        inptr += strlen(s);
        !            43:                        skipbl();
        !            44:                        switch (i) {
        !            45:                        case 0:         /* old */
        !            46:                        case 9:         /* load */
        !            47:                                old(inptr);
        !            48:                                fprintf(stderr, "%d lines\n",linecnt);
        !            49:                                break;
        !            50:                        case 22:        /* spool - listing & run on lp 
        !            51:                                file = (*inptr == 0)? curfile : inptr;
        !            52:                                exec("/bin/spool", "spool", file, 0);   */
        !            53:                                break;
        !            54:                        case 1:         /* save */
        !            55:                        case 19:        /* bsave */
        !            56:                                file = (*inptr == 0)? curfile : inptr;
        !            57:                                if (i == 1)     /* save */
        !            58:                                        save(file);
        !            59:                                else            /* bsave */
        !            60:                                        fastsave(file);
        !            61:                                fprintf(stderr, "%s saved\n", file);
        !            62:                                break;
        !            63:                        case 2:         /* bye */
        !            64:                                exit(0);
        !            65:                        case 3:         /* dump */
        !            66:                                dumpsym();
        !            67:                                break;
        !            68:                        case 4:         /* edit */
        !            69:                                edit("/usr/ucb/edit");
        !            70:                                break;
        !            71:                        case 5:         /* vi */
        !            72:                                edit("/usr/ucb/vi");
        !            73:                                break;
        !            74:                        case 6:         /* ! */
        !            75:                                exec("/bin/sh", "sh", "-c", inptr, 0);
        !            76:                                break;
        !            77:                        case 7:         /* fp */
        !            78:                                apple();
        !            79:                                prompt = "]";
        !            80:                                break;
        !            81:                        case 8:         /* int */
        !            82:                                apple();
        !            83:                                prompt = ">";
        !            84:                                break;
        !            85:                        case 20:        /* ren  */
        !            86:                        case 10:        /* renumber */
        !            87:                                line1 = RENBASE;
        !            88:                                line2 = RENINCR;
        !            89:                                getlnrs2(&line1, &line2, DEFAULTLNRS);
        !            90:                                if (line1 == 0)
        !            91:                                        line1 = RENBASE;
        !            92:                                if (line2 == 0)
        !            93:                                        line2 = RENINCR;
        !            94:                                getlnrs(&line3, &line4, LISTLNRS);
        !            95:                                renumber(line1, line2, line3, line4);
        !            96:                                break;
        !            97:                        case 11:        /* list */
        !            98:                                getlnrs(&line1, &line2, LISTLNRS);
        !            99:                                list(line1, line2, stdout);
        !           100:                                break;
        !           101:                        case 12:        /* delete */
        !           102:                                getlnrs(&line1, &line2, LISTLNRS);
        !           103:                                delete(line1, line2);
        !           104:                                break;
        !           105:                        case 21:
        !           106:                        case 13:        /* scratch */
        !           107:                                delete(MINLNR, MAXLNR);
        !           108:                                clrstk();
        !           109:                                clrsym();
        !           110:                                break;
        !           111:                        case 14:        /* run */
        !           112:                                run();
        !           113:                                break;
        !           114:                        case 15:        /* clear */
        !           115:                                clrstk();
        !           116:                                clrsym();
        !           117:                                break;
        !           118:                        case 16:        /* auto */
        !           119:                                line1 = RENBASE;
        !           120:                                line2 = RENINCR;
        !           121:                                getlnrs(&line1, &line2, DEFAULTLNRS);
        !           122:                                autonumber(line1, line2);
        !           123:                                break;
        !           124:                        case 17:        /* catalog */
        !           125:                                exec("/bin/ls", "ls", inptr, 0);
        !           126:                                break;
        !           127:                        case 18:        /* unsave */
        !           128:                                exec("/bin/rm", "rm", inptr, 0);
        !           129:                                break;
        !           130:                        default:
        !           131:                                return (NO);
        !           132:                                }
        !           133:                        return (YES);
        !           134:                        }
        !           135:                }
        !           136:        return (NO);
        !           137: }
        !           138: 
        !           139: 
        !           140: /*
        !           141:  * getword --- fetch next word from input line
        !           142:  */
        !           143: 
        !           144: getword(word, len)
        !           145: char   *word;
        !           146: {
        !           147:        register char   *p, *w;
        !           148:        register int    c;
        !           149:        
        !           150:        p = inptr;
        !           151:        w = word;
        !           152:        if (*p == '!')
        !           153:                *w++ = *p;
        !           154:        else
        !           155:                do {
        !           156:                        c = *p++;
        !           157:                        if (isupper(c))
        !           158:                                c = tolower(c);
        !           159:                        if (islower(c))
        !           160:                                *w++ = c;
        !           161:                        else
        !           162:                                break;
        !           163:                        } while (--len > 0);
        !           164:        *w = 0;
        !           165: }
        !           166:                
        !           167: 
        !           168: /*
        !           169:  * apple ---
        !           170:  */
        !           171: 
        !           172: apple()
        !           173: {
        !           174: 
        !           175:        fprintf(stderr, "Now in Apple mode\n");
        !           176: }
        !           177: 
        !           178: 
        !           179: /*
        !           180:  * edit --- invoke and editor on the current program
        !           181:  */
        !           182: 
        !           183: edit(file)
        !           184: char   *file;
        !           185: {
        !           186:        struct stat     statb;
        !           187:        long            now;
        !           188: 
        !           189:        save(tempfile);
        !           190:        time(&now);
        !           191:        exec(file, file, tempfile, 0);
        !           192:        stat(tempfile, &statb);
        !           193:        if (statb.st_mtime > now)
        !           194:                old(tempfile);
        !           195:        unlink(tempfile);
        !           196: }
        !           197: 
        !           198: 
        !           199: /*
        !           200:  * exec --- execute the given command
        !           201:  */
        !           202: 
        !           203: exec(file, args)
        !           204: char   *file, **args;
        !           205: {
        !           206:        int             status;
        !           207:        register int    pid, i, (*oldattn)();
        !           208: 
        !           209:        if ((pid = fork()) == 0) {
        !           210:                closeall();
        !           211:                execv(file, &args);
        !           212:                fprintf(stderr, "no %s\n", file);
        !           213:                exit(1);
        !           214:                }
        !           215:        oldattn = signal(SIGINT, SIG_IGN);
        !           216:        while ((i = wait(&status)) != -1 && i != pid)
        !           217:                ;
        !           218:        signal(SIGINT, oldattn);
        !           219:        return (status);
        !           220: }
        !           221: 
        !           222: 
        !           223: /*
        !           224:  * chain --- replace the current program with a new one and execute it
        !           225:  */
        !           226: 
        !           227: chain()
        !           228: {
        !           229:        int             len;
        !           230:        char            *ptr, file[32];
        !           231:        Lnr             lnr;
        !           232: 
        !           233:        expr();
        !           234:        popstring(&ptr, &len);
        !           235:        move(len, ptr, file);
        !           236:        file[len] = 0;
        !           237:        if (*inptr == COMMA) {
        !           238:                ++inptr;
        !           239:                lnr = cvtlnr();
        !           240:                }
        !           241:        else
        !           242:                lnr = MINLNR;
        !           243:        endchk();
        !           244:        old(file);
        !           245:        curline = findline(lnr, NEXTLNR);
        !           246:        inptr = NULL;
        !           247: }
        !           248: 
        !           249: 
        !           250: /*
        !           251:  * notimpl --- report an unimplemented construct
        !           252:  */
        !           253: 
        !           254: notimpl()
        !           255: {
        !           256: 
        !           257:        err("not implemented");
        !           258: }

unix.superglobalmegacorp.com

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