chiark / gitweb /
util : fallback to plain ASCII drawing if locale is not UTF-8
authorMichal Schmidt <mschmidt@redhat.com>
Fri, 2 Nov 2012 16:35:30 +0000 (17:35 +0100)
committerMichal Schmidt <mschmidt@redhat.com>
Fri, 2 Nov 2012 16:39:52 +0000 (17:39 +0100)
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

src/login/sysfs-show.c
src/shared/cgroup-show.c
src/shared/util.c
src/shared/util.h

index bc1bbccff71593ebab87008f43f229aa035d3d1f..172c75d82b0fc4c3a5215eadfd6be2e5a720a1cf 100644 (file)
@@ -105,7 +105,8 @@ static int show_sysfs_one(
                 }
 
                 k = ellipsize(sysfs, n_columns, 20);
                 }
 
                 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,
                 free(k);
 
                 if (asprintf(&l,
@@ -117,7 +118,8 @@ static int show_sysfs_one(
                 }
 
                 k = ellipsize(l, n_columns, 70);
                 }
 
                 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);
 
                 free(k);
                 free(l);
 
@@ -125,7 +127,7 @@ static int show_sysfs_one(
                 if (*item) {
                         char *p;
 
                 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);
                 }
                         show_sysfs_one(udev, seat, item, sysfs, p ? p : prefix, n_columns - 2);
                         free(p);
                 }
index 2ffed8b739acc2d36cd44a655e14642e4d1281dd..69fe7fc96db4ceba6a5860d655557b443896a33c 100644 (file)
@@ -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,
 
                 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));
                        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) {
                 }
 
                 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) {
 
                         if (!p1) {
-                                p1 = strappend(prefix, "\342\224\202 ");
+                                p1 = strjoin(prefix, draw_special_char(DRAW_BOX_VERT), " ", NULL);
                                 if (!p1) {
                                         free(k);
                                         r = -ENOMEM;
                                 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) {
                 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, "  ");
 
                 if (!p2) {
                         p2 = strappend(prefix, "  ");
index 3ac67505f0b2aaf0d99749658fc3c127cf49f07d..2a8afae0ecebb122d9ab07feaa80f330252b9dbe 100644 (file)
@@ -6140,3 +6140,22 @@ bool is_locale_utf8(void) {
 out:
         return (bool)cached_answer;
 }
 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];
+}
index b979b0e89fe9c0208955aa3cfc24f323d6f9935d..e387b1268ad2975a8e967ea1bba99b885e6dfc67 100644 (file)
@@ -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);
                  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);