Annotation of researchv9/cmd/eqn/lex.c, revision 1.1

1.1     ! root        1: #include "e.h"
        !             2: #include "y.tab.h"
        !             3: #include "ctype.h"
        !             4: 
        !             5: #define        SSIZE   400
        !             6: char   token[SSIZE];
        !             7: int    sp;
        !             8: 
        !             9: yylex()
        !            10: {
        !            11:        register int c;
        !            12:        tbl *tp;
        !            13: 
        !            14:   begin:
        !            15:        while ((c=input()) == ' ' || c == '\n')
        !            16:                ;
        !            17:        yylval = c;
        !            18:        switch (c) {
        !            19:        case EOF:
        !            20:                error(!FATAL, "unexpected end of input inside equation");
        !            21:                return(EOF);
        !            22:        case '~':
        !            23:                return(SPACE);
        !            24:        case '^':
        !            25:                return(THIN);
        !            26:        case '\t':
        !            27:                return(TAB);
        !            28:        case '{':
        !            29:                return('{');
        !            30:        case '}':
        !            31:                return('}');
        !            32:        case '"':
        !            33:                for (sp = 0; (c=input())!='"' && c != '\n'; ) {
        !            34:                        if (c == '\\')
        !            35:                                if ((c = input()) != '"')
        !            36:                                        token[sp++] = '\\';
        !            37:                        token[sp++] = c;
        !            38:                        if (sp >= SSIZE)
        !            39:                                error(FATAL, "quoted string %.20s... too long", token);
        !            40:                }
        !            41:                token[sp] = '\0';
        !            42:                yylval = (int) &token[0];
        !            43:                if (c == '\n')
        !            44:                        error(!FATAL, "missing \" in %.20s", token);
        !            45:                return(QTEXT);
        !            46:        }
        !            47:        if (!display && c == righteq)
        !            48:                return(EOF);
        !            49: 
        !            50:        unput(c);
        !            51:        getstr(token, SSIZE);
        !            52:        dprintf(".\tlex token = |%s|\n", token);
        !            53:        if ((tp = lookup(deftbl, token, NULL)) != NULL) {       /* defined term */
        !            54:                c = input();
        !            55:                unput(c);
        !            56:                if (c == '(')   /* macro with args */
        !            57:                        dodef(tp);
        !            58:                else {          /* no args */
        !            59:                        unput(' ');
        !            60:                        pbstr(tp->defn);
        !            61:                        dprintf(".\tfound %s|=%s|\n", token, tp->defn);
        !            62:                }
        !            63:                goto begin;
        !            64:        }
        !            65: 
        !            66:        if ((tp = lookup(keytbl, token, NULL)) == NULL) /* not a keyword */
        !            67:                return CONTIG;
        !            68: 
        !            69:        switch ((int) tp->defn) {               /* some kind of keyword */
        !            70:        case DEFINE: case TDEFINE: case NDEFINE:
        !            71:                define(tp->defn);
        !            72:                break;
        !            73:        case IFDEF:
        !            74:                ifdef();
        !            75:                break;
        !            76:        case DELIM:
        !            77:                delim();
        !            78:                break;
        !            79:        case GSIZE:
        !            80:                globsize();
        !            81:                break;
        !            82:        case GFONT:
        !            83:                globfont();
        !            84:                break;
        !            85:        case INCLUDE:
        !            86:                include();
        !            87:                break;
        !            88:        case SPACE:
        !            89:                space();
        !            90:                break;
        !            91:        case DOTEQ:
        !            92:                        /* .EQ inside equation -- should warn if at bottom level */
        !            93:                break;
        !            94:        case DOTEN:
        !            95:                if (curfile == infile)
        !            96:                        return EOF;
        !            97:                /* else ignore nested .EN */
        !            98:                break;
        !            99:        default:
        !           100:                return (int) tp->defn;
        !           101:        }
        !           102:        goto begin;
        !           103: }
        !           104: 
        !           105: getstr(s, n)
        !           106:        char *s;
        !           107:        register int n;
        !           108: {
        !           109:        register int c;
        !           110:        register char *p;
        !           111: 
        !           112:        p = s;
        !           113:        while ((c = input()) == ' ' || c == '\n')
        !           114:                ;
        !           115:        if (c == EOF) {
        !           116:                *s = 0;
        !           117:                return;
        !           118:        }
        !           119:        while (c != ' ' && c != '\t' && c != '\n' && c != '{' && c != '}'
        !           120:            && c != '"' && c != '~' && c != '^') {
        !           121:                if (!display && c == righteq)
        !           122:                        break;
        !           123:                if (c == '(' && p > s) {        /* might be defined(...) */
        !           124:                        *p = '\0';
        !           125:                        if (lookup(deftbl, s, NULL) != NULL)
        !           126:                                break;
        !           127:                }
        !           128:                if (c == '\\')
        !           129:                        if ((c = input()) != '"')
        !           130:                                *p++ = '\\';
        !           131:                *p++ = c;
        !           132:                if (--n <= 0)
        !           133:                        error(FATAL, "token %.20s... too long", s);
        !           134:                c = input();
        !           135:        }
        !           136:        unput(c);
        !           137:        *p = '\0';
        !           138:        yylval = (int) s;
        !           139: }
        !           140: 
        !           141: cstr(s, quote, maxs)
        !           142:        char *s;
        !           143:        int quote;
        !           144: {
        !           145:        int del, c, i;
        !           146: 
        !           147:        s[0] = 0;
        !           148:        while ((del=input()) == ' ' || del == '\t')
        !           149:                ;
        !           150:        if (quote)
        !           151:                for (i=0; (c=input()) != del && c != EOF;) {
        !           152:                        s[i++] = c;
        !           153:                        if (i >= maxs)
        !           154:                                return(1);      /* disaster */
        !           155:                }
        !           156:        else {
        !           157:                if (del == '\n')
        !           158:                        return(1);
        !           159:                s[0] = del;
        !           160:                for (i=1; (c=input())!=' ' && c!= '\t' && c!='\n' && c!=EOF;) {
        !           161:                        s[i++] = c;
        !           162:                        if (i >= maxs)
        !           163:                                return(1);      /* disaster */
        !           164:                }
        !           165:        }
        !           166:        s[i] = '\0';
        !           167:        if (c == EOF)
        !           168:                error(FATAL, "Unexpected end of input at %.20s", s);
        !           169:        return(0);
        !           170: }
        !           171: 
        !           172: define(type)
        !           173:        int type;
        !           174: {
        !           175:        char *p1, *p2;
        !           176: 
        !           177:        getstr(token, SSIZE);   /* get name */
        !           178:        if (type != DEFINE) {
        !           179:                cstr(token, 1, SSIZE);  /* skip the definition too */
        !           180:                return;
        !           181:        }
        !           182:        p1 = strsave(token);
        !           183:        if (cstr(token, 1, SSIZE))
        !           184:                error(FATAL, "Unterminated definition at %.20s", token);
        !           185:        p2 = strsave(token);
        !           186:        lookup(deftbl, p1, p2);
        !           187:        dprintf(".\tname %s defined as %s\n", p1, p2);
        !           188: }
        !           189: 
        !           190: ifdef()                /* do body if name is defined */
        !           191: {
        !           192:        tbl *tp;
        !           193:        char name[100], *p;
        !           194: 
        !           195:        getstr(name, sizeof(name));     /* get name */
        !           196:        cstr(token, 1, SSIZE);          /* and body */
        !           197:        if ((tp = lookup(deftbl, name, NULL)) != NULL) {        /* found it */
        !           198:                p = strsave(token);
        !           199:                pushsrc(Free, p);
        !           200:                pushsrc(String, p);
        !           201:        }
        !           202: }
        !           203: 
        !           204: char   *spaceval       = NULL;
        !           205: 
        !           206: space()        /* collect line of form "space amt" to replace \x in output */
        !           207: {
        !           208:        getstr(token, SSIZE);
        !           209:        spaceval = strsave(token);
        !           210:        dprintf(".\tsetting spaceval to %s\n", token);
        !           211: }
        !           212: 
        !           213: char *strsave(s)
        !           214:        char *s;
        !           215: {
        !           216:        register char *q;
        !           217: 
        !           218:        q = malloc(strlen(s)+1);
        !           219:        if (q == NULL)
        !           220:                error(FATAL, "out of space in strsave on %s", s);
        !           221:        strcpy(q, s);
        !           222:        return(q);
        !           223: }
        !           224: 
        !           225: include()
        !           226: {
        !           227:        char name[100];
        !           228:        FILE *fin;
        !           229:        int c;
        !           230:        extern int errno;
        !           231: 
        !           232:        while ((c = input()) == ' ')
        !           233:                ;
        !           234:        unput(c);
        !           235:        cstr(name, c == '"', sizeof(name));     /* gets it quoted or not */
        !           236:        if ((fin = fopen(name, "r")) == NULL)
        !           237:                fatal("can't open file %s", name);
        !           238:        errno = 0;
        !           239:        curfile++;
        !           240:        curfile->fin = fin;
        !           241:        curfile->fname = strsave(name);
        !           242:        curfile->lineno = 0;
        !           243:        printf(".lf 1 %s\n", curfile->fname);
        !           244:        pushsrc(File, curfile);
        !           245: }
        !           246: 
        !           247: delim()
        !           248: {
        !           249:        yyval = eqnreg = 0;
        !           250:        if (cstr(token, 0, SSIZE))
        !           251:                error(FATAL, "Bizarre delimiters");
        !           252:        lefteq = token[0];
        !           253:        righteq = token[1];
        !           254:         if (!isprint(lefteq) || !isprint(righteq))
        !           255:                error(FATAL, "Bizarre delimiters");
        !           256:        if (lefteq == 'o' && righteq == 'f')
        !           257:                lefteq = righteq = '\0';
        !           258: }

unix.superglobalmegacorp.com

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