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

Annotation of OpenXM_contrib2/asir2000/builtin/file.c, Revision 1.7

1.4       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.5       noro       26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.4       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/builtin/file.c,v 1.6 2000/08/25 08:06:19 noro Exp $
1.4       noro       49: */
1.1       noro       50: #include "ca.h"
                     51: #include "parse.h"
                     52: #include "base.h"
1.2       noro       53: #include "unistd.h"
                     54: #if PARI
1.1       noro       55: #include "genpari.h"
1.2       noro       56: #endif
1.1       noro       57:
                     58: #if defined(VISUAL)
                     59: #include <windows.h>
                     60: /* #define ECGEN_KEYNAME "SoftWare\\Fujitsu\\WinECgen\\1.00.000" */
                     61: #define ECGEN_KEYNAME "SoftWare\\Fujitsu\\FSEcParamGen\\V1.0L10"
                     62: #define ASIR_KEYNAME "SoftWare\\Fujitsu\\Asir\\1999.03.31"
                     63: #endif
                     64:
                     65: void Pget_rootdir();
                     66: void Paccess(),Premove_file();
                     67: void Pbsave_enc(), Pbload_enc();
                     68:
                     69: void Pload(), Pwhich(), Ploadfiles(), Poutput();
                     70: void Pbsave(), Pbload(), Pbload27();
                     71: void Pbsave_compat(), Pbload_compat();
                     72: void Pbsave_cmo(), Pbload_cmo();
1.3       noro       73: void Popen_file(), Pclose_file(), Pget_line();
1.1       noro       74:
                     75: extern int des_encryption;
                     76: extern char *asir_libdir;
                     77:
                     78: struct ftab file_tab[] = {
1.3       noro       79:        {"open_file",Popen_file,1},
                     80:        {"close_file",Pclose_file,1},
                     81:        {"get_line",Pget_line,1},
1.1       noro       82:        {"remove_file",Premove_file,1},
                     83:        {"access",Paccess,1},
                     84:        {"load",Pload,-1},
                     85:        {"which",Pwhich,1},
                     86:        {"loadfiles",Ploadfiles,1},
                     87:        {"output",Poutput,-1},
                     88:        {"bsave",Pbsave,2},
                     89:        {"bload",Pbload,1},
                     90:        {"get_rootdir",Pget_rootdir,0},
1.6       noro       91: #if defined(VISUAL) && defined(DES_ENC)
1.1       noro       92:        {"bsave_enc",Pbsave_enc,2},
                     93:        {"bload_enc",Pbload_enc,1},
1.6       noro       94: #endif
1.1       noro       95:        {"bload27",Pbload27,1},
                     96:        {"bsave_compat",Pbsave_compat,2},
                     97:        {"bload_compat",Pbload_compat,1},
                     98:        {"bsave_cmo",Pbsave_cmo,2},
                     99:        {"bload_cmo",Pbload_cmo,1},
                    100:        {0,0,0},
                    101: };
