diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-01-04 20:49:58 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-01-04 20:49:58 +0100 |
commit | 28055028a709020ba7eb44f9e5037d0a952b51d6 (patch) | |
tree | bb5dc7052f04e66ad74bbbcc9917d638cd603b24 /libbb | |
parent | d2b1ba6fdee59645763e915ade3ec55e67d5839a (diff) | |
download | busybox-w32-28055028a709020ba7eb44f9e5037d0a952b51d6.tar.gz busybox-w32-28055028a709020ba7eb44f9e5037d0a952b51d6.tar.bz2 busybox-w32-28055028a709020ba7eb44f9e5037d0a952b51d6.zip |
fold: unicode support. Based on a patch by Tomas Heinrich <heinrich.tomas@gmail.com>
General Unicode support is tweaked to expose unicode_status.
function old new delta
init_unicode - 77 +77
write2stdout - 19 +19
adjust_column 68 71 +3
unicode_status - 1 +1
unicode_is_enabled 1 - -1
grep_main 780 773 -7
fold_main 619 552 -67
check_unicode_in_env 77 - -77
------------------------------------------------------------------------------
(add/remove: 3/2 grow/shrink: 1/2 up/down: 100/-152) Total: -52 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/lineedit.c | 2 | ||||
-rw-r--r-- | libbb/progress.c | 2 | ||||
-rw-r--r-- | libbb/unicode.c | 46 |
3 files changed, 29 insertions, 21 deletions
diff --git a/libbb/lineedit.c b/libbb/lineedit.c index c73e6b712..b73f1d6da 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c | |||
@@ -1763,7 +1763,7 @@ int FAST_FUNC read_line_input(const char *prompt, char *command, int maxsize, li | |||
1763 | return len; | 1763 | return len; |
1764 | } | 1764 | } |
1765 | 1765 | ||
1766 | check_unicode_in_env(); | 1766 | init_unicode(); |
1767 | 1767 | ||
1768 | // FIXME: audit & improve this | 1768 | // FIXME: audit & improve this |
1769 | if (maxsize > MAX_LINELEN) | 1769 | if (maxsize > MAX_LINELEN) |
diff --git a/libbb/progress.c b/libbb/progress.c index f6f26922c..3a245ae6c 100644 --- a/libbb/progress.c +++ b/libbb/progress.c | |||
@@ -78,7 +78,7 @@ void FAST_FUNC bb_progress_update(bb_progress_t *p, | |||
78 | } | 78 | } |
79 | 79 | ||
80 | #if ENABLE_FEATURE_ASSUME_UNICODE | 80 | #if ENABLE_FEATURE_ASSUME_UNICODE |
81 | check_unicode_in_env(); | 81 | init_unicode(); |
82 | /* libbb candidate? */ | 82 | /* libbb candidate? */ |
83 | { | 83 | { |
84 | wchar_t wbuf21[21]; | 84 | wchar_t wbuf21[21]; |
diff --git a/libbb/unicode.c b/libbb/unicode.c index 544528acd..9d316df04 100644 --- a/libbb/unicode.c +++ b/libbb/unicode.c | |||
@@ -7,7 +7,9 @@ | |||
7 | * Licensed under GPL version 2, see file LICENSE in this tarball for details. | 7 | * Licensed under GPL version 2, see file LICENSE in this tarball for details. |
8 | */ | 8 | */ |
9 | #include "libbb.h" | 9 | #include "libbb.h" |
10 | # include "unicode.h" | 10 | #include "unicode.h" |
11 | |||
12 | uint8_t unicode_status; | ||
11 | 13 | ||
12 | size_t FAST_FUNC bb_mbstrlen(const char *string) | 14 | size_t FAST_FUNC bb_mbstrlen(const char *string) |
13 | { | 15 | { |
@@ -17,32 +19,38 @@ size_t FAST_FUNC bb_mbstrlen(const char *string) | |||
17 | return width; | 19 | return width; |
18 | } | 20 | } |
19 | 21 | ||
20 | #if !ENABLE_LOCALE_SUPPORT | 22 | #if ENABLE_LOCALE_SUPPORT |
23 | |||
24 | /* Unicode support using libc */ | ||
25 | |||
26 | void FAST_FUNC init_unicode(void) | ||
27 | { | ||
28 | /* In unicode, this is a one character string */ | ||
29 | static const char unicode_0x394[] = { 0xce, 0x94, 0 }; | ||
30 | |||
31 | if (unicode_status != UNICODE_UNKNOWN) | ||
32 | return; | ||
33 | |||
34 | unicode_status = bb_mbstrlen(unicode_0x394) == 1 ? UNICODE_ON : UNICODE_OFF; | ||
35 | } | ||
36 | |||
37 | #else | ||
21 | 38 | ||
22 | /* Crude "locale support" which knows only C and Unicode locales */ | 39 | /* Crude "locale support" which knows only C and Unicode locales */ |
23 | 40 | ||
24 | /* unicode_is_enabled: | 41 | # if ENABLE_FEATURE_CHECK_UNICODE_IN_ENV |
25 | * 0: not known yet, | 42 | void FAST_FUNC init_unicode(void) |
26 | * 1: not unicode (IOW: assuming one char == one byte) | ||
27 | * 2: unicode | ||
28 | */ | ||
29 | # if !ENABLE_FEATURE_CHECK_UNICODE_IN_ENV | ||
30 | # define unicode_is_enabled 2 | ||
31 | # else | ||
32 | static smallint unicode_is_enabled; | ||
33 | void FAST_FUNC check_unicode_in_env(void) | ||
34 | { | 43 | { |
35 | char *lang; | 44 | char *lang; |
36 | 45 | ||
37 | if (unicode_is_enabled) | 46 | if (unicode_status != UNICODE_UNKNOWN) |
38 | return; | 47 | return; |
39 | unicode_is_enabled = 1; | ||
40 | 48 | ||
49 | unicode_status = UNICODE_OFF; | ||
41 | lang = getenv("LANG"); | 50 | lang = getenv("LANG"); |
42 | if (!lang || !(strstr(lang, ".utf") || strstr(lang, ".UTF"))) | 51 | if (!lang || !(strstr(lang, ".utf") || strstr(lang, ".UTF"))) |
43 | return; | 52 | return; |
44 | 53 | unicode_status = UNICODE_ON; | |
45 | unicode_is_enabled = 2; | ||
46 | } | 54 | } |
47 | # endif | 55 | # endif |
48 | 56 | ||
@@ -85,7 +93,7 @@ static size_t wcrtomb_internal(char *s, wchar_t wc) | |||
85 | 93 | ||
86 | size_t FAST_FUNC wcrtomb(char *s, wchar_t wc, mbstate_t *ps UNUSED_PARAM) | 94 | size_t FAST_FUNC wcrtomb(char *s, wchar_t wc, mbstate_t *ps UNUSED_PARAM) |
87 | { | 95 | { |
88 | if (unicode_is_enabled != 2) { | 96 | if (unicode_status != UNICODE_ON) { |
89 | *s = wc; | 97 | *s = wc; |
90 | return 1; | 98 | return 1; |
91 | } | 99 | } |
@@ -97,7 +105,7 @@ size_t FAST_FUNC wcstombs(char *dest, const wchar_t *src, size_t n) | |||
97 | { | 105 | { |
98 | size_t org_n = n; | 106 | size_t org_n = n; |
99 | 107 | ||
100 | if (unicode_is_enabled != 2) { | 108 | if (unicode_status != UNICODE_ON) { |
101 | while (n) { | 109 | while (n) { |
102 | wchar_t c = *src++; | 110 | wchar_t c = *src++; |
103 | *dest++ = c; | 111 | *dest++ = c; |
@@ -137,7 +145,7 @@ size_t FAST_FUNC mbstowcs(wchar_t *dest, const char *src, size_t n) | |||
137 | { | 145 | { |
138 | size_t org_n = n; | 146 | size_t org_n = n; |
139 | 147 | ||
140 | if (unicode_is_enabled != 2) { | 148 | if (unicode_status != UNICODE_ON) { |
141 | while (n) { | 149 | while (n) { |
142 | unsigned char c = *src++; | 150 | unsigned char c = *src++; |
143 | 151 | ||