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

1.4     ! takayama    1: /* $OpenXM: OpenXM/src/kan96xx/Kan/ext.c,v 1.3 2000/02/24 12:33:47 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++) {
                     29:        Mychildren[j] = Mychildren[j+1];
                     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;
                     84:   int m,i;
                     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,
                    136:                       CurrentContextp->userDictionary);
                    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) {
                    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));
1.4     ! takayama  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 */
1.1       maekawa   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.");
                    191:       m = KopInteger(obj3);  /* m == 1 : block ctrl-C. */
                    192:       argListc = getoaSize(obj1);
                    193:       fdListc =  getoaSize(obj2);
                    194:       if ((m = fork()) > 0) {
                    195:        /* parent */
                    196:        signal(SIGCHLD,mywait); /* to kill Zombie */
                    197:        Mychildren[Mycp++] = m;
                    198:        if (Mycp >= MYCP_SIZE-1) {
                    199:          errorKan1("%s\n","Child process table is full.\n");
                    200:          Mycp = 0;
                    201:        }
                    202:        rob = KpoInteger(m);
                    203:        /* Done */
                    204:       }else{
                    205:        /* Child */
                    206:        for (i=0; i<fdListc; i++) {
                    207:          /* close the specified files */
                    208:          close(KopInteger(getoa(obj2,i)));
                    209:        }
                    210:        /* execl */
                    211:        if (m == 1) {
                    212:          {
                    213:            sigset_t sss;
                    214:            sigemptyset(&sss);
                    215:            sigaddset(&sss,SIGINT);
                    216:            sigprocmask(SIG_BLOCK,&sss,NULL);
                    217:          }
                    218:        }
                    219:        argv = (char **) sGC_malloc(sizeof(char *)*(argListc+1));
                    220:        if (argv == NULL) {
                    221:          fprintf(stderr," no more momory. forkExec --- exiting.\n");
                    222:          _exit(10);
                    223:        }
                    224:        for (i=0; i<argListc; i++) {
                    225:          argv[i] = KopString(getoa(obj1,i));
                    226:          argv[i+1] = NULL;
                    227:        }
                    228:        execv(argv[0],argv);
                    229:         /* This place will never be reached unless execv fails. */
                    230:         fprintf(stderr,"forkExec fails: ");
                    231:        for (i=0; i<argListc; i++) {
                    232:          fprintf(stderr,"%s ",argv[i]);
                    233:        }
                    234:        fprintf(stderr,"\nExiting, but staying as Zombie.\n");
                    235:        _exit(10);
                    236:       }
                    237:     }else if (strcmp(key,"getchild")==0) {
                    238:       if (size != 1) errorKan1("%s\n","[(getchild)] extension.");
                    239:       rob = newObjectArray(Mycp);
                    240:       for (i=0; i<Mycp; i++) {
                    241:        putoa(rob,i,KpoInteger(Mychildren[i]));
                    242:       }
                    243:     }else if (strcmp(key,"getUniqueFileName")==0) {
                    244:       if (size != 2) errorKan1("%s\n","[(getUniqueFileName) path] extension.");
                    245:       obj1 = getoa(obj,1);
                    246:       if (obj1.tag != Sdollar) errorKan1("%s\n","[(getUniqueFileName) path] extension. path must be a string.");
                    247:       rob = KpoString(ext_generateUniqueFileName(KopString(obj1)));
                    248:     }else if (strcmp(key,"outputObjectToFile")==0) {
                    249:       if (size != 3) errorKan1("%s\n","[(outputObjectToFile) path obj] extension.");
                    250:       obj1 = getoa(obj,1);
                    251:       if (obj1.tag != Sdollar) errorKan1("%s\n","[(outputObjectToFile) path obj] extension. path must be a string.");
                    252:       obj2 = getoa(obj,2);
                    253:       fp = fopen(KopString(obj1),"w");
                    254:       if (fp == NULL) errorKan1("%s\n","[(outputObjectToFile) path object] extension : could not open the path.");
                    255:       printObject(obj2,0,fp);
                    256:       fclose(fp);
                    257:       rob = NullObject;
                    258:     }else if (strcmp(key,"hilbert")==0) {
                    259:       if (size != 3) errorKan1("%s\n","[(hilbert) obgb obvlist] extension.");
                    260:       rob = hilberto(getoa(obj,1),getoa(obj,2));
                    261:     }else if (strcmp(key,"chattr")==0) {
                    262:       if (size != 3) errorKan1("%s\n","[(chattr)  num symbol] extension.");
                    263:       obj1 = getoa(obj,1);
                    264:       obj2 = getoa(obj,2);
                    265:       if (obj1.tag != Sinteger) errorKan1("%s\n","[(chattr)  num symbol] extension.");
                    266:       if (obj2.tag != Sstring)  errorKan1("%s\n","[(chattr)  num symbol] extension.");
                    267:       m = KopInteger(obj1);
                    268:       if (!( m == 0 || m == PROTECT || m == ABSOLUTE_PROTECT))
                    269:        errorKan1("%s\n","The number must be 0, 1 or 2.");
                    270:       putUserDictionary2(obj2.lc.str,(obj2.rc.op->lc).ival,(obj2.rc.op->rc).ival,
                    271:                         m,CurrentContextp->userDictionary);
                    272:     }
                    273: #include "plugin.hh"
                    274:   else{
                    275:     errorKan1("%s\n","Unknown tag for extension.");
                    276:   }
                    277:
                    278:
                    279:   return(rob);
                    280: }
                    281:

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