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