Blame posix/wordexp.c

Packit 6c4009
/* POSIX.2 wordexp implementation.
Packit 6c4009
   Copyright (C) 1997-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Tim Waugh <tim@cyberelk.demon.co.uk>.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <ctype.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <fnmatch.h>
Packit 6c4009
#include <glob.h>
Packit 6c4009
#include <libintl.h>
Packit 6c4009
#include <paths.h>
Packit 6c4009
#include <pwd.h>
Packit 6c4009
#include <signal.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/param.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include <sys/time.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <sys/wait.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <wchar.h>
Packit 6c4009
#include <wordexp.h>
Packit 6c4009
#include <kernel-features.h>
Packit 6c4009
#include <scratch_buffer.h>
Packit 6c4009
Packit 6c4009
#include <libc-lock.h>
Packit 6c4009
#include <_itoa.h>
Packit 6c4009
Packit 6c4009
/* Undefine the following line for the production version.  */
Packit 6c4009
/* #define NDEBUG 1 */
Packit 6c4009
#include <assert.h>
Packit 6c4009
Packit 6c4009
/* Get some device information.  */
Packit 6c4009
#include <device-nrs.h>
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * This is a recursive-descent-style word expansion routine.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
/* These variables are defined and initialized in the startup code.  */
Packit 6c4009
extern int __libc_argc attribute_hidden;
Packit 6c4009
extern char **__libc_argv attribute_hidden;
Packit 6c4009
Packit 6c4009
/* Some forward declarations */
Packit 6c4009
static int parse_dollars (char **word, size_t *word_length, size_t *max_length,
Packit 6c4009
			  const char *words, size_t *offset, int flags,
Packit 6c4009
			  wordexp_t *pwordexp, const char *ifs,
Packit 6c4009
			  const char *ifs_white, int quoted);
Packit 6c4009
static int parse_backtick (char **word, size_t *word_length,
Packit 6c4009
			   size_t *max_length, const char *words,
Packit 6c4009
			   size_t *offset, int flags, wordexp_t *pwordexp,
Packit 6c4009
			   const char *ifs, const char *ifs_white);
Packit 6c4009
static int parse_dquote (char **word, size_t *word_length, size_t *max_length,
Packit 6c4009
			 const char *words, size_t *offset, int flags,
Packit 6c4009
			 wordexp_t *pwordexp, const char *ifs,
Packit 6c4009
			 const char *ifs_white);
