Annotation of OpenXM_contrib2/asir2000/builtin/file.c, Revision 1.12
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.12 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/builtin/file.c,v 1.11 2000/12/05 01:24:50 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 ( ARG0(arg) ) {
237: switch (OID(ARG0(arg))) {
238: case O_STR:
239: name0 = BDY((STRING)ARG0(arg));
240: searchasirpath(name0,&name);
241: if ( !name ) {
242: sprintf(errbuf,"load : \"%s\" not found in the search path",name0);
243: error(errbuf);
244: }
245: ret = loadfile(name);
246: if ( !ret ) {
247: sprintf(errbuf,"load : \"%s\" could not be loaded",name);
248: error(errbuf);
249: }
250: break;
251: default:
252: error("load : invalid argument");
253: break;
254: }
255: }
256: STOQ(ret,*rp);
257: }
258:
259: void Pwhich(arg,rp)
260: NODE arg;
261: STRING *rp;
262: {
263: char *name;
264: STRING str;
265:
266: switch (OID(ARG0(arg))) {
267: case O_STR:
268: searchasirpath(BDY((STRING)ARG0(arg)),&name);
269: break;
270: default:
271: name = 0;
272: break;
273: }
274: if ( name ) {
275: MKSTR(str,name); *rp = str;
276: } else
277: *rp = 0;
278: }
279:
280: void Ploadfiles(arg,rp)
281: NODE arg;
282: Q *rp;
283: {
284: int ret;
285:
286: if ( ARG0(arg) )
287: if ( OID(ARG0(arg)) != O_LIST )
288: ret = 0;
289: else
290: ret = loadfiles(BDY((LIST)ARG0(arg)));
291: else
292: ret = 0;
293: STOQ(ret,*rp);
294: }
295:
296: void Poutput(arg,rp)
297: NODE arg;
298: Q *rp;
299: {
300: #if PARI
301: extern FILE *outfile;
302: #endif
303: FILE *fp;
304:
305: fflush(asir_out);
306: if ( asir_out != stdout )
307: fclose(asir_out);
308: switch ( argc(arg) ) {
309: case 0:
310: fp = stdout; break;
311: case 1:
312: asir_assert(ARG0(arg),O_STR,"output");
313: fp = fopen(((STRING)ARG0(arg))->body,"a+");
314: if ( !fp )
315: error("output : invalid filename");
316: break;
317: }
318: #if PARI
319: pari_outfile =
320: #endif
321: asir_out = fp;
322: *rp = ONE;
323: }
324:
325: extern int ox_file_io;
326:
327: void Pbsave(arg,rp)
328: NODE arg;
329: Q *rp;
330: {
331: FILE *fp;
332: VL vl,t;
333:
334: asir_assert(ARG1(arg),O_STR,"bsave");
335: get_vars_recursive(ARG0(arg),&vl);
336: for ( t = vl; t; t = NEXT(t) )
337: if ( t->v->attr == (pointer)V_UC )
338: error("bsave : not implemented");
339: fp = fopen(BDY((STRING)ARG1(arg)),"wb");
340: if ( !fp )
341: error("bsave : invalid filename");
342: ox_file_io = 1; /* network byte order is used */
343: savevl(fp,vl);
344: saveobj(fp,ARG0(arg));
345: fclose(fp);
346: ox_file_io = 0;
347: *rp = ONE;
348: }
349:
350: void Pbload(arg,rp)
351: NODE arg;
352: Obj *rp;
353: {
354: FILE *fp;
355:
356: asir_assert(ARG0(arg),O_STR,"bload");
357: fp = fopen(BDY((STRING)ARG0(arg)),"rb");
358: if ( !fp )
359: error("bload : invalid filename");
360: ox_file_io = 1; /* network byte order is used */
361: loadvl(fp);
362: loadobj(fp,rp);
363: fclose(fp);
364: ox_file_io = 0;
365: }
366:
367: void Pbsave_cmo(arg,rp)
368: NODE arg;
369: Q *rp;
370: {
371: FILE *fp;
372: VL vl,t;
373:
374: asir_assert(ARG1(arg),O_STR,"bsave_cmo");
375: fp = fopen(BDY((STRING)ARG1(arg)),"wb");
376: if ( !fp )
377: error("bsave_cmo : invalid filename");
378: ox_file_io = 1; /* network byte order is used */
379: write_cmo(fp,ARG0(arg));
380: fclose(fp);
381: ox_file_io = 0;
382: *rp = ONE;
383: }
384:
385: void Pbload_cmo(arg,rp)
386: NODE arg;
387: Obj *rp;
388: {
389: FILE *fp;
390:
391: asir_assert(ARG0(arg),O_STR,"bload_cmo");
392: fp = fopen(BDY((STRING)ARG0(arg)),"rb");
393: if ( !fp )
394: error("bload_cmo : invalid filename");
395: ox_file_io = 1; /* network byte order is used */
396: read_cmo(fp,rp);
397: fclose(fp);
398: ox_file_io = 0;
399: }
400:
1.7 noro 401: static struct oSTRING rootdir;
402:
1.1 noro 403: #if defined(VISUAL)
404: void get_rootdir(name,len)
405: char *name;
406: int len;
407: {
408: LONG ret;
409: HKEY hOpenKey;
410: DWORD Type;
1.7 noro 411: char *slash;
1.12 ! noro 412:
1.7 noro 413: if ( rootdir.body ) {
414: strcpy(name,rootdir.body);
1.12 ! noro 415: return;
! 416: }
! 417:
! 418: if ( access("UseCurrentDir",0) >= 0 ) {
! 419: GetCurrentDirectory(BUFSIZ,name);
! 420: slash = strrchr(name,'\\');
! 421: if ( slash )
! 422: *slash = 0;
1.7 noro 423: return;
424: }
1.1 noro 425: name[0] = 0;
426: ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ECGEN_KEYNAME, 0,
427: KEY_QUERY_VALUE, &hOpenKey);
428: if ( ret != ERROR_SUCCESS )
429: ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ASIR_KEYNAME, 0,
430: KEY_QUERY_VALUE, &hOpenKey);
431: if( ret == ERROR_SUCCESS ) {
432: RegQueryValueEx(hOpenKey, "Directory", NULL, &Type, name, &len);
433: RegCloseKey(hOpenKey);
1.7 noro 434: } else {
435: GetCurrentDirectory(len,name);
436: slash = strrchr(name,'\\');
437: if ( slash )
438: *slash = 0;
1.1 noro 439: }
440: }
1.7 noro 441:
442: void set_rootdir(name)
443: char *name;
444: {
445: static char DirName[BUFSIZ];
446:
447: strcpy(DirName,name);
448: rootdir.id = O_STR;
449: rootdir.body = DirName;
450: asir_libdir = DirName;
451: /* XXX */
452: env_init();
453: }
454:
1.1 noro 455: #else
456: void get_rootdir(name,len)
457: char *name;
458: int len;
459: {
460: strcpy(name,asir_libdir);
461: }
1.7 noro 462:
463: void set_rootdir(name)
464: char *name;
465: {
466: static char DirName[BUFSIZ];
467:
468: strcpy(DirName,name);
469: asir_libdir = DirName;
470: /* XXX */
471: env_init();
472: }
473:
1.1 noro 474: #endif
475:
476: void Pget_rootdir(rp)
477: STRING *rp;
478: {
479: static char DirName[BUFSIZ];
480: int DirNameLen;
481:
482: if ( !rootdir.body ) {
483: get_rootdir(DirName,sizeof(DirName));
484: rootdir.id = O_STR;
485: rootdir.body = DirName;
486: }
487: *rp = &rootdir;
488: }
489:
1.6 noro 490: #if defined(VISUAL) && defined(DES_ENC)
1.1 noro 491: void Pbsave_enc(arg,rp)
492: NODE arg;
493: Obj *rp;
494: {
495: init_deskey();
496: des_encryption = 1;
497: Pbsave(arg,rp);
498: des_encryption = 0;
499: }
500:
501: void Pbload_enc(arg,rp)
502: NODE arg;
503: Obj *rp;
504: {
505: init_deskey();
506: des_encryption = 1;
507: Pbload(arg,rp);
508: des_encryption = 0;
509: }
1.6 noro 510: #endif
1.1 noro 511:
512: void Pbload27(arg,rp)
513: NODE arg;
514: Obj *rp;
515: {
516: FILE *fp;
517: Obj r;
518:
519: asir_assert(ARG0(arg),O_STR,"bload27");
520: fp = fopen(BDY((STRING)ARG0(arg)),"rb");
521: if ( !fp )
522: error("bload : invalid filename");
523: loadvl(fp);
524: loadobj(fp,&r);
525: fclose(fp);
526: bobjtoobj(BASE27,r,rp);
527: }
528:
529: void Pbsave_compat(arg,rp)
530: NODE arg;
531: Q *rp;
532: {
533: FILE *fp;
534: VL vl,t;
535:
536: asir_assert(ARG1(arg),O_STR,"bsave_compat");
537: get_vars_recursive(ARG0(arg),&vl);
538: for ( t = vl; t; t = NEXT(t) )
539: if ( t->v->attr == (pointer)V_UC )
540: error("bsave : not implemented");
541: fp = fopen(BDY((STRING)ARG1(arg)),"wb");
542: if ( !fp )
543: error("bsave : invalid filename");
544: /* indicator of an asir32 file */
545: putw(0,fp); putw(0,fp);
546: savevl(fp,vl);
547: saveobj(fp,ARG0(arg));
548: fclose(fp);
549: *rp = ONE;
550: }
551:
552: void Pbload_compat(arg,rp)
553: NODE arg;
554: Obj *rp;
555: {
556: FILE *fp;
557: unsigned int hdr[2];
558: Obj r;
559: int c;
560:
561: asir_assert(ARG0(arg),O_STR,"bload_compat");
562: fp = fopen(BDY((STRING)ARG0(arg)),"rb");
563: if ( !fp )
564: error("bload : invalid filename");
565: fread(hdr,sizeof(unsigned int),2,fp);
566: if ( !hdr[0] && !hdr[1] ) {
567: /* asir32 file or asir27 0 */
568: c = fgetc(fp);
569: if ( c == EOF ) {
570: /* asir27 0 */
571: *rp = 0;
572: } else {
573: /* asir32 file */
574: ungetc(c,fp);
575: loadvl(fp);
576: loadobj(fp,rp);
577: }
578: } else {
579: /* asir27 file */
580: rewind(fp);
581: loadvl(fp);
582: loadobj(fp,&r);
583: bobjtoobj(BASE27,r,rp);
584: }
585: fclose(fp);
586: }
587:
588: void Premove_file(arg,rp)
589: NODE arg;
590: Q *rp;
591: {
592: unlink((char *)BDY((STRING)ARG0(arg)));
593: *rp = ONE;
594: }
595:
596: void Paccess(arg,rp)
597: NODE arg;
598: Q *rp;
599: {
600: char *name;
601: STRING str;
602:
603: #if defined(VISUAL)
604: if ( access(BDY((STRING)ARG0(arg)),04) >= 0 )
605: #else
606: if ( access(BDY((STRING)ARG0(arg)),R_OK) >= 0 )
607: #endif
608: *rp = ONE;
609: else
610: *rp = 0;
611: }
1.9 noro 612:
613: #if defined(VISUAL)
614: int process_id()
615: {
616: return GetCurrentProcessId();
617: }
618:
619: void call_exe(name,av)
620: char *name;
621: char **av;
622: {
623: _spawnv(_P_WAIT,name,av);
624: }
625: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>