=================================================================== RCS file: /home/cvs/OpenXM/src/kan96xx/Kan/shell.c,v retrieving revision 1.6 retrieving revision 1.13 diff -u -p -r1.6 -r1.13 --- OpenXM/src/kan96xx/Kan/shell.c 2003/12/04 05:29:12 1.6 +++ OpenXM/src/kan96xx/Kan/shell.c 2005/06/16 05:07:23 1.13 @@ -1,4 +1,4 @@ -/* $OpenXM: OpenXM/src/kan96xx/Kan/shell.c,v 1.5 2003/12/04 05:27:19 takayama Exp $ */ +/* $OpenXM: OpenXM/src/kan96xx/Kan/shell.c,v 1.12 2004/10/14 10:08:09 takayama Exp $ */ #include #include #include @@ -41,7 +41,7 @@ static int AfterPt=0; static char *AfterDeleteFile[MAXFILES]; static int AfterD=0; -static int KeepTmpFiles = 1; +static int KeepTmpFiles = 0; extern int OX_P_stdin; extern int OX_P_stdout; @@ -53,7 +53,8 @@ struct object KoxShell(struct object ob) { /* A temporary help system */ void KoxShellHelp(char *key,FILE *fp) { - char *keys[]={"command","export","which","redirect","@@@@gatekeeper"}; + char *keys[]={"command","export","keep_tmp_files", + "killall","redirect","which","@@@@gatekeeper"}; int i; #define HSIZE 20 char *s[HSIZE]; @@ -89,6 +90,13 @@ void KoxShellHelp(char *key,FILE *fp) { s[2] = "Example 1: [(ls) (hoge) (2>) (stringOut://afo)] oxshell\n afo ::"; s[3] = "Example 2: [(cp) ] addStdoutStderr oxshell\n [@@@stdout @@@stderr] ::"; s[4] = NULL; + }else if (strcmp(key,"killall")==0) { + s[0] = "Kill all the processes envoked by oxshell"; + s[1] = NULL; + }else if (strcmp(key,"keep_tmp_files")==0) { + s[0] = "keep_tmp_files value"; + s[1] = "If value is zero, then temporary files are removed after execution."; + s[2] = NULL; }else{ } i = 0; @@ -100,7 +108,7 @@ void KoxShellHelp(char *key,FILE *fp) { static struct object KoxShell_test1(struct object ob) { /* A simple shell. It does not implement a parser. */ - struct object rob; + struct object rob = OINIT; char *cmd; char *arg1,*arg2; int i,n; @@ -117,15 +125,32 @@ static struct object KoxShell_test1(struct object ob) rob = testmain(ob); }else if (strcmp(cmd,"which")==0) { if (n == 2) { + pathFinderErrorVerbose(0); rob = KoxWhich(getoa(ob,1),KpoInteger(0)); + pathFinderErrorVerbose(-1); }else if (n==3) { + pathFinderErrorVerbose(0); rob = KoxWhich(getoa(ob,1),getoa(ob,2)); + pathFinderErrorVerbose(-1); }else{ errorKan1("%s\n","shell: << which command-name >> or << which command-name path >>"); } return(rob); }else if (strcmp(cmd,"export")==0) { rob=oxsSetenv(ob); + }else if (strcmp(cmd,"keep_tmp_files")==0) { + if (n != 2) errorKan1("%s\n","shell: << keep_tmp_files value >>"); + if (strcmp("0",KopString(getoa(ob,1))) == 0) { + KeepTmpFiles = 0; + }else{ + KeepTmpFiles = 1; + } + rob = KpoInteger(KeepTmpFiles); + }else if (strcmp(cmd,"killall")==0) { + /* It is called from ctrl-C hook of oxrfc103.sm1 */ + fprintf(stderr,"Killing all child processes (oxshell) ..."); + rob = KpoInteger(oxKillAll()); + fprintf(stderr,"\nDone.\n"); }else{ rob = oxsExecuteBlocked(ob); } @@ -134,7 +159,7 @@ static struct object KoxShell_test1(struct object ob) /* Functions for ox_shell */ struct object KoxWhich(struct object cmdo,struct object patho) { - struct object rob; + struct object rob = OINIT; char *sss; rob = NullObject; if (cmdo.tag != Sdollar) errorKan1("%s\n","KoxWhich(str-obj,str-obj)"); @@ -152,9 +177,31 @@ struct object KoxWhich(struct object cmdo,struct objec return(rob); } +static int mysetenv(char *name, char *value, int overwrite); +static int myunsetenv(char *name); +static int mysetenv(char *name, char *value, int overwrite) { + char *s; + char *orig; + s = (char *)getenv(name); + if ((s == NULL) || overwrite) { + s = (char *) mymalloc(strlen(name)+strlen(value)+5); + if (s == 0) { fprintf(stderr,"No more memory.\n"); exit(10); } + strcpy(s,name); + strcat(s,"="); strcat(s,value); + return(putenv(s)); + } + return (0); +} + +/* bug on Solaris. It does not unsetenv. + libc4, libc5, glibc. It does unsetenv. */ +static myunsetenv(char *name) { + return(putenv(name)); +} + /* Example. [(export) (PATH) (=) (/usr/new/bin:$PATH)] */ static struct object oxsSetenv(struct object ob) { - struct object rob; + struct object rob = OINIT; int i,n; char *envp; char *new; @@ -176,9 +223,10 @@ static struct object oxsSetenv(struct object ob) { /* printf("%s\n",new); */ new = oxEvalEnvVar(new); /* printf("%s\n",new); */ - r = setenv(envp,new,1); + r = mysetenv(envp,new,1); }else{ - unsetenv(envp); r = 0; + myunsetenv(envp); r = 0; + /* bug: On Solaris, unsetenv will not work. */ } if (r != 0) errorKan1("%s\n","setenv failed."); new = (char *) getenv(envp); @@ -327,9 +375,9 @@ char *oxsURIgetFileName(char *s) { static struct object testmain(struct object ob) { - struct object rob; + struct object rob = OINIT; char *s; - struct object ot; + struct object ot = OINIT; char **av; int i; rob = NullObject; @@ -358,7 +406,7 @@ char *oxsVarToFile(char *v,char *ext,char *command,int FILE *fp; int n,i,prevc,c; char *prog; - struct object vv; + struct object vv = OINIT; /*bug; winname must be automatically set by looking at command. If command is win32-native-application, then winname=1; else winname=0. @@ -399,12 +447,18 @@ int oxsFileToVar(char *v,char *fname) { int limit; int c,i; - if (v == NULL) errorKan1("%s\n","oxsFileToVar(), v is NULL."); + if (v == NULL) { + /* errorKan1("%s\n","oxsFileToVar(), v is NULL."); */ + fprintf(stderr,"oxsFileToVar(), v is NULL."); + return(-1); + } limit = 1024; fp = fopen(fname,"r"); if (fp == NULL) { fprintf(stderr,"Filename=%s\n",fname); - errorKan1("%s\n","oxsFileToVar(), the file cannot be opened."); + /* errorKan1("%s\n","oxsFileToVar(), the file cannot be opened."); */ + fprintf(stderr,"oxsFileToVar(), the file cannot be opened."); + return(-1); } s = (char *)mymalloc(limit); if (s == NULL) errorKan1("%s\n","No more memory in oxsFileToVar()."); @@ -434,7 +488,7 @@ static char **oxsBuildArgv(struct object ob) { char *ext, *v; int usetmp=1; int win=0; - struct object ocmd; + struct object ocmd = OINIT; /* bug: win variable must be properly set on windows native. */ @@ -512,6 +566,7 @@ static struct object oxsExecuteBlocked(struct object o { int r,i,n; char **argv; + int errorf; argv = oxsBuildArgv(ob); argv = oxsBuildArgvRedirect(argv); @@ -524,9 +579,12 @@ static struct object oxsExecuteBlocked(struct object o errorKan1("%s\n","ForkExecBlocked failed."); } */ + errorf=0; if (AfterPt > 0) { for (i=0; i< AfterPt; i++) { - oxsFileToVar(AfterSetVar[i],AfterReadFile[i]); + if (oxsFileToVar(AfterSetVar[i],AfterReadFile[i]) != 0) { + errorf=1; + } } } AfterPt = 0; @@ -539,6 +597,7 @@ static struct object oxsExecuteBlocked(struct object o } } AfterD = 0; + if (errorf) errorKan1("%s\n","Some errors in oxsFileToVar()."); return(KpoInteger(r)); }