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