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