(mnemonics, parser, args)
Expands mnemonic options
Expands mnemonic options
Expands mnemonic options
def expandMnemonics(mnemonics, parser, args):
    """
    Expands mnemonic options
    """

    class MnemonicNode(object):
        def __init__(self):
            self.next = {}
            self.current = []

    head = MnemonicNode()
    pointer = None

    for group in parser.option_groups:
        for option in group.option_list:
            for opt in option._long_opts + option._short_opts:
                pointer = head

                for char in opt:
                    if char == "-":
                        continue
                    elif char not in pointer.next:
                        pointer.next[char] = MnemonicNode()

                    pointer = pointer.next[char]
                    pointer.current.append(option)

    for mnemonic in (mnemonics or "").split(','):
        found = None
        name = mnemonic.split('=')[0].replace("-", "").strip()
        value = mnemonic.split('=')[1] if len(mnemonic.split('=')) > 1 else None
        pointer = head

        for char in name:
            if char in pointer.next:
                pointer = pointer.next[char]
            else:
                pointer = None
                break

        if pointer in (None, head):
            errMsg = "mnemonic '%s' can't be resolved to any parameter name" % name
            raise SqlmapSyntaxException(errMsg)

        elif len(pointer.current) > 1:
            options = {}

            for option in pointer.current:
                for opt in option._long_opts + option._short_opts:
                    opt = opt.strip('-')
                    if opt.startswith(name):
                        options[opt] = option

            if not options:
                warnMsg = "mnemonic '%s' can't be resolved" % name
                logger.warn(warnMsg)
            elif name in options:
                found = name
                debugMsg = "mnemonic '%s' resolved to %s). " % (name, found)
                logger.debug(debugMsg)
            else:
                found = sorted(options.keys(), key=lambda x: len(x))[0]
                warnMsg = "detected ambiguity (mnemonic '%s' can be resolved to: %s). " % (name, ", ".join("'%s'" % key for key in options.keys()))
                warnMsg += "Resolved to shortest of those ('%s')" % found
                logger.warn(warnMsg)

            if found:
                found = options[found]
        else:
            found = pointer.current[0]
            debugMsg = "mnemonic '%s' resolved to %s). " % (name, found)
            logger.debug(debugMsg)

        if found:
            try:
                value = found.convert_value(found, value)
            except OptionValueError:
                value = None

            if value is not None:
                setattr(args, found.dest, value)
            elif not found.type:  # boolean
                setattr(args, found.dest, True)
            else:
                errMsg = "mnemonic '%s' requires value of type '%s'" % (name, found.type)
                raise SqlmapSyntaxException(errMsg)
def expandMnemonics ( mnemonics , parser , args ) : class MnemonicNode ( object ) : def __init__ ( self ) : self . next = { } self . current = [ ] head = MnemonicNode ( ) pointer = None for group in parser . option_groups : for option in group . option_list : for opt in option . _long_opts + option . _short_opts : pointer = head for char in opt : if char == "-" : continue elif char not in pointer . next : pointer . next [ char ] = MnemonicNode ( ) pointer = pointer . next [ char ] pointer . current . append ( option ) for mnemonic in ( mnemonics or "" ) . split ( ',' ) : found = None name = mnemonic . split ( '=' ) [ 0 ] . replace ( "-" , "" ) . strip ( ) value = mnemonic . split ( '=' ) [ 1 ] if len ( mnemonic . split ( '=' ) ) > 1 else None pointer = head for char in name : if char in pointer . next : pointer = pointer . next [ char ] else : pointer = None break if pointer in ( None , head ) : errMsg = "mnemonic '%s' can't be resolved to any parameter name" % name raise SqlmapSyntaxException ( errMsg ) elif len ( pointer . current ) > 1 : options = { } for option in pointer . current : for opt in option . _long_opts + option . _short_opts : opt = opt . strip ( '-' ) if opt . startswith ( name ) : options [ opt ] = option if not options : warnMsg = "mnemonic '%s' can't be resolved" % name logger . warn ( warnMsg ) elif name in options : found = name debugMsg = "mnemonic '%s' resolved to %s). " % ( name , found ) logger . debug ( debugMsg ) else : found = sorted ( options . keys ( ) , key = lambda x : len ( x ) ) [ 0 ] warnMsg = "detected ambiguity (mnemonic '%s' can be resolved to: %s). " % ( name , ", " . join ( "'%s'" % key for key in options . keys ( ) ) ) warnMsg += "Resolved to shortest of those ('%s')" % found logger . warn ( warnMsg ) if found : found = options [ found ] else : found = pointer . current [ 0 ] debugMsg = "mnemonic '%s' resolved to %s). " % ( name , found ) logger . debug ( debugMsg ) if found : try : value = found . convert_value ( found , value ) except OptionValueError : value = None if value is not None : setattr ( args , found . dest , value ) elif not found . type : # boolean setattr ( args , found . dest , True ) else : errMsg = "mnemonic '%s' requires value of type '%s'" % ( name , found . type ) raise SqlmapSyntaxException ( errMsg )