]> git.ipfire.org Git - thirdparty/bash.git/blame - arrayfunc.c
commit bash-20120622 snapshot
[thirdparty/bash.git] / arrayfunc.c
CommitLineData
f73dda09
JA
1/* arrayfunc.c -- High-level array functions used by other parts of the shell. */
2
276cb932 3/* Copyright (C) 2001-2011 Free Software Foundation, Inc.
f73dda09
JA
4
5 This file is part of GNU Bash, the Bourne Again SHell.
6
2e4498b3
CR
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19*/
f73dda09
JA
20
21#include "config.h"
22
23#if defined (ARRAY_VARS)
24
25#if defined (HAVE_UNISTD_H)
26# include <unistd.h>
27#endif
28#include <stdio.h>
29
5e13499c
CR
30#include "bashintl.h"
31
f73dda09 32#include "shell.h"
48ff5447 33#include "pathexp.h"
7117c2d2
JA
34
35#include "shmbutil.h"
36
f73dda09
JA
37#include "builtins/common.h"
38
39extern char *this_command_name;
40extern int last_command_exit_value;
633e5c6d 41extern int array_needs_making;
f73dda09 42
fdf670ea 43static SHELL_VAR *bind_array_var_internal __P((SHELL_VAR *, arrayind_t, char *, char *, int));
276cb932 44static SHELL_VAR *assign_array_element_internal __P((SHELL_VAR *, char *, char *, char *, int, char *, int));
d11b8b46 45
4ac1ff98 46static char *quote_assign __P((const char *));
f73dda09 47static void quote_array_assignment_chars __P((WORD_LIST *));
68dfe178 48static char *array_value_internal __P((char *, int, int, int *, arrayind_t *));
f73dda09 49
5e13499c 50/* Standard error message to use when encountering an invalid array subscript */
d3ad40de 51const char * const bash_badsub_errmsg = N_("bad array subscript");
5e13499c 52
f73dda09
JA
53/* **************************************************************** */
54/* */
55/* Functions to manipulate array variables and perform assignments */
56/* */
57/* **************************************************************** */
58
59/* Convert a shell variable to an array variable. The original value is
60 saved as array[0]. */
61SHELL_VAR *
62convert_var_to_array (var)
63 SHELL_VAR *var;
64{
65 char *oldval;
66 ARRAY *array;
67
68 oldval = value_cell (var);
7117c2d2 69 array = array_create ();
d3a24ed2
CR
70 if (oldval)
71 array_insert (array, 0, oldval);
f73dda09
JA
72
73 FREE (value_cell (var));
7117c2d2
JA
74 var_setarray (var, array);
75
76 /* these aren't valid anymore */
77 var->dynamic_value = (sh_var_value_func_t *)NULL;
78 var->assign_func = (sh_var_assign_func_t *)NULL;
f73dda09
JA
79
80 INVALIDATE_EXPORTSTR (var);
633e5c6d
CR
81 if (exported_p (var))
82 array_needs_making++;
f73dda09
JA
83
84 VSETATTR (var, att_array);
85 VUNSETATTR (var, att_invisible);
86
87 return var;
88}
89
fdf670ea
CR
90/* Convert a shell variable to an array variable. The original value is
91 saved as array[0]. */
92SHELL_VAR *
93convert_var_to_assoc (var)
94 SHELL_VAR *var;
95{
96 char *oldval;
97 HASH_TABLE *hash;
98
99 oldval = value_cell (var);
100 hash = assoc_create (0);
101 if (oldval)
af98a2a6 102 assoc_insert (hash, savestring ("0"), oldval);
fdf670ea
CR
103
104 FREE (value_cell (var));
105 var_setassoc (var, hash);
106
107 /* these aren't valid anymore */
108 var->dynamic_value = (sh_var_value_func_t *)NULL;
109 var->assign_func = (sh_var_assign_func_t *)NULL;
110
111 INVALIDATE_EXPORTSTR (var);
112 if (exported_p (var))
113 array_needs_making++;
114
115 VSETATTR (var, att_assoc);
116 VUNSETATTR (var, att_invisible);
117
118 return var;
119}
120
861a1900
CR
121char *
122make_array_variable_value (entry, ind, key, value, flags)
d11b8b46
CR
123 SHELL_VAR *entry;
124 arrayind_t ind;
fdf670ea 125 char *key;
d11b8b46
CR
126 char *value;
127 int flags;
128{
129 SHELL_VAR *dentry;
130 char *newval;
131
132 /* If we're appending, we need the old value of the array reference, so
133 fake out make_variable_value with a dummy SHELL_VAR */
134 if (flags & ASS_APPEND)
135 {
136 dentry = (SHELL_VAR *)xmalloc (sizeof (SHELL_VAR));
137 dentry->name = savestring (entry->name);
fdf670ea
CR
138 if (assoc_p (entry))
139 newval = assoc_reference (assoc_cell (entry), key);
140 else
141 newval = array_reference (array_cell (entry), ind);
d11b8b46
CR
142 if (newval)
143 dentry->value = savestring (newval);
144 else
145 {
146 dentry->value = (char *)xmalloc (1);
147 dentry->value[0] = '\0';
148 }
149 dentry->exportstr = 0;
fdf670ea 150 dentry->attributes = entry->attributes & ~(att_array|att_assoc|att_exported);
d11b8b46
CR
151 /* Leave the rest of the members uninitialized; the code doesn't look
152 at them. */
153 newval = make_variable_value (dentry, value, flags);
154 dispose_variable (dentry);
155 }
156 else
157 newval = make_variable_value (entry, value, flags);
158
861a1900
CR
159 return newval;
160}
161
162static SHELL_VAR *
163bind_array_var_internal (entry, ind, key, value, flags)
164 SHELL_VAR *entry;
165 arrayind_t ind;
166 char *key;
167 char *value;
168 int flags;
169{
170 char *newval;
171
172 newval = make_array_variable_value (entry, ind, key, value, flags);
173
d11b8b46 174 if (entry->assign_func)
fdf670ea
CR
175 (*entry->assign_func) (entry, newval, ind, key);
176 else if (assoc_p (entry))
177 assoc_insert (assoc_cell (entry), key, newval);
d11b8b46
CR
178 else
179 array_insert (array_cell (entry), ind, newval);
180 FREE (newval);
181
182 return (entry);
183}
184
f73dda09
JA
185/* Perform an array assignment name[ind]=value. If NAME already exists and
186 is not an array, and IND is 0, perform name=value instead. If NAME exists
187 and is not an array, and IND is not 0, convert it into an array with the
188 existing value as name[0].
189
190 If NAME does not exist, just create an array variable, no matter what
191 IND's value may be. */
192SHELL_VAR *
d11b8b46 193bind_array_variable (name, ind, value, flags)
f73dda09
JA
194 char *name;
195 arrayind_t ind;
196 char *value;
d11b8b46 197 int flags;
f73dda09
JA
198{
199 SHELL_VAR *entry;
f73dda09 200
348a457e 201 entry = find_shell_variable (name);
f73dda09
JA
202
203 if (entry == (SHELL_VAR *) 0)
204 entry = make_new_array_variable (name);
205 else if (readonly_p (entry) || noassign_p (entry))
206 {
207 if (readonly_p (entry))
7117c2d2 208 err_readonly (name);
f73dda09
JA
209 return (entry);
210 }
211 else if (array_p (entry) == 0)
212 entry = convert_var_to_array (entry);
213
214 /* ENTRY is an array variable, and ARRAY points to the value. */
fdf670ea 215 return (bind_array_var_internal (entry, ind, 0, value, flags));
f73dda09
JA
216}
217
6a8fd0ed
CR
218SHELL_VAR *
219bind_array_element (entry, ind, value, flags)
220 SHELL_VAR *entry;
221 arrayind_t ind;
222 char *value;
223 int flags;
224{
fdf670ea 225 return (bind_array_var_internal (entry, ind, 0, value, flags));
6a8fd0ed
CR
226}
227
fdf670ea
CR
228SHELL_VAR *
229bind_assoc_variable (entry, name, key, value, flags)
230 SHELL_VAR *entry;
231 char *name;
232 char *key;
233 char *value;
234 int flags;
235{
236 SHELL_VAR *dentry;
237 char *newval;
238
239 if (readonly_p (entry) || noassign_p (entry))
240 {
241 if (readonly_p (entry))
242 err_readonly (name);
243 return (entry);
244 }
245
246 return (bind_array_var_internal (entry, 0, key, value, flags));
247}
248
f73dda09
JA
249/* Parse NAME, a lhs of an assignment statement of the form v[s], and
250 assign VALUE to that array element by calling bind_array_variable(). */
251SHELL_VAR *
d11b8b46 252assign_array_element (name, value, flags)
f73dda09 253 char *name, *value;
d11b8b46 254 int flags;
f73dda09 255{
276cb932 256 char *sub, *vname;
f73dda09
JA
257 int sublen;
258 SHELL_VAR *entry;
259
260 vname = array_variable_name (name, &sub, &sublen);
261
262 if (vname == 0)
263 return ((SHELL_VAR *)NULL);
264
265 if ((ALL_ELEMENT_SUB (sub[0]) && sub[1] == ']') || (sublen <= 1))
266 {
267 free (vname);
7117c2d2 268 err_badarraysub (name);
f73dda09
JA
269 return ((SHELL_VAR *)NULL);
270 }
271
fdf670ea 272 entry = find_variable (vname);
276cb932
CR
273 entry = assign_array_element_internal (entry, name, vname, sub, sublen, value, flags);
274
275 free (vname);
276 return entry;
277}
278
279static SHELL_VAR *
280assign_array_element_internal (entry, name, vname, sub, sublen, value, flags)
281 SHELL_VAR *entry;
282 char *name; /* only used for error messages */
283 char *vname;
284 char *sub;
285 int sublen;
286 char *value;
287 int flags;
288{
289 char *akey;
290 arrayind_t ind;
fdf670ea
CR
291
292 if (entry && assoc_p (entry))
f73dda09 293 {
fdf670ea
CR
294 sub[sublen-1] = '\0';
295 akey = expand_assignment_string_to_string (sub, 0); /* [ */
296 sub[sublen-1] = ']';
297 if (akey == 0 || *akey == 0)
298 {
fdf670ea 299 err_badarraysub (name);
631b20c6 300 FREE (akey);
fdf670ea
CR
301 return ((SHELL_VAR *)NULL);
302 }
303 entry = bind_assoc_variable (entry, vname, akey, value, flags);
304 }
305 else
306 {
d9e1f41e 307 ind = array_expand_index (entry, sub, sublen);
fdf670ea
CR
308 if (ind < 0)
309 {
fdf670ea
CR
310 err_badarraysub (name);
311 return ((SHELL_VAR *)NULL);
312 }
313 entry = bind_array_variable (vname, ind, value, flags);
f73dda09 314 }
f73dda09 315
f73dda09
JA
316 return (entry);
317}
318
319/* Find the array variable corresponding to NAME. If there is no variable,
320 create a new array variable. If the variable exists but is not an array,
fdf670ea 321 convert it to an indexed array. If FLAGS&1 is non-zero, an existing
f73dda09 322 variable is checked for the readonly or noassign attribute in preparation
fdf670ea
CR
323 for assignment (e.g., by the `read' builtin). If FLAGS&2 is non-zero, we
324 create an associative array. */
f73dda09 325SHELL_VAR *
fdf670ea 326find_or_make_array_variable (name, flags)
f73dda09 327 char *name;
fdf670ea 328 int flags;
f73dda09
JA
329{
330 SHELL_VAR *var;
331
332 var = find_variable (name);
333
334 if (var == 0)
fdf670ea
CR
335 var = (flags & 2) ? make_new_assoc_variable (name) : make_new_array_variable (name);
336 else if ((flags & 1) && (readonly_p (var) || noassign_p (var)))
f73dda09
JA
337 {
338 if (readonly_p (var))
7117c2d2 339 err_readonly (name);
f73dda09
JA
340 return ((SHELL_VAR *)NULL);
341 }
fdf670ea
CR
342 else if ((flags & 2) && array_p (var))
343 {
1f6ec1a8 344 last_command_exit_value = 1;
fdf670ea
CR
345 report_error (_("%s: cannot convert indexed to associative array"), name);
346 return ((SHELL_VAR *)NULL);
347 }
348 else if (array_p (var) == 0 && assoc_p (var) == 0)
f73dda09
JA
349 var = convert_var_to_array (var);
350
351 return (var);
352}
353
354/* Perform a compound assignment statement for array NAME, where VALUE is
355 the text between the parens: NAME=( VALUE ) */
356SHELL_VAR *
d11b8b46 357assign_array_from_string (name, value, flags)
f73dda09 358 char *name, *value;
d11b8b46 359 int flags;
f73dda09
JA
360{
361 SHELL_VAR *var;
fdf670ea 362 int vflags;
f73dda09 363
fdf670ea
CR
364 vflags = 1;
365 if (flags & ASS_MKASSOC)
366 vflags |= 2;
367
368 var = find_or_make_array_variable (name, vflags);
f73dda09
JA
369 if (var == 0)
370 return ((SHELL_VAR *)NULL);
371
d11b8b46 372 return (assign_array_var_from_string (var, value, flags));
f73dda09
JA
373}
374
375/* Sequentially assign the indices of indexed array variable VAR from the
376 words in LIST. */
377SHELL_VAR *
d11b8b46 378assign_array_var_from_word_list (var, list, flags)
f73dda09
JA
379 SHELL_VAR *var;
380 WORD_LIST *list;
d11b8b46 381 int flags;
f73dda09
JA
382{
383 register arrayind_t i;
384 register WORD_LIST *l;
385 ARRAY *a;
386
d11b8b46
CR
387 a = array_cell (var);
388 i = (flags & ASS_APPEND) ? array_max_index (a) + 1 : 0;
389
390 for (l = list; l; l = l->next, i++)
f73dda09 391 if (var->assign_func)
fdf670ea 392 (*var->assign_func) (var, l->word->word, i, 0);
f73dda09 393 else
7117c2d2 394 array_insert (a, i, l->word->word);
f73dda09
JA
395 return var;
396}
397
d3ad40de 398WORD_LIST *
fdf670ea
CR
399expand_compound_array_assignment (var, value, flags)
400 SHELL_VAR *var;
f73dda09 401 char *value;
d11b8b46 402 int flags;
f73dda09 403{
f73dda09 404 WORD_LIST *list, *nlist;
276cb932 405 WORD_LIST *hd, *tl, *t, *n;
d3ad40de
CR
406 char *val;
407 int ni;
f73dda09 408
276cb932
CR
409 /* This condition is true when invoked from the declare builtin with a
410 command like
411 declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")' */
f73dda09
JA
412 if (*value == '(') /*)*/
413 {
414 ni = 1;
415 val = extract_array_assignment_list (value, &ni);
416 if (val == 0)
d3ad40de 417 return (WORD_LIST *)NULL;
f73dda09
JA
418 }
419 else
420 val = value;
421
422 /* Expand the value string into a list of words, performing all the
423 shell expansions including pathname generation and word splitting. */
424 /* First we split the string on whitespace, using the shell parser
425 (ksh93 seems to do this). */
5e13499c 426 list = parse_string_to_word_list (val, 1, "array assign");
f73dda09 427
276cb932
CR
428 if (var && assoc_p (var))
429 {
430 if (val != value)
431 free (val);
432 return list;
433 }
434
f73dda09 435 /* If we're using [subscript]=value, we need to quote each [ and ] to
276cb932
CR
436 prevent unwanted filename expansion. This doesn't need to be done
437 for associative array expansion, since that uses a different expansion
438 function (see assign_compound_array_list below). */
f73dda09
JA
439 if (list)
440 quote_array_assignment_chars (list);
441
442 /* Now that we've split it, perform the shell expansions on each
443 word in the list. */
444 nlist = list ? expand_words_no_vars (list) : (WORD_LIST *)NULL;
445
446 dispose_words (list);
447
448 if (val != value)
449 free (val);
450
d3ad40de
CR
451 return nlist;
452}
453
824dfe68 454/* Callers ensure that VAR is not NULL */
d3ad40de
CR
455void
456assign_compound_array_list (var, nlist, flags)
457 SHELL_VAR *var;
458 WORD_LIST *nlist;
459 int flags;
460{
461 ARRAY *a;
fdf670ea 462 HASH_TABLE *h;
d3ad40de
CR
463 WORD_LIST *list;
464 char *w, *val, *nval;
276cb932 465 int len, iflags, free_val;
d3ad40de 466 arrayind_t ind, last_ind;
fdf670ea 467 char *akey;
d3ad40de 468
fdf670ea
CR
469 a = (var && array_p (var)) ? array_cell (var) : (ARRAY *)0;
470 h = (var && assoc_p (var)) ? assoc_cell (var) : (HASH_TABLE *)0;
471
472 akey = (char *)0;
473 ind = 0;
f73dda09
JA
474
475 /* Now that we are ready to assign values to the array, kill the existing
476 value. */
fdf670ea
CR
477 if ((flags & ASS_APPEND) == 0)
478 {
824dfe68 479 if (a && array_p (var))
fdf670ea 480 array_flush (a);
824dfe68 481 else if (h && assoc_p (var))
fdf670ea
CR
482 assoc_flush (h);
483 }
484
485 last_ind = (a && (flags & ASS_APPEND)) ? array_max_index (a) + 1 : 0;
f73dda09 486
d11b8b46 487 for (list = nlist; list; list = list->next)
f73dda09 488 {
d3ad40de 489 iflags = flags;
f73dda09
JA
490 w = list->word->word;
491
492 /* We have a word of the form [ind]=value */
5e13499c 493 if ((list->word->flags & W_ASSIGNMENT) && w[0] == '[')
f73dda09 494 {
276cb932
CR
495 /* Don't have to handle embedded quotes specially any more, since
496 associative array subscripts have not been expanded yet (see
497 above). */
498 len = skipsubscript (w, 0, 0);
f73dda09 499
d11b8b46
CR
500 /* XXX - changes for `+=' */
501 if (w[len] != ']' || (w[len+1] != '=' && (w[len+1] != '+' || w[len+2] != '=')))
f73dda09 502 {
fdf670ea
CR
503 if (assoc_p (var))
504 {
505 err_badarraysub (w);
506 continue;
507 }
d11b8b46 508 nval = make_variable_value (var, w, flags);
f73dda09 509 if (var->assign_func)
fdf670ea 510 (*var->assign_func) (var, nval, last_ind, 0);
f73dda09 511 else
7117c2d2 512 array_insert (a, last_ind, nval);
f73dda09
JA
513 FREE (nval);
514 last_ind++;
515 continue;
516 }
517
518 if (len == 1)
519 {
7117c2d2 520 err_badarraysub (w);
f73dda09
JA
521 continue;
522 }
523
524 if (ALL_ELEMENT_SUB (w[1]) && len == 2)
525 {
1f6ec1a8 526 last_command_exit_value = 1;
fdf670ea
CR
527 if (assoc_p (var))
528 report_error (_("%s: invalid associative array key"), w);
529 else
530 report_error (_("%s: cannot assign to non-numeric index"), w);
f73dda09
JA
531 continue;
532 }
533
fdf670ea 534 if (array_p (var))
f73dda09 535 {
d9e1f41e 536 ind = array_expand_index (var, w + 1, len);
fdf670ea
CR
537 if (ind < 0)
538 {
539 err_badarraysub (w);
540 continue;
541 }
542
543 last_ind = ind;
544 }
545 else if (assoc_p (var))
546 {
276cb932
CR
547 /* This is not performed above, see expand_compound_array_assignment */
548 w[len] = '\0'; /*[*/
549 akey = expand_assignment_string_to_string (w+1, 0);
550 w[len] = ']';
551 /* And we need to expand the value also, see below */
fdf670ea
CR
552 if (akey == 0 || *akey == 0)
553 {
554 err_badarraysub (w);
631b20c6 555 FREE (akey);
fdf670ea
CR
556 continue;
557 }
f73dda09 558 }
fdf670ea 559
d3ad40de 560 /* XXX - changes for `+=' -- just accept the syntax. ksh93 doesn't do this */
d11b8b46
CR
561 if (w[len + 1] == '+' && w[len + 2] == '=')
562 {
d3ad40de 563 iflags |= ASS_APPEND;
d11b8b46
CR
564 val = w + len + 3;
565 }
566 else
276cb932 567 val = w + len + 2;
f73dda09 568 }
fdf670ea
CR
569 else if (assoc_p (var))
570 {
1f6ec1a8 571 last_command_exit_value = 1;
fdf670ea
CR
572 report_error (_("%s: %s: must use subscript when assigning associative array"), var->name, w);
573 continue;
574 }
f73dda09
JA
575 else /* No [ind]=value, just a stray `=' */
576 {
577 ind = last_ind;
578 val = w;
579 }
580
276cb932
CR
581 free_val = 0;
582 /* See above; we need to expand the value here */
583 if (assoc_p (var))
584 {
585 val = expand_assignment_string_to_string (val, 0);
586 free_val = 1;
587 }
588
f73dda09
JA
589 if (integer_p (var))
590 this_command_name = (char *)NULL; /* no command name for errors */
fdf670ea 591 bind_array_var_internal (var, ind, akey, val, iflags);
f73dda09 592 last_ind++;
276cb932
CR
593
594 if (free_val)
595 free (val);
f73dda09 596 }
d3ad40de
CR
597}
598
599/* Perform a compound array assignment: VAR->name=( VALUE ). The
600 VALUE has already had the parentheses stripped. */
601SHELL_VAR *
602assign_array_var_from_string (var, value, flags)
603 SHELL_VAR *var;
604 char *value;
605 int flags;
606{
607 WORD_LIST *nlist;
608
609 if (value == 0)
610 return var;
611
fdf670ea 612 nlist = expand_compound_array_assignment (var, value, flags);
d3ad40de 613 assign_compound_array_list (var, nlist, flags);
f73dda09 614
d3ad40de
CR
615 if (nlist)
616 dispose_words (nlist);
f73dda09
JA
617 return (var);
618}
619
fdf670ea
CR
620/* Quote globbing chars and characters in $IFS before the `=' in an assignment
621 statement (usually a compound array assignment) to protect them from
622 unwanted filename expansion or word splitting. */
4ac1ff98
CR
623static char *
624quote_assign (string)
625 const char *string;
626{
627 size_t slen;
628 int saw_eq;
3eb2d94a 629 char *temp, *t, *subs;
4ac1ff98 630 const char *s, *send;
3eb2d94a 631 int ss, se;
4ac1ff98
CR
632 DECLARE_MBSTATE;
633
634 slen = strlen (string);
635 send = string + slen;
636
637 t = temp = (char *)xmalloc (slen * 2 + 1);
638 saw_eq = 0;
639 for (s = string; *s; )
640 {
641 if (*s == '=')
642 saw_eq = 1;
3eb2d94a
CR
643 if (saw_eq == 0 && *s == '[') /* looks like a subscript */
644 {
645 ss = s - string;
646 se = skipsubscript (string, ss, 0);
647 subs = substring (s, ss, se);
648 *t++ = '\\';
649 strcpy (t, subs);
650 t += se - ss;
651 *t++ = '\\';
652 *t++ = ']';
653 s += se + 1;
654 free (subs);
655 continue;
656 }
4ac1ff98
CR
657 if (saw_eq == 0 && (glob_char_p (s) || isifs (*s)))
658 *t++ = '\\';
659
660 COPY_CHAR_P (t, s, send);
661 }
662 *t = '\0';
663 return temp;
664}
665
f73dda09 666/* For each word in a compound array assignment, if the word looks like
fdf670ea 667 [ind]=value, quote globbing chars and characters in $IFS before the `='. */
f73dda09
JA
668static void
669quote_array_assignment_chars (list)
670 WORD_LIST *list;
671{
4ac1ff98 672 char *nword;
f73dda09
JA
673 WORD_LIST *l;
674
675 for (l = list; l; l = l->next)
676 {
677 if (l->word == 0 || l->word->word == 0 || l->word->word[0] == '\0')
678 continue; /* should not happen, but just in case... */
679 /* Don't bother if it doesn't look like [ind]=value */
d0ca3503 680 if (l->word->word[0] != '[' || mbschr (l->word->word, '=') == 0) /* ] */
f73dda09 681 continue;
4ac1ff98 682 nword = quote_assign (l->word->word);
f73dda09
JA
683 free (l->word->word);
684 l->word->word = nword;
685 }
686}
687
6932f7f5 688/* skipsubscript moved to subst.c to use private functions. 2009/02/24. */
f73dda09
JA
689
690/* This function is called with SUB pointing to just after the beginning
691 `[' of an array subscript and removes the array element to which SUB
692 expands from array VAR. A subscript of `*' or `@' unsets the array. */
693int
694unbind_array_element (var, sub)
695 SHELL_VAR *var;
696 char *sub;
697{
698 int len;
699 arrayind_t ind;
fdf670ea 700 char *akey;
f73dda09
JA
701 ARRAY_ELEMENT *ae;
702
3eb2d94a 703 len = skipsubscript (sub, 0, 0);
f73dda09
JA
704 if (sub[len] != ']' || len == 0)
705 {
5e13499c 706 builtin_error ("%s[%s: %s", var->name, sub, _(bash_badsub_errmsg));
f73dda09
JA
707 return -1;
708 }
709 sub[len] = '\0';
710
711 if (ALL_ELEMENT_SUB (sub[0]) && sub[1] == 0)
712 {
7117c2d2 713 unbind_variable (var->name);
f73dda09
JA
714 return (0);
715 }
fdf670ea
CR
716
717 if (assoc_p (var))
f73dda09 718 {
fdf670ea
CR
719 akey = expand_assignment_string_to_string (sub, 0); /* [ */
720 if (akey == 0 || *akey == 0)
721 {
722 builtin_error ("[%s]: %s", sub, _(bash_badsub_errmsg));
631b20c6 723 FREE (akey);
fdf670ea
CR
724 return -1;
725 }
726 assoc_remove (assoc_cell (var), akey);
68dfe178 727 free (akey);
f73dda09 728 }
fdf670ea
CR
729 else
730 {
d9e1f41e 731 ind = array_expand_index (var, sub, len+1);
fdf670ea
CR
732 if (ind < 0)
733 {
734 builtin_error ("[%s]: %s", sub, _(bash_badsub_errmsg));
735 return -1;
736 }
737 ae = array_remove (array_cell (var), ind);
738 if (ae)
739 array_dispose_element (ae);
740 }
741
f73dda09
JA
742 return 0;
743}
744
745/* Format and output an array assignment in compound form VAR=(VALUES),
746 suitable for re-use as input. */
747void
748print_array_assignment (var, quoted)
749 SHELL_VAR *var;
750 int quoted;
751{
752 char *vstr;
753
7117c2d2 754 vstr = array_to_assign (array_cell (var), quoted);
f73dda09
JA
755
756 if (vstr == 0)
757 printf ("%s=%s\n", var->name, quoted ? "'()'" : "()");
758 else
759 {
760 printf ("%s=%s\n", var->name, vstr);
761 free (vstr);
762 }
763}
764
fdf670ea
CR
765/* Format and output an associative array assignment in compound form
766 VAR=(VALUES), suitable for re-use as input. */
767void
768print_assoc_assignment (var, quoted)
769 SHELL_VAR *var;
770 int quoted;
771{
772 char *vstr;
773
774 vstr = assoc_to_assign (assoc_cell (var), quoted);
775
776 if (vstr == 0)
777 printf ("%s=%s\n", var->name, quoted ? "'()'" : "()");
778 else
779 {
780 printf ("%s=%s\n", var->name, vstr);
781 free (vstr);
782 }
783}
784
f73dda09
JA
785/***********************************************************************/
786/* */
787/* Utility functions to manage arrays and their contents for expansion */
788/* */
789/***********************************************************************/
790
791/* Return 1 if NAME is a properly-formed array reference v[sub]. */
792int
793valid_array_reference (name)
794 char *name;
795{
796 char *t;
797 int r, len;
798
d0ca3503 799 t = mbschr (name, '['); /* ] */
f73dda09
JA
800 if (t)
801 {
802 *t = '\0';
803 r = legal_identifier (name);
804 *t = '[';
805 if (r == 0)
806 return 0;
807 /* Check for a properly-terminated non-blank subscript. */
3eb2d94a 808 len = skipsubscript (t, 0, 0);
f73dda09
JA
809 if (t[len] != ']' || len == 1)
810 return 0;
811 for (r = 1; r < len; r++)
812 if (whitespace (t[r]) == 0)
813 return 1;
814 return 0;
815 }
816 return 0;
817}
818
819/* Expand the array index beginning at S and extending LEN characters. */
820arrayind_t
d9e1f41e
CR
821array_expand_index (var, s, len)
822 SHELL_VAR *var;
f73dda09
JA
823 char *s;
824 int len;
825{
826 char *exp, *t;
827 int expok;
828 arrayind_t val;
829
830 exp = (char *)xmalloc (len);
831 strncpy (exp, s, len - 1);
832 exp[len - 1] = '\0';
d3ad40de 833 t = expand_arith_string (exp, 0);
f73dda09
JA
834 this_command_name = (char *)NULL;
835 val = evalexp (t, &expok);
836 free (t);
837 free (exp);
838 if (expok == 0)
839 {
840 last_command_exit_value = EXECUTION_FAILURE;
c184f645
CR
841
842 top_level_cleanup ();
f73dda09
JA
843 jump_to_top_level (DISCARD);
844 }
845 return val;
846}
847
848/* Return the name of the variable specified by S without any subscript.
849 If SUBP is non-null, return a pointer to the start of the subscript
850 in *SUBP. If LENP is non-null, the length of the subscript is returned
851 in *LENP. This returns newly-allocated memory. */
852char *
853array_variable_name (s, subp, lenp)
854 char *s, **subp;
855 int *lenp;
856{
857 char *t, *ret;
858 int ind, ni;
859
d0ca3503 860 t = mbschr (s, '[');
f73dda09 861 if (t == 0)
5e13499c
CR
862 {
863 if (subp)
864 *subp = t;
865 if (lenp)
866 *lenp = 0;
867 return ((char *)NULL);
868 }
f73dda09 869 ind = t - s;
3eb2d94a 870 ni = skipsubscript (s, ind, 0);
f73dda09
JA
871 if (ni <= ind + 1 || s[ni] != ']')
872 {
7117c2d2 873 err_badarraysub (s);
5e13499c
CR
874 if (subp)
875 *subp = t;
876 if (lenp)
877 *lenp = 0;
f73dda09
JA
878 return ((char *)NULL);
879 }
880
881 *t = '\0';
882 ret = savestring (s);
883 *t++ = '['; /* ] */
884
885 if (subp)
886 *subp = t;
887 if (lenp)
888 *lenp = ni - ind;
889
890 return ret;
891}
892
893/* Return the variable specified by S without any subscript. If SUBP is
894 non-null, return a pointer to the start of the subscript in *SUBP.
895 If LENP is non-null, the length of the subscript is returned in *LENP. */
896SHELL_VAR *
897array_variable_part (s, subp, lenp)
898 char *s, **subp;
899 int *lenp;
900{
901 char *t;
902 SHELL_VAR *var;
903
904 t = array_variable_name (s, subp, lenp);
905 if (t == 0)
906 return ((SHELL_VAR *)NULL);
907 var = find_variable (t);
908
909 free (t);
898cc92e 910 return (var == 0 || invisible_p (var)) ? (SHELL_VAR *)0 : var;
f73dda09
JA
911}
912
67362c60
CR
913#define INDEX_ERROR() \
914 do \
915 { \
916 if (var) \
917 err_badarraysub (var->name); \
918 else \
919 { \
920 t[-1] = '\0'; \
921 err_badarraysub (s); \
922 t[-1] = '['; /* ] */\
923 } \
924 return ((char *)NULL); \
925 } \
926 while (0)
927
f73dda09
JA
928/* Return a string containing the elements in the array and subscript
929 described by S. If the subscript is * or @, obeys quoting rules akin
7117c2d2 930 to the expansion of $* and $@ including double quoting. If RTYPE
b1a26c01
CR
931 is non-null it gets 1 if the array reference is name[*], 2 if the
932 reference is name[@], and 0 otherwise. */
f73dda09 933static char *
5f8cde23 934array_value_internal (s, quoted, flags, rtype, indp)
f73dda09 935 char *s;
5f8cde23 936 int quoted, flags, *rtype;
68dfe178 937 arrayind_t *indp;
f73dda09
JA
938{
939 int len;
940 arrayind_t ind;
fdf670ea 941 char *akey;
f73dda09
JA
942 char *retval, *t, *temp;
943 WORD_LIST *l;
944 SHELL_VAR *var;
945
946 var = array_variable_part (s, &t, &len);
947
7117c2d2
JA
948 /* Expand the index, even if the variable doesn't exist, in case side
949 effects are needed, like ${w[i++]} where w is unset. */
950#if 0
f73dda09
JA
951 if (var == 0)
952 return (char *)NULL;
7117c2d2 953#endif
f73dda09 954
d3a24ed2
CR
955 if (len == 0)
956 return ((char *)NULL); /* error message already printed */
957
f73dda09 958 /* [ */
631b20c6 959 akey = 0;
f73dda09
JA
960 if (ALL_ELEMENT_SUB (t[0]) && t[1] == ']')
961 {
7117c2d2 962 if (rtype)
b1a26c01 963 *rtype = (t[0] == '*') ? 1 : 2;
5f8cde23 964 if ((flags & AV_ALLOWALL) == 0)
f73dda09 965 {
7117c2d2 966 err_badarraysub (s);
f73dda09
JA
967 return ((char *)NULL);
968 }
68dfe178 969 else if (var == 0 || value_cell (var) == 0) /* XXX - check for invisible_p(var) ? */
7117c2d2 970 return ((char *)NULL);
fdf670ea 971 else if (array_p (var) == 0 && assoc_p (var) == 0)
7117c2d2 972 l = add_string_to_list (value_cell (var), (WORD_LIST *)NULL);
fdf670ea
CR
973 else if (assoc_p (var))
974 {
975 l = assoc_to_word_list (assoc_cell (var));
976 if (l == (WORD_LIST *)NULL)
977 return ((char *)NULL);
978 }
f73dda09
JA
979 else
980 {
981 l = array_to_word_list (array_cell (var));
982 if (l == (WORD_LIST *)NULL)
983 return ((char *) NULL);
984 }
985
986 if (t[0] == '*' && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
987 {
988 temp = string_list_dollar_star (l);
631b20c6 989 retval = quote_string (temp); /* XXX - leak here */
f73dda09
JA
990 free (temp);
991 }
992 else /* ${name[@]} or unquoted ${name[*]} */
631b20c6 993 retval = string_list_dollar_at (l, quoted); /* XXX - leak here */
f73dda09
JA
994
995 dispose_words (l);
996 }
997 else
998 {
7117c2d2
JA
999 if (rtype)
1000 *rtype = 0;
fdf670ea 1001 if (var == 0 || array_p (var) || assoc_p (var) == 0)
f73dda09 1002 {
5f8cde23 1003 if ((flags & AV_USEIND) == 0 || indp == 0)
7117c2d2 1004 {
d9e1f41e 1005 ind = array_expand_index (var, t, len);
67362c60 1006 if (ind < 0)
5f8cde23
CR
1007 {
1008 /* negative subscripts to indexed arrays count back from end */
1009 if (var && array_p (var))
1010 ind = array_max_index (array_cell (var)) + 1 + ind;
1011 if (ind < 0)
1012 INDEX_ERROR();
1013 }
1014 if (indp)
1015 *indp = ind;
7117c2d2 1016 }
5f8cde23
CR
1017 else if (indp)
1018 ind = *indp;
f73dda09 1019 }
fdf670ea
CR
1020 else if (assoc_p (var))
1021 {
1022 t[len - 1] = '\0';
1023 akey = expand_assignment_string_to_string (t, 0); /* [ */
1024 t[len - 1] = ']';
1025 if (akey == 0 || *akey == 0)
631b20c6
CR
1026 {
1027 FREE (akey);
1028 INDEX_ERROR();
1029 }
fdf670ea
CR
1030 }
1031
68dfe178 1032 if (var == 0 || value_cell (var) == 0) /* XXX - check invisible_p(var) ? */
631b20c6
CR
1033 {
1034 FREE (akey);
1035 return ((char *)NULL);
1036 }
fdf670ea 1037 if (array_p (var) == 0 && assoc_p (var) == 0)
7117c2d2 1038 return (ind == 0 ? value_cell (var) : (char *)NULL);
fdf670ea 1039 else if (assoc_p (var))
68dfe178
CR
1040 {
1041 retval = assoc_reference (assoc_cell (var), akey);
1042 free (akey);
1043 }
fdf670ea
CR
1044 else
1045 retval = array_reference (array_cell (var), ind);
f73dda09
JA
1046 }
1047
1048 return retval;
1049}
1050
1051/* Return a string containing the elements described by the array and
1052 subscript contained in S, obeying quoting for subscripts * and @. */
1053char *
5f8cde23 1054array_value (s, quoted, flags, rtype, indp)
f73dda09 1055 char *s;
5f8cde23 1056 int quoted, flags, *rtype;
68dfe178 1057 arrayind_t *indp;
f73dda09 1058{
5f8cde23 1059 return (array_value_internal (s, quoted, flags|AV_ALLOWALL, rtype, indp));
f73dda09
JA
1060}
1061
1062/* Return the value of the array indexing expression S as a single string.
5f8cde23
CR
1063 If (FLAGS & AV_ALLOWALL) is 0, do not allow `@' and `*' subscripts. This
1064 is used by other parts of the shell such as the arithmetic expression
1065 evaluator in expr.c. */
f73dda09 1066char *
5f8cde23 1067get_array_value (s, flags, rtype, indp)
f73dda09 1068 char *s;
5f8cde23 1069 int flags, *rtype;
68dfe178 1070 arrayind_t *indp;
f73dda09 1071{
5f8cde23 1072 return (array_value_internal (s, 0, flags, rtype, indp));
f73dda09
JA
1073}
1074
d3a24ed2
CR
1075char *
1076array_keys (s, quoted)
1077 char *s;
1078 int quoted;
1079{
1080 int len;
1081 char *retval, *t, *temp;
1082 WORD_LIST *l;
1083 SHELL_VAR *var;
1084
1085 var = array_variable_part (s, &t, &len);
1086
1087 /* [ */
1088 if (var == 0 || ALL_ELEMENT_SUB (t[0]) == 0 || t[1] != ']')
1089 return (char *)NULL;
1090
68dfe178
CR
1091 if (var_isset (var) == 0 || invisible_p (var))
1092 return (char *)NULL;
1093
fdf670ea 1094 if (array_p (var) == 0 && assoc_p (var) == 0)
d3a24ed2 1095 l = add_string_to_list ("0", (WORD_LIST *)NULL);
fdf670ea
CR
1096 else if (assoc_p (var))
1097 l = assoc_keys_to_word_list (assoc_cell (var));
d3a24ed2 1098 else
fdf670ea
CR
1099 l = array_keys_to_word_list (array_cell (var));
1100 if (l == (WORD_LIST *)NULL)
1101 return ((char *) NULL);
d3a24ed2
CR
1102
1103 if (t[0] == '*' && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
1104 {
1105 temp = string_list_dollar_star (l);
1106 retval = quote_string (temp);
1107 free (temp);
1108 }
1109 else /* ${!name[@]} or unquoted ${!name[*]} */
1110 retval = string_list_dollar_at (l, quoted);
1111
1112 dispose_words (l);
1113 return retval;
1114}
f73dda09 1115#endif /* ARRAY_VARS */