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