This patch makes it easier to analyze "bool" type options -
now they are all just bits in a single return value,
no need to have an integer variable for each option.
The previous behavior is retained too: it is useful for
"cumulative" options like -vvv.
The patch also removes all OPT_GROUPs.
Run tested. Please review.
--
vda
diff -x '*.po' -d -urpN abrt.2/lib/utils/parse_options.c
abrt.3/lib/utils/parse_options.c
--- abrt.2/lib/utils/parse_options.c 2010-10-08 15:14:49.727480330 +0200
+++ abrt.3/lib/utils/parse_options.c 2010-10-08 15:09:04.736481218 +0200
@@ -61,7 +61,7 @@ static int parse_opt_size(const struct o
return size;
}
-void parse_opts(int argc, char **argv, const struct options *opt,
+unsigned parse_opts(int argc, char **argv, const struct options *opt,
const char *usage)
{
int help = 0;
@@ -107,10 +107,10 @@ void parse_opts(int argc, char **argv, c
longopts[ii].val = 0;
*/
- int option_index = 0;
+ unsigned retval = 0;
while (1)
{
- int c = getopt_long(argc, argv, shortopts->buf, longopts, &option_index);
+ int c = getopt_long(argc, argv, shortopts->buf, longopts, NULL);
if (c == -1)
break;
@@ -126,7 +126,10 @@ void parse_opts(int argc, char **argv, c
{
if (opt[ii].short_name == c)
{
- switch (opt[ii].type)
+ if (ii < sizeof(retval)*8)
+ retval |= (1 << ii);
+
+ if (opt[ii].value != NULL) switch (opt[ii].type)
{
case OPTION_BOOL:
*(int*)opt[ii].value += 1;
@@ -148,4 +151,6 @@ void parse_opts(int argc, char **argv, c
free(longopts);
strbuf_free(shortopts);
+
+ return retval;
}
diff -x '*.po' -d -urpN abrt.2/lib/utils/parse_options.h
abrt.3/lib/utils/parse_options.h
--- abrt.2/lib/utils/parse_options.h 2010-10-08 13:45:05.351480477 +0200
+++ abrt.3/lib/utils/parse_options.h 2010-10-08 14:11:49.404480788 +0200
@@ -39,7 +39,7 @@ struct options {
#define OPT__VERBOSE(v) OPT_BOOL('v', "verbose", (v), "Be
verbose")
-void parse_opts(int argc, char **argv, const struct options *opt,
+unsigned parse_opts(int argc, char **argv, const struct options *opt,
const char *usage);
void parse_usage_and_die(const char *usage, const struct options *opt);
diff -x '*.po' -d -urpN abrt.2/src/daemon/abrt-action-generate-backtrace.c
abrt.3/src/daemon/abrt-action-generate-backtrace.c
--- abrt.2/src/daemon/abrt-action-generate-backtrace.c 2010-10-08 14:06:36.810230965
+0200
+++ abrt.3/src/daemon/abrt-action-generate-backtrace.c 2010-10-08 14:26:01.017480544
+0200
@@ -240,18 +240,24 @@ static char *get_backtrace(struct dump_d
return bt;
}
-static char *d_opt, *i_opt;
-static int s_opt;
+enum {
+ OPT_v = 1 << 0,
+ OPT_d = 1 << 1,
+ OPT_i = 1 << 2,
+ OPT_t = 1 << 3,
+ OPT_s = 1 << 4,
+};
+
+static char *i_opt;
static const char abrt_action_generage_backtrace_usage[] =
"abrt-action-generate-backtrace [options] -d DIR";
static struct options abrt_action_generate_backtrace_options[] = {
OPT__VERBOSE(&g_verbose),
- OPT_GROUP(""),
- OPT_STRING( 'd' , 0, &d_opt, "dir", "Crash dump
directory"),
+ OPT_STRING( 'd' , 0, &dump_dir_name, "dir", "Crash dump
directory"),
OPT_STRING( 'i' , 0, &i_opt, "dir1[:dir2]...", "Additional
debuginfo directories"),
OPT_INTEGER( 't' , 0, &exec_timeout_sec, "Kill gdb if it runs for
more than SECONDS"),
- OPT_BOOL( 's' , 0, &s_opt, "Log to syslog even with -d"),
+ OPT_BOOL( 's' , 0, NULL, "Log to syslog even with -d"),
OPT_END()
};
@@ -261,26 +267,18 @@ int main(int argc, char **argv)
if (env_verbose)
g_verbose = atoi(env_verbose);
- parse_opts(argc, argv, abrt_action_generate_backtrace_options,
+ unsigned opts = parse_opts(argc, argv, abrt_action_generate_backtrace_options,
abrt_action_generage_backtrace_usage);
- if (!d_opt)
- parse_usage_and_die(abrt_action_generage_backtrace_usage,
- abrt_action_generate_backtrace_options);
-
- dump_dir_name = d_opt;
-
- debuginfo_dirs = xstrdup("");
if (i_opt)
- {
- free(debuginfo_dirs);
debuginfo_dirs = xasprintf("%s:%s", DEBUGINFO_CACHE_DIR, i_opt);
- }
+ else
+ debuginfo_dirs = xstrdup("");
putenv(xasprintf("ABRT_VERBOSE=%u", g_verbose));
msg_prefix = xasprintf("abrt-action-generate-backtrace[%u]", getpid());
- if (s_opt)
+ if (opts & OPT_s)
{
openlog(msg_prefix, 0, LOG_DAEMON);
logmode = LOGMODE_SYSLOG;
diff -x '*.po' -d -urpN abrt.2/src/daemon/abrt-action-save-package-data.cpp
abrt.3/src/daemon/abrt-action-save-package-data.cpp
--- abrt.2/src/daemon/abrt-action-save-package-data.cpp 2010-10-08 14:09:48.925231033
+0200
+++ abrt.3/src/daemon/abrt-action-save-package-data.cpp 2010-10-08 14:30:10.713230481
+0200
@@ -267,16 +267,20 @@ static int SavePackageDescriptionToDebug
return error;
}
-static char *d_opt;
-static int s_opt;
+enum {
+ OPT_v = 1 << 0,
+ OPT_d = 1 << 1,
+ OPT_s = 1 << 2,
+};
+
+static const char *dump_dir_name = ".";
static const char abrt_action_save_package_data_usage[] =
"abrt-action-save-package-data [options] -d DIR";
static struct options abrt_action_save_package_data_options[] = {
OPT__VERBOSE(&g_verbose),
- OPT_GROUP(""),
- OPT_STRING( 'd' , 0, &d_opt, "dir", "Crash dump
directory"),
- OPT_BOOL( 's' , 0, &s_opt, "Log to syslog"),
+ OPT_STRING( 'd' , 0, &dump_dir_name, "dir", "Crash dump
directory"),
+ OPT_BOOL( 's' , 0, NULL, "Log to syslog"),
OPT_END()
};
@@ -286,20 +290,13 @@ int main(int argc, char **argv)
if (env_verbose)
g_verbose = atoi(env_verbose);
- const char *dump_dir_name = ".";
-
- parse_opts(argc, argv, abrt_action_save_package_data_options,
+ unsigned opts = parse_opts(argc, argv, abrt_action_save_package_data_options,
abrt_action_save_package_data_usage);
- if (!d_opt)
- parse_usage_and_die(abrt_action_save_package_data_usage,
- abrt_action_save_package_data_options);
-
- dump_dir_name = d_opt;
-
putenv(xasprintf("ABRT_VERBOSE=%u", g_verbose));
msg_prefix = xasprintf("abrt-action-save-package-data[%u]", getpid());
- if (s_opt)
+
+ if (opts & OPT_s)
{
openlog(msg_prefix, 0, LOG_DAEMON);
logmode = LOGMODE_SYSLOG;
diff -x '*.po' -d -urpN abrt.2/src/daemon/abrt-server.c
abrt.3/src/daemon/abrt-server.c
--- abrt.2/src/daemon/abrt-server.c 2010-10-08 13:41:39.833480840 +0200
+++ abrt.3/src/daemon/abrt-server.c 2010-10-08 14:19:56.006230679 +0200
@@ -277,15 +277,18 @@ static void process_message(const char *
static void dummy_handler(int sig_unused) {}
-static int s_opt;
+enum {
+ OPT_v = 1 << 0,
+ OPT_u = 1 << 1,
+ OPT_s = 1 << 2,
+};
static const char abrt_server_usage[] = "abrt-server [options]";
static struct options abrt_server_options[] = {
OPT__VERBOSE(&g_verbose),
- OPT_GROUP(""),
OPT_INTEGER( 'u' , 0, &client_uid, "Use UID as client uid"),
- OPT_BOOL( 's' , 0, &s_opt, "Log to syslog"),
+ OPT_BOOL( 's' , 0, NULL, "Log to syslog"),
OPT_END()
};
@@ -295,12 +298,12 @@ int main(int argc, char **argv)
if (env_verbose)
g_verbose = atoi(env_verbose);
- parse_opts(argc, argv, abrt_server_options,
+ unsigned opts = parse_opts(argc, argv, abrt_server_options,
abrt_server_usage);
putenv(xasprintf("ABRT_VERBOSE=%u", g_verbose));
msg_prefix = xasprintf("abrt-server[%u]", getpid());
- if (s_opt)
+ if (opts & OPT_s)
{
openlog(msg_prefix, 0, LOG_DAEMON);
logmode = LOGMODE_SYSLOG;
diff -x '*.po' -d -urpN abrt.2/src/daemon/Daemon.cpp abrt.3/src/daemon/Daemon.cpp
--- abrt.2/src/daemon/Daemon.cpp 2010-10-08 14:07:54.858480511 +0200
+++ abrt.3/src/daemon/Daemon.cpp 2010-10-08 14:29:41.929480303 +0200
@@ -87,8 +87,6 @@ using namespace std;
*/
CCommLayerServer* g_pCommLayer;
-static bool daemonize = true;
-
static volatile sig_atomic_t s_sig_caught;
static int s_signal_pipe[2];
static int s_signal_pipe_write = -1;
@@ -860,16 +858,21 @@ static void sanitize_dump_dir_rights()
ensure_writable_dir(VAR_RUN"/abrt", 0755, "root");
}
-static int daemonize_opt, syslog_opt;
+enum {
+ OPT_v = 1 << 0,
+ OPT_d = 1 << 1,
+ OPT_s = 1 << 2,
+ OPT_t = 1 << 3,
+};
+
static char *timeout_opt;
static const char* abrtd_usage = _("abrtd [options]");
static struct options abrtd_options[] = {
OPT__VERBOSE(&g_verbose),
- OPT_GROUP(""),
- OPT_BOOL( 'd' , 0, &daemonize_opt, _("Do not daemonize")),
- OPT_BOOL( 's' , 0, &syslog_opt, _("Log to syslog even with
-d")),
+ OPT_BOOL( 'd' , 0, NULL, _("Do not daemonize")),
+ OPT_BOOL( 's' , 0, NULL, _("Log to syslog even with -d")),
OPT_INTEGER( 't' , 0, &timeout_opt, _("Exit after SEC seconds of
inactivity")),
OPT_END()
};
@@ -892,12 +895,9 @@ int main(int argc, char** argv)
if (env_verbose)
g_verbose = atoi(env_verbose);
- parse_opts(argc, argv, abrtd_options, abrtd_usage);
-
- if (daemonize_opt)
- daemonize = false;
+ unsigned opts = parse_opts(argc, argv, abrtd_options, abrtd_usage);
- if (syslog_opt)
+ if (opts & OPT_s)
start_syslog_logging();
putenv(xasprintf("ABRT_VERBOSE=%u", g_verbose));
@@ -914,7 +914,7 @@ int main(int argc, char** argv)
signal(SIGALRM, handle_signal);
/* Daemonize unless -d */
- if (daemonize)
+ if (!(opts & OPT_d))
{
/* forking to background */
pid_t pid = fork();
@@ -1042,13 +1042,13 @@ int main(int argc, char** argv)
/* Initialization error */
error_msg("Error while initializing daemon");
/* Inform parent that initialization failed */
- if (daemonize)
+ if (!(opts & OPT_d))
kill(parent_pid, SIGINT);
goto cleanup;
}
/* Inform parent that we initialized ok */
- if (daemonize)
+ if (!(opts & OPT_d))
{
VERB1 log("Signalling parent");
kill(parent_pid, SIGTERM);