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