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

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

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