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

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

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