>From 86086f7b425a2355550447644720f217b7c83a91 Mon Sep 17 00:00:00 2001 From: Frank Dittrich Date: Thu, 19 Jul 2012 21:11:23 +0200 Subject: [PATCH] New --list=[sections|parameters=SECTION|list-data=SECTION] --list=sections prints all existing sections from john.conf or the config file specified with --config=, resolving includes The order in which these sections are listed is reversed to the order of the definition of these sections --list=parameters:SECTION prints all the parameter definitions that exist in a section, included sections and included files are resolved. If a parameter is defined multiple times, it appears multiple times in the list. The order of definitions printed is reversed, compared to the order of the parameter definitions. --list=list-data:SECTION prints all the lines in a section that doesn't contain parameter definitions, but list data. Included sections and included files are resolved. Comments (lines starting with '#' or ';') are not printed. Indention is not preserved, i.e., there are no leading spaces. --list=help:parameters lists the names of sections which include parameter definitions --list=help:list-data lists the names of sections which (are supposed to) contain list data instead of parameter definitions The bash completion script also needed a few adjustments to get this working. --- run/john.bash_completion | 19 +++++++++++-- src/config.c | 50 ++++++++++++++++++++++++++++++++++++ src/config.h | 25 ++++++++++++++++++ src/john.c | 64 ++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 147 insertions(+), 11 deletions(-) diff --git a/run/john.bash_completion b/run/john.bash_completion index e001576..0c5b47e 100644 --- a/run/john.bash_completion +++ b/run/john.bash_completion @@ -100,7 +100,7 @@ ## have grep && have sed && have tr && _john() { - local first cur options valopts compreplya compreplyb encodings formats subformats list hidden dir cmd i ver ver1 ver2 ver3 prev words + local first cur options valopts compreplya compreplyb encodings formats subformats list hidden dir cmd i ver ver1 ver2 ver3 prev words prefix # Without LC_ALL=C, [A-Z] match [a-z] (case "${cur}" in ... esac) LC_ALL=C @@ -583,11 +583,24 @@ _john() fi return 0 ;; - -?(-)li?(s|st)+(+(=|:)+([a-z_-]):|:+([a-z_-])=)*([a-z_-])) + -?(-)li?(s|st)+(+(=|:)+([a-z_-]):|:+([a-z._-])=)*([a-z0-9:._-])) cur=${cur#*[=:]} cmd=${cur%[=:]*} + prefix="" + if [[ "_${cmd}" == _*:* ]] ; then + prefix=${cmd#*:} + prefix=${prefix#*:} + cmd=${cmd%:*} + cur=${cur#*[=:]} + fi cur=${cur#*[=:]} - list=`${first} --list=help:${cmd} 2>/dev/null |sed 's#,# #g'` + # Q&D fix, a general solution will take longer: + if [[ ( "_${cmd}" == _parameters || "_${cmd}" == _list-data ) && "_${prefix}" != "_" ]] ; then + # list subsection names + list=`${first} --list=${prefix} 2>/dev/null|sed 's#^.*\s.*$##'` + else + list=`${first} --list=help:${cmd} 2>/dev/null |sed 's#,#\n#g'` + fi if [[ $? -eq 0 ]] ; then COMPREPLY=( $(compgen -W "${list}" -- ${cur}) ) fi diff --git a/src/config.c b/src/config.c index 2fa4fb5..bc28a62 100644 --- a/src/config.c +++ b/src/config.c @@ -199,6 +199,56 @@ struct cfg_list *cfg_get_list(char *section, char *subsection) } #ifndef BENCH_BUILD +void cfg_print_section_names(int which) +{ + struct cfg_section *current; + + if ((current = cfg_database)) + do { + if ((which == 0) || + (which == 1 && current->params != NULL) || + (which == 2 && current->list != NULL)) + printf("%s\n", current->name); + } while ((current = current->next)); +} + +int cfg_print_section_params(char *section, char *subsection) +{ + struct cfg_section *current; + struct cfg_param *param; + int param_count = 0; + + if ((current = cfg_get_section(section, subsection))) { + if((param = current->params)) + do { + printf("%s = %s\n", param->name, param->value); + param_count++; + } while ((param = param-> next)); + return param_count; + } + else return -1; + +} + +int cfg_print_section_list_lines(char *section, char *subsection) +{ + struct cfg_section *current; + struct cfg_line *line; + int line_count = 0; + + if ((current = cfg_get_section(section, subsection))) { + if (current->list && (line = current->list->head)) + do { + // we only want to see the line contents + // printf("%s-%d_%d:%s\n", line->cfg_name, line->id, line->number, line->data); + printf("%s\n", line->data); + line_count++; + } while((line = line->next)); + return line_count; + } + else return -1; +} + int cfg_print_subsections(char *section, char *function, char *notfunction, int print_heading) { int ret = 0; diff --git a/src/config.h b/src/config.h index 9e9d68e..5cc5132 100644 --- a/src/config.h +++ b/src/config.h @@ -72,6 +72,31 @@ extern struct cfg_section *cfg_get_section(char *section, char *subsection); extern struct cfg_list *cfg_get_list(char *section, char *subsection); /* + * Prints a list of all section names, if which == 0. + * + * If which == 1, only names of sections which don't contain + * list lines (and which should contain parameters instead) get printed, + * unless these sections are empty (no parameter definitions). + * + * If which == 2, only names of sections which should contain + * list lines, not parameter definitions, get printed. + */ +void cfg_print_section_names(int which); + +/* + * Prints all the parameter definitions of a section, + * returns the number of parameter definitions found, or -1 + * if the section doesn't exist. + */ +int cfg_print_section_params(char *section, char *subsection); + +/* + * Prints the contents of a section's list, returns the number + * of lines printed, or -1 if the section doesn't exist. + */ +int cfg_print_section_list_lines(char *section, char *subsection); + +/* * Searches for sections with the supplied name, and prints a list of * valid subsections. If function is non-null, only prints subsections * (ie. external modes) that has function (ie. generate or filter). diff --git a/src/john.c b/src/john.c index 2e3e96a..93afb7f 100644 --- a/src/john.c +++ b/src/john.c @@ -577,8 +577,12 @@ static void CPU_detect_or_fallback(char **argv, int make_check) static void john_list_options() { puts("help[:WHAT], subformats, inc-modes, rules, externals, ext-filters,"); - puts("ext-filters-only, ext-modes, build-info, hidden-options, encodings, formats,"); - puts("format-details, format-all-details, format-methods[:WHICH],"); + puts("ext-filters-only, ext-modes, build-info, hidden-options, encodings,"); + puts("formats, format-details, format-all-details, format-methods[:WHICH],"); + // With "opencl-devices, cuda-devices, " added, + // the resulting line will get too long + // printf("sections, parameters:SECTION, list-data:SECTION, "); + puts("sections, parameters:SECTION, list-data:SECTION,"); #ifdef CL_VERSION_1_0 printf("opencl-devices, "); #endif @@ -588,12 +592,16 @@ static void john_list_options() /* NOTE: The following must end the list. Anything listed after will be ignored by current bash completion scripts. */ + + /* FIXME: Should all the section names get printed instead? + * But that would require a valid config. + */ puts(""); } static void john_list_help_options() { - puts("help, format-methods"); + puts("help, format-methods, parameters, list-data"); } static void john_list_method_names() @@ -650,11 +658,15 @@ static void john_init(char *name, int argc, char **argv) } if (options.listconf && !strncasecmp(options.listconf, "help:", 5)) { - fprintf(stderr, - "%s is not a --list option that supports additional values.\nSupported options:\n", - options.listconf+5); - john_list_help_options(); - exit(1); + if (strcasecmp(options.listconf, "help:parameters") && + strcasecmp(options.listconf, "help:list-data")) + { + fprintf(stderr, + "%s is not a --list option that supports additional values.\nSupported options:\n", + options.listconf+5); + john_list_help_options(); + exit(1); + } } if (options.listconf && !strcasecmp(options.listconf, "hidden-options")) { @@ -792,6 +804,27 @@ static void john_init(char *name, int argc, char **argv) cfg_print_subsections("List.External", NULL, NULL, 0); exit(0); } + if (options.listconf && !strcasecmp(options.listconf, "sections")) + { + cfg_print_section_names(0); + exit(0); + } + if (options.listconf && + !strncasecmp(options.listconf, "parameters", 10) && + (options.listconf[10] == '=' || options.listconf[10] == ':') && + options.listconf[11] != '\0') + { + cfg_print_section_params(&options.listconf[11], NULL); + exit(0); + } + if (options.listconf && + !strncasecmp(options.listconf, "list-data", 9) && + (options.listconf[9] == '=' || options.listconf[9] == ':') && + options.listconf[10] != '\0') + { + cfg_print_section_list_lines(&options.listconf[10], NULL); + exit(0); + } if (options.listconf && !strcasecmp(options.listconf, "ext-filters")) { cfg_print_subsections("List.External", "filter", NULL, 0); @@ -1047,6 +1080,21 @@ static void john_init(char *name, int argc, char **argv) } while ((format = format->next)); exit(0); } + /* + * Other --list=help:WHAT are processed earlier, but these require + * a valid config: + */ + if (options.listconf && !strcasecmp(options.listconf, "help:parameters")) + { + cfg_print_section_names(1); + exit(0); + } + if (options.listconf && !strcasecmp(options.listconf, "help:list-data")) + { + cfg_print_section_names(2); + exit(0); + } + /* --list last resort: list subsections of any john.conf section name */ if (options.listconf) { -- 1.7.7.6