Annotation of OpenXM/src/kan96xx/Kan/ext.c, Revision 1.35
1.35 ! takayama 1: /* $OpenXM: OpenXM/src/kan96xx/Kan/ext.c,v 1.34 2004/12/21 03:25:04 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;
1.35 ! takayama 315: }else if (strcmp(key,"getAttributeList")==0) {
! 316: if (size != 2) errorKan1("%s\n","[(getAttributeList) ob] extension rob");
! 317: rob = KgetAttributeList(getoa(obj,1));
! 318: }else if (strcmp(key,"putAttributeList")==0) {
! 319: if (size != 3) errorKan1("%s\n","[(putAttributeList) ob attrlist] extension rob");
! 320: rob = KputAttributeList(getoa(obj,1), getoa(obj,2));
! 321: }else if (strcmp(key,"getAttribute")==0) {
! 322: if (size != 3) errorKan1("%s\n","[(getAttribute) ob key] extension rob");
! 323: rob = KgetAttribute(getoa(obj,1),getoa(obj,2));
! 324: }else if (strcmp(key,"putAttribute")==0) {
! 325: if (size != 4) errorKan1("%s\n","[(putAttributeList) ob key value] extension rob");
! 326: rob = KputAttribute(getoa(obj,1), getoa(obj,2),getoa(obj,3));
1.5 takayama 327: }else if (strcmp(key,"hilbert")==0) {
328: if (size != 3) errorKan1("%s\n","[(hilbert) obgb obvlist] extension.");
329: rob = hilberto(getoa(obj,1),getoa(obj,2));
1.13 takayama 330: }else if (strcmp(key,"nobody") == 0) {
331: uid = getuid();
332: if (uid == 0) {
333: #define NOBODY 65534
334: /* If I'm a super user, then change uid to nobody. */
335: if (setuid(NOBODY) != 0) {
336: fprintf(stderr,"Failed to change uid to nobody (%d)\n",NOBODY);
337: exit(10);
338: }
339: fprintf(stderr,"uid is changed to nobody (%d).\n",NOBODY);
340: rob.tag = Snull;
341: }
1.5 takayama 342: }else if (strcmp(key,"chattr")==0) {
343: if (size != 3) errorKan1("%s\n","[(chattr) num symbol] extension.");
344: obj1 = getoa(obj,1);
345: obj2 = getoa(obj,2);
346: if (obj1.tag != Sinteger) errorKan1("%s\n","[(chattr) num symbol] extension.");
347: if (obj2.tag != Sstring) errorKan1("%s\n","[(chattr) num symbol] extension.");
348: m = KopInteger(obj1);
1.28 takayama 349: /* if (!( m == 0 || m == PROTECT || m == ABSOLUTE_PROTECT || m == ATTR_INFIX))
350: errorKan1("%s\n","The number must be 0, 1 or 2.");*/
1.29 takayama 351: putUserDictionary2(obj2.lc.str,(obj2.rc.op->lc).ival,(obj2.rc.op->rc).ival,
352: m,CurrentContextp->userDictionary);
353: }else if (strcmp(key,"or_attr")==0) {
354: if (size != 3) errorKan1("%s\n","[(or_attr) num symbol] extension.");
355: obj1 = getoa(obj,1);
356: obj2 = getoa(obj,2);
357: if (obj1.tag != Sinteger) errorKan1("%s\n","[(or_attr) num symbol] extension.");
358: if (obj2.tag != Sstring) errorKan1("%s\n","[(or_attr) num symbol] extension.");
359: m = KopInteger(obj1);
360: rob = KfindUserDictionary(obj2.lc.str);
361: if (rob.tag != NoObject.tag) {
362: if (strcmp(UD_str,obj2.lc.str) == 0) {
363: m |= UD_attr;
364: }else errorKan1("%s\n","or_attr: internal error.");
365: }
366: rob = KpoInteger(m);
1.5 takayama 367: putUserDictionary2(obj2.lc.str,(obj2.rc.op->lc).ival,(obj2.rc.op->rc).ival,
368: m,CurrentContextp->userDictionary);
1.27 takayama 369: }else if (strcmp(key,"getattr")==0) {
370: if (size != 2) errorKan1("%s\n","[(getattr) symbol] extension.");
371: obj1 = getoa(obj,1);
372: if (obj1.tag != Sstring) errorKan1("%s\n","[(getattr) symbol] extension.");
373: rob = KfindUserDictionary(obj1.lc.str);
374: if (rob.tag != NoObject.tag) {
375: if (strcmp(UD_str,obj1.lc.str) == 0) {
376: rob = KpoInteger(UD_attr);
377: }else errorKan1("%s\n","getattr: internal error.");
378: }else rob = NullObject;
1.15 takayama 379: }else if (strcmp(key,"getServerEnv")==0) {
380: if (size != 2) errorKan1("%s\n","[(getServerEnv) serverName] extension.");
381: obj1 = getoa(obj,1);
382: if (obj1.tag != Sdollar) errorKan1("%s\n","[(getServerEnv) serverName] extension.");
383: {
384: char **se; int ii; int nn;
385: se = getServerEnv(KopString(obj1));
386: if (se == NULL) {
387: debugServerEnv(KopString(obj1));
388: rob = NullObject;
389: }else{
390: for (ii=0,nn=0; se[ii] != NULL; ii++) nn++;
391: rob = newObjectArray(nn);
392: for (ii=0; ii<nn; ii++) {
393: putoa(rob,ii,KpoString(se[ii]));
394: }
395: }
396: }
1.33 takayama 397: }else if (strcmp(key,"read")==0) {
398: if (size != 3) errorKan1("%s\n","[(read) fd size] extension.");
399: obj1 = getoa(obj,1);
400: if (obj1.tag != Sinteger) errorKan1("%s\n","[(read) fd size] extension. fd must be an integer.");
401: obj2 = getoa(obj,2);
402: if (obj2.tag != Sinteger) errorKan1("%s\n","[(read) fd size] extension. size must be an integer.");
403: {
404: int total, n, fd;
405: char *s; char *s0;
406: fd = KopInteger(obj1);
407: total = KopInteger(obj2);
408: if (total <= 0) errorKan1("%s\n","[(read) ...]; negative size has not yet been implemented.");
409: /* Return a string. todo: implement SbyteArray case. */
410: s0 = s = (char *) sGC_malloc(total+1);
411: if (s0 == NULL) errorKan1("%s\n","[(read) ...]; no more memory.");
412: while (total >0) {
413: n = read(fd, s, total);
414: if (n < 0) { perror("read"); errorKan1("%s\n","[(read) ...]; read error.");}
415: s[n] = 0;
416: total -= n; s = &(s[n]);
417: }
418: rob = KpoString(s0);
419: }
1.12 takayama 420: }else if (strcmp(key,"regionMatches")==0) {
421: if (size != 3) errorKan1("%s\n","[(regionMatches) str strArray] extension.");
422: obj1 = getoa(obj,1);
423: if (obj1.tag != Sdollar) errorKan1("%s\n","[(regionMatches) str strArray] extension. str must be a string.");
424: obj2 = getoa(obj,2);
425: if (obj2.tag != Sarray) errorKan1("%s\n","[(regionMatches) str strArray] extension. strArray must be an array.");
426: rob = KregionMatches(obj1,obj2);
1.16 takayama 427: }else if (strcmp(key,"newVector")==0) {
428: if (size != 2) errorKan1("%s\n","[(newVector) m] extension.");
429: obj1 = getoa(obj,1);
430: if (obj1.tag != Sinteger) errorKan1("%s\n","[(newVector) m] extension. m must be an integer.");
431: rob = newObjectArray(KopInteger(obj1));
432: }else if (strcmp(key,"newMatrix")==0) {
433: if (size != 3) errorKan1("%s\n","[(newMatrix) m n] extension.");
434: obj1 = getoa(obj,1);
435: if (obj1.tag != Sinteger) errorKan1("%s\n","[(newMatrix) m n] extension. m must be an integer.");
436: obj2 = getoa(obj,2);
437: if (obj2.tag != Sinteger) errorKan1("%s\n","[(newMatrix) m n] extension. n must be an integer.");
438: rob = newObjectArray(KopInteger(obj1));
439: for (i=0; i<KopInteger(obj1); i++) {
440: putoa(rob,i,newObjectArray(KopInteger(obj2)));
441: }
1.21 takayama 442: }else if (strcmp(key,"ooPower")==0) {
443: if (size != 3) errorKan1("%s\n","[(ooPower) a b] extension.");
444: obj1 = getoa(obj,1);
445: obj2 = getoa(obj,2);
446: rob = KooPower(obj1,obj2);
1.25 takayama 447: }else if (strcmp(key,"Krest")==0) {
448: if (size != 2) errorKan1("%s\n","[(Krest) a] extension b");
449: obj1 = getoa(obj,1);
450: rob = Krest(obj1);
451: }else if (strcmp(key,"Kjoin")==0) {
452: if (size != 3) errorKan1("%s\n","[(Kjoin) a b] extension c");
453: obj1 = getoa(obj,1);
454: obj2 = getoa(obj,2);
455: rob = Kjoin(obj1,obj2);
1.8 takayama 456: }else if (strcmp(key,"ostype")==0) {
457: rob = newObjectArray(1);
458: /* Hard encode the OS type. */
459: #if defined(__CYGWIN__)
460: putoa(rob,0,KpoString("windows"));
461: #else
462: putoa(rob,0,KpoString("unix"));
463: #endif
1.24 takayama 464: }else if (strcmp(key,"traceClearStack")==0) {
465: traceClearStack();
466: rob = NullObject;
467: }else if (strcmp(key,"traceShowStack")==0) {
468: char *ssst;
469: ssst = traceShowStack();
470: if (ssst != NULL) {
471: rob = KpoString(ssst);
472: }else{
473: rob = NullObject;
474: }
1.30 takayama 475: }else if (strcmp(key,"regexec")==0) {
476: if ((size != 3) && (size != 4)) errorKan1("%s\n","[(regexec) reg strArray flag(optional)] extension b");
477: obj1 = getoa(obj,1);
478: if (obj1.tag != Sdollar) errorKan1("%s\n","regexec, the first argument should be a string (regular expression).");
479: obj2 = getoa(obj,2);
480: if (obj2.tag != Sarray) errorKan1("%s\n","regexec, the second argument should be an array of a string.");
481: if (size == 3) obj3 = newObjectArray(0);
482: else obj3 = getoa(obj,3);
483: rob = oregexec(obj1,obj2,obj3);
1.34 takayama 484: }else if (strcmp(key,"unlink")==0) {
485: if (size != 2) errorKan1("%s\n","[(unlink) filename] extension b");
486: obj1 = getoa(obj,1);
487: if (obj1.tag != Sdollar) errorKan1("%s\n","unlink, the first argument should be a string (filename).");
488: rob = KpoInteger(oxDeleteFile(KopString(obj1)));
1.5 takayama 489: }
1.1 maekawa 490: #include "plugin.hh"
1.17 takayama 491: #include "Kclass/tree.hh"
1.1 maekawa 492: else{
1.33 takayama 493: fprintf(stderr,"key=%s; ",key);
494: errorKan1("%s\n","Unknown key for extension.");
1.1 maekawa 495: }
496:
497:
498: return(rob);
1.12 takayama 499: }
500:
501: struct object KregionMatches(struct object sobj, struct object keyArray)
502: {
503: struct object rob;
504: int n,i,j,m,keyn;
505: char *s,*key;
506: rob = newObjectArray(3);
507: getoa(rob,0) = KpoInteger(-1);
508: getoa(rob,1) = NullObject;
509: getoa(rob,2) = NullObject;
510:
511: if (sobj.tag != Sdollar) return rob;
512: if (keyArray.tag != Sarray) return rob;
513: n = getoaSize(keyArray);
514: for (i=0; i<n; i++) {
515: if (getoa(keyArray,i).tag != Sdollar) { return rob; }
516: }
517:
518: s = KopString(sobj);
519: m = strlen(s);
520:
521: for (i=0; i<n; i++) {
522: key = KopString(getoa(keyArray,i));
523: keyn = strlen(key);
524: for (j=0; j<m; j++) {
525: if (strncmp(&(s[j]),key,keyn) == 0) {
526: getoa(rob,0) = KpoInteger(j);
527: getoa(rob,1) = KpoString(key);
528: getoa(rob,2) = KpoInteger(i);
529: return rob;
530: }
531: }
532: }
533: return rob;
1.1 maekawa 534: }
535:
1.30 takayama 536: static struct object oregexec(struct object oregex,struct object ostrArray,struct object oflag) {
537: struct object rob;
538: struct object ob;
539: int n,i,j,m,keyn,cflag,eflag,er;
540: char *regex;
541: regex_t preg;
542: char *s;
543: char *mbuf; int mbufSize;
544: #define REGMATCH_SIZE 100
545: regmatch_t pmatch[100]; size_t nmatch;
546: int size;
547:
548: nmatch = (size_t) REGMATCH_SIZE;
549: rob = newObjectArray(0);
550: mbufSize = 1024;
551:
552: if (oregex.tag != Sdollar) return rob;
553: if (ostrArray.tag != Sarray) return rob;
554: n = getoaSize(ostrArray);
555: for (i=0; i<n; i++) {
556: if (getoa(ostrArray,i).tag != Sdollar) { return rob; }
557: }
558: if (oflag.tag != Sarray) errorKan1("%s\n","oregexec: oflag should be an array of integers.");
559: cflag = eflag = 0;
560: oflag = Kto_int32(oflag);
561: for (i=0; i<getoaSize(oflag); i++) {
562: ob = getoa(oflag,i);
563: if (ob.tag != Sinteger) errorKan1("%s\n","oregexec: oflag is not an array of integers.");
564: if (i == 0) cflag = KopInteger(ob);
565: if (i == 1) eflag = KopInteger(ob);
566: }
567:
568: regex = KopString(oregex);
569: if (er=regcomp(&preg,regex,cflag)) {
570: mbuf = (char *) sGC_malloc(mbufSize);
571: if (mbuf == NULL) errorKan1("%s\n","No more memory.");
572: regerror(er,&preg,mbuf,mbufSize-1);
573: errorKan1("regcomp error: %s\n",mbuf);
574: }
575:
576: size = 0; /* We should use list instead of counting the size. */
577: for (i=0; i<n; i++) {
578: s = KopString(getoa(ostrArray,i));
579: er=regexec(&preg,s,nmatch,pmatch,eflag);
580: if ((er != 0) && (er != REG_NOMATCH)) {
581: mbuf = (char *) sGC_malloc(mbufSize);
582: if (mbuf == NULL) errorKan1("%s\n","No more memory.");
583: regerror(er,&preg,mbuf,mbufSize-1);
584: errorKan1("regcomp error: %s\n",mbuf);
585: }
586: if (er == 0) size++;
587: }
588:
589: rob = newObjectArray(size);
590: size = 0;
591: for (i=0; i<n; i++) {
592: s = KopString(getoa(ostrArray,i));
593: er=regexec(&preg,s,nmatch,pmatch,eflag);
594: if ((er != 0) && (er != REG_NOMATCH)) {
595: mbuf = (char *) sGC_malloc(mbufSize);
596: if (mbuf == NULL) errorKan1("%s\n","No more memory.");
597: regerror(er,&preg,mbuf,mbufSize-1);
598: errorKan1("regcomp error: %s\n",mbuf);
599: }
600: if (er == 0) {
601: ob = newObjectArray(3);
602: putoa(ob,0,KpoString(s));
603: /* temporary */
604: putoa(ob,1,KpoInteger((int) (pmatch[0].rm_so)));
605: putoa(ob,2,KpoInteger((int) (pmatch[0].rm_eo)));
606: putoa(rob,size,ob);
607: size++;
608: }
609: }
610: regfree(&preg);
611: return rob;
612: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>