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

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

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