Commit 0066e387bcf5e77411ff5edb4896df69c3df72cd

Ran Benita 2020-07-25T15:49:17

tools: make independent from src/ Signed-off-by: Ran Benita <ran@unusedvar.com>

diff --git a/meson.build b/meson.build
index 4d2f8e8..d7be3d4 100644
--- a/meson.build
+++ b/meson.build
@@ -532,11 +532,10 @@ if build_tools
         'tools-internal',
         'tools/tools-common.h',
         'tools/tools-common.c',
-        include_directories: include_directories('src'),
         dependencies: libxkbcommon_dep,
     )
     tools_dep = declare_dependency(
-        include_directories: [include_directories('src'), include_directories('tools')],
+        include_directories: [include_directories('tools')],
         link_with: libxkbcommon_tools_internal,
     )
 
@@ -556,6 +555,7 @@ if build_tools
                libxkbcommon_sources,
                dependencies: [tools_dep],
                c_args: ['-DENABLE_PRIVATE_APIS'],
+               include_directories: [include_directories('src')],
                install: false)
     configh_data.set10('HAVE_XKBCLI_COMPILE_KEYMAP', true)
     executable('xkbcli-how-to-type',
diff --git a/tools/compile-keymap.c b/tools/compile-keymap.c
index 370a67c..6308449 100644
--- a/tools/compile-keymap.c
+++ b/tools/compile-keymap.c
@@ -31,9 +31,12 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "xkbcommon/xkbcommon.h"
+#if ENABLE_PRIVATE_APIS
 #include "xkbcomp/xkbcomp-priv.h"
 #include "xkbcomp/rules.h"
-#include "xkbcommon/xkbcommon.h"
+#endif
+#include "tools-common.h"
 
 #define DEFAULT_INCLUDE_PATH_PLACEHOLDER "__defaults__"
 
@@ -44,7 +47,8 @@ static enum output_format {
     FORMAT_KCCGST,
     FORMAT_KEYMAP_FROM_XKB,
 } output_format = FORMAT_KEYMAP;
-static darray(const char *) includes;
+static const char *includes[64];
+static size_t num_includes = 0;
 
 static void
 usage(char **argv)
@@ -152,10 +156,18 @@ parse_options(int argc, char **argv, struct xkb_rule_names *names)
             output_format = FORMAT_KEYMAP_FROM_XKB;
             break;
         case OPT_INCLUDE:
-            darray_append(includes, optarg);
+            if (num_includes >= ARRAY_SIZE(includes)) {
+                fprintf(stderr, "error: too many includes\n");
+                exit(EXIT_INVALID_USAGE);
+            }
+            includes[num_includes++] = optarg;
             break;
         case OPT_INCLUDE_DEFAULTS:
-            darray_append(includes, DEFAULT_INCLUDE_PATH_PLACEHOLDER);
+            if (num_includes >= ARRAY_SIZE(includes)) {
+                fprintf(stderr, "error: too many includes\n");
+                exit(EXIT_INVALID_USAGE);
+            }
+            includes[num_includes++] = DEFAULT_INCLUDE_PATH_PLACEHOLDER;
             break;
         case OPT_RULES:
             names->rules = optarg;
@@ -307,7 +319,6 @@ main(int argc, char **argv)
         .options = DEFAULT_XKB_OPTIONS,
     };
     int rc = 1;
-    const char **path;
 
     if (argc <= 1) {
         usage(argv);
@@ -318,8 +329,8 @@ main(int argc, char **argv)
         return EXIT_INVALID_USAGE;
 
     /* Now fill in the layout */
-    if (isempty(names.layout)) {
-        if (!isempty(names.variant)) {
+    if (!names.layout || !*names.layout) {
+        if (names.variant && *names.variant) {
             fprintf(stderr, "Error: a variant requires a layout\n");
             return EXIT_INVALID_USAGE;
         }
@@ -335,14 +346,15 @@ main(int argc, char **argv)
         xkb_context_set_log_verbosity(ctx, 10);
     }
 
-    if (darray_empty(includes))
-        darray_append(includes, DEFAULT_INCLUDE_PATH_PLACEHOLDER);
+    if (num_includes == 0)
+        includes[num_includes++] = DEFAULT_INCLUDE_PATH_PLACEHOLDER;
 
-    darray_foreach(path, includes) {
-        if (streq(*path, DEFAULT_INCLUDE_PATH_PLACEHOLDER))
+    for (size_t i = 0; i < num_includes; i++) {
+        const char *include = includes[i];
+        if (strcmp(include, DEFAULT_INCLUDE_PATH_PLACEHOLDER))
             xkb_context_include_path_append_default(ctx);
         else
-            xkb_context_include_path_append(ctx, *path);
+            xkb_context_include_path_append(ctx, include);
     }
 
     if (output_format == FORMAT_RMLVO) {
diff --git a/tools/tools-common.c b/tools/tools-common.c
index db17880..0c1ffb9 100644
--- a/tools/tools-common.c
+++ b/tools/tools-common.c
@@ -32,6 +32,7 @@
 
 #include "config.h"
 
+#include <errno.h>
 #include <limits.h>
 #include <fcntl.h>
 #include <stdlib.h>
@@ -49,7 +50,6 @@
 #include <termios.h>
 #endif
 
-#include "utils.h"
 #include "tools-common.h"
 
 void
@@ -212,6 +212,7 @@ tools_exec_command(const char *prefix, int real_argc, char **real_argv)
     char *argv[64] = {NULL};
     char executable[PATH_MAX];
     const char *command;
+    int rc;
 
     if (((size_t)real_argc >= ARRAY_SIZE(argv))) {
         fprintf(stderr, "Too many arguments\n");
@@ -220,8 +221,9 @@ tools_exec_command(const char *prefix, int real_argc, char **real_argv)
 
     command = real_argv[0];
 
-    if (!snprintf_safe(executable, sizeof(executable),
-                       "%s/%s-%s", LIBXKBCOMMON_TOOL_PATH, prefix, command)) {
+    rc = snprintf(executable, sizeof(executable),
+                  "%s/%s-%s", LIBXKBCOMMON_TOOL_PATH, prefix, command);
+    if (rc < 0 || (size_t) rc >= sizeof(executable)) {
         fprintf(stderr, "Failed to assemble command\n");
         return EXIT_FAILURE;
     }
diff --git a/tools/tools-common.h b/tools/tools-common.h
index bc6fa38..780720a 100644
--- a/tools/tools-common.h
+++ b/tools/tools-common.h
@@ -34,6 +34,8 @@
 #include "xkbcommon/xkbcommon.h"
 #include "xkbcommon/xkbcommon-compose.h"
 
+#define ARRAY_SIZE(arr) ((sizeof(arr) / sizeof(*(arr))))
+
 void
 tools_print_keycode_state(struct xkb_state *state,
                           struct xkb_compose_state *compose_state,