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