[BACK]Return to ox_pathfinder.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / util

Annotation of OpenXM/src/util/ox_pathfinder.c, Revision 1.15

1.15    ! takayama    1: /* $OpenXM: OpenXM/src/util/ox_pathfinder.c,v 1.14 2003/12/04 03:17:33 takayama Exp $ */
1.1       takayama    2: /* Moved from misc-2003/07/cygwin/test.c */
1.5       takayama    3:
1.1       takayama    4: #include <stdio.h>
                      5: #include <sys/types.h>
                      6: #include <sys/stat.h>
                      7: #include <fcntl.h>
                      8: #include <stdlib.h>
                      9: #include <unistd.h>
                     10: #include <sys/wait.h>
                     11: #include <signal.h>
                     12: #include <ctype.h>
1.5       takayama   13: #include <time.h>
                     14: #include <string.h>
1.1       takayama   15: #include "ox_pathfinder.h"
                     16:
1.14      takayama   17: int OX_P_stdin = -1;
                     18: int OX_P_stdout = -1;
                     19: int OX_P_stderr = -1;
                     20:
1.1       takayama   21: extern char **environ;
                     22:
                     23: static int getOStypei();
                     24: static char *addSlash();
                     25: static char *get_sm1_lib_path();
                     26: static char *get_k0_lib_path();
                     27: static char *get_ox_asir_path();
                     28: static char *get_ox_sm1_path();
                     29: static char *get_ox_path();
                     30: static char *get_oxc_path();
                     31: static char *get_oxlog_path();
1.5       takayama   32: static int getFileSize(char *fn);
1.8       takayama   33: static void errorPathFinder(char *s);
                     34: static void msgPathFinder(char *s);
1.1       takayama   35:
                     36:
1.4       takayama   37: static int Verbose_get_home = 0;
1.1       takayama   38: static int NoX = 0;
                     39:
1.2       takayama   40:
                     41: #define nomemory(a) {fprintf(stderr,"(%d) no more memory.\n",a);exit(10);}
1.3       takayama   42: #define mymalloc(a)  sGC_malloc(a)
1.2       takayama   43:
1.8       takayama   44: static void errorPathFinder(char *s) {
                     45:   /* Todo; we need to return the error message to the client if it is used
                     46:      in ox_shell */
                     47:   fprintf(stderr,"Error: %s",s);
                     48: }
                     49: static void msgPathFinder(char *s) {
                     50:   /* Todo; we need to return the error message to the client if it is used
                     51:      in ox_shell */
                     52:   fprintf(stderr,"Log(ox_pathfinder): %s",s);
                     53: }
                     54:
1.2       takayama   55: int ox_pathfinderNoX(int f) {
                     56:   if (f < 0) return NoX;
                     57:   NoX = f;
1.4       takayama   58:   return f;
                     59: }
                     60: int ox_pathfinderVerbose(int f) {
                     61:   extern Verbose_get_home;
                     62:   if (f < 0) return Verbose_get_home;
                     63:   Verbose_get_home = f;
1.2       takayama   64:   return f;
                     65: }
                     66:
1.5       takayama   67: /* test main   */
                     68: /*
                     69: main() {
                     70:   char *outfile;
                     71:   char *cpp;
                     72:   char *argv[10];
                     73:   int n;
                     74:   char *sfile = "ox_pathfinder.c";
                     75:   if (getFileSize(sfile) < 0) {
                     76:     fprintf(stderr,"The source file is not found.\n"); return -1;
                     77:   }
                     78:   cpp = getCppPath();
                     79:   if (cpp == NULL) {
                     80:     fprintf(stderr,"cpp is not found.\n"); return -1;
                     81:   }
                     82:   printf("%s\n",cpp);
                     83:   outfile = generateTMPfileName("seed");
                     84:   if (outfile == NULL) {
                     85:     fprintf(stderr,"Failed to generate a temporary file name.\n"); return -1;
                     86:   }
                     87:   printf("%s\n",outfile);
                     88:   if ((char *)strstr(cpp,"/asir/bin/cpp.exe") == NULL) {
                     89:     argv[0] = cpp;
                     90:     argv[1] = "-P";
                     91:     argv[2] = "-lang-c++";
                     92:     argv[3] = sfile;
                     93:     argv[4] = outfile;
                     94:     argv[5] = NULL;
                     95:   }else{
                     96:     argv[0] = cpp;
                     97:     argv[1] = cygwinPathToWinPath(sfile);
                     98:     argv[2] = cygwinPathToWinPath(outfile);
                     99:     argv[3] = NULL;
                    100:   }
                    101:   n=oxForkExecBlocked(argv);
                    102: }
                    103:
                    104: void *sGC_malloc(int s) { return (void *) malloc(s); }
                    105: */
                    106:
                    107: /* -------- end of test main ----------- */
                    108:
1.2       takayama  109: /* See kxx/ox100start.c for main */
                    110:
1.1       takayama  111: #define MYFORKCP_SIZE 100
                    112: static int Myforkchildren[MYFORKCP_SIZE];
                    113: static int Myforkcp=0;
                    114: static void myforkwait() {
                    115:   int status;
                    116:   int pid;
                    117:   int i,j;
1.13      takayama  118:   /* signal(SIGCHLD,SIG_IGN);  It is not allowed in posix */
1.1       takayama  119:   pid = wait(&status);
                    120:   fprintf(stderr,"Child process %d is exiting.\n",pid);
1.13      takayama  121:   if (pid < 0) {
1.14      takayama  122:     perror("wait");
1.13      takayama  123:   }
1.1       takayama  124:   for (i=0; i<Myforkcp; i++) {
                    125:     if (Myforkchildren[i]  == pid) {
                    126:       for (j=i; j<Myforkcp-1; j++) {
                    127:         Myforkchildren[j] = Myforkchildren[j+1];
                    128:       }
                    129:       if (Myforkcp > 0) Myforkcp--;
                    130:     }
                    131:   }
                    132:   signal(SIGCHLD,myforkwait);
                    133: }
                    134:
