summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichal Schmidt <mschmidt@redhat.com>2012-11-02 17:35:30 +0100
committerMichal Schmidt <mschmidt@redhat.com>2012-11-02 17:39:52 +0100
commitc339d9775d1df19fdbbafc57486f7cd51af6b7fb (patch)
treee0689a930c57e861641308340165d9f7431ab24e
parent0901758558506273c0b7553dc3cae587f2b94290 (diff)
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
-rw-r--r--src/login/sysfs-show.c8
-rw-r--r--src/shared/cgroup-show.c11
-rw-r--r--src/shared/util.c19
-rw-r--r--src/shared/util.h9
4 files changed, 40 insertions, 7 deletions
diff --git a/src/login/sysfs-show.c b/src/login/sysfs-show.c
index bc1bbccff..172c75d82 100644
--- a/src/login/sysfs-show.c
+++ b/src/login/sysfs-show.c
@@ -105,7 +105,8 @@ static int show_sysfs_one(
105 } 105 }
106 106
107 k = ellipsize(sysfs, n_columns, 20); 107 k = ellipsize(sysfs, n_columns, 20);
108 printf("%s%s %s\n", prefix, lookahead ? "\342\224\234" : "\342\224\224", k ? k : sysfs); 108 printf("%s%s %s\n", prefix, draw_special_char(lookahead ? DRAW_BOX_VERT_AND_RIGHT : DRAW_BOX_UP_AND_RIGHT),
109 k ? k : sysfs);
109 free(k); 110 free(k);
110 111
111 if (asprintf(&l, 112 if (asprintf(&l,
@@ -117,7 +118,8 @@ static int show_sysfs_one(
117 } 118 }
118 119
119 k = ellipsize(l, n_columns, 70); 120 k = ellipsize(l, n_columns, 70);
120 printf("%s%s %s\n", prefix, lookahead ? "\342\224\202" : " ", k ? k : l); 121 printf("%s%s %s\n", prefix, lookahead ? draw_special_char(DRAW_BOX_VERT) : " ",
122 k ? k : l);
121 free(k); 123 free(k);
122 free(l); 124 free(l);
123 125
@@ -125,7 +127,7 @@ static int show_sysfs_one(
125 if (*item) { 127 if (*item) {
126 char *p; 128 char *p;
127 129
128 p = strappend(prefix, lookahead ? "\342\224\202 " : " "); 130 p = strjoin(prefix, lookahead ? draw_special_char(DRAW_BOX_VERT) : " ", " ", NULL);
129 show_sysfs_one(udev, seat, item, sysfs, p ? p : prefix, n_columns - 2); 131 show_sysfs_one(udev, seat, item, sysfs, p ? p : prefix, n_columns - 2);
130 free(p); 132 free(p);
131 } 133 }
diff --git a/src/shared/cgroup-show.c b/src/shared/cgroup-show.c
index 2ffed8b73..69fe7fc96 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
88 88
89 printf("%s%s %*lu %s\n", 89 printf("%s%s %*lu %s\n",
90 prefix, 90 prefix,
91 extra ? "\342\200\243" : ((more || i < n_pids-1) ? "\342\224\234" : "\342\224\224"), 91 draw_special_char(extra ? DRAW_TRIANGULAR_BULLET :
92 ((more || i < n_pids-1) ? DRAW_BOX_VERT_AND_RIGHT : DRAW_BOX_UP_AND_RIGHT)),
92 pid_width, 93 pid_width,
93 (unsigned long) pids[i], 94 (unsigned long) pids[i],
94 strna(t)); 95 strna(t));
@@ -207,10 +208,11 @@ int show_cgroup_by_path(const char *path, const char *prefix, unsigned n_columns
207 } 208 }
208 209
209 if (last) { 210 if (last) {
210 printf("%s\342\224\234 %s\n", prefix, path_get_file_name(last)); 211 printf("%s%s %s\n", prefix, draw_special_char(DRAW_BOX_VERT_AND_RIGHT),
212 path_get_file_name(last));
211 213
212 if (!p1) { 214 if (!p1) {
213 p1 = strappend(prefix, "\342\224\202 "); 215 p1 = strjoin(prefix, draw_special_char(DRAW_BOX_VERT), " ", NULL);
214 if (!p1) { 216 if (!p1) {
215 free(k); 217 free(k);
216 r = -ENOMEM; 218 r = -ENOMEM;
@@ -232,7 +234,8 @@ int show_cgroup_by_path(const char *path, const char *prefix, unsigned n_columns
232 show_cgroup_one_by_path(path, prefix, n_columns, !!last, kernel_threads); 234 show_cgroup_one_by_path(path, prefix, n_columns, !!last, kernel_threads);
233 235
234 if (last) { 236 if (last) {
235 printf("%s\342\224\224 %s\n", prefix, path_get_file_name(last)); 237 printf("%s%s %s\n", prefix, draw_special_char(DRAW_BOX_UP_AND_RIGHT),
238 path_get_file_name(last));
236 239
237 if (!p2) { 240 if (!p2) {
238 p2 = strappend(prefix, " "); 241 p2 = strappend(prefix, " ");
diff --git a/src/shared/util.c b/src/shared/util.c
index 3ac67505f..2a8afae0e 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -6140,3 +6140,22 @@ bool is_locale_utf8(void) {
6140out: 6140out:
6141 return (bool)cached_answer; 6141 return (bool)cached_answer;
6142} 6142}
6143
6144const char *draw_special_char(DrawSpecialChar ch) {
6145 static const char *draw_table[2][_DRAW_SPECIAL_CHAR_MAX] = {
6146 /* UTF-8 */ {
6147 [DRAW_BOX_VERT] = "\342\224\202", /* │ */
6148 [DRAW_BOX_VERT_AND_RIGHT] = "\342\224\234", /* ├ */
6149 [DRAW_BOX_UP_AND_RIGHT] = "\342\224\224", /* └ */
6150 [DRAW_TRIANGULAR_BULLET] = "\342\200\243", /* ‣ */
6151 },
6152 /* ASCII fallback */ {
6153 [DRAW_BOX_VERT] = "|",
6154 [DRAW_BOX_VERT_AND_RIGHT] = "+",
6155 [DRAW_BOX_UP_AND_RIGHT] = "\\",
6156 [DRAW_TRIANGULAR_BULLET] = ">",
6157 }
6158 };
6159
6160 return draw_table[!is_locale_utf8()][ch];
6161}
diff --git a/src/shared/util.h b/src/shared/util.h
index b979b0e89..e387b1268 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,
600 void *arg); 600 void *arg);
601 601
602bool is_locale_utf8(void); 602bool is_locale_utf8(void);
603
604typedef enum DrawSpecialChar {
605 DRAW_BOX_VERT,
606 DRAW_BOX_VERT_AND_RIGHT,
607 DRAW_BOX_UP_AND_RIGHT,
608 DRAW_TRIANGULAR_BULLET,
609 _DRAW_SPECIAL_CHAR_MAX
610} DrawSpecialChar;
611const char *draw_special_char(DrawSpecialChar ch);