| version 1.3, 2000/03/23 07:59:55 | 
version 1.22, 2021/03/12 06:45:55 | 
 | 
 | 
|  /* $OpenXM: OpenXM/src/k097/d.c,v 1.2 2000/01/21 03:01:25 takayama Exp $ */ | 
 /* $OpenXM: OpenXM/src/k097/d.c,v 1.21 2020/10/07 23:53:25 takayama Exp $ */ | 
|  /* simple.c,  1996, 1/1 --- 1/5 */ | 
 /* simple.c,  1996, 1/1 --- 1/5 */ | 
|  #include <stdio.h> | 
 #include <stdio.h> | 
|  #include <ctype.h> | 
 #include <ctype.h> | 
|  #include <setjmp.h> | 
 #include <setjmp.h> | 
|  #include <stdlib.h> | 
 #include <stdlib.h> | 
|   | 
 #include <unistd.h> | 
|   | 
 #include <sys/types.h> | 
|   | 
 #include <sys/stat.h> | 
|  #include "d.h" | 
 #include "d.h" | 
|  #include "simple.tab.h" | 
 #include "simple.tab.h" | 
|   | 
 #include "ox_pathfinder.h" | 
|   | 
  | 
|   | 
 #if defined(__CYGWIN__) | 
|   | 
 #define JMP_BUF sigjmp_buf | 
|   | 
 #define SETJMP(env)  sigsetjmp(env,1) | 
|   | 
 #define LONGJMP(env,p)  siglongjmp(env,p) | 
|   | 
 #else | 
|   | 
 #define JMP_BUF jmp_buf | 
|   | 
 #define SETJMP(env)  setjmp(env) | 
|   | 
 #define LONGJMP(env,p)  longjmp(env,p) | 
|   | 
 #endif | 
|   | 
  | 
|  jmp_buf KCenvOfParser; | 
 JMP_BUF KCenvOfParser; | 
|   | 
  | 
|  int DebugMode = 1; | 
 int DebugMode = 1; | 
|  extern int K00_verbose; | 
 extern int K00_verbose; | 
 | 
 | 
|   | 
  | 
|  int Interactive = 1; | 
 int Interactive = 1; | 
|   | 
  | 
|  main2(int argc, char *argv[]) { | 
 static int isThereStdin(); | 
|   | 
 #define MARK_CHAR  3 | 
|   | 
  | 
|   | 
 void main2(int argc, char *argv[]) { | 
|    FILE *f; | 
   FILE *f; | 
|    FILE *outf; | 
   FILE *outf; | 
|    char name[1024]; | 
   char name[1024]; | 
| Line 64  main2(int argc, char *argv[]) { | 
 
  | 
| Line 80  main2(int argc, char *argv[]) { | 
 
 
 | 
|    } | 
   } | 
|  } | 
 } | 
|   | 
  | 
