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

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

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