1.3       noro      102:
                    103: static FILE *file_ptrs[BUFSIZ];
                    104:
                    105: void Popen_file(arg,rp)
                    106: NODE arg;
                    107: Q *rp;
                    108: {
                    109:        char *name;
                    110:        FILE *fp;
                    111:        char errbuf[BUFSIZ];
                    112:        int i;
                    113:
                    114:        asir_assert(ARG0(arg),O_STR,"open_file");
                    115:        for ( i = 0; i < BUFSIZ && file_ptrs[i]; i++ );
                    116:        if ( i == BUFSIZ )
                    117:                error("open_file : too many open files");
                    118:        name = BDY((STRING)ARG0(arg));
                    119:        fp = fopen(name,"r");
                    120:        if ( !fp ) {
                    121:                sprintf(errbuf,"open_file : \"%s\" not found",name);
                    122:                error(errbuf);
                    123:        }
                    124:        file_ptrs[i] = fp;
                    125:        STOQ(i,*rp);
                    126: }
                    127:
                    128: void Pclose_file(arg,rp)
                    129: NODE arg;
                    130: Q *rp;
                    131: {
                    132:        int i;
                    133:
                    134:        asir_assert(ARG0(arg),O_N,"open_file");
                    135:        i = QTOS((Q)ARG0(arg));
                    136:        if ( file_ptrs[i] ) {
                    137:                fclose(file_ptrs[i]);
                    138:                file_ptrs[i] = 0;
                    139:        } else
                    140:                error("close_file : invalid argument");
                    141:        *rp = ONE;
                    142: }
                    143:
                    144: void Pget_line(arg,rp)
                    145: NODE arg;
                    146: STRING *rp;
                    147: {
                    148:        int i,j,c;
                    149:        FILE *fp;
                    150:        fpos_t head;
                    151:        char *str;
                    152:
                    153:        asir_assert(ARG0(arg),O_N,"open_file");
                    154:        i = QTOS((Q)ARG0(arg));
                    155:        if ( fp = file_ptrs[i] ) {
                    156:                if ( feof(fp) ) {
                    157:                        *rp = 0;
                    158:                        return;
                    159:                }
                    160:                fgetpos(fp,&head);
                    161:                j = 0;
                    162:                while ( 1 ) {
                    163:                        c = getc(fp);
                    164:                        if ( c == EOF ) {
                    165:                                if ( !j ) {
                    166:                                        *rp = 0;
                    167:                                        return;
                    168:                                } else
                    169:                                        break;
                    170:                        }
                    171:                        j++;
                    172:                        if ( c == '\n' )
                    173:                                break;
                    174:                }
                    175:                fsetpos(fp,&head);
                    176:                str = (char *)MALLOC_ATOMIC(j+1);
                    177:                fgets(str,j+1,fp);
                    178:                MKSTR(*rp,str);
                    179:        } else
                    180:                error("get_line : invalid argument");
                    181: }
