[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.30

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

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