summaryrefslogtreecommitdiff
path: root/userspace/foolshell.c
diff options
context:
space:
mode:
authorMiguel <m.i@gmx.at>2018-09-21 03:14:52 +0200
committerMiguel <m.i@gmx.at>2018-09-21 03:14:52 +0200
commitd4bc2ecdd1d0b3d3f3642a5f02840d1e0cb5e199 (patch)
tree076ecc41b928c057a6c10df6508237961d714958 /userspace/foolshell.c
parentace0646608c393d8952b14536090c302bed2ee85 (diff)
piper works so nice
Diffstat (limited to 'userspace/foolshell.c')
-rw-r--r--userspace/foolshell.c54
1 files changed, 52 insertions, 2 deletions
diff --git a/userspace/foolshell.c b/userspace/foolshell.c
index e8b384e..4d011da 100644
--- a/userspace/foolshell.c
+++ b/userspace/foolshell.c
@@ -15,9 +15,13 @@
#include <errno.h>
#include <string.h>
+
+#include "newcalls.h"
+
extern char **environ;
bool process(char *buf);
+bool metaprocess(char *buf);
bool cd(char *path);
void version()
@@ -96,7 +100,7 @@ int main(int argc, char **argv)
}
}
- if(!process(buf))break; // process input and return if exit
+ if(!metaprocess(buf))break; // process input and return if exit
}
return 0;
@@ -142,6 +146,52 @@ char **tokenize(char *buf,char chr)
return token;
}
+bool metaprocess2(char *buf);
+bool metaprocess(char *buf)
+{
+ char **token=tokenize(buf,'|');
+ if(token[0]==0 || token[1]==0)return process(buf); // no pipes
+
+ int pid=_fork();
+
+ if(pid==0)
+ {
+ metaprocess2(buf);
+ exit(1);
+ }
+
+ _wait(pid);
+ return true;
+}
+
+bool metaprocess2(char *buf)
+{
+ char **token=tokenize(buf,'|');
+ if(token[0]==0 || token[1]==0)return process(buf); // no pipes
+
+ int fds[2];
+ _pipe(fds);
+
+ int pid=_fork();
+
+ if(pid==0)
+ {
+ // first child shall write to the pipe
+ _close(fds[0]);
+ _dup2(fds[1],1); // replace stdout with the write-end of our pipe
+ process(token[0]);
+ exit(1);
+ }
+
+ _close(fds[1]); // and we read from it
+ _dup2(fds[0],0);
+ metaprocess2(strchr(buf,'|')+1);
+
+ _wait(pid);
+
+ return true;
+}
+
bool process(char *buf)
{
char **token=tokenize(buf,' ');
@@ -179,7 +229,7 @@ bool process(char *buf)
else sprintf(buf,"%s/%s",getenv("PATH"),token[0]);
_execve(buf,token,environ);
printf("foolshell: %s (errno: %d)\n",strerror(errno),errno);
- return false;
+ exit(1);
}
if(token[1]==NULL||strcmp(token[1],"&"))_wait(pid);