Annotation of OpenXM_contrib2/asir2000/parse/load.c, Revision 1.3
1.3 ! 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@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: *
! 48: * $OpenXM: OpenXM_contrib2/asir2000/parse/load.c,v 1.2 2000/01/19 05:05:04 noro Exp $
! 49: */
1.1 noro 50: #include "ca.h"
51: #include "parse.h"
52: #if defined(THINK_C)
53: #include <string.h>
54: #include <StandardFile.h>
55: #include <Windows.h>
56: #include <OSUtils.h>
57: #include <ToolUtils.h>
58: #else
59: #if defined(VISUAL)
60: #include <string.h>
61: #include <fcntl.h>
62: #if defined(GO32)
63: #include <unistd.h>
64: #endif
65: #include <sys/stat.h>
66: #else
67: #if defined(_PA_RISC1_1) || defined(SYSV) || defined(__SVR4)
68: #include <unistd.h>
69: #include <string.h>
70: #else
71: #include <strings.h>
72: #endif
73: #include <sys/types.h>
74: #include <sys/file.h>
75: #include <sys/stat.h>
76: #endif
77: #endif
78:
79: #if defined(linux)
80: #include <unistd.h>
81: #endif
82:
83: #if defined(VISUAL)
84: #include <io.h>
85: #endif
86:
87: #undef MALLOC
88: #define MALLOC(x) GC_malloc((x)+4)
89:
90: char *ASIRLOADPATH[32];
91:
92: void encrypt_file(char *,char *);
93: void decrypt_file(char *,char *);
94:
95: #if defined(THINK_C)
96: void searchasirpath(char *,char **);
97: void loadasirfile(char *);
98:
99: short initvol;
100:
101: void initVol();
102: short getVol();
103: void setDir(short);
104: void resetDir();
105:
106: void initVol() {
107: char buf[BUFSIZ];
108:
109: GetVol((StringPtr)buf,&initvol);
110: }
111:
112: short getVol() {
113: short vol;
114: char buf[BUFSIZ];
115:
116: GetVol((StringPtr)buf,&vol);
117: return vol;
118: }
119:
120: void setDir(short vol)
121: {
122: SetVol(0,vol);
123: }
124:
125: void resetDir() {
126: SetVol(0,initvol);
127: }
128: #endif
129:
130: #if defined(VISUAL)
131: #define ENVDELIM ';'
132: #define MORE "more < "
133: #else
134: #define ENVDELIM ':'
135: #define MORE "more"
136: #endif
137:
138: #ifndef ASIR_LIBDIR
139: #define ASIR_LIBDIR "."
140: #endif
141:
142: char *getenv();
143: extern char *asir_libdir;
144: extern char *asir_pager;
145: extern int read_exec_file;
146: extern int main_parser;
147:
148: void env_init() {
149: char *e,*p,*q;
150: int i,l;
151: char *getenv();
152: char *oxhome;
153: char rootname[BUFSIZ];
154:
155: if ( !(asir_libdir = getenv("ASIR_LIBDIR")) ) {
156: if ( oxhome = getenv("OpenXM_HOME") ) {
157: asir_libdir = (char *)malloc(strlen(oxhome)+strlen("/lib/asir")+1);
158: sprintf(asir_libdir,"%s/lib/asir",oxhome);
159: } else {
160: #if defined(VISUAL)
161: get_rootdir(rootname,sizeof(rootname));
162: asir_libdir = (char *)malloc(strlen(rootname)+strlen("/lib")+1);
163: sprintf(asir_libdir,"%s/lib",rootname);
164: #else
165: asir_libdir = (char *)malloc(strlen(ASIR_LIBDIR)+1);
166: strcpy(asir_libdir,ASIR_LIBDIR);
167: #endif
168: }
169: }
170: if ( !(asir_pager = getenv("PAGER")) ) {
171: asir_pager = (char *)malloc(strlen(MORE)+1);
172: strcpy(asir_pager,MORE);
173: }
174: if ( e = getenv("ASIRLOADPATH" ) )
175: for ( i = 0; ; i++, e = p+1 ) {
176: p = (char *)index(e,ENVDELIM);
177: l = p ? p-e : strlen(e); q = (char *)MALLOC(l+1);
178: if ( l ) {
179: strncpy(q,e,l); q[l] = 0; ASIRLOADPATH[i] = q;
180: }
181: if ( !p )
182: break;
183: }
184: for ( i = 0; ASIRLOADPATH[i]; i++ );
185: ASIRLOADPATH[i] = asir_libdir;
186: }
187:
188: void searchasirpath(name,pathp)
189: char *name;
190: char **pathp;
191: {
192: char **p;
193: char *q;
194: int l;
195: #if !defined(THINK_C) && !defined(VISUAL)
196: struct stat sbuf;
197:
198: if ( (name[0] == '/') || ( name[0] == '.') || strchr(name,':')
199: || !ASIRLOADPATH[0] ) {
200: if ( access(name,R_OK) >= 0 ) {
201: stat(name,&sbuf);
202: if ( (sbuf.st_mode & S_IFMT) != S_IFDIR )
203: *pathp = name;
204: else
205: *pathp = 0;
206: } else
207: *pathp = 0;
208: } else {
209: for ( p = ASIRLOADPATH; *p; p++ ) {
210: l = strlen(*p)+strlen(name)+2;
211: q = (char *)ALLOCA(l); sprintf(q,"%s/%s",*p,name);
212: if ( access(q,R_OK) >= 0 ) {
213: stat(q,&sbuf);
214: if ( (sbuf.st_mode & S_IFMT) != S_IFDIR ) {
215: *pathp = (char *)MALLOC(l); strcpy(*pathp,q);
216: return;
217: }
218: }
219: }
220: *pathp = 0;
221: }
222: #elif defined(VISUAL)
223: if ( (name[0] == '/') || ( name[0] == '.') || strchr(name,':')
224: || !ASIRLOADPATH[0] )
225: *pathp = name;
226: else {
227: for ( p = ASIRLOADPATH; *p; p++ ) {
228: l = strlen(*p)+strlen(name)+2;
229: q = (char *)ALLOCA(l); sprintf(q,"%s/%s",*p,name);
230: if ( access(q,04) >= 0 ) {
231: *pathp = (char *)MALLOC(l); strcpy(*pathp,q);
232: return;
233: }
234: }
235: *pathp = 0;
236: }
237: #else
238: if ( (name[0] == '/') || ( name[0] == '.') || !ASIRLOADPATH[0] )
239: *pathp = name;
240: else {
241: for ( p = ASIRLOADPATH; *p; p++ ) {
242: l = strlen(*p)+strlen(name)+2;
243: q = (char *)ALLOCA(l); sprintf(q,"%s/%s",*p,name);
244: *pathp = (char *)MALLOC(l); strcpy(*pathp,q);
245: return;
246: }
247: *pathp = 0;
248: }
249: #endif
250: }
251:
252: #if defined(THINK_C)
253: #define DELIM ':'
254: #elif defined(VISUAL)
255: #define DELIM '/'
256: #endif
257:
258: void Eungetc(int,FILE *);
259:
260: void loadasirfile(name0)
261: char *name0;
262: {
263: FILE *in;
264: IN t;
265: extern char cppname[];
266: #if defined(THINK_C) || defined(VISUAL)
267: char tname[BUFSIZ],dname[BUFSIZ],ibuf1[BUFSIZ],ibuf2[BUFSIZ];
268: int ac;
269: #if defined(__MWERKS__)
270: char *av[64];
271: #else
272: char *av[BUFSIZ];
273: #endif
274: char *p,*c;
275: FILE *fp;
276: char name[BUFSIZ],tname0[BUFSIZ];
277: int encoded;
278:
279: #if defined(VISUAL)
280: fp = fopen(name0,"rb");
281: if ( getc(fp) == 0xff ) {
282: /* encoded file */
283: fclose(fp);
284: sprintf(name,"%s.$$$",name0);
285: decrypt_file(name0,name);
286: encoded = 1;
287: } else {
288: fclose(fp);
289: strcpy(name,name0);
290: encoded = 0;
291: }
292: #else
293: strcpy(name,name0);
294: #endif
295:
296: strcpy(dname,name);
297: p = strrchr(dname,DELIM);
298: av[0] = "cpp"; av[1] = name; av[2] = tname;
299: sprintf(ibuf1,"-I%s",asir_libdir); av[3] = ibuf1; av[4] = "-DWINDOWS";
300: if ( !p ) {
301: sprintf(tname,"%s.___",name); av[5] = 0; ac = 5;
302: } else {
303: *p++ = 0;
304: #if defined(VISUAL)
305: if ( c = strchr(dname,':') ) {
306: *c = 0;
307: sprintf(tname,"%s:%s%c%s.___",dname,c+1,DELIM,p);
308: *c = ':';
309: } else
310:
311: #endif
312: sprintf(tname,"%s%c%s.___",dname,DELIM,p);
313: sprintf(ibuf2,"-I%s",dname); av[5] = ibuf2; av[6] = 0; ac = 6;
314: }
315: cpp_main(ac,av);
316: if ( encoded ) {
317: unlink(name);
318: strcpy(tname0,tname);
319: sprintf(tname,"%s.###",tname0);
320: encrypt_file(tname0,tname);
321: unlink(tname0);
322: in = fopen(tname,"rb");
323: } else
324: in = fopen(tname,"r");
325: if ( !in ) {
326: perror("fopen");
327: error("load : failed");
328: }
329: t = (IN)MALLOC(sizeof(struct oIN));
330: t->name = (char *)MALLOC(strlen(name)+1); strcpy(t->name,name);
331: t->tname = (char *)MALLOC(strlen(tname)+1); strcpy(t->tname,tname);
332: t->encoded = encoded;
333: #if defined(THINK_C)
334: t->vol = getVol();
335: #endif
336: #else
337: char com[BUFSIZ];
338:
339: sprintf(com,"%s -I%s %s",cppname,asir_libdir,name0); in = popen(com,"r");
340: if ( !in ) {
341: perror("popen");
342: error("load : failed");
343: }
344: t = (IN)MALLOC(sizeof(struct oIN));
345: t->name = (char *)MALLOC(strlen(name0)+1); strcpy(t->name,name0);
346: #endif
347: t->fp = in; t->ln = 1; t->next = asir_infile; asir_infile = t;
348: main_parser = 1; /* XXX */
349: Eungetc(afternl(),asir_infile->fp);
350: if ( !EPVS->va )
351: reallocarray((char **)&EPVS->va,(int *)&EPVS->asize,(int *)&EPVS->n,(int)sizeof(struct oPV));
352: }
353:
354: void execasirfile(name)
355: char *name;
356: {
357: loadasirfile(name);
358: read_exec_file = 1;
359: read_eval_loop();
360: read_exec_file = 0;
361: }
362:
363: static NODE objfile = 0;
364:
365: int loadfile(s)
366: char *s;
367: {
368: FILE *in;
369:
370: if ( in = fopen(s,"r") ) {
371: fclose(in);
372: loadasirfile(s);
373: return 1;
374: } else
375: return 0;
376: }
377:
378: int loadfiles(node) NODE node; { return 0; }
379:
380: static unsigned char encrypt_tab[128][2] = {
381: {137,40},{1,194},{133,79},{48,20},{254,76},{98,17},{110,233},{19,231},
382: {55,223},{75,65},{178,151},{85,222},{201,46},{51,243},{235,61},{106,113},
383: {116,121},{24,205},{146,244},{89,234},{163,173},{140,162},{188,45},{195,132},
384: {202,94},{236,87},{2,127},{253,47},{211,138},{58,252},{142,190},{77,209},
385: {16,53},{200,220},{99,165},{28,59},{78,0},{208,248},{50,229},{217,39},
386: {112,15},{18,60},{108,175},{10,67},{23,176},{49,245},{160,27},{171,219},
387: {105,144},{122,172},{255,114},{226,100},{70,117},{197,11},{180,36},{136,101},
388: {238,212},{125,120},{103,199},{38,119},{7,206},{181,228},{62,3},{30,185},
389: {154,63},{247,81},{187,80},{225,56},{210,221},{37,135},{155,52},{54,153},
390: {84,246},{166,90},{124,167},{41,184},{145,204},{147,198},{25,21},{72,97},
391: {66,128},{95,139},{93,42},{224,43},{35,143},{111,13},{82,249},{12,148},
392: {32,152},{186,168},{177,115},{216,251},{5,131},{123,170},{149,161},{213,203},
393: {126,150},{88,158},{74,169},{159,182},{156,26},{34,104},{8,91},{207,183},
394: {9,64},{83,241},{134,102},{69,33},{179,237},{129,250},{14,71},{230,4},
395: {218,6},{189,68},{193,22},{57,174},{215,214},{130,92},{240,31},{118,239},
396: {109,192},{232,196},{86,107},{96,141},{157,227},{164,242},{44,191},{29,73}
397: };
398:
399: static unsigned char decrypt_tab[] = {
400: 36,1,26,62,111,92,112,60,102,104,43,53,87,85,110,40,
401: 32,5,41,7,3,78,114,44,17,78,100,46,35,127,63,118,
402: 88,107,101,84,54,69,59,39,0,75,82,83,126,22,12,27,
403: 3,45,38,13,70,32,71,8,67,115,29,35,41,14,62,64,
404: 104,9,80,43,113,107,52,110,79,127,98,9,4,31,36,2,
405: 66,65,86,105,72,11,122,25,97,19,73,102,117,82,24,81,
406: 123,79,5,34,51,55,106,58,101,48,15,122,42,120,6,85,
407: 40,15,50,90,16,52,119,59,57,16,49,93,74,57,96,26,
408: 80,109,117,92,23,2,106,69,55,0,28,81,21,123,30,84,
409: 48,76,18,77,87,94,96,10,88,71,64,70,100,124,97,99,
410: 46,94,21,20,125,34,73,74,89,98,93,47,49,20,115,42,
411: 44,90,10,108,54,61,99,103,75,63,89,66,22,113,30,126,
412: 120,114,1,23,121,53,77,58,33,12,24,95,76,17,60,103,
413: 37,31,68,28,56,95,116,116,91,39,112,47,33,68,11,8,
414: 83,67,51,124,61,38,111,7,121,6,19,14,25,108,56,119,
415: 118,105,125,13,18,45,72,65,37,86,109,91,29,27,4,50
416: };
417:
418: unsigned char encrypt_char(unsigned char c)
419: {
420: return encrypt_tab[c][mt_genrand()&1];
421: }
422:
423: unsigned char decrypt_char(unsigned char c)
424: {
425: return decrypt_tab[c];
426: }
427:
428: void encrypt_file(in,out)
429: char *in,*out;
430: {
431: FILE *infp,*outfp;
432: int c;
433:
434: infp = fopen(in,"r");
435: outfp = fopen(out,"wb");
436: while ( 1 ) {
437: c = getc(infp);
438: if ( c == EOF )
439: break;
440: putc(encrypt_char(c),outfp);
441: }
442: fclose(infp); fclose(outfp);
443: }
444:
445: void decrypt_file(in,out)
446: char *in,*out;
447: {
448: FILE *infp,*outfp;
449: int c;
450:
451: infp = fopen(in,"rb");
452: outfp = fopen(out,"w");
453: /* skip the magic number (=0xff) */
454: getc(infp);
455: while ( 1 ) {
456: c = getc(infp);
457: if ( c == EOF )
458: break;
459: putc(decrypt_char(c),outfp);
460: }
461: fclose(infp); fclose(outfp);
462: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>