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

1.8     ! takayama    1: /* $OpenXM: OpenXM/src/kan96xx/Kan/ext.c,v 1.7 2001/08/10 13:48:38 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"
                     15:
                     16: #define MYCP_SIZE 100
                     17: static int Mychildren[MYCP_SIZE];
                     18: static int Mycp = 0;
                     19: static void mywait() {
                     20:   int status;
                     21:   int pid;
                     22:   int i,j;
                     23:   signal(SIGCHLD,SIG_IGN);
                     24:   pid = wait(&status);
                     25:   fprintf(stderr,"Child process %d is exiting.\n",pid);
                     26:   for (i=0; i<Mycp; i++) {
                     27:     if (Mychildren[i]  == pid) {
                     28:       for (j=i; j<Mycp-1; j++) {
1.5       takayama   29:         Mychildren[j] = Mychildren[j+1];
1.1       maekawa    30:       }
                     31:       if (Mycp > 0) Mycp--;
                     32:     }
                     33:   }
                     34:   signal(SIGCHLD,mywait);
                     35: }
                     36:
                     37: #define SIZE_OF_ENVSTACK 5
                     38: static jmp_buf EnvStack[SIZE_OF_ENVSTACK];
                     39: static int Envp = 0;
                     40: static void pushEnv(jmp_buf jb) {
                     41:   if (Envp < SIZE_OF_ENVSTACK) {
                     42:     *(EnvStack[Envp]) = *jb;
                     43:     Envp++;
                     44:   }else{
                     45:     fprintf(stderr,"Overflow of  EnvStack.\n");
                     46:     exit(2);
                     47:   }
                     48: }
                     49: static void popEnv(jmp_buf jbp) {
                     50:   if (Envp <= 0) {
                     51:     fprintf(stderr,"Underflow of EnvStack.\n");
                     52:     exit(3);
                     53:   }else{
                     54:     Envp--;
                     55:     *jbp = *EnvStack[Envp];
                     56:   }
                     57: }
                     58:
                     59: static char *ext_generateUniqueFileName(char *s)
                     60: {
                     61:   char *t;
                     62:   int i;
                     63:   struct stat statbuf;
                     64:   t = (char *)sGC_malloc(sizeof(char)*strlen(s)+4+2);
                     65:   for (i=0; i<1000; i++) {
                     66:     /* Give up if we failed for 1000 names. */
                     67:     sprintf(t,"%s.%d",s,i);
                     68:     /* if (phc_overwrite) return(t); */
                     69:     if (stat(t,&statbuf) < 0) {
                     70:       return(t);
                     71:     }
                     72:   }
                     73:   errorKan1("%s\n","ext_generateUniqueFileName: could not generate a unique file name. Exhausted all the names.");
                     74:   return(NULL);
                     75: }
                     76:
                     77: struct object Kextension(struct object obj)
                     78: {
                     79:   char *key;
                     80:   int size;
                     81:   struct object keyo;
                     82:   struct object rob = NullObject;
                     83:   struct object obj1,obj2,obj3,obj4;
1.6       takayama   84:   int m,i,pid;
1.1       maekawa    85:   int argListc, fdListc;
                     86:   char *abc;
                     87:   char *abc2;
                     88:   extern struct context *CurrentContextp;
                     89:   extern jmp_buf EnvOfStackMachine;
                     90:   extern void ctrlC();
                     91:   extern int SigIgn;
                     92:   extern errno;
                     93:   extern int DebugCMO;
                     94:   extern int OXprintMessage;
                     95:   struct stat buf;
                     96:   char **argv;
                     97:   FILE *fp;
1.3       takayama   98:   void (*oldsig)();
                     99:   extern SecureMode;
1.1       maekawa   100:
                    101:   if (obj.tag != Sarray) errorKan1("%s\n","Kextension(): The argument must be an array.");
                    102:   size = getoaSize(obj);
                    103:   if (size < 1) errorKan1("%s\n","Kextension(): Empty array.");
                    104:   keyo = getoa(obj,0);
                    105:   if (keyo.tag != Sdollar) errorKan1("%s\n","Kextension(): No key word.");
                    106:   key = KopString(keyo);
                    107:
                    108:   /* branch by they key word. */
                    109:   if (strcmp(key,"parse")==0) {
                    110:     if (size != 2) errorKan1("%s\n","[(parse)  string] extension.");
                    111:     obj1 = getoa(obj,1);
                    112:     if (obj1.tag != Sdollar) errorKan1("%s\n","[(parse)  string] extension");
                    113:
                    114:     pushEnv(EnvOfStackMachine);
                    115:     m = KSexecuteString(obj1.lc.str);
                    116:     /* This is critical area. If you catch ctrl-c here, program crashes. */
                    117:     oldsig = signal(SIGINT,SIG_IGN);
                    118:     popEnv(EnvOfStackMachine);
                    119:     /* OK! We passed the critical area. */
                    120:     signal(SIGINT,oldsig);
                    121:     rob = KpoInteger(m);
                    122:   }else if (strcmp(key,"getpid") == 0) {
                    123:     rob = KpoInteger( (int) getpid() );
                    124:   }else if (strcmp(key,"flush") == 0) {
                    125:     /* fflush(NULL); */
                    126:     fflush(stdout);
                    127:     rob.tag = Snull;
                    128:   }else if (strcmp(key,"chattrs")==0) {
                    129:     if (size != 2) errorKan1("%s\n","[(chattrs)  num] extension.");
                    130:     obj1 = getoa(obj,1);
                    131:     if (obj1.tag != Sinteger) errorKan1("%s\n","[(chattrs)  num] extension.");
                    132:     m = KopInteger(obj1);
                    133:     if (!( m == 0 || m == PROTECT || m == ABSOLUTE_PROTECT))
                    134:       errorKan1("%s\n","The number must be 0, 1 or 2.");
                    135:     putUserDictionary2((char *)NULL,0,0,m | SET_ATTR_FOR_ALL_WORDS,
1.5       takayama  136:                        CurrentContextp->userDictionary);
1.1       maekawa   137:   }else if (strcmp(key,"keywords")==0) {
                    138:     if (size != 1) errorKan1("%s\n","[(keywords)] extension.");
                    139:     rob = showSystemDictionary(1);
                    140:     /*  }else if (strcmp(key,"fork0")==0) {
1.5       takayama  141:         if (size != 2) errorKan1("%s\n","[(fork0) sss] extension.");
                    142:         m = fork();
                    143:         if (m>0) { rob = KpoInteger(m); }
                    144:         else {
                    145:         system(KopString(getoa(obj,1))); exit(0);
                    146:         } */
                    147:   }else if (strcmp(key,"defaultPolyRing")==0) {
                    148:     if (size != 2) errorKan1("%s\n","[(defaultPolyRing) n] extension.");
                    149:     rob = KdefaultPolyRing(getoa(obj,1));
                    150:   }else if (strcmp(key,"getenv")==0) {
                    151:     if (size != 2) errorKan1("%s\n","[(getenv) envstr] extension.");
                    152:     obj1 = getoa(obj,1);
                    153:     if (obj1.tag != Sdollar) errorKan1("%s\n","[(getenv) envstr] extension");
                    154:     abc = getenv(KopString(obj1));
                    155:     if (abc == NULL) {
                    156:       rob = NullObject;
                    157:     }else{
                    158:       abc2 = (char *)sGC_malloc(sizeof(char)*(strlen(abc)+2));
                    159:       strcpy(abc2,abc);
                    160:       rob = KpoString(abc2);
                    161:     }
                    162:   }else if (strcmp(key,"stat")==0) {
                    163:     if (size != 2) errorKan1("%s\n","[(stat) fname] extension.");
                    164:     obj1 = getoa(obj,1);
                    165:     if (obj1.tag != Sdollar) errorKan1("%s\n","[(stat) fname] extension ; string fname.");
                    166:     m = stat(KopString(obj1),&buf);
                    167:     rob = newObjectArray(2);
                    168:     if (m == -1) {
                    169:       /* fail */
                    170:       obj2 = NullObject;
                    171:       putoa(rob,0,obj2);
                    172:       obj3 = newObjectArray(2);
                    173:       putoa(obj3,0,KpoString("error no"));
                    174:       putoa(obj3,1,KpoInteger(errno));
                    175:       putoa(rob,1,obj3);
                    176:     }else{
                    177:       /* success */
                    178:       putoa(rob,0,KpoInteger(0));
                    179:       obj3 = newObjectArray(1);
                    180:       putoa(obj3,0,KpoInteger((int) buf.st_size));
                    181:       putoa(rob,1,obj3); /* We have not yet read buf fully */
                    182:     }
                    183:   }else if (strcmp(key,"forkExec")==0) {
                    184:     if (size != 4) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension.");
                    185:     obj1 = getoa(obj,1);
                    186:     if (obj1.tag != Sarray) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension. array argList.");
                    187:     obj2 = getoa(obj,2);
                    188:     if (obj2.tag != Sarray) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension. array fdList.");
                    189:     obj3 = getoa(obj,3);
                    190:     if (obj3.tag != Sinteger) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension. integer sigblock.");
1.7       takayama  191:     m = KopInteger(obj3);  /* m&1 : block ctrl-C. */
1.5       takayama  192:     argListc = getoaSize(obj1);
                    193:     fdListc =  getoaSize(obj2);
1.6       takayama  194:     if ((pid = fork()) > 0) {
1.5       takayama  195:       /* parent */
1.7       takayama  196:       if (m&2) {
1.6       takayama  197:          /* Do not call singal to turn around a trouble on cygwin. BUG. */
                    198:       }else{
                    199:          signal(SIGCHLD,mywait); /* to kill Zombie */
                    200:       }
                    201:       Mychildren[Mycp++] = pid;
1.5       takayama  202:       if (Mycp >= MYCP_SIZE-1) {
                    203:         errorKan1("%s\n","Child process table is full.\n");
                    204:         Mycp = 0;
                    205:       }
1.6       takayama  206:       rob = KpoInteger(pid);
1.5       takayama  207:       /* Done */
                    208:     }else{
                    209:       /* Child */
                    210:       for (i=0; i<fdListc; i++) {
                    211:         /* close the specified files */
                    212:         close(KopInteger(getoa(obj2,i)));
1.1       maekawa   213:       }
1.5       takayama  214:       /* execl */
1.7       takayama  215:       if (m&1) {
1.5       takayama  216:         {
                    217:           sigset_t sss;
                    218:           sigemptyset(&sss);
                    219:           sigaddset(&sss,SIGINT);
                    220:           sigprocmask(SIG_BLOCK,&sss,NULL);
                    221:         }
1.1       maekawa   222:       }
1.5       takayama  223:       argv = (char **) sGC_malloc(sizeof(char *)*(argListc+1));
                    224:       if (argv == NULL) {
                    225:         fprintf(stderr," no more momory. forkExec --- exiting.\n");
                    226:         _exit(10);
1.1       maekawa   227:       }
1.5       takayama  228:       for (i=0; i<argListc; i++) {
                    229:         argv[i] = KopString(getoa(obj1,i));
                    230:         argv[i+1] = NULL;
1.7       takayama  231:       }
                    232:
                    233:       if (m&4) {
                    234:        fprintf(stderr,"execv %s\n",argv[0]);
                    235:        sleep(5);
                    236:         fprintf(stderr,">>>\n");
1.1       maekawa   237:       }
1.5       takayama  238:       execv(argv[0],argv);
                    239:       /* This place will never be reached unless execv fails. */
                    240:       fprintf(stderr,"forkExec fails: ");
                    241:       for (i=0; i<argListc; i++) {
                    242:         fprintf(stderr,"%s ",argv[i]);
                    243:       }
                    244:       fprintf(stderr,"\nExiting, but staying as Zombie.\n");
                    245:       _exit(10);
                    246:     }
                    247:   }else if (strcmp(key,"getchild")==0) {
                    248:     if (size != 1) errorKan1("%s\n","[(getchild)] extension.");
                    249:     rob = newObjectArray(Mycp);
                    250:     for (i=0; i<Mycp; i++) {
                    251:       putoa(rob,i,KpoInteger(Mychildren[i]));
1.1       maekawa   252:     }
1.5       takayama  253:   }else if (strcmp(key,"getUniqueFileName")==0) {
                    254:     if (size != 2) errorKan1("%s\n","[(getUniqueFileName) path] extension.");
                    255:     obj1 = getoa(obj,1);
                    256:     if (obj1.tag != Sdollar) errorKan1("%s\n","[(getUniqueFileName) path] extension. path must be a string.");
                    257:     rob = KpoString(ext_generateUniqueFileName(KopString(obj1)));
                    258:   }else if (strcmp(key,"outputObjectToFile")==0) {
                    259:     if (size != 3) errorKan1("%s\n","[(outputObjectToFile) path obj] extension.");
                    260:     obj1 = getoa(obj,1);
                    261:     if (obj1.tag != Sdollar) errorKan1("%s\n","[(outputObjectToFile) path obj] extension. path must be a string.");
                    262:     obj2 = getoa(obj,2);
                    263:     fp = fopen(KopString(obj1),"w");
                    264:     if (fp == NULL) errorKan1("%s\n","[(outputObjectToFile) path object] extension : could not open the path.");
                    265:     printObject(obj2,0,fp);
                    266:     fclose(fp);
                    267:     rob = NullObject;
                    268:   }else if (strcmp(key,"hilbert")==0) {
                    269:     if (size != 3) errorKan1("%s\n","[(hilbert) obgb obvlist] extension.");
                    270:     rob = hilberto(getoa(obj,1),getoa(obj,2));
                    271:   }else if (strcmp(key,"chattr")==0) {
                    272:     if (size != 3) errorKan1("%s\n","[(chattr)  num symbol] extension.");
                    273:     obj1 = getoa(obj,1);
                    274:     obj2 = getoa(obj,2);
                    275:     if (obj1.tag != Sinteger) errorKan1("%s\n","[(chattr)  num symbol] extension.");
                    276:     if (obj2.tag != Sstring)  errorKan1("%s\n","[(chattr)  num symbol] extension.");
                    277:     m = KopInteger(obj1);
                    278:     if (!( m == 0 || m == PROTECT || m == ABSOLUTE_PROTECT))
                    279:       errorKan1("%s\n","The number must be 0, 1 or 2.");
                    280:     putUserDictionary2(obj2.lc.str,(obj2.rc.op->lc).ival,(obj2.rc.op->rc).ival,
                    281:                        m,CurrentContextp->userDictionary);
1.8     ! takayama  282:   }else if (strcmp(key,"ostype")==0) {
        !           283:     rob = newObjectArray(1);
        !           284:     /* Hard encode the OS type. */
        !           285: #if defined(__CYGWIN__)
        !           286:     putoa(rob,0,KpoString("windows"));
        !           287: #else
        !           288:     putoa(rob,0,KpoString("unix"));
        !           289: #endif
1.5       takayama  290:   }
1.1       maekawa   291: #include "plugin.hh"
                    292:   else{
                    293:     errorKan1("%s\n","Unknown tag for extension.");
                    294:   }
                    295:
                    296:
                    297:   return(rob);
                    298: }
                    299:

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