[BACK]Return to load.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / parse

Annotation of OpenXM_contrib2/asir2000/parse/load.c, Revision 1.2

1.2     ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2000/parse/load.c,v 1.1.1.1 1999/12/03 07:39:12 noro Exp $ */
1.1       noro        2: #include "ca.h"
                      3: #include "parse.h"
                      4: #if defined(THINK_C)
                      5: #include <string.h>
                      6: #include        <StandardFile.h>
                      7: #include        <Windows.h>
                      8: #include        <OSUtils.h>
                      9: #include        <ToolUtils.h>
                     10: #else
                     11: #if defined(VISUAL)
                     12: #include <string.h>
                     13: #include <fcntl.h>
                     14: #if defined(GO32)
                     15: #include <unistd.h>
                     16: #endif
                     17: #include <sys/stat.h>
                     18: #else
                     19: #if defined(_PA_RISC1_1) || defined(SYSV) || defined(__SVR4)
                     20: #include <unistd.h>
                     21: #include <string.h>
                     22: #else
                     23: #include <strings.h>
                     24: #endif
                     25: #include <sys/types.h>
                     26: #include <sys/file.h>
                     27: #include <sys/stat.h>
                     28: #endif
                     29: #endif
                     30:
                     31: #if defined(linux)
                     32: #include <unistd.h>
                     33: #endif
                     34:
                     35: #if defined(VISUAL)
                     36: #include <io.h>
                     37: #endif
                     38:
                     39: #undef MALLOC
                     40: #define MALLOC(x) GC_malloc((x)+4)
                     41:
                     42: char *ASIRLOADPATH[32];
                     43:
                     44: void encrypt_file(char *,char *);
                     45: void decrypt_file(char *,char *);
                     46:
                     47: #if defined(THINK_C)
                     48: void searchasirpath(char *,char **);
                     49: void loadasirfile(char *);
                     50:
                     51: short initvol;
                     52:
                     53: void initVol();
                     54: short getVol();
                     55: void setDir(short);
                     56: void resetDir();
                     57:
                     58: void initVol() {
                     59:        char buf[BUFSIZ];
                     60:
                     61:        GetVol((StringPtr)buf,&initvol);
                     62: }
                     63:
                     64: short getVol() {
                     65:        short vol;
                     66:        char buf[BUFSIZ];
                     67:
                     68:        GetVol((StringPtr)buf,&vol);
                     69:        return vol;
                     70: }
                     71:
                     72: void setDir(short vol)
                     73: {
                     74:        SetVol(0,vol);
                     75: }
                     76:
                     77: void resetDir() {
                     78:        SetVol(0,initvol);
                     79: }
                     80: #endif
                     81:
                     82: #if defined(VISUAL)
                     83: #define ENVDELIM ';'
                     84: #define MORE "more < "
                     85: #else
                     86: #define ENVDELIM ':'
                     87: #define MORE "more"
                     88: #endif
                     89:
                     90: #ifndef ASIR_LIBDIR
                     91: #define ASIR_LIBDIR "."
                     92: #endif
                     93:
                     94: char *getenv();
                     95: extern char *asir_libdir;
                     96: extern char *asir_pager;
                     97: extern int read_exec_file;
                     98: extern int main_parser;
                     99:
                    100: void env_init() {
                    101:        char *e,*p,*q;
                    102:        int i,l;
                    103:        char *getenv();
                    104:        char *oxhome;
                    105:        char rootname[BUFSIZ];
                    106:
                    107:        if ( !(asir_libdir = getenv("ASIR_LIBDIR")) ) {
                    108:                if ( oxhome = getenv("OpenXM_HOME") ) {
                    109:                        asir_libdir = (char *)malloc(strlen(oxhome)+strlen("/lib/asir")+1);
                    110:                        sprintf(asir_libdir,"%s/lib/asir",oxhome);
                    111:                } else {
                    112: #if defined(VISUAL)
                    113:                        get_rootdir(rootname,sizeof(rootname));
                    114:                        asir_libdir = (char *)malloc(strlen(rootname)+strlen("/lib")+1);
                    115:                        sprintf(asir_libdir,"%s/lib",rootname);
                    116: #else
                    117:                        asir_libdir = (char *)malloc(strlen(ASIR_LIBDIR)+1);
                    118:                        strcpy(asir_libdir,ASIR_LIBDIR);
                    119: #endif
                    120:                }
                    121:        }
                    122:        if ( !(asir_pager = getenv("PAGER")) ) {
                    123:                asir_pager = (char *)malloc(strlen(MORE)+1);
                    124:                strcpy(asir_pager,MORE);
                    125:        }
                    126:        if ( e = getenv("ASIRLOADPATH" ) )
                    127:                for ( i = 0; ; i++, e = p+1 ) {
                    128:                        p = (char *)index(e,ENVDELIM);
                    129:                        l = p ? p-e : strlen(e); q = (char *)MALLOC(l+1);
                    130:                        if ( l ) {
                    131:                                strncpy(q,e,l); q[l] = 0; ASIRLOADPATH[i] = q;
                    132:                        }
                    133:                        if ( !p )
                    134:                                break;
                    135:                }
                    136:        for ( i = 0; ASIRLOADPATH[i]; i++ );
                    137:        ASIRLOADPATH[i] = asir_libdir;
                    138: }
                    139:
                    140: void searchasirpath(name,pathp)
                    141: char *name;
                    142: char **pathp;
                    143: {
                    144:        char **p;
                    145:        char *q;
                    146:        int l;
                    147: #if !defined(THINK_C) && !defined(VISUAL)
                    148:        struct stat sbuf;
                    149:
                    150:        if ( (name[0] == '/') || ( name[0] == '.') || strchr(name,':')
                    151:                || !ASIRLOADPATH[0] ) {
                    152:                if ( access(name,R_OK) >= 0 ) {
                    153:                        stat(name,&sbuf);
                    154:                        if ( (sbuf.st_mode & S_IFMT) != S_IFDIR )
                    155:                                *pathp = name;
                    156:                        else
                    157:                                *pathp = 0;
                    158:                } else
                    159:                        *pathp = 0;
                    160:        } else {
                    161:                for ( p = ASIRLOADPATH; *p; p++ ) {
                    162:                        l = strlen(*p)+strlen(name)+2;
                    163:                        q = (char *)ALLOCA(l); sprintf(q,"%s/%s",*p,name);
                    164:                        if ( access(q,R_OK) >= 0 ) {
                    165:                                stat(q,&sbuf);
                    166:                                if ( (sbuf.st_mode & S_IFMT) != S_IFDIR ) {
                    167:                                        *pathp = (char *)MALLOC(l); strcpy(*pathp,q);
                    168:                                        return;
                    169:                                }
                    170:                        }
                    171:                }
                    172:                *pathp = 0;
                    173:        }
                    174: #elif defined(VISUAL)
                    175:        if ( (name[0] == '/') || ( name[0] == '.') || strchr(name,':')
                    176:                || !ASIRLOADPATH[0] )
                    177:                *pathp = name;
                    178:        else {
                    179:                for ( p = ASIRLOADPATH; *p; p++ ) {
                    180:                        l = strlen(*p)+strlen(name)+2;
                    181:                        q = (char *)ALLOCA(l); sprintf(q,"%s/%s",*p,name);
                    182:                        if ( access(q,04) >= 0 ) {
                    183:                                *pathp = (char *)MALLOC(l); strcpy(*pathp,q);
                    184:                                return;
                    185:                        }
                    186:                }
                    187:                *pathp = 0;
                    188:        }
                    189: #else
                    190:        if ( (name[0] == '/') || ( name[0] == '.') || !ASIRLOADPATH[0] )
                    191:                *pathp = name;
                    192:        else {
                    193:                for ( p = ASIRLOADPATH; *p; p++ ) {
                    194:                        l = strlen(*p)+strlen(name)+2;
                    195:                        q = (char *)ALLOCA(l); sprintf(q,"%s/%s",*p,name);
                    196:                        *pathp = (char *)MALLOC(l); strcpy(*pathp,q);
                    197:                        return;
                    198:                }
                    199:                *pathp = 0;
                    200:        }
                    201: #endif
                    202: }
                    203:
                    204: #if defined(THINK_C)
                    205: #define DELIM ':'
                    206: #elif defined(VISUAL)
                    207: #define DELIM '/'
                    208: #endif
                    209:
                    210: void Eungetc(int,FILE *);
                    211:
                    212: void loadasirfile(name0)
                    213: char *name0;
                    214: {
                    215:        FILE *in;
                    216:        IN t;
                    217:        extern char cppname[];
                    218: #if defined(THINK_C) || defined(VISUAL)
                    219:        char tname[BUFSIZ],dname[BUFSIZ],ibuf1[BUFSIZ],ibuf2[BUFSIZ];
                    220:        int ac;
                    221: #if defined(__MWERKS__)
                    222:        char *av[64];
                    223: #else
                    224:        char *av[BUFSIZ];
                    225: #endif
                    226:        char *p,*c;
                    227:        FILE *fp;
                    228:        char name[BUFSIZ],tname0[BUFSIZ];
                    229:        int encoded;
                    230:
                    231: #if defined(VISUAL)
                    232:        fp = fopen(name0,"rb");
                    233:        if ( getc(fp) == 0xff ) {
                    234:                /* encoded file */
                    235:                fclose(fp);
                    236:                sprintf(name,"%s.$$$",name0);
                    237:                decrypt_file(name0,name);
                    238:                encoded = 1;
                    239:        } else {
                    240:                fclose(fp);
                    241:                strcpy(name,name0);
                    242:                encoded = 0;
                    243:        }
                    244: #else
                    245:        strcpy(name,name0);
                    246: #endif
                    247:
                    248:        strcpy(dname,name);
                    249:        p = strrchr(dname,DELIM);
                    250:        av[0] = "cpp"; av[1] = name; av[2] = tname;
                    251:        sprintf(ibuf1,"-I%s",asir_libdir); av[3] = ibuf1; av[4] = "-DWINDOWS";
                    252:        if ( !p ) {
                    253:                sprintf(tname,"%s.___",name); av[5] = 0; ac = 5;
                    254:        } else {
                    255:                *p++ = 0;
                    256: #if defined(VISUAL)
                    257:                if ( c = strchr(dname,':') ) {
                    258:                        *c = 0;
                    259:                        sprintf(tname,"%s:%s%c%s.___",dname,c+1,DELIM,p);
                    260:                        *c = ':';
                    261:                } else
                    262:
                    263: #endif
                    264:                sprintf(tname,"%s%c%s.___",dname,DELIM,p);
                    265:                sprintf(ibuf2,"-I%s",dname); av[5] = ibuf2; av[6] = 0; ac = 6;
                    266:        }
                    267:        cpp_main(ac,av);
                    268:        if ( encoded ) {
                    269:                unlink(name);
                    270:                strcpy(tname0,tname);
                    271:                sprintf(tname,"%s.###",tname0);
                    272:                encrypt_file(tname0,tname);
                    273:                unlink(tname0);
                    274:                in = fopen(tname,"rb");
                    275:        } else
                    276:                in = fopen(tname,"r");
                    277:        if ( !in ) {
                    278:                perror("fopen");
                    279:                error("load : failed");
                    280:        }
                    281:        t = (IN)MALLOC(sizeof(struct oIN));
                    282:        t->name = (char *)MALLOC(strlen(name)+1); strcpy(t->name,name);
                    283:        t->tname = (char *)MALLOC(strlen(tname)+1); strcpy(t->tname,tname);
                    284:        t->encoded = encoded;
                    285: #if defined(THINK_C)
                    286:        t->vol = getVol();
                    287: #endif
                    288: #else
                    289:        char com[BUFSIZ];
                    290:
                    291:        sprintf(com,"%s -I%s %s",cppname,asir_libdir,name0); in = popen(com,"r");
                    292:        if ( !in ) {
                    293:                perror("popen");
                    294:                error("load : failed");
                    295:        }
                    296:        t = (IN)MALLOC(sizeof(struct oIN));
                    297:        t->name = (char *)MALLOC(strlen(name0)+1); strcpy(t->name,name0);
                    298: #endif
                    299:        t->fp = in; t->ln = 1; t->next = asir_infile; asir_infile = t;
                    300:        main_parser = 1; /* XXX */
                    301:        Eungetc(afternl(),asir_infile->fp);
                    302:        if ( !EPVS->va )
                    303:                reallocarray((char **)&EPVS->va,(int *)&EPVS->asize,(int *)&EPVS->n,(int)sizeof(struct oPV));
                    304: }
                    305:
                    306: void execasirfile(name)
                    307: char *name;
                    308: {
                    309:        loadasirfile(name);
                    310:        read_exec_file = 1;
                    311:        read_eval_loop();
                    312:        read_exec_file = 0;
                    313: }
                    314:
                    315: static NODE objfile = 0;
                    316:
                    317: int loadfile(s)
                    318: char *s;
                    319: {
                    320:        FILE *in;
                    321:
                    322:        if ( in = fopen(s,"r") ) {
                    323:                fclose(in);
                    324:                loadasirfile(s);
                    325:                return 1;
                    326:        } else
                    327:                return 0;
                    328: }
                    329:
                    330: int loadfiles(node) NODE node; { return 0; }
                    331:
                    332: static unsigned char encrypt_tab[128][2] = {
                    333: {137,40},{1,194},{133,79},{48,20},{254,76},{98,17},{110,233},{19,231},
                    334: {55,223},{75,65},{178,151},{85,222},{201,46},{51,243},{235,61},{106,113},
                    335: {116,121},{24,205},{146,244},{89,234},{163,173},{140,162},{188,45},{195,132},
                    336: {202,94},{236,87},{2,127},{253,47},{211,138},{58,252},{142,190},{77,209},
                    337: {16,53},{200,220},{99,165},{28,59},{78,0},{208,248},{50,229},{217,39},
                    338: {112,15},{18,60},{108,175},{10,67},{23,176},{49,245},{160,27},{171,219},
                    339: {105,144},{122,172},{255,114},{226,100},{70,117},{197,11},{180,36},{136,101},
                    340: {238,212},{125,120},{103,199},{38,119},{7,206},{181,228},{62,3},{30,185},
                    341: {154,63},{247,81},{187,80},{225,56},{210,221},{37,135},{155,52},{54,153},
                    342: {84,246},{166,90},{124,167},{41,184},{145,204},{147,198},{25,21},{72,97},
                    343: {66,128},{95,139},{93,42},{224,43},{35,143},{111,13},{82,249},{12,148},
                    344: {32,152},{186,168},{177,115},{216,251},{5,131},{123,170},{149,161},{213,203},
                    345: {126,150},{88,158},{74,169},{159,182},{156,26},{34,104},{8,91},{207,183},
                    346: {9,64},{83,241},{134,102},{69,33},{179,237},{129,250},{14,71},{230,4},
                    347: {218,6},{189,68},{193,22},{57,174},{215,214},{130,92},{240,31},{118,239},
                    348: {109,192},{232,196},{86,107},{96,141},{157,227},{164,242},{44,191},{29,73}
                    349: };
                    350:
                    351: static unsigned char decrypt_tab[] = {
                    352: 36,1,26,62,111,92,112,60,102,104,43,53,87,85,110,40,
                    353: 32,5,41,7,3,78,114,44,17,78,100,46,35,127,63,118,
                    354: 88,107,101,84,54,69,59,39,0,75,82,83,126,22,12,27,
                    355: 3,45,38,13,70,32,71,8,67,115,29,35,41,14,62,64,
                    356: 104,9,80,43,113,107,52,110,79,127,98,9,4,31,36,2,
                    357: 66,65,86,105,72,11,122,25,97,19,73,102,117,82,24,81,
                    358: 123,79,5,34,51,55,106,58,101,48,15,122,42,120,6,85,
                    359: 40,15,50,90,16,52,119,59,57,16,49,93,74,57,96,26,
                    360: 80,109,117,92,23,2,106,69,55,0,28,81,21,123,30,84,
                    361: 48,76,18,77,87,94,96,10,88,71,64,70,100,124,97,99,
                    362: 46,94,21,20,125,34,73,74,89,98,93,47,49,20,115,42,
                    363: 44,90,10,108,54,61,99,103,75,63,89,66,22,113,30,126,
                    364: 120,114,1,23,121,53,77,58,33,12,24,95,76,17,60,103,
                    365: 37,31,68,28,56,95,116,116,91,39,112,47,33,68,11,8,
                    366: 83,67,51,124,61,38,111,7,121,6,19,14,25,108,56,119,
                    367: 118,105,125,13,18,45,72,65,37,86,109,91,29,27,4,50
                    368: };
                    369:
                    370: unsigned char encrypt_char(unsigned char c)
                    371: {
                    372:        return encrypt_tab[c][mt_genrand()&1];
                    373: }
                    374:
                    375: unsigned char decrypt_char(unsigned char c)
                    376: {
                    377:        return decrypt_tab[c];
                    378: }
                    379:
                    380: void encrypt_file(in,out)
                    381: char *in,*out;
                    382: {
                    383:        FILE *infp,*outfp;
                    384:        int c;
                    385:
                    386:        infp = fopen(in,"r");
                    387:        outfp = fopen(out,"wb");
                    388:        while ( 1 ) {
                    389:                c = getc(infp);
                    390:                if ( c == EOF )
                    391:                        break;
                    392:                putc(encrypt_char(c),outfp);
                    393:        }
                    394:        fclose(infp); fclose(outfp);
                    395: }
                    396:
                    397: void decrypt_file(in,out)
                    398: char *in,*out;
                    399: {
                    400:        FILE *infp,*outfp;
                    401:        int c;
                    402:
                    403:        infp = fopen(in,"rb");
                    404:        outfp = fopen(out,"w");
                    405:        /* skip the magic number (=0xff) */
                    406:        getc(infp);
                    407:        while ( 1 ) {
                    408:                c = getc(infp);
                    409:                if ( c == EOF )
                    410:                        break;
                    411:                putc(decrypt_char(c),outfp);
                    412:        }
                    413:        fclose(infp); fclose(outfp);
                    414: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>