Annotation of MiNT/src/trutil.c, revision 1.1.1.1

1.1       root        1: #define YYSTYPE char *
                      2: 
                      3: #include "asmtrans.h"
                      4: 
                      5: #include "asmtab.h"
                      6: 
                      7: 
                      8: 
                      9: #define QUOTESYM '"'
                     10: 
                     11: #define CMDSYM '%'
                     12: 
                     13: 
                     14: 
                     15: FILE *infile, *outfile;
                     16: 
                     17: 
                     18: 
                     19: #define MAXNEST 10
                     20: 
                     21: int hidecnt = 0;
                     22: 
                     23: int ifstack[MAXNEST], ifstkptr;
                     24: 
                     25: 
                     26: 
                     27: void emit(s)
                     28: 
                     29:        char *s;
                     30: 
                     31: {
                     32: 
                     33:        if (hidecnt == 0)
                     34: 
                     35:                fputs(s, outfile);
                     36: 
                     37:        free(s);
                     38: 
                     39: }
                     40: 
                     41: 
                     42: 
                     43: char *concat(s1, s2)
                     44: 
                     45:        char *s1, *s2;
                     46: 
                     47: {
                     48: 
                     49:        size_t siz = strlen(s1) + strlen(s2) + 1;
                     50: 
                     51:        char *r;
                     52: 
                     53: 
                     54: 
                     55:        r = malloc(siz);
                     56: 
                     57:        if (!r) return 0;
                     58: 
                     59: 
                     60: 
                     61:        strcpy(r, s1);
                     62: 
                     63:        strcat(r, s2);
                     64: 
                     65:        return r;
                     66: 
                     67: }
                     68: 
                     69: 
                     70: 
                     71: char *concat3(s1, s2, s3)
                     72: 
                     73:        char *s1, *s2, *s3;
                     74: 
                     75: {
                     76: 
                     77:        size_t siz = strlen(s1) + strlen(s2) + strlen(s3) + 1;
                     78: 
                     79:        char *r;
                     80: 
                     81: 
                     82: 
                     83:        r = malloc(siz);
                     84: 
                     85:        if (!r) return 0;
                     86: 
                     87: 
                     88: 
                     89:        strcpy(r, s1);
                     90: 
                     91:        strcat(r, s2);
                     92: 
                     93:        strcat(r, s3);
                     94: 
                     95:        return r;
                     96: 
                     97: }
                     98: 
                     99: 
                    100: 
                    101: char *concat4(s1, s2, s3, s4)
                    102: 
                    103:        char *s1, *s2, *s3, *s4;
                    104: 
                    105: {
                    106: 
                    107:        size_t siz = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
                    108: 
                    109:        char *r;
                    110: 
                    111: 
                    112: 
                    113:        r = malloc(siz);
                    114: 
                    115:        if (!r) return 0;
                    116: 
                    117: 
                    118: 
                    119:        strcpy(r, s1);
                    120: 
                    121:        strcat(r, s2);
                    122: 
                    123:        strcat(r, s3);
                    124: 
                    125:        strcat(r, s4);
                    126: 
                    127:        return r;
                    128: 
                    129: }
                    130: 
                    131: 
                    132: 
                    133: char *concat5(s1, s2, s3, s4, s5)
                    134: 
                    135:        char *s1, *s2, *s3, *s4, *s5;
                    136: 
                    137: {
                    138: 
                    139:        size_t siz = strlen(s1) + strlen(s2) + strlen(s3)
                    140: 
                    141:                        + strlen(s4) + strlen(s5) + 1;
                    142: 
                    143:        char *r;
                    144: 
                    145: 
                    146: 
                    147:        r = malloc(siz);
                    148: 
                    149:        if (!r) return 0;
                    150: 
                    151: 
                    152: 
                    153:        strcpy(r, s1);
                    154: 
                    155:        strcat(r, s2);
                    156: 
                    157:        strcat(r, s3);
                    158: 
                    159:        strcat(r, s4);
                    160: 
                    161:        strcat(r, s5);
                    162: 
                    163:        return r;
                    164: 
                    165: }
                    166: 
                    167: 
                    168: 
                    169: char *concat6(s1, s2, s3, s4, s5, s6)
                    170: 
                    171:        char *s1, *s2, *s3, *s4, *s5, *s6;
                    172: 
                    173: {
                    174: 
                    175:        size_t siz = strlen(s1) + strlen(s2) + strlen(s3)
                    176: 
                    177:                        + strlen(s4) + strlen(s5) + strlen(s6) + 1;
                    178: 
                    179:        char *r;
                    180: 
                    181: 
                    182: 
                    183:        r = malloc(siz);
                    184: 
                    185:        if (!r) return 0;
                    186: 
                    187: 
                    188: 
                    189:        strcpy(r, s1);
                    190: 
                    191:        strcat(r, s2);
                    192: 
                    193:        strcat(r, s3);
                    194: 
                    195:        strcat(r, s4);
                    196: 
                    197:        strcat(r, s5);
                    198: 
                    199:        strcat(r, s6);
                    200: 
                    201:        return r;
                    202: 
                    203: }
                    204: 
                    205: 
                    206: 
                    207: static int is_word_sym(c)
                    208: 
                    209:        int c;
                    210: 
                    211: {
                    212: 
                    213:        if (c == '.' || (c >= '0' && c <= '9')) return 1;
                    214: 
                    215:        if (c >= 'a' && c <= 'z') return 1;
                    216: 
                    217:        if (c >= 'A' && c <= 'Z') return 1;
                    218: 
                    219:        if (c == '_') return 1;
                    220: 
                    221:        return 0;
                    222: 
                    223: }
                    224: 
                    225: 
                    226: 
                    227: typedef struct wordtab {
                    228: 
                    229:        char *word;
                    230: 
                    231:        char *defn;
                    232: 
                    233:        struct wordtab *next;
                    234: 
                    235: } WORDTAB;
                    236: 
                    237: 
                    238: 
                    239: WORDTAB *globaltab;
                    240: 
                    241: 
                    242: 
                    243: void
                    244: 
                    245: do_define(word, defn)
                    246: 
                    247:        char *word, *defn;
                    248: 
                    249: {
                    250: 
                    251:        WORDTAB *w;
                    252: 
                    253: 
                    254: 
                    255:        w = malloc(sizeof(WORDTAB));
                    256: 
                    257:        if (!w) return;
                    258: 
                    259:        w->word = strdup(word);
                    260: 
                    261:        w->defn = strdup(defn);
                    262: 
                    263:        w->next = globaltab;
                    264: 
                    265:        globaltab = w;
                    266: 
                    267: }
                    268: 
                    269: 
                    270: 
                    271: /*
                    272: 
                    273:  * if we were actually using this program for
                    274: 
                    275:  * large things, we would use a hash table
                    276: 
                    277:  * to speed up the table lookup; but for the
                    278: 
                    279:  * uses we have, there aren't likely to be
                    280: 
                    281:  * many defines
                    282: 
                    283:  */
                    284: 
                    285: 
                    286: 
                    287: char *
                    288: 
                    289: wordlookup(which)
                    290: 
                    291:        char *which;
                    292: 
                    293: {
                    294: 
                    295:        WORDTAB *w;
                    296: 
                    297: 
                    298: 
                    299:        for (w = globaltab; w; w = w->next) {
                    300: 
                    301:                if (!strcmp(which, w->word)) {
                    302: 
                    303:                        return strdup(w->defn);
                    304: 
                    305:                }
                    306: 
                    307:        }
                    308: 
                    309:        return strdup(which);
                    310: 
                    311: }
                    312: 
                    313: 
                    314: 
                    315: void
                    316: 
                    317: do_ifdef(which)
                    318: 
                    319:        char *which;
                    320: 
                    321: {
                    322: 
                    323:        int output = 0;
                    324: 
                    325:        WORDTAB *w;
                    326: 
                    327: 
                    328: 
                    329:        for (w = globaltab; w; w = w->next) {
                    330: 
                    331:                if (!strcmp(which, w->word)) {
                    332: 
                    333:                        output = 1;
                    334: 
                    335:                        break;
                    336: 
                    337:                }
                    338: 
                    339:        }
                    340: 
                    341:        if (ifstkptr < MAXNEST) {
                    342: 
                    343:                ifstack[ifstkptr++] = output;
                    344: 
                    345:                if (!output)
                    346: 
                    347:                        hidecnt++;
                    348: 
                    349:        } else {
                    350: 
                    351:                ifstkptr++;
                    352: 
                    353:                yyerror("too many levels of %ifdef");
                    354: 
                    355:        }
                    356: 
                    357: }
                    358: 
                    359: 
                    360: 
                    361: void
                    362: 
                    363: do_ifndef(which)
                    364: 
                    365:        char *which;
                    366: 
                    367: {
                    368: 
                    369:        int output = 1;
                    370: 
                    371:        WORDTAB *w;
                    372: 
                    373: 
                    374: 
                    375:        for (w = globaltab; w; w = w->next) {
                    376: 
                    377:                if (!strcmp(which, w->word)) {
                    378: 
                    379:                        output = 0;
                    380: 
                    381:                        break;
                    382: 
                    383:                }
                    384: 
                    385:        }
                    386: 
                    387:        if (ifstkptr < MAXNEST) {
                    388: 
                    389:                ifstack[ifstkptr++] = output;
                    390: 
                    391:                if (!output)
                    392: 
                    393:                        hidecnt++;
                    394: 
                    395:        } else {
                    396: 
                    397:                ifstkptr++;
                    398: 
                    399:                yyerror("too many levels of %ifdef");
                    400: 
                    401:        }
                    402: 
                    403: }
                    404: 
                    405: 
                    406: 
                    407: void
                    408: 
                    409: do_else()
                    410: 
                    411: {
                    412: 
                    413:        int output;
                    414: 
                    415: 
                    416: 
                    417:        if (ifstkptr == 0) {
                    418: 
                    419:                yyerror("%else without %ifdef");
                    420: 
                    421:        } else {
                    422: 
                    423:                if (ifstkptr > MAXNEST) return;
                    424: 
                    425:        /* if we were outputting, stop */
                    426: 
                    427:        /* otherwise, start again */
                    428: 
                    429:                output = !ifstack[ifstkptr-1];
                    430: 
                    431:                if (output)
                    432: 
                    433:                        hidecnt--;
                    434: 
                    435:                else
                    436: 
                    437:                        hidecnt++;
                    438: 
                    439:                ifstack[ifstkptr-1] = output;
                    440: 
                    441:        }
                    442: 
                    443: }
                    444: 
                    445: 
                    446: 
                    447: void
                    448: 
                    449: do_endif()
                    450: 
                    451: {
                    452: 
                    453:        int output;
                    454: 
                    455: 
                    456: 
                    457:        if (ifstkptr == 0) {
                    458: 
                    459:                yyerror("%endif without %ifdef");
                    460: 
                    461:        } else {
                    462: 
                    463:                ifstkptr--;
                    464: 
                    465:                if (ifstkptr >= MAXNEST) return;
                    466: 
                    467: 
                    468: 
                    469:                output = ifstack[ifstkptr];
                    470: 
                    471:                if (!output)
                    472: 
                    473:                        hidecnt--;
                    474: 
                    475:        }
                    476: 
                    477: }
                    478: 
                    479: 
                    480: 
                    481: /* this is a terrible hack to remove the leading
                    482: 
                    483:  * '_' from labels...
                    484: 
                    485:  */
                    486: 
                    487: 
                    488: 
                    489: char *
                    490: 
                    491: fixupword(w)
                    492: 
                    493:        char *w;
                    494: 
                    495: {
                    496: 
                    497:        if (*w == '_' && syntax == PUREC)
                    498: 
                    499:                return strdup(w+1);
                    500: 
                    501:        else
                    502: 
                    503:                return strdup(w);
                    504: 
                    505: }
                    506: 
                    507: 
                    508: 
                    509: 
                    510: 
                    511: static char footext[1024];
                    512: 
                    513: 
                    514: 
                    515: int
                    516: 
                    517: yylex()
                    518: 
                    519: {
                    520: 
                    521:        int c;
                    522: 
                    523:        char *to = footext;
                    524: 
                    525:        int cmdword;
                    526: 
                    527:        static int saweoln = 1;
                    528: 
                    529: 
                    530: 
                    531:        c = getc(infile);
                    532: 
                    533: 
                    534: 
                    535:        if (c < 0) {
                    536: 
                    537: doeof:
                    538: 
                    539:                saweoln = 1;
                    540: 
                    541:                return 0;
                    542: 
                    543:        }
                    544: 
                    545:        if (c == ';') {
                    546: 
                    547: docomment:
                    548: 
                    549:                if (syntax == GAS)
                    550: 
                    551:                        c = '|';
                    552: 
                    553:                *to++ = c;
                    554: 
                    555:                do {
                    556: 
                    557:                        c = getc(infile);
                    558: 
                    559:                        if (c < 0) return 0;
                    560: 
                    561:                        if (c != '\r')
                    562: 
                    563:                                *to++ = c;
                    564: 
                    565:                } while (c != '\n');
                    566: 
                    567:                *to = 0;
                    568: 
                    569:                yylval = strdup(footext);
                    570: 
                    571:                saweoln = 1;
                    572: 
                    573:                return EOLN;
                    574: 
                    575:        }
                    576: 
                    577:        if (c == '\n') {
                    578: 
                    579: doeoln:
                    580: 
                    581:                *to++ = c;
                    582: 
                    583:                *to = 0;
                    584: 
                    585:                yylval = strdup(footext);
                    586: 
                    587:                saweoln = 1;
                    588: 
                    589:                return EOLN;
                    590: 
                    591:        }
                    592: 
                    593:        if (c == CMDSYM && saweoln) {
                    594: 
                    595:                cmdword = 1;
                    596: 
                    597:                c = getc(infile);
                    598: 
                    599:        } else {
                    600: 
                    601:                cmdword = 0;
                    602: 
                    603:        }
                    604: 
                    605: 
                    606: 
                    607:        if (c == ' ' || c == '\t' || c == '\r') {
                    608: 
                    609:                do {
                    610: 
                    611:                        if (c == '\r')
                    612: 
                    613:                                c = ' ';
                    614: 
                    615:                        *to++ = c;
                    616: 
                    617:                        c = getc(infile);
                    618: 
                    619:                } while (c == ' ' || c == '\t');
                    620: 
                    621:                if (c == '\n') goto doeoln;
                    622: 
                    623:                if (c == ';') goto docomment;
                    624: 
                    625:                if (!cmdword) {
                    626: 
                    627:                        ungetc(c, infile);
                    628: 
                    629:                        *to = 0;
                    630: 
                    631:                        yylval = strdup(footext);
                    632: 
                    633:                        return WHITESP;
                    634: 
                    635:                } else {
                    636: 
                    637:                        to = footext;
                    638: 
                    639:                }
                    640: 
                    641:        }
                    642: 
                    643: 
                    644: 
                    645:        saweoln = 0;
                    646: 
                    647: 
                    648: 
                    649:        if (c == QUOTESYM) {
                    650: 
                    651:                for(;;) {
                    652: 
                    653:                        c = getc(infile);
                    654: 
                    655:                        if (c < 0) goto doeof;
                    656: 
                    657:                        if (c == QUOTESYM) break;
                    658: 
                    659:                        *to++ = c;
                    660: 
                    661:                }
                    662: 
                    663:                *to = 0;
                    664: 
                    665:                yylval = strdup(footext);
                    666: 
                    667:                return STRING;
                    668: 
                    669:        }
                    670: 
                    671:        if (is_word_sym(c)) {
                    672: 
                    673:                do {
                    674: 
                    675:                        *to++ = c;
                    676: 
                    677:                        c = getc(infile);
                    678: 
                    679:                } while (is_word_sym(c));
                    680: 
                    681:                ungetc(c, infile);
                    682: 
                    683:                *to = 0;
                    684: 
                    685:                if (cmdword) {
                    686: 
                    687:                        yylval = footext;
                    688: 
                    689:                        if (!strcmp(footext, "define")) {
                    690: 
                    691:                                return DEFINECMD;
                    692: 
                    693:                        } else if (!strcmp(footext, "include")) {
                    694: 
                    695:                                return INCLUDECMD;
                    696: 
                    697:                        } else if (!strcmp(footext, "ifdef")) {
                    698: 
                    699:                                return IFDEFCMD;
                    700: 
                    701:                        } else if (!strcmp(footext, "ifndef")) {
                    702: 
                    703:                                return IFNDEFCMD;
                    704: 
                    705:                        } else if (!strcmp(footext, "else")) {
                    706: 
                    707:                                return ELSECMD;
                    708: 
                    709:                        } else if (!strcmp(footext, "endif")) {
                    710: 
                    711:                                return ENDIFCMD;
                    712: 
                    713:                        } else {
                    714: 
                    715:                                fprintf(stderr, "Unknown command: %s\n", footext);
                    716: 
                    717:                        }
                    718: 
                    719:                }
                    720: 
                    721:                yylval = fixupword(footext);
                    722: 
                    723:                return WORD;
                    724: 
                    725:        }
                    726: 
                    727: 
                    728: 
                    729:        *to++ = c;
                    730: 
                    731:        *to = 0;
                    732: 
                    733:        yylval = footext;
                    734: 
                    735:        return c;
                    736: 
                    737: }
                    738: 

unix.superglobalmegacorp.com

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