ast: add error handling to XkbFileFromComponents And try to not repeat ourselves. Signed-off-by: Ran Benita <ran234@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
diff --git a/src/xkbcomp/ast-build.c b/src/xkbcomp/ast-build.c
index 7259dc5..420f8e2 100644
--- a/src/xkbcomp/ast-build.c
+++ b/src/xkbcomp/ast-build.c
@@ -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