This repository has been archived on 2015-04-29. You can view files and clone it, but cannot push or open issues or pull requests.
psh/main.py
Ian Adam Naval 9324774208 Improve shell argument parsing
For example, this patch fixes the command "df | awk '{print $1}'"
2015-02-20 22:53:22 -05:00

63 lines
1.7 KiB
Python

from commands import RawCommand, registered_cmds
from formatters import Printer
from example_cmd import example_cmd, echo
import os
import os.path
import shlex
for path in os.environ['PATH'].split(':'):
if os.path.exists(path):
binaries = os.listdir(path)
for binary in binaries:
globals()[binary] = RawCommand(binary)
def parse_cmd(potential_cmd):
"""Evaluates a potential command. If it exists in the list of
registered commands, we return a string that would call the
constructor for that command. If it does not exist, we wrap the
name of the command with the RawCommand class.
:return: A string that when evaluated by Python's `eval` feature
would build an object of the correct type
"""
args = potential_cmd.strip().split(' ')
cmd_name = args[0]
if args:
args = args[1:]
if cmd_name not in registered_cmds:
return "RawCommand({})".format(shlex.split(potential_cmd))
else:
return "{}()".format(cmd_name)
def parse_cmds(raw):
potential_cmds = raw.split('|')
cmds = [parse_cmd(cmd) for cmd in potential_cmds]
return cmds
def handle_input(prompt=""):
raw = input(prompt)
if raw[0] == '>':
return raw[1:]
else:
cmds = parse_cmds(raw)
cmds.append("Printer()")
mangled_input = ""
for cmd in reversed(cmds):
mangled_input += cmd + "("
mangled_input += ")" * len(cmds)
print("[DEBUG]: evaluating Python: ", mangled_input)
return mangled_input
def main():
import code
code.interact("Augmented Unix Userland", handle_input, globals())
if __name__ == '__main__':
main()