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