Annotation of OpenXM_contrib2/asir2000/parse/load.c, Revision 1.1.1.1
1.1 noro 1: /* $OpenXM: OpenXM/src/asir99/parse/load.c,v 1.1.1.1 1999/11/10 08:12:34 noro Exp $ */
2: #include "ca.h"
3: #include "parse.h"
4: #if defined(THINK_C)
5: #include <string.h>
6: #include <StandardFile.h>
7: #include <Windows.h>
8: #include <OSUtils.h>
9: #include <ToolUtils.h>
10: #else
11: #if defined(VISUAL)
12: #include <string.h>
13: #include <fcntl.h>
14: #if defined(GO32)
15: #include <unistd.h>
16: #endif
17: #include <sys/stat.h>
18: #else
19: #if defined(_PA_RISC1_1) || defined(SYSV) || defined(__SVR4)
20: #include <unistd.h>
21: #include <string.h>
22: #else
23: #include <strings.h>
24: #endif
25: #include <sys/types.h>
26: #include <sys/file.h>
27: #include <sys/stat.h>
28: #endif
29: #endif
30:
31: #if defined(linux)
32: #include <unistd.h>
33: #endif
34:
35: #if defined(VISUAL)
36: #include <io.h>
37: #endif
38:
39: #undef MALLOC
40: #define MALLOC(x) GC_malloc((x)+4)
41:
42: char *ASIRLOADPATH[32];
43:
44: void encrypt_file(char *,char *);
45: void decrypt_file(char *,char *);
46:
47: #if defined(THINK_C)
48: void searchasirpath(char *,char **);
49: void loadasirfile(char *);
50:
51: short initvol;
52:
53: void initVol();
54: short getVol();
55: void setDir(short);
56: void resetDir();
57:
58: void initVol() {
59: char buf[BUFSIZ];
60:
61: GetVol((StringPtr)buf,&initvol);
62: }
63:
64: short getVol() {
65: short vol;
66: char buf[BUFSIZ];
67:
68: GetVol((StringPtr)buf,&vol);
69: return vol;
70: }
71:
72: void setDir(short vol)
73: {
74: SetVol(0,vol);
75: }
76:
77: void resetDir() {
78: SetVol(0,initvol);
79: }
80: #endif
81:
82: #if defined(VISUAL)
83: #define ENVDELIM ';'
84: #define MORE "more < "
85: #else
86: #define ENVDELIM ':'
87: #define MORE "more"
88: #endif
89:
90: #ifndef ASIR_LIBDIR
91: #define ASIR_LIBDIR "."
92: #endif
93:
94: char *getenv();
95: extern char *asir_libdir;
96: extern char *asir_pager;
97: extern int read_exec_file;
98: extern int main_parser;
99:
100: void env_init() {
101: char *e,*p,*q;
102: int i,l;
103: char *getenv();
104: char *oxhome;
105: char rootname[BUFSIZ];
106:
107: if ( !(asir_libdir = getenv("ASIR_LIBDIR")) ) {
108: if ( oxhome = getenv("OpenXM_HOME") ) {
109: asir_libdir = (char *)malloc(strlen(oxhome)+strlen("/lib/asir")+1);
110: sprintf(asir_libdir,"%s/lib/asir",oxhome);
111: } else {
112: #if defined(VISUAL)
113: get_rootdir(rootname,sizeof(rootname));
114: asir_libdir = (char *)malloc(strlen(rootname)+strlen("/lib")+1);
115: sprintf(asir_libdir,"%s/lib",rootname);
116: #else
117: asir_libdir = (char *)malloc(strlen(ASIR_LIBDIR)+1);
118: strcpy(asir_libdir,ASIR_LIBDIR);
119: #endif
120: }
121: }
122: if ( !(asir_pager = getenv("PAGER")) ) {
123: asir_pager = (char *)malloc(strlen(MORE)+1);
124: strcpy(asir_pager,MORE);
125: }
126: if ( e = getenv("ASIRLOADPATH" ) )
127: for ( i = 0; ; i++, e = p+1 ) {
128: p = (char *)index(e,ENVDELIM);
129: l = p ? p-e : strlen(e); q = (char *)MALLOC(l+1);
130: if ( l ) {
131: strncpy(q,e,l); q[l] = 0; ASIRLOADPATH[i] = q;
132: }
133: if ( !p )
134: break;
135: }
136: for ( i = 0; ASIRLOADPATH[i]; i++ );
137: ASIRLOADPATH[i] = asir_libdir;
138: }
139:
140: void searchasirpath(name,pathp)
141: char *name;
142: char **pathp;
143: {
144: char **p;
145: char *q;
146: int l;
147: #if !defined(THINK_C) && !defined(VISUAL)
148: struct stat sbuf;
149:
150: if ( (name[0] == '/') || ( name[0] == '.') || strchr(name,':')
151: || !ASIRLOADPATH[0] ) {
152: if ( access(name,R_OK) >= 0 ) {
153: stat(name,&sbuf);
154: if ( (sbuf.st_mode & S_IFMT) != S_IFDIR )
155: *pathp = name;
156: else
157: *pathp = 0;
158: } else
159: *pathp = 0;
160: } else {
161: for ( p = ASIRLOADPATH; *p; p++ ) {
162: l = strlen(*p)+strlen(name)+2;
163: q = (char *)ALLOCA(l); sprintf(q,"%s/%s",*p,name);
164: if ( access(q,R_OK) >= 0 ) {
165: stat(q,&sbuf);
166: if ( (sbuf.st_mode & S_IFMT) != S_IFDIR ) {
167: *pathp = (char *)MALLOC(l); strcpy(*pathp,q);
168: return;
169: }
170: }
171: }
172: *pathp = 0;
173: }
174: #elif defined(VISUAL)
175: if ( (name[0] == '/') || ( name[0] == '.') || strchr(name,':')
176: || !ASIRLOADPATH[0] )
177: *pathp = name;
178: else {
179: for ( p = ASIRLOADPATH; *p; p++ ) {
180: l = strlen(*p)+strlen(name)+2;
181: q = (char *)ALLOCA(l); sprintf(q,"%s/%s",*p,name);
182: if ( access(q,04) >= 0 ) {
183: *pathp = (char *)MALLOC(l); strcpy(*pathp,q);
184: return;
185: }
186: }
187: *pathp = 0;
188: }
189: #else
190: if ( (name[0] == '/') || ( name[0] == '.') || !ASIRLOADPATH[0] )
191: *pathp = name;
192: else {
193: for ( p = ASIRLOADPATH; *p; p++ ) {
194: l = strlen(*p)+strlen(name)+2;
195: q = (char *)ALLOCA(l); sprintf(q,"%s/%s",*p,name);
196: *pathp = (char *)MALLOC(l); strcpy(*pathp,q);
197: return;
198: }
199: *pathp = 0;
200: }
201: #endif
202: }
203:
204: #if defined(THINK_C)
205: #define DELIM ':'
206: #elif defined(VISUAL)
207: #define DELIM '/'
208: #endif
209:
210: void Eungetc(int,FILE *);
211:
212: void loadasirfile(name0)
213: char *name0;
214: {
215: FILE *in;
216: IN t;
217: extern char cppname[];
218: #if defined(THINK_C) || defined(VISUAL)
219: char tname[BUFSIZ],dname[BUFSIZ],ibuf1[BUFSIZ],ibuf2[BUFSIZ];
220: int ac;
221: #if defined(__MWERKS__)
222: char *av[64];
223: #else
224: char *av[BUFSIZ];
225: #endif
226: char *p,*c;
227: FILE *fp;
228: char name[BUFSIZ],tname0[BUFSIZ];
229: int encoded;
230:
231: #if defined(VISUAL)
232: fp = fopen(name0,"rb");
233: if ( getc(fp) == 0xff ) {
234: /* encoded file */
235: fclose(fp);
236: sprintf(name,"%s.$$$",name0);
237: decrypt_file(name0,name);
238: encoded = 1;
239: } else {
240: fclose(fp);
241: strcpy(name,name0);
242: encoded = 0;
243: }
244: #else
245: strcpy(name,name0);
246: #endif
247:
248: strcpy(dname,name);
249: p = strrchr(dname,DELIM);
250: av[0] = "cpp"; av[1] = name; av[2] = tname;
251: sprintf(ibuf1,"-I%s",asir_libdir); av[3] = ibuf1; av[4] = "-DWINDOWS";
252: if ( !p ) {
253: sprintf(tname,"%s.___",name); av[5] = 0; ac = 5;
254: } else {
255: *p++ = 0;
256: #if defined(VISUAL)
257: if ( c = strchr(dname,':') ) {
258: *c = 0;
259: sprintf(tname,"%s:%s%c%s.___",dname,c+1,DELIM,p);
260: *c = ':';
261: } else
262:
263: #endif
264: sprintf(tname,"%s%c%s.___",dname,DELIM,p);
265: sprintf(ibuf2,"-I%s",dname); av[5] = ibuf2; av[6] = 0; ac = 6;
266: }
267: cpp_main(ac,av);
268: if ( encoded ) {
269: unlink(name);
270: strcpy(tname0,tname);
271: sprintf(tname,"%s.###",tname0);
272: encrypt_file(tname0,tname);
273: unlink(tname0);
274: in = fopen(tname,"rb");
275: } else
276: in = fopen(tname,"r");
277: if ( !in ) {
278: perror("fopen");
279: error("load : failed");
280: }
281: t = (IN)MALLOC(sizeof(struct oIN));
282: t->name = (char *)MALLOC(strlen(name)+1); strcpy(t->name,name);
283: t->tname = (char *)MALLOC(strlen(tname)+1); strcpy(t->tname,tname);
284: t->encoded = encoded;
285: #if defined(THINK_C)
286: t->vol = getVol();
287: #endif
288: #else
289: char com[BUFSIZ];
290:
291: sprintf(com,"%s -I%s %s",cppname,asir_libdir,name0); in = popen(com,"r");
292: if ( !in ) {
293: perror("popen");
294: error("load : failed");
295: }
296: t = (IN)MALLOC(sizeof(struct oIN));
297: t->name = (char *)MALLOC(strlen(name0)+1); strcpy(t->name,name0);
298: #endif
299: t->fp = in; t->ln = 1; t->next = asir_infile; asir_infile = t;
300: main_parser = 1; /* XXX */
301: Eungetc(afternl(),asir_infile->fp);
302: if ( !EPVS->va )
303: reallocarray((char **)&EPVS->va,(int *)&EPVS->asize,(int *)&EPVS->n,(int)sizeof(struct oPV));
304: }
305:
306: void execasirfile(name)
307: char *name;
308: {
309: loadasirfile(name);
310: read_exec_file = 1;
311: read_eval_loop();
312: read_exec_file = 0;
313: }
314:
315: static NODE objfile = 0;
316:
317: #if defined(apollo) || defined(VISUAL) || defined(_PA_RISC1_1) || defined(__alpha) || defined(linux) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(SYSV) || defined(__SVR4)
318:
319: int loadfile(s)
320: char *s;
321: {
322: FILE *in;
323:
324: if ( in = fopen(s,"r") ) {
325: fclose(in);
326: loadasirfile(s);
327: return 1;
328: } else
329: return 0;
330: }
331:
332: int loadfiles(node) NODE node; { return 0; }
333:
334: #else
335: #if defined(THINK_C)
336:
337: int loadfile(s)
338: char *s;
339: {
340: FILE *in;
341:
342: if ( in = fopen(s,"r") ) {
343: loadasirfile(s); fclose(in);
344: return 1;
345: } else
346: return 0;
347: }
348:
349: int loadfiles(node) NODE node; { return 0; }
350:
351: int finder_loadfile() {
352: Point p;
353: SFTypeList t;
354: SFReply r;
355: int ret;
356:
357: p.h = p.v = 50; t[0] = 'TEXT';
358: SFGetFile(p,"\p",0,1,t,0,&r);
359: if ( r.good ) {
360: setDir(r.vRefNum);
361: ret = loadfile(PtoCstr(r.fName));
362: resetDir();
363: return ret;
364: } else
365: return 0;
366: }
367:
368: #else
369: #if defined(NeXT)
370: #include <rld.h>
371: #include <ldsyms.h>
372:
373: void loadmachofile(char **);
374: void unloadmachofile(void);
375:
376: int loadfile(s)
377: char *s;
378: {
379: FILE *in;
380: unsigned magic;
381: char **oname;
382:
383: if ( in = fopen(s,"r") ) {
384: fread(&magic,sizeof(unsigned),1,in); fclose(in);
385: if (magic == MH_MAGIC) {
386: oname = (char **)ALLOCA(BUFSIZ);
387: oname[0] = s; oname[1] = "/usr/lib/libm.a",oname[2] = 0;
388: loadmachofile(oname);
389: } else
390: loadasirfile(s);
391: return 1;
392: } else
393: return 0;
394: }
395:
396: int loadfiles(node)
397: NODE node;
398: {
399: int i;
400: NODE n;
401: char *name;
402: char **oname;
403:
404: for ( i = 0, n = node; n; i++, n = NEXT(n) );
405: if ( OID(BDY(n)) != O_STR )
406: return 0;
407: oname = (char **)ALLOCA(i+1);
408: for ( i = 0, n = node; n; i++, n = NEXT(n) ) {
409: searchasirpath(BDY((STRING)BDY(node)),&name);
410: if ( !name )
411: return 0;
412: else
413: oname[i] = name;
414: }
415: oname[i] = 0;
416: loadmachofile(oname);
417: return 1;
418: }
419:
420: static NXStream *nxs = 0;
421:
422: void loadmachofile(oname)
423: char **oname;
424: {
425: struct mach_header *h;
426: unsigned int data_start,data_end;
427: struct section *tsec,*dsec,*bsec,*csec;
428: struct section *getsectbynamefromheader();
429:
430: if ( !nxs )
431: nxs = NXOpenFile(fileno(stderr),NX_WRITEONLY);
432: if ( rld_load(nxs,&h,oname,"/tmp/afo") ) {
433: tsec = getsectbynamefromheader(h,"__TEXT","__text");
434: dsec = getsectbynamefromheader(h,"__DATA","__data");
435: bsec = getsectbynamefromheader(h,"__DATA","__bss");
436: csec = getsectbynamefromheader(h,"__DATA","__common");
437: data_start = dsec->addr; data_end = dsec->addr+dsec->size;
438: if ( bsec->size ) {
439: data_start = MIN(data_start,bsec->addr);
440: data_end = MAX(data_end,(bsec->addr+bsec->size));
441: }
442: if ( csec->size ) {
443: data_start = MIN(data_start,csec->addr);
444: data_end = MAX(data_end,(csec->addr+csec->size));
445: }
446: #if 0
447: if ( data_start != data_end )
448: add_data(data_start,data_end);
449: #endif
450: (*(int(*)())(tsec->addr))();
451: } else if ( nxs )
452: NXFlush(nxs);
453: }
454:
455: void unloadmachofile()
456: {
457: if ( !rld_unload(nxs) && nxs )
458: NXFlush(nxs);
459: }
460:
461: #else
462: #ifdef mips
463: #include <sys/exec.h>
464: #else
465: #include <a.out.h>
466: #endif
467:
468: void loadaoutfile(/* char ** */);
469:
470: int loadfiles(node) NODE node; { return 0; }
471:
472: int loadfile(s)
473: char *s;
474: {
475: FILE *in;
476: struct exec h;
477: char **oname;
478:
479: if ( in = fopen(s,"r") ) {
480: fread(&h,sizeof(h),1,in); fclose(in);
481: #if defined(__NetBSD__)
482: if ((N_GETMAGIC(h) == OMAGIC) || (N_GETMAGIC(h) == ZMAGIC)) {
483: #else
484: if ((h.a_magic == OMAGIC) || (h.a_magic == ZMAGIC)) {
485: #endif
486: oname = (char **)ALLOCA(BUFSIZ);
487: oname[0] = s; oname[1] = 0;
488: loadaoutfile(oname);
489: } else
490: loadasirfile(s);
491: return 1;
492: } else
493: return 0;
494: }
495:
496: #if 0
497: void loadaoutfile(name)
498: char **name;
499: {
500: #include <sys/wait.h>
501: FILE *in;
502: char *tmpf,*buf;
503: char **n;
504: NODE tn;
505: struct exec h;
506: char com[BUFSIZ];
507: char *ldargv[256];
508: char nbuf[BUFSIZ];
509: int i,w;
510: union wait status;
511: static char ldcom1[] = "ld -d -N -x -A %s -T %x ";
512: static char ldcom2[] = " -lm -lc -o ";
513: extern char asirname[];
514:
515: tmpf = tempnam(0,"asir");
516: ldargv[0] = "ld"; ldargv[1] = "-d"; ldargv[2] = "-N";
517: ldargv[3] = "-x"; ldargv[4] = "-A"; ldargv[5] = asirname;
518: ldargv[6] = "-T"; ldargv[7] = "0";
519: for ( i = 8, n = name; *n; n++, i++ )
520: ldargv[i] = *n;
521: ldargv[i++] = "-lm"; ldargv[i++] = "-lc"; ldargv[i++] = "-o";
522: ldargv[i++] = tmpf; ldargv[i] = 0;
523: if ( !vfork() ) {
524: if ( execv("/bin/ld",ldargv) < 0 )
525: _exit(0);
526: } else
527: wait(&status);
528: if ( status.w_status ) {
529: error("loadaoutfile : faild");
530: }
531: in = fopen(tmpf,"r"); fread(&h,sizeof(h),1,in);
532: fclose(in); unlink(tmpf);
533: buf = (char *)CALLOC(h.a_text+h.a_data+h.a_bss,1);
534: sprintf(nbuf,"%x",buf); ldargv[7] = nbuf;
535: if ( !vfork() ) {
536: if ( execv("/bin/ld",ldargv) < 0 );
537: _exit(0);
538: } else
539: wait(&status);
540: in = fopen(tmpf,"r"); fread(&h,sizeof(h),1,in);
541: #ifdef mips
542: fseek(in,N_TXTOFF(h.ex_f,h.ex_o),0);
543: #endif
544: fread(buf,h.a_text+h.a_data,1,in);
545: MKNODE(tn,buf,objfile); objfile = tn;
546: (*(int(*)())buf)();
547: fclose(in); unlink(tmpf);
548: }
549: #endif
550:
551: #if 1
552: void loadaoutfile(name)
553: char **name;
554: {
555: FILE *in;
556: char *tmpf,*buf,*p;
557: char **n;
558: NODE tn;
559: struct exec h;
560: char com[BUFSIZ];
561: int status,i,len;
562: #ifdef mips
563: static char ldcom1[] = "ld -G 0 -d -N -x -A %s -T %x ";
564: static char ldcom2[] = " -lm_G0 -lc -o ";
565: #else
566: static char ldcom1[] = "ld -d -N -x -A %s -T %x ";
567: static char ldcom2[] = " -lm -lc -o ";
568: #endif
569: extern char asirname[];
570:
571: tmpf = tempnam(0,"asir");
572: sprintf(com,ldcom1,asirname,0);
573: for ( n = name; *n; n++ )
574: strcat(com,*n);
575: strcat(com,ldcom2); strcat(com,tmpf);
576: status = system(com);
577: if ( status ) {
578: fprintf(stderr,"system() : status = %d\n",status);
579: error("loadaoutfile : could not exec system()");
580: }
581: in = fopen(tmpf,"r");
582: fread(&h,sizeof(h),1,in);
583: fclose(in); unlink(tmpf);
584: buf = (char *)CALLOC(h.a_text+h.a_data+h.a_bss,1);
585: sprintf(com,ldcom1,asirname,buf);
586: for ( n = name; *n; n++ )
587: strcat(com,*n);
588: strcat(com,ldcom2); strcat(com,tmpf);
589: system(com);
590: in = fopen(tmpf,"r"); fread(&h,sizeof(h),1,in);
591: #ifdef mips
592: fseek(in,N_TXTOFF(h.ex_f,h.ex_o),0);
593: #endif
594: len = h.a_text+h.a_data; fread(buf,1,len,in);
595: /* to avoid a bug on RISC-NEWS */
596: fprintf(stderr,"text=%d data=%d bss=%d\n",h.a_text,h.a_data,h.a_bss);
597: MKNODE(tn,buf,objfile); objfile = tn;
598: /* fprintf(stderr,"calling reg_sysf()...\n"); */
599: (*(int(*)())buf)();
600: fclose(in); unlink(tmpf);
601: }
602: #endif
603: #endif
604: #endif
605: #endif
606:
607: static unsigned char encrypt_tab[128][2] = {
608: {137,40},{1,194},{133,79},{48,20},{254,76},{98,17},{110,233},{19,231},
609: {55,223},{75,65},{178,151},{85,222},{201,46},{51,243},{235,61},{106,113},
610: {116,121},{24,205},{146,244},{89,234},{163,173},{140,162},{188,45},{195,132},
611: {202,94},{236,87},{2,127},{253,47},{211,138},{58,252},{142,190},{77,209},
612: {16,53},{200,220},{99,165},{28,59},{78,0},{208,248},{50,229},{217,39},
613: {112,15},{18,60},{108,175},{10,67},{23,176},{49,245},{160,27},{171,219},
614: {105,144},{122,172},{255,114},{226,100},{70,117},{197,11},{180,36},{136,101},
615: {238,212},{125,120},{103,199},{38,119},{7,206},{181,228},{62,3},{30,185},
616: {154,63},{247,81},{187,80},{225,56},{210,221},{37,135},{155,52},{54,153},
617: {84,246},{166,90},{124,167},{41,184},{145,204},{147,198},{25,21},{72,97},
618: {66,128},{95,139},{93,42},{224,43},{35,143},{111,13},{82,249},{12,148},
619: {32,152},{186,168},{177,115},{216,251},{5,131},{123,170},{149,161},{213,203},
620: {126,150},{88,158},{74,169},{159,182},{156,26},{34,104},{8,91},{207,183},
621: {9,64},{83,241},{134,102},{69,33},{179,237},{129,250},{14,71},{230,4},
622: {218,6},{189,68},{193,22},{57,174},{215,214},{130,92},{240,31},{118,239},
623: {109,192},{232,196},{86,107},{96,141},{157,227},{164,242},{44,191},{29,73}
624: };
625:
626: static unsigned char decrypt_tab[] = {
627: 36,1,26,62,111,92,112,60,102,104,43,53,87,85,110,40,
628: 32,5,41,7,3,78,114,44,17,78,100,46,35,127,63,118,
629: 88,107,101,84,54,69,59,39,0,75,82,83,126,22,12,27,
630: 3,45,38,13,70,32,71,8,67,115,29,35,41,14,62,64,
631: 104,9,80,43,113,107,52,110,79,127,98,9,4,31,36,2,
632: 66,65,86,105,72,11,122,25,97,19,73,102,117,82,24,81,
633: 123,79,5,34,51,55,106,58,101,48,15,122,42,120,6,85,
634: 40,15,50,90,16,52,119,59,57,16,49,93,74,57,96,26,
635: 80,109,117,92,23,2,106,69,55,0,28,81,21,123,30,84,
636: 48,76,18,77,87,94,96,10,88,71,64,70,100,124,97,99,
637: 46,94,21,20,125,34,73,74,89,98,93,47,49,20,115,42,
638: 44,90,10,108,54,61,99,103,75,63,89,66,22,113,30,126,
639: 120,114,1,23,121,53,77,58,33,12,24,95,76,17,60,103,
640: 37,31,68,28,56,95,116,116,91,39,112,47,33,68,11,8,
641: 83,67,51,124,61,38,111,7,121,6,19,14,25,108,56,119,
642: 118,105,125,13,18,45,72,65,37,86,109,91,29,27,4,50
643: };
644:
645: unsigned char encrypt_char(unsigned char c)
646: {
647: return encrypt_tab[c][mt_genrand()&1];
648: }
649:
650: unsigned char decrypt_char(unsigned char c)
651: {
652: return decrypt_tab[c];
653: }
654:
655: void encrypt_file(in,out)
656: char *in,*out;
657: {
658: FILE *infp,*outfp;
659: int c;
660:
661: infp = fopen(in,"r");
662: outfp = fopen(out,"wb");
663: while ( 1 ) {
664: c = getc(infp);
665: if ( c == EOF )
666: break;
667: putc(encrypt_char(c),outfp);
668: }
669: fclose(infp); fclose(outfp);
670: }
671:
672: void decrypt_file(in,out)
673: char *in,*out;
674: {
675: FILE *infp,*outfp;
676: int c;
677:
678: infp = fopen(in,"rb");
679: outfp = fopen(out,"w");
680: /* skip the magic number (=0xff) */
681: getc(infp);
682: while ( 1 ) {
683: c = getc(infp);
684: if ( c == EOF )
685: break;
686: putc(decrypt_char(c),outfp);
687: }
688: fclose(infp); fclose(outfp);
689: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>