summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2019-03-14 12:40:29 +0100
committerLennart Poettering <lennart@poettering.net>2019-03-14 13:25:51 +0100
commitf05e1d0d9f21dd057bdf0f31d9bc3de509791eaa (patch)
tree97d9ca47bd370ab284d5a471af90de7a9353a5f3
parentecd5f1a9e060746258e8f93e8ba6b331e49e92f3 (diff)
downloadsystemd-f05e1d0d9f21dd057bdf0f31d9bc3de509791eaa.tar.gz
util: split out kbd related stuff
This stuff is neither generic enough to be in def.h, nor really has much to do with locale, hence give it its own .c/.h file pair.
-rw-r--r--src/basic/def.h13
-rw-r--r--src/basic/kbd-util.c108
-rw-r--r--src/basic/kbd-util.h20
-rw-r--r--src/basic/locale-util.c93
-rw-r--r--src/basic/locale-util.h3
-rw-r--r--src/basic/meson.build2
-rw-r--r--src/firstboot/firstboot.c1
-rw-r--r--src/locale/keymap-util.c4
-rw-r--r--src/locale/localectl.c2
-rw-r--r--src/test/test-locale-util.c1
10 files changed, 135 insertions, 112 deletions
diff --git a/src/basic/def.h b/src/basic/def.h
index 48344f7a8e..196778fcf9 100644
--- a/src/basic/def.h
+++ b/src/basic/def.h
@@ -21,19 +21,6 @@
#define SIGNALS_CRASH_HANDLER SIGSEGV,SIGILL,SIGFPE,SIGBUS,SIGQUIT,SIGABRT
#define SIGNALS_IGNORE SIGPIPE
-#if HAVE_SPLIT_USR
-#define KBD_KEYMAP_DIRS \
- "/usr/share/keymaps/\0" \
- "/usr/share/kbd/keymaps/\0" \
- "/usr/lib/kbd/keymaps/\0" \
- "/lib/kbd/keymaps/\0"
-#else
-#define KBD_KEYMAP_DIRS \
- "/usr/share/keymaps/\0" \
- "/usr/share/kbd/keymaps/\0" \
- "/usr/lib/kbd/keymaps/\0"
-#endif
-
/* Note that we use the new /run prefix here (instead of /var/run) since we require them to be aliases and that way we
* become independent of /var being mounted */
#define DEFAULT_SYSTEM_BUS_ADDRESS "unix:path=/run/dbus/system_bus_socket"
diff --git a/src/basic/kbd-util.c b/src/basic/kbd-util.c
new file mode 100644
index 0000000000..f178b5dfea
--- /dev/null
+++ b/src/basic/kbd-util.c
@@ -0,0 +1,108 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+
+#include <ftw.h>
+
+#include "kbd-util.h"
+#include "log.h"
+#include "path-util.h"
+#include "set.h"
+#include "string-util.h"
+#include "strv.h"
+#include "utf8.h"
+
+static thread_local Set *keymaps = NULL;
+
+static int nftw_cb(
+ const char *fpath,
+ const struct stat *sb,
+ int tflag,
+ struct FTW *ftwbuf) {
+
+ _cleanup_free_ char *p = NULL;
+ char *e;
+ int r;
+
+ if (tflag != FTW_F)
+ return 0;
+
+ if (!endswith(fpath, ".map") &&
+ !endswith(fpath, ".map.gz"))
+ return 0;
+
+ p = strdup(basename(fpath));
+ if (!p)
+ return FTW_STOP;
+
+ e = endswith(p, ".map");
+ if (e)
+ *e = 0;
+
+ e = endswith(p, ".map.gz");
+ if (e)
+ *e = 0;
+
+ if (!keymap_is_valid(p))
+ return 0;
+
+ r = set_consume(keymaps, TAKE_PTR(p));
+ if (r < 0 && r != -EEXIST)
+ return r;
+
+ return 0;
+}
+
+int get_keymaps(char ***ret) {
+ _cleanup_strv_free_ char **l = NULL;
+ const char *dir;
+ int r;
+
+ keymaps = set_new(&string_hash_ops);
+ if (!keymaps)
+ return -ENOMEM;
+
+ NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS) {
+ r = nftw(dir, nftw_cb, 20, FTW_PHYS|FTW_ACTIONRETVAL);
+
+ if (r == FTW_STOP)
+ log_debug("Directory not found %s", dir);
+ else if (r < 0)
+ log_debug_errno(r, "Can't add keymap: %m");
+ }
+
+ l = set_get_strv(keymaps);
+ if (!l) {
+ set_free_free(keymaps);
+ return -ENOMEM;
+ }
+
+ set_free(keymaps);
+
+ if (strv_isempty(l))
+ return -ENOENT;
+
+ strv_sort(l);
+
+ *ret = TAKE_PTR(l);
+
+ return 0;
+}
+
+bool keymap_is_valid(const char *name) {
+
+ if (isempty(name))
+ return false;
+
+ if (strlen(name) >= 128)
+ return false;
+
+ if (!utf8_is_valid(name))
+ return false;
+
+ if (!filename_is_valid(name))
+ return false;
+
+ if (!string_is_safe(name))
+ return false;
+
+ return true;
+}
diff --git a/src/basic/kbd-util.h b/src/basic/kbd-util.h
new file mode 100644
index 0000000000..9efd2c7052
--- /dev/null
+++ b/src/basic/kbd-util.h
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+#pragma once
+
+#include <stdbool.h>
+
+#if HAVE_SPLIT_USR
+#define KBD_KEYMAP_DIRS \
+ "/usr/share/keymaps/\0" \
+ "/usr/share/kbd/keymaps/\0" \
+ "/usr/lib/kbd/keymaps/\0" \
+ "/lib/kbd/keymaps/\0"
+#else
+#define KBD_KEYMAP_DIRS \
+ "/usr/share/keymaps/\0" \
+ "/usr/share/kbd/keymaps/\0" \
+ "/usr/lib/kbd/keymaps/\0"
+#endif
+
+int get_keymaps(char ***l);
+bool keymap_is_valid(const char *name);
diff --git a/src/basic/locale-util.c b/src/basic/locale-util.c
index fc1577a83f..6f41f50641 100644
--- a/src/basic/locale-util.c
+++ b/src/basic/locale-util.c
@@ -255,99 +255,6 @@ out:
return (bool) cached_answer;
}
-static thread_local Set *keymaps = NULL;
-
-static int nftw_cb(
- const char *fpath,
- const struct stat *sb,
- int tflag,
- struct FTW *ftwbuf) {
-
- char *p, *e;
- int r;
-
- if (tflag != FTW_F)
- return 0;
-
- if (!endswith(fpath, ".map") &&
- !endswith(fpath, ".map.gz"))
- return 0;
-
- p = strdup(basename(fpath));
- if (!p)
- return FTW_STOP;
-
- e = endswith(p, ".map");
- if (e)
- *e = 0;
-
- e = endswith(p, ".map.gz");
- if (e)
- *e = 0;
-
- r = set_consume(keymaps, p);
- if (r < 0 && r != -EEXIST)
- return r;
-
- return 0;
-}
-
-int get_keymaps(char ***ret) {
- _cleanup_strv_free_ char **l = NULL;
- const char *dir;
- int r;
-
- keymaps = set_new(&string_hash_ops);
- if (!keymaps)
- return -ENOMEM;
-
- NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS) {
- r = nftw(dir, nftw_cb, 20, FTW_PHYS|FTW_ACTIONRETVAL);
-
- if (r == FTW_STOP)
- log_debug("Directory not found %s", dir);
- else if (r < 0)
- log_debug_errno(r, "Can't add keymap: %m");
- }
-
- l = set_get_strv(keymaps);
- if (!l) {
- set_free_free(keymaps);
- return -ENOMEM;
- }
-
- set_free(keymaps);
-
- if (strv_isempty(l))
- return -ENOENT;
-
- strv_sort(l);
-
- *ret = TAKE_PTR(l);
-
- return 0;
-}
-
-bool keymap_is_valid(const char *name) {
-
- if (isempty(name))
- return false;
-
- if (strlen(name) >= 128)
- return false;
-
- if (!utf8_is_valid(name))
- return false;
-
- if (!filename_is_valid(name))
- return false;
-
- if (!string_is_safe(name))
- return false;
-
- return true;
-}
-
static bool emoji_enabled(void) {
static int cached_emoji_enabled = -1;
diff --git a/src/basic/locale-util.h b/src/basic/locale-util.h
index e64f0ce15c..78abbafd8f 100644
--- a/src/basic/locale-util.h
+++ b/src/basic/locale-util.h
@@ -68,9 +68,6 @@ const char *special_glyph(SpecialGlyph code) _const_;
const char* locale_variable_to_string(LocaleVariable i) _const_;
LocaleVariable locale_variable_from_string(const char *s) _pure_;
-int get_keymaps(char ***l);
-bool keymap_is_valid(const char *name);
-
static inline void freelocalep(locale_t *p) {
if (*p == (locale_t) 0)
return;
diff --git a/src/basic/meson.build b/src/basic/meson.build
index 1f7ef8683a..82b245c90b 100644
--- a/src/basic/meson.build
+++ b/src/basic/meson.build
@@ -76,6 +76,8 @@ basic_sources = files('''
io-util.c
io-util.h
ioprio.h
+ kbd-util.c
+ kbd-util.h
khash.c
khash.h
label.c
diff --git a/src/firstboot/firstboot.c b/src/firstboot/firstboot.c
index dde11576ea..6bbdafa81a 100644
--- a/src/firstboot/firstboot.c
+++ b/src/firstboot/firstboot.c
@@ -27,6 +27,7 @@
#include "fileio.h"
#include "fs-util.h"
#include "hostname-util.h"
+#include "kbd-util.h"
#include "locale-util.h"
#include "main-func.h"
#include "mkdir.h"
diff --git a/src/locale/keymap-util.c b/src/locale/keymap-util.c
index 6b6b32a591..64d18d916e 100644
--- a/src/locale/keymap-util.c
+++ b/src/locale/keymap-util.c
@@ -6,13 +6,13 @@
#include <unistd.h>
#include "bus-util.h"
-#include "def.h"
-#include "env-file.h"
#include "env-file-label.h"
+#include "env-file.h"
#include "env-util.h"
#include "fd-util.h"
#include "fileio-label.h"
#include "fileio.h"
+#include "kbd-util.h"
#include "keymap-util.h"
#include "locale-util.h"
#include "macro.h"
diff --git a/src/locale/localectl.c b/src/locale/localectl.c
index 683b127ba1..c8b195d9a6 100644
--- a/src/locale/localectl.c
+++ b/src/locale/localectl.c
@@ -11,9 +11,9 @@
#include "bus-error.h"
#include "bus-util.h"
-#include "def.h"
#include "fd-util.h"
#include "fileio.h"
+#include "kbd-util.h"
#include "locale-util.h"
#include "main-func.h"
#include "memory-util.h"
diff --git a/src/test/test-locale-util.c b/src/test/test-locale-util.c
index c6f8c1fb4f..f1d044082b 100644
--- a/src/test/test-locale-util.c
+++ b/src/test/test-locale-util.c
@@ -1,5 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
+#include "kbd-util.h"
#include "locale-util.h"
#include "macro.h"
#include "strv.h"