Annotation of OpenXM/src/kan96xx/Kan/ext.c, Revision 1.41
1.41 ! takayama 1: /* $OpenXM: OpenXM/src/kan96xx/Kan/ext.c,v 1.40 2005/09/27 05:46:50 takayama Exp $ */
1.1 maekawa 2: #include <stdio.h>
1.38 ohara 3: #include <string.h>
1.1 maekawa 4: #include <sys/types.h>
5: #include <sys/stat.h>
6: #include <fcntl.h>
7: #include <stdlib.h>
8: #include <unistd.h>
9: #include <sys/wait.h>
10: #include "datatype.h"
11: #include "stackm.h"
12: #include "extern.h"
13: #include "extern2.h"
14: #include <signal.h>
15: #include "plugin.h"
1.17 takayama 16: #include "kclass.h"
1.10 takayama 17: #include <ctype.h>
1.22 takayama 18: #include <errno.h>
1.30 takayama 19: #include <regex.h>
1.15 takayama 20: #include "ox_pathfinder.h"
1.1 maekawa 21:
1.23 takayama 22: extern int Quiet;
1.14 takayama 23: extern char **environ;
1.39 takayama 24: extern char *MsgSourceTrace;
1.14 takayama 25:
1.1 maekawa 26: #define MYCP_SIZE 100
27: static int Mychildren[MYCP_SIZE];
28: static int Mycp = 0;
1.26 takayama 29: static int Verbose_mywait = 0;
1.1 maekawa 30: static void mywait() {
31: int status;
32: int pid;
33: int i,j;
1.20 takayama 34: /* signal(SIGCHLD,SIG_IGN); */
1.1 maekawa 35: pid = wait(&status);
1.26 takayama 36: if ((!Quiet) && (Verbose_mywait)) fprintf(stderr,"Child process %d is exiting.\n",pid);
1.1 maekawa 37: for (i=0; i<Mycp; i++) {
38: if (Mychildren[i] == pid) {
39: for (j=i; j<Mycp-1; j++) {
1.5 takayama 40: Mychildren[j] = Mychildren[j+1];
1.1 maekawa 41: }
42: if (Mycp > 0) Mycp--;
43: }
44: }
45: signal(SIGCHLD,mywait);
46: }
47:
48: #define SIZE_OF_ENVSTACK 5
1.9 takayama 49: #if defined(__CYGWIN__)
50: #define JMP_BUF sigjmp_buf
51: #else
52: #define JMP_BUF jmp_buf
53: #endif
54: static JMP_BUF EnvStack[SIZE_OF_ENVSTACK];
1.1 maekawa 55: static int Envp = 0;
1.9 takayama 56: static void pushEnv(JMP_BUF jb) {
1.1 maekawa 57: if (Envp < SIZE_OF_ENVSTACK) {
58: *(EnvStack[Envp]) = *jb;
59: Envp++;
60: }else{
61: fprintf(stderr,"Overflow of EnvStack.\n");
62: exit(2);
63: }
64: }
1.9 takayama 65: static void popEnv(JMP_BUF jbp) {
1.1 maekawa 66: if (Envp <= 0) {
67: fprintf(stderr,"Underflow of EnvStack.\n");
68: exit(3);
69: }else{
70: Envp--;
71: *jbp = *EnvStack[Envp];
72: }
73: }
74:
75: static char *ext_generateUniqueFileName(char *s)
76: {
77: char *t;
78: int i;
79: struct stat statbuf;
80: t = (char *)sGC_malloc(sizeof(char)*strlen(s)+4+2);
81: for (i=0; i<1000; i++) {
82: /* Give up if we failed for 1000 names. */
83: sprintf(t,"%s.%d",s,i);
84: /* if (phc_overwrite) return(t); */
85: if (stat(t,&statbuf) < 0) {
86: return(t);
87: }
88: }
89: errorKan1("%s\n","ext_generateUniqueFileName: could not generate a unique file name. Exhausted all the names.");
90: return(NULL);
91: }
92:
1.30 takayama 93: static struct object oregexec(struct object oregex,struct object ostrArray,struct object oflag);
94:
1.1 maekawa 95: struct object Kextension(struct object obj)
96: {
97: char *key;
98: int size;
1.36 takayama 99: struct object keyo = OINIT;
1.1 maekawa 100: struct object rob = NullObject;
1.36 takayama 101: struct object obj1 = OINIT;
102: struct object obj2 = OINIT;
103: struct object obj3 = OINIT;
104: struct object obj4 = OINIT;
1.13 takayama 105: int m,i,pid, uid;
1.1 maekawa 106: int argListc, fdListc;
107: char *abc;
108: char *abc2;
109: extern struct context *CurrentContextp;
1.9 takayama 110: #if (__CYGWIN__)
111: extern sigjmp_buf EnvOfStackMachine;
112: #else
1.1 maekawa 113: extern jmp_buf EnvOfStackMachine;
1.9 takayama 114: #endif
1.1 maekawa 115: extern void ctrlC();
116: extern int SigIgn;
117: extern int DebugCMO;
118: extern int OXprintMessage;
119: struct stat buf;
120: char **argv;
121: FILE *fp;
1.3 takayama 122: void (*oldsig)();
123: extern SecureMode;
1.27 takayama 124: extern char *UD_str;
125: extern int UD_attr;
1.1 maekawa 126:
127: if (obj.tag != Sarray) errorKan1("%s\n","Kextension(): The argument must be an array.");
128: size = getoaSize(obj);
129: if (size < 1) errorKan1("%s\n","Kextension(): Empty array.");
130: keyo = getoa(obj,0);
131: if (keyo.tag != Sdollar) errorKan1("%s\n","Kextension(): No key word.");
132: key = KopString(keyo);
133:
134: /* branch by they key word. */
135: if (strcmp(key,"parse")==0) {
136: if (size != 2) errorKan1("%s\n","[(parse) string] extension.");
137: obj1 = getoa(obj,1);
138: if (obj1.tag != Sdollar) errorKan1("%s\n","[(parse) string] extension");
139:
140: pushEnv(EnvOfStackMachine);
141: m = KSexecuteString(obj1.lc.str);
142: /* This is critical area. If you catch ctrl-c here, program crashes. */
143: oldsig = signal(SIGINT,SIG_IGN);
144: popEnv(EnvOfStackMachine);
145: /* OK! We passed the critical area. */
146: signal(SIGINT,oldsig);
147: rob = KpoInteger(m);
148: }else if (strcmp(key,"getpid") == 0) {
149: rob = KpoInteger( (int) getpid() );
150: }else if (strcmp(key,"flush") == 0) {
151: /* fflush(NULL); */
152: fflush(stdout);
153: rob.tag = Snull;
154: }else if (strcmp(key,"chattrs")==0) {
155: if (size != 2) errorKan1("%s\n","[(chattrs) num] extension.");
156: obj1 = getoa(obj,1);
157: if (obj1.tag != Sinteger) errorKan1("%s\n","[(chattrs) num] extension.");
158: m = KopInteger(obj1);
1.28 takayama 159: /* if (!( m == 0 || m == PROTECT || m == ABSOLUTE_PROTECT || m == ATTR_INFIX))
160: errorKan1("%s\n","The number must be 0, 1 or 2.");*/
1.1 maekawa 161: putUserDictionary2((char *)NULL,0,0,m | SET_ATTR_FOR_ALL_WORDS,
1.31 takayama 162: CurrentContextp->userDictionary);
163: }else if (strcmp(key,"or_attrs")==0) {
164: if (size != 2) errorKan1("%s\n","[(or_attrs) num] extension.");
165: obj1 = getoa(obj,1);
166: if (obj1.tag != Sinteger) errorKan1("%s\n","[(or_attrs) num] extension.");
167: m = KopInteger(obj1);
168: putUserDictionary2((char *)NULL,0,0,m | OR_ATTR_FOR_ALL_WORDS,
1.5 takayama 169: CurrentContextp->userDictionary);
1.1 maekawa 170: }else if (strcmp(key,"keywords")==0) {
171: if (size != 1) errorKan1("%s\n","[(keywords)] extension.");
172: rob = showSystemDictionary(1);
173: /* }else if (strcmp(key,"fork0")==0) {
1.5 takayama 174: if (size != 2) errorKan1("%s\n","[(fork0) sss] extension.");
175: m = fork();
176: if (m>0) { rob = KpoInteger(m); }
177: else {
178: system(KopString(getoa(obj,1))); exit(0);
179: } */
180: }else if (strcmp(key,"defaultPolyRing")==0) {
181: if (size != 2) errorKan1("%s\n","[(defaultPolyRing) n] extension.");
182: rob = KdefaultPolyRing(getoa(obj,1));
183: }else if (strcmp(key,"getenv")==0) {
184: if (size != 2) errorKan1("%s\n","[(getenv) envstr] extension.");
185: obj1 = getoa(obj,1);
186: if (obj1.tag != Sdollar) errorKan1("%s\n","[(getenv) envstr] extension");
187: abc = getenv(KopString(obj1));
1.10 takayama 188: #if defined(__CYGWIN__)
189: if (abc == NULL) {
1.11 takayama 190: abc2 = (char *)sGC_malloc(sizeof(char)*(strlen(KopString(obj1))+2));
1.10 takayama 191: strcpy(abc2,KopString(obj1));
192: for (i=0; i<strlen(abc2); i++) {
193: abc2[i] = toupper(abc2[i]);
194: }
195: abc = getenv(abc2);
196: }
197: #endif
1.5 takayama 198: if (abc == NULL) {
199: rob = NullObject;
200: }else{
201: abc2 = (char *)sGC_malloc(sizeof(char)*(strlen(abc)+2));
202: strcpy(abc2,abc);
203: rob = KpoString(abc2);
204: }
205: }else if (strcmp(key,"stat")==0) {
206: if (size != 2) errorKan1("%s\n","[(stat) fname] extension.");
207: obj1 = getoa(obj,1);
208: if (obj1.tag != Sdollar) errorKan1("%s\n","[(stat) fname] extension ; string fname.");
209: m = stat(KopString(obj1),&buf);
210: rob = newObjectArray(2);
211: if (m == -1) {
212: /* fail */
213: obj2 = NullObject;
214: putoa(rob,0,obj2);
215: obj3 = newObjectArray(2);
216: putoa(obj3,0,KpoString("error no"));
217: putoa(obj3,1,KpoInteger(errno));
218: putoa(rob,1,obj3);
219: }else{
220: /* success */
221: putoa(rob,0,KpoInteger(0));
222: obj3 = newObjectArray(1);
223: putoa(obj3,0,KpoInteger((int) buf.st_size));
224: putoa(rob,1,obj3); /* We have not yet read buf fully */
225: }
1.32 takayama 226: }else if (strcmp(key,"gethostname")==0) {
227: abc = (char *)sGC_malloc(sizeof(char)*1024);
228: if (gethostname(abc,1023) < 0) {
229: errorKan1("%s\n","hostname could not be obtained.");
230: }
231: rob = KpoString(abc);
1.5 takayama 232: }else if (strcmp(key,"forkExec")==0) {
233: if (size != 4) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension.");
234: obj1 = getoa(obj,1);
1.14 takayama 235: if (obj1.tag == Sdollar) {
236: obj1 = KstringToArgv(obj1);
237: }
1.5 takayama 238: if (obj1.tag != Sarray) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension. array argList.");
1.14 takayama 239: obj2 = getoa(obj,2);
1.5 takayama 240: if (obj2.tag != Sarray) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension. array fdList.");
241: obj3 = getoa(obj,3);
242: if (obj3.tag != Sinteger) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension. integer sigblock.");
1.7 takayama 243: m = KopInteger(obj3); /* m&1 : block ctrl-C. */
1.5 takayama 244: argListc = getoaSize(obj1);
245: fdListc = getoaSize(obj2);
1.6 takayama 246: if ((pid = fork()) > 0) {
1.5 takayama 247: /* parent */
1.7 takayama 248: if (m&2) {
1.6 takayama 249: /* Do not call singal to turn around a trouble on cygwin. BUG. */
250: }else{
251: signal(SIGCHLD,mywait); /* to kill Zombie */
252: }
253: Mychildren[Mycp++] = pid;
1.5 takayama 254: if (Mycp >= MYCP_SIZE-1) {
255: errorKan1("%s\n","Child process table is full.\n");
256: Mycp = 0;
257: }
1.6 takayama 258: rob = KpoInteger(pid);
1.5 takayama 259: /* Done */
260: }else{
261: /* Child */
262: for (i=0; i<fdListc; i++) {
263: /* close the specified files */
264: close(KopInteger(getoa(obj2,i)));
1.1 maekawa 265: }
1.5 takayama 266: /* execl */
1.7 takayama 267: if (m&1) {
1.5 takayama 268: {
269: sigset_t sss;
270: sigemptyset(&sss);
271: sigaddset(&sss,SIGINT);
272: sigprocmask(SIG_BLOCK,&sss,NULL);
273: }
1.1 maekawa 274: }
1.5 takayama 275: argv = (char **) sGC_malloc(sizeof(char *)*(argListc+1));
276: if (argv == NULL) {
277: fprintf(stderr," no more momory. forkExec --- exiting.\n");
278: _exit(10);
1.1 maekawa 279: }
1.5 takayama 280: for (i=0; i<argListc; i++) {
281: argv[i] = KopString(getoa(obj1,i));
282: argv[i+1] = NULL;
1.7 takayama 283: }
284:
285: if (m&4) {
286: fprintf(stderr,"execv %s\n",argv[0]);
287: sleep(5);
288: fprintf(stderr,">>>\n");
1.1 maekawa 289: }
1.14 takayama 290: execve(argv[0],argv,environ);
1.5 takayama 291: /* This place will never be reached unless execv fails. */
292: fprintf(stderr,"forkExec fails: ");
293: for (i=0; i<argListc; i++) {
294: fprintf(stderr,"%s ",argv[i]);
295: }
296: fprintf(stderr,"\nExiting, but staying as Zombie.\n");
297: _exit(10);
298: }
299: }else if (strcmp(key,"getchild")==0) {
300: if (size != 1) errorKan1("%s\n","[(getchild)] extension.");
301: rob = newObjectArray(Mycp);
302: for (i=0; i<Mycp; i++) {
303: putoa(rob,i,KpoInteger(Mychildren[i]));
1.1 maekawa 304: }
1.5 takayama 305: }else if (strcmp(key,"getUniqueFileName")==0) {
306: if (size != 2) errorKan1("%s\n","[(getUniqueFileName) path] extension.");
307: obj1 = getoa(obj,1);
308: if (obj1.tag != Sdollar) errorKan1("%s\n","[(getUniqueFileName) path] extension. path must be a string.");
309: rob = KpoString(ext_generateUniqueFileName(KopString(obj1)));
310: }else if (strcmp(key,"outputObjectToFile")==0) {
311: if (size != 3) errorKan1("%s\n","[(outputObjectToFile) path obj] extension.");
312: obj1 = getoa(obj,1);
313: if (obj1.tag != Sdollar) errorKan1("%s\n","[(outputObjectToFile) path obj] extension. path must be a string.");
314: obj2 = getoa(obj,2);
315: fp = fopen(KopString(obj1),"w");
316: if (fp == NULL) errorKan1("%s\n","[(outputObjectToFile) path object] extension : could not open the path.");
317: printObject(obj2,0,fp);
318: fclose(fp);
319: rob = NullObject;
1.35 takayama 320: }else if (strcmp(key,"getAttributeList")==0) {
321: if (size != 2) errorKan1("%s\n","[(getAttributeList) ob] extension rob");
322: rob = KgetAttributeList(getoa(obj,1));
1.37 takayama 323: }else if (strcmp(key,"setAttributeList")==0) {
324: if (size != 3) errorKan1("%s\n","[(setAttributeList) ob attrlist] extension rob");
325: rob = KsetAttributeList(getoa(obj,1), getoa(obj,2));
1.35 takayama 326: }else if (strcmp(key,"getAttribute")==0) {
327: if (size != 3) errorKan1("%s\n","[(getAttribute) ob key] extension rob");
328: rob = KgetAttribute(getoa(obj,1),getoa(obj,2));
1.37 takayama 329: }else if (strcmp(key,"setAttribute")==0) {
330: if (size != 4) errorKan1("%s\n","[(setAttributeList) ob key value] extension rob");
331: rob = KsetAttribute(getoa(obj,1), getoa(obj,2),getoa(obj,3));
1.5 takayama 332: }else if (strcmp(key,"hilbert")==0) {
333: if (size != 3) errorKan1("%s\n","[(hilbert) obgb obvlist] extension.");
334: rob = hilberto(getoa(obj,1),getoa(obj,2));
1.13 takayama 335: }else if (strcmp(key,"nobody") == 0) {
336: uid = getuid();
337: if (uid == 0) {
338: #define NOBODY 65534
339: /* If I'm a super user, then change uid to nobody. */
340: if (setuid(NOBODY) != 0) {
341: fprintf(stderr,"Failed to change uid to nobody (%d)\n",NOBODY);
342: exit(10);
343: }
344: fprintf(stderr,"uid is changed to nobody (%d).\n",NOBODY);
345: rob.tag = Snull;
346: }
1.5 takayama 347: }else if (strcmp(key,"chattr")==0) {
348: if (size != 3) errorKan1("%s\n","[(chattr) num symbol] extension.");
349: obj1 = getoa(obj,1);
350: obj2 = getoa(obj,2);
351: if (obj1.tag != Sinteger) errorKan1("%s\n","[(chattr) num symbol] extension.");
352: if (obj2.tag != Sstring) errorKan1("%s\n","[(chattr) num symbol] extension.");
353: m = KopInteger(obj1);
1.28 takayama 354: /* if (!( m == 0 || m == PROTECT || m == ABSOLUTE_PROTECT || m == ATTR_INFIX))
355: errorKan1("%s\n","The number must be 0, 1 or 2.");*/
1.29 takayama 356: putUserDictionary2(obj2.lc.str,(obj2.rc.op->lc).ival,(obj2.rc.op->rc).ival,
357: m,CurrentContextp->userDictionary);
358: }else if (strcmp(key,"or_attr")==0) {
359: if (size != 3) errorKan1("%s\n","[(or_attr) num symbol] extension.");
360: obj1 = getoa(obj,1);
361: obj2 = getoa(obj,2);
362: if (obj1.tag != Sinteger) errorKan1("%s\n","[(or_attr) num symbol] extension.");
363: if (obj2.tag != Sstring) errorKan1("%s\n","[(or_attr) num symbol] extension.");
364: m = KopInteger(obj1);
365: rob = KfindUserDictionary(obj2.lc.str);
366: if (rob.tag != NoObject.tag) {
367: if (strcmp(UD_str,obj2.lc.str) == 0) {
368: m |= UD_attr;
369: }else errorKan1("%s\n","or_attr: internal error.");
370: }
371: rob = KpoInteger(m);
1.5 takayama 372: putUserDictionary2(obj2.lc.str,(obj2.rc.op->lc).ival,(obj2.rc.op->rc).ival,
373: m,CurrentContextp->userDictionary);
1.27 takayama 374: }else if (strcmp(key,"getattr")==0) {
375: if (size != 2) errorKan1("%s\n","[(getattr) symbol] extension.");
376: obj1 = getoa(obj,1);
377: if (obj1.tag != Sstring) errorKan1("%s\n","[(getattr) symbol] extension.");
378: rob = KfindUserDictionary(obj1.lc.str);
379: if (rob.tag != NoObject.tag) {
380: if (strcmp(UD_str,obj1.lc.str) == 0) {
381: rob = KpoInteger(UD_attr);
382: }else errorKan1("%s\n","getattr: internal error.");
383: }else rob = NullObject;
1.15 takayama 384: }else if (strcmp(key,"getServerEnv")==0) {
385: if (size != 2) errorKan1("%s\n","[(getServerEnv) serverName] extension.");
386: obj1 = getoa(obj,1);
387: if (obj1.tag != Sdollar) errorKan1("%s\n","[(getServerEnv) serverName] extension.");
388: {
389: char **se; int ii; int nn;
390: se = getServerEnv(KopString(obj1));
391: if (se == NULL) {
392: debugServerEnv(KopString(obj1));
393: rob = NullObject;
394: }else{
395: for (ii=0,nn=0; se[ii] != NULL; ii++) nn++;
396: rob = newObjectArray(nn);
397: for (ii=0; ii<nn; ii++) {
398: putoa(rob,ii,KpoString(se[ii]));
399: }
400: }
401: }
1.33 takayama 402: }else if (strcmp(key,"read")==0) {
403: if (size != 3) errorKan1("%s\n","[(read) fd size] extension.");
404: obj1 = getoa(obj,1);
405: if (obj1.tag != Sinteger) errorKan1("%s\n","[(read) fd size] extension. fd must be an integer.");
406: obj2 = getoa(obj,2);
407: if (obj2.tag != Sinteger) errorKan1("%s\n","[(read) fd size] extension. size must be an integer.");
408: {
409: int total, n, fd;
410: char *s; char *s0;
411: fd = KopInteger(obj1);
412: total = KopInteger(obj2);
413: if (total <= 0) errorKan1("%s\n","[(read) ...]; negative size has not yet been implemented.");
414: /* Return a string. todo: implement SbyteArray case. */
415: s0 = s = (char *) sGC_malloc(total+1);
416: if (s0 == NULL) errorKan1("%s\n","[(read) ...]; no more memory.");
417: while (total >0) {
418: n = read(fd, s, total);
419: if (n < 0) { perror("read"); errorKan1("%s\n","[(read) ...]; read error.");}
420: s[n] = 0;
421: total -= n; s = &(s[n]);
422: }
423: rob = KpoString(s0);
424: }
1.12 takayama 425: }else if (strcmp(key,"regionMatches")==0) {
426: if (size != 3) errorKan1("%s\n","[(regionMatches) str strArray] extension.");
427: obj1 = getoa(obj,1);
428: if (obj1.tag != Sdollar) errorKan1("%s\n","[(regionMatches) str strArray] extension. str must be a string.");
429: obj2 = getoa(obj,2);
430: if (obj2.tag != Sarray) errorKan1("%s\n","[(regionMatches) str strArray] extension. strArray must be an array.");
431: rob = KregionMatches(obj1,obj2);
1.16 takayama 432: }else if (strcmp(key,"newVector")==0) {
433: if (size != 2) errorKan1("%s\n","[(newVector) m] extension.");
434: obj1 = getoa(obj,1);
435: if (obj1.tag != Sinteger) errorKan1("%s\n","[(newVector) m] extension. m must be an integer.");
436: rob = newObjectArray(KopInteger(obj1));
437: }else if (strcmp(key,"newMatrix")==0) {
438: if (size != 3) errorKan1("%s\n","[(newMatrix) m n] extension.");
439: obj1 = getoa(obj,1);
440: if (obj1.tag != Sinteger) errorKan1("%s\n","[(newMatrix) m n] extension. m must be an integer.");
441: obj2 = getoa(obj,2);
442: if (obj2.tag != Sinteger) errorKan1("%s\n","[(newMatrix) m n] extension. n must be an integer.");
443: rob = newObjectArray(KopInteger(obj1));
444: for (i=0; i<KopInteger(obj1); i++) {
445: putoa(rob,i,newObjectArray(KopInteger(obj2)));
446: }
1.21 takayama 447: }else if (strcmp(key,"ooPower")==0) {
448: if (size != 3) errorKan1("%s\n","[(ooPower) a b] extension.");
449: obj1 = getoa(obj,1);
450: obj2 = getoa(obj,2);
451: rob = KooPower(obj1,obj2);
1.25 takayama 452: }else if (strcmp(key,"Krest")==0) {
453: if (size != 2) errorKan1("%s\n","[(Krest) a] extension b");
454: obj1 = getoa(obj,1);
455: rob = Krest(obj1);
456: }else if (strcmp(key,"Kjoin")==0) {
457: if (size != 3) errorKan1("%s\n","[(Kjoin) a b] extension c");
458: obj1 = getoa(obj,1);
459: obj2 = getoa(obj,2);
460: rob = Kjoin(obj1,obj2);
1.8 takayama 461: }else if (strcmp(key,"ostype")==0) {
462: rob = newObjectArray(1);
463: /* Hard encode the OS type. */
464: #if defined(__CYGWIN__)
465: putoa(rob,0,KpoString("windows"));
466: #else
467: putoa(rob,0,KpoString("unix"));
468: #endif
1.40 takayama 469: }else if (strcmp(key,"stringToArgv")==0) {
470: if (size != 2) errorKan1("%s\n","[(stringToArgv) a ] extension b");
471: obj1 = getoa(obj,1);
472: if (obj1.tag != Sdollar) errorKan1("%s\n","[(stringToArgv) a ] extension b, a must be a string.");
473: rob = KstringToArgv(obj1);
1.41 ! takayama 474: }else if (strcmp(key,"stringToArgv2")==0) {
! 475: if (size != 3) errorKan1("%s\n","[(stringToArgv2) str separator] extension b");
! 476: obj1 = getoa(obj,1);
! 477: obj2 = getoa(obj,2);
! 478: rob = KstringToArgv2(obj1,obj2);
1.24 takayama 479: }else if (strcmp(key,"traceClearStack")==0) {
480: traceClearStack();
481: rob = NullObject;
482: }else if (strcmp(key,"traceShowStack")==0) {
483: char *ssst;
484: ssst = traceShowStack();
1.39 takayama 485: if (ssst != NULL) {
486: rob = KpoString(ssst);
487: }else{
488: rob = NullObject;
489: }
490: }else if (strcmp(key,"traceShowScannerBuf")==0) {
491: char *ssst;
492: ssst = MsgSourceTrace;
1.24 takayama 493: if (ssst != NULL) {
494: rob = KpoString(ssst);
495: }else{
496: rob = NullObject;
497: }
1.30 takayama 498: }else if (strcmp(key,"regexec")==0) {
499: if ((size != 3) && (size != 4)) errorKan1("%s\n","[(regexec) reg strArray flag(optional)] extension b");
500: obj1 = getoa(obj,1);
501: if (obj1.tag != Sdollar) errorKan1("%s\n","regexec, the first argument should be a string (regular expression).");
502: obj2 = getoa(obj,2);
503: if (obj2.tag != Sarray) errorKan1("%s\n","regexec, the second argument should be an array of a string.");
504: if (size == 3) obj3 = newObjectArray(0);
505: else obj3 = getoa(obj,3);
506: rob = oregexec(obj1,obj2,obj3);
1.34 takayama 507: }else if (strcmp(key,"unlink")==0) {
508: if (size != 2) errorKan1("%s\n","[(unlink) filename] extension b");
509: obj1 = getoa(obj,1);
510: if (obj1.tag != Sdollar) errorKan1("%s\n","unlink, the first argument should be a string (filename).");
511: rob = KpoInteger(oxDeleteFile(KopString(obj1)));
1.5 takayama 512: }
1.1 maekawa 513: #include "plugin.hh"
1.17 takayama 514: #include "Kclass/tree.hh"
1.1 maekawa 515: else{
1.33 takayama 516: fprintf(stderr,"key=%s; ",key);
517: errorKan1("%s\n","Unknown key for extension.");
1.1 maekawa 518: }
519:
520:
521: return(rob);
1.12 takayama 522: }
523:
524: struct object KregionMatches(struct object sobj, struct object keyArray)
525: {
1.36 takayama 526: struct object rob = OINIT;
1.12 takayama 527: int n,i,j,m,keyn;
528: char *s,*key;
529: rob = newObjectArray(3);
530: getoa(rob,0) = KpoInteger(-1);
531: getoa(rob,1) = NullObject;
532: getoa(rob,2) = NullObject;
533:
534: if (sobj.tag != Sdollar) return rob;
535: if (keyArray.tag != Sarray) return rob;
536: n = getoaSize(keyArray);
537: for (i=0; i<n; i++) {
538: if (getoa(keyArray,i).tag != Sdollar) { return rob; }
539: }
540:
541: s = KopString(sobj);
542: m = strlen(s);
543:
544: for (i=0; i<n; i++) {
545: key = KopString(getoa(keyArray,i));
546: keyn = strlen(key);
547: for (j=0; j<m; j++) {
548: if (strncmp(&(s[j]),key,keyn) == 0) {
549: getoa(rob,0) = KpoInteger(j);
550: getoa(rob,1) = KpoString(key);
551: getoa(rob,2) = KpoInteger(i);
552: return rob;
553: }
554: }
555: }
556: return rob;
1.1 maekawa 557: }
558:
1.30 takayama 559: static struct object oregexec(struct object oregex,struct object ostrArray,struct object oflag) {
1.36 takayama 560: struct object rob = OINIT;
561: struct object ob = OINIT;
1.30 takayama 562: int n,i,j,m,keyn,cflag,eflag,er;
563: char *regex;
564: regex_t preg;
565: char *s;
566: char *mbuf; int mbufSize;
567: #define REGMATCH_SIZE 100
568: regmatch_t pmatch[100]; size_t nmatch;
569: int size;
570:
571: nmatch = (size_t) REGMATCH_SIZE;
572: rob = newObjectArray(0);
573: mbufSize = 1024;
574:
575: if (oregex.tag != Sdollar) return rob;
576: if (ostrArray.tag != Sarray) return rob;
577: n = getoaSize(ostrArray);
578: for (i=0; i<n; i++) {
579: if (getoa(ostrArray,i).tag != Sdollar) { return rob; }
580: }
581: if (oflag.tag != Sarray) errorKan1("%s\n","oregexec: oflag should be an array of integers.");
582: cflag = eflag = 0;
583: oflag = Kto_int32(oflag);
584: for (i=0; i<getoaSize(oflag); i++) {
585: ob = getoa(oflag,i);
586: if (ob.tag != Sinteger) errorKan1("%s\n","oregexec: oflag is not an array of integers.");
587: if (i == 0) cflag = KopInteger(ob);
588: if (i == 1) eflag = KopInteger(ob);
589: }
590:
591: regex = KopString(oregex);
592: if (er=regcomp(&preg,regex,cflag)) {
593: mbuf = (char *) sGC_malloc(mbufSize);
594: if (mbuf == NULL) errorKan1("%s\n","No more memory.");
595: regerror(er,&preg,mbuf,mbufSize-1);
596: errorKan1("regcomp error: %s\n",mbuf);
597: }
598:
599: size = 0; /* We should use list instead of counting the size. */
600: for (i=0; i<n; i++) {
601: s = KopString(getoa(ostrArray,i));
602: er=regexec(&preg,s,nmatch,pmatch,eflag);
603: if ((er != 0) && (er != REG_NOMATCH)) {
604: mbuf = (char *) sGC_malloc(mbufSize);
605: if (mbuf == NULL) errorKan1("%s\n","No more memory.");
606: regerror(er,&preg,mbuf,mbufSize-1);
607: errorKan1("regcomp error: %s\n",mbuf);
608: }
609: if (er == 0) size++;
610: }
611:
612: rob = newObjectArray(size);
613: size = 0;
614: for (i=0; i<n; i++) {
615: s = KopString(getoa(ostrArray,i));
616: er=regexec(&preg,s,nmatch,pmatch,eflag);
617: if ((er != 0) && (er != REG_NOMATCH)) {
618: mbuf = (char *) sGC_malloc(mbufSize);
619: if (mbuf == NULL) errorKan1("%s\n","No more memory.");
620: regerror(er,&preg,mbuf,mbufSize-1);
621: errorKan1("regcomp error: %s\n",mbuf);
622: }
623: if (er == 0) {
624: ob = newObjectArray(3);
625: putoa(ob,0,KpoString(s));
626: /* temporary */
627: putoa(ob,1,KpoInteger((int) (pmatch[0].rm_so)));
628: putoa(ob,2,KpoInteger((int) (pmatch[0].rm_eo)));
629: putoa(rob,size,ob);
630: size++;
631: }
632: }
633: regfree(&preg);
634: return rob;
635: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>