Skip to content
Snippets Groups Projects
sh.c 7.17 KiB
Newer Older
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fs.h"
#include "fcntl.h"

kaashoek's avatar
kaashoek committed
#define BUFSIZ  512
#define MAXARGS  10
kaashoek's avatar
kaashoek committed
#define MAXIO 2
kaashoek's avatar
kaashoek committed
#define MAXCMD  2
kaashoek's avatar
kaashoek committed

kaashoek's avatar
kaashoek committed
// an embarrassingly naive shell

// some day a real parse tree; for now ad-hoc
struct ionode {
kaashoek's avatar
kaashoek committed
  int token;
  char *s;
};

kaashoek's avatar
kaashoek committed
struct cmd {
  char *argv[MAXARGS];
  char argv0buf[BUFSIZ];
  int argc;
  int token;
kaashoek's avatar
kaashoek committed
  struct ionode iolist[MAXIO];
  struct ionode *io;
kaashoek's avatar
kaashoek committed
};
rsc's avatar
rsc committed
struct cmd cmdlist[MAXCMD];
struct cmd *cmd;
kaashoek's avatar
kaashoek committed

kaashoek's avatar
kaashoek committed
char buf[BUFSIZ];
rsc's avatar
rsc committed
int debug;
kaashoek's avatar
kaashoek committed

int parse(char *s);
void runcmd(void);
rsc's avatar
rsc committed
int getcmd(char *buf, int nbuf);
int ioredirection(struct ionode *iolist, int nio);
kaashoek's avatar
kaashoek committed
int gettoken(char *s, char **token);
int _gettoken(char *s, char **p1, char **p2);
rsc's avatar
rsc committed
  while(getcmd(buf, sizeof buf) >= 0) {
    if(parse(buf) >= 0)
      runcmd();
kaashoek's avatar
kaashoek committed
  }
rsc's avatar
rsc committed
  exit();
kaashoek's avatar
kaashoek committed
}

rsc's avatar
rsc committed
int
getcmd(char *buf, int nbuf)
{
  puts("$ ");
  memset(buf, 0, nbuf);
  gets(buf, nbuf);
  if(buf[0] == 0) // EOF
    return -1;
  return 0;
}

