=================================================================== RCS file: /home/cvs/OpenXM_contrib2/asir2000/parse/load.c,v retrieving revision 1.2 retrieving revision 1.22 diff -u -p -r1.2 -r1.22 --- OpenXM_contrib2/asir2000/parse/load.c 2000/01/19 05:05:04 1.2 +++ OpenXM_contrib2/asir2000/parse/load.c 2012/12/17 07:20:45 1.22 @@ -1,21 +1,60 @@ -/* $OpenXM: OpenXM_contrib2/asir2000/parse/load.c,v 1.1.1.1 1999/12/03 07:39:12 noro Exp $ */ +/* + * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED + * All rights reserved. + * + * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited, + * non-exclusive and royalty-free license to use, copy, modify and + * redistribute, solely for non-commercial and non-profit purposes, the + * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and + * conditions of this Agreement. For the avoidance of doubt, you acquire + * only a limited right to use the SOFTWARE hereunder, and FLL or any + * third party developer retains all rights, including but not limited to + * copyrights, in and to the SOFTWARE. + * + * (1) FLL does not grant you a license in any way for commercial + * purposes. You may use the SOFTWARE only for non-commercial and + * non-profit purposes only, such as academic, research and internal + * business use. + * (2) The SOFTWARE is protected by the Copyright Law of Japan and + * international copyright treaties. If you make copies of the SOFTWARE, + * with or without modification, as permitted hereunder, you shall affix + * to all such copies of the SOFTWARE the above copyright notice. + * (3) An explicit reference to this SOFTWARE and its copyright owner + * shall be made on your publication or presentation in any form of the + * results obtained by use of the SOFTWARE. + * (4) In the event that you modify the SOFTWARE, you shall notify FLL by + * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification + * for such modification or the source code of the modified part of the + * SOFTWARE. + * + * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL + * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND + * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES' + * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY + * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY. + * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT, + * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL + * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES + * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES + * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY + * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF + * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART + * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY + * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE, + * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE. + * + * $OpenXM: OpenXM_contrib2/asir2000/parse/load.c,v 1.21 2006/09/13 02:26:13 noro Exp $ +*/ #include "ca.h" #include "parse.h" -#if defined(THINK_C) -#include -#include -#include -#include -#include -#else + #if defined(VISUAL) #include #include -#if defined(GO32) -#include -#endif #include -#else +#else /* VISUAL */ #if defined(_PA_RISC1_1) || defined(SYSV) || defined(__SVR4) #include #include @@ -26,7 +65,6 @@ #include #include #endif -#endif #if defined(linux) #include @@ -36,49 +74,13 @@ #include #endif +#ifdef MALLOC #undef MALLOC -#define MALLOC(x) GC_malloc((x)+4) - -char *ASIRLOADPATH[32]; - -void encrypt_file(char *,char *); -void decrypt_file(char *,char *); - -#if defined(THINK_C) -void searchasirpath(char *,char **); -void loadasirfile(char *); - -short initvol; - -void initVol(); -short getVol(); -void setDir(short); -void resetDir(); - -void initVol() { - char buf[BUFSIZ]; - - GetVol((StringPtr)buf,&initvol); -} - -short getVol() { - short vol; - char buf[BUFSIZ]; - - GetVol((StringPtr)buf,&vol); - return vol; -} - -void setDir(short vol) -{ - SetVol(0,vol); -} - -void resetDir() { - SetVol(0,initvol); -} +#define MALLOC(x) Risa_GC_malloc((x)+4) #endif +char **ASIRLOADPATH; + #if defined(VISUAL) #define ENVDELIM ';' #define MORE "more < " @@ -88,18 +90,56 @@ void resetDir() { #endif #ifndef ASIR_LIBDIR -#define ASIR_LIBDIR "." +#define ASIR_LIBDIR "/usr/local/lib/asir" #endif +#ifndef ASIR_CONTRIB_DIR +#define ASIR_CONTRIB_DIR "/usr/local/lib/asir-contrib" +#endif char *getenv(); +void Pget_rootdir(); +char *search_executable(char *name); + extern char *asir_libdir; +extern char *asir_contrib_dir; extern char *asir_pager; -extern int read_exec_file; extern int main_parser; +extern JMP_BUF exec_env; +char *search_executable(char *name) +{ + char *c,*s,*ret; + int len,nlen; + char dir[BUFSIZ],path[BUFSIZ]; + struct stat buf; + + nlen = strlen(name); + for ( s = (char *)getenv("PATH"); s; ) { + c = (char *)index(s,':'); + len = c ? c-s : strlen(s); + if ( len >= BUFSIZ ) continue; + strncpy(dir,s,len); dir[len] = 0; + if ( c ) s = c+1; + else s = 0; + if ( len+nlen+1 >= BUFSIZ ) continue; + sprintf(path,"%s/%s",dir,name); + if ( !stat(path,&buf) && !(buf.st_mode & S_IFDIR) +#if !defined(VISUAL) + && !access(path,X_OK) +#endif + ) { + len = strlen(path)+1; + ret = (char *)MALLOC(len); + strcpy(ret,path); + return ret; + } + } + return 0; +} + void env_init() { char *e,*p,*q; - int i,l; + int i,l,japanese; char *getenv(); char *oxhome; char rootname[BUFSIZ]; @@ -119,13 +159,53 @@ void env_init() { #endif } } + + if ( !(asir_contrib_dir = getenv("ASIR_CONTRIB_DIR")) ) { + if ( oxhome = getenv("OpenXM_HOME") ) { + asir_contrib_dir = (char *)malloc(strlen(oxhome)+strlen("/lib/asir-contrib")+1); + sprintf(asir_contrib_dir,"%s/lib/asir-contrib",oxhome); + } else { +#if defined(VISUAL) + get_rootdir(rootname,sizeof(rootname)); + asir_contrib_dir = (char *)malloc(strlen(rootname)+strlen("/lib-asir-contrib")+1); + sprintf(asir_contrib_dir,"%s/lib-asir-contrib",rootname); +#else + asir_contrib_dir = (char *)malloc(strlen(ASIR_CONTRIB_DIR)+1); + strcpy(asir_contrib_dir,ASIR_CONTRIB_DIR); +#endif + } + } + if ( !(asir_pager = getenv("PAGER")) ) { - asir_pager = (char *)malloc(strlen(MORE)+1); - strcpy(asir_pager,MORE); + japanese = 0; + if ( (e = getenv("LANGUAGE")) && + (!strncmp(e,"japan",5) || !strncmp(e,"ja_JP",5)) ) japanese = 1; + else if ( (e = getenv("LC_ALL")) && + (!strncmp(e,"japan",5) || !strncmp(e,"ja_JP",5)) ) japanese = 1; + else if ( (e = getenv("LC_CTYPE")) && + (!strncmp(e,"japan",5) || !strncmp(e,"ja_JP",5)) ) japanese = 1; + else if ( (e = getenv("LANG")) && + (!strncmp(e,"japan",5) || !strncmp(e,"ja_JP",5)) ) japanese = 1; + if ( japanese ) + asir_pager = search_executable("jless"); + if ( !asir_pager ) { + /* default: more */ + asir_pager = (char *)malloc(strlen(MORE)+1); + strcpy(asir_pager,MORE); + } } - if ( e = getenv("ASIRLOADPATH" ) ) + if ( e = getenv("ASIRLOADPATH" ) ) { for ( i = 0; ; i++, e = p+1 ) { p = (char *)index(e,ENVDELIM); + if ( !p ) + break; + } + i += 4; + ASIRLOADPATH=(char **)MALLOC(sizeof(char *)*i); + for ( l = 0; l= 0 ) { - stat(name,&sbuf); - if ( (sbuf.st_mode & S_IFMT) != S_IFDIR ) + ret = stat(name,&sbuf); + if ( ret == 0 && (sbuf.st_mode & S_IFMT) != S_IFDIR ) *pathp = name; else *pathp = 0; @@ -162,8 +250,8 @@ char **pathp; l = strlen(*p)+strlen(name)+2; q = (char *)ALLOCA(l); sprintf(q,"%s/%s",*p,name); if ( access(q,R_OK) >= 0 ) { - stat(q,&sbuf); - if ( (sbuf.st_mode & S_IFMT) != S_IFDIR ) { + ret = stat(q,&sbuf); + if ( ret == 0 && (sbuf.st_mode & S_IFMT) != S_IFDIR ) { *pathp = (char *)MALLOC(l); strcpy(*pathp,q); return; } @@ -171,106 +259,86 @@ char **pathp; } *pathp = 0; } -#elif defined(VISUAL) - if ( (name[0] == '/') || ( name[0] == '.') || strchr(name,':') - || !ASIRLOADPATH[0] ) - *pathp = name; - else { - for ( p = ASIRLOADPATH; *p; p++ ) { - l = strlen(*p)+strlen(name)+2; - q = (char *)ALLOCA(l); sprintf(q,"%s/%s",*p,name); - if ( access(q,04) >= 0 ) { - *pathp = (char *)MALLOC(l); strcpy(*pathp,q); - return; - } - } - *pathp = 0; - } -#else - if ( (name[0] == '/') || ( name[0] == '.') || !ASIRLOADPATH[0] ) - *pathp = name; - else { - for ( p = ASIRLOADPATH; *p; p++ ) { - l = strlen(*p)+strlen(name)+2; - q = (char *)ALLOCA(l); sprintf(q,"%s/%s",*p,name); - *pathp = (char *)MALLOC(l); strcpy(*pathp,q); - return; - } - *pathp = 0; - } -#endif } -#if defined(THINK_C) -#define DELIM ':' -#elif defined(VISUAL) #define DELIM '/' -#endif -void Eungetc(int,FILE *); - -void loadasirfile(name0) -char *name0; +void loadasirfile(char *name0) { FILE *in; - IN t; + INFILE t; extern char cppname[]; -#if defined(THINK_C) || defined(VISUAL) - char tname[BUFSIZ],dname[BUFSIZ],ibuf1[BUFSIZ],ibuf2[BUFSIZ]; +#if defined(VISUAL) + char ibuf1[BUFSIZ],ibuf2[BUFSIZ]; int ac; -#if defined(__MWERKS__) - char *av[64]; -#else char *av[BUFSIZ]; -#endif - char *p,*c; + char *p; FILE *fp; - char name[BUFSIZ],tname0[BUFSIZ]; + char dname[BUFSIZ],tname0[BUFSIZ]; + char *name,*tname; int encoded; + static char prefix[BUFSIZ]; + int process_id(); + char CppExe[BUFSIZ]; + char nbuf[BUFSIZ],tnbuf[BUFSIZ]; + STRING rootdir; + void call_exe(char *,char **); -#if defined(VISUAL) + /* create the unique prefix */ + if ( !prefix[0] ) + sprintf(prefix,"asir%d",process_id()); + fp = fopen(name0,"rb"); if ( getc(fp) == 0xff ) { /* encoded file */ fclose(fp); - sprintf(name,"%s.$$$",name0); + name = tempnam(NULL,prefix); decrypt_file(name0,name); + /* the file 'name' created */ encoded = 1; } else { fclose(fp); - strcpy(name,name0); + name = name0; encoded = 0; } -#else - strcpy(name,name0); -#endif strcpy(dname,name); + av[0] = "cpp"; + sprintf(nbuf,"\"%s\"",name); + av[1] = nbuf; + tname = tempnam(NULL,prefix); + sprintf(tnbuf,"\"%s\"",tname); + av[2] = tnbuf; + sprintf(ibuf1,"-I\"%s\"",asir_libdir); + av[3] = ibuf1; + av[4] = "-DWINDOWS"; + + /* set the include directory */ p = strrchr(dname,DELIM); - av[0] = "cpp"; av[1] = name; av[2] = tname; - sprintf(ibuf1,"-I%s",asir_libdir); av[3] = ibuf1; av[4] = "-DWINDOWS"; if ( !p ) { - sprintf(tname,"%s.___",name); av[5] = 0; ac = 5; + av[5] = 0; ac = 5; } else { - *p++ = 0; -#if defined(VISUAL) - if ( c = strchr(dname,':') ) { - *c = 0; - sprintf(tname,"%s:%s%c%s.___",dname,c+1,DELIM,p); - *c = ':'; - } else - -#endif - sprintf(tname,"%s%c%s.___",dname,DELIM,p); - sprintf(ibuf2,"-I%s",dname); av[5] = ibuf2; av[6] = 0; ac = 6; + *p = 0; + sprintf(ibuf2,"-I\"%s\"",dname); + av[5] = ibuf2; + av[6] = 0; ac = 6; } - cpp_main(ac,av); +// cpp_main(ac,av); + Pget_rootdir(&rootdir); + sprintf(CppExe,"%s\\bin\\cpp.exe",BDY(rootdir)); + call_exe(CppExe,av); + + /* the file tname created */ if ( encoded ) { - unlink(name); - strcpy(tname0,tname); - sprintf(tname,"%s.###",tname0); + unlink(name); free(name); + + strcpy(tname0,tname); free(tname); + tname = tempnam(NULL,prefix); + encrypt_file(tname0,tname); + /* the file tname created */ unlink(tname0); + in = fopen(tname,"rb"); } else in = fopen(tname,"r"); @@ -278,13 +346,10 @@ char *name0; perror("fopen"); error("load : failed"); } - t = (IN)MALLOC(sizeof(struct oIN)); - t->name = (char *)MALLOC(strlen(name)+1); strcpy(t->name,name); - t->tname = (char *)MALLOC(strlen(tname)+1); strcpy(t->tname,tname); + t = (INFILE)MALLOC(sizeof(struct oINFILE)); + t->name = (char *)MALLOC(strlen(name0)+1); strcpy(t->name,name0); + t->tname = (char *)MALLOC(strlen(tname)+1); strcpy(t->tname,tname); free(tname); t->encoded = encoded; -#if defined(THINK_C) - t->vol = getVol(); -#endif #else char com[BUFSIZ]; @@ -293,7 +358,7 @@ char *name0; perror("popen"); error("load : failed"); } - t = (IN)MALLOC(sizeof(struct oIN)); + t = (INFILE)MALLOC(sizeof(struct oINFILE)); t->name = (char *)MALLOC(strlen(name0)+1); strcpy(t->name,name0); #endif t->fp = in; t->ln = 1; t->next = asir_infile; asir_infile = t; @@ -303,19 +368,19 @@ char *name0; reallocarray((char **)&EPVS->va,(int *)&EPVS->asize,(int *)&EPVS->n,(int)sizeof(struct oPV)); } -void execasirfile(name) -char *name; +void execasirfile(char *name) { loadasirfile(name); - read_exec_file = 1; - read_eval_loop(); - read_exec_file = 0; + if ( !SETJMP(asir_infile->jmpbuf) ) { + asir_infile->ready_for_longjmp = 1; + read_eval_loop(); + } + closecurrentinput(); } static NODE objfile = 0; -int loadfile(s) -char *s; +int loadfile(char *s) { FILE *in; @@ -327,7 +392,7 @@ char *s; return 0; } -int loadfiles(node) NODE node; { return 0; } +int loadfiles(NODE node) { return 0; } static unsigned char encrypt_tab[128][2] = { {137,40},{1,194},{133,79},{48,20},{254,76},{98,17},{110,233},{19,231}, @@ -377,8 +442,7 @@ unsigned char decrypt_char(unsigned char c) return decrypt_tab[c]; } -void encrypt_file(in,out) -char *in,*out; +void encrypt_file(char *in,char *out) { FILE *infp,*outfp; int c; @@ -389,13 +453,12 @@ char *in,*out; c = getc(infp); if ( c == EOF ) break; - putc(encrypt_char(c),outfp); + putc(encrypt_char((unsigned char)c),outfp); } fclose(infp); fclose(outfp); } -void decrypt_file(in,out) -char *in,*out; +void decrypt_file(char *in,char *out) { FILE *infp,*outfp; int c; @@ -408,7 +471,7 @@ char *in,*out; c = getc(infp); if ( c == EOF ) break; - putc(decrypt_char(c),outfp); + putc(decrypt_char((unsigned char)c),outfp); } fclose(infp); fclose(outfp); }