Patch-ID: bash32-035
[bash.git] / array.c
blob4cfa5116946f39261d2ed9722fab9c4a9f0c9e70
1 /*
2 * array.c - functions to create, destroy, access, and manipulate arrays
3 * of strings.
5 * Arrays are sparse doubly-linked lists. An element's index is stored
6 * with it.
8 * Chet Ramey
9 * chet@ins.cwru.edu
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
19 version.
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
24 for more details.
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. */
30 #include "config.h"
32 #if defined (ARRAY_VARS)
34 #if defined (HAVE_UNISTD_H)
35 # ifdef _MINIX
36 # include <sys/types.h>
37 # endif
38 # include <unistd.h>
39 #endif
41 #include <stdio.h>
42 #include "bashansi.h"
44 #include "shell.h"
45 #include "array.h"
46 #include "builtins/common.h"
48 #define ADD_BEFORE(ae, new) \
49 do { \
50 ae->prev->next = new; \
51 new->prev = ae->prev; \
52 ae->prev = new; \
53 new->next = ae; \
54 } while(0)
56 static char *array_to_string_internal __P((ARRAY_ELEMENT *, ARRAY_ELEMENT *, char *, int));
58 ARRAY *
59 array_create()
61 ARRAY *r;
62 ARRAY_ELEMENT *head;
64 r =(ARRAY *)xmalloc(sizeof(ARRAY));
65 r->type = array_indexed;
66 r->max_index = -1;
67 r->num_elements = 0;
68 head = array_create_element(-1, (char *)NULL); /* dummy head */
69 head->prev = head->next = head;
70 r->head = head;
71 return(r);
74 void
75 array_flush (a)
76 ARRAY *a;
78 register ARRAY_ELEMENT *r, *r1;
80 if (a == 0)
81 return;
82 for (r = element_forw(a->head); r != a->head; ) {
83 r1 = element_forw(r);
84 array_dispose_element(r);
85 r = r1;
87 a->head->next = a->head->prev = a->head;
88 a->max_index = -1;
89 a->num_elements = 0;
92 void
93 array_dispose(a)
94 ARRAY *a;
96 if (a == 0)
97 return;
98 array_flush (a);
99 array_dispose_element(a->head);
100 free(a);
103 ARRAY *
104 array_copy(a)
105 ARRAY *a;
107 ARRAY *a1;
108 ARRAY_ELEMENT *ae, *new;
110 if (a == 0)
111 return((ARRAY *) NULL);
112 a1 = array_create();
113 a1->type = a->type;
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);
120 return(a1);
124 * Make and return a new array composed of the elements in array A from
125 * S to E, inclusive.
127 ARRAY *
128 array_slice(array, s, e)
129 ARRAY *array;
130 ARRAY_ELEMENT *s, *e;
132 ARRAY *a;
133 ARRAY_ELEMENT *p, *n;
134 int i;
135 arrayind_t mi;
137 a = array_create ();
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);
145 a->num_elements = i;
146 a->max_index = mi;
147 return a;
151 * Walk the array, calling FUNC once for each element, with the array
152 * element as the argument.
154 void
155 array_walk(a, func, udata)
156 ARRAY *a;
157 sh_ae_map_func_t *func;
158 void *udata;
160 register ARRAY_ELEMENT *ae;
162 if (a == 0 || array_empty(a))
163 return;
164 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae))
165 if ((*func)(ae, udata) < 0)
166 return;
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
175 * and returns NULL.
177 ARRAY_ELEMENT *
178 array_shift(a, n, flags)
179 ARRAY *a;
180 int n, flags;
182 register ARRAY_ELEMENT *ae, *ret;
183 register int i;
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++)
190 if (ae == a->head) {
191 /* Easy case; shifting out all of the elements */
192 if (flags & AS_DISPOSE) {
193 array_flush (a);
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;
200 a->max_index = -1;
201 a->num_elements = 0;
202 return ret;
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 */
211 ae->prev = a->head;
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 */
217 a->max_index -= n;
219 if (flags & AS_DISPOSE) {
220 for (ae = ret; ae; ) {
221 ret = element_forw(ae);
222 array_dispose_element(ae);
223 ae = ret;
225 return ((ARRAY_ELEMENT *)NULL);
228 return ret;
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
234 * shift.
237 array_rshift (a, n, s)
238 ARRAY *a;
239 int n;
240 char *s;
242 register ARRAY_ELEMENT *ae, *new;
244 if (a == 0 || (array_empty(a) && s == 0))
245 return 0;
246 else if (n <= 0)
247 return (a->num_elements);
249 ae = element_forw(a->head);
250 if (s) {
251 new = array_create_element(0, s);
252 ADD_BEFORE(ae, new);
253 a->num_elements++;
254 if (array_num_elements(a) == 1) /* array was empty */
255 return 1;
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);
269 ARRAY_ELEMENT *
270 array_unshift_element(a)
271 ARRAY *a;
273 return (array_shift (a, 1, 0));
277 array_shift_element(a, v)
278 ARRAY *a;
279 char *v;
281 return (array_rshift (a, 1, v));
284 ARRAY *
285 array_quote(array)
286 ARRAY *array;
288 ARRAY_ELEMENT *a;
289 char *t;
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);
295 FREE(a->value);
296 a->value = t;
298 return array;
301 ARRAY *
302 array_quote_escapes(array)
303 ARRAY *array;
305 ARRAY_ELEMENT *a;
306 char *t;
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);
312 FREE(a->value);
313 a->value = t;
315 return array;
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.
323 char *
324 array_subrange (a, start, nelem, starsub, quoted)
325 ARRAY *a;
326 arrayind_t start, nelem;
327 int starsub, quoted;
329 ARRAY *a2;
330 ARRAY_ELEMENT *h, *p;
331 arrayind_t i;
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
343 * index).
345 for (p = element_forw(p); p != array_head(a) && start > element_index(p); p = element_forw(p))
348 if (p == a->head)
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))
358 array_quote(a2);
359 else
360 array_quote_escapes(a2);
362 if (starsub && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) {
363 ifs = getifs();
364 sep[0] = ifs ? *ifs : '\0';
365 } else
366 sep[0] = ' ';
367 sep[1] = '\0';
369 t = array_to_string (a2, sep, 0);
370 array_dispose(a2);
372 return t;
375 char *
376 array_patsub (a, pat, rep, mflags)
377 ARRAY *a;
378 char *pat, *rep;
379 int mflags;
381 ARRAY *a2;
382 ARRAY_ELEMENT *e;
383 char *t, *ifs, sifs[2];
385 if (a == 0 || array_head(a) == 0 || array_empty(a))
386 return ((char *)NULL);
388 a2 = array_copy(a);
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));
392 e->value = t;
395 if (mflags & MATCH_QUOTED)
396 array_quote(a2);
397 else
398 array_quote_escapes(a2);
399 if (mflags & MATCH_STARSUB) {
400 ifs = getifs();
401 sifs[0] = ifs ? *ifs : '\0';
402 sifs[1] = '\0';
403 t = array_to_string (a2, sifs, 0);
404 } else
405 t = array_to_string (a2, " ", 0);
406 array_dispose (a2);
408 return t;
412 * Allocate and return a new array element with index INDEX and value
413 * VALUE.
415 ARRAY_ELEMENT *
416 array_create_element(indx, value)
417 arrayind_t indx;
418 char *value;
420 ARRAY_ELEMENT *r;
422 r = (ARRAY_ELEMENT *)xmalloc(sizeof(ARRAY_ELEMENT));
423 r->ind = indx;
424 r->value = value ? savestring(value) : (char *)NULL;
425 r->next = r->prev = (ARRAY_ELEMENT *) NULL;
426 return(r);
429 #ifdef INCLUDE_UNUSED
430 ARRAY_ELEMENT *
431 array_copy_element(ae)
432 ARRAY_ELEMENT *ae;
434 return(ae ? array_create_element(element_index(ae), element_value(ae))
435 : (ARRAY_ELEMENT *) NULL);
437 #endif
439 void
440 array_dispose_element(ae)
441 ARRAY_ELEMENT *ae;
443 if (ae) {
444 FREE(ae->value);
445 free(ae);
450 * Add a new element with index I and value V to array A (a[i] = v).
453 array_insert(a, i, v)
454 ARRAY *a;
455 arrayind_t i;
456 char *v;
458 register ARRAY_ELEMENT *new, *ae;
460 if (a == 0)
461 return(-1);
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
467 * sequentially.
469 ADD_BEFORE(a->head, new);
470 a->max_index = i;
471 a->num_elements++;
472 return(0);
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;
485 return(0);
486 } else if (element_index(ae) > i) {
487 ADD_BEFORE(ae, new);
488 a->num_elements++;
489 return(0);
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.
499 ARRAY_ELEMENT *
500 array_remove(a, i)
501 ARRAY *a;
502 arrayind_t i;
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;
512 a->num_elements--;
513 if (i == array_max_index(a))
514 a->max_index = element_index(ae->prev);
515 return(ae);
517 return((ARRAY_ELEMENT *) NULL);
521 * Return the value of a[i].
523 char *
524 array_reference(a, i)
525 ARRAY *a;
526 arrayind_t 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. */
541 WORD_LIST *
542 array_to_word_list(a)
543 ARRAY *a;
545 WORD_LIST *list;
546 ARRAY_ELEMENT *ae;
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 *));
556 ARRAY *
557 array_from_word_list (list)
558 WORD_LIST *list;
560 ARRAY *a;
562 if (list == 0)
563 return((ARRAY *)NULL);
564 a = array_create();
565 return (array_assign_list (a, list));
568 WORD_LIST *
569 array_keys_to_word_list(a)
570 ARRAY *a;
572 WORD_LIST *list;
573 ARRAY_ELEMENT *ae;
574 char *t;
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);
582 free(t);
584 return (REVERSE_LIST(list, WORD_LIST *));
587 ARRAY *
588 array_assign_list (array, list)
589 ARRAY *array;
590 WORD_LIST *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);
597 return array;
600 char **
601 array_to_argv (a)
602 ARRAY *a;
604 char **ret, *t;
605 int i;
606 ARRAY_ELEMENT *ae;
608 if (a == 0 || array_empty(a))
609 return ((char **)NULL);
610 ret = strvec_create (array_num_elements (a) + 1);
611 i = 0;
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;
617 return (ret);
621 * Return a string that is the concatenation of all the elements in A,
622 * separated by SEP.
624 static char *
625 array_to_string_internal (start, end, sep, quoted)
626 ARRAY_ELEMENT *start, *end;
627 char *sep;
628 int quoted;
630 char *result, *t;
631 ARRAY_ELEMENT *ae;
632 int slen, rsize, rlen, reg;
634 if (start == end) /* XXX - should not happen */
635 return ((char *)NULL);
637 slen = strlen(sep);
638 result = NULL;
639 for (rsize = rlen = 0, ae = start; ae != end; ae = element_forw(ae)) {
640 if (rsize == 0)
641 result = (char *)xmalloc (rsize = 64);
642 if (element_value(ae)) {
643 t = quoted ? quote_string(element_value(ae)) : element_value(ae);
644 reg = strlen(t);
645 RESIZE_MALLOCED_BUFFER (result, rlen, (reg + slen + 2),
646 rsize, rsize);
647 strcpy(result + rlen, t);
648 rlen += reg;
649 if (quoted && t)
650 free(t);
652 * Add a separator only after non-null elements.
654 if (element_forw(ae) != end) {
655 strcpy(result + rlen, sep);
656 rlen += slen;
660 if (result)
661 result[rlen] = '\0'; /* XXX */
662 return(result);
665 char *
666 array_to_assign (a, quoted)
667 ARRAY *a;
668 int quoted;
670 char *result, *valstr, *is;
671 char indstr[INT_STRLEN_BOUND(intmax_t) + 1];
672 ARRAY_ELEMENT *ae;
673 int rsize, rlen, elen;
675 if (a == 0 || array_empty (a))
676 return((char *)NULL);
678 result = (char *)xmalloc (rsize = 128);
679 result[0] = '(';
680 rlen = 1;
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))
685 : (char *)NULL;
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);
691 rlen += STRLEN (is);
692 result[rlen++] = ']';
693 result[rlen++] = '=';
694 if (valstr) {
695 strcpy (result + rlen, valstr);
696 rlen += STRLEN (valstr);
699 if (element_forw(ae) != a->head)
700 result[rlen++] = ' ';
702 FREE (valstr);
704 RESIZE_MALLOCED_BUFFER (result, rlen, 1, rsize, 8);
705 result[rlen++] = ')';
706 result[rlen] = '\0';
707 if (quoted) {
708 /* This is not as efficient as it could be... */
709 valstr = sh_single_quote (result);
710 free (result);
711 result = valstr;
713 return(result);
716 char *
717 array_to_string (a, sep, quoted)
718 ARRAY *a;
719 char *sep;
720 int quoted;
722 if (a == 0)
723 return((char *)NULL);
724 if (array_empty(a))
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
733 ARRAY *
734 array_from_string(s, sep)
735 char *s, *sep;
737 ARRAY *a;
738 WORD_LIST *w;
740 if (s == 0)
741 return((ARRAY *)NULL);
742 w = list_string (s, sep, 0);
743 if (w == 0)
744 return((ARRAY *)NULL);
745 a = array_from_word_list (w);
746 return (a);
748 #endif
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;
758 signal_is_trapped(s)
759 int s;
761 return 0;
764 void
765 fatal_error(const char *s, ...)
767 fprintf(stderr, "array_test: fatal memory error\n");
768 abort();
771 void
772 programming_error(const char *s, ...)
774 fprintf(stderr, "array_test: fatal programming error\n");
775 abort();
778 WORD_DESC *
779 make_bare_word (s)
780 const char *s;
782 WORD_DESC *w;
784 w = (WORD_DESC *)xmalloc(sizeof(WORD_DESC));
785 w->word = s ? savestring(s) : savestring ("");
786 w->flags = 0;
787 return w;
790 WORD_LIST *
791 make_word_list(x, l)
792 WORD_DESC *x;
793 WORD_LIST *l;
795 WORD_LIST *w;
797 w = (WORD_LIST *)xmalloc(sizeof(WORD_LIST));
798 w->word = x;
799 w->next = l;
800 return w;
803 WORD_LIST *
804 list_string(s, t, i)
805 char *s, *t;
806 int i;
808 char *r, *a;
809 WORD_LIST *wl;
811 if (s == 0)
812 return (WORD_LIST *)NULL;
813 r = savestring(s);
814 wl = (WORD_LIST *)NULL;
815 a = strtok(r, t);
816 while (a) {
817 wl = make_word_list (make_bare_word(a), wl);
818 a = strtok((char *)NULL, t);
820 return (REVERSE_LIST (wl, WORD_LIST *));
823 GENERIC_LIST *
824 list_reverse (list)
825 GENERIC_LIST *list;
827 register GENERIC_LIST *next, *prev;
829 for (prev = 0; list; ) {
830 next = list->next;
831 list->next = prev;
832 prev = list;
833 list = next;
835 return prev;
838 char *
839 pat_subst(s, t, u, i)
840 char *s, *t, *u;
841 int i;
843 return ((char *)NULL);
846 char *
847 quote_string(s)
848 char *s;
850 return savestring(s);
853 print_element(ae)
854 ARRAY_ELEMENT *ae;
856 char lbuf[INT_STRLEN_BOUND (intmax_t) + 1];
858 printf("array[%s] = %s\n",
859 inttostr (element_index(ae), lbuf, sizeof (lbuf)),
860 element_value(ae));
863 print_array(a)
864 ARRAY *a;
866 printf("\n");
867 array_walk(a, print_element, (void *)NULL);
870 main()
872 ARRAY *a, *new_a, *copy_of_a;
873 ARRAY_ELEMENT *ae, *aew;
874 char *s;
876 a = array_create();
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");
883 print_array(a);
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);
890 printf("\n");
891 free(s);
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");
897 print_array(a);
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);
904 printf("\n");
905 free(s);
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, "");
910 print_array(a);
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);
917 printf("\n");
918 free(s);
919 new_a = array_copy(a);
920 print_array(new_a);
921 s = array_to_string (new_a, ":", 0);
922 printf("s = %s\n", s);
923 copy_of_a = array_from_string(s, ":");
924 free(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);
933 for ( ; ae; ) {
934 aew = element_forw(ae);
935 array_dispose_element(ae);
936 ae = aew;
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);
946 free(s);
947 ae = array_shift(copy_of_a, array_num_elements(copy_of_a), 0);
948 for ( ; ae; ) {
949 aew = element_forw(ae);
950 array_dispose_element(ae);
951 ae = aew;
953 array_dispose(copy_of_a);
954 printf("\n");
955 array_dispose(a);
956 array_dispose(new_a);
959 #endif /* TEST_ARRAY */
960 #endif /* ARRAY_VARS */