diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-10-23 21:06:06 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-10-23 21:06:06 +0200 |
commit | 53600591311a129717abd2e3bcaa302622a6ce67 (patch) | |
tree | 4b0d08ed429d4b73a9739339e74d84a8a72fe25e | |
parent | 6a0d7490ea6ad97aeafb9da04acab13bd3c38e4d (diff) | |
download | busybox-w32-53600591311a129717abd2e3bcaa302622a6ce67.tar.gz busybox-w32-53600591311a129717abd2e3bcaa302622a6ce67.tar.bz2 busybox-w32-53600591311a129717abd2e3bcaa302622a6ce67.zip |
libbb: introduce and use strcpy_and_process_escape_sequences
function old new delta
strcpy_and_process_escape_sequences - 50 +50
bb_process_escape_sequence 148 138 -10
printf_main 789 776 -13
getty_main 1897 1831 -66
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/3 up/down: 50/-89) Total: -39 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | coreutils/printf.c | 18 | ||||
-rw-r--r-- | e2fsprogs/old_e2fsprogs/fsck.c | 10 | ||||
-rw-r--r-- | include/libbb.h | 1 | ||||
-rw-r--r-- | libbb/parse_config.c | 14 | ||||
-rw-r--r-- | libbb/process_escape_sequence.c | 44 | ||||
-rw-r--r-- | loginutils/getty.c | 18 |
6 files changed, 44 insertions, 61 deletions
diff --git a/coreutils/printf.c b/coreutils/printf.c index 2cc238439..c8395fa89 100644 --- a/coreutils/printf.c +++ b/coreutils/printf.c | |||
@@ -122,16 +122,14 @@ static double my_xstrtod(const char *arg) | |||
122 | return result; | 122 | return result; |
123 | } | 123 | } |
124 | 124 | ||
125 | static void print_esc_string(char *str) | 125 | static void print_esc_string(const char *str) |
126 | { | 126 | { |
127 | while (*str) { | 127 | char c; |
128 | if (*str == '\\') { | 128 | while ((c = *str) != '\0') { |
129 | str++; | 129 | str++; |
130 | bb_putchar(bb_process_escape_sequence((const char **)&str)); | 130 | if (c == '\\') |
131 | } else { | 131 | c = bb_process_escape_sequence(&str); |
132 | bb_putchar(*str); | 132 | putchar(c); |
133 | str++; | ||
134 | } | ||
135 | } | 133 | } |
136 | } | 134 | } |
137 | 135 | ||
@@ -344,7 +342,7 @@ static char **print_formatted(char *f, char **argv, int *conv_err) | |||
344 | f--; | 342 | f--; |
345 | break; | 343 | break; |
346 | default: | 344 | default: |
347 | bb_putchar(*f); | 345 | putchar(*f); |
348 | } | 346 | } |
349 | } | 347 | } |
350 | 348 | ||
diff --git a/e2fsprogs/old_e2fsprogs/fsck.c b/e2fsprogs/old_e2fsprogs/fsck.c index 524b84652..3a0743bb1 100644 --- a/e2fsprogs/old_e2fsprogs/fsck.c +++ b/e2fsprogs/old_e2fsprogs/fsck.c | |||
@@ -349,15 +349,7 @@ static void parse_escape(char *word) | |||
349 | if (!word) | 349 | if (!word) |
350 | return; | 350 | return; |
351 | 351 | ||
352 | for (p = q = word; *p; q++) { | 352 | strcpy_and_process_escape_sequences(word, word); |
353 | c = *p++; | ||
354 | if (c != '\\') { | ||
355 | *q = c; | ||
356 | } else { | ||
357 | *q = bb_process_escape_sequence(&p); | ||
358 | } | ||
359 | } | ||
360 | *q = 0; | ||
361 | } | 353 | } |
362 | 354 | ||
363 | static void free_instance(struct fsck_instance *i) | 355 | static void free_instance(struct fsck_instance *i) |
diff --git a/include/libbb.h b/include/libbb.h index 409c434cb..c85dab282 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -324,6 +324,7 @@ extern void bb_copyfd_exact_size(int fd1, int fd2, off_t size) FAST_FUNC; | |||
324 | /* this helper yells "short read!" if param is not -1 */ | 324 | /* this helper yells "short read!" if param is not -1 */ |
325 | extern void complain_copyfd_and_die(off_t sz) NORETURN FAST_FUNC; | 325 | extern void complain_copyfd_and_die(off_t sz) NORETURN FAST_FUNC; |
326 | extern char bb_process_escape_sequence(const char **ptr) FAST_FUNC; | 326 | extern char bb_process_escape_sequence(const char **ptr) FAST_FUNC; |
327 | char* strcpy_and_process_escape_sequences(char *dst, const char *src) FAST_FUNC; | ||
327 | /* xxxx_strip version can modify its parameter: | 328 | /* xxxx_strip version can modify its parameter: |
328 | * "/" -> "/" | 329 | * "/" -> "/" |
329 | * "abc" -> "abc" | 330 | * "abc" -> "abc" |
diff --git a/libbb/parse_config.c b/libbb/parse_config.c index 3fff9f212..9dbfaf5d7 100644 --- a/libbb/parse_config.c +++ b/libbb/parse_config.c | |||
@@ -187,19 +187,7 @@ again: | |||
187 | 187 | ||
188 | #if 0 /* unused so far */ | 188 | #if 0 /* unused so far */ |
189 | if (flags & PARSE_ESCAPE) { | 189 | if (flags & PARSE_ESCAPE) { |
190 | const char *from; | 190 | strcpy_and_process_escape_sequences(tokens[t], tokens[t]); |
191 | char *to; | ||
192 | |||
193 | from = to = tokens[t]; | ||
194 | while (*from) { | ||
195 | if (*from == '\\') { | ||
196 | from++; | ||
197 | *to++ = bb_process_escape_sequence(&from); | ||
198 | } else { | ||
199 | *to++ = *from++; | ||
200 | } | ||
201 | } | ||
202 | *to = '\0'; | ||
203 | } | 191 | } |
204 | #endif | 192 | #endif |
205 | /* Skip possible delimiters */ | 193 | /* Skip possible delimiters */ |
diff --git a/libbb/process_escape_sequence.c b/libbb/process_escape_sequence.c index 82cbe10dc..dd6e076b0 100644 --- a/libbb/process_escape_sequence.c +++ b/libbb/process_escape_sequence.c | |||
@@ -31,14 +31,13 @@ char FAST_FUNC bb_process_escape_sequence(const char **ptr) | |||
31 | unsigned num_digits; | 31 | unsigned num_digits; |
32 | unsigned r; | 32 | unsigned r; |
33 | unsigned n; | 33 | unsigned n; |
34 | unsigned d; | ||
35 | unsigned base; | 34 | unsigned base; |
36 | 35 | ||
37 | num_digits = n = 0; | 36 | num_digits = n = 0; |
38 | base = 8; | 37 | base = 8; |
39 | q = *ptr; | 38 | q = *ptr; |
40 | 39 | ||
41 | #ifdef WANT_HEX_ESCAPES | 40 | #if WANT_HEX_ESCAPES |
42 | if (*q == 'x') { | 41 | if (*q == 'x') { |
43 | ++q; | 42 | ++q; |
44 | base = 16; | 43 | base = 16; |
@@ -50,20 +49,21 @@ char FAST_FUNC bb_process_escape_sequence(const char **ptr) | |||
50 | * \02 works, \2 does not (prints \ and 2). | 49 | * \02 works, \2 does not (prints \ and 2). |
51 | * We treat \2 as a valid octal escape sequence. */ | 50 | * We treat \2 as a valid octal escape sequence. */ |
52 | do { | 51 | do { |
53 | d = (unsigned char)(*q) - '0'; | 52 | #if !WANT_HEX_ESCAPES |
54 | #ifdef WANT_HEX_ESCAPES | 53 | unsigned d = (unsigned char)(*q) - '0'; |
55 | if (d >= 10) { | 54 | #else |
56 | d = (unsigned char)(_tolower(*q)) - 'a' + 10; | 55 | unsigned d = (unsigned char)_tolower(*q) - '0'; |
57 | } | 56 | if (d >= 10) |
57 | d += ('0' - 'a' + 10); | ||
58 | #endif | 58 | #endif |
59 | |||
60 | if (d >= base) { | 59 | if (d >= base) { |
61 | #ifdef WANT_HEX_ESCAPES | 60 | if (WANT_HEX_ESCAPES && base == 16) { |
62 | if ((base == 16) && (!--num_digits)) { | 61 | --num_digits; |
63 | /* return '\\'; */ | 62 | if (num_digits == 0) { |
64 | --q; | 63 | /* \x<bad_char> */ |
64 | --q; /* go back to x */ | ||
65 | } | ||
65 | } | 66 | } |
66 | #endif | ||
67 | break; | 67 | break; |
68 | } | 68 | } |
69 | 69 | ||
@@ -85,7 +85,9 @@ char FAST_FUNC bb_process_escape_sequence(const char **ptr) | |||
85 | } | 85 | } |
86 | } while (*++p); | 86 | } while (*++p); |
87 | /* p points to found escape char or NUL, | 87 | /* p points to found escape char or NUL, |
88 | * advance it and find what it translates to */ | 88 | * advance it and find what it translates to. |
89 | * Note that unrecognized sequence \z returns '\' | ||
90 | * and leaves ptr pointing to z. */ | ||
89 | p += sizeof(charmap) / 2; | 91 | p += sizeof(charmap) / 2; |
90 | n = *p; | 92 | n = *p; |
91 | } | 93 | } |
@@ -94,3 +96,17 @@ char FAST_FUNC bb_process_escape_sequence(const char **ptr) | |||
94 | 96 | ||
95 | return (char) n; | 97 | return (char) n; |
96 | } | 98 | } |
99 | |||
100 | char* FAST_FUNC strcpy_and_process_escape_sequences(char *dst, const char *src) | ||
101 | { | ||
102 | while (1) { | ||
103 | char c, c1; | ||
104 | c = c1 = *src++; | ||
105 | if (c1 == '\\') | ||
106 | c1 = bb_process_escape_sequence(&src); | ||
107 | *dst = c1; | ||
108 | if (c == '\0') | ||
109 | return dst; | ||
110 | dst++; | ||
111 | } | ||
112 | } | ||
diff --git a/loginutils/getty.c b/loginutils/getty.c index b1cd235fb..ab55ea4b0 100644 --- a/loginutils/getty.c +++ b/loginutils/getty.c | |||
@@ -188,21 +188,9 @@ static void parse_args(char **argv, struct options *op, char **fakehost_p) | |||
188 | &(op->login), &op->timeout); | 188 | &(op->login), &op->timeout); |
189 | argv += optind; | 189 | argv += optind; |
190 | if (op->flags & F_INITSTRING) { | 190 | if (op->flags & F_INITSTRING) { |
191 | const char *p = op->initstring; | 191 | op->initstring = xstrdup(op->initstring); |
192 | char *q; | 192 | /* decode \ddd octal codes into chars */ |
193 | 193 | strcpy_and_process_escape_sequences((char*)op->initstring, op->initstring); | |
194 | op->initstring = q = xstrdup(p); | ||
195 | /* copy optarg into op->initstring decoding \ddd | ||
196 | octal codes into chars */ | ||
197 | while (*p) { | ||
198 | if (*p == '\\') { | ||
199 | p++; | ||
200 | *q++ = bb_process_escape_sequence(&p); | ||
201 | } else { | ||
202 | *q++ = *p++; | ||
203 | } | ||
204 | } | ||
205 | *q = '\0'; | ||
206 | } | 194 | } |
207 | op->flags ^= F_ISSUE; /* invert flag "show /etc/issue" */ | 195 | op->flags ^= F_ISSUE; /* invert flag "show /etc/issue" */ |
208 | debug("after getopt\n"); | 196 | debug("after getopt\n"); |