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

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

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