Annotation of OpenXM/src/kan96xx/Kan/ext.c, Revision 1.26
1.26 ! takayama 1: /* $OpenXM: OpenXM/src/kan96xx/Kan/ext.c,v 1.25 2004/09/09 03:14:46 takayama Exp $ */
1.1 maekawa 2: #include <stdio.h>
3: #include <sys/types.h>
4: #include <sys/stat.h>
5: #include <fcntl.h>
6: #include <stdlib.h>
7: #include <unistd.h>
8: #include <sys/wait.h>
9: #include "datatype.h"
10: #include "stackm.h"
11: #include "extern.h"
12: #include "extern2.h"
13: #include <signal.h>
14: #include "plugin.h"
1.17 takayama 15: #include "kclass.h"
1.10 takayama 16: #include <ctype.h>
1.22 takayama 17: #include <errno.h>
1.15 takayama 18: #include "ox_pathfinder.h"
1.1 maekawa 19:
1.23 takayama 20: extern int Quiet;
1.14 takayama 21: extern char **environ;
22:
1.1 maekawa 23: #define MYCP_SIZE 100
24: static int Mychildren[MYCP_SIZE];
25: static int Mycp = 0;
1.26 ! takayama 26: static int Verbose_mywait = 0;
1.1 maekawa 27: static void mywait() {
28: int status;
29: int pid;
30: int i,j;
1.20 takayama 31: /* signal(SIGCHLD,SIG_IGN); */
1.1 maekawa 32: pid = wait(&status);
1.26 ! takayama 33: if ((!Quiet) && (Verbose_mywait)) fprintf(stderr,"Child process %d is exiting.\n",pid);
1.1 maekawa 34: for (i=0; i<Mycp; i++) {
35: if (Mychildren[i] == pid) {
36: for (j=i; j<Mycp-1; j++) {
1.5 takayama 37: Mychildren[j] = Mychildren[j+1];
1.1 maekawa 38: }
39: if (Mycp > 0) Mycp--;
40: }
41: }
42: signal(SIGCHLD,mywait);
43: }
44:
45: #define SIZE_OF_ENVSTACK 5
1.9 takayama 46: #if defined(__CYGWIN__)
47: #define JMP_BUF sigjmp_buf
48: #else
49: #define JMP_BUF jmp_buf
50: #endif
51: static JMP_BUF EnvStack[SIZE_OF_ENVSTACK];
1.1 maekawa 52: static int Envp = 0;
1.9 takayama 53: static void pushEnv(JMP_BUF jb) {
1.1 maekawa 54: if (Envp < SIZE_OF_ENVSTACK) {
55: *(EnvStack[Envp]) = *jb;
56: Envp++;
57: }else{
58: fprintf(stderr,"Overflow of EnvStack.\n");
59: exit(2);
60: }
61: }
1.9 takayama 62: static void popEnv(JMP_BUF jbp) {
1.1 maekawa 63: if (Envp <= 0) {
64: fprintf(stderr,"Underflow of EnvStack.\n");
65: exit(3);
66: }else{
67: Envp--;
68: *jbp = *EnvStack[Envp];
69: }
70: }
71:
72: static char *ext_generateUniqueFileName(char *s)
73: {
74: char *t;
75: int i;
76: struct stat statbuf;
77: t = (char *)sGC_malloc(sizeof(char)*strlen(s)+4+2);
78: for (i=0; i<1000; i++) {
79: /* Give up if we failed for 1000 names. */
80: sprintf(t,"%s.%d",s,i);
81: /* if (phc_overwrite) return(t); */
82: if (stat(t,&statbuf) < 0) {
83: return(t);
84: }
85: }
86: errorKan1("%s\n","ext_generateUniqueFileName: could not generate a unique file name. Exhausted all the names.");
87: return(NULL);
88: }
89:
90: struct object Kextension(struct object obj)
91: {
92: char *key;
93: int size;
94: struct object keyo;
95: struct object rob = NullObject;
96: struct object obj1,obj2,obj3,obj4;
1.13 takayama 97: int m,i,pid, uid;
1.1 maekawa 98: int argListc, fdListc;
99: char *abc;
100: char *abc2;
101: extern struct context *CurrentContextp;
1.9 takayama 102: #if (__CYGWIN__)
103: extern sigjmp_buf EnvOfStackMachine;
104: #else
1.1 maekawa 105: extern jmp_buf EnvOfStackMachine;
1.9 takayama 106: #endif
1.1 maekawa 107: extern void ctrlC();
108: extern int SigIgn;
109: extern int DebugCMO;
110: extern int OXprintMessage;
111: struct stat buf;
112: char **argv;
113: FILE *fp;
1.3 takayama 114: void (*oldsig)();
115: extern SecureMode;
1.1 maekawa 116:
117: if (obj.tag != Sarray) errorKan1("%s\n","Kextension(): The argument must be an array.");
118: size = getoaSize(obj);
119: if (size < 1) errorKan1("%s\n","Kextension(): Empty array.");
120: keyo = getoa(obj,0);
121: if (keyo.tag != Sdollar) errorKan1("%s\n","Kextension(): No key word.");
122: key = KopString(keyo);
123:
124: /* branch by they key word. */
125: if (strcmp(key,"parse")==0) {
126: if (size != 2) errorKan1("%s\n","[(parse) string] extension.");
127: obj1 = getoa(obj,1);
128: if (obj1.tag != Sdollar) errorKan1("%s\n","[(parse) string] extension");
129:
130: pushEnv(EnvOfStackMachine);
131: m = KSexecuteString(obj1.lc.str);
132: /* This is critical area. If you catch ctrl-c here, program crashes. */
133: oldsig = signal(SIGINT,SIG_IGN);
134: popEnv(EnvOfStackMachine);
135: /* OK! We passed the critical area. */
136: signal(SIGINT,oldsig);
137: rob = KpoInteger(m);
138: }else if (strcmp(key,"getpid") == 0) {
139: rob = KpoInteger( (int) getpid() );
140: }else if (strcmp(key,"flush") == 0) {
141: /* fflush(NULL); */
142: fflush(stdout);
143: rob.tag = Snull;
144: }else if (strcmp(key,"chattrs")==0) {
145: if (size != 2) errorKan1("%s\n","[(chattrs) num] extension.");
146: obj1 = getoa(obj,1);
147: if (obj1.tag != Sinteger) errorKan1("%s\n","[(chattrs) num] extension.");
148: m = KopInteger(obj1);
149: if (!( m == 0 || m == PROTECT || m == ABSOLUTE_PROTECT))
150: errorKan1("%s\n","The number must be 0, 1 or 2.");
151: putUserDictionary2((char *)NULL,0,0,m | SET_ATTR_FOR_ALL_WORDS,
1.5 takayama 152: CurrentContextp->userDictionary);
1.1 maekawa 153: }else if (strcmp(key,"keywords")==0) {
154: if (size != 1) errorKan1("%s\n","[(keywords)] extension.");
155: rob = showSystemDictionary(1);
156: /* }else if (strcmp(key,"fork0")==0) {
1.5 takayama 157: if (size != 2) errorKan1("%s\n","[(fork0) sss] extension.");
158: m = fork();
159: if (m>0) { rob = KpoInteger(m); }
160: else {
161: system(KopString(getoa(obj,1))); exit(0);
162: } */
163: }else if (strcmp(key,"defaultPolyRing")==0) {
164: if (size != 2) errorKan1("%s\n","[(defaultPolyRing) n] extension.");
165: rob = KdefaultPolyRing(getoa(obj,1));
166: }else if (strcmp(key,"getenv")==0) {
167: if (size != 2) errorKan1("%s\n","[(getenv) envstr] extension.");
168: obj1 = getoa(obj,1);
169: if (obj1.tag != Sdollar) errorKan1("%s\n","[(getenv) envstr] extension");
170: abc = getenv(KopString(obj1));
1.10 takayama 171: #if defined(__CYGWIN__)
172: if (abc == NULL) {
1.11 takayama 173: abc2 = (char *)sGC_malloc(sizeof(char)*(strlen(KopString(obj1))+2));
1.10 takayama 174: strcpy(abc2,KopString(obj1));
175: for (i=0; i<strlen(abc2); i++) {
176: abc2[i] = toupper(abc2[i]);
177: }
178: abc = getenv(abc2);
179: }
180: #endif
1.5 takayama 181: if (abc == NULL) {
182: rob = NullObject;
183: }else{
184: abc2 = (char *)sGC_malloc(sizeof(char)*(strlen(abc)+2));
185: strcpy(abc2,abc);
186: rob = KpoString(abc2);
187: }
188: }else if (strcmp(key,"stat")==0) {
189: if (size != 2) errorKan1("%s\n","[(stat) fname] extension.");
190: obj1 = getoa(obj,1);
191: if (obj1.tag != Sdollar) errorKan1("%s\n","[(stat) fname] extension ; string fname.");
192: m = stat(KopString(obj1),&buf);
193: rob = newObjectArray(2);
194: if (m == -1) {
195: /* fail */
196: obj2 = NullObject;
197: putoa(rob,0,obj2);
198: obj3 = newObjectArray(2);
199: putoa(obj3,0,KpoString("error no"));
200: putoa(obj3,1,KpoInteger(errno));
201: putoa(rob,1,obj3);
202: }else{
203: /* success */
204: putoa(rob,0,KpoInteger(0));
205: obj3 = newObjectArray(1);
206: putoa(obj3,0,KpoInteger((int) buf.st_size));
207: putoa(rob,1,obj3); /* We have not yet read buf fully */
208: }
209: }else if (strcmp(key,"forkExec")==0) {
210: if (size != 4) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension.");
211: obj1 = getoa(obj,1);
1.14 takayama 212: if (obj1.tag == Sdollar) {
213: obj1 = KstringToArgv(obj1);
214: }
1.5 takayama 215: if (obj1.tag != Sarray) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension. array argList.");
1.14 takayama 216: obj2 = getoa(obj,2);
1.5 takayama 217: if (obj2.tag != Sarray) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension. array fdList.");
218: obj3 = getoa(obj,3);
219: if (obj3.tag != Sinteger) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension. integer sigblock.");
1.7 takayama 220: m = KopInteger(obj3); /* m&1 : block ctrl-C. */
1.5 takayama 221: argListc = getoaSize(obj1);
222: fdListc = getoaSize(obj2);
1.6 takayama 223: if ((pid = fork()) > 0) {
1.5 takayama 224: /* parent */
1.7 takayama 225: if (m&2) {
1.6 takayama 226: /* Do not call singal to turn around a trouble on cygwin. BUG. */
227: }else{
228: signal(SIGCHLD,mywait); /* to kill Zombie */
229: }
230: Mychildren[Mycp++] = pid;
1.5 takayama 231: if (Mycp >= MYCP_SIZE-1) {
232: errorKan1("%s\n","Child process table is full.\n");
233: Mycp = 0;
234: }
1.6 takayama 235: rob = KpoInteger(pid);
1.5 takayama 236: /* Done */
237: }else{
238: /* Child */
239: for (i=0; i<fdListc; i++) {
240: /* close the specified files */
241: close(KopInteger(getoa(obj2,i)));
1.1 maekawa 242: }
1.5 takayama 243: /* execl */
1.7 takayama 244: if (m&1) {
1.5 takayama 245: {
246: sigset_t sss;
247: sigemptyset(&sss);
248: sigaddset(&sss,SIGINT);
249: sigprocmask(SIG_BLOCK,&sss,NULL);
250: }
1.1 maekawa 251: }
1.5 takayama 252: argv = (char **) sGC_malloc(sizeof(char *)*(argListc+1));
253: if (argv == NULL) {
254: fprintf(stderr," no more momory. forkExec --- exiting.\n");
255: _exit(10);
1.1 maekawa 256: }
1.5 takayama 257: for (i=0; i<argListc; i++) {
258: argv[i] = KopString(getoa(obj1,i));
259: argv[i+1] = NULL;
1.7 takayama 260: }
261:
262: if (m&4) {
263: fprintf(stderr,"execv %s\n",argv[0]);
264: sleep(5);
265: fprintf(stderr,">>>\n");
1.1 maekawa 266: }
1.14 takayama 267: execve(argv[0],argv,environ);
1.5 takayama 268: /* This place will never be reached unless execv fails. */
269: fprintf(stderr,"forkExec fails: ");
270: for (i=0; i<argListc; i++) {
271: fprintf(stderr,"%s ",argv[i]);
272: }
273: fprintf(stderr,"\nExiting, but staying as Zombie.\n");
274: _exit(10);
275: }
276: }else if (strcmp(key,"getchild")==0) {
277: if (size != 1) errorKan1("%s\n","[(getchild)] extension.");
278: rob = newObjectArray(Mycp);
279: for (i=0; i<Mycp; i++) {
280: putoa(rob,i,KpoInteger(Mychildren[i]));
1.1 maekawa 281: }
1.5 takayama 282: }else if (strcmp(key,"getUniqueFileName")==0) {
283: if (size != 2) errorKan1("%s\n","[(getUniqueFileName) path] extension.");
284: obj1 = getoa(obj,1);
285: if (obj1.tag != Sdollar) errorKan1("%s\n","[(getUniqueFileName) path] extension. path must be a string.");
286: rob = KpoString(ext_generateUniqueFileName(KopString(obj1)));
287: }else if (strcmp(key,"outputObjectToFile")==0) {
288: if (size != 3) errorKan1("%s\n","[(outputObjectToFile) path obj] extension.");
289: obj1 = getoa(obj,1);
290: if (obj1.tag != Sdollar) errorKan1("%s\n","[(outputObjectToFile) path obj] extension. path must be a string.");
291: obj2 = getoa(obj,2);
292: fp = fopen(KopString(obj1),"w");
293: if (fp == NULL) errorKan1("%s\n","[(outputObjectToFile) path object] extension : could not open the path.");
294: printObject(obj2,0,fp);
295: fclose(fp);
296: rob = NullObject;
297: }else if (strcmp(key,"hilbert")==0) {
298: if (size != 3) errorKan1("%s\n","[(hilbert) obgb obvlist] extension.");
299: rob = hilberto(getoa(obj,1),getoa(obj,2));
1.13 takayama 300: }else if (strcmp(key,"nobody") == 0) {
301: uid = getuid();
302: if (uid == 0) {
303: #define NOBODY 65534
304: /* If I'm a super user, then change uid to nobody. */
305: if (setuid(NOBODY) != 0) {
306: fprintf(stderr,"Failed to change uid to nobody (%d)\n",NOBODY);
307: exit(10);
308: }
309: fprintf(stderr,"uid is changed to nobody (%d).\n",NOBODY);
310: rob.tag = Snull;
311: }
1.5 takayama 312: }else if (strcmp(key,"chattr")==0) {
313: if (size != 3) errorKan1("%s\n","[(chattr) num symbol] extension.");
314: obj1 = getoa(obj,1);
315: obj2 = getoa(obj,2);
316: if (obj1.tag != Sinteger) errorKan1("%s\n","[(chattr) num symbol] extension.");
317: if (obj2.tag != Sstring) errorKan1("%s\n","[(chattr) num symbol] extension.");
318: m = KopInteger(obj1);
319: if (!( m == 0 || m == PROTECT || m == ABSOLUTE_PROTECT))
320: errorKan1("%s\n","The number must be 0, 1 or 2.");
321: putUserDictionary2(obj2.lc.str,(obj2.rc.op->lc).ival,(obj2.rc.op->rc).ival,
322: m,CurrentContextp->userDictionary);
1.15 takayama 323: }else if (strcmp(key,"getServerEnv")==0) {
324: if (size != 2) errorKan1("%s\n","[(getServerEnv) serverName] extension.");
325: obj1 = getoa(obj,1);
326: if (obj1.tag != Sdollar) errorKan1("%s\n","[(getServerEnv) serverName] extension.");
327: {
328: char **se; int ii; int nn;
329: se = getServerEnv(KopString(obj1));
330: if (se == NULL) {
331: debugServerEnv(KopString(obj1));
332: rob = NullObject;
333: }else{
334: for (ii=0,nn=0; se[ii] != NULL; ii++) nn++;
335: rob = newObjectArray(nn);
336: for (ii=0; ii<nn; ii++) {
337: putoa(rob,ii,KpoString(se[ii]));
338: }
339: }
340: }
1.12 takayama 341: }else if (strcmp(key,"regionMatches")==0) {
342: if (size != 3) errorKan1("%s\n","[(regionMatches) str strArray] extension.");
343: obj1 = getoa(obj,1);
344: if (obj1.tag != Sdollar) errorKan1("%s\n","[(regionMatches) str strArray] extension. str must be a string.");
345: obj2 = getoa(obj,2);
346: if (obj2.tag != Sarray) errorKan1("%s\n","[(regionMatches) str strArray] extension. strArray must be an array.");
347: rob = KregionMatches(obj1,obj2);
1.16 takayama 348: }else if (strcmp(key,"newVector")==0) {
349: if (size != 2) errorKan1("%s\n","[(newVector) m] extension.");
350: obj1 = getoa(obj,1);
351: if (obj1.tag != Sinteger) errorKan1("%s\n","[(newVector) m] extension. m must be an integer.");
352: rob = newObjectArray(KopInteger(obj1));
353: }else if (strcmp(key,"newMatrix")==0) {
354: if (size != 3) errorKan1("%s\n","[(newMatrix) m n] extension.");
355: obj1 = getoa(obj,1);
356: if (obj1.tag != Sinteger) errorKan1("%s\n","[(newMatrix) m n] extension. m must be an integer.");
357: obj2 = getoa(obj,2);
358: if (obj2.tag != Sinteger) errorKan1("%s\n","[(newMatrix) m n] extension. n must be an integer.");
359: rob = newObjectArray(KopInteger(obj1));
360: for (i=0; i<KopInteger(obj1); i++) {
361: putoa(rob,i,newObjectArray(KopInteger(obj2)));
362: }
1.21 takayama 363: }else if (strcmp(key,"ooPower")==0) {
364: if (size != 3) errorKan1("%s\n","[(ooPower) a b] extension.");
365: obj1 = getoa(obj,1);
366: obj2 = getoa(obj,2);
367: rob = KooPower(obj1,obj2);
1.25 takayama 368: }else if (strcmp(key,"Krest")==0) {
369: if (size != 2) errorKan1("%s\n","[(Krest) a] extension b");
370: obj1 = getoa(obj,1);
371: rob = Krest(obj1);
372: }else if (strcmp(key,"Kjoin")==0) {
373: if (size != 3) errorKan1("%s\n","[(Kjoin) a b] extension c");
374: obj1 = getoa(obj,1);
375: obj2 = getoa(obj,2);
376: rob = Kjoin(obj1,obj2);
1.8 takayama 377: }else if (strcmp(key,"ostype")==0) {
378: rob = newObjectArray(1);
379: /* Hard encode the OS type. */
380: #if defined(__CYGWIN__)
381: putoa(rob,0,KpoString("windows"));
382: #else
383: putoa(rob,0,KpoString("unix"));
384: #endif
1.24 takayama 385: }else if (strcmp(key,"traceClearStack")==0) {
386: traceClearStack();
387: rob = NullObject;
388: }else if (strcmp(key,"traceShowStack")==0) {
389: char *ssst;
390: ssst = traceShowStack();
391: if (ssst != NULL) {
392: rob = KpoString(ssst);
393: }else{
394: rob = NullObject;
395: }
1.5 takayama 396: }
1.1 maekawa 397: #include "plugin.hh"
1.17 takayama 398: #include "Kclass/tree.hh"
1.1 maekawa 399: else{
400: errorKan1("%s\n","Unknown tag for extension.");
401: }
402:
403:
404: return(rob);
1.12 takayama 405: }
406:
407: struct object KregionMatches(struct object sobj, struct object keyArray)
408: {
409: struct object rob;
410: int n,i,j,m,keyn;
411: char *s,*key;
412: rob = newObjectArray(3);
413: getoa(rob,0) = KpoInteger(-1);
414: getoa(rob,1) = NullObject;
415: getoa(rob,2) = NullObject;
416:
417: if (sobj.tag != Sdollar) return rob;
418: if (keyArray.tag != Sarray) return rob;
419: n = getoaSize(keyArray);
420: for (i=0; i<n; i++) {
421: if (getoa(keyArray,i).tag != Sdollar) { return rob; }
422: }
423:
424: s = KopString(sobj);
425: m = strlen(s);
426:
427: for (i=0; i<n; i++) {
428: key = KopString(getoa(keyArray,i));
429: keyn = strlen(key);
430: for (j=0; j<m; j++) {
431: if (strncmp(&(s[j]),key,keyn) == 0) {
432: getoa(rob,0) = KpoInteger(j);
433: getoa(rob,1) = KpoString(key);
434: getoa(rob,2) = KpoInteger(i);
435: return rob;
436: }
437: }
438: }
439: return rob;
1.1 maekawa 440: }
441:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>