2007-08-28 03:28:29 +00:00
|
|
|
// Shell.
|
|
|
|
|
2019-06-11 13:57:14 +00:00
|
|
|
#include "kernel/types.h"
|
|
|
|
#include "user/user.h"
|
|
|
|
#include "kernel/fcntl.h"
|
2006-08-11 13:55:18 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
// Parsed command representation
|
|
|
|
#define EXEC 1
|
|
|
|
#define REDIR 2
|
|
|
|
#define PIPE 3
|
|
|
|
#define LIST 4
|
|
|
|
#define BACK 5
|
2006-08-23 01:09:24 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
#define MAXARGS 10
|
2006-09-06 15:32:21 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
struct cmd {
|
|
|
|
int type;
|
2006-08-23 01:09:24 +00:00
|
|
|
};
|
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
struct execcmd {
|
|
|
|
int type;
|
2006-09-06 15:32:21 +00:00
|
|
|
char *argv[MAXARGS];
|
2007-08-28 03:28:29 +00:00
|
|
|
char *eargv[MAXARGS];
|
2006-09-06 15:32:21 +00:00
|
|
|
};
|
2006-08-23 01:09:24 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
struct redircmd {
|
|
|
|
int type;
|
|
|
|
struct cmd *cmd;
|
|
|
|
char *file;
|
|
|
|
char *efile;
|
|
|
|
int mode;
|
|
|
|
int fd;
|
|
|
|
};
|
2006-08-23 01:09:24 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
struct pipecmd {
|
|
|
|
int type;
|
|
|
|
struct cmd *left;
|
|
|
|
struct cmd *right;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct listcmd {
|
|
|
|
int type;
|
|
|
|
struct cmd *left;
|
|
|
|
struct cmd *right;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct backcmd {
|
|
|
|
int type;
|
|
|
|
struct cmd *cmd;
|
|
|
|
};
|
|
|
|
|
2007-08-28 03:46:58 +00:00
|
|
|
int fork1(void); // Fork but panics on failure.
|
2007-08-28 03:28:29 +00:00
|
|
|
void panic(char*);
|
2007-08-28 03:46:58 +00:00
|
|
|
struct cmd *parsecmd(char*);
|
2022-08-02 13:38:22 +00:00
|
|
|
void runcmd(struct cmd*) __attribute__((noreturn));
|
2007-08-28 03:28:29 +00:00
|
|
|
|
|
|
|
// Execute cmd. Never returns.
|
|
|
|
void
|
|
|
|
runcmd(struct cmd *cmd)
|
|
|
|
{
|
|
|
|
int p[2];
|
|
|
|
struct backcmd *bcmd;
|
|
|
|
struct execcmd *ecmd;
|
|
|
|
struct listcmd *lcmd;
|
|
|
|
struct pipecmd *pcmd;
|
|
|
|
struct redircmd *rcmd;
|
|
|
|
|
|
|
|
if(cmd == 0)
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
switch(cmd->type){
|
|
|
|
default:
|
|
|
|
panic("runcmd");
|
|
|
|
|
|
|
|
case EXEC:
|
|
|
|
ecmd = (struct execcmd*)cmd;
|
|
|
|
if(ecmd->argv[0] == 0)
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-28 03:28:29 +00:00
|
|
|
exec(ecmd->argv[0], ecmd->argv);
|
2019-08-27 17:13:03 +00:00
|
|
|
fprintf(2, "exec %s failed\n", ecmd->argv[0]);
|
2007-08-28 03:28:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case REDIR:
|
|
|
|
rcmd = (struct redircmd*)cmd;
|
|
|
|
close(rcmd->fd);
|
|
|
|
if(open(rcmd->file, rcmd->mode) < 0){
|
2019-08-27 17:13:03 +00:00
|
|
|
fprintf(2, "open %s failed\n", rcmd->file);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-28 03:28:29 +00:00
|
|
|
}
|
|
|
|
runcmd(rcmd->cmd);
|
|
|
|
break;
|
|
|
|
|
2007-08-28 03:46:58 +00:00
|
|
|
case LIST:
|
|
|
|
lcmd = (struct listcmd*)cmd;
|
|
|
|
if(fork1() == 0)
|
|
|
|
runcmd(lcmd->left);
|
2019-09-10 16:30:10 +00:00
|
|
|
wait(0);
|
2007-08-28 03:46:58 +00:00
|
|
|
runcmd(lcmd->right);
|
|
|
|
break;
|
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
case PIPE:
|
|
|
|
pcmd = (struct pipecmd*)cmd;
|
|
|
|
if(pipe(p) < 0)
|
|
|
|
panic("pipe");
|
|
|
|
if(fork1() == 0){
|
|
|
|
close(1);
|
|
|
|
dup(p[1]);
|
|
|
|
close(p[0]);
|
|
|
|
close(p[1]);
|
|
|
|
runcmd(pcmd->left);
|
|
|
|
}
|
|
|
|
if(fork1() == 0){
|
|
|
|
close(0);
|
|
|
|
dup(p[0]);
|
|
|
|
close(p[0]);
|
|
|
|
close(p[1]);
|
|
|
|
runcmd(pcmd->right);
|
|
|
|
}
|
|
|
|
close(p[0]);
|
|
|
|
close(p[1]);
|
2019-09-10 16:30:10 +00:00
|
|
|
wait(0);
|
|
|
|
wait(0);
|
2007-08-28 03:28:29 +00:00
|
|
|
break;
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
case BACK:
|
|
|
|
bcmd = (struct backcmd*)cmd;
|
|
|
|
if(fork1() == 0)
|
|
|
|
runcmd(bcmd->cmd);
|
|
|
|
break;
|
2006-08-23 01:09:24 +00:00
|
|
|
}
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(0);
|
2006-08-23 01:09:24 +00:00
|
|
|
}
|
|
|
|
|
2007-08-10 17:00:00 +00:00
|
|
|
int
|
|
|
|
getcmd(char *buf, int nbuf)
|
|
|
|
{
|
2019-08-27 17:13:03 +00:00
|
|
|
fprintf(2, "$ ");
|
2007-08-10 17:00:00 +00:00
|
|
|
memset(buf, 0, nbuf);
|
|
|
|
gets(buf, nbuf);
|
|
|
|
if(buf[0] == 0) // EOF
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-06 17:27:19 +00:00
|
|
|
int
|
2007-08-28 03:28:29 +00:00
|
|
|
main(void)
|
2006-08-23 01:09:24 +00:00
|
|
|
{
|
2007-08-28 03:28:29 +00:00
|
|
|
static char buf[100];
|
2007-08-28 03:46:58 +00:00
|
|
|
int fd;
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2016-08-08 17:06:38 +00:00
|
|
|
// Ensure that three file descriptors are open.
|
2007-08-28 03:46:58 +00:00
|
|
|
while((fd = open("console", O_RDWR)) >= 0){
|
|
|
|
if(fd >= 3){
|
|
|
|
close(fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2007-08-28 03:46:58 +00:00
|
|
|
// Read and run input commands.
|
2007-08-28 18:32:08 +00:00
|
|
|
while(getcmd(buf, sizeof(buf)) >= 0){
|
2007-08-30 18:30:26 +00:00
|
|
|
if(buf[0] == 'c' && buf[1] == 'd' && buf[2] == ' '){
|
2016-08-08 17:06:38 +00:00
|
|
|
// Chdir must be called by the parent, not the child.
|
2007-08-30 18:30:26 +00:00
|
|
|
buf[strlen(buf)-1] = 0; // chop \n
|
|
|
|
if(chdir(buf+3) < 0)
|
2019-08-27 17:13:03 +00:00
|
|
|
fprintf(2, "cannot cd %s\n", buf+3);
|
2007-08-30 18:30:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-08-28 03:28:29 +00:00
|
|
|
if(fork1() == 0)
|
|
|
|
runcmd(parsecmd(buf));
|
2019-09-10 16:30:10 +00:00
|
|
|
wait(0);
|
2006-09-06 15:32:21 +00:00
|
|
|
}
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(0);
|
2007-08-28 03:28:29 +00:00
|
|
|
}
|
2006-09-06 17:27:19 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
void
|
|
|
|
panic(char *s)
|
|
|
|
{
|
2019-08-27 17:13:03 +00:00
|
|
|
fprintf(2, "%s\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-28 03:28:29 +00:00
|
|
|
}
|
2006-09-06 15:32:21 +00:00
|
|
|
|
2007-08-28 03:46:58 +00:00
|
|
|
int
|
|
|
|
fork1(void)
|
|
|
|
{
|
|
|
|
int pid;
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2007-08-28 03:46:58 +00:00
|
|
|
pid = fork();
|
|
|
|
if(pid == -1)
|
|
|
|
panic("fork");
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
//PAGEBREAK!
|
2007-08-28 03:28:29 +00:00
|
|
|
// Constructors
|
2006-08-23 01:09:24 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
struct cmd*
|
|
|
|
execcmd(void)
|
|
|
|
{
|
|
|
|
struct execcmd *cmd;
|
2006-09-06 17:27:19 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
cmd = malloc(sizeof(*cmd));
|
|
|
|
memset(cmd, 0, sizeof(*cmd));
|
|
|
|
cmd->type = EXEC;
|
|
|
|
return (struct cmd*)cmd;
|
|
|
|
}
|
2006-09-06 17:27:19 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
struct cmd*
|
|
|
|
redircmd(struct cmd *subcmd, char *file, char *efile, int mode, int fd)
|
|
|
|
{
|
|
|
|
struct redircmd *cmd;
|
|
|
|
|
|
|
|
cmd = malloc(sizeof(*cmd));
|
|
|
|
memset(cmd, 0, sizeof(*cmd));
|
|
|
|
cmd->type = REDIR;
|
|
|
|
cmd->cmd = subcmd;
|
|
|
|
cmd->file = file;
|
|
|
|
cmd->efile = efile;
|
|
|
|
cmd->mode = mode;
|
|
|
|
cmd->fd = fd;
|
|
|
|
return (struct cmd*)cmd;
|
2006-08-11 13:55:18 +00:00
|
|
|
}
|
2006-08-14 21:22:13 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
struct cmd*
|
|
|
|
pipecmd(struct cmd *left, struct cmd *right)
|
2006-08-23 01:09:24 +00:00
|
|
|
{
|
2007-08-28 03:28:29 +00:00
|
|
|
struct pipecmd *cmd;
|
|
|
|
|
|
|
|
cmd = malloc(sizeof(*cmd));
|
|
|
|
memset(cmd, 0, sizeof(*cmd));
|
|
|
|
cmd->type = PIPE;
|
|
|
|
cmd->left = left;
|
|
|
|
cmd->right = right;
|
|
|
|
return (struct cmd*)cmd;
|
|
|
|
}
|
2006-08-23 01:09:24 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
struct cmd*
|
|
|
|
listcmd(struct cmd *left, struct cmd *right)
|
|
|
|
{
|
|
|
|
struct listcmd *cmd;
|
|
|
|
|
|
|
|
cmd = malloc(sizeof(*cmd));
|
|
|
|
memset(cmd, 0, sizeof(*cmd));
|
|
|
|
cmd->type = LIST;
|
|
|
|
cmd->left = left;
|
|
|
|
cmd->right = right;
|
|
|
|
return (struct cmd*)cmd;
|
|
|
|
}
|
2006-08-23 01:09:24 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
struct cmd*
|
|
|
|
backcmd(struct cmd *subcmd)
|
|
|
|
{
|
|
|
|
struct backcmd *cmd;
|
2006-08-23 01:09:24 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
cmd = malloc(sizeof(*cmd));
|
|
|
|
memset(cmd, 0, sizeof(*cmd));
|
|
|
|
cmd->type = BACK;
|
|
|
|
cmd->cmd = subcmd;
|
|
|
|
return (struct cmd*)cmd;
|
2006-08-23 01:09:24 +00:00
|
|
|
}
|
2007-08-28 03:46:58 +00:00
|
|
|
//PAGEBREAK!
|
2007-08-28 03:28:29 +00:00
|
|
|
// Parsing
|
|
|
|
|
|
|
|
char whitespace[] = " \t\r\n\v";
|
|
|
|
char symbols[] = "<|>&;()";
|
|
|
|
|
|
|
|
int
|
|
|
|
gettoken(char **ps, char *es, char **q, char **eq)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int ret;
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
s = *ps;
|
|
|
|
while(s < es && strchr(whitespace, *s))
|
|
|
|
s++;
|
|
|
|
if(q)
|
|
|
|
*q = s;
|
|
|
|
ret = *s;
|
|
|
|
switch(*s){
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case '|':
|
|
|
|
case '(':
|
|
|
|
case ')':
|
|
|
|
case ';':
|
|
|
|
case '&':
|
|
|
|
case '<':
|
|
|
|
s++;
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
s++;
|
|
|
|
if(*s == '>'){
|
|
|
|
ret = '+';
|
|
|
|
s++;
|
2006-08-14 21:22:13 +00:00
|
|
|
}
|
2007-08-28 03:28:29 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = 'a';
|
|
|
|
while(s < es && !strchr(whitespace, *s) && !strchr(symbols, *s))
|
|
|
|
s++;
|
|
|
|
break;
|
2006-08-14 21:22:13 +00:00
|
|
|
}
|
2007-08-28 03:28:29 +00:00
|
|
|
if(eq)
|
|
|
|
*eq = s;
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
while(s < es && strchr(whitespace, *s))
|
|
|
|
s++;
|
|
|
|
*ps = s;
|
|
|
|
return ret;
|
2006-08-23 01:09:24 +00:00
|
|
|
}
|
|
|
|
|
2007-08-28 03:46:58 +00:00
|
|
|
int
|
|
|
|
peek(char **ps, char *es, char *toks)
|
|
|
|
{
|
|
|
|
char *s;
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2007-08-28 03:46:58 +00:00
|
|
|
s = *ps;
|
|
|
|
while(s < es && strchr(whitespace, *s))
|
|
|
|
s++;
|
|
|
|
*ps = s;
|
|
|
|
return *s && strchr(toks, *s);
|
|
|
|
}
|
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
struct cmd *parseline(char**, char*);
|
|
|
|
struct cmd *parsepipe(char**, char*);
|
|
|
|
struct cmd *parseexec(char**, char*);
|
2007-08-28 03:46:58 +00:00
|
|
|
struct cmd *nulterminate(struct cmd*);
|
2006-08-23 01:09:24 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
struct cmd*
|
|
|
|
parsecmd(char *s)
|
2006-08-23 01:09:24 +00:00
|
|
|
{
|
2007-08-28 03:28:29 +00:00
|
|
|
char *es;
|
|
|
|
struct cmd *cmd;
|
2010-07-02 18:51:53 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
es = s + strlen(s);
|
|
|
|
cmd = parseline(&s, es);
|
|
|
|
peek(&s, es, "");
|
|
|
|
if(s != es){
|
2019-08-27 17:13:03 +00:00
|
|
|
fprintf(2, "leftovers: %s\n", s);
|
2007-08-28 03:28:29 +00:00
|
|
|
panic("syntax");
|
2006-08-23 01:09:24 +00:00
|
|
|
}
|
2007-08-28 03:28:29 +00:00
|
|
|
nulterminate(cmd);
|
|
|
|
return cmd;
|
2006-08-14 21:22:13 +00:00
|
|
|
}
|
2006-08-23 01:09:24 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
struct cmd*
|
|
|
|
parseline(char **ps, char *es)
|
2006-08-23 01:09:24 +00:00
|
|
|
{
|
2007-08-28 03:28:29 +00:00
|
|
|
struct cmd *cmd;
|
2006-08-23 01:09:24 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
cmd = parsepipe(ps, es);
|
|
|
|
while(peek(ps, es, "&")){
|
|
|
|
gettoken(ps, es, 0, 0);
|
|
|
|
cmd = backcmd(cmd);
|
2006-08-23 01:09:24 +00:00
|
|
|
}
|
2007-08-28 03:28:29 +00:00
|
|
|
if(peek(ps, es, ";")){
|
|
|
|
gettoken(ps, es, 0, 0);
|
|
|
|
cmd = listcmd(cmd, parseline(ps, es));
|
|
|
|
}
|
|
|
|
return cmd;
|
|
|
|
}
|
2006-08-23 01:09:24 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
struct cmd*
|
|
|
|
parsepipe(char **ps, char *es)
|
|
|
|
{
|
|
|
|
struct cmd *cmd;
|
2010-07-02 18:51:53 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
cmd = parseexec(ps, es);
|
|
|
|
if(peek(ps, es, "|")){
|
|
|
|
gettoken(ps, es, 0, 0);
|
|
|
|
cmd = pipecmd(cmd, parsepipe(ps, es));
|
|
|
|
}
|
|
|
|
return cmd;
|
|
|
|
}
|
2006-08-23 01:09:24 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
struct cmd*
|
|
|
|
parseredirs(struct cmd *cmd, char **ps, char *es)
|
|
|
|
{
|
|
|
|
int tok;
|
|
|
|
char *q, *eq;
|
|
|
|
|
|
|
|
while(peek(ps, es, "<>")){
|
|
|
|
tok = gettoken(ps, es, 0, 0);
|
|
|
|
if(gettoken(ps, es, &q, &eq) != 'a')
|
|
|
|
panic("missing file for redirection");
|
|
|
|
switch(tok){
|
|
|
|
case '<':
|
|
|
|
cmd = redircmd(cmd, q, eq, O_RDONLY, 0);
|
|
|
|
break;
|
|
|
|
case '>':
|
2020-07-16 15:38:08 +00:00
|
|
|
cmd = redircmd(cmd, q, eq, O_WRONLY|O_CREATE|O_TRUNC, 1);
|
2007-08-28 03:28:29 +00:00
|
|
|
break;
|
|
|
|
case '+': // >>
|
|
|
|
cmd = redircmd(cmd, q, eq, O_WRONLY|O_CREATE, 1);
|
|
|
|
break;
|
|
|
|
}
|
2006-08-23 01:09:24 +00:00
|
|
|
}
|
2007-08-28 03:28:29 +00:00
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
|
2007-08-28 03:46:58 +00:00
|
|
|
struct cmd*
|
|
|
|
parseblock(char **ps, char *es)
|
|
|
|
{
|
|
|
|
struct cmd *cmd;
|
|
|
|
|
|
|
|
if(!peek(ps, es, "("))
|
|
|
|
panic("parseblock");
|
|
|
|
gettoken(ps, es, 0, 0);
|
|
|
|
cmd = parseline(ps, es);
|
|
|
|
if(!peek(ps, es, ")"))
|
|
|
|
panic("syntax - missing )");
|
|
|
|
gettoken(ps, es, 0, 0);
|
|
|
|
cmd = parseredirs(cmd, ps, es);
|
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
struct cmd*
|
|
|
|
parseexec(char **ps, char *es)
|
|
|
|
{
|
|
|
|
char *q, *eq;
|
|
|
|
int tok, argc;
|
|
|
|
struct execcmd *cmd;
|
|
|
|
struct cmd *ret;
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
if(peek(ps, es, "("))
|
|
|
|
return parseblock(ps, es);
|
|
|
|
|
|
|
|
ret = execcmd();
|
|
|
|
cmd = (struct execcmd*)ret;
|
|
|
|
|
|
|
|
argc = 0;
|
|
|
|
ret = parseredirs(ret, ps, es);
|
|
|
|
while(!peek(ps, es, "|)&;")){
|
|
|
|
if((tok=gettoken(ps, es, &q, &eq)) == 0)
|
|
|
|
break;
|
|
|
|
if(tok != 'a')
|
|
|
|
panic("syntax");
|
|
|
|
cmd->argv[argc] = q;
|
|
|
|
cmd->eargv[argc] = eq;
|
|
|
|
argc++;
|
|
|
|
if(argc >= MAXARGS)
|
|
|
|
panic("too many args");
|
|
|
|
ret = parseredirs(ret, ps, es);
|
2006-08-23 01:09:24 +00:00
|
|
|
}
|
2007-08-28 03:28:29 +00:00
|
|
|
cmd->argv[argc] = 0;
|
|
|
|
cmd->eargv[argc] = 0;
|
|
|
|
return ret;
|
2006-08-23 01:09:24 +00:00
|
|
|
}
|
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
// NUL-terminate all the counted strings.
|
2007-08-28 04:19:47 +00:00
|
|
|
struct cmd*
|
2007-08-28 03:28:29 +00:00
|
|
|
nulterminate(struct cmd *cmd)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct backcmd *bcmd;
|
|
|
|
struct execcmd *ecmd;
|
|
|
|
struct listcmd *lcmd;
|
|
|
|
struct pipecmd *pcmd;
|
|
|
|
struct redircmd *rcmd;
|
|
|
|
|
|
|
|
if(cmd == 0)
|
2007-08-28 03:46:58 +00:00
|
|
|
return 0;
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
switch(cmd->type){
|
|
|
|
case EXEC:
|
|
|
|
ecmd = (struct execcmd*)cmd;
|
|
|
|
for(i=0; ecmd->argv[i]; i++)
|
|
|
|
*ecmd->eargv[i] = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REDIR:
|
|
|
|
rcmd = (struct redircmd*)cmd;
|
|
|
|
nulterminate(rcmd->cmd);
|
|
|
|
*rcmd->efile = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PIPE:
|
|
|
|
pcmd = (struct pipecmd*)cmd;
|
|
|
|
nulterminate(pcmd->left);
|
|
|
|
nulterminate(pcmd->right);
|
|
|
|
break;
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2007-08-28 03:28:29 +00:00
|
|
|
case LIST:
|
|
|
|
lcmd = (struct listcmd*)cmd;
|
|
|
|
nulterminate(lcmd->left);
|
|
|
|
nulterminate(lcmd->right);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BACK:
|
|
|
|
bcmd = (struct backcmd*)cmd;
|
|
|
|
nulterminate(bcmd->cmd);
|
|
|
|
break;
|
|
|
|
}
|
2007-08-28 03:46:58 +00:00
|
|
|
return cmd;
|
2007-08-28 03:28:29 +00:00
|
|
|
}
|