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