Index: trunk/src/kmk/expand.c
===================================================================
--- trunk/src/kmk/expand.c	(revision 1830)
+++ trunk/src/kmk/expand.c	(revision 1831)
@@ -505,4 +505,5 @@
       o = variable_buffer_output (o, "\0", 2);
       *eolp = o - 2;
+      assert (strchr (variable_buffer + line_offset, '\0') == *eolp);
       return (variable_buffer + line_offset);
     }
@@ -706,4 +707,5 @@
   o = variable_buffer_output (o, "\0", 2); /* KMK: compensate for the strlen + 1 that was removed above. */
   *eolp = o - 2;
+  assert (strchr (variable_buffer + line_offset, '\0') == *eolp);
   return (variable_buffer + line_offset);
 }
Index: trunk/src/kmk/kbuild.c
===================================================================
--- trunk/src/kmk/kbuild.c	(revision 1830)
+++ trunk/src/kmk/kbuild.c	(revision 1831)
@@ -1903,5 +1903,5 @@
     kbuild_put_sdks(&Sdks);
     (void)pszFuncName;
-    return variable_buffer_output(o, "", 1); /** @todo not right. */
+    return variable_buffer_output(o, "", 1) - 1; /** @todo why? */
 }
 
Index: trunk/src/kmk/read.c
===================================================================
--- trunk/src/kmk/read.c	(revision 1830)
+++ trunk/src/kmk/read.c	(revision 1831)
@@ -158,9 +158,27 @@
 #ifndef CONFIG_WITH_VALUE_LENGTH
 static void remove_comments (char *line);
-#else
-static char *remove_comments (char *line, char *eol);
-#endif
 static char *find_char_unquote (char *string, int stop1, int stop2,
                                 int blank, int ignorevars);
+#else
+__inline static char *remove_comments (char *line, char *eol);
+__inline static char *find_char_unquote_0 (char *string, int stop1, char **eosp);
+static char * find_char_unquote_2 (char *string, int stop1, int stop2,
+                                   int blank, int ignorevars,
+                                   unsigned int string_len);
+__inline static char *
+find_char_unquote (char *string, int stop1, int stop2, int blank, int ignorevars)
+{
+    if (!stop2 && !blank && !ignorevars)
+      {
+        char *p = strchr (string, stop1);
+        if (!p)
+          return NULL;
+        if (p <= string || p[-1] != '\\')
+          return p;
+        /* fall back on find_char_unquote_2 */
+      }
+    return find_char_unquote_2 (string, stop1, stop2, blank, ignorevars, 0);
+}
+#endif
 
 
@@ -536,4 +554,7 @@
   struct floc *fstart;
   struct floc fi;
+#ifdef CONFIG_WITH_VALUE_LENGTH
+  unsigned int tmp_len;
+#endif
 
 #define record_waiting_files()						      \
@@ -1068,5 +1089,9 @@
         /* Search the line for an unquoted ; that is not after an
            unquoted #.  */
+#ifndef CONFIG_WITH_VALUE_LENGTH
         cmdleft = find_char_unquote (line, ';', '#', 0, 1);
+#else
+        cmdleft = find_char_unquote_2 (line, ';', '#', 0, 1, ebuf->eol - line);
+#endif
         if (cmdleft != 0 && *cmdleft == '#')
           {
@@ -1111,5 +1136,11 @@
           }
 
+
+#ifndef CONFIG_WITH_VALUE_LENGTH
         p2 = variable_expand_string(NULL, lb_next, wlen);
+#else
+        p2 = variable_expand_string_2 (NULL, lb_next, wlen, &eol);
+        assert (strchr (p2, '\0') == eol);
+#endif
 
         while (1)
@@ -1119,5 +1150,9 @@
               {
                 /* Look for a semicolon in the expanded line.  */
+#ifndef CONFIG_WITH_VALUE_LENGTH
                 cmdleft = find_char_unquote (p2, ';', 0, 0, 0);
+#else
+                cmdleft = find_char_unquote_0 (p2, ';', &eol);
+#endif
 
                 if (cmdleft != 0)
@@ -1125,5 +1160,7 @@
                     unsigned long p2_off = p2 - variable_buffer;
                     unsigned long cmd_off = cmdleft - variable_buffer;
+#ifndef CONFIG_WITH_VALUE_LENGTH
                     char *pend = p2 + strlen(p2);
+#endif
 
                     /* Append any remnants of lb, then cut the line short
@@ -1139,6 +1176,12 @@
                        expand below once we know we don't have a
                        target-specific variable. */
+#ifndef CONFIG_WITH_VALUE_LENGTH
                     (void)variable_expand_string(pend, lb_next, (long)-1);
                     lb_next += strlen(lb_next);
+#else
+                    tmp_len = strlen (lb_next);
+                    variable_expand_string_2 (eol, lb_next, tmp_len, &eol);
+                    lb_next += tmp_len;
+#endif
                     p2 = variable_buffer + p2_off;
                     cmdleft = variable_buffer + cmd_off + 1;
@@ -1146,5 +1189,9 @@
               }
 
+#ifndef CONFIG_WITH_VALUE_LENGTH
             colonp = find_char_unquote(p2, ':', 0, 0, 0);
+#else
+            colonp = find_char_unquote_0 (p2, ':', &eol);
+#endif
 #ifdef HAVE_DOS_PATHS
             /* The drive spec brain-damage strikes again...  */
@@ -1155,5 +1202,9 @@
                    colonp > p2 && isalpha ((unsigned char)colonp[-1]) &&
                    (colonp == p2 + 1 || strchr (" \t(", colonp[-2]) != 0))
