diff options
author | Olivier Gayot <olivier.gayot@sigexec.com> | 2020-03-20 19:58:03 +0100 |
---|---|---|
committer | Olivier Gayot <olivier.gayot@sigexec.com> | 2020-03-21 12:30:14 +0100 |
commit | 609fad38b5818806323757d816d9fac5ba3c8a4c (patch) | |
tree | d6db6eb2499ee612443af6eda338d2bd3884fe85 | |
parent | 693b25deab67fcd3a499fa3db55f5a218b3b7ae3 (diff) |
Add first version capable of parsing very simple commands
The script is able to parse very simple commands that don't wrap multiple
lines.
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
-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)) |