From 1d93a03eeb2e7fa8139c7a0d4b0794b8452d3e2a Mon Sep 17 00:00:00 2001 From: Miroslav Stampar Date: Wed, 15 Jun 2011 11:58:50 +0000 Subject: [PATCH] introducing mnemonics --- lib/core/common.py | 66 ++++++++++++++++++++++++++++++++++++++++++++ lib/parse/cmdline.py | 10 +++++++ 2 files changed, 76 insertions(+) diff --git a/lib/core/common.py b/lib/core/common.py index 15d5bba91..8635a1ca8 100644 --- a/lib/core/common.py +++ b/lib/core/common.py @@ -2614,3 +2614,69 @@ def isNoneValue(value): return not any(value) else: return value is None + +def expandMnemonics(mnemonics, parser, args): + """ + Expand mnemonic options + """ + + class MnemonicNode: + 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.split(','): + found = None + name = mnemonic.split('=')[0].replace("-", "") + 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 + + if pointer in (None, head): + errMsg = "mnemonic '%s' can't be resolved to any parameter name" % name + logger.error(errMsg) + elif len(pointer.current) > 1: + options = {} + for option in pointer.current: + for opt in option._long_opts + option._short_opts: + options[opt.strip('-')] = option + if 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(options.keys())) + warnMsg += "resolved to shortest parameter name (%s)" % found + logger.warn(warnMsg) + found = options[found] + else: + found = pointer.current[0] + debugMsg = "mnemonic '%s' resolved to %s). " % (name, found) + logger.debug(debugMsg) + + if found: + value = found.convert_value(found, value) + if value is not None: + setattr(args, found.dest, value) + else: + setattr(args, found.dest, True) diff --git a/lib/parse/cmdline.py b/lib/parse/cmdline.py index 8362e0b22..da91d5743 100644 --- a/lib/parse/cmdline.py +++ b/lib/parse/cmdline.py @@ -14,6 +14,7 @@ from optparse import OptionGroup from optparse import OptionParser from optparse import SUPPRESS_HELP +from lib.core.common import expandMnemonics from lib.core.common import getUnicode from lib.core.data import logger from lib.core.settings import IS_WIN @@ -28,6 +29,7 @@ def cmdLineParser(): usage = "%s%s [options]" % ("python " if not IS_WIN else "", \ "\"%s\"" % sys.argv[0] if " " in sys.argv[0] else sys.argv[0]) + parser = OptionParser(usage=usage, version=VERSION_STRING) try: @@ -493,6 +495,9 @@ def cmdLineParser(): # Miscellaneous options miscellaneous = OptionGroup(parser, "Miscellaneous") + miscellaneous.add_option("-z", dest="mnemonics", + help="Use mnemonics for shorter parameter setup") + miscellaneous.add_option("--beep", dest="beep", action="store_true", default=False, help="Alert when sql injection found") @@ -589,6 +594,11 @@ def cmdLineParser(): (args, _) = parser.parse_args(args) + for i in xrange(len(sys.argv)-1): + if sys.argv[i] == '-z': + expandMnemonics(sys.argv[i+1], parser, args) + break + if not any([args.direct, args.url, args.logFile, args.bulkFile, args.googleDork, args.configFile, \ args.requestFile, args.updateAll, args.smokeTest, args.liveTest, args.realTest, args.wizard, args.dependencies]): errMsg = "missing a mandatory parameter (-d, -u, -l, -m, -r, -g, -c, --wizard, --update or --dependencies), "