diff options
author | Andrés M <andmarti1424@users.noreply.github.com> | 2021-03-22 09:10:07 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-22 09:10:07 -0300 |
commit | 23b59d56245fd43c0def7a4ecad76e384bf5d6f7 (patch) | |
tree | ef7864664588ede1fc765de9a3ecd3d943ce3698 /src | |
parent | 34b4619e1c856dd4f48f9ea55cff603ca6d19a98 (diff) | |
parent | d76c7668fb509e2817e0ff95913eb28e2519f741 (diff) |
Merge pull request #526 from npitre/dict
fix dictionary string parser
Diffstat (limited to 'src')
-rw-r--r-- | src/conf.c | 7 | ||||
-rw-r--r-- | src/main.c | 10 | ||||
-rwxr-xr-x | src/utils/dictionary.c | 60 | ||||
-rwxr-xr-x | src/utils/dictionary.h | 2 |
4 files changed, 51 insertions, 28 deletions
@@ -107,12 +107,7 @@ const char default_config[] = // argument rather than using user_conf_d directly. void store_default_config_values() { - const char *line = default_config; - - do { - parse_str(user_conf_d, line, 0); - line = strchr(line, '\n'); - } while(line && *++line != 0); + parse_str(user_conf_d, default_config, 0); // Calculate GMT offset (not on Solaris, doesn't have tm_gmtoff) #if defined(USELOCALE) && !defined(__sun) @@ -551,15 +551,7 @@ void read_argv(int argc, char ** argv) { int i; for (i = 1; i < argc; i++) { if ( ! strncmp(argv[i], "--", 2) ) { // it was passed a parameter - char *dup = strdup(argv[i]); - char *rest = dup; - char *name = strsep(&rest, "="); - if (rest) { - put(user_conf_d, &name[2], rest); // --parameter=value - } else { - put(user_conf_d, &name[2], "1"); // --parameter - } - free(dup); + parse_str(user_conf_d, argv[i] + 2, 0); } else { // it was passed a file strncpy(loadingfile, argv[i], PATHLEN-1); } diff --git a/src/utils/dictionary.c b/src/utils/dictionary.c index 617b861..3d7e3a0 100755 --- a/src/utils/dictionary.c +++ b/src/utils/dictionary.c @@ -204,32 +204,68 @@ char * get_key_name(struct dictionary * d, const char * value) { * * \param[in] d * \param[in] str - * \param[in] blank_space + * \param[in] split_on_blanks * * \return dictionary */ -void parse_str(struct dictionary * d, const char * str, int no_blanks) { +void parse_str(struct dictionary *d, const char *str, int split_on_blanks) { char key[90]; char value[90]; int i; - while (*str != '\0') { + while (*str != 0) { + /* remove leading field separators */ + if (*str == ' ' || *str == '\n') { + str++; + continue; + } + + /* collect the key */ i = 0; - while (*str != 0 && *str != '=' && *str != '\n' && i < sizeof(key) && *str != ' ') { + for (;;) { + if (*str == '=') { + /* we are done with the key */ + key[i] = 0; + break; + } + if (*str == 0 || *str == '\n' || (split_on_blanks && *str == ' ')) { + /* got only a key: pretend the value is 1 */ + key[i] = 0; + put(d, key, "1"); + break; + } + if (*str == ' ') { + /* spaces in the key are invalid */ + return; + } + if (i >= sizeof(key) - 1) { + /* won't have room for final '\0' */ + return; + } key[i++] = *str++; } - if (i >= sizeof(key) || *str++ != '=') continue; - key[i] = 0; + if (*str != '=') { + /* no value to collect */ + continue; + } + str++; + + /* collect the value */ i = 0; - while (*str != 0 && *str != '\n' && i < sizeof(value) && !(no_blanks && *str == ' ')) { + for (;;) { + if (*str == 0 || *str == '\n' || (split_on_blanks && *str == ' ')) { + /* we are done with the value */ + value[i] = 0; + put(d, key, value); + break; + } + if (i >= sizeof(value) - 1) { + /* won't have room for final '\0' */ + return; + } value[i++] = *str++; } - if (i >= sizeof(value)) return; - value[i] = 0; - - // Create the dictionary - put(d, key, value); } } diff --git a/src/utils/dictionary.h b/src/utils/dictionary.h index b5a1838..83bb955 100755 --- a/src/utils/dictionary.h +++ b/src/utils/dictionary.h @@ -60,5 +60,5 @@ void destroy_dictionary(struct dictionary * d); char * get(struct dictionary * d, const char * key); int get_int(struct dictionary * d, const char * key); //char * get_key_name(struct dictionary * d, const char * value); -void parse_str(struct dictionary * d, const char * str, int no_blanks); +void parse_str(struct dictionary * d, const char * str, int split_on_blanks); int get_dict_buffer_size(struct dictionary * d); |