[BACK]Return to ext.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kan96xx / Kan

Annotation of OpenXM/src/kan96xx/Kan/ext.c, Revision 1.31

1.31    ! takayama    1: /* $OpenXM: OpenXM/src/kan96xx/Kan/ext.c,v 1.30 2004/09/14 01:57:15 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:     }
                    221:   }else if (strcmp(key,"forkExec")==0) {
                    222:     if (size != 4) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension.");
                    223:     obj1 = getoa(obj,1);
1.14      takayama  224:        if (obj1.tag == Sdollar) {
                    225:          obj1 = KstringToArgv(obj1);
                    226:        }
1.5       takayama  227:     if (obj1.tag != Sarray) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension. array argList.");
1.14      takayama  228:        obj2 = getoa(obj,2);
1.5       takayama  229:     if (obj2.tag != Sarray) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension. array fdList.");
                    230:     obj3 = getoa(obj,3);
                    231:     if (obj3.tag != Sinteger) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension. integer sigblock.");
1.7       takayama  232:     m = KopInteger(obj3);  /* m&1 : block ctrl-C. */
1.5       takayama  233:     argListc = getoaSize(obj1);
                    234:     fdListc =  getoaSize(obj2);
1.6       takayama  235:     if ((pid = fork()) > 0) {
1.5       takayama  236:       /* parent */
1.7       takayama  237:       if (m&2) {
1.6       takayama  238:          /* Do not call singal to turn around a trouble on cygwin. BUG. */
                    239:       }else{
                    240:          signal(SIGCHLD,mywait); /* to kill Zombie */
                    241:       }
                    242:       Mychildren[Mycp++] = pid;
1.5       takayama  243:       if (Mycp >= MYCP_SIZE-1) {
                    244:         errorKan1("%s\n","Child process table is full.\n");
                    245:         Mycp = 0;
                    246:       }
1.6       takayama  247:       rob = KpoInteger(pid);
1.5       takayama  248:       /* Done */
                    249:     }else{
                    250:       /* Child */
                    251:       for (i=0; i<fdListc; i++) {
                    252:         /* close the specified files */
                    253:         close(KopInteger(getoa(obj2,i)));
1.1       maekawa   254:       }
1.5       takayama  255:       /* execl */
1.7       takayama  256:       if (m&1) {
1.5       takayama  257:         {
                    258:           sigset_t sss;
                    259:           sigemptyset(&sss);
                    260:           sigaddset(&sss,SIGINT);
                    261:           sigprocmask(SIG_BLOCK,&sss,NULL);
                    262:         }
1.1       maekawa   263:       }
1.5       takayama  264:       argv = (char **) sGC_malloc(sizeof(char *)*(argListc+1));
                    265:       if (argv == NULL) {
                    266:         fprintf(stderr," no more momory. forkExec --- exiting.\n");
                    267:         _exit(10);
1.1       maekawa   268:       }
1.5       takayama  269:       for (i=0; i<argListc; i++) {
                    270:         argv[i] = KopString(getoa(obj1,i));
                    271:         argv[i+1] = NULL;
1.7       takayama  272:       }
                    273:
                    274:       if (m&4) {
                    275:        fprintf(stderr,"execv %s\n",argv[0]);
                    276:        sleep(5);
                    277:         fprintf(stderr,">>>\n");
1.1       maekawa   278:       }
1.14      takayama  279:       execve(argv[0],argv,environ);
1.5       takayama  280:       /* This place will never be reached unless execv fails. */
                    281:       fprintf(stderr,"forkExec fails: ");
                    282:       for (i=0; i<argListc; i++) {
                    283:         fprintf(stderr,"%s ",argv[i]);
                    284:       }
                    285:       fprintf(stderr,"\nExiting, but staying as Zombie.\n");
                    286:       _exit(10);
                    287:     }
                    288:   }else if (strcmp(key,"getchild")==0) {
                    289:     if (size != 1) errorKan1("%s\n","[(getchild)] extension.");
                    290:     rob = newObjectArray(Mycp);
                    291:     for (i=0; i<Mycp; i++) {
                    292:       putoa(rob,i,KpoInteger(Mychildren[i]));
1.1       maekawa   293:     }
1.5       takayama  294:   }else if (strcmp(key,"getUniqueFileName")==0) {
                    295:     if (size != 2) errorKan1("%s\n","[(getUniqueFileName) path] extension.");
                    296:     obj1 = getoa(obj,1);
                    297:     if (obj1.tag != Sdollar) errorKan1("%s\n","[(getUniqueFileName) path] extension. path must be a string.");
                    298:     rob = KpoString(ext_generateUniqueFileName(KopString(obj1)));
                    299:   }else if (strcmp(key,"outputObjectToFile")==0) {
                    300:     if (size != 3) errorKan1("%s\n","[(outputObjectToFile) path obj] extension.");
                    301:     obj1 = getoa(obj,1);
                    302:     if (obj1.tag != Sdollar) errorKan1("%s\n","[(outputObjectToFile) path obj] extension. path must be a string.");
                    303:     obj2 = getoa(obj,2);
                    304:     fp = fopen(KopString(obj1),"w");
                    305:     if (fp == NULL) errorKan1("%s\n","[(outputObjectToFile) path object] extension : could not open the path.");
                    306:     printObject(obj2,0,fp);
                    307:     fclose(fp);
                    308:     rob = NullObject;
                    309:   }else if (strcmp(key,"hilbert")==0) {
                    310:     if (size != 3) errorKan1("%s\n","[(hilbert) obgb obvlist] extension.");
                    311:     rob = hilberto(getoa(obj,1),getoa(obj,2));
1.13      takayama  312:   }else if (strcmp(key,"nobody") == 0) {
                    313:       uid = getuid();
                    314:          if (uid == 0) {
                    315: #define NOBODY 65534
                    316:                /* If I'm a super user, then change uid to nobody. */
                    317:                if (setuid(NOBODY) != 0) {
                    318:                  fprintf(stderr,"Failed to change uid to nobody (%d)\n",NOBODY);
                    319:                  exit(10);
                    320:                }
                    321:                fprintf(stderr,"uid is changed to nobody (%d).\n",NOBODY);
                    322:                rob.tag = Snull;
                    323:          }
1.5       takayama  324:   }else if (strcmp(key,"chattr")==0) {
                    325:     if (size != 3) errorKan1("%s\n","[(chattr)  num symbol] extension.");
                    326:     obj1 = getoa(obj,1);
                    327:     obj2 = getoa(obj,2);
                    328:     if (obj1.tag != Sinteger) errorKan1("%s\n","[(chattr)  num symbol] extension.");
                    329:     if (obj2.tag != Sstring)  errorKan1("%s\n","[(chattr)  num symbol] extension.");
                    330:     m = KopInteger(obj1);
1.28      takayama  331:        /* if (!( m == 0 || m == PROTECT || m == ABSOLUTE_PROTECT || m == ATTR_INFIX))
                    332:           errorKan1("%s\n","The number must be 0, 1 or 2.");*/
1.29      takayama  333:     putUserDictionary2(obj2.lc.str,(obj2.rc.op->lc).ival,(obj2.rc.op->rc).ival,
                    334:                        m,CurrentContextp->userDictionary);
                    335:   }else if (strcmp(key,"or_attr")==0) {
                    336:     if (size != 3) errorKan1("%s\n","[(or_attr)  num symbol] extension.");
                    337:     obj1 = getoa(obj,1);
                    338:     obj2 = getoa(obj,2);
                    339:     if (obj1.tag != Sinteger) errorKan1("%s\n","[(or_attr)  num symbol] extension.");
                    340:     if (obj2.tag != Sstring)  errorKan1("%s\n","[(or_attr)  num symbol] extension.");
                    341:     m = KopInteger(obj1);
                    342:     rob = KfindUserDictionary(obj2.lc.str);
                    343:     if (rob.tag != NoObject.tag) {
                    344:       if (strcmp(UD_str,obj2.lc.str) == 0) {
                    345:         m |= UD_attr;
                    346:       }else errorKan1("%s\n","or_attr: internal error.");
                    347:     }
                    348:     rob = KpoInteger(m);
1.5       takayama  349:     putUserDictionary2(obj2.lc.str,(obj2.rc.op->lc).ival,(obj2.rc.op->rc).ival,
                    350:                        m,CurrentContextp->userDictionary);
1.27      takayama  351:   }else if (strcmp(key,"getattr")==0) {
                    352:     if (size != 2) errorKan1("%s\n","[(getattr) symbol] extension.");
                    353:     obj1 = getoa(obj,1);
                    354:     if (obj1.tag != Sstring)  errorKan1("%s\n","[(getattr) symbol] extension.");
                    355:     rob = KfindUserDictionary(obj1.lc.str);
                    356:        if (rob.tag != NoObject.tag) {
                    357:          if (strcmp(UD_str,obj1.lc.str) == 0) {
                    358:                rob = KpoInteger(UD_attr);
                    359:          }else errorKan1("%s\n","getattr: internal error.");
                    360:        }else rob = NullObject;
1.15      takayama  361:   }else if (strcmp(key,"getServerEnv")==0) {
                    362:     if (size != 2) errorKan1("%s\n","[(getServerEnv) serverName] extension.");
                    363:     obj1 = getoa(obj,1);
                    364:     if (obj1.tag != Sdollar) errorKan1("%s\n","[(getServerEnv) serverName] extension.");
                    365:     {
                    366:       char **se; int ii; int nn;
                    367:       se = getServerEnv(KopString(obj1));
                    368:       if (se == NULL) {
                    369:         debugServerEnv(KopString(obj1));
                    370:         rob = NullObject;
                    371:       }else{
                    372:         for (ii=0,nn=0; se[ii] != NULL; ii++) nn++;
                    373:         rob = newObjectArray(nn);
                    374:         for (ii=0; ii<nn; ii++) {
                    375:           putoa(rob,ii,KpoString(se[ii]));
                    376:         }
                    377:       }
                    378:     }
1.12      takayama  379:   }else if (strcmp(key,"regionMatches")==0) {
                    380:     if (size != 3) errorKan1("%s\n","[(regionMatches) str strArray] extension.");
                    381:     obj1 = getoa(obj,1);
                    382:     if (obj1.tag != Sdollar) errorKan1("%s\n","[(regionMatches) str strArray] extension. str must be a string.");
                    383:        obj2 = getoa(obj,2);
                    384:     if (obj2.tag != Sarray) errorKan1("%s\n","[(regionMatches) str strArray] extension. strArray must be an array.");
                    385:     rob = KregionMatches(obj1,obj2);
1.16      takayama  386:   }else if (strcmp(key,"newVector")==0) {
                    387:     if (size != 2) errorKan1("%s\n","[(newVector) m] extension.");
                    388:     obj1 = getoa(obj,1);
                    389:     if (obj1.tag != Sinteger) errorKan1("%s\n","[(newVector) m] extension. m must be an integer.");
                    390:     rob = newObjectArray(KopInteger(obj1));
                    391:   }else if (strcmp(key,"newMatrix")==0) {
                    392:     if (size != 3) errorKan1("%s\n","[(newMatrix) m n] extension.");
                    393:     obj1 = getoa(obj,1);
                    394:     if (obj1.tag != Sinteger) errorKan1("%s\n","[(newMatrix) m n] extension. m must be an integer.");
                    395:        obj2 = getoa(obj,2);
                    396:     if (obj2.tag != Sinteger) errorKan1("%s\n","[(newMatrix) m n] extension. n must be an integer.");
                    397:     rob = newObjectArray(KopInteger(obj1));
                    398:        for (i=0; i<KopInteger(obj1); i++) {
                    399:       putoa(rob,i,newObjectArray(KopInteger(obj2)));
                    400:        }
1.21      takayama  401:   }else if (strcmp(key,"ooPower")==0) {
                    402:     if (size != 3) errorKan1("%s\n","[(ooPower) a b] extension.");
                    403:     obj1 = getoa(obj,1);
                    404:        obj2 = getoa(obj,2);
                    405:     rob = KooPower(obj1,obj2);
1.25      takayama  406:   }else if (strcmp(key,"Krest")==0) {
                    407:     if (size != 2) errorKan1("%s\n","[(Krest) a] extension b");
                    408:     obj1 = getoa(obj,1);
                    409:     rob = Krest(obj1);
                    410:   }else if (strcmp(key,"Kjoin")==0) {
                    411:     if (size != 3) errorKan1("%s\n","[(Kjoin) a b] extension c");
                    412:     obj1 = getoa(obj,1);
                    413:        obj2 = getoa(obj,2);
                    414:     rob = Kjoin(obj1,obj2);
1.8       takayama  415:   }else if (strcmp(key,"ostype")==0) {
                    416:     rob = newObjectArray(1);
                    417:     /* Hard encode the OS type. */
                    418: #if defined(__CYGWIN__)
                    419:     putoa(rob,0,KpoString("windows"));
                    420: #else
                    421:     putoa(rob,0,KpoString("unix"));
                    422: #endif
1.24      takayama  423:   }else if (strcmp(key,"traceClearStack")==0) {
                    424:     traceClearStack();
                    425:     rob = NullObject;
                    426:   }else if (strcmp(key,"traceShowStack")==0) {
                    427:     char *ssst;
                    428:     ssst = traceShowStack();
                    429:     if (ssst != NULL) {
                    430:       rob = KpoString(ssst);
                    431:     }else{
                    432:       rob = NullObject;
                    433:     }
1.30      takayama  434:   }else if (strcmp(key,"regexec")==0) {
                    435:     if ((size != 3) && (size != 4)) errorKan1("%s\n","[(regexec) reg strArray flag(optional)] extension b");
                    436:     obj1 = getoa(obj,1);
                    437:     if (obj1.tag != Sdollar) errorKan1("%s\n","regexec, the first argument should be a string (regular expression).");
                    438:     obj2 = getoa(obj,2);
                    439:     if (obj2.tag != Sarray) errorKan1("%s\n","regexec, the second argument should be an array of a string.");
                    440:     if (size == 3) obj3 = newObjectArray(0);
                    441:     else obj3 = getoa(obj,3);
                    442:     rob = oregexec(obj1,obj2,obj3);
                    443:
1.5       takayama  444:   }
1.1       maekawa   445: #include "plugin.hh"
1.17      takayama  446: #include "Kclass/tree.hh"
1.1       maekawa   447:   else{
                    448:     errorKan1("%s\n","Unknown tag for extension.");
                    449:   }
                    450:
                    451:
                    452:   return(rob);
