]>
git.ipfire.org Git - thirdparty/bash.git/blob - array.c
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-2002 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;
249 return (a
->num_elements
);
251 ae
= element_forw(a
->head
);
253 new = array_create_element(0, s
);
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 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
== 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_subrange (a
, start
, end
, starsub
, quoted
)
304 arrayind_t start
, end
;
307 ARRAY_ELEMENT
*h
, *p
;
312 if (p
== 0 || array_empty (a
) || start
> array_num_elements (a
))
313 return ((char *)NULL
);
315 for (i
= 0, p
= element_forw(p
); p
!= a
->head
&& i
< start
; i
++, p
= element_forw(p
))
318 return ((char *)NULL
);
319 for (h
= p
; p
!= a
->head
&& i
< end
; i
++, p
= element_forw(p
))
322 if (starsub
&& (quoted
& (Q_DOUBLE_QUOTES
|Q_HERE_DOCUMENT
))) {
324 sep
[0] = ifs
? *ifs
: '\0';
329 return (array_to_string_internal (h
, p
, sep
, quoted
));
333 array_patsub (a
, pat
, rep
, mflags
)
340 char *t
, *ifs
, sifs
[2];
342 if (array_head (a
) == 0 || array_empty (a
))
343 return ((char *)NULL
);
346 for (e
= element_forw(a2
->head
); e
!= a2
->head
; e
= element_forw(e
)) {
347 t
= pat_subst(element_value(e
), pat
, rep
, mflags
);
348 FREE(element_value(e
));
352 if (mflags
& MATCH_QUOTED
)
354 if (mflags
& MATCH_STARSUB
) {
356 sifs
[0] = ifs
? *ifs
: '\0';
358 t
= array_to_string (a2
, sifs
, 0);
360 t
= array_to_string (a2
, " ", 0);
367 * Allocate and return a new array element with index INDEX and value
371 array_create_element(indx
, value
)
377 r
= (ARRAY_ELEMENT
*)xmalloc(sizeof(ARRAY_ELEMENT
));
379 r
->value
= value
? savestring(value
) : (char *)NULL
;
380 r
->next
= r
->prev
= (ARRAY_ELEMENT
*) NULL
;
384 #ifdef INCLUDE_UNUSED
386 array_copy_element(ae
)
389 return(ae
? array_create_element(element_index(ae
), element_value(ae
))
390 : (ARRAY_ELEMENT
*) NULL
);
395 array_dispose_element(ae
)
405 * Add a new element with index I and value V to array A (a[i] = v).
408 array_insert(a
, i
, v
)
413 register ARRAY_ELEMENT
*new, *ae
;
417 new = array_create_element(i
, v
);
418 if (i
> array_max_index(a
)) {
420 * Hook onto the end. This also works for an empty array.
421 * Fast path for the common case of allocating arrays
424 ADD_BEFORE(a
->head
, new);
430 * Otherwise we search for the spot to insert it.
432 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
)) {
433 if (element_index(ae
) == i
) {
435 * Replacing an existing element.
437 array_dispose_element(new);
438 free(element_value(ae
));
439 ae
->value
= savestring(v
);
441 } else if (element_index(ae
) > i
) {
447 return (-1); /* problem */
451 * Delete the element with index I from array A and return it so the
452 * caller can dispose of it.
459 register ARRAY_ELEMENT
*ae
;
461 if (!a
|| array_empty(a
))
462 return((ARRAY_ELEMENT
*) NULL
);
463 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
))
464 if (element_index(ae
) == i
) {
465 ae
->next
->prev
= ae
->prev
;
466 ae
->prev
->next
= ae
->next
;
468 if (i
== array_max_index(a
))
469 a
->max_index
= element_index(ae
->prev
);
472 return((ARRAY_ELEMENT
*) NULL
);
476 * Return the value of a[i].
479 array_reference(a
, i
)
483 register ARRAY_ELEMENT
*ae
;
485 if (a
== 0 || array_empty(a
))
486 return((char *) NULL
);
487 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
))
488 if (element_index(ae
) == i
)
489 return(element_value(ae
));
490 return((char *) NULL
);
493 /* Convenience routines for the shell to translate to and from the form used
494 by the rest of the code. */
497 array_to_word_list(a
)
503 if (a
== 0 || array_empty(a
))
504 return((WORD_LIST
*)NULL
);
505 list
= (WORD_LIST
*)NULL
;
506 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
))
507 list
= make_word_list (make_bare_word(element_value(ae
)), list
);
508 return (REVERSE_LIST(list
, WORD_LIST
*));
512 array_from_word_list (list
)
518 return((ARRAY
*)NULL
);
520 return (array_assign_list (a
, list
));
524 array_keys_to_word_list(a
)
531 if (a
== 0 || array_empty(a
))
532 return((WORD_LIST
*)NULL
);
533 list
= (WORD_LIST
*)NULL
;
534 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
)) {
535 t
= itos(element_index(ae
));
536 list
= make_word_list (make_bare_word(t
), list
);
539 return (REVERSE_LIST(list
, WORD_LIST
*));
543 array_assign_list (array
, list
)
547 register WORD_LIST
*l
;
548 register arrayind_t i
;
550 for (l
= list
, i
= 0; l
; l
= l
->next
, i
++)
551 array_insert(array
, i
, l
->word
->word
);
563 if (a
== 0 || array_empty(a
))
564 return ((char **)NULL
);
565 ret
= strvec_create (array_num_elements (a
) + 1);
567 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
)) {
568 t
= element_value (ae
);
569 ret
[i
++] = t
? savestring (t
) : (char *)NULL
;
571 ret
[i
] = (char *)NULL
;
576 * Return a string that is the concatenation of all the elements in A,
580 array_to_string_internal (start
, end
, sep
, quoted
)
581 ARRAY_ELEMENT
*start
, *end
;
587 int slen
, rsize
, rlen
, reg
;
589 if (start
== end
) /* XXX - should not happen */
590 return ((char *)NULL
);
594 for (rsize
= rlen
= 0, ae
= start
; ae
!= end
; ae
= element_forw(ae
)) {
596 result
= (char *)xmalloc (rsize
= 64);
597 if (element_value(ae
)) {
598 t
= quoted
? quote_string(element_value(ae
)) : element_value(ae
);
600 RESIZE_MALLOCED_BUFFER (result
, rlen
, (reg
+ slen
+ 2),
602 strcpy(result
+ rlen
, t
);
607 * Add a separator only after non-null elements.
609 if (element_forw(ae
) != end
) {
610 strcpy(result
+ rlen
, sep
);
616 result
[rlen
] = '\0'; /* XXX */
621 array_to_assign (a
, quoted
)
625 char *result
, *valstr
, *is
;
626 char indstr
[INT_STRLEN_BOUND(intmax_t) + 1];
628 int rsize
, rlen
, elen
;
630 if (a
== 0 || array_empty (a
))
631 return((char *)NULL
);
633 result
= (char *)xmalloc (rsize
= 128);
637 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
)) {
638 is
= inttostr (element_index(ae
), indstr
, sizeof(indstr
));
639 valstr
= element_value (ae
) ? sh_double_quote (element_value(ae
))
641 elen
= STRLEN (indstr
) + 8 + STRLEN (valstr
);
642 RESIZE_MALLOCED_BUFFER (result
, rlen
, (elen
+ 1), rsize
, rsize
);
644 result
[rlen
++] = '[';
645 strcpy (result
+ rlen
, is
);
647 result
[rlen
++] = ']';
648 result
[rlen
++] = '=';
650 strcpy (result
+ rlen
, valstr
);
651 rlen
+= STRLEN (valstr
);
654 if (element_forw(ae
) != a
->head
)
655 result
[rlen
++] = ' ';
659 RESIZE_MALLOCED_BUFFER (result
, rlen
, 1, rsize
, 8);
660 result
[rlen
++] = ')';
663 /* This is not as efficient as it could be... */
664 valstr
= sh_single_quote (result
);
672 array_to_string (a
, sep
, quoted
)
678 return((char *)NULL
);
680 return(savestring(""));
681 return (array_to_string_internal (element_forw(a
->head
), a
->head
, sep
, quoted
));
684 #if defined (INCLUDE_UNUSED) || defined (TEST_ARRAY)
686 * Return an array consisting of elements in S, separated by SEP
689 array_from_string(s
, sep
)
696 return((ARRAY
*)NULL
);
697 w
= list_string (s
, sep
, 0);
699 return((ARRAY
*)NULL
);
700 a
= array_from_word_list (w
);
705 #if defined (TEST_ARRAY)
707 * To make a running version, compile -DTEST_ARRAY and link with:
708 * xmalloc.o syntax.o lib/malloc/libmalloc.a lib/sh/libsh.a
710 int interrupt_immediately
= 0;
720 fatal_error(const char *s
, ...)
722 fprintf(stderr
, "array_test: fatal memory error\n");
727 programming_error(const char *s
, ...)
729 fprintf(stderr
, "array_test: fatal programming error\n");
739 w
= (WORD_DESC
*)xmalloc(sizeof(WORD_DESC
));
740 w
->word
= s
? savestring(s
) : savestring ("");
752 w
= (WORD_LIST
*)xmalloc(sizeof(WORD_LIST
));
767 return (WORD_LIST
*)NULL
;
769 wl
= (WORD_LIST
*)NULL
;
772 wl
= make_word_list (make_bare_word(a
), wl
);
773 a
= strtok((char *)NULL
, t
);
775 return (REVERSE_LIST (wl
, WORD_LIST
*));
782 register GENERIC_LIST
*next
, *prev
;
784 for (prev
= 0; list
; ) {
794 pat_subst(s
, t
, u
, i
)
798 return ((char *)NULL
);
805 return savestring(s
);
811 char lbuf
[INT_STRLEN_BOUND (intmax_t) + 1];
813 printf("array[%s] = %s\n",
814 inttostr (element_index(ae
), lbuf
, sizeof (lbuf
)),
822 array_walk(a
, print_element
, (void *)NULL
);
827 ARRAY
*a
, *new_a
, *copy_of_a
;
828 ARRAY_ELEMENT
*ae
, *aew
;
832 array_insert(a
, 1, "one");
833 array_insert(a
, 7, "seven");
834 array_insert(a
, 4, "four");
835 array_insert(a
, 1029, "one thousand twenty-nine");
836 array_insert(a
, 12, "twelve");
837 array_insert(a
, 42, "forty-two");
839 s
= array_to_string (a
, " ", 0);
840 printf("s = %s\n", s
);
841 copy_of_a
= array_from_string(s
, " ");
842 printf("copy_of_a:");
843 print_array(copy_of_a
);
844 array_dispose(copy_of_a
);
847 ae
= array_remove(a
, 4);
848 array_dispose_element(ae
);
849 ae
= array_remove(a
, 1029);
850 array_dispose_element(ae
);
851 array_insert(a
, 16, "sixteen");
853 s
= array_to_string (a
, " ", 0);
854 printf("s = %s\n", s
);
855 copy_of_a
= array_from_string(s
, " ");
856 printf("copy_of_a:");
857 print_array(copy_of_a
);
858 array_dispose(copy_of_a
);
861 array_insert(a
, 2, "two");
862 array_insert(a
, 1029, "new one thousand twenty-nine");
863 array_insert(a
, 0, "zero");
864 array_insert(a
, 134, "");
866 s
= array_to_string (a
, ":", 0);
867 printf("s = %s\n", s
);
868 copy_of_a
= array_from_string(s
, ":");
869 printf("copy_of_a:");
870 print_array(copy_of_a
);
871 array_dispose(copy_of_a
);
874 new_a
= array_copy(a
);
876 s
= array_to_string (new_a
, ":", 0);
877 printf("s = %s\n", s
);
878 copy_of_a
= array_from_string(s
, ":");
880 printf("copy_of_a:");
881 print_array(copy_of_a
);
882 array_shift(copy_of_a
, 2, AS_DISPOSE
);
883 printf("copy_of_a shifted by two:");
884 print_array(copy_of_a
);
885 ae
= array_shift(copy_of_a
, 2, 0);
886 printf("copy_of_a shifted by two:");
887 print_array(copy_of_a
);
889 aew
= element_forw(ae
);
890 array_dispose_element(ae
);
893 array_rshift(copy_of_a
, 1, (char *)0);
894 printf("copy_of_a rshift by 1:");
895 print_array(copy_of_a
);
896 array_rshift(copy_of_a
, 2, "new element zero");
897 printf("copy_of_a rshift again by 2 with new element zero:");
898 print_array(copy_of_a
);
899 s
= array_to_assign(copy_of_a
, 0);
900 printf("copy_of_a=%s\n", s
);
902 ae
= array_shift(copy_of_a
, array_num_elements(copy_of_a
), 0);
904 aew
= element_forw(ae
);
905 array_dispose_element(ae
);
908 array_dispose(copy_of_a
);
911 array_dispose(new_a
);
914 #endif /* TEST_ARRAY */
915 #endif /* ARRAY_VARS */