1.2       takayama  135: int oxForkExec(char **argv) {
1.1       takayama  136:   int pid;
                    137:   char **eee;
                    138:   int m;
                    139:   m = 0;
                    140:   if (argv == NULL) {
                    141:     fprintf(stderr,"Cannot fork and exec.\n"); return -1;
                    142:   }
                    143:   if ((pid = fork()) > 0) {
1.15    ! takayama  144:        oxResetRedirect();
1.1       takayama  145:     if (m&2) {
1.5       takayama  146:       /* Do not call singal to turn around a trouble on cygwin. BUG. */
                    147:     }else{
                    148:       signal(SIGCHLD,myforkwait); /* to kill Zombie */
                    149:     }
                    150:     Myforkchildren[Myforkcp++] = pid;
                    151:     if (Myforkcp >= MYFORKCP_SIZE-1) {
                    152:       fprintf(stderr,"Child process table is full.\n");
                    153:       Myforkcp = 0;
                    154:     }
1.1       takayama  155:   }else{
                    156:     /* close the specified files */
1.5       takayama  157:     if (m&1) {
1.1       takayama  158:        sigset_t sss;
                    159:        sigemptyset(&sss);
                    160:        sigaddset(&sss,SIGINT);
                    161:        sigprocmask(SIG_BLOCK,&sss,NULL);
1.5       takayama  162:     }
                    163:     if (NoX) {
                    164:       FILE *null;
                    165:       null = fopen("/dev/null","wb");
1.14      takayama  166:       if (OX_P_stdout >= 0) dup2(OX_P_stdout,1); else dup2(fileno(null),1);
                    167:       if (OX_P_stderr >= 0) dup2(OX_P_stderr,2); else dup2(fileno(null),2);
                    168:     }else{
                    169:       if (OX_P_stdout >= 0) dup2(OX_P_stdout,1);
                    170:       if (OX_P_stderr >= 0) dup2(OX_P_stderr,2);
1.5       takayama  171:     }
1.14      takayama  172:     if (OX_P_stdin >= 0) dup2(OX_P_stdin,0);
1.1       takayama  173:     execve(argv[0],argv,environ);
                    174:     /* This place will never be reached unless execv fails. */
1.2       takayama  175:     fprintf(stderr,"oxForkExec fails: ");
1.5       takayama  176:     exit(3);
                    177:   }
                    178: }
                    179:
                    180: int oxForkExecBlocked(char **argv) {
                    181:   int pid;
                    182:   char **eee;
                    183:   int m;
                    184:   int status;
                    185:   if (argv == NULL) {
                    186:     fprintf(stderr,"Cannot fork and exec.\n"); return -1;
                    187:   }
                    188:   if ((pid = fork()) > 0) {
1.15    ! takayama  189:        oxResetRedirect();
1.5       takayama  190:     Myforkchildren[Myforkcp++] = pid;
                    191:     if (Myforkcp >= MYFORKCP_SIZE-1) {
                    192:       fprintf(stderr,"Child process table is full.\n");
                    193:       Myforkcp = 0;
                    194:     }
1.14      takayama  195:     if (waitpid(pid,&status,0) < 0) {  /* blocked */
                    196:       perror("waitpid");
                    197:     }
1.5       takayama  198:     return status;
                    199:   }else{
                    200:     /* close the specified files */
                    201:     if (m&1) {
                    202:        sigset_t sss;
                    203:        sigemptyset(&sss);
                    204:        sigaddset(&sss,SIGINT);
                    205:        sigprocmask(SIG_BLOCK,&sss,NULL);
                    206:     }
                    207:     if (NoX) {
                    208:       FILE *null;
                    209:       null = fopen("/dev/null","wb");
1.14      takayama  210:       if (OX_P_stdout >= 0) dup2(OX_P_stdout,1); else dup2(fileno(null),1);
                    211:       if (OX_P_stderr >= 0) dup2(OX_P_stderr,2); else dup2(fileno(null),2);
                    212:     }else{
                    213:       if (OX_P_stdout >= 0) dup2(OX_P_stdout,1);
                    214:       if (OX_P_stderr >= 0) dup2(OX_P_stderr,2);
1.5       takayama  215:     }
1.14      takayama  216:     if (OX_P_stdin >= 0) dup2(OX_P_stdin,0);
1.5       takayama  217:     execve(argv[0],argv,environ);
                    218:     /* This place will never be reached unless execv fails. */
                    219:     fprintf(stderr,"oxForkExecBlock fails: ");
                    220:     exit(3);
1.1       takayama  221:   }
                    222: }
                    223:
                    224: static int getOStypei() {
                    225:   /*
                    226:      0  unix
                    227:      1  windows-cygwin
                    228:      2  windows-cygwin-on-X
                    229:      3  windows-native
                    230:   */
                    231:   int ostype;
                    232:   char *s,*s2,*s3;
                    233: #if defined(__CYGWIN__)
                    234:   ostype = 1;
                    235: #else
                    236:   ostype = 0;
                    237: #endif
                    238:   if (ostype == 0) return ostype;
                    239:   /* Heuristic method */
                    240:   s = (char *)getenv("WINDOWID");
                    241:   if (s != NULL) {
                    242:     return 2;
                    243:   }
                    244:   s = (char *)getenv("OSTYPE");
                    245:   s2 = (char *)getenv("MACHTYPE");
                    246:   s3 = (char *)getenv("PWD");
                    247:   if ((s != NULL) || (s2 != NULL) || (s3 != NULL)) {
                    248:     return 1;
                    249:   }
                    250:   return 3;
                    251: }
                    252:
                    253: char *getOStypes() {
                    254:   int ostype;
                    255:   ostype = getOStypei();
                    256:
                    257:   if (ostype == 1) {
                    258:     return("Windows-cygwin");
                    259:   }else if (ostype == 2) {
                    260:     return("Windows-cygwin-on-X");
                    261:   }else if (ostype == 3) {
                    262:     return("Windows-native");
                    263:   }else{
                    264:     return("unix");
                    265:   }
                    266: }
                    267:
                    268: /*
                    269:   kan96xx/Kan/ext.c
                    270: */
                    271: /*
                    272:  -1          : no file
                    273:  non-negative: there is a regular file or a directory
                    274: */
                    275: static int getFileSize(char *s) {
                    276:    struct stat buf;
                    277:    int m;
                    278:    if (s == NULL) return -1;
                    279:    m = stat(s,&buf);
                    280:    if (m == -1) {
                    281:      return -1;
                    282:       /* fail */
                    283:    }else{
                    284:       /* success */
                    285:      return (int) buf.st_size;
                    286:    }
                    287: }
                    288:
                    289: static char *addSlash(char *p) {
                    290:   char *p2;
                    291:    if ((strlen(p) == 0) || (p == NULL)) return(p);
                    292:     if (p[strlen(p)-1] == '/') return(p);
                    293:     /* Add / */
                    294:     p2 = (char *) mymalloc(sizeof(char)*(strlen(p)+3));
                    295:     if (p2 == NULL) { fprintf(stderr,"No more memory.\n"); exit(10); }
                    296:     strcpy(p2,p); strcat(p2,"/");
                    297:     return(p2);
                    298:
                    299: }
                    300:
                    301: static void msg_get_home(int t,char *s) {
                    302:   extern int Verbose_get_home;
                    303:   if (!Verbose_get_home) return;
                    304:   if (t == 1) {
                    305:     fprintf(stderr,"getOpenXM_HOME(): ");
                    306:   }else if (t == 2) {
                    307:     fprintf(stderr,"getServerEnv(): ");
                    308:   }else if (t == 3) {
1.2       takayama  309:     fprintf(stderr,"?? ");
1.1       takayama  310:   }else if (t == 4) {
                    311:     fprintf(stderr,"cygwinPathToWinPath(): ");
                    312:   }else if (t == 5) {
                    313:     fprintf(stderr,"catArgv(): ");
                    314:   }else{
                    315:     fprintf(stderr,"getting path...: ");
                    316:   }
                    317:   if (s != NULL) {
                    318:     fprintf(stderr,"%s\n",s);
                    319:   }else{
                    320:     fprintf(stderr," --NULL-- \n");
                    321:   }
                    322: }
                    323: /* cf. k097/d.c    getLOAD_K_PATH();
                    324:        kan96xx/Kan/scanner.c   getLOAD_SM1_PATH();
                    325:  */
                    326: char *getOpenXM_HOME() {
                    327:   char *p;
                    328:   char *h;
                    329:   p = getOStypes();
                    330:   msg_get_home(1,p);
                    331:
                    332:   p = (char *) getenv("OpenXM_HOME");
                    333:   if (getFileSize(p) != -1) return addSlash(p);
                    334:   msg_get_home(1,"OpenXM_HOME is not found.");
                    335:
                    336:   p = (char *) getenv("OPENXM_HOME");
                    337:   if (getFileSize(p) != -1) return addSlash(p);
                    338:   msg_get_home(1,"OPENXM_HOME is not found.");
                    339:
                    340:   if (getOStypei() == 3) { /* cygwin-native */
                    341:     p = (char *) getenv("OpenXM_HOME_WIN");
                    342:     if (getFileSize(p) != -1) return addSlash(p);
                    343:     msg_get_home(1,"OpenXM_HOME_WIN is not found.");
                    344:
                    345:     p = (char *) getenv("OPENXMHOMEWIN");
                    346:     if (getFileSize(p) != -1) return addSlash(p);
                    347:     msg_get_home(1,"OPENXMHOMEWIN is not found.");
                    348:   }
                    349:
                    350:   /* Try to find default directories */
                    351:   h = (char *)getenv("HOME");
                    352:   if (h != NULL) {
                    353:     p = (char *)mymalloc(strlen(h)+100);
                    354:     if (p == NULL) {
                    355:       fprintf(stderr,"No more memory.\n"); exit(100);
                    356:     }
                    357:     strcat(h,"/OpenXM");
                    358:     p = h;
                    359:     if (getFileSize(p) != -1) return addSlash(p);
                    360:     msg_get_home(1,"OpenXM is not found under the home directory.");
                    361:   }
                    362:
                    363:   if (getOStypei() != 3) {
                    364:     p = "/usr/local/OpenXM";
                    365:   }else{
                    366:     p = "/cygdrive/c/usr/local/OpenXM";
                    367:   }
                    368:   if (getFileSize(p) != -1) return addSlash(p);
                    369:   msg_get_home(1,"OpenXM is not found under /usr/local");
                    370:
                    371:   if (getOStypei() != 0) {
                    372:     p = "/cygdrive/c/OpenXM";
                    373:     if (getFileSize(p) != -1) return addSlash(p);
                    374:     msg_get_home(1,"OpenXM is not found under c:\\");
                    375:
                    376:     p = "/cygdrive/c/OpenXM-win";
                    377:     if (getFileSize(p) != -1) return addSlash(p);
                    378:     msg_get_home(1,"OpenXM-win is not found under c:\\");
                    379:
                    380:     p = "/cygdrive/c/Program Files/OpenXM";
                    381:     if (getFileSize(p) != -1) return addSlash(p);
                    382:     msg_get_home(1,"OpenXM is not found under c:\\Program Files");
                    383:
                    384:     p = "/cygdrive/c/Program Files/OpenXM-win";
                    385:     if (getFileSize(p) != -1) return addSlash(p);
                    386:     msg_get_home(1,"OpenXM-win is not found under c:\\Program Files");
                    387:
                    388:   }
                    389:
                    390:   msg_get_home(1,"Giving up!");
                    391:   return NULL;
                    392:
                    393: }
                    394:
                    395: static char *get_k0_lib_path() {
                    396:   char *oxhome;
                    397:   char *p;
                    398:
                    399:
                    400:   p = (char *)getenv("LOAD_K_PATH");
                    401:   if (p != NULL) {
                    402:     if (getFileSize(p) != -1) return addSlash(p);
                    403:     msg_get_home(1,"LOAD_K0_PATH is not found.");
                    404:   }
                    405:
                    406:   oxhome = getOpenXM_HOME();
                    407:   if (oxhome == NULL) return (char *)NULL;
                    408:   p = (char *) mymalloc(strlen(oxhome)+100);
                    409:   if (p == NULL) {fprintf(stderr,"No more memory.\n"); exit(100);}
                    410:
                    411:   strcpy(p,oxhome);
                    412:   strcat(p,"lib/k097");
                    413:   if (getFileSize(p) != -1) return addSlash(p);
                    414:   msg_get_home(1,oxhome);
                    415:   msg_get_home(1,"     is found, but ");
                    416:   msg_get_home(1,p);
                    417:   msg_get_home(1,"     is not found.");
                    418:
                    419:   msg_get_home(1,"Giving up!");
                    420:   return NULL;
                    421: }
                    422:
                    423: static char *get_sm1_lib_path() {
                    424:   char *oxhome;
                    425:   char *p;
                    426:
                    427:   p = (char *)getenv("LOAD_SM1_PATH");
                    428:   if (p != NULL) {
                    429:     if (getFileSize(p) != -1) return addSlash(p);
                    430:     msg_get_home(1,"LOAD_SM1_PATH is not found.");
                    431:   }
                    432:
                    433:   oxhome = getOpenXM_HOME();
                    434:   if (oxhome == NULL) return NULL;
                    435:   p = (char *) mymalloc(strlen(oxhome)+100);
                    436:   if (p == NULL) {fprintf(stderr,"No more memory.\n"); exit(100);}
                    437:
                    438:   strcpy(p,oxhome);
                    439:   strcat(p,"lib/sm1");
                    440:   if (getFileSize(p) != -1) return addSlash(p);
                    441:   msg_get_home(1,oxhome);
                    442:   msg_get_home(1,"     is found, but ");
                    443:   msg_get_home(1,p);
                    444:   msg_get_home(1,"     is not found.");
                    445:
                    446:   msg_get_home(1,"Giving up!");
                    447:   return NULL;
                    448: }
                    449:
                    450: static char *get_ox_asir_path() {
                    451:   char *oxhome;
                    452:   char *p;
                    453:
                    454:   oxhome = getOpenXM_HOME();
                    455:   if (oxhome == NULL) return NULL;
                    456:   p = (char *) mymalloc(strlen(oxhome)+100);
                    457:   if (p == NULL) {fprintf(stderr,"No more memory.\n"); exit(100);}
                    458:
                    459:   strcpy(p,oxhome);
                    460:   strcat(p,"bin/ox_asir");
                    461:   if (getFileSize(p) != -1) return p;
                    462:   msg_get_home(1,oxhome);
                    463:   msg_get_home(1,"     is found, but ");
                    464:   msg_get_home(1,p);
                    465:   msg_get_home(1,"     is not found.");
                    466:
                    467:   strcpy(p,oxhome);
                    468:   strcat(p,"lib/asir/ox_asir");
                    469:   if (getFileSize(p) != -1) return p;
                    470:   msg_get_home(1,oxhome);
                    471:   msg_get_home(1,"     is found, but ");
                    472:   msg_get_home(1,p);
                    473:   msg_get_home(1,"     is not found.");
                    474:
                    475:   msg_get_home(1,"Giving up!");
                    476:   return NULL;
                    477: }
                    478:
                    479: static char *get_ox_path() {
                    480:   char *oxhome;
                    481:   char *p;
                    482:
                    483:   oxhome = getOpenXM_HOME();
                    484:   if (oxhome == NULL) return NULL;
                    485:   p = (char *) mymalloc(strlen(oxhome)+100);
                    486:   if (p == NULL) {fprintf(stderr,"No more memory.\n"); exit(100);}
                    487:
                    488:   strcpy(p,oxhome);
                    489:   strcat(p,"bin/ox");
                    490:   if (getFileSize(p) != -1) return p;
                    491:   msg_get_home(1,oxhome);
                    492:   msg_get_home(1,"     is found, but ");
                    493:   msg_get_home(1,p);
                    494:   msg_get_home(1,"     is not found.");
                    495:
                    496:   msg_get_home(1,"Giving up!");
                    497:   return NULL;
                    498: }
                    499:
                    500:
                    501: static char *get_oxc_path() {
                    502:   char *oxhome;
                    503:   char *p;
                    504:
                    505:   oxhome = getOpenXM_HOME();
                    506:   if (oxhome == NULL) return NULL;
                    507:   p = (char *) mymalloc(strlen(oxhome)+100);
                    508:   if (p == NULL) {fprintf(stderr,"No more memory.\n"); exit(100);}
                    509:
                    510:   strcpy(p,oxhome);
                    511:   strcat(p,"bin/oxc");
                    512:   if (getFileSize(p) != -1) return p;
                    513:   msg_get_home(1,oxhome);
                    514:   msg_get_home(1,"     is found, but ");
                    515:   msg_get_home(1,p);
                    516:   msg_get_home(1,"     is not found.");
                    517:
                    518:   msg_get_home(1,"Giving up!");
                    519:   return NULL;
                    520: }
                    521:
                    522: static char *get_oxlog_path() {
                    523:   char *oxhome;
                    524:   char *p;
                    525:
                    526:   oxhome = getOpenXM_HOME();
                    527:   if (oxhome == NULL) return NULL;
                    528:   p = (char *) mymalloc(strlen(oxhome)+100);
                    529:   if (p == NULL) {fprintf(stderr,"No more memory.\n"); exit(100);}
                    530:
                    531:   strcpy(p,oxhome);
                    532:   strcat(p,"bin/oxlog");
                    533:   if (getFileSize(p) != -1) return p;
                    534:   msg_get_home(1,oxhome);
                    535:   msg_get_home(1,"     is found, but ");
                    536:   msg_get_home(1,p);
                    537:   msg_get_home(1,"     is not found.");
                    538:
                    539:   msg_get_home(1,"Giving up!");
                    540:   return NULL;
                    541: }
                    542:
                    543: char *cygwinPathToWinPath(char *s) {
                    544:   char *pos;
                    545:   char *ans;
                    546:   int i;
                    547:   msg_get_home(4,s);
                    548:   if (s == NULL) return NULL;
                    549:   if (strlen(s) == 0) return s;
                    550:
                    551:   ans = (char *) mymalloc(strlen(s) + 32);
                    552:   if (ans == NULL) nomemory(0);
                    553:
                    554:   pos = (char *)strstr(s,"/cygdrive/");
                    555:   if (pos == s) {
                    556:     strcpy(ans,&(s[9]));
                    557:     ans[0] = s[10]; ans[1] = ':'; ans[2] = '\\';
                    558:   }else{
                    559:     strcpy(ans,s);
                    560:   }
                    561:
                    562:   if (ans[0] == '/') {
                    563:     strcpy(ans,"C:\\cygwin");
                    564:     strcat(ans,s);
                    565:   }
                    566:
                    567:
                    568:   for (i=0; i <strlen(ans); i++) {
                    569:     if (ans[i] == '/') ans[i] = '\\';
                    570:   }
                    571:   return ans;
                    572: }
                    573:
                    574: char **getServerEnv(char *oxServer) {
                    575:   int ostype;
                    576:   char *p;
                    577:   char *oxhome;
                    578:   char *xterm;
                    579:   char *oxlog;
                    580:   char *load_sm1_path;
                    581:   char *load_k0_path;
                    582:   char *openXM_HOME;
                    583: #define ARGV_SIZE 100
                    584:   char *argv[ARGV_SIZE];
                    585:   int i,k;
                    586:   char **aaa;
                    587:
                    588:   if (Verbose_get_home) {
1.5       takayama  589:     if (oxServer == NULL) {
                    590:       fprintf(stderr,"Server name is NULL.\n");
                    591:     }else{
                    592:       fprintf(stderr,"Server name is %s\n",oxServer);
                    593:     }
1.1       takayama  594:   }
                    595:
                    596:   if (oxServer == NULL) return NULL;
                    597:   i = 0;
                    598:   argv[i] = NULL;
                    599:
                    600:   ostype = getOStypei();
                    601:
                    602:   oxhome = getOpenXM_HOME();
                    603:   if (oxhome == NULL) return NULL;
                    604:   p = (char *) mymalloc(strlen(oxhome)+strlen(oxServer)+100);
                    605:   if (p == NULL) {fprintf(stderr,"No more memory.\n"); exit(100);}
                    606:
                    607:   strcpy(p,oxhome);
                    608:   strcat(p,oxServer);
                    609:   if (getFileSize(p) == -1) {
                    610:     msg_get_home(2,oxhome);
                    611:     msg_get_home(2,"     is found, but ");
                    612:     msg_get_home(2,p);
                    613:     msg_get_home(2,"     is not found.");
                    614:     return (NULL);
                    615:   }
                    616:   oxServer = (char *) mymalloc(strlen(oxhome)+strlen(oxServer)+100);
                    617:   if (oxServer == NULL) {fprintf(stderr,"No more memory.\n"); exit(100);}
                    618:   strcpy(oxServer,p);
                    619:
                    620:   if ((ostype == 0) || (ostype == 2)) {
1.5       takayama  621:     if (!NoX) {
                    622:       xterm = "/usr/X11R6/bin/xterm";
                    623:       if (getFileSize(xterm) == -1) {
                    624:         msg_get_home(2,"xterm is not found. NoX is automatically set.");
                    625:         NoX = 1;
                    626:       }
                    627:     }
1.1       takayama  628:     oxlog = get_oxlog_path();
                    629:     xterm = "/usr/X11R6/bin/xterm -icon -e ";
                    630:     argv[i] = oxlog; i++; argv[i] = NULL;
1.5       takayama  631:     if (!NoX) {
                    632:       argv[i] = "/usr/X11R6/bin/xterm"; i++; argv[i] = NULL;
                    633:       argv[i] = "-icon"; i++; argv[i] = NULL;
                    634:       argv[i] = "-e"; i++; argv[i] = NULL;
                    635:     }
1.1       takayama  636:     argv[i] = get_ox_path(); i++; argv[i] = NULL;
                    637:     argv[i] = "-ox"; i++; argv[i] = NULL;
                    638:     argv[i] = oxServer; i++; argv[i] = NULL;
                    639:   }else{
1.5       takayama  640:     if (!NoX) {
                    641:       if (getFileSize("/cygdrive/c/winnt/system32/cmd.exe") >= 0) {
                    642:         xterm = "/cygdrive/c/winnt/system32/cmd.exe /c start /min ";
                    643:         argv[i] = "/cygdrive/c/winnt/system32/cmd.exe"; i++; argv[i] = NULL;
                    644:       }else if (getFileSize("/cygdrive/c/windows/system32/cmd.exe") >= 0) {
                    645:         xterm = "/cygdrive/c/windows/system32/cmd.exe  /c start /min ";
                    646:         argv[i] = "/cygdrive/c/windows/system32/cmd.exe"; i++; argv[i] = NULL;
                    647:       }else{
                    648:         msg_get_home(2,"cmd.exe is not found. NoX is automatically set.");
1.1       takayama  649:         NoX = 1;
1.5       takayama  650:       }
                    651:     }
1.1       takayama  652:     oxlog = " ";
1.5       takayama  653:     if (!NoX) {
                    654:       argv[i] = "/c"; i++; argv[i] = NULL;
                    655:       argv[i] = "start"; i++; argv[i] = NULL;
                    656:       argv[i] = "/min"; i++; argv[i] = NULL;
                    657:     }
1.1       takayama  658:     argv[i] = cygwinPathToWinPath(get_ox_path()); i++; argv[i] = NULL;
                    659:     argv[i] = "-ox"; i++; argv[i] = NULL;
                    660:     argv[i] = oxServer; i++; argv[i] = NULL;
                    661:   }
                    662:
                    663:   aaa = (char **) mymalloc(sizeof(char*)*(i+1));
                    664:   if (aaa == NULL) nomemory(0);
                    665:   msg_get_home(2,"--------- Result --------------");
                    666:   for (k=0; k<i; k++) {
                    667:     aaa[k] = argv[k];
                    668:     msg_get_home(2,aaa[k]);
                    669:     aaa[k+1] = NULL;
                    670:   }
                    671:   return aaa;
                    672: }
                    673:
                    674: char **setOXenv_old() {
                    675:   char *openXM_HOME;
                    676:   char *load_sm1_path;
                    677:   char *load_k0_path;
                    678:   char *asir_config;
                    679:   char *asir_libdir;
                    680:   char *asirloadpath;
                    681:   char *asir_rsh;
                    682:   char *ld_library_path;
                    683:   char **aaa;
                    684:   int n,i,k;
                    685:
                    686:   /* set environmental variables */
                    687:   n = 20;  /* n must be larger than the number of env vars */
                    688:   n++;
                    689:   load_sm1_path = (char *) getenv("LOAD_SM1_PATH");
                    690:   load_k0_path = (char *) getenv("LOAD_SM1_PATH");
                    691:   asir_config = (char *) getenv("ASIR_CONFIG");
                    692:   asir_libdir = (char *) getenv("ASIR_LIBDIR");
                    693:   asirloadpath = (char *) getenv("ASIRLOADPATH");
                    694:   asir_rsh = (char *) getenv("ASIR_RSH");
                    695:   ld_library_path = (char *) getenv("LD_LIBRARY_PATH");
                    696:
                    697:   openXM_HOME = getOpenXM_HOME();
                    698:   if (openXM_HOME != NULL)
                    699:     openXM_HOME[strlen(openXM_HOME)-1] = '\0';
                    700:   /* How about ASIR... ? */
                    701:
                    702:   msg_get_home(3,"OpenXM_HOME is"); msg_get_home(2,openXM_HOME);
                    703:   msg_get_home(3,"LOAD_SM1_PATH is"); msg_get_home(2,load_sm1_path);
                    704:   msg_get_home(3,"LOAD_K0_PATH is"); msg_get_home(2,load_k0_path);
                    705:
                    706:   aaa = (char **) mymalloc(sizeof(char*)*n);
                    707:   if (aaa == NULL) nomemory(0);
                    708:
                    709:   i = 0;
                    710:   if (openXM_HOME != NULL) {
                    711:     aaa[i] = openXM_HOME; i++; aaa[i] = NULL; if (i > n-2) return aaa;
                    712:   }
                    713:   if (load_sm1_path != NULL) {
                    714:     aaa[i] = load_sm1_path; i++; aaa[i] = NULL; if (i > n-2) return aaa;
                    715:   }
                    716:   if (load_k0_path != NULL) {
                    717:     aaa[i] = load_k0_path; i++; aaa[i] = NULL; if (i > n-2) return aaa;
                    718:   }
                    719:   if (asir_config != NULL) {
                    720:     aaa[i] = asir_config; i++; aaa[i] = NULL; if (i > n-2) return aaa;
                    721:   }
                    722:   if (asir_libdir != NULL) {
                    723:     aaa[i] = asir_libdir; i++; aaa[i] = NULL; if (i > n-2) return aaa;
                    724:   }
                    725:   if (asirloadpath != NULL) {
                    726:     aaa[i] = asirloadpath; i++; aaa[i] = NULL; if (i > n-2) return aaa;
                    727:   }
                    728:   if (asir_rsh != NULL) {
                    729:     aaa[i] = asir_rsh; i++; aaa[i] = NULL; if (i > n-2) return aaa;
                    730:   }
                    731:
                    732:   msg_get_home(3,"--------- Result --------------");
                    733:   for (k=0; k<n; k++) {
                    734:     if (aaa[k] == NULL) break;
                    735:     msg_get_home(3,aaa[k]);
                    736:   }
                    737:
                    738:   return aaa;
                    739: }
                    740:
                    741: char **debugServerEnv(char *oxServer)
                    742: {
                    743:   int t;
                    744:   char **aaa;
                    745:   t = Verbose_get_home;
                    746:   Verbose_get_home = 1;
                    747:   aaa = getServerEnv(oxServer);
                    748:   Verbose_get_home = t;
                    749:   return ( aaa );
                    750: }
                    751:
                    752: char **catArgv(char **argv1,char **argv2)
                    753: {
                    754:   int i,n1,n2;
                    755:   char **argv;
                    756:   n1=0;
                    757:   while (argv1[n1] != NULL) n1++;
                    758:   n2=0;
                    759:   while (argv2[n2] != NULL) n2++;
                    760:   argv = (char **) mymalloc(sizeof(char *)*(n1+n2+1));
                    761:   if (argv == NULL) nomemory(0);
                    762:   for (i=0; i<n1; i++) argv[i] = argv1[i];
                    763:   for (i=0; i<n2; i++) argv[n1+i] = argv2[i];
                    764:   argv[n1+n2]=NULL;
                    765:   for (i=0; i<n1+n2; i++) {
1.5       takayama  766:     msg_get_home(5,argv[i]);
1.1       takayama  767:   }
                    768:   return argv;
                    769: }
                    770:
                    771: char *getLOAD_SM1_PATH2() {
                    772:   char *p;
                    773:   p = get_sm1_lib_path();
                    774:   if (p == NULL) {
                    775:     return("/usr/local/lib/sm1/");
1.3       takayama  776:   }else{
1.5       takayama  777:     return p;
1.3       takayama  778:   }
                    779: }
                    780:
                    781: char *getLOAD_K_PATH2(void) {
                    782:   char *p;
                    783:   p = get_k0_lib_path();
                    784:   if (p == NULL) {
                    785:     return("/usr/local/lib/kxx97/yacc/");
1.1       takayama  786:   }else{
1.5       takayama  787:     return p;
                    788:   }
                    789: }
                    790:
                    791: char *winPathToCygwinPath(char *s) {
                    792:   char *new;
                    793:   int n,i;
                    794:   if (s == NULL) return s;
                    795:   new = (char *) mymalloc(strlen(s)+20);
                    796:   if (new == NULL) nomemory(new);
                    797:   if (strlen(s) >= 3) {
                    798:     /* case of c:\xxx ==> /cygdrive/c/xxx */
                    799:     if ((s[1] == ':') && (s[2] == '\\')) {
                    800:       sprintf(new,"/cygdrive/%c/%s",s[0],&(s[3]));
                    801:     }else{
                    802:       strcpy(new,s);
                    803:     }
                    804:   }else{
                    805:     strcpy(new,s);
                    806:   }
                    807:   n = strlen(s);
                    808:   for (i=0; i<n; i++) {
                    809:     if (new[i] == '\\') new[i] = '/';
                    810:   }
                    811:   return new;
                    812: }
                    813:
                    814: char *generateTMPfileName(char *seed) {
                    815:   char *tmp;
                    816:   char *fname;
                    817:   char *tt;
                    818:   int num;
1.6       takayama  819:   static int prevnum=0;
                    820:    /* Bugs for k0.
                    821:       (1) unlink does not work so, load["t.k"];; load["t.k"];; fails (only cygwin.
                    822:       (2) In case of  error, TMP file is not removed. cf KCerror().
                    823:      In case of cygwin, we can only load 90 times.
                    824:    */
1.5       takayama  825:   int i;
                    826:   int clean = 0;
                    827:   tmp = getenv("TMP");
                    828:   if (tmp == NULL) {
                    829:     tmp = getenv("TEMP");
                    830:   }
                    831:   if ((tmp == NULL) && (strcmp(getOStypes(),"Windows-native") != 0)) {
                    832:     tmp = "/tmp";
                    833:   }
                    834:   tmp = winPathToCygwinPath(tmp);
                    835:   if (tmp != NULL) {
                    836:     fname = (char *)mymalloc(strlen(tmp)+strlen(seed)+40);
                    837:     if (fname == NULL) nomemory(fname);
                    838:   }else{
                    839:     fname = (char *)mymalloc(strlen(seed)+40);
                    840:     if (fname == NULL) nomemory(fname);
                    841:   }
1.6       takayama  842:   for (num=prevnum+1; num <100; num++) {
1.5       takayama  843:     if (tmp != NULL) {
                    844:       sprintf(fname,"%s/%s-tmp-%d.txt",tmp,seed,num);
                    845:     }else{
                    846:       sprintf(fname,"%s-tmp-%d.txt",seed,num);
                    847:     }
1.6       takayama  848:     if (getFileSize(fname) < 0) {
                    849:       prevnum = num;
                    850:       return fname;
                    851:     } else {
1.5       takayama  852:       if ((num > 90) && (!clean)) {
                    853:         /* Clean the old garbages. */
                    854:         for (i=0; i<100; i++) {
                    855:           if (tmp != NULL) {
                    856:             sprintf(fname,"%s/%s-tmp-%d.txt",tmp,seed,i);
                    857:           }else{
                    858:             sprintf(fname,"%s-tmp-%d.txt",seed,i);
                    859:           }
                    860:           {
                    861:             struct stat buf;
                    862:             int m;
                    863:             m = stat(fname,&buf);
                    864:             if ((m >= 0) && (buf.st_mtime+120 < time(NULL))) {
                    865:               unlink(fname);
                    866:             }
                    867:           }
                    868:         }
1.6       takayama  869:         num = 0; clean=1; prevnum=0;
1.5       takayama  870:       }
                    871:     }
                    872:   }
                    873:   return NULL;
                    874: }
                    875:
