Ejemplo n.º 1
0
class Console(Cmd, UI):
    def __init__(self, completekey='tab', stdin=None, stdout=None, sigstp=True, arguments=None):
        Cmd.__init__(self, completekey, stdin, stdout)
        UI.__init__(self, arguments)
        self.arguments = arguments
        self.cm = ConfigManager.Get()
        self.history = history()
        self.api = ApiManager()
        self.vfs = self.api.vfs()
        self.taskmanager = self.api.TaskManager()
	self.line_to_arguments = line_to_arguments.Line_to_arguments()
        self.old_completer = ""
        self.prompt = "dff / > "
        self.intro = "\n##########################################\n\
# Welcome on Digital Forensics Framework #\n\
##########################################\n"
	self.stdin = self
	self.completekey = '\t'
	self.comp_raw = complete_raw_input(self)
        if self.arguments:
            print arguments.verbosity
            self.completion = completion.Completion(self.comp_raw, arguments.debug, arguments.verbosity)
        else:
            self.completion = completion.Completion(self.comp_raw, False, 0)
	self.proc = None
	if os.name == 'posix' and sigstp:
  	  signal.signal(signal.SIGTSTP, self.bg)



    def launch(self, modulesPaths = None, defaultConfig = None):
       if modulesPaths or defaultConfig:
         self.loadModules(modulesPaths, defaultConfig=defaultConfig)
       self.cmdloop()


    def bg(self, signum, trace):
	if self.proc:
	   proc = self.proc
	   proc.event.set()
  	   proc.exec_flags += ["thread"]
	   print "\n\n[" + str(proc.pid) + "]" + " background " + proc.name
	   return None


    def precmd(self, line):
        return line

    def postcmd(self, stop, line):
        self.prompt = "dff " + self.vfs.getcwd().absolute() + " > "
        return stop

    def preloop(self):
	return 
 
    def postloop(self):
        print "Exiting..."

    def onecmd(self, line, wait=False):
        try:
	    if line == 'exit' or line == 'quit':
	      return 'stop'
            if len(line.strip()) == 0:
                return self.emptyline()
            iterator = re.finditer('(?<!\\\)\&&', line)
            prevpos = 0
            commands = []
            itcount = 0
            for match in iterator:
                commands.append(line[prevpos:match.span()[0]].strip())
                prevpos = match.span()[1]
            if prevpos != len(line):
                commands.append(line[prevpos:])
            noerror = True
            for command in commands:
                cmds = self.completion.lp.makeCommands(command)
                for cmd in cmds:
                    if len(cmd[3]):
                        noerror = False
                        print cmd[3]
                    else:
                        exec_type = ["console"]
                        cname = cmd[0]
                        config = self.cm.configByName(cname)
                        try:
                            args = config.generate(cmd[1])
                            if cmd[2]:
                                exec_type.append("thread")
                            self.proc = self.taskmanager.add(cname, args, exec_type)
                            if self.proc and not cmd[2]:
                                if wait:
                                    self.proc.event.wait()
                                else:
                                    while not self.proc.event.isSet():
                                        self.comp_raw.get_char(1)
                        except RuntimeError, error:
                            noerror = False
                            print "module " + cmd[0]
                            print "\t" + str(error)
                    self.proc = None
            if noerror:
                self.history.add(line.strip())
        except: