summaryrefslogtreecommitdiffstats
path: root/lib/utils
diff options
context:
space:
mode:
authorNikola Pajkovsky <npajkovs@redhat.com>2010-09-29 13:06:44 +0200
committerNikola Pajkovsky <npajkovs@redhat.com>2010-10-04 15:42:44 +0200
commit8ff7545715add7b2924483c03508609dd9cd063a (patch)
tree99979845f8bfac1ec77e57a986ceffee6886c7c0 /lib/utils
parent36d755a2c09c630cad442a6424381683070b2cc0 (diff)
downloadabrt-8ff7545715add7b2924483c03508609dd9cd063a.tar.gz
abrt-8ff7545715add7b2924483c03508609dd9cd063a.tar.xz
abrt-8ff7545715add7b2924483c03508609dd9cd063a.zip
new args parser
Signed-off-by: Nikola Pajkovsky <npajkovs@redhat.com>
Diffstat (limited to 'lib/utils')
-rw-r--r--lib/utils/Makefile.am3
-rw-r--r--lib/utils/parse_options.c130
-rw-r--r--lib/utils/parse_options.h37
3 files changed, 169 insertions, 1 deletions
diff --git a/lib/utils/Makefile.am b/lib/utils/Makefile.am
index 1e104821..ac5f4837 100644
--- a/lib/utils/Makefile.am
+++ b/lib/utils/Makefile.am
@@ -33,7 +33,8 @@ libABRTUtils_la_SOURCES = \
abrt_packages.c abrt_packages.h \
hooklib.c hooklib.h \
sha1.c sha1.h \
- database.c
+ database.c \
+ parse_options.c parse_options.h
libABRTUtils_la_CPPFLAGS = \
-Wall -Werror \
-I$(srcdir)/../../inc \
diff --git a/lib/utils/parse_options.c b/lib/utils/parse_options.c
new file mode 100644
index 00000000..6928e481
--- /dev/null
+++ b/lib/utils/parse_options.c
@@ -0,0 +1,130 @@
+
+#include <getopt.h>
+#include "abrtlib.h"
+#include "parse_options.h"
+
+static int parse_opt_size(const struct options *opt)
+{
+ unsigned size = 1;
+ for (; opt->type != OPTION_END; opt++)
+ size++;
+
+ return size;
+}
+
+#define USAGE_OPTS_WIDTH 24
+#define USAGE_GAP 2
+
+void parse_usage_and_die(const char * const * usage, const struct options *opt)
+{
+ fprintf(stderr, _("usage: %s\n"), *usage++);
+
+ while (*usage && **usage)
+ fprintf(stderr, _(" or: %s\n"), *usage++);
+
+ fputc('\n', stderr);
+
+ for (; opt->type != OPTION_END; opt++)
+ {
+ size_t pos;
+ int pad;
+
+ pos = fprintf(stderr, " ");
+ if (opt->short_name)
+ pos += fprintf(stderr, "-%c", opt->short_name);
+
+ if (opt->short_name && opt->long_name)
+ pos += fprintf(stderr, ", ");
+
+ if (opt->long_name)
+ pos += fprintf(stderr, "--%s", opt->long_name);
+
+ if (pos <= USAGE_OPTS_WIDTH)
+ pad = USAGE_OPTS_WIDTH - pos;
+ else
+ {
+ fputc('\n', stderr);
+ pad = USAGE_OPTS_WIDTH;
+ }
+ fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opt->help);
+ }
+ fputc('\n', stderr);
+ exit(1);
+}
+
+void parse_opts(int argc, char **argv, const struct options *opt,
+ const char * const usage[])
+{
+ int size = parse_opt_size(opt);
+
+ struct strbuf *shortopts = strbuf_new();
+
+ struct option *longopts = xcalloc(sizeof(struct options), size);
+ int ii;
+ for (ii = 0; ii < size - 1; ++ii)
+ {
+ longopts[ii].name = opt[ii].long_name;
+
+ switch (opt[ii].type)
+ {
+ case OPTION_BOOL:
+ longopts[ii].has_arg = no_argument;
+ if (opt[ii].short_name)
+ strbuf_append_char(shortopts, opt[ii].short_name);
+ break;
+ case OPTION_STRING:
+ longopts[ii].has_arg = required_argument;
+ if (opt[ii].short_name)
+ strbuf_append_strf(shortopts, "%c:", opt[ii].short_name);
+ break;
+ case OPTION_END:
+ break;
+ }
+ longopts[ii].flag = 0;
+ longopts[ii].val = opt[ii].short_name;
+ }
+
+ longopts[ii].name = 0;
+ longopts[ii].has_arg = 0;
+ longopts[ii].flag = 0;
+ longopts[ii].val = 0;
+
+ int option_index = 0;
+ while (1)
+ {
+ int c = getopt_long(argc, argv, shortopts->buf, longopts, &option_index);
+
+ if (c == -1)
+ break;
+
+ if (c == '?')
+ {
+ free(longopts);
+ strbuf_free(shortopts);
+ parse_usage_and_die(usage, opt);
+ }
+
+ for (ii = 0; ii < size - 1; ++ii)
+ {
+ if (opt[ii].short_name == c)
+ {
+
+ switch (opt[ii].type)
+ {
+ case OPTION_BOOL:
+ *(int*)opt[ii].value += 1;
+ break;
+ case OPTION_STRING:
+ if (optarg)
+ *(char**)opt[ii].value = (char*)optarg;
+ break;
+ case OPTION_END:
+ break;
+ }
+ }
+ }
+ }
+
+ free(longopts);
+ strbuf_free(shortopts);
+}
diff --git a/lib/utils/parse_options.h b/lib/utils/parse_options.h
new file mode 100644
index 00000000..fe76a7f3
--- /dev/null
+++ b/lib/utils/parse_options.h
@@ -0,0 +1,37 @@
+
+#ifndef PARSE_OPTIONS_H
+#define PARSE_OPTIONS_H
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum parse_opt_type {
+ OPTION_BOOL,
+ OPTION_STRING,
+ OPTION_END,
+};
+
+struct options {
+ enum parse_opt_type type;
+ int short_name;
+ const char *long_name;
+ void *value;
+ const char *help;
+};
+
+#define OPT_END() { OPTION_END }
+#define OPT_BOOL(s, l, v, h) { OPTION_BOOL, (s), (l), (v), (h) }
+#define OPT_STRING(s, l, v, h) { OPTION_STRING, (s), (l), (v), (h) }
+
+void parse_opts(int argc, char **argv, const struct options *opt,
+ const char * const usage[]);
+
+void parse_usage_and_die(const char * const * usage, const struct options *opt);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif