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

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

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