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