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