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