summaryrefslogtreecommitdiff
path: root/vish.py
blob: a8febc393fe32902cc71e1560a2ed776717ae1b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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))