[systemd-commits] 2 commits - src/journal src/login src/shared src/vconsole

Michal Schmidt michich at kemper.freedesktop.org
Fri Nov 2 09:41:50 PDT 2012


 src/journal/journalctl.c      |    6 -----
 src/login/sysfs-show.c        |    8 ++++---
 src/shared/cgroup-show.c      |   11 ++++++----
 src/shared/util.c             |   44 ++++++++++++++++++++++++++++++++++++++++++
 src/shared/util.h             |   11 ++++++++++
 src/vconsole/vconsole-setup.c |   15 --------------
 6 files changed, 68 insertions(+), 27 deletions(-)

New commits:
commit c339d9775d1df19fdbbafc57486f7cd51af6b7fb
Author: Michal Schmidt <mschmidt at redhat.com>
Date:   Fri Nov 2 17:35:30 2012 +0100

    util : fallback to plain ASCII drawing if locale is not UTF-8
    
    When printing cgroup and sysfs hierarchies, avoid using UTF-8 box drawing
    characters if the locale is not UTF-8.
    
    https://bugzilla.redhat.com/show_bug.cgi?id=871153

diff --git a/src/login/sysfs-show.c b/src/login/sysfs-show.c
index bc1bbcc..172c75d 100644
--- a/src/login/sysfs-show.c
+++ b/src/login/sysfs-show.c
@@ -105,7 +105,8 @@ static int show_sysfs_one(
                 }
 
                 k = ellipsize(sysfs, n_columns, 20);
