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