ast: add error handling to XkbFileFromComponents
authorRan Benita <ran234@gmail.com>
Sun, 24 Feb 2013 23:12:38 +0000 (01:12 +0200)
committerDaniel Stone <daniel@fooishbar.org>
Mon, 18 Mar 2013 22:20:03 +0000 (22:20 +0000)
And try to not repeat ourselves.

Signed-off-by: Ran Benita <ran234@gmail.com>
src/xkbcomp/ast-build.c

index 7259dc5..420f8e2 100644 (file)
@@ -503,30 +503,38 @@ XkbFile *
 XkbFileFromComponents(struct xkb_context *ctx,
                       const struct xkb_component_names *kkctgs)
 {
-    IncludeStmt *inc;
-    XkbFile *keycodes, *types, *compat, *symbols;
-
-    inc = IncludeCreate(ctx, kkctgs->keycodes, MERGE_DEFAULT);
-    keycodes = XkbFileCreate(ctx, FILE_TYPE_KEYCODES, NULL,
-                             (ParseCommon *) inc, 0);
-
-    inc = IncludeCreate(ctx, kkctgs->types, MERGE_DEFAULT);
-    types = XkbFileCreate(ctx, FILE_TYPE_TYPES, NULL,
-                          (ParseCommon *) inc, 0);
-    AppendStmt(&keycodes->common, &types->common);
-
-    inc = IncludeCreate(ctx, kkctgs->compat, MERGE_DEFAULT);
-    compat = XkbFileCreate(ctx, FILE_TYPE_COMPAT, NULL,
-                           (ParseCommon *) inc, 0);
-    AppendStmt(&keycodes->common, &compat->common);
-
-    inc = IncludeCreate(ctx, kkctgs->symbols, MERGE_DEFAULT);
-    symbols = XkbFileCreate(ctx, FILE_TYPE_SYMBOLS, NULL,
-                            (ParseCommon *) inc, 0);
-    AppendStmt(&keycodes->common, &symbols->common);
-
-    return XkbFileCreate(ctx, FILE_TYPE_KEYMAP, NULL,
-                         &keycodes->common, 0);
+    char *const components[] = {
+        kkctgs->keycodes, kkctgs->types,
+        kkctgs->compat, kkctgs->symbols,
+    };
+    enum xkb_file_type type;
+    IncludeStmt *include = NULL;
+    XkbFile *file = NULL;
+    ParseCommon *defs = NULL;
+
+    for (type = FIRST_KEYMAP_FILE_TYPE; type <= LAST_KEYMAP_FILE_TYPE; type++) {
+        include = IncludeCreate(ctx, components[type], MERGE_DEFAULT);
+        if (!include)
+            goto err;
+
+        file = XkbFileCreate(ctx, type, NULL, &include->common, 0);
+        if (!file) {
+            FreeInclude(include);
+            goto err;
+        }
+
+        defs = AppendStmt(defs, &file->common);
+    }
+
+    file = XkbFileCreate(ctx, FILE_TYPE_KEYMAP, NULL, defs, 0);
+    if (!file)
+        goto err;
+
+    return file;
+
+err:
+    FreeXkbFile((XkbFile *) defs);
+    return NULL;
 }
 
 static void