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

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

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