[BACK]Return to load.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / parse

Diff for /OpenXM_contrib2/asir2000/parse/load.c between version 1.32 and 1.33

version 1.32, 2017/02/07 08:30:31 version 1.33, 2018/03/29 01:32:54
Line 45 
Line 45 
  * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,   * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
  * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.   * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
  *   *
  * $OpenXM: OpenXM_contrib2/asir2000/parse/load.c,v 1.31 2016/08/30 02:29:11 noro Exp $   * $OpenXM: OpenXM_contrib2/asir2000/parse/load.c,v 1.32 2017/02/07 08:30:31 noro Exp $
 */  */
 #include "ca.h"  #include "ca.h"
 #include "parse.h"  #include "parse.h"
Line 110  extern JMP_BUF exec_env;
Line 110  extern JMP_BUF exec_env;
   
 char *search_executable(char *name)  char *search_executable(char *name)
 {  {
         char *c,*s,*ret;    char *c,*s,*ret;
         int len,nlen;    int len,nlen;
         char dir[BUFSIZ],path[BUFSIZ];    char dir[BUFSIZ],path[BUFSIZ];
         struct stat buf;    struct stat buf;
   
         nlen = strlen(name);    nlen = strlen(name);
         for ( s = (char *)getenv("PATH"); s; ) {    for ( s = (char *)getenv("PATH"); s; ) {
                 c = (char *)index(s,':');      c = (char *)index(s,':');
                 len = c ? c-s : strlen(s);      len = c ? c-s : strlen(s);
                 if ( len >= BUFSIZ ) continue;      if ( len >= BUFSIZ ) continue;
                 strncpy(dir,s,len); dir[len] = 0;      strncpy(dir,s,len); dir[len] = 0;
                 if ( c ) s = c+1;      if ( c ) s = c+1;
                 else s = 0;      else s = 0;
                 if ( len+nlen+1 >= BUFSIZ ) continue;      if ( len+nlen+1 >= BUFSIZ ) continue;
                 sprintf(path,"%s/%s",dir,name);      sprintf(path,"%s/%s",dir,name);
                 if ( !stat(path,&buf) && !(buf.st_mode & S_IFDIR)      if ( !stat(path,&buf) && !(buf.st_mode & S_IFDIR)
 #if !defined(VISUAL) && !defined(__MINGW32__)  #if !defined(VISUAL) && !defined(__MINGW32__)
                         && !access(path,X_OK)        && !access(path,X_OK)
 #endif  #endif
                 ) {                  ) {
                         len = strlen(path)+1;        len = strlen(path)+1;
                         ret = (char *)MALLOC(len);        ret = (char *)MALLOC(len);
                         strcpy(ret,path);        strcpy(ret,path);
                         return ret;        return ret;
                 }      }
         }    }
         return 0;    return 0;
 }  }
   
 void env_init() {  void env_init() {
         char *e,*p,*q;    char *e,*p,*q;
         int i,l,japanese;    int i,l,japanese;
         char *getenv();    char *getenv();
         char *oxhome;    char *oxhome;
         char rootname[BUFSIZ];    char rootname[BUFSIZ];
     size_t len;      size_t len;
   
         if ( oxhome = getenv("OpenXM_HOME") ) {    if ( oxhome = getenv("OpenXM_HOME") ) {
                 len = strlen(oxhome);      len = strlen(oxhome);
         }else {    }else {
 #if defined(VISUAL) || defined(__MINGW32__)  #if defined(VISUAL) || defined(__MINGW32__)
                 get_rootdir(rootname,sizeof(rootname));      get_rootdir(rootname,sizeof(rootname));
                 len = strlen(rootname);      len = strlen(rootname);
                 oxhome = rootname;      oxhome = rootname;
 #endif  #endif
         }    }
   
         if ( !(asir_libdir = getenv("ASIR_LIBDIR")) ) {    if ( !(asir_libdir = getenv("ASIR_LIBDIR")) ) {
                 if ( oxhome ) {      if ( oxhome ) {
                         asir_libdir = (char *)malloc(len+strlen("/lib/asir")+1);        asir_libdir = (char *)malloc(len+strlen("/lib/asir")+1);
                         sprintf(asir_libdir,"%s/lib/asir",oxhome);        sprintf(asir_libdir,"%s/lib/asir",oxhome);
                 } else {      } else {
                         asir_libdir = (char *)malloc(strlen(ASIR_LIBDIR)+1);        asir_libdir = (char *)malloc(strlen(ASIR_LIBDIR)+1);
                         strcpy(asir_libdir,ASIR_LIBDIR);        strcpy(asir_libdir,ASIR_LIBDIR);
                 }      }
         }    }
   
         if ( !(asir_contrib_dir = getenv("ASIR_CONTRIB_DIR")) ) {    if ( !(asir_contrib_dir = getenv("ASIR_CONTRIB_DIR")) ) {
                 if ( oxhome ) {      if ( oxhome ) {
                         asir_contrib_dir = (char *)malloc(len+strlen("/lib/asir-contrib")+1);        asir_contrib_dir = (char *)malloc(len+strlen("/lib/asir-contrib")+1);
                         sprintf(asir_contrib_dir,"%s/lib/asir-contrib",oxhome);        sprintf(asir_contrib_dir,"%s/lib/asir-contrib",oxhome);
                 } else {      } else {
                         asir_contrib_dir = (char *)malloc(strlen(ASIR_CONTRIB_DIR)+1);        asir_contrib_dir = (char *)malloc(strlen(ASIR_CONTRIB_DIR)+1);
                         strcpy(asir_contrib_dir,ASIR_CONTRIB_DIR);        strcpy(asir_contrib_dir,ASIR_CONTRIB_DIR);
                 }      }
         }    }
   
     asir_private_dir = NULL;      asir_private_dir = NULL;
 #if defined(VISUAL) || defined(__MINGW32__)  #if defined(VISUAL) || defined(__MINGW32__)
Line 185  void env_init() {
Line 185  void env_init() {
     }      }
 #endif  #endif
   
         if ( !(asir_pager = getenv("PAGER")) ) {    if ( !(asir_pager = getenv("PAGER")) ) {
 #if 0  #if 0
                 japanese = 0;      japanese = 0;
                 if ( (e = getenv("LANGUAGE")) &&      if ( (e = getenv("LANGUAGE")) &&
                         (!strncmp(e,"japan",5) || !strncmp(e,"ja_JP",5)) ) japanese = 1;        (!strncmp(e,"japan",5) || !strncmp(e,"ja_JP",5)) ) japanese = 1;
                 else if ( (e = getenv("LC_ALL")) &&      else if ( (e = getenv("LC_ALL")) &&
                         (!strncmp(e,"japan",5) || !strncmp(e,"ja_JP",5)) ) japanese = 1;        (!strncmp(e,"japan",5) || !strncmp(e,"ja_JP",5)) ) japanese = 1;
                 else if ( (e = getenv("LC_CTYPE")) &&      else if ( (e = getenv("LC_CTYPE")) &&
                         (!strncmp(e,"japan",5) || !strncmp(e,"ja_JP",5)) ) japanese = 1;        (!strncmp(e,"japan",5) || !strncmp(e,"ja_JP",5)) ) japanese = 1;
                 else if ( (e = getenv("LANG")) &&      else if ( (e = getenv("LANG")) &&
                         (!strncmp(e,"japan",5) || !strncmp(e,"ja_JP",5)) ) japanese = 1;        (!strncmp(e,"japan",5) || !strncmp(e,"ja_JP",5)) ) japanese = 1;
                 if ( japanese )      if ( japanese )
                         asir_pager = search_executable("jless");        asir_pager = search_executable("jless");
 #endif  #endif
                 asir_pager = search_executable("less");      asir_pager = search_executable("less");
                 if ( !asir_pager ) {      if ( !asir_pager ) {
                         /* default: more */        /* default: more */
                         asir_pager = (char *)malloc(strlen(MORE)+1);        asir_pager = (char *)malloc(strlen(MORE)+1);
                         strcpy(asir_pager,MORE);        strcpy(asir_pager,MORE);
                 }      }
         }    }
         if ( e = getenv("ASIRLOADPATH" ) ) {    if ( e = getenv("ASIRLOADPATH" ) ) {
                 for ( i = 0; ; i++, e = p+1 ) {      for ( i = 0; ; i++, e = p+1 ) {
                         p = (char *)index(e,ENVDELIM);        p = (char *)index(e,ENVDELIM);
                         if ( !p )        if ( !p )
                                 break;          break;
                 }      }
                 i += 5;      i += 5;
                 ASIRLOADPATH_LEN=i;      ASIRLOADPATH_LEN=i;
         ASIRLOADPATH=(char **)MALLOC(sizeof(char *)*i);          ASIRLOADPATH=(char **)MALLOC(sizeof(char *)*i);
                 for ( l = 0; l<i; l++) ASIRLOADPATH[l] = NULL;      for ( l = 0; l<i; l++) ASIRLOADPATH[l] = NULL;
                 e = getenv("ASIRLOADPATH");      e = getenv("ASIRLOADPATH");
                 for ( i = 0; ; i++, e = p+1 ) {      for ( i = 0; ; i++, e = p+1 ) {
                         p = (char *)index(e,ENVDELIM);        p = (char *)index(e,ENVDELIM);
                         l = p ? p-e : strlen(e); q = (char *)MALLOC(l+1);        l = p ? p-e : strlen(e); q = (char *)MALLOC(l+1);
                         if ( l ) {        if ( l ) {
                                 strncpy(q,e,l); q[l] = 0; ASIRLOADPATH[i] = q;          strncpy(q,e,l); q[l] = 0; ASIRLOADPATH[i] = q;
                         }        }
                         if ( !p )        if ( !p )
                                 break;          break;
                 }      }
     }else{      }else{
           ASIRLOADPATH=(char **)MALLOC(sizeof(char *)*3);      ASIRLOADPATH=(char **)MALLOC(sizeof(char *)*3);
           ASIRLOADPATH[0] = NULL;      ASIRLOADPATH[0] = NULL;
         }    }
   
         for ( i = 0; ASIRLOADPATH[i]; i++ );    for ( i = 0; ASIRLOADPATH[i]; i++ );
         if (asir_private_dir) ASIRLOADPATH[i++] = asir_private_dir;    if (asir_private_dir) ASIRLOADPATH[i++] = asir_private_dir;
         if (asir_contrib_dir) ASIRLOADPATH[i++] = asir_contrib_dir;    if (asir_contrib_dir) ASIRLOADPATH[i++] = asir_contrib_dir;
         if (asir_libdir) ASIRLOADPATH[i++] = asir_libdir;    if (asir_libdir) ASIRLOADPATH[i++] = asir_libdir;
         ASIRLOADPATH[i++] = ".";    ASIRLOADPATH[i++] = ".";
         ASIRLOADPATH[i] = NULL;    ASIRLOADPATH[i] = NULL;
 }  }
   
 #if defined(VISUAL) || defined(__MINGW32__)  #if defined(VISUAL) || defined(__MINGW32__)
