Merge commit 'goog/master' into merge_master
diff --git a/include/private/android_filesystem_config.h b/include/private/android_filesystem_config.h
index 1a14842..ad4bd74 100644
--- a/include/private/android_filesystem_config.h
+++ b/include/private/android_filesystem_config.h
@@ -62,6 +62,7 @@
 #define AID_NET_BT        3002  /* bluetooth: create sco, rfcomm or l2cap sockets */
 #define AID_INET          3003  /* can create AF_INET and AF_INET6 sockets */
 #define AID_NET_RAW       3004  /* can create raw INET sockets */
+#define AID_NET_ADMIN     3005  /* can configure interfaces and routing tables. */
 
 #define AID_MISC          9998  /* access to misc storage */
 #define AID_NOBODY        9999
@@ -101,6 +102,7 @@
     { "keystore",  AID_KEYSTORE, },
     { "inet",      AID_INET, },
     { "net_raw",   AID_NET_RAW, },
+    { "net_admin", AID_NET_ADMIN, },
     { "misc",      AID_MISC, },
     { "nobody",    AID_NOBODY, },
 };
@@ -135,7 +137,7 @@
     { 00750, AID_ROOT,   AID_SHELL,  "sbin" },
     { 00755, AID_ROOT,   AID_SHELL,  "system/bin" },
     { 00755, AID_ROOT,   AID_SHELL,  "system/xbin" },
-    { 00777, AID_ROOT,   AID_ROOT,   "system/etc/ppp" }, /* REMOVE */
+    { 00755, AID_ROOT,   AID_ROOT,   "system/etc/ppp" },
     { 00777, AID_ROOT,   AID_ROOT,   "sdcard" },
     { 00755, AID_ROOT,   AID_ROOT,   0 },
 };
@@ -147,20 +149,18 @@
 ** and will allow partial matches.
 */
 static struct fs_path_config android_files[] = {
-    { 00555, AID_ROOT,      AID_ROOT,      "system/etc/ppp/ip-up" },
-    { 00555, AID_ROOT,      AID_ROOT,      "system/etc/ppp/ip-down" },
     { 00440, AID_ROOT,      AID_SHELL,     "system/etc/init.goldfish.rc" },
     { 00550, AID_ROOT,      AID_SHELL,     "system/etc/init.goldfish.sh" },
     { 00440, AID_ROOT,      AID_SHELL,     "system/etc/init.trout.rc" },
     { 00550, AID_ROOT,      AID_SHELL,     "system/etc/init.ril" },
     { 00550, AID_ROOT,      AID_SHELL,     "system/etc/init.testmenu" },
-    { 00550, AID_ROOT,      AID_SHELL,     "system/etc/init.gprs-pppd" },
     { 00550, AID_DHCP,      AID_SHELL,     "system/etc/dhcpcd/dhcpcd-run-hooks" },
     { 00440, AID_BLUETOOTH, AID_BLUETOOTH, "system/etc/dbus.conf" },
     { 00440, AID_BLUETOOTH, AID_BLUETOOTH, "system/etc/bluez/main.conf" },
     { 00440, AID_BLUETOOTH, AID_BLUETOOTH, "system/etc/bluez/input.conf" },
     { 00440, AID_BLUETOOTH, AID_BLUETOOTH, "system/etc/bluez/audio.conf" },
-    { 00440, AID_RADIO,     AID_AUDIO,     "/system/etc/AudioPara4.csv" },
+    { 00440, AID_RADIO,     AID_AUDIO,     "system/etc/AudioPara4.csv" },
+    { 00555, AID_ROOT,      AID_ROOT,      "system/etc/ppp/*" },
     { 00644, AID_SYSTEM,    AID_SYSTEM,    "data/app/*" },
     { 00644, AID_SYSTEM,    AID_SYSTEM,    "data/app-private/*" },
     { 00644, AID_APP,       AID_APP,       "data/data/*" },
diff --git a/init/builtins.c b/init/builtins.c
index 93ce6e8..16601ce 100644
--- a/init/builtins.c
+++ b/init/builtins.c
@@ -49,7 +49,7 @@
     fd = open(path, O_WRONLY|O_CREAT, 0622);
 
     if (fd < 0)
-        return -1;
+        return -errno;
 
     len = strlen(value);
 
@@ -59,7 +59,7 @@
 
     close(fd);
     if (ret < 0) {
-        return -1;
+        return -errno;
     } else {
         return 0;
     }
diff --git a/init/devices.c b/init/devices.c
index 880536c..e76d354 100644
--- a/init/devices.c
+++ b/init/devices.c
@@ -118,6 +118,9 @@
     { "/dev/msm_camera/",   0660,   AID_SYSTEM,     AID_SYSTEM,     1 },
     { "/dev/akm8976_daemon",0640,   AID_COMPASS,    AID_SYSTEM,     0 },
     { "/dev/akm8976_aot",   0640,   AID_COMPASS,    AID_SYSTEM,     0 },
+    { "/dev/akm8973_daemon",0640,   AID_COMPASS,    AID_SYSTEM,     0 },
+    { "/dev/akm8973_aot",   0640,   AID_COMPASS,    AID_SYSTEM,     0 },
+    { "/dev/bma150",        0640,   AID_COMPASS,    AID_SYSTEM,     0 },
     { "/dev/akm8976_pffd",  0640,   AID_COMPASS,    AID_SYSTEM,     0 },
     { "/dev/msm_pcm_out",   0660,   AID_SYSTEM,     AID_AUDIO,      1 },
     { "/dev/msm_pcm_in",    0660,   AID_SYSTEM,     AID_AUDIO,      1 },
@@ -137,7 +140,8 @@
     { "/dev/qmi2",          0640,   AID_RADIO,      AID_RADIO,      0 },
         /* CDMA radio interface MUX */
     { "/dev/ts0710mux",     0640,   AID_RADIO,      AID_RADIO,      1 },
-    { "/dev/tun",           0640,   AID_VPN  ,      AID_VPN,        0 },
+    { "/dev/ppp",           0660,   AID_RADIO,      AID_VPN,        0 },
+    { "/dev/tun",           0640,   AID_VPN,        AID_VPN,        0 },
     { NULL, 0, 0, 0, 0 },
 };
 
diff --git a/init/property_service.c b/init/property_service.c
index 48ca3ea..23a8821 100644
--- a/init/property_service.c
+++ b/init/property_service.c
@@ -67,6 +67,8 @@
     { "wlan.",		AID_SYSTEM },
     { "dhcp.",		AID_SYSTEM },
     { "dhcp.",		AID_DHCP },
+    { "vpn.",		AID_SYSTEM },
+    { "vpn.",		AID_VPN },
     { "debug.",		AID_SHELL },
     { "log.",		AID_SHELL },
     { "service.adb.root",	AID_SHELL },
diff --git a/libacc/acc.cpp b/libacc/acc.cpp
index fdddf22..0d26a5c 100644
--- a/libacc/acc.cpp
+++ b/libacc/acc.cpp
@@ -1266,6 +1266,278 @@
 
 #endif // PROVIDE_TRACE_CODEGEN
 
