Remove sindex() and replace with strstr().
authorPaul Smith <psmith@gnu.org>
Tue, 21 Sep 2004 12:07:12 +0000 (12:07 +0000)
committerPaul Smith <psmith@gnu.org>
Tue, 21 Sep 2004 12:07:12 +0000 (12:07 +0000)
Windows: allow users to set SHELL to cmd.exe and have it behave as if no
UNIX shell were found.

ChangeLog
commands.c
function.c
main.c
make.h
misc.c
tests/ChangeLog

index 9b2f30dcb82f172549ec94e8fba1909c0dd23a3f..b5af21ed78afdcc7611870a28bfebf8d960c7650 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,23 @@
+2004-09-21  Paul D. Smith  <psmith@gnu.org>
+
+       * misc.c: Removed the sindex() function.  All instances of this
+       function were trivially replaceable by the standard strstr()
+       function, and that function will always have better (or certainly
+       no worse) performance than the very simple-minded algorithm
+       sindex() used.  This can matter with complex makefiles.
+       * make.h: Remove the prototype for sindex().
+       * function.c (subst_expand): Convert sindex() call to strstr().
+       This means we no longer need to track the TLEN value so remove that.
+       (func_findstring): Convert sindex() to strstr().
+       * commands.c (chop_commands): Convert sindex() calls to strstr().
+       Suggested by: Markus Mauhart <qwe123@chello.at>.
+
+       * main.c (find_and_set_default_shell) [WINDOWS32]: Implement the
+       idea behind Savannah Patch #3144 from david.baird@homemail.com.
+       If SHELL is set to CMD.EXE then assume it's batch-mode and
+       non-unixy.  I wrote the code differently from the patch, though,
+       to make it safer.
+
 2004-09-20  Paul D. Smith  <psmith@gnu.org>
 
        * expand.c (variable_expand_string): Modify to invoke
index b202f6cded9ad610962efae1e0503ba988dc9340..6fdb5bbb01382c5924604a6cac0f929777539cdf 100644 (file)
@@ -346,13 +346,11 @@ chop_commands (struct commands *cmds)
             flags |= COMMANDS_NOERROR;
             break;
           }
-      if (!(flags & COMMANDS_RECURSE))
-        {
-          unsigned int len = strlen (p);
-          if (sindex (p, len, "$(MAKE)", 7) != 0
-              || sindex (p, len, "${MAKE}", 7) != 0)
-            flags |= COMMANDS_RECURSE;
-        }
+
+      /* If no explicit '+' was given, look for MAKE variable references.  */
+      if (!(flags & COMMANDS_RECURSE)
+          && (strstr (p, "$(MAKE)") != 0 || strstr (p, "${MAKE}") != 0))
+        flags |= COMMANDS_RECURSE;
 
       cmds->lines_flags[idx] = flags;
       cmds->any_recurse |= flags & COMMANDS_RECURSE;