Line 245  void env_init() {
Line 245  void env_init() {
   
 void searchasirpath(char *name,char **pathp)  void searchasirpath(char *name,char **pathp)
 {  {
         char **p;    char **p;
         char *q;    char *q;
         size_t l;    size_t l;
         int ret;    int ret;
         struct stat sbuf;    struct stat sbuf;
   
         if ( (name[0] == '/') || ( name[0] == '.') || strchr(name,':')    if ( (name[0] == '/') || ( name[0] == '.') || strchr(name,':')
                 || !ASIRLOADPATH[0] ) {      || !ASIRLOADPATH[0] ) {
                 if ( access(name,R_OK) >= 0 ) {      if ( access(name,R_OK) >= 0 ) {
                         ret = stat(name,&sbuf);        ret = stat(name,&sbuf);
                         if ( ret == 0 && (sbuf.st_mode & S_IFMT) != S_IFDIR )        if ( ret == 0 && (sbuf.st_mode & S_IFMT) != S_IFDIR )
                                 *pathp = name;          *pathp = name;
                         else        else
                                 *pathp = 0;          *pathp = 0;
                 } else      } else
                         *pathp = 0;        *pathp = 0;
         } else {    } else {
                 for ( p = ASIRLOADPATH; *p; p++ ) {      for ( p = ASIRLOADPATH; *p; p++ ) {
                         l = strlen(*p)+strlen(name)+2;        l = strlen(*p)+strlen(name)+2;
                         q = (char *)ALLOCA(l); sprintf(q,"%s/%s",*p,name);        q = (char *)ALLOCA(l); sprintf(q,"%s/%s",*p,name);
                         if ( access(q,R_OK) >= 0 ) {        if ( access(q,R_OK) >= 0 ) {
                                 ret = stat(q,&sbuf);          ret = stat(q,&sbuf);
                                 if ( ret == 0 && (sbuf.st_mode & S_IFMT) != S_IFDIR ) {          if ( ret == 0 && (sbuf.st_mode & S_IFMT) != S_IFDIR ) {
                                         *pathp = (char *)MALLOC(l); strcpy(*pathp,q);            *pathp = (char *)MALLOC(l); strcpy(*pathp,q);
                                         return;            return;
                                 }          }
                         }        }
                 }      }
                 *pathp = 0;      *pathp = 0;
         }    }
 }  }
   
 #define DELIM '/'  #define DELIM '/'
   
 void loadasirfile(char *name0)  void loadasirfile(char *name0)
 {  {
         FILE *in;    FILE *in;
         INFILE t;    INFILE t;
         extern char cppname[];    extern char cppname[];
 #if defined(VISUAL) || defined(__MINGW32__)  #if defined(VISUAL) || defined(__MINGW32__)
         char ibuf1[BUFSIZ],ibuf2[BUFSIZ];    char ibuf1[BUFSIZ],ibuf2[BUFSIZ];
         int ac;    int ac;
         char *av[BUFSIZ];    char *av[BUFSIZ];
         char *p;    char *p;
         FILE *fp;    FILE *fp;
         char dname[BUFSIZ],tname0[BUFSIZ];    char dname[BUFSIZ],tname0[BUFSIZ];
         char *name,*tname;    char *name,*tname;
         int encoded;    int encoded;
         static char prefix[BUFSIZ];    static char prefix[BUFSIZ];
         int process_id();    int process_id();
         char CppExe[BUFSIZ];    char CppExe[BUFSIZ];
         char nbuf[BUFSIZ],tnbuf[BUFSIZ];    char nbuf[BUFSIZ],tnbuf[BUFSIZ];
         STRING rootdir;    STRING rootdir;
         void call_exe(char *,char **);    void call_exe(char *,char **);
   
         /* create the unique prefix */    /* create the unique prefix */
         if ( !prefix[0] )    if ( !prefix[0] )
                 sprintf(prefix,"asir%d",process_id());      sprintf(prefix,"asir%d",process_id());
   
         fp = fopen(name0,"rb");    fp = fopen(name0,"rb");
         if ( getc(fp) == 0xff ) {    if ( getc(fp) == 0xff ) {
                 /* encoded file */      /* encoded file */
                 fclose(fp);      fclose(fp);
                 name = tempnam(NULL,prefix);      name = tempnam(NULL,prefix);
                 decrypt_file(name0,name);      decrypt_file(name0,name);
                 /* the file 'name' created */      /* the file 'name' created */
                 encoded = 1;      encoded = 1;
         } else {    } else {
                 fclose(fp);      fclose(fp);
                 name = name0;      name = name0;
                 encoded = 0;      encoded = 0;
         }    }
   
         strcpy(dname,name);    strcpy(dname,name);
         av[0] = "cpp";    av[0] = "cpp";
         sprintf(nbuf,"\"%s\"",name);    sprintf(nbuf,"\"%s\"",name);
         av[1] = nbuf;    av[1] = nbuf;
         tname = tempnam(NULL,prefix);    tname = tempnam(NULL,prefix);
         sprintf(tnbuf,"\"%s\"",tname);    sprintf(tnbuf,"\"%s\"",tname);
         av[2] = tnbuf;    av[2] = tnbuf;
         sprintf(ibuf1,"-I\"%s\"",asir_libdir);    sprintf(ibuf1,"-I\"%s\"",asir_libdir);
         av[3] = ibuf1;    av[3] = ibuf1;
         av[4] = "-DWINDOWS";    av[4] = "-DWINDOWS";
   
         /* set the include directory */    /* set the include directory */
         p = strrchr(dname,DELIM);    p = strrchr(dname,DELIM);
         if ( !p ) {    if ( !p ) {
                 av[5] = 0; ac = 5;      av[5] = 0; ac = 5;
         } else {    } else {
                 *p = 0;      *p = 0;
                 sprintf(ibuf2,"-I\"%s\"",dname);      sprintf(ibuf2,"-I\"%s\"",dname);
                 av[5] = ibuf2;      av[5] = ibuf2;
                 av[6] = 0; ac = 6;      av[6] = 0; ac = 6;
         }    }
 //      cpp_main(ac,av);  //  cpp_main(ac,av);
         Pget_rootdir(&rootdir);    Pget_rootdir(&rootdir);
         sprintf(CppExe,"%s\\bin\\cpp.exe",BDY(rootdir));    sprintf(CppExe,"%s\\bin\\cpp.exe",BDY(rootdir));
         call_exe(CppExe,av);    call_exe(CppExe,av);
   
         /* the file tname created */    /* the file tname created */
         if ( encoded ) {    if ( encoded ) {
                 unlink(name); free(name);      unlink(name); free(name);
   
                 strcpy(tname0,tname); free(tname);      strcpy(tname0,tname); free(tname);
                 tname = tempnam(NULL,prefix);      tname = tempnam(NULL,prefix);
   
                 encrypt_file(tname0,tname);      encrypt_file(tname0,tname);
                 /* the file tname created */      /* the file tname created */
                 unlink(tname0);      unlink(tname0);
   
                 in = fopen(tname,"rb");      in = fopen(tname,"rb");
         } else    } else
                 in = fopen(tname,"r");      in = fopen(tname,"r");
         if ( !in ) {    if ( !in ) {
                 perror("fopen");      perror("fopen");
                 error("load : failed");      error("load : failed");
         }    }
         t = (INFILE)MALLOC(sizeof(struct oINFILE));    t = (INFILE)MALLOC(sizeof(struct oINFILE));
         t->name = (char *)MALLOC(strlen(name0)+1); strcpy(t->name,name0);    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->tname = (char *)MALLOC(strlen(tname)+1); strcpy(t->tname,tname); free(tname);
         t->encoded = encoded;    t->encoded = encoded;
 #else  #else
         char com[BUFSIZ];    char com[BUFSIZ];
   
         sprintf(com,"%s -I%s -D__FILE__=%s %s",cppname,asir_libdir,name0,name0); in = popen(com,"r");    sprintf(com,"%s -I%s -D__FILE__=%s %s",cppname,asir_libdir,name0,name0); in = popen(com,"r");
         if ( !in ) {    if ( !in ) {
                 perror("popen");      perror("popen");
                 error("load : failed");      error("load : failed");
         }    }
         t = (INFILE)MALLOC(sizeof(struct oINFILE));    t = (INFILE)MALLOC(sizeof(struct oINFILE));
         t->name = (char *)MALLOC(strlen(name0)+1); strcpy(t->name,name0);    t->name = (char *)MALLOC(strlen(name0)+1); strcpy(t->name,name0);
 #endif  #endif
         t->fp = in; t->ln = 1; t->next = asir_infile; asir_infile = t;    t->fp = in; t->ln = 1; t->next = asir_infile; asir_infile = t;
         main_parser = 1; /* XXX */    main_parser = 1; /* XXX */
         Eungetc(afternl(),asir_infile->fp);    Eungetc(afternl(),asir_infile->fp);
         if ( !EPVS->va )    if ( !EPVS->va )
                 asir_reallocarray((char **)&EPVS->va,(int *)&EPVS->asize,(int *)&EPVS->n,(int)sizeof(struct oPV));      asir_reallocarray((char **)&EPVS->va,(int *)&EPVS->asize,(int *)&EPVS->n,(int)sizeof(struct oPV));
 }  }
   
 void execasirfile(char *name)  void execasirfile(char *name)
 {  {
         loadasirfile(name);    loadasirfile(name);
         if ( !SETJMP(asir_infile->jmpbuf) ) {    if ( !SETJMP(asir_infile->jmpbuf) ) {
                 asir_infile->ready_for_longjmp = 1;      asir_infile->ready_for_longjmp = 1;
                 read_eval_loop();      read_eval_loop();
         }    }
         closecurrentinput();    closecurrentinput();
 }  }
   
 static NODE objfile = 0;  static NODE objfile = 0;
   
 int loadfile(char *s)  int loadfile(char *s)
 {  {
         FILE *in;    FILE *in;
   
         if ( in = fopen(s,"r") ) {    if ( in = fopen(s,"r") ) {
                 fclose(in);      fclose(in);
                 loadasirfile(s);      loadasirfile(s);
                 return 1;      return 1;
         } else    } else
                 return 0;      return 0;
 }  }
   
 int loadfiles(NODE node) { return 0; }  int loadfiles(NODE node) { return 0; }
Line 450  static unsigned char decrypt_tab[] = {
Line 450  static unsigned char decrypt_tab[] = {
   
 unsigned char encrypt_char(unsigned char c)  unsigned char encrypt_char(unsigned char c)
 {  {
         return encrypt_tab[c][mt_genrand()&1];    return encrypt_tab[c][mt_genrand()&1];
 }  }
   
 unsigned char decrypt_char(unsigned char c)  unsigned char decrypt_char(unsigned char c)
 {  {
         return decrypt_tab[c];    return decrypt_tab[c];
 }  }
   
 void encrypt_file(char *in,char *out)  void encrypt_file(char *in,char *out)
 {  {
         FILE *infp,*outfp;    FILE *infp,*outfp;
         int c;    int c;
   
         infp = fopen(in,"r");    infp = fopen(in,"r");
         outfp = fopen(out,"wb");    outfp = fopen(out,"wb");
         while ( 1 ) {    while ( 1 ) {
                 c = getc(infp);      c = getc(infp);
                 if ( c == EOF )      if ( c == EOF )
                         break;        break;
                 putc(encrypt_char((unsigned char)c),outfp);      putc(encrypt_char((unsigned char)c),outfp);
         }    }
         fclose(infp); fclose(outfp);    fclose(infp); fclose(outfp);
 }  }
   
 void decrypt_file(char *in,char *out)  void decrypt_file(char *in,char *out)
 {  {
         FILE *infp,*outfp;    FILE *infp,*outfp;
         int c;    int c;
   
         infp = fopen(in,"rb");    infp = fopen(in,"rb");
         outfp = fopen(out,"w");    outfp = fopen(out,"w");
         /* skip the magic number (=0xff) */    /* skip the magic number (=0xff) */
         getc(infp);    getc(infp);
         while ( 1 ) {    while ( 1 ) {
                 c = getc(infp);      c = getc(infp);
                 if ( c == EOF )      if ( c == EOF )
                         break;        break;
                 putc(decrypt_char((unsigned char)c),outfp);      putc(decrypt_char((unsigned char)c),outfp);
         }    }
         fclose(infp); fclose(outfp);    fclose(infp); fclose(outfp);
 }  }

Legend:
Removed from v.1.32  
changed lines
  Added in v.1.33

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>