=================================================================== RCS file: /home/cvs/OpenXM_contrib2/asir2000/parse/load.c,v retrieving revision 1.1.1.1 retrieving revision 1.15 diff -u -p -r1.1.1.1 -r1.15 --- OpenXM_contrib2/asir2000/parse/load.c 1999/12/03 07:39:12 1.1.1.1 +++ OpenXM_contrib2/asir2000/parse/load.c 2005/03/24 12:46:45 1.15 @@ -1,21 +1,60 @@ -/* $OpenXM: OpenXM/src/asir99/parse/load.c,v 1.1.1.1 1999/11/10 08:12:34 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.14 2004/03/18 01:59:41 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); -} #endif +char **ASIRLOADPATH; + #if defined(VISUAL) #define ENVDELIM ';' #define MORE "more < " @@ -92,14 +94,49 @@ void resetDir() { #endif char *getenv(); +void Pget_rootdir(); +char *search_executable(char *name); + extern char *asir_libdir; 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]; @@ -120,12 +157,35 @@ void env_init() { } } 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 += 2; + ASIRLOADPATH=(char **)MALLOC(sizeof(char *)*i); + for ( l = 0; lname = (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 +345,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,8 +355,7 @@ 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; @@ -312,298 +363,46 @@ char *name; read_exec_file = 0; } -static NODE objfile = 0; - -#if defined(apollo) || defined(VISUAL) || defined(_PA_RISC1_1) || defined(__alpha) || defined(linux) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(SYSV) || defined(__SVR4) - -int loadfile(s) -char *s; +void load_and_execfile(char *name) { - FILE *in; + FILE *fp; + INFILE save_asir_infile; + int save_prresult; + extern prresult; - if ( in = fopen(s,"r") ) { - fclose(in); - loadasirfile(s); - return 1; - } else - return 0; -} - -int loadfiles(node) NODE node; { return 0; } - -#else -#if defined(THINK_C) - -int loadfile(s) -char *s; -{ - FILE *in; - - if ( in = fopen(s,"r") ) { - loadasirfile(s); fclose(in); - return 1; - } else - return 0; -} - -int loadfiles(node) NODE node; { return 0; } - -int finder_loadfile() { - Point p; - SFTypeList t; - SFReply r; - int ret; - - p.h = p.v = 50; t[0] = 'TEXT'; - SFGetFile(p,"\p",0,1,t,0,&r); - if ( r.good ) { - setDir(r.vRefNum); - ret = loadfile(PtoCstr(r.fName)); - resetDir(); - return ret; - } else - return 0; -} - -#else -#if defined(NeXT) -#include -#include - -void loadmachofile(char **); -void unloadmachofile(void); - -int loadfile(s) -char *s; -{ - FILE *in; - unsigned magic; - char **oname; - - if ( in = fopen(s,"r") ) { - fread(&magic,sizeof(unsigned),1,in); fclose(in); - if (magic == MH_MAGIC) { - oname = (char **)ALLOCA(BUFSIZ); - oname[0] = s; oname[1] = "/usr/lib/libm.a",oname[2] = 0; - loadmachofile(oname); - } else - loadasirfile(s); - return 1; - } else - return 0; -} - -int loadfiles(node) -NODE node; -{ - int i; - NODE n; - char *name; - char **oname; - - for ( i = 0, n = node; n; i++, n = NEXT(n) ); - if ( OID(BDY(n)) != O_STR ) - return 0; - oname = (char **)ALLOCA(i+1); - for ( i = 0, n = node; n; i++, n = NEXT(n) ) { - searchasirpath(BDY((STRING)BDY(node)),&name); - if ( !name ) - return 0; - else - oname[i] = name; + savepvs(); + save_asir_infile = asir_infile; + save_prresult = prresult; + fp = fopen(name,"rb"); + input_init(fp,name); + if ( !SETJMP(exec_env) ) { + /* XXX : information for asir_teriminate() */ + read_exec_file = 2; + read_eval_loop(); + read_exec_file = 0; } - oname[i] = 0; - loadmachofile(oname); - return 1; + fclose(fp); + restorepvs(); + asir_infile = save_asir_infile; + prresult = save_prresult; } -static NXStream *nxs = 0; +static NODE objfile = 0; -void loadmachofile(oname) -char **oname; +int loadfile(char *s) { - struct mach_header *h; - unsigned int data_start,data_end; - struct section *tsec,*dsec,*bsec,*csec; - struct section *getsectbynamefromheader(); - - if ( !nxs ) - nxs = NXOpenFile(fileno(stderr),NX_WRITEONLY); - if ( rld_load(nxs,&h,oname,"/tmp/afo") ) { - tsec = getsectbynamefromheader(h,"__TEXT","__text"); - dsec = getsectbynamefromheader(h,"__DATA","__data"); - bsec = getsectbynamefromheader(h,"__DATA","__bss"); - csec = getsectbynamefromheader(h,"__DATA","__common"); - data_start = dsec->addr; data_end = dsec->addr+dsec->size; - if ( bsec->size ) { - data_start = MIN(data_start,bsec->addr); - data_end = MAX(data_end,(bsec->addr+bsec->size)); - } - if ( csec->size ) { - data_start = MIN(data_start,csec->addr); - data_end = MAX(data_end,(csec->addr+csec->size)); - } -#if 0 - if ( data_start != data_end ) - add_data(data_start,data_end); -#endif - (*(int(*)())(tsec->addr))(); - } else if ( nxs ) - NXFlush(nxs); -} - -void unloadmachofile() -{ - if ( !rld_unload(nxs) && nxs ) - NXFlush(nxs); -} - -#else -#ifdef mips -#include -#else -#include -#endif - -void loadaoutfile(/* char ** */); - -int loadfiles(node) NODE node; { return 0; } - -int loadfile(s) -char *s; -{ FILE *in; - struct exec h; - char **oname; if ( in = fopen(s,"r") ) { - fread(&h,sizeof(h),1,in); fclose(in); -#if defined(__NetBSD__) - if ((N_GETMAGIC(h) == OMAGIC) || (N_GETMAGIC(h) == ZMAGIC)) { -#else - if ((h.a_magic == OMAGIC) || (h.a_magic == ZMAGIC)) { -#endif - oname = (char **)ALLOCA(BUFSIZ); - oname[0] = s; oname[1] = 0; - loadaoutfile(oname); - } else - loadasirfile(s); + fclose(in); + loadasirfile(s); return 1; - } else + } else return 0; } -#if 0 -void loadaoutfile(name) -char **name; -{ -#include - FILE *in; - char *tmpf,*buf; - char **n; - NODE tn; - struct exec h; - char com[BUFSIZ]; - char *ldargv[256]; - char nbuf[BUFSIZ]; - int i,w; - union wait status; - static char ldcom1[] = "ld -d -N -x -A %s -T %x "; - static char ldcom2[] = " -lm -lc -o "; - extern char asirname[]; +int loadfiles(NODE node) { return 0; } - tmpf = tempnam(0,"asir"); - ldargv[0] = "ld"; ldargv[1] = "-d"; ldargv[2] = "-N"; - ldargv[3] = "-x"; ldargv[4] = "-A"; ldargv[5] = asirname; - ldargv[6] = "-T"; ldargv[7] = "0"; - for ( i = 8, n = name; *n; n++, i++ ) - ldargv[i] = *n; - ldargv[i++] = "-lm"; ldargv[i++] = "-lc"; ldargv[i++] = "-o"; - ldargv[i++] = tmpf; ldargv[i] = 0; - if ( !vfork() ) { - if ( execv("/bin/ld",ldargv) < 0 ) - _exit(0); - } else - wait(&status); - if ( status.w_status ) { - error("loadaoutfile : faild"); - } - in = fopen(tmpf,"r"); fread(&h,sizeof(h),1,in); - fclose(in); unlink(tmpf); - buf = (char *)CALLOC(h.a_text+h.a_data+h.a_bss,1); - sprintf(nbuf,"%x",buf); ldargv[7] = nbuf; - if ( !vfork() ) { - if ( execv("/bin/ld",ldargv) < 0 ); - _exit(0); - } else - wait(&status); - in = fopen(tmpf,"r"); fread(&h,sizeof(h),1,in); -#ifdef mips - fseek(in,N_TXTOFF(h.ex_f,h.ex_o),0); -#endif - fread(buf,h.a_text+h.a_data,1,in); - MKNODE(tn,buf,objfile); objfile = tn; - (*(int(*)())buf)(); - fclose(in); unlink(tmpf); -} -#endif - -#if 1 -void loadaoutfile(name) -char **name; -{ - FILE *in; - char *tmpf,*buf,*p; - char **n; - NODE tn; - struct exec h; - char com[BUFSIZ]; - int status,i,len; -#ifdef mips - static char ldcom1[] = "ld -G 0 -d -N -x -A %s -T %x "; - static char ldcom2[] = " -lm_G0 -lc -o "; -#else - static char ldcom1[] = "ld -d -N -x -A %s -T %x "; - static char ldcom2[] = " -lm -lc -o "; -#endif - extern char asirname[]; - - tmpf = tempnam(0,"asir"); - sprintf(com,ldcom1,asirname,0); - for ( n = name; *n; n++ ) - strcat(com,*n); - strcat(com,ldcom2); strcat(com,tmpf); - status = system(com); - if ( status ) { - fprintf(stderr,"system() : status = %d\n",status); - error("loadaoutfile : could not exec system()"); - } - in = fopen(tmpf,"r"); - fread(&h,sizeof(h),1,in); - fclose(in); unlink(tmpf); - buf = (char *)CALLOC(h.a_text+h.a_data+h.a_bss,1); - sprintf(com,ldcom1,asirname,buf); - for ( n = name; *n; n++ ) - strcat(com,*n); - strcat(com,ldcom2); strcat(com,tmpf); - system(com); - in = fopen(tmpf,"r"); fread(&h,sizeof(h),1,in); -#ifdef mips - fseek(in,N_TXTOFF(h.ex_f,h.ex_o),0); -#endif - len = h.a_text+h.a_data; fread(buf,1,len,in); - /* to avoid a bug on RISC-NEWS */ - fprintf(stderr,"text=%d data=%d bss=%d\n",h.a_text,h.a_data,h.a_bss); - MKNODE(tn,buf,objfile); objfile = tn; -/* fprintf(stderr,"calling reg_sysf()...\n"); */ - (*(int(*)())buf)(); - fclose(in); unlink(tmpf); -} -#endif -#endif -#endif -#endif - static unsigned char encrypt_tab[128][2] = { {137,40},{1,194},{133,79},{48,20},{254,76},{98,17},{110,233},{19,231}, {55,223},{75,65},{178,151},{85,222},{201,46},{51,243},{235,61},{106,113}, @@ -652,8 +451,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; @@ -664,13 +462,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; @@ -683,7 +480,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); }