488b3f2
From b126aca8d6b8b81527a7dcab0d9659a9b5d63d01 Mon Sep 17 00:00:00 2001
15e2ab3
From: =?UTF-8?q?Kristian=20H=C3=B8gsberg?= <krh@bitplanet.net>
15e2ab3
Date: Tue, 9 Apr 2013 16:54:55 -0400
973c76f
Subject: [PATCH 02/39] xkb: Add struct XkbCompContext
15e2ab3
15e2ab3
This commit adds a struct that contains most of the context for starting,
15e2ab3
running and cleaning up after xkbcomp.
15e2ab3
15e2ab3
Reviewed-by: Daniel Stone <daniel@fooishbar.org>
15e2ab3
---
15e2ab3
 xkb/ddxLoad.c | 76 +++++++++++++++++++++++++++++------------------------------
15e2ab3
 1 file changed, 38 insertions(+), 38 deletions(-)
15e2ab3
15e2ab3
diff --git a/xkb/ddxLoad.c b/xkb/ddxLoad.c
15e2ab3
index d462957..5da3a35 100644
15e2ab3
--- a/xkb/ddxLoad.c
15e2ab3
+++ b/xkb/ddxLoad.c
15e2ab3
@@ -90,14 +90,21 @@ OutputDirectory(char *outdir, size_t size)
15e2ab3
     }
15e2ab3
 }
15e2ab3
 
15e2ab3
+typedef struct XkbCompContext {
15e2ab3
+    char keymap[PATH_MAX];
15e2ab3
+    FILE *out;
15e2ab3
+    char *buf;
15e2ab3
+    char tmpname[PATH_MAX];
15e2ab3
+    const char *xkmfile;
15e2ab3
+} XkbCompContextRec, *XkbCompContextPtr;
15e2ab3
+
15e2ab3
 static Bool
