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