version 1.3, 2003/12/03 09:00:46 |
version 1.5, 2003/12/04 05:27:19 |
|
|
/* $OpenXM: OpenXM/src/kan96xx/Kan/shell.c,v 1.2 2003/12/03 01:21:43 takayama Exp $ */ |
/* $OpenXM: OpenXM/src/kan96xx/Kan/shell.c,v 1.4 2003/12/03 23:26:39 takayama Exp $ */ |
#include <stdio.h> |
#include <stdio.h> |
#include <sys/types.h> |
#include <sys/types.h> |
#include <sys/stat.h> |
#include <sys/stat.h> |
Line 22 static struct object oxsSetenv(struct object ob); |
|
Line 22 static struct object oxsSetenv(struct object ob); |
|
static char *oxsIsURI(char *s); |
static char *oxsIsURI(char *s); |
static char *oxsURIgetVarName(char *s); |
static char *oxsURIgetVarName(char *s); |
static char *oxsURIgetExtension(char *s); |
static char *oxsURIgetExtension(char *s); |
|
static char *oxsURIgetFileName(char *s); |
|
static char *oxsRemoveOpt(char *s); |
|
static char *oxsGetOpt(char *s); |
static char *oxsVarToFile(char *v,char *extension,char *comamnd,int tmp); |
static char *oxsVarToFile(char *v,char *extension,char *comamnd,int tmp); |
static int oxsFileToVar(char *v,char *fname); |
static int oxsFileToVar(char *v,char *fname); |
static char **oxsBuildArgv(struct object ob); |
static char **oxsBuildArgv(struct object ob); |
|
static char **oxsBuildArgvRedirect(char **argv); |
static struct object testmain(struct object ob); |
static struct object testmain(struct object ob); |
|
|
#define mymalloc(n) sGC_malloc(n) |
#define mymalloc(n) sGC_malloc(n) |
Line 37 static int AfterPt=0; |
|
Line 41 static int AfterPt=0; |
|
static char *AfterDeleteFile[MAXFILES]; |
static char *AfterDeleteFile[MAXFILES]; |
static int AfterD=0; |
static int AfterD=0; |
|
|
|
static int KeepTmpFiles = 1; |
|
|
|
extern int OX_P_stdin; |
|
extern int OX_P_stdout; |
|
extern int OX_P_stderr; |
|
|
struct object KoxShell(struct object ob) { |
struct object KoxShell(struct object ob) { |
return KoxShell_test1(ob); |
return KoxShell_test1(ob); |
} |
} |
|
|
/* A temporary help system */ |
/* A temporary help system */ |
void KoxShellHelp(char *key,FILE *fp) { |
void KoxShellHelp(char *key,FILE *fp) { |
char *keys[]={"command","export","which","@@@@gatekeeper"}; |
char *keys[]={"command","export","which","redirect","@@@@gatekeeper"}; |
int i; |
int i; |
#define HSIZE 20 |
#define HSIZE 20 |
char *s[HSIZE]; |
char *s[HSIZE]; |
if (key == NULL) { |
if (key == NULL) { |
|
fprintf(fp,"\n"); |
for (i=0; strcmp(keys[i],"@@@@gatekeeper") != 0; i++) { |
for (i=0; strcmp(keys[i],"@@@@gatekeeper") != 0; i++) { |
fprintf(fp,"%s\n",keys[i]); |
fprintf(fp,"%s\n",keys[i]); |
KoxShellHelp(keys[i],fp); |
KoxShellHelp(keys[i],fp); |
Line 72 void KoxShellHelp(char *key,FILE *fp) { |
|
Line 83 void KoxShellHelp(char *key,FILE *fp) { |
|
s[2] = "Example 1: /afo (Hello! ) def [(cat) (stringIn://afo)] oxshell"; |
s[2] = "Example 1: /afo (Hello! ) def [(cat) (stringIn://afo)] oxshell"; |
s[3] = "Example 2: [(polymake) (stringInOut://afo.poly) (FACETS)] oxshell"; |
s[3] = "Example 2: [(polymake) (stringInOut://afo.poly) (FACETS)] oxshell"; |
s[4] = NULL; |
s[4] = NULL; |
|
}else if (strcmp(key,"redirect")==0) { |
|
s[0] = "The following redirect operators are implemented."; |
|
s[1] = "< > 2>"; |
|
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{ |
}else{ |
} |
} |
i = 0; |
i = 0; |
Line 178 char *oxsIsURI(char *s) { |
|
Line 195 char *oxsIsURI(char *s) { |
|
int n,i,j; |
int n,i,j; |
char *u; |
char *u; |
if (s == NULL) return((char *)NULL); |
if (s == NULL) return((char *)NULL); |
|
s = oxsRemoveOpt(s); |
n = strlen(s); |
n = strlen(s); |
for (i=0; i<n-3;i++) { |
for (i=0; i<n-3;i++) { |
if ((s[i] == ':') && (s[i+1] == '/') && (s[i+2] == '/')) { |
if ((s[i] == ':') && (s[i+1] == '/') && (s[i+2] == '/')) { |
Line 201 char *oxsURIgetVarName(char *s) { |
|
Line 219 char *oxsURIgetVarName(char *s) { |
|
int n,i,j; |
int n,i,j; |
char *u; |
char *u; |
if (s == NULL) return((char *)NULL); |
if (s == NULL) return((char *)NULL); |
|
s = oxsRemoveOpt(s); |
n = strlen(s); |
n = strlen(s); |
for (i=0; i<n-3;i++) { |
for (i=0; i<n-3;i++) { |
if ((s[i] == ':') && (s[i+1] == '/') && (s[i+2] == '/')) { |
if ((s[i] == ':') && (s[i+1] == '/') && (s[i+2] == '/')) { |
Line 225 char *oxsURIgetExtension(char *s) { |
|
Line 244 char *oxsURIgetExtension(char *s) { |
|
int n,i,j,k; |
int n,i,j,k; |
char *u; |
char *u; |
if (s == NULL) return((char *)NULL); |
if (s == NULL) return((char *)NULL); |
|
s = oxsRemoveOpt(s); |
n = strlen(s); |
n = strlen(s); |
for (i=0; i<n-3;i++) { |
for (i=0; i<n-3;i++) { |
if ((s[i] == ':') && (s[i+1] == '/') && (s[i+2] == '/')) { |
if ((s[i] == ':') && (s[i+1] == '/') && (s[i+2] == '/')) { |
Line 244 char *oxsURIgetExtension(char *s) { |
|
Line 264 char *oxsURIgetExtension(char *s) { |
|
return(NULL); |
return(NULL); |
} |
} |
|
|
|
/* stringInOut://abc.poly=:file://pqr.txt */ |
|
static char *oxsRemoveOpt(char *s) { |
|
int n,i,j; |
|
char *u; |
|
if (s == NULL) return((char *)NULL); |
|
n = strlen(s); |
|
for (i=0; i<n-1;i++) { |
|
if ((s[i] == '=') && (s[i+1] == ':')) { |
|
u = (char *) mymalloc(i+1); |
|
if (u == NULL) nomemory(1); |
|
u[0]=0; |
|
for (j=0; j<i; j++) { |
|
u[j] = s[j]; u[j+1]=0; |
|
} |
|
return(u); |
|
} |
|
} |
|
return(s); |
|
} |
|
|
|
/* stringInOut://abc.poly=:file://pqr.txt */ |
|
/* stringInOut://abc.poly */ |
|
char *oxsGetOpt(char *s) { |
|
int n,i,j; |
|
char *u; |
|
if (s == NULL) return((char *)NULL); |
|
n = strlen(s); |
|
for (i=0; i<n-1;i++) { |
|
if ((s[i] == '=') && (s[i+1] == ':')) { |
|
u = (char *) mymalloc(n-i+1); |
|
if (u == NULL) nomemory(1); |
|
u[0]=0; |
|
for (j=i+2; j<n; j++) { |
|
u[j-i-2] = s[j]; u[j-i-2+1]=0; |
|
} |
|
return(u); |
|
} |
|
} |
|
return(NULL); |
|
} |
|
|
|
char *oxsURIgetFileName(char *s) { |
|
int n,i,j; |
|
char *u; |
|
if (s == NULL) return((char *)NULL); |
|
s = oxsRemoveOpt(s); |
|
n = strlen(s); |
|
for (i=0; i<n-3;i++) { |
|
if ((s[i] == ':') && (s[i+1] == '/') && (s[i+2] == '/')) { |
|
u = (char *) mymalloc(n-i+1); |
|
if (u == NULL) nomemory(1); |
|
u[0]=0; |
|
for (j=i+3; j<n; j++) { |
|
u[j-i-3] = s[j]; u[j-i-3+1]=0; |
|
} |
|
return(u); |
|
} |
|
} |
|
return(NULL); |
|
} |
|
|
|
|
static struct object testmain(struct object ob) { |
static struct object testmain(struct object ob) { |
struct object rob; |
struct object rob; |
char *s; |
char *s; |
Line 352 static char **oxsBuildArgv(struct object ob) { |
|
Line 434 static char **oxsBuildArgv(struct object ob) { |
|
char *ext, *v; |
char *ext, *v; |
int usetmp=1; |
int usetmp=1; |
int win=0; |
int win=0; |
|
struct object ocmd; |
|
|
/* bug: win variable must be properly set on windows native. */ |
/* bug: win variable must be properly set on windows native. */ |
|
|
Line 367 static char **oxsBuildArgv(struct object ob) { |
|
Line 450 static char **oxsBuildArgv(struct object ob) { |
|
|
|
s = KopString(getoa(ob,0)); |
s = KopString(getoa(ob,0)); |
s = oxEvalEnvVar(s); |
s = oxEvalEnvVar(s); |
argv[0] = oxWhich(s,(char *)getenv("PATH")); |
ocmd = KoxWhich(KpoString(s),KpoInteger(0)); |
|
if (ocmd.tag != Sdollar) { |
|
argv[0] = NULL; |
|
}else{ |
|
argv[0] = KopString(ocmd); |
|
} |
argv[1] = (char *)NULL; |
argv[1] = (char *)NULL; |
if (argv[0] == NULL) { |
if (argv[0] == NULL) { |
fprintf(stderr,"cmdname=%s\n",s); |
fprintf(stderr,"cmdname=%s\n",s); |
Line 425 static struct object oxsExecuteBlocked(struct object o |
|
Line 513 static struct object oxsExecuteBlocked(struct object o |
|
int r,i,n; |
int r,i,n; |
char **argv; |
char **argv; |
|
|
/* bug: Set stdout, stderr to result variables. */ |
|
argv = oxsBuildArgv(ob); |
argv = oxsBuildArgv(ob); |
|
argv = oxsBuildArgvRedirect(argv); |
r=oxForkExecBlocked(argv); /* bug: what happen when NoX? */ |
r=oxForkExecBlocked(argv); /* bug: what happen when NoX? */ |
/* |
/* |
if (1) { |
if (1) { |
Line 445 static struct object oxsExecuteBlocked(struct object o |
|
Line 533 static struct object oxsExecuteBlocked(struct object o |
|
|
|
if (AfterD > 0) { |
if (AfterD > 0) { |
for (i=0; i< AfterD; i++) { |
for (i=0; i< AfterD; i++) { |
/* oxDeleteFile(AfterDeleteFile[i]); not implemented. */ |
if (!KeepTmpFiles) { |
|
oxDeleteFile(AfterDeleteFile[i]); |
|
} |
} |
} |
} |
} |
AfterD = 0; |
AfterD = 0; |
|
|
return(KpoInteger(r)); |
return(KpoInteger(r)); |
|
} |
|
|
|
static char **oxsBuildArgvRedirect(char **argv) { |
|
char **newargv; |
|
int n,i,j; |
|
FILE *fp; |
|
char *fname; |
|
|
|
n = 0; while (argv[n] != NULL) n++; |
|
newargv = (char **) mymalloc(sizeof(char *)*(n+1)); |
|
for (i=0; i<=n; i++) newargv[i]=(char *)NULL; |
|
j=0; |
|
/* bug: Critical area, do not make an interruption. */ |
|
for (i=0; i<n; i++) { |
|
if (strcmp(argv[i],"<")==0) { |
|
fname=argv[i+1]; |
|
OX_P_stdin = open(fname,O_RDONLY); |
|
if (OX_P_stdin < 0) { |
|
OX_P_stdin = -1; |
|
oxResetRedirect(); |
|
errorKan1("%s\n","oxsBuildArgvRedirect fails to open the input file."); |
|
} |
|
i++; |
|
}else if (strcmp(argv[i],">")==0) { |
|
fname = argv[i+1]; |
|
fp == NULL; |
|
if (fname != NULL) { |
|
fp = fopen(fname,"w"); |
|
} |
|
if (fp == NULL) { |
|
oxResetRedirect(); |
|
errorKan1("%s\n","oxsBuildArgvRedirect, cannot open the output file.\n"); |
|
} |
|
fclose(fp); /* touch */ |
|
OX_P_stdout = open(fname,O_WRONLY); |
|
i++; |
|
}else if (strcmp(argv[i],"2>") == 0) { |
|
fname = argv[i+1]; |
|
fp == NULL; |
|
if (fname != NULL) { |
|
fp = fopen(fname,"w"); |
|
} |
|
if (fp == NULL) { |
|
oxResetRedirect(); |
|
errorKan1("%s\n","oxsBuildArgvRedirect, cannot open the output (stderr) file.\n"); |
|
} |
|
fclose(fp); /* touch */ |
|
OX_P_stderr = open(fname,O_WRONLY); |
|
i++; |
|
}else{ |
|
newargv[j++] = argv[i]; |
|
} |
|
} |
|
return( newargv ); |
} |
} |