15e2ab3
 XkbDDXCompileKeymapByNames(XkbDescPtr xkb,
15e2ab3
                            XkbComponentNamesPtr names,
15e2ab3
                            unsigned want,
15e2ab3
-                           unsigned need, char *nameRtrn, int nameRtrnLen)
15e2ab3
+                           unsigned need, XkbCompContextPtr ctx)
15e2ab3
 {
15e2ab3
-    FILE *out;
15e2ab3
-    char *buf = NULL, keymap[PATH_MAX], xkm_output_dir[PATH_MAX];
15e2ab3
+    char xkm_output_dir[PATH_MAX];
15e2ab3
 
15e2ab3
     const char *emptystring = "";
15e2ab3
     char *xkbbasedirflag = NULL;
15e2ab3
@@ -105,22 +112,19 @@ XkbDDXCompileKeymapByNames(XkbDescPtr xkb,
15e2ab3
     const char *xkbbindirsep = emptystring;
15e2ab3
 
15e2ab3
 #ifdef WIN32
15e2ab3
-    /* WIN32 has no popen. The input must be stored in a file which is
15e2ab3
-       used as input for xkbcomp. xkbcomp does not read from stdin. */
15e2ab3
-    char tmpname[PATH_MAX];
15e2ab3
-    const char *xkmfile = tmpname;
15e2ab3
+    ctx->xkmfile = ctx->tmpname;
15e2ab3
 #else
15e2ab3
-    const char *xkmfile = "-";
15e2ab3
+    ctx->xkmfile = "-";
15e2ab3
 #endif
15e2ab3
 
15e2ab3
-    snprintf(keymap, sizeof(keymap), "server-%s", display);
15e2ab3
+    snprintf(ctx->keymap, sizeof(ctx->keymap), "server-%s", display);
15e2ab3
 
15e2ab3
     OutputDirectory(xkm_output_dir, sizeof(xkm_output_dir));
15e2ab3
 
15e2ab3
 #ifdef WIN32
15e2ab3
-    strcpy(tmpname, Win32TempDir());
15e2ab3
-    strcat(tmpname, "\\xkb_XXXXXX");
15e2ab3
-    (void) mktemp(tmpname);
15e2ab3
+    strcpy(ctx->tmpname, Win32TempDir());
15e2ab3
+    strcat(ctx->tmpname, "\\xkb_XXXXXX");
15e2ab3
+    (void) mktemp(ctx->tmpname);
15e2ab3
 #endif
15e2ab3
 
15e2ab3
     if (XkbBaseDirectory != NULL) {
15e2ab3
@@ -139,73 +143,69 @@ XkbDDXCompileKeymapByNames(XkbDescPtr xkb,
15e2ab3
         }
15e2ab3
     }
15e2ab3
 
15e2ab3
-    if (asprintf(&buf,
15e2ab3
+    if (asprintf(&ctx->buf,
15e2ab3
                  "\"%s%sxkbcomp\" -w %d %s -xkm \"%s\" "
15e2ab3
                  "-em1 %s -emp %s -eml %s \"%s%s.xkm\"",
15e2ab3
                  xkbbindir, xkbbindirsep,
15e2ab3
                  ((xkbDebugFlags < 2) ? 1 :
15e2ab3
                   ((xkbDebugFlags > 10) ? 10 : (int) xkbDebugFlags)),
15e2ab3
-                 xkbbasedirflag ? xkbbasedirflag : "", xkmfile,
15e2ab3
+                 xkbbasedirflag ? xkbbasedirflag : "", ctx->xkmfile,
15e2ab3
                  PRE_ERROR_MSG, ERROR_PREFIX, POST_ERROR_MSG1,
15e2ab3
-                 xkm_output_dir, keymap) == -1)
15e2ab3
-        buf = NULL;
15e2ab3
+                 xkm_output_dir, ctx->keymap) == -1)
15e2ab3
+        ctx->buf = NULL;
15e2ab3
 
15e2ab3
     free(xkbbasedirflag);
15e2ab3
 
15e2ab3
-    if (!buf) {
15e2ab3
+    if (!ctx->buf) {
15e2ab3
         LogMessage(X_ERROR,
15e2ab3
                    "XKB: Could not invoke xkbcomp: not enough memory\n");
15e2ab3
         return FALSE;
15e2ab3
     }
15e2ab3
 
15e2ab3
 #ifndef WIN32
15e2ab3
-    out = Popen(buf, "w");
15e2ab3
+    ctx->out = Popen(ctx->buf, "w");
15e2ab3
 #else
15e2ab3
-    out = fopen(tmpname, "w");
15e2ab3
+    ctx->out = fopen(ctx->tmpname, "w");
15e2ab3
 #endif
15e2ab3
 
15e2ab3
-    if (out != NULL) {
15e2ab3
+    if (ctx->out != NULL) {
15e2ab3
 #ifdef DEBUG
15e2ab3
         if (xkbDebugFlags) {
15e2ab3
             ErrorF("[xkb] XkbDDXCompileKeymapByNames compiling keymap:\n");
15e2ab3
             XkbWriteXKBKeymapForNames(stderr, names, xkb, want, need);
15e2ab3
         }
15e2ab3
 #endif
15e2ab3
-        XkbWriteXKBKeymapForNames(out, names, xkb, want, need);
15e2ab3
+        XkbWriteXKBKeymapForNames(ctx->out, names, xkb, want, need);
15e2ab3
 #ifndef WIN32
15e2ab3
-        if (Pclose(out) == 0)
15e2ab3
+        if (Pclose(ctx->out) == 0)
15e2ab3
 #else
15e2ab3
-        if (fclose(out) == 0 && System(buf) >= 0)
15e2ab3
+        if (fclose(ctx->out) == 0 && System(ctx->buf) >= 0)
15e2ab3
 #endif
15e2ab3
         {
15e2ab3
             if (xkbDebugFlags)
15e2ab3
-                DebugF("[xkb] xkb executes: %s\n", buf);
15e2ab3
-            if (nameRtrn) {
15e2ab3
-                strlcpy(nameRtrn, keymap, nameRtrnLen);
15e2ab3
-            }
15e2ab3
-            free(buf);
15e2ab3
+                DebugF("[xkb] xkb executes: %s\n", ctx->buf);
15e2ab3
+            free(ctx->buf);
15e2ab3
 #ifdef WIN32
15e2ab3
-            unlink(tmpname);
15e2ab3
+            unlink(ctx->tmpname);
15e2ab3
 #endif
15e2ab3
             return TRUE;
15e2ab3
         }
15e2ab3
         else
15e2ab3
-            LogMessage(X_ERROR, "Error compiling keymap (%s)\n", keymap);
15e2ab3
+            LogMessage(X_ERROR, "Error compiling keymap (%s)\n", ctx->keymap);
15e2ab3
 #ifdef WIN32
15e2ab3
         /* remove the temporary file */
15e2ab3
-        unlink(tmpname);
15e2ab3
+        unlink(ctx->tmpname);
15e2ab3
 #endif
15e2ab3
     }
15e2ab3
     else {
15e2ab3
 #ifndef WIN32
15e2ab3
         LogMessage(X_ERROR, "XKB: Could not invoke xkbcomp\n");
15e2ab3
 #else
15e2ab3
-        LogMessage(X_ERROR, "Could not open file %s\n", tmpname);
15e2ab3
+        LogMessage(X_ERROR, "Could not open file %s\n", ctx->tmpname);
15e2ab3
 #endif
15e2ab3
     }
15e2ab3
-    if (nameRtrn)
15e2ab3
-        nameRtrn[0] = '\0';
15e2ab3
-    free(buf);
15e2ab3
+    ctx->keymap[0] = '\0';
15e2ab3
+    free(ctx->buf);
15e2ab3
     return FALSE;
15e2ab3
 }
15e2ab3
 
15e2ab3
@@ -256,6 +256,7 @@ XkbDDXLoadKeymapByNames(DeviceIntPtr keybd,
15e2ab3
     FILE *file;
15e2ab3
     char fileName[PATH_MAX];
15e2ab3
     unsigned missing;
15e2ab3
+    XkbCompContextRec ctx;
15e2ab3
 
15e2ab3
     *xkbRtrn = NULL;
15e2ab3
     if ((keybd == NULL) || (keybd->key == NULL) ||
15e2ab3
@@ -270,12 +271,11 @@ XkbDDXLoadKeymapByNames(DeviceIntPtr keybd,
15e2ab3
                    keybd->name ? keybd->name : "(unnamed keyboard)");
15e2ab3
         return 0;
15e2ab3
     }
15e2ab3
-    else if (!XkbDDXCompileKeymapByNames(xkb, names, want, need,
15e2ab3
-                                         nameRtrn, nameRtrnLen)) {
15e2ab3
+    else if (!XkbDDXCompileKeymapByNames(xkb, names, want, need, &ctx)) {
15e2ab3
         LogMessage(X_ERROR, "XKB: Couldn't compile keymap\n");
15e2ab3
         return 0;
15e2ab3
     }
15e2ab3
-    file = XkbDDXOpenConfigFile(nameRtrn, fileName, PATH_MAX);
15e2ab3
+    file = XkbDDXOpenConfigFile(ctx.keymap, fileName, PATH_MAX);
15e2ab3
     if (file == NULL) {
15e2ab3
         LogMessage(X_ERROR, "Couldn't open compiled keymap file %s\n",
15e2ab3
                    fileName);
15e2ab3
-- 
15e2ab3
1.8.3.1
15e2ab3