1.12      takayama  453: }
                    454:
                    455: struct object KregionMatches(struct object sobj, struct object keyArray)
                    456: {
                    457:   struct object rob;
                    458:   int n,i,j,m,keyn;
                    459:   char *s,*key;
                    460:   rob = newObjectArray(3);
                    461:   getoa(rob,0) = KpoInteger(-1);
                    462:   getoa(rob,1) = NullObject;
                    463:   getoa(rob,2) = NullObject;
                    464:
                    465:   if (sobj.tag != Sdollar) return rob;
                    466:   if (keyArray.tag != Sarray) return rob;
                    467:   n = getoaSize(keyArray);
                    468:   for (i=0; i<n; i++) {
                    469:        if (getoa(keyArray,i).tag != Sdollar) { return rob; }
                    470:   }
                    471:
                    472:   s = KopString(sobj);
                    473:   m = strlen(s);
                    474:
                    475:   for (i=0; i<n; i++) {
                    476:        key = KopString(getoa(keyArray,i));
                    477:        keyn = strlen(key);
                    478:        for (j=0; j<m; j++) {
                    479:          if (strncmp(&(s[j]),key,keyn) == 0) {
                    480:                getoa(rob,0) = KpoInteger(j);
                    481:                getoa(rob,1) = KpoString(key);
                    482:                getoa(rob,2) = KpoInteger(i);
                    483:                return rob;
                    484:          }
                    485:        }
                    486:   }
                    487:   return rob;
1.1       maekawa   488: }
                    489:
1.30      takayama  490: static struct object oregexec(struct object oregex,struct object ostrArray,struct object oflag) {
                    491:   struct object rob;
                    492:   struct object ob;
                    493:   int n,i,j,m,keyn,cflag,eflag,er;
                    494:   char *regex;
                    495:   regex_t preg;
                    496:   char *s;
                    497:   char *mbuf; int mbufSize;
                    498: #define REGMATCH_SIZE 100
                    499:   regmatch_t pmatch[100]; size_t nmatch;
                    500:   int size;
                    501:
                    502:   nmatch = (size_t) REGMATCH_SIZE;
                    503:   rob = newObjectArray(0);
                    504:   mbufSize = 1024;
                    505:
                    506:   if (oregex.tag != Sdollar) return rob;
                    507:   if (ostrArray.tag != Sarray) return rob;
                    508:   n = getoaSize(ostrArray);
                    509:   for (i=0; i<n; i++) {
                    510:     if (getoa(ostrArray,i).tag != Sdollar) { return rob; }
                    511:   }
                    512:   if (oflag.tag != Sarray) errorKan1("%s\n","oregexec: oflag should be an array of integers.");
                    513:   cflag = eflag = 0;
                    514:   oflag = Kto_int32(oflag);
                    515:   for (i=0; i<getoaSize(oflag); i++) {
                    516:     ob = getoa(oflag,i);
                    517:     if (ob.tag != Sinteger) errorKan1("%s\n","oregexec: oflag is not an array of integers.");
                    518:     if (i == 0) cflag = KopInteger(ob);
                    519:     if (i == 1) eflag = KopInteger(ob);
                    520:   }
                    521:
                    522:   regex = KopString(oregex);
                    523:   if (er=regcomp(&preg,regex,cflag)) {
                    524:     mbuf = (char *) sGC_malloc(mbufSize);
                    525:     if (mbuf == NULL) errorKan1("%s\n","No more memory.");
                    526:     regerror(er,&preg,mbuf,mbufSize-1);
                    527:     errorKan1("regcomp error: %s\n",mbuf);
                    528:   }
                    529:
                    530:   size = 0; /* We should use list instead of counting the size. */
                    531:   for (i=0; i<n; i++) {
                    532:     s = KopString(getoa(ostrArray,i));
                    533:     er=regexec(&preg,s,nmatch,pmatch,eflag);
                    534:     if ((er != 0) && (er != REG_NOMATCH)) {
                    535:       mbuf = (char *) sGC_malloc(mbufSize);
                    536:       if (mbuf == NULL) errorKan1("%s\n","No more memory.");
                    537:       regerror(er,&preg,mbuf,mbufSize-1);
                    538:       errorKan1("regcomp error: %s\n",mbuf);
                    539:     }
                    540:     if (er == 0) size++;
                    541:   }
                    542:
                    543:   rob = newObjectArray(size);
                    544:   size = 0;
                    545:   for (i=0; i<n; i++) {
                    546:     s = KopString(getoa(ostrArray,i));
                    547:     er=regexec(&preg,s,nmatch,pmatch,eflag);
                    548:     if ((er != 0) && (er != REG_NOMATCH)) {
                    549:       mbuf = (char *) sGC_malloc(mbufSize);
                    550:       if (mbuf == NULL) errorKan1("%s\n","No more memory.");
                    551:       regerror(er,&preg,mbuf,mbufSize-1);
                    552:       errorKan1("regcomp error: %s\n",mbuf);
                    553:     }
                    554:     if (er == 0) {
                    555:       ob = newObjectArray(3);
                    556:       putoa(ob,0,KpoString(s));
                    557:       /* temporary */
                    558:       putoa(ob,1,KpoInteger((int) (pmatch[0].rm_so)));
                    559:       putoa(ob,2,KpoInteger((int) (pmatch[0].rm_eo)));
                    560:       putoa(rob,size,ob);
                    561:       size++;
                    562:     }
                    563:   }
                    564:   regfree(&preg);
                    565:   return rob;
                    566: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>