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