From 18f21ff584ebf84408d28ff03b2c9fb0da8d3675 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Sat, 21 Mar 2020 01:59:47 +0100 Subject: Implement stdout and stderr redirections Signed-off-by: Olivier Gayot --- vish.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'vish.py') 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) -- cgit v1.2.3