summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlivier Gayot <olivier.gayot@sigexec.com>2020-03-21 12:44:11 +0100
committerOlivier Gayot <olivier.gayot@sigexec.com>2020-03-21 12:59:52 +0100
commit5596b2ed6b7109db1ddfd474d77e24cac647fd66 (patch)
tree8e393c26f026191b64a2323db38951f9678b463f
parent7b51b6435a48efc3667f016b48b39d462158f1bb (diff)
Split instruction between builtin and commandHEADmaster
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
-rwxr-xr-xvish.py41
1 files changed, 38 insertions, 3 deletions
diff --git a/vish.py b/vish.py
index 8d8f6e1..43c4090 100755
--- a/vish.py
+++ b/vish.py
@@ -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