+# ifndef CONFIG_WITH_VALUE_LENGTH
               colonp = find_char_unquote(colonp + 1, ':', 0, 0, 0);
+# else
+              colonp = find_char_unquote_0 (colonp + 1, ':', &eol);
+# endif
 #endif
             if (colonp != 0)
@@ -1164,7 +1215,12 @@
               break;
 
+#ifndef CONFIG_WITH_VALUE_LENGTH
             p2 += strlen(p2);
             *(p2++) = ' ';
             p2 = variable_expand_string(p2, lb_next, wlen);
+#else
+            *(eol++) = ' ';
+            p2 = variable_expand_string_2 (eol, lb_next, wlen, &eol);
+#endif
             /* We don't need to worry about cmdleft here, because if it was
                found in the variable_buffer the entire buffer has already
@@ -1277,5 +1333,10 @@
           {
             unsigned int l = p2 - variable_buffer;
+#ifndef CONFIG_WITH_VALUE_LENGTH
             (void) variable_expand_string (p2 + plen, lb_next, (long)-1);
+#else
+            char *eos;
+            (void) variable_expand_string_2 (p2 + plen, lb_next, (long)-1, &eos);
+#endif
             p2 = variable_buffer + l;
 
@@ -1283,5 +1344,9 @@
             if (cmdleft == 0)
               {
+#ifndef CONFIG_WITH_VALUE_LENGTH
                 cmdleft = find_char_unquote (p2, ';', 0, 0, 0);
+#else
+                cmdleft = find_char_unquote_0 (p2, ';', &eos);
+#endif
                 if (cmdleft != 0)
                   *(cmdleft++) = '\0';
@@ -1494,8 +1559,4 @@
 static void
 remove_comments (char *line)
-#else
-static char *
-remove_comments (char *line, char *eol)
-#endif
 {
   char *comment;
@@ -1506,12 +1567,61 @@
     /* Cut off the line at the #.  */
     *comment = '\0';
-
-#ifdef CONFIG_WITH_VALUE_LENGTH
-  if (comment)
-    eol = comment;
-  assert (strchr (line, '\0') == eol);
-  return eol;
-#endif
 }
+#else  /* CONFIG_WITH_VALUE_LENGTH */
+__inline static char *
+remove_comments (char *line, char *eol)
+{
+  unsigned int string_len = eol - line;
+  register int ch;
+  char *p;
+
+  /* Hope for simple (no comments). */
+  p = memchr (line, '#', string_len);
+  if (!p)
+    return eol;
+
+  /* Found potential comment, enter the slow route. */
+  for (;;)
+    {
+      if (p > line && p[-1] == '\\')
+	{
+	  /* Search for more backslashes.  */
+	  int i = -2;
+	  while (&p[i] >= line && p[i] == '\\')
+	    --i;
+	  ++i;
+
+	  /* The number of backslashes is now -I.
+	     Copy P over itself to swallow half of them.  */
+	  memmove (&p[i], &p[i/2], (string_len - (p - line)) - (i/2) + 1);
+	  p += i/2;
+	  if (i % 2 == 0)
+            {
+	      /* All the backslashes quoted each other; the STOPCHAR was
+                 unquoted.  */
+              *p = '\0';
+              return p;
+            }
+
+	  /* The '#' was quoted by a backslash.  Look for another.  */
+	}
+      else
+        {
+	  /* No backslash in sight.  */
+          *p = '\0';
+	  return p;
+        }
+
+      /* lazy, string_len isn't correct so do it the slow way. */
+      while ((ch = *p) != '#')
+        {
+          if (ch == '\0')
+            return p;
+          ++p;
+        }
+    }
+  /* won't ever get here. */
+}
+#endif /* CONFIG_WITH_VALUE_LENGTH */
 
 /* Execute a `define' directive.
@@ -2540,11 +2650,22 @@
    STOPCHAR _cannot_ be '$' if IGNOREVARS is true.  */
 
+#ifndef CONFIG_WITH_VALUE_LENGTH
 static char *
 find_char_unquote (char *string, int stop1, int stop2, int blank,
                    int ignorevars)
+#else
+static char *
+find_char_unquote_2 (char *string, int stop1, int stop2, int blank,
+                     int ignorevars, unsigned int string_len)
+#endif
 {
+#ifndef CONFIG_WITH_VALUE_LENGTH
   unsigned int string_len = 0;
+#endif
   char *p = string;
   register int ch; /* bird: 'optimiziations' */
+#ifdef CONFIG_WITH_VALUE_LENGTH
+  assert (string_len == 0 || string_len == strlen (string));
+#endif
 
   if (ignorevars)
@@ -2631,4 +2752,25 @@
   return 0;
 }
+
+#ifdef CONFIG_WITH_VALUE_LENGTH
+/* Special case version of find_char_unquote that only takes stop1.
+   This is so common that it makes a lot of sense to specialize this.
+   */
+__inline static char *
+find_char_unquote_0 (char *string, int stop1, char **eosp)
+{
+  unsigned int string_len = *eosp - string;
+  char *p = (char *)memchr (string, stop1, string_len);
+  assert (strlen (string) == string_len);
+  if (!p)
+    return NULL;
+  if (p <= string || p[-1] != '\\')
+    return p;
+
+  p = find_char_unquote_2 (string, stop1, 0, 0, 0, string_len);
+  *eosp = memchr (string, '\0', string_len);
+  return p;
+}
+#endif
 
 /* Search PATTERN for an unquoted % and handle quoting.  */
