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