From 6cf0a115451d6d5ead94860f0731040dc2293db0 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Mon, 26 Nov 2012 14:14:29 -0600 Subject: Cleanup i18n support (#ifdefectomy, move global init to process launch). Teach make.sh to emit "#define FLAG_x 0" for options inside disabled USE macros so we can unconditionally refer to them. --- main.c | 5 ++--- scripts/make.sh | 32 +++++++++++++++++++++++++------- toys.h | 4 ++-- toys/posix/wc.c | 9 +++------ 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/main.c b/main.c index 2b04d135..829b787a 100644 --- a/main.c +++ b/main.c @@ -101,9 +101,6 @@ void toy_exec(char *argv[]) which = toy_find(argv[0]); if (!which) return; toy_init(which, argv); -#ifdef CFG_TOYBOX_I18N - setlocale(LC_ALL, ""); -#endif toys.which->toy_main(); exit(toys.exitval); } @@ -145,6 +142,8 @@ void toybox_main(void) int main(int argc, char *argv[]) { + if (CFG_TOYBOX_I18N) setlocale(LC_ALL, ""); + // Artificial scope to eat less stack for things we call { char *name; diff --git a/scripts/make.sh b/scripts/make.sh index f9fa0a40..921d1994 100755 --- a/scripts/make.sh +++ b/scripts/make.sh @@ -54,11 +54,27 @@ sed -n -e 's/^USE_[A-Z0-9_]*(/&/p' toys/*/*.c \ | sed 's/\(.*TOY(\)\([^,]*\),\(.*\)/\2 \1\2,\3/' | sort -k 1,1 \ | sed 's/[^ ]* //' >> generated/newtoys.h +# Extract list of command letters from processed header file + +function getflags() +{ + sed -n -e "s/.*TOY($1"',[ \t]*"\([^"]*\)"[ \t]*,.*)/\1/' \ + -e 't keep;d;:keep' -e 's/^[<>=][0-9]//' -e 's/[?&^]//' \ + -e 't keep' -e 's/[><=][0-9][0-9]*//g' -e 's/+.//g' \ + -e 's/([^)]*)//g' -e 's/\[[^]]*\]//g' -e 's/[-?^:&#|@*]//g' -e 'p' +} + # Extract global structure definitions and flag definitions from toys/*/*.c function getglobals() { + # Run newtoys.h through the compiler's preprocessor to resolve USE macros + # against current config. NEWTOYS="$(cat generated/config.h generated/newtoys.h | gcc -E - | sed 's/" *"//g')" + + # Grab allyesconfig for comparison + ALLTOYS="$((sed '/USE_.*([^)]*)$/s/$/ __VA_ARGS__/' generated/config.h && cat generated/newtoys.h) | gcc -E - | sed 's/" *"//g')" + for i in toys/*/*.c do NAME="$(echo $i | sed 's@.*/\(.*\)\.c@\1@')" @@ -68,13 +84,9 @@ function getglobals() -e 's/^GLOBALS(/struct '"$NAME"'_data {/' \ -e 's/^)/};/' -e 'p' $i - # And get flag definitions - FLAGS="$(echo "$NEWTOYS" | sed -n \ - -e "s/.*TOY($NAME"',[ \t]*"\([^"]*\)"[ \t]*,.*)/\1/' \ - -e 't keep;d;:keep' -e 's/^[<>=][0-9]//' -e 's/[?&^]//' \ - -e 't keep' -e 's/[><=][0-9][0-9]*//g' -e 's/+.//g' \ - -e 's/([^)]*)//g' -e 's/\[[^]]*\]//g' -e 's/[-?^:&#|@*]//g' \ - -e 'p')" + FLAGS="$(echo "$NEWTOYS" | getflags "$NAME")" + ZFLAGS="$(echo "$ALLTOYS" | getflags "$NAME" | sed 's/[-'"$FLAGS"']//g')" + echo "#ifdef FOR_${NAME}" X=0 while [ $X -lt ${#FLAGS} ] @@ -83,6 +95,12 @@ function getglobals() X=$(($X+1)) echo "(1<<$((${#FLAGS}-$X)))" done + X=0 + while [ $X -lt ${#ZFLAGS} ] + do + echo "#define FLAG_${ZFLAGS:$X:1} 0" + X=$(($X+1)) + done echo "#define TT this.${NAME}" echo "#endif" done diff --git a/toys.h b/toys.h index 69876f23..40946d79 100644 --- a/toys.h +++ b/toys.h @@ -47,11 +47,11 @@ #include #include -#ifdef CFG_TOYBOX_I18N +// Internationalization support + #include #include #include -#endif #include "lib/lib.h" #include "toys/e2fs.h" diff --git a/toys/posix/wc.c b/toys/posix/wc.c index 63128d67..d2eb306d 100644 --- a/toys/posix/wc.c +++ b/toys/posix/wc.c @@ -4,7 +4,7 @@ * * See http://opengroup.org/onlinepubs/9699919799/utilities/wc.html -USE_WC(NEWTOY(wc, "mcwl", TOYFLAG_USR|TOYFLAG_BIN)) +USE_WC(NEWTOY(wc, USE_TOYBOX_I18N("m")"cwl", TOYFLAG_USR|TOYFLAG_BIN)) config WC bool "wc" @@ -58,9 +58,8 @@ static void do_wc(int fd, char *name) } if (len<1) break; for (i=0; i