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