1.11      takayama  876: #define MAXTMP2  0xffffff
                    877: char *generateTMPfileName2(char *seed,char *ext,int usetmp,int win){
                    878:   char *tmp;
                    879:   char *fname;
                    880:   char *tt;
                    881:   int num;
                    882:   static int prevnum=0;
                    883:   int i;
                    884:   int clean = 0;
1.12      takayama  885:   char *extold;
                    886:   if (ext == NULL) ext="";
                    887:   else {
1.14      takayama  888:     extold = ext;
                    889:     ext = (char *) mymalloc(strlen(ext)+3);
                    890:     if (ext == NULL) {fprintf(stderr,"No more memory.\n"); return NULL;}
                    891:     strcpy(ext,".");
                    892:     strcat(ext,extold);
1.12      takayama  893:   }
1.11      takayama  894:   if (usetmp) {
1.14      takayama  895:     tmp = getenv("TMP");
                    896:     if (tmp == NULL) {
                    897:       tmp = getenv("TEMP");
                    898:     }
                    899:     if ((tmp == NULL) && (strcmp(getOStypes(),"Windows-native") != 0)) {
                    900:       tmp = "/tmp";
                    901:     }
                    902:     tmp = winPathToCygwinPath(tmp);
1.11      takayama  903:   }else{
1.14      takayama  904:     tmp = NULL;
1.11      takayama  905:   }
                    906:   if (tmp != NULL) {
                    907:     fname = (char *)mymalloc(strlen(tmp)+strlen(seed)+40);
                    908:     if (fname == NULL) nomemory(fname);
                    909:   }else{
                    910:     fname = (char *)mymalloc(strlen(seed)+40);
                    911:     if (fname == NULL) nomemory(fname);
                    912:   }
                    913:   for (num=prevnum+1; num <MAXTMP2; num++) {
                    914:     if (tmp != NULL) {
1.12      takayama  915:       sprintf(fname,"%s/%s-tmp-%d%s",tmp,seed,num,ext);
1.11      takayama  916:     }else{
1.12      takayama  917:       sprintf(fname,"%s-tmp-%d%s",seed,num,ext);
1.11      takayama  918:     }
                    919:     if (getFileSize(fname) < 0) {
                    920:       prevnum = num;
1.14      takayama  921:       if (win) fname= cygwinPathToWinPath(fname);
1.11      takayama  922:       return fname;
                    923:     } else {
                    924:       if ((num > MAXTMP2-10) && (!clean)) {
                    925:         /* Clean the old garbages. */
                    926:         for (i=0; i<MAXTMP2; i++) {
                    927:           if (tmp != NULL) {
1.12      takayama  928:             sprintf(fname,"%s/%s-tmp-%d%s",tmp,seed,i,ext);
1.11      takayama  929:           }else{
1.12      takayama  930:             sprintf(fname,"%s-tmp-%d%s",seed,i,ext);
1.11      takayama  931:           }
                    932:           {
                    933:             struct stat buf;
                    934:             int m;
                    935:             m = stat(fname,&buf);
                    936:             if ((m >= 0) && (buf.st_mtime+120 < time(NULL))) {
                    937:               unlink(fname);
                    938:             }
                    939:           }
                    940:         }
                    941:         num = 0; clean=1; prevnum=0;
                    942:       }
                    943:     }
                    944:   }
                    945:   return NULL;
                    946: }
