diff options
author | James Booth <boothj5@gmail.com> | 2014-04-14 23:01:57 +0100 |
---|---|---|
committer | James Booth <boothj5@gmail.com> | 2014-04-14 23:01:57 +0100 |
commit | 241973700697abb60104c656a0aacfd182eb2316 (patch) | |
tree | a9aa0dcf7576a819a835e48d6c35c4c17de32552 /src | |
parent | 8d77930eceba6841673605f7df5083657a0ecd28 (diff) | |
download | profani-tty-241973700697abb60104c656a0aacfd182eb2316.tar.gz |
Check for duplicate options in option parser
Diffstat (limited to 'src')
-rw-r--r-- | src/tools/parser.c | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/src/tools/parser.c b/src/tools/parser.c index 375572e0..b4403d06 100644 --- a/src/tools/parser.c +++ b/src/tools/parser.c @@ -385,14 +385,31 @@ parse_options(gchar **args, int start, GList *keys, gboolean *res) return options; } - // check each option is valid and has value, failure if not + // validate options int curr; + GList *found_keys = NULL; for (curr = start; curr < g_strv_length(args); curr+= 2) { - if ((g_list_find(keys, args[curr]) == NULL) || (args[curr+1] == NULL)) { + // check if option valid + if (g_list_find(keys, args[curr]) == NULL) { *res = FALSE; return options; } + + // check if duplicate + if (g_list_find(found_keys, args[curr]) != NULL) { + *res = FALSE; + return options; + } + + // check value given + if (args[curr+1] == NULL) { + *res = FALSE; + return options; + } + + found_keys = g_list_append(found_keys, args[curr]); } + g_list_free(found_keys); // create map options = g_hash_table_new(g_str_hash, g_str_equal); |