1.1       noro      182:
                    183: void Pload(arg,rp)
                    184: NODE arg;
                    185: Q *rp;
                    186: {
                    187:        int ret = 0;
                    188:        char *name,*name0;
                    189:        char errbuf[BUFSIZ];
                    190:
                    191: #if defined THINK_C
                    192:        if ( !arg ) {
                    193:                ret = finder_loadfile();
                    194:        } else
                    195: #endif
                    196:        if ( ARG0(arg) ) {
                    197:                switch (OID(ARG0(arg))) {
                    198:                        case O_STR:
                    199:                                name0 = BDY((STRING)ARG0(arg));
                    200:                                searchasirpath(name0,&name);
                    201:                                if ( !name ) {
                    202:                                        sprintf(errbuf,"load : \"%s\" not found in the search path",name0);
                    203:                                        error(errbuf);
                    204:                                }
                    205:                                ret = loadfile(name);
                    206:                                if ( !ret ) {
                    207:                                        sprintf(errbuf,"load : \"%s\" could not be loaded",name);
                    208:                                        error(errbuf);
                    209:                                }
                    210:                                break;
                    211:                        default:
                    212:                                error("load : invalid argument");
                    213:                                break;
                    214:                }
                    215:        }
                    216:        STOQ(ret,*rp);
                    217: }
                    218:
                    219: void Pwhich(arg,rp)
                    220: NODE arg;
                    221: STRING *rp;
                    222: {
                    223:        char *name;
                    224:        STRING str;
                    225:
                    226:        switch (OID(ARG0(arg))) {
                    227:                case O_STR:
                    228:                        searchasirpath(BDY((STRING)ARG0(arg)),&name);
                    229:                        break;
                    230:                default:
                    231:                        name = 0;
                    232:                        break;
                    233:        }
                    234:        if ( name ) {
                    235:                MKSTR(str,name); *rp = str;
                    236:        } else
                    237:                *rp = 0;
                    238: }
                    239:
                    240: void Ploadfiles(arg,rp)
                    241: NODE arg;
                    242: Q *rp;
                    243: {
                    244:        int ret;
                    245:
                    246:        if ( ARG0(arg) )
                    247:                if ( OID(ARG0(arg)) != O_LIST )
                    248:                        ret = 0;
                    249:                else
                    250:                        ret = loadfiles(BDY((LIST)ARG0(arg)));
                    251:        else
                    252:                ret = 0;
                    253:        STOQ(ret,*rp);
                    254: }
                    255:
                    256: void Poutput(arg,rp)
                    257: NODE arg;
                    258: Q *rp;
                    259: {
                    260: #if PARI
                    261:        extern FILE *outfile;
                    262: #endif
                    263:        FILE *fp;
                    264:
                    265:        fflush(asir_out);
                    266:        if ( asir_out != stdout )
                    267:                fclose(asir_out);
                    268:        switch ( argc(arg) ) {
                    269:                case 0:
                    270:                        fp = stdout; break;
                    271:                case 1:
                    272:                        asir_assert(ARG0(arg),O_STR,"output");
                    273:                        fp = fopen(((STRING)ARG0(arg))->body,"a+");
                    274:                        if ( !fp )
                    275:                                error("output : invalid filename");
                    276:                        break;
                    277:        }
                    278: #if PARI
                    279:        pari_outfile =
                    280: #endif
                    281:        asir_out = fp;
                    282:        *rp = ONE;
                    283: }
                    284:
                    285: extern int ox_file_io;
                    286:
                    287: void Pbsave(arg,rp)
                    288: NODE arg;
                    289: Q *rp;
                    290: {
                    291:        FILE *fp;
                    292:        VL vl,t;
                    293:
                    294:        asir_assert(ARG1(arg),O_STR,"bsave");
                    295:        get_vars_recursive(ARG0(arg),&vl);
                    296:        for ( t = vl; t; t = NEXT(t) )
                    297:                if ( t->v->attr == (pointer)V_UC )
                    298:                        error("bsave : not implemented");
                    299: #if defined(THINK_C) || defined(VISUAL)
                    300:        fp = fopen(BDY((STRING)ARG1(arg)),"wb");
                    301: #else
                    302:        fp = fopen(BDY((STRING)ARG1(arg)),"w");
                    303: #endif
                    304:        if ( !fp )
                    305:                error("bsave : invalid filename");
                    306:        ox_file_io = 1; /* network byte order is used */
                    307:        savevl(fp,vl);
                    308:        saveobj(fp,ARG0(arg));
                    309:        fclose(fp);
                    310:        ox_file_io = 0;
                    311:        *rp = ONE;
                    312: }
                    313:
                    314: void Pbload(arg,rp)
                    315: NODE arg;
                    316: Obj *rp;
                    317: {
                    318:        FILE *fp;
                    319:
                    320:        asir_assert(ARG0(arg),O_STR,"bload");
                    321: #if defined(THINK_C) || defined(VISUAL)
                    322:        fp = fopen(BDY((STRING)ARG0(arg)),"rb");
                    323: #else
                    324:        fp = fopen(BDY((STRING)ARG0(arg)),"r");
                    325: #endif
                    326:        if ( !fp )
                    327:                error("bload : invalid filename");
                    328:        ox_file_io = 1; /* network byte order is used */
                    329:        loadvl(fp);
                    330:        loadobj(fp,rp);
                    331:        fclose(fp);
                    332:        ox_file_io = 0;
                    333: }
                    334:
                    335: void Pbsave_cmo(arg,rp)
                    336: NODE arg;
                    337: Q *rp;
                    338: {
                    339:        FILE *fp;
                    340:        VL vl,t;
                    341:
                    342:        asir_assert(ARG1(arg),O_STR,"bsave_cmo");
                    343: #if defined(THINK_C) || defined(VISUAL)
                    344:        fp = fopen(BDY((STRING)ARG1(arg)),"wb");
                    345: #else
                    346:        fp = fopen(BDY((STRING)ARG1(arg)),"w");
                    347: #endif
                    348:        if ( !fp )
                    349:                error("bsave_cmo : invalid filename");
                    350:        ox_file_io = 1; /* network byte order is used */
                    351:        write_cmo(fp,ARG0(arg));
                    352:        fclose(fp);
                    353:        ox_file_io = 0;
                    354:        *rp = ONE;
                    355: }
                    356:
                    357: void Pbload_cmo(arg,rp)
                    358: NODE arg;
                    359: Obj *rp;
                    360: {
                    361:        FILE *fp;
                    362:
                    363:        asir_assert(ARG0(arg),O_STR,"bload_cmo");
                    364: #if defined(THINK_C) || defined(VISUAL)
                    365:        fp = fopen(BDY((STRING)ARG0(arg)),"rb");
                    366: #else
                    367:        fp = fopen(BDY((STRING)ARG0(arg)),"r");
                    368: #endif
                    369:        if ( !fp )
                    370:                error("bload_cmo : invalid filename");
                    371:        ox_file_io = 1; /* network byte order is used */
                    372:        read_cmo(fp,rp);
                    373:        fclose(fp);
                    374:        ox_file_io = 0;
                    375: }
                    376:
