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-2009 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
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation, either version 3 of the License, or
19 (at your option) any later version.
21 Bash is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with Bash. If not, see <http://www.gnu.org/licenses/>.
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));
58 static ARRAY
*lastarray
= 0;
59 static ARRAY_ELEMENT
*lastref
= 0;
61 #define IS_LASTREF(a) ((a) == lastarray)
63 #define INVALIDATE_LASTREF(a) \
65 if ((a) == lastarray) { \
71 #define SET_LASTREF(a, e) \
77 #define UNSET_LASTREF() \
89 r
=(ARRAY
*)xmalloc(sizeof(ARRAY
));
90 r
->type
= array_indexed
;
93 head
= array_create_element(-1, (char *)NULL
); /* dummy head */
94 head
->prev
= head
->next
= head
;
103 register ARRAY_ELEMENT
*r
, *r1
;
107 for (r
= element_forw(a
->head
); r
!= a
->head
; ) {
108 r1
= element_forw(r
);
109 array_dispose_element(r
);
112 a
->head
->next
= a
->head
->prev
= a
->head
;
115 INVALIDATE_LASTREF(a
);
125 array_dispose_element(a
->head
);
134 ARRAY_ELEMENT
*ae
, *new;
137 return((ARRAY
*) NULL
);
140 a1
->max_index
= a
->max_index
;
141 a1
->num_elements
= a
->num_elements
;
142 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
)) {
143 new = array_create_element(element_index(ae
), element_value(ae
));
144 ADD_BEFORE(a1
->head
, new);
150 * Make and return a new array composed of the elements in array A from
154 array_slice(array
, s
, e
)
156 ARRAY_ELEMENT
*s
, *e
;
159 ARRAY_ELEMENT
*p
, *n
;
164 a
->type
= array
->type
;
166 for (mi
= 0, p
= s
, i
= 0; p
!= e
; p
= element_forw(p
), i
++) {
167 n
= array_create_element (element_index(p
), element_value(p
));
168 ADD_BEFORE(a
->head
, n
);
169 mi
= element_index(n
);
177 * Walk the array, calling FUNC once for each element, with the array
178 * element as the argument.
181 array_walk(a
, func
, udata
)
183 sh_ae_map_func_t
*func
;
186 register ARRAY_ELEMENT
*ae
;
188 if (a
== 0 || array_empty(a
))
190 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
))
191 if ((*func
)(ae
, udata
) < 0)
196 * Shift the array A N elements to the left. Delete the first N elements
197 * and subtract N from the indices of the remaining elements. If FLAGS
198 * does not include AS_DISPOSE, this returns a singly-linked null-terminated
199 * list of elements so the caller can dispose of the chain. If FLAGS
200 * includes AS_DISPOSE, this function disposes of the shifted-out elements
204 array_shift(a
, n
, flags
)
208 register ARRAY_ELEMENT
*ae
, *ret
;
211 if (a
== 0 || array_empty(a
) || n
<= 0)
212 return ((ARRAY_ELEMENT
*)NULL
);
214 INVALIDATE_LASTREF(a
);
215 for (i
= 0, ret
= ae
= element_forw(a
->head
); ae
!= a
->head
&& i
< n
; ae
= element_forw(ae
), i
++)
218 /* Easy case; shifting out all of the elements */
219 if (flags
& AS_DISPOSE
) {
221 return ((ARRAY_ELEMENT
*)NULL
);
223 for (ae
= ret
; element_forw(ae
) != a
->head
; ae
= element_forw(ae
))
225 element_forw(ae
) = (ARRAY_ELEMENT
*)NULL
;
226 a
->head
->next
= a
->head
->prev
= a
->head
;
232 * ae now points to the list of elements we want to retain.
233 * ret points to the list we want to either destroy or return.
235 ae
->prev
->next
= (ARRAY_ELEMENT
*)NULL
; /* null-terminate RET */
237 a
->head
->next
= ae
; /* slice RET out of the array */
240 for ( ; ae
!= a
->head
; ae
= element_forw(ae
))
241 element_index(ae
) -= n
; /* renumber retained indices */
243 a
->num_elements
-= n
; /* modify bookkeeping information */
244 a
->max_index
= element_index(a
->head
->prev
);
246 if (flags
& AS_DISPOSE
) {
247 for (ae
= ret
; ae
; ) {
248 ret
= element_forw(ae
);
249 array_dispose_element(ae
);
252 return ((ARRAY_ELEMENT
*)NULL
);
259 * Shift array A right N indices. If S is non-null, it becomes the value of
260 * the new element 0. Returns the number of elements in the array after the
264 array_rshift (a
, n
, s
)
269 register ARRAY_ELEMENT
*ae
, *new;
271 if (a
== 0 || (array_empty(a
) && s
== 0))
274 return (a
->num_elements
);
276 ae
= element_forw(a
->head
);
278 new = array_create_element(0, s
);
281 if (array_num_elements(a
) == 1) { /* array was empty */
288 * Renumber all elements in the array except the one we just added.
290 for ( ; ae
!= a
->head
; ae
= element_forw(ae
))
291 element_index(ae
) += n
;
293 a
->max_index
= element_index(a
->head
->prev
);
295 INVALIDATE_LASTREF(a
);
296 return (a
->num_elements
);
300 array_unshift_element(a
)
303 return (array_shift (a
, 1, 0));
307 array_shift_element(a
, v
)
311 return (array_rshift (a
, 1, v
));
321 if (array
== 0 || array_head(array
) == 0 || array_empty(array
))
322 return (ARRAY
*)NULL
;
323 for (a
= element_forw(array
->head
); a
!= array
->head
; a
= element_forw(a
)) {
324 t
= quote_string (a
->value
);
332 array_quote_escapes(array
)
338 if (array
== 0 || array_head(array
) == 0 || array_empty(array
))
339 return (ARRAY
*)NULL
;
340 for (a
= element_forw(array
->head
); a
!= array
->head
; a
= element_forw(a
)) {
341 t
= quote_escapes (a
->value
);
355 if (array
== 0 || array_head(array
) == 0 || array_empty(array
))
356 return (ARRAY
*)NULL
;
357 for (a
= element_forw(array
->head
); a
!= array
->head
; a
= element_forw(a
)) {
358 t
= dequote_string (a
->value
);
366 array_dequote_escapes(array
)
372 if (array
== 0 || array_head(array
) == 0 || array_empty(array
))
373 return (ARRAY
*)NULL
;
374 for (a
= element_forw(array
->head
); a
!= array
->head
; a
= element_forw(a
)) {
375 t
= dequote_escapes (a
->value
);
383 array_remove_quoted_nulls(array
)
389 if (array
== 0 || array_head(array
) == 0 || array_empty(array
))
390 return (ARRAY
*)NULL
;
391 for (a
= element_forw(array
->head
); a
!= array
->head
; a
= element_forw(a
))
392 a
->value
= remove_quoted_nulls (a
->value
);
397 * Return a string whose elements are the members of array A beginning at
398 * index START and spanning NELEM members. Null elements are counted.
399 * Since arrays are sparse, unset array elements are not counted.
402 array_subrange (a
, start
, nelem
, starsub
, quoted
)
404 arrayind_t start
, nelem
;
408 ARRAY_ELEMENT
*h
, *p
;
410 char *ifs
, *sifs
, *t
;
413 p
= a
? array_head (a
) : 0;
414 if (p
== 0 || array_empty (a
) || start
> array_max_index(a
))
415 return ((char *)NULL
);
418 * Find element with index START. If START corresponds to an unset
419 * element (arrays can be sparse), use the first element whose index
420 * is >= START. If START is < 0, we count START indices back from
421 * the end of A (not elements, even with sparse arrays -- START is an
424 for (p
= element_forw(p
); p
!= array_head(a
) && start
> element_index(p
); p
= element_forw(p
))
428 return ((char *)NULL
);
430 /* Starting at P, take NELEM elements, inclusive. */
431 for (i
= 0, h
= p
; p
!= a
->head
&& i
< nelem
; i
++, p
= element_forw(p
))
434 a2
= array_slice(a
, h
, p
);
436 if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))
439 array_quote_escapes(a2
);
441 if (starsub
&& (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))) {
443 array_remove_quoted_nulls (a2
);
444 sifs
= ifs_firstchar ((int *)NULL
);
445 t
= array_to_string (a2
, sifs
, 0);
447 } else if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) {
449 sifs
= ifs_firstchar (&slen
);
451 if (ifs
== 0 || *ifs
== 0) {
453 sifs
= xrealloc(sifs
, 2);
457 t
= array_to_string (a2
, sifs
, 0);
460 t
= array_to_string (a2
, " ", 0);
467 array_patsub (a
, pat
, rep
, mflags
)
474 char *t
, *sifs
, *ifs
;
477 if (a
== 0 || array_head(a
) == 0 || array_empty(a
))
478 return ((char *)NULL
);
481 for (e
= element_forw(a2
->head
); e
!= a2
->head
; e
= element_forw(e
)) {
482 t
= pat_subst(element_value(e
), pat
, rep
, mflags
);
483 FREE(element_value(e
));
487 if (mflags
& MATCH_QUOTED
)
490 array_quote_escapes(a2
);
492 if (mflags
& MATCH_STARSUB
) {
493 array_remove_quoted_nulls (a2
);
494 sifs
= ifs_firstchar((int *)NULL
);
495 t
= array_to_string (a2
, sifs
, 0);
497 } else if (mflags
& MATCH_QUOTED
) {
499 sifs
= ifs_firstchar (&slen
);
501 if (ifs
== 0 || *ifs
== 0) {
503 sifs
= xrealloc (sifs
, 2);
507 t
= array_to_string (a2
, sifs
, 0);
510 t
= array_to_string (a2
, " ", 0);
517 array_modcase (a
, pat
, modop
, mflags
)
525 char *t
, *sifs
, *ifs
;
528 if (a
== 0 || array_head(a
) == 0 || array_empty(a
))
529 return ((char *)NULL
);
532 for (e
= element_forw(a2
->head
); e
!= a2
->head
; e
= element_forw(e
)) {
533 t
= sh_modcase(element_value(e
), pat
, modop
);
534 FREE(element_value(e
));
538 if (mflags
& MATCH_QUOTED
)
541 array_quote_escapes(a2
);
543 if (mflags
& MATCH_STARSUB
) {
544 array_remove_quoted_nulls (a2
);
545 sifs
= ifs_firstchar((int *)NULL
);
546 t
= array_to_string (a2
, sifs
, 0);
548 } else if (mflags
& MATCH_QUOTED
) {
550 sifs
= ifs_firstchar (&slen
);
552 if (ifs
== 0 || *ifs
== 0) {
554 sifs
= xrealloc (sifs
, 2);
558 t
= array_to_string (a2
, sifs
, 0);
561 t
= array_to_string (a2
, " ", 0);
567 * Allocate and return a new array element with index INDEX and value
571 array_create_element(indx
, value
)
577 r
= (ARRAY_ELEMENT
*)xmalloc(sizeof(ARRAY_ELEMENT
));
579 r
->value
= value
? savestring(value
) : (char *)NULL
;
580 r
->next
= r
->prev
= (ARRAY_ELEMENT
*) NULL
;
584 #ifdef INCLUDE_UNUSED
586 array_copy_element(ae
)
589 return(ae
? array_create_element(element_index(ae
), element_value(ae
))
590 : (ARRAY_ELEMENT
*) NULL
);
595 array_dispose_element(ae
)
605 * Add a new element with index I and value V to array A (a[i] = v).
608 array_insert(a
, i
, v
)
613 register ARRAY_ELEMENT
*new, *ae
;
617 new = array_create_element(i
, v
);
618 if (i
> array_max_index(a
)) {
620 * Hook onto the end. This also works for an empty array.
621 * Fast path for the common case of allocating arrays
624 ADD_BEFORE(a
->head
, new);
631 * Otherwise we search for the spot to insert it.
633 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
)) {
634 if (element_index(ae
) == i
) {
636 * Replacing an existing element.
638 array_dispose_element(new);
639 free(element_value(ae
));
640 ae
->value
= v
? savestring(v
) : (char *)NULL
;
643 } else if (element_index(ae
) > i
) {
650 INVALIDATE_LASTREF(a
);
651 return (-1); /* problem */
655 * Delete the element with index I from array A and return it so the
656 * caller can dispose of it.
663 register ARRAY_ELEMENT
*ae
;
665 if (a
== 0 || array_empty(a
))
666 return((ARRAY_ELEMENT
*) NULL
);
667 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
))
668 if (element_index(ae
) == i
) {
669 ae
->next
->prev
= ae
->prev
;
670 ae
->prev
->next
= ae
->next
;
672 if (i
== array_max_index(a
))
673 a
->max_index
= element_index(ae
->prev
);
674 INVALIDATE_LASTREF(a
);
677 return((ARRAY_ELEMENT
*) NULL
);
681 * Return the value of a[i].
684 array_reference(a
, i
)
688 register ARRAY_ELEMENT
*ae
;
690 if (a
== 0 || array_empty(a
))
691 return((char *) NULL
);
692 if (i
> array_max_index(a
))
693 return((char *)NULL
);
694 /* Keep roving pointer into array to optimize sequential access */
695 if (lastref
&& IS_LASTREF(a
))
696 ae
= (i
>= element_index(lastref
)) ? lastref
: element_forw(a
->head
);
698 ae
= element_forw(a
->head
);
699 for ( ; ae
!= a
->head
; ae
= element_forw(ae
))
700 if (element_index(ae
) == i
) {
702 return(element_value(ae
));
705 return((char *) NULL
);
708 /* Convenience routines for the shell to translate to and from the form used
709 by the rest of the code. */
712 array_to_word_list(a
)
718 if (a
== 0 || array_empty(a
))
719 return((WORD_LIST
*)NULL
);
720 list
= (WORD_LIST
*)NULL
;
721 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
))
722 list
= make_word_list (make_bare_word(element_value(ae
)), list
);
723 return (REVERSE_LIST(list
, WORD_LIST
*));
727 array_from_word_list (list
)
733 return((ARRAY
*)NULL
);
735 return (array_assign_list (a
, list
));
739 array_keys_to_word_list(a
)
746 if (a
== 0 || array_empty(a
))
747 return((WORD_LIST
*)NULL
);
748 list
= (WORD_LIST
*)NULL
;
749 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
)) {
750 t
= itos(element_index(ae
));
751 list
= make_word_list (make_bare_word(t
), list
);
754 return (REVERSE_LIST(list
, WORD_LIST
*));
758 array_assign_list (array
, list
)
762 register WORD_LIST
*l
;
763 register arrayind_t i
;
765 for (l
= list
, i
= 0; l
; l
= l
->next
, i
++)
766 array_insert(array
, i
, l
->word
->word
);
778 if (a
== 0 || array_empty(a
))
779 return ((char **)NULL
);
780 ret
= strvec_create (array_num_elements (a
) + 1);
782 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
)) {
783 t
= element_value (ae
);
784 ret
[i
++] = t
? savestring (t
) : (char *)NULL
;
786 ret
[i
] = (char *)NULL
;
791 * Return a string that is the concatenation of the elements in A from START
792 * to END, separated by SEP.
795 array_to_string_internal (start
, end
, sep
, quoted
)
796 ARRAY_ELEMENT
*start
, *end
;
802 int slen
, rsize
, rlen
, reg
;
804 if (start
== end
) /* XXX - should not happen */
805 return ((char *)NULL
);
809 for (rsize
= rlen
= 0, ae
= start
; ae
!= end
; ae
= element_forw(ae
)) {
811 result
= (char *)xmalloc (rsize
= 64);
812 if (element_value(ae
)) {
813 t
= quoted
? quote_string(element_value(ae
)) : element_value(ae
);
815 RESIZE_MALLOCED_BUFFER (result
, rlen
, (reg
+ slen
+ 2),
817 strcpy(result
+ rlen
, t
);
822 * Add a separator only after non-null elements.
824 if (element_forw(ae
) != end
) {
825 strcpy(result
+ rlen
, sep
);
831 result
[rlen
] = '\0'; /* XXX */
836 array_to_assign (a
, quoted
)
840 char *result
, *valstr
, *is
;
841 char indstr
[INT_STRLEN_BOUND(intmax_t) + 1];
843 int rsize
, rlen
, elen
;
845 if (a
== 0 || array_empty (a
))
846 return((char *)NULL
);
848 result
= (char *)xmalloc (rsize
= 128);
852 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
)) {
853 is
= inttostr (element_index(ae
), indstr
, sizeof(indstr
));
854 valstr
= element_value (ae
) ? sh_double_quote (element_value(ae
))
856 elen
= STRLEN (is
) + 8 + STRLEN (valstr
);
857 RESIZE_MALLOCED_BUFFER (result
, rlen
, (elen
+ 1), rsize
, rsize
);
859 result
[rlen
++] = '[';
860 strcpy (result
+ rlen
, is
);
862 result
[rlen
++] = ']';
863 result
[rlen
++] = '=';
865 strcpy (result
+ rlen
, valstr
);
866 rlen
+= STRLEN (valstr
);
869 if (element_forw(ae
) != a
->head
)
870 result
[rlen
++] = ' ';
874 RESIZE_MALLOCED_BUFFER (result
, rlen
, 1, rsize
, 8);
875 result
[rlen
++] = ')';
878 /* This is not as efficient as it could be... */
879 valstr
= sh_single_quote (result
);
887 array_to_string (a
, sep
, quoted
)
893 return((char *)NULL
);
895 return(savestring(""));
896 return (array_to_string_internal (element_forw(a
->head
), a
->head
, sep
, quoted
));
899 #if defined (INCLUDE_UNUSED) || defined (TEST_ARRAY)
901 * Return an array consisting of elements in S, separated by SEP
904 array_from_string(s
, sep
)
911 return((ARRAY
*)NULL
);
912 w
= list_string (s
, sep
, 0);
914 return((ARRAY
*)NULL
);
915 a
= array_from_word_list (w
);
920 #if defined (TEST_ARRAY)
922 * To make a running version, compile -DTEST_ARRAY and link with:
923 * xmalloc.o syntax.o lib/malloc/libmalloc.a lib/sh/libsh.a
925 int interrupt_immediately
= 0;
935 fatal_error(const char *s
, ...)
937 fprintf(stderr
, "array_test: fatal memory error\n");
942 programming_error(const char *s
, ...)
944 fprintf(stderr
, "array_test: fatal programming error\n");
954 w
= (WORD_DESC
*)xmalloc(sizeof(WORD_DESC
));
955 w
->word
= s
? savestring(s
) : savestring ("");
967 w
= (WORD_LIST
*)xmalloc(sizeof(WORD_LIST
));
982 return (WORD_LIST
*)NULL
;
984 wl
= (WORD_LIST
*)NULL
;
987 wl
= make_word_list (make_bare_word(a
), wl
);
988 a
= strtok((char *)NULL
, t
);
990 return (REVERSE_LIST (wl
, WORD_LIST
*));
997 register GENERIC_LIST
*next
, *prev
;
999 for (prev
= 0; list
; ) {
1009 pat_subst(s
, t
, u
, i
)
1013 return ((char *)NULL
);
1020 return savestring(s
);
1026 char lbuf
[INT_STRLEN_BOUND (intmax_t) + 1];
1028 printf("array[%s] = %s\n",
1029 inttostr (element_index(ae
), lbuf
, sizeof (lbuf
)),
1037 array_walk(a
, print_element
, (void *)NULL
);
1042 ARRAY
*a
, *new_a
, *copy_of_a
;
1043 ARRAY_ELEMENT
*ae
, *aew
;
1047 array_insert(a
, 1, "one");
1048 array_insert(a
, 7, "seven");
1049 array_insert(a
, 4, "four");
1050 array_insert(a
, 1029, "one thousand twenty-nine");
1051 array_insert(a
, 12, "twelve");
1052 array_insert(a
, 42, "forty-two");
1054 s
= array_to_string (a
, " ", 0);
1055 printf("s = %s\n", s
);
1056 copy_of_a
= array_from_string(s
, " ");
1057 printf("copy_of_a:");
1058 print_array(copy_of_a
);
1059 array_dispose(copy_of_a
);
1062 ae
= array_remove(a
, 4);
1063 array_dispose_element(ae
);
1064 ae
= array_remove(a
, 1029);
1065 array_dispose_element(ae
);
1066 array_insert(a
, 16, "sixteen");
1068 s
= array_to_string (a
, " ", 0);
1069 printf("s = %s\n", s
);
1070 copy_of_a
= array_from_string(s
, " ");
1071 printf("copy_of_a:");
1072 print_array(copy_of_a
);
1073 array_dispose(copy_of_a
);
1076 array_insert(a
, 2, "two");
1077 array_insert(a
, 1029, "new one thousand twenty-nine");
1078 array_insert(a
, 0, "zero");
1079 array_insert(a
, 134, "");
1081 s
= array_to_string (a
, ":", 0);
1082 printf("s = %s\n", s
);
1083 copy_of_a
= array_from_string(s
, ":");
1084 printf("copy_of_a:");
1085 print_array(copy_of_a
);
1086 array_dispose(copy_of_a
);
1089 new_a
= array_copy(a
);
1091 s
= array_to_string (new_a
, ":", 0);
1092 printf("s = %s\n", s
);
1093 copy_of_a
= array_from_string(s
, ":");
1095 printf("copy_of_a:");
1096 print_array(copy_of_a
);
1097 array_shift(copy_of_a
, 2, AS_DISPOSE
);
1098 printf("copy_of_a shifted by two:");
1099 print_array(copy_of_a
);
1100 ae
= array_shift(copy_of_a
, 2, 0);
1101 printf("copy_of_a shifted by two:");
1102 print_array(copy_of_a
);
1104 aew
= element_forw(ae
);
1105 array_dispose_element(ae
);
1108 array_rshift(copy_of_a
, 1, (char *)0);
1109 printf("copy_of_a rshift by 1:");
1110 print_array(copy_of_a
);
1111 array_rshift(copy_of_a
, 2, "new element zero");
1112 printf("copy_of_a rshift again by 2 with new element zero:");
1113 print_array(copy_of_a
);
1114 s
= array_to_assign(copy_of_a
, 0);
1115 printf("copy_of_a=%s\n", s
);
1117 ae
= array_shift(copy_of_a
, array_num_elements(copy_of_a
), 0);
1119 aew
= element_forw(ae
);
1120 array_dispose_element(ae
);
1123 array_dispose(copy_of_a
);
1126 array_dispose(new_a
);
1129 #endif /* TEST_ARRAY */
1130 #endif /* ARRAY_VARS */