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