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

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.38    ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2000/builtin/file.c,v 1.37 2017/09/02 04:04:41 ohara Exp $
1.4       noro       49: */
1.1       noro       50: #include "ca.h"
                     51: #include "parse.h"
1.15      noro       52: #include "ox.h"
1.1       noro       53: #include "base.h"
1.37      ohara      54: #include <stdlib.h>
1.34      fujimoto   55: #if !defined(VISUAL) && !defined(__MINGW32__)
1.2       noro       56: #include "unistd.h"
1.16      noro       57: #endif
1.17      ohara      58: #if defined(PARI)
1.1       noro       59: #include "genpari.h"
1.2       noro       60: #endif
1.1       noro       61:
1.34      fujimoto   62: #if defined(VISUAL) || defined(__MINGW32__)
1.1       noro       63: #include <windows.h>
1.9       noro       64: #include <process.h>
1.15      noro       65: #include <io.h>
1.1       noro       66: /* #define ECGEN_KEYNAME "SoftWare\\Fujitsu\\WinECgen\\1.00.000" */
                     67: #define ECGEN_KEYNAME "SoftWare\\Fujitsu\\FSEcParamGen\\V1.0L10"
                     68: #define ASIR_KEYNAME "SoftWare\\Fujitsu\\Asir\\1999.03.31"
1.27      ohara      69: #define R_OK 0x04
1.1       noro       70: #endif
                     71:
1.32      ohara      72: void Psprintf(NODE,STRING *);
                     73:
1.1       noro       74: void Pget_rootdir();
                     75: void Paccess(),Premove_file();
                     76: void Pbsave_enc(), Pbload_enc();
                     77:
                     78: void Pload(), Pwhich(), Ploadfiles(), Poutput();
                     79: void Pbsave(), Pbload(), Pbload27();
                     80: void Pbsave_compat(), Pbload_compat();
                     81: void Pbsave_cmo(), Pbload_cmo();
1.13      noro       82: void Popen_file(), Pclose_file(), Pget_line(), Pget_byte(), Pput_byte();
1.24      noro       83: void Pput_word(), Pget_word();
1.10      noro       84: void Ppurge_stdin();
1.25      ohara      85: void Pfprintf();
1.22      noro       86: void Pimport();
1.28      noro       87: void Pgetpid();
1.1       noro       88:
                     89: extern int des_encryption;
                     90: extern char *asir_libdir;
                     91:
                     92: struct ftab file_tab[] = {
1.38    ! noro       93:   {"fprintf",Pfprintf,-99999999},
        !            94:   {"purge_stdin",Ppurge_stdin,0},
        !            95:   {"open_file",Popen_file,-2},
        !            96:   {"close_file",Pclose_file,1},
        !            97:   {"get_byte",Pget_byte,1},
        !            98:   {"put_byte",Pput_byte,2},
        !            99:   {"get_word",Pget_word,1},
        !           100:   {"put_word",Pput_word,2},
        !           101:   {"get_line",Pget_line,-1},
        !           102:   {"remove_file",Premove_file,1},
        !           103:   {"access",Paccess,1},
        !           104:   {"load",Pload,-1},
        !           105:   {"import",Pimport,-1},
        !           106:   {"which",Pwhich,1},
        !           107:   {"loadfiles",Ploadfiles,1},
        !           108:   {"output",Poutput,-1},
        !           109:   {"bsave",Pbsave,2},
        !           110:   {"bload",Pbload,1},
        !           111:   {"get_rootdir",Pget_rootdir,0},
1.27      ohara     112: #if defined(DES_ENC)
1.38    ! noro      113:   {"bsave_enc",Pbsave_enc,2},
        !           114:   {"bload_enc",Pbload_enc,1},
1.6       noro      115: #endif
1.38    ! noro      116:   {"bload27",Pbload27,1},
        !           117:   {"bsave_compat",Pbsave_compat,2},
        !           118:   {"bload_compat",Pbload_compat,1},
        !           119:   {"bsave_cmo",Pbsave_cmo,2},
        !           120:   {"bload_cmo",Pbload_cmo,1},
        !           121:   {"getpid",Pgetpid,0},
        !           122:   {0,0,0},
1.1       noro      123: };
1.3       noro      124:
                    125: static FILE *file_ptrs[BUFSIZ];
                    126:
1.25      ohara     127: void Pfprintf(NODE arg,pointer *rp)
                    128: {
1.38    ! noro      129:   FILE *fp;
        !           130:   STRING s;
        !           131:   asir_assert(ARG0(arg),O_N,"fprintf");
        !           132:   fp = file_ptrs[QTOS((Q)ARG0(arg))];
        !           133:   if ( !fp ) {
        !           134:     error("fprintf : invalid argument");
        !           135:   }
        !           136:   arg = NEXT(arg);
        !           137:   if ( arg ) {
        !           138:     Psprintf(arg,&s);
        !           139:     fputs(BDY(s),fp);
        !           140:     /* fflush(fp); */
        !           141:   }
        !           142:   *rp = 0;
        !           143:   return;
1.25      ohara     144: }
                    145:
1.15      noro      146: void Ppurge_stdin(Q *rp)
1.10      noro      147: {
1.38    ! noro      148:   purge_stdin(stdin);
        !           149:   *rp = 0;
1.10      noro      150: }
                    151:
1.15      noro      152: void Popen_file(NODE arg,Q *rp)
1.3       noro      153: {
1.38    ! noro      154:   char *name;
        !           155:   FILE *fp;
        !           156:   char errbuf[BUFSIZ];
        !           157:   int i;
        !           158:
        !           159:   asir_assert(ARG0(arg),O_STR,"open_file");
        !           160:   for ( i = 0; i < BUFSIZ && file_ptrs[i]; i++ );
        !           161:   if ( i == BUFSIZ )
        !           162:     error("open_file : too many open files");
        !           163:   name = BDY((STRING)ARG0(arg));
        !           164:   if (strcmp(name,"unix://stdin") == 0) {
        !           165:     fp = stdin;
        !           166:   }else if (strcmp(name,"unix://stdout") == 0) {
        !           167:     fp = stdout;
1.18      takayama  168:     }else if (strcmp(name,"unix://stderr") == 0) {
1.38    ! noro      169:     fp = stderr;
1.18      takayama  170:     }else{
1.38    ! noro      171:     if ( argc(arg) == 2 ) {
        !           172:     asir_assert(ARG1(arg),O_STR,"open_file");
        !           173:     fp = fopen(name,BDY((STRING)ARG1(arg)));
        !           174:     } else
        !           175:     fp = fopen(name,"rb");
1.18      takayama  176:     }
1.38    ! noro      177:   if ( !fp ) {
        !           178:     sprintf(errbuf,"open_file : cannot open \"%s\"",name);
        !           179:     error(errbuf);
        !           180:   }
        !           181:   file_ptrs[i] = fp;
        !           182:   STOQ(i,*rp);
1.3       noro      183: }
                    184:
1.15      noro      185: void Pclose_file(NODE arg,Q *rp)
1.3       noro      186: {
1.38    ! noro      187:   int i;
1.3       noro      188:
1.38    ! noro      189:   asir_assert(ARG0(arg),O_N,"close_file");
        !           190:   i = QTOS((Q)ARG0(arg));
        !           191:   if ( file_ptrs[i] ) {
        !           192:     fclose(file_ptrs[i]);
        !           193:     file_ptrs[i] = 0;
        !           194:   } else
        !           195:     error("close_file : invalid argument");
        !           196:   *rp = ONE;
1.3       noro      197: }
                    198:
