Annotation of OpenXM/src/kan96xx/Kan/ext.c, Revision 1.3
1.3 ! takayama 1: /* $OpenXM: OpenXM/src/kan96xx/Kan/ext.c,v 1.2 2000/01/16 07:55:38 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++) {
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;
1.3 ! takayama 98: void (*oldsig)();
! 99: extern SecureMode;
1.1 maekawa 100:
101: if (obj.tag != Sarray) errorKan1("%s\n","Kextension(): The argument must be an array.");
102: size = getoaSize(obj);
103: if (size < 1) errorKan1("%s\n","Kextension(): Empty array.");
104: keyo = getoa(obj,0);
105: if (keyo.tag != Sdollar) errorKan1("%s\n","Kextension(): No key word.");
106: key = KopString(keyo);
107:
108: /* branch by they key word. */
109: if (strcmp(key,"parse")==0) {
110: if (size != 2) errorKan1("%s\n","[(parse) string] extension.");
111: obj1 = getoa(obj,1);
112: if (obj1.tag != Sdollar) errorKan1("%s\n","[(parse) string] extension");
113:
114: pushEnv(EnvOfStackMachine);
115: m = KSexecuteString(obj1.lc.str);
116: /* This is critical area. If you catch ctrl-c here, program crashes. */
117: oldsig = signal(SIGINT,SIG_IGN);
118: popEnv(EnvOfStackMachine);
119: /* OK! We passed the critical area. */
120: signal(SIGINT,oldsig);
121: rob = KpoInteger(m);
122: }else if (strcmp(key,"getpid") == 0) {
123: rob = KpoInteger( (int) getpid() );
124: }else if (strcmp(key,"flush") == 0) {
125: /* fflush(NULL); */
126: fflush(stdout);
127: rob.tag = Snull;
128: }else if (strcmp(key,"chattrs")==0) {
129: if (size != 2) errorKan1("%s\n","[(chattrs) num] extension.");
130: obj1 = getoa(obj,1);
131: if (obj1.tag != Sinteger) errorKan1("%s\n","[(chattrs) num] extension.");
132: m = KopInteger(obj1);
133: if (!( m == 0 || m == PROTECT || m == ABSOLUTE_PROTECT))
134: errorKan1("%s\n","The number must be 0, 1 or 2.");
135: putUserDictionary2((char *)NULL,0,0,m | SET_ATTR_FOR_ALL_WORDS,
136: CurrentContextp->userDictionary);
137: }else if (strcmp(key,"keywords")==0) {
138: if (size != 1) errorKan1("%s\n","[(keywords)] extension.");
139: rob = showSystemDictionary(1);
140: /* }else if (strcmp(key,"fork0")==0) {
141: if (size != 2) errorKan1("%s\n","[(fork0) sss] extension.");
142: m = fork();
143: if (m>0) { rob = KpoInteger(m); }
144: else {
145: system(KopString(getoa(obj,1))); exit(0);
146: } */
147: }else if (strcmp(key,"defaultPolyRing")==0) {
148: if (size != 2) errorKan1("%s\n","[(defaultPolyRing) n] extension.");
149: rob = KdefaultPolyRing(getoa(obj,1));
150: }else if (strcmp(key,"getenv")==0) {
151: if (size != 2) errorKan1("%s\n","[(getenv) envstr] extension.");
152: obj1 = getoa(obj,1);
153: if (obj1.tag != Sdollar) errorKan1("%s\n","[(getenv) envstr] extension");
154: abc = getenv(KopString(obj1));
155: if (abc == NULL) {
156: rob = NullObject;
157: }else{
158: abc2 = (char *)sGC_malloc(sizeof(char)*(strlen(abc)+2));
159: strcpy(abc2,abc);
160: rob = KpoString(abc2);
161: }
162: }else if (strcmp(key,"stat")==0) {
163: if (size != 2) errorKan1("%s\n","[(stat) fname] extension.");
164: obj1 = getoa(obj,1);
165: if (obj1.tag != Sdollar) errorKan1("%s\n","[(stat) fname] extension ; string fname.");
166: m = stat(KopString(obj1),&buf);
167: rob = newObjectArray(2);
168: if (m == -1) {
169: /* fail */
170: obj2 = NullObject;
171: putoa(rob,0,obj2);
172: obj3 = newObjectArray(2);
173: putoa(obj3,0,KpoString("error no"));
174: putoa(obj3,1,KpoInteger(errno));
175: putoa(rob,1,obj3);
176: }else{
177: /* success */
178: putoa(rob,0,KpoInteger(0));
179: putoa(rob,1,newObjectArray(0)); /* We have not yet read buf */
180: }
181: }else if (strcmp(key,"forkExec")==0) {
182: if (size != 4) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension.");
183: obj1 = getoa(obj,1);
184: if (obj1.tag != Sarray) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension. array argList.");
185: obj2 = getoa(obj,2);
186: if (obj2.tag != Sarray) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension. array fdList.");
187: obj3 = getoa(obj,3);
188: if (obj3.tag != Sinteger) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension. integer sigblock.");
189: m = KopInteger(obj3); /* m == 1 : block ctrl-C. */
190: argListc = getoaSize(obj1);
191: fdListc = getoaSize(obj2);
192: if ((m = fork()) > 0) {
193: /* parent */
194: signal(SIGCHLD,mywait); /* to kill Zombie */
195: Mychildren[Mycp++] = m;
196: if (Mycp >= MYCP_SIZE-1) {
197: errorKan1("%s\n","Child process table is full.\n");
198: Mycp = 0;
199: }
200: rob = KpoInteger(m);
201: /* Done */
202: }else{
203: /* Child */
204: for (i=0; i<fdListc; i++) {
205: /* close the specified files */
206: close(KopInteger(getoa(obj2,i)));
207: }
208: /* execl */
209: if (m == 1) {
210: {
211: sigset_t sss;
212: sigemptyset(&sss);
213: sigaddset(&sss,SIGINT);
214: sigprocmask(SIG_BLOCK,&sss,NULL);
215: }
216: }
217: argv = (char **) sGC_malloc(sizeof(char *)*(argListc+1));
218: if (argv == NULL) {
219: fprintf(stderr," no more momory. forkExec --- exiting.\n");
220: _exit(10);
221: }
222: for (i=0; i<argListc; i++) {
223: argv[i] = KopString(getoa(obj1,i));
224: argv[i+1] = NULL;
225: }
226: execv(argv[0],argv);
227: /* This place will never be reached unless execv fails. */
228: fprintf(stderr,"forkExec fails: ");
229: for (i=0; i<argListc; i++) {
230: fprintf(stderr,"%s ",argv[i]);
231: }
232: fprintf(stderr,"\nExiting, but staying as Zombie.\n");
233: _exit(10);
234: }
235: }else if (strcmp(key,"getchild")==0) {
236: if (size != 1) errorKan1("%s\n","[(getchild)] extension.");
237: rob = newObjectArray(Mycp);
238: for (i=0; i<Mycp; i++) {
239: putoa(rob,i,KpoInteger(Mychildren[i]));
240: }
241: }else if (strcmp(key,"getUniqueFileName")==0) {
242: if (size != 2) errorKan1("%s\n","[(getUniqueFileName) path] extension.");
243: obj1 = getoa(obj,1);
244: if (obj1.tag != Sdollar) errorKan1("%s\n","[(getUniqueFileName) path] extension. path must be a string.");
245: rob = KpoString(ext_generateUniqueFileName(KopString(obj1)));
246: }else if (strcmp(key,"outputObjectToFile")==0) {
247: if (size != 3) errorKan1("%s\n","[(outputObjectToFile) path obj] extension.");
248: obj1 = getoa(obj,1);
249: if (obj1.tag != Sdollar) errorKan1("%s\n","[(outputObjectToFile) path obj] extension. path must be a string.");
250: obj2 = getoa(obj,2);
251: fp = fopen(KopString(obj1),"w");
252: if (fp == NULL) errorKan1("%s\n","[(outputObjectToFile) path object] extension : could not open the path.");
253: printObject(obj2,0,fp);
254: fclose(fp);
255: rob = NullObject;
256: }else if (strcmp(key,"hilbert")==0) {
257: if (size != 3) errorKan1("%s\n","[(hilbert) obgb obvlist] extension.");
258: rob = hilberto(getoa(obj,1),getoa(obj,2));
259: }else if (strcmp(key,"chattr")==0) {
260: if (size != 3) errorKan1("%s\n","[(chattr) num symbol] extension.");
261: obj1 = getoa(obj,1);
262: obj2 = getoa(obj,2);
263: if (obj1.tag != Sinteger) errorKan1("%s\n","[(chattr) num symbol] extension.");
264: if (obj2.tag != Sstring) errorKan1("%s\n","[(chattr) num symbol] extension.");
265: m = KopInteger(obj1);
266: if (!( m == 0 || m == PROTECT || m == ABSOLUTE_PROTECT))
267: errorKan1("%s\n","The number must be 0, 1 or 2.");
268: putUserDictionary2(obj2.lc.str,(obj2.rc.op->lc).ival,(obj2.rc.op->rc).ival,
269: m,CurrentContextp->userDictionary);
270: }
271: #include "plugin.hh"
272: else{
273: errorKan1("%s\n","Unknown tag for extension.");
274: }
275:
276:
277: return(rob);
278: }
279:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>