changeset 712:a950dd960593

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.
author Rob Landley <rob@landley.net>
date Mon, 26 Nov 2012 14:14:29 -0600
parents dcc6136e6659
children ff8a27d3ab8f
files main.c scripts/make.sh toys.h toys/posix/wc.c
diffstat 4 files changed, 32 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/main.c	Sun Nov 25 19:23:10 2012 -0600
+++ b/main.c	Mon Nov 26 14:14:29 2012 -0600
@@ -101,9 +101,6 @@
   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 @@
 
 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;
--- a/scripts/make.sh	Sun Nov 25 19:23:10 2012 -0600
+++ b/scripts/make.sh	Mon Nov 26 14:14:29 2012 -0600
@@ -54,11 +54,27 @@
 	| 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 @@
         -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 @@
       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
--- a/toys.h	Sun Nov 25 19:23:10 2012 -0600
+++ b/toys.h	Mon Nov 26 14:14:29 2012 -0600
@@ -47,11 +47,11 @@
 #include <utime.h>
 #include <utmpx.h>
 
-#ifdef CFG_TOYBOX_I18N
+// Internationalization support
+
 #include <locale.h>
 #include <wchar.h>
 #include <wctype.h>
-#endif
 
 #include "lib/lib.h"
 #include "toys/e2fs.h"
--- a/toys/posix/wc.c	Sun Nov 25 19:23:10 2012 -0600
+++ b/toys/posix/wc.c	Mon Nov 26 14:14:29 2012 -0600
@@ -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 @@
     }
     if (len<1) break;
     for (i=0; i<len; i+=clen) {
-#ifdef CFG_TOYBOX_I18N
       wchar_t wchar;
-      if(toys.optflags&8) {
+      if (CFG_TOYBOX_I18N && (toys.optflags&FLAG_m)) {
         clen = mbrtowc(&wchar, toybuf+i, len-i, 0);
         if(clen==(size_t)(-1)) {
           if(i!=len-1) {
@@ -71,9 +70,7 @@
         if(clen==(size_t)(-2)) break;
         if(clen==0) clen=1;
         space = iswspace(wchar);
-      } else
-#endif
-             space = isspace(toybuf[i]);
+      } else space = isspace(toybuf[i]);
 
       if (toybuf[i]==10) lengths[0]++;
       if (space) word=0;