10 tuerSock = "/run/tuer.sock"
13 histfile = os.path.join(os.path.expanduser("~"), ".tyshellhist")
15 readline.read_history_file(histfile)
19 atexit.register(readline.write_history_file, histfile)
23 print("Available commands: %s" % ", ".join(sorted(commands.keys())))
27 ret = subprocess.call(cmd)
29 print("Command returned non-zero exit statis %d" % ret)
32 def sendcmd(addr, cmd):
34 print("Running %s..." % (cmd))
35 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
41 print(data.decode('utf-8'))
49 for p in filter(lambda x:x.pw_shell=="/opt/tuer/tyshell",pwd.getpwall()):
50 print (p.pw_name, " - ", p.pw_gecos)
55 'open': sendcmd(tuerSock, 'unlock'),
56 'unlock': sendcmd(tuerSock, 'unlock'),
57 'buzz': sendcmd(tuerSock, 'buzz'),
61 def complete_command(cmd):
62 '''returns a list of commands (as strings) starting with cmd'''
63 return list(filter(lambda x: x.startswith(cmd), commands.keys()))
64 readline.set_completer(lambda cmd, num: (complete_command(cmd)+[None])[num]) # wrap complete_command for readline's weird completer API
65 readline.parse_and_bind("tab: complete") # run completion on tab
68 print("Welcome to tyshell. Use help to see what you can do.")
75 command = shlex.split(command)
76 if not len(command): continue
77 # find suiting commands
78 if command[0] in commands: # needed in case a complete command is a prefix of another one
79 cmdoptions = [command[0]]
81 cmdoptions = complete_command(command[0])
82 # check how many we found
83 if len(cmdoptions) == 0: # no commands fit prefix
84 print("Command %s not found. Use help." % command[0])
85 elif len(cmdoptions) == 1: # exactly one command fits (prefix)
87 res = commands[cmdoptions[0]](command)
89 except Exception as e:
90 print("Error while executing %s: %s" % (command[0], str(e)))
91 else: # multiple commands fit the prefix
92 print("Ambiguous command prefix, please choose one of the following:")
93 print("\t", " ".join(cmdoptions))
94 # TODO: put current "command[0]" into the shell for the next command, but such that it is deletable with backspace