This is the mail archive of the libc-hacker@sourceware.cygnus.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: wordexp IFS fixes


Okay, here's a patch that should get wordexp knowing the difference
between word-splitting and field-splitting.

Tim.
*/

Sat Sep 12 01:09:34 1998  Tim Waugh  <tim@cyberelk.demon.co.uk>

	* posix/wordexp-test.c: Fix wrong tests.  Add new tests.

	* posix/wordexp.c (wordexp): Perform word-splitting instead of
	field-splitting here.
	(wordexp): If out of memory mid-word, free the word (but still
	leave pwordexp alone for caller to see).
	(parse_param): Allow for zero-length fields (smarter checking of
	memory allocation failure).
	(w_addword): Convert NULL words to "".
	(wordexp): Convert left-over IFS characters to blanks (like bash).

--- libc/posix/wordexp-test.c	Tue Sep  8 17:35:21 1998
+++ /big/libc/posix/wordexp-test.c	Sat Sep 12 01:53:57 1998
@@ -38,12 +38,21 @@
   const char *ifs;
 } test_case[] =
   {
-    /* Simple field-splitting */
+    /* Simple word- and field-splitting */
     { 0, NULL, "one", 0, 1, { "one", }, IFS },
     { 0, NULL, "one two", 0, 2, { "one", "two", }, IFS },
     { 0, NULL, "one two three", 0, 3, { "one", "two", "three", }, IFS },
     { 0, NULL, " \tfoo\t\tbar ", 0, 2, { "foo", "bar", }, IFS },
-    { 0, NULL, "  red  , white blue", 0, 3, { "red", "white", "blue", }, ", \n\t" },
+    { 0, NULL, "red , white blue", 0, 4, { "red", " ", "white", "blue", }, " ," },
+    { 0, NULL, "one two three", 0, 3, { "one", "two", "three", }, "" },
+    { 0, NULL, "one \"two three\"", 0, 2, { "one", "two three", }, IFS },
+    { 0, NULL, "one \"two three\"", 0, 2, { "one", "two three", }, "" },
+    { 0, "two three", "one \"$var\"", 0, 2, { "one", "two three", }, IFS },
+    { 0, "two three", "one $var", 0, 3, { "one", "two", "three", }, IFS },
+    { 0, "two three", "one \"$var\"", 0, 2, { "one", "two three", }, "" },
+    { 0, "two three", "one $var", 0, 2, { "one", "two three", }, "" },
+    { 0, ":abc:", "$var", 0, 2, { "", "abc", }, ":" }, /* cf. bash */
+    { 0, NULL, ":abc:", 0, 1, { " abc ", }, ":" },
 
     /* Simple parameter expansion */
     { 0, "foo", "${var}", 0, 1, { "foo", }, IFS },
@@ -120,9 +129,9 @@
     { 0, "o thr", "*$var*", 0, 2, { "two", "three" }, IFS },
 
     /* Different IFS values */
-    { 0, NULL, "a b\tc\nd  ", 0, 4, { "a", "b", "c", "d" }, NULL /* unset */ },
-    { 0, NULL, "a b\tc d  ", 0, 1, { "a b\tc d  " }, "" /* `null' */ },
-    { 0, NULL, "a,b c\n, d", 0, 3, { "a", "b c", " d" }, "\t\n," },
+    { 0, "a b\tc\nd  ", "$var", 0, 4, { "a", "b", "c", "d" }, NULL /* unset */ },
+    { 0, "a b\tc d  ", "$var", 0, 1, { "a b\tc d  " }, "" /* `null' */ },
+    { 0, "a,b c\n, d", "$var", 0, 3, { "a", "b c", " d" }, "\t\n," },
 
     /* Other things that should succeed */
     { 0, NULL, "\\*\"|&;<>\"\\(\\)\\{\\}", 0, 1, { "*|&;<>(){}", }, IFS },
--- libc/posix/wordexp.c	Thu Sep 10 13:12:31 1998
+++ /big/libc/posix/wordexp.c	Sat Sep 12 01:51:36 1998
@@ -159,6 +159,16 @@
   size_t num_p;
   char **new_wordv;
 
+  /* Internally, NULL acts like "".  Convert NULLs to "" before
+   * the caller sees them.
+   */
+  if (word == NULL)
+    {
+      word = __strdup ("");
+      if (word == NULL)
+	goto no_space;
+    }
+
   num_p = 2 + pwordexp->we_wordc + pwordexp->we_offs;
   new_wordv = realloc (pwordexp->we_wordv, sizeof (char *) * num_p);
   if (new_wordv != NULL)
@@ -169,6 +179,7 @@
       return 0;
     }
 
+no_space:
   return WRDE_NOSPACE;
 }
 
@@ -1759,7 +1770,7 @@
 	  /* Tag a copy onto the current word */
 	  *word = w_addstr (*word, word_length, max_length, field_begin);
 
-	  if (*word == NULL)
+	  if (*word == NULL && *field_begin != '\0')
 	    {
 	      free (value_copy);
 	      return WRDE_NOSPACE;
@@ -2160,11 +2171,13 @@
 	break;
 
       default:
-	/* Is it a field separator? */
-	if (strchr (ifs, words[words_offset]) == NULL)
+	/* Is it a word separator? */
+	if (strchr (" \t", words[words_offset]) == NULL)
 	  {
-	    /* Not a field separator -- but is it a valid word char? */
-	    if (strchr ("\n|&;<>(){}", words[words_offset]))
+	    char ch = words[words_offset];
+
+	    /* Not a word separator -- but is it a valid word char? */
+	    if (strchr ("\n|&;<>(){}", ch))
 	      {
 		/* Fail */
 		wordfree (pwordexp);
@@ -2175,8 +2188,12 @@
 
 	    /* "Ordinary" character -- add it to word */
 
+	    /* Convert IFS chars to blanks -- bash does this */
+	    if (strchr (ifs, ch))
+	      ch = ' ';
+
 	    word = w_addchar (word, &word_length, &max_length,
-			      words[words_offset]);
+			      ch);
 	    if (word == NULL)
 	      {
 		error = WRDE_NOSPACE;
@@ -2186,34 +2203,12 @@
 	    break;
 	  }
 
-	/* Field separator */
-	if (strchr (ifs_white, words[words_offset]))
-	  {
-	    /* It's a whitespace IFS char.  Ignore it at the beginning
-	       of a line and ignore multiple instances.  */
-	    if (!word || !*word)
-	      break;
-
-	    if (w_addword (pwordexp, word) == WRDE_NOSPACE)
-	      {
-		error = WRDE_NOSPACE;
-		goto do_error;
-	      }
-
-	    word = w_newword (&word_length, &max_length);
-	    break;
-	  }
-
-	/* It's a non-whitespace IFS char */
-
-	/* Multiple non-whitespace IFS chars are treated as one.  */
+	/* If a word has been delimited, add it to the list. */
 	if (word != NULL)
 	  {
-	    if (w_addword (pwordexp, word) == WRDE_NOSPACE)
-	      {
-		error = WRDE_NOSPACE;
-		goto do_error;
-	      }
+	    error = w_addword (pwordexp, word);
+	    if (error)
+	      goto do_error;
 	  }
 
 	word = w_newword (&word_length, &max_length);
@@ -2221,7 +2216,7 @@
 
   /* End of string */
 
-  /* There was a field separator at the end */
+  /* There was a word separator at the end */
   if (word == NULL)
     return 0;
 
@@ -2234,11 +2229,11 @@
    *	set we_wordc and wd_wordv back to what they were.
    */
 
-  if (error == WRDE_NOSPACE)
-    return WRDE_NOSPACE;
-
   if (word != NULL)
     free (word);
+
+  if (error == WRDE_NOSPACE)
+    return WRDE_NOSPACE;
 
   wordfree (pwordexp);
   pwordexp->we_wordv = old_wordv;



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]