def doCommand(passedInput=None, pipedInput=None): commandName = passedInput.split(' ')[0] if commandName is None or passedInput is None: print("we somehow processed a None parameter?") return parameters = shlex.split(passedInput)[1:] if commandName in localCommands: #print("we attempted to run a non-existing command. how.") #parameters = passedInput.split(' ')[1:] localCommands[commandName](parameters, pipedInput) elif commandName in cmd.getCommands(): # if the command isn't internal then it's probably an external command # so, let commands.py handle that cmd.runCommand(commandName, parameters, pipedInput)
def showHelp(parameters=None, pipedInput=None): global ourShell import platform helpString = f""" {ourShell.shellName}, version {ourShell.shellVersion} (x86_64-{platform.system()}) These shell commands are defined internally. Type "help" to see this list. """ print(helpString) t_funcs = [] # iterate key and value in dict for k, v in localCommands.items(): # if it's an internal command (starts with "__") ignore it # if it's not, add it to the table of functions if not k[:2] == "__": t_funcs.append(k) for v in cmd.getCommands(): # repeat process with external commands if not v[:2] == "__": t_funcs.append(v) #for each sorted command name, print it for i in sorted(t_funcs): print(i) pass
import commands import configparser import cryptostorm conf = 'conf.ini' print(commands.NAME + ' ' + commands.VERSION) print(cryptostorm.NAME + ' ' + cryptostorm.VERSION) while True: command = input('>> ') commands.doCommands(commands.getCommands(command), conf)
def main(Arguments=None): global ourShell, lastCmd, up_index printMOTD() # create a new instance so we can set up the class for later reference cmd.init(ourShell) while not isDone: # get the global nextCmd global nextCmd # read user input nextCmd = readCmd() # if we have too much in history, keep it 10 items long if len(lastCmd) > 10: del (lastCmd[-1]) # append the command we just read from user input lastCmd.append(nextCmd) # stupid workaround to print a newline without changing readCmd() print() # if we didn't get a None/null input regExp = r'(?!\B"[^"]*)\|(?![^"]*"\B)(?<!\\\|)' # this absolute behemoth of a regex is a mix of https://stackoverflow.com/questions/21105360/regex-find-comma-not-inside-quotes/21106122 # and my own workings to detect pipes that aren't escaped or in quotes try: splitCmd = list(re.split(regExp, nextCmd)) except: continue pipedInput = "" for i, item in enumerate(splitCmd): nextCmd = item.strip() if nextCmd != None: # get the command name (without parameters) nextCmdName = nextCmd.split(' ')[0] # check if the current cmd instance knows the command or if it's a shell command if nextCmdName in cmd.getCommands( ) or nextCmdName in localCommands: # if it exists, and is the only/last command #print(len(splitCmd), i) #print(i == (len(splitCmd)-1)) if (len(splitCmd) <= 1) or (i == (len(splitCmd) - 1)): #doCommand(nextCmdName, nextCmd, pipedInput) doCommand(nextCmd, pipedInput) #print(lastCmd) # reset up_index after executing so our up arrow works properly up_index = 0 # if the command doesn't exist, error out else: #print("piped") # source: https://www.kite.com/python/answers/how-to-redirect-print-output-to-a-variable-in-python # store the reference to stdout old_stdout = sys.stdout # create a new stdout as a StringIO new_stdout = StringIO() # assign stdout to the StringIO instance sys.stdout = new_stdout # run command #doCommand(nextCmdName, nextCmd, pipedInput) doCommand(nextCmd, pipedInput) # store output in pipedInput pipedInput = new_stdout.getvalue() # restore stdout so we're back to normal sys.stdout = old_stdout else: print("{0}: {1}: command not found".format( ourShell.shellName, nextCmd)) else: print("Goodbye!") return 0 return 0
def update(self): self.commands = commands.getCommands()
def __init__(self, instance): self.commands = commands.getCommands() self.instance = instance