|  repl(FILE *inFile,FILE *outFile) { | 
 void repl(FILE *inFile,FILE *outFile) { | 
|    int c; | 
   int c; | 
|    int t; | 
   int t; | 
|    Inop = newObject_d(); | 
   Inop = newObject_d(); | 
| Line 145  repl(FILE *inFile,FILE *outFile) { | 
 
  | 
| Line 161  repl(FILE *inFile,FILE *outFile) { | 
 
 
 | 
|          case INCREMENT: printf("\nKClex returns --- ++."); break; | 
         case INCREMENT: printf("\nKClex returns --- ++."); break; | 
|          case DECREMENT: printf("\nKClex returns --- --."); break; | 
         case DECREMENT: printf("\nKClex returns --- --."); break; | 
|          case MEMBER: printf("\nKClex returns --- ->."); break; | 
         case MEMBER: printf("\nKClex returns --- ->."); break; | 
|          case RESIDUEPUT: printf("\nKClex returns --- %=."); break; | 
         case RESIDUEPUT: printf("\nKClex returns --- %%=."); break; | 
|          case NEGATEPUT: printf("\nKClex returns --- ^=."); break; | 
         case NEGATEPUT: printf("\nKClex returns --- ^=."); break; | 
|          case MULTPUT: printf("\nKClex returns --- *=."); break; | 
         case MULTPUT: printf("\nKClex returns --- *=."); break; | 
|   | 
  | 
| Line 166  objectp newObject_d() { | 
 
  | 
| Line 182  objectp newObject_d() { | 
 
 
 | 
|      fprintf(stderr,"Error: No more memory in newObject_d().\n"); | 
     fprintf(stderr,"Error: No more memory in newObject_d().\n"); | 
|      exit(10); | 
     exit(10); | 
|    } | 
   } | 
|    obj->tag = Snull; | 
   obj->tag = Snull;  obj->attr = NULL; | 
|    return(obj); | 
   return(obj); | 
|  } | 
 } | 
|   | 
  | 
 | 
 | 
|        /*if (Mydebug) printf("identifier string=[%s]",name);*/ | 
       /*if (Mydebug) printf("identifier string=[%s]",name);*/ | 
|        if (isdigit(name[0])) { | 
       if (isdigit(name[0])) { | 
|          /**************************** | 
         /**************************** | 
|            /**case : machine integer. | 
           **case : machine integer. | 
|          KClval = newObject_d(); | 
         KClval = newObject_d(); | 
|          KClval->tag = Sinteger; | 
         KClval->tag = Sinteger; | 
|          sscanf(name,"%d",&(KClval->lc.ival));*************/ | 
         sscanf(name,"%d",&(KClval->lc.ival));*************/ | 
 | 
 | 
|          break; | 
         break; | 
|        } /* else : Identifier case.*/ | 
       } /* else : Identifier case.*/ | 
|   | 
  | 
|        if (d = isReserved(name)) { | 
       if ((d = isReserved(name))) { | 
|          if (Replace) printf0(name); | 
         if (Replace) printf0(name); | 
|          return(d); | 
         return(d); | 
|        } else { | 
       } else { | 
 | 
 | 
|   | 
  | 
|  } | 
 } | 
|   | 
  | 
|  KCerror(char *s)   /* You need this function. Otherwise, you get core. */ | 
 int KCerror(char *s)   /* You need this function. Otherwise, you get core. */ | 
|  { | 
 { | 
|    K00recoverFromError(); | 
   K00recoverFromError(); | 
|    fprintf(stderr,"\nSyntax Error in the line %d:%s\n",Linenumber,s); | 
   fprintf(stderr,"\nSyntax Error in the line %d:%s\n",Linenumber,s); | 
|    showStringBuff(Inop); return ; | 
   showStringBuff(Inop); | 
|    longjmp(KCenvOfParser,2); | 
   /* Clean the junks. Try load("debug/buggy.k"); */ | 
|   | 
   if (isThereStdin()) { | 
|   | 
         ungetc(MARK_CHAR,stdin); | 
|   | 
         while (fsgetc(Inop) > MARK_CHAR) ; | 
|   | 
   } | 
|   | 
   return 0; | 
|   | 
   LONGJMP(KCenvOfParser,2); | 
|    exit(1); | 
   exit(1); | 
|  } | 
 } | 
|   | 
  | 
|  readcomment() { | 
 int readcomment() { | 
|    int c; | 
   int c; | 
|    while (1) { | 
   while (1) { | 
|      c = fsgetc(Inop); | 
     c = fsgetc(Inop); | 
|      if (c == EOF) { | 
     if (c == EOF) { | 
|        fprintf(stderr,"%d: Unexpected end of file in a comment.\n",Linenumber); | 
       fprintf(stderr,"%d: Unexpected end of file in a comment.\n",Linenumber); | 
|        fsungetc(c,Inop); /* should change */ | 
       fsungetc(c,Inop); /* should change */ | 
|        return; | 
       return 0; | 
|      } | 
     } | 
|      if (c == '*') { | 
     if (c == '*') { | 
|        c = fsgetc(Inop); | 
       c = fsgetc(Inop); | 
|        if (c == '/') return; | 
       if (c == '/') return 0; | 
|      } | 
     } | 
|    } | 
   } | 
|  } | 
 } | 
| Line 653  char *readstring() { | 
 
  | 
| Line 675  char *readstring() { | 
 
 
 | 
|  } | 
 } | 
|   | 
  | 
|   | 
  | 
|  readchar() { | 
 int readchar() { | 
|    int c; | 
   int c; | 
|    if (Replace) putchar0('\''); | 
   if (Replace) putchar0('\''); | 
|    c = fsgetc(Inop); /* 'c.'   '\.c' */ | 
   c = fsgetc(Inop); /* 'c.'   '\.c' */ | 
 | 
 | 
|    return(c); | 
   return(c); | 
|  } | 
 } | 
|   | 
  | 
|  putchar0(c) | 
 void putchar0(c) | 
|  int c; | 
 int c; | 
|  { | 
 { | 
|    if (c > 0) fputc(c,outfile); | 
   if (c > 0) fputc(c,outfile); | 
|  } | 
 } | 
|   | 
  | 
|  printf0(s) | 
 void printf0(s) | 
|  char *s; | 
 char *s; | 
|  { | 
 { | 
|    int i = 0; | 
   int i = 0; | 
|    while (s[i] != '\0') putchar0(s[i++]); | 
   while (s[i] != '\0') putchar0(s[i++]); | 
|  } | 
 } | 
|   | 
  | 
|  printf1(s) | 
 void printf1(s) | 
|  char *s; | 
 char *s; | 
|  { | 
 { | 
|    int i = 0; | 
   int i = 0; | 
 | 
 | 
|    while (s[i] != '\0') putchar0(s[i++]); | 
   while (s[i] != '\0') putchar0(s[i++]); | 
|  } | 
 } | 
|   | 
  | 
|  isReserved(s) | 
 int isReserved(s) | 
|  char *s; | 
 char *s; | 
|  { | 
 { | 
|    char *r[] = {"auto","break","case","char","const","continue", | 
   char *r[] = {"auto","break","case","char","const","continue", | 
 | 
 | 
|   | 
  | 
|  } | 
 } | 
|   | 
  | 
|  shouldReplace(s) | 
 int shouldReplace(s) | 
|  char *s; | 
 char *s; | 
|  { | 
 { | 
|    char *r[] = {"dummy"}; | 
   char *r[] = {"dummy"}; | 
| Line 877  static int popFile() { | 
 
  | 
| Line 899  static int popFile() { | 
 
 
 | 
|    return(Saki); | 
   return(Saki); | 
|  } | 
 } | 
|   | 
  | 
|   | 
 static int isThereStdin() { | 
|   | 
   if (Stackp > 1 && (InopStack[1])->tag == Sfile | 
|   | 
           && (InopStack[1])->lc.file == stdin) { | 
|   | 
         return(1); | 
|   | 
   }else{ | 
|   | 
         return(0); | 
|   | 
   } | 
|   | 
 } | 
|   | 
  | 
|  int fsgetc(objectp op) { | 
 int fsgetc(objectp op) { | 
|    struct stringBuf *obuf; | 
   struct stringBuf *obuf; | 
|    char *str; | 
   char *str; | 
| Line 909  int fsgetc(objectp op) { | 
 
  | 
| Line 940  int fsgetc(objectp op) { | 
 
 
 | 
|   | 
  | 
|  int fsungetc(int c,objectp op) { | 
 int fsungetc(int c,objectp op) { | 
|    fprintf(stderr,"Sorry. fsungetc has not yet implemented.\n"); | 
   fprintf(stderr,"Sorry. fsungetc has not yet implemented.\n"); | 
|   | 
   return -1; | 
|  } | 
 } | 
|   | 
  | 
|  void clearInop() { | 
 void clearInop() { | 
| Line 961  void parseAstring(char *s) | 
 
  | 
| Line 993  void parseAstring(char *s) | 
 
 
 | 
|   | 
  | 
|  } | 
 } | 
|   | 
  | 
|  void loadFile(objectp op) | 
 objectp checkIfTheFileExists(objectp op) { | 
|  { | 
  | 
|    FILE *fp; | 
   FILE *fp; | 
|    char fname[1024]; | 
   char fname[1024]; | 
|   | 
   char *s; | 
|   | 
   objectp nullObj; | 
|   | 
   nullObj = NULL; | 
|    if (op->tag != Sstring) { | 
   if (op->tag != Sstring) { | 
|      fprintf(stderr,"File name must be given as an argment of load.\n"); | 
     fprintf(stderr,"File name must be given as an argment of load.\n"); | 
|      return; | 
     return nullObj; | 
|    } | 
   } | 
|    if (strlen(op->lc.str) > 1000) { | 
   if (strlen(op->lc.str) > 800) { | 
|      fprintf(stderr,"Too long file name.\n"); | 
     fprintf(stderr,"Too long file name.\n"); | 
|      return; | 
     return nullObj; | 
|    } | 
   } | 
|    fp = fopen(op->lc.str,"r"); | 
   strcpy(fname,op->lc.str); | 
|   | 
   fp = fopen(fname,"r"); | 
|    if (fp == (FILE *)NULL) { | 
   if (fp == (FILE *)NULL) { | 
|      strcpy(fname,getLOAD_K_PATH()); | 
     strcpy(fname,getLOAD_K_PATH()); | 
|      strcat(fname,op->lc.str); | 
     strcat(fname,op->lc.str); | 
| Line 983  void loadFile(objectp op) | 
 
  | 
| Line 1018  void loadFile(objectp op) | 
 
 
 | 
|        strcat(fname,op->lc.str); | 
       strcat(fname,op->lc.str); | 
|        fp = fopen(fname,"r"); | 
       fp = fopen(fname,"r"); | 
|        if (fp == (FILE *)NULL) { | 
       if (fp == (FILE *)NULL) { | 
|          fprintf(stderr,"Cannot open the file <<%s>> for loading in the current directory nor the library directories %s and %s.\n",op->lc.str,getLOAD_K_PATH(),LOAD_K_PATH); | 
                 fprintf(stderr,"Cannot open the file <<%s>> for loading in the current directory nor the library directories %s and %s.\n",op->lc.str,getLOAD_K_PATH(),LOAD_K_PATH); | 
|          return; | 
                 return nullObj; | 
|        } | 
       } | 
|      } | 
     } | 
|    } | 
   } | 
|   | 
   fclose(fp); | 
|   | 
   op = newObject_d(); | 
|   | 
   op->tag = Sstring; | 
|   | 
   s = (char *)GC_malloc(sizeof(char)*(strlen(fname)+1)); | 
|   | 
   if (s == NULL) fprintf(stderr,"No more memory.\n"); | 
|   | 
   strcpy(s,fname); | 
|   | 
   op->lc.str = s; | 
|   | 
   return(op); | 
|   | 
 } | 
|   | 
  | 
|   | 
 void loadFile(objectp op) | 
|   | 
 { | 
|   | 
   FILE *fp; | 
|   | 
   char fname[1024]; | 
|   | 
   if (op->tag != Sstring) { | 
|   | 
     fprintf(stderr,"File name must be given as an argment of load.\n"); | 
|   | 
     return; | 
|   | 
   } | 
|   | 
   op = checkIfTheFileExists(op); | 
|   | 
   if (op == NULL) return; | 
|   | 
   if (strlen(op->lc.str) > 1000) { | 
|   | 
     fprintf(stderr,"Too long file name.\n"); | 
|   | 
     return; | 
|   | 
   } | 
|   | 
   fp = fopen(op->lc.str,"r"); | 
|    if (K00_verbose) fprintf(stderr,"Reading the file <<%s>>...  ",op->lc.str); | 
   if (K00_verbose) fprintf(stderr,"Reading the file <<%s>>...  ",op->lc.str); | 
|    parseAfile(fp); | 
   parseAfile(fp); | 
|    if (K00_verbose) fprintf(stderr,"\nClosed the file <<%s>>.\n",op->lc.str); | 
   if (K00_verbose) fprintf(stderr,"\nClosed the file <<%s>>.\n",op->lc.str); | 
| Line 997  void loadFileWithCpp(objectp op) | 
 
  | 
| Line 1057  void loadFileWithCpp(objectp op) | 
 
 
 | 
|  { | 
 { | 
|    FILE *fp; | 
   FILE *fp; | 
|    char fname[1024]; | 
   char fname[1024]; | 
|   | 
   char tmpName[1024]; | 
|   | 
   int pid; | 
|    objectp ob; | 
   objectp ob; | 
|   | 
   char *outfile; | 
|   | 
   char *cpp; | 
|   | 
   char *argv[10]; | 
|   | 
   int n; | 
|   | 
   char *sfile = NULL; | 
|    if (op->tag != Sstring) { | 
   if (op->tag != Sstring) { | 
|      fprintf(stderr,"File name must be given as an argment of load.\n"); | 
     fprintf(stderr,"File name must be given as an argment of load.\n"); | 
|      return; | 
     return; | 
|    } | 
   } | 
|   | 
   op = checkIfTheFileExists(op); | 
|   | 
   if (op == NULL) return; | 
|    if (strlen(op->lc.str) > 900) { | 
   if (strlen(op->lc.str) > 900) { | 
|      fprintf(stderr,"Too long file name.\n"); | 
     fprintf(stderr,"Too long file name.\n"); | 
|      return; | 
     return; | 
|    } | 
   } | 
|    system("/bin/rm -f k00.cppload.tmp"); | 
   /* Use gcc -v to know what symbols are defined. | 
|    strcpy(fname,"/lib/cpp -P -lang-c++ <"); | 
          if  defined(linux) || defined(__linux__) | 
|    strcat(fname,op->lc.str); | 
          Removed old codes. */ | 
|    strcat(fname,"  >k00.cppload.tmp"); | 
  | 
|    system(fname); | 
   sfile = op->lc.str; | 
|   | 
   cpp = getCppPath(); | 
|   | 
   if (cpp == NULL) { | 
|   | 
         fprintf(stderr,"cpp is not found.\n"); return; | 
|   | 
   } | 
|   | 
   /* printf("%s\n",cpp); */ | 
|   | 
   outfile = generateTMPfileName("k0-cpp"); | 
|   | 
   if (outfile == NULL) { | 
|   | 
         fprintf(stderr,"Failed to generate a temporary file name.\n"); return; | 
|   | 
   } | 
|   | 
   /* printf("%s\n",outfile); */ | 
|   | 
   if ((char *)strstr(cpp,"/asir/bin/cpp.exe") == NULL) { | 
|   | 
 #if defined(__clang__) || defined(__FreeBSD__) | 
|   | 
 /* cpp of the FreeBSD is the cpp of the clang, but gcc is selected by configure. | 
|   | 
    echo | gcc -dM -E - | 
|   | 
 */ | 
|   | 
     sprintf(tmpName,"cpp -E -P %s | sed -e 's/^#.*//g' >%s",sfile,outfile); | 
|   | 
 #else | 
|   | 
         argv[0] = cpp; | 
|   | 
         argv[1] = "-P"; | 
|   | 
         argv[2] = "-lang-c++"; | 
|   | 
         argv[3] = sfile; | 
|   | 
         argv[4] = outfile; | 
|   | 
         argv[5] = NULL; | 
|   | 
 #endif | 
|   | 
   }else{ | 
|   | 
         argv[0] = cpp; | 
|   | 
         argv[1] = "-P"; | 
|   | 
         argv[2] = cygwinPathToWinPath(sfile); | 
|   | 
         argv[3] = cygwinPathToWinPath(outfile); | 
|   | 
         argv[4] = NULL; | 
|   | 
   } | 
|   | 
 #if defined(__clang__) || defined(__FreeBSD__) | 
|   | 
   system(tmpName); | 
|   | 
 #else | 
|   | 
   n=oxForkExecBlocked(argv); | 
|   | 
 #endif | 
|   | 
  | 
|    ob = newObject_d(); | 
   ob = newObject_d(); | 
|    ob->tag = Sstring; | 
   ob->tag = Sstring; | 
|    ob->lc.str = "k00.cppload.tmp"; | 
   ob->lc.str = outfile; | 
|    loadFile(ob); | 
   loadFile(ob); | 
|   | 
   unlink(outfile); | 
|  } | 
 } | 
|   | 
  | 
|  void showStringBuff(objectp op) | 
 void showStringBuff(objectp op) | 
| Line 1032  void showStringBuff(objectp op) | 
 
  | 
| Line 1139  void showStringBuff(objectp op) | 
 
 
 | 
|    if (K00_verbose) { | 
   if (K00_verbose) { | 
|      fprintf(stderr,"stringBuff ptr = %d, ",ptr); | 
     fprintf(stderr,"stringBuff ptr = %d, ",ptr); | 
|      fprintf(stderr,"sb[ptr] = %x,%d",(sb->str)[ptr],(sb->str)[ptr]); | 
     fprintf(stderr,"sb[ptr] = %x,%d",(sb->str)[ptr],(sb->str)[ptr]); | 
|      fprintf(stderr,"Saki(yomi) = %x \n",Saki,Saki); | 
     fprintf(stderr,"Saki(yomi) = %x,%d \n",Saki,Saki); | 
|    } | 
   } | 
|    if (ptr == 0 && Saki == -1) { | 
   if (ptr == 0 && Saki == -1) { | 
|      fprintf(stderr," ; was expected.\n"); | 
     fprintf(stderr," ; was expected.\n"); | 
| Line 1046  void showStringBuff(objectp op) | 
 
  | 
| Line 1153  void showStringBuff(objectp op) | 
 
 
 | 
|    fprintf(stderr,"\n"); | 
   fprintf(stderr,"\n"); | 
|  } | 
 } | 
|   | 
  | 
|   | 
  | 
|   | 
  | 
|  char *getLOAD_K_PATH() { | 
 char *getLOAD_K_PATH() { | 
|    char *p; | 
   return getLOAD_K_PATH2(); | 
|    char *p2; | 
  | 
|    p = getenv("LOAD_K_PATH"); | 
  | 
|    if (p == NULL) { | 
  | 
|      return("/usr/local/lib/kxx97/yacc/"); | 
  | 
|    }else{ | 
  | 
|      if (strlen(p) == 0) return(p); | 
  | 
|      if (p[strlen(p)-1] == '/') return(p); | 
  | 
|      /* Add / */ | 
  | 
|      p2 = (char *) malloc(sizeof(char)*(strlen(p)+3)); | 
  | 
|      if (p2 == NULL) { fprintf(stderr,"No more memory.\n"); exit(10); } | 
  | 
|      strcpy(p2,p); strcat(p2,"/"); | 
  | 
|      return(p2); | 
  | 
|    } | 
  | 
|  } | 
 } | 
|   | 
  |