1.5       takayama  947:
                    948: char *getCppPath(void) {
                    949:   static char *cpp = "/usr/local/bin/cpp";
                    950:   int ostype;
                    951:   char *oxhome;
                    952:   if ((cpp != NULL) && (getFileSize(cpp) >= 0)) return cpp;
                    953:   ostype = getOStypei();
                    954:   if (ostype < 3) {
                    955:     /* unix or cygwin env */
                    956:     cpp = "/lib/cpp";       /* on linux */
                    957:     if (getFileSize(cpp) >= 0) return cpp;
                    958:     cpp = "/usr/bin/cpp";   /* on freebsd, on cygwin */
                    959:     if (getFileSize(cpp) >= 0) return cpp;
                    960:     cpp = "/bin/cpp";
                    961:     if (getFileSize(cpp) >= 0) return cpp;
                    962:     cpp = NULL; return cpp;
                    963:   }else {
                    964:     /* On Windows */
                    965:     oxhome = getOpenXM_HOME();
                    966:     if (oxhome == NULL) {
                    967:       cpp = NULL; return cpp;
                    968:     }
                    969:     cpp = (char *) mymalloc(strlen(oxhome)+strlen("/asir/bin/cpp.exe")+5);
                    970:     if (cpp == NULL) nomemory(cpp);
                    971:     sprintf(cpp,"%s/asir/bin/cpp.exe",oxhome);
                    972:     if (getFileSize(cpp) >= 0) return cpp;
                    973:     else return NULL;
1.1       takayama  974:   }
                    975: }
