Annotation of OpenXM_contrib2/asir2000/builtin/file.c, Revision 1.11
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.11 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/builtin/file.c,v 1.10 2000/11/13 01:48:12 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.1 noro 412:
1.7 noro 413: if ( rootdir.body ) {
414: strcpy(name,rootdir.body);
415: return;
416: }
1.1 noro 417: name[0] = 0;
418: ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ECGEN_KEYNAME, 0,
419: KEY_QUERY_VALUE, &hOpenKey);
420: if ( ret != ERROR_SUCCESS )
421: ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ASIR_KEYNAME, 0,
422: KEY_QUERY_VALUE, &hOpenKey);
423: if( ret == ERROR_SUCCESS ) {
424: RegQueryValueEx(hOpenKey, "Directory", NULL, &Type, name, &len);
425: RegCloseKey(hOpenKey);
1.7 noro 426: } else {
427: GetCurrentDirectory(len,name);
428: slash = strrchr(name,'\\');
429: if ( slash )
430: *slash = 0;
1.1 noro 431: }
432: }
1.7 noro 433:
434: void set_rootdir(name)
435: char *name;
436: {
437: static char DirName[BUFSIZ];
438:
439: strcpy(DirName,name);
440: rootdir.id = O_STR;
441: rootdir.body = DirName;
442: asir_libdir = DirName;
443: /* XXX */
444: env_init();
445: }
446:
1.1 noro 447: #else
448: void get_rootdir(name,len)
449: char *name;
450: int len;
451: {
452: strcpy(name,asir_libdir);
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: asir_libdir = DirName;
462: /* XXX */
463: env_init();
464: }
465:
1.1 noro 466: #endif
467:
468: void Pget_rootdir(rp)
469: STRING *rp;
470: {
471: static char DirName[BUFSIZ];
472: int DirNameLen;
473:
474: if ( !rootdir.body ) {
475: get_rootdir(DirName,sizeof(DirName));
476: rootdir.id = O_STR;
477: rootdir.body = DirName;
478: }
479: *rp = &rootdir;
480: }
481:
1.6 noro 482: #if defined(VISUAL) && defined(DES_ENC)
1.1 noro 483: void Pbsave_enc(arg,rp)
484: NODE arg;
485: Obj *rp;
486: {
487: init_deskey();
488: des_encryption = 1;
489: Pbsave(arg,rp);
490: des_encryption = 0;
491: }
492:
493: void Pbload_enc(arg,rp)
494: NODE arg;
495: Obj *rp;
496: {
497: init_deskey();
498: des_encryption = 1;
499: Pbload(arg,rp);
500: des_encryption = 0;
501: }
1.6 noro 502: #endif
1.1 noro 503:
504: void Pbload27(arg,rp)
505: NODE arg;
506: Obj *rp;
507: {
508: FILE *fp;
509: Obj r;
510:
511: asir_assert(ARG0(arg),O_STR,"bload27");
512: fp = fopen(BDY((STRING)ARG0(arg)),"rb");
513: if ( !fp )
514: error("bload : invalid filename");
515: loadvl(fp);
516: loadobj(fp,&r);
517: fclose(fp);
518: bobjtoobj(BASE27,r,rp);
519: }
520:
521: void Pbsave_compat(arg,rp)
522: NODE arg;
523: Q *rp;
524: {
525: FILE *fp;
526: VL vl,t;
527:
528: asir_assert(ARG1(arg),O_STR,"bsave_compat");
529: get_vars_recursive(ARG0(arg),&vl);
530: for ( t = vl; t; t = NEXT(t) )
531: if ( t->v->attr == (pointer)V_UC )
532: error("bsave : not implemented");
533: fp = fopen(BDY((STRING)ARG1(arg)),"wb");
534: if ( !fp )
535: error("bsave : invalid filename");
536: /* indicator of an asir32 file */
537: putw(0,fp); putw(0,fp);
538: savevl(fp,vl);
539: saveobj(fp,ARG0(arg));
540: fclose(fp);
541: *rp = ONE;
542: }
543:
544: void Pbload_compat(arg,rp)
545: NODE arg;
546: Obj *rp;
547: {
548: FILE *fp;
549: unsigned int hdr[2];
550: Obj r;
551: int c;
552:
553: asir_assert(ARG0(arg),O_STR,"bload_compat");
554: fp = fopen(BDY((STRING)ARG0(arg)),"rb");
555: if ( !fp )
556: error("bload : invalid filename");
557: fread(hdr,sizeof(unsigned int),2,fp);
558: if ( !hdr[0] && !hdr[1] ) {
559: /* asir32 file or asir27 0 */
560: c = fgetc(fp);
561: if ( c == EOF ) {
562: /* asir27 0 */
563: *rp = 0;
564: } else {
565: /* asir32 file */
566: ungetc(c,fp);
567: loadvl(fp);
568: loadobj(fp,rp);
569: }
570: } else {
571: /* asir27 file */
572: rewind(fp);
573: loadvl(fp);
574: loadobj(fp,&r);
575: bobjtoobj(BASE27,r,rp);
576: }
577: fclose(fp);
578: }
579:
580: void Premove_file(arg,rp)
581: NODE arg;
582: Q *rp;
583: {
584: unlink((char *)BDY((STRING)ARG0(arg)));
585: *rp = ONE;
586: }
587:
588: void Paccess(arg,rp)
589: NODE arg;
590: Q *rp;
591: {
592: char *name;
593: STRING str;
594:
595: #if defined(VISUAL)
596: if ( access(BDY((STRING)ARG0(arg)),04) >= 0 )
597: #else
598: if ( access(BDY((STRING)ARG0(arg)),R_OK) >= 0 )
599: #endif
600: *rp = ONE;
601: else
602: *rp = 0;
603: }
1.9 noro 604:
605: #if defined(VISUAL)
606: int process_id()
607: {
608: return GetCurrentProcessId();
609: }
610:
611: void call_exe(name,av)
612: char *name;
613: char **av;
614: {
615: _spawnv(_P_WAIT,name,av);
616: }
617: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>