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));
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 (mi
= 0, 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
);
325 if (array
== 0 || array_head(array
) == 0 || array_empty(array
))
326 return (ARRAY
*)NULL
;
327 for (a
= element_forw(array
->head
); a
!= array
->head
; a
= element_forw(a
)) {
328 t
= dequote_string (a
->value
);
336 array_dequote_escapes(array
)
342 if (array
== 0 || array_head(array
) == 0 || array_empty(array
))
343 return (ARRAY
*)NULL
;
344 for (a
= element_forw(array
->head
); a
!= array
->head
; a
= element_forw(a
)) {
345 t
= dequote_escapes (a
->value
);
353 array_remove_quoted_nulls(array
)
359 if (array
== 0 || array_head(array
) == 0 || array_empty(array
))
360 return (ARRAY
*)NULL
;
361 for (a
= element_forw(array
->head
); a
!= array
->head
; a
= element_forw(a
))
362 a
->value
= remove_quoted_nulls (a
->value
);
367 * Return a string whose elements are the members of array A beginning at
368 * index START and spanning NELEM members. Null elements are counted.
369 * Since arrays are sparse, unset array elements are not counted.
372 array_subrange (a
, start
, nelem
, starsub
, quoted
)
374 arrayind_t start
, nelem
;
378 ARRAY_ELEMENT
*h
, *p
;
380 char *ifs
, *sifs
, *t
;
383 p
= a
? array_head (a
) : 0;
384 if (p
== 0 || array_empty (a
) || start
> array_max_index(a
))
385 return ((char *)NULL
);
388 * Find element with index START. If START corresponds to an unset
389 * element (arrays can be sparse), use the first element whose index
390 * is >= START. If START is < 0, we count START indices back from
391 * the end of A (not elements, even with sparse arrays -- START is an
394 for (p
= element_forw(p
); p
!= array_head(a
) && start
> element_index(p
); p
= element_forw(p
))
398 return ((char *)NULL
);
400 /* Starting at P, take NELEM elements, inclusive. */
401 for (i
= 0, h
= p
; p
!= a
->head
&& i
< nelem
; i
++, p
= element_forw(p
))
404 a2
= array_slice(a
, h
, p
);
406 if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))
409 array_quote_escapes(a2
);
411 if (starsub
&& (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))) {
413 array_remove_quoted_nulls (a2
);
414 sifs
= ifs_firstchar ((int *)NULL
);
415 t
= array_to_string (a2
, sifs
, 0);
417 } else if (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
)) {
419 sifs
= ifs_firstchar (&slen
);
421 if (ifs
== 0 || *ifs
== 0) {
423 sifs
= xrealloc(sifs
, 2);
427 t
= array_to_string (a2
, sifs
, 0);
430 t
= array_to_string (a2
, " ", 0);
437 array_patsub (a
, pat
, rep
, mflags
)
444 char *t
, *sifs
, *ifs
;
447 if (a
== 0 || array_head(a
) == 0 || array_empty(a
))
448 return ((char *)NULL
);
451 for (e
= element_forw(a2
->head
); e
!= a2
->head
; e
= element_forw(e
)) {
452 t
= pat_subst(element_value(e
), pat
, rep
, mflags
);
453 FREE(element_value(e
));
457 if (mflags
& MATCH_QUOTED
)
460 array_quote_escapes(a2
);
462 if (mflags
& MATCH_STARSUB
) {
463 array_remove_quoted_nulls (a2
);
464 sifs
= ifs_firstchar((int *)NULL
);
465 t
= array_to_string (a2
, sifs
, 0);
467 } else if (mflags
& MATCH_QUOTED
) {
469 sifs
= ifs_firstchar (&slen
);
471 if (ifs
== 0 || *ifs
== 0) {
473 sifs
= xrealloc (sifs
, 2);
477 t
= array_to_string (a2
, sifs
, 0);
480 t
= array_to_string (a2
, " ", 0);
487 array_modcase (a
, pat
, modop
, mflags
)
495 char *t
, *sifs
, *ifs
;
498 if (a
== 0 || array_head(a
) == 0 || array_empty(a
))
499 return ((char *)NULL
);
502 for (e
= element_forw(a2
->head
); e
!= a2
->head
; e
= element_forw(e
)) {
503 t
= sh_modcase(element_value(e
), pat
, modop
);
504 FREE(element_value(e
));
508 if (mflags
& MATCH_QUOTED
)
511 array_quote_escapes(a2
);
513 if (mflags
& MATCH_STARSUB
) {
514 array_remove_quoted_nulls (a2
);
515 sifs
= ifs_firstchar((int *)NULL
);
516 t
= array_to_string (a2
, sifs
, 0);
518 } else if (mflags
& MATCH_QUOTED
) {
520 sifs
= ifs_firstchar (&slen
);
522 if (ifs
== 0 || *ifs
== 0) {
524 sifs
= xrealloc (sifs
, 2);
528 t
= array_to_string (a2
, sifs
, 0);
531 t
= array_to_string (a2
, " ", 0);
537 * Allocate and return a new array element with index INDEX and value
541 array_create_element(indx
, value
)
547 r
= (ARRAY_ELEMENT
*)xmalloc(sizeof(ARRAY_ELEMENT
));
549 r
->value
= value
? savestring(value
) : (char *)NULL
;
550 r
->next
= r
->prev
= (ARRAY_ELEMENT
*) NULL
;
554 #ifdef INCLUDE_UNUSED
556 array_copy_element(ae
)
559 return(ae
? array_create_element(element_index(ae
), element_value(ae
))
560 : (ARRAY_ELEMENT
*) NULL
);
565 array_dispose_element(ae
)
575 * Add a new element with index I and value V to array A (a[i] = v).
578 array_insert(a
, i
, v
)
583 register ARRAY_ELEMENT
*new, *ae
;
587 new = array_create_element(i
, v
);
588 if (i
> array_max_index(a
)) {
590 * Hook onto the end. This also works for an empty array.
591 * Fast path for the common case of allocating arrays
594 ADD_BEFORE(a
->head
, new);
600 * Otherwise we search for the spot to insert it.
602 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
)) {
603 if (element_index(ae
) == i
) {
605 * Replacing an existing element.
607 array_dispose_element(new);
608 free(element_value(ae
));
609 ae
->value
= v
? savestring(v
) : (char *)NULL
;
611 } else if (element_index(ae
) > i
) {
617 return (-1); /* problem */
621 * Delete the element with index I from array A and return it so the
622 * caller can dispose of it.
629 register ARRAY_ELEMENT
*ae
;
631 if (a
== 0 || array_empty(a
))
632 return((ARRAY_ELEMENT
*) NULL
);
633 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
))
634 if (element_index(ae
) == i
) {
635 ae
->next
->prev
= ae
->prev
;
636 ae
->prev
->next
= ae
->next
;
638 if (i
== array_max_index(a
))
639 a
->max_index
= element_index(ae
->prev
);
642 return((ARRAY_ELEMENT
*) NULL
);
646 * Return the value of a[i].
649 array_reference(a
, i
)
653 register ARRAY_ELEMENT
*ae
;
655 if (a
== 0 || array_empty(a
))
656 return((char *) NULL
);
657 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
))
658 if (element_index(ae
) == i
)
659 return(element_value(ae
));
660 return((char *) NULL
);
663 /* Convenience routines for the shell to translate to and from the form used
664 by the rest of the code. */
667 array_to_word_list(a
)
673 if (a
== 0 || array_empty(a
))
674 return((WORD_LIST
*)NULL
);
675 list
= (WORD_LIST
*)NULL
;
676 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
))
677 list
= make_word_list (make_bare_word(element_value(ae
)), list
);
678 return (REVERSE_LIST(list
, WORD_LIST
*));
682 array_from_word_list (list
)
688 return((ARRAY
*)NULL
);
690 return (array_assign_list (a
, list
));
694 array_keys_to_word_list(a
)
701 if (a
== 0 || array_empty(a
))
702 return((WORD_LIST
*)NULL
);
703 list
= (WORD_LIST
*)NULL
;
704 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
)) {
705 t
= itos(element_index(ae
));
706 list
= make_word_list (make_bare_word(t
), list
);
709 return (REVERSE_LIST(list
, WORD_LIST
*));
713 array_assign_list (array
, list
)
717 register WORD_LIST
*l
;
718 register arrayind_t i
;
720 for (l
= list
, i
= 0; l
; l
= l
->next
, i
++)
721 array_insert(array
, i
, l
->word
->word
);
733 if (a
== 0 || array_empty(a
))
734 return ((char **)NULL
);
735 ret
= strvec_create (array_num_elements (a
) + 1);
737 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
)) {
738 t
= element_value (ae
);
739 ret
[i
++] = t
? savestring (t
) : (char *)NULL
;
741 ret
[i
] = (char *)NULL
;
746 * Return a string that is the concatenation of the elements in A from START
747 * to END, separated by SEP.
750 array_to_string_internal (start
, end
, sep
, quoted
)
751 ARRAY_ELEMENT
*start
, *end
;
757 int slen
, rsize
, rlen
, reg
;
759 if (start
== end
) /* XXX - should not happen */
760 return ((char *)NULL
);
764 for (rsize
= rlen
= 0, ae
= start
; ae
!= end
; ae
= element_forw(ae
)) {
766 result
= (char *)xmalloc (rsize
= 64);
767 if (element_value(ae
)) {
768 t
= quoted
? quote_string(element_value(ae
)) : element_value(ae
);
770 RESIZE_MALLOCED_BUFFER (result
, rlen
, (reg
+ slen
+ 2),
772 strcpy(result
+ rlen
, t
);
777 * Add a separator only after non-null elements.
779 if (element_forw(ae
) != end
) {
780 strcpy(result
+ rlen
, sep
);
786 result
[rlen
] = '\0'; /* XXX */
791 array_to_assign (a
, quoted
)
795 char *result
, *valstr
, *is
;
796 char indstr
[INT_STRLEN_BOUND(intmax_t) + 1];
798 int rsize
, rlen
, elen
;
800 if (a
== 0 || array_empty (a
))
801 return((char *)NULL
);
803 result
= (char *)xmalloc (rsize
= 128);
807 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
)) {
808 is
= inttostr (element_index(ae
), indstr
, sizeof(indstr
));
809 valstr
= element_value (ae
) ? sh_double_quote (element_value(ae
))
811 elen
= STRLEN (is
) + 8 + STRLEN (valstr
);
812 RESIZE_MALLOCED_BUFFER (result
, rlen
, (elen
+ 1), rsize
, rsize
);
814 result
[rlen
++] = '[';
815 strcpy (result
+ rlen
, is
);
817 result
[rlen
++] = ']';
818 result
[rlen
++] = '=';
820 strcpy (result
+ rlen
, valstr
);
821 rlen
+= STRLEN (valstr
);
824 if (element_forw(ae
) != a
->head
)
825 result
[rlen
++] = ' ';
829 RESIZE_MALLOCED_BUFFER (result
, rlen
, 1, rsize
, 8);
830 result
[rlen
++] = ')';
833 /* This is not as efficient as it could be... */
834 valstr
= sh_single_quote (result
);
842 array_to_string (a
, sep
, quoted
)
848 return((char *)NULL
);
850 return(savestring(""));
851 return (array_to_string_internal (element_forw(a
->head
), a
->head
, sep
, quoted
));
854 #if defined (INCLUDE_UNUSED) || defined (TEST_ARRAY)
856 * Return an array consisting of elements in S, separated by SEP
859 array_from_string(s
, sep
)
866 return((ARRAY
*)NULL
);
867 w
= list_string (s
, sep
, 0);
869 return((ARRAY
*)NULL
);
870 a
= array_from_word_list (w
);
875 #if defined (TEST_ARRAY)
877 * To make a running version, compile -DTEST_ARRAY and link with:
878 * xmalloc.o syntax.o lib/malloc/libmalloc.a lib/sh/libsh.a
880 int interrupt_immediately
= 0;
890 fatal_error(const char *s
, ...)
892 fprintf(stderr
, "array_test: fatal memory error\n");
897 programming_error(const char *s
, ...)
899 fprintf(stderr
, "array_test: fatal programming error\n");
909 w
= (WORD_DESC
*)xmalloc(sizeof(WORD_DESC
));
910 w
->word
= s
? savestring(s
) : savestring ("");
922 w
= (WORD_LIST
*)xmalloc(sizeof(WORD_LIST
));
937 return (WORD_LIST
*)NULL
;
939 wl
= (WORD_LIST
*)NULL
;
942 wl
= make_word_list (make_bare_word(a
), wl
);
943 a
= strtok((char *)NULL
, t
);
945 return (REVERSE_LIST (wl
, WORD_LIST
*));
952 register GENERIC_LIST
*next
, *prev
;
954 for (prev
= 0; list
; ) {
964 pat_subst(s
, t
, u
, i
)
968 return ((char *)NULL
);
975 return savestring(s
);
981 char lbuf
[INT_STRLEN_BOUND (intmax_t) + 1];
983 printf("array[%s] = %s\n",
984 inttostr (element_index(ae
), lbuf
, sizeof (lbuf
)),
992 array_walk(a
, print_element
, (void *)NULL
);
997 ARRAY
*a
, *new_a
, *copy_of_a
;
998 ARRAY_ELEMENT
*ae
, *aew
;
1002 array_insert(a
, 1, "one");
1003 array_insert(a
, 7, "seven");
1004 array_insert(a
, 4, "four");
1005 array_insert(a
, 1029, "one thousand twenty-nine");
1006 array_insert(a
, 12, "twelve");
1007 array_insert(a
, 42, "forty-two");
1009 s
= array_to_string (a
, " ", 0);
1010 printf("s = %s\n", s
);
1011 copy_of_a
= array_from_string(s
, " ");
1012 printf("copy_of_a:");
1013 print_array(copy_of_a
);
1014 array_dispose(copy_of_a
);
1017 ae
= array_remove(a
, 4);
1018 array_dispose_element(ae
);
1019 ae
= array_remove(a
, 1029);
1020 array_dispose_element(ae
);
1021 array_insert(a
, 16, "sixteen");
1023 s
= array_to_string (a
, " ", 0);
1024 printf("s = %s\n", s
);
1025 copy_of_a
= array_from_string(s
, " ");
1026 printf("copy_of_a:");
1027 print_array(copy_of_a
);
1028 array_dispose(copy_of_a
);
1031 array_insert(a
, 2, "two");
1032 array_insert(a
, 1029, "new one thousand twenty-nine");
1033 array_insert(a
, 0, "zero");
1034 array_insert(a
, 134, "");
1036 s
= array_to_string (a
, ":", 0);
1037 printf("s = %s\n", s
);
1038 copy_of_a
= array_from_string(s
, ":");
1039 printf("copy_of_a:");
1040 print_array(copy_of_a
);
1041 array_dispose(copy_of_a
);
1044 new_a
= array_copy(a
);
1046 s
= array_to_string (new_a
, ":", 0);
1047 printf("s = %s\n", s
);
1048 copy_of_a
= array_from_string(s
, ":");
1050 printf("copy_of_a:");
1051 print_array(copy_of_a
);
1052 array_shift(copy_of_a
, 2, AS_DISPOSE
);
1053 printf("copy_of_a shifted by two:");
1054 print_array(copy_of_a
);
1055 ae
= array_shift(copy_of_a
, 2, 0);
1056 printf("copy_of_a shifted by two:");
1057 print_array(copy_of_a
);
1059 aew
= element_forw(ae
);
1060 array_dispose_element(ae
);
1063 array_rshift(copy_of_a
, 1, (char *)0);
1064 printf("copy_of_a rshift by 1:");
1065 print_array(copy_of_a
);
1066 array_rshift(copy_of_a
, 2, "new element zero");
1067 printf("copy_of_a rshift again by 2 with new element zero:");
1068 print_array(copy_of_a
);
1069 s
= array_to_assign(copy_of_a
, 0);
1070 printf("copy_of_a=%s\n", s
);
1072 ae
= array_shift(copy_of_a
, array_num_elements(copy_of_a
), 0);
1074 aew
= element_forw(ae
);
1075 array_dispose_element(ae
);
1078 array_dispose(copy_of_a
);
1081 array_dispose(new_a
);
1084 #endif /* TEST_ARRAY */
1085 #endif /* ARRAY_VARS */