]> git.ipfire.org Git - thirdparty/bash.git/blob - array.c
Initial devel branch import from bash-3.0-alpha
[thirdparty/bash.git] / array.c
1 /*
2 * array.c - functions to create, destroy, access, and manipulate arrays
3 * of strings.
4 *
5 * Arrays are sparse doubly-linked lists. An element's index is stored
6 * with it.
7 *
8 * Chet Ramey
9 * chet@ins.cwru.edu
10 */
11
12 /* Copyright (C) 1997-2002 Free Software Foundation, Inc.
13
14 This file is part of GNU Bash, the Bourne Again SHell.
15
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.
20
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.
25
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. */
29
30 #include "config.h"
31
32 #if defined (ARRAY_VARS)
33
34 #if defined (HAVE_UNISTD_H)
35 # ifdef _MINIX
36 # include <sys/types.h>
37 # endif
38 # include <unistd.h>
39 #endif
40
41 #include <stdio.h>
42 #include "bashansi.h"
43
44 #include "shell.h"
45 #include "array.h"
46 #include "builtins/common.h"
47
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)
55
56 static char *array_to_string_internal __P((ARRAY_ELEMENT *, ARRAY_ELEMENT *, char *, int));
57
58 ARRAY *
59 array_create()
60 {
61 ARRAY *r;
62 ARRAY_ELEMENT *head;
63
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);
72 }
73
74 void
75 array_flush (a)
76 ARRAY *a;
77 {
78 register ARRAY_ELEMENT *r, *r1;
79
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;
86 }
87 a->head->next = a->head->prev = a->head;
88 a->max_index = -1;
89 a->num_elements = 0;
90 }
91
92 void
93 array_dispose(a)
94 ARRAY *a;
95 {
96 if (a == 0)
97 return;
98 array_flush (a);
99 array_dispose_element(a->head);
100 free(a);
101 }
102
103 ARRAY *
104 array_copy(a)
105 ARRAY *a;
106 {
107 ARRAY *a1;
108 ARRAY_ELEMENT *ae, *new;
109
110 if (!a)
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);
119 }
120 return(a1);
121 }
122
123 #ifdef INCLUDE_UNUSED
124 /*
125 * Make and return a new array composed of the elements in array A from
126 * S to E, inclusive.
127 */
128 ARRAY *
129 array_slice(array, s, e)
130 ARRAY *array;
131 ARRAY_ELEMENT *s, *e;
132 {
133 ARRAY *a;
134 ARRAY_ELEMENT *p, *n;
135 int i;
136 arrayind_t mi;
137
138 a = array_create ();
139 a->type = array->type;
140
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);
145 }
146 a->num_elements = i;
147 a->max_index = mi;
148 return a;
149 }
150 #endif
151
152 /*
153 * Walk the array, calling FUNC once for each element, with the array
154 * element as the argument.
155 */
156 void
157 array_walk(a, func, udata)
158 ARRAY *a;
159 sh_ae_map_func_t *func;
160 void *udata;
161 {
162 register ARRAY_ELEMENT *ae;
163
164 if (a == 0 || array_empty(a))
165 return;
166 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae))
167 if ((*func)(ae, udata) < 0)
168 return;
169 }
170
171 /*
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
177 * and returns NULL.
178 */
179 ARRAY_ELEMENT *
180 array_shift(a, n, flags)
181 ARRAY *a;
182 int n, flags;
183 {
184 register ARRAY_ELEMENT *ae, *ret;
185 register int i;
186
187 if (a == 0 || array_empty(a) || n <= 0)
188 return ((ARRAY_ELEMENT *)NULL);
189
190 for (i = 0, ret = ae = element_forw(a->head); ae != a->head && i < n; ae = element_forw(ae), i++)
191 ;
192 if (ae == a->head) {
193 /* Easy case; shifting out all of the elements */
194 if (flags & AS_DISPOSE) {
195 array_flush (a);
196 return ((ARRAY_ELEMENT *)NULL);
197 }
198 for (ae = ret; element_forw(ae) != a->head; ae = element_forw(ae))
199 ;
200 element_forw(ae) = (ARRAY_ELEMENT *)NULL;
201 a->head->next = a->head->prev = a->head;
202 a->max_index = -1;
203 a->num_elements = 0;
204 return ret;
205 }
206 /*
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.
209 */
210 ae->prev->next = (ARRAY_ELEMENT *)NULL; /* null-terminate RET */
211
212 a->head->next = ae; /* slice RET out of the array */
213 ae->prev = a->head;
214
215 for ( ; ae != a->head; ae = element_forw(ae))
216 element_index(ae) -= n; /* renumber retained indices */
217
218 a->num_elements -= n; /* modify bookkeeping information */
219 a->max_index -= n;
220
221 if (flags & AS_DISPOSE) {
222 for (ae = ret; ae; ) {
223 ret = element_forw(ae);
224 array_dispose_element(ae);
225 ae = ret;
226 }
227 return ((ARRAY_ELEMENT *)NULL);
228 }
229
230 return ret;
231 }
232
233 /*
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
236 * shift.
237 */
238 int
239 array_rshift (a, n, s)
240 ARRAY *a;
241 int n;
242 char *s;
243 {
244 register ARRAY_ELEMENT *ae, *new;
245
246 if (a == 0)
247 return 0;
248 if (n <= 0)
249 return (a->num_elements);
250
251 ae = element_forw(a->head);
252 if (s) {
253 new = array_create_element(0, s);
254 ADD_BEFORE(ae, new);
255 a->num_elements++;
256 }
257
258 a->max_index += n;
259
260 /*
261 * Renumber all elements in the array except the one we just added.
262 */
263 for ( ; ae != a->head; ae = element_forw(ae))
264 element_index(ae) += n;
265
266 return (a->num_elements);
267 }
268
269 ARRAY_ELEMENT *
270 array_unshift_element(a)
271 ARRAY *a;
272 {
273 return (array_shift (a, 1, 0));
274 }
275
276 int
277 array_shift_element(a, v)
278 ARRAY *a;
279 char *v;
280 {
281 return (array_rshift (a, 1, v));
282 }
283
284 ARRAY *
285 array_quote(array)
286 ARRAY *array;
287 {
288 ARRAY_ELEMENT *a;
289 char *t;
290
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);
295 FREE(a->value);
296 a->value = t;
297 }
298 return array;
299 }
300
301 char *
302 array_subrange (a, start, end, starsub, quoted)
303 ARRAY *a;
304 arrayind_t start, end;
305 int starsub, quoted;
306 {
307 ARRAY_ELEMENT *h, *p;
308 arrayind_t i;
309 char *ifs, sep[2];
310
311 p = array_head (a);
312 if (p == 0 || array_empty (a) || start > array_num_elements (a))
313 return ((char *)NULL);
314
315 for (i = 0, p = element_forw(p); p != a->head && i < start; i++, p = element_forw(p))
316 ;
317 if (p == a->head)
318 return ((char *)NULL);
319 for (h = p; p != a->head && i < end; i++, p = element_forw(p))
320 ;
321
322 if (starsub && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) {
323 ifs = getifs();
324 sep[0] = ifs ? *ifs : '\0';
325 } else
326 sep[0] = ' ';
327 sep[1] = '\0';
328
329 return (array_to_string_internal (h, p, sep, quoted));
330 }
331
332 char *
333 array_patsub (a, pat, rep, mflags)
334 ARRAY *a;
335 char *pat, *rep;
336 int mflags;
337 {
338 ARRAY *a2;
339 ARRAY_ELEMENT *e;
340 char *t, *ifs, sifs[2];
341
342 if (array_head (a) == 0 || array_empty (a))
343 return ((char *)NULL);
344
345 a2 = array_copy (a);
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));
349 e->value = t;
350 }
351
352 if (mflags & MATCH_QUOTED)
353 array_quote (a2);
354 if (mflags & MATCH_STARSUB) {
355 ifs = getifs();
356 sifs[0] = ifs ? *ifs : '\0';
357 sifs[1] = '\0';
358 t = array_to_string (a2, sifs, 0);
359 } else
360 t = array_to_string (a2, " ", 0);
361 array_dispose (a2);
362
363 return t;
364 }
365
366 /*
367 * Allocate and return a new array element with index INDEX and value
368 * VALUE.
369 */
370 ARRAY_ELEMENT *
371 array_create_element(indx, value)
372 arrayind_t indx;
373 char *value;
374 {
375 ARRAY_ELEMENT *r;
376
377 r = (ARRAY_ELEMENT *)xmalloc(sizeof(ARRAY_ELEMENT));
378 r->ind = indx;
379 r->value = value ? savestring(value) : (char *)NULL;
380 r->next = r->prev = (ARRAY_ELEMENT *) NULL;
381 return(r);
382 }
383
384 #ifdef INCLUDE_UNUSED
385 ARRAY_ELEMENT *
386 array_copy_element(ae)
387 ARRAY_ELEMENT *ae;
388 {
389 return(ae ? array_create_element(element_index(ae), element_value(ae))
390 : (ARRAY_ELEMENT *) NULL);
391 }
392 #endif
393
394 void
395 array_dispose_element(ae)
396 ARRAY_ELEMENT *ae;
397 {
398 if (ae) {
399 FREE(ae->value);
400 free(ae);
401 }
402 }
403
404 /*
405 * Add a new element with index I and value V to array A (a[i] = v).
406 */
407 int
408 array_insert(a, i, v)
409 ARRAY *a;
410 arrayind_t i;
411 char *v;
412 {
413 register ARRAY_ELEMENT *new, *ae;
414
415 if (!a)
416 return(-1);
417 new = array_create_element(i, v);
418 if (i > array_max_index(a)) {
419 /*
420 * Hook onto the end. This also works for an empty array.
421 * Fast path for the common case of allocating arrays
422 * sequentially.
423 */
424 ADD_BEFORE(a->head, new);
425 a->max_index = i;
426 a->num_elements++;
427 return(0);
428 }
429 /*
430 * Otherwise we search for the spot to insert it.
431 */
432 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
433 if (element_index(ae) == i) {
434 /*
435 * Replacing an existing element.
436 */
437 array_dispose_element(new);
438 free(element_value(ae));
439 ae->value = savestring(v);
440 return(0);
441 } else if (element_index(ae) > i) {
442 ADD_BEFORE(ae, new);
443 a->num_elements++;
444 return(0);
445 }
446 }
447 return (-1); /* problem */
448 }
449
450 /*
451 * Delete the element with index I from array A and return it so the
452 * caller can dispose of it.
453 */
454 ARRAY_ELEMENT *
455 array_remove(a, i)
456 ARRAY *a;
457 arrayind_t i;
458 {
459 register ARRAY_ELEMENT *ae;
460
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;
467 a->num_elements--;
468 if (i == array_max_index(a))
469 a->max_index = element_index(ae->prev);
470 return(ae);
471 }
472 return((ARRAY_ELEMENT *) NULL);
473 }
474
475 /*
476 * Return the value of a[i].
477 */
478 char *
479 array_reference(a, i)
480 ARRAY *a;
481 arrayind_t i;
482 {
483 register ARRAY_ELEMENT *ae;
484
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);
491 }
492
493 /* Convenience routines for the shell to translate to and from the form used
494 by the rest of the code. */
495
496 WORD_LIST *
497 array_to_word_list(a)
498 ARRAY *a;
499 {
500 WORD_LIST *list;
501 ARRAY_ELEMENT *ae;
502
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 *));
509 }
510
511 ARRAY *
512 array_from_word_list (list)
513 WORD_LIST *list;
514 {
515 ARRAY *a;
516
517 if (list == 0)
518 return((ARRAY *)NULL);
519 a = array_create();
520 return (array_assign_list (a, list));
521 }
522
523 WORD_LIST *
524 array_keys_to_word_list(a)
525 ARRAY *a;
526 {
527 WORD_LIST *list;
528 ARRAY_ELEMENT *ae;
529 char *t;
530
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);
537 free(t);
538 }
539 return (REVERSE_LIST(list, WORD_LIST *));
540 }
541
542 ARRAY *
543 array_assign_list (array, list)
544 ARRAY *array;
545 WORD_LIST *list;
546 {
547 register WORD_LIST *l;
548 register arrayind_t i;
549
550 for (l = list, i = 0; l; l = l->next, i++)
551 array_insert(array, i, l->word->word);
552 return array;
553 }
554
555 char **
556 array_to_argv (a)
557 ARRAY *a;
558 {
559 char **ret, *t;
560 int i;
561 ARRAY_ELEMENT *ae;
562
563 if (a == 0 || array_empty(a))
564 return ((char **)NULL);
565 ret = strvec_create (array_num_elements (a) + 1);
566 i = 0;
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;
570 }
571 ret[i] = (char *)NULL;
572 return (ret);
573 }
574
575 /*
576 * Return a string that is the concatenation of all the elements in A,
577 * separated by SEP.
578 */
579 static char *
580 array_to_string_internal (start, end, sep, quoted)
581 ARRAY_ELEMENT *start, *end;
582 char *sep;
583 int quoted;
584 {
585 char *result, *t;
586 ARRAY_ELEMENT *ae;
587 int slen, rsize, rlen, reg;
588
589 if (start == end) /* XXX - should not happen */
590 return ((char *)NULL);
591
592 slen = strlen(sep);
593 result = NULL;
594 for (rsize = rlen = 0, ae = start; ae != end; ae = element_forw(ae)) {
595 if (rsize == 0)
596 result = (char *)xmalloc (rsize = 64);
597 if (element_value(ae)) {
598 t = quoted ? quote_string(element_value(ae)) : element_value(ae);
599 reg = strlen(t);
600 RESIZE_MALLOCED_BUFFER (result, rlen, (reg + slen + 2),
601 rsize, rsize);
602 strcpy(result + rlen, t);
603 rlen += reg;
604 if (quoted && t)
605 free(t);
606 /*
607 * Add a separator only after non-null elements.
608 */
609 if (element_forw(ae) != end) {
610 strcpy(result + rlen, sep);
611 rlen += slen;
612 }
613 }
614 }
615 if (result)
616 result[rlen] = '\0'; /* XXX */
617 return(result);
618 }
619
620 char *
621 array_to_assign (a, quoted)
622 ARRAY *a;
623 int quoted;
624 {
625 char *result, *valstr, *is;
626 char indstr[INT_STRLEN_BOUND(intmax_t) + 1];
627 ARRAY_ELEMENT *ae;
628 int rsize, rlen, elen;
629
630 if (a == 0 || array_empty (a))
631 return((char *)NULL);
632
633 result = (char *)xmalloc (rsize = 128);
634 result[0] = '(';
635 rlen = 1;
636
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))
640 : (char *)NULL;
641 elen = STRLEN (indstr) + 8 + STRLEN (valstr);
642 RESIZE_MALLOCED_BUFFER (result, rlen, (elen + 1), rsize, rsize);
643
644 result[rlen++] = '[';
645 strcpy (result + rlen, is);
646 rlen += STRLEN (is);
647 result[rlen++] = ']';
648 result[rlen++] = '=';
649 if (valstr) {
650 strcpy (result + rlen, valstr);
651 rlen += STRLEN (valstr);
652 }
653
654 if (element_forw(ae) != a->head)
655 result[rlen++] = ' ';
656
657 FREE (valstr);
658 }
659 RESIZE_MALLOCED_BUFFER (result, rlen, 1, rsize, 8);
660 result[rlen++] = ')';
661 result[rlen] = '\0';
662 if (quoted) {
663 /* This is not as efficient as it could be... */
664 valstr = sh_single_quote (result);
665 free (result);
666 result = valstr;
667 }
668 return(result);
669 }
670
671 char *
672 array_to_string (a, sep, quoted)
673 ARRAY *a;
674 char *sep;
675 int quoted;
676 {
677 if (a == 0)
678 return((char *)NULL);
679 if (array_empty(a))
680 return(savestring(""));
681 return (array_to_string_internal (element_forw(a->head), a->head, sep, quoted));
682 }
683
684 #if defined (INCLUDE_UNUSED) || defined (TEST_ARRAY)
685 /*
686 * Return an array consisting of elements in S, separated by SEP
687 */
688 ARRAY *
689 array_from_string(s, sep)
690 char *s, *sep;
691 {
692 ARRAY *a;
693 WORD_LIST *w;
694
695 if (s == 0)
696 return((ARRAY *)NULL);
697 w = list_string (s, sep, 0);
698 if (w == 0)
699 return((ARRAY *)NULL);
700 a = array_from_word_list (w);
701 return (a);
702 }
703 #endif
704
705 #if defined (TEST_ARRAY)
706 /*
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
709 */
710 int interrupt_immediately = 0;
711
712 int
713 signal_is_trapped(s)
714 int s;
715 {
716 return 0;
717 }
718
719 void
720 fatal_error(const char *s, ...)
721 {
722 fprintf(stderr, "array_test: fatal memory error\n");
723 abort();
724 }
725
726 void
727 programming_error(const char *s, ...)
728 {
729 fprintf(stderr, "array_test: fatal programming error\n");
730 abort();
731 }
732
733 WORD_DESC *
734 make_bare_word (s)
735 const char *s;
736 {
737 WORD_DESC *w;
738
739 w = (WORD_DESC *)xmalloc(sizeof(WORD_DESC));
740 w->word = s ? savestring(s) : savestring ("");
741 w->flags = 0;
742 return w;
743 }
744
745 WORD_LIST *
746 make_word_list(x, l)
747 WORD_DESC *x;
748 WORD_LIST *l;
749 {
750 WORD_LIST *w;
751
752 w = (WORD_LIST *)xmalloc(sizeof(WORD_LIST));
753 w->word = x;
754 w->next = l;
755 return w;
756 }
757
758 WORD_LIST *
759 list_string(s, t, i)
760 char *s, *t;
761 int i;
762 {
763 char *r, *a;
764 WORD_LIST *wl;
765
766 if (s == 0)
767 return (WORD_LIST *)NULL;
768 r = savestring(s);
769 wl = (WORD_LIST *)NULL;
770 a = strtok(r, t);
771 while (a) {
772 wl = make_word_list (make_bare_word(a), wl);
773 a = strtok((char *)NULL, t);
774 }
775 return (REVERSE_LIST (wl, WORD_LIST *));
776 }
777
778 GENERIC_LIST *
779 list_reverse (list)
780 GENERIC_LIST *list;
781 {
782 register GENERIC_LIST *next, *prev;
783
784 for (prev = 0; list; ) {
785 next = list->next;
786 list->next = prev;
787 prev = list;
788 list = next;
789 }
790 return prev;
791 }
792
793 char *
794 pat_subst(s, t, u, i)
795 char *s, *t, *u;
796 int i;
797 {
798 return ((char *)NULL);
799 }
800
801 char *
802 quote_string(s)
803 char *s;
804 {
805 return savestring(s);
806 }
807
808 print_element(ae)
809 ARRAY_ELEMENT *ae;
810 {
811 char lbuf[INT_STRLEN_BOUND (intmax_t) + 1];
812
813 printf("array[%s] = %s\n",
814 inttostr (element_index(ae), lbuf, sizeof (lbuf)),
815 element_value(ae));
816 }
817
818 print_array(a)
819 ARRAY *a;
820 {
821 printf("\n");
822 array_walk(a, print_element, (void *)NULL);
823 }
824
825 main()
826 {
827 ARRAY *a, *new_a, *copy_of_a;
828 ARRAY_ELEMENT *ae, *aew;
829 char *s;
830
831 a = array_create();
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");
838 print_array(a);
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);
845 printf("\n");
846 free(s);
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");
852 print_array(a);
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);
859 printf("\n");
860 free(s);
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, "");
865 print_array(a);
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);
872 printf("\n");
873 free(s);
874 new_a = array_copy(a);
875 print_array(new_a);
876 s = array_to_string (new_a, ":", 0);
877 printf("s = %s\n", s);
878 copy_of_a = array_from_string(s, ":");
879 free(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);
888 for ( ; ae; ) {
889 aew = element_forw(ae);
890 array_dispose_element(ae);
891 ae = aew;
892 }
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);
901 free(s);
902 ae = array_shift(copy_of_a, array_num_elements(copy_of_a), 0);
903 for ( ; ae; ) {
904 aew = element_forw(ae);
905 array_dispose_element(ae);
906 ae = aew;
907 }
908 array_dispose(copy_of_a);
909 printf("\n");
910 array_dispose(a);
911 array_dispose(new_a);
912 }
913
914 #endif /* TEST_ARRAY */
915 #endif /* ARRAY_VARS */