-                printf("%s%s %s\n", prefix, lookahead ? "\342\224\234" : "\342\224\224", k ? k : sysfs);
+                printf("%s%s %s\n", prefix, draw_special_char(lookahead ? DRAW_BOX_VERT_AND_RIGHT : DRAW_BOX_UP_AND_RIGHT),
+                                    k ? k : sysfs);
                 free(k);
 
                 if (asprintf(&l,
@@ -117,7 +118,8 @@ static int show_sysfs_one(
                 }
 
                 k = ellipsize(l, n_columns, 70);
-                printf("%s%s %s\n", prefix, lookahead ? "\342\224\202" : " ", k ? k : l);
+                printf("%s%s %s\n", prefix, lookahead ? draw_special_char(DRAW_BOX_VERT) : " ",
+                                    k ? k : l);
                 free(k);
                 free(l);
 
@@ -125,7 +127,7 @@ static int show_sysfs_one(
                 if (*item) {
                         char *p;
 
-                        p = strappend(prefix, lookahead ? "\342\224\202 " : "  ");
+                        p = strjoin(prefix, lookahead ? draw_special_char(DRAW_BOX_VERT) : " ", " ", NULL);
                         show_sysfs_one(udev, seat, item, sysfs, p ? p : prefix, n_columns - 2);
                         free(p);
                 }
diff --git a/src/shared/cgroup-show.c b/src/shared/cgroup-show.c
index 2ffed8b..69fe7fc 100644
--- a/src/shared/cgroup-show.c
+++ b/src/shared/cgroup-show.c
@@ -88,7 +88,8 @@ static void show_pid_array(int pids[], unsigned n_pids, const char *prefix, unsi
 
                 printf("%s%s %*lu %s\n",
                        prefix,
-                       extra ? "\342\200\243" : ((more || i < n_pids-1) ? "\342\224\234" : "\342\224\224"),
+                       draw_special_char(extra ? DRAW_TRIANGULAR_BULLET :
+                                         ((more || i < n_pids-1) ? DRAW_BOX_VERT_AND_RIGHT : DRAW_BOX_UP_AND_RIGHT)),
                        pid_width,
                        (unsigned long) pids[i],
                        strna(t));
@@ -207,10 +208,11 @@ int show_cgroup_by_path(const char *path, const char *prefix, unsigned n_columns
                 }
 
                 if (last) {
-                        printf("%s\342\224\234 %s\n", prefix, path_get_file_name(last));
+                        printf("%s%s %s\n", prefix, draw_special_char(DRAW_BOX_VERT_AND_RIGHT),
+                                            path_get_file_name(last));
 
                         if (!p1) {
-                                p1 = strappend(prefix, "\342\224\202 ");
+                                p1 = strjoin(prefix, draw_special_char(DRAW_BOX_VERT), " ", NULL);
                                 if (!p1) {
                                         free(k);
                                         r = -ENOMEM;
@@ -232,7 +234,8 @@ int show_cgroup_by_path(const char *path, const char *prefix, unsigned n_columns
                 show_cgroup_one_by_path(path, prefix, n_columns, !!last, kernel_threads);
 
         if (last) {
-                printf("%s\342\224\224 %s\n", prefix, path_get_file_name(last));
+                printf("%s%s %s\n", prefix, draw_special_char(DRAW_BOX_UP_AND_RIGHT),
+                                    path_get_file_name(last));
 
                 if (!p2) {
                         p2 = strappend(prefix, "  ");
diff --git a/src/shared/util.c b/src/shared/util.c
index 3ac6750..2a8afae 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -6140,3 +6140,22 @@ bool is_locale_utf8(void) {
 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_BOX_VERT]           = "\342\224\202", /* │ */
+                        [DRAW_BOX_VERT_AND_RIGHT] = "\342\224\234", /* ├ */
+                        [DRAW_BOX_UP_AND_RIGHT]   = "\342\224\224", /* â”” */
+                        [DRAW_TRIANGULAR_BULLET]  = "\342\200\243", /* ‣ */
+                },
+                /* ASCII fallback */ {
+                        [DRAW_BOX_VERT] = "|",
+                        [DRAW_BOX_VERT_AND_RIGHT] = "+",
+                        [DRAW_BOX_UP_AND_RIGHT]   = "\\",
+                        [DRAW_TRIANGULAR_BULLET]  = ">",
+                }
+        };
+
+        return draw_table[!is_locale_utf8()][ch];
+}
diff --git a/src/shared/util.h b/src/shared/util.h
index b979b0e..e387b12 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -600,3 +600,12 @@ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
                  void *arg);
 
 bool is_locale_utf8(void);
+
+typedef enum DrawSpecialChar {
+        DRAW_BOX_VERT,
+        DRAW_BOX_VERT_AND_RIGHT,
+        DRAW_BOX_UP_AND_RIGHT,
+        DRAW_TRIANGULAR_BULLET,
+        _DRAW_SPECIAL_CHAR_MAX
+} DrawSpecialChar;
+const char *draw_special_char(DrawSpecialChar ch);

commit 0901758558506273c0b7553dc3cae587f2b94290
Author: Michal Schmidt <mschmidt at redhat.com>
Date:   Fri Nov 2 17:27:15 2012 +0100

    util: add is_locale_utf8()
    
    journalctl and vconsole-setup both implement utf8 locale detection.
    Let's have a common function for it.
    The next patch will add another use.

diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index 0f20448..d1407ab 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -33,8 +33,6 @@
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <linux/fs.h>
-#include <locale.h>
-#include <langinfo.h>
 
 #include <systemd/sd-journal.h>
 
@@ -719,9 +717,7 @@ static int setup_keys(void) {
 
 #ifdef HAVE_QRENCODE
                 /* If this is not an UTF-8 system don't print any QR codes */
-                setlocale(LC_CTYPE, "");
-
-                if (streq_ptr(nl_langinfo(CODESET), "UTF-8")) {
+                if (is_locale_utf8()) {
                         fputs("\nTo transfer the verification key to your phone please scan the QR code below:\n\n", stderr);
                         print_qr_code(stderr, seed, seed_size, n, arg_interval, hn, machine);
                 }
diff --git a/src/shared/util.c b/src/shared/util.c
index 402b7ca..3ac6750 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -57,6 +57,8 @@
 #include <sys/vfs.h>
 #include <linux/magic.h>
 #include <limits.h>
+#include <langinfo.h>
+#include <locale.h>
 
 #include "macro.h"
 #include "util.h"
@@ -6115,3 +6117,26 @@ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
         }
         return NULL;
 }
+
+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;
+        }
+
+        cached_answer = streq(set, "UTF-8");
+out:
+        return (bool)cached_answer;
+}
diff --git a/src/shared/util.h b/src/shared/util.h
index 284035c..b979b0e 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -598,3 +598,5 @@ int parse_timestamp(const char *t, usec_t *usec);
 void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
                  int (*compar) (const void *, const void *, void *),
                  void *arg);
+
+bool is_locale_utf8(void);
diff --git a/src/vconsole/vconsole-setup.c b/src/vconsole/vconsole-setup.c
index 6e016ad..cb7ade0 100644
--- a/src/vconsole/vconsole-setup.c
+++ b/src/vconsole/vconsole-setup.c
@@ -29,8 +29,6 @@
 #include <stdbool.h>
 #include <stdarg.h>
 #include <limits.h>
-#include <locale.h>
-#include <langinfo.h>
 #include <sys/ioctl.h>
 #include <sys/wait.h>
 #include <linux/tiocl.h>
@@ -48,19 +46,6 @@ static bool is_vconsole(int fd) {
         return ioctl(fd, TIOCLINUX, data) >= 0;
 }
 
-static bool is_locale_utf8(void) {
-        const char *set;
-
-        if (!setlocale(LC_ALL, ""))
-                return true;
-
-        set = nl_langinfo(CODESET);
-        if (!set)
-                return true;
-
-        return streq(set, "UTF-8");
-}
-
 static int disable_utf8(int fd) {
         int r = 0, k;
 



More information about the systemd-commits mailing list