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

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.6     ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2000/builtin/file.c,v 1.5 2000/08/22 05:03:57 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:
                    377: #if defined(VISUAL)
                    378: void get_rootdir(name,len)
                    379: char *name;
                    380: int len;
                    381: {
                    382:        LONG    ret;
                    383:        HKEY    hOpenKey;
                    384:        DWORD   Type;
                    385:
                    386:        name[0] = 0;
                    387:        ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ECGEN_KEYNAME, 0,
                    388:                KEY_QUERY_VALUE, &hOpenKey);
                    389:        if ( ret != ERROR_SUCCESS )
                    390:                ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ASIR_KEYNAME, 0,
                    391:                        KEY_QUERY_VALUE, &hOpenKey);
                    392:        if( ret == ERROR_SUCCESS ) {
                    393:                RegQueryValueEx(hOpenKey, "Directory", NULL, &Type, name, &len);
                    394:                RegCloseKey(hOpenKey);
                    395:        }
                    396: }
                    397: #else
                    398: void get_rootdir(name,len)
                    399: char *name;
                    400: int len;
                    401: {
                    402:        strcpy(name,asir_libdir);
                    403: }
                    404: #endif
                    405:
                    406: void Pget_rootdir(rp)
                    407: STRING *rp;
                    408: {
                    409:        static struct oSTRING rootdir;
                    410:        static char DirName[BUFSIZ];
                    411:        int DirNameLen;
                    412:
                    413:        if ( !rootdir.body ) {
                    414:                get_rootdir(DirName,sizeof(DirName));
                    415:                rootdir.id = O_STR;
                    416:                rootdir.body = DirName;
                    417:        }
                    418:        *rp = &rootdir;
                    419: }
                    420:
1.6     ! noro      421: #if defined(VISUAL) && defined(DES_ENC)
1.1       noro      422: void Pbsave_enc(arg,rp)
                    423: NODE arg;
                    424: Obj *rp;
                    425: {
                    426:        init_deskey();
                    427:        des_encryption = 1;
                    428:        Pbsave(arg,rp);
                    429:        des_encryption = 0;
                    430: }
                    431:
                    432: void Pbload_enc(arg,rp)
                    433: NODE arg;
                    434: Obj *rp;
                    435: {
                    436:        init_deskey();
                    437:        des_encryption = 1;
                    438:        Pbload(arg,rp);
                    439:        des_encryption = 0;
                    440: }
1.6     ! noro      441: #endif
1.1       noro      442:
                    443: void Pbload27(arg,rp)
                    444: NODE arg;
                    445: Obj *rp;
                    446: {
                    447:        FILE *fp;
                    448:        Obj r;
                    449:
                    450:        asir_assert(ARG0(arg),O_STR,"bload27");
                    451: #if defined(THINK_C) || defined(VISUAL)
                    452:        fp = fopen(BDY((STRING)ARG0(arg)),"rb");
                    453: #else
                    454:        fp = fopen(BDY((STRING)ARG0(arg)),"r");
                    455: #endif
                    456:        if ( !fp )
                    457:                error("bload : invalid filename");
                    458:        loadvl(fp);
                    459:        loadobj(fp,&r);
                    460:        fclose(fp);
                    461:        bobjtoobj(BASE27,r,rp);
                    462: }
                    463:
                    464: void Pbsave_compat(arg,rp)
                    465: NODE arg;
                    466: Q *rp;
                    467: {
                    468:        FILE *fp;
                    469:        VL vl,t;
                    470:
                    471:        asir_assert(ARG1(arg),O_STR,"bsave_compat");
                    472:        get_vars_recursive(ARG0(arg),&vl);
                    473:        for ( t = vl; t; t = NEXT(t) )
                    474:                if ( t->v->attr == (pointer)V_UC )
                    475:                        error("bsave : not implemented");
                    476: #if defined(THINK_C) || defined(VISUAL)
                    477:        fp = fopen(BDY((STRING)ARG1(arg)),"wb");
                    478: #else
                    479:        fp = fopen(BDY((STRING)ARG1(arg)),"w");
                    480: #endif
                    481:        if ( !fp )
                    482:                error("bsave : invalid filename");
                    483:        /* indicator of an asir32 file */
                    484:        putw(0,fp); putw(0,fp);
                    485:        savevl(fp,vl);
                    486:        saveobj(fp,ARG0(arg));
                    487:        fclose(fp);
                    488:        *rp = ONE;
                    489: }
                    490:
                    491: void Pbload_compat(arg,rp)
                    492: NODE arg;
                    493: Obj *rp;
                    494: {
                    495:        FILE *fp;
                    496:        unsigned int hdr[2];
                    497:        Obj r;
                    498:        int c;
                    499:
                    500:        asir_assert(ARG0(arg),O_STR,"bload_compat");
                    501: #if defined(THINK_C) || defined(VISUAL)
                    502:        fp = fopen(BDY((STRING)ARG0(arg)),"rb");
                    503: #else
                    504:        fp = fopen(BDY((STRING)ARG0(arg)),"r");
                    505: #endif
                    506:        if ( !fp )
                    507:                error("bload : invalid filename");
                    508:        fread(hdr,sizeof(unsigned int),2,fp);
                    509:        if ( !hdr[0] && !hdr[1] ) {
                    510:                /* asir32 file or asir27 0 */
                    511:                c = fgetc(fp);
                    512:                if ( c == EOF ) {
                    513:                        /* asir27 0 */
                    514:                        *rp = 0;
                    515:                } else {
                    516:                        /* asir32 file */
                    517:                        ungetc(c,fp);
                    518:                        loadvl(fp);
                    519:                        loadobj(fp,rp);
                    520:                }
                    521:        } else {
                    522:                /* asir27 file */
                    523:                rewind(fp);
                    524:                loadvl(fp);
                    525:                loadobj(fp,&r);
                    526:                bobjtoobj(BASE27,r,rp);
                    527:        }
                    528:        fclose(fp);
                    529: }
                    530:
                    531: void Premove_file(arg,rp)
                    532: NODE arg;
                    533: Q *rp;
                    534: {
                    535:        unlink((char *)BDY((STRING)ARG0(arg)));
                    536:        *rp = ONE;
                    537: }
                    538:
                    539: void Paccess(arg,rp)
                    540: NODE arg;
                    541: Q *rp;
                    542: {
                    543:        char *name;
                    544:        STRING str;
                    545:
                    546: #if defined(VISUAL)
                    547:        if ( access(BDY((STRING)ARG0(arg)),04) >= 0 )
                    548: #else
                    549:        if ( access(BDY((STRING)ARG0(arg)),R_OK) >= 0 )
                    550: #endif
                    551:                *rp = ONE;
                    552:        else
                    553:                *rp = 0;
                    554: }

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