+
+    // Subset of STL vector.
+    template<class E> class Vector {
+        public:
+        Vector() {
+            mpBase = 0;
+            mUsed = 0;
+            mSize = 0;
+        }
+
+        ~Vector() {
+            if (mpBase) {
+                for(size_t i = 0; i < mUsed; i++)  {
+                    mpBase[mUsed].~E();
+                }
+                free(mpBase);
+            }
+        }
+
+        inline E& operator[](size_t i) {
+            return mpBase[i];
+        }
+
+        inline E& front() {
+            return mpBase[0];
+        }
+
+        inline E& back() {
+            return mpBase[mUsed - 1];
+        }
+
+        void pop_back() {
+            mUsed -= 1;
+            mpBase[mUsed].~E();
+        }
+
+        void push_back(const E& item) {
+            * ensure(1) = item;
+        }
+
+        size_t size() {
+            return mUsed;
+        }
+
+    private:
+        E* ensure(int n) {
+            size_t newUsed = mUsed + n;
+            if (newUsed > mSize) {
+                size_t newSize = mSize * 2 + 10;
+                if (newSize < newUsed) {
+                    newSize = newUsed;
+                }
+                mpBase = (E*) realloc(mpBase, sizeof(E) * newSize);
+                mSize = newSize;
+            }
+            E* result = mpBase + mUsed;
+            mUsed = newUsed;
+            return result;
+        }
+
+        E* mpBase;
+        size_t mUsed;
+        size_t mSize;
+    };
+
+    class Arena {
+    public:
+        // Used to record a given allocation amount.
+        // Used:
+        // Mark mark = arena.mark();
+        // ... lots of arena.allocate()
+        // arena.free(mark);
+
+        struct Mark {
+            size_t chunk;
+            size_t offset;
+        };
+
+        Arena() {
+            mCurrentChunk = 0;
+            Chunk start(CHUNK_SIZE);
+            mData.push_back(start);
+        }
+
+        ~Arena() {
+            for(size_t i = 0; i < mData.size(); i++) {
+                mData[i].free();
+            }
+        }
+
+        // Alloc using the standard alignment size safe for any variable
+        void* alloc(size_t size) {
+            return alloc(size, 8);
+        }
+
+        Mark mark(){
+            Mark result;
+            result.chunk = mCurrentChunk;
+            result.offset = mData[mCurrentChunk].mOffset;
+            return result;
+        }
+
+        void freeToMark(const Mark& mark) {
+            mCurrentChunk = mark.chunk;
+            mData[mCurrentChunk].mOffset = mark.offset;
+        }
+
+    private:
+        // Allocate memory aligned to a given size
+        // and a given power-of-two-sized alignment (e.g. 1,2,4,8,...)
+        // Memory is not zero filled.
+
+        void* alloc(size_t size, size_t alignment) {
+            while (size > mData[mCurrentChunk].remainingCapacity(alignment)) {
+                if (mCurrentChunk + 1 < mData.size()) {
+                    mCurrentChunk++;
+                } else {
+                    size_t allocSize = CHUNK_SIZE;
+                    if (allocSize < size + alignment - 1) {
+                        allocSize = size + alignment - 1;
+                    }
+                    Chunk chunk(allocSize);
+                    mData.push_back(chunk);
+                    mCurrentChunk++;
+                }
+            }
+            return mData[mCurrentChunk].allocate(size, alignment);
+        }
+
+        static const size_t CHUNK_SIZE = 128*1024;
+        // Note: this class does not deallocate its
+        // memory when it's destroyed. It depends upon
+        // its parent to deallocate the memory.
+        struct Chunk {
+            Chunk() {
+                mpData = 0;
+                mSize = 0;
+                mOffset = 0;
+            }
+
+            Chunk(size_t size) {
+                mSize = size;
+                mpData = (char*) malloc(size);
+                mOffset = 0;
+            }
+
+            ~Chunk() {
+                // Doesn't deallocate memory.
+            }
+
+            void* allocate(size_t size, size_t alignment) {
+                size_t alignedOffset = aligned(mOffset, alignment);
+                void* result = mpData + alignedOffset;
+                mOffset = alignedOffset + size;
+                return result;
+            }
+
+            void free() {
+                if (mpData) {
+                    ::free(mpData);
+                    mpData = 0;
+                }
+            }
+
+            size_t remainingCapacity(size_t alignment) {
+                return aligned(mSize, alignment) - aligned(mOffset, alignment);
+            }
+
+            // Assume alignment is a power of two
+            inline size_t aligned(size_t v, size_t alignment) {
+                size_t mask = alignment-1;
+                return (v + mask) & ~mask;
+            }
+
+            char* mpData;
+            size_t mSize;
+            size_t mOffset;
+        };
+
+        size_t mCurrentChunk;
+
+        Vector<Chunk> mData;
+    };
+
+    typedef int tokenid_t;
+    struct VariableInfo;
+
+    struct Token {
+        int hash;
+        size_t length;
+        char* pText;
+        tokenid_t id;
+
+        // Current values for the token
+        char* mpMacroDefinition;
+        VariableInfo* mpVariableInfo;
+    };
+
+    class TokenTable {
+    public:
+        // Don't use 0..0xff, allows characters and operators to be tokens too.
+
+        static const int TOKEN_BASE = 0x100;
+        TokenTable() {
+            mpMap = hashmapCreate(128, hashFn, equalsFn);
+        }
+
+        ~TokenTable() {
+            hashmapFree(mpMap);
+        }
+
+        void setArena(Arena* pArena) {
+            mpArena = pArena;
+        }
+
+        // Returns a token for a given string of characters.
+        tokenid_t intern(const char* pText, size_t length) {
+            Token probe;
+            int hash = hashmapHash((void*) pText, length);
+            {
+                Token probe;
+                probe.hash = hash;
+                probe.length = length;
+                probe.pText = (char*) pText;
+                Token* pValue = (Token*) hashmapGet(mpMap, &probe);
+                if (pValue) {
+                    return pValue->id;
+                }
+            }
+
+            Token* pToken = (Token*) mpArena->alloc(sizeof(Token));
+            memset(pToken, 0, sizeof(*pToken));
+            pToken->hash = hash;
+            pToken->length = length;
+            pToken->pText = (char*) mpArena->alloc(length + 1);
+            memcpy(pToken->pText, pText, length);
+            pToken->pText[length] = 0;
+            pToken->id = mTokens.size() + TOKEN_BASE;
+            mTokens.push_back(pToken);
+            hashmapPut(mpMap, pToken, pToken);
+            return pToken->id;
+        }
+
+        // Return the Token for a given tokenid.
+        Token& operator[](tokenid_t id) {
+            return *mTokens[id - TOKEN_BASE];
+        }
+
+        inline size_t size() {
+            return mTokens.size();
+        }
+
+    private:
+
+        static int hashFn(void* pKey) {
+            Token* pToken = (Token*) pKey;
+            return pToken->hash;
+        }
+
+        static bool equalsFn(void* keyA, void* keyB) {
+            Token* pTokenA = (Token*) keyA;
+            Token* pTokenB = (Token*) keyB;
+            // Don't need to compare hash values, they should always be equal
+            return pTokenA->length == pTokenB->length
+                && strcmp(pTokenA->pText, pTokenB->pText) == 0;
+        }
+
+        Hashmap* mpMap;
+        Vector<Token*> mTokens;
+        Arena* mpArena;
+    };
+
     class InputStream {
     public:
         int getChar() {
@@ -1382,6 +1654,10 @@
             * ensure(1) = c;
         }
 
+        void append(String& other) {
+            appendBytes(other.getUnwrapped(), other.len());
+        }
+
         char* orphan() {
             char* result = mpBase;
             mpBase = 0;
@@ -1430,381 +1706,156 @@
         size_t mSize;
     };
 
-    /**
-     * Wrap an externally allocated string for use as a hash key.
-     */
-    class FakeString : public String {
-    public:
-        FakeString(const char* string, size_t length) :
-            String((char*) string, length, true) {}
+    void internKeywords() {
+        // Note: order has to match TOK_ constants
+        static const char* keywords[] = {
+            "int",
+            "char",
+            "void",
+            "if",
+            "else",
+            "while",
+            "break",
+            "return",
+            "for",
+            "pragma",
+            "define",
+            "auto",
+            "case",
+            "const",
+            "continue",
+            "default",
+            "do",
+            "double",
+            "enum",
+            "extern",
+            "float",
+            "goto",
+            "long",
+            "register",
+            "short",
+            "signed",
+            "sizeof",
+            "static",
+            "struct",
+            "switch",
+            "typedef",
+            "union",
+            "unsigned",
+            "volatile",
+            "_Bool",
+            "_Complex",
+            "_Imaginary",
+            "inline",
+            "restrict",
+            0};
 
-        ~FakeString() {
-            orphan();
+        for(int i = 0; keywords[i]; i++) {
+            mTokenTable.intern(keywords[i], strlen(keywords[i]));
         }
-    };
-
-    template<class V> class StringTable {
-    public:
-        StringTable() {
-            init(10);
-        }
-
-        StringTable(size_t initialCapacity) {
-            init(initialCapacity);
-        }
-
-        ~StringTable() {
-            clear();
-            hashmapFree(mpMap);
-        }
-
-        void clear() {
-            hashmapForEach(mpMap, freeKeyValue, this);
-        }
-
-        bool contains(String* pKey) {
-            bool result = hashmapContainsKey(mpMap, pKey);
-            return result;
-        }
-
-        V* get(String* pKey) {
-            V* result = (V*) hashmapGet(mpMap, pKey);
-            return result;
-        }
-
-        V* remove(String* pKey) {
-            V* result = (V*) hashmapRemove(mpMap, pKey);
-            return result;
-        }
-
-        V* put(String* pKey, V* value) {
-            V* result = (V*) hashmapPut(mpMap, pKey, value);
-            if (result) {
-                // The key was not adopted by the map, so delete it here.
-                delete pKey;
-            }
-            return result;
-        }
-
-        void forEach(bool (*callback)(String* key, V* value, void* context),
-                     void* context) {
-            hashmapForEach(mpMap, (bool (*)(void*, void*, void*)) callback,
-                           context);
-        }
-
-    protected:
-
-        void init(size_t initialCapacity) {
-            mpMap = hashmapCreate(initialCapacity, hashFn, equalsFn);
-        }
-
-        static int hashFn(void* pKey) {
-            String* pString = (String*) pKey;
-            return hashmapHash(pString->getUnwrapped(), pString->len());
-        }
-
-        static bool equalsFn(void* keyA, void* keyB) {
-            String* pStringA = (String*) keyA;
-            String* pStringB = (String*) keyB;
-            return pStringA->len() == pStringB->len()
-                && strcmp(pStringA->getUnwrapped(), pStringB->getUnwrapped())
-                    == 0;
-        }
-
-        static bool freeKeyValue(void* key, void* value, void* context) {
-            delete (String*) key;
-            delete (V*) value;
-            return true;
-        }
-
-        Hashmap* mpMap;
-    };
-
-    class MacroTable : public StringTable<String> {
-    public:
-        MacroTable() : StringTable<String>(10) {}
-    };
-
-    class KeywordTable {
-    public:
-
-        KeywordTable(){
-            mpMap = hashmapCreate(40, hashFn, equalsFn);
-            put("int", TOK_INT);
-            put("char", TOK_CHAR);
-            put("void", TOK_VOID);
-            put("if", TOK_IF);
-            put("else", TOK_ELSE);
-            put("while", TOK_WHILE);
-            put("break", TOK_BREAK);
-            put("return", TOK_RETURN);
-            put("for", TOK_FOR);
-            // TODO: remove these preprocessor-specific keywords. You should
-            // be able to have symbols named pragma or define.
-            put("pragma", TOK_PRAGMA);
-            put("define", TOK_DEFINE);
-
-            const char* unsupported[] = {
-                "auto",
-                "case",
-                "const",
-                "continue",
-                "default",
-                "do",
-                "double",
-                "enum",
-                "extern",
-                "float",
-                "goto",
-                "long",
-                "register",
-                "short",
-                "signed",
-                "sizeof",
-                "static",
-                "struct",
-                "switch",
-                "typedef",
-                "union",
-                "unsigned",
-                "volatile",
-                "_Bool",
-                "_Complex",
-                "_Imaginary",
-                "inline",
-                "restrict",
-                0};
-
-            for(int i = 0; unsupported[i]; i++) {
-                put(unsupported[i], TOK_UNSUPPORTED_KEYWORD);
-            }
-        }
-
-        ~KeywordTable() {
-            hashmapFree(mpMap);
-        }
-
-        int get(String* key) {
-            return (int) hashmapGet(mpMap, key->getUnwrapped());
-        }
-
-        const char* lookupKeyFor(int value) {
-            FindValContext context;
-            context.key = 0;
-            hashmapForEach(mpMap, findKeyFn, &context);
-            return context.key;
-        }
-
-    private:
-        void put(const char* kw, int val) {
-            hashmapPut(mpMap, (void*) kw, (void*) val);
-        }
-
-        static int hashFn(void* pKey) {
-            char* pString = (char*) pKey;
-            return hashmapHash(pString, strlen(pString));
-        }
-
-        static bool equalsFn(void* keyA, void* keyB) {
-            const char* pStringA = (const char*) keyA;
-            const char* pStringB = (const char*) keyB;
-            return strcmp(pStringA, pStringB)  == 0;
-        }
-
-        struct FindValContext {
-            char* key;
-            int value;
-        };
-
-        static bool findKeyFn(void* key, void* value, void* context) {
-            FindValContext* pContext = (FindValContext*) context;
-            if ((int) value == pContext->value) {
-                pContext->key = (char*) key;
-                return false;
-            }
-            return true;
-        }
-
-        Hashmap* mpMap;
-    };
-
-    template<class E> class Array {
-        public:
-        Array() {
-            mpBase = 0;
-            mUsed = 0;
-            mSize = 0;
-        }
-
-        ~Array() {
-            if (mpBase) {
-                free(mpBase);
-            }
-        }
-
-        E get(int i) {
-            if (i < 0 || i > (int) mUsed) {
-                // error("internal error: Index out of range");
-                return E();
-            }
-            return mpBase[i];
-        }
-
-        void set(int i, E val) {
-            mpBase[i] =  val;
-        }
-
-        void pop() {
-            if (mUsed > 0) {
-                mUsed -= 1;
-            } else {
-                // error("internal error: Popped empty stack.");
-            }
-        }
-
-        void push(E item) {
-            * ensure(1) = item;
-        }
-
-        size_t len() {
-            return mUsed;
-        }
-
-    private:
-        E* ensure(int n) {
-            size_t newUsed = mUsed + n;
-            if (newUsed > mSize) {
-                size_t newSize = mSize * 2 + 10;
-                if (newSize < newUsed) {
-                    newSize = newUsed;
-                }
-                mpBase = (E*) realloc(mpBase, sizeof(E) * newSize);
-                mSize = newSize;
-            }
-            E* result = mpBase + mUsed;
-            mUsed = newUsed;
-            return result;
-        }
-
-        E* mpBase;
-        size_t mUsed;
-        size_t mSize;
-    };
+    }
 
     struct InputState {
         InputStream* pStream;
         int oldCh;
     };
 
+    struct Type;
+
     struct VariableInfo {
-        VariableInfo() {
-            pAddress = 0;
-            pForward = 0;
-        }
         void* pAddress;
         void* pForward; // For a forward direction, linked list of data to fix up
+        tokenid_t tok;
+        size_t level;
+        VariableInfo* pOldDefinition;
+        Type* pType;
     };
 
-    typedef StringTable<VariableInfo> SymbolTable;
-
     class SymbolStack {
     public:
         SymbolStack() {
-            mLevel = 0;
-            addEntry();
+            mpArena = 0;
+            mpTokenTable = 0;
+        }
+
+        void setArena(Arena* pArena) {
+            mpArena = pArena;
+        }
+
+        void setTokenTable(TokenTable* pTokenTable) {
+            mpTokenTable = pTokenTable;
         }
 
         void pushLevel() {
-            mLevel++;
+            Mark mark;
+            mark.mArenaMark = mpArena->mark();
+            mark.mSymbolHead = mStack.size();
+            mLevelStack.push_back(mark);
         }
 
         void popLevel() {
-            mLevel--;
-            Entry e = mStack.get(mStack.len()-1);
-            if (mLevel < e.level) {
-                mStack.pop();
-                delete e.pTable;
+            // Undo any shadowing that was done:
+            Mark mark = mLevelStack.back();
+            mLevelStack.pop_back();
+            while (mStack.size() > mark.mSymbolHead) {
+                VariableInfo* pV = mStack.back();
+                mStack.pop_back();
+                (*mpTokenTable)[pV->tok].mpVariableInfo = pV->pOldDefinition;
             }
+            mpArena->freeToMark(mark.mArenaMark);
         }
 
-        VariableInfo* get(String* pName) {
-            int len = mStack.len();
-            VariableInfo* v = NULL;
-            int level = -1;
-            for (int i = len - 1; i >= 0; i--) {
-                Entry e = mStack.get(i);
-                v = e.pTable->get(pName);
-                if (v) {
-                    level = e.level;
+        bool isDefinedAtCurrentLevel(tokenid_t tok) {
+            VariableInfo* pV = (*mpTokenTable)[tok].mpVariableInfo;
+            return pV && pV->level == level();
+        }
+
+        VariableInfo* add(tokenid_t tok) {
+            Token& token = (*mpTokenTable)[tok];
+            VariableInfo* pOldV = token.mpVariableInfo;
+            VariableInfo* pNewV =
+                (VariableInfo*) mpArena->alloc(sizeof(VariableInfo));
+            memset(pNewV, 0, sizeof(VariableInfo));
+            pNewV->tok = tok;
+            pNewV->level = level();
+            pNewV->pOldDefinition = pOldV;
+            token.mpVariableInfo = pNewV;
+            mStack.push_back(pNewV);
+            return pNewV;
+        }
+
+        VariableInfo* add(Type* pType) {
+            VariableInfo* pVI = add(pType->id);
+            pVI->pType = pType;
+            return pVI;
+        }
+
+        void forEach(bool (*fn)(VariableInfo*, void*), void* context) {
+            for (size_t i = 0; i < mStack.size(); i++) {
+                if (! fn(mStack[i], context)) {
                     break;
                 }
             }
-#if 0
-            fprintf(stderr, "Lookup %s %08x level %d\n", pName->getUnwrapped(), v, level);
-            if (v) {
-                fprintf(stderr, "  %08x %08x\n", v->pAddress, v->pForward);
-            }
-#endif
-            return v;
-        }
-
-        VariableInfo* addLocal(String* pName) {
-            int len = mStack.len();
-            if (mStack.get(len-1).level != mLevel) {
-                addEntry();
-                len++;
-            }
-            return addImp(len-1, pName);
-        }
-
-        VariableInfo* addGlobal(String* pName) {
-            return addImp(0, pName);
-        }
-
-        void forEachGlobal(
-            bool (*callback)(String* key, VariableInfo* value, void* context),
-            void* context) {
-            mStack.get(0).pTable->forEach(callback, context);
         }
 
     private:
-        VariableInfo* addImp(int entryIndex, String* pName) {
-            Entry e = mStack.get(entryIndex);
-            SymbolTable* pTable = e.pTable;
-            if (pTable->contains(pName)) {
-                return NULL;
-            }
-            VariableInfo* v = new VariableInfo();
-
-            delete pTable->put(pName, v);
-#if 0
-            fprintf(stderr, "Add \"%s\" %08x level %d\n", pName->getUnwrapped(), v, e.level);
-#endif
-            return v;
+        inline size_t level() {
+            return mLevelStack.size();
         }
 
-        void addEntry() {
-            Entry e;
-            e.level = mLevel;
-            e.pTable = new SymbolTable();
-            mStack.push(e);
-        }
-
-        struct Entry {
-            Entry() {
-                level = 0;
-                pTable = NULL;
-            }
-            int level;
-            SymbolTable* pTable;
+        struct Mark {
+            Arena::Mark mArenaMark;
+            size_t mSymbolHead;
         };
 
-        int mLevel;
-        Array<Entry> mStack;
+        Arena* mpArena;
+        TokenTable* mpTokenTable;
+        Vector<VariableInfo*> mStack;
+        Vector<Mark> mLevelStack;
     };
 
     int ch; // Current input character, or EOF
-    intptr_t tok;     // token
+    tokenid_t tok;      // token
     intptr_t tokc;    // token extra info
     int tokl;         // token operator level
     intptr_t rsym; // return symbol
@@ -1814,16 +1865,25 @@
     char* dptr; // Macro state: Points to macro text during macro playback.
     int dch;    // Macro state: Saves old value of ch during a macro playback.
     char* pGlobalBase;
-    KeywordTable mKeywords;
-    SymbolStack mSymbolTable;
+
+    // Arena for the duration of the compile
+    Arena mGlobalArena;
+    // Arena for data that's only needed when compiling a single function
+    Arena mLocalArena;
+
+    TokenTable mTokenTable;
+    SymbolStack mGlobals;
+    SymbolStack mLocals;
+
+    Type* mkpInt;
+    Type* mkpChar;
+    Type* mkpVoid;
+
     InputStream* file;
 
     CodeBuf codeBuf;
     CodeGenerator* pGen;
 
-    MacroTable mMacros;
-    Array<InputState> mInputStateStack;
-
     String mErrorBuf;
 
     String mPragmas;
@@ -1837,25 +1897,51 @@
     // 3..255 are character and/or operators
 
     // Keywords start at 0x100 and increase by 1
-    static const int TOK_KEYWORD = 0x100;
-    static const int TOK_INT = TOK_KEYWORD + 0;
-    static const int TOK_CHAR = TOK_KEYWORD + 1;
-    static const int TOK_VOID = TOK_KEYWORD + 2;
-    static const int TOK_IF = TOK_KEYWORD + 3;
-    static const int TOK_ELSE = TOK_KEYWORD + 4;
-    static const int TOK_WHILE = TOK_KEYWORD + 5;
-    static const int TOK_BREAK = TOK_KEYWORD + 6;
-    static const int TOK_RETURN = TOK_KEYWORD + 7;
-    static const int TOK_FOR = TOK_KEYWORD + 8;
-    static const int TOK_PRAGMA = TOK_KEYWORD + 9;
-    static const int TOK_DEFINE = TOK_KEYWORD + 10;
-    static const int TOK_UNSUPPORTED_KEYWORD = TOK_KEYWORD + 0xff;
-
-    static const int TOK_UNDEFINED_SYMBOL = 0x200;
-
-    // Symbols start at 0x300, but are really pointers to VariableInfo structs.
-    static const int TOK_SYMBOL = 0x300;
-
+    // Order has to match string list in "internKeywords".
+    enum {
+        TOK_KEYWORD = TokenTable::TOKEN_BASE,
+        TOK_INT = TOK_KEYWORD,
+        TOK_CHAR,
+        TOK_VOID,
+        TOK_IF,
+        TOK_ELSE,
+        TOK_WHILE,
+        TOK_BREAK,
+        TOK_RETURN,
+        TOK_FOR,
+        TOK_PRAGMA,
+        TOK_DEFINE,
+        TOK_AUTO,
+        TOK_CASE,
+        TOK_CONST,
+        TOK_CONTINUE,
+        TOK_DEFAULT,
+        TOK_DO,
+        TOK_DOUBLE,
+        TOK_ENUM,
+        TOK_EXTERN,
+        TOK_FLOAT,
+        TOK_GOTO,
+        TOK_LONG,
+        TOK_REGISTER,
+        TOK_SHORT,
+        TOK_SIGNED,
+        TOK_SIZEOF,
+        TOK_STATIC,
+        TOK_STRUCT,
+        TOK_SWITCH,
+        TOK_TYPEDEF,
+        TOK_UNION,
+        TOK_UNSIGNED,
+        TOK_VOLATILE,
+        TOK__BOOL,
+        TOK__COMPLEX,
+        TOK__IMAGINARY,
+        TOK_INLINE,
+        TOK_RESTRICT,
+        // Symbols start after tokens
+        TOK_SYMBOL
+    };
 
     static const int LOCAL = 0x200;
 
@@ -1896,6 +1982,38 @@
     static const char* operatorChars;
     static const char operatorLevel[];
 
+    /* Called when we detect an internal problem. Does nothing in production.
+     *
+     */
+    void internalError() {
+        * (char*) 0 = 0;
+    }
+
+    void assert(bool isTrue) {
+        if (!isTrue) {
+            internalError();
+        }
+    }
+
+    VariableInfo* VI(tokenid_t t) {
+        if ( t < TOK_SYMBOL || ((size_t) (t-TOK_SYMBOL)) >= mTokenTable.size()) {
+            internalError();
+        }
+        VariableInfo* pV = mTokenTable[t].mpVariableInfo;
+        if (pV && pV->tok != t) {
+            internalError();
+        }
+        return pV;
+    }
+
+    inline bool isDefined(tokenid_t t) {
+        return t >= TOK_SYMBOL && VI(t) != 0;
+    }
+
+    inline const char* nameof(tokenid_t t) {
+        return mTokenTable[t].pText;
+    }
+
     void pdef(int t) {
         mTokenString.append(t);
     }
@@ -2037,27 +2155,16 @@
                 tokc = strtol(mTokenString.getUnwrapped(), 0, 0);
                 tok = TOK_NUM;
             } else {
+                tok = mTokenTable.intern(mTokenString.getUnwrapped(),
+                                         mTokenString.len());
                 // Is this a macro?
-                String* pValue = mMacros.get(&mTokenString);
-                if (pValue) {
+                char* pMacroDefinition = mTokenTable[tok].mpMacroDefinition;
+                if(pMacroDefinition) {
                     // Yes, it is a macro
-                    dptr = pValue->getUnwrapped();
+                    dptr = pMacroDefinition;
                     dch = ch;
                     inp();
                     next();
-                } else {
-                    // Is this a keyword?
-                    int kwtok = mKeywords.get(&mTokenString);
-                    if (kwtok) {
-                        tok = kwtok;
-                        // fprintf(stderr, "tok= keyword %s %x\n", last_id, tok);
-                    } else {
-                        tok = (intptr_t) mSymbolTable.get(&mTokenString);
-                        if (!tok) {
-                            tok = TOK_UNDEFINED_SYMBOL;
-                        }
-                        // fprintf(stderr, "tok= symbol %s %x\n", last_id, tok);
-                    }
                 }
             }
         } else {
@@ -2119,40 +2226,20 @@
         }
 #if 0
         {
-            const char* p;
-
-            printf("tok=0x%x ", tok);
-            if (tok >= TOK_KEYWORD) {
-                printf("'");
-                if (tok>= TOK_SYMBOL)
-                    p = sym_stk + 1 + ((char*) tok - (char*) pVarsBase) / 8;
-                else {
-                    p = mKeywords.lookupKeyFor(tok);
-                    if (!p) {
-                        p = "unknown keyword";
-                    }
-                }
-                while (*p != TAG_TOK && *p)
-                printf("%c", *p++);
-                printf("'\n");
-            } else if (tok == TOK_NUM) {
-                printf("%d\n", tokc);
-            } else {
-                printf("'%c'\n", tok);
-            }
+            String buf;
+            decodeToken(buf, tok);
+            fprintf(stderr, "%s\n", buf.getUnwrapped());
         }
 #endif
     }
 
     void doDefine() {
+        next();
+        tokenid_t name = tok;
         String* pName = new String();
         while (isspace(ch)) {
             inp();
         }
-        while (isid()) {
-            pName->append(ch);
-            inp();
-        }
         if (ch == '(') {
             delete pName;
             error("Defines with arguments not supported");
@@ -2161,12 +2248,15 @@
         while (isspace(ch)) {
             inp();
         }
-        String* pValue = new String();
+        String value;
         while (ch != '\n' && ch != EOF) {
-            pValue->append(ch);
+            value.append(ch);
             inp();
         }
-        delete mMacros.put(pName, pValue);
+        char* pDefn = (char*)mGlobalArena.alloc(value.len() + 1);
+        memcpy(pDefn, value.getUnwrapped(), value.len());
+        pDefn[value.len()] = 0;
+        mTokenTable[name].mpMacroDefinition = pDefn;
     }
 
     void doPragma() {
@@ -2226,6 +2316,14 @@
         next();
     }
 
+    bool accept(intptr_t c) {
+        if (tok == c) {
+            next();
+            return true;
+        }
+        return false;
+    }
+
     /* l is one if '=' parsing wanted (quick hack) */
     void unary(intptr_t l) {
         intptr_t n, t, a;
@@ -2291,7 +2389,7 @@
                     pGen->loadR0FromR0(t == TOK_INT);
                 }
             } else if (t == '&') {
-                pGen->leaR0(*(int *) tok);
+                pGen->leaR0((int) VI(tok)->pAddress);
                 next();
             } else if (t == EOF ) {
                 error("Unexpected EOF.");
@@ -2299,17 +2397,17 @@
                 // Don't have to do anything special here, the error
                 // message was printed by checkSymbol() above.
             } else {
-                if (t == TOK_UNDEFINED_SYMBOL) {
-                    t = (intptr_t) mSymbolTable.addGlobal(
-                        new String(tString));
+                if (!isDefined(t)) {
+                    mGlobals.add(t);
+                    // printf("Adding new global function %s\n", nameof(t));
                 }
 
-                n = (intptr_t) ((VariableInfo*) t)->pAddress;
+                n = (intptr_t) VI(t)->pAddress;
                 /* forward reference: try dlsym */
                 if (!n) {
                     n = (intptr_t) dlsym(RTLD_DEFAULT,
                                          tString.getUnwrapped());
-                    ((VariableInfo*) t)->pAddress = (void*) n;
+                    VI(t)->pAddress = (void*) n;
                 }
                 if ((tok == '=') & l) {
                     /* assignment */
@@ -2349,8 +2447,8 @@
             skip(')');
             if (!n) {
                 /* forward reference */
-                t = t + 4;
-                *(int *) t = pGen->callForward(*(int *) t);
+                VariableInfo* pVI = VI(t);
+                pVI->pForward = (void*) pGen->callForward((int) pVI->pForward);
             } else if (n == 1) {
                 pGen->callIndirect(l);
             } else {
@@ -2459,14 +2557,14 @@
             pGen->gsym(a);
         } else if (tok == '{') {
             if (! outermostFunctionBlock) {
-                mSymbolTable.pushLevel();
+                mLocals.pushLevel();
             }
             next();
             while (tok != '}' && tok != EOF)
                 block(l, false);
             skip('}');
             if (! outermostFunctionBlock) {
-                mSymbolTable.popLevel();
+                mLocals.popLevel();
             }
         } else {
             if (tok == TOK_RETURN) {
@@ -2483,102 +2581,223 @@
         }
     }
 
-    typedef int Type;
-    static const Type TY_UNKNOWN = 0;
-    static const Type TY_INT = 1;
-    static const Type TY_CHAR = 2;
-    static const Type TY_VOID = 3;
-    static const int TY_BASE_TYPE_MASK = 0xf;
-    static const int TY_INDIRECTION_MASK = 0xf0;
-    static const int TY_INDIRECTION_SHIFT = 4;
-    static const int MAX_INDIRECTION_COUNT = 15;
+    enum TypeTag {
+        TY_INT, TY_CHAR, TY_VOID, TY_POINTER, TY_FUNC, TY_PARAM
+    };
 
-    Type getBaseType(Type t) {
-        return t & TY_BASE_TYPE_MASK;
+    struct Type {
+        TypeTag tag;
+        tokenid_t id; // For function arguments
+        Type* pHead;
+        Type* pTail;
+    };
+
+    Type* createType(TypeTag tag, Type* pHead, Type* pTail, Arena& arena) {
+        assert(tag >= TY_INT && tag <= TY_PARAM);
+        Type* pType = (Type*) arena.alloc(sizeof(Type));
+        memset(pType, 0, sizeof(*pType));
+        pType->tag = tag;
+        pType->pHead = pHead;
+        pType->pTail = pTail;
+        return pType;
     }
 
-    int getIndirectionCount(Type t) {
-        return (TY_INDIRECTION_MASK & t) >> TY_INDIRECTION_SHIFT;
+    void decodeType(String& buffer, Type* pType) {
+        if (pType == NULL) {
+            buffer.appendCStr("null");
+            return;
+        }
+        buffer.append('(');
+        String temp;
+        if (pType->id != 0) {
+            decodeToken(temp, pType->id);
+            buffer.append(temp);
+            buffer.append(' ');
+        }
+        bool printHead = false;
+        bool printTail = false;
+        switch (pType->tag) {
+            case TY_INT:
+                buffer.appendCStr("int");
+                break;
+            case TY_CHAR:
+                buffer.appendCStr("char");
+                break;
+            case TY_VOID:
+                buffer.appendCStr("void");
+                break;
+            case TY_POINTER:
+                buffer.appendCStr("*");
+                printHead = true;
+                break;
+            case TY_FUNC:
+                buffer.appendCStr("func");
+                printHead = true;
+                printTail = true;
+                break;
+            case TY_PARAM:
+                buffer.appendCStr("param");
+                printHead = true;
+                printTail = true;
+                break;
+            default:
+                String temp;
+                temp.printf("Unknown tag %d", pType->tag);
+                buffer.append(temp);
+                break;
+        }
+        if (printHead) {
+            buffer.append(' ');
+            decodeType(buffer, pType->pHead);
+        }
+        if (printTail) {
+            buffer.append(' ');
+            decodeType(buffer, pType->pTail);
+        }
+        buffer.append(')');
     }
 
-    void setIndirectionCount(Type& t, int count) {
-        t = ((TY_INDIRECTION_MASK & (count << TY_INDIRECTION_SHIFT))
-                | (t & ~TY_INDIRECTION_MASK));
+    void printType(Type* pType) {
+        String buffer;
+        decodeType(buffer, pType);
+        fprintf(stderr, "%s\n", buffer.getUnwrapped());
     }
 
-    bool acceptType(Type& t) {
-        t = TY_UNKNOWN;
+    Type* acceptPrimitiveType(Arena& arena) {
+        Type* pType;
         if (tok == TOK_INT) {
-            t = TY_INT;
+            pType = mkpInt;
         } else if (tok == TOK_CHAR) {
-            t = TY_CHAR;
+            pType = mkpChar;
         } else if (tok == TOK_VOID) {
-            t = TY_VOID;
+            pType = mkpVoid;
         } else {
-            return false;
+            return NULL;
         }
         next();
-        return true;
+        return pType;
     }
 
-    Type acceptPointerDeclaration(Type& base) {
-        Type t = base;
-        int indirectionCount = 0;
-        while (tok == '*' && indirectionCount <= MAX_INDIRECTION_COUNT) {
+    Type* acceptDeclaration(const Type* pBaseType, Arena& arena) {
+        Type* pType = createType(pBaseType->tag, pBaseType->pHead,
+                                 pBaseType->pTail, arena);
+        tokenid_t declName;
+        if (pType) {
+            pType = acceptDecl2(pType, declName, arena);
+            pType->id = declName;
+            // fprintf(stderr, "Parsed a declaration:       ");
+            // printType(pType);
+        }
+        return pType;
+    }
+
+    Type* expectDeclaration(const Type* pBaseType, Arena& arena) {
+        Type* pType = acceptDeclaration(pBaseType, arena);
+        if (! pType) {
+            error("Expected a declaration");
+        }
+        return pType;
+    }
+
+    Type* acceptDecl2(Type* pType, tokenid_t& declName, Arena& arena) {
+        while (tok == '*') {
+            pType = createType(TY_POINTER, pType, NULL, arena);
             next();
-            indirectionCount++;
         }
-        if (indirectionCount > MAX_INDIRECTION_COUNT) {
-            error("Too many levels of pointer. Max %d", MAX_INDIRECTION_COUNT);
-        }
-        setIndirectionCount(t, indirectionCount);
-        return t;
+        pType = acceptDecl3(pType, declName, arena);
+        return pType;
     }
 
-    void expectType(Type& t) {
-        if (!acceptType(t)) {
-            error("Expected a type.");
+    Type* acceptDecl3(Type* pType, tokenid_t& declName, Arena& arena) {
+        if (accept('(')) {
+            pType = acceptDecl2(pType, declName, arena);
+            skip(')');
+        } else {
+            declName = acceptSymbol();
         }
-    }
-
-    void addGlobalSymbol() {
-        tok = (intptr_t) mSymbolTable.addGlobal(
-            new String(mTokenString));
-        reportIfDuplicate();
-    }
-
-    void reportIfDuplicate() {
-        if (!tok) {
-            error("Duplicate definition of %s", mTokenString.getUnwrapped());
+        while (tok == '(') {
+            // Function declaration
+            skip('(');
+            Type* pTail = acceptArgs(arena);
+            pType = createType(TY_FUNC, pType, pTail, arena);
+            skip(')');
         }
+        return pType;
     }
 
-    void addLocalSymbol() {
-        tok = (intptr_t) mSymbolTable.addLocal(
-                new String(mTokenString));
-        reportIfDuplicate();
+    Type* acceptArgs(Arena& arena) {
+        Type* pHead = NULL;
+        Type* pTail = NULL;
+        for(;;) {
+            Type* pBaseArg = acceptPrimitiveType(arena);
+            if (pBaseArg) {
+                Type* pArg = acceptDeclaration(pBaseArg, arena);
+                if (pArg) {
+                    Type* pParam = createType(TY_PARAM, pArg, NULL, arena);
+                    if (!pHead) {
+                        pHead = pParam;
+                        pTail = pParam;
+                    } else {
+                        pTail->pTail = pParam;
+                        pTail = pParam;
+                    }
+                }
+            }
+            if (! accept(',')) {
+                break;
+            }
+        }
+        return pHead;
+    }
+
+    Type* expectPrimitiveType(Arena& arena) {
+        Type* pType = acceptPrimitiveType(arena);
+        if (!pType) {
+            String buf;
+            decodeToken(buf, tok);
+            error("Expected a type, got %s", buf.getUnwrapped());
+        }
+        return pType;
+    }
+
+    void addGlobalSymbol(Type* pDecl) {
+        tokenid_t t = pDecl->id;
+        VariableInfo* pVI = VI(t);
+        if(pVI && pVI->pAddress) {
+            reportDuplicate(t);
+        }
+        mGlobals.add(pDecl);
+    }
+
+    void reportDuplicate(tokenid_t t) {
+        error("Duplicate definition of %s", nameof(t));
+    }
+
+    void addLocalSymbol(Type* pDecl) {
+        tokenid_t t = pDecl->id;
+        if (mLocals.isDefinedAtCurrentLevel(t)) {
+            reportDuplicate(t);
+        }
+        mLocals.add(pDecl);
     }
 
     void localDeclarations() {
         intptr_t a;
-        Type base;
+        Type* pBaseType;
 
-        while (acceptType(base)) {
+        while ((pBaseType = acceptPrimitiveType(mLocalArena)) != NULL) {
             while (tok != ';' && tok != EOF) {
-                Type t = acceptPointerDeclaration(t);
-                int variableAddress = 0;
-                if (checkSymbol()) {
-                    addLocalSymbol();
-                    if (tok) {
-                        loc = loc + 4;
-                        variableAddress = -loc;
-                        ((VariableInfo*) tok)->pAddress = (void*) variableAddress;
-                    }
+                Type* pDecl = expectDeclaration(pBaseType, mLocalArena);
+                if (!pDecl) {
+                    break;
                 }
-                next();
-                if (tok == '=') {
+                int variableAddress = 0;
+                addLocalSymbol(pDecl);
+                loc = loc + 4;
+                variableAddress = -loc;
+                VI(pDecl->id)->pAddress = (void*) variableAddress;
+                if (accept('=')) {
                     /* assignment */
-                    next();
                     expr();
                     pGen->storeR0(variableAddress);
                 }
@@ -2593,22 +2812,42 @@
         return checkSymbol(tok, &mTokenString);
     }
 
-    bool checkSymbol(int token, String* pText) {
-        bool result = token < EOF || token >= TOK_UNDEFINED_SYMBOL;
+    void decodeToken(String& buffer, tokenid_t token) {
+        if (token == EOF ) {
+            buffer.printf("EOF");
+        } else if (token == TOK_NUM) {
+            buffer.printf("numeric constant");
+        } else if (token >= 0 && token < 256) {
+            if (token < 32) {
+                buffer.printf("'\\x%02x'", token);
+            } else {
+                buffer.printf("'%c'", token);
+            }
+        } else if (token >= TOK_KEYWORD && token < TOK_SYMBOL) {
+            buffer.printf("keyword \"%s\"", nameof(token));
+        } else {
+            buffer.printf("symbol \"%s\"", nameof(token));
+        }
+    }
+
+    bool checkSymbol(tokenid_t token, String* pText) {
+        bool result = token >= TOK_SYMBOL;
         if (!result) {
             String temp;
-            if (token == EOF ) {
-                temp.printf("EOF");
-            } else if (token == TOK_NUM) {
-                temp.printf("numeric constant");
-            } else if (token >= 0 && token < 256) {
-                temp.printf("char \'%c\'", token);
-            } else if (token >= TOK_KEYWORD && token < TOK_UNSUPPORTED_KEYWORD) {
-                temp.printf("keyword \"%s\"", pText->getUnwrapped());
-            } else {
-                temp.printf("reserved keyword \"%s\"",
-                            pText->getUnwrapped());
-            }
+            decodeToken(temp, token);
+            error("Expected symbol. Got %s", temp.getUnwrapped());
+        }
+        return result;
+    }
+
+    tokenid_t acceptSymbol() {
+        tokenid_t result = 0;
+        if (tok >= TOK_SYMBOL) {
+            result = tok;
+            next();
+        } else {
+            String temp;
+            decodeToken(temp, tok);
             error("Expected symbol. Got %s", temp.getUnwrapped());
         }
         return result;
@@ -2616,30 +2855,28 @@
 
     void globalDeclarations() {
         while (tok != EOF) {
-            Type base;
-            expectType(base);
-            Type t = acceptPointerDeclaration(t);
-            if (tok >=  0 && tok < TOK_UNDEFINED_SYMBOL) {
-                error("Unexpected token %d", tok);
+            Type* pBaseType = expectPrimitiveType(mGlobalArena);
+            if (!pBaseType) {
                 break;
             }
-            if (tok == TOK_UNDEFINED_SYMBOL) {
-                addGlobalSymbol();
+            Type* pDecl = expectDeclaration(pBaseType, mGlobalArena);
+            if (!pDecl) {
+                break;
             }
-            VariableInfo* name = (VariableInfo*) tok;
+            if (! isDefined(pDecl->id)) {
+                addGlobalSymbol(pDecl);
+            }
+            VariableInfo* name = VI(pDecl->id);
             if (name && name->pAddress) {
-                error("Already defined global %s",
-                      mTokenString.getUnwrapped());
+                error("Already defined global %s", nameof(pDecl->id));
             }
-            next();
-            if (tok == ',' || tok == ';' || tok == '=') {
+            if (pDecl->tag < TY_FUNC) {
                 // it's a variable declaration
                 for(;;) {
-                    if (name) {
+                    if (name && !name->pAddress) {
                         name->pAddress = (int*) allocGlobalSpace(4);
                     }
-                    if (tok == '=') {
-                        next();
+                    if (accept('=')) {
                         if (tok == TOK_NUM) {
                             if (name) {
                                 * (int*) name->pAddress = tokc;
@@ -2649,17 +2886,21 @@
                             error("Expected an integer constant");
                         }
                     }
-                    if (tok != ',') {
+                    if (!accept(',')) {
                         break;
                     }
-                    skip(',');
-                    t = acceptPointerDeclaration(t);
-                    addGlobalSymbol();
-                    name = (VariableInfo*) tok;
-                    next();
+                    pDecl = expectDeclaration(pBaseType, mGlobalArena);
+                    if (!pDecl) {
+                        break;
+                    }
+                    if (! isDefined(pDecl->id)) {
+                        addGlobalSymbol(pDecl);
+                    }
+                    name = VI(pDecl->id);
                 }
                 skip(';');
             } else {
+                // Function declaration
                 if (name) {
                     /* patch forward references (XXX: does not work for function
                      pointers) */
@@ -2667,34 +2908,24 @@
                     /* put function address */
                     name->pAddress = (void*) codeBuf.getPC();
                 }
-                skip('(');
-                mSymbolTable.pushLevel();
+                // Calculate stack offsets for parameters
+                mLocals.pushLevel();
                 intptr_t a = 8;
                 int argCount = 0;
-                while (tok != ')' && tok != EOF) {
-                    Type aType;
-                    expectType(aType);
-                    aType = acceptPointerDeclaration(aType);
-                    if (checkSymbol()) {
-                        addLocalSymbol();
-                        if (tok) {
-                            /* read param name and compute offset */
-                            *(int *) tok = a;
-                            a = a + 4;
-                        }
-                    }
-                    next();
-                    if (tok == ',')
-                        next();
+                for (Type* pP = pDecl->pTail; pP; pP = pP->pTail) {
+                    Type* pArg = pP->pHead;
+                    addLocalSymbol(pArg);
+                    /* read param name and compute offset */
+                    VI(pArg->id)->pAddress = (void*) a;
+                    a = a + 4;
                     argCount++;
                 }
-                skip(')');
                 rsym = loc = 0;
                 a = pGen->functionEntry(argCount);
                 block(0, true);
                 pGen->gsym(rsym);
                 pGen->functionExit(argCount, a, loc);
-                mSymbolTable.popLevel();
+                mLocals.popLevel();
             }
         }
     }
@@ -2795,6 +3026,14 @@
 
         cleanup();
         clear();
+        mTokenTable.setArena(&mGlobalArena);
+        mGlobals.setArena(&mGlobalArena);
+        mGlobals.setTokenTable(&mTokenTable);
+        mLocals.setArena(&mLocalArena);
+        mLocals.setTokenTable(&mTokenTable);
+
+        internKeywords();
+        createPrimitiveTypes();
         codeBuf.init(ALLOC_SIZE);
         setArchitecture(NULL);
         if (!pGen) {
@@ -2821,23 +3060,25 @@
         return result;
     }
 
+    void createPrimitiveTypes() {
+        mkpInt = createType(TY_INT, NULL, NULL, mGlobalArena);
+        mkpChar = createType(TY_CHAR, NULL, NULL, mGlobalArena);
+        mkpVoid = createType(TY_VOID, NULL, NULL, mGlobalArena);
+    }
+
     void checkForUndefinedForwardReferences() {
-        mSymbolTable.forEachGlobal(static_ufrcFn, this);
+        mGlobals.forEach(static_ufrcFn, this);
     }
 
-    static bool static_ufrcFn(String* key, VariableInfo* value,
-                                                 void* context) {
+    static bool static_ufrcFn(VariableInfo* value, void* context) {
         Compiler* pCompiler = (Compiler*) context;
-        return pCompiler->undefinedForwardReferenceCheck(key, value);
+        return pCompiler->undefinedForwardReferenceCheck(value);
     }
 
-    bool undefinedForwardReferenceCheck(String* key, VariableInfo* value) {
-#if 0
-        fprintf(stderr, "%s 0x%8x 0x%08x\n", key->getUnwrapped(),
-                value->pAddress, value->pForward);
-#endif
+    bool undefinedForwardReferenceCheck(VariableInfo* value) {
         if (!value->pAddress && value->pForward) {
-            error("Undefined forward reference: %s", key->getUnwrapped());
+            error("Undefined forward reference: %s",
+                  mTokenTable[value->tok].pText);
         }
         return true;
     }
@@ -2855,8 +3096,8 @@
      * If found, return its value.
      */
     void* lookup(const char* name) {
-        String string(name, -1, false);
-        VariableInfo* pVariableInfo = mSymbolTable.get(&string);
+        tokenid_t tok = mTokenTable.intern(name, strlen(name));
+        VariableInfo* pVariableInfo = VI(tok);
         if (pVariableInfo) {
             return pVariableInfo->pAddress;
         }
diff --git a/libacc/tests/test b/libacc/tests/test
new file mode 100755
index 0000000..ef10500
--- /dev/null
+++ b/libacc/tests/test
@@ -0,0 +1,6 @@
+#!/bin/bash
+
+SCRIPT_DIR=`dirname $BASH_SOURCE`
+cd $SCRIPT_DIR
+python test.py
+
diff --git a/libacc/tests/test.py b/libacc/tests/test.py
new file mode 100644
index 0000000..ef5963b
--- /dev/null
+++ b/libacc/tests/test.py
@@ -0,0 +1,114 @@
+#
+# Test the acc compiler
+
+import unittest
+import subprocess
+import os
+
+def compile(args):
+    proc = subprocess.Popen(["acc"] + args, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
+    result = proc.communicate()
+    return result
+
+def runCmd(args):
+    proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+    result = proc.communicate()
+    return result[0].strip()
+
+def which(item):
+    return runCmd(["which", item])
+
+def fileType(item):
+    return runCmd(["file", item])
+
+def outputCanRun():
+    ft = fileType(which("acc"))
+    return ft.find("ELF 32-bit LSB executable, Intel 80386") >= 0
+
+def adb(args):
+    return runCmd(["adb"] + args)
+
+gArmInitialized = False
+
+def setupArm():
+    if gArmInitialized:
+        return
+    print "Setting up arm"
+    adb(["remount"])
+    adb(["shell", "rm", "/system/bin/acc"])
+    adb(["shell", "mkdir", "/system/bin/accdata"])
+    adb(["shell", "mkdir", "/system/bin/accdata/data"])
+    # Clear out old data TODO: handle recursion
+    adb(["shell", "rm", "/system/bin/accdata/data/*"])
+    # Copy over data
+    for root, dirs, files in os.walk("data"):
+        for d in dirs:
+            adb(["shell", "mkdir", os.path.join(root, d)])
+        for f in files:
+            adb(["push", os.path.join(root, f), os.path.join("/system/bin/accdata", root, f)])
+    # Copy over compiler
+    adb(["sync"])
+    gArmInitialied = True
+
+def compileArm(args):
+    setupArm()
+    proc = subprocess.Popen(["adb", "shell", "/system/bin/acc"] + args, stdout=subprocess.PIPE)
+    result = proc.communicate()
+    return result[0].replace("\r","")
+
+def compare(a, b):
+    if a != b:
+        firstDiff = firstDifference(a,b)
+        print "Strings differ at character", firstDiff, a[firstDiff], b[firstDiff]
+
+def firstDifference(a, b):
+    commonLen = min(len(a), len(b))
+    for i in xrange(0, commonLen):
+        if a[i] != b[i]:
+            return i
+    return commonLen
+
+class TestACC(unittest.TestCase):
+ 
+    def compileCheck(self, args, stdErrResult, stdOutResult=""):
+        out, err = compile(args)
+        compare(out, stdOutResult)
+        compare(err, stdErrResult)
+        self.assertEqual(out, stdOutResult)
+        self.assertEqual(err, stdErrResult)
+
+    def compileCheckArm(self, args, result):
+        self.assertEqual(compileArm(args), result)
+
+    def testCompileReturnVal(self):
+        self.compileCheck(["data/returnval-ansi.c"], "") 
+
+    def testCompileReturnVal(self):
+        self.compileCheck(["data/otcc-ansi.c"], "")
+
+    def testRunReturnVal(self):
+        self.compileCheck(["-R", "data/returnval-ansi.c"],
+		"Executing compiled code:\nresult: 42\n")
+    def testRunOTCCANSI(self):
+        self.compileCheck(["-R", "data/otcc-ansi.c", "data/returnval.c"], 
+            "Executing compiled code:\notcc-ansi.c: About to execute compiled code:\natcc-ansi.c: result: 42\nresult: 42\n")
+
+    def testRunOTCCANSI2(self):
+        self.compileCheck(["-R", "data/otcc-ansi.c", "data/otcc.c", "data/returnval.c"], 
+            "Executing compiled code:\notcc-ansi.c: About to execute compiled code:\notcc.c: about to execute compiled code.\natcc-ansi.c: result: 42\nresult: 42\n")
+
+    def testRunConstants(self):
+        self.compileCheck(["-R", "data/constants.c"],
+            "Executing compiled code:\nresult: 12\n",
+            "0 = 0\n010 = 8\n0x10 = 16\n'\\a' = 7\n'\\b' = 8\n'\\f' = 12\n'\\n' = 10\n'\\r' = 13\n'\\t' = 9\n'\\v' = 11\n'\\\\' = 92\n'\\'' = 39\n" +
+            "'\\\"' = 34\n'\\?' = 63\n'\\0' = 0\n'\\1' = 1\n'\\12' = 10\n'\\123' = 83\n'\\x0' = 0\n'\\x1' = 1\n'\\x12' = 18\n'\\x123' = 291\n'\\x1f' = 31\n'\\x1F' = 31\n")
+
+    def testArmRunReturnVal(self):
+        self.compileCheckArm(["-R", "/system/bin/accdata/data/returnval-ansi.c"],
+            "Executing compiled code:\nresult: 42\n")
+
+if __name__ == '__main__':
+    if not outputCanRun():
+        print "Many tests are expected to fail, because acc is not a 32-bit x86 Linux executable."
+    unittest.main()
+
diff --git a/libacc/tests/testarm b/libacc/tests/testarm
deleted file mode 100755
index 1d4b866..0000000
--- a/libacc/tests/testarm
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/bin/sh
-adb remount
-adb shell rm /system/bin/acc
-adb push data/returnval-ansi.c /system/bin/returnval-ansi.c
-cd ..
-mm -j8
-cd tests
-adb sync
-adb shell /system/bin/acc -R -S /system/bin/returnval-ansi.c
diff --git a/libacc/tests/testlocal b/libacc/tests/testlocal
deleted file mode 100755
index 1a0b4c5..0000000
--- a/libacc/tests/testlocal
+++ /dev/null
@@ -1,23 +0,0 @@
-#!/bin/bash
-
-SCRIPT_DIR=`dirname $BASH_SOURCE`
-DATA=$SCRIPT_DIR/data
-ACC=`which acc`
-
-echo "Compiling returnval-ansi.c"
-$ACC -S $DATA/returnval-ansi.c
-
-echo "Compiling whole compiler."
-$ACC -S "$DATA/otcc-ansi.c"
-
-if file $ACC | grep -q "ELF 32-bit LSB executable, Intel 80386"; then
-	echo "Linux 32bit Intel."
-	echo "TESTING returnval-ansi.c:"
-        $ACC -R $DATA/returnval-ansi.c
-        echo TESTING otcc-ansi.c returnval-ansi.c
-	$ACC -R "$DATA/otcc-ansi.c" "$DATA/returnval.c"
-	echo TESTING otcc-ansi.c otcc.c returnval-ansi.c
-	$ACC -R $DATA/otcc-ansi.c $DATA/otcc.c $DATA/returnval.c
-fi
-
-echo "Done with tests."
diff --git a/rootdir/Android.mk b/rootdir/Android.mk
index 44e343c..e66b1c3 100644
--- a/rootdir/Android.mk
+++ b/rootdir/Android.mk
@@ -6,16 +6,9 @@
 copy_from := \
 	etc/dbus.conf \
 	etc/init.goldfish.sh \
-	etc/ppp/ip-up \
-	etc/ppp/ip-down \
-	etc/ppp/peers/common \
-	etc/racoon/racoon.conf \
+	etc/ppp/ip-up-vpn \
 	etc/hosts
 
-dont_copy := \
-	etc/init.gprs-pppd \
-	etc/ppp/chap-secrets
-
 copy_to := $(addprefix $(TARGET_OUT)/,$(copy_from))
 copy_from := $(addprefix $(LOCAL_PATH)/,$(copy_from))
 
diff --git a/rootdir/etc/init.gprs-pppd b/rootdir/etc/init.gprs-pppd
deleted file mode 100755
index 521eec9..0000000
--- a/rootdir/etc/init.gprs-pppd
+++ /dev/null
@@ -1,23 +0,0 @@
-#!/system/bin/sh
-# An unforunate wrapper script 
-# so that the exit code of pppd may be retrieved
-
-
-# this is a workaround for issue #651747
-#trap "/system/bin/sleep 1;exit 0" TERM
-
-
-PPPD_PID=
-
-/system/bin/setprop "net.gprs.ppp-exit" ""
-
-/system/bin/log -t pppd "Starting pppd"
-
-/system/bin/pppd $*
-
-PPPD_EXIT=$?
-PPPD_PID=$!
-
-/system/bin/log -t pppd "pppd exited with $PPPD_EXIT"
-
-/system/bin/setprop "net.gprs.ppp-exit" "$PPPD_EXIT"
diff --git a/rootdir/etc/ppp/chap-secrets b/rootdir/etc/ppp/chap-secrets
deleted file mode 100644
index 6546b0f..0000000
--- a/rootdir/etc/ppp/chap-secrets
+++ /dev/null
@@ -1,2 +0,0 @@
-* * bogus
-
diff --git a/rootdir/etc/ppp/ip-down b/rootdir/etc/ppp/ip-down
deleted file mode 100755
index 58d21e5..0000000
--- a/rootdir/etc/ppp/ip-down
+++ /dev/null
@@ -1 +0,0 @@
-#!/system/bin/sh
diff --git a/rootdir/etc/ppp/ip-up b/rootdir/etc/ppp/ip-up
deleted file mode 100755
index 8c8f12c..0000000
--- a/rootdir/etc/ppp/ip-up
+++ /dev/null
@@ -1,33 +0,0 @@
-#!/system/bin/sh
-
-# Use interface name if linkname is not available
-
-/system/bin/setprop "net.dns1" "$DNS1"
-/system/bin/setprop "net.dns2" "$DNS2"
-
-# Retrieve the default gateway from /proc/net/route
-RTAB=`cat /proc/net/route`
-flag=-1; i=0;
-for l in $RTAB; do
-    if (exp flag==1) then DGW=$l; flag=0; fi;
-    if (exp i%11 == 1) then
-        if (exp $l=="00000000") then flag=1; fi;
-    fi;
-    i=`exp i+1`;
-done
-FH=${DGW%????}
-LH=${DGW#????}
-A=`exp 0x${LH#??}`
-B=`exp 0x${LH%??}`
-C=`exp 0x${FH#??}`
-D=`exp 0x${FH%??}`
-GATEWAY="$A.$B.$C.$D"
-VPNSERVER=`getprop "net.vpn.server_ip"`
-
-# Protect the route to vpn server
-/system/bin/route add -net $VPNSERVER netmask 255.255.255.255 gw $GATEWAY
-
-# Route all traffic to vpn connection
-/system/bin/route add -net 0.0.0.0 netmask 128.0.0.0 gw $IPREMOTE
-/system/bin/route add -net 128.0.0.0 netmask 128.0.0.0 gw $IPREMOTE
-
diff --git a/rootdir/etc/ppp/ip-up-vpn b/rootdir/etc/ppp/ip-up-vpn
new file mode 100755
index 0000000..5d6b611
--- /dev/null
+++ b/rootdir/etc/ppp/ip-up-vpn
@@ -0,0 +1,18 @@
+#!/system/bin/sh
+
+/system/bin/setprop "vpn.dns1" "$DNS1"
+/system/bin/setprop "vpn.dns2" "$DNS2"
+
+GATEWAY=${6#*@}
+VPNSERVER=${6%@*}
+
+# Protect the route to vpn server
+/system/bin/route add -net "$VPNSERVER" netmask 255.255.255.255 gw "$GATEWAY"
+
+if (exp $?) ; then exit $?; fi
+
+# Route all traffic to vpn connection
+/system/bin/route add -net 0.0.0.0 netmask 128.0.0.0 gw $IPREMOTE
+/system/bin/route add -net 128.0.0.0 netmask 128.0.0.0 gw $IPREMOTE
+
+/system/bin/setprop "vpn.up" "1"
diff --git a/rootdir/etc/ppp/peers/common b/rootdir/etc/ppp/peers/common
deleted file mode 100755
index 4183841..0000000
--- a/rootdir/etc/ppp/peers/common
+++ /dev/null
@@ -1,10 +0,0 @@
-ipcp-accept-local
-ipcp-accept-remote
-refuse-eap
-noccp
-noauth
-idle 1800
-mtu 1400
-mru 1400
-nodefaultroute
-usepeerdns
diff --git a/rootdir/etc/racoon/racoon.conf b/rootdir/etc/racoon/racoon.conf
deleted file mode 100644
index eb50a2d..0000000
--- a/rootdir/etc/racoon/racoon.conf
+++ /dev/null
@@ -1,35 +0,0 @@
-#path certificate "";
-path certificate "/";
-
-sainfo anonymous {
-   encryption_algorithm aes, 3des;
-   authentication_algorithm hmac_sha1, hmac_md5;
-   compression_algorithm deflate;
-   lifetime time 3600 sec;
-}
-
-remote anonymous {
-   exchange_mode main;
-   doi ipsec_doi;
-   situation identity_only;
-   ike_frag on;
-   generate_policy on;
-   my_identifier asn1dn;
-   nat_traversal on; # always use NAT-T
-   ca_type x509 "ca.crt";
-   certificate_type x509 "user.crt" "user.key";
-   verify_identifier off;
-   verify_cert on;
-   nonce_size 16;
-   initial_contact on;
-   proposal_check obey;
-
-   proposal {
-      authentication_method rsasig;
-      hash_algorithm sha1;
-      encryption_algorithm 3des;
-      lifetime time 3600 sec;
-      dh_group 2;
-   }
-}
-
diff --git a/rootdir/init.rc b/rootdir/init.rc
index 47acd15..18db10e 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -34,6 +34,7 @@
     write /proc/cpu/alignment 4
     write /proc/sys/kernel/sched_latency_ns 10000000
     write /proc/sys/kernel/sched_wakeup_granularity_ns 2000000
+    write /proc/sys/kernel/sched_compat_yield 1
 
 # Create cgroup mount points for process groups
     mkdir /dev/cpuctl
diff --git a/toolbox/mount.c b/toolbox/mount.c
index 395c943..472c952 100644
--- a/toolbox/mount.c
+++ b/toolbox/mount.c
@@ -226,7 +226,7 @@
 {
 	char *type = NULL;
 	int c;
-	int loop;
+	int loop = 0;
 
 	progname = argv[0];
 	rwflag = MS_VERBOSE;
diff --git a/toolbox/route.c b/toolbox/route.c
index 2fd7108..4f66201 100644
--- a/toolbox/route.c
+++ b/toolbox/route.c
@@ -1,130 +1,103 @@
+/*
+ * Copyright (c) 2009, The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *  * Neither the name of Google, Inc. nor the names of its contributors
+ *    may be used to endorse or promote products derived from this
+ *    software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
 
 #include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdarg.h>
-
-#include <errno.h>
 #include <string.h>
-#include <ctype.h>
-
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
-#include <linux/if.h>
-#include <linux/sockios.h>
 #include <arpa/inet.h>
 #include <linux/route.h>
 
-static void die(const char *fmt, ...)
-{
-    va_list p;
-
-    va_start(p, fmt);
-    fprintf(stderr,"error(%s): ", strerror(errno));
-    fprintf(stderr, fmt, p);
-    va_end(p);
-    exit(-1);
+static inline int set_address(const char *address, struct sockaddr *sa) {
+    return inet_aton(address, &((struct sockaddr_in *)sa)->sin_addr);
 }
 
-static inline void init_sockaddr_in(struct sockaddr_in *sin, const char *addr)
-{
-	sin->sin_family = AF_INET;
-	sin->sin_port = 0;
-	sin->sin_addr.s_addr = inet_addr(addr);
-}
-
-#define ADVANCE(argc, argv) do { argc--, argv++; } while(0)
-#define EXPECT_NEXT(argc, argv) do {        \
-    ADVANCE(argc, argv);                    \
-	if (0 == argc) {  						\
-		errno = EINVAL;                     \
-		die("expecting one more argument"); \
-	}                                       \
-} while(0)		
-
 /* current support the following routing entries */
 /* route add default dev wlan0 */
-/* route add default gw 192.168.20.1 dev wlan0 */
-/* route add net 192.168.1.1 netmask 255.255.255.0 gw 172.24.192.10 */
+/* route add default gw 192.168.1.1 dev wlan0 */
+/* route add -net 192.168.1.2 netmask 255.255.255.0 gw 192.168.1.1 */
 
 int route_main(int argc, char *argv[])
 {
-    struct ifreq ifr;
-    int s,i;
-    struct rtentry rt;
-    struct sockaddr_in ina;
-   
-    if (!argc)
-        return 0;
+    struct rtentry rt = {
+        .rt_dst     = {.sa_family = AF_INET},
+        .rt_genmask = {.sa_family = AF_INET},
+        .rt_gateway = {.sa_family = AF_INET},
+    };
 
-    strncpy(ifr.ifr_name, argv[0], IFNAMSIZ);
-    ifr.ifr_name[IFNAMSIZ-1] = 0;
-    ADVANCE(argc, argv);
+    errno = EINVAL;
+    if (argc > 2 && !strcmp(argv[1], "add")) {
+        if (!strcmp(argv[2], "default")) {
+            /* route add default dev wlan0 */
+            if (argc > 4 && !strcmp(argv[3], "dev")) {
+                rt.rt_flags = RTF_UP | RTF_HOST;
+                rt.rt_dev = argv[4];
+                errno = 0;
+                goto apply;
+            }
 
-    if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
-        die("cannot open control socket\n");
-    }
-
-    while(argc > 0){
-        if (!strcmp(argv[0], "add")) {
-            EXPECT_NEXT(argc, argv);
-            if (!strcmp(argv[0], "default")) {
-                EXPECT_NEXT(argc, argv);
-                memset((char *) &rt, 0, sizeof(struct rtentry));
-                rt.rt_dst.sa_family = AF_INET;	
-                if(!strcmp(argv[0], "dev")) {
-                    EXPECT_NEXT(argc, argv);
-                    rt.rt_flags = RTF_UP | RTF_HOST;
-                    rt.rt_dev = argv[0];
-                    if (ioctl(s, SIOCADDRT, &rt) < 0)
-                        die("SIOCADDRT\n");
-                } else if (!strcmp(argv[0], "gw")) {
-                    EXPECT_NEXT(argc, argv);
-                    rt.rt_flags = RTF_UP | RTF_GATEWAY;
-                    init_sockaddr_in((struct sockaddr_in *)&(rt.rt_genmask), "0.0.0.0");
-                    if(isdigit(argv[0][0])) {
-                        init_sockaddr_in((struct sockaddr_in *)&(rt.rt_gateway), argv[0]);
-                    } else {
-                        die("expecting an IP address for parameter \"gw\"\n");
-                    }
-                    EXPECT_NEXT(argc, argv);
-                    if (!strcmp(argv[0], "dev")) {
-                        EXPECT_NEXT(argc, argv);
-                        rt.rt_dev = argv[0];
-                        if (ioctl(s, SIOCADDRT, &rt) < 0) {
-                            die("SIOCADDRT\n");
-                        }
-                    }
-                }
-            } else {
-                char keywords[3][10] = { "-net", "netmask", "gw" };
-                struct sockaddr_in *paddr[3] = { &rt.rt_dst, &rt.rt_genmask, &rt.rt_gateway };
-                int k = 0;
-
-                memset((char *) &rt, 0, sizeof(struct rtentry));
+            /* route add default gw 192.168.1.1 dev wlan0 */
+            if (argc > 6 && !strcmp(argv[3], "gw") && !strcmp(argv[5], "dev")) {
                 rt.rt_flags = RTF_UP | RTF_GATEWAY;
-                do {
-                     if (!strcmp(argv[0], keywords[k])) {
-                         EXPECT_NEXT(argc, argv);
-                         if (isdigit(argv[0][0])) {
-                             init_sockaddr_in(paddr[k], argv[0]);
-                         } else {
-                            die("expecting an IP/MASK address for parameter %s\n", keywords[k]);
-                         }
-                     if (k < 2)
-                         EXPECT_NEXT(argc, argv);
-                     } else {
-                         die("expecting keyword(s)\n");
-                     }
-                 } while (++k < 3);
-
-                if (ioctl(s, SIOCADDRT, &rt) < 0) {
-                    die("SIOCADDRT\n");
+                rt.rt_dev = argv[6];
+                if (set_address(argv[4], &rt.rt_gateway)) {
+                    errno = 0;
                 }
+                goto apply;
             }
         }
-        ADVANCE(argc, argv);
+
+        /* route add -net 192.168.1.2 netmask 255.255.255.0 gw 192.168.1.1 */
+        if (argc > 7 && !strcmp(argv[2], "-net") &&
+            !strcmp(argv[4], "netmask") && !strcmp(argv[6], "gw")) {
+            rt.rt_flags = RTF_UP | RTF_GATEWAY;
+            if (set_address(argv[3], &rt.rt_dst) &&
+                set_address(argv[5], &rt.rt_genmask) &&
+                set_address(argv[7], &rt.rt_gateway)) {
+                errno = 0;
+            }
+            goto apply;
+        }
     }
 
-    return 0;
+apply:
+    if (!errno) {
+        int s = socket(AF_INET, SOCK_DGRAM, 0);
+        if (s != -1 && (ioctl(s, SIOCADDRT, &rt) != -1 || errno == EEXIST)) {
+            return 0;
+        }
+    }
+    puts(strerror(errno));
+    return errno;
 }
diff --git a/vold/format.c b/vold/format.c
index a1faf7a..d4e2327 100755
--- a/vold/format.c
+++ b/vold/format.c
@@ -39,12 +39,13 @@
     if (!strcmp(type, FORMAT_TYPE_FAT32)) {
         char *args[6];
         args[0] = MKDOSFS_PATH;
-        args[1] = "-c 32";
-        args[2] = "-n 2";
-        args[3] = "-O android";
-        args[4] = devpath;
-        args[5] = NULL;
-        rc = logwrap(5, args, 1);
+        args[1] = "-F 32";
+        args[2] = "-c 32";
+        args[3] = "-n 2";
+        args[4] = "-O android";
+        args[5] = devpath;
+        args[6] = NULL;
+        rc = logwrap(6, args, 1);
     } else {
         char *args[7];
         args[0] = MKE2FS_PATH;
diff --git a/vold/logwrapper.c b/vold/logwrapper.c
index 46f6ed3..8da4892 100644
--- a/vold/logwrapper.c
+++ b/vold/logwrapper.c
@@ -42,7 +42,8 @@
                 buffer[b] = '\0';
             } else if (buffer[b] == '\n') {
                 buffer[b] = '\0';
-                LOG(LOG_INFO, tag, &buffer[a]);
+
+                LOG(LOG_INFO, tag, "%s", &buffer[a]);
                 a = b + 1;
             }
         }
diff --git a/vold/volmgr_vfat.c b/vold/volmgr_vfat.c
index 7833222..7a4e12f 100644
--- a/vold/volmgr_vfat.c
+++ b/vold/volmgr_vfat.c
@@ -26,7 +26,7 @@
 
 #define VFAT_DEBUG 0
 
-static char FSCK_MSDOS_PATH[] = "/system/bin/dosfsck";
+static char FSCK_MSDOS_PATH[] = "/system/bin/fsck_msdos";
 
 int vfat_identify(blkdev_t *dev)
 {
@@ -51,51 +51,28 @@
         return 0;
     }
 
-    do {
+    char *args[5];
+    args[0] = FSCK_MSDOS_PATH;
+    args[1] = "-p";
+    args[2] = "-f";
+    args[3] = blkdev_get_devpath(dev);
+    args[4] = NULL;
+    rc = logwrap(4, args, 1);
+    free(args[3]);
 
-        char *args[6];
-        args[0] = FSCK_MSDOS_PATH;
-        args[1] = "-v";
-
-        if (rw) {
-            args[2] = "-w";
-            args[3] = "-p";
-            args[4] = blkdev_get_devpath(dev);
-            args[5] = NULL;
-            rc = logwrap(5, args, 1);
-            free(args[4]);
-        } else {
-            args[2] = "-n";
-            args[3] = blkdev_get_devpath(dev);
-            args[4] = NULL;
-            rc = logwrap(4, args, 1);
-            free(args[3]);
-        }
-
-        if (rc == 0) {
-            LOG_VOL("Filesystem check completed OK");
-            return 0;
-        } else if (rc == 1) {
-            LOG_VOL("Filesystem check failed (general failure)");
-            return -EINVAL;
-        } else if (rc == 2) {
-            LOG_VOL("Filesystem check failed (invalid usage)");
-            return -EIO;
-        } else if (rc == 4) {
-            LOG_VOL("Filesystem check completed (errors fixed)");
-        } else if (rc == 6) {
-            LOG_VOL("Filesystem read-only - retrying check RO");
-            rw = false;
-            continue;
-        } else if (rc == 8) {
-            LOG_VOL("Filesystem check failed (not a FAT filesystem)");
-            return -ENODATA;
-        } else {
-            LOG_VOL("Filesystem check failed (unknown exit code %d)", rc);
-            return -EIO;
-        }
-    } while (0);
-
+    if (rc == 0) {
+        LOG_VOL("Filesystem check completed OK");
+        return 0;
+    } else if (rc == 2) {
+        LOG_VOL("Filesystem check failed (not a FAT filesystem)");
+        return -ENODATA;
+    } else if (rc == -11) {
+        LOG_VOL("Filesystem check crashed");
+        return -EIO;
+    } else {
+        LOG_VOL("Filesystem check failed (unknown exit code %d)", rc);
+        return -EIO;
+    }
     return 0;
 }