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