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

1.24    ! takayama    1: /* $OpenXM: OpenXM/src/kan96xx/Kan/ext.c,v 1.23 2004/02/28 13:39:42 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.15      takayama   18: #include "ox_pathfinder.h"
1.1       maekawa    19:
1.23      takayama   20: extern int Quiet;
1.14      takayama   21: extern char **environ;
                     22:
1.1       maekawa    23: #define MYCP_SIZE 100
                     24: static int Mychildren[MYCP_SIZE];
                     25: static int Mycp = 0;
                     26: static void mywait() {
                     27:   int status;
                     28:   int pid;
                     29:   int i,j;
1.20      takayama   30:   /* signal(SIGCHLD,SIG_IGN); */
1.1       maekawa    31:   pid = wait(&status);
1.23      takayama   32:   if (!Quiet) fprintf(stderr,"Child process %d is exiting.\n",pid);
1.1       maekawa    33:   for (i=0; i<Mycp; i++) {
                     34:     if (Mychildren[i]  == pid) {
                     35:       for (j=i; j<Mycp-1; j++) {
1.5       takayama   36:         Mychildren[j] = Mychildren[j+1];
1.1       maekawa    37:       }
                     38:       if (Mycp > 0) Mycp--;
                     39:     }
                     40:   }
                     41:   signal(SIGCHLD,mywait);
                     42: }
                     43:
                     44: #define SIZE_OF_ENVSTACK 5
1.9       takayama   45: #if defined(__CYGWIN__)
                     46: #define JMP_BUF sigjmp_buf
                     47: #else
                     48: #define JMP_BUF jmp_buf
                     49: #endif
                     50: static JMP_BUF EnvStack[SIZE_OF_ENVSTACK];
1.1       maekawa    51: static int Envp = 0;
1.9       takayama   52: static void pushEnv(JMP_BUF jb) {
1.1       maekawa    53:   if (Envp < SIZE_OF_ENVSTACK) {
                     54:     *(EnvStack[Envp]) = *jb;
                     55:     Envp++;
                     56:   }else{
                     57:     fprintf(stderr,"Overflow of  EnvStack.\n");
                     58:     exit(2);
                     59:   }
                     60: }
