Commit c8fd3ea4b67b3764ee7c0cf4ad35ac9ba89ec518

Daniel Stone 2012-03-10T13:48:13

Move allocation of xkb_desc into CompileKeymap Signed-off-by: Daniel Stone <daniel@fooishbar.org>

diff --git a/src/xkbcomp/keymap.c b/src/xkbcomp/keymap.c
index f73eeff..76e2bc8 100644
--- a/src/xkbcomp/keymap.c
+++ b/src/xkbcomp/keymap.c
@@ -31,14 +31,15 @@
 #include "action.h"
 #include "misc.h"
 #include "indicators.h"
+#include "xkballoc.h"
 
 /**
  * Compile the given file and store the output in xkb.
  * @param file A list of XkbFiles, each denoting one type (e.g.
  * XkmKeyNamesIdx, etc.)
  */
-Bool
-CompileKeymap(XkbFile *file, struct xkb_desc * xkb, unsigned merge)
+struct xkb_desc *
+CompileKeymap(XkbFile *file, unsigned merge)
 {
     unsigned have;
     Bool ok;
@@ -46,6 +47,7 @@ CompileKeymap(XkbFile *file, struct xkb_desc * xkb, unsigned merge)
     unsigned mainType;
     char *mainName;
     LEDInfo *unbound = NULL;
+    struct xkb_desc *xkb = XkbcAllocKeyboard();
     struct {
         XkbFile *keycodes;
         XkbFile *types;
@@ -53,6 +55,9 @@ CompileKeymap(XkbFile *file, struct xkb_desc * xkb, unsigned merge)
         XkbFile *symbols;
     } sections;
 
+    if (!xkb)
+        return NULL;
+
     memset(&sections, 0, sizeof(sections));
     mainType = file->type;
     mainName = file->name;
@@ -91,13 +96,13 @@ CompileKeymap(XkbFile *file, struct xkb_desc * xkb, unsigned merge)
             ERROR("More than one %s section in a %s file\n",
                    XkbcConfigText(file->type), XkbcConfigText(mainType));
             ACTION("All sections after the first ignored\n");
-            return False;
+            continue;
         }
         else if ((1 << file->type) & (~legal))
         {
             ERROR("Cannot define %s in a %s file\n",
                    XkbcConfigText(file->type), XkbcConfigText(mainType));
-            return False;
+            continue;
         }
 
         switch (file->type)
@@ -146,32 +151,50 @@ CompileKeymap(XkbFile *file, struct xkb_desc * xkb, unsigned merge)
         {
             if (missing & bit)
             {
-                ERROR("Missing %s section in a %s file\n",
-                       XkbcConfigText(i), XkbcConfigText(mainType));
+                ERROR("Required section %s missing from keymap\n", XkbcConfigText(i));
                 missing &= ~bit;
             }
         }
-        ACTION("Description of %s not compiled\n",
-                XkbcConfigText(mainType));
-        return False;
+        goto err;
     }
 
     /* compile the sections we have in the file one-by-one, or fail. */
     if (sections.keycodes != NULL &&
         !CompileKeycodes(sections.keycodes, xkb, MergeOverride))
-        return False;
+    {
+        ERROR("Failed to compile keycodes\n");
+        goto err;
+    }
     if (sections.types != NULL &&
         !CompileKeyTypes(sections.types, xkb, MergeOverride))
-        return False;
+    {
+        ERROR("Failed to compile key types\n");
+        goto err;
+    }
     if (sections.compat != NULL &&
         !CompileCompatMap(sections.compat, xkb, MergeOverride, &unbound))
-        return False;
+    {
+        ERROR("Failed to compile compat map\n");
+        goto err;
+    }
     if (sections.symbols != NULL &&
         !CompileSymbols(sections.symbols, xkb, MergeOverride))
-        return False;
+    {
+        ERROR("Failed to compile symbols\n");
+        goto err;
+    }
 
     xkb->defined = have;
 
     ok = BindIndicators(xkb, True, unbound, NULL);
-    return ok;
+    if (!ok)
+        goto err;
+
+    return xkb;
+
+err:
+    ACTION("Failed to compile keymap\n");
+    if (xkb)
+        xkb_free_keymap(xkb);
+    return NULL;
 }
diff --git a/src/xkbcomp/xkbcomp.c b/src/xkbcomp/xkbcomp.c
index 74aa1fb..6c01224 100644
--- a/src/xkbcomp/xkbcomp.c
+++ b/src/xkbcomp/xkbcomp.c
@@ -189,8 +189,9 @@ compile_keymap(XkbFile *file, const char *mapName)
     struct xkb_desc * xkb = NULL;
 
     /* Find map to use */
-    if (!(mapToUse = XkbChooseMap(file, mapName)))
-        goto unwind_file;
+    mapToUse = XkbChooseMap(file, mapName);
+    if (!mapToUse)
+        goto err;
 
     switch (mapToUse->type) {
     case XkmSemanticsFile:
@@ -199,26 +200,20 @@ compile_keymap(XkbFile *file, const char *mapName)
         break;
     default:
         ERROR("file type %d not handled\n", mapToUse->type);
-        goto unwind_file;
+        goto err;
     }
 
-    /* Compile the keyboard */
-    if (!(xkb = XkbcAllocKeyboard())) {
-        ERROR("could not allocate keyboard description\n");
-        goto unwind_file;
-    }
+    xkb = CompileKeymap(mapToUse, MergeReplace);
+    if (!xkb)
+        goto err;
 
-    if (!CompileKeymap(mapToUse, xkb, MergeReplace)) {
-        ERROR("failed to compile keymap\n");
-        XkbcFreeKeyboard(xkb);
-        xkb = NULL;
-    }
+    return xkb;
 
-unwind_file:
+err:
     FreeXKBFile(file);
     free(scanFile);
     XkbFreeIncludePath();
-    return xkb;
+    return NULL;
 }
 
 struct xkb_desc *
diff --git a/src/xkbcomp/xkbcomp.h b/src/xkbcomp/xkbcomp.h
index f03de4f..db81e80 100644
--- a/src/xkbcomp/xkbcomp.h
+++ b/src/xkbcomp/xkbcomp.h
@@ -267,8 +267,8 @@ typedef struct _XkbFile
     Bool compiled;
 } XkbFile;
 
-extern Bool
-CompileKeymap(XkbFile *file, struct xkb_desc * xkb, unsigned merge);
+extern struct xkb_desc *
+CompileKeymap(XkbFile *file, unsigned merge);
 
 extern Bool
 CompileKeycodes(XkbFile *file, struct xkb_desc * xkb, unsigned merge);