Skip to content

Commit

Permalink
formatting tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
rsc committed Aug 28, 2007
1 parent 2868ca0 commit a759b8a
Showing 1 changed file with 66 additions and 55 deletions.
121 changes: 66 additions & 55 deletions sh.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,9 @@ struct backcmd {
struct cmd *cmd;
};

struct cmd *parsecmd(char*);
int fork1(void); // Fork but panics on failure.
void panic(char*);

int
fork1(void)
{
int pid;

pid = fork();
if(pid == -1)
panic("fork");
return pid;
}
struct cmd *parsecmd(char*);

// Execute cmd. Never returns.
void
Expand Down Expand Up @@ -99,6 +89,14 @@ runcmd(struct cmd *cmd)
runcmd(rcmd->cmd);
break;

case LIST:
lcmd = (struct listcmd*)cmd;
if(fork1() == 0)
runcmd(lcmd->left);
wait();
runcmd(lcmd->right);
break;

case PIPE:
pcmd = (struct pipecmd*)cmd;
if(pipe(p) < 0)
Expand All @@ -123,14 +121,6 @@ runcmd(struct cmd *cmd)
wait();
break;

case LIST:
lcmd = (struct listcmd*)cmd;
if(fork1() == 0)
runcmd(lcmd->left);
wait();
runcmd(lcmd->right);
break;

case BACK:
bcmd = (struct backcmd*)cmd;
if(fork1() == 0)
Expand All @@ -155,7 +145,17 @@ int
main(void)
{
static char buf[100];

int fd;

// Assumes three file descriptors open.
while((fd = open("console", O_RDWR)) >= 0){
if(fd >= 3){
close(fd);
break;
}
}

// Read and run input commands.
while(getcmd(buf, sizeof(buf)) >= 0) {
if(fork1() == 0)
runcmd(parsecmd(buf));
Expand All @@ -171,6 +171,18 @@ panic(char *s)
exit();
}

int
fork1(void)
{
int pid;

pid = fork();
if(pid == -1)
panic("fork");
return pid;
}

//PAGEBREAK!
// Constructors

struct cmd*
Expand Down Expand Up @@ -237,24 +249,12 @@ backcmd(struct cmd *subcmd)
cmd->cmd = subcmd;
return (struct cmd*)cmd;
}

//PAGEBREAK!
// Parsing

char whitespace[] = " \t\r\n\v";
char symbols[] = "<|>&;()";

int
peek(char **ps, char *es, char *toks)
{
char *s;

s = *ps;
while(s < es && strchr(whitespace, *s))
s++;
*ps = s;
return *s && strchr(toks, *s);
}

int
gettoken(char **ps, char *es, char **q, char **eq)
{
Expand Down Expand Up @@ -300,12 +300,22 @@ gettoken(char **ps, char *es, char **q, char **eq)
return ret;
}

void nulterminate(struct cmd*);
int
peek(char **ps, char *es, char *toks)
{
char *s;

s = *ps;
while(s < es && strchr(whitespace, *s))
s++;
*ps = s;
return *s && strchr(toks, *s);
}

struct cmd *parseline(char**, char*);
struct cmd *parsepipe(char**, char*);
struct cmd *parseredirs(struct cmd*, char**, char*);
struct cmd *parseblock(char**, char*);
struct cmd *parseexec(char**, char*);
struct cmd *nulterminate(struct cmd*);

struct cmd*
parsecmd(char *s)
Expand Down Expand Up @@ -354,22 +364,6 @@ parsepipe(char **ps, char *es)
return cmd;
}

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;
}

struct cmd*
parseredirs(struct cmd *cmd, char **ps, char *es)
{
Expand All @@ -395,6 +389,22 @@ parseredirs(struct cmd *cmd, char **ps, char *es)
return cmd;
}

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;
}

struct cmd*
parseexec(char **ps, char *es)
{
Expand Down Expand Up @@ -429,7 +439,7 @@ parseexec(char **ps, char *es)
}

// NUL-terminate all the counted strings.
void
struct cmd:
nulterminate(struct cmd *cmd)
{
int i;
Expand All @@ -440,7 +450,7 @@ nulterminate(struct cmd *cmd)
struct redircmd *rcmd;

if(cmd == 0)
return;
return 0;

switch(cmd->type){
case EXEC:
Expand Down Expand Up @@ -472,4 +482,5 @@ nulterminate(struct cmd *cmd)
nulterminate(bcmd->cmd);
break;
}
return cmd;
}

0 comments on commit a759b8a

Please sign in to comment.