1.9       takayama   61: static void popEnv(JMP_BUF jbp) {
1.1       maekawa    62:   if (Envp <= 0) {
                     63:     fprintf(stderr,"Underflow of EnvStack.\n");
                     64:     exit(3);
                     65:   }else{
                     66:     Envp--;
                     67:     *jbp = *EnvStack[Envp];
                     68:   }
                     69: }
                     70:
                     71: static char *ext_generateUniqueFileName(char *s)
                     72: {
                     73:   char *t;
                     74:   int i;
                     75:   struct stat statbuf;
                     76:   t = (char *)sGC_malloc(sizeof(char)*strlen(s)+4+2);
                     77:   for (i=0; i<1000; i++) {
                     78:     /* Give up if we failed for 1000 names. */
                     79:     sprintf(t,"%s.%d",s,i);
                     80:     /* if (phc_overwrite) return(t); */
                     81:     if (stat(t,&statbuf) < 0) {
                     82:       return(t);
                     83:     }
                     84:   }
                     85:   errorKan1("%s\n","ext_generateUniqueFileName: could not generate a unique file name. Exhausted all the names.");
                     86:   return(NULL);
                     87: }
                     88:
                     89: struct object Kextension(struct object obj)
                     90: {
                     91:   char *key;
                     92:   int size;
                     93:   struct object keyo;
                     94:   struct object rob = NullObject;
                     95:   struct object obj1,obj2,obj3,obj4;
1.13      takayama   96:   int m,i,pid, uid;
1.1       maekawa    97:   int argListc, fdListc;
                     98:   char *abc;
                     99:   char *abc2;
                    100:   extern struct context *CurrentContextp;
1.9       takayama  101: #if (__CYGWIN__)
                    102:   extern sigjmp_buf EnvOfStackMachine;
                    103: #else
1.1       maekawa   104:   extern jmp_buf EnvOfStackMachine;
1.9       takayama  105: #endif
1.1       maekawa   106:   extern void ctrlC();
                    107:   extern int SigIgn;
                    108:   extern int DebugCMO;
                    109:   extern int OXprintMessage;
                    110:   struct stat buf;
                    111:   char **argv;
                    112:   FILE *fp;
1.3       takayama  113:   void (*oldsig)();
                    114:   extern SecureMode;
1.1       maekawa   115:
                    116:   if (obj.tag != Sarray) errorKan1("%s\n","Kextension(): The argument must be an array.");
                    117:   size = getoaSize(obj);
                    118:   if (size < 1) errorKan1("%s\n","Kextension(): Empty array.");
                    119:   keyo = getoa(obj,0);
                    120:   if (keyo.tag != Sdollar) errorKan1("%s\n","Kextension(): No key word.");
                    121:   key = KopString(keyo);
                    122:
                    123:   /* branch by they key word. */
                    124:   if (strcmp(key,"parse")==0) {
                    125:     if (size != 2) errorKan1("%s\n","[(parse)  string] extension.");
                    126:     obj1 = getoa(obj,1);
                    127:     if (obj1.tag != Sdollar) errorKan1("%s\n","[(parse)  string] extension");
                    128:
                    129:     pushEnv(EnvOfStackMachine);
                    130:     m = KSexecuteString(obj1.lc.str);
                    131:     /* This is critical area. If you catch ctrl-c here, program crashes. */
                    132:     oldsig = signal(SIGINT,SIG_IGN);
                    133:     popEnv(EnvOfStackMachine);
                    134:     /* OK! We passed the critical area. */
                    135:     signal(SIGINT,oldsig);
                    136:     rob = KpoInteger(m);
                    137:   }else if (strcmp(key,"getpid") == 0) {
                    138:     rob = KpoInteger( (int) getpid() );
                    139:   }else if (strcmp(key,"flush") == 0) {
                    140:     /* fflush(NULL); */
                    141:     fflush(stdout);
                    142:     rob.tag = Snull;
                    143:   }else if (strcmp(key,"chattrs")==0) {
                    144:     if (size != 2) errorKan1("%s\n","[(chattrs)  num] extension.");
                    145:     obj1 = getoa(obj,1);
                    146:     if (obj1.tag != Sinteger) errorKan1("%s\n","[(chattrs)  num] extension.");
                    147:     m = KopInteger(obj1);
                    148:     if (!( m == 0 || m == PROTECT || m == ABSOLUTE_PROTECT))
                    149:       errorKan1("%s\n","The number must be 0, 1 or 2.");
                    150:     putUserDictionary2((char *)NULL,0,0,m | SET_ATTR_FOR_ALL_WORDS,
1.5       takayama  151:                        CurrentContextp->userDictionary);
1.1       maekawa   152:   }else if (strcmp(key,"keywords")==0) {
                    153:     if (size != 1) errorKan1("%s\n","[(keywords)] extension.");
                    154:     rob = showSystemDictionary(1);
                    155:     /*  }else if (strcmp(key,"fork0")==0) {
1.5       takayama  156:         if (size != 2) errorKan1("%s\n","[(fork0) sss] extension.");
                    157:         m = fork();
                    158:         if (m>0) { rob = KpoInteger(m); }
                    159:         else {
                    160:         system(KopString(getoa(obj,1))); exit(0);
                    161:         } */
                    162:   }else if (strcmp(key,"defaultPolyRing")==0) {
                    163:     if (size != 2) errorKan1("%s\n","[(defaultPolyRing) n] extension.");
                    164:     rob = KdefaultPolyRing(getoa(obj,1));
                    165:   }else if (strcmp(key,"getenv")==0) {
                    166:     if (size != 2) errorKan1("%s\n","[(getenv) envstr] extension.");
                    167:     obj1 = getoa(obj,1);
                    168:     if (obj1.tag != Sdollar) errorKan1("%s\n","[(getenv) envstr] extension");
                    169:     abc = getenv(KopString(obj1));
1.10      takayama  170: #if defined(__CYGWIN__)
                    171:     if (abc == NULL) {
1.11      takayama  172:          abc2 = (char *)sGC_malloc(sizeof(char)*(strlen(KopString(obj1))+2));
1.10      takayama  173:       strcpy(abc2,KopString(obj1));
                    174:          for (i=0; i<strlen(abc2); i++) {
                    175:                abc2[i] = toupper(abc2[i]);
                    176:          }
                    177:       abc = getenv(abc2);
                    178:     }
                    179: #endif
1.5       takayama  180:     if (abc == NULL) {
                    181:       rob = NullObject;
                    182:     }else{
                    183:       abc2 = (char *)sGC_malloc(sizeof(char)*(strlen(abc)+2));
                    184:       strcpy(abc2,abc);
                    185:       rob = KpoString(abc2);
                    186:     }
                    187:   }else if (strcmp(key,"stat")==0) {
                    188:     if (size != 2) errorKan1("%s\n","[(stat) fname] extension.");
                    189:     obj1 = getoa(obj,1);
                    190:     if (obj1.tag != Sdollar) errorKan1("%s\n","[(stat) fname] extension ; string fname.");
                    191:     m = stat(KopString(obj1),&buf);
                    192:     rob = newObjectArray(2);
                    193:     if (m == -1) {
                    194:       /* fail */
                    195:       obj2 = NullObject;
                    196:       putoa(rob,0,obj2);
                    197:       obj3 = newObjectArray(2);
                    198:       putoa(obj3,0,KpoString("error no"));
                    199:       putoa(obj3,1,KpoInteger(errno));
                    200:       putoa(rob,1,obj3);
                    201:     }else{
                    202:       /* success */
                    203:       putoa(rob,0,KpoInteger(0));
                    204:       obj3 = newObjectArray(1);
                    205:       putoa(obj3,0,KpoInteger((int) buf.st_size));
                    206:       putoa(rob,1,obj3); /* We have not yet read buf fully */
                    207:     }
                    208:   }else if (strcmp(key,"forkExec")==0) {
                    209:     if (size != 4) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension.");
                    210:     obj1 = getoa(obj,1);
1.14      takayama  211:        if (obj1.tag == Sdollar) {
                    212:          obj1 = KstringToArgv(obj1);
                    213:        }
1.5       takayama  214:     if (obj1.tag != Sarray) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension. array argList.");
1.14      takayama  215:        obj2 = getoa(obj,2);
1.5       takayama  216:     if (obj2.tag != Sarray) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension. array fdList.");
                    217:     obj3 = getoa(obj,3);
                    218:     if (obj3.tag != Sinteger) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension. integer sigblock.");
1.7       takayama  219:     m = KopInteger(obj3);  /* m&1 : block ctrl-C. */
1.5       takayama  220:     argListc = getoaSize(obj1);
                    221:     fdListc =  getoaSize(obj2);
1.6       takayama  222:     if ((pid = fork()) > 0) {
1.5       takayama  223:       /* parent */
1.7       takayama  224:       if (m&2) {
1.6       takayama  225:          /* Do not call singal to turn around a trouble on cygwin. BUG. */
                    226:       }else{
                    227:          signal(SIGCHLD,mywait); /* to kill Zombie */
                    228:       }
                    229:       Mychildren[Mycp++] = pid;
1.5       takayama  230:       if (Mycp >= MYCP_SIZE-1) {
                    231:         errorKan1("%s\n","Child process table is full.\n");
                    232:         Mycp = 0;
                    233:       }
1.6       takayama  234:       rob = KpoInteger(pid);
1.5       takayama  235:       /* Done */
                    236:     }else{
                    237:       /* Child */
                    238:       for (i=0; i<fdListc; i++) {
                    239:         /* close the specified files */
                    240:         close(KopInteger(getoa(obj2,i)));
1.1       maekawa   241:       }
1.5       takayama  242:       /* execl */
1.7       takayama  243:       if (m&1) {
1.5       takayama  244:         {
                    245:           sigset_t sss;
                    246:           sigemptyset(&sss);
                    247:           sigaddset(&sss,SIGINT);
                    248:           sigprocmask(SIG_BLOCK,&sss,NULL);
                    249:         }
1.1       maekawa   250:       }
1.5       takayama  251:       argv = (char **) sGC_malloc(sizeof(char *)*(argListc+1));
                    252:       if (argv == NULL) {
                    253:         fprintf(stderr," no more momory. forkExec --- exiting.\n");
                    254:         _exit(10);
1.1       maekawa   255:       }
1.5       takayama  256:       for (i=0; i<argListc; i++) {
                    257:         argv[i] = KopString(getoa(obj1,i));
                    258:         argv[i+1] = NULL;
1.7       takayama  259:       }
                    260:
                    261:       if (m&4) {
                    262:        fprintf(stderr,"execv %s\n",argv[0]);
                    263:        sleep(5);
                    264:         fprintf(stderr,">>>\n");
1.1       maekawa   265:       }
1.14      takayama  266:       execve(argv[0],argv,environ);
1.5       takayama  267:       /* This place will never be reached unless execv fails. */
                    268:       fprintf(stderr,"forkExec fails: ");
                    269:       for (i=0; i<argListc; i++) {
                    270:         fprintf(stderr,"%s ",argv[i]);
                    271:       }
                    272:       fprintf(stderr,"\nExiting, but staying as Zombie.\n");
                    273:       _exit(10);
                    274:     }
                    275:   }else if (strcmp(key,"getchild")==0) {
                    276:     if (size != 1) errorKan1("%s\n","[(getchild)] extension.");
                    277:     rob = newObjectArray(Mycp);
                    278:     for (i=0; i<Mycp; i++) {
                    279:       putoa(rob,i,KpoInteger(Mychildren[i]));
1.1       maekawa   280:     }
1.5       takayama  281:   }else if (strcmp(key,"getUniqueFileName")==0) {
                    282:     if (size != 2) errorKan1("%s\n","[(getUniqueFileName) path] extension.");
                    283:     obj1 = getoa(obj,1);
                    284:     if (obj1.tag != Sdollar) errorKan1("%s\n","[(getUniqueFileName) path] extension. path must be a string.");
                    285:     rob = KpoString(ext_generateUniqueFileName(KopString(obj1)));
                    286:   }else if (strcmp(key,"outputObjectToFile")==0) {
                    287:     if (size != 3) errorKan1("%s\n","[(outputObjectToFile) path obj] extension.");
                    288:     obj1 = getoa(obj,1);
                    289:     if (obj1.tag != Sdollar) errorKan1("%s\n","[(outputObjectToFile) path obj] extension. path must be a string.");
                    290:     obj2 = getoa(obj,2);
                    291:     fp = fopen(KopString(obj1),"w");
                    292:     if (fp == NULL) errorKan1("%s\n","[(outputObjectToFile) path object] extension : could not open the path.");
                    293:     printObject(obj2,0,fp);
                    294:     fclose(fp);
                    295:     rob = NullObject;
                    296:   }else if (strcmp(key,"hilbert")==0) {
                    297:     if (size != 3) errorKan1("%s\n","[(hilbert) obgb obvlist] extension.");
                    298:     rob = hilberto(getoa(obj,1),getoa(obj,2));
1.13      takayama  299:   }else if (strcmp(key,"nobody") == 0) {
                    300:       uid = getuid();
                    301:          if (uid == 0) {
                    302: #define NOBODY 65534
                    303:                /* If I'm a super user, then change uid to nobody. */
                    304:                if (setuid(NOBODY) != 0) {
                    305:                  fprintf(stderr,"Failed to change uid to nobody (%d)\n",NOBODY);
                    306:                  exit(10);
                    307:                }
                    308:                fprintf(stderr,"uid is changed to nobody (%d).\n",NOBODY);
                    309:                rob.tag = Snull;
                    310:          }
1.5       takayama  311:   }else if (strcmp(key,"chattr")==0) {
                    312:     if (size != 3) errorKan1("%s\n","[(chattr)  num symbol] extension.");
                    313:     obj1 = getoa(obj,1);
                    314:     obj2 = getoa(obj,2);
                    315:     if (obj1.tag != Sinteger) errorKan1("%s\n","[(chattr)  num symbol] extension.");
                    316:     if (obj2.tag != Sstring)  errorKan1("%s\n","[(chattr)  num symbol] extension.");
                    317:     m = KopInteger(obj1);
                    318:     if (!( m == 0 || m == PROTECT || m == ABSOLUTE_PROTECT))
                    319:       errorKan1("%s\n","The number must be 0, 1 or 2.");
                    320:     putUserDictionary2(obj2.lc.str,(obj2.rc.op->lc).ival,(obj2.rc.op->rc).ival,
                    321:                        m,CurrentContextp->userDictionary);
1.15      takayama  322:   }else if (strcmp(key,"getServerEnv")==0) {
                    323:     if (size != 2) errorKan1("%s\n","[(getServerEnv) serverName] extension.");
                    324:     obj1 = getoa(obj,1);
                    325:     if (obj1.tag != Sdollar) errorKan1("%s\n","[(getServerEnv) serverName] extension.");
                    326:     {
                    327:       char **se; int ii; int nn;
                    328:       se = getServerEnv(KopString(obj1));
                    329:       if (se == NULL) {
                    330:         debugServerEnv(KopString(obj1));
                    331:         rob = NullObject;
                    332:       }else{
                    333:         for (ii=0,nn=0; se[ii] != NULL; ii++) nn++;
                    334:         rob = newObjectArray(nn);
                    335:         for (ii=0; ii<nn; ii++) {
                    336:           putoa(rob,ii,KpoString(se[ii]));
                    337:         }
                    338:       }
                    339:     }
1.12      takayama  340:   }else if (strcmp(key,"regionMatches")==0) {
                    341:     if (size != 3) errorKan1("%s\n","[(regionMatches) str strArray] extension.");
                    342:     obj1 = getoa(obj,1);
                    343:     if (obj1.tag != Sdollar) errorKan1("%s\n","[(regionMatches) str strArray] extension. str must be a string.");
                    344:        obj2 = getoa(obj,2);
                    345:     if (obj2.tag != Sarray) errorKan1("%s\n","[(regionMatches) str strArray] extension. strArray must be an array.");
                    346:     rob = KregionMatches(obj1,obj2);
1.16      takayama  347:   }else if (strcmp(key,"newVector")==0) {
                    348:     if (size != 2) errorKan1("%s\n","[(newVector) m] extension.");
                    349:     obj1 = getoa(obj,1);
                    350:     if (obj1.tag != Sinteger) errorKan1("%s\n","[(newVector) m] extension. m must be an integer.");
                    351:     rob = newObjectArray(KopInteger(obj1));
                    352:   }else if (strcmp(key,"newMatrix")==0) {
                    353:     if (size != 3) errorKan1("%s\n","[(newMatrix) m n] extension.");
                    354:     obj1 = getoa(obj,1);
                    355:     if (obj1.tag != Sinteger) errorKan1("%s\n","[(newMatrix) m n] extension. m must be an integer.");
                    356:        obj2 = getoa(obj,2);
                    357:     if (obj2.tag != Sinteger) errorKan1("%s\n","[(newMatrix) m n] extension. n must be an integer.");
                    358:     rob = newObjectArray(KopInteger(obj1));
                    359:        for (i=0; i<KopInteger(obj1); i++) {
                    360:       putoa(rob,i,newObjectArray(KopInteger(obj2)));
                    361:        }
1.21      takayama  362:   }else if (strcmp(key,"ooPower")==0) {
                    363:     if (size != 3) errorKan1("%s\n","[(ooPower) a b] extension.");
                    364:     obj1 = getoa(obj,1);
                    365:        obj2 = getoa(obj,2);
                    366:     rob = KooPower(obj1,obj2);
1.8       takayama  367:   }else if (strcmp(key,"ostype")==0) {
                    368:     rob = newObjectArray(1);
                    369:     /* Hard encode the OS type. */
                    370: #if defined(__CYGWIN__)
                    371:     putoa(rob,0,KpoString("windows"));
                    372: #else
                    373:     putoa(rob,0,KpoString("unix"));
                    374: #endif
1.24    ! takayama  375:   }else if (strcmp(key,"traceClearStack")==0) {
        !           376:     traceClearStack();
        !           377:     rob = NullObject;
        !           378:   }else if (strcmp(key,"traceShowStack")==0) {
        !           379:     char *ssst;
        !           380:     ssst = traceShowStack();
        !           381:     if (ssst != NULL) {
        !           382:       rob = KpoString(ssst);
        !           383:     }else{
        !           384:       rob = NullObject;
        !           385:     }
1.5       takayama  386:   }
1.1       maekawa   387: #include "plugin.hh"
1.17      takayama  388: #include "Kclass/tree.hh"
1.1       maekawa   389:   else{
                    390:     errorKan1("%s\n","Unknown tag for extension.");
                    391:   }
                    392:
                    393:
                    394:   return(rob);
1.12      takayama  395: }
                    396:
                    397: struct object KregionMatches(struct object sobj, struct object keyArray)
                    398: {
                    399:   struct object rob;
                    400:   int n,i,j,m,keyn;
                    401:   char *s,*key;
                    402:   rob = newObjectArray(3);
                    403:   getoa(rob,0) = KpoInteger(-1);
                    404:   getoa(rob,1) = NullObject;
                    405:   getoa(rob,2) = NullObject;
                    406:
                    407:   if (sobj.tag != Sdollar) return rob;
                    408:   if (keyArray.tag != Sarray) return rob;
                    409:   n = getoaSize(keyArray);
                    410:   for (i=0; i<n; i++) {
                    411:        if (getoa(keyArray,i).tag != Sdollar) { return rob; }
                    412:   }
                    413:
                    414:   s = KopString(sobj);
                    415:   m = strlen(s);
                    416:
                    417:   for (i=0; i<n; i++) {
                    418:        key = KopString(getoa(keyArray,i));
                    419:        keyn = strlen(key);
                    420:        for (j=0; j<m; j++) {
                    421:          if (strncmp(&(s[j]),key,keyn) == 0) {
                    422:                getoa(rob,0) = KpoInteger(j);
                    423:                getoa(rob,1) = KpoString(key);
                    424:                getoa(rob,2) = KpoInteger(i);
                    425:                return rob;
                    426:          }
                    427:        }
                    428:   }
                    429:   return rob;
1.1       maekawa   430: }
                    431:

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