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