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

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

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