11 tuerSock = "/run/tuer.sock"
14 histfile = os.path.join(os.path.expanduser("~"), ".tyshellhist")
16 readline.read_history_file(histfile)
20 atexit.register(readline.write_history_file, histfile)
24 print("Available commands: %s" % ", ".join(sorted(commands.keys())))
28 ret = subprocess.call(cmd)
30 print("Command returned non-zero exit statis %d" % ret)
33 def sendcmd(addr, cmd):
35 print("206 Sending command %s..." % (cmd))
36 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
42 print(data.decode('utf-8'))
50 for n in grp.getgrnam("tuer").gr_mem:
52 print (p.pw_name, " - ", p.pw_gecos)
54 def alias (cmds, aliases):
55 for newname, oldname in aliases.items():
56 cmds[newname] = cmds[oldname]
62 'open': sendcmd(tuerSock, 'unlock'),
63 'buzz': sendcmd(tuerSock, 'buzz'),
70 def complete_command(cmd):
71 '''returns a list of commands (as strings) starting with cmd'''
72 return list(filter(lambda x: x.startswith(cmd), commands.keys()))
73 readline.set_completer(lambda cmd, num: (complete_command(cmd)+[None])[num]) # wrap complete_command for readline's weird completer API
74 readline.parse_and_bind("tab: complete") # run completion on tab
77 print("Welcome to tyshell. Use help to see what you can do.")
84 command = shlex.split(command)
85 if not len(command): continue
86 # find suiting commands
87 if command[0] in commands: # needed in case a complete command is a prefix of another one
88 cmdoptions = [command[0]]
90 cmdoptions = complete_command(command[0])
91 # check how many we found
92 if len(cmdoptions) == 0: # no commands fit prefix
93 print("Command %s not found. Use help." % command[0])
94 elif len(cmdoptions) == 1: # exactly one command fits (prefix)
96 res = commands[cmdoptions[0]](command)
98 except Exception as e:
99 print("Error while executing %s: %s" % (command[0], str(e)))
100 else: # multiple commands fit the prefix
101 print("Ambiguous command prefix, please choose one of the following:")
102 print("\t", " ".join(cmdoptions))
103 # TODO: put current "command[0]" into the shell for the next command, but such that it is deletable with backspace