summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlivier Gayot <olivier.gayot@sigexec.com>2020-03-21 01:59:47 +0100
committerOlivier Gayot <olivier.gayot@sigexec.com>2020-03-21 12:42:42 +0100
commit18f21ff584ebf84408d28ff03b2c9fb0da8d3675 (patch)
treedf4f207bc7de3411b83d390a008b007f6099ca07
parent3da7ccbf94fc9dfeead62b0091b0f365bf89c0ef (diff)
Implement stdout and stderr redirections
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
-rwxr-xr-xvish.py12
1 files changed, 11 insertions, 1 deletions
diff --git a/vish.py b/vish.py
index ec601a0..608a8b4 100755
--- a/vish.py
+++ b/vish.py
@@ -3,6 +3,7 @@
import os
import re
import argparse
+import sys
IFS = (" ", "\t", "\n")
@@ -29,14 +30,23 @@ class Pipeline():
class Instruction():
- def __init__(self, tokens: list=[]):
+ def __init__(self, tokens: list=[], stdout=None, stderr=None):
self.prog = tokens[0]
self.args = tokens[1:]
+ self.stdout = stdout
+ self.stderr = stderr
def execute(self):
cpid = os.fork()
if cpid == 0:
+ if self.stdout is not None:
+ fd = os.open(self.stdout, os.O_WRONLY)
+ os.dup2(sys.stdout.fileno(), fd)
+ if self.stderr is not None:
+ fd = os.open(self.stderr, os.O_WRONLY)
+ os.dup2(sys.stderr.fileno(), fd)
+
os.execvp(self.prog, [self.prog] + self.args)
else:
os.waitpid(cpid, 0)