1.8       takayama  976:
                    977: char *getCommandPath(char *cmdname)
                    978: {
                    979:   char *path;
                    980:   char *msg;
                    981:   char *path2;
1.9       takayama  982:   char *ss;
1.8       takayama  983:   int i,j;
1.9       takayama  984:   /* Use /cygdrive format always. */
1.8       takayama  985:   if (cmdname == NULL) return NULL;
                    986:   if (strlen(cmdname) < 1) {
1.14      takayama  987:     errorPathFinder("getCommandPath: cmdname is an empty string.\n");
                    988:     return NULL;
1.8       takayama  989:   }
                    990:   if (cmdname[0] == '/') {
1.14      takayama  991:     if (getFileSize(cmdname) >= 0) { /* Todo: isExecutableFile() */
                    992:     }else{
                    993:       msg = (char *)mymalloc(strlen(cmdname)+30);
                    994:       sprintf(msg,"getCommandPath: %s is not found.");
                    995:       errorPathFinder(msg);
                    996:       return NULL;
                    997:     }
                    998:     return cmdname;
1.8       takayama  999:   }
                   1000:
1.9       takayama 1001:   path = getOpenXM_HOME();  /* It will return /cygdrive for windows. */
1.8       takayama 1002:   if (path != NULL) {
1.14      takayama 1003:     path2 = (char *)mymalloc(strlen(path)+5);
                   1004:     strcpy(path2,path);
                   1005:     strcat(path2,"bin");
                   1006:     ss = oxWhich(cmdname,path2);
1.9       takayama 1007:     if (ss != NULL) return ss;
                   1008:   }
                   1009:
                   1010:   path = (char *)getenv("PATH");  /* Todo: it will not give /cygdrive format*/
                   1011:   ss = oxWhich(cmdname,path);
                   1012:   if (ss == NULL) {
1.14      takayama 1013:     errorPathFinder("oxWhich_unix: could not find it in the path string.\n");
1.8       takayama 1014:   }
1.9       takayama 1015:   return ss;
                   1016: }