1.7     ! noro      377: static struct oSTRING rootdir;
        !           378:
1.1       noro      379: #if defined(VISUAL)
                    380: void get_rootdir(name,len)
                    381: char *name;
                    382: int len;
                    383: {
                    384:        LONG    ret;
                    385:        HKEY    hOpenKey;
                    386:        DWORD   Type;
1.7     ! noro      387:        char    *slash;
1.1       noro      388:
1.7     ! noro      389:        if ( rootdir.body ) {
        !           390:                strcpy(name,rootdir.body);
        !           391:                return;
        !           392:        }
1.1       noro      393:        name[0] = 0;
                    394:        ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ECGEN_KEYNAME, 0,
                    395:                KEY_QUERY_VALUE, &hOpenKey);
                    396:        if ( ret != ERROR_SUCCESS )
                    397:                ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ASIR_KEYNAME, 0,
                    398:                        KEY_QUERY_VALUE, &hOpenKey);
                    399:        if( ret == ERROR_SUCCESS ) {
                    400:                RegQueryValueEx(hOpenKey, "Directory", NULL, &Type, name, &len);
                    401:                RegCloseKey(hOpenKey);
1.7     ! noro      402:        } else {
        !           403:                GetCurrentDirectory(len,name);
        !           404:                slash = strrchr(name,'\\');
        !           405:                if ( slash )
        !           406:                        *slash = 0;
1.1       noro      407:        }
                    408: }
1.7     ! noro      409:
        !           410: void set_rootdir(name)
        !           411: char *name;
        !           412: {
        !           413:        static char DirName[BUFSIZ];
        !           414:
        !           415:        strcpy(DirName,name);
        !           416:        rootdir.id = O_STR;
        !           417:        rootdir.body = DirName;
        !           418:        asir_libdir = DirName;
        !           419:        /* XXX */
        !           420:        env_init();
        !           421: }
        !           422:
1.1       noro      423: #else
                    424: void get_rootdir(name,len)
                    425: char *name;
                    426: int len;
                    427: {
                    428:        strcpy(name,asir_libdir);
                    429: }
1.7     ! noro      430:
        !           431: void set_rootdir(name)
        !           432: char *name;
        !           433: {
        !           434:        static char DirName[BUFSIZ];
        !           435:
        !           436:        strcpy(DirName,name);
        !           437:        asir_libdir = DirName;
        !           438:        /* XXX */
        !           439:        env_init();
        !           440: }
        !           441:
1.1       noro      442: #endif
                    443:
                    444: void Pget_rootdir(rp)
                    445: STRING *rp;
                    446: {
                    447:        static char DirName[BUFSIZ];
                    448:        int DirNameLen;
                    449:
                    450:        if ( !rootdir.body ) {
                    451:                get_rootdir(DirName,sizeof(DirName));
                    452:                rootdir.id = O_STR;
                    453:                rootdir.body = DirName;
                    454:        }
                    455:        *rp = &rootdir;
                    456: }
                    457:
1.6       noro      458: #if defined(VISUAL) && defined(DES_ENC)
1.1       noro      459: void Pbsave_enc(arg,rp)
                    460: NODE arg;
                    461: Obj *rp;
                    462: {
                    463:        init_deskey();
                    464:        des_encryption = 1;
                    465:        Pbsave(arg,rp);
                    466:        des_encryption = 0;
                    467: }
                    468:
                    469: void Pbload_enc(arg,rp)
                    470: NODE arg;
                    471: Obj *rp;
                    472: {
                    473:        init_deskey();
                    474:        des_encryption = 1;
                    475:        Pbload(arg,rp);
                    476:        des_encryption = 0;
                    477: }
