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