From a0dc58e8355a0d4b98b9c88e0f26172b1418d7c9 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 20 Jan 2011 21:32:58 +0100 Subject: add OPT_LIST mechanism to handle multiply occurring options This allowed to convert abrt-action-bugzilla, abrt-action-rhtsupport and abrt-action-kerneloops to parse_opts() Signed-off-by: Denys Vlasenko --- src/daemon/CommLayerServer.h | 1 - src/daemon/Daemon.h | 2 +- src/daemon/Settings.cpp | 2 +- src/daemon/abrt-server.c | 1 - src/lib/crash_data.c | 1 - src/lib/make_descr.c | 1 - src/lib/parse_options.c | 10 +++-- src/lib/parse_options.h | 8 ++-- src/plugins/abrt-action-bugzilla.cpp | 71 +++++++++++++++++------------------- src/plugins/abrt-action-kerneloops.c | 68 ++++++++++++++++------------------ src/plugins/abrt-action-mailx.c | 1 - src/plugins/abrt-action-print.c | 1 - src/plugins/abrt-action-rhtsupport.c | 68 ++++++++++++++++------------------ src/plugins/abrt-action-upload.c | 1 - 14 files changed, 110 insertions(+), 126 deletions(-) diff --git a/src/daemon/CommLayerServer.h b/src/daemon/CommLayerServer.h index 997f6c41..b6da0ae5 100644 --- a/src/daemon/CommLayerServer.h +++ b/src/daemon/CommLayerServer.h @@ -20,7 +20,6 @@ #define COMMLAYERSERVER_H_ #include "abrtlib.h" -#include "abrt_crash_data.h" class CCommLayerServer { public: diff --git a/src/daemon/Daemon.h b/src/daemon/Daemon.h index eb691262..9072f844 100644 --- a/src/daemon/Daemon.h +++ b/src/daemon/Daemon.h @@ -21,7 +21,7 @@ #include #include "abrt_types.h" -#include "abrt_crash_data.h" +//#include "abrt_crash_data.h" class CCrashWatcher; class CCommLayerServer; diff --git a/src/daemon/Settings.cpp b/src/daemon/Settings.cpp index ff9062fc..e25b7959 100644 --- a/src/daemon/Settings.cpp +++ b/src/daemon/Settings.cpp @@ -76,7 +76,7 @@ static GList *parse_list(const char* list) } if (item->len > 0) - l = g_list_append(l, xstrdup(item->buf)); + l = g_list_append(l, xstrdup(item->buf)); strbuf_free(item); return l; diff --git a/src/daemon/abrt-server.c b/src/daemon/abrt-server.c index 3415e95c..ae1e3f35 100644 --- a/src/daemon/abrt-server.c +++ b/src/daemon/abrt-server.c @@ -17,7 +17,6 @@ */ #include "abrtlib.h" #include "dump_dir.h" -#include "abrt_crash_data.h" /* FILENAME_foo */ #include "hooklib.h" #include "parse_options.h" diff --git a/src/lib/crash_data.c b/src/lib/crash_data.c index a372ab58..410f35f2 100644 --- a/src/lib/crash_data.c +++ b/src/lib/crash_data.c @@ -17,7 +17,6 @@ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "abrtlib.h" -#include "abrt_crash_data.h" static void free_crash_item(void *ptr) { diff --git a/src/lib/make_descr.c b/src/lib/make_descr.c index ed0b99a2..d1209101 100644 --- a/src/lib/make_descr.c +++ b/src/lib/make_descr.c @@ -17,7 +17,6 @@ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "abrtlib.h" -#include "abrt_crash_data.h" // caller is responsible for freeing **dsc static void add_content(bool *was_multiline, char **dsc, const char *header, const char *content) diff --git a/src/lib/parse_options.c b/src/lib/parse_options.c index bcec7869..3d631461 100644 --- a/src/lib/parse_options.c +++ b/src/lib/parse_options.c @@ -101,6 +101,7 @@ unsigned parse_opts(int argc, char **argv, const struct options *opt, break; case OPTION_INTEGER: case OPTION_STRING: + case OPTION_LIST: curopt->has_arg = required_argument; if (opt[ii].short_name) strbuf_append_strf(shortopts, "%c:", opt[ii].short_name); @@ -165,15 +166,18 @@ unsigned parse_opts(int argc, char **argv, const struct options *opt, if (opt[ii].value != NULL) switch (opt[ii].type) { case OPTION_BOOL: - *(int*)opt[ii].value += 1; + *(int*)(opt[ii].value) += 1; break; case OPTION_INTEGER: - *(int*)opt[ii].value = xatoi(optarg); + *(int*)(opt[ii].value) = xatoi(optarg); break; case OPTION_STRING: case OPTION_OPTSTRING: if (optarg) - *(char**)opt[ii].value = (char*)optarg; + *(char**)(opt[ii].value) = (char*)optarg; + break; + case OPTION_LIST: + *(GList**)(opt[ii].value) = g_list_append(*(GList**)(opt[ii].value), optarg); break; case OPTION_END: break; diff --git a/src/lib/parse_options.h b/src/lib/parse_options.h index 05d6fd90..82f3c6b8 100644 --- a/src/lib/parse_options.h +++ b/src/lib/parse_options.h @@ -28,6 +28,7 @@ enum parse_opt_type { OPTION_STRING, OPTION_INTEGER, OPTION_OPTSTRING, + OPTION_LIST, OPTION_END, }; @@ -44,16 +45,17 @@ struct options { * s - short_name * l - long_name * v - value - * a - argh argument help + * a - option parameter name (for help text) * h - help */ #define OPT_END() { OPTION_END } #define OPT_BOOL(s, l, v, h) { OPTION_BOOL, (s), (l), (v), NULL, (h) } -#define OPT_INTEGER(s, l, v, h) { OPTION_INTEGER, (s), (l), (v), "n", (h) } +#define OPT_INTEGER(s, l, v, h) { OPTION_INTEGER, (s), (l), (v), "NUM", (h) } #define OPT_STRING(s, l, v, a, h) { OPTION_STRING, (s), (l), (v), (a), (h) } #define OPT_OPTSTRING(s, l, v, a, h) { OPTION_OPTSTRING, (s), (l), (v), (a), (h) } +#define OPT_LIST(s, l, v, a, h) { OPTION_LIST, (s), (l), (v), (a), (h) } -#define OPT__VERBOSE(v) OPT_BOOL('v', "verbose", (v), "Be verbose") +#define OPT__VERBOSE(v) OPT_BOOL('v', "verbose", (v), _("Be verbose")) #define parse_opts abrt_parse_opts unsigned parse_opts(int argc, char **argv, const struct options *opt, diff --git a/src/plugins/abrt-action-bugzilla.cpp b/src/plugins/abrt-action-bugzilla.cpp index 9d849d99..4dfafe90 100644 --- a/src/plugins/abrt-action-bugzilla.cpp +++ b/src/plugins/abrt-action-bugzilla.cpp @@ -19,6 +19,9 @@ #include "abrtlib.h" #include "abrt_xmlrpc.h" #include "abrt_crash_data.h" +#include "parse_options.h" + +#define PROGNAME "abrt-action-bugzilla" #define XML_RPC_SUFFIX "/xmlrpc.cgi" #define MAX_HOPS 5 @@ -903,54 +906,48 @@ int main(int argc, char **argv) map_string_h *settings = new_map_string(); const char *dump_dir_name = "."; + GList *conf_file = NULL; + + /* Can't keep these strings/structs static: _() doesn't support that */ + const char *program_usage_string = _( + PROGNAME" [-vs] -c CONFFILE -d DIR" + "\n" + "\nReport a crash to Bugzilla" + ); enum { - OPT_s = (1 << 0), + OPT_v = 1 << 0, + OPT_s = 1 << 1, + OPT_d = 1 << 2, + OPT_c = 1 << 3, }; - int opts = 0; - int opt; - while ((opt = getopt(argc, argv, "c:d:vs")) != -1) - { - switch (opt) - { - case 'c': - VERB1 log("Loading settings from '%s'", optarg); - load_conf_file(optarg, settings, /*skip key w/o values:*/ true); - VERB3 log("Loaded '%s'", optarg); - break; - case 'd': - dump_dir_name = optarg; - break; - case 'v': - g_verbose++; - break; - case 's': - opts |= OPT_s; - break; - default: - /* Careful: the string below contains tabs, dont replace with spaces */ - error_msg_and_die( - "Usage: abrt-action-bugzilla -c CONFFILE -d DIR [-vs]" - "\n" - "\nReport a crash to Bugzilla" - "\n" - "\nOptions:" - "\n -c FILE Configuration file (may be given many times)" - "\n -d DIR Crash dump directory" - "\n -v Be verbose" - "\n -s Log to syslog" - ); - } - } + /* Keep enum above and order of options below in sync! */ + struct options program_options[] = { + OPT__VERBOSE(&g_verbose), + OPT_BOOL( 's', NULL, NULL , _("Log to syslog")), + OPT_STRING('d', NULL, &dump_dir_name, "DIR" , _("Crash dump directory")), + OPT_LIST( 'c', NULL, &conf_file , "FILE", _("Configuration file (may be given many times)")), + OPT_END() + }; + unsigned opts = parse_opts(argc, argv, program_options, program_usage_string); putenv(xasprintf("ABRT_VERBOSE=%u", g_verbose)); //DONT! our stdout/stderr goes directly to daemon, don't want to have prefix there. -// msg_prefix = xasprintf("abrt-action-bugzilla[%u]", getpid()); +// msg_prefix = xasprintf(PROGNAME"[%u]", getpid()); if (opts & OPT_s) { openlog(msg_prefix, 0, LOG_DAEMON); logmode = LOGMODE_SYSLOG; } + while (conf_file) + { + char *fn = (char *)conf_file->data; + VERB1 log("Loading settings from '%s'", fn); + load_conf_file(fn, settings, /*skip key w/o values:*/ true); + VERB3 log("Loaded '%s'", fn); + conf_file = g_list_remove(conf_file, fn); + } + VERB1 log("Initializing XML-RPC library"); xmlrpc_env env; xmlrpc_env_init(&env); diff --git a/src/plugins/abrt-action-kerneloops.c b/src/plugins/abrt-action-kerneloops.c index 4fef19b4..8d00da52 100644 --- a/src/plugins/abrt-action-kerneloops.c +++ b/src/plugins/abrt-action-kerneloops.c @@ -19,7 +19,7 @@ #include #include "abrtlib.h" -#include "abrt_crash_data.h" +#include "parse_options.h" #define PROGNAME "abrt-action-kerneloops" @@ -126,44 +126,29 @@ int main(int argc, char **argv) map_string_h *settings = new_map_string(); const char *dump_dir_name = "."; + GList *conf_file = NULL; + + /* Can't keep these strings/structs static: _() doesn't support that */ + const char *program_usage_string = _( + PROGNAME" [-vs] -c CONFFILE -d DIR" + "\n" + "\nReport a kernel oops to kerneloops.org (or similar) site" + ); enum { - OPT_s = (1 << 0), + OPT_v = 1 << 0, + OPT_s = 1 << 1, + OPT_d = 1 << 2, + OPT_c = 1 << 3, }; - int opts = 0; - int opt; - while ((opt = getopt(argc, argv, "c:d:vs")) != -1) - { - switch (opt) - { - case 'c': - VERB1 log("Loading settings from '%s'", optarg); - load_conf_file(optarg, settings, /*skip key w/o values:*/ true); - VERB3 log("Loaded '%s'", optarg); - break; - case 'd': - dump_dir_name = optarg; - break; - case 'v': - g_verbose++; - break; - case 's': - opts |= OPT_s; - break; - default: - /* Careful: the string below contains tabs, dont replace with spaces */ - error_msg_and_die( - "Usage: "PROGNAME" -c CONFFILE -d DIR [-vs]" - "\n" - "\nReport a kernel oops to kerneloops.org (or similar) site" - "\n" - "\nOptions:" - "\n -c FILE Configuration file (may be given many times)" - "\n -d DIR Crash dump directory" - "\n -v Be verbose" - "\n -s Log to syslog" - ); - } - } + /* Keep enum above and order of options below in sync! */ + struct options program_options[] = { + OPT__VERBOSE(&g_verbose), + OPT_BOOL( 's', NULL, NULL , _("Log to syslog")), + OPT_STRING('d', NULL, &dump_dir_name, "DIR" , _("Crash dump directory")), + OPT_LIST( 'c', NULL, &conf_file , "FILE", _("Configuration file (may be given many times)")), + OPT_END() + }; + unsigned opts = parse_opts(argc, argv, program_options, program_usage_string); putenv(xasprintf("ABRT_VERBOSE=%u", g_verbose)); //DONT! our stdout/stderr goes directly to daemon, don't want to have prefix there. @@ -174,6 +159,15 @@ int main(int argc, char **argv) logmode = LOGMODE_SYSLOG; } + while (conf_file) + { + char *fn = (char *)conf_file->data; + VERB1 log("Loading settings from '%s'", fn); + load_conf_file(fn, settings, /*skip key w/o values:*/ true); + VERB3 log("Loaded '%s'", fn); + conf_file = g_list_remove(conf_file, fn); + } + report_to_kerneloops(dump_dir_name, settings); free_map_string(settings); diff --git a/src/plugins/abrt-action-mailx.c b/src/plugins/abrt-action-mailx.c index 0ac5454d..3debf449 100644 --- a/src/plugins/abrt-action-mailx.c +++ b/src/plugins/abrt-action-mailx.c @@ -21,7 +21,6 @@ #include "abrtlib.h" #include "parse_options.h" -#include "abrt_crash_data.h" #define PROGNAME "abrt-action-mailx" diff --git a/src/plugins/abrt-action-print.c b/src/plugins/abrt-action-print.c index 830d0b11..cc7fbb34 100644 --- a/src/plugins/abrt-action-print.c +++ b/src/plugins/abrt-action-print.c @@ -20,7 +20,6 @@ */ #include "abrtlib.h" #include "parse_options.h" -#include "abrt_crash_data.h" #define PROGNAME "abrt-action-print" diff --git a/src/plugins/abrt-action-rhtsupport.c b/src/plugins/abrt-action-rhtsupport.c index 42859d5c..337adae1 100644 --- a/src/plugins/abrt-action-rhtsupport.c +++ b/src/plugins/abrt-action-rhtsupport.c @@ -22,7 +22,7 @@ #include "abrt_curl.h" #include "abrt_xmlrpc.h" #include "abrt_rh_support.h" -#include "abrt_crash_data.h" +#include "parse_options.h" #define PROGNAME "abrt-action-rhtsupport" @@ -256,44 +256,29 @@ int main(int argc, char **argv) map_string_h *settings = new_map_string(); const char *dump_dir_name = "."; + GList *conf_file = NULL; + + /* Can't keep these strings/structs static: _() doesn't support that */ + const char *program_usage_string = _( + PROGNAME" [-vs] -c CONFFILE -d DIR" + "\n" + "\nReport a crash to RHTSupport" + ); enum { - OPT_s = (1 << 0), + OPT_v = 1 << 0, + OPT_s = 1 << 1, + OPT_d = 1 << 2, + OPT_c = 1 << 3, }; - int opts = 0; - int opt; - while ((opt = getopt(argc, argv, "c:d:vs")) != -1) - { - switch (opt) - { - case 'c': - VERB1 log("Loading settings from '%s'", optarg); - load_conf_file(optarg, settings, /*skip key w/o values:*/ true); - VERB3 log("Loaded '%s'", optarg); - break; - case 'd': - dump_dir_name = optarg; - break; - case 'v': - g_verbose++; - break; - case 's': - opts |= OPT_s; - break; - default: - /* Careful: the string below contains tabs, dont replace with spaces */ - error_msg_and_die( - "Usage: "PROGNAME" -c CONFFILE -d DIR [-vs]" - "\n" - "\nReport a crash to RHTSupport" - "\n" - "\nOptions:" - "\n -c FILE Configuration file (may be given many times)" - "\n -d DIR Crash dump directory" - "\n -v Be verbose" - "\n -s Log to syslog" - ); - } - } + /* Keep enum above and order of options below in sync! */ + struct options program_options[] = { + OPT__VERBOSE(&g_verbose), + OPT_BOOL( 's', NULL, NULL , _("Log to syslog")), + OPT_STRING('d', NULL, &dump_dir_name, "DIR" , _("Crash dump directory")), + OPT_LIST( 'c', NULL, &conf_file , "FILE", _("Configuration file (may be given many times)")), + OPT_END() + }; + unsigned opts = parse_opts(argc, argv, program_options, program_usage_string); putenv(xasprintf("ABRT_VERBOSE=%u", g_verbose)); //DONT! our stdout/stderr goes directly to daemon, don't want to have prefix there. @@ -304,6 +289,15 @@ int main(int argc, char **argv) logmode = LOGMODE_SYSLOG; } + while (conf_file) + { + char *fn = (char *)conf_file->data; + VERB1 log("Loading settings from '%s'", fn); + load_conf_file(fn, settings, /*skip key w/o values:*/ true); + VERB3 log("Loaded '%s'", fn); + conf_file = g_list_remove(conf_file, fn); + } + VERB1 log("Initializing XML-RPC library"); xmlrpc_env env; xmlrpc_env_init(&env); diff --git a/src/plugins/abrt-action-upload.c b/src/plugins/abrt-action-upload.c index 6b54962b..82c99fd5 100644 --- a/src/plugins/abrt-action-upload.c +++ b/src/plugins/abrt-action-upload.c @@ -21,7 +21,6 @@ #include #include "abrtlib.h" #include "parse_options.h" -#include "abrt_crash_data.h" #define PROGNAME "abrt-action-upload" -- cgit