rsc's avatar
rsc committed
int
kaashoek's avatar
kaashoek committed
parse(char *s)
{
  char *t;
kaashoek's avatar
kaashoek committed
  int c, i;
kaashoek's avatar
kaashoek committed

  gettoken(s, 0);

rsc's avatar
rsc committed
  cmd = &cmdlist[0];
rsc's avatar
rsc committed
  for(i = 0; i < MAXCMD; i++) {
kaashoek's avatar
kaashoek committed
    cmdlist[i].argc = 0;
    cmdlist[i].token = 0;
    cmdlist[i].io = cmdlist[i].iolist;
kaashoek's avatar
kaashoek committed
  }
rsc's avatar
rsc committed
  for(;;){
rsc's avatar
rsc committed
    switch((c = gettoken(0, &t))) {
kaashoek's avatar
kaashoek committed

    case 'w':   // Add an argument
      if(cmd->argc >= MAXARGS) {
        printf(2, "too many arguments\n");
        return -1;
kaashoek's avatar
kaashoek committed
      }
      cmd->argv[cmd->argc++] = t;
kaashoek's avatar
kaashoek committed
      break;
kaashoek's avatar
kaashoek committed
    case '>':   // Input and output redirection
    case '<':
kaashoek's avatar
kaashoek committed
      // Grab the filename from the argument list
rsc's avatar
rsc committed
      if(gettoken(0, &t) != 'w') {
kaashoek's avatar
kaashoek committed
        printf(2, "syntax error: > not followed by word\n");
kaashoek's avatar
kaashoek committed
      }
kaashoek's avatar
kaashoek committed
      if(cmd->io - cmd->iolist >= MAXIO) {
        printf(2, "too many redirections\n");
kaashoek's avatar
kaashoek committed
      }
kaashoek's avatar
kaashoek committed
      cmd->io->token = c;
      cmd->io->s = t;
      cmd->io++;
kaashoek's avatar
kaashoek committed
      break;

    case ';':  // command sequence
    case '|':  // pipe
kaashoek's avatar
kaashoek committed
      if(cmd->io - cmd->iolist >= MAXIO) {
        printf(2, "too many redirections\n");
        return -1;
      }
      cmd->token = c;
      cmd++;
kaashoek's avatar
kaashoek committed
      break;

    case 0:             // String is complete
kaashoek's avatar
kaashoek committed
      return 0;
kaashoek's avatar
kaashoek committed
    default:
      printf(2, "syntax error: bad return %d from gettoken", c);
      return -1;
kaashoek's avatar
kaashoek committed
runcmd(void)
{
  int i, r, pid, tfd;
kaashoek's avatar
kaashoek committed
  int fdarray[2];
  struct cmd *c;
  struct ionode *io;
kaashoek's avatar
kaashoek committed

  // Return immediately if command line was empty.
kaashoek's avatar
kaashoek committed
  if(cmdlist[0].argc == 0) {
rsc's avatar
rsc committed
    if(debug)
kaashoek's avatar
kaashoek committed
      printf(2, "EMPTY COMMAND\n");
    return;
  }

  for(c = &cmdlist[0]; c <= cmd; c++) {
kaashoek's avatar
kaashoek committed
    // Clean up command line.
    // Read all commands from the filesystem: add an initial '/' to
    // the command name.
    // This essentially acts like 'PATH=/'.
    if(c->argv[0][0] != '/') {
      c->argv0buf[0] = '/';
      strcpy(c->argv0buf + 1, c->argv[0]);
      c->argv[0] = c->argv0buf;
kaashoek's avatar
kaashoek committed
    }
    c->argv[c->argc] = 0;
kaashoek's avatar
kaashoek committed
    // Print the command.
rsc's avatar
rsc committed
    if(debug) {
kaashoek's avatar
kaashoek committed
      printf(2, "[%d] SPAWN:", getpid());
      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);
kaashoek's avatar
kaashoek committed
      }
      printf(2, "\n");
    if(strcmp(c->argv[0], "/cd") == 0) {
rsc's avatar
rsc committed
      if(debug)
        printf (2, "/cd %s is build in\n", c->argv[1]);
      chdir(c->argv[1]);
kaashoek's avatar
kaashoek committed
      return;
    if(c->token == '|')
rsc's avatar
rsc committed
      if(pipe(fdarray) < 0)
        printf(2, "cmd %d pipe failed\n", c);
kaashoek's avatar
kaashoek committed

    pid = fork();
rsc's avatar
rsc committed
    if(pid == 0) {
      if(c->token == '|') {
rsc's avatar
rsc committed
        if(close(1) < 0)
          printf(2, "close 1 failed\n");
rsc's avatar
rsc committed
        if((tfd = dup(fdarray[1])) < 0)
          printf(2, "dup failed\n");
rsc's avatar
rsc committed
        if(close(fdarray[0]) < 0)
          printf(2, "close fdarray[0] failed\n");
rsc's avatar
rsc committed
        if(close(fdarray[1]) < 0)
          printf(2, "close fdarray[1] failed\n");
kaashoek's avatar
kaashoek committed
      }
      if(c > cmdlist && (c-1)->token == '|') {
rsc's avatar
rsc committed
        if(close(0) < 0)
          printf(2, "close 0 failed\n");
rsc's avatar
rsc committed
        if((tfd = dup(fdarray[0])) < 0)
          printf(2, "dup failed\n");
rsc's avatar
rsc committed
        if(close(fdarray[0]) < 0)
          printf(2, "close fdarray[0] failed\n");
rsc's avatar
rsc committed
        if(close(fdarray[1]) < 0)
          printf(2, "close fdarray[1] failed\n");
kaashoek's avatar
kaashoek committed
      }
      if(ioredirection(c->iolist, c->io - c->iolist) < 0)
      if((r = exec(c->argv0buf, (char**) c->argv)) < 0) {
        printf(2, "exec %s: %d\n", c->argv[0], r);
kaashoek's avatar
kaashoek committed
      }
rsc's avatar
rsc committed
    } else if(pid > 0) {
kaashoek's avatar
kaashoek committed
      int p;
rsc's avatar
rsc committed
      if(debug)
        printf(2, "[%d] FORKED child %d\n", getpid(), pid);
kaashoek's avatar
kaashoek committed

      if(c > cmdlist && (c-1)->token == '|') {
        close(fdarray[0]);
        close(fdarray[1]);
kaashoek's avatar
kaashoek committed
      }
      if(c->token != '|') {
rsc's avatar
rsc committed
        if(debug)
          printf(2, "[%d] WAIT for children\n", getpid());
        do {
          p = wait();
rsc's avatar
rsc committed
          if(debug)
            printf(2, "[%d] WAIT child %d finished\n", getpid(), p);
rsc's avatar
rsc committed
        } while(p > 0);
        if(debug)
          printf(2, "[%d] wait finished\n", getpid());
kaashoek's avatar
kaashoek committed
      }
    }
ioredirection(struct ionode *iolist, int nio)
  int fd;
  struct ionode *io;
kaashoek's avatar
kaashoek committed

  for(io = iolist; io < &iolist[nio]; io++) {
    switch(io->token) {
kaashoek's avatar
kaashoek committed
    case '<':
rsc's avatar
rsc committed
      if(close(0) < 0)
        printf(2, "close 0 failed\n");
      if((fd = open(io->s, O_RDONLY)) < 0) {
        printf(2, "failed to open %s for read: %d", io->s, fd);
kaashoek's avatar
kaashoek committed
      }
rsc's avatar
rsc committed
      if(debug)
        printf(2, "redirect 0 from %s\n", io->s);
kaashoek's avatar
kaashoek committed
      break;
    case '>':
rsc's avatar
rsc committed
      if(close(1) < 0)
        printf(2, "close 1 failed\n");
      if((fd = open(io->s, O_WRONLY|O_CREATE)) < 0) {
        printf(2, "failed to open %s for write: %d", io->s, fd);
rsc's avatar
rsc committed
      if(debug)
        printf(2, "redirect 1 to %s\n", io->s);
kaashoek's avatar
kaashoek committed
      break;
kaashoek's avatar
kaashoek committed
  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;
rsc's avatar
rsc committed
  static char *np1, *np2;
kaashoek's avatar
kaashoek committed

rsc's avatar
rsc committed
  if(s) {
kaashoek's avatar
kaashoek committed
    nc = _gettoken(s, &np1, &np2);
    return 0;
  }
  c = nc;
  *p1 = np1;
  nc = _gettoken(np2, &np1, &np2);
  return c;
kaashoek's avatar
kaashoek committed


// Get the next token from string s.
// Set *p1 to the beginning of the token and *p2 just past the token.
// Returns
//      0 for end-of-string;
//      < for <;
//      > for >;
//      | for |;
//      w for a word.
kaashoek's avatar
kaashoek committed
//
// 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;

rsc's avatar
rsc committed
  if(s == 0) {
    if(debug > 1)
rsc's avatar
rsc committed
      printf(2, "GETTOKEN 0\n");
kaashoek's avatar
kaashoek committed
    return 0;
  }

rsc's avatar
rsc committed
  if(debug > 1)
kaashoek's avatar
kaashoek committed
    printf(2, "GETTOKEN: %s\n", s);

  *p1 = 0;
  *p2 = 0;

rsc's avatar
rsc committed
  while(strchr(WHITESPACE, *s))
kaashoek's avatar
kaashoek committed
    *s++ = 0;
rsc's avatar
rsc committed
  if(*s == 0) {
    if(debug > 1)
kaashoek's avatar
kaashoek committed
      printf(2, "EOL\n");
    return 0;
  }
rsc's avatar
rsc committed
  if(strchr(SYMBOLS, *s)) {
kaashoek's avatar
kaashoek committed
    t = *s;
    *p1 = s;
    *s++ = 0;
    *p2 = s;
rsc's avatar
rsc committed
    if(debug > 1)
kaashoek's avatar
kaashoek committed
      printf(2, "TOK %c\n", t);
    return t;
  }
  *p1 = s;
rsc's avatar
rsc committed
  while(*s && !strchr(WHITESPACE SYMBOLS, *s))
kaashoek's avatar
kaashoek committed
    s++;
  *p2 = s;
rsc's avatar
rsc committed
  if(debug > 1) {
kaashoek's avatar
kaashoek committed
    t = **p2;
    **p2 = 0;
    printf(2, "WORD: %s\n", *p1);
    **p2 = t;
  }
  return 'w';
}