| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | /* arrayfunc.c -- High-level array functions used by other parts of the shell. */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | /* Copyright (C) 2001-2009 Free Software Foundation, Inc.
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    This file is part of GNU Bash, the Bourne Again SHell. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |    Bash is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |    it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |    the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  |    (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |    Bash is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |    GNU General Public License for more details. | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |    You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |    along with Bash.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "config.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined (ARRAY_VARS)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined (HAVE_UNISTD_H)
 | 
					
						
							|  |  |  | #  include <unistd.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | #include "bashintl.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | #include "shell.h"
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | #include "pathexp.h"
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "shmbutil.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | #include "builtins/common.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | extern char *this_command_name; | 
					
						
							|  |  |  | extern int last_command_exit_value; | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | extern int array_needs_making; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | static SHELL_VAR *bind_array_var_internal __P((SHELL_VAR *, arrayind_t, char *, char *, int)); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | static char *quote_assign __P((const char *)); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | static void quote_array_assignment_chars __P((WORD_LIST *)); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | static char *array_value_internal __P((char *, int, int, int *)); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | /* Standard error message to use when encountering an invalid array subscript */ | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | const char * const bash_badsub_errmsg = N_("bad array subscript"); | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | /* **************************************************************** */ | 
					
						
							|  |  |  | /*								    */ | 
					
						
							|  |  |  | /*  Functions to manipulate array variables and perform assignments */ | 
					
						
							|  |  |  | /*								    */ | 
					
						
							|  |  |  | /* **************************************************************** */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Convert a shell variable to an array variable.  The original value is
 | 
					
						
							|  |  |  |    saved as array[0]. */ | 
					
						
							|  |  |  | SHELL_VAR * | 
					
						
							|  |  |  | convert_var_to_array (var) | 
					
						
							|  |  |  |      SHELL_VAR *var; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   char *oldval; | 
					
						
							|  |  |  |   ARRAY *array; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   oldval = value_cell (var); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   array = array_create (); | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |   if (oldval) | 
					
						
							|  |  |  |     array_insert (array, 0, oldval); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   FREE (value_cell (var)); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   var_setarray (var, array); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* these aren't valid anymore */ | 
					
						
							|  |  |  |   var->dynamic_value = (sh_var_value_func_t *)NULL; | 
					
						
							|  |  |  |   var->assign_func = (sh_var_assign_func_t *)NULL; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   INVALIDATE_EXPORTSTR (var); | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |   if (exported_p (var)) | 
					
						
							|  |  |  |     array_needs_making++; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   VSETATTR (var, att_array); | 
					
						
							|  |  |  |   VUNSETATTR (var, att_invisible); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return var; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | /* Convert a shell variable to an array variable.  The original value is
 | 
					
						
							|  |  |  |    saved as array[0]. */ | 
					
						
							|  |  |  | SHELL_VAR * | 
					
						
							|  |  |  | convert_var_to_assoc (var) | 
					
						
							|  |  |  |      SHELL_VAR *var; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   char *oldval; | 
					
						
							|  |  |  |   HASH_TABLE *hash; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   oldval = value_cell (var); | 
					
						
							|  |  |  |   hash = assoc_create (0); | 
					
						
							|  |  |  |   if (oldval) | 
					
						
							|  |  |  |     assoc_insert (hash, "0", oldval); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   FREE (value_cell (var)); | 
					
						
							|  |  |  |   var_setassoc (var, hash); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* these aren't valid anymore */ | 
					
						
							|  |  |  |   var->dynamic_value = (sh_var_value_func_t *)NULL; | 
					
						
							|  |  |  |   var->assign_func = (sh_var_assign_func_t *)NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   INVALIDATE_EXPORTSTR (var); | 
					
						
							|  |  |  |   if (exported_p (var)) | 
					
						
							|  |  |  |     array_needs_making++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   VSETATTR (var, att_assoc); | 
					
						
							|  |  |  |   VUNSETATTR (var, att_invisible); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return var; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | static SHELL_VAR * | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | bind_array_var_internal (entry, ind, key, value, flags) | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |      SHELL_VAR *entry; | 
					
						
							|  |  |  |      arrayind_t ind; | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |      char *key; | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |      char *value; | 
					
						
							|  |  |  |      int flags; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   SHELL_VAR *dentry; | 
					
						
							|  |  |  |   char *newval; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* If we're appending, we need the old value of the array reference, so
 | 
					
						
							|  |  |  |      fake out make_variable_value with a dummy SHELL_VAR */ | 
					
						
							|  |  |  |   if (flags & ASS_APPEND) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       dentry = (SHELL_VAR *)xmalloc (sizeof (SHELL_VAR)); | 
					
						
							|  |  |  |       dentry->name = savestring (entry->name); | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |       if (assoc_p (entry)) | 
					
						
							|  |  |  | 	newval = assoc_reference (assoc_cell (entry), key); | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  | 	newval = array_reference (array_cell (entry), ind); | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |       if (newval) | 
					
						
							|  |  |  | 	dentry->value = savestring (newval); | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  dentry->value = (char *)xmalloc (1); | 
					
						
							|  |  |  | 	  dentry->value[0] = '\0'; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |       dentry->exportstr = 0; | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |       dentry->attributes = entry->attributes & ~(att_array|att_assoc|att_exported); | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |       /* Leave the rest of the members uninitialized; the code doesn't look
 | 
					
						
							|  |  |  | 	 at them. */ | 
					
						
							|  |  |  |       newval = make_variable_value (dentry, value, flags);	  | 
					
						
							|  |  |  |       dispose_variable (dentry); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     newval = make_variable_value (entry, value, flags); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (entry->assign_func) | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |     (*entry->assign_func) (entry, newval, ind, key); | 
					
						
							|  |  |  |   else if (assoc_p (entry)) | 
					
						
							|  |  |  |     assoc_insert (assoc_cell (entry), key, newval); | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |   else | 
					
						
							|  |  |  |     array_insert (array_cell (entry), ind, newval); | 
					
						
							|  |  |  |   FREE (newval); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return (entry); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | /* Perform an array assignment name[ind]=value.  If NAME already exists and
 | 
					
						
							|  |  |  |    is not an array, and IND is 0, perform name=value instead.  If NAME exists | 
					
						
							|  |  |  |    and is not an array, and IND is not 0, convert it into an array with the | 
					
						
							|  |  |  |    existing value as name[0]. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    If NAME does not exist, just create an array variable, no matter what | 
					
						
							|  |  |  |    IND's value may be. */ | 
					
						
							|  |  |  | SHELL_VAR * | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | bind_array_variable (name, ind, value, flags) | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |      char *name; | 
					
						
							|  |  |  |      arrayind_t ind; | 
					
						
							|  |  |  |      char *value; | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |      int flags; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   SHELL_VAR *entry; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   entry = var_lookup (name, shell_variables); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (entry == (SHELL_VAR *) 0) | 
					
						
							|  |  |  |     entry = make_new_array_variable (name); | 
					
						
							|  |  |  |   else if (readonly_p (entry) || noassign_p (entry)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (readonly_p (entry)) | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	err_readonly (name); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       return (entry); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   else if (array_p (entry) == 0) | 
					
						
							|  |  |  |     entry = convert_var_to_array (entry); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ENTRY is an array variable, and ARRAY points to the value. */ | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |   return (bind_array_var_internal (entry, ind, 0, value, flags)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SHELL_VAR * | 
					
						
							|  |  |  | bind_array_element (entry, ind, value, flags) | 
					
						
							|  |  |  |      SHELL_VAR *entry; | 
					
						
							|  |  |  |      arrayind_t ind; | 
					
						
							|  |  |  |      char *value; | 
					
						
							|  |  |  |      int flags; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return (bind_array_var_internal (entry, ind, 0, value, flags)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  |                      | 
					
						
							|  |  |  | SHELL_VAR * | 
					
						
							|  |  |  | bind_assoc_variable (entry, name, key, value, flags) | 
					
						
							|  |  |  |      SHELL_VAR *entry; | 
					
						
							|  |  |  |      char *name; | 
					
						
							|  |  |  |      char *key; | 
					
						
							|  |  |  |      char *value; | 
					
						
							|  |  |  |      int flags; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   SHELL_VAR *dentry; | 
					
						
							|  |  |  |   char *newval; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (readonly_p (entry) || noassign_p (entry)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (readonly_p (entry)) | 
					
						
							|  |  |  | 	err_readonly (name); | 
					
						
							|  |  |  |       return (entry); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return (bind_array_var_internal (entry, 0, key, value, flags)); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Parse NAME, a lhs of an assignment statement of the form v[s], and
 | 
					
						
							|  |  |  |    assign VALUE to that array element by calling bind_array_variable(). */ | 
					
						
							|  |  |  | SHELL_VAR * | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | assign_array_element (name, value, flags) | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |      char *name, *value; | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |      int flags; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |   char *sub, *vname, *akey; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   arrayind_t ind; | 
					
						
							|  |  |  |   int sublen; | 
					
						
							|  |  |  |   SHELL_VAR *entry; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   vname = array_variable_name (name, &sub, &sublen); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (vname == 0) | 
					
						
							|  |  |  |     return ((SHELL_VAR *)NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ((ALL_ELEMENT_SUB (sub[0]) && sub[1] == ']') || (sublen <= 1)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       free (vname); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |       err_badarraysub (name); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       return ((SHELL_VAR *)NULL); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |   entry = find_variable (vname); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (entry && assoc_p (entry)) | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |       sub[sublen-1] = '\0'; | 
					
						
							|  |  |  |       akey = expand_assignment_string_to_string (sub, 0);	/* [ */ | 
					
						
							|  |  |  |       sub[sublen-1] = ']'; | 
					
						
							|  |  |  |       if (akey == 0 || *akey == 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  free (vname); | 
					
						
							|  |  |  | 	  err_badarraysub (name); | 
					
						
							|  |  |  | 	  return ((SHELL_VAR *)NULL); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |       entry = bind_assoc_variable (entry, vname, akey, value, flags); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       ind = array_expand_index (sub, sublen); | 
					
						
							|  |  |  |       if (ind < 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  free (vname); | 
					
						
							|  |  |  | 	  err_badarraysub (name); | 
					
						
							|  |  |  | 	  return ((SHELL_VAR *)NULL); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |       entry = bind_array_variable (vname, ind, value, flags); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   free (vname); | 
					
						
							|  |  |  |   return (entry); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Find the array variable corresponding to NAME.  If there is no variable,
 | 
					
						
							|  |  |  |    create a new array variable.  If the variable exists but is not an array, | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |    convert it to an indexed array.  If FLAGS&1 is non-zero, an existing | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |    variable is checked for the readonly or noassign attribute in preparation | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |    for assignment (e.g., by the `read' builtin).  If FLAGS&2 is non-zero, we | 
					
						
							|  |  |  |    create an associative array. */ | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | SHELL_VAR * | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | find_or_make_array_variable (name, flags) | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |      char *name; | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |      int flags; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   SHELL_VAR *var; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   var = find_variable (name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (var == 0) | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |     var = (flags & 2) ? make_new_assoc_variable (name) : make_new_array_variable (name); | 
					
						
							|  |  |  |   else if ((flags & 1) && (readonly_p (var) || noassign_p (var))) | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |       if (readonly_p (var)) | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	err_readonly (name); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       return ((SHELL_VAR *)NULL); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |   else if ((flags & 2) && array_p (var)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       report_error (_("%s: cannot convert indexed to associative array"), name); | 
					
						
							|  |  |  |       return ((SHELL_VAR *)NULL); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   else if (array_p (var) == 0 && assoc_p (var) == 0) | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |     var = convert_var_to_array (var); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return (var); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  | /* Perform a compound assignment statement for array NAME, where VALUE is
 | 
					
						
							|  |  |  |    the text between the parens:  NAME=( VALUE ) */ | 
					
						
							|  |  |  | SHELL_VAR * | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | assign_array_from_string (name, value, flags) | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |      char *name, *value; | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |      int flags; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   SHELL_VAR *var; | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |   int vflags; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |   vflags = 1; | 
					
						
							|  |  |  |   if (flags & ASS_MKASSOC) | 
					
						
							|  |  |  |     vflags |= 2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   var = find_or_make_array_variable (name, vflags); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   if (var == 0) | 
					
						
							|  |  |  |     return ((SHELL_VAR *)NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |   return (assign_array_var_from_string (var, value, flags)); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Sequentially assign the indices of indexed array variable VAR from the
 | 
					
						
							|  |  |  |    words in LIST. */ | 
					
						
							|  |  |  | SHELL_VAR * | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | assign_array_var_from_word_list (var, list, flags) | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |      SHELL_VAR *var; | 
					
						
							|  |  |  |      WORD_LIST *list; | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |      int flags; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   register arrayind_t i; | 
					
						
							|  |  |  |   register WORD_LIST *l; | 
					
						
							|  |  |  |   ARRAY *a; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |   a = array_cell (var); | 
					
						
							|  |  |  |   i = (flags & ASS_APPEND) ? array_max_index (a) + 1 : 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (l = list; l; l = l->next, i++) | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |     if (var->assign_func) | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |       (*var->assign_func) (var, l->word->word, i, 0); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |       array_insert (a, i, l->word->word); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   return var; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-10-10 14:15:34 +00:00
										 |  |  | WORD_LIST * | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | expand_compound_array_assignment (var, value, flags) | 
					
						
							|  |  |  |      SHELL_VAR *var; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |      char *value; | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |      int flags; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   WORD_LIST *list, *nlist; | 
					
						
							| 
									
										
										
										
											2006-10-10 14:15:34 +00:00
										 |  |  |   char *val; | 
					
						
							|  |  |  |   int ni; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-10-10 14:15:34 +00:00
										 |  |  |   /* I don't believe this condition is ever true any more. */ | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   if (*value == '(')	/*)*/ | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       ni = 1; | 
					
						
							|  |  |  |       val = extract_array_assignment_list (value, &ni); | 
					
						
							|  |  |  |       if (val == 0) | 
					
						
							| 
									
										
										
										
											2006-10-10 14:15:34 +00:00
										 |  |  | 	return (WORD_LIST *)NULL; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     val = value; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Expand the value string into a list of words, performing all the
 | 
					
						
							|  |  |  |      shell expansions including pathname generation and word splitting. */ | 
					
						
							|  |  |  |   /* First we split the string on whitespace, using the shell parser
 | 
					
						
							|  |  |  |      (ksh93 seems to do this). */ | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |   list = parse_string_to_word_list (val, 1, "array assign"); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* If we're using [subscript]=value, we need to quote each [ and ] to
 | 
					
						
							|  |  |  |      prevent unwanted filename expansion. */ | 
					
						
							|  |  |  |   if (list) | 
					
						
							|  |  |  |     quote_array_assignment_chars (list); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Now that we've split it, perform the shell expansions on each
 | 
					
						
							|  |  |  |      word in the list. */ | 
					
						
							|  |  |  |   nlist = list ? expand_words_no_vars (list) : (WORD_LIST *)NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   dispose_words (list); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (val != value) | 
					
						
							|  |  |  |     free (val); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-10-10 14:15:34 +00:00
										 |  |  |   return nlist; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | assign_compound_array_list (var, nlist, flags) | 
					
						
							|  |  |  |      SHELL_VAR *var; | 
					
						
							|  |  |  |      WORD_LIST *nlist; | 
					
						
							|  |  |  |      int flags; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   ARRAY *a; | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |   HASH_TABLE *h; | 
					
						
							| 
									
										
										
										
											2006-10-10 14:15:34 +00:00
										 |  |  |   WORD_LIST *list; | 
					
						
							|  |  |  |   char *w, *val, *nval; | 
					
						
							|  |  |  |   int len, iflags; | 
					
						
							|  |  |  |   arrayind_t ind, last_ind; | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |   char *akey; | 
					
						
							| 
									
										
										
										
											2006-10-10 14:15:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |   a = (var && array_p (var)) ? array_cell (var) : (ARRAY *)0; | 
					
						
							|  |  |  |   h = (var && assoc_p (var)) ? assoc_cell (var) : (HASH_TABLE *)0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   akey = (char *)0; | 
					
						
							|  |  |  |   ind = 0; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* Now that we are ready to assign values to the array, kill the existing
 | 
					
						
							|  |  |  |      value. */ | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |   if ((flags & ASS_APPEND) == 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (array_p (var) && a) | 
					
						
							|  |  |  | 	array_flush (a); | 
					
						
							|  |  |  |       else if (assoc_p (var) && h) | 
					
						
							|  |  |  | 	assoc_flush (h); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   last_ind = (a && (flags & ASS_APPEND)) ? array_max_index (a) + 1 : 0; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |   for (list = nlist; list; list = list->next) | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2006-10-10 14:15:34 +00:00
										 |  |  |       iflags = flags; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       w = list->word->word; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       /* We have a word of the form [ind]=value */ | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |       if ((list->word->flags & W_ASSIGNMENT) && w[0] == '[') | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 	  len = skipsubscript (w, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | 	  /* XXX - changes for `+=' */ | 
					
						
							|  |  |  |  	  if (w[len] != ']' || (w[len+1] != '=' && (w[len+1] != '+' || w[len+2] != '='))) | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 	    { | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 	      if (assoc_p (var)) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 		  err_badarraysub (w); | 
					
						
							|  |  |  | 		  continue; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | 	      nval = make_variable_value (var, w, flags); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 	      if (var->assign_func) | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 		(*var->assign_func) (var, nval, last_ind, 0); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 	      else | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 		array_insert (a, last_ind, nval); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 	      FREE (nval); | 
					
						
							|  |  |  | 	      last_ind++; | 
					
						
							|  |  |  | 	      continue; | 
					
						
							|  |  |  | 	    } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	  if (len == 1) | 
					
						
							|  |  |  | 	    { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	      err_badarraysub (w); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 	      continue; | 
					
						
							|  |  |  | 	    } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	  if (ALL_ELEMENT_SUB (w[1]) && len == 2) | 
					
						
							|  |  |  | 	    { | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 	      if (assoc_p (var)) | 
					
						
							|  |  |  | 		report_error (_("%s: invalid associative array key"), w); | 
					
						
							|  |  |  | 	      else | 
					
						
							|  |  |  | 		report_error (_("%s: cannot assign to non-numeric index"), w); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 	      continue; | 
					
						
							|  |  |  | 	    } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 	  if (array_p (var)) | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 	    { | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 	      ind = array_expand_index (w + 1, len); | 
					
						
							|  |  |  | 	      if (ind < 0) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 		  err_badarraysub (w); | 
					
						
							|  |  |  | 		  continue; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	      last_ind = ind; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 	    } | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 	  else if (assoc_p (var)) | 
					
						
							|  |  |  | 	    { | 
					
						
							|  |  |  | 	      akey = substring (w, 1, len); | 
					
						
							|  |  |  | 	      if (akey == 0 || *akey == 0) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 		  err_badarraysub (w); | 
					
						
							|  |  |  | 		  continue; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	    } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-10-10 14:15:34 +00:00
										 |  |  | 	  /* XXX - changes for `+=' -- just accept the syntax.  ksh93 doesn't do this */ | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | 	  if (w[len + 1] == '+' && w[len + 2] == '=') | 
					
						
							|  |  |  | 	    { | 
					
						
							| 
									
										
										
										
											2006-10-10 14:15:34 +00:00
										 |  |  | 	      iflags |= ASS_APPEND; | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | 	      val = w + len + 3; | 
					
						
							|  |  |  | 	    } | 
					
						
							|  |  |  | 	  else | 
					
						
							|  |  |  | 	    val = w + len + 2; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |       else if (assoc_p (var)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  report_error (_("%s: %s: must use subscript when assigning associative array"), var->name, w); | 
					
						
							|  |  |  | 	  continue; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       else		/* No [ind]=value, just a stray `=' */ | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  ind = last_ind; | 
					
						
							|  |  |  | 	  val = w; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (integer_p (var)) | 
					
						
							|  |  |  | 	this_command_name = (char *)NULL;	/* no command name for errors */ | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |       bind_array_var_internal (var, ind, akey, val, iflags); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       last_ind++; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2006-10-10 14:15:34 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-10-10 14:15:34 +00:00
										 |  |  | /* Perform a compound array assignment:  VAR->name=( VALUE ).  The
 | 
					
						
							|  |  |  |    VALUE has already had the parentheses stripped. */ | 
					
						
							|  |  |  | SHELL_VAR * | 
					
						
							|  |  |  | assign_array_var_from_string (var, value, flags) | 
					
						
							|  |  |  |      SHELL_VAR *var; | 
					
						
							|  |  |  |      char *value; | 
					
						
							|  |  |  |      int flags; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   WORD_LIST *nlist; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (value == 0) | 
					
						
							|  |  |  |     return var; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |   nlist = expand_compound_array_assignment (var, value, flags); | 
					
						
							| 
									
										
										
										
											2006-10-10 14:15:34 +00:00
										 |  |  |   assign_compound_array_list (var, nlist, flags); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (nlist) | 
					
						
							|  |  |  |     dispose_words (nlist); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   return (var); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | /* Quote globbing chars and characters in $IFS before the `=' in an assignment
 | 
					
						
							|  |  |  |    statement (usually a compound array assignment) to protect them from | 
					
						
							|  |  |  |    unwanted filename expansion or word splitting. */ | 
					
						
							|  |  |  | static char * | 
					
						
							|  |  |  | quote_assign (string) | 
					
						
							|  |  |  |      const char *string; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   size_t slen; | 
					
						
							|  |  |  |   int saw_eq; | 
					
						
							|  |  |  |   char *temp, *t; | 
					
						
							|  |  |  |   const char *s, *send; | 
					
						
							|  |  |  |   DECLARE_MBSTATE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   slen = strlen (string); | 
					
						
							|  |  |  |   send = string + slen; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   t = temp = (char *)xmalloc (slen * 2 + 1); | 
					
						
							|  |  |  |   saw_eq = 0; | 
					
						
							|  |  |  |   for (s = string; *s; ) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (*s == '=') | 
					
						
							|  |  |  | 	saw_eq = 1; | 
					
						
							|  |  |  |       if (saw_eq == 0 && (glob_char_p (s) || isifs (*s))) | 
					
						
							|  |  |  | 	*t++ = '\\'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       COPY_CHAR_P (t, s, send); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   *t = '\0'; | 
					
						
							|  |  |  |   return temp; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | /* For each word in a compound array assignment, if the word looks like
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |    [ind]=value, quote globbing chars and characters in $IFS before the `='. */ | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | static void | 
					
						
							|  |  |  | quote_array_assignment_chars (list) | 
					
						
							|  |  |  |      WORD_LIST *list; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |   char *nword; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   WORD_LIST *l; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (l = list; l; l = l->next) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (l->word == 0 || l->word->word == 0 || l->word->word[0] == '\0') | 
					
						
							|  |  |  | 	continue;	/* should not happen, but just in case... */ | 
					
						
							|  |  |  |       /* Don't bother if it doesn't look like [ind]=value */ | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |       if (l->word->word[0] != '[' || xstrchr (l->word->word, '=') == 0) /* ] */ | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 	continue; | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |       nword = quote_assign (l->word->word); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       free (l->word->word); | 
					
						
							|  |  |  |       l->word->word = nword; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* This function assumes s[i] == '['; returns with s[ret] == ']' if
 | 
					
						
							|  |  |  |    an array subscript is correctly parsed. */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | skipsubscript (s, i) | 
					
						
							|  |  |  |      const char *s; | 
					
						
							|  |  |  |      int i; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   int count, c; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | #if defined (HANDLE_MULTIBYTE)
 | 
					
						
							|  |  |  |   mbstate_t state, state_bak; | 
					
						
							|  |  |  |   size_t slength, mblength; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | #if defined (HANDLE_MULTIBYTE)
 | 
					
						
							|  |  |  |   memset (&state, '\0', sizeof (mbstate_t)); | 
					
						
							|  |  |  |   slength = strlen (s + i); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   count = 1; | 
					
						
							|  |  |  |   while (count) | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |       /* Advance one (possibly multibyte) character in S starting at I. */ | 
					
						
							|  |  |  | #if defined (HANDLE_MULTIBYTE)
 | 
					
						
							| 
									
										
										
										
											2006-10-10 14:15:34 +00:00
										 |  |  |       if (MB_CUR_MAX > 1) | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 	  state_bak = state; | 
					
						
							|  |  |  | 	  mblength = mbrlen (s + i, slength, &state); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | 	  if (MB_INVALIDCH (mblength)) | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	    { | 
					
						
							|  |  |  | 	      state = state_bak; | 
					
						
							|  |  |  | 	      i++; | 
					
						
							|  |  |  | 	      slength--; | 
					
						
							|  |  |  | 	    } | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | 	  else if (MB_NULLWCH (mblength)) | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	    return i; | 
					
						
							|  |  |  | 	  else | 
					
						
							|  |  |  | 	    { | 
					
						
							|  |  |  | 	      i += mblength; | 
					
						
							|  |  |  | 	      slength -= mblength; | 
					
						
							|  |  |  | 	    } | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |       ++i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       c = s[i]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (c == 0) | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 	break; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |       else if (c == '[') | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 	count++; | 
					
						
							|  |  |  |       else if (c == ']') | 
					
						
							|  |  |  | 	count--; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   return i; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* This function is called with SUB pointing to just after the beginning
 | 
					
						
							|  |  |  |    `[' of an array subscript and removes the array element to which SUB | 
					
						
							|  |  |  |    expands from array VAR.  A subscript of `*' or `@' unsets the array. */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | unbind_array_element (var, sub) | 
					
						
							|  |  |  |      SHELL_VAR *var; | 
					
						
							|  |  |  |      char *sub; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   int len; | 
					
						
							|  |  |  |   arrayind_t ind; | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |   char *akey; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   ARRAY_ELEMENT *ae; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   len = skipsubscript (sub, 0); | 
					
						
							|  |  |  |   if (sub[len] != ']' || len == 0) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |       builtin_error ("%s[%s: %s", var->name, sub, _(bash_badsub_errmsg)); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   sub[len] = '\0'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (ALL_ELEMENT_SUB (sub[0]) && sub[1] == 0) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |       unbind_variable (var->name); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       return (0); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (assoc_p (var)) | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |       akey = expand_assignment_string_to_string (sub, 0);     /* [ */ | 
					
						
							|  |  |  |       if (akey == 0 || *akey == 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  builtin_error ("[%s]: %s", sub, _(bash_badsub_errmsg)); | 
					
						
							|  |  |  | 	  return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |       assoc_remove (assoc_cell (var), akey); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       ind = array_expand_index (sub, len+1); | 
					
						
							|  |  |  |       if (ind < 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  builtin_error ("[%s]: %s", sub, _(bash_badsub_errmsg)); | 
					
						
							|  |  |  | 	  return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |       ae = array_remove (array_cell (var), ind); | 
					
						
							|  |  |  |       if (ae) | 
					
						
							|  |  |  | 	array_dispose_element (ae); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Format and output an array assignment in compound form VAR=(VALUES),
 | 
					
						
							|  |  |  |    suitable for re-use as input. */ | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | print_array_assignment (var, quoted) | 
					
						
							|  |  |  |      SHELL_VAR *var; | 
					
						
							|  |  |  |      int quoted; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   char *vstr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   vstr = array_to_assign (array_cell (var), quoted); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (vstr == 0) | 
					
						
							|  |  |  |     printf ("%s=%s\n", var->name, quoted ? "'()'" : "()"); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       printf ("%s=%s\n", var->name, vstr); | 
					
						
							|  |  |  |       free (vstr); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | /* Format and output an associative array assignment in compound form
 | 
					
						
							|  |  |  |    VAR=(VALUES), suitable for re-use as input. */ | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | print_assoc_assignment (var, quoted) | 
					
						
							|  |  |  |      SHELL_VAR *var; | 
					
						
							|  |  |  |      int quoted; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   char *vstr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   vstr = assoc_to_assign (assoc_cell (var), quoted); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (vstr == 0) | 
					
						
							|  |  |  |     printf ("%s=%s\n", var->name, quoted ? "'()'" : "()"); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       printf ("%s=%s\n", var->name, vstr); | 
					
						
							|  |  |  |       free (vstr); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | /***********************************************************************/ | 
					
						
							|  |  |  | /*								       */ | 
					
						
							|  |  |  | /* Utility functions to manage arrays and their contents for expansion */ | 
					
						
							|  |  |  | /*								       */ | 
					
						
							|  |  |  | /***********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Return 1 if NAME is a properly-formed array reference v[sub]. */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | valid_array_reference (name) | 
					
						
							|  |  |  |      char *name; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   char *t; | 
					
						
							|  |  |  |   int r, len; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   t = xstrchr (name, '[');	/* ] */ | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   if (t) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       *t = '\0'; | 
					
						
							|  |  |  |       r = legal_identifier (name); | 
					
						
							|  |  |  |       *t = '['; | 
					
						
							|  |  |  |       if (r == 0) | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  |       /* Check for a properly-terminated non-blank subscript. */ | 
					
						
							|  |  |  |       len = skipsubscript (t, 0); | 
					
						
							|  |  |  |       if (t[len] != ']' || len == 1) | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  |       for (r = 1; r < len; r++) | 
					
						
							|  |  |  | 	if (whitespace (t[r]) == 0) | 
					
						
							|  |  |  | 	  return 1; | 
					
						
							|  |  |  |       return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Expand the array index beginning at S and extending LEN characters. */ | 
					
						
							|  |  |  | arrayind_t | 
					
						
							|  |  |  | array_expand_index (s, len) | 
					
						
							|  |  |  |      char *s; | 
					
						
							|  |  |  |      int len; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   char *exp, *t; | 
					
						
							|  |  |  |   int expok; | 
					
						
							|  |  |  |   arrayind_t val; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   exp = (char *)xmalloc (len); | 
					
						
							|  |  |  |   strncpy (exp, s, len - 1); | 
					
						
							|  |  |  |   exp[len - 1] = '\0'; | 
					
						
							| 
									
										
										
										
											2006-10-10 14:15:34 +00:00
										 |  |  |   t = expand_arith_string (exp, 0); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   this_command_name = (char *)NULL; | 
					
						
							|  |  |  |   val = evalexp (t, &expok); | 
					
						
							|  |  |  |   free (t); | 
					
						
							|  |  |  |   free (exp); | 
					
						
							|  |  |  |   if (expok == 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       last_command_exit_value = EXECUTION_FAILURE; | 
					
						
							| 
									
										
										
										
											2008-11-18 13:15:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       top_level_cleanup ();       | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       jump_to_top_level (DISCARD); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   return val; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Return the name of the variable specified by S without any subscript.
 | 
					
						
							|  |  |  |    If SUBP is non-null, return a pointer to the start of the subscript | 
					
						
							|  |  |  |    in *SUBP. If LENP is non-null, the length of the subscript is returned | 
					
						
							|  |  |  |    in *LENP.  This returns newly-allocated memory. */ | 
					
						
							|  |  |  | char * | 
					
						
							|  |  |  | array_variable_name (s, subp, lenp) | 
					
						
							|  |  |  |      char *s, **subp; | 
					
						
							|  |  |  |      int *lenp; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   char *t, *ret; | 
					
						
							|  |  |  |   int ind, ni; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   t = xstrchr (s, '['); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   if (t == 0) | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |       if (subp) | 
					
						
							|  |  |  |       	*subp = t; | 
					
						
							|  |  |  |       if (lenp) | 
					
						
							|  |  |  | 	*lenp = 0; | 
					
						
							|  |  |  |       return ((char *)NULL); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   ind = t - s; | 
					
						
							|  |  |  |   ni = skipsubscript (s, ind); | 
					
						
							|  |  |  |   if (ni <= ind + 1 || s[ni] != ']') | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |       err_badarraysub (s); | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |       if (subp) | 
					
						
							|  |  |  |       	*subp = t; | 
					
						
							|  |  |  |       if (lenp) | 
					
						
							|  |  |  | 	*lenp = 0; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       return ((char *)NULL); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   *t = '\0'; | 
					
						
							|  |  |  |   ret = savestring (s); | 
					
						
							|  |  |  |   *t++ = '[';		/* ] */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (subp) | 
					
						
							|  |  |  |     *subp = t; | 
					
						
							|  |  |  |   if (lenp) | 
					
						
							|  |  |  |     *lenp = ni - ind; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Return the variable specified by S without any subscript.  If SUBP is
 | 
					
						
							|  |  |  |    non-null, return a pointer to the start of the subscript in *SUBP. | 
					
						
							|  |  |  |    If LENP is non-null, the length of the subscript is returned in *LENP. */ | 
					
						
							|  |  |  | SHELL_VAR * | 
					
						
							|  |  |  | array_variable_part (s, subp, lenp) | 
					
						
							|  |  |  |      char *s, **subp; | 
					
						
							|  |  |  |      int *lenp; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   char *t; | 
					
						
							|  |  |  |   SHELL_VAR *var; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   t = array_variable_name (s, subp, lenp); | 
					
						
							|  |  |  |   if (t == 0) | 
					
						
							|  |  |  |     return ((SHELL_VAR *)NULL); | 
					
						
							|  |  |  |   var = find_variable (t); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   free (t); | 
					
						
							| 
									
										
										
										
											2004-11-09 21:37:25 +00:00
										 |  |  |   return (var == 0 || invisible_p (var)) ? (SHELL_VAR *)0 : var; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Return a string containing the elements in the array and subscript
 | 
					
						
							|  |  |  |    described by S.  If the subscript is * or @, obeys quoting rules akin | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |    to the expansion of $* and $@ including double quoting.  If RTYPE | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |    is non-null it gets 1 if the array reference is name[*], 2 if the | 
					
						
							|  |  |  |    reference is name[@], and 0 otherwise. */ | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | static char * | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | array_value_internal (s, quoted, allow_all, rtype) | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |      char *s; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |      int quoted, allow_all, *rtype; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   int len; | 
					
						
							|  |  |  |   arrayind_t ind; | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |   char *akey; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   char *retval, *t, *temp; | 
					
						
							|  |  |  |   WORD_LIST *l; | 
					
						
							|  |  |  |   SHELL_VAR *var; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   var = array_variable_part (s, &t, &len); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   /* Expand the index, even if the variable doesn't exist, in case side
 | 
					
						
							|  |  |  |      effects are needed, like ${w[i++]} where w is unset. */ | 
					
						
							|  |  |  | #if 0
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   if (var == 0) | 
					
						
							|  |  |  |     return (char *)NULL; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |   if (len == 0) | 
					
						
							|  |  |  |     return ((char *)NULL);	/* error message already printed */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   /* [ */ | 
					
						
							|  |  |  |   if (ALL_ELEMENT_SUB (t[0]) && t[1] == ']') | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |       if (rtype) | 
					
						
							| 
									
										
										
										
											2008-11-18 13:15:12 +00:00
										 |  |  | 	*rtype = (t[0] == '*') ? 1 : 2; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       if (allow_all == 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	  err_badarraysub (s); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 	  return ((char *)NULL); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |       else if (var == 0 || value_cell (var) == 0) | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	return ((char *)NULL); | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |       else if (array_p (var) == 0 && assoc_p (var) == 0) | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	l = add_string_to_list (value_cell (var), (WORD_LIST *)NULL); | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |       else if (assoc_p (var)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  l = assoc_to_word_list (assoc_cell (var)); | 
					
						
							|  |  |  | 	  if (l == (WORD_LIST *)NULL) | 
					
						
							|  |  |  | 	    return ((char *)NULL); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       else | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  l = array_to_word_list (array_cell (var)); | 
					
						
							|  |  |  | 	  if (l == (WORD_LIST *)NULL) | 
					
						
							|  |  |  | 	    return ((char *) NULL); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (t[0] == '*' && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  temp = string_list_dollar_star (l); | 
					
						
							|  |  |  | 	  retval = quote_string (temp); | 
					
						
							|  |  |  | 	  free (temp); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |       else	/* ${name[@]} or unquoted ${name[*]} */ | 
					
						
							|  |  |  | 	retval = string_list_dollar_at (l, quoted); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       dispose_words (l); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |       if (rtype) | 
					
						
							|  |  |  | 	*rtype = 0; | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |       if (var == 0 || array_p (var) || assoc_p (var) == 0) | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 	  ind = array_expand_index (t, len); | 
					
						
							|  |  |  | 	  if (ind < 0) | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	    { | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | index_error: | 
					
						
							|  |  |  | 	      if (var) | 
					
						
							|  |  |  | 		err_badarraysub (var->name); | 
					
						
							|  |  |  | 	      else | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 		  t[-1] = '\0'; | 
					
						
							|  |  |  | 		  err_badarraysub (s); | 
					
						
							|  |  |  | 		  t[-1] = '[';	/* ] */ | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	      return ((char *)NULL); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	    } | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |       else if (assoc_p (var)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  t[len - 1] = '\0'; | 
					
						
							|  |  |  | 	  akey = expand_assignment_string_to_string (t, 0);	/* [ */ | 
					
						
							|  |  |  | 	  t[len - 1] = ']'; | 
					
						
							|  |  |  | 	  if (akey == 0 || *akey == 0) | 
					
						
							|  |  |  | 	    goto index_error; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |       | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |       if (var == 0) | 
					
						
							|  |  |  | 	return ((char *)NULL); | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |       if (array_p (var) == 0 && assoc_p (var) == 0) | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	return (ind == 0 ? value_cell (var) : (char *)NULL); | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |       else if (assoc_p (var)) | 
					
						
							|  |  |  | 	retval = assoc_reference (assoc_cell (var), akey); | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  | 	retval = array_reference (array_cell (var), ind); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return retval; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Return a string containing the elements described by the array and
 | 
					
						
							|  |  |  |    subscript contained in S, obeying quoting for subscripts * and @. */ | 
					
						
							|  |  |  | char * | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | array_value (s, quoted, rtype) | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |      char *s; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |      int quoted, *rtype; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   return (array_value_internal (s, quoted, 1, rtype)); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Return the value of the array indexing expression S as a single string.
 | 
					
						
							|  |  |  |    If ALLOW_ALL is 0, do not allow `@' and `*' subscripts.  This is used | 
					
						
							|  |  |  |    by other parts of the shell such as the arithmetic expression evaluator | 
					
						
							|  |  |  |    in expr.c. */ | 
					
						
							|  |  |  | char * | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | get_array_value (s, allow_all, rtype) | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |      char *s; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |      int allow_all, *rtype; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   return (array_value_internal (s, 0, allow_all, rtype)); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | char * | 
					
						
							|  |  |  | array_keys (s, quoted) | 
					
						
							|  |  |  |      char *s; | 
					
						
							|  |  |  |      int quoted; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   int len; | 
					
						
							|  |  |  |   char *retval, *t, *temp; | 
					
						
							|  |  |  |   WORD_LIST *l; | 
					
						
							|  |  |  |   SHELL_VAR *var; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   var = array_variable_part (s, &t, &len); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* [ */ | 
					
						
							|  |  |  |   if (var == 0 || ALL_ELEMENT_SUB (t[0]) == 0 || t[1] != ']') | 
					
						
							|  |  |  |     return (char *)NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |   if (array_p (var) == 0 && assoc_p (var) == 0) | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |     l = add_string_to_list ("0", (WORD_LIST *)NULL); | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |   else if (assoc_p (var)) | 
					
						
							|  |  |  |     l = assoc_keys_to_word_list (assoc_cell (var)); | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |     l = array_keys_to_word_list (array_cell (var)); | 
					
						
							|  |  |  |   if (l == (WORD_LIST *)NULL) | 
					
						
							|  |  |  |     return ((char *) NULL); | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (t[0] == '*' && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES))) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       temp = string_list_dollar_star (l); | 
					
						
							|  |  |  |       retval = quote_string (temp); | 
					
						
							|  |  |  |       free (temp); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   else	/* ${!name[@]} or unquoted ${!name[*]} */ | 
					
						
							|  |  |  |     retval = string_list_dollar_at (l, quoted); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   dispose_words (l); | 
					
						
							|  |  |  |   return retval; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | #endif /* ARRAY_VARS */
 |