1.6       noro      478: #endif
1.1       noro      479:
                    480: void Pbload27(arg,rp)
                    481: NODE arg;
                    482: Obj *rp;
                    483: {
                    484:        FILE *fp;
                    485:        Obj r;
                    486:
                    487:        asir_assert(ARG0(arg),O_STR,"bload27");
                    488: #if defined(THINK_C) || defined(VISUAL)
                    489:        fp = fopen(BDY((STRING)ARG0(arg)),"rb");
                    490: #else
                    491:        fp = fopen(BDY((STRING)ARG0(arg)),"r");
                    492: #endif
                    493:        if ( !fp )
                    494:                error("bload : invalid filename");
                    495:        loadvl(fp);
                    496:        loadobj(fp,&r);
                    497:        fclose(fp);
                    498:        bobjtoobj(BASE27,r,rp);
                    499: }
                    500:
                    501: void Pbsave_compat(arg,rp)
                    502: NODE arg;
                    503: Q *rp;
                    504: {
                    505:        FILE *fp;
                    506:        VL vl,t;
                    507:
                    508:        asir_assert(ARG1(arg),O_STR,"bsave_compat");
                    509:        get_vars_recursive(ARG0(arg),&vl);
                    510:        for ( t = vl; t; t = NEXT(t) )
                    511:                if ( t->v->attr == (pointer)V_UC )
                    512:                        error("bsave : not implemented");
                    513: #if defined(THINK_C) || defined(VISUAL)
                    514:        fp = fopen(BDY((STRING)ARG1(arg)),"wb");
                    515: #else
                    516:        fp = fopen(BDY((STRING)ARG1(arg)),"w");
                    517: #endif
                    518:        if ( !fp )
                    519:                error("bsave : invalid filename");
                    520:        /* indicator of an asir32 file */
                    521:        putw(0,fp); putw(0,fp);
                    522:        savevl(fp,vl);
                    523:        saveobj(fp,ARG0(arg));
                    524:        fclose(fp);
                    525:        *rp = ONE;
                    526: }
                    527:
                    528: void Pbload_compat(arg,rp)
                    529: NODE arg;
                    530: Obj *rp;
                    531: {
                    532:        FILE *fp;
                    533:        unsigned int hdr[2];
                    534:        Obj r;
                    535:        int c;
                    536:
                    537:        asir_assert(ARG0(arg),O_STR,"bload_compat");
                    538: #if defined(THINK_C) || defined(VISUAL)
                    539:        fp = fopen(BDY((STRING)ARG0(arg)),"rb");
                    540: #else
                    541:        fp = fopen(BDY((STRING)ARG0(arg)),"r");
                    542: #endif
                    543:        if ( !fp )
                    544:                error("bload : invalid filename");
                    545:        fread(hdr,sizeof(unsigned int),2,fp);
                    546:        if ( !hdr[0] && !hdr[1] ) {
                    547:                /* asir32 file or asir27 0 */
                    548:                c = fgetc(fp);
                    549:                if ( c == EOF ) {
                    550:                        /* asir27 0 */
                    551:                        *rp = 0;
                    552:                } else {
                    553:                        /* asir32 file */
                    554:                        ungetc(c,fp);
                    555:                        loadvl(fp);
                    556:                        loadobj(fp,rp);
                    557:                }
                    558:        } else {
                    559:                /* asir27 file */
                    560:                rewind(fp);
                    561:                loadvl(fp);
                    562:                loadobj(fp,&r);
                    563:                bobjtoobj(BASE27,r,rp);
                    564:        }
                    565:        fclose(fp);
                    566: }
                    567:
                    568: void Premove_file(arg,rp)
                    569: NODE arg;
                    570: Q *rp;
                    571: {
                    572:        unlink((char *)BDY((STRING)ARG0(arg)));
                    573:        *rp = ONE;
                    574: }
                    575:
                    576: void Paccess(arg,rp)
                    577: NODE arg;
                    578: Q *rp;
                    579: {
                    580:        char *name;
                    581:        STRING str;
                    582:
                    583: #if defined(VISUAL)
                    584:        if ( access(BDY((STRING)ARG0(arg)),04) >= 0 )
                    585: #else
                    586:        if ( access(BDY((STRING)ARG0(arg)),R_OK) >= 0 )
                    587: #endif
                    588:                *rp = ONE;
                    589:        else
                    590:                *rp = 0;
                    591: }

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