[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.14

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

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