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

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

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