1.8       takayama 1017:
1.9       takayama 1018: char *oxWhich(char *cmdname,char *path) {
                   1019:   return(oxWhich_unix(cmdname,path));
                   1020: }
                   1021:
                   1022: char *oxWhich_unix(char *cmdname,char *path) {
                   1023:   char *path2;
                   1024:   int i,j;
1.8       takayama 1025:   if (path == NULL) {
1.14      takayama 1026:     return NULL;
1.8       takayama 1027:   }
                   1028:
1.9       takayama 1029:   path2 = (char *)mymalloc(strlen(path)+strlen(cmdname)+2);
                   1030:   for (i=0, j=0; i <= strlen(path); i++) {
1.14      takayama 1031:     path2[j] = 0;
                   1032:     if ((path[i] == ':') || (path[i] == 0)) {
                   1033:       strcat(path2,"/"); strcat(path2,cmdname);
                   1034:       if (getFileSize(path2) >= 0) { /* Todo: isExecutableFile() */
                   1035:         return path2;
                   1036:       }
                   1037:       j = 0; path2[j] = 0;
                   1038:     }else{
                   1039:       path2[j] = path[i]; j++; path2[j] = 0;
                   1040:     }
1.8       takayama 1041:   }
                   1042:   return NULL;
1.10      takayama 1043: }
                   1044:
                   1045: char *oxEvalEnvVar(char *s) {
                   1046:   int n, i,j;
                   1047:   char *es;
                   1048:   char *news;
                   1049:   int flag,flag2;
                   1050:   flag=-1;
                   1051:   n = strlen(s);
                   1052:   es = (char *)mymalloc(n+1); es[0] = 0;
                   1053:   if (es == NULL) nomemory(1);
                   1054:   for (i=0; i<n; i++) {
                   1055:     if ((s[i] == '$') && (s[i+1] == '{')) {
                   1056:       for (j=0; ; j++) {
                   1057:         if ((s[i+2+j] == 0) || (s[i+2+j] == '}')) {
                   1058:           flag2 = i+2+j+1;
                   1059:           break;
                   1060:         }
                   1061:         es[j] = s[i+2+j]; es[j+1]=0;
                   1062:       }
                   1063:       if (es[0] != 0) { flag=i; break; }
                   1064:     }
                   1065:   }
                   1066:   if (flag >= 0) {
                   1067:     es = (char *)getenv(es);
                   1068:     if (es == NULL) es="";
                   1069:     news = (char *) mymalloc(n+5+strlen(es));
                   1070:     if (news == NULL) nomemory(1);
                   1071:     j = 0;
                   1072:     for (i=0; i<flag; i++) {
                   1073:       news[j] = s[i]; j++;
                   1074:     }
                   1075:     for (i=0; i<strlen(es); i++) {
                   1076:       news[j] = es[i]; j++;
                   1077:     }
                   1078:     for (i=flag2; i<strlen(s); i++) {
                   1079:       news[j] = s[i]; j++;
                   1080:     }
                   1081:     news[j] = 0;
                   1082:     return(oxEvalEnvVar(news));
                   1083:   }else{
                   1084:     return(s);
                   1085:   }
1.8       takayama 1086: }
                   1087:
1.14      takayama 1088: void oxResetRedirect(void) {
1.15    ! takayama 1089:   if (OX_P_stdin >= 0) close(OX_P_stdin);
        !          1090:   if (OX_P_stdout >= 0) close(OX_P_stdout);
        !          1091:   if (OX_P_stderr >= 0) close(OX_P_stderr);
1.14      takayama 1092:   OX_P_stdin = OX_P_stdout = OX_P_stderr = -1;
                   1093: }
                   1094:
                   1095: int oxDeleteFile(char *fname) {
                   1096:   if (getFileSize(fname) >= 0) {
                   1097:     return(unlink(fname));
                   1098:   }else{
                   1099:     return(-1);
                   1100:   }
                   1101: }

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