From d1f7100bcfa20a395f5c90710d3ef23b46a51bc7 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Mon, 25 Feb 2013 01:12:38 +0200 Subject: [PATCH] ast: add error handling to XkbFileFromComponents And try to not repeat ourselves. Signed-off-by: Ran Benita --- src/xkbcomp/ast-build.c | 56 ++++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 24 deletions(-) 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 -- 2.7.4