From 6e6f99e0b83962152dbaf262d4a55759d2dd8851 Mon Sep 17 00:00:00 2001 From: John Johansen Date: Tue, 1 Sep 2020 00:29:13 -0700 Subject: [PATCH] parser: add the ability to print what flags are set in option flag tables Add the ability to show which warnings are enabled by specifying "show" as an to the --dump, --warn, and --Optimize options Eg. --warn=show MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/600 Signed-off-by: John Johansen --- parser/common_optarg.c | 18 ++++++++++++++++++ parser/common_optarg.h | 1 + parser/parser_main.c | 10 ++++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/parser/common_optarg.c b/parser/common_optarg.c index 3035f9ad2..5d95c3cb0 100644 --- a/parser/common_optarg.c +++ b/parser/common_optarg.c @@ -112,12 +112,30 @@ void print_flag_table(optflag_table_t *table) longest = strlen(table[i].option); } + printf("%-*s \t%s\n", longest, " show", "show flags that have been set and exit"); for (i = 0; table[i].option; i++) { printf("%5s%-*s \t%s\n", (table[i].control & 1) ? "[no-]" : "", longest, table[i].option, table[i].desc); } } +void print_flags(const char *prefix, optflag_table_t *table, dfaflags_t flags) +{ + int i, count = 0; + + printf("%s=", prefix); + for (i = 0; table[i].option; i++) { + if ((table[i].flags & flags) == table[i].flags) { + if (count) + printf(", "); + printf("%s", table[i].option); + count++; + } + } + if (count) + printf("\n"); +} + int handle_flag_table(optflag_table_t *table, const char *optarg, dfaflags_t *flags) { diff --git a/parser/common_optarg.h b/parser/common_optarg.h index 048484d61..d10e70109 100644 --- a/parser/common_optarg.h +++ b/parser/common_optarg.h @@ -37,6 +37,7 @@ typedef struct { extern optflag_table_t dumpflag_table[]; extern optflag_table_t optflag_table[]; +void print_flags(const char *prefix, optflag_table_t *table, dfaflags_t flags); int handle_flag_table(optflag_table_t *table, const char *optarg, dfaflags_t *flags); void flagtable_help(const char *name, const char *header, const char *command, diff --git a/parser/parser_main.c b/parser/parser_main.c index 04d6c0113..53e3dc1f9 100644 --- a/parser/parser_main.c +++ b/parser/parser_main.c @@ -536,6 +536,8 @@ static int process_arg(int c, char *optarg) skip_read_cache = 1; if (!optarg) { dump_vars = 1; + } else if (strcmp(optarg, "show") == 0) { + print_flags("dump", dumpflag_table, dfaflags); } else if (strcmp(optarg, "variables") == 0) { dump_vars = 1; } else if (strcmp(optarg, "expanded-variables") == 0) { @@ -548,7 +550,9 @@ static int process_arg(int c, char *optarg) } break; case 'O': - if (!handle_flag_table(optflag_table, optarg, + if (strcmp(optarg, "show") == 0) { + print_flags("Optimize", optflag_table, dfaflags); + } else if (!handle_flag_table(optflag_table, optarg, &dfaflags)) { PERROR("%s: Invalid --Optimize option %s\n", progname, optarg); @@ -687,7 +691,9 @@ static int process_arg(int c, char *optarg) skip_mode_force = 1; break; case ARG_WARN: - if (!handle_flag_table(warnflag_table, optarg, + if (strcmp(optarg, "show") == 0) { + print_flags("warn", warnflag_table, warnflags); + } else if (!handle_flag_table(warnflag_table, optarg, &warnflags)) { PERROR("%s: Invalid --warn option %s\n", progname, optarg);