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