Commit f57c13ea4f1e53507540dab5ddb73617ec4d635d

Peter Hutterer 2019-09-03T10:56:01

rules: factor out the function to parse a rules file No functional changes, this just makes the part to parse a single rules file re-usable. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>

diff --git a/src/xkbcomp/rules.c b/src/xkbcomp/rules.c
index 2a364c8..47c2ff9 100644
--- a/src/xkbcomp/rules.c
+++ b/src/xkbcomp/rules.c
@@ -999,6 +999,37 @@ error:
     return false;
 }
 
+static bool
+read_rules_file(struct xkb_context *ctx,
+                struct matcher *matcher,
+                const char *path,
+                struct xkb_component_names *out)
+{
+    bool ret = false;
+    FILE *file;
+    char *string;
+    size_t size;
+
+    file = fopen(path, "r");
+    if (!file)
+        goto out;
+
+    ret = map_file(file, &string, &size);
+    if (!ret) {
+        log_err(ctx, "Couldn't read rules file \"%s\": %s\n",
+                path, strerror(errno));
+        goto out;
+    }
+
+    ret = matcher_match(matcher, string, size, path, out);
+
+    unmap_file(string, size);
+out:
+    if (file)
+        fclose(file);
+    return ret;
+}
+
 bool
 xkb_components_from_rules(struct xkb_context *ctx,
                           const struct xkb_rule_names *rmlvo,
@@ -1007,31 +1038,23 @@ xkb_components_from_rules(struct xkb_context *ctx,
     bool ret = false;
     FILE *file;
     char *path;
-    char *string;
-    size_t size;
     struct matcher *matcher;
 
     file = FindFileInXkbPath(ctx, rmlvo->rules, FILE_TYPE_RULES, &path);
     if (!file)
         goto err_out;
 
-    ret = map_file(file, &string, &size);
-    if (!ret) {
-        log_err(ctx, "Couldn't read rules file \"%s\": %s\n",
-                path, strerror(errno));
-        goto err_file;
-    }
+    fclose(file); /* FIXME: fix FindFileInXkbPath to not open the file in
+                     the first place */
 
     matcher = matcher_new(ctx, rmlvo);
-    ret = matcher_match(matcher, string, size, path, out);
+
+    ret = read_rules_file(ctx, matcher, path, out);
     if (!ret)
         log_err(ctx, "No components returned from XKB rules \"%s\"\n", path);
-    matcher_free(matcher);
 
-    unmap_file(string, size);
-err_file:
+    matcher_free(matcher);
     free(path);
-    fclose(file);
 err_out:
     return ret;
 }