formatting tweaks

This commit is contained in:
rsc 2007-08-28 03:46:58 +00:00
parent 2868ca0f37
commit a759b8a450

121
sh.c
View file

@ -49,19 +49,9 @@ struct backcmd {
struct cmd *cmd; struct cmd *cmd;
}; };
struct cmd *parsecmd(char*); int fork1(void); // Fork but panics on failure.
void panic(char*); void panic(char*);
struct cmd *parsecmd(char*);
int
fork1(void)
{
int pid;
pid = fork();
if(pid == -1)
panic("fork");
return pid;
}
// Execute cmd. Never returns. // Execute cmd. Never returns.
void void
@ -99,6 +89,14 @@ runcmd(struct cmd *cmd)
runcmd(rcmd->cmd); runcmd(rcmd->cmd);
break; break;
case LIST:
lcmd = (struct listcmd*)cmd;
if(fork1() == 0)
runcmd(lcmd->left);
wait();
runcmd(lcmd->right);
break;
case PIPE: case PIPE:
pcmd = (struct pipecmd*)cmd; pcmd = (struct pipecmd*)cmd;
if(pipe(p) < 0) if(pipe(p) < 0)
@ -123,14 +121,6 @@ runcmd(struct cmd *cmd)
wait(); wait();
break; break;
case LIST:
lcmd = (struct listcmd*)cmd;
if(fork1() == 0)
runcmd(lcmd->left);
wait();
runcmd(lcmd->right);
break;
case BACK: case BACK:
bcmd = (struct backcmd*)cmd; bcmd = (struct backcmd*)cmd;
if(fork1() == 0) if(fork1() == 0)
@ -155,7 +145,17 @@ int
main(void) main(void)
{ {
static char buf[100]; 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) { while(getcmd(buf, sizeof(buf)) >= 0) {
if(fork1() == 0) if(fork1() == 0)
runcmd(parsecmd(buf)); runcmd(parsecmd(buf));
@ -171,6 +171,18 @@ panic(char *s)
exit(); exit();
} }
int
fork1(void)
{
int pid;
pid = fork();
if(pid == -1)
panic("fork");
return pid;
}
//PAGEBREAK!
// Constructors // Constructors
struct cmd* struct cmd*
@ -237,24 +249,12 @@ backcmd(struct cmd *subcmd)
cmd->cmd = subcmd; cmd->cmd = subcmd;
return (struct cmd*)cmd; return (struct cmd*)cmd;
} }
//PAGEBREAK!
// Parsing // Parsing
char whitespace[] = " \t\r\n\v"; char whitespace[] = " \t\r\n\v";
char symbols[] = "<|>&;()"; 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 int
gettoken(char **ps, char *es, char **q, char **eq) gettoken(char **ps, char *es, char **q, char **eq)
{ {
@ -300,12 +300,22 @@ gettoken(char **ps, char *es, char **q, char **eq)
return ret; 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 *parseline(char**, char*);
struct cmd *parsepipe(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 *parseexec(char**, char*);
struct cmd *nulterminate(struct cmd*);
struct cmd* struct cmd*
parsecmd(char *s) parsecmd(char *s)
@ -354,22 +364,6 @@ parsepipe(char **ps, char *es)
return cmd; 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* struct cmd*
parseredirs(struct cmd *cmd, char **ps, char *es) parseredirs(struct cmd *cmd, char **ps, char *es)
{ {
@ -395,6 +389,22 @@ parseredirs(struct cmd *cmd, char **ps, char *es)
return cmd; 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* struct cmd*
parseexec(char **ps, char *es) parseexec(char **ps, char *es)
{ {
@ -429,7 +439,7 @@ parseexec(char **ps, char *es)
} }
// NUL-terminate all the counted strings. // NUL-terminate all the counted strings.
void struct cmd:
nulterminate(struct cmd *cmd) nulterminate(struct cmd *cmd)
{ {
int i; int i;
@ -440,7 +450,7 @@ nulterminate(struct cmd *cmd)
struct redircmd *rcmd; struct redircmd *rcmd;
if(cmd == 0) if(cmd == 0)
return; return 0;
switch(cmd->type){ switch(cmd->type){
case EXEC: case EXEC:
@ -472,4 +482,5 @@ nulterminate(struct cmd *cmd)
nulterminate(bcmd->cmd); nulterminate(bcmd->cmd);
break; break;
} }
return cmd;
} }