summaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorNikola Pajkovsky <npajkovs@redhat.com>2011-03-25 08:45:05 +0100
committerNikola Pajkovsky <npajkovs@redhat.com>2011-03-25 08:45:05 +0100
commitb3f653c67570f69fb459acf1b462957937fb89ac (patch)
tree10438030603db09ab611445f91c8bd6d1c6e37f0 /src/lib
parent31e8c83a5748a385f3db4561605da54b52ac2dc9 (diff)
parentefc9f956c65da5b151f20371c00d76aad326dc7b (diff)
downloadabrt-b3f653c67570f69fb459acf1b462957937fb89ac.tar.gz
abrt-b3f653c67570f69fb459acf1b462957937fb89ac.tar.xz
abrt-b3f653c67570f69fb459acf1b462957937fb89ac.zip
Merge branch 'event'
* event: validate input in cli and fix ask_for_missing_settings function parse and store 'allow-empty' value
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/event_config.c69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/lib/event_config.c b/src/lib/event_config.c
index 3f760214..4e14c79a 100644
--- a/src/lib/event_config.c
+++ b/src/lib/event_config.c
@@ -280,3 +280,72 @@ void unexport_event_config(GList *env_list)
free(var_val);
}
}
+
+GHashTable *validate_event(const char *event_name)
+{
+ event_config_t *config = get_event_config(event_name);
+ if (!config)
+ return NULL;
+
+
+ GHashTable *errors = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
+
+ for (GList *li = config->options; li; li = li->next)
+ {
+ event_option_t *opt = (event_option_t *)li->data;
+ char *err = validate_event_option(opt);
+ if (err)
+ g_hash_table_insert(errors, xstrdup(opt->name), err);
+ }
+
+ if (g_hash_table_size(errors))
+ return errors;
+
+ g_hash_table_destroy(errors);
+
+ return NULL;
+}
+
+/* return NULL if successful otherwise appropriate error message */
+char *validate_event_option(event_option_t *opt)
+{
+ if (!opt->allow_empty && (!opt->value || !opt->value[0]))
+ return xstrdup(_("Missing mandatory value"));
+
+ /* if value is NULL and allow-empty yes than it doesn't make sence to check it */
+ if (!opt->value)
+ return NULL;
+
+ const gchar *s = NULL;
+ if (!g_utf8_validate(opt->value, -1, &s))
+ return xasprintf(_("Invalid utf8 character '%c'"), *s);
+
+ switch (opt->type) {
+ case OPTION_TYPE_TEXT:
+ case OPTION_TYPE_PASSWORD:
+ break;
+ case OPTION_TYPE_NUMBER:
+ {
+ long r = strtol(opt->value, (char **)&s, 10);
+ (void) r;
+ if (*s || errno)
+ return xasprintf(_("Invalid number '%s'"), opt->value);
+ break;
+ }
+ case OPTION_TYPE_BOOL:
+ if (strcmp(opt->value, "yes") != 0
+ && strcmp(opt->value, "no") != 0
+ && strcmp(opt->value, "on") != 0
+ && strcmp(opt->value, "off") != 0
+ && strcmp(opt->value, "1") != 0
+ && strcmp(opt->value, "0") != 0)
+ {
+ return xasprintf(_("Invalid boolean value '%s'"), opt->value);
+ }
+ break;
+ default:
+ return xstrdup(_("Unsupported option type"));
+ };
+
+ return NULL;
+}