From: Tom Gundersen Date: Tue, 29 Oct 2013 12:03:13 +0000 (+0100) Subject: conf-parser: add macro for ENUMV X-Git-Tag: v209~1760 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=916484f54d084e11a11458716b2e0bbdf4822d40;ds=sidebyside conf-parser: add macro for ENUMV Parses a whitespace separated list of strings into a vector of enums. --- diff --git a/src/shared/conf-parser.h b/src/shared/conf-parser.h index 9435d54b1..f988e1c44 100644 --- a/src/shared/conf-parser.h +++ b/src/shared/conf-parser.h @@ -149,3 +149,64 @@ int log_syntax_internal(const char *unit, int level, *i = x; \ return 0; \ } + +#define DEFINE_CONFIG_PARSE_ENUMV(function,name,type,invalid,msg) \ + int function(const char *unit, \ + const char *filename, \ + unsigned line, \ + const char *section, \ + const char *lvalue, \ + int ltype, \ + const char *rvalue, \ + void *data, \ + void *userdata) { \ + \ + type **enums = data, *xs, x, *ys; \ + char *w, *state; \ + size_t l, i = 0; \ + \ + assert(filename); \ + assert(lvalue); \ + assert(rvalue); \ + assert(data); \ + \ + xs = new0(type, 1); \ + *xs = invalid; \ + \ + FOREACH_WORD(w, l, rvalue, state) { \ + _cleanup_free_ char *en = NULL; \ + \ + en = strndup(w, l); \ + if (!en) \ + return -ENOMEM; \ + \ + if ((x = name##_from_string(en)) < 0) { \ + log_syntax(unit, LOG_ERR, filename, line, \ + -x, msg ", ignoring: %s", en); \ + continue; \ + } \ + \ + for (ys = xs; x != invalid && *ys != invalid; ys++) { \ + if (*ys == x) { \ + log_syntax(unit, LOG_ERR, filename, \ + line, -x, \ + "Duplicate entry, ignoring: %s", \ + en); \ + x = invalid; \ + } \ + } \ + \ + if (x == invalid) \ + continue; \ + \ + *(xs + i) = x; \ + xs = realloc(xs, ++i + 1); \ + if (!xs) \ + return -ENOMEM; \ + *(xs + i) = invalid; \ + } \ + \ + free(*enums); \ + *enums = xs; \ + return 0; \ + }