]>
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-2021 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 #define ADD_AFTER(ae, new) \
58 ae->next->prev = new; \
59 new->next = ae->next; \
64 static char *array_to_string_internal
PARAMS((ARRAY_ELEMENT
*, ARRAY_ELEMENT
*, char *, int));
66 static char *spacesep
= " ";
68 #define IS_LASTREF(a) (a->lastref)
70 #define LASTREF_START(a, i) \
71 (IS_LASTREF(a) && i >= element_index(a->lastref)) ? a->lastref \
72 : element_forw(a->head)
74 #define LASTREF(a) (a->lastref ? a->lastref : element_forw(a->head))
76 #define INVALIDATE_LASTREF(a) a->lastref = 0
77 #define SET_LASTREF(a, e) a->lastref = (e)
78 #define UNSET_LASTREF(a) a->lastref = 0;
86 r
= (ARRAY
*)xmalloc(sizeof(ARRAY
));
89 r
->lastref
= (ARRAY_ELEMENT
*)0;
90 head
= array_create_element(-1, (char *)NULL
); /* dummy head */
91 head
->prev
= head
->next
= head
;
100 register ARRAY_ELEMENT
*r
, *r1
;
104 for (r
= element_forw(a
->head
); r
!= a
->head
; ) {
105 r1
= element_forw(r
);
106 array_dispose_element(r
);
109 a
->head
->next
= a
->head
->prev
= a
->head
;
112 INVALIDATE_LASTREF(a
);
122 array_dispose_element(a
->head
);
131 ARRAY_ELEMENT
*ae
, *new;
134 return((ARRAY
*) NULL
);
136 a1
->max_index
= a
->max_index
;
137 a1
->num_elements
= a
->num_elements
;
138 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
)) {
139 new = array_create_element(element_index(ae
), element_value(ae
));
140 ADD_BEFORE(a1
->head
, new);
141 if (ae
== LASTREF(a
))
142 SET_LASTREF(a1
, new);
148 * Make and return a new array composed of the elements in array A from
152 array_slice(array
, s
, e
)
154 ARRAY_ELEMENT
*s
, *e
;
157 ARRAY_ELEMENT
*p
, *n
;
163 for (mi
= 0, p
= s
, i
= 0; p
!= e
; p
= element_forw(p
), i
++) {
164 n
= array_create_element (element_index(p
), element_value(p
));
165 ADD_BEFORE(a
->head
, n
);
166 mi
= element_index(n
);
174 * Walk the array, calling FUNC once for each element, with the array
175 * element as the argument.
178 array_walk(a
, func
, udata
)
180 sh_ae_map_func_t
*func
;
183 register ARRAY_ELEMENT
*ae
;
185 if (a
== 0 || array_empty(a
))
187 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
))
188 if ((*func
)(ae
, udata
) < 0)
193 * Shift the array A N elements to the left. Delete the first N elements
194 * and subtract N from the indices of the remaining elements. If FLAGS
195 * does not include AS_DISPOSE, this returns a singly-linked null-terminated
196 * list of elements so the caller can dispose of the chain. If FLAGS
197 * includes AS_DISPOSE, this function disposes of the shifted-out elements
201 array_shift(a
, n
, flags
)
205 register ARRAY_ELEMENT
*ae
, *ret
;
208 if (a
== 0 || array_empty(a
) || n
<= 0)
209 return ((ARRAY_ELEMENT
*)NULL
);
211 INVALIDATE_LASTREF(a
);
212 for (i
= 0, ret
= ae
= element_forw(a
->head
); ae
!= a
->head
&& i
< n
; ae
= element_forw(ae
), i
++)
215 /* Easy case; shifting out all of the elements */
216 if (flags
& AS_DISPOSE
) {
218 return ((ARRAY_ELEMENT
*)NULL
);
220 for (ae
= ret
; element_forw(ae
) != a
->head
; ae
= element_forw(ae
))
222 element_forw(ae
) = (ARRAY_ELEMENT
*)NULL
;
223 a
->head
->next
= a
->head
->prev
= a
->head
;
229 * ae now points to the list of elements we want to retain.
230 * ret points to the list we want to either destroy or return.
232 ae
->prev
->next
= (ARRAY_ELEMENT
*)NULL
; /* null-terminate RET */
234 a
->head
->next
= ae
; /* slice RET out of the array */
237 for ( ; ae
!= a
->head
; ae
= element_forw(ae
))
238 element_index(ae
) -= n
; /* renumber retained indices */
240 a
->num_elements
-= n
; /* modify bookkeeping information */
241 a
->max_index
= element_index(a
->head
->prev
);
243 if (flags
& AS_DISPOSE
) {
244 for (ae
= ret
; ae
; ) {
245 ret
= element_forw(ae
);
246 array_dispose_element(ae
);
249 return ((ARRAY_ELEMENT
*)NULL
);
256 * Shift array A right N indices. If S is non-null, it becomes the value of
257 * the new element 0. Returns the number of elements in the array after the
261 array_rshift (a
, n
, s
)
266 register ARRAY_ELEMENT
*ae
, *new;
268 if (a
== 0 || (array_empty(a
) && s
== 0))
271 return (a
->num_elements
);
273 ae
= element_forw(a
->head
);
275 new = array_create_element(0, s
);
278 if (array_num_elements(a
) == 1) { /* array was empty */
285 * Renumber all elements in the array except the one we just added.
287 for ( ; ae
!= a
->head
; ae
= element_forw(ae
))
288 element_index(ae
) += n
;
290 a
->max_index
= element_index(a
->head
->prev
);
292 INVALIDATE_LASTREF(a
);
293 return (a
->num_elements
);
297 array_unshift_element(a
)
300 return (array_shift (a
, 1, 0));
304 array_shift_element(a
, v
)
308 return (array_rshift (a
, 1, v
));
318 if (array
== 0 || array_head(array
) == 0 || array_empty(array
))
319 return (ARRAY
*)NULL
;
320 for (a
= element_forw(array
->head
); a
!= array
->head
; a
= element_forw(a
)) {
321 t
= quote_string (a
->value
);
329 array_quote_escapes(array
)
335 if (array
== 0 || array_head(array
) == 0 || array_empty(array
))
336 return (ARRAY
*)NULL
;
337 for (a
= element_forw(array
->head
); a
!= array
->head
; a
= element_forw(a
)) {
338 t
= quote_escapes (a
->value
);
352 if (array
== 0 || array_head(array
) == 0 || array_empty(array
))
353 return (ARRAY
*)NULL
;
354 for (a
= element_forw(array
->head
); a
!= array
->head
; a
= element_forw(a
)) {
355 t
= dequote_string (a
->value
);
363 array_dequote_escapes(array
)
369 if (array
== 0 || array_head(array
) == 0 || array_empty(array
))
370 return (ARRAY
*)NULL
;
371 for (a
= element_forw(array
->head
); a
!= array
->head
; a
= element_forw(a
)) {
372 t
= dequote_escapes (a
->value
);
380 array_remove_quoted_nulls(array
)
385 if (array
== 0 || array_head(array
) == 0 || array_empty(array
))
386 return (ARRAY
*)NULL
;
387 for (a
= element_forw(array
->head
); a
!= array
->head
; a
= element_forw(a
))
388 a
->value
= remove_quoted_nulls (a
->value
);
393 * Return a string whose elements are the members of array A beginning at
394 * index START and spanning NELEM members. Null elements are counted.
395 * Since arrays are sparse, unset array elements are not counted.
398 array_subrange (a
, start
, nelem
, starsub
, quoted
, pflags
)
400 arrayind_t start
, nelem
;
401 int starsub
, quoted
, pflags
;
404 ARRAY_ELEMENT
*h
, *p
;
409 p
= a
? array_head (a
) : 0;
410 if (p
== 0 || array_empty (a
) || start
> array_max_index(a
))
411 return ((char *)NULL
);
414 * Find element with index START. If START corresponds to an unset
415 * element (arrays can be sparse), use the first element whose index
416 * is >= START. If START is < 0, we count START indices back from
417 * the end of A (not elements, even with sparse arrays -- START is an
420 for (p
= element_forw(p
); p
!= array_head(a
) && start
> element_index(p
); p
= element_forw(p
))
424 return ((char *)NULL
);
426 /* Starting at P, take NELEM elements, inclusive. */
427 for (i
= 0, h
= p
; p
!= a
->head
&& i
< nelem
; i
++, p
= element_forw(p
))
430 a2
= array_slice(a
, h
, p
);
432 wl
= array_to_word_list(a2
);
436 t
= string_list_pos_params(starsub
? '*' : '@', wl
, quoted
, pflags
); /* XXX */
443 array_patsub (a
, pat
, rep
, mflags
)
449 int pchar
, qflags
, pflags
;
450 WORD_LIST
*wl
, *save
;
452 if (a
== 0 || array_head(a
) == 0 || array_empty(a
))
453 return ((char *)NULL
);
455 wl
= array_to_word_list(a
);
459 for (save
= wl
; wl
; wl
= wl
->next
) {
460 t
= pat_subst (wl
->word
->word
, pat
, rep
, mflags
);
461 FREE (wl
->word
->word
);
465 pchar
= (mflags
& MATCH_STARSUB
) == MATCH_STARSUB
? '*' : '@';
466 qflags
= (mflags
& MATCH_QUOTED
) == MATCH_QUOTED
? Q_DOUBLE_QUOTES
: 0;
467 pflags
= (mflags
& MATCH_ASSIGNRHS
) ? PF_ASSIGNRHS
: 0;
469 t
= string_list_pos_params (pchar
, save
, qflags
, pflags
);
476 array_modcase (a
, pat
, modop
, mflags
)
483 int pchar
, qflags
, pflags
;
484 WORD_LIST
*wl
, *save
;
486 if (a
== 0 || array_head(a
) == 0 || array_empty(a
))
487 return ((char *)NULL
);
489 wl
= array_to_word_list(a
);
491 return ((char *)NULL
);
493 for (save
= wl
; wl
; wl
= wl
->next
) {
494 t
= sh_modcase(wl
->word
->word
, pat
, modop
);
495 FREE(wl
->word
->word
);
499 pchar
= (mflags
& MATCH_STARSUB
) == MATCH_STARSUB
? '*' : '@';
500 qflags
= (mflags
& MATCH_QUOTED
) == MATCH_QUOTED
? Q_DOUBLE_QUOTES
: 0;
501 pflags
= (mflags
& MATCH_ASSIGNRHS
) ? PF_ASSIGNRHS
: 0;
503 t
= string_list_pos_params (pchar
, save
, qflags
, pflags
);
510 * Allocate and return a new array element with index INDEX and value
514 array_create_element(indx
, value
)
520 r
= (ARRAY_ELEMENT
*)xmalloc(sizeof(ARRAY_ELEMENT
));
522 r
->value
= value
? savestring(value
) : (char *)NULL
;
523 r
->next
= r
->prev
= (ARRAY_ELEMENT
*) NULL
;
527 #ifdef INCLUDE_UNUSED
529 array_copy_element(ae
)
532 return(ae
? array_create_element(element_index(ae
), element_value(ae
))
533 : (ARRAY_ELEMENT
*) NULL
);
538 array_dispose_element(ae
)
548 * Add a new element with index I and value V to array A (a[i] = v).
551 array_insert(a
, i
, v
)
556 register ARRAY_ELEMENT
*new, *ae
, *start
;
562 new = array_create_element(i
, v
);
563 if (i
> array_max_index(a
)) {
565 * Hook onto the end. This also works for an empty array.
566 * Fast path for the common case of allocating arrays
569 ADD_BEFORE(a
->head
, new);
574 } else if (i
< array_first_index(a
)) {
575 /* Hook at the beginning */
576 ADD_AFTER(a
->head
, new);
581 #if OPTIMIZE_SEQUENTIAL_ARRAY_ASSIGNMENT
583 * Otherwise we search for the spot to insert it. The lastref
584 * handle optimizes the case of sequential or almost-sequential
585 * assignments that are not at the end of the array.
588 /* Use same strategy as array_reference to avoid paying large penalty
589 for semi-random assignment pattern. */
590 startind
= element_index(start
);
591 if (i
< startind
/2) {
592 start
= element_forw(a
->head
);
593 startind
= element_index(start
);
595 } else if (i
>= startind
) {
601 start
= element_forw(ae
->head
);
602 startind
= element_index(start
);
605 for (ae
= start
; ae
!= a
->head
; ) {
606 if (element_index(ae
) == i
) {
608 * Replacing an existing element.
610 free(element_value(ae
));
611 /* Just swap in the new value */
612 ae
->value
= new->value
;
614 array_dispose_element(new);
617 } else if (direction
== 1 && element_index(ae
) > i
) {
622 } else if (direction
== -1 && element_index(ae
) < i
) {
628 ae
= direction
== 1 ? element_forw(ae
) : element_back(ae
);
630 array_dispose_element(new);
631 INVALIDATE_LASTREF(a
);
632 return (-1); /* problem */
636 * Delete the element with index I from array A and return it so the
637 * caller can dispose of it.
644 register ARRAY_ELEMENT
*ae
, *start
;
648 if (a
== 0 || array_empty(a
))
649 return((ARRAY_ELEMENT
*) NULL
);
650 if (i
> array_max_index(a
) || i
< array_first_index(a
))
651 return((ARRAY_ELEMENT
*)NULL
); /* Keep roving pointer into array to optimize sequential access */
653 /* Use same strategy as array_reference to avoid paying large penalty
654 for semi-random assignment pattern. */
655 startind
= element_index(start
);
656 if (i
< startind
/2) {
657 start
= element_forw(a
->head
);
658 startind
= element_index(start
);
660 } else if (i
>= startind
) {
665 for (ae
= start
; ae
!= a
->head
; ) {
666 if (element_index(ae
) == i
) {
667 ae
->next
->prev
= ae
->prev
;
668 ae
->prev
->next
= ae
->next
;
670 if (i
== array_max_index(a
))
671 a
->max_index
= element_index(ae
->prev
);
673 INVALIDATE_LASTREF(a
);
675 if (ae
->next
!= a
->head
)
676 SET_LASTREF(a
, ae
->next
);
677 else if (ae
->prev
!= a
->head
)
678 SET_LASTREF(a
, ae
->prev
);
680 INVALIDATE_LASTREF(a
);
684 ae
= (direction
== 1) ? element_forw(ae
) : element_back(ae
);
685 if (direction
== 1 && element_index(ae
) > i
)
687 else if (direction
== -1 && element_index(ae
) < i
)
690 return((ARRAY_ELEMENT
*) NULL
);
694 * Return the value of a[i].
697 array_reference(a
, i
)
701 register ARRAY_ELEMENT
*ae
, *start
;
705 if (a
== 0 || array_empty(a
))
706 return((char *) NULL
);
707 if (i
> array_max_index(a
) || i
< array_first_index(a
))
708 return((char *)NULL
); /* Keep roving pointer into array to optimize sequential access */
709 start
= LASTREF(a
); /* lastref pointer */
710 startind
= element_index(start
);
711 if (i
< startind
/2) { /* XXX - guess */
712 start
= element_forw(a
->head
);
713 startind
= element_index(start
);
715 } else if (i
>= startind
) {
720 for (ae
= start
; ae
!= a
->head
; ) {
721 if (element_index(ae
) == i
) {
723 return(element_value(ae
));
725 ae
= (direction
== 1) ? element_forw(ae
) : element_back(ae
);
726 /* Take advantage of index ordering to short-circuit */
727 /* If we don't find it, set the lastref pointer to the element
728 that's `closest', assuming that the unsuccessful reference
729 will quickly be followed by an assignment. No worse than
730 not changing it from the previous value or resetting it. */
731 if (direction
== 1 && element_index(ae
) > i
) {
732 start
= ae
; /* use for SET_LASTREF below */
734 } else if (direction
== -1 && element_index(ae
) < i
) {
735 start
= ae
; /* use for SET_LASTREF below */
742 SET_LASTREF(a
, start
);
744 return((char *) NULL
);
747 /* Convenience routines for the shell to translate to and from the form used
748 by the rest of the code. */
751 array_to_word_list(a
)
757 if (a
== 0 || array_empty(a
))
758 return((WORD_LIST
*)NULL
);
759 list
= (WORD_LIST
*)NULL
;
760 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
))
761 list
= make_word_list (make_bare_word(element_value(ae
)), list
);
762 return (REVERSE_LIST(list
, WORD_LIST
*));
766 array_from_word_list (list
)
772 return((ARRAY
*)NULL
);
774 return (array_assign_list (a
, list
));
778 array_keys_to_word_list(a
)
785 if (a
== 0 || array_empty(a
))
786 return((WORD_LIST
*)NULL
);
787 list
= (WORD_LIST
*)NULL
;
788 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
)) {
789 t
= itos(element_index(ae
));
790 list
= make_word_list (make_bare_word(t
), list
);
793 return (REVERSE_LIST(list
, WORD_LIST
*));
797 array_to_kvpair_list(a
)
804 if (a
== 0 || array_empty(a
))
805 return((WORD_LIST
*)NULL
);
806 list
= (WORD_LIST
*)NULL
;
807 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
)) {
808 k
= itos(element_index(ae
));
809 v
= element_value(ae
);
810 list
= make_word_list (make_bare_word(k
), list
);
811 list
= make_word_list (make_bare_word(v
), list
);
814 return (REVERSE_LIST(list
, WORD_LIST
*));
818 array_assign_list (array
, list
)
822 register WORD_LIST
*l
;
823 register arrayind_t i
;
825 for (l
= list
, i
= 0; l
; l
= l
->next
, i
++)
826 array_insert(array
, i
, l
->word
->word
);
831 array_to_argv (a
, countp
)
839 if (a
== 0 || array_empty(a
)) {
842 return ((char **)NULL
);
844 ret
= strvec_create (array_num_elements (a
) + 1);
846 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
)) {
847 t
= element_value (ae
);
849 ret
[i
++] = savestring (t
);
851 ret
[i
] = (char *)NULL
;
858 array_from_argv(a
, vec
, count
)
867 if (a
== 0 || array_num_elements (a
) == 0)
869 for (i
= 0; i
< count
; i
++)
870 array_insert (a
, i
, t
);
875 if (array_num_elements (a
) == count
&& count
== 1)
877 ae
= element_forw (a
->head
);
878 t
= vec
[0] ? savestring (vec
[0]) : 0;
879 ARRAY_ELEMENT_REPLACE (ae
, t
);
881 else if (array_num_elements (a
) <= count
)
883 /* modify in array_num_elements members in place, then add */
885 for (i
= 0; i
< array_num_elements (a
); i
++)
887 ae
= element_forw (ae
);
888 t
= vec
[0] ? savestring (vec
[0]) : 0;
889 ARRAY_ELEMENT_REPLACE (ae
, t
);
892 for ( ; i
< count
; i
++)
893 array_insert (a
, i
, vec
[i
]);
897 /* deleting elements. it's faster to rebuild the array. */
899 for (i
= 0; i
< count
; i
++)
900 array_insert (a
, i
, vec
[i
]);
907 * Return a string that is the concatenation of the elements in A from START
908 * to END, separated by SEP.
911 array_to_string_internal (start
, end
, sep
, quoted
)
912 ARRAY_ELEMENT
*start
, *end
;
918 int slen
, rsize
, rlen
, reg
;
920 if (start
== end
) /* XXX - should not happen */
921 return ((char *)NULL
);
925 for (rsize
= rlen
= 0, ae
= start
; ae
!= end
; ae
= element_forw(ae
)) {
927 result
= (char *)xmalloc (rsize
= 64);
928 if (element_value(ae
)) {
929 t
= quoted
? quote_string(element_value(ae
)) : element_value(ae
);
931 RESIZE_MALLOCED_BUFFER (result
, rlen
, (reg
+ slen
+ 2),
933 strcpy(result
+ rlen
, t
);
938 * Add a separator only after non-null elements.
940 if (element_forw(ae
) != end
) {
941 strcpy(result
+ rlen
, sep
);
947 result
[rlen
] = '\0'; /* XXX */
952 array_to_kvpair (a
, quoted
)
956 char *result
, *valstr
, *is
;
957 char indstr
[INT_STRLEN_BOUND(intmax_t) + 1];
959 int rsize
, rlen
, elen
;
961 if (a
== 0 || array_empty (a
))
962 return((char *)NULL
);
964 result
= (char *)xmalloc (rsize
= 128);
965 result
[rlen
= 0] = '\0';
967 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
)) {
968 is
= inttostr (element_index(ae
), indstr
, sizeof(indstr
));
969 valstr
= element_value (ae
) ?
970 (ansic_shouldquote (element_value (ae
)) ?
971 ansic_quote (element_value(ae
), 0, (int *)0) :
972 sh_double_quote (element_value (ae
)))
974 elen
= STRLEN (is
) + 8 + STRLEN (valstr
);
975 RESIZE_MALLOCED_BUFFER (result
, rlen
, (elen
+ 1), rsize
, rsize
);
977 strcpy (result
+ rlen
, is
);
979 result
[rlen
++] = ' ';
981 strcpy (result
+ rlen
, valstr
);
982 rlen
+= STRLEN (valstr
);
984 strcpy (result
+ rlen
, "\"\"");
988 if (element_forw(ae
) != a
->head
)
989 result
[rlen
++] = ' ';
993 RESIZE_MALLOCED_BUFFER (result
, rlen
, 1, rsize
, 8);
997 /* This is not as efficient as it could be... */
998 valstr
= sh_single_quote (result
);
1006 array_to_assign (a
, quoted
)
1010 char *result
, *valstr
, *is
;
1011 char indstr
[INT_STRLEN_BOUND(intmax_t) + 1];
1013 int rsize
, rlen
, elen
;
1015 if (a
== 0 || array_empty (a
))
1016 return((char *)NULL
);
1018 result
= (char *)xmalloc (rsize
= 128);
1022 for (ae
= element_forw(a
->head
); ae
!= a
->head
; ae
= element_forw(ae
)) {
1023 is
= inttostr (element_index(ae
), indstr
, sizeof(indstr
));
1024 valstr
= element_value (ae
) ?
1025 (ansic_shouldquote (element_value (ae
)) ?
1026 ansic_quote (element_value(ae
), 0, (int *)0) :
1027 sh_double_quote (element_value (ae
)))
1029 elen
= STRLEN (is
) + 8 + STRLEN (valstr
);
1030 RESIZE_MALLOCED_BUFFER (result
, rlen
, (elen
+ 1), rsize
, rsize
);
1032 result
[rlen
++] = '[';
1033 strcpy (result
+ rlen
, is
);
1034 rlen
+= STRLEN (is
);
1035 result
[rlen
++] = ']';
1036 result
[rlen
++] = '=';
1038 strcpy (result
+ rlen
, valstr
);
1039 rlen
+= STRLEN (valstr
);
1042 if (element_forw(ae
) != a
->head
)
1043 result
[rlen
++] = ' ';
1047 RESIZE_MALLOCED_BUFFER (result
, rlen
, 1, rsize
, 8);
1048 result
[rlen
++] = ')';
1049 result
[rlen
] = '\0';
1051 /* This is not as efficient as it could be... */
1052 valstr
= sh_single_quote (result
);
1060 array_to_string (a
, sep
, quoted
)
1066 return((char *)NULL
);
1068 return(savestring(""));
1069 return (array_to_string_internal (element_forw(a
->head
), a
->head
, sep
, quoted
));
1072 #if defined (INCLUDE_UNUSED) || defined (TEST_ARRAY)
1074 * Return an array consisting of elements in S, separated by SEP
1077 array_from_string(s
, sep
)
1084 return((ARRAY
*)NULL
);
1085 w
= list_string (s
, sep
, 0);
1087 return((ARRAY
*)NULL
);
1088 a
= array_from_word_list (w
);
1093 #if defined (TEST_ARRAY)
1095 * To make a running version, compile -DTEST_ARRAY and link with:
1096 * xmalloc.o syntax.o lib/malloc/libmalloc.a lib/sh/libsh.a
1098 int interrupt_immediately
= 0;
1101 signal_is_trapped(s
)
1108 fatal_error(const char *s
, ...)
1110 fprintf(stderr
, "array_test: fatal memory error\n");
1115 programming_error(const char *s
, ...)
1117 fprintf(stderr
, "array_test: fatal programming error\n");
1127 w
= (WORD_DESC
*)xmalloc(sizeof(WORD_DESC
));
1128 w
->word
= s
? savestring(s
) : savestring ("");
1134 make_word_list(x
, l
)
1140 w
= (WORD_LIST
*)xmalloc(sizeof(WORD_LIST
));
1147 list_string(s
, t
, i
)
1155 return (WORD_LIST
*)NULL
;
1157 wl
= (WORD_LIST
*)NULL
;
1160 wl
= make_word_list (make_bare_word(a
), wl
);
1161 a
= strtok((char *)NULL
, t
);
1163 return (REVERSE_LIST (wl
, WORD_LIST
*));
1170 register GENERIC_LIST
*next
, *prev
;
1172 for (prev
= 0; list
; ) {
1182 pat_subst(s
, t
, u
, i
)
1186 return ((char *)NULL
);
1193 return savestring(s
);
1199 char lbuf
[INT_STRLEN_BOUND (intmax_t) + 1];
1201 printf("array[%s] = %s\n",
1202 inttostr (element_index(ae
), lbuf
, sizeof (lbuf
)),
1210 array_walk(a
, print_element
, (void *)NULL
);
1215 ARRAY
*a
, *new_a
, *copy_of_a
;
1216 ARRAY_ELEMENT
*ae
, *aew
;
1220 array_insert(a
, 1, "one");
1221 array_insert(a
, 7, "seven");
1222 array_insert(a
, 4, "four");
1223 array_insert(a
, 1029, "one thousand twenty-nine");
1224 array_insert(a
, 12, "twelve");
1225 array_insert(a
, 42, "forty-two");
1227 s
= array_to_string (a
, " ", 0);
1228 printf("s = %s\n", s
);
1229 copy_of_a
= array_from_string(s
, " ");
1230 printf("copy_of_a:");
1231 print_array(copy_of_a
);
1232 array_dispose(copy_of_a
);
1235 ae
= array_remove(a
, 4);
1236 array_dispose_element(ae
);
1237 ae
= array_remove(a
, 1029);
1238 array_dispose_element(ae
);
1239 array_insert(a
, 16, "sixteen");
1241 s
= array_to_string (a
, " ", 0);
1242 printf("s = %s\n", s
);
1243 copy_of_a
= array_from_string(s
, " ");
1244 printf("copy_of_a:");
1245 print_array(copy_of_a
);
1246 array_dispose(copy_of_a
);
1249 array_insert(a
, 2, "two");
1250 array_insert(a
, 1029, "new one thousand twenty-nine");
1251 array_insert(a
, 0, "zero");
1252 array_insert(a
, 134, "");
1254 s
= array_to_string (a
, ":", 0);
1255 printf("s = %s\n", s
);
1256 copy_of_a
= array_from_string(s
, ":");
1257 printf("copy_of_a:");
1258 print_array(copy_of_a
);
1259 array_dispose(copy_of_a
);
1262 new_a
= array_copy(a
);
1264 s
= array_to_string (new_a
, ":", 0);
1265 printf("s = %s\n", s
);
1266 copy_of_a
= array_from_string(s
, ":");
1268 printf("copy_of_a:");
1269 print_array(copy_of_a
);
1270 array_shift(copy_of_a
, 2, AS_DISPOSE
);
1271 printf("copy_of_a shifted by two:");
1272 print_array(copy_of_a
);
1273 ae
= array_shift(copy_of_a
, 2, 0);
1274 printf("copy_of_a shifted by two:");
1275 print_array(copy_of_a
);
1277 aew
= element_forw(ae
);
1278 array_dispose_element(ae
);
1281 array_rshift(copy_of_a
, 1, (char *)0);
1282 printf("copy_of_a rshift by 1:");
1283 print_array(copy_of_a
);
1284 array_rshift(copy_of_a
, 2, "new element zero");
1285 printf("copy_of_a rshift again by 2 with new element zero:");
1286 print_array(copy_of_a
);
1287 s
= array_to_assign(copy_of_a
, 0);
1288 printf("copy_of_a=%s\n", s
);
1290 ae
= array_shift(copy_of_a
, array_num_elements(copy_of_a
), 0);
1292 aew
= element_forw(ae
);
1293 array_dispose_element(ae
);
1296 array_dispose(copy_of_a
);
1299 array_dispose(new_a
);
1302 #endif /* TEST_ARRAY */
1303 #endif /* ARRAY_VARS */