Packit 6c4009
static int eval_expr (char *expr, long int *result);
Packit 6c4009
Packit 6c4009
/* The w_*() functions manipulate word lists. */
Packit 6c4009
Packit 6c4009
#define W_CHUNK	(100)
Packit 6c4009
Packit 6c4009
/* Result of w_newword will be ignored if it's the last word. */
Packit 6c4009
static inline char *
Packit 6c4009
w_newword (size_t *actlen, size_t *maxlen)
Packit 6c4009
{
Packit 6c4009
  *actlen = *maxlen = 0;
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static char *
Packit 6c4009
w_addchar (char *buffer, size_t *actlen, size_t *maxlen, char ch)
Packit 6c4009
     /* (lengths exclude trailing zero) */
Packit 6c4009
{
Packit 6c4009
  /* Add a character to the buffer, allocating room for it if needed.  */
Packit 6c4009
Packit 6c4009
  if (*actlen == *maxlen)
Packit 6c4009
    {
Packit 6c4009
      char *old_buffer = buffer;
Packit 6c4009
      assert (buffer == NULL || *maxlen != 0);
Packit 6c4009
      *maxlen += W_CHUNK;
Packit 6c4009
      buffer = (char *) realloc (buffer, 1 + *maxlen);
Packit 6c4009
Packit 6c4009
      if (buffer == NULL)
Packit 6c4009
	free (old_buffer);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (buffer != NULL)
Packit 6c4009
    {
Packit 6c4009
      buffer[*actlen] = ch;
Packit 6c4009
      buffer[++(*actlen)] = '\0';
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return buffer;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static char *
Packit 6c4009
w_addmem (char *buffer, size_t *actlen, size_t *maxlen, const char *str,
Packit 6c4009
	  size_t len)
Packit 6c4009
{
Packit 6c4009
  /* Add a string to the buffer, allocating room for it if needed.
Packit 6c4009
   */
Packit 6c4009
  if (*actlen + len > *maxlen)
Packit 6c4009
    {
Packit 6c4009
      char *old_buffer = buffer;
Packit 6c4009
      assert (buffer == NULL || *maxlen != 0);
Packit 6c4009
      *maxlen += MAX (2 * len, W_CHUNK);
Packit 6c4009
      buffer = realloc (old_buffer, 1 + *maxlen);
Packit 6c4009
Packit 6c4009
      if (buffer == NULL)
Packit 6c4009
	free (old_buffer);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (buffer != NULL)
Packit 6c4009
    {
Packit 6c4009
      *((char *) __mempcpy (&buffer[*actlen], str, len)) = '\0';
Packit 6c4009
      *actlen += len;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return buffer;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static char *
Packit 6c4009
w_addstr (char *buffer, size_t *actlen, size_t *maxlen, const char *str)
Packit 6c4009
     /* (lengths exclude trailing zero) */
Packit 6c4009
{
Packit 6c4009
  /* Add a string to the buffer, allocating room for it if needed.
Packit 6c4009
   */
Packit 6c4009
  size_t len;
Packit 6c4009
Packit 6c4009
  assert (str != NULL); /* w_addstr only called from this file */
Packit 6c4009
  len = strlen (str);
Packit 6c4009
Packit 6c4009
  return w_addmem (buffer, actlen, maxlen, str, len);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
w_addword (wordexp_t *pwordexp, char *word)
Packit 6c4009
{
Packit 6c4009
  /* Add a word to the wordlist */
Packit 6c4009
  size_t num_p;
Packit 6c4009
  char **new_wordv;
Packit 6c4009
  bool allocated = false;
Packit 6c4009
Packit 6c4009
  /* Internally, NULL acts like "".  Convert NULLs to "" before
Packit 6c4009
   * the caller sees them.
Packit 6c4009
   */
Packit 6c4009
  if (word == NULL)
Packit 6c4009
    {
Packit 6c4009
      word = __strdup ("");
Packit 6c4009
      if (word == NULL)
Packit 6c4009
	goto no_space;
Packit 6c4009
      allocated = true;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  num_p = 2 + pwordexp->we_wordc + pwordexp->we_offs;
Packit 6c4009
  new_wordv = realloc (pwordexp->we_wordv, sizeof (char *) * num_p);
Packit 6c4009
  if (new_wordv != NULL)
Packit 6c4009
    {
Packit 6c4009
      pwordexp->we_wordv = new_wordv;
Packit 6c4009
      pwordexp->we_wordv[pwordexp->we_offs + pwordexp->we_wordc++] = word;
Packit 6c4009
      pwordexp->we_wordv[pwordexp->we_offs + pwordexp->we_wordc] = NULL;
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (allocated)
Packit 6c4009
    free (word);
Packit 6c4009
Packit 6c4009
no_space:
Packit 6c4009
  return WRDE_NOSPACE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* The parse_*() functions should leave *offset being the offset in 'words'
Packit 6c4009
 * to the last character processed.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
parse_backslash (char **word, size_t *word_length, size_t *max_length,
Packit 6c4009
		 const char *words, size_t *offset)
Packit 6c4009
{
Packit 6c4009
  /* We are poised _at_ a backslash, not in quotes */
Packit 6c4009
Packit 6c4009
  switch (words[1 + *offset])
Packit 6c4009
    {
Packit 6c4009
    case 0:
Packit 6c4009
      /* Backslash is last character of input words */
Packit 6c4009
      return WRDE_SYNTAX;
Packit 6c4009
Packit 6c4009
    case '\n':
Packit 6c4009
      ++(*offset);
Packit 6c4009
      break;
Packit 6c4009
Packit 6c4009
    default:
Packit 6c4009
      *word = w_addchar (*word, word_length, max_length, words[1 + *offset]);
Packit 6c4009
      if (*word == NULL)
Packit 6c4009
	return WRDE_NOSPACE;
Packit 6c4009
Packit 6c4009
      ++(*offset);
Packit 6c4009
      break;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
parse_qtd_backslash (char **word, size_t *word_length, size_t *max_length,
Packit 6c4009
		     const char *words, size_t *offset)
Packit 6c4009
{
Packit 6c4009
  /* We are poised _at_ a backslash, inside quotes */
Packit 6c4009
Packit 6c4009
  switch (words[1 + *offset])
Packit 6c4009
    {
Packit 6c4009
    case 0:
Packit 6c4009
      /* Backslash is last character of input words */
Packit 6c4009
      return WRDE_SYNTAX;
Packit 6c4009
Packit 6c4009
    case '\n':
Packit 6c4009
      ++(*offset);
Packit 6c4009
      break;
Packit 6c4009
Packit 6c4009
    case '$':
Packit 6c4009
    case '`':
Packit 6c4009
    case '"':
Packit 6c4009
    case '\\':
Packit 6c4009
      *word = w_addchar (*word, word_length, max_length, words[1 + *offset]);
Packit 6c4009
      if (*word == NULL)
Packit 6c4009
	return WRDE_NOSPACE;
Packit 6c4009
Packit 6c4009
      ++(*offset);
Packit 6c4009
      break;
Packit 6c4009
Packit 6c4009
    default:
Packit 6c4009
      *word = w_addchar (*word, word_length, max_length, words[*offset]);
Packit 6c4009
      if (*word != NULL)
Packit 6c4009
	*word = w_addchar (*word, word_length, max_length, words[1 + *offset]);
Packit 6c4009
Packit 6c4009
      if (*word == NULL)
Packit 6c4009
	return WRDE_NOSPACE;
Packit 6c4009
Packit 6c4009
      ++(*offset);
Packit 6c4009
      break;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
parse_tilde (char **word, size_t *word_length, size_t *max_length,
Packit 6c4009
	     const char *words, size_t *offset, size_t wordc)
Packit 6c4009
{
Packit 6c4009
  /* We are poised _at_ a tilde */
Packit 6c4009
  size_t i;
Packit 6c4009
Packit 6c4009
  if (*word_length != 0)
Packit 6c4009
    {
Packit 6c4009
      if (!((*word)[*word_length - 1] == '=' && wordc == 0))
Packit 6c4009
	{
Packit 6c4009
	  if (!((*word)[*word_length - 1] == ':'
Packit 6c4009
		&& strchr (*word, '=') && wordc == 0))
Packit 6c4009
	    {
Packit 6c4009
	      *word = w_addchar (*word, word_length, max_length, '~');
Packit 6c4009
	      return *word ? 0 : WRDE_NOSPACE;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (i = 1 + *offset; words[i]; i++)
Packit 6c4009
    {
Packit 6c4009
      if (words[i] == ':' || words[i] == '/' || words[i] == ' ' ||
Packit 6c4009
	  words[i] == '\t' || words[i] == 0 )
Packit 6c4009
	break;
Packit 6c4009
Packit 6c4009
      if (words[i] == '\\')
Packit 6c4009
	{
Packit 6c4009
	  *word = w_addchar (*word, word_length, max_length, '~');
Packit 6c4009
	  return *word ? 0 : WRDE_NOSPACE;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (i == 1 + *offset)
Packit 6c4009
    {
Packit 6c4009
      /* Tilde appears on its own */
Packit 6c4009
      char* home;
Packit 6c4009
Packit 6c4009
      /* POSIX.2 says ~ expands to $HOME and if HOME is unset the
Packit 6c4009
	 results are unspecified.  We do a lookup on the uid if
Packit 6c4009
	 HOME is unset. */
Packit 6c4009
Packit 6c4009
      home = getenv ("HOME");
Packit 6c4009
      if (home != NULL)
Packit 6c4009
	{
Packit 6c4009
	  *word = w_addstr (*word, word_length, max_length, home);
Packit 6c4009
	  if (*word == NULL)
Packit 6c4009
	    return WRDE_NOSPACE;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  struct passwd pwd, *tpwd;
Packit 6c4009
	  uid_t uid = __getuid ();
Packit 6c4009
	  int result;
Packit 6c4009
	  struct scratch_buffer tmpbuf;
Packit 6c4009
	  scratch_buffer_init (&tmpbuf);
Packit 6c4009
Packit 6c4009
	  while ((result = __getpwuid_r (uid, &pwd,
Packit 6c4009
					 tmpbuf.data, tmpbuf.length,
Packit 6c4009
					 &tpwd)) != 0
Packit 6c4009
		 && errno == ERANGE)
Packit 6c4009
	    if (!scratch_buffer_grow (&tmpbuf))
Packit 6c4009
	      return WRDE_NOSPACE;
Packit 6c4009
Packit 6c4009
	  if (result == 0 && tpwd != NULL && pwd.pw_dir != NULL)
Packit 6c4009
	    {
Packit 6c4009
	      *word = w_addstr (*word, word_length, max_length, pwd.pw_dir);
Packit 6c4009
	      if (*word == NULL)
Packit 6c4009
		{
Packit 6c4009
		  scratch_buffer_free (&tmpbuf);
Packit 6c4009
		  return WRDE_NOSPACE;
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      *word = w_addchar (*word, word_length, max_length, '~');
Packit 6c4009
	      if (*word == NULL)
Packit 6c4009
		{
Packit 6c4009
		  scratch_buffer_free (&tmpbuf);
Packit 6c4009
		  return WRDE_NOSPACE;
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
	  scratch_buffer_free (&tmpbuf);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* Look up user name in database to get home directory */
Packit 6c4009
      char *user = strndupa (&words[1 + *offset], i - (1 + *offset));
Packit 6c4009
      struct passwd pwd, *tpwd;
Packit 6c4009
      int result;
Packit 6c4009
      struct scratch_buffer tmpbuf;
Packit 6c4009
      scratch_buffer_init (&tmpbuf);
Packit 6c4009
Packit 6c4009
      while ((result = __getpwnam_r (user, &pwd, tmpbuf.data, tmpbuf.length,
Packit 6c4009
				     &tpwd)) != 0
Packit 6c4009
	     && errno == ERANGE)
Packit 6c4009
	if (!scratch_buffer_grow (&tmpbuf))
Packit 6c4009
	  return WRDE_NOSPACE;
Packit 6c4009
Packit 6c4009
      if (result == 0 && tpwd != NULL && pwd.pw_dir)
Packit 6c4009
	*word = w_addstr (*word, word_length, max_length, pwd.pw_dir);
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* (invalid login name) */
Packit 6c4009
	  *word = w_addchar (*word, word_length, max_length, '~');
Packit 6c4009
	  if (*word != NULL)
Packit 6c4009
	    *word = w_addstr (*word, word_length, max_length, user);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      scratch_buffer_free (&tmpbuf);
Packit 6c4009
Packit 6c4009
      *offset = i - 1;
Packit 6c4009
    }
Packit 6c4009
  return *word ? 0 : WRDE_NOSPACE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_parse_glob (const char *glob_word, char **word, size_t *word_length,
Packit 6c4009
	       size_t *max_length, wordexp_t *pwordexp, const char *ifs,
Packit 6c4009
	       const char *ifs_white)
Packit 6c4009
{
Packit 6c4009
  int error;
Packit 6c4009
  unsigned int match;
Packit 6c4009
  glob_t globbuf;
Packit 6c4009
Packit 6c4009
  error = glob (glob_word, GLOB_NOCHECK, NULL, &globbuf);
Packit 6c4009
Packit 6c4009
  if (error != 0)
Packit 6c4009
    {
Packit 6c4009
      /* We can only run into memory problems.  */
Packit 6c4009
      assert (error == GLOB_NOSPACE);
Packit 6c4009
      return WRDE_NOSPACE;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (ifs && !*ifs)
Packit 6c4009
    {
Packit 6c4009
      /* No field splitting allowed. */
Packit 6c4009
      assert (globbuf.gl_pathv[0] != NULL);
Packit 6c4009
      *word = w_addstr (*word, word_length, max_length, globbuf.gl_pathv[0]);
Packit 6c4009
      for (match = 1; match < globbuf.gl_pathc && *word != NULL; ++match)
Packit 6c4009
	{
Packit 6c4009
	  *word = w_addchar (*word, word_length, max_length, ' ');
Packit 6c4009
	  if (*word != NULL)
Packit 6c4009
	    *word = w_addstr (*word, word_length, max_length,
Packit 6c4009
			      globbuf.gl_pathv[match]);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      globfree (&globbuf);
Packit 6c4009
      return *word ? 0 : WRDE_NOSPACE;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  assert (ifs == NULL || *ifs != '\0');
Packit 6c4009
  if (*word != NULL)
Packit 6c4009
    {
Packit 6c4009
      free (*word);
Packit 6c4009
      *word = w_newword (word_length, max_length);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (match = 0; match < globbuf.gl_pathc; ++match)
Packit 6c4009
    {
Packit 6c4009
      char *matching_word = __strdup (globbuf.gl_pathv[match]);
Packit 6c4009
      if (matching_word == NULL || w_addword (pwordexp, matching_word))
Packit 6c4009
	{
Packit 6c4009
	  globfree (&globbuf);
Packit 6c4009
	  return WRDE_NOSPACE;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  globfree (&globbuf);
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
parse_glob (char **word, size_t *word_length, size_t *max_length,
Packit 6c4009
	    const char *words, size_t *offset, int flags,
Packit 6c4009
	    wordexp_t *pwordexp, const char *ifs, const char *ifs_white)
Packit 6c4009
{
Packit 6c4009
  /* We are poised just after a '*', a '[' or a '?'. */
Packit 6c4009
  int error = WRDE_NOSPACE;
Packit 6c4009
  int quoted = 0; /* 1 if singly-quoted, 2 if doubly */
Packit 6c4009
  size_t i;
Packit 6c4009
  wordexp_t glob_list; /* List of words to glob */
Packit 6c4009
Packit 6c4009
  glob_list.we_wordc = 0;
Packit 6c4009
  glob_list.we_wordv = NULL;
Packit 6c4009
  glob_list.we_offs = 0;
Packit 6c4009
  for (; words[*offset] != '\0'; ++*offset)
Packit 6c4009
    {
Packit 6c4009
      if (strchr (ifs, words[*offset]) != NULL)
Packit 6c4009
	/* Reached IFS */
Packit 6c4009
	break;
Packit 6c4009
Packit 6c4009
      /* Sort out quoting */
Packit 6c4009
      if (words[*offset] == '\'')
Packit 6c4009
	{
Packit 6c4009
	  if (quoted == 0)
Packit 6c4009
	    {
Packit 6c4009
	      quoted = 1;
Packit 6c4009
	      continue;
Packit 6c4009
	    }
Packit 6c4009
	  else if (quoted == 1)
Packit 6c4009
	    {
Packit 6c4009
	      quoted = 0;
Packit 6c4009
	      continue;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else if (words[*offset] == '"')
Packit 6c4009
	{
Packit 6c4009
	  if (quoted == 0)
Packit 6c4009
	    {
Packit 6c4009
	      quoted = 2;
Packit 6c4009
	      continue;
Packit 6c4009
	    }
Packit 6c4009
	  else if (quoted == 2)
Packit 6c4009
	    {
Packit 6c4009
	      quoted = 0;
Packit 6c4009
	      continue;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Sort out other special characters */
Packit 6c4009
      if (quoted != 1 && words[*offset] == '$')
Packit 6c4009
	{
Packit 6c4009
	  error = parse_dollars (word, word_length, max_length, words,
Packit 6c4009
				 offset, flags, &glob_list, ifs, ifs_white,
Packit 6c4009
				 quoted == 2);
Packit 6c4009
	  if (error)
Packit 6c4009
	    goto tidy_up;
Packit 6c4009
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
      else if (words[*offset] == '\\')
Packit 6c4009
	{
Packit 6c4009
	  if (quoted)
Packit 6c4009
	    error = parse_qtd_backslash (word, word_length, max_length,
Packit 6c4009
					 words, offset);
Packit 6c4009
	  else
Packit 6c4009
	    error = parse_backslash (word, word_length, max_length,
Packit 6c4009
				     words, offset);
Packit 6c4009
Packit 6c4009
	  if (error)
Packit 6c4009
	    goto tidy_up;
Packit 6c4009
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      *word = w_addchar (*word, word_length, max_length, words[*offset]);
Packit 6c4009
      if (*word == NULL)
Packit 6c4009
	goto tidy_up;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Don't forget to re-parse the character we stopped at. */
Packit 6c4009
  --*offset;
Packit 6c4009
Packit 6c4009
  /* Glob the words */
Packit 6c4009
  error = w_addword (&glob_list, *word);
Packit 6c4009
  *word = w_newword (word_length, max_length);
Packit 6c4009
  for (i = 0; error == 0 && i < glob_list.we_wordc; i++)
Packit 6c4009
    error = do_parse_glob (glob_list.we_wordv[i], word, word_length,
Packit 6c4009
			   max_length, pwordexp, ifs, ifs_white);
Packit 6c4009
Packit 6c4009
  /* Now tidy up */
Packit 6c4009
tidy_up:
Packit 6c4009
  wordfree (&glob_list);
Packit 6c4009
  return error;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
parse_squote (char **word, size_t *word_length, size_t *max_length,
Packit 6c4009
	      const char *words, size_t *offset)
Packit 6c4009
{
Packit 6c4009
  /* We are poised just after a single quote */
Packit 6c4009
  for (; words[*offset]; ++(*offset))
Packit 6c4009
    {
Packit 6c4009
      if (words[*offset] != '\'')
Packit 6c4009
	{
Packit 6c4009
	  *word = w_addchar (*word, word_length, max_length, words[*offset]);
Packit 6c4009
	  if (*word == NULL)
Packit 6c4009
	    return WRDE_NOSPACE;
Packit 6c4009
	}
Packit 6c4009
      else return 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Unterminated string */
Packit 6c4009
  return WRDE_SYNTAX;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Functions to evaluate an arithmetic expression */
Packit 6c4009
static int
Packit 6c4009
eval_expr_val (char **expr, long int *result)
Packit 6c4009
{
Packit 6c4009
  char *digit;
Packit 6c4009
Packit 6c4009
  /* Skip white space */
Packit 6c4009
  for (digit = *expr; digit && *digit && isspace (*digit); ++digit);
Packit 6c4009
Packit 6c4009
  if (*digit == '(')
Packit 6c4009
    {
Packit 6c4009
      /* Scan for closing paren */
Packit 6c4009
      for (++digit; **expr && **expr != ')'; ++(*expr));
Packit 6c4009
Packit 6c4009
      /* Is there one? */
Packit 6c4009
      if (!**expr)
Packit 6c4009
	return WRDE_SYNTAX;
Packit 6c4009
Packit 6c4009
      *(*expr)++ = 0;
Packit 6c4009
Packit 6c4009
      if (eval_expr (digit, result))
Packit 6c4009
	return WRDE_SYNTAX;
Packit 6c4009
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* POSIX requires that decimal, octal, and hexadecimal constants are
Packit 6c4009
     recognized.  Therefore we pass 0 as the third parameter to strtol.  */
Packit 6c4009
  *result = strtol (digit, expr, 0);
Packit 6c4009
  if (digit == *expr)
Packit 6c4009
    return WRDE_SYNTAX;
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
eval_expr_multdiv (char **expr, long int *result)
Packit 6c4009
{
Packit 6c4009
  long int arg;
Packit 6c4009
Packit 6c4009
  /* Read a Value */
Packit 6c4009
  if (eval_expr_val (expr, result) != 0)
Packit 6c4009
    return WRDE_SYNTAX;
Packit 6c4009
Packit 6c4009
  while (**expr)
Packit 6c4009
    {
Packit 6c4009
      /* Skip white space */
Packit 6c4009
      for (; *expr && **expr && isspace (**expr); ++(*expr));
Packit 6c4009
Packit 6c4009
      if (**expr == '*')
Packit 6c4009
	{
Packit 6c4009
	  ++(*expr);
Packit 6c4009
	  if (eval_expr_val (expr, &arg) != 0)
Packit 6c4009
	    return WRDE_SYNTAX;
Packit 6c4009
Packit 6c4009
	  *result *= arg;
Packit 6c4009
	}
Packit 6c4009
      else if (**expr == '/')
Packit 6c4009
	{
Packit 6c4009
	  ++(*expr);
Packit 6c4009
	  if (eval_expr_val (expr, &arg) != 0)
Packit 6c4009
	    return WRDE_SYNTAX;
Packit 6c4009
Packit 6c4009
	  /* Division by zero or integer overflow.  */
Packit 6c4009
	  if (arg == 0 || (arg == -1 && *result == LONG_MIN))
Packit 6c4009
	    return WRDE_SYNTAX;
Packit 6c4009
Packit 6c4009
	  *result /= arg;
Packit 6c4009
	}
Packit 6c4009
      else break;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
eval_expr (char *expr, long int *result)
Packit 6c4009
{
Packit 6c4009
  long int arg;
Packit 6c4009
Packit 6c4009
  /* Read a Multdiv */
Packit 6c4009
  if (eval_expr_multdiv (&expr, result) != 0)
Packit 6c4009
    return WRDE_SYNTAX;
Packit 6c4009
Packit 6c4009
  while (*expr)
Packit 6c4009
    {
Packit 6c4009
      /* Skip white space */
Packit 6c4009
      for (; expr && *expr && isspace (*expr); ++expr);
Packit 6c4009
Packit 6c4009
      if (*expr == '+')
Packit 6c4009
	{
Packit 6c4009
	  ++expr;
Packit 6c4009
	  if (eval_expr_multdiv (&expr, &arg) != 0)
Packit 6c4009
	    return WRDE_SYNTAX;
Packit 6c4009
Packit 6c4009
	  *result += arg;
Packit 6c4009
	}
Packit 6c4009
      else if (*expr == '-')
Packit 6c4009
	{
Packit 6c4009
	  ++expr;
Packit 6c4009
	  if (eval_expr_multdiv (&expr, &arg) != 0)
Packit 6c4009
	    return WRDE_SYNTAX;
Packit 6c4009
Packit 6c4009
	  *result -= arg;
Packit 6c4009
	}
Packit 6c4009
      else break;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
parse_arith (char **word, size_t *word_length, size_t *max_length,
Packit 6c4009
	     const char *words, size_t *offset, int flags, int bracket)
Packit 6c4009
{
Packit 6c4009
  /* We are poised just after "$((" or "$[" */
Packit 6c4009
  int error;
Packit 6c4009
  int paren_depth = 1;
Packit 6c4009
  size_t expr_length;
Packit 6c4009
  size_t expr_maxlen;
Packit 6c4009
  char *expr;
Packit 6c4009
Packit 6c4009
  expr = w_newword (&expr_length, &expr_maxlen);
Packit 6c4009
  for (; words[*offset]; ++(*offset))
Packit 6c4009
    {
Packit 6c4009
      switch (words[*offset])
Packit 6c4009
	{
Packit 6c4009
	case '$':
Packit 6c4009
	  error = parse_dollars (&expr, &expr_length, &expr_maxlen,
Packit 6c4009
				 words, offset, flags, NULL, NULL, NULL, 1);
Packit 6c4009
	  /* The ``1'' here is to tell parse_dollars not to
Packit 6c4009
	   * split the fields.
Packit 6c4009
	   */
Packit 6c4009
	  if (error)
Packit 6c4009
	    {
Packit 6c4009
	      free (expr);
Packit 6c4009
	      return error;
Packit 6c4009
	    }
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case '`':
Packit 6c4009
	  (*offset)++;
Packit 6c4009
	  error = parse_backtick (&expr, &expr_length, &expr_maxlen,
Packit 6c4009
				  words, offset, flags, NULL, NULL, NULL);
Packit 6c4009
	  /* The first NULL here is to tell parse_backtick not to
Packit 6c4009
	   * split the fields.
Packit 6c4009
	   */
Packit 6c4009
	  if (error)
Packit 6c4009
	    {
Packit 6c4009
	      free (expr);
Packit 6c4009
	      return error;
Packit 6c4009
	    }
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case '\\':
Packit 6c4009
	  error = parse_qtd_backslash (&expr, &expr_length, &expr_maxlen,
Packit 6c4009
				       words, offset);
Packit 6c4009
	  if (error)
Packit 6c4009
	    {
Packit 6c4009
	      free (expr);
Packit 6c4009
	      return error;
Packit 6c4009
	    }
Packit 6c4009
	  /* I think that a backslash within an
Packit 6c4009
	   * arithmetic expansion is bound to
Packit 6c4009
	   * cause an error sooner or later anyway though.
Packit 6c4009
	   */
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case ')':
Packit 6c4009
	  if (--paren_depth == 0)
Packit 6c4009
	    {
Packit 6c4009
	      char result[21];	/* 21 = ceil(log10(2^64)) + 1 */
Packit 6c4009
	      long int numresult = 0;
Packit 6c4009
	      long long int convertme;
Packit 6c4009
Packit 6c4009
	      if (bracket || words[1 + *offset] != ')')
Packit 6c4009
		{
Packit 6c4009
		  free (expr);
Packit 6c4009
		  return WRDE_SYNTAX;
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
	      ++(*offset);
Packit 6c4009
Packit 6c4009
	      /* Go - evaluate. */
Packit 6c4009
	      if (*expr && eval_expr (expr, &numresult) != 0)
Packit 6c4009
		{
Packit 6c4009
		  free (expr);
Packit 6c4009
		  return WRDE_SYNTAX;
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
	      if (numresult < 0)
Packit 6c4009
		{
Packit 6c4009
		  convertme = -numresult;
Packit 6c4009
		  *word = w_addchar (*word, word_length, max_length, '-');
Packit 6c4009
		  if (!*word)
Packit 6c4009
		    {
Packit 6c4009
		      free (expr);
Packit 6c4009
		      return WRDE_NOSPACE;
Packit 6c4009
		    }
Packit 6c4009
		}
Packit 6c4009
	      else
Packit 6c4009
		convertme = numresult;
Packit 6c4009
Packit 6c4009
	      result[20] = '\0';
Packit 6c4009
	      *word = w_addstr (*word, word_length, max_length,
Packit 6c4009
				_itoa (convertme, &result[20], 10, 0));
Packit 6c4009
	      free (expr);
Packit 6c4009
	      return *word ? 0 : WRDE_NOSPACE;
Packit 6c4009
	    }
Packit 6c4009
	  expr = w_addchar (expr, &expr_length, &expr_maxlen, words[*offset]);
Packit 6c4009
	  if (expr == NULL)
Packit 6c4009
	    return WRDE_NOSPACE;
Packit 6c4009
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case ']':
Packit 6c4009
	  if (bracket && paren_depth == 1)
Packit 6c4009
	    {
Packit 6c4009
	      char result[21];	/* 21 = ceil(log10(2^64)) + 1 */
Packit 6c4009
	      long int numresult = 0;
Packit 6c4009
Packit 6c4009
	      /* Go - evaluate. */
Packit 6c4009
	      if (*expr && eval_expr (expr, &numresult) != 0)
Packit 6c4009
		{
Packit 6c4009
		  free (expr);
Packit 6c4009
		  return WRDE_SYNTAX;
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
	      result[20] = '\0';
Packit 6c4009
	      *word = w_addstr (*word, word_length, max_length,
Packit 6c4009
				_itoa_word (numresult, &result[20], 10, 0));
Packit 6c4009
	      free (expr);
Packit 6c4009
	      return *word ? 0 : WRDE_NOSPACE;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  free (expr);
Packit 6c4009
	  return WRDE_SYNTAX;
Packit 6c4009
Packit 6c4009
	case '\n':
Packit 6c4009
	case ';':
Packit 6c4009
	case '{':
Packit 6c4009
	case '}':
Packit 6c4009
	  free (expr);
Packit 6c4009
	  return WRDE_BADCHAR;
Packit 6c4009
Packit 6c4009
	case '(':
Packit 6c4009
	  ++paren_depth;
Packit 6c4009
	default:
Packit 6c4009
	  expr = w_addchar (expr, &expr_length, &expr_maxlen, words[*offset]);
Packit 6c4009
	  if (expr == NULL)
Packit 6c4009
	    return WRDE_NOSPACE;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Premature end */
Packit 6c4009
  free (expr);
Packit 6c4009
  return WRDE_SYNTAX;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Function called by child process in exec_comm() */
Packit 6c4009
static inline void
Packit 6c4009
__attribute__ ((always_inline))
Packit 6c4009
exec_comm_child (char *comm, int *fildes, int showerr, int noexec)
Packit 6c4009
{
Packit 6c4009
  const char *args[4] = { _PATH_BSHELL, "-c", comm, NULL };
Packit 6c4009
Packit 6c4009
  /* Execute the command, or just check syntax? */
Packit 6c4009
  if (noexec)
Packit 6c4009
    args[1] = "-nc";
Packit 6c4009
Packit 6c4009
  /* Redirect output.  */
Packit 6c4009
  if (__glibc_likely (fildes[1] != STDOUT_FILENO))
Packit 6c4009
    {
Packit 6c4009
      __dup2 (fildes[1], STDOUT_FILENO);
Packit 6c4009
      __close (fildes[1]);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    /* Reset the close-on-exec flag (if necessary).  */
Packit 6c4009
    __fcntl (fildes[1], F_SETFD, 0);
Packit 6c4009
Packit 6c4009
  /* Redirect stderr to /dev/null if we have to.  */
Packit 6c4009
  if (showerr == 0)
Packit 6c4009
    {
Packit 6c4009
      struct stat64 st;
Packit 6c4009
      int fd;
Packit 6c4009
      __close (STDERR_FILENO);
Packit 6c4009
      fd = __open (_PATH_DEVNULL, O_WRONLY);
Packit 6c4009
      if (fd >= 0 && fd != STDERR_FILENO)
Packit 6c4009
	{
Packit 6c4009
	  __dup2 (fd, STDERR_FILENO);
Packit 6c4009
	  __close (fd);
Packit 6c4009
	}
Packit 6c4009
      /* Be paranoid.  Check that we actually opened the /dev/null
Packit 6c4009
	 device.  */
Packit 6c4009
      if (__builtin_expect (__fxstat64 (_STAT_VER, STDERR_FILENO, &st), 0) != 0
Packit 6c4009
	  || __builtin_expect (S_ISCHR (st.st_mode), 1) == 0
Packit 6c4009
#if defined DEV_NULL_MAJOR && defined DEV_NULL_MINOR
Packit 6c4009
	  || st.st_rdev != __gnu_dev_makedev (DEV_NULL_MAJOR, DEV_NULL_MINOR)
Packit 6c4009
#endif
Packit 6c4009
	  )
Packit 6c4009
	/* It's not the /dev/null device.  Stop right here.  The
Packit 6c4009
	   problem is: how do we stop?  We use _exit() with an
Packit 6c4009
	   hopefully unusual exit code.  */
Packit 6c4009
	_exit (90);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Make sure the subshell doesn't field-split on our behalf. */
Packit 6c4009
  __unsetenv ("IFS");
Packit 6c4009
Packit 6c4009
  __close (fildes[0]);
Packit 6c4009
  __execve (_PATH_BSHELL, (char *const *) args, __environ);
Packit 6c4009
Packit 6c4009
  /* Bad.  What now?  */
Packit 6c4009
  abort ();
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Function to execute a command and retrieve the results */
Packit 6c4009
/* pwordexp contains NULL if field-splitting is forbidden */
Packit 6c4009
static int
Packit 6c4009
exec_comm (char *comm, char **word, size_t *word_length, size_t *max_length,
Packit 6c4009
	   int flags, wordexp_t *pwordexp, const char *ifs,
Packit 6c4009
	   const char *ifs_white)
Packit 6c4009
{
Packit 6c4009
  int fildes[2];
Packit 6c4009
#define bufsize 128
Packit 6c4009
  int buflen;
Packit 6c4009
  int i;
Packit 6c4009
  int status = 0;
Packit 6c4009
  size_t maxnewlines = 0;
Packit 6c4009
  char buffer[bufsize];
Packit 6c4009
  pid_t pid;
Packit 6c4009
  int noexec = 0;
Packit 6c4009
Packit 6c4009
  /* Do nothing if command substitution should not succeed.  */
Packit 6c4009
  if (flags & WRDE_NOCMD)
Packit 6c4009
    return WRDE_CMDSUB;
Packit 6c4009
Packit 6c4009
  /* Don't fork() unless necessary */
Packit 6c4009
  if (!comm || !*comm)
Packit 6c4009
    return 0;
Packit 6c4009
Packit 6c4009
  if (__pipe2 (fildes, O_CLOEXEC) < 0)
Packit 6c4009
    return WRDE_NOSPACE;
Packit 6c4009
Packit 6c4009
 again:
Packit 6c4009
  if ((pid = __fork ()) < 0)
Packit 6c4009
    {
Packit 6c4009
      /* Bad */
Packit 6c4009
      __close (fildes[0]);
Packit 6c4009
      __close (fildes[1]);
Packit 6c4009
      return WRDE_NOSPACE;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pid == 0)
Packit 6c4009
    exec_comm_child (comm, fildes, noexec ? 0 : flags & WRDE_SHOWERR, noexec);
Packit 6c4009
Packit 6c4009
  /* Parent */
Packit 6c4009
Packit 6c4009
  /* If we are just testing the syntax, only wait.  */
Packit 6c4009
  if (noexec)
Packit 6c4009
    return (TEMP_FAILURE_RETRY (__waitpid (pid, &status, 0)) == pid
Packit 6c4009
	    && status != 0) ? WRDE_SYNTAX : 0;
Packit 6c4009
Packit 6c4009
  __close (fildes[1]);
Packit 6c4009
  fildes[1] = -1;
Packit 6c4009
Packit 6c4009
  if (!pwordexp)
Packit 6c4009
    /* Quoted - no field splitting */
Packit 6c4009
    {
Packit 6c4009
      while (1)
Packit 6c4009
	{
Packit 6c4009
	  if ((buflen = TEMP_FAILURE_RETRY (__read (fildes[0], buffer,
Packit 6c4009
						    bufsize))) < 1)
Packit 6c4009
	    {
Packit 6c4009
	      /* If read returned 0 then the process has closed its
Packit 6c4009
		 stdout.  Don't use WNOHANG in that case to avoid busy
Packit 6c4009
		 looping until the process eventually exits.  */
Packit 6c4009
	      if (TEMP_FAILURE_RETRY (__waitpid (pid, &status,
Packit 6c4009
						 buflen == 0 ? 0 : WNOHANG))
Packit 6c4009
		  == 0)
Packit 6c4009
		continue;
Packit 6c4009
	      if ((buflen = TEMP_FAILURE_RETRY (__read (fildes[0], buffer,
Packit 6c4009
							bufsize))) < 1)
Packit 6c4009
		break;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  maxnewlines += buflen;
Packit 6c4009
Packit 6c4009
	  *word = w_addmem (*word, word_length, max_length, buffer, buflen);
Packit 6c4009
	  if (*word == NULL)
Packit 6c4009
	    goto no_space;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    /* Not quoted - split fields */
Packit 6c4009
    {
Packit 6c4009
      int copying = 0;
Packit 6c4009
      /* 'copying' is:
Packit 6c4009
       *  0 when searching for first character in a field not IFS white space
Packit 6c4009
       *  1 when copying the text of a field
Packit 6c4009
       *  2 when searching for possible non-whitespace IFS
Packit 6c4009
       *  3 when searching for non-newline after copying field
Packit 6c4009
       */
Packit 6c4009
Packit 6c4009
      while (1)
Packit 6c4009
	{
Packit 6c4009
	  if ((buflen = TEMP_FAILURE_RETRY (__read (fildes[0], buffer,
Packit 6c4009
						    bufsize))) < 1)
Packit 6c4009
	    {
Packit 6c4009
	      /* If read returned 0 then the process has closed its
Packit 6c4009
		 stdout.  Don't use WNOHANG in that case to avoid busy
Packit 6c4009
		 looping until the process eventually exits.  */
Packit 6c4009
	      if (TEMP_FAILURE_RETRY (__waitpid (pid, &status,
Packit 6c4009
						 buflen == 0 ? 0 : WNOHANG))
Packit 6c4009
		  == 0)
Packit 6c4009
		continue;
Packit 6c4009
	      if ((buflen = TEMP_FAILURE_RETRY (__read (fildes[0], buffer,
Packit 6c4009
							bufsize))) < 1)
Packit 6c4009
		break;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  for (i = 0; i < buflen; ++i)
Packit 6c4009
	    {
Packit 6c4009
	      if (strchr (ifs, buffer[i]) != NULL)
Packit 6c4009
		{
Packit 6c4009
		  /* Current character is IFS */
Packit 6c4009
		  if (strchr (ifs_white, buffer[i]) == NULL)
Packit 6c4009
		    {
Packit 6c4009
		      /* Current character is IFS but not whitespace */
Packit 6c4009
		      if (copying == 2)
Packit 6c4009
			{
Packit 6c4009
			  /*            current character
Packit 6c4009
			   *                   |
Packit 6c4009
			   *                   V
Packit 6c4009
			   * eg: text<space><comma><space>moretext
Packit 6c4009
			   *
Packit 6c4009
			   * So, strip whitespace IFS (like at the start)
Packit 6c4009
			   */
Packit 6c4009
			  copying = 0;
Packit 6c4009
			  continue;
Packit 6c4009
			}
Packit 6c4009
Packit 6c4009
		      copying = 0;
Packit 6c4009
		      /* fall through and delimit field.. */
Packit 6c4009
		    }
Packit 6c4009
		  else
Packit 6c4009
		    {
Packit 6c4009
		      if (buffer[i] == '\n')
Packit 6c4009
			{
Packit 6c4009
			  /* Current character is (IFS) newline */
Packit 6c4009
Packit 6c4009
			  /* If copying a field, this is the end of it,
Packit 6c4009
			     but maybe all that's left is trailing newlines.
Packit 6c4009
			     So start searching for a non-newline. */
Packit 6c4009
			  if (copying == 1)
Packit 6c4009
			    copying = 3;
Packit 6c4009
Packit 6c4009
			  continue;
Packit 6c4009
			}
Packit 6c4009
		      else
Packit 6c4009
			{
Packit 6c4009
			  /* Current character is IFS white space, but
Packit 6c4009
			     not a newline */
Packit 6c4009
Packit 6c4009
			  /* If not either copying a field or searching
Packit 6c4009
			     for non-newline after a field, ignore it */
Packit 6c4009
			  if (copying != 1 && copying != 3)
Packit 6c4009
			    continue;
Packit 6c4009
Packit 6c4009
			  /* End of field (search for non-ws IFS afterwards) */
Packit 6c4009
			  copying = 2;
Packit 6c4009
			}
Packit 6c4009
		    }
Packit 6c4009
Packit 6c4009
		  /* First IFS white space (non-newline), or IFS non-whitespace.
Packit 6c4009
		   * Delimit the field.  Nulls are converted by w_addword. */
Packit 6c4009
		  if (w_addword (pwordexp, *word) == WRDE_NOSPACE)
Packit 6c4009
		    goto no_space;
Packit 6c4009
Packit 6c4009
		  *word = w_newword (word_length, max_length);
Packit 6c4009
Packit 6c4009
		  maxnewlines = 0;
Packit 6c4009
		  /* fall back round the loop.. */
Packit 6c4009
		}
Packit 6c4009
	      else
Packit 6c4009
		{
Packit 6c4009
		  /* Not IFS character */
Packit 6c4009
Packit 6c4009
		  if (copying == 3)
Packit 6c4009
		    {
Packit 6c4009
		      /* Nothing but (IFS) newlines since the last field,
Packit 6c4009
			 so delimit it here before starting new word */
Packit 6c4009
		      if (w_addword (pwordexp, *word) == WRDE_NOSPACE)
Packit 6c4009
			goto no_space;
Packit 6c4009
Packit 6c4009
		      *word = w_newword (word_length, max_length);
Packit 6c4009
		    }
Packit 6c4009
Packit 6c4009
		  copying = 1;
Packit 6c4009
Packit 6c4009
		  if (buffer[i] == '\n') /* happens if newline not in IFS */
Packit 6c4009
		    maxnewlines++;
Packit 6c4009
		  else
Packit 6c4009
		    maxnewlines = 0;
Packit 6c4009
Packit 6c4009
		  *word = w_addchar (*word, word_length, max_length,
Packit 6c4009
				     buffer[i]);
Packit 6c4009
		  if (*word == NULL)
Packit 6c4009
		    goto no_space;
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Chop off trailing newlines (required by POSIX.2)  */
Packit 6c4009
  /* Ensure we don't go back further than the beginning of the
Packit 6c4009
     substitution (i.e. remove maxnewlines bytes at most) */
Packit 6c4009
  while (maxnewlines-- != 0 &&
Packit 6c4009
	 *word_length > 0 && (*word)[*word_length - 1] == '\n')
Packit 6c4009
    {
Packit 6c4009
      (*word)[--*word_length] = '\0';
Packit 6c4009
Packit 6c4009
      /* If the last word was entirely newlines, turn it into a new word
Packit 6c4009
       * which can be ignored if there's nothing following it. */
Packit 6c4009
      if (*word_length == 0)
Packit 6c4009
	{
Packit 6c4009
	  free (*word);
Packit 6c4009
	  *word = w_newword (word_length, max_length);
Packit 6c4009
	  break;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  __close (fildes[0]);
Packit 6c4009
  fildes[0] = -1;
Packit 6c4009
Packit 6c4009
  /* Check for syntax error (re-execute but with "-n" flag) */
Packit 6c4009
  if (buflen < 1 && status != 0)
Packit 6c4009
    {
Packit 6c4009
      noexec = 1;
Packit 6c4009
      goto again;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
Packit 6c4009
no_space:
Packit 6c4009
  __kill (pid, SIGKILL);
Packit 6c4009
  TEMP_FAILURE_RETRY (__waitpid (pid, NULL, 0));
Packit 6c4009
  __close (fildes[0]);
Packit 6c4009
  return WRDE_NOSPACE;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
parse_comm (char **word, size_t *word_length, size_t *max_length,
Packit 6c4009
	    const char *words, size_t *offset, int flags, wordexp_t *pwordexp,
Packit 6c4009
	    const char *ifs, const char *ifs_white)
Packit 6c4009
{
Packit 6c4009
  /* We are poised just after "$(" */
Packit 6c4009
  int paren_depth = 1;
Packit 6c4009
  int error = 0;
Packit 6c4009
  int quoted = 0; /* 1 for singly-quoted, 2 for doubly-quoted */
Packit 6c4009
  size_t comm_length;
Packit 6c4009
  size_t comm_maxlen;
Packit 6c4009
  char *comm = w_newword (&comm_length, &comm_maxlen);
Packit 6c4009
Packit 6c4009
  for (; words[*offset]; ++(*offset))
Packit 6c4009
    {
Packit 6c4009
      switch (words[*offset])
Packit 6c4009
	{
Packit 6c4009
	case '\'':
Packit 6c4009
	  if (quoted == 0)
Packit 6c4009
	    quoted = 1;
Packit 6c4009
	  else if (quoted == 1)
Packit 6c4009
	    quoted = 0;
Packit 6c4009
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case '"':
Packit 6c4009
	  if (quoted == 0)
Packit 6c4009
	    quoted = 2;
Packit 6c4009
	  else if (quoted == 2)
Packit 6c4009
	    quoted = 0;
Packit 6c4009
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case ')':
Packit 6c4009
	  if (!quoted && --paren_depth == 0)
Packit 6c4009
	    {
Packit 6c4009
	      /* Go -- give script to the shell */
Packit 6c4009
	      if (comm)
Packit 6c4009
		{
Packit 6c4009
#ifdef __libc_ptf_call
Packit 6c4009
		  /* We do not want the exec_comm call to be cut short
Packit 6c4009
		     by a thread cancellation since cleanup is very
Packit 6c4009
		     ugly.  Therefore disable cancellation for
Packit 6c4009
		     now.  */
Packit 6c4009
		  // XXX Ideally we do want the thread being cancelable.
Packit 6c4009
		  // XXX If demand is there we'll change it.
Packit 6c4009
		  int state = PTHREAD_CANCEL_ENABLE;
Packit 6c4009
		  __libc_ptf_call (__pthread_setcancelstate,
Packit 6c4009
				   (PTHREAD_CANCEL_DISABLE, &state), 0);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
		  error = exec_comm (comm, word, word_length, max_length,
Packit 6c4009
				     flags, pwordexp, ifs, ifs_white);
Packit 6c4009
Packit 6c4009
#ifdef __libc_ptf_call
Packit 6c4009
		  __libc_ptf_call (__pthread_setcancelstate,
Packit 6c4009
				   (state, NULL), 0);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
		  free (comm);
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
	      return error;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  /* This is just part of the script */
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case '(':
Packit 6c4009
	  if (!quoted)
Packit 6c4009
	    ++paren_depth;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      comm = w_addchar (comm, &comm_length, &comm_maxlen, words[*offset]);
Packit 6c4009
      if (comm == NULL)
Packit 6c4009
	return WRDE_NOSPACE;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Premature end.  */
Packit 6c4009
  free (comm);
Packit 6c4009
Packit 6c4009
  return WRDE_SYNTAX;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define CHAR_IN_SET(ch, char_set) \
Packit 6c4009
  (memchr (char_set "", ch, sizeof (char_set) - 1) != NULL)
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
parse_param (char **word, size_t *word_length, size_t *max_length,
Packit 6c4009
	     const char *words, size_t *offset, int flags, wordexp_t *pwordexp,
Packit 6c4009
	     const char *ifs, const char *ifs_white, int quoted)
Packit 6c4009
{
Packit 6c4009
  /* We are poised just after "$" */
Packit 6c4009
  enum action
Packit 6c4009
  {
Packit 6c4009
    ACT_NONE,
Packit 6c4009
    ACT_RP_SHORT_LEFT = '#',
Packit 6c4009
    ACT_RP_LONG_LEFT = 'L',
Packit 6c4009
    ACT_RP_SHORT_RIGHT = '%',
Packit 6c4009
    ACT_RP_LONG_RIGHT = 'R',
Packit 6c4009
    ACT_NULL_ERROR = '?',
Packit 6c4009
    ACT_NULL_SUBST = '-',
Packit 6c4009
    ACT_NONNULL_SUBST = '+',
Packit 6c4009
    ACT_NULL_ASSIGN = '='
Packit 6c4009
  };
Packit 6c4009
  size_t env_length;
Packit 6c4009
  size_t env_maxlen;
Packit 6c4009
  size_t pat_length;
Packit 6c4009
  size_t pat_maxlen;
Packit 6c4009
  size_t start = *offset;
Packit 6c4009
  char *env;
Packit 6c4009
  char *pattern;
Packit 6c4009
  char *value = NULL;
Packit 6c4009
  enum action action = ACT_NONE;
Packit 6c4009
  int depth = 0;
Packit 6c4009
  int colon_seen = 0;
Packit 6c4009
  int seen_hash = 0;
Packit 6c4009
  int free_value = 0;
Packit 6c4009
  int pattern_is_quoted = 0; /* 1 for singly-quoted, 2 for doubly-quoted */
Packit 6c4009
  int error;
Packit 6c4009
  int special = 0;
Packit 6c4009
  char buffer[21];
Packit 6c4009
  int brace = words[*offset] == '{';
Packit 6c4009
Packit 6c4009
  env = w_newword (&env_length, &env_maxlen);
Packit 6c4009
  pattern = w_newword (&pat_length, &pat_maxlen);
Packit 6c4009
Packit 6c4009
  if (brace)
Packit 6c4009
    ++*offset;
Packit 6c4009
Packit 6c4009
  /* First collect the parameter name. */
Packit 6c4009
Packit 6c4009
  if (words[*offset] == '#')
Packit 6c4009
    {
Packit 6c4009
      seen_hash = 1;
Packit 6c4009
      if (!brace)
Packit 6c4009
	goto envsubst;
Packit 6c4009
      ++*offset;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (isalpha (words[*offset]) || words[*offset] == '_')
Packit 6c4009
    {
Packit 6c4009
      /* Normal parameter name. */
Packit 6c4009
      do
Packit 6c4009
	{
Packit 6c4009
	  env = w_addchar (env, &env_length, &env_maxlen,
Packit 6c4009
			   words[*offset]);
Packit 6c4009
	  if (env == NULL)
Packit 6c4009
	    goto no_space;
Packit 6c4009
	}
Packit 6c4009
      while (isalnum (words[++*offset]) || words[*offset] == '_');
Packit 6c4009
    }
Packit 6c4009
  else if (isdigit (words[*offset]))
Packit 6c4009
    {
Packit 6c4009
      /* Numeric parameter name. */
Packit 6c4009
      special = 1;
Packit 6c4009
      do
Packit 6c4009
	{
Packit 6c4009
	  env = w_addchar (env, &env_length, &env_maxlen,
Packit 6c4009
			   words[*offset]);
Packit 6c4009
	  if (env == NULL)
Packit 6c4009
	    goto no_space;
Packit 6c4009
	  if (!brace)
Packit 6c4009
	    goto envsubst;
Packit 6c4009
	}
Packit 6c4009
      while (isdigit(words[++*offset]));
Packit 6c4009
    }
Packit 6c4009
  else if (CHAR_IN_SET (words[*offset], "*@$"))
Packit 6c4009
    {
Packit 6c4009
      /* Special parameter. */
Packit 6c4009
      special = 1;
Packit 6c4009
      env = w_addchar (env, &env_length, &env_maxlen,
Packit 6c4009
		       words[*offset]);
Packit 6c4009
      if (env == NULL)
Packit 6c4009
	goto no_space;
Packit 6c4009
      ++*offset;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (brace)
Packit 6c4009
	goto syntax;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (brace)
Packit 6c4009
    {
Packit 6c4009
      /* Check for special action to be applied to the value. */
Packit 6c4009
      switch (words[*offset])
Packit 6c4009
	{
Packit 6c4009
	case '}':
Packit 6c4009
	  /* Evaluate. */
Packit 6c4009
	  goto envsubst;
Packit 6c4009
Packit 6c4009
	case '#':
Packit 6c4009
	  action = ACT_RP_SHORT_LEFT;
Packit 6c4009
	  if (words[1 + *offset] == '#')
Packit 6c4009
	    {
Packit 6c4009
	      ++*offset;
Packit 6c4009
	      action = ACT_RP_LONG_LEFT;
Packit 6c4009
	    }
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case '%':
Packit 6c4009
	  action = ACT_RP_SHORT_RIGHT;
Packit 6c4009
	  if (words[1 + *offset] == '%')
Packit 6c4009
	    {
Packit 6c4009
	      ++*offset;
Packit 6c4009
	      action = ACT_RP_LONG_RIGHT;
Packit 6c4009
	    }
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case ':':
Packit 6c4009
	  if (!CHAR_IN_SET (words[1 + *offset], "-=?+"))
Packit 6c4009
	    goto syntax;
Packit 6c4009
Packit 6c4009
	  colon_seen = 1;
Packit 6c4009
	  action = words[++*offset];
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case '-':
Packit 6c4009
	case '=':
Packit 6c4009
	case '?':
Packit 6c4009
	case '+':
Packit 6c4009
	  action = words[*offset];
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	default:
Packit 6c4009
	  goto syntax;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Now collect the pattern, but don't expand it yet. */
Packit 6c4009
      ++*offset;
Packit 6c4009
      for (; words[*offset]; ++(*offset))
Packit 6c4009
	{
Packit 6c4009
	  switch (words[*offset])
Packit 6c4009
	    {
Packit 6c4009
	    case '{':
Packit 6c4009
	      if (!pattern_is_quoted)
Packit 6c4009
		++depth;
Packit 6c4009
	      break;
Packit 6c4009
Packit 6c4009
	    case '}':
Packit 6c4009
	      if (!pattern_is_quoted)
Packit 6c4009
		{
Packit 6c4009
		  if (depth == 0)
Packit 6c4009
		    goto envsubst;
Packit 6c4009
		  --depth;
Packit 6c4009
		}
Packit 6c4009
	      break;
Packit 6c4009
Packit 6c4009
	    case '\\':
Packit 6c4009
	      if (pattern_is_quoted)
Packit 6c4009
		/* Quoted; treat as normal character. */
Packit 6c4009
		break;
Packit 6c4009
Packit 6c4009
	      /* Otherwise, it's an escape: next character is literal. */
Packit 6c4009
	      if (words[++*offset] == '\0')
Packit 6c4009
		goto syntax;
Packit 6c4009
Packit 6c4009
	      pattern = w_addchar (pattern, &pat_length, &pat_maxlen, '\\');
Packit 6c4009
	      if (pattern == NULL)
Packit 6c4009
		goto no_space;
Packit 6c4009
Packit 6c4009
	      break;
Packit 6c4009
Packit 6c4009
	    case '\'':
Packit 6c4009
	      if (pattern_is_quoted == 0)
Packit 6c4009
		pattern_is_quoted = 1;
Packit 6c4009
	      else if (pattern_is_quoted == 1)
Packit 6c4009
		pattern_is_quoted = 0;
Packit 6c4009
Packit 6c4009
	      break;
Packit 6c4009
Packit 6c4009
	    case '"':
Packit 6c4009
	      if (pattern_is_quoted == 0)
Packit 6c4009
		pattern_is_quoted = 2;
Packit 6c4009
	      else if (pattern_is_quoted == 2)
Packit 6c4009
		pattern_is_quoted = 0;
Packit 6c4009
Packit 6c4009
	      break;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  pattern = w_addchar (pattern, &pat_length, &pat_maxlen,
Packit 6c4009
			       words[*offset]);
Packit 6c4009
	  if (pattern == NULL)
Packit 6c4009
	    goto no_space;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* End of input string -- remember to reparse the character that we
Packit 6c4009
   * stopped at.  */
Packit 6c4009
  --(*offset);
Packit 6c4009
Packit 6c4009
envsubst:
Packit 6c4009
  if (words[start] == '{' && words[*offset] != '}')
Packit 6c4009
    goto syntax;
Packit 6c4009
Packit 6c4009
  if (env == NULL)
Packit 6c4009
    {
Packit 6c4009
      if (seen_hash)
Packit 6c4009
	{
Packit 6c4009
	  /* $# expands to the number of positional parameters */
Packit 6c4009
	  buffer[20] = '\0';
Packit 6c4009
	  value = _itoa_word (__libc_argc - 1, &buffer[20], 10, 0);
Packit 6c4009
	  seen_hash = 0;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* Just $ on its own */
Packit 6c4009
	  *offset = start - 1;
Packit 6c4009
	  *word = w_addchar (*word, word_length, max_length, '$');
Packit 6c4009
	  return *word ? 0 : WRDE_NOSPACE;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  /* Is it a numeric parameter? */
Packit 6c4009
  else if (isdigit (env[0]))
Packit 6c4009
    {
Packit 6c4009
      int n = atoi (env);
Packit 6c4009
Packit 6c4009
      if (n >= __libc_argc)
Packit 6c4009
	/* Substitute NULL. */
Packit 6c4009
	value = NULL;
Packit 6c4009
      else
Packit 6c4009
	/* Replace with appropriate positional parameter. */
Packit 6c4009
	value = __libc_argv[n];
Packit 6c4009
    }
Packit 6c4009
  /* Is it a special parameter? */
Packit 6c4009
  else if (special)
Packit 6c4009
    {
Packit 6c4009
      /* Is it `$$'? */
Packit 6c4009
      if (*env == '$')
Packit 6c4009
	{
Packit 6c4009
	  buffer[20] = '\0';
Packit 6c4009
	  value = _itoa_word (__getpid (), &buffer[20], 10, 0);
Packit 6c4009
	}
Packit 6c4009
      /* Is it `${#*}' or `${#@}'? */
Packit 6c4009
      else if ((*env == '*' || *env == '@') && seen_hash)
Packit 6c4009
	{
Packit 6c4009
	  buffer[20] = '\0';
Packit 6c4009
	  value = _itoa_word (__libc_argc > 0 ? __libc_argc - 1 : 0,
Packit 6c4009
			      &buffer[20], 10, 0);
Packit 6c4009
	  *word = w_addstr (*word, word_length, max_length, value);
Packit 6c4009
	  free (env);
Packit 6c4009
	  free (pattern);
Packit 6c4009
	  return *word ? 0 : WRDE_NOSPACE;
Packit 6c4009
	}
Packit 6c4009
      /* Is it `$*' or `$@' (unquoted) ? */
Packit 6c4009
      else if (*env == '*' || (*env == '@' && !quoted))
Packit 6c4009
	{
Packit 6c4009
	  size_t plist_len = 0;
Packit 6c4009
	  int p;
Packit 6c4009
	  char *end;
Packit 6c4009
Packit 6c4009
	  /* Build up value parameter by parameter (copy them) */
Packit 6c4009
	  for (p = 1; __libc_argv[p]; ++p)
Packit 6c4009
	    plist_len += strlen (__libc_argv[p]) + 1; /* for space */
Packit 6c4009
	  value = malloc (plist_len);
Packit 6c4009
	  if (value == NULL)
Packit 6c4009
	    goto no_space;
Packit 6c4009
	  end = value;
Packit 6c4009
	  *end = 0;
Packit 6c4009
	  for (p = 1; __libc_argv[p]; ++p)
Packit 6c4009
	    {
Packit 6c4009
	      if (p > 1)
Packit 6c4009
		*end++ = ' ';
Packit 6c4009
	      end = __stpcpy (end, __libc_argv[p]);
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  free_value = 1;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* Must be a quoted `$@' */
Packit 6c4009
	  assert (*env == '@' && quoted);
Packit 6c4009
Packit 6c4009
	  /* Each parameter is a separate word ("$@") */
Packit 6c4009
	  if (__libc_argc == 2)
Packit 6c4009
	    value = __libc_argv[1];
Packit 6c4009
	  else if (__libc_argc > 2)
Packit 6c4009
	    {
Packit 6c4009
	      int p;
Packit 6c4009
Packit 6c4009
	      /* Append first parameter to current word. */
Packit 6c4009
	      value = w_addstr (*word, word_length, max_length,
Packit 6c4009
				__libc_argv[1]);
Packit 6c4009
	      if (value == NULL || w_addword (pwordexp, value))
Packit 6c4009
		goto no_space;
Packit 6c4009
Packit 6c4009
	      for (p = 2; __libc_argv[p + 1]; p++)
Packit 6c4009
		{
Packit 6c4009
		  char *newword = __strdup (__libc_argv[p]);
Packit 6c4009
		  if (newword == NULL || w_addword (pwordexp, newword))
Packit 6c4009
		    goto no_space;
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
	      /* Start a new word with the last parameter. */
Packit 6c4009
	      *word = w_newword (word_length, max_length);
Packit 6c4009
	      value = __libc_argv[p];
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      free (env);
Packit 6c4009
	      free (pattern);
Packit 6c4009
	      return 0;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    value = getenv (env);
Packit 6c4009
Packit 6c4009
  if (value == NULL && (flags & WRDE_UNDEF))
Packit 6c4009
    {
Packit 6c4009
      /* Variable not defined. */
Packit 6c4009
      error = WRDE_BADVAL;
Packit 6c4009
      goto do_error;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (action != ACT_NONE)
Packit 6c4009
    {
Packit 6c4009
      int expand_pattern = 0;
Packit 6c4009
Packit 6c4009
      /* First, find out if we need to expand pattern (i.e. if we will
Packit 6c4009
       * use it). */
Packit 6c4009
      switch (action)
Packit 6c4009
	{
Packit 6c4009
	case ACT_RP_SHORT_LEFT:
Packit 6c4009
	case ACT_RP_LONG_LEFT:
Packit 6c4009
	case ACT_RP_SHORT_RIGHT:
Packit 6c4009
	case ACT_RP_LONG_RIGHT:
Packit 6c4009
	  /* Always expand for these. */
Packit 6c4009
	  expand_pattern = 1;
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case ACT_NULL_ERROR:
Packit 6c4009
	case ACT_NULL_SUBST:
Packit 6c4009
	case ACT_NULL_ASSIGN:
Packit 6c4009
	  if (!value || (!*value && colon_seen))
Packit 6c4009
	    /* If param is unset, or set but null and a colon has been seen,
Packit 6c4009
	       the expansion of the pattern will be needed. */
Packit 6c4009
	    expand_pattern = 1;
Packit 6c4009
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case ACT_NONNULL_SUBST:
Packit 6c4009
	  /* Expansion of word will be needed if parameter is set and not null,
Packit 6c4009
	     or set null but no colon has been seen. */
Packit 6c4009
	  if (value && (*value || !colon_seen))
Packit 6c4009
	    expand_pattern = 1;
Packit 6c4009
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	default:
Packit 6c4009
	  assert (! "Unrecognised action!");
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (expand_pattern)
Packit 6c4009
	{
Packit 6c4009
	  /* We need to perform tilde expansion, parameter expansion,
Packit 6c4009
	     command substitution, and arithmetic expansion.  We also
Packit 6c4009
	     have to be a bit careful with wildcard characters, as
Packit 6c4009
	     pattern might be given to fnmatch soon.  To do this, we
Packit 6c4009
	     convert quotes to escapes. */
Packit 6c4009
Packit 6c4009
	  char *expanded;
Packit 6c4009
	  size_t exp_len;
Packit 6c4009
	  size_t exp_maxl;
Packit 6c4009
	  char *p;
Packit 6c4009
	  int quoted = 0; /* 1: single quotes; 2: double */
Packit 6c4009
Packit 6c4009
	  expanded = w_newword (&exp_len, &exp_maxl);
Packit 6c4009
	  for (p = pattern; p && *p; p++)
Packit 6c4009
	    {
Packit 6c4009
	      size_t offset;
Packit 6c4009
Packit 6c4009
	      switch (*p)
Packit 6c4009
		{
Packit 6c4009
		case '"':
Packit 6c4009
		  if (quoted == 2)
Packit 6c4009
		    quoted = 0;
Packit 6c4009
		  else if (quoted == 0)
Packit 6c4009
		    quoted = 2;
Packit 6c4009
		  else break;
Packit 6c4009
Packit 6c4009
		  continue;
Packit 6c4009
Packit 6c4009
		case '\'':
Packit 6c4009
		  if (quoted == 1)
Packit 6c4009
		    quoted = 0;
Packit 6c4009
		  else if (quoted == 0)
Packit 6c4009
		    quoted = 1;
Packit 6c4009
		  else break;
Packit 6c4009
Packit 6c4009
		  continue;
Packit 6c4009
Packit 6c4009
		case '*':
Packit 6c4009
		case '?':
Packit 6c4009
		  if (quoted)
Packit 6c4009
		    {
Packit 6c4009
		      /* Convert quoted wildchar to escaped wildchar. */
Packit 6c4009
		      expanded = w_addchar (expanded, &exp_len,
Packit 6c4009
					    &exp_maxl, '\\');
Packit 6c4009
Packit 6c4009
		      if (expanded == NULL)
Packit 6c4009
			goto no_space;
Packit 6c4009
		    }
Packit 6c4009
		  break;
Packit 6c4009
Packit 6c4009
		case '$':
Packit 6c4009
		  offset = 0;
Packit 6c4009
		  error = parse_dollars (&expanded, &exp_len, &exp_maxl, p,
Packit 6c4009
					 &offset, flags, NULL, NULL, NULL, 1);
Packit 6c4009
		  if (error)
Packit 6c4009
		    {
Packit 6c4009
		      if (free_value)
Packit 6c4009
			free (value);
Packit 6c4009
Packit 6c4009
		      free (expanded);
Packit 6c4009
Packit 6c4009
		      goto do_error;
Packit 6c4009
		    }
Packit 6c4009
Packit 6c4009
		  p += offset;
Packit 6c4009
		  continue;
Packit 6c4009
Packit 6c4009
		case '~':
Packit 6c4009
		  if (quoted || exp_len)
Packit 6c4009
		    break;
Packit 6c4009
Packit 6c4009
		  offset = 0;
Packit 6c4009
		  error = parse_tilde (&expanded, &exp_len, &exp_maxl, p,
Packit 6c4009
				       &offset, 0);
Packit 6c4009
		  if (error)
Packit 6c4009
		    {
Packit 6c4009
		      if (free_value)
Packit 6c4009
			free (value);
Packit 6c4009
Packit 6c4009
		      free (expanded);
Packit 6c4009
Packit 6c4009
		      goto do_error;
Packit 6c4009
		    }
Packit 6c4009
Packit 6c4009
		  p += offset;
Packit 6c4009
		  continue;
Packit 6c4009
Packit 6c4009
		case '\\':
Packit 6c4009
		  expanded = w_addchar (expanded, &exp_len, &exp_maxl, '\\');
Packit 6c4009
		  ++p;
Packit 6c4009
		  assert (*p); /* checked when extracted initially */
Packit 6c4009
		  if (expanded == NULL)
Packit 6c4009
		    goto no_space;
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
	      expanded = w_addchar (expanded, &exp_len, &exp_maxl, *p);
Packit 6c4009
Packit 6c4009
	      if (expanded == NULL)
Packit 6c4009
		goto no_space;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  free (pattern);
Packit 6c4009
Packit 6c4009
	  pattern = expanded;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      switch (action)
Packit 6c4009
	{
Packit 6c4009
	case ACT_RP_SHORT_LEFT:
Packit 6c4009
	case ACT_RP_LONG_LEFT:
Packit 6c4009
	case ACT_RP_SHORT_RIGHT:
Packit 6c4009
	case ACT_RP_LONG_RIGHT:
Packit 6c4009
	  {
Packit 6c4009
	    char *p;
Packit 6c4009
	    char c;
Packit 6c4009
	    char *end;
Packit 6c4009
Packit 6c4009
	    if (value == NULL || pattern == NULL || *pattern == '\0')
Packit 6c4009
	      break;
Packit 6c4009
Packit 6c4009
	    end = value + strlen (value);
Packit 6c4009
Packit 6c4009
	    switch (action)
Packit 6c4009
	      {
Packit 6c4009
	      case ACT_RP_SHORT_LEFT:
Packit 6c4009
		for (p = value; p <= end; ++p)
Packit 6c4009
		  {
Packit 6c4009
		    c = *p;
Packit 6c4009
		    *p = '\0';
Packit 6c4009
		    if (fnmatch (pattern, value, 0) != FNM_NOMATCH)
Packit 6c4009
		      {
Packit 6c4009
			*p = c;
Packit 6c4009
			if (free_value)
Packit 6c4009
			  {
Packit 6c4009
			    char *newval = __strdup (p);
Packit 6c4009
			    if (newval == NULL)
Packit 6c4009
			      {
Packit 6c4009
				free (value);
Packit 6c4009
				goto no_space;
Packit 6c4009
			      }
Packit 6c4009
			    free (value);
Packit 6c4009
			    value = newval;
Packit 6c4009
			  }
Packit 6c4009
			else
Packit 6c4009
			  value = p;
Packit 6c4009
			break;
Packit 6c4009
		      }
Packit 6c4009
		    *p = c;
Packit 6c4009
		  }
Packit 6c4009
Packit 6c4009
		break;
Packit 6c4009
Packit 6c4009
	      case ACT_RP_LONG_LEFT:
Packit 6c4009
		for (p = end; p >= value; --p)
Packit 6c4009
		  {
Packit 6c4009
		    c = *p;
Packit 6c4009
		    *p = '\0';
Packit 6c4009
		    if (fnmatch (pattern, value, 0) != FNM_NOMATCH)
Packit 6c4009
		      {
Packit 6c4009
			*p = c;
Packit 6c4009
			if (free_value)
Packit 6c4009
			  {
Packit 6c4009
			    char *newval = __strdup (p);
Packit 6c4009
			    if (newval == NULL)
Packit 6c4009
			      {
Packit 6c4009
				free (value);
Packit 6c4009
				goto no_space;
Packit 6c4009
			      }
Packit 6c4009
			    free (value);
Packit 6c4009
			    value = newval;
Packit 6c4009
			  }
Packit 6c4009
			else
Packit 6c4009
			  value = p;
Packit 6c4009
			break;
Packit 6c4009
		      }
Packit 6c4009
		    *p = c;
Packit 6c4009
		  }
Packit 6c4009
Packit 6c4009
		break;
Packit 6c4009
Packit 6c4009
	      case ACT_RP_SHORT_RIGHT:
Packit 6c4009
		for (p = end; p >= value; --p)
Packit 6c4009
		  {
Packit 6c4009
		    if (fnmatch (pattern, p, 0) != FNM_NOMATCH)
Packit 6c4009
		      {
Packit 6c4009
			char *newval;
Packit 6c4009
			newval = malloc (p - value + 1);
Packit 6c4009
Packit 6c4009
			if (newval == NULL)
Packit 6c4009
			  {
Packit 6c4009
			    if (free_value)
Packit 6c4009
			      free (value);
Packit 6c4009
			    goto no_space;
Packit 6c4009
			  }
Packit 6c4009
Packit 6c4009
			*(char *) __mempcpy (newval, value, p - value) = '\0';
Packit 6c4009
			if (free_value)
Packit 6c4009
			  free (value);
Packit 6c4009
			value = newval;
Packit 6c4009
			free_value = 1;
Packit 6c4009
			break;
Packit 6c4009
		      }
Packit 6c4009
		  }
Packit 6c4009
Packit 6c4009
		break;
Packit 6c4009
Packit 6c4009
	      case ACT_RP_LONG_RIGHT:
Packit 6c4009
		for (p = value; p <= end; ++p)
Packit 6c4009
		  {
Packit 6c4009
		    if (fnmatch (pattern, p, 0) != FNM_NOMATCH)
Packit 6c4009
		      {
Packit 6c4009
			char *newval;
Packit 6c4009
			newval = malloc (p - value + 1);
Packit 6c4009
Packit 6c4009
			if (newval == NULL)
Packit 6c4009
			  {
Packit 6c4009
			    if (free_value)
Packit 6c4009
			      free (value);
Packit 6c4009
			    goto no_space;
Packit 6c4009
			  }
Packit 6c4009
Packit 6c4009
			*(char *) __mempcpy (newval, value, p - value) = '\0';
Packit 6c4009
			if (free_value)
Packit 6c4009
			  free (value);
Packit 6c4009
			value = newval;
Packit 6c4009
			free_value = 1;
Packit 6c4009
			break;
Packit 6c4009
		      }
Packit 6c4009
		  }
Packit 6c4009
Packit 6c4009
		break;
Packit 6c4009
Packit 6c4009
	      default:
Packit 6c4009
		break;
Packit 6c4009
	      }
Packit 6c4009
Packit 6c4009
	    break;
Packit 6c4009
	  }
Packit 6c4009
Packit 6c4009
	case ACT_NULL_ERROR:
Packit 6c4009
	  if (value && *value)
Packit 6c4009
	    /* Substitute parameter */
Packit 6c4009
	    break;
Packit 6c4009
Packit 6c4009
	  error = 0;
Packit 6c4009
	  if (!colon_seen && value)
Packit 6c4009
	    /* Substitute NULL */
Packit 6c4009
	    ;
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      const char *str = pattern;
Packit 6c4009
Packit 6c4009
	      if (str[0] == '\0')
Packit 6c4009
		str = _("parameter null or not set");
Packit 6c4009
Packit 6c4009
	      __fxprintf (NULL, "%s: %s\n", env, str);
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  if (free_value)
Packit 6c4009
	    free (value);
Packit 6c4009
	  goto do_error;
Packit 6c4009
Packit 6c4009
	case ACT_NULL_SUBST:
Packit 6c4009
	  if (value && *value)
Packit 6c4009
	    /* Substitute parameter */
Packit 6c4009
	    break;
Packit 6c4009
Packit 6c4009
	  if (free_value)
Packit 6c4009
	    free (value);
Packit 6c4009
Packit 6c4009
	  if (!colon_seen && value)
Packit 6c4009
	    /* Substitute NULL */
Packit 6c4009
	    goto success;
Packit 6c4009
Packit 6c4009
	  value = pattern ? __strdup (pattern) : pattern;
Packit 6c4009
	  free_value = 1;
Packit 6c4009
Packit 6c4009
	  if (pattern && !value)
Packit 6c4009
	    goto no_space;
Packit 6c4009
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case ACT_NONNULL_SUBST:
Packit 6c4009
	  if (value && (*value || !colon_seen))
Packit 6c4009
	    {
Packit 6c4009
	      if (free_value)
Packit 6c4009
		free (value);
Packit 6c4009
Packit 6c4009
	      value = pattern ? __strdup (pattern) : pattern;
Packit 6c4009
	      free_value = 1;
Packit 6c4009
Packit 6c4009
	      if (pattern && !value)
Packit 6c4009
		goto no_space;
Packit 6c4009
Packit 6c4009
	      break;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  /* Substitute NULL */
Packit 6c4009
	  if (free_value)
Packit 6c4009
	    free (value);
Packit 6c4009
	  goto success;
Packit 6c4009
Packit 6c4009
	case ACT_NULL_ASSIGN:
Packit 6c4009
	  if (value && *value)
Packit 6c4009
	    /* Substitute parameter */
Packit 6c4009
	    break;
Packit 6c4009
Packit 6c4009
	  if (!colon_seen && value)
Packit 6c4009
	    {
Packit 6c4009
	      /* Substitute NULL */
Packit 6c4009
	      if (free_value)
Packit 6c4009
		free (value);
Packit 6c4009
	      goto success;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  if (free_value)
Packit 6c4009
	    free (value);
Packit 6c4009
Packit 6c4009
	  value = pattern ? __strdup (pattern) : pattern;
Packit 6c4009
	  free_value = 1;
Packit 6c4009
Packit 6c4009
	  if (pattern && !value)
Packit 6c4009
	    goto no_space;
Packit 6c4009
Packit 6c4009
	  __setenv (env, value ?: "", 1);
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	default:
Packit 6c4009
	  assert (! "Unrecognised action!");
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  free (env);
Packit 6c4009
  env = NULL;
Packit 6c4009
  free (pattern);
Packit 6c4009
  pattern = NULL;
Packit 6c4009
Packit 6c4009
  if (seen_hash)
Packit 6c4009
    {
Packit 6c4009
      char param_length[21];
Packit 6c4009
      param_length[20] = '\0';
Packit 6c4009
      *word = w_addstr (*word, word_length, max_length,
Packit 6c4009
			_itoa_word (value ? strlen (value) : 0,
Packit 6c4009
				    &param_length[20], 10, 0));
Packit 6c4009
      if (free_value)
Packit 6c4009
	{
Packit 6c4009
	  assert (value != NULL);
Packit 6c4009
	  free (value);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      return *word ? 0 : WRDE_NOSPACE;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (value == NULL)
Packit 6c4009
    return 0;
Packit 6c4009
Packit 6c4009
  if (quoted || !pwordexp)
Packit 6c4009
    {
Packit 6c4009
      /* Quoted - no field split */
Packit 6c4009
      *word = w_addstr (*word, word_length, max_length, value);
Packit 6c4009
      if (free_value)
Packit 6c4009
	free (value);
Packit 6c4009
Packit 6c4009
      return *word ? 0 : WRDE_NOSPACE;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* Need to field-split */
Packit 6c4009
      char *value_copy = __strdup (value); /* Don't modify value */
Packit 6c4009
      char *field_begin = value_copy;
Packit 6c4009
      int seen_nonws_ifs = 0;
Packit 6c4009
Packit 6c4009
      if (free_value)
Packit 6c4009
	free (value);
Packit 6c4009
Packit 6c4009
      if (value_copy == NULL)
Packit 6c4009
	goto no_space;
Packit 6c4009
Packit 6c4009
      do
Packit 6c4009
	{
Packit 6c4009
	  char *field_end = field_begin;
Packit 6c4009
	  char *next_field;
Packit 6c4009
Packit 6c4009
	  /* If this isn't the first field, start a new word */
Packit 6c4009
	  if (field_begin != value_copy)
Packit 6c4009
	    {
Packit 6c4009
	      if (w_addword (pwordexp, *word) == WRDE_NOSPACE)
Packit 6c4009
		{
Packit 6c4009
		  free (value_copy);
Packit 6c4009
		  goto no_space;
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
	      *word = w_newword (word_length, max_length);
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  /* Skip IFS whitespace before the field */
Packit 6c4009
	  field_begin += strspn (field_begin, ifs_white);
Packit 6c4009
Packit 6c4009
	  if (!seen_nonws_ifs && *field_begin == 0)
Packit 6c4009
	    /* Nothing but whitespace */
Packit 6c4009
	    break;
Packit 6c4009
Packit 6c4009
	  /* Search for the end of the field */
Packit 6c4009
	  field_end = field_begin + strcspn (field_begin, ifs);
Packit 6c4009
Packit 6c4009
	  /* Set up pointer to the character after end of field and
Packit 6c4009
	     skip whitespace IFS after it. */
Packit 6c4009
	  next_field = field_end + strspn (field_end, ifs_white);
Packit 6c4009
Packit 6c4009
	  /* Skip at most one non-whitespace IFS character after the field */
Packit 6c4009
	  seen_nonws_ifs = 0;
Packit 6c4009
	  if (*next_field && strchr (ifs, *next_field))
Packit 6c4009
	    {
Packit 6c4009
	      seen_nonws_ifs = 1;
Packit 6c4009
	      next_field++;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  /* Null-terminate it */
Packit 6c4009
	  *field_end = 0;
Packit 6c4009
Packit 6c4009
	  /* Tag a copy onto the current word */
Packit 6c4009
	  *word = w_addstr (*word, word_length, max_length, field_begin);
Packit 6c4009
Packit 6c4009
	  if (*word == NULL && *field_begin != '\0')
Packit 6c4009
	    {
Packit 6c4009
	      free (value_copy);
Packit 6c4009
	      goto no_space;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  field_begin = next_field;
Packit 6c4009
	}
Packit 6c4009
      while (seen_nonws_ifs || *field_begin);
Packit 6c4009
Packit 6c4009
      free (value_copy);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
Packit 6c4009
success:
Packit 6c4009
  error = 0;
Packit 6c4009
  goto do_error;
Packit 6c4009
Packit 6c4009
no_space:
Packit 6c4009
  error = WRDE_NOSPACE;
Packit 6c4009
  goto do_error;
Packit 6c4009
Packit 6c4009
syntax:
Packit 6c4009
  error = WRDE_SYNTAX;
Packit 6c4009
Packit 6c4009
do_error:
Packit 6c4009
  free (env);
Packit 6c4009
Packit 6c4009
  free (pattern);
Packit 6c4009
Packit 6c4009
  return error;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#undef CHAR_IN_SET
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
parse_dollars (char **word, size_t *word_length, size_t *max_length,
Packit 6c4009
	       const char *words, size_t *offset, int flags,
Packit 6c4009
	       wordexp_t *pwordexp, const char *ifs, const char *ifs_white,
Packit 6c4009
	       int quoted)
Packit 6c4009
{
Packit 6c4009
  /* We are poised _at_ "$" */
Packit 6c4009
  switch (words[1 + *offset])
Packit 6c4009
    {
Packit 6c4009
    case '"':
Packit 6c4009
    case '\'':
Packit 6c4009
    case 0:
Packit 6c4009
      *word = w_addchar (*word, word_length, max_length, '$');
Packit 6c4009
      return *word ? 0 : WRDE_NOSPACE;
Packit 6c4009
Packit 6c4009
    case '(':
Packit 6c4009
      if (words[2 + *offset] == '(')
Packit 6c4009
	{
Packit 6c4009
	  /* Differentiate between $((1+3)) and $((echo);(ls)) */
Packit 6c4009
	  int i = 3 + *offset;
Packit 6c4009
	  int depth = 0;
Packit 6c4009
	  while (words[i] && !(depth == 0 && words[i] == ')'))
Packit 6c4009
	    {
Packit 6c4009
	      if (words[i] == '(')
Packit 6c4009
		++depth;
Packit 6c4009
	      else if (words[i] == ')')
Packit 6c4009
		--depth;
Packit 6c4009
Packit 6c4009
	      ++i;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  if (words[i] == ')' && words[i + 1] == ')')
Packit 6c4009
	    {
Packit 6c4009
	      (*offset) += 3;
Packit 6c4009
	      /* Call parse_arith -- 0 is for "no brackets" */
Packit 6c4009
	      return parse_arith (word, word_length, max_length, words, offset,
Packit 6c4009
				  flags, 0);
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      (*offset) += 2;
Packit 6c4009
      return parse_comm (word, word_length, max_length, words, offset, flags,
Packit 6c4009
			 quoted? NULL : pwordexp, ifs, ifs_white);
Packit 6c4009
Packit 6c4009
    case '[':
Packit 6c4009
      (*offset) += 2;
Packit 6c4009
      /* Call parse_arith -- 1 is for "brackets" */
Packit 6c4009
      return parse_arith (word, word_length, max_length, words, offset, flags,
Packit 6c4009
			  1);
Packit 6c4009
Packit 6c4009
    case '{':
Packit 6c4009
    default:
Packit 6c4009
      ++(*offset);	/* parse_param needs to know if "{" is there */
Packit 6c4009
      return parse_param (word, word_length, max_length, words, offset, flags,
Packit 6c4009
			   pwordexp, ifs, ifs_white, quoted);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
parse_backtick (char **word, size_t *word_length, size_t *max_length,
Packit 6c4009
		const char *words, size_t *offset, int flags,
Packit 6c4009
		wordexp_t *pwordexp, const char *ifs, const char *ifs_white)
Packit 6c4009
{
Packit 6c4009
  /* We are poised just after "`" */
Packit 6c4009
  int error;
Packit 6c4009
  int squoting = 0;
Packit 6c4009
  size_t comm_length;
Packit 6c4009
  size_t comm_maxlen;
Packit 6c4009
  char *comm = w_newword (&comm_length, &comm_maxlen);
Packit 6c4009
Packit 6c4009
  for (; words[*offset]; ++(*offset))
Packit 6c4009
    {
Packit 6c4009
      switch (words[*offset])
Packit 6c4009
	{
Packit 6c4009
	case '`':
Packit 6c4009
	  /* Go -- give the script to the shell */
Packit 6c4009
	  error = exec_comm (comm, word, word_length, max_length, flags,
Packit 6c4009
			     pwordexp, ifs, ifs_white);
Packit 6c4009
	  free (comm);
Packit 6c4009
	  return error;
Packit 6c4009
Packit 6c4009
	case '\\':
Packit 6c4009
	  if (squoting)
Packit 6c4009
	    {
Packit 6c4009
	      error = parse_qtd_backslash (&comm, &comm_length, &comm_maxlen,
Packit 6c4009
					   words, offset);
Packit 6c4009
Packit 6c4009
	      if (error)
Packit 6c4009
		{
Packit 6c4009
		  free (comm);
Packit 6c4009
		  return error;
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
	      break;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  error = parse_backslash (&comm, &comm_length, &comm_maxlen, words,
Packit 6c4009
				   offset);
Packit 6c4009
Packit 6c4009
	  if (error)
Packit 6c4009
	    {
Packit 6c4009
	      free (comm);
Packit 6c4009
	      return error;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case '\'':
Packit 6c4009
	  squoting = 1 - squoting;
Packit 6c4009
	default:
Packit 6c4009
	  comm = w_addchar (comm, &comm_length, &comm_maxlen, words[*offset]);
Packit 6c4009
	  if (comm == NULL)
Packit 6c4009
	    return WRDE_NOSPACE;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Premature end */
Packit 6c4009
  free (comm);
Packit 6c4009
  return WRDE_SYNTAX;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
parse_dquote (char **word, size_t *word_length, size_t *max_length,
Packit 6c4009
	      const char *words, size_t *offset, int flags,
Packit 6c4009
	      wordexp_t *pwordexp, const char * ifs, const char * ifs_white)
Packit 6c4009
{
Packit 6c4009
  /* We are poised just after a double-quote */
Packit 6c4009
  int error;
Packit 6c4009
Packit 6c4009
  for (; words[*offset]; ++(*offset))
Packit 6c4009
    {
Packit 6c4009
      switch (words[*offset])
Packit 6c4009
	{
Packit 6c4009
	case '"':
Packit 6c4009
	  return 0;
Packit 6c4009
Packit 6c4009
	case '$':
Packit 6c4009
	  error = parse_dollars (word, word_length, max_length, words, offset,
Packit 6c4009
				 flags, pwordexp, ifs, ifs_white, 1);
Packit 6c4009
	  /* The ``1'' here is to tell parse_dollars not to
Packit 6c4009
	   * split the fields.  It may need to, however ("$@").
Packit 6c4009
	   */
Packit 6c4009
	  if (error)
Packit 6c4009
	    return error;
Packit 6c4009
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case '`':
Packit 6c4009
	  ++(*offset);
Packit 6c4009
	  error = parse_backtick (word, word_length, max_length, words,
Packit 6c4009
				  offset, flags, NULL, NULL, NULL);
Packit 6c4009
	  /* The first NULL here is to tell parse_backtick not to
Packit 6c4009
	   * split the fields.
Packit 6c4009
	   */
Packit 6c4009
	  if (error)
Packit 6c4009
	    return error;
Packit 6c4009
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	case '\\':
Packit 6c4009
	  error = parse_qtd_backslash (word, word_length, max_length, words,
Packit 6c4009
				       offset);
Packit 6c4009
Packit 6c4009
	  if (error)
Packit 6c4009
	    return error;
Packit 6c4009
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
	default:
Packit 6c4009
	  *word = w_addchar (*word, word_length, max_length, words[*offset]);
Packit 6c4009
	  if (*word == NULL)
Packit 6c4009
	    return WRDE_NOSPACE;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Unterminated string */
Packit 6c4009
  return WRDE_SYNTAX;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * wordfree() is to be called after pwordexp is finished with.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
wordfree (wordexp_t *pwordexp)
Packit 6c4009
{
Packit 6c4009
Packit 6c4009
  /* wordexp can set pwordexp to NULL */
Packit 6c4009
  if (pwordexp && pwordexp->we_wordv)
Packit 6c4009
    {
Packit 6c4009
      char **wordv = pwordexp->we_wordv;
Packit 6c4009
Packit 6c4009
      for (wordv += pwordexp->we_offs; *wordv; ++wordv)
Packit 6c4009
	free (*wordv);
Packit 6c4009
Packit 6c4009
      free (pwordexp->we_wordv);
Packit 6c4009
      pwordexp->we_wordv = NULL;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (wordfree)
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * wordexp()
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
wordexp (const char *words, wordexp_t *pwordexp, int flags)
Packit 6c4009
{
Packit 6c4009
  size_t words_offset;
Packit 6c4009
  size_t word_length;
Packit 6c4009
  size_t max_length;
Packit 6c4009
  char *word = w_newword (&word_length, &max_length);
Packit 6c4009
  int error;
Packit 6c4009
  char *ifs;
Packit 6c4009
  char ifs_white[4];
Packit 6c4009
  wordexp_t old_word = *pwordexp;
Packit 6c4009
Packit 6c4009
  if (flags & WRDE_REUSE)
Packit 6c4009
    {
Packit 6c4009
      /* Minimal implementation of WRDE_REUSE for now */
Packit 6c4009
      wordfree (pwordexp);
Packit 6c4009
      old_word.we_wordv = NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if ((flags & WRDE_APPEND) == 0)
Packit 6c4009
    {
Packit 6c4009
      pwordexp->we_wordc = 0;
Packit 6c4009
Packit 6c4009
      if (flags & WRDE_DOOFFS)
Packit 6c4009
	{
Packit 6c4009
	  pwordexp->we_wordv = calloc (1 + pwordexp->we_offs, sizeof (char *));
Packit 6c4009
	  if (pwordexp->we_wordv == NULL)
Packit 6c4009
	    {
Packit 6c4009
	      error = WRDE_NOSPACE;
Packit 6c4009
	      goto do_error;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  pwordexp->we_wordv = calloc (1, sizeof (char *));
Packit 6c4009
	  if (pwordexp->we_wordv == NULL)
Packit 6c4009
	    {
Packit 6c4009
	      error = WRDE_NOSPACE;
Packit 6c4009
	      goto do_error;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  pwordexp->we_offs = 0;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Find out what the field separators are.
Packit 6c4009
   * There are two types: whitespace and non-whitespace.
Packit 6c4009
   */
Packit 6c4009
  ifs = getenv ("IFS");
Packit 6c4009
Packit 6c4009
  if (ifs == NULL)
Packit 6c4009
    /* IFS unset - use <space><tab><newline>. */
Packit 6c4009
    ifs = strcpy (ifs_white, " \t\n");
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      char *ifsch = ifs;
Packit 6c4009
      char *whch = ifs_white;
Packit 6c4009
Packit 6c4009
      while (*ifsch != '\0')
Packit 6c4009
	{
Packit 6c4009
	  if (*ifsch == ' ' || *ifsch == '\t' || *ifsch == '\n')
Packit 6c4009
	    {
Packit 6c4009
	      /* Whitespace IFS.  See first whether it is already in our
Packit 6c4009
		 collection.  */
Packit 6c4009
	      char *runp = ifs_white;
Packit 6c4009
Packit 6c4009
	      while (runp < whch && *runp != *ifsch)
Packit 6c4009
		++runp;
Packit 6c4009
Packit 6c4009
	      if (runp == whch)
Packit 6c4009
		*whch++ = *ifsch;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  ++ifsch;
Packit 6c4009
	}
Packit 6c4009
      *whch = '\0';
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (words_offset = 0 ; words[words_offset] ; ++words_offset)
Packit 6c4009
    switch (words[words_offset])
Packit 6c4009
      {
Packit 6c4009
      case '\\':
Packit 6c4009
	error = parse_backslash (&word, &word_length, &max_length, words,
Packit 6c4009
				 &words_offset);
Packit 6c4009
Packit 6c4009
	if (error)
Packit 6c4009
	  goto do_error;
Packit 6c4009
Packit 6c4009
	break;
Packit 6c4009
Packit 6c4009
      case '$':
Packit 6c4009
	error = parse_dollars (&word, &word_length, &max_length, words,
Packit 6c4009
			       &words_offset, flags, pwordexp, ifs, ifs_white,
Packit 6c4009
			       0);
Packit 6c4009
Packit 6c4009
	if (error)
Packit 6c4009
	  goto do_error;
Packit 6c4009
Packit 6c4009
	break;
Packit 6c4009
Packit 6c4009
      case '`':
Packit 6c4009
	++words_offset;
Packit 6c4009
	error = parse_backtick (&word, &word_length, &max_length, words,
Packit 6c4009
				&words_offset, flags, pwordexp, ifs,
Packit 6c4009
				ifs_white);
Packit 6c4009
Packit 6c4009
	if (error)
Packit 6c4009
	  goto do_error;
Packit 6c4009
Packit 6c4009
	break;
Packit 6c4009
Packit 6c4009
      case '"':
Packit 6c4009
	++words_offset;
Packit 6c4009
	error = parse_dquote (&word, &word_length, &max_length, words,
Packit 6c4009
			      &words_offset, flags, pwordexp, ifs, ifs_white);
Packit 6c4009
Packit 6c4009
	if (error)
Packit 6c4009
	  goto do_error;
Packit 6c4009
Packit 6c4009
	if (!word_length)
Packit 6c4009
	  {
Packit 6c4009
	    error = w_addword (pwordexp, NULL);
Packit 6c4009
Packit 6c4009
	    if (error)
Packit 6c4009
	      return error;
Packit 6c4009
	  }
Packit 6c4009
Packit 6c4009
	break;
Packit 6c4009
Packit 6c4009
      case '\'':
Packit 6c4009
	++words_offset;
Packit 6c4009
	error = parse_squote (&word, &word_length, &max_length, words,
Packit 6c4009
			      &words_offset);
Packit 6c4009
Packit 6c4009
	if (error)
Packit 6c4009
	  goto do_error;
Packit 6c4009
Packit 6c4009
	if (!word_length)
Packit 6c4009
	  {
Packit 6c4009
	    error = w_addword (pwordexp, NULL);
Packit 6c4009
Packit 6c4009
	    if (error)
Packit 6c4009
	      return error;
Packit 6c4009
	  }
Packit 6c4009
Packit 6c4009
	break;
Packit 6c4009
Packit 6c4009
      case '~':
Packit 6c4009
	error = parse_tilde (&word, &word_length, &max_length, words,
Packit 6c4009
			     &words_offset, pwordexp->we_wordc);
Packit 6c4009
Packit 6c4009
	if (error)
Packit 6c4009
	  goto do_error;
Packit 6c4009
Packit 6c4009
	break;
Packit 6c4009
Packit 6c4009
      case '*':
Packit 6c4009
      case '[':
Packit 6c4009
      case '?':
Packit 6c4009
	error = parse_glob (&word, &word_length, &max_length, words,
Packit 6c4009
			    &words_offset, flags, pwordexp, ifs, ifs_white);
Packit 6c4009
Packit 6c4009
	if (error)
Packit 6c4009
	  goto do_error;
Packit 6c4009
Packit 6c4009
	break;
Packit 6c4009
Packit 6c4009
      default:
Packit 6c4009
	/* Is it a word separator? */
Packit 6c4009
	if (strchr (" \t", words[words_offset]) == NULL)
Packit 6c4009
	  {
Packit 6c4009
	    char ch = words[words_offset];
Packit 6c4009
Packit 6c4009
	    /* Not a word separator -- but is it a valid word char? */
Packit 6c4009
	    if (strchr ("\n|&;<>(){}", ch))
Packit 6c4009
	      {
Packit 6c4009
		/* Fail */
Packit 6c4009
		error = WRDE_BADCHAR;
Packit 6c4009
		goto do_error;
Packit 6c4009
	      }
Packit 6c4009
Packit 6c4009
	    /* "Ordinary" character -- add it to word */
Packit 6c4009
	    word = w_addchar (word, &word_length, &max_length,
Packit 6c4009
			      ch);
Packit 6c4009
	    if (word == NULL)
Packit 6c4009
	      {
Packit 6c4009
		error = WRDE_NOSPACE;
Packit 6c4009
		goto do_error;
Packit 6c4009
	      }
Packit 6c4009
Packit 6c4009
	    break;
Packit 6c4009
	  }
Packit 6c4009
Packit 6c4009
	/* If a word has been delimited, add it to the list. */
Packit 6c4009
	if (word != NULL)
Packit 6c4009
	  {
Packit 6c4009
	    error = w_addword (pwordexp, word);
Packit 6c4009
	    if (error)
Packit 6c4009
	      goto do_error;
Packit 6c4009
	  }
Packit 6c4009
Packit 6c4009
	word = w_newword (&word_length, &max_length);
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  /* End of string */
Packit 6c4009
Packit 6c4009
  /* There was a word separator at the end */
Packit 6c4009
  if (word == NULL) /* i.e. w_newword */
Packit 6c4009
    return 0;
Packit 6c4009
Packit 6c4009
  /* There was no field separator at the end */
Packit 6c4009
  return w_addword (pwordexp, word);
Packit 6c4009
Packit 6c4009
do_error:
Packit 6c4009
  /* Error:
Packit 6c4009
   *	free memory used (unless error is WRDE_NOSPACE), and
Packit 6c4009
   *	set pwordexp members back to what they were.
Packit 6c4009
   */
Packit 6c4009
Packit 6c4009
  free (word);
Packit 6c4009
Packit 6c4009
  if (error == WRDE_NOSPACE)
Packit 6c4009
    return WRDE_NOSPACE;
Packit 6c4009
Packit 6c4009
  if ((flags & WRDE_APPEND) == 0)
Packit 6c4009
    wordfree (pwordexp);
Packit 6c4009
Packit 6c4009
  *pwordexp = old_word;
Packit 6c4009
  return error;
Packit 6c4009
}