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

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

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