diff options
author | Olivier Gayot <olivier.gayot@sigexec.com> | 2020-03-21 12:44:11 +0100 |
---|---|---|
committer | Olivier Gayot <olivier.gayot@sigexec.com> | 2020-03-21 12:59:52 +0100 |
commit | 5596b2ed6b7109db1ddfd474d77e24cac647fd66 (patch) | |
tree | 8e393c26f026191b64a2323db38951f9678b463f /vish.py | |
parent | 7b51b6435a48efc3667f016b48b39d462158f1bb (diff) |
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
Diffstat (limited to 'vish.py')
-rwxr-xr-x | vish.py | 41 |
1 files changed, 38 insertions, 3 deletions
@@ -30,12 +30,20 @@ class Pipeline(): class Instruction(): - def __init__(self, tokens: list=[], stdout=None, stderr=None): - self.prog = tokens[0] - self.args = tokens[1:] + def __init__(self, stdout=None, stderr=None): self.stdout = stdout self.stderr = stderr + def execute(self): + pass + + +class Command(Instruction): + def __init__(self, prog: str, args: list=[], **kwargs): + super().__init__(**kwargs) + + self.prog = prog + self.args = args def execute(self): cpid = os.fork() @@ -53,6 +61,33 @@ class Instruction(): return status +class Builtin(Instruction): + def __init__(self, function, args: list=[], **kwargs): + super().__init__(**kwargs) + + self.function = function + self.args = args + + def execute(self): + # TODO Rewrite using with statements + if self.stdout is not None: + stdout = open(self.stdout, mode="w", encoding="utf-8") + else: + stdout = sys.stdout + + if self.stderr is not None: + stderr = open(self.stderr, mode="w", encoding="utf-8") + else: + stderr = sys.stderr + + try: + self.function(self.args, stdout=stdout) + except Exception as e: + print(e.__repr__(), file=stderr) + return 1 + return 0 + + class AndInstruction(Instruction): def __init__(self, instruction1: Instruction, instruction2: Instruction): self.instruction1 = instruction1 |