From a483c39fc20aced2178c908405fd020eda753964 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 18 Mar 2025 10:24:37 +0900 Subject: [PATCH] analyze-condition: rewrite condition parser No functional change, just a refactoring. --- src/analyze/analyze-condition.c | 67 ++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/src/analyze/analyze-condition.c b/src/analyze/analyze-condition.c index e7871060fe5..630c4b389be 100644 --- a/src/analyze/analyze-condition.c +++ b/src/analyze/analyze-condition.c @@ -14,41 +14,48 @@ static int parse_condition(Unit *u, const char *line) { assert(u); assert(line); - for (ConditionType t = 0; t < _CONDITION_TYPE_MAX; t++) { - ConfigParserCallback callback; - Condition **target; - const char *p, *name; - - name = condition_type_to_string(t); - p = startswith(line, name); - if (p) - target = &u->conditions; - else { - name = assert_type_to_string(t); - p = startswith(line, name); - if (!p) - continue; - - target = &u->asserts; - } - - p += strspn(p, WHITESPACE); + line = skip_leading_chars(line, /* bad = */ NULL); - if (*p != '=') - continue; - p++; + const char *eq = strchr(line, '='); + if (!eq) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot parse \"%s\".", line); - p += strspn(p, WHITESPACE); + _cleanup_free_ char *type_string = strndup(line, eq - line); + if (!type_string) + return log_oom(); - if (condition_takes_path(t)) - callback = config_parse_unit_condition_path; - else - callback = config_parse_unit_condition_string; + delete_trailing_chars(type_string, /* bad = */ NULL); - return callback(NULL, "(cmdline)", 0, NULL, 0, name, t, p, target, u); + Condition **target; + ConditionType type; + if (startswith(type_string, "Condition")) { + target = &u->conditions; + type = condition_type_from_string(type_string); + } else { + target = &u->asserts; + type = assert_type_from_string(type_string); } - - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot parse \"%s\".", line); + if (type < 0) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot parse \"%s\".", line); + + const char *val = skip_leading_chars(eq + 1, /* bad = */ NULL); + + ConfigParserCallback callback; + if (condition_takes_path(type)) + callback = config_parse_unit_condition_path; + else + callback = config_parse_unit_condition_string; + + return callback(/* unit = */ NULL, + /* filename = */ "(cmdline)", + /* line = */ 0, + /* section = */ NULL, + /* section_line = */ 0, + /* lvalue = */ type_string, + /* ltype = */ type, + /* rvalue = */ val, + /* data = */ target, + /* userdata = */ u); } _printf_(7, 8) -- 2.47.3