Annotation of OpenXM/src/util/ox_pathfinder.c, Revision 1.2
1.2 ! takayama 1: /* $OpenXM: OpenXM/src/util/ox_pathfinder.c,v 1.1 2003/07/21 11:36:10 takayama Exp $ */
1.1 takayama 2: /* Moved from misc-2003/07/cygwin/test.c */
3: #include <stdio.h>
4: #include <sys/types.h>
5: #include <sys/stat.h>
6: #include <fcntl.h>
7: #include <stdlib.h>
8: #include <unistd.h>
9: #include <sys/wait.h>
10: #include <signal.h>
11: #include <ctype.h>
12: #include "ox_pathfinder.h"
13:
14: extern char **environ;
15:
16: static int getOStypei();
17: static char *addSlash();
18: static char *get_sm1_lib_path();
19: static char *get_k0_lib_path();
20: static char *get_ox_asir_path();
21: static char *get_ox_sm1_path();
22: static char *get_ox_path();
23: static char *get_oxc_path();
24: static char *get_oxlog_path();
1.2 ! takayama 25:
1.1 takayama 26:
27:
28: static int Verbose_get_home = 1;
29: static int NoX = 0;
30:
1.2 ! takayama 31:
! 32: #define nomemory(a) {fprintf(stderr,"(%d) no more memory.\n",a);exit(10);}
! 33: #define mymalloc(a) malloc(a)
! 34:
! 35: int ox_pathfinderNoX(int f) {
! 36: if (f < 0) return NoX;
! 37: NoX = f;
! 38: return f;
! 39: }
! 40:
! 41: /* See kxx/ox100start.c for main */
! 42:
1.1 takayama 43: #define MYFORKCP_SIZE 100
44: static int Myforkchildren[MYFORKCP_SIZE];
45: static int Myforkcp=0;
46: static void myforkwait() {
47: int status;
48: int pid;
49: int i,j;
50: signal(SIGCHLD,SIG_IGN);
51: pid = wait(&status);
52: fprintf(stderr,"Child process %d is exiting.\n",pid);
53: for (i=0; i<Myforkcp; i++) {
54: if (Myforkchildren[i] == pid) {
55: for (j=i; j<Myforkcp-1; j++) {
56: Myforkchildren[j] = Myforkchildren[j+1];
57: }
58: if (Myforkcp > 0) Myforkcp--;
59: }
60: }
61: signal(SIGCHLD,myforkwait);
62: }
63:
1.2 ! takayama 64: int oxForkExec(char **argv) {
1.1 takayama 65: int pid;
66: char **eee;
67: int m;
68: m = 0;
69: if (argv == NULL) {
70: fprintf(stderr,"Cannot fork and exec.\n"); return -1;
71: }
72: if ((pid = fork()) > 0) {
73: if (m&2) {
74: /* Do not call singal to turn around a trouble on cygwin. BUG. */
75: }else{
76: signal(SIGCHLD,myforkwait); /* to kill Zombie */
77: }
78: Myforkchildren[Myforkcp++] = pid;
79: if (Myforkcp >= MYFORKCP_SIZE-1) {
80: fprintf(stderr,"Child process table is full.\n");
81: Myforkcp = 0;
82: }
83: }else{
84: /* close the specified files */
85: if (m&1) {
86: sigset_t sss;
87: sigemptyset(&sss);
88: sigaddset(&sss,SIGINT);
89: sigprocmask(SIG_BLOCK,&sss,NULL);
90: }
91: if (NoX) {
92: FILE *null;
93: null = fopen("/dev/null","wb");
94: dup2(fileno(null),1);
95: dup2(fileno(null),2);
96: }
97: execve(argv[0],argv,environ);
98: /* This place will never be reached unless execv fails. */
1.2 ! takayama 99: fprintf(stderr,"oxForkExec fails: ");
1.1 takayama 100: }
101: }
102:
103: static int getOStypei() {
104: /*
105: 0 unix
106: 1 windows-cygwin
107: 2 windows-cygwin-on-X
108: 3 windows-native
109: */
110: int ostype;
111: char *s,*s2,*s3;
112: #if defined(__CYGWIN__)
113: ostype = 1;
114: #else
115: ostype = 0;
116: #endif
117: if (ostype == 0) return ostype;
118: /* Heuristic method */
119: s = (char *)getenv("WINDOWID");
120: if (s != NULL) {
121: return 2;
122: }
123: s = (char *)getenv("OSTYPE");
124: s2 = (char *)getenv("MACHTYPE");
125: s3 = (char *)getenv("PWD");
126: if ((s != NULL) || (s2 != NULL) || (s3 != NULL)) {
127: return 1;
128: }
129: return 3;
130: }
131:
132: char *getOStypes() {
133: int ostype;
134: ostype = getOStypei();
135:
136: if (ostype == 1) {
137: return("Windows-cygwin");
138: }else if (ostype == 2) {
139: return("Windows-cygwin-on-X");
140: }else if (ostype == 3) {
141: return("Windows-native");
142: }else{
143: return("unix");
144: }
145: }
146:
147: /*
148: kan96xx/Kan/ext.c
149: */
150: /*
151: -1 : no file
152: non-negative: there is a regular file or a directory
153: */
154: static int getFileSize(char *s) {
155: struct stat buf;
156: int m;
157: if (s == NULL) return -1;
158: m = stat(s,&buf);
159: if (m == -1) {
160: return -1;
161: /* fail */
162: }else{
163: /* success */
164: return (int) buf.st_size;
165: }
166: }
167:
168: static char *addSlash(char *p) {
169: char *p2;
170: if ((strlen(p) == 0) || (p == NULL)) return(p);
171: if (p[strlen(p)-1] == '/') return(p);
172: /* Add / */
173: p2 = (char *) mymalloc(sizeof(char)*(strlen(p)+3));
174: if (p2 == NULL) { fprintf(stderr,"No more memory.\n"); exit(10); }
175: strcpy(p2,p); strcat(p2,"/");
176: return(p2);
177:
178: }
179:
180: static void msg_get_home(int t,char *s) {
181: extern int Verbose_get_home;
182: if (!Verbose_get_home) return;
183: if (t == 1) {
184: fprintf(stderr,"getOpenXM_HOME(): ");
185: }else if (t == 2) {
186: fprintf(stderr,"getServerEnv(): ");
187: }else if (t == 3) {
1.2 ! takayama 188: fprintf(stderr,"?? ");
1.1 takayama 189: }else if (t == 4) {
190: fprintf(stderr,"cygwinPathToWinPath(): ");
191: }else if (t == 5) {
192: fprintf(stderr,"catArgv(): ");
193: }else{
194: fprintf(stderr,"getting path...: ");
195: }
196: if (s != NULL) {
197: fprintf(stderr,"%s\n",s);
198: }else{
199: fprintf(stderr," --NULL-- \n");
200: }
201: }
202: /* cf. k097/d.c getLOAD_K_PATH();
203: kan96xx/Kan/scanner.c getLOAD_SM1_PATH();
204: */
205: char *getOpenXM_HOME() {
206: char *p;
207: char *h;
208: p = getOStypes();
209: msg_get_home(1,p);
210:
211: p = (char *) getenv("OpenXM_HOME");
212: if (getFileSize(p) != -1) return addSlash(p);
213: msg_get_home(1,"OpenXM_HOME is not found.");
214:
215: p = (char *) getenv("OPENXM_HOME");
216: if (getFileSize(p) != -1) return addSlash(p);
217: msg_get_home(1,"OPENXM_HOME is not found.");
218:
219: if (getOStypei() == 3) { /* cygwin-native */
220: p = (char *) getenv("OpenXM_HOME_WIN");
221: if (getFileSize(p) != -1) return addSlash(p);
222: msg_get_home(1,"OpenXM_HOME_WIN is not found.");
223:
224: p = (char *) getenv("OPENXMHOMEWIN");
225: if (getFileSize(p) != -1) return addSlash(p);
226: msg_get_home(1,"OPENXMHOMEWIN is not found.");
227: }
228:
229: /* Try to find default directories */
230: h = (char *)getenv("HOME");
231: if (h != NULL) {
232: p = (char *)mymalloc(strlen(h)+100);
233: if (p == NULL) {
234: fprintf(stderr,"No more memory.\n"); exit(100);
235: }
236: strcat(h,"/OpenXM");
237: p = h;
238: if (getFileSize(p) != -1) return addSlash(p);
239: msg_get_home(1,"OpenXM is not found under the home directory.");
240: }
241:
242: if (getOStypei() != 3) {
243: p = "/usr/local/OpenXM";
244: }else{
245: p = "/cygdrive/c/usr/local/OpenXM";
246: }
247: if (getFileSize(p) != -1) return addSlash(p);
248: msg_get_home(1,"OpenXM is not found under /usr/local");
249:
250: if (getOStypei() != 0) {
251: p = "/cygdrive/c/OpenXM";
252: if (getFileSize(p) != -1) return addSlash(p);
253: msg_get_home(1,"OpenXM is not found under c:\\");
254:
255: p = "/cygdrive/c/OpenXM-win";
256: if (getFileSize(p) != -1) return addSlash(p);
257: msg_get_home(1,"OpenXM-win is not found under c:\\");
258:
259: p = "/cygdrive/c/Program Files/OpenXM";
260: if (getFileSize(p) != -1) return addSlash(p);
261: msg_get_home(1,"OpenXM is not found under c:\\Program Files");
262:
263: p = "/cygdrive/c/Program Files/OpenXM-win";
264: if (getFileSize(p) != -1) return addSlash(p);
265: msg_get_home(1,"OpenXM-win is not found under c:\\Program Files");
266:
267: }
268:
269: msg_get_home(1,"Giving up!");
270: return NULL;
271:
272: }
273:
274: static char *get_k0_lib_path() {
275: char *oxhome;
276: char *p;
277:
278:
279: p = (char *)getenv("LOAD_K_PATH");
280: if (p != NULL) {
281: if (getFileSize(p) != -1) return addSlash(p);
282: msg_get_home(1,"LOAD_K0_PATH is not found.");
283: }
284:
285: oxhome = getOpenXM_HOME();
286: if (oxhome == NULL) return (char *)NULL;
287: p = (char *) mymalloc(strlen(oxhome)+100);
288: if (p == NULL) {fprintf(stderr,"No more memory.\n"); exit(100);}
289:
290: strcpy(p,oxhome);
291: strcat(p,"lib/k097");
292: if (getFileSize(p) != -1) return addSlash(p);
293: msg_get_home(1,oxhome);
294: msg_get_home(1," is found, but ");
295: msg_get_home(1,p);
296: msg_get_home(1," is not found.");
297:
298: msg_get_home(1,"Giving up!");
299: return NULL;
300: }
301:
302: static char *get_sm1_lib_path() {
303: char *oxhome;
304: char *p;
305:
306: p = (char *)getenv("LOAD_SM1_PATH");
307: if (p != NULL) {
308: if (getFileSize(p) != -1) return addSlash(p);
309: msg_get_home(1,"LOAD_SM1_PATH is not found.");
310: }
311:
312: oxhome = getOpenXM_HOME();
313: if (oxhome == NULL) return NULL;
314: p = (char *) mymalloc(strlen(oxhome)+100);
315: if (p == NULL) {fprintf(stderr,"No more memory.\n"); exit(100);}
316:
317: strcpy(p,oxhome);
318: strcat(p,"lib/sm1");
319: if (getFileSize(p) != -1) return addSlash(p);
320: msg_get_home(1,oxhome);
321: msg_get_home(1," is found, but ");
322: msg_get_home(1,p);
323: msg_get_home(1," is not found.");
324:
325: msg_get_home(1,"Giving up!");
326: return NULL;
327: }
328:
329: static char *get_ox_asir_path() {
330: char *oxhome;
331: char *p;
332:
333: oxhome = getOpenXM_HOME();
334: if (oxhome == NULL) return NULL;
335: p = (char *) mymalloc(strlen(oxhome)+100);
336: if (p == NULL) {fprintf(stderr,"No more memory.\n"); exit(100);}
337:
338: strcpy(p,oxhome);
339: strcat(p,"bin/ox_asir");
340: if (getFileSize(p) != -1) return p;
341: msg_get_home(1,oxhome);
342: msg_get_home(1," is found, but ");
343: msg_get_home(1,p);
344: msg_get_home(1," is not found.");
345:
346: strcpy(p,oxhome);
347: strcat(p,"lib/asir/ox_asir");
348: if (getFileSize(p) != -1) return p;
349: msg_get_home(1,oxhome);
350: msg_get_home(1," is found, but ");
351: msg_get_home(1,p);
352: msg_get_home(1," is not found.");
353:
354: msg_get_home(1,"Giving up!");
355: return NULL;
356: }
357:
358: static char *get_ox_path() {
359: char *oxhome;
360: char *p;
361:
362: oxhome = getOpenXM_HOME();
363: if (oxhome == NULL) return NULL;
364: p = (char *) mymalloc(strlen(oxhome)+100);
365: if (p == NULL) {fprintf(stderr,"No more memory.\n"); exit(100);}
366:
367: strcpy(p,oxhome);
368: strcat(p,"bin/ox");
369: if (getFileSize(p) != -1) return p;
370: msg_get_home(1,oxhome);
371: msg_get_home(1," is found, but ");
372: msg_get_home(1,p);
373: msg_get_home(1," is not found.");
374:
375: msg_get_home(1,"Giving up!");
376: return NULL;
377: }
378:
379:
380: static char *get_oxc_path() {
381: char *oxhome;
382: char *p;
383:
384: oxhome = getOpenXM_HOME();
385: if (oxhome == NULL) return NULL;
386: p = (char *) mymalloc(strlen(oxhome)+100);
387: if (p == NULL) {fprintf(stderr,"No more memory.\n"); exit(100);}
388:
389: strcpy(p,oxhome);
390: strcat(p,"bin/oxc");
391: if (getFileSize(p) != -1) return p;
392: msg_get_home(1,oxhome);
393: msg_get_home(1," is found, but ");
394: msg_get_home(1,p);
395: msg_get_home(1," is not found.");
396:
397: msg_get_home(1,"Giving up!");
398: return NULL;
399: }
400:
401: static char *get_oxlog_path() {
402: char *oxhome;
403: char *p;
404:
405: oxhome = getOpenXM_HOME();
406: if (oxhome == NULL) return NULL;
407: p = (char *) mymalloc(strlen(oxhome)+100);
408: if (p == NULL) {fprintf(stderr,"No more memory.\n"); exit(100);}
409:
410: strcpy(p,oxhome);
411: strcat(p,"bin/oxlog");
412: if (getFileSize(p) != -1) return p;
413: msg_get_home(1,oxhome);
414: msg_get_home(1," is found, but ");
415: msg_get_home(1,p);
416: msg_get_home(1," is not found.");
417:
418: msg_get_home(1,"Giving up!");
419: return NULL;
420: }
421:
422: char *cygwinPathToWinPath(char *s) {
423: char *pos;
424: char *ans;
425: int i;
426: msg_get_home(4,s);
427: if (s == NULL) return NULL;
428: if (strlen(s) == 0) return s;
429:
430: ans = (char *) mymalloc(strlen(s) + 32);
431: if (ans == NULL) nomemory(0);
432:
433: pos = (char *)strstr(s,"/cygdrive/");
434: if (pos == s) {
435: strcpy(ans,&(s[9]));
436: ans[0] = s[10]; ans[1] = ':'; ans[2] = '\\';
437: }else{
438: strcpy(ans,s);
439: }
440:
441: if (ans[0] == '/') {
442: strcpy(ans,"C:\\cygwin");
443: strcat(ans,s);
444: }
445:
446:
447: for (i=0; i <strlen(ans); i++) {
448: if (ans[i] == '/') ans[i] = '\\';
449: }
450: return ans;
451: }
452:
453: char **getServerEnv(char *oxServer) {
454: int ostype;
455: char *p;
456: char *oxhome;
457: char *xterm;
458: char *oxlog;
459: char *load_sm1_path;
460: char *load_k0_path;
461: char *openXM_HOME;
462: #define ARGV_SIZE 100
463: char *argv[ARGV_SIZE];
464: int i,k;
465: char **aaa;
466:
467: if (Verbose_get_home) {
468: if (oxServer == NULL) {
469: fprintf(stderr,"Server name is NULL.\n");
470: }else{
471: fprintf(stderr,"Server name is %s\n",oxServer);
472: }
473: }
474:
475: if (oxServer == NULL) return NULL;
476: i = 0;
477: argv[i] = NULL;
478:
479: ostype = getOStypei();
480:
481: oxhome = getOpenXM_HOME();
482: if (oxhome == NULL) return NULL;
483: p = (char *) mymalloc(strlen(oxhome)+strlen(oxServer)+100);
484: if (p == NULL) {fprintf(stderr,"No more memory.\n"); exit(100);}
485:
486: strcpy(p,oxhome);
487: strcat(p,oxServer);
488: if (getFileSize(p) == -1) {
489: msg_get_home(2,oxhome);
490: msg_get_home(2," is found, but ");
491: msg_get_home(2,p);
492: msg_get_home(2," is not found.");
493: return (NULL);
494: }
495: oxServer = (char *) mymalloc(strlen(oxhome)+strlen(oxServer)+100);
496: if (oxServer == NULL) {fprintf(stderr,"No more memory.\n"); exit(100);}
497: strcpy(oxServer,p);
498:
499: if ((ostype == 0) || (ostype == 2)) {
500: if (!NoX) {
501: xterm = "/usr/X11R6/bin/xterm";
502: if (getFileSize(xterm) == -1) {
503: msg_get_home(2,"xterm is not found. NoX is automatically set.");
504: NoX = 1;
505: }
506: }
507: oxlog = get_oxlog_path();
508: xterm = "/usr/X11R6/bin/xterm -icon -e ";
509: argv[i] = oxlog; i++; argv[i] = NULL;
510: if (!NoX) {
511: argv[i] = "/usr/X11R6/bin/xterm"; i++; argv[i] = NULL;
512: argv[i] = "-icon"; i++; argv[i] = NULL;
513: argv[i] = "-e"; i++; argv[i] = NULL;
514: }
515: argv[i] = get_ox_path(); i++; argv[i] = NULL;
516: argv[i] = "-ox"; i++; argv[i] = NULL;
517: argv[i] = oxServer; i++; argv[i] = NULL;
518: }else{
519: if (!NoX) {
520: if (getFileSize("/cygdrive/c/winnt/system32/cmd.exe") >= 0) {
521: xterm = "/cygdrive/c/winnt/system32/cmd.exe /c start /min ";
522: argv[i] = "/cygdrive/c/winnt/system32/cmd.exe"; i++; argv[i] = NULL;
523: }else if (getFileSize("/cygdrive/c/windows/system32/cmd.exe") >= 0) {
524: xterm = "/cygdrive/c/windows/system32/cmd.exe /c start /min ";
525: argv[i] = "/cygdrive/c/windows/system32/cmd.exe"; i++; argv[i] = NULL;
526: }else{
527: msg_get_home(2,"cmd.exe is not found. NoX is automatically set.");
528: NoX = 1;
529: }
530: }
531: oxlog = " ";
532: if (!NoX) {
533: argv[i] = "/c"; i++; argv[i] = NULL;
534: argv[i] = "start"; i++; argv[i] = NULL;
535: argv[i] = "/min"; i++; argv[i] = NULL;
536: }
537: argv[i] = cygwinPathToWinPath(get_ox_path()); i++; argv[i] = NULL;
538: argv[i] = "-ox"; i++; argv[i] = NULL;
539: argv[i] = oxServer; i++; argv[i] = NULL;
540: }
541:
542: aaa = (char **) mymalloc(sizeof(char*)*(i+1));
543: if (aaa == NULL) nomemory(0);
544: msg_get_home(2,"--------- Result --------------");
545: for (k=0; k<i; k++) {
546: aaa[k] = argv[k];
547: msg_get_home(2,aaa[k]);
548: aaa[k+1] = NULL;
549: }
550: return aaa;
551: }
552:
553: char **setOXenv_old() {
554: char *openXM_HOME;
555: char *load_sm1_path;
556: char *load_k0_path;
557: char *asir_config;
558: char *asir_libdir;
559: char *asirloadpath;
560: char *asir_rsh;
561: char *ld_library_path;
562: char **aaa;
563: int n,i,k;
564:
565: /* set environmental variables */
566: n = 20; /* n must be larger than the number of env vars */
567: n++;
568: load_sm1_path = (char *) getenv("LOAD_SM1_PATH");
569: load_k0_path = (char *) getenv("LOAD_SM1_PATH");
570: asir_config = (char *) getenv("ASIR_CONFIG");
571: asir_libdir = (char *) getenv("ASIR_LIBDIR");
572: asirloadpath = (char *) getenv("ASIRLOADPATH");
573: asir_rsh = (char *) getenv("ASIR_RSH");
574: ld_library_path = (char *) getenv("LD_LIBRARY_PATH");
575:
576: openXM_HOME = getOpenXM_HOME();
577: if (openXM_HOME != NULL)
578: openXM_HOME[strlen(openXM_HOME)-1] = '\0';
579: /* How about ASIR... ? */
580:
581: msg_get_home(3,"OpenXM_HOME is"); msg_get_home(2,openXM_HOME);
582: msg_get_home(3,"LOAD_SM1_PATH is"); msg_get_home(2,load_sm1_path);
583: msg_get_home(3,"LOAD_K0_PATH is"); msg_get_home(2,load_k0_path);
584:
585: aaa = (char **) mymalloc(sizeof(char*)*n);
586: if (aaa == NULL) nomemory(0);
587:
588: i = 0;
589: if (openXM_HOME != NULL) {
590: aaa[i] = openXM_HOME; i++; aaa[i] = NULL; if (i > n-2) return aaa;
591: }
592: if (load_sm1_path != NULL) {
593: aaa[i] = load_sm1_path; i++; aaa[i] = NULL; if (i > n-2) return aaa;
594: }
595: if (load_k0_path != NULL) {
596: aaa[i] = load_k0_path; i++; aaa[i] = NULL; if (i > n-2) return aaa;
597: }
598: if (asir_config != NULL) {
599: aaa[i] = asir_config; i++; aaa[i] = NULL; if (i > n-2) return aaa;
600: }
601: if (asir_libdir != NULL) {
602: aaa[i] = asir_libdir; i++; aaa[i] = NULL; if (i > n-2) return aaa;
603: }
604: if (asirloadpath != NULL) {
605: aaa[i] = asirloadpath; i++; aaa[i] = NULL; if (i > n-2) return aaa;
606: }
607: if (asir_rsh != NULL) {
608: aaa[i] = asir_rsh; i++; aaa[i] = NULL; if (i > n-2) return aaa;
609: }
610:
611: msg_get_home(3,"--------- Result --------------");
612: for (k=0; k<n; k++) {
613: if (aaa[k] == NULL) break;
614: msg_get_home(3,aaa[k]);
615: }
616:
617: return aaa;
618: }
619:
620: char **debugServerEnv(char *oxServer)
621: {
622: int t;
623: char **aaa;
624: t = Verbose_get_home;
625: Verbose_get_home = 1;
626: aaa = getServerEnv(oxServer);
627: Verbose_get_home = t;
628: return ( aaa );
629: }
630:
631: char **catArgv(char **argv1,char **argv2)
632: {
633: int i,n1,n2;
634: char **argv;
635: n1=0;
636: while (argv1[n1] != NULL) n1++;
637: n2=0;
638: while (argv2[n2] != NULL) n2++;
639: argv = (char **) mymalloc(sizeof(char *)*(n1+n2+1));
640: if (argv == NULL) nomemory(0);
641: for (i=0; i<n1; i++) argv[i] = argv1[i];
642: for (i=0; i<n2; i++) argv[n1+i] = argv2[i];
643: argv[n1+n2]=NULL;
644: for (i=0; i<n1+n2; i++) {
645: msg_get_home(5,argv[i]);
646: }
647: return argv;
648: }
649:
650: char *getLOAD_SM1_PATH2() {
651: char *p;
652: p = get_sm1_lib_path();
653: if (p == NULL) {
654: return("/usr/local/lib/sm1/");
655: }else{
656: return p;
657: }
658: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>