2006-08-11 15:55:18 +02:00
|
|
|
#include "types.h"
|
2006-08-14 23:22:13 +02:00
|
|
|
#include "stat.h"
|
|
|
|
#include "user.h"
|
2006-08-11 15:55:18 +02:00
|
|
|
#include "fs.h"
|
|
|
|
#include "fcntl.h"
|
|
|
|
|
2006-08-23 03:09:24 +02:00
|
|
|
#define BUFSIZ 512
|
|
|
|
#define MAXARGS 10
|
2006-09-07 05:16:15 +02:00
|
|
|
#define MAXIO 2
|
2006-09-06 17:32:21 +02:00
|
|
|
#define MAXCMD 2
|
2006-08-23 03:09:24 +02:00
|
|
|
|
2006-09-06 17:32:21 +02:00
|
|
|
// an embarrassingly naive shell
|
|
|
|
|
|
|
|
// some day a real parse tree; for now ad-hoc
|
|
|
|
struct ionode {
|
2006-08-23 03:09:24 +02:00
|
|
|
int token;
|
|
|
|
char *s;
|
|
|
|
};
|
|
|
|
|
2006-09-06 17:32:21 +02:00
|
|
|
struct cmd {
|
|
|
|
char *argv[MAXARGS];
|
|
|
|
char argv0buf[BUFSIZ];
|
|
|
|
int argc;
|
|
|
|
int token;
|
2006-09-07 05:16:15 +02:00
|
|
|
struct ionode iolist[MAXIO];
|
2006-09-07 04:15:28 +02:00
|
|
|
struct ionode *io;
|
2006-09-06 17:32:21 +02:00
|
|
|
};
|
2006-09-06 19:27:19 +02:00
|
|
|
struct cmd cmdlist[MAXCMD];
|
2006-09-07 04:15:28 +02:00
|
|
|
struct cmd *cmd;
|
2006-08-23 03:09:24 +02:00
|
|
|
|
2006-09-06 17:32:21 +02:00
|
|
|
char buf[BUFSIZ];
|
2007-08-10 19:00:00 +02:00
|
|
|
int debug;
|
2006-08-23 03:09:24 +02:00
|
|
|
|
|
|
|
int parse(char *s);
|
|
|
|
void runcmd(void);
|
2007-08-10 19:00:00 +02:00
|
|
|
int getcmd(char *buf, int nbuf);
|
2006-09-07 04:15:28 +02:00
|
|
|
int ioredirection(struct ionode *iolist, int nio);
|
2006-08-23 03:09:24 +02:00
|
|
|
int gettoken(char *s, char **token);
|
|
|
|
int _gettoken(char *s, char **p1, char **p2);
|
2006-08-11 15:55:18 +02:00
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
2007-08-22 08:01:32 +02:00
|
|
|
while(getcmd(buf, sizeof(buf)) >= 0) {
|
2007-08-10 19:00:00 +02:00
|
|
|
if(parse(buf) >= 0)
|
|
|
|
runcmd();
|
2006-08-23 03:09:24 +02:00
|
|
|
}
|
2007-08-08 10:39:07 +02:00
|
|
|
exit();
|
2006-08-23 03:09:24 +02:00
|
|
|
}
|
|
|
|
|
2007-08-10 19:00:00 +02:00
|
|
|
int
|
|
|
|
getcmd(char *buf, int nbuf)
|
|
|
|
{
|
2007-08-24 22:03:40 +02:00
|
|
|
printf(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
|
2006-08-23 03:09:24 +02:00
|
|
|
parse(char *s)
|
|
|
|
{
|
|
|
|
char *t;
|
2006-09-06 17:32:21 +02:00
|
|
|
int c, i;
|
2006-08-23 03:09:24 +02:00
|
|
|
|
|
|
|
gettoken(s, 0);
|
|
|
|
|
2007-08-10 19:00:00 +02:00
|
|
|
cmd = &cmdlist[0];
|
2006-09-06 19:27:19 +02:00
|
|
|
for(i = 0; i < MAXCMD; i++) {
|
2006-09-06 17:32:21 +02:00
|
|
|
cmdlist[i].argc = 0;
|
|
|
|
cmdlist[i].token = 0;
|
2006-09-07 04:15:28 +02:00
|
|
|
cmdlist[i].io = cmdlist[i].iolist;
|
2006-09-06 17:32:21 +02:00
|
|
|
}
|
2007-08-10 19:00:00 +02:00
|
|
|
for(;;){
|
2006-09-06 19:27:19 +02:00
|
|
|
switch((c = gettoken(0, &t))) {
|
2006-08-23 03:09:24 +02:00
|
|
|
|
2006-09-06 19:04:06 +02:00
|
|
|
case 'w': // Add an argument
|
2006-09-07 04:15:28 +02:00
|
|
|
if(cmd->argc >= MAXARGS) {
|
2006-09-06 19:04:06 +02:00
|
|
|
printf(2, "too many arguments\n");
|
|
|
|
return -1;
|
2006-08-20 01:41:34 +02:00
|
|
|
}
|
2006-09-07 04:15:28 +02:00
|
|
|
cmd->argv[cmd->argc++] = t;
|
2006-08-23 03:09:24 +02:00
|
|
|
break;
|
2006-09-06 19:27:19 +02:00
|
|
|
|
2006-09-07 05:16:15 +02:00
|
|
|
case '>': // Input and output redirection
|
|
|
|
case '<':
|
2006-08-23 03:09:24 +02:00
|
|
|
// Grab the filename from the argument list
|
2006-09-06 19:27:19 +02:00
|
|
|
if(gettoken(0, &t) != 'w') {
|
2006-09-07 05:16:15 +02:00
|
|
|
printf(2, "syntax error: > not followed by word\n");
|
2006-09-06 19:04:06 +02:00
|
|
|
return -1;
|
2006-08-23 03:09:24 +02:00
|
|
|
}
|
2006-09-07 05:16:15 +02:00
|
|
|
if(cmd->io - cmd->iolist >= MAXIO) {
|
|
|
|
printf(2, "too many redirections\n");
|
2006-09-06 19:04:06 +02:00
|
|
|
return -1;
|
2006-08-23 03:09:24 +02:00
|
|
|
}
|
2006-09-07 05:16:15 +02:00
|
|
|
cmd->io->token = c;
|
2006-09-07 04:15:28 +02:00
|
|
|
cmd->io->s = t;
|
|
|
|
cmd->io++;
|
2006-09-06 17:32:21 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ';': // command sequence
|
|
|
|
case '|': // pipe
|
2006-09-07 05:16:15 +02:00
|
|
|
if(cmd->io - cmd->iolist >= MAXIO) {
|
|
|
|
printf(2, "too many redirections\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-09-07 04:15:28 +02:00
|
|
|
cmd->token = c;
|
|
|
|
cmd++;
|
2006-08-23 03:09:24 +02:00
|
|
|
break;
|
|
|
|
|
2006-09-06 19:04:06 +02:00
|
|
|
case 0: // String is complete
|
2006-08-23 03:09:24 +02:00
|
|
|
return 0;
|
2006-09-06 19:27:19 +02:00
|
|
|
|
2006-08-23 03:09:24 +02:00
|
|
|
default:
|
|
|
|
printf(2, "syntax error: bad return %d from gettoken", c);
|
|
|
|
return -1;
|
2006-09-06 19:27:19 +02:00
|
|
|
|
2006-08-11 15:55:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-08-14 23:22:13 +02:00
|
|
|
|
|
|
|
void
|
2006-08-23 03:09:24 +02:00
|
|
|
runcmd(void)
|
|
|
|
{
|
2006-09-07 04:15:28 +02:00
|
|
|
int i, r, pid, tfd;
|
2006-09-06 17:32:21 +02:00
|
|
|
int fdarray[2];
|
2006-09-07 04:15:28 +02:00
|
|
|
struct cmd *c;
|
|
|
|
struct ionode *io;
|
2006-08-23 03:09:24 +02:00
|
|
|
|
|
|
|
// Return immediately if command line was empty.
|
2006-09-06 17:32:21 +02:00
|
|
|
if(cmdlist[0].argc == 0) {
|
2006-09-06 19:27:19 +02:00
|
|
|
if(debug)
|
2006-08-23 03:09:24 +02:00
|
|
|
printf(2, "EMPTY COMMAND\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-09-07 04:15:28 +02:00
|
|
|
for(c = &cmdlist[0]; c <= cmd; c++) {
|
2006-09-06 17:32:21 +02:00
|
|
|
// Clean up command line.
|
|
|
|
// Read all commands from the filesystem: add an initial '/' to
|
|
|
|
// the command name.
|
|
|
|
// This essentially acts like 'PATH=/'.
|
2006-09-07 04:15:28 +02:00
|
|
|
if(c->argv[0][0] != '/') {
|
|
|
|
c->argv0buf[0] = '/';
|
|
|
|
strcpy(c->argv0buf + 1, c->argv[0]);
|
|
|
|
c->argv[0] = c->argv0buf;
|
2006-09-06 17:32:21 +02:00
|
|
|
}
|
2006-09-07 04:15:28 +02:00
|
|
|
c->argv[c->argc] = 0;
|
2006-09-06 19:27:19 +02:00
|
|
|
|
2006-09-06 17:32:21 +02:00
|
|
|
// Print the command.
|
2006-09-06 19:27:19 +02:00
|
|
|
if(debug) {
|
2006-09-06 17:32:21 +02:00
|
|
|
printf(2, "[%d] SPAWN:", getpid());
|
2006-09-07 04:15:28 +02:00
|
|
|
for(i = 0; c->argv[i]; i++)
|
|
|
|
printf(2, " %s", c->argv[i]);
|
|
|
|
for(io = c->iolist; io <= c->io; io++) {
|
|
|
|
printf(2, "%c %s", io->token, io->s);
|
2006-09-06 17:32:21 +02:00
|
|
|
}
|
|
|
|
printf(2, "\n");
|
2006-08-23 03:09:24 +02:00
|
|
|
}
|
|
|
|
|
2006-09-07 04:15:28 +02:00
|
|
|
if(strcmp(c->argv[0], "/cd") == 0) {
|
2006-09-06 19:57:47 +02:00
|
|
|
if(debug)
|
2006-09-07 04:15:28 +02:00
|
|
|
printf (2, "/cd %s is build in\n", c->argv[1]);
|
|
|
|
chdir(c->argv[1]);
|
2006-09-06 17:32:21 +02:00
|
|
|
return;
|
2006-08-23 03:09:24 +02:00
|
|
|
}
|
|
|
|
|
2006-09-07 04:15:28 +02:00
|
|
|
if(c->token == '|')
|
2006-09-06 19:27:19 +02:00
|
|
|
if(pipe(fdarray) < 0)
|
2006-09-06 19:04:06 +02:00
|
|
|
printf(2, "cmd %d pipe failed\n", c);
|
2006-09-06 17:32:21 +02:00
|
|
|
|
|
|
|
pid = fork();
|
2006-09-06 19:27:19 +02:00
|
|
|
if(pid == 0) {
|
2006-09-07 04:15:28 +02:00
|
|
|
if(c->token == '|') {
|
2006-09-06 19:27:19 +02:00
|
|
|
if(close(1) < 0)
|
2006-09-06 19:04:06 +02:00
|
|
|
printf(2, "close 1 failed\n");
|
2006-09-06 19:27:19 +02:00
|
|
|
if((tfd = dup(fdarray[1])) < 0)
|
2006-09-06 19:04:06 +02:00
|
|
|
printf(2, "dup failed\n");
|
2006-09-06 19:27:19 +02:00
|
|
|
if(close(fdarray[0]) < 0)
|
2006-09-06 19:04:06 +02:00
|
|
|
printf(2, "close fdarray[0] failed\n");
|
2006-09-06 19:27:19 +02:00
|
|
|
if(close(fdarray[1]) < 0)
|
2006-09-06 19:04:06 +02:00
|
|
|
printf(2, "close fdarray[1] failed\n");
|
2006-09-06 17:32:21 +02:00
|
|
|
}
|
2006-09-07 04:15:28 +02:00
|
|
|
if(c > cmdlist && (c-1)->token == '|') {
|
2006-09-06 19:27:19 +02:00
|
|
|
if(close(0) < 0)
|
2006-09-06 19:04:06 +02:00
|
|
|
printf(2, "close 0 failed\n");
|
2006-09-06 19:27:19 +02:00
|
|
|
if((tfd = dup(fdarray[0])) < 0)
|
2006-09-06 19:04:06 +02:00
|
|
|
printf(2, "dup failed\n");
|
2006-09-06 19:27:19 +02:00
|
|
|
if(close(fdarray[0]) < 0)
|
2006-09-06 19:04:06 +02:00
|
|
|
printf(2, "close fdarray[0] failed\n");
|
2006-09-06 19:27:19 +02:00
|
|
|
if(close(fdarray[1]) < 0)
|
2006-09-06 19:04:06 +02:00
|
|
|
printf(2, "close fdarray[1] failed\n");
|
2006-09-06 17:32:21 +02:00
|
|
|
}
|
2006-09-07 04:15:28 +02:00
|
|
|
if(ioredirection(c->iolist, c->io - c->iolist) < 0)
|
2006-09-06 19:04:06 +02:00
|
|
|
exit();
|
2006-09-07 04:15:28 +02:00
|
|
|
if((r = exec(c->argv0buf, (char**) c->argv)) < 0) {
|
|
|
|
printf(2, "exec %s: %d\n", c->argv[0], r);
|
2006-09-06 19:04:06 +02:00
|
|
|
exit();
|
2006-09-06 17:32:21 +02:00
|
|
|
}
|
2006-09-06 19:27:19 +02:00
|
|
|
} else if(pid > 0) {
|
2006-09-06 17:32:21 +02:00
|
|
|
int p;
|
2006-09-06 19:27:19 +02:00
|
|
|
if(debug)
|
2006-09-06 19:04:06 +02:00
|
|
|
printf(2, "[%d] FORKED child %d\n", getpid(), pid);
|
2006-09-06 17:32:21 +02:00
|
|
|
|
2006-09-07 04:15:28 +02:00
|
|
|
if(c > cmdlist && (c-1)->token == '|') {
|
2006-09-06 19:04:06 +02:00
|
|
|
close(fdarray[0]);
|
|
|
|
close(fdarray[1]);
|
2006-09-06 17:32:21 +02:00
|
|
|
}
|
2006-09-07 04:15:28 +02:00
|
|
|
if(c->token != '|') {
|
2006-09-06 19:27:19 +02:00
|
|
|
if(debug)
|
2006-09-06 19:04:06 +02:00
|
|
|
printf(2, "[%d] WAIT for children\n", getpid());
|
|
|
|
do {
|
|
|
|
p = wait();
|
2006-09-06 19:27:19 +02:00
|
|
|
if(debug)
|
2006-09-06 19:04:06 +02:00
|
|
|
printf(2, "[%d] WAIT child %d finished\n", getpid(), p);
|
2006-09-06 19:27:19 +02:00
|
|
|
} while(p > 0);
|
|
|
|
if(debug)
|
2006-09-06 19:04:06 +02:00
|
|
|
printf(2, "[%d] wait finished\n", getpid());
|
2006-09-06 17:32:21 +02:00
|
|
|
}
|
|
|
|
}
|
2006-08-23 03:09:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-09-07 04:15:28 +02:00
|
|
|
ioredirection(struct ionode *iolist, int nio)
|
2006-08-14 23:22:13 +02:00
|
|
|
{
|
2006-09-07 04:15:28 +02:00
|
|
|
int fd;
|
|
|
|
struct ionode *io;
|
2006-08-23 03:09:24 +02:00
|
|
|
|
2006-09-07 04:15:28 +02:00
|
|
|
for(io = iolist; io < &iolist[nio]; io++) {
|
|
|
|
switch(io->token) {
|
2006-08-23 03:09:24 +02:00
|
|
|
case '<':
|
2006-09-06 19:27:19 +02:00
|
|
|
if(close(0) < 0)
|
2006-09-06 19:04:06 +02:00
|
|
|
printf(2, "close 0 failed\n");
|
2006-09-07 04:15:28 +02:00
|
|
|
if((fd = open(io->s, O_RDONLY)) < 0) {
|
|
|
|
printf(2, "failed to open %s for read: %d", io->s, fd);
|
2006-09-06 19:04:06 +02:00
|
|
|
return -1;
|
2006-08-23 03:09:24 +02:00
|
|
|
}
|
2006-09-06 19:27:19 +02:00
|
|
|
if(debug)
|
2006-09-07 04:15:28 +02:00
|
|
|
printf(2, "redirect 0 from %s\n", io->s);
|
2006-08-23 03:09:24 +02:00
|
|
|
break;
|
|
|
|
case '>':
|
2006-09-06 19:27:19 +02:00
|
|
|
if(close(1) < 0)
|
2006-09-06 19:04:06 +02:00
|
|
|
printf(2, "close 1 failed\n");
|
2006-09-07 04:15:28 +02:00
|
|
|
if((fd = open(io->s, O_WRONLY|O_CREATE)) < 0) {
|
|
|
|
printf(2, "failed to open %s for write: %d", io->s, fd);
|
2006-09-06 19:04:06 +02:00
|
|
|
exit();
|
2006-08-14 23:22:13 +02:00
|
|
|
}
|
2006-09-06 19:27:19 +02:00
|
|
|
if(debug)
|
2006-09-07 04:15:28 +02:00
|
|
|
printf(2, "redirect 1 to %s\n", io->s);
|
2006-08-23 03:09:24 +02:00
|
|
|
break;
|
2006-08-14 23:22:13 +02:00
|
|
|
}
|
|
|
|
}
|
2006-08-23 03:09:24 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// gettoken(s, 0) prepares gettoken for subsequent calls and returns 0.
|
|
|
|
// gettoken(0, token) parses a shell token from the previously set string,
|
|
|
|
// null-terminates that token, stores the token pointer in '*token',
|
|
|
|
// and returns a token ID (0, '<', '>', '|', or 'w').
|
|
|
|
// Subsequent calls to 'gettoken(0, token)' will return subsequent
|
|
|
|
// tokens from the string.
|
|
|
|
|
|
|
|
int
|
|
|
|
gettoken(char *s, char **p1)
|
|
|
|
{
|
|
|
|
static int c, nc;
|
2006-09-06 19:27:19 +02:00
|
|
|
static char *np1, *np2;
|
2006-08-23 03:09:24 +02:00
|
|
|
|
2006-09-06 19:27:19 +02:00
|
|
|
if(s) {
|
2006-08-23 03:09:24 +02:00
|
|
|
nc = _gettoken(s, &np1, &np2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
c = nc;
|
|
|
|
*p1 = np1;
|
|
|
|
nc = _gettoken(np2, &np1, &np2);
|
|
|
|
return c;
|
2006-08-14 23:22:13 +02:00
|
|
|
}
|
2006-08-23 03:09:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Get the next token from string s.
|
|
|
|
// Set *p1 to the beginning of the token and *p2 just past the token.
|
|
|
|
// Returns
|
2006-09-06 19:04:06 +02:00
|
|
|
// 0 for end-of-string;
|
|
|
|
// < for <;
|
|
|
|
// > for >;
|
|
|
|
// | for |;
|
|
|
|
// w for a word.
|
2006-08-23 03:09:24 +02:00
|
|
|
//
|
|
|
|
// Eventually (once we parse the space where the \0 will go),
|
|
|
|
// words get nul-terminated.
|
|
|
|
#define WHITESPACE " \t\r\n"
|
|
|
|
#define SYMBOLS "<|>&;()"
|
|
|
|
|
|
|
|
int
|
|
|
|
_gettoken(char *s, char **p1, char **p2)
|
|
|
|
{
|
|
|
|
int t;
|
|
|
|
|
2006-09-06 19:27:19 +02:00
|
|
|
if(s == 0) {
|
|
|
|
if(debug > 1)
|
2006-09-06 20:21:54 +02:00
|
|
|
printf(2, "GETTOKEN 0\n");
|
2006-08-23 03:09:24 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-06 19:27:19 +02:00
|
|
|
if(debug > 1)
|
2006-08-23 03:09:24 +02:00
|
|
|
printf(2, "GETTOKEN: %s\n", s);
|
|
|
|
|
|
|
|
*p1 = 0;
|
|
|
|
*p2 = 0;
|
|
|
|
|
2006-09-06 19:27:19 +02:00
|
|
|
while(strchr(WHITESPACE, *s))
|
2006-08-23 03:09:24 +02:00
|
|
|
*s++ = 0;
|
2006-09-06 19:27:19 +02:00
|
|
|
if(*s == 0) {
|
|
|
|
if(debug > 1)
|
2006-08-23 03:09:24 +02:00
|
|
|
printf(2, "EOL\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2006-09-06 19:27:19 +02:00
|
|
|
if(strchr(SYMBOLS, *s)) {
|
2006-08-23 03:09:24 +02:00
|
|
|
t = *s;
|
|
|
|
*p1 = s;
|
|
|
|
*s++ = 0;
|
|
|
|
*p2 = s;
|
2006-09-06 19:27:19 +02:00
|
|
|
if(debug > 1)
|
2006-08-23 03:09:24 +02:00
|
|
|
printf(2, "TOK %c\n", t);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
*p1 = s;
|
2006-09-06 19:27:19 +02:00
|
|
|
while(*s && !strchr(WHITESPACE SYMBOLS, *s))
|
2006-08-23 03:09:24 +02:00
|
|
|
s++;
|
|
|
|
*p2 = s;
|
2006-09-06 19:27:19 +02:00
|
|
|
if(debug > 1) {
|
2006-08-23 03:09:24 +02:00
|
|
|
t = **p2;
|
|
|
|
**p2 = 0;
|
|
|
|
printf(2, "WORD: %s\n", *p1);
|
|
|
|
**p2 = t;
|
|
|
|
}
|
|
|
|
return 'w';
|
|
|
|
}
|
|
|
|
|