Annotation of OpenXM/src/util/ox_pathfinder.c, Revision 1.34
1.34 ! takayama 1: /* $OpenXM: OpenXM/src/util/ox_pathfinder.c,v 1.33 2015/08/06 22:33:21 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.34 ! takayama 597: #if defined(__CYGWIN64__) || (defined(__CYGWIN__) && defined(__x86_64__))
1.32 takayama 598: strcpy(ans,"C:\\cygwin64");
599: #else
1.1 takayama 600: strcpy(ans,"C:\\cygwin");
1.32 takayama 601: #endif
1.1 takayama 602: strcat(ans,s);
603: }
604:
605:
606: for (i=0; i <strlen(ans); i++) {
607: if (ans[i] == '/') ans[i] = '\\';
608: }
609: return ans;
610: }
611:
612: char **getServerEnv(char *oxServer) {
613: int ostype;
614: char *p;
615: char *oxhome;
1.20 takayama 616: char *oxterm;
617: int oxtermType=0;
618: char *oxtermOpt;
1.1 takayama 619: char *oxlog;
620: char *load_sm1_path;
621: char *load_k0_path;
622: char *openXM_HOME;
623: #define ARGV_SIZE 100
624: char *argv[ARGV_SIZE];
625: int i,k;
626: char **aaa;
627:
628: if (Verbose_get_home) {
1.5 takayama 629: if (oxServer == NULL) {
630: fprintf(stderr,"Server name is NULL.\n");
631: }else{
632: fprintf(stderr,"Server name is %s\n",oxServer);
633: }
1.1 takayama 634: }
635:
636: if (oxServer == NULL) return NULL;
637: i = 0;
638: argv[i] = NULL;
639:
640: ostype = getOStypei();
641:
642: oxhome = getOpenXM_HOME();
643: if (oxhome == NULL) return NULL;
644: p = (char *) mymalloc(strlen(oxhome)+strlen(oxServer)+100);
645: if (p == NULL) {fprintf(stderr,"No more memory.\n"); exit(100);}
646:
647: strcpy(p,oxhome);
648: strcat(p,oxServer);
649: if (getFileSize(p) == -1) {
650: msg_get_home(2,oxhome);
651: msg_get_home(2," is found, but ");
652: msg_get_home(2,p);
653: msg_get_home(2," is not found.");
654: return (NULL);
655: }
656: oxServer = (char *) mymalloc(strlen(oxhome)+strlen(oxServer)+100);
657: if (oxServer == NULL) {fprintf(stderr,"No more memory.\n"); exit(100);}
658: strcpy(oxServer,p);
659:
660: if ((ostype == 0) || (ostype == 2)) {
1.20 takayama 661: oxlog = get_oxlog_path();
662: if (!NoX) {
663: oxterm = oxTermWhich_unix(&oxtermType);
664: if (oxterm == NULL) {
665: msg_get_home(2,"oxterm, rxvt, xterm is not found. NoX is automatically set.");
1.5 takayama 666: NoX = 1;
1.20 takayama 667: }
668: if (oxtermType == T_XTERM) oxtermOpt = "-icon";
669: else oxtermOpt = "-iconic";
670: }
1.1 takayama 671: argv[i] = oxlog; i++; argv[i] = NULL;
1.5 takayama 672: if (!NoX) {
1.20 takayama 673: argv[i] = oxterm ; i++; argv[i] = NULL;
1.25 takayama 674: if (((char *)getenv("OX_XTERM_GEOMETRY")) != NULL) {
675: /* ex. OX_XTERM_GEOMETRY=80x20+0+0 */
676: argv[i] = "-geometry"; i++; argv[i] = NULL;
677: argv[i] = (char *) getenv("OX_XTERM_GEOMETRY"); i++; argv[i] = NULL;
678: }else{
679: argv[i] = oxtermOpt; i++; argv[i] = NULL;
680: }
1.26 takayama 681: /* dirty hack for buggy international xterm cf. OpenXM FAQ */
682: argv[i] = "-xrm"; i++; argv[i] = NULL;
683: argv[i] = "XTerm*locale:false"; i++; argv[i] = NULL;
684:
1.22 takayama 685: if (((char *)getenv("OX_XTERM_SCROLL")) != NULL) {
1.24 takayama 686: argv[i] = "-sb"; i++; argv[i] = NULL;
1.22 takayama 687: argv[i] = "-sl"; i++; argv[i] = NULL;
688: argv[i] = (char *) getenv("OX_XTERM_SCROLL"); i++; argv[i] = NULL;
689: }
1.23 takayama 690: argv[i] = "-e"; i++; argv[i] = NULL;
1.5 takayama 691: }
1.1 takayama 692: argv[i] = get_ox_path(); i++; argv[i] = NULL;
693: argv[i] = "-ox"; i++; argv[i] = NULL;
694: argv[i] = oxServer; i++; argv[i] = NULL;
695: }else{
1.5 takayama 696: if (!NoX) {
697: if (getFileSize("/cygdrive/c/winnt/system32/cmd.exe") >= 0) {
1.20 takayama 698: oxterm = "/cygdrive/c/winnt/system32/cmd.exe /c start /min ";
1.5 takayama 699: argv[i] = "/cygdrive/c/winnt/system32/cmd.exe"; i++; argv[i] = NULL;
700: }else if (getFileSize("/cygdrive/c/windows/system32/cmd.exe") >= 0) {
1.20 takayama 701: oxterm = "/cygdrive/c/windows/system32/cmd.exe /c start /min ";
1.5 takayama 702: argv[i] = "/cygdrive/c/windows/system32/cmd.exe"; i++; argv[i] = NULL;
703: }else{
704: msg_get_home(2,"cmd.exe is not found. NoX is automatically set.");
1.1 takayama 705: NoX = 1;
1.5 takayama 706: }
707: }
1.1 takayama 708: oxlog = " ";
1.5 takayama 709: if (!NoX) {
710: argv[i] = "/c"; i++; argv[i] = NULL;
711: argv[i] = "start"; i++; argv[i] = NULL;
712: argv[i] = "/min"; i++; argv[i] = NULL;
713: }
1.1 takayama 714: argv[i] = cygwinPathToWinPath(get_ox_path()); i++; argv[i] = NULL;
715: argv[i] = "-ox"; i++; argv[i] = NULL;
716: argv[i] = oxServer; i++; argv[i] = NULL;
717: }
718:
719: aaa = (char **) mymalloc(sizeof(char*)*(i+1));
720: if (aaa == NULL) nomemory(0);
721: msg_get_home(2,"--------- Result --------------");
722: for (k=0; k<i; k++) {
723: aaa[k] = argv[k];
724: msg_get_home(2,aaa[k]);
725: aaa[k+1] = NULL;
726: }
727: return aaa;
728: }
729:
730: char **setOXenv_old() {
731: char *openXM_HOME;
732: char *load_sm1_path;
733: char *load_k0_path;
734: char *asir_config;
735: char *asir_libdir;
736: char *asirloadpath;
737: char *asir_rsh;
738: char *ld_library_path;
739: char **aaa;
740: int n,i,k;
741:
742: /* set environmental variables */
743: n = 20; /* n must be larger than the number of env vars */
744: n++;
745: load_sm1_path = (char *) getenv("LOAD_SM1_PATH");
746: load_k0_path = (char *) getenv("LOAD_SM1_PATH");
747: asir_config = (char *) getenv("ASIR_CONFIG");
748: asir_libdir = (char *) getenv("ASIR_LIBDIR");
749: asirloadpath = (char *) getenv("ASIRLOADPATH");
750: asir_rsh = (char *) getenv("ASIR_RSH");
751: ld_library_path = (char *) getenv("LD_LIBRARY_PATH");
752:
753: openXM_HOME = getOpenXM_HOME();
754: if (openXM_HOME != NULL)
755: openXM_HOME[strlen(openXM_HOME)-1] = '\0';
756: /* How about ASIR... ? */
757:
758: msg_get_home(3,"OpenXM_HOME is"); msg_get_home(2,openXM_HOME);
759: msg_get_home(3,"LOAD_SM1_PATH is"); msg_get_home(2,load_sm1_path);
760: msg_get_home(3,"LOAD_K0_PATH is"); msg_get_home(2,load_k0_path);
761:
762: aaa = (char **) mymalloc(sizeof(char*)*n);
763: if (aaa == NULL) nomemory(0);
764:
765: i = 0;
766: if (openXM_HOME != NULL) {
767: aaa[i] = openXM_HOME; i++; aaa[i] = NULL; if (i > n-2) return aaa;
768: }
769: if (load_sm1_path != NULL) {
770: aaa[i] = load_sm1_path; i++; aaa[i] = NULL; if (i > n-2) return aaa;
771: }
772: if (load_k0_path != NULL) {
773: aaa[i] = load_k0_path; i++; aaa[i] = NULL; if (i > n-2) return aaa;
774: }
775: if (asir_config != NULL) {
776: aaa[i] = asir_config; i++; aaa[i] = NULL; if (i > n-2) return aaa;
777: }
778: if (asir_libdir != NULL) {
779: aaa[i] = asir_libdir; i++; aaa[i] = NULL; if (i > n-2) return aaa;
780: }
781: if (asirloadpath != NULL) {
782: aaa[i] = asirloadpath; i++; aaa[i] = NULL; if (i > n-2) return aaa;
783: }
784: if (asir_rsh != NULL) {
785: aaa[i] = asir_rsh; i++; aaa[i] = NULL; if (i > n-2) return aaa;
786: }
787:
788: msg_get_home(3,"--------- Result --------------");
789: for (k=0; k<n; k++) {
790: if (aaa[k] == NULL) break;
791: msg_get_home(3,aaa[k]);
792: }
793:
794: return aaa;
795: }
796:
797: char **debugServerEnv(char *oxServer)
798: {
799: int t;
800: char **aaa;
801: t = Verbose_get_home;
802: Verbose_get_home = 1;
803: aaa = getServerEnv(oxServer);
804: Verbose_get_home = t;
805: return ( aaa );
806: }
807:
808: char **catArgv(char **argv1,char **argv2)
809: {
810: int i,n1,n2;
811: char **argv;
812: n1=0;
813: while (argv1[n1] != NULL) n1++;
814: n2=0;
815: while (argv2[n2] != NULL) n2++;
816: argv = (char **) mymalloc(sizeof(char *)*(n1+n2+1));
817: if (argv == NULL) nomemory(0);
818: for (i=0; i<n1; i++) argv[i] = argv1[i];
819: for (i=0; i<n2; i++) argv[n1+i] = argv2[i];
820: argv[n1+n2]=NULL;
821: for (i=0; i<n1+n2; i++) {
1.5 takayama 822: msg_get_home(5,argv[i]);
1.1 takayama 823: }
824: return argv;
825: }
826:
827: char *getLOAD_SM1_PATH2() {
828: char *p;
829: p = get_sm1_lib_path();
830: if (p == NULL) {
831: return("/usr/local/lib/sm1/");
1.3 takayama 832: }else{
1.5 takayama 833: return p;
1.3 takayama 834: }
835: }
836:
837: char *getLOAD_K_PATH2(void) {
838: char *p;
839: p = get_k0_lib_path();
840: if (p == NULL) {
841: return("/usr/local/lib/kxx97/yacc/");
1.1 takayama 842: }else{
1.5 takayama 843: return p;
844: }
845: }
846:
847: char *winPathToCygwinPath(char *s) {
848: char *new;
849: int n,i;
850: if (s == NULL) return s;
851: new = (char *) mymalloc(strlen(s)+20);
852: if (new == NULL) nomemory(new);
853: if (strlen(s) >= 3) {
854: /* case of c:\xxx ==> /cygdrive/c/xxx */
855: if ((s[1] == ':') && (s[2] == '\\')) {
856: sprintf(new,"/cygdrive/%c/%s",s[0],&(s[3]));
857: }else{
858: strcpy(new,s);
859: }
860: }else{
861: strcpy(new,s);
862: }
863: n = strlen(s);
864: for (i=0; i<n; i++) {
865: if (new[i] == '\\') new[i] = '/';
866: }
867: return new;
868: }
869:
870: char *generateTMPfileName(char *seed) {
871: char *tmp;
872: char *fname;
873: char *tt;
874: int num;
1.6 takayama 875: static int prevnum=0;
876: /* Bugs for k0.
877: (1) unlink does not work so, load["t.k"];; load["t.k"];; fails (only cygwin.
878: (2) In case of error, TMP file is not removed. cf KCerror().
879: In case of cygwin, we can only load 90 times.
880: */
1.5 takayama 881: int i;
882: int clean = 0;
883: tmp = getenv("TMP");
884: if (tmp == NULL) {
885: tmp = getenv("TEMP");
886: }
887: if ((tmp == NULL) && (strcmp(getOStypes(),"Windows-native") != 0)) {
888: tmp = "/tmp";
889: }
890: tmp = winPathToCygwinPath(tmp);
891: if (tmp != NULL) {
892: fname = (char *)mymalloc(strlen(tmp)+strlen(seed)+40);
893: if (fname == NULL) nomemory(fname);
894: }else{
895: fname = (char *)mymalloc(strlen(seed)+40);
896: if (fname == NULL) nomemory(fname);
897: }
1.6 takayama 898: for (num=prevnum+1; num <100; num++) {
1.5 takayama 899: if (tmp != NULL) {
900: sprintf(fname,"%s/%s-tmp-%d.txt",tmp,seed,num);
901: }else{
902: sprintf(fname,"%s-tmp-%d.txt",seed,num);
903: }
1.6 takayama 904: if (getFileSize(fname) < 0) {
905: prevnum = num;
906: return fname;
907: } else {
1.5 takayama 908: if ((num > 90) && (!clean)) {
909: /* Clean the old garbages. */
910: for (i=0; i<100; i++) {
911: if (tmp != NULL) {
912: sprintf(fname,"%s/%s-tmp-%d.txt",tmp,seed,i);
913: }else{
914: sprintf(fname,"%s-tmp-%d.txt",seed,i);
915: }
916: {
917: struct stat buf;
918: int m;
919: m = stat(fname,&buf);
920: if ((m >= 0) && (buf.st_mtime+120 < time(NULL))) {
921: unlink(fname);
922: }
923: }
924: }
1.6 takayama 925: num = 0; clean=1; prevnum=0;
1.5 takayama 926: }
927: }
928: }
929: return NULL;
930: }
931:
1.11 takayama 932: #define MAXTMP2 0xffffff
933: char *generateTMPfileName2(char *seed,char *ext,int usetmp,int win){
934: char *tmp;
935: char *fname;
936: char *tt;
937: int num;
938: static int prevnum=0;
939: int i;
940: int clean = 0;
1.12 takayama 941: char *extold;
942: if (ext == NULL) ext="";
943: else {
1.14 takayama 944: extold = ext;
945: ext = (char *) mymalloc(strlen(ext)+3);
946: if (ext == NULL) {fprintf(stderr,"No more memory.\n"); return NULL;}
947: strcpy(ext,".");
948: strcat(ext,extold);
1.12 takayama 949: }
1.11 takayama 950: if (usetmp) {
1.14 takayama 951: tmp = getenv("TMP");
952: if (tmp == NULL) {
953: tmp = getenv("TEMP");
954: }
955: if ((tmp == NULL) && (strcmp(getOStypes(),"Windows-native") != 0)) {
956: tmp = "/tmp";
957: }
958: tmp = winPathToCygwinPath(tmp);
1.11 takayama 959: }else{
1.14 takayama 960: tmp = NULL;
1.11 takayama 961: }
962: if (tmp != NULL) {
963: fname = (char *)mymalloc(strlen(tmp)+strlen(seed)+40);
964: if (fname == NULL) nomemory(fname);
965: }else{
966: fname = (char *)mymalloc(strlen(seed)+40);
967: if (fname == NULL) nomemory(fname);
968: }
969: for (num=prevnum+1; num <MAXTMP2; num++) {
970: if (tmp != NULL) {
1.12 takayama 971: sprintf(fname,"%s/%s-tmp-%d%s",tmp,seed,num,ext);
1.11 takayama 972: }else{
1.12 takayama 973: sprintf(fname,"%s-tmp-%d%s",seed,num,ext);
1.11 takayama 974: }
975: if (getFileSize(fname) < 0) {
976: prevnum = num;
1.14 takayama 977: if (win) fname= cygwinPathToWinPath(fname);
1.11 takayama 978: return fname;
979: } else {
980: if ((num > MAXTMP2-10) && (!clean)) {
981: /* Clean the old garbages. */
982: for (i=0; i<MAXTMP2; i++) {
983: if (tmp != NULL) {
1.12 takayama 984: sprintf(fname,"%s/%s-tmp-%d%s",tmp,seed,i,ext);
1.11 takayama 985: }else{
1.12 takayama 986: sprintf(fname,"%s-tmp-%d%s",seed,i,ext);
1.11 takayama 987: }
988: {
989: struct stat buf;
990: int m;
991: m = stat(fname,&buf);
992: if ((m >= 0) && (buf.st_mtime+120 < time(NULL))) {
993: unlink(fname);
994: }
995: }
996: }
997: num = 0; clean=1; prevnum=0;
998: }
999: }
1000: }
1001: return NULL;
1002: }
1.5 takayama 1003:
1004: char *getCppPath(void) {
1005: static char *cpp = "/usr/local/bin/cpp";
1006: int ostype;
1007: char *oxhome;
1008: if ((cpp != NULL) && (getFileSize(cpp) >= 0)) return cpp;
1009: ostype = getOStypei();
1010: if (ostype < 3) {
1011: /* unix or cygwin env */
1012: cpp = "/lib/cpp"; /* on linux */
1013: if (getFileSize(cpp) >= 0) return cpp;
1014: cpp = "/usr/bin/cpp"; /* on freebsd, on cygwin */
1015: if (getFileSize(cpp) >= 0) return cpp;
1016: cpp = "/bin/cpp";
1017: if (getFileSize(cpp) >= 0) return cpp;
1018: cpp = NULL; return cpp;
1019: }else {
1020: /* On Windows */
1021: oxhome = getOpenXM_HOME();
1022: if (oxhome == NULL) {
1023: cpp = NULL; return cpp;
1024: }
1025: cpp = (char *) mymalloc(strlen(oxhome)+strlen("/asir/bin/cpp.exe")+5);
1026: if (cpp == NULL) nomemory(cpp);
1027: sprintf(cpp,"%s/asir/bin/cpp.exe",oxhome);
1028: if (getFileSize(cpp) >= 0) return cpp;
1029: else return NULL;
1.1 takayama 1030: }
1031: }
1.8 takayama 1032:
1033: char *getCommandPath(char *cmdname)
1034: {
1035: char *path;
1036: char *msg;
1037: char *path2;
1.9 takayama 1038: char *ss;
1.8 takayama 1039: int i,j;
1.9 takayama 1040: /* Use /cygdrive format always. */
1.8 takayama 1041: if (cmdname == NULL) return NULL;
1042: if (strlen(cmdname) < 1) {
1.14 takayama 1043: errorPathFinder("getCommandPath: cmdname is an empty string.\n");
1044: return NULL;
1.8 takayama 1045: }
1046: if (cmdname[0] == '/') {
1.14 takayama 1047: if (getFileSize(cmdname) >= 0) { /* Todo: isExecutableFile() */
1048: }else{
1049: msg = (char *)mymalloc(strlen(cmdname)+30);
1.29 takayama 1050: sprintf(msg,"getCommandPath: %s is not found.",cmdname);
1.14 takayama 1051: errorPathFinder(msg);
1052: return NULL;
1053: }
1054: return cmdname;
1.8 takayama 1055: }
1056:
1.9 takayama 1057: path = getOpenXM_HOME(); /* It will return /cygdrive for windows. */
1.8 takayama 1058: if (path != NULL) {
1.14 takayama 1059: path2 = (char *)mymalloc(strlen(path)+5);
1060: strcpy(path2,path);
1061: strcat(path2,"bin");
1062: ss = oxWhich(cmdname,path2);
1.9 takayama 1063: if (ss != NULL) return ss;
1064: }
1065:
1066: path = (char *)getenv("PATH"); /* Todo: it will not give /cygdrive format*/
1067: ss = oxWhich(cmdname,path);
1068: if (ss == NULL) {
1.14 takayama 1069: errorPathFinder("oxWhich_unix: could not find it in the path string.\n");
1.8 takayama 1070: }
1.9 takayama 1071: return ss;
1072: }
1.8 takayama 1073:
1.9 takayama 1074: char *oxWhich(char *cmdname,char *path) {
1075: return(oxWhich_unix(cmdname,path));
1076: }
1077:
1078: char *oxWhich_unix(char *cmdname,char *path) {
1079: char *path2;
1080: int i,j;
1.8 takayama 1081: if (path == NULL) {
1.14 takayama 1082: return NULL;
1.8 takayama 1083: }
1084:
1.9 takayama 1085: path2 = (char *)mymalloc(strlen(path)+strlen(cmdname)+2);
1086: for (i=0, j=0; i <= strlen(path); i++) {
1.14 takayama 1087: path2[j] = 0;
1088: if ((path[i] == ':') || (path[i] == 0)) {
1089: strcat(path2,"/"); strcat(path2,cmdname);
1090: if (getFileSize(path2) >= 0) { /* Todo: isExecutableFile() */
1091: return path2;
1092: }
1093: j = 0; path2[j] = 0;
1094: }else{
1095: path2[j] = path[i]; j++; path2[j] = 0;
1096: }
1.8 takayama 1097: }
1098: return NULL;
1.10 takayama 1099: }
1100:
1101: char *oxEvalEnvVar(char *s) {
1102: int n, i,j;
1103: char *es;
1104: char *news;
1105: int flag,flag2;
1106: flag=-1;
1107: n = strlen(s);
1108: es = (char *)mymalloc(n+1); es[0] = 0;
1109: if (es == NULL) nomemory(1);
1110: for (i=0; i<n; i++) {
1111: if ((s[i] == '$') && (s[i+1] == '{')) {
1112: for (j=0; ; j++) {
1113: if ((s[i+2+j] == 0) || (s[i+2+j] == '}')) {
1114: flag2 = i+2+j+1;
1115: break;
1116: }
1117: es[j] = s[i+2+j]; es[j+1]=0;
1118: }
1119: if (es[0] != 0) { flag=i; break; }
1120: }
1121: }
1122: if (flag >= 0) {
1123: es = (char *)getenv(es);
1124: if (es == NULL) es="";
1125: news = (char *) mymalloc(n+5+strlen(es));
1126: if (news == NULL) nomemory(1);
1127: j = 0;
1128: for (i=0; i<flag; i++) {
1129: news[j] = s[i]; j++;
1130: }
1131: for (i=0; i<strlen(es); i++) {
1132: news[j] = es[i]; j++;
1133: }
1134: for (i=flag2; i<strlen(s); i++) {
1135: news[j] = s[i]; j++;
1136: }
1137: news[j] = 0;
1138: return(oxEvalEnvVar(news));
1139: }else{
1140: return(s);
1141: }
1.8 takayama 1142: }
1143:
1.14 takayama 1144: void oxResetRedirect(void) {
1.15 takayama 1145: if (OX_P_stdin >= 0) close(OX_P_stdin);
1146: if (OX_P_stdout >= 0) close(OX_P_stdout);
1147: if (OX_P_stderr >= 0) close(OX_P_stderr);
1.14 takayama 1148: OX_P_stdin = OX_P_stdout = OX_P_stderr = -1;
1149: }
1150:
1151: int oxDeleteFile(char *fname) {
1152: if (getFileSize(fname) >= 0) {
1153: return(unlink(fname));
1154: }else{
1155: return(-1);
1156: }
1.16 takayama 1157: }
1158:
1159: /* This function just kills processes, so if there is a process which
1160: uses ox protocol, it is not relevant to use this functions.
1161: */
1.33 takayama 1162: #if (!defined(__MINGW32__) && !defined(__MINGW64__))
1.16 takayama 1163: int oxKillAll(void) {
1164: int i;
1165: int pid;
1166: int status;
1167: for (i=0; i<Myforkcp; i++) {
1168: pid = Myforkchildren[i];
1.19 takayama 1169: if (Verbose) fprintf(stderr,"Sending signal to %d ... ",pid);
1.17 takayama 1170: kill(pid,SIGKILL);
1171: waitpid(pid,&status,0);
1.19 takayama 1172: if (Verbose) fprintf(stderr,"Gone.\n");
1.16 takayama 1173: }
1174: Myforkcp = 0;
1175: return(0);
1.18 takayama 1176: }
1.33 takayama 1177: #else
1178: int oxKillAll(void) {
1179: fprintf(stderr,"ERROR: oxKillAll is not implented in mingw\n");
1180: return(-1);
1181: }
1182: #endif
1.18 takayama 1183: void ox_pathfinder_quiet(void) {
1184: Verbose_get_home = 0;
1.19 takayama 1185: Verbose = 0;
1.20 takayama 1186: }
1187:
1188: char *oxTermWhich_unix(int *typep) {
1189: char *s;
1190: char *p;
1191: p = (char *) getenv("PATH");
1192: s = oxWhich("oxterm",p); *typep = T_OXTERM;
1193: if (s != NULL) return s;
1194:
1.21 takayama 1195: /* skip the search of rxvt (temporary)
1.20 takayama 1196: s = oxWhich("rxvt",p); *typep = T_RXVT;
1197: if (s != NULL) return s;
1.21 takayama 1198: */
1.20 takayama 1199:
1200: s = oxWhich("xterm",p); *typep = T_XTERM;
1201: if (s != NULL) return s;
1202:
1203: return NULL;
1.27 takayama 1204: }
1205:
1206: int oxpSendStringAsFile(char *user,char *hostname, char *filename, char *str)
1207: {
1208: FILE *fp;
1209: int i;
1210: char *comm;
1211: char *argv[10];
1212: mode_t oumask;
1213: oumask = umask((mode_t) (64-1));
1214: /* 077=111 111 */
1215: fp = fopen(filename,"w");
1216: umask(oumask);
1217: if (fp == NULL) {
1218: return -1;
1219: }
1220: for (i=0; i <strlen(str); i++) {
1221: fputc(str[i],fp);
1222: }
1223: fclose(fp);
1224: if (strcmp(hostname,"localhost") == 0) return 0;
1225: comm = (char *)malloc(strlen(user)+strlen(hostname)+strlen(filename)*2+1024);
1226: if (comm == NULL) return -2;
1227:
1228: argv[0] = getCommandPath("scp");
1229: if (argv[0] == NULL) return -3;
1230: argv[1] = filename;
1231: comm = (char *)malloc(strlen(user)+strlen(hostname)+strlen(filename)+256);
1232: sprintf(comm,"%s@%s:%s",user,hostname,filename);
1233: argv[2] = comm;
1234: argv[3] = NULL;
1235: return oxForkExec(argv);
1236: }
1237:
1238: char *oxpReadOneTimePasswordFromFile(char *filename) {
1.14 takayama 1239: }
1.30 takayama 1240:
1241: int ox_pathfinderEngineLogToStdout(int state) {
1242: EngineLogToStdout = state;
1243: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>