diff options
author | Olivier Gayot <olivier.gayot@sigexec.com> | 2020-03-21 01:59:47 +0100 |
---|---|---|
committer | Olivier Gayot <olivier.gayot@sigexec.com> | 2020-03-21 12:42:42 +0100 |
commit | 18f21ff584ebf84408d28ff03b2c9fb0da8d3675 (patch) | |
tree | df4f207bc7de3411b83d390a008b007f6099ca07 | |
parent | 3da7ccbf94fc9dfeead62b0091b0f365bf89c0ef (diff) |
Implement stdout and stderr redirections
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
-rwxr-xr-x | vish.py | 12 |
1 files changed, 11 insertions, 1 deletions
@@ -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) |