Annotation of OpenXM_contrib2/asir2000/builtin/file.c, Revision 1.3
1.3 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2000/builtin/file.c,v 1.2 2000/02/08 04:47:09 noro Exp $ */
1.1 noro 2: #include "ca.h"
3: #include "parse.h"
4: #include "base.h"
1.2 noro 5: #include "unistd.h"
6: #if PARI
1.1 noro 7: #include "genpari.h"
1.2 noro 8: #endif
1.1 noro 9:
10: #if defined(VISUAL)
11: #include <windows.h>
12: /* #define ECGEN_KEYNAME "SoftWare\\Fujitsu\\WinECgen\\1.00.000" */
13: #define ECGEN_KEYNAME "SoftWare\\Fujitsu\\FSEcParamGen\\V1.0L10"
14: #define ASIR_KEYNAME "SoftWare\\Fujitsu\\Asir\\1999.03.31"
15: #endif
16:
17: void Pget_rootdir();
18: void Paccess(),Premove_file();
19: void Pbsave_enc(), Pbload_enc();
20:
21: void Pload(), Pwhich(), Ploadfiles(), Poutput();
22: void Pbsave(), Pbload(), Pbload27();
23: void Pbsave_compat(), Pbload_compat();
24: void Pbsave_cmo(), Pbload_cmo();
1.3 ! noro 25: void Popen_file(), Pclose_file(), Pget_line();
1.1 noro 26:
27: extern int des_encryption;
28: extern char *asir_libdir;
29:
30: struct ftab file_tab[] = {
1.3 ! noro 31: {"open_file",Popen_file,1},
! 32: {"close_file",Pclose_file,1},
! 33: {"get_line",Pget_line,1},
1.1 noro 34: {"remove_file",Premove_file,1},
35: {"access",Paccess,1},
36: {"load",Pload,-1},
37: {"which",Pwhich,1},
38: {"loadfiles",Ploadfiles,1},
39: {"output",Poutput,-1},
40: {"bsave",Pbsave,2},
41: {"bload",Pbload,1},
42: {"get_rootdir",Pget_rootdir,0},
43: {"bsave_enc",Pbsave_enc,2},
44: {"bload_enc",Pbload_enc,1},
45: {"bload27",Pbload27,1},
46: {"bsave_compat",Pbsave_compat,2},
47: {"bload_compat",Pbload_compat,1},
48: {"bsave_cmo",Pbsave_cmo,2},
49: {"bload_cmo",Pbload_cmo,1},
50: {0,0,0},
51: };
1.3 ! noro 52:
! 53: static FILE *file_ptrs[BUFSIZ];
! 54:
! 55: void Popen_file(arg,rp)
! 56: NODE arg;
! 57: Q *rp;
! 58: {
! 59: char *name;
! 60: FILE *fp;
! 61: char errbuf[BUFSIZ];
! 62: int i;
! 63:
! 64: asir_assert(ARG0(arg),O_STR,"open_file");
! 65: for ( i = 0; i < BUFSIZ && file_ptrs[i]; i++ );
! 66: if ( i == BUFSIZ )
! 67: error("open_file : too many open files");
! 68: name = BDY((STRING)ARG0(arg));
! 69: fp = fopen(name,"r");
! 70: if ( !fp ) {
! 71: sprintf(errbuf,"open_file : \"%s\" not found",name);
! 72: error(errbuf);
! 73: }
! 74: file_ptrs[i] = fp;
! 75: STOQ(i,*rp);
! 76: }
! 77:
! 78: void Pclose_file(arg,rp)
! 79: NODE arg;
! 80: Q *rp;
! 81: {
! 82: int i;
! 83:
! 84: asir_assert(ARG0(arg),O_N,"open_file");
! 85: i = QTOS((Q)ARG0(arg));
! 86: if ( file_ptrs[i] ) {
! 87: fclose(file_ptrs[i]);
! 88: file_ptrs[i] = 0;
! 89: } else
! 90: error("close_file : invalid argument");
! 91: *rp = ONE;
! 92: }
! 93:
! 94: void Pget_line(arg,rp)
! 95: NODE arg;
! 96: STRING *rp;
! 97: {
! 98: int i,j,c;
! 99: FILE *fp;
! 100: fpos_t head;
! 101: char *str;
! 102:
! 103: asir_assert(ARG0(arg),O_N,"open_file");
! 104: i = QTOS((Q)ARG0(arg));
! 105: if ( fp = file_ptrs[i] ) {
! 106: if ( feof(fp) ) {
! 107: *rp = 0;
! 108: return;
! 109: }
! 110: fgetpos(fp,&head);
! 111: j = 0;
! 112: while ( 1 ) {
! 113: c = getc(fp);
! 114: if ( c == EOF ) {
! 115: if ( !j ) {
! 116: *rp = 0;
! 117: return;
! 118: } else
! 119: break;
! 120: }
! 121: j++;
! 122: if ( c == '\n' )
! 123: break;
! 124: }
! 125: fsetpos(fp,&head);
! 126: str = (char *)MALLOC_ATOMIC(j+1);
! 127: fgets(str,j+1,fp);
! 128: MKSTR(*rp,str);
! 129: } else
! 130: error("get_line : invalid argument");
! 131: }
1.1 noro 132:
133: void Pload(arg,rp)
134: NODE arg;
135: Q *rp;
136: {
137: int ret = 0;
138: char *name,*name0;
139: char errbuf[BUFSIZ];
140:
141: #if defined THINK_C
142: if ( !arg ) {
143: ret = finder_loadfile();
144: } else
145: #endif
146: if ( ARG0(arg) ) {
147: switch (OID(ARG0(arg))) {
148: case O_STR:
149: name0 = BDY((STRING)ARG0(arg));
150: searchasirpath(name0,&name);
151: if ( !name ) {
152: sprintf(errbuf,"load : \"%s\" not found in the search path",name0);
153: error(errbuf);
154: }
155: ret = loadfile(name);
156: if ( !ret ) {
157: sprintf(errbuf,"load : \"%s\" could not be loaded",name);
158: error(errbuf);
159: }
160: break;
161: default:
162: error("load : invalid argument");
163: break;
164: }
165: }
166: STOQ(ret,*rp);
167: }
168:
169: void Pwhich(arg,rp)
170: NODE arg;
171: STRING *rp;
172: {
173: char *name;
174: STRING str;
175:
176: switch (OID(ARG0(arg))) {
177: case O_STR:
178: searchasirpath(BDY((STRING)ARG0(arg)),&name);
179: break;
180: default:
181: name = 0;
182: break;
183: }
184: if ( name ) {
185: MKSTR(str,name); *rp = str;
186: } else
187: *rp = 0;
188: }
189:
190: void Ploadfiles(arg,rp)
191: NODE arg;
192: Q *rp;
193: {
194: int ret;
195:
196: if ( ARG0(arg) )
197: if ( OID(ARG0(arg)) != O_LIST )
198: ret = 0;
199: else
200: ret = loadfiles(BDY((LIST)ARG0(arg)));
201: else
202: ret = 0;
203: STOQ(ret,*rp);
204: }
205:
206: void Poutput(arg,rp)
207: NODE arg;
208: Q *rp;
209: {
210: #if PARI
211: extern FILE *outfile;
212: #endif
213: FILE *fp;
214:
215: fflush(asir_out);
216: if ( asir_out != stdout )
217: fclose(asir_out);
218: switch ( argc(arg) ) {
219: case 0:
220: fp = stdout; break;
221: case 1:
222: asir_assert(ARG0(arg),O_STR,"output");
223: fp = fopen(((STRING)ARG0(arg))->body,"a+");
224: if ( !fp )
225: error("output : invalid filename");
226: break;
227: }
228: #if PARI
229: pari_outfile =
230: #endif
231: asir_out = fp;
232: *rp = ONE;
233: }
234:
235: extern int ox_file_io;
236:
237: void Pbsave(arg,rp)
238: NODE arg;
239: Q *rp;
240: {
241: FILE *fp;
242: VL vl,t;
243:
244: asir_assert(ARG1(arg),O_STR,"bsave");
245: get_vars_recursive(ARG0(arg),&vl);
246: for ( t = vl; t; t = NEXT(t) )
247: if ( t->v->attr == (pointer)V_UC )
248: error("bsave : not implemented");
249: #if defined(THINK_C) || defined(VISUAL)
250: fp = fopen(BDY((STRING)ARG1(arg)),"wb");
251: #else
252: fp = fopen(BDY((STRING)ARG1(arg)),"w");
253: #endif
254: if ( !fp )
255: error("bsave : invalid filename");
256: ox_file_io = 1; /* network byte order is used */
257: savevl(fp,vl);
258: saveobj(fp,ARG0(arg));
259: fclose(fp);
260: ox_file_io = 0;
261: *rp = ONE;
262: }
263:
264: void Pbload(arg,rp)
265: NODE arg;
266: Obj *rp;
267: {
268: FILE *fp;
269:
270: asir_assert(ARG0(arg),O_STR,"bload");
271: #if defined(THINK_C) || defined(VISUAL)
272: fp = fopen(BDY((STRING)ARG0(arg)),"rb");
273: #else
274: fp = fopen(BDY((STRING)ARG0(arg)),"r");
275: #endif
276: if ( !fp )
277: error("bload : invalid filename");
278: ox_file_io = 1; /* network byte order is used */
279: loadvl(fp);
280: loadobj(fp,rp);
281: fclose(fp);
282: ox_file_io = 0;
283: }
284:
285: void Pbsave_cmo(arg,rp)
286: NODE arg;
287: Q *rp;
288: {
289: FILE *fp;
290: VL vl,t;
291:
292: asir_assert(ARG1(arg),O_STR,"bsave_cmo");
293: #if defined(THINK_C) || defined(VISUAL)
294: fp = fopen(BDY((STRING)ARG1(arg)),"wb");
295: #else
296: fp = fopen(BDY((STRING)ARG1(arg)),"w");
297: #endif
298: if ( !fp )
299: error("bsave_cmo : invalid filename");
300: ox_file_io = 1; /* network byte order is used */
301: write_cmo(fp,ARG0(arg));
302: fclose(fp);
303: ox_file_io = 0;
304: *rp = ONE;
305: }
306:
307: void Pbload_cmo(arg,rp)
308: NODE arg;
309: Obj *rp;
310: {
311: FILE *fp;
312:
313: asir_assert(ARG0(arg),O_STR,"bload_cmo");
314: #if defined(THINK_C) || defined(VISUAL)
315: fp = fopen(BDY((STRING)ARG0(arg)),"rb");
316: #else
317: fp = fopen(BDY((STRING)ARG0(arg)),"r");
318: #endif
319: if ( !fp )
320: error("bload_cmo : invalid filename");
321: ox_file_io = 1; /* network byte order is used */
322: read_cmo(fp,rp);
323: fclose(fp);
324: ox_file_io = 0;
325: }
326:
327: #if defined(VISUAL)
328: void get_rootdir(name,len)
329: char *name;
330: int len;
331: {
332: LONG ret;
333: HKEY hOpenKey;
334: DWORD Type;
335:
336: name[0] = 0;
337: ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ECGEN_KEYNAME, 0,
338: KEY_QUERY_VALUE, &hOpenKey);
339: if ( ret != ERROR_SUCCESS )
340: ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ASIR_KEYNAME, 0,
341: KEY_QUERY_VALUE, &hOpenKey);
342: if( ret == ERROR_SUCCESS ) {
343: RegQueryValueEx(hOpenKey, "Directory", NULL, &Type, name, &len);
344: RegCloseKey(hOpenKey);
345: }
346: }
347: #else
348: void get_rootdir(name,len)
349: char *name;
350: int len;
351: {
352: strcpy(name,asir_libdir);
353: }
354: #endif
355:
356: void Pget_rootdir(rp)
357: STRING *rp;
358: {
359: static struct oSTRING rootdir;
360: static char DirName[BUFSIZ];
361: int DirNameLen;
362:
363: if ( !rootdir.body ) {
364: get_rootdir(DirName,sizeof(DirName));
365: rootdir.id = O_STR;
366: rootdir.body = DirName;
367: }
368: *rp = &rootdir;
369: }
370:
371: void Pbsave_enc(arg,rp)
372: NODE arg;
373: Obj *rp;
374: {
375: init_deskey();
376: des_encryption = 1;
377: Pbsave(arg,rp);
378: des_encryption = 0;
379: }
380:
381: void Pbload_enc(arg,rp)
382: NODE arg;
383: Obj *rp;
384: {
385: init_deskey();
386: des_encryption = 1;
387: Pbload(arg,rp);
388: des_encryption = 0;
389: }
390:
391: void Pbload27(arg,rp)
392: NODE arg;
393: Obj *rp;
394: {
395: FILE *fp;
396: Obj r;
397:
398: asir_assert(ARG0(arg),O_STR,"bload27");
399: #if defined(THINK_C) || defined(VISUAL)
400: fp = fopen(BDY((STRING)ARG0(arg)),"rb");
401: #else
402: fp = fopen(BDY((STRING)ARG0(arg)),"r");
403: #endif
404: if ( !fp )
405: error("bload : invalid filename");
406: loadvl(fp);
407: loadobj(fp,&r);
408: fclose(fp);
409: bobjtoobj(BASE27,r,rp);
410: }
411:
412: void Pbsave_compat(arg,rp)
413: NODE arg;
414: Q *rp;
415: {
416: FILE *fp;
417: VL vl,t;
418:
419: asir_assert(ARG1(arg),O_STR,"bsave_compat");
420: get_vars_recursive(ARG0(arg),&vl);
421: for ( t = vl; t; t = NEXT(t) )
422: if ( t->v->attr == (pointer)V_UC )
423: error("bsave : not implemented");
424: #if defined(THINK_C) || defined(VISUAL)
425: fp = fopen(BDY((STRING)ARG1(arg)),"wb");
426: #else
427: fp = fopen(BDY((STRING)ARG1(arg)),"w");
428: #endif
429: if ( !fp )
430: error("bsave : invalid filename");
431: /* indicator of an asir32 file */
432: putw(0,fp); putw(0,fp);
433: savevl(fp,vl);
434: saveobj(fp,ARG0(arg));
435: fclose(fp);
436: *rp = ONE;
437: }
438:
439: void Pbload_compat(arg,rp)
440: NODE arg;
441: Obj *rp;
442: {
443: FILE *fp;
444: unsigned int hdr[2];
445: Obj r;
446: int c;
447:
448: asir_assert(ARG0(arg),O_STR,"bload_compat");
449: #if defined(THINK_C) || defined(VISUAL)
450: fp = fopen(BDY((STRING)ARG0(arg)),"rb");
451: #else
452: fp = fopen(BDY((STRING)ARG0(arg)),"r");
453: #endif
454: if ( !fp )
455: error("bload : invalid filename");
456: fread(hdr,sizeof(unsigned int),2,fp);
457: if ( !hdr[0] && !hdr[1] ) {
458: /* asir32 file or asir27 0 */
459: c = fgetc(fp);
460: if ( c == EOF ) {
461: /* asir27 0 */
462: *rp = 0;
463: } else {
464: /* asir32 file */
465: ungetc(c,fp);
466: loadvl(fp);
467: loadobj(fp,rp);
468: }
469: } else {
470: /* asir27 file */
471: rewind(fp);
472: loadvl(fp);
473: loadobj(fp,&r);
474: bobjtoobj(BASE27,r,rp);
475: }
476: fclose(fp);
477: }
478:
479: void Premove_file(arg,rp)
480: NODE arg;
481: Q *rp;
482: {
483: unlink((char *)BDY((STRING)ARG0(arg)));
484: *rp = ONE;
485: }
486:
487: void Paccess(arg,rp)
488: NODE arg;
489: Q *rp;
490: {
491: char *name;
492: STRING str;
493:
494: #if defined(VISUAL)
495: if ( access(BDY((STRING)ARG0(arg)),04) >= 0 )
496: #else
497: if ( access(BDY((STRING)ARG0(arg)),R_OK) >= 0 )
498: #endif
499: *rp = ONE;
500: else
501: *rp = 0;
502: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>