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