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
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)
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)
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));
exit();
}
+int
+fork1(void)
+{
+ int pid;
+
+ pid = fork();
+ if(pid == -1)
+ panic("fork");
+ return pid;
+}
+
+//PAGEBREAK!
// Constructors
struct cmd*
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)
{
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)
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)
{
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)
{
}
// NUL-terminate all the counted strings.
-void
+struct cmd:
nulterminate(struct cmd *cmd)
{
int i;
struct redircmd *rcmd;
if(cmd == 0)
- return;
+ return 0;
switch(cmd->type){
case EXEC:
nulterminate(bcmd->cmd);
break;
}
+ return cmd;
}