[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.7

1.3       noro        1: /*
                      2:  * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
                      3:  * All rights reserved.
                      4:  *
                      5:  * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
                      6:  * non-exclusive and royalty-free license to use, copy, modify and
                      7:  * redistribute, solely for non-commercial and non-profit purposes, the
                      8:  * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
                      9:  * conditions of this Agreement. For the avoidance of doubt, you acquire
                     10:  * only a limited right to use the SOFTWARE hereunder, and FLL or any
                     11:  * third party developer retains all rights, including but not limited to
                     12:  * copyrights, in and to the SOFTWARE.
                     13:  *
                     14:  * (1) FLL does not grant you a license in any way for commercial
                     15:  * purposes. You may use the SOFTWARE only for non-commercial and
                     16:  * non-profit purposes only, such as academic, research and internal
                     17:  * business use.
                     18:  * (2) The SOFTWARE is protected by the Copyright Law of Japan and
                     19:  * international copyright treaties. If you make copies of the SOFTWARE,
                     20:  * with or without modification, as permitted hereunder, you shall affix
                     21:  * to all such copies of the SOFTWARE the above copyright notice.
                     22:  * (3) An explicit reference to this SOFTWARE and its copyright owner
                     23:  * shall be made on your publication or presentation in any form of the
                     24:  * results obtained by use of the SOFTWARE.
                     25:  * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.4       noro       26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.3       noro       27:  * for such modification or the source code of the modified part of the
                     28:  * SOFTWARE.
                     29:  *
                     30:  * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
                     31:  * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
                     32:  * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
                     33:  * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
                     34:  * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
                     35:  * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
                     36:  * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
                     37:  * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
                     38:  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
                     39:  * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
                     40:  * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
                     41:  * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
                     42:  * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
                     43:  * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
                     44:  * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
                     45:  * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
                     46:  * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
                     47:  *
1.7     ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2000/parse/load.c,v 1.6 2001/08/20 09:03:27 noro Exp $
1.3       noro       49: */
1.1       noro       50: #include "ca.h"
                     51: #include "parse.h"
1.5       noro       52:
1.1       noro       53: #if defined(VISUAL)
                     54: #include <string.h>
                     55: #include <fcntl.h>
                     56: #include <sys/stat.h>
1.5       noro       57: #else /* VISUAL */
1.1       noro       58: #if defined(_PA_RISC1_1) || defined(SYSV) || defined(__SVR4)
                     59: #include <unistd.h>
                     60: #include <string.h>
                     61: #else
                     62: #include <strings.h>
                     63: #endif
                     64: #include <sys/types.h>
                     65: #include <sys/file.h>
                     66: #include <sys/stat.h>
                     67: #endif
                     68:
                     69: #if defined(linux)
                     70: #include <unistd.h>
                     71: #endif
                     72:
                     73: #if defined(VISUAL)
                     74: #include <io.h>
                     75: #endif
                     76:
                     77: #undef MALLOC
                     78: #define MALLOC(x) GC_malloc((x)+4)
                     79:
                     80: char *ASIRLOADPATH[32];
                     81:
                     82: void encrypt_file(char *,char *);
                     83: void decrypt_file(char *,char *);
                     84:
                     85: #if defined(VISUAL)
                     86: #define ENVDELIM ';'
                     87: #define MORE "more < "
                     88: #else
                     89: #define ENVDELIM ':'
                     90: #define MORE "more"
                     91: #endif
                     92:
                     93: #ifndef ASIR_LIBDIR
                     94: #define ASIR_LIBDIR "."
                     95: #endif
                     96:
                     97: char *getenv();
                     98: extern char *asir_libdir;
                     99: extern char *asir_pager;
                    100: extern int read_exec_file;
                    101: extern int main_parser;
1.6       noro      102: extern jmp_buf exec_env;
1.1       noro      103:
                    104: void env_init() {
                    105:        char *e,*p,*q;
                    106:        int i,l;
                    107:        char *getenv();
                    108:        char *oxhome;
                    109:        char rootname[BUFSIZ];
                    110:
                    111:        if ( !(asir_libdir = getenv("ASIR_LIBDIR")) ) {
                    112:                if ( oxhome = getenv("OpenXM_HOME") ) {
                    113:                        asir_libdir = (char *)malloc(strlen(oxhome)+strlen("/lib/asir")+1);
                    114:                        sprintf(asir_libdir,"%s/lib/asir",oxhome);
                    115:                } else {
                    116: #if defined(VISUAL)
                    117:                        get_rootdir(rootname,sizeof(rootname));
                    118:                        asir_libdir = (char *)malloc(strlen(rootname)+strlen("/lib")+1);
                    119:                        sprintf(asir_libdir,"%s/lib",rootname);
                    120: #else
                    121:                        asir_libdir = (char *)malloc(strlen(ASIR_LIBDIR)+1);
                    122:                        strcpy(asir_libdir,ASIR_LIBDIR);
                    123: #endif
                    124:                }
                    125:        }
                    126:        if ( !(asir_pager = getenv("PAGER")) ) {
                    127:                asir_pager = (char *)malloc(strlen(MORE)+1);
                    128:                strcpy(asir_pager,MORE);
                    129:        }
                    130:        if ( e = getenv("ASIRLOADPATH" ) )
                    131:                for ( i = 0; ; i++, e = p+1 ) {
                    132:                        p = (char *)index(e,ENVDELIM);
                    133:                        l = p ? p-e : strlen(e); q = (char *)MALLOC(l+1);
                    134:                        if ( l ) {
                    135:                                strncpy(q,e,l); q[l] = 0; ASIRLOADPATH[i] = q;
                    136:                        }
                    137:                        if ( !p )
                    138:                                break;
                    139:                }
                    140:        for ( i = 0; ASIRLOADPATH[i]; i++ );
                    141:        ASIRLOADPATH[i] = asir_libdir;
                    142: }
                    143:
                    144: void searchasirpath(name,pathp)
                    145: char *name;
                    146: char **pathp;
                    147: {
                    148:        char **p;
                    149:        char *q;
                    150:        int l;
1.5       noro      151: #if !defined(VISUAL)
1.1       noro      152:        struct stat sbuf;
                    153:
                    154:        if ( (name[0] == '/') || ( name[0] == '.') || strchr(name,':')
                    155:                || !ASIRLOADPATH[0] ) {
                    156:                if ( access(name,R_OK) >= 0 ) {
                    157:                        stat(name,&sbuf);
                    158:                        if ( (sbuf.st_mode & S_IFMT) != S_IFDIR )
                    159:                                *pathp = name;
                    160:                        else
                    161:                                *pathp = 0;
                    162:                } else
                    163:                        *pathp = 0;
                    164:        } else {
                    165:                for ( p = ASIRLOADPATH; *p; p++ ) {
                    166:                        l = strlen(*p)+strlen(name)+2;
                    167:                        q = (char *)ALLOCA(l); sprintf(q,"%s/%s",*p,name);
                    168:                        if ( access(q,R_OK) >= 0 ) {
                    169:                                stat(q,&sbuf);
                    170:                                if ( (sbuf.st_mode & S_IFMT) != S_IFDIR ) {
                    171:                                        *pathp = (char *)MALLOC(l); strcpy(*pathp,q);
                    172:                                        return;
                    173:                                }
                    174:                        }
                    175:                }
                    176:                *pathp = 0;
                    177:        }
1.5       noro      178: #else
1.1       noro      179:        if ( (name[0] == '/') || ( name[0] == '.') || strchr(name,':')
                    180:                || !ASIRLOADPATH[0] )
                    181:                *pathp = name;
                    182:        else {
                    183:                for ( p = ASIRLOADPATH; *p; p++ ) {
                    184:                        l = strlen(*p)+strlen(name)+2;
                    185:                        q = (char *)ALLOCA(l); sprintf(q,"%s/%s",*p,name);
                    186:                        if ( access(q,04) >= 0 ) {
                    187:                                *pathp = (char *)MALLOC(l); strcpy(*pathp,q);
                    188:                                return;
                    189:                        }
                    190:                }
                    191:                *pathp = 0;
                    192:        }
                    193: #endif
                    194: }
                    195:
                    196: #define DELIM '/'
                    197:
                    198: void Eungetc(int,FILE *);
                    199:
                    200: void loadasirfile(name0)
                    201: char *name0;
                    202: {
                    203:        FILE *in;
                    204:        IN t;
                    205:        extern char cppname[];
1.5       noro      206: #if defined(VISUAL)
                    207:        char ibuf1[BUFSIZ],ibuf2[BUFSIZ];
1.1       noro      208:        int ac;
                    209:        char *av[BUFSIZ];
                    210:        char *p,*c;
                    211:        FILE *fp;
1.5       noro      212:        char dname[BUFSIZ],tname0[BUFSIZ];
                    213:        char *name,*tname;
1.1       noro      214:        int encoded;
1.5       noro      215:        static char prefix[BUFSIZ];
                    216:        int process_id();
                    217:        char CppExe[BUFSIZ];
                    218:        char nbuf[BUFSIZ],tnbuf[BUFSIZ];
                    219:        STRING rootdir;
                    220:
                    221:        /* create the unique prefix */
                    222:        if ( !prefix[0] )
                    223:                sprintf(prefix,"asir%d",process_id());
1.1       noro      224:
                    225:        fp = fopen(name0,"rb");
                    226:        if ( getc(fp) == 0xff ) {
                    227:                /* encoded file */
                    228:                fclose(fp);
1.5       noro      229:                name = tempnam(NULL,prefix);
1.1       noro      230:                decrypt_file(name0,name);
1.5       noro      231:                /* the file 'name' created */
1.1       noro      232:                encoded = 1;
                    233:        } else {
                    234:                fclose(fp);
1.5       noro      235:                name = name0;
1.1       noro      236:                encoded = 0;
                    237:        }
                    238:
                    239:        strcpy(dname,name);
1.5       noro      240:        av[0] = "cpp";
                    241:        sprintf(nbuf,"\"%s\"",name);
                    242:        av[1] = nbuf;
                    243:        tname = tempnam(NULL,prefix);
                    244:        sprintf(tnbuf,"\"%s\"",tname);
                    245:        av[2] = tnbuf;
                    246:        sprintf(ibuf1,"-I\"%s\"",asir_libdir);
                    247:        av[3] = ibuf1;
                    248:        av[4] = "-DWINDOWS";
                    249:
                    250:        /* set the include directory */
1.1       noro      251:        p = strrchr(dname,DELIM);
                    252:        if ( !p ) {
1.5       noro      253:                av[5] = 0; ac = 5;
1.1       noro      254:        } else {
1.5       noro      255:                *p = 0;
                    256:                sprintf(ibuf2,"-I\"%s\"",dname);
                    257:                av[5] = ibuf2;
                    258:                av[6] = 0; ac = 6;
                    259:        }
                    260: //     cpp_main(ac,av);
                    261:        Pget_rootdir(&rootdir);
                    262:        sprintf(CppExe,"%s\\bin\\cpp.exe",BDY(rootdir));
                    263:        call_exe(CppExe,av);
1.1       noro      264:
1.5       noro      265:        /* the file tname created */
1.1       noro      266:        if ( encoded ) {
1.5       noro      267:                unlink(name); free(name);
                    268:
                    269:                strcpy(tname0,tname); free(tname);
                    270:                tname = tempnam(NULL,prefix);
                    271:
1.1       noro      272:                encrypt_file(tname0,tname);
1.5       noro      273:                /* the file tname created */
1.1       noro      274:                unlink(tname0);
1.5       noro      275:
1.1       noro      276:                in = fopen(tname,"rb");
                    277:        } else
                    278:                in = fopen(tname,"r");
                    279:        if ( !in ) {
                    280:                perror("fopen");
                    281:                error("load : failed");
                    282:        }
                    283:        t = (IN)MALLOC(sizeof(struct oIN));
1.5       noro      284:        t->name = (char *)MALLOC(strlen(name0)+1); strcpy(t->name,name0);
                    285:        t->tname = (char *)MALLOC(strlen(tname)+1); strcpy(t->tname,tname); free(tname);
1.1       noro      286:        t->encoded = encoded;
                    287: #else
                    288:        char com[BUFSIZ];
                    289:
                    290:        sprintf(com,"%s -I%s %s",cppname,asir_libdir,name0); in = popen(com,"r");
                    291:        if ( !in ) {
                    292:                perror("popen");
                    293:                error("load : failed");
                    294:        }
                    295:        t = (IN)MALLOC(sizeof(struct oIN));
                    296:        t->name = (char *)MALLOC(strlen(name0)+1); strcpy(t->name,name0);
                    297: #endif
                    298:        t->fp = in; t->ln = 1; t->next = asir_infile; asir_infile = t;
                    299:        main_parser = 1; /* XXX */
                    300:        Eungetc(afternl(),asir_infile->fp);
                    301:        if ( !EPVS->va )
                    302:                reallocarray((char **)&EPVS->va,(int *)&EPVS->asize,(int *)&EPVS->n,(int)sizeof(struct oPV));
                    303: }
                    304:
                    305: void execasirfile(name)
                    306: char *name;
                    307: {
                    308:        loadasirfile(name);
                    309:        read_exec_file = 1;
                    310:        read_eval_loop();
                    311:        read_exec_file = 0;
1.6       noro      312: }
                    313:
                    314: void load_and_execfile(name)
                    315: char *name;
                    316: {
                    317:        FILE *fp;
                    318:        IN save_asir_infile;
1.7     ! noro      319:        int save_prresult;
        !           320:        extern prresult;
1.6       noro      321:
                    322:        savepvs();
                    323:        save_asir_infile = asir_infile;
1.7     ! noro      324:        save_prresult = prresult;
1.6       noro      325:        fp = fopen(name,"rb");
                    326:        input_init(fp,name);
                    327:        if ( !setjmp(exec_env) ) {
                    328:                /* XXX : information for asir_teriminate() */
                    329:                read_exec_file = 2;
                    330:                read_eval_loop();
                    331:                read_exec_file = 0;
                    332:        }
                    333:        fclose(fp);
                    334:        restorepvs();
                    335:        asir_infile = save_asir_infile;
1.7     ! noro      336:        prresult = save_prresult;
1.1       noro      337: }
                    338:
                    339: static NODE objfile = 0;
                    340:
                    341: int loadfile(s)
                    342: char *s;
                    343: {
                    344:        FILE *in;
                    345:
                    346:        if ( in = fopen(s,"r") ) {
                    347:                fclose(in);
                    348:                loadasirfile(s);
                    349:                return 1;
                    350:        } else
                    351:                return 0;
                    352: }
                    353:
                    354: int loadfiles(node) NODE node; { return 0; }
                    355:
                    356: static unsigned char encrypt_tab[128][2] = {
                    357: {137,40},{1,194},{133,79},{48,20},{254,76},{98,17},{110,233},{19,231},
                    358: {55,223},{75,65},{178,151},{85,222},{201,46},{51,243},{235,61},{106,113},
                    359: {116,121},{24,205},{146,244},{89,234},{163,173},{140,162},{188,45},{195,132},
                    360: {202,94},{236,87},{2,127},{253,47},{211,138},{58,252},{142,190},{77,209},
                    361: {16,53},{200,220},{99,165},{28,59},{78,0},{208,248},{50,229},{217,39},
                    362: {112,15},{18,60},{108,175},{10,67},{23,176},{49,245},{160,27},{171,219},
                    363: {105,144},{122,172},{255,114},{226,100},{70,117},{197,11},{180,36},{136,101},
                    364: {238,212},{125,120},{103,199},{38,119},{7,206},{181,228},{62,3},{30,185},
                    365: {154,63},{247,81},{187,80},{225,56},{210,221},{37,135},{155,52},{54,153},
                    366: {84,246},{166,90},{124,167},{41,184},{145,204},{147,198},{25,21},{72,97},
                    367: {66,128},{95,139},{93,42},{224,43},{35,143},{111,13},{82,249},{12,148},
                    368: {32,152},{186,168},{177,115},{216,251},{5,131},{123,170},{149,161},{213,203},
                    369: {126,150},{88,158},{74,169},{159,182},{156,26},{34,104},{8,91},{207,183},
                    370: {9,64},{83,241},{134,102},{69,33},{179,237},{129,250},{14,71},{230,4},
                    371: {218,6},{189,68},{193,22},{57,174},{215,214},{130,92},{240,31},{118,239},
                    372: {109,192},{232,196},{86,107},{96,141},{157,227},{164,242},{44,191},{29,73}
                    373: };
                    374:
                    375: static unsigned char decrypt_tab[] = {
                    376: 36,1,26,62,111,92,112,60,102,104,43,53,87,85,110,40,
                    377: 32,5,41,7,3,78,114,44,17,78,100,46,35,127,63,118,
                    378: 88,107,101,84,54,69,59,39,0,75,82,83,126,22,12,27,
                    379: 3,45,38,13,70,32,71,8,67,115,29,35,41,14,62,64,
                    380: 104,9,80,43,113,107,52,110,79,127,98,9,4,31,36,2,
                    381: 66,65,86,105,72,11,122,25,97,19,73,102,117,82,24,81,
                    382: 123,79,5,34,51,55,106,58,101,48,15,122,42,120,6,85,
                    383: 40,15,50,90,16,52,119,59,57,16,49,93,74,57,96,26,
                    384: 80,109,117,92,23,2,106,69,55,0,28,81,21,123,30,84,
                    385: 48,76,18,77,87,94,96,10,88,71,64,70,100,124,97,99,
                    386: 46,94,21,20,125,34,73,74,89,98,93,47,49,20,115,42,
                    387: 44,90,10,108,54,61,99,103,75,63,89,66,22,113,30,126,
                    388: 120,114,1,23,121,53,77,58,33,12,24,95,76,17,60,103,
                    389: 37,31,68,28,56,95,116,116,91,39,112,47,33,68,11,8,
                    390: 83,67,51,124,61,38,111,7,121,6,19,14,25,108,56,119,
                    391: 118,105,125,13,18,45,72,65,37,86,109,91,29,27,4,50
                    392: };
                    393:
                    394: unsigned char encrypt_char(unsigned char c)
                    395: {
                    396:        return encrypt_tab[c][mt_genrand()&1];
                    397: }
                    398:
                    399: unsigned char decrypt_char(unsigned char c)
                    400: {
                    401:        return decrypt_tab[c];
                    402: }
                    403:
                    404: void encrypt_file(in,out)
                    405: char *in,*out;
                    406: {
                    407:        FILE *infp,*outfp;
                    408:        int c;
                    409:
                    410:        infp = fopen(in,"r");
                    411:        outfp = fopen(out,"wb");
                    412:        while ( 1 ) {
                    413:                c = getc(infp);
                    414:                if ( c == EOF )
                    415:                        break;
                    416:                putc(encrypt_char(c),outfp);
                    417:        }
                    418:        fclose(infp); fclose(outfp);
                    419: }
                    420:
                    421: void decrypt_file(in,out)
                    422: char *in,*out;
                    423: {
                    424:        FILE *infp,*outfp;
                    425:        int c;
                    426:
                    427:        infp = fopen(in,"rb");
                    428:        outfp = fopen(out,"w");
                    429:        /* skip the magic number (=0xff) */
                    430:        getc(infp);
                    431:        while ( 1 ) {
                    432:                c = getc(infp);
                    433:                if ( c == EOF )
                    434:                        break;
                    435:                putc(decrypt_char(c),outfp);
                    436:        }
                    437:        fclose(infp); fclose(outfp);
                    438: }

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