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