1.15      noro      199: void Pget_line(NODE arg,STRING *rp)
1.3       noro      200: {
1.38    ! noro      201:   int i,j,c;
        !           202:   FILE *fp;
        !           203:   fpos_t head;
        !           204:   char *str;
        !           205:   char buf[BUFSIZ];
1.10      noro      206:
1.38    ! noro      207:   if ( !arg ) {
1.10      noro      208: #if defined(VISUAL_LIB)
1.38    ! noro      209:     get_string(buf,sizeof(buf));
1.10      noro      210: #else
1.38    ! noro      211:     fgets(buf,sizeof(buf),stdin);
1.10      noro      212: #endif
1.38    ! noro      213:     i = strlen(buf);
        !           214:     str = (char *)MALLOC_ATOMIC(i+1);
        !           215:     strcpy(str,buf);
        !           216:     MKSTR(*rp,str);
        !           217:     return;
        !           218:   }
        !           219:
        !           220:   asir_assert(ARG0(arg),O_N,"get_line");
        !           221:   i = QTOS((Q)ARG0(arg));
        !           222:   if ( fp = file_ptrs[i] ) {
        !           223:     if ( feof(fp) ) {
        !           224:       *rp = 0;
        !           225:       return;
        !           226:     }
        !           227:     fgetpos(fp,&head);
        !           228:     j = 0;
        !           229:     while ( 1 ) {
        !           230:       c = getc(fp);
        !           231:       if ( c == EOF ) {
        !           232:         if ( !j ) {
        !           233:           *rp = 0;
        !           234:           return;
        !           235:         } else
        !           236:           break;
        !           237:       }
        !           238:       j++;
        !           239:       if ( c == '\n' )
        !           240:         break;
        !           241:     }
        !           242:     fsetpos(fp,&head);
        !           243:     str = (char *)MALLOC_ATOMIC(j+1);
        !           244:     fgets(str,j+1,fp);
1.36      noro      245:                 for ( i = 0; i < j; i++ )
                    246:                   if ( str[i] == '\r' ) {
                    247:                      str[i] = '\n';
                    248:                      str[i+1] = 0;
                    249:                      break;
                    250:                   }
1.38    ! noro      251:     MKSTR(*rp,str);
        !           252:   } else
        !           253:     error("get_line : invalid argument");
1.8       noro      254: }
                    255:
1.15      noro      256: void Pget_byte(NODE arg,Q *rp)
1.8       noro      257: {
1.38    ! noro      258:   int i,c;
        !           259:   FILE *fp;
1.8       noro      260:
1.38    ! noro      261:   asir_assert(ARG0(arg),O_N,"get_byte");
        !           262:   i = QTOS((Q)ARG0(arg));
        !           263:   if ( fp = file_ptrs[i] ) {
        !           264:     if ( feof(fp) ) {
        !           265:       STOQ(-1,*rp);
        !           266:       return;
        !           267:     }
        !           268:     c = getc(fp);
        !           269:     STOQ(c,*rp);
        !           270:   } else
        !           271:     error("get_byte : invalid argument");
1.13      noro      272: }
                    273:
1.24      noro      274: void Pget_word(NODE arg,Q *rp)
                    275: {
1.38    ! noro      276:   int i,c;
        !           277:   FILE *fp;
1.24      noro      278:
1.38    ! noro      279:   asir_assert(ARG0(arg),O_N,"get_word");
        !           280:   i = QTOS((Q)ARG0(arg));
        !           281:   if ( fp = file_ptrs[i] ) {
        !           282:     if ( feof(fp) ) {
        !           283:       error("get_word : end of file");
        !           284:       return;
        !           285:     }
        !           286:     read_int(fp,&c);
        !           287:     STOQ(c,*rp);
        !           288:   } else
        !           289:     error("get_word : invalid argument");
1.24      noro      290: }
                    291:
1.20      noro      292: void Pput_byte(NODE arg,Obj *rp)
1.13      noro      293: {
1.38    ! noro      294:   int i,j,c;
        !           295:   FILE *fp;
        !           296:   Obj obj;
        !           297:   TB tb;
        !           298:
        !           299:   asir_assert(ARG0(arg),O_N,"put_byte");
        !           300:   i = QTOS((Q)ARG0(arg));
        !           301:   if ( !(fp = file_ptrs[i]) )
        !           302:     error("put_byte : invalid argument");
        !           303:
        !           304:   obj = (Obj)ARG1(arg);
        !           305:   if ( !obj || OID(obj) == O_N ) {
        !           306:     c = QTOS((Q)obj);
        !           307:     putc(c,fp);
        !           308:   } else if ( OID(obj) == O_STR )
        !           309:     fputs(BDY((STRING)obj),fp);
        !           310:   else if ( OID(obj) == O_TB ) {
        !           311:     tb = (TB)obj;
        !           312:     for ( j = 0; j < tb->next; j++ )
        !           313:       fputs(tb->body[j],fp);
        !           314:   }
        !           315:   *rp = obj;
1.3       noro      316: }
1.1       noro      317:
1.24      noro      318: void Pput_word(NODE arg,Obj *rp)
                    319: {
1.38    ! noro      320:   int i,c;
        !           321:   FILE *fp;
        !           322:   Obj obj;
        !           323:
        !           324:   asir_assert(ARG0(arg),O_N,"put_word");
        !           325:   asir_assert(ARG1(arg),O_N,"put_word");
        !           326:   i = QTOS((Q)ARG0(arg));
        !           327:   if ( !(fp = file_ptrs[i]) )
        !           328:     error("put_word : invalid argument");
        !           329:
        !           330:   obj = (Q)ARG1(arg);
        !           331:   c = QTOS((Q)obj);
        !           332:   write_int(fp,&c);
        !           333:   *rp = obj;
1.24      noro      334: }
                    335:
1.15      noro      336: void Pload(NODE arg,Q *rp)
1.1       noro      337: {
1.38    ! noro      338:   int ret = 0;
        !           339:   char *name,*name0;
        !           340:   char errbuf[BUFSIZ];
        !           341:
        !           342:   if ( !arg ) error("load : invalid argument");
        !           343:   if ( ARG0(arg) ) {
        !           344:     switch (OID(ARG0(arg))) {
        !           345:       case O_STR:
        !           346:         name0 = BDY((STRING)ARG0(arg));
        !           347:         searchasirpath(name0,&name);
        !           348:         if ( !name ) {
        !           349:           sprintf(errbuf,"load : \"%s\" not found in the search path",name0);
        !           350:           error(errbuf);
        !           351:         }
        !           352:         execasirfile(name);
        !           353:         break;
        !           354:       default:
        !           355:         error("load : invalid argument");
        !           356:         break;
        !           357:     }
        !           358:   }
        !           359:   STOQ(ret,*rp);
1.14      noro      360: }
                    361:
1.21      noro      362: NODE imported_files;
                    363:
                    364: void Pimport(NODE arg,Q *rp)
                    365: {
1.38    ! noro      366:   char *name;
        !           367:   NODE t,p,opt;
1.22      noro      368:
1.38    ! noro      369:   name = BDY((STRING)ARG0(arg));
        !           370:   for ( t = imported_files; t; t = NEXT(t) )
        !           371:     if ( !strcmp((char *)BDY(t),name) ) break;
        !           372:   if ( !t ) {
        !           373:     Pload(arg,rp);
        !           374:     MKNODE(t,name,imported_files);
        !           375:     imported_files = t;
        !           376:     return;
        !           377:   } else if ( current_option ) {
        !           378:     for ( opt = current_option; opt; opt = NEXT(opt) ) {
        !           379:       p = BDY((LIST)BDY(opt));
        !           380:       if ( !strcmp(BDY((STRING)BDY(p)),"reimport") && BDY(NEXT(p)) ) {
        !           381:         Pload(arg,rp);
        !           382:         return;
        !           383:       }
        !           384:     }
        !           385:   }
        !           386:   *rp = 0;
1.1       noro      387: }
                    388:
1.15      noro      389: void Pwhich(NODE arg,STRING *rp)
1.1       noro      390: {
1.38    ! noro      391:   char *name;
        !           392:   STRING str;
1.1       noro      393:
1.38    ! noro      394:   switch (OID(ARG0(arg))) {
        !           395:     case O_STR:
        !           396:       searchasirpath(BDY((STRING)ARG0(arg)),&name);
        !           397:       break;
        !           398:     default:
        !           399:       name = 0;
        !           400:       break;
        !           401:   }
        !           402:   if ( name ) {
        !           403:     MKSTR(str,name); *rp = str;
        !           404:   } else
        !           405:     *rp = 0;
1.1       noro      406: }
                    407:
1.15      noro      408: void Ploadfiles(NODE arg,Q *rp)
1.1       noro      409: {
1.38    ! noro      410:   int ret;
1.1       noro      411:
1.38    ! noro      412:   if ( ARG0(arg) )
        !           413:     if ( OID(ARG0(arg)) != O_LIST )
        !           414:       ret = 0;
        !           415:     else
        !           416:       ret = loadfiles(BDY((LIST)ARG0(arg)));
        !           417:   else
        !           418:     ret = 0;
        !           419:   STOQ(ret,*rp);
1.1       noro      420: }
                    421:
1.15      noro      422: void Poutput(NODE arg,Q *rp)
1.1       noro      423: {
1.17      ohara     424: #if defined(PARI)
1.38    ! noro      425:   extern FILE *outfile;
1.1       noro      426: #endif
1.38    ! noro      427:   FILE *fp;
1.1       noro      428:
1.38    ! noro      429:   fflush(asir_out);
        !           430:   if ( asir_out != stdout )
        !           431:     fclose(asir_out);
        !           432:   switch ( argc(arg) ) {
        !           433:     case 0:
        !           434:       fp = stdout; break;
        !           435:     case 1:
        !           436:       asir_assert(ARG0(arg),O_STR,"output");
        !           437:       fp = fopen(((STRING)ARG0(arg))->body,"a+");
        !           438:       if ( !fp )
        !           439:         error("output : invalid filename");
        !           440:       break;
        !           441:   }
1.17      ohara     442: #if defined(PARI)
1.38    ! noro      443:   pari_outfile =
1.1       noro      444: #endif
1.38    ! noro      445:   asir_out = fp;
        !           446:   *rp = ONE;
1.1       noro      447: }
                    448:
                    449: extern int ox_file_io;
                    450:
1.15      noro      451: void Pbsave(NODE arg,Q *rp)
1.1       noro      452: {
1.38    ! noro      453:   FILE *fp;
        !           454:   VL vl,t;
1.1       noro      455:
1.38    ! noro      456:   asir_assert(ARG1(arg),O_STR,"bsave");
        !           457:   get_vars_recursive(ARG0(arg),&vl);
        !           458:   for ( t = vl; t; t = NEXT(t) )
        !           459:     if ( t->v->attr == (pointer)V_UC )
        !           460:       error("bsave : not implemented");
        !           461:   fp = fopen(BDY((STRING)ARG1(arg)),"wb");
        !           462:   if ( !fp )
        !           463:     error("bsave : invalid filename");
        !           464:   ox_file_io = 1; /* network byte order is used */
        !           465:   savevl(fp,vl);
        !           466:   saveobj(fp,ARG0(arg));
        !           467:   fclose(fp);
        !           468:   ox_file_io = 0;
        !           469:   *rp = ONE;
1.1       noro      470: }
                    471:
1.15      noro      472: void Pbload(NODE arg,Obj *rp)
1.1       noro      473: {
1.38    ! noro      474:   FILE *fp;
1.1       noro      475:
1.38    ! noro      476:   asir_assert(ARG0(arg),O_STR,"bload");
        !           477:   fp = fopen(BDY((STRING)ARG0(arg)),"rb");
        !           478:   if ( !fp )
        !           479:     error("bload : invalid filename");
        !           480:   ox_file_io = 1; /* network byte order is used */
        !           481:   loadvl(fp);
        !           482:   loadobj(fp,rp);
        !           483:   fclose(fp);
        !           484:   ox_file_io = 0;
1.1       noro      485: }
                    486:
1.15      noro      487: void Pbsave_cmo(NODE arg,Q *rp)
1.1       noro      488: {
1.38    ! noro      489:   FILE *fp;
1.1       noro      490:
1.38    ! noro      491:   asir_assert(ARG1(arg),O_STR,"bsave_cmo");
        !           492:   fp = fopen(BDY((STRING)ARG1(arg)),"wb");
        !           493:   if ( !fp )
        !           494:     error("bsave_cmo : invalid filename");
        !           495:   ox_file_io = 1; /* network byte order is used */
        !           496:   write_cmo(fp,ARG0(arg));
        !           497:   fclose(fp);
        !           498:   ox_file_io = 0;
        !           499:   *rp = ONE;
1.1       noro      500: }
                    501:
1.15      noro      502: void Pbload_cmo(NODE arg,Obj *rp)
1.1       noro      503: {
1.38    ! noro      504:   FILE *fp;
1.1       noro      505:
1.38    ! noro      506:   asir_assert(ARG0(arg),O_STR,"bload_cmo");
        !           507:   fp = fopen(BDY((STRING)ARG0(arg)),"rb");
        !           508:   if ( !fp )
        !           509:     error("bload_cmo : invalid filename");
        !           510:   ox_file_io = 1; /* network byte order is used */
        !           511:   read_cmo(fp,rp);
        !           512:   fclose(fp);
        !           513:   ox_file_io = 0;
1.1       noro      514: }
                    515:
1.7       noro      516: static struct oSTRING rootdir;
                    517:
1.34      fujimoto  518: #if defined(VISUAL) || defined(__MINGW32__)
1.37      ohara     519: static void chop_delim(char *s)
                    520: {
                    521:     int n=strlen(s);
                    522:     if(n>1 && s[n-1]=='\\') {
                    523:         s[n-1]=0;
                    524:     }
                    525: }
                    526:
1.15      noro      527: void get_rootdir(char *name,int len)
1.1       noro      528: {
1.38    ! noro      529:   LONG  ret;
        !           530:   HKEY  hOpenKey;
        !           531:   DWORD  Type;
        !           532:   char    drive[_MAX_DRIVE], dir0[_MAX_DIR], dir[_MAX_DIR];
        !           533:   char    fullname[_MAX_PATH];
        !           534:
        !           535:   if ( rootdir.body ) {
        !           536:     strcpy_s(name,len,rootdir.body);
        !           537:     return;
        !           538:   }
        !           539:   if ( GetModuleFileName(NULL,fullname,_MAX_PATH) ) {
1.37      ohara     540:         _splitpath(fullname,drive,dir0,NULL,NULL);
                    541:         chop_delim(dir0);
                    542:         _splitpath(dir0,NULL,dir,NULL,NULL);
                    543:         chop_delim(dir);
                    544:         strcpy_s(name,len,drive);
                    545:         strcat_s(name,len,dir);
1.38    ! noro      546:     return;
        !           547:   }
        !           548:   name[0] = 0;
        !           549:   ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ECGEN_KEYNAME, 0,
        !           550:     KEY_QUERY_VALUE, &hOpenKey);
        !           551:   if ( ret != ERROR_SUCCESS )
        !           552:     ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ASIR_KEYNAME, 0,
        !           553:       KEY_QUERY_VALUE, &hOpenKey);
        !           554:   if( ret == ERROR_SUCCESS ) {
        !           555:     RegQueryValueEx(hOpenKey, "Directory", NULL, &Type, name, &len);
        !           556:     RegCloseKey(hOpenKey);
        !           557:   } else {
1.37      ohara     558:         GetCurrentDirectory(_MAX_PATH,fullname);
                    559:         _splitpath(fullname,drive,dir,NULL,NULL);
                    560:         chop_delim(dir);
                    561:         strcpy_s(name,len,drive);
                    562:         strcat_s(name,len,dir);
1.38    ! noro      563:   }
1.1       noro      564: }
1.7       noro      565:
1.15      noro      566: void set_rootdir(char *name)
1.7       noro      567: {
1.38    ! noro      568:   static char DirName[BUFSIZ];
1.7       noro      569:
1.38    ! noro      570:   strcpy(DirName,name);
        !           571:   rootdir.id = O_STR;
        !           572:   rootdir.body = DirName;
        !           573:   asir_libdir = DirName;
        !           574:   /* XXX */
        !           575:   env_init();
1.7       noro      576: }
                    577:
1.1       noro      578: #else
1.15      noro      579: void get_rootdir(char *name,int len)
1.1       noro      580: {
1.38    ! noro      581:   strcpy(name,asir_libdir);
1.1       noro      582: }
1.7       noro      583:
1.15      noro      584: void set_rootdir(char *name)
1.7       noro      585: {
1.38    ! noro      586:   static char DirName[BUFSIZ];
1.7       noro      587:
1.38    ! noro      588:   strcpy(DirName,name);
        !           589:   asir_libdir = DirName;
        !           590:   /* XXX */
        !           591:   env_init();
1.7       noro      592: }
                    593:
1.1       noro      594: #endif
                    595:
1.15      noro      596: void Pget_rootdir(STRING *rp)
1.1       noro      597: {
1.38    ! noro      598:   static char DirName[BUFSIZ];
1.1       noro      599:
1.38    ! noro      600:   if ( !rootdir.body ) {
        !           601:     get_rootdir(DirName,sizeof(DirName));
        !           602:     rootdir.id = O_STR;
        !           603:     rootdir.body = DirName;
        !           604:   }
        !           605:   *rp = &rootdir;
1.1       noro      606: }
                    607:
1.28      noro      608: void Pgetpid(Q *rp)
                    609: {
1.38    ! noro      610:   int id;
1.28      noro      611:
1.34      fujimoto  612: #if defined(VISUAL) || defined(__MINGW32__)
1.38    ! noro      613:   id = GetCurrentProcessId();
1.28      noro      614: #else
1.38    ! noro      615:   id = getpid();
1.28      noro      616: #endif
1.38    ! noro      617:   STOQ(id,*rp);
1.28      noro      618: }
                    619:
1.27      ohara     620: #if defined(DES_ENC)
1.15      noro      621: void Pbsave_enc(NODE arg,Obj *rp)
1.1       noro      622: {
1.38    ! noro      623:   init_deskey();
        !           624:   des_encryption = 1;
        !           625:   Pbsave(arg,rp);
        !           626:   des_encryption = 0;
1.1       noro      627: }
                    628:
1.15      noro      629: void Pbload_enc(NODE arg,Obj *rp)
1.1       noro      630: {
1.38    ! noro      631:   init_deskey();
        !           632:   des_encryption = 1;
        !           633:   Pbload(arg,rp);
        !           634:   des_encryption = 0;
1.1       noro      635: }
1.6       noro      636: #endif
1.1       noro      637:
1.15      noro      638: void Pbload27(NODE arg,Obj *rp)
1.1       noro      639: {
1.38    ! noro      640:   FILE *fp;
        !           641:   Obj r;
1.1       noro      642:
1.38    ! noro      643:   asir_assert(ARG0(arg),O_STR,"bload27");
        !           644:   fp = fopen(BDY((STRING)ARG0(arg)),"rb");
        !           645:   if ( !fp )
        !           646:     error("bload : invalid filename");
        !           647:   loadvl(fp);
        !           648:   loadobj(fp,&r);
        !           649:   fclose(fp);
        !           650:   bobjtoobj(BASE27,r,rp);
1.1       noro      651: }
                    652:
1.15      noro      653: void Pbsave_compat(NODE arg,Q *rp)
1.1       noro      654: {
1.38    ! noro      655:   FILE *fp;
        !           656:   VL vl,t;
1.1       noro      657:
1.38    ! noro      658:   asir_assert(ARG1(arg),O_STR,"bsave_compat");
        !           659:   get_vars_recursive(ARG0(arg),&vl);
        !           660:   for ( t = vl; t; t = NEXT(t) )
        !           661:     if ( t->v->attr == (pointer)V_UC )
        !           662:       error("bsave : not implemented");
        !           663:   fp = fopen(BDY((STRING)ARG1(arg)),"wb");
        !           664:   if ( !fp )
        !           665:     error("bsave : invalid filename");
        !           666:   /* indicator of an asir32 file */
        !           667:   putw(0,fp); putw(0,fp);
        !           668:   savevl(fp,vl);
        !           669:   saveobj(fp,ARG0(arg));
        !           670:   fclose(fp);
        !           671:   *rp = ONE;
1.1       noro      672: }
                    673:
1.15      noro      674: void Pbload_compat(NODE arg,Obj *rp)
1.1       noro      675: {
1.38    ! noro      676:   FILE *fp;
        !           677:   unsigned int hdr[2];
        !           678:   Obj r;
        !           679:   int c;
        !           680:
        !           681:   asir_assert(ARG0(arg),O_STR,"bload_compat");
        !           682:   fp = fopen(BDY((STRING)ARG0(arg)),"rb");
        !           683:   if ( !fp )
        !           684:     error("bload : invalid filename");
        !           685:   fread(hdr,sizeof(unsigned int),2,fp);
        !           686:   if ( !hdr[0] && !hdr[1] ) {
        !           687:     /* asir32 file or asir27 0 */
        !           688:     c = fgetc(fp);
        !           689:     if ( c == EOF ) {
        !           690:       /* asir27 0 */
        !           691:       *rp = 0;
        !           692:     } else {
        !           693:       /* asir32 file */
        !           694:       ungetc(c,fp);
        !           695:       loadvl(fp);
        !           696:       loadobj(fp,rp);
        !           697:     }
        !           698:   } else {
        !           699:     /* asir27 file */
        !           700:     rewind(fp);
        !           701:     loadvl(fp);
        !           702:     loadobj(fp,&r);
        !           703:     bobjtoobj(BASE27,r,rp);
        !           704:   }
        !           705:   fclose(fp);
1.1       noro      706: }
                    707:
1.15      noro      708: void Premove_file(NODE arg,Q *rp)
1.1       noro      709: {
1.38    ! noro      710:   unlink((char *)BDY((STRING)ARG0(arg)));
        !           711:   *rp = ONE;
1.1       noro      712: }
                    713:
1.15      noro      714: void Paccess(NODE arg,Q *rp)
1.1       noro      715: {
1.38    ! noro      716:   if ( access(BDY((STRING)ARG0(arg)),R_OK) >= 0 )
        !           717:     *rp = ONE;
        !           718:   else
        !           719:     *rp = 0;
1.1       noro      720: }
1.9       noro      721:
1.34      fujimoto  722: #if defined(VISUAL) || defined(__MINGW32__)
1.9       noro      723: int process_id()
                    724: {
1.38    ! noro      725:   return GetCurrentProcessId();
1.9       noro      726: }
                    727:
1.15      noro      728: void call_exe(char *name,char **av)
1.9       noro      729: {
1.38    ! noro      730:   _spawnv(_P_WAIT,name,av);
1.9       noro      731: }
                    732: #endif

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