diff options
author | Lennart Poettering <lennart@poettering.net> | 2015-10-26 23:01:30 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2015-10-27 13:25:56 +0100 |
commit | 8752c5752f3b9023f9ce96a55d70c6e5fc31118f (patch) | |
tree | 9a14d641981c51f82962d9c9652d63b1293bb753 /src | |
parent | b8faf2ecd551d853c449c7cadc3944af84156bc9 (diff) |
util-lib: move more locale-related calls to locale-util.[ch]
Diffstat (limited to 'src')
-rw-r--r-- | src/analyze/analyze.c | 1 | ||||
-rw-r--r-- | src/basic/locale-util.c | 84 | ||||
-rw-r--r-- | src/basic/locale-util.h | 21 | ||||
-rw-r--r-- | src/basic/util.c | 77 | ||||
-rw-r--r-- | src/basic/util.h | 19 | ||||
-rw-r--r-- | src/boot/bootctl.c | 1 | ||||
-rw-r--r-- | src/bus-proxyd/bus-xml-policy.c | 1 | ||||
-rw-r--r-- | src/core/dbus-unit.c | 3 | ||||
-rw-r--r-- | src/delta/delta.c | 1 | ||||
-rw-r--r-- | src/journal/journalctl.c | 1 | ||||
-rw-r--r-- | src/libsystemd/sd-bus/bus-dump.c | 1 | ||||
-rw-r--r-- | src/libsystemd/sd-bus/busctl.c | 1 | ||||
-rw-r--r-- | src/login/sysfs-show.c | 1 | ||||
-rw-r--r-- | src/network/networkctl.c | 4 | ||||
-rw-r--r-- | src/resolve/resolved-dns-transaction.c | 1 | ||||
-rw-r--r-- | src/shared/cgroup-show.c | 1 | ||||
-rw-r--r-- | src/shared/pager.c | 1 | ||||
-rw-r--r-- | src/systemctl/systemctl.c | 1 | ||||
-rw-r--r-- | src/vconsole/vconsole-setup.c | 1 |
19 files changed, 122 insertions, 99 deletions
diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c index 6ba16d8b65..a165152cb2 100644 --- a/src/analyze/analyze.c +++ b/src/analyze/analyze.c @@ -31,6 +31,7 @@ #include "bus-error.h" #include "bus-util.h" #include "hashmap.h" +#include "locale-util.h" #include "log.h" #include "pager.h" #include "parse-util.h" diff --git a/src/basic/locale-util.c b/src/basic/locale-util.c index 9db906316b..b87fd7670b 100644 --- a/src/basic/locale-util.c +++ b/src/basic/locale-util.c @@ -19,6 +19,8 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>. ***/ +#include <langinfo.h> +#include <locale.h> #include <sys/mman.h> #include "dirent-util.h" @@ -208,6 +210,88 @@ bool locale_is_valid(const char *name) { return true; } +void init_gettext(void) { + setlocale(LC_ALL, ""); + textdomain(GETTEXT_PACKAGE); +} + +bool is_locale_utf8(void) { + const char *set; + static int cached_answer = -1; + + /* Note that we default to 'true' here, since today UTF8 is + * pretty much supported everywhere. */ + + if (cached_answer >= 0) + goto out; + + if (!setlocale(LC_ALL, "")) { + cached_answer = true; + goto out; + } + + set = nl_langinfo(CODESET); + if (!set) { + cached_answer = true; + goto out; + } + + if (streq(set, "UTF-8")) { + cached_answer = true; + goto out; + } + + /* For LC_CTYPE=="C" return true, because CTYPE is effectly + * unset and everything can do to UTF-8 nowadays. */ + set = setlocale(LC_CTYPE, NULL); + if (!set) { + cached_answer = true; + goto out; + } + + /* Check result, but ignore the result if C was set + * explicitly. */ + cached_answer = + STR_IN_SET(set, "C", "POSIX") && + !getenv("LC_ALL") && + !getenv("LC_CTYPE") && + !getenv("LANG"); + +out: + return (bool) cached_answer; +} + + +const char *draw_special_char(DrawSpecialChar ch) { + + static const char *draw_table[2][_DRAW_SPECIAL_CHAR_MAX] = { + + /* UTF-8 */ { + [DRAW_TREE_VERTICAL] = "\342\224\202 ", /* │ */ + [DRAW_TREE_BRANCH] = "\342\224\234\342\224\200", /* ├─ */ + [DRAW_TREE_RIGHT] = "\342\224\224\342\224\200", /* └─ */ + [DRAW_TREE_SPACE] = " ", /* */ + [DRAW_TRIANGULAR_BULLET] = "\342\200\243", /* ‣ */ + [DRAW_BLACK_CIRCLE] = "\342\227\217", /* ● */ + [DRAW_ARROW] = "\342\206\222", /* → */ + [DRAW_DASH] = "\342\200\223", /* – */ + }, + + /* ASCII fallback */ { + [DRAW_TREE_VERTICAL] = "| ", + [DRAW_TREE_BRANCH] = "|-", + [DRAW_TREE_RIGHT] = "`-", + [DRAW_TREE_SPACE] = " ", + [DRAW_TRIANGULAR_BULLET] = ">", + [DRAW_BLACK_CIRCLE] = "*", + [DRAW_ARROW] = "->", + [DRAW_DASH] = "-", + } + }; + + return draw_table[!is_locale_utf8()][ch]; +} + static const char * const locale_variable_table[_VARIABLE_LC_MAX] = { [VARIABLE_LANG] = "LANG", [VARIABLE_LANGUAGE] = "LANGUAGE", diff --git a/src/basic/locale-util.h b/src/basic/locale-util.h index e48aa3d9af..c71d145139 100644 --- a/src/basic/locale-util.h +++ b/src/basic/locale-util.h @@ -21,6 +21,7 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>. ***/ +#include <libintl.h> #include <stdbool.h> #include "macro.h" @@ -50,5 +51,25 @@ typedef enum LocaleVariable { int get_locales(char ***l); bool locale_is_valid(const char *name); +#define _(String) gettext(String) +#define N_(String) String +void init_gettext(void); + +bool is_locale_utf8(void); + +typedef enum DrawSpecialChar { + DRAW_TREE_VERTICAL, + DRAW_TREE_BRANCH, + DRAW_TREE_RIGHT, + DRAW_TREE_SPACE, + DRAW_TRIANGULAR_BULLET, + DRAW_BLACK_CIRCLE, + DRAW_ARROW, + DRAW_DASH, + _DRAW_SPECIAL_CHAR_MAX +} DrawSpecialChar; + +const char *draw_special_char(DrawSpecialChar ch); + const char* locale_variable_to_string(LocaleVariable i) _const_; LocaleVariable locale_variable_from_string(const char *s) _pure_; diff --git a/src/basic/util.c b/src/basic/util.c index 0878adbfac..b079e8af16 100644 --- a/src/basic/util.c +++ b/src/basic/util.c @@ -810,83 +810,6 @@ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size, return NULL; } -void init_gettext(void) { - setlocale(LC_ALL, ""); - textdomain(GETTEXT_PACKAGE); -} - -bool is_locale_utf8(void) { - const char *set; - static int cached_answer = -1; - - if (cached_answer >= 0) - goto out; - - if (!setlocale(LC_ALL, "")) { - cached_answer = true; - goto out; - } - - set = nl_langinfo(CODESET); - if (!set) { - cached_answer = true; - goto out; - } - - if (streq(set, "UTF-8")) { - cached_answer = true; - goto out; - } - - /* For LC_CTYPE=="C" return true, because CTYPE is effectly - * unset and everything can do to UTF-8 nowadays. */ - set = setlocale(LC_CTYPE, NULL); - if (!set) { - cached_answer = true; - goto out; - } - - /* Check result, but ignore the result if C was set - * explicitly. */ - cached_answer = - STR_IN_SET(set, "C", "POSIX") && - !getenv("LC_ALL") && - !getenv("LC_CTYPE") && - !getenv("LANG"); - -out: - return (bool) cached_answer; -} - -const char *draw_special_char(DrawSpecialChar ch) { - static const char *draw_table[2][_DRAW_SPECIAL_CHAR_MAX] = { - - /* UTF-8 */ { - [DRAW_TREE_VERTICAL] = "\342\224\202 ", /* │ */ - [DRAW_TREE_BRANCH] = "\342\224\234\342\224\200", /* ├─ */ - [DRAW_TREE_RIGHT] = "\342\224\224\342\224\200", /* └─ */ - [DRAW_TREE_SPACE] = " ", /* */ - [DRAW_TRIANGULAR_BULLET] = "\342\200\243", /* ‣ */ - [DRAW_BLACK_CIRCLE] = "\342\227\217", /* ● */ - [DRAW_ARROW] = "\342\206\222", /* → */ - [DRAW_DASH] = "\342\200\223", /* – */ - }, - - /* ASCII fallback */ { - [DRAW_TREE_VERTICAL] = "| ", - [DRAW_TREE_BRANCH] = "|-", - [DRAW_TREE_RIGHT] = "`-", - [DRAW_TREE_SPACE] = " ", - [DRAW_TRIANGULAR_BULLET] = ">", - [DRAW_BLACK_CIRCLE] = "*", - [DRAW_ARROW] = "->", - [DRAW_DASH] = "-", - } - }; - - return draw_table[!is_locale_utf8()][ch]; -} - int on_ac_power(void) { bool found_offline = false, found_online = false; _cleanup_closedir_ DIR *d = NULL; diff --git a/src/basic/util.h b/src/basic/util.h index d18b151d88..680317909d 100644 --- a/src/basic/util.h +++ b/src/basic/util.h @@ -192,25 +192,6 @@ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *, void *), void *arg); -#define _(String) gettext (String) -#define N_(String) String -void init_gettext(void); -bool is_locale_utf8(void); - -typedef enum DrawSpecialChar { - DRAW_TREE_VERTICAL, - DRAW_TREE_BRANCH, - DRAW_TREE_RIGHT, - DRAW_TREE_SPACE, - DRAW_TRIANGULAR_BULLET, - DRAW_BLACK_CIRCLE, - DRAW_ARROW, - DRAW_DASH, - _DRAW_SPECIAL_CHAR_MAX -} DrawSpecialChar; - -const char *draw_special_char(DrawSpecialChar ch); - int on_ac_power(void); static inline void *mempset(void *s, int c, size_t n) { diff --git a/src/boot/bootctl.c b/src/boot/bootctl.c index 7e06abd3bf..332bb33545 100644 --- a/src/boot/bootctl.c +++ b/src/boot/bootctl.c @@ -41,6 +41,7 @@ #include "efivars.h" #include "fd-util.h" #include "fileio.h" +#include "locale-util.h" #include "rm-rf.h" #include "string-util.h" #include "util.h" diff --git a/src/bus-proxyd/bus-xml-policy.c b/src/bus-proxyd/bus-xml-policy.c index cf922ef02f..1b9fdedca3 100644 --- a/src/bus-proxyd/bus-xml-policy.c +++ b/src/bus-proxyd/bus-xml-policy.c @@ -26,6 +26,7 @@ #include "conf-files.h" #include "fileio.h" #include "formats-util.h" +#include "locale-util.h" #include "set.h" #include "string-table.h" #include "string-util.h" diff --git a/src/core/dbus-unit.c b/src/core/dbus-unit.c index 7220fe688f..62086a7fde 100644 --- a/src/core/dbus-unit.c +++ b/src/core/dbus-unit.c @@ -23,13 +23,14 @@ #include "bus-common-errors.h" #include "cgroup-util.h" +#include "dbus-unit.h" #include "dbus.h" +#include "locale-util.h" #include "log.h" #include "selinux-access.h" #include "special.h" #include "string-util.h" #include "strv.h" -#include "dbus-unit.h" static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state, unit_load_state, UnitLoadState); static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode, job_mode, JobMode); diff --git a/src/delta/delta.c b/src/delta/delta.c index e5e0be476f..4ade23ffab 100644 --- a/src/delta/delta.c +++ b/src/delta/delta.c @@ -30,6 +30,7 @@ #include "fd-util.h" #include "fs-util.h" #include "hashmap.h" +#include "locale-util.h" #include "log.h" #include "pager.h" #include "parse-util.h" diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 61d502f0a1..8b8c50b436 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -54,6 +54,7 @@ #include "journal-qrcode.h" #include "journal-vacuum.h" #include "journal-verify.h" +#include "locale-util.h" #include "log.h" #include "logs-show.h" #include "mkdir.h" diff --git a/src/libsystemd/sd-bus/bus-dump.c b/src/libsystemd/sd-bus/bus-dump.c index 9ddd059072..02c9ff8f2f 100644 --- a/src/libsystemd/sd-bus/bus-dump.c +++ b/src/libsystemd/sd-bus/bus-dump.c @@ -27,6 +27,7 @@ #include "capability.h" #include "fileio.h" #include "formats-util.h" +#include "locale-util.h" #include "macro.h" #include "string-util.h" #include "strv.h" diff --git a/src/libsystemd/sd-bus/busctl.c b/src/libsystemd/sd-bus/busctl.c index 4b121e849a..4fa60bb185 100644 --- a/src/libsystemd/sd-bus/busctl.c +++ b/src/libsystemd/sd-bus/busctl.c @@ -31,6 +31,7 @@ #include "busctl-introspect.h" #include "escape.h" #include "fd-util.h" +#include "locale-util.h" #include "log.h" #include "pager.h" #include "parse-util.h" diff --git a/src/login/sysfs-show.c b/src/login/sysfs-show.c index 32e53c0a11..0f671ac90e 100644 --- a/src/login/sysfs-show.c +++ b/src/login/sysfs-show.c @@ -24,6 +24,7 @@ #include "libudev.h" +#include "locale-util.h" #include "path-util.h" #include "string-util.h" #include "sysfs-show.h" diff --git a/src/network/networkctl.c b/src/network/networkctl.c index 6ea9563fcf..d0d7669c87 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -34,12 +34,14 @@ #include "hwdb-util.h" #include "lldp.h" #include "local-addresses.h" +#include "locale-util.h" +#include "locale-util.h" #include "netlink-util.h" #include "pager.h" #include "parse-util.h" #include "socket-util.h" -#include "string-util.h" #include "string-table.h" +#include "string-util.h" #include "strv.h" #include "terminal-util.h" #include "util.h" diff --git a/src/resolve/resolved-dns-transaction.c b/src/resolve/resolved-dns-transaction.c index 1e38547e23..c60197cf8d 100644 --- a/src/resolve/resolved-dns-transaction.c +++ b/src/resolve/resolved-dns-transaction.c @@ -20,7 +20,6 @@ ***/ #include "af-list.h" - #include "dns-domain.h" #include "fd-util.h" #include "random-util.h" diff --git a/src/shared/cgroup-show.c b/src/shared/cgroup-show.c index 395e41930c..9e70713ce8 100644 --- a/src/shared/cgroup-show.c +++ b/src/shared/cgroup-show.c @@ -28,6 +28,7 @@ #include "cgroup-util.h" #include "fd-util.h" #include "formats-util.h" +#include "locale-util.h" #include "macro.h" #include "path-util.h" #include "process-util.h" diff --git a/src/shared/pager.c b/src/shared/pager.c index 7029d6b2e2..d149bc1722 100644 --- a/src/shared/pager.c +++ b/src/shared/pager.c @@ -27,6 +27,7 @@ #include "copy.h" #include "fd-util.h" +#include "locale-util.h" #include "macro.h" #include "pager.h" #include "process-util.h" diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 9156760027..48d17cb52a 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -57,6 +57,7 @@ #include "install.h" #include "io-util.h" #include "list.h" +#include "locale-util.h" #include "log.h" #include "logs-show.h" #include "macro.h" diff --git a/src/vconsole/vconsole-setup.c b/src/vconsole/vconsole-setup.c index 49523a0a67..2da28f8be0 100644 --- a/src/vconsole/vconsole-setup.c +++ b/src/vconsole/vconsole-setup.c @@ -34,6 +34,7 @@ #include "fd-util.h" #include "fileio.h" #include "io-util.h" +#include "locale-util.h" #include "log.h" #include "process-util.h" #include "signal-util.h" |