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