Annotation of OpenXM/src/kan96xx/Kan/ext.c, Revision 1.29
1.29 ! takayama 1: /* $OpenXM: OpenXM/src/kan96xx/Kan/ext.c,v 1.28 2004/09/11 23:49:34 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.27 takayama 116: extern char *UD_str;
117: extern int UD_attr;
1.1 maekawa 118:
119: if (obj.tag != Sarray) errorKan1("%s\n","Kextension(): The argument must be an array.");
120: size = getoaSize(obj);
121: if (size < 1) errorKan1("%s\n","Kextension(): Empty array.");
122: keyo = getoa(obj,0);
123: if (keyo.tag != Sdollar) errorKan1("%s\n","Kextension(): No key word.");
124: key = KopString(keyo);
125:
126: /* branch by they key word. */
127: if (strcmp(key,"parse")==0) {
128: if (size != 2) errorKan1("%s\n","[(parse) string] extension.");
129: obj1 = getoa(obj,1);
130: if (obj1.tag != Sdollar) errorKan1("%s\n","[(parse) string] extension");
131:
132: pushEnv(EnvOfStackMachine);
133: m = KSexecuteString(obj1.lc.str);
134: /* This is critical area. If you catch ctrl-c here, program crashes. */
135: oldsig = signal(SIGINT,SIG_IGN);
136: popEnv(EnvOfStackMachine);
137: /* OK! We passed the critical area. */
138: signal(SIGINT,oldsig);
139: rob = KpoInteger(m);
140: }else if (strcmp(key,"getpid") == 0) {
141: rob = KpoInteger( (int) getpid() );
142: }else if (strcmp(key,"flush") == 0) {
143: /* fflush(NULL); */
144: fflush(stdout);
145: rob.tag = Snull;
146: }else if (strcmp(key,"chattrs")==0) {
147: if (size != 2) errorKan1("%s\n","[(chattrs) num] extension.");
148: obj1 = getoa(obj,1);
149: if (obj1.tag != Sinteger) errorKan1("%s\n","[(chattrs) num] extension.");
150: m = KopInteger(obj1);
1.28 takayama 151: /* if (!( m == 0 || m == PROTECT || m == ABSOLUTE_PROTECT || m == ATTR_INFIX))
152: errorKan1("%s\n","The number must be 0, 1 or 2.");*/
1.1 maekawa 153: putUserDictionary2((char *)NULL,0,0,m | SET_ATTR_FOR_ALL_WORDS,
1.5 takayama 154: CurrentContextp->userDictionary);
1.1 maekawa 155: }else if (strcmp(key,"keywords")==0) {
156: if (size != 1) errorKan1("%s\n","[(keywords)] extension.");
157: rob = showSystemDictionary(1);
158: /* }else if (strcmp(key,"fork0")==0) {
1.5 takayama 159: if (size != 2) errorKan1("%s\n","[(fork0) sss] extension.");
160: m = fork();
161: if (m>0) { rob = KpoInteger(m); }
162: else {
163: system(KopString(getoa(obj,1))); exit(0);
164: } */
165: }else if (strcmp(key,"defaultPolyRing")==0) {
166: if (size != 2) errorKan1("%s\n","[(defaultPolyRing) n] extension.");
167: rob = KdefaultPolyRing(getoa(obj,1));
168: }else if (strcmp(key,"getenv")==0) {
169: if (size != 2) errorKan1("%s\n","[(getenv) envstr] extension.");
170: obj1 = getoa(obj,1);
171: if (obj1.tag != Sdollar) errorKan1("%s\n","[(getenv) envstr] extension");
172: abc = getenv(KopString(obj1));
1.10 takayama 173: #if defined(__CYGWIN__)
174: if (abc == NULL) {
1.11 takayama 175: abc2 = (char *)sGC_malloc(sizeof(char)*(strlen(KopString(obj1))+2));
1.10 takayama 176: strcpy(abc2,KopString(obj1));
177: for (i=0; i<strlen(abc2); i++) {
178: abc2[i] = toupper(abc2[i]);
179: }
180: abc = getenv(abc2);
181: }
182: #endif
1.5 takayama 183: if (abc == NULL) {
184: rob = NullObject;
185: }else{
186: abc2 = (char *)sGC_malloc(sizeof(char)*(strlen(abc)+2));
187: strcpy(abc2,abc);
188: rob = KpoString(abc2);
189: }
190: }else if (strcmp(key,"stat")==0) {
191: if (size != 2) errorKan1("%s\n","[(stat) fname] extension.");
192: obj1 = getoa(obj,1);
193: if (obj1.tag != Sdollar) errorKan1("%s\n","[(stat) fname] extension ; string fname.");
194: m = stat(KopString(obj1),&buf);
195: rob = newObjectArray(2);
196: if (m == -1) {
197: /* fail */
198: obj2 = NullObject;
199: putoa(rob,0,obj2);
200: obj3 = newObjectArray(2);
201: putoa(obj3,0,KpoString("error no"));
202: putoa(obj3,1,KpoInteger(errno));
203: putoa(rob,1,obj3);
204: }else{
205: /* success */
206: putoa(rob,0,KpoInteger(0));
207: obj3 = newObjectArray(1);
208: putoa(obj3,0,KpoInteger((int) buf.st_size));
209: putoa(rob,1,obj3); /* We have not yet read buf fully */
210: }
211: }else if (strcmp(key,"forkExec")==0) {
212: if (size != 4) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension.");
213: obj1 = getoa(obj,1);
1.14 takayama 214: if (obj1.tag == Sdollar) {
215: obj1 = KstringToArgv(obj1);
216: }
1.5 takayama 217: if (obj1.tag != Sarray) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension. array argList.");
1.14 takayama 218: obj2 = getoa(obj,2);
1.5 takayama 219: if (obj2.tag != Sarray) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension. array fdList.");
220: obj3 = getoa(obj,3);
221: if (obj3.tag != Sinteger) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension. integer sigblock.");
1.7 takayama 222: m = KopInteger(obj3); /* m&1 : block ctrl-C. */
1.5 takayama 223: argListc = getoaSize(obj1);
224: fdListc = getoaSize(obj2);
1.6 takayama 225: if ((pid = fork()) > 0) {
1.5 takayama 226: /* parent */
1.7 takayama 227: if (m&2) {
1.6 takayama 228: /* Do not call singal to turn around a trouble on cygwin. BUG. */
229: }else{
230: signal(SIGCHLD,mywait); /* to kill Zombie */
231: }
232: Mychildren[Mycp++] = pid;
1.5 takayama 233: if (Mycp >= MYCP_SIZE-1) {
234: errorKan1("%s\n","Child process table is full.\n");
235: Mycp = 0;
236: }
1.6 takayama 237: rob = KpoInteger(pid);
1.5 takayama 238: /* Done */
239: }else{
240: /* Child */
241: for (i=0; i<fdListc; i++) {
242: /* close the specified files */
243: close(KopInteger(getoa(obj2,i)));
1.1 maekawa 244: }
1.5 takayama 245: /* execl */
1.7 takayama 246: if (m&1) {
1.5 takayama 247: {
248: sigset_t sss;
249: sigemptyset(&sss);
250: sigaddset(&sss,SIGINT);
251: sigprocmask(SIG_BLOCK,&sss,NULL);
252: }
1.1 maekawa 253: }
1.5 takayama 254: argv = (char **) sGC_malloc(sizeof(char *)*(argListc+1));
255: if (argv == NULL) {
256: fprintf(stderr," no more momory. forkExec --- exiting.\n");
257: _exit(10);
1.1 maekawa 258: }
1.5 takayama 259: for (i=0; i<argListc; i++) {
260: argv[i] = KopString(getoa(obj1,i));
261: argv[i+1] = NULL;
1.7 takayama 262: }
263:
264: if (m&4) {
265: fprintf(stderr,"execv %s\n",argv[0]);
266: sleep(5);
267: fprintf(stderr,">>>\n");
1.1 maekawa 268: }
1.14 takayama 269: execve(argv[0],argv,environ);
1.5 takayama 270: /* This place will never be reached unless execv fails. */
271: fprintf(stderr,"forkExec fails: ");
272: for (i=0; i<argListc; i++) {
273: fprintf(stderr,"%s ",argv[i]);
274: }
275: fprintf(stderr,"\nExiting, but staying as Zombie.\n");
276: _exit(10);
277: }
278: }else if (strcmp(key,"getchild")==0) {
279: if (size != 1) errorKan1("%s\n","[(getchild)] extension.");
280: rob = newObjectArray(Mycp);
281: for (i=0; i<Mycp; i++) {
282: putoa(rob,i,KpoInteger(Mychildren[i]));
1.1 maekawa 283: }
1.5 takayama 284: }else if (strcmp(key,"getUniqueFileName")==0) {
285: if (size != 2) errorKan1("%s\n","[(getUniqueFileName) path] extension.");
286: obj1 = getoa(obj,1);
287: if (obj1.tag != Sdollar) errorKan1("%s\n","[(getUniqueFileName) path] extension. path must be a string.");
288: rob = KpoString(ext_generateUniqueFileName(KopString(obj1)));
289: }else if (strcmp(key,"outputObjectToFile")==0) {
290: if (size != 3) errorKan1("%s\n","[(outputObjectToFile) path obj] extension.");
291: obj1 = getoa(obj,1);
292: if (obj1.tag != Sdollar) errorKan1("%s\n","[(outputObjectToFile) path obj] extension. path must be a string.");
293: obj2 = getoa(obj,2);
294: fp = fopen(KopString(obj1),"w");
295: if (fp == NULL) errorKan1("%s\n","[(outputObjectToFile) path object] extension : could not open the path.");
296: printObject(obj2,0,fp);
297: fclose(fp);
298: rob = NullObject;
299: }else if (strcmp(key,"hilbert")==0) {
300: if (size != 3) errorKan1("%s\n","[(hilbert) obgb obvlist] extension.");
301: rob = hilberto(getoa(obj,1),getoa(obj,2));
1.13 takayama 302: }else if (strcmp(key,"nobody") == 0) {
303: uid = getuid();
304: if (uid == 0) {
305: #define NOBODY 65534
306: /* If I'm a super user, then change uid to nobody. */
307: if (setuid(NOBODY) != 0) {
308: fprintf(stderr,"Failed to change uid to nobody (%d)\n",NOBODY);
309: exit(10);
310: }
311: fprintf(stderr,"uid is changed to nobody (%d).\n",NOBODY);
312: rob.tag = Snull;
313: }
1.5 takayama 314: }else if (strcmp(key,"chattr")==0) {
315: if (size != 3) errorKan1("%s\n","[(chattr) num symbol] extension.");
316: obj1 = getoa(obj,1);
317: obj2 = getoa(obj,2);
318: if (obj1.tag != Sinteger) errorKan1("%s\n","[(chattr) num symbol] extension.");
319: if (obj2.tag != Sstring) errorKan1("%s\n","[(chattr) num symbol] extension.");
320: m = KopInteger(obj1);
1.28 takayama 321: /* if (!( m == 0 || m == PROTECT || m == ABSOLUTE_PROTECT || m == ATTR_INFIX))
322: errorKan1("%s\n","The number must be 0, 1 or 2.");*/
1.29 ! takayama 323: putUserDictionary2(obj2.lc.str,(obj2.rc.op->lc).ival,(obj2.rc.op->rc).ival,
! 324: m,CurrentContextp->userDictionary);
! 325: }else if (strcmp(key,"or_attr")==0) {
! 326: if (size != 3) errorKan1("%s\n","[(or_attr) num symbol] extension.");
! 327: obj1 = getoa(obj,1);
! 328: obj2 = getoa(obj,2);
! 329: if (obj1.tag != Sinteger) errorKan1("%s\n","[(or_attr) num symbol] extension.");
! 330: if (obj2.tag != Sstring) errorKan1("%s\n","[(or_attr) num symbol] extension.");
! 331: m = KopInteger(obj1);
! 332: rob = KfindUserDictionary(obj2.lc.str);
! 333: if (rob.tag != NoObject.tag) {
! 334: if (strcmp(UD_str,obj2.lc.str) == 0) {
! 335: m |= UD_attr;
! 336: }else errorKan1("%s\n","or_attr: internal error.");
! 337: }
! 338: rob = KpoInteger(m);
1.5 takayama 339: putUserDictionary2(obj2.lc.str,(obj2.rc.op->lc).ival,(obj2.rc.op->rc).ival,
340: m,CurrentContextp->userDictionary);
1.27 takayama 341: }else if (strcmp(key,"getattr")==0) {
342: if (size != 2) errorKan1("%s\n","[(getattr) symbol] extension.");
343: obj1 = getoa(obj,1);
344: if (obj1.tag != Sstring) errorKan1("%s\n","[(getattr) symbol] extension.");
345: rob = KfindUserDictionary(obj1.lc.str);
346: if (rob.tag != NoObject.tag) {
347: if (strcmp(UD_str,obj1.lc.str) == 0) {
348: rob = KpoInteger(UD_attr);
349: }else errorKan1("%s\n","getattr: internal error.");
350: }else rob = NullObject;
1.15 takayama 351: }else if (strcmp(key,"getServerEnv")==0) {
352: if (size != 2) errorKan1("%s\n","[(getServerEnv) serverName] extension.");
353: obj1 = getoa(obj,1);
354: if (obj1.tag != Sdollar) errorKan1("%s\n","[(getServerEnv) serverName] extension.");
355: {
356: char **se; int ii; int nn;
357: se = getServerEnv(KopString(obj1));
358: if (se == NULL) {
359: debugServerEnv(KopString(obj1));
360: rob = NullObject;
361: }else{
362: for (ii=0,nn=0; se[ii] != NULL; ii++) nn++;
363: rob = newObjectArray(nn);
364: for (ii=0; ii<nn; ii++) {
365: putoa(rob,ii,KpoString(se[ii]));
366: }
367: }
368: }
1.12 takayama 369: }else if (strcmp(key,"regionMatches")==0) {
370: if (size != 3) errorKan1("%s\n","[(regionMatches) str strArray] extension.");
371: obj1 = getoa(obj,1);
372: if (obj1.tag != Sdollar) errorKan1("%s\n","[(regionMatches) str strArray] extension. str must be a string.");
373: obj2 = getoa(obj,2);
374: if (obj2.tag != Sarray) errorKan1("%s\n","[(regionMatches) str strArray] extension. strArray must be an array.");
375: rob = KregionMatches(obj1,obj2);
1.16 takayama 376: }else if (strcmp(key,"newVector")==0) {
377: if (size != 2) errorKan1("%s\n","[(newVector) m] extension.");
378: obj1 = getoa(obj,1);
379: if (obj1.tag != Sinteger) errorKan1("%s\n","[(newVector) m] extension. m must be an integer.");
380: rob = newObjectArray(KopInteger(obj1));
381: }else if (strcmp(key,"newMatrix")==0) {
382: if (size != 3) errorKan1("%s\n","[(newMatrix) m n] extension.");
383: obj1 = getoa(obj,1);
384: if (obj1.tag != Sinteger) errorKan1("%s\n","[(newMatrix) m n] extension. m must be an integer.");
385: obj2 = getoa(obj,2);
386: if (obj2.tag != Sinteger) errorKan1("%s\n","[(newMatrix) m n] extension. n must be an integer.");
387: rob = newObjectArray(KopInteger(obj1));
388: for (i=0; i<KopInteger(obj1); i++) {
389: putoa(rob,i,newObjectArray(KopInteger(obj2)));
390: }
1.21 takayama 391: }else if (strcmp(key,"ooPower")==0) {
392: if (size != 3) errorKan1("%s\n","[(ooPower) a b] extension.");
393: obj1 = getoa(obj,1);
394: obj2 = getoa(obj,2);
395: rob = KooPower(obj1,obj2);
1.25 takayama 396: }else if (strcmp(key,"Krest")==0) {
397: if (size != 2) errorKan1("%s\n","[(Krest) a] extension b");
398: obj1 = getoa(obj,1);
399: rob = Krest(obj1);
400: }else if (strcmp(key,"Kjoin")==0) {
401: if (size != 3) errorKan1("%s\n","[(Kjoin) a b] extension c");
402: obj1 = getoa(obj,1);
403: obj2 = getoa(obj,2);
404: rob = Kjoin(obj1,obj2);
1.8 takayama 405: }else if (strcmp(key,"ostype")==0) {
406: rob = newObjectArray(1);
407: /* Hard encode the OS type. */
408: #if defined(__CYGWIN__)
409: putoa(rob,0,KpoString("windows"));
410: #else
411: putoa(rob,0,KpoString("unix"));
412: #endif
1.24 takayama 413: }else if (strcmp(key,"traceClearStack")==0) {
414: traceClearStack();
415: rob = NullObject;
416: }else if (strcmp(key,"traceShowStack")==0) {
417: char *ssst;
418: ssst = traceShowStack();
419: if (ssst != NULL) {
420: rob = KpoString(ssst);
421: }else{
422: rob = NullObject;
423: }
1.5 takayama 424: }
1.1 maekawa 425: #include "plugin.hh"
1.17 takayama 426: #include "Kclass/tree.hh"
1.1 maekawa 427: else{
428: errorKan1("%s\n","Unknown tag for extension.");
429: }
430:
431:
432: return(rob);
1.12 takayama 433: }
434:
435: struct object KregionMatches(struct object sobj, struct object keyArray)
436: {
437: struct object rob;
438: int n,i,j,m,keyn;
439: char *s,*key;
440: rob = newObjectArray(3);
441: getoa(rob,0) = KpoInteger(-1);
442: getoa(rob,1) = NullObject;
443: getoa(rob,2) = NullObject;
444:
445: if (sobj.tag != Sdollar) return rob;
446: if (keyArray.tag != Sarray) return rob;
447: n = getoaSize(keyArray);
448: for (i=0; i<n; i++) {
449: if (getoa(keyArray,i).tag != Sdollar) { return rob; }
450: }
451:
452: s = KopString(sobj);
453: m = strlen(s);
454:
455: for (i=0; i<n; i++) {
456: key = KopString(getoa(keyArray,i));
457: keyn = strlen(key);
458: for (j=0; j<m; j++) {
459: if (strncmp(&(s[j]),key,keyn) == 0) {
460: getoa(rob,0) = KpoInteger(j);
461: getoa(rob,1) = KpoString(key);
462: getoa(rob,2) = KpoInteger(i);
463: return rob;
464: }
465: }
466: }
467: return rob;
1.1 maekawa 468: }
469:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>