=================================================================== RCS file: /home/cvs/OpenXM/src/kan96xx/Kan/ext.c,v retrieving revision 1.19 retrieving revision 1.48 diff -u -p -r1.19 -r1.48 --- OpenXM/src/kan96xx/Kan/ext.c 2003/12/03 01:21:43 1.19 +++ OpenXM/src/kan96xx/Kan/ext.c 2016/03/31 05:27:34 1.48 @@ -1,11 +1,14 @@ -/* $OpenXM: OpenXM/src/kan96xx/Kan/ext.c,v 1.18 2003/11/24 12:38:17 takayama Exp $ */ +/* $OpenXM: OpenXM/src/kan96xx/Kan/ext.c,v 1.47 2016/03/31 03:22:54 takayama Exp $ */ #include +#include #include #include #include #include #include #include +#include +#include #include "datatype.h" #include "stackm.h" #include "extern.h" @@ -14,20 +17,26 @@ #include "plugin.h" #include "kclass.h" #include +#include +#include #include "ox_pathfinder.h" +#include "mysig.h" +extern int Quiet; extern char **environ; +extern char *MsgSourceTrace; #define MYCP_SIZE 100 static int Mychildren[MYCP_SIZE]; static int Mycp = 0; +static int Verbose_mywait = 0; static void mywait() { int status; int pid; int i,j; - signal(SIGCHLD,SIG_IGN); + /* mysignal(SIGCHLD,SIG_IGN); */ pid = wait(&status); - fprintf(stderr,"Child process %d is exiting.\n",pid); + if ((!Quiet) && (Verbose_mywait)) fprintf(stderr,"Child process %d is exiting.\n",pid); for (i=0; i 0) Mycp--; } } - signal(SIGCHLD,mywait); + mysignal(SIGCHLD,mywait); } #define SIZE_OF_ENVSTACK 5 @@ -84,18 +93,24 @@ static char *ext_generateUniqueFileName(char *s) return(NULL); } +static struct object oregexec(struct object oregex,struct object ostrArray,struct object oflag); + struct object Kextension(struct object obj) { char *key; int size; - struct object keyo; + struct object keyo = OINIT; struct object rob = NullObject; - struct object obj1,obj2,obj3,obj4; + struct object obj1 = OINIT; + struct object obj2 = OINIT; + struct object obj3 = OINIT; + struct object obj4 = OINIT; int m,i,pid, uid; int argListc, fdListc; char *abc; char *abc2; extern struct context *CurrentContextp; + struct timeval tm; #if (__CYGWIN__) extern sigjmp_buf EnvOfStackMachine; #else @@ -103,7 +118,6 @@ struct object Kextension(struct object obj) #endif extern void ctrlC(); extern int SigIgn; - extern errno; extern int DebugCMO; extern int OXprintMessage; struct stat buf; @@ -111,6 +125,8 @@ struct object Kextension(struct object obj) FILE *fp; void (*oldsig)(); extern SecureMode; + extern char *UD_str; + extern int UD_attr; if (obj.tag != Sarray) errorKan1("%s\n","Kextension(): The argument must be an array."); size = getoaSize(obj); @@ -128,10 +144,10 @@ struct object Kextension(struct object obj) pushEnv(EnvOfStackMachine); m = KSexecuteString(obj1.lc.str); /* This is critical area. If you catch ctrl-c here, program crashes. */ - oldsig = signal(SIGINT,SIG_IGN); + oldsig = mysignal(SIGINT,SIG_IGN); popEnv(EnvOfStackMachine); /* OK! We passed the critical area. */ - signal(SIGINT,oldsig); + mysignal(SIGINT,oldsig); rob = KpoInteger(m); }else if (strcmp(key,"getpid") == 0) { rob = KpoInteger( (int) getpid() ); @@ -144,10 +160,17 @@ struct object Kextension(struct object obj) obj1 = getoa(obj,1); if (obj1.tag != Sinteger) errorKan1("%s\n","[(chattrs) num] extension."); m = KopInteger(obj1); - if (!( m == 0 || m == PROTECT || m == ABSOLUTE_PROTECT)) - errorKan1("%s\n","The number must be 0, 1 or 2."); + /* if (!( m == 0 || m == PROTECT || m == ABSOLUTE_PROTECT || m == ATTR_INFIX)) + errorKan1("%s\n","The number must be 0, 1 or 2.");*/ putUserDictionary2((char *)NULL,0,0,m | SET_ATTR_FOR_ALL_WORDS, CurrentContextp->userDictionary); + }else if (strcmp(key,"or_attrs")==0) { + if (size != 2) errorKan1("%s\n","[(or_attrs) num] extension."); + obj1 = getoa(obj,1); + if (obj1.tag != Sinteger) errorKan1("%s\n","[(or_attrs) num] extension."); + m = KopInteger(obj1); + putUserDictionary2((char *)NULL,0,0,m | OR_ATTR_FOR_ALL_WORDS, + CurrentContextp->userDictionary); }else if (strcmp(key,"keywords")==0) { if (size != 1) errorKan1("%s\n","[(keywords)] extension."); rob = showSystemDictionary(1); @@ -158,6 +181,10 @@ struct object Kextension(struct object obj) else { system(KopString(getoa(obj,1))); exit(0); } */ + }else if (strcmp(key,"date")==0) { + if (size != 1) errorKan1("%s\n","[(date)] extension."); + gettimeofday(&tm,NULL); + rob = KpoString(ctime((time_t *)&(tm.tv_sec))); }else if (strcmp(key,"defaultPolyRing")==0) { if (size != 2) errorKan1("%s\n","[(defaultPolyRing) n] extension."); rob = KdefaultPolyRing(getoa(obj,1)); @@ -204,6 +231,12 @@ struct object Kextension(struct object obj) putoa(obj3,0,KpoInteger((int) buf.st_size)); putoa(rob,1,obj3); /* We have not yet read buf fully */ } + }else if (strcmp(key,"gethostname")==0) { + abc = (char *)sGC_malloc(sizeof(char)*1024); + if (gethostname(abc,1023) < 0) { + errorKan1("%s\n","hostname could not be obtained."); + } + rob = KpoString(abc); }else if (strcmp(key,"forkExec")==0) { if (size != 4) errorKan1("%s\n","[(forkExec) argList fdList sigblock] extension."); obj1 = getoa(obj,1); @@ -223,7 +256,7 @@ struct object Kextension(struct object obj) if (m&2) { /* Do not call singal to turn around a trouble on cygwin. BUG. */ }else{ - signal(SIGCHLD,mywait); /* to kill Zombie */ + mysignal(SIGCHLD,mywait); /* to kill Zombie */ } Mychildren[Mycp++] = pid; if (Mycp >= MYCP_SIZE-1) { @@ -292,6 +325,18 @@ struct object Kextension(struct object obj) printObject(obj2,0,fp); fclose(fp); rob = NullObject; + }else if (strcmp(key,"getAttributeList")==0) { + if (size != 2) errorKan1("%s\n","[(getAttributeList) ob] extension rob"); + rob = KgetAttributeList(getoa(obj,1)); + }else if (strcmp(key,"setAttributeList")==0) { + if (size != 3) errorKan1("%s\n","[(setAttributeList) ob attrlist] extension rob"); + rob = KsetAttributeList(getoa(obj,1), getoa(obj,2)); + }else if (strcmp(key,"getAttribute")==0) { + if (size != 3) errorKan1("%s\n","[(getAttribute) ob key] extension rob"); + rob = KgetAttribute(getoa(obj,1),getoa(obj,2)); + }else if (strcmp(key,"setAttribute")==0) { + if (size != 4) errorKan1("%s\n","[(setAttributeList) ob key value] extension rob"); + rob = KsetAttribute(getoa(obj,1), getoa(obj,2),getoa(obj,3)); }else if (strcmp(key,"hilbert")==0) { if (size != 3) errorKan1("%s\n","[(hilbert) obgb obvlist] extension."); rob = hilberto(getoa(obj,1),getoa(obj,2)); @@ -314,16 +359,43 @@ struct object Kextension(struct object obj) if (obj1.tag != Sinteger) errorKan1("%s\n","[(chattr) num symbol] extension."); if (obj2.tag != Sstring) errorKan1("%s\n","[(chattr) num symbol] extension."); m = KopInteger(obj1); - if (!( m == 0 || m == PROTECT || m == ABSOLUTE_PROTECT)) - errorKan1("%s\n","The number must be 0, 1 or 2."); + /* if (!( m == 0 || m == PROTECT || m == ABSOLUTE_PROTECT || m == ATTR_INFIX)) + errorKan1("%s\n","The number must be 0, 1 or 2.");*/ putUserDictionary2(obj2.lc.str,(obj2.rc.op->lc).ival,(obj2.rc.op->rc).ival, m,CurrentContextp->userDictionary); + }else if (strcmp(key,"or_attr")==0) { + if (size != 3) errorKan1("%s\n","[(or_attr) num symbol] extension."); + obj1 = getoa(obj,1); + obj2 = getoa(obj,2); + if (obj1.tag != Sinteger) errorKan1("%s\n","[(or_attr) num symbol] extension."); + if (obj2.tag != Sstring) errorKan1("%s\n","[(or_attr) num symbol] extension."); + m = KopInteger(obj1); + rob = KfindUserDictionary(obj2.lc.str); + if (rob.tag != NoObject.tag) { + if (strcmp(UD_str,obj2.lc.str) == 0) { + m |= UD_attr; + }else errorKan1("%s\n","or_attr: internal error."); + } + rob = KpoInteger(m); + putUserDictionary2(obj2.lc.str,(obj2.rc.op->lc).ival,(obj2.rc.op->rc).ival, + m,CurrentContextp->userDictionary); + }else if (strcmp(key,"getattr")==0) { + if (size != 2) errorKan1("%s\n","[(getattr) symbol] extension."); + obj1 = getoa(obj,1); + if (obj1.tag != Sstring) errorKan1("%s\n","[(getattr) symbol] extension."); + rob = KfindUserDictionary(obj1.lc.str); + if (rob.tag != NoObject.tag) { + if (strcmp(UD_str,obj1.lc.str) == 0) { + rob = KpoInteger(UD_attr); + }else errorKan1("%s\n","getattr: internal error."); + }else rob = NullObject; }else if (strcmp(key,"getServerEnv")==0) { if (size != 2) errorKan1("%s\n","[(getServerEnv) serverName] extension."); obj1 = getoa(obj,1); if (obj1.tag != Sdollar) errorKan1("%s\n","[(getServerEnv) serverName] extension."); { char **se; int ii; int nn; + char **getServerEnv(char *); se = getServerEnv(KopString(obj1)); if (se == NULL) { debugServerEnv(KopString(obj1)); @@ -336,6 +408,29 @@ struct object Kextension(struct object obj) } } } + }else if (strcmp(key,"read")==0) { + if (size != 3) errorKan1("%s\n","[(read) fd size] extension."); + obj1 = getoa(obj,1); + if (obj1.tag != Sinteger) errorKan1("%s\n","[(read) fd size] extension. fd must be an integer."); + obj2 = getoa(obj,2); + if (obj2.tag != Sinteger) errorKan1("%s\n","[(read) fd size] extension. size must be an integer."); + { + int total, n, fd; + char *s; char *s0; + fd = KopInteger(obj1); + total = KopInteger(obj2); + if (total <= 0) errorKan1("%s\n","[(read) ...]; negative size has not yet been implemented."); + /* Return a string. todo: implement SbyteArray case. */ + s0 = s = (char *) sGC_malloc(total+1); + if (s0 == NULL) errorKan1("%s\n","[(read) ...]; no more memory."); + while (total >0) { + n = read(fd, s, total); + if (n < 0) { perror("read"); errorKan1("%s\n","[(read) ...]; read error.");} + s[n] = 0; + total -= n; s = &(s[n]); + } + rob = KpoString(s0); + } }else if (strcmp(key,"regionMatches")==0) { if (size != 3) errorKan1("%s\n","[(regionMatches) str strArray] extension."); obj1 = getoa(obj,1); @@ -358,19 +453,83 @@ struct object Kextension(struct object obj) for (i=0; i