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