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