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