xv6-riscv-kernel/user/sh.c

495 lines
8.4 KiB
C
Raw Normal View History

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