index 392ff25d6b1e288a304b560c45a14a80397839b9..54f5445b35b60f10cc73ef79c6817818916fbfa7 100644 (file)
@@ -79,13 +79,12 @@ subst_expand (char *o, char *text, char *subst, char *replace,
               unsigned int slen, unsigned int rlen, int by_word)
 {
   char *t = text;
-  unsigned int tlen = strlen (text);
   char *p;
 
   if (slen == 0 && !by_word)
     {
       /* The first occurrence of "" in any string is its end.  */
-      o = variable_buffer_output (o, t, tlen);
+      o = variable_buffer_output (o, t, strlen (t));
       if (rlen > 0)
        o = variable_buffer_output (o, replace, rlen);
       return o;
@@ -99,11 +98,11 @@ subst_expand (char *o, char *text, char *subst, char *replace,
        p = end_of_token (next_token (t));
       else
        {
-         p = sindex (t, tlen, subst, slen);
+         p = strstr (t, subst);
          if (p == 0)
            {
              /* No more matches.  Output everything left on the end.  */
-             o = variable_buffer_output (o, t, tlen);
+             o = variable_buffer_output (o, t, strlen (t));
              return o;
            }
        }
@@ -124,10 +123,9 @@ subst_expand (char *o, char *text, char *subst, char *replace,
        /* Output the replacement string.  */
        o = variable_buffer_output (o, replace, rlen);
 
-      /* Advance T past the string to be replaced; adjust tlen.  */
+      /* Advance T past the string to be replaced.  */
       {
         char *nt = p + slen;
-        tlen -= nt - t;
         t = nt;
       }
     } while (*t != '\0');
@@ -786,9 +784,8 @@ static char*
 func_findstring (char *o, char **argv, const char *funcname UNUSED)
 {
   /* Find the first occurrence of the first string in the second.  */
-  int i = strlen (argv[0]);
-  if (sindex (argv[1], 0, argv[0], i) != 0)
-    o = variable_buffer_output (o, argv[0], i);
+  if (strstr (argv[1], argv[0]) != 0)
+    o = variable_buffer_output (o, argv[0], strlen (argv[0]));
 
   return o;
 }
diff --git a/main.c b/main.c
index 13c341568de7a07ad3074aec448d884c6ba9a99f..5cb387ffd2bbe42da0948a975ed77e63bb83b720 100644 (file)
--- a/main.c
+++ b/main.c
@@ -699,7 +699,8 @@ int
 find_and_set_default_shell (char *token)
 {
   int sh_found = 0;
-  char* search_token;
+  char *search_token;
+  char *tokend;
   PATH_VAR(sh_path);
   extern char *default_shell;
 
@@ -708,8 +709,25 @@ find_and_set_default_shell (char *token)
   else
     search_token = token;
 
-  if (!no_default_sh_exe &&
-      (token == NULL || !strcmp(search_token, default_shell))) {
+
+  /* If the user explicitly requests the DOS cmd shell, obey that request.
+     However, make sure that's what they really want by requiring the value
+     of SHELL either equal, or have a final path element of, "cmd" or
+     "cmd.exe" case-insensitive.  */
+  tokend = search_token + strlen (search_token) - 3;
+  if (((tokend == search_token
+        || (tokend > search_token
+            && (tokend[-1] == '/' || tokend[-1] == '\\')))
+       && strcmpi (tokend, "cmd"))
+      || ((tokend - 4 == search_token
+           || (tokend - 4 > search_token
+               && (tokend[-5] == '/' || tokend[-5] == '\\')))
+          && strcmpi (tokend - 4, "cmd.exe"))) {
+    batch_mode_shell = 1;
+    unixy_shell = 0;
+    sh_found = 0;
+  } else if (!no_default_sh_exe &&
+             (token == NULL || !strcmp (search_token, default_shell))) {
     /* no new information, path already set or known */
     sh_found = 1;
   } else if (file_exists_p(search_token)) {
diff --git a/make.h b/make.h
index ce88d5c4afd1b3e2ef131e7e99b7f73ce3d31c88..ad46e1f8a7cf6756c46f479eac9bfeab90974a19 100644 (file)
--- a/make.h
+++ b/make.h
@@ -423,8 +423,6 @@ extern char *next_token PARAMS ((const char *));
 extern char *end_of_token PARAMS ((char *));
 extern void collapse_continuations PARAMS ((char *));
 extern void remove_comments PARAMS((char *));
-extern char *sindex PARAMS ((const char *, unsigned int, \
-                             const char *, unsigned int));
 extern char *lindex PARAMS ((const char *, const char *, int));
 extern int alpha_compare PARAMS ((const void *, const void *));
 extern void print_spaces PARAMS ((unsigned int));
diff --git a/misc.c b/misc.c
index 9d3ff4fb39789ef5e76f03196c2a7d11a437f37d..5532369d8957a42dfac5808bfbedb73db3281e3e 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -412,34 +412,6 @@ savestring (const char *str, unsigned int length)
   return out;
 }
 \f
-/* Search string BIG (length BLEN) for an occurrence of
-   string SMALL (length SLEN).  Return a pointer to the
-   beginning of the first occurrence, or return nil if none found.  */
-
-char *
-sindex (const char *big, unsigned int blen,
-        const char *small, unsigned int slen)
-{
-  if (!blen)
-    blen = strlen (big);
-  if (!slen)
-    slen = strlen (small);
-
-  if (slen && blen >= slen)
-    {
-      register unsigned int b;
-
-      /* Quit when there's not enough room left for the small string.  */
-      --slen;
-      blen -= slen;
-
-      for (b = 0; b < blen; ++b, ++big)
-        if (*big == *small && strneq (big + 1, small + 1, slen))
-          return (char *)big;
-    }
-
-  return 0;
-}
 
 /* Limited INDEX:
    Search through the string STRING, which ends at LIMIT, for the character C.
index eec2ef1f7a5731d9e702418f5abae1f25fb8da98..ba6b59e2aefc8e70ba0776445126c5c665b00583 100644 (file)
@@ -1,3 +1,12 @@
+2004-09-21  Paul D. Smith  <psmith@gnu.org>
+
+       * run_make_tests.pl (run_make_test): Enhance to allow the make
+       string to be undef: in that case it reuses the previous make
+       string.  Allows multiple tests on the same makefile.
+
+       * scripts/variables/flavors: Add some tests for prefix characters
+       interacting with define/endef variables.
+
 2004-09-20  Paul D. Smith  <psmith@gnu.org>
 
        * scripts/functions/substitution: Rewrite to use run_make_test()