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)
25 print(commands.get(c[1],(None,'Can\'t find help for command %s'%(c[1])))[1])
27 print("Available commands: %s" % ", ".join(sorted(commands.keys())))
28 print("Use 'help command' to get more information on the command 'command'")
32 ret = subprocess.call(cmd)
34 print("Command returned non-zero exit statis %d" % ret)
37 def sendcmd(addr, cmd):
39 print("206 Sending command %s..." % (cmd))
40 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
46 print(data.decode('utf-8'))
54 for n in grp.getgrnam("tuer").gr_mem:
56 print (p.pw_name, " - ", p.pw_gecos)
58 def alias (cmds, aliases):
59 for newname, oldname in aliases.items():
60 cmds[newname] = cmds[oldname]
64 'exit': (exitcmd, 'Quits this shell'),
65 'help': (helpcmd, 'Helps you getting to know the available commands'),
66 'open': (sendcmd(tuerSock, 'unlock'), 'Will try to unlock the apartment door'),
67 'buzz': (sendcmd(tuerSock, 'buzz'), 'Will buzz the buzzer for the street door'),
68 'who': (whocmd, 'Shows the list of people, who are allowed to control this system'),
74 def complete_command(cmd):
75 '''returns a list of commands (as strings) starting with cmd'''
76 return list(filter(lambda x: x.startswith(cmd), commands.keys()))
77 readline.set_completer(lambda cmd, num: (complete_command(cmd)+[None])[num]) # wrap complete_command for readline's weird completer API
78 readline.parse_and_bind("tab: complete") # run completion on tab
81 print("Welcome to tyshell. Use help to see what you can do.")
88 command = shlex.split(command)
89 if not len(command): continue
90 # find suiting commands
91 if command[0] in commands: # needed in case a complete command is a prefix of another one
92 cmdoptions = [command[0]]
94 cmdoptions = complete_command(command[0])
95 # check how many we found
96 if len(cmdoptions) == 0: # no commands fit prefix
97 print("Command %s not found. Use help." % command[0])
98 elif len(cmdoptions) == 1: # exactly one command fits (prefix)
100 res = commands[cmdoptions[0]][0](command)
102 except Exception as e:
103 print("Error while executing %s: %s" % (command[0], str(e)))
104 else: # multiple commands fit the prefix
105 print("Ambiguous command prefix, please choose one of the following:")
106 print("\t", " ".join(cmdoptions))
107 # TODO: put current "command[0]" into the shell for the next command, but such that it is deletable with backspace