diff options
Diffstat (limited to 'vish.py')
-rwxr-xr-x | vish.py | 57 |
1 files changed, 57 insertions, 0 deletions
@@ -0,0 +1,57 @@ +#!/usr/bin/env python3 + +import os +import re +import argparse + +class EOFException(Exception): + pass + + +class Instruction(): + def __init__(self, string: str): + # TODO handle quoting and escapments + keywords = string.split() + self.prog = keywords[0] + self.arguments = keywords[1:] + + + def execute(self): + cpid = os.fork() + if cpid == 0: + os.execvp(self.prog, [self.prog] + self.arguments) + else: + os.waitpid(cpid, 0) + + +def read_instruction(fh): + # TODO We should be reading an instruction, not a line. + while True: + line = fh.readline() + if line == "": + raise EOFException() + + line = line.strip() + # We ignore empty lines and lines which start with # + if not line.startswith("#") and not line == "": + break + + return Instruction(line) + + +def main(arguments): + for script in arguments["<script>"]: + with open(script, mode="r", encoding="utf-8") as fh: + while True: + try: + instruction = read_instruction(fh) + instruction.execute() + + except EOFException: + break + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Command interpreter written in Python.") + parser.add_argument("<script>", action="store", nargs="+", help="Script to run") + args = parser.parse_args() + main(vars(args)) |