2 * array.c - functions to create, destroy, access, and manipulate arrays
5 * Arrays are sparse doubly-linked lists. An element's index is stored
12 /* Copyright (C) 1997-2004 Free Software Foundation, Inc.
14 This file is part of GNU Bash, the Bourne Again SHell.
16 Bash is free software; you can redistribute it and/or modify it under
17 the terms of the GNU General Public License as published by the Free
18 Software Foundation; either version 2, or (at your option) any later
21 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
22 WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 You should have received a copy of the GNU General Public License along
27 with Bash; see the file COPYING. If not, write to the Free Software
28 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
32 #if defined (ARRAY_VARS)
34 #if defined (HAVE_UNISTD_H)
36 # include <sys/types.h>
46 #include "builtins/common.h"
48 #define ADD_BEFORE(ae, new) \
50 ae->prev->next = new; \
51 new->prev = ae->prev; \
56 static char *array_to_string_internal __P((ARRAY_ELEMENT *, ARRAY_ELEMENT *, char *, int));
64 r =(ARRAY *)xmalloc(sizeof(ARRAY));
65 r->type = array_indexed;
68 head = array_create_element(-1, (char *)NULL); /* dummy head */
69 head->prev = head->next = head;
78 register ARRAY_ELEMENT *r, *r1;
82 for (r = element_forw(a->head); r != a->head; ) {
84 array_dispose_element(r);
87 a->head->next = a->head->prev = a->head;
99 array_dispose_element(a->head);
108 ARRAY_ELEMENT *ae, *new;
111 return((ARRAY *) NULL);
114 a1->max_index = a->max_index;
115 a1->num_elements = a->num_elements;
116 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
117 new = array_create_element(element_index(ae), element_value(ae));
118 ADD_BEFORE(a1->head, new);
123 #ifdef INCLUDE_UNUSED
125 * Make and return a new array composed of the elements in array A from
129 array_slice(array, s, e)
131 ARRAY_ELEMENT *s, *e;
134 ARRAY_ELEMENT *p, *n;
139 a->type = array->type;
141 for (p = s, i = 0; p != e; p = element_forw(p), i++) {
142 n = array_create_element (element_index(p), element_value(p));
143 ADD_BEFORE(a->head, n);
144 mi = element_index(ae);
153 * Walk the array, calling FUNC once for each element, with the array
154 * element as the argument.
157 array_walk(a, func, udata)
159 sh_ae_map_func_t *func;
162 register ARRAY_ELEMENT *ae;
164 if (a == 0 || array_empty(a))
166 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae))
167 if ((*func)(ae, udata) < 0)
172 * Shift the array A N elements to the left. Delete the first N elements
173 * and subtract N from the indices of the remaining elements. If FLAGS
174 * does not include AS_DISPOSE, this returns a singly-linked null-terminated
175 * list of elements so the caller can dispose of the chain. If FLAGS
176 * includes AS_DISPOSE, this function disposes of the shifted-out elements
180 array_shift(a, n, flags)
184 register ARRAY_ELEMENT *ae, *ret;
187 if (a == 0 || array_empty(a) || n <= 0)
188 return ((ARRAY_ELEMENT *)NULL);
190 for (i = 0, ret = ae = element_forw(a->head); ae != a->head && i < n; ae = element_forw(ae), i++)
193 /* Easy case; shifting out all of the elements */
194 if (flags & AS_DISPOSE) {
196 return ((ARRAY_ELEMENT *)NULL);
198 for (ae = ret; element_forw(ae) != a->head; ae = element_forw(ae))
200 element_forw(ae) = (ARRAY_ELEMENT *)NULL;
201 a->head->next = a->head->prev = a->head;
207 * ae now points to the list of elements we want to retain.
208 * ret points to the list we want to either destroy or return.
210 ae->prev->next = (ARRAY_ELEMENT *)NULL; /* null-terminate RET */
212 a->head->next = ae; /* slice RET out of the array */
215 for ( ; ae != a->head; ae = element_forw(ae))
216 element_index(ae) -= n; /* renumber retained indices */
218 a->num_elements -= n; /* modify bookkeeping information */
221 if (flags & AS_DISPOSE) {
222 for (ae = ret; ae; ) {
223 ret = element_forw(ae);
224 array_dispose_element(ae);
227 return ((ARRAY_ELEMENT *)NULL);
234 * Shift array A right N indices. If S is non-null, it becomes the value of
235 * the new element 0. Returns the number of elements in the array after the
239 array_rshift (a, n, s)
244 register ARRAY_ELEMENT *ae, *new;
246 if (a == 0 || (array_empty(a) && s == 0))
249 return (a->num_elements);
251 ae = element_forw(a->head);
253 new = array_create_element(0, s);
256 if (array_num_elements(a) == 1) /* array was empty */
261 * Renumber all elements in the array except the one we just added.
263 for ( ; ae != a->head; ae = element_forw(ae))
264 element_index(ae) += n;
266 a->max_index = element_index(a->head->prev);
268 return (a->num_elements);
272 array_unshift_element(a)
275 return (array_shift (a, 1, 0));
279 array_shift_element(a, v)
283 return (array_rshift (a, 1, v));
293 if (array == 0 || array_head(array) == 0 || array_empty(array))
294 return (ARRAY *)NULL;
295 for (a = element_forw(array->head); a != array->head; a = element_forw(a)) {
296 t = quote_string (a->value);
304 * Return a string whose elements are the members of array A beginning at
305 * index START and spanning NELEM members. Null elements are counted.
306 * Since arrays are sparse, unset array elements are not counted.
309 array_subrange (a, start, nelem, starsub, quoted)
311 arrayind_t start, nelem;
314 ARRAY_ELEMENT *h, *p;
318 p = a ? array_head (a) : 0;
319 if (p == 0 || array_empty (a) || start > array_max_index(a))
320 return ((char *)NULL);
323 * Find element with index START. If START corresponds to an unset
324 * element (arrays can be sparse), use the first element whose index
325 * is >= START. If START is < 0, we count START indices back from
326 * the end of A (not elements, even with sparse arrays -- START is an
329 for (p = element_forw(p); p != array_head(a) && start > element_index(p); p = element_forw(p))
333 return ((char *)NULL);
335 /* Starting at P, take NELEM elements, inclusive. */
336 for (i = 0, h = p; p != a->head && i < nelem; i++, p = element_forw(p))
339 if (starsub && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) {
341 sep[0] = ifs ? *ifs : '\0';
346 return (array_to_string_internal (h, p, sep, quoted));
350 array_patsub (a, pat, rep, mflags)
357 char *t, *ifs, sifs[2];
359 if (a == 0 || array_head(a) == 0 || array_empty(a))
360 return ((char *)NULL);
363 for (e = element_forw(a2->head); e != a2->head; e = element_forw(e)) {
364 t = pat_subst(element_value(e), pat, rep, mflags);
365 FREE(element_value(e));
369 if (mflags & MATCH_QUOTED)
371 if (mflags & MATCH_STARSUB) {
373 sifs[0] = ifs ? *ifs : '\0';
375 t = array_to_string (a2, sifs, 0);
377 t = array_to_string (a2, " ", 0);
384 * Allocate and return a new array element with index INDEX and value
388 array_create_element(indx, value)
394 r = (ARRAY_ELEMENT *)xmalloc(sizeof(ARRAY_ELEMENT));
396 r->value = value ? savestring(value) : (char *)NULL;
397 r->next = r->prev = (ARRAY_ELEMENT *) NULL;
401 #ifdef INCLUDE_UNUSED
403 array_copy_element(ae)
406 return(ae ? array_create_element(element_index(ae), element_value(ae))
407 : (ARRAY_ELEMENT *) NULL);
412 array_dispose_element(ae)
422 * Add a new element with index I and value V to array A (a[i] = v).
425 array_insert(a, i, v)
430 register ARRAY_ELEMENT *new, *ae;
434 new = array_create_element(i, v);
435 if (i > array_max_index(a)) {
437 * Hook onto the end. This also works for an empty array.
438 * Fast path for the common case of allocating arrays
441 ADD_BEFORE(a->head, new);
447 * Otherwise we search for the spot to insert it.
449 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
450 if (element_index(ae) == i) {
452 * Replacing an existing element.
454 array_dispose_element(new);
455 free(element_value(ae));
456 ae->value = v ? savestring(v) : (char *)NULL;
458 } else if (element_index(ae) > i) {
464 return (-1); /* problem */
468 * Delete the element with index I from array A and return it so the
469 * caller can dispose of it.
476 register ARRAY_ELEMENT *ae;
478 if (a == 0 || array_empty(a))
479 return((ARRAY_ELEMENT *) NULL);
480 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae))
481 if (element_index(ae) == i) {
482 ae->next->prev = ae->prev;
483 ae->prev->next = ae->next;
485 if (i == array_max_index(a))
486 a->max_index = element_index(ae->prev);
489 return((ARRAY_ELEMENT *) NULL);
493 * Return the value of a[i].
496 array_reference(a, i)
500 register ARRAY_ELEMENT *ae;
502 if (a == 0 || array_empty(a))
503 return((char *) NULL);
504 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae))
505 if (element_index(ae) == i)
506 return(element_value(ae));
507 return((char *) NULL);
510 /* Convenience routines for the shell to translate to and from the form used
511 by the rest of the code. */
514 array_to_word_list(a)
520 if (a == 0 || array_empty(a))
521 return((WORD_LIST *)NULL);
522 list = (WORD_LIST *)NULL;
523 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae))
524 list = make_word_list (make_bare_word(element_value(ae)), list);
525 return (REVERSE_LIST(list, WORD_LIST *));
529 array_from_word_list (list)
535 return((ARRAY *)NULL);
537 return (array_assign_list (a, list));
541 array_keys_to_word_list(a)
548 if (a == 0 || array_empty(a))
549 return((WORD_LIST *)NULL);
550 list = (WORD_LIST *)NULL;
551 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
552 t = itos(element_index(ae));
553 list = make_word_list (make_bare_word(t), list);
556 return (REVERSE_LIST(list, WORD_LIST *));
560 array_assign_list (array, list)
564 register WORD_LIST *l;
565 register arrayind_t i;
567 for (l = list, i = 0; l; l = l->next, i++)
568 array_insert(array, i, l->word->word);
580 if (a == 0 || array_empty(a))
581 return ((char **)NULL);
582 ret = strvec_create (array_num_elements (a) + 1);
584 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
585 t = element_value (ae);
586 ret[i++] = t ? savestring (t) : (char *)NULL;
588 ret[i] = (char *)NULL;
593 * Return a string that is the concatenation of all the elements in A,
597 array_to_string_internal (start, end, sep, quoted)
598 ARRAY_ELEMENT *start, *end;
604 int slen, rsize, rlen, reg;
606 if (start == end) /* XXX - should not happen */
607 return ((char *)NULL);
611 for (rsize = rlen = 0, ae = start; ae != end; ae = element_forw(ae)) {
613 result = (char *)xmalloc (rsize = 64);
614 if (element_value(ae)) {
615 t = quoted ? quote_string(element_value(ae)) : element_value(ae);
617 RESIZE_MALLOCED_BUFFER (result, rlen, (reg + slen + 2),
619 strcpy(result + rlen, t);
624 * Add a separator only after non-null elements.
626 if (element_forw(ae) != end) {
627 strcpy(result + rlen, sep);
633 result[rlen] = '\0'; /* XXX */
638 array_to_assign (a, quoted)
642 char *result, *valstr, *is;
643 char indstr[INT_STRLEN_BOUND(intmax_t) + 1];
645 int rsize, rlen, elen;
647 if (a == 0 || array_empty (a))
648 return((char *)NULL);
650 result = (char *)xmalloc (rsize = 128);
654 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
655 is = inttostr (element_index(ae), indstr, sizeof(indstr));
656 valstr = element_value (ae) ? sh_double_quote (element_value(ae))
658 elen = STRLEN (indstr) + 8 + STRLEN (valstr);
659 RESIZE_MALLOCED_BUFFER (result, rlen, (elen + 1), rsize, rsize);
661 result[rlen++] = '[';
662 strcpy (result + rlen, is);
664 result[rlen++] = ']';
665 result[rlen++] = '=';
667 strcpy (result + rlen, valstr);
668 rlen += STRLEN (valstr);
671 if (element_forw(ae) != a->head)
672 result[rlen++] = ' ';
676 RESIZE_MALLOCED_BUFFER (result, rlen, 1, rsize, 8);
677 result[rlen++] = ')';
680 /* This is not as efficient as it could be... */
681 valstr = sh_single_quote (result);
689 array_to_string (a, sep, quoted)
695 return((char *)NULL);
697 return(savestring(""));
698 return (array_to_string_internal (element_forw(a->head), a->head, sep, quoted));
701 #if defined (INCLUDE_UNUSED) || defined (TEST_ARRAY)
703 * Return an array consisting of elements in S, separated by SEP
706 array_from_string(s, sep)
713 return((ARRAY *)NULL);
714 w = list_string (s, sep, 0);
716 return((ARRAY *)NULL);
717 a = array_from_word_list (w);
722 #if defined (TEST_ARRAY)
724 * To make a running version, compile -DTEST_ARRAY and link with:
725 * xmalloc.o syntax.o lib/malloc/libmalloc.a lib/sh/libsh.a
727 int interrupt_immediately = 0;
737 fatal_error(const char *s, ...)
739 fprintf(stderr, "array_test: fatal memory error\n");
744 programming_error(const char *s, ...)
746 fprintf(stderr, "array_test: fatal programming error\n");
756 w = (WORD_DESC *)xmalloc(sizeof(WORD_DESC));
757 w->word = s ? savestring(s) : savestring ("");
769 w = (WORD_LIST *)xmalloc(sizeof(WORD_LIST));
784 return (WORD_LIST *)NULL;
786 wl = (WORD_LIST *)NULL;
789 wl = make_word_list (make_bare_word(a), wl);
790 a = strtok((char *)NULL, t);
792 return (REVERSE_LIST (wl, WORD_LIST *));
799 register GENERIC_LIST *next, *prev;
801 for (prev = 0; list; ) {
811 pat_subst(s, t, u, i)
815 return ((char *)NULL);
822 return savestring(s);
828 char lbuf[INT_STRLEN_BOUND (intmax_t) + 1];
830 printf("array[%s] = %s\n",
831 inttostr (element_index(ae), lbuf, sizeof (lbuf)),
839 array_walk(a, print_element, (void *)NULL);
844 ARRAY *a, *new_a, *copy_of_a;
845 ARRAY_ELEMENT *ae, *aew;
849 array_insert(a, 1, "one");
850 array_insert(a, 7, "seven");
851 array_insert(a, 4, "four");
852 array_insert(a, 1029, "one thousand twenty-nine");
853 array_insert(a, 12, "twelve");
854 array_insert(a, 42, "forty-two");
856 s = array_to_string (a, " ", 0);
857 printf("s = %s\n", s);
858 copy_of_a = array_from_string(s, " ");
859 printf("copy_of_a:");
860 print_array(copy_of_a);
861 array_dispose(copy_of_a);
864 ae = array_remove(a, 4);
865 array_dispose_element(ae);
866 ae = array_remove(a, 1029);
867 array_dispose_element(ae);
868 array_insert(a, 16, "sixteen");
870 s = array_to_string (a, " ", 0);
871 printf("s = %s\n", s);
872 copy_of_a = array_from_string(s, " ");
873 printf("copy_of_a:");
874 print_array(copy_of_a);
875 array_dispose(copy_of_a);
878 array_insert(a, 2, "two");
879 array_insert(a, 1029, "new one thousand twenty-nine");
880 array_insert(a, 0, "zero");
881 array_insert(a, 134, "");
883 s = array_to_string (a, ":", 0);
884 printf("s = %s\n", s);
885 copy_of_a = array_from_string(s, ":");
886 printf("copy_of_a:");
887 print_array(copy_of_a);
888 array_dispose(copy_of_a);
891 new_a = array_copy(a);
893 s = array_to_string (new_a, ":", 0);
894 printf("s = %s\n", s);
895 copy_of_a = array_from_string(s, ":");
897 printf("copy_of_a:");
898 print_array(copy_of_a);
899 array_shift(copy_of_a, 2, AS_DISPOSE);
900 printf("copy_of_a shifted by two:");
901 print_array(copy_of_a);
902 ae = array_shift(copy_of_a, 2, 0);
903 printf("copy_of_a shifted by two:");
904 print_array(copy_of_a);
906 aew = element_forw(ae);
907 array_dispose_element(ae);
910 array_rshift(copy_of_a, 1, (char *)0);
911 printf("copy_of_a rshift by 1:");
912 print_array(copy_of_a);
913 array_rshift(copy_of_a, 2, "new element zero");
914 printf("copy_of_a rshift again by 2 with new element zero:");
915 print_array(copy_of_a);
916 s = array_to_assign(copy_of_a, 0);
917 printf("copy_of_a=%s\n", s);
919 ae = array_shift(copy_of_a, array_num_elements(copy_of_a), 0);
921 aew = element_forw(ae);
922 array_dispose_element(ae);
925 array_dispose(copy_of_a);
928 array_dispose(new_a);
931 #endif /* TEST_ARRAY */
932 #endif /* ARRAY_VARS */