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

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

unix.superglobalmegacorp.com

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