diff options
-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 |