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