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);
124 * Make and return a new array composed of the elements in array A from
128 array_slice(array
, s
, e
)
130 ARRAY_ELEMENT
*s
, *e
;
133 ARRAY_ELEMENT
*p
, *n
;
138 a
->type
= array
->type
;
140 for (p
= s
, i
= 0; p
!= e
; p
= element_forw(p
), i
++) {
141 n
= array_create_element (element_index(p
), element_value(p
));
142 ADD_BEFORE(a
->head
, n
);
143 mi
= element_index(n
);
151 * Walk the array, calling FUNC once for each element, with the array
152 * element as the argument.
155 array_walk(a
, func
, udata
)
157 sh_ae_map_func_t
*func
;
160 register ARRAY_ELEMENT
*ae
;
162 if (a
== 0 || array_empty(a
))
164 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
))
165 if ((*func
)(ae
, udata
) < 0)
170 * Shift the array A N elements to the left. Delete the first N elements
171 * and subtract N from the indices of the remaining elements. If FLAGS
172 * does not include AS_DISPOSE, this returns a singly-linked null-terminated
173 * list of elements so the caller can dispose of the chain. If FLAGS
174 * includes AS_DISPOSE, this function disposes of the shifted-out elements
178 array_shift(a
, n
, flags
)
182 register ARRAY_ELEMENT
*ae
, *ret
;
185 if (a
== 0 || array_empty(a
) || n
<= 0)
186 return ((ARRAY_ELEMENT
*)NULL
);
188 for (i
= 0, ret
= ae
= element_forw(a
->head
); ae
!= a
->head
&& i
< n
; ae
= element_forw(ae
), i
++)
191 /* Easy case; shifting out all of the elements */
192 if (flags
& AS_DISPOSE
) {
194 return ((ARRAY_ELEMENT
*)NULL
);
196 for (ae
= ret
; element_forw(ae
) != a
->head
; ae
= element_forw(ae
))
198 element_forw(ae
) = (ARRAY_ELEMENT
*)NULL
;
199 a
->head
->next
= a
->head
->prev
= a
->head
;
205 * ae now points to the list of elements we want to retain.
206 * ret points to the list we want to either destroy or return.
208 ae
->prev
->next
= (ARRAY_ELEMENT
*)NULL
; /* null-terminate RET */
210 a
->head
->next
= ae
; /* slice RET out of the array */
213 for ( ; ae
!= a
->head
; ae
= element_forw(ae
))
214 element_index(ae
) -= n
; /* renumber retained indices */
216 a
->num_elements
-= n
; /* modify bookkeeping information */
219 if (flags
& AS_DISPOSE
) {
220 for (ae
= ret
; ae
; ) {
221 ret
= element_forw(ae
);
222 array_dispose_element(ae
);
225 return ((ARRAY_ELEMENT
*)NULL
);
232 * Shift array A right N indices. If S is non-null, it becomes the value of
233 * the new element 0. Returns the number of elements in the array after the
237 array_rshift (a
, n
, s
)
242 register ARRAY_ELEMENT
*ae
, *new;
244 if (a
== 0 || (array_empty(a
) && s
== 0))
247 return (a
->num_elements
);
249 ae
= element_forw(a
->head
);
251 new = array_create_element(0, s
);
254 if (array_num_elements(a
) == 1) /* array was empty */
259 * Renumber all elements in the array except the one we just added.
261 for ( ; ae
!= a
->head
; ae
= element_forw(ae
))
262 element_index(ae
) += n
;
264 a
->max_index
= element_index(a
->head
->prev
);
266 return (a
->num_elements
);
270 array_unshift_element(a
)
273 return (array_shift (a
, 1, 0));
277 array_shift_element(a
, v
)
281 return (array_rshift (a
, 1, v
));
291 if (array
== 0 || array_head(array
) == 0 || array_empty(array
))
292 return (ARRAY
*)NULL
;
293 for (a
= element_forw(array
->head
); a
!= array
->head
; a
= element_forw(a
)) {
294 t
= quote_string (a
->value
);
302 array_quote_escapes(array
)
308 if (array
== 0 || array_head(array
) == 0 || array_empty(array
))
309 return (ARRAY
*)NULL
;
310 for (a
= element_forw(array
->head
); a
!= array
->head
; a
= element_forw(a
)) {
311 t
= quote_escapes (a
->value
);
319 * Return a string whose elements are the members of array A beginning at
320 * index START and spanning NELEM members. Null elements are counted.
321 * Since arrays are sparse, unset array elements are not counted.
324 array_subrange (a
, start
, nelem
, starsub
, quoted
)
326 arrayind_t start
, nelem
;
330 ARRAY_ELEMENT
*h
, *p
;
332 char *ifs
, sep
[2], *t
;
334 p
= a
? array_head (a
) : 0;
335 if (p
== 0 || array_empty (a
) || start
> array_max_index(a
))
336 return ((char *)NULL
);
339 * Find element with index START. If START corresponds to an unset
340 * element (arrays can be sparse), use the first element whose index
341 * is >= START. If START is < 0, we count START indices back from
342 * the end of A (not elements, even with sparse arrays -- START is an
345 for (p
= element_forw(p
); p
!= array_head(a
) && start
> element_index(p
); p
= element_forw(p
))
349 return ((char *)NULL
);
351 /* Starting at P, take NELEM elements, inclusive. */
352 for (i
= 0, h
= p
; p
!= a
->head
&& i
< nelem
; i
++, p
= element_forw(p
))
355 a2
= array_slice(a
, h
, p
);
357 if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))
360 array_quote_escapes(a2
);
362 if (starsub
&& (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))) {
364 sep
[0] = ifs
? *ifs
: '\0';
369 t
= array_to_string (a2
, sep
, 0);
376 array_patsub (a
, pat
, rep
, mflags
)
383 char *t
, *ifs
, sifs
[2];
385 if (a
== 0 || array_head(a
) == 0 || array_empty(a
))
386 return ((char *)NULL
);
389 for (e
= element_forw(a2
->head
); e
!= a2
->head
; e
= element_forw(e
)) {
390 t
= pat_subst(element_value(e
), pat
, rep
, mflags
);
391 FREE(element_value(e
));
395 if (mflags
& MATCH_QUOTED
)
398 array_quote_escapes(a2
);
399 if (mflags
& MATCH_STARSUB
) {
401 sifs
[0] = ifs
? *ifs
: '\0';
403 t
= array_to_string (a2
, sifs
, 0);
405 t
= array_to_string (a2
, " ", 0);
412 * Allocate and return a new array element with index INDEX and value
416 array_create_element(indx
, value
)
422 r
= (ARRAY_ELEMENT
*)xmalloc(sizeof(ARRAY_ELEMENT
));
424 r
->value
= value
? savestring(value
) : (char *)NULL
;
425 r
->next
= r
->prev
= (ARRAY_ELEMENT
*) NULL
;
429 #ifdef INCLUDE_UNUSED
431 array_copy_element(ae
)
434 return(ae
? array_create_element(element_index(ae
), element_value(ae
))
435 : (ARRAY_ELEMENT
*) NULL
);
440 array_dispose_element(ae
)
450 * Add a new element with index I and value V to array A (a[i] = v).
453 array_insert(a
, i
, v
)
458 register ARRAY_ELEMENT
*new, *ae
;
462 new = array_create_element(i
, v
);
463 if (i
> array_max_index(a
)) {
465 * Hook onto the end. This also works for an empty array.
466 * Fast path for the common case of allocating arrays
469 ADD_BEFORE(a
->head
, new);
475 * Otherwise we search for the spot to insert it.
477 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
)) {
478 if (element_index(ae
) == i
) {
480 * Replacing an existing element.
482 array_dispose_element(new);
483 free(element_value(ae
));
484 ae
->value
= v
? savestring(v
) : (char *)NULL
;
486 } else if (element_index(ae
) > i
) {
492 return (-1); /* problem */
496 * Delete the element with index I from array A and return it so the
497 * caller can dispose of it.
504 register ARRAY_ELEMENT
*ae
;
506 if (a
== 0 || array_empty(a
))
507 return((ARRAY_ELEMENT
*) NULL
);
508 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
))
509 if (element_index(ae
) == i
) {
510 ae
->next
->prev
= ae
->prev
;
511 ae
->prev
->next
= ae
->next
;
513 if (i
== array_max_index(a
))
514 a
->max_index
= element_index(ae
->prev
);
517 return((ARRAY_ELEMENT
*) NULL
);
521 * Return the value of a[i].
524 array_reference(a
, i
)
528 register ARRAY_ELEMENT
*ae
;
530 if (a
== 0 || array_empty(a
))
531 return((char *) NULL
);
532 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
))
533 if (element_index(ae
) == i
)
534 return(element_value(ae
));
535 return((char *) NULL
);
538 /* Convenience routines for the shell to translate to and from the form used
539 by the rest of the code. */
542 array_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 list
= make_word_list (make_bare_word(element_value(ae
)), list
);
553 return (REVERSE_LIST(list
, WORD_LIST
*));
557 array_from_word_list (list
)
563 return((ARRAY
*)NULL
);
565 return (array_assign_list (a
, list
));
569 array_keys_to_word_list(a
)
576 if (a
== 0 || array_empty(a
))
577 return((WORD_LIST
*)NULL
);
578 list
= (WORD_LIST
*)NULL
;
579 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
)) {
580 t
= itos(element_index(ae
));
581 list
= make_word_list (make_bare_word(t
), list
);
584 return (REVERSE_LIST(list
, WORD_LIST
*));
588 array_assign_list (array
, list
)
592 register WORD_LIST
*l
;
593 register arrayind_t i
;
595 for (l
= list
, i
= 0; l
; l
= l
->next
, i
++)
596 array_insert(array
, i
, l
->word
->word
);
608 if (a
== 0 || array_empty(a
))
609 return ((char **)NULL
);
610 ret
= strvec_create (array_num_elements (a
) + 1);
612 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
)) {
613 t
= element_value (ae
);
614 ret
[i
++] = t
? savestring (t
) : (char *)NULL
;
616 ret
[i
] = (char *)NULL
;
621 * Return a string that is the concatenation of all the elements in A,
625 array_to_string_internal (start
, end
, sep
, quoted
)
626 ARRAY_ELEMENT
*start
, *end
;
632 int slen
, rsize
, rlen
, reg
;
634 if (start
== end
) /* XXX - should not happen */
635 return ((char *)NULL
);
639 for (rsize
= rlen
= 0, ae
= start
; ae
!= end
; ae
= element_forw(ae
)) {
641 result
= (char *)xmalloc (rsize
= 64);
642 if (element_value(ae
)) {
643 t
= quoted
? quote_string(element_value(ae
)) : element_value(ae
);
645 RESIZE_MALLOCED_BUFFER (result
, rlen
, (reg
+ slen
+ 2),
647 strcpy(result
+ rlen
, t
);
652 * Add a separator only after non-null elements.
654 if (element_forw(ae
) != end
) {
655 strcpy(result
+ rlen
, sep
);
661 result
[rlen
] = '\0'; /* XXX */
666 array_to_assign (a
, quoted
)
670 char *result
, *valstr
, *is
;
671 char indstr
[INT_STRLEN_BOUND(intmax_t) + 1];
673 int rsize
, rlen
, elen
;
675 if (a
== 0 || array_empty (a
))
676 return((char *)NULL
);
678 result
= (char *)xmalloc (rsize
= 128);
682 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
)) {
683 is
= inttostr (element_index(ae
), indstr
, sizeof(indstr
));
684 valstr
= element_value (ae
) ? sh_double_quote (element_value(ae
))
686 elen
= STRLEN (indstr
) + 8 + STRLEN (valstr
);
687 RESIZE_MALLOCED_BUFFER (result
, rlen
, (elen
+ 1), rsize
, rsize
);
689 result
[rlen
++] = '[';
690 strcpy (result
+ rlen
, is
);
692 result
[rlen
++] = ']';
693 result
[rlen
++] = '=';
695 strcpy (result
+ rlen
, valstr
);
696 rlen
+= STRLEN (valstr
);
699 if (element_forw(ae
) != a
->head
)
700 result
[rlen
++] = ' ';
704 RESIZE_MALLOCED_BUFFER (result
, rlen
, 1, rsize
, 8);
705 result
[rlen
++] = ')';
708 /* This is not as efficient as it could be... */
709 valstr
= sh_single_quote (result
);
717 array_to_string (a
, sep
, quoted
)
723 return((char *)NULL
);
725 return(savestring(""));
726 return (array_to_string_internal (element_forw(a
->head
), a
->head
, sep
, quoted
));
729 #if defined (INCLUDE_UNUSED) || defined (TEST_ARRAY)
731 * Return an array consisting of elements in S, separated by SEP
734 array_from_string(s
, sep
)
741 return((ARRAY
*)NULL
);
742 w
= list_string (s
, sep
, 0);
744 return((ARRAY
*)NULL
);
745 a
= array_from_word_list (w
);
750 #if defined (TEST_ARRAY)
752 * To make a running version, compile -DTEST_ARRAY and link with:
753 * xmalloc.o syntax.o lib/malloc/libmalloc.a lib/sh/libsh.a
755 int interrupt_immediately
= 0;
765 fatal_error(const char *s
, ...)
767 fprintf(stderr
, "array_test: fatal memory error\n");
772 programming_error(const char *s
, ...)
774 fprintf(stderr
, "array_test: fatal programming error\n");
784 w
= (WORD_DESC
*)xmalloc(sizeof(WORD_DESC
));
785 w
->word
= s
? savestring(s
) : savestring ("");
797 w
= (WORD_LIST
*)xmalloc(sizeof(WORD_LIST
));
812 return (WORD_LIST
*)NULL
;
814 wl
= (WORD_LIST
*)NULL
;
817 wl
= make_word_list (make_bare_word(a
), wl
);
818 a
= strtok((char *)NULL
, t
);
820 return (REVERSE_LIST (wl
, WORD_LIST
*));
827 register GENERIC_LIST
*next
, *prev
;
829 for (prev
= 0; list
; ) {
839 pat_subst(s
, t
, u
, i
)
843 return ((char *)NULL
);
850 return savestring(s
);
856 char lbuf
[INT_STRLEN_BOUND (intmax_t) + 1];
858 printf("array[%s] = %s\n",
859 inttostr (element_index(ae
), lbuf
, sizeof (lbuf
)),
867 array_walk(a
, print_element
, (void *)NULL
);
872 ARRAY
*a
, *new_a
, *copy_of_a
;
873 ARRAY_ELEMENT
*ae
, *aew
;
877 array_insert(a
, 1, "one");
878 array_insert(a
, 7, "seven");
879 array_insert(a
, 4, "four");
880 array_insert(a
, 1029, "one thousand twenty-nine");
881 array_insert(a
, 12, "twelve");
882 array_insert(a
, 42, "forty-two");
884 s
= array_to_string (a
, " ", 0);
885 printf("s = %s\n", s
);
886 copy_of_a
= array_from_string(s
, " ");
887 printf("copy_of_a:");
888 print_array(copy_of_a
);
889 array_dispose(copy_of_a
);
892 ae
= array_remove(a
, 4);
893 array_dispose_element(ae
);
894 ae
= array_remove(a
, 1029);
895 array_dispose_element(ae
);
896 array_insert(a
, 16, "sixteen");
898 s
= array_to_string (a
, " ", 0);
899 printf("s = %s\n", s
);
900 copy_of_a
= array_from_string(s
, " ");
901 printf("copy_of_a:");
902 print_array(copy_of_a
);
903 array_dispose(copy_of_a
);
906 array_insert(a
, 2, "two");
907 array_insert(a
, 1029, "new one thousand twenty-nine");
908 array_insert(a
, 0, "zero");
909 array_insert(a
, 134, "");
911 s
= array_to_string (a
, ":", 0);
912 printf("s = %s\n", s
);
913 copy_of_a
= array_from_string(s
, ":");
914 printf("copy_of_a:");
915 print_array(copy_of_a
);
916 array_dispose(copy_of_a
);
919 new_a
= array_copy(a
);
921 s
= array_to_string (new_a
, ":", 0);
922 printf("s = %s\n", s
);
923 copy_of_a
= array_from_string(s
, ":");
925 printf("copy_of_a:");
926 print_array(copy_of_a
);
927 array_shift(copy_of_a
, 2, AS_DISPOSE
);
928 printf("copy_of_a shifted by two:");
929 print_array(copy_of_a
);
930 ae
= array_shift(copy_of_a
, 2, 0);
931 printf("copy_of_a shifted by two:");
932 print_array(copy_of_a
);
934 aew
= element_forw(ae
);
935 array_dispose_element(ae
);
938 array_rshift(copy_of_a
, 1, (char *)0);
939 printf("copy_of_a rshift by 1:");
940 print_array(copy_of_a
);
941 array_rshift(copy_of_a
, 2, "new element zero");
942 printf("copy_of_a rshift again by 2 with new element zero:");
943 print_array(copy_of_a
);
944 s
= array_to_assign(copy_of_a
, 0);
945 printf("copy_of_a=%s\n", s
);
947 ae
= array_shift(copy_of_a
, array_num_elements(copy_of_a
), 0);
949 aew
= element_forw(ae
);
950 array_dispose_element(ae
);
953 array_dispose(copy_of_a
);
956 array_dispose(new_a
);
959 #endif /* TEST_ARRAY */
960 #endif /* ARRAY_VARS */