]> git.ipfire.org Git - thirdparty/bash.git/blob - array.c
Bash-4.4 patch 6
[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-2009 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
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.
20
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.
25
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/>.
28 */
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 /* lastref should be moved into the array structure so each array can be
59 optimized separately */
60
61 static ARRAY *lastarray = 0;
62 static ARRAY_ELEMENT *lastref = 0;
63
64 #define IS_LASTREF(a) (lastarray && (a) == lastarray)
65
66 #define LASTREF_START(a, i) \
67 (IS_LASTREF(a) && i >= element_index(lastref)) ? lastref \
68 : element_forw(a->head)
69
70 #define INVALIDATE_LASTREF(a) \
71 do { \
72 if ((a) == lastarray) { \
73 lastarray = 0; \
74 lastref = 0; \
75 } \
76 } while (0)
77
78 #define SET_LASTREF(a, e) \
79 do { \
80 lastarray = (a); \
81 lastref = (e); \
82 } while (0)
83
84 #define UNSET_LASTREF() \
85 do { \
86 lastarray = 0; \
87 lastref = 0; \
88 } while (0)
89
90 ARRAY *
91 array_create()
92 {
93 ARRAY *r;
94 ARRAY_ELEMENT *head;
95
96 r =(ARRAY *)xmalloc(sizeof(ARRAY));
97 r->type = array_indexed;
98 r->max_index = -1;
99 r->num_elements = 0;
100 head = array_create_element(-1, (char *)NULL); /* dummy head */
101 head->prev = head->next = head;
102 r->head = head;
103 return(r);
104 }
105
106 void
107 array_flush (a)
108 ARRAY *a;
109 {
110 register ARRAY_ELEMENT *r, *r1;
111
112 if (a == 0)
113 return;
114 for (r = element_forw(a->head); r != a->head; ) {
115 r1 = element_forw(r);
116 array_dispose_element(r);
117 r = r1;
118 }
119 a->head->next = a->head->prev = a->head;
120 a->max_index = -1;
121 a->num_elements = 0;
122 INVALIDATE_LASTREF(a);
123 }
124
125 void
126 array_dispose(a)
127 ARRAY *a;
128 {
129 if (a == 0)
130 return;
131 array_flush (a);
132 array_dispose_element(a->head);
133 free(a);
134 }
135
136 ARRAY *
137 array_copy(a)
138 ARRAY *a;
139 {
140 ARRAY *a1;
141 ARRAY_ELEMENT *ae, *new;
142
143 if (a == 0)
144 return((ARRAY *) NULL);
145 a1 = array_create();
146 a1->type = a->type;
147 a1->max_index = a->max_index;
148 a1->num_elements = a->num_elements;
149 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
150 new = array_create_element(element_index(ae), element_value(ae));
151 ADD_BEFORE(a1->head, new);
152 }
153 return(a1);
154 }
155
156 /*
157 * Make and return a new array composed of the elements in array A from
158 * S to E, inclusive.
159 */
160 ARRAY *
161 array_slice(array, s, e)
162 ARRAY *array;
163 ARRAY_ELEMENT *s, *e;
164 {
165 ARRAY *a;
166 ARRAY_ELEMENT *p, *n;
167 int i;
168 arrayind_t mi;
169
170 a = array_create ();
171 a->type = array->type;
172
173 for (mi = 0, p = s, i = 0; p != e; p = element_forw(p), i++) {
174 n = array_create_element (element_index(p), element_value(p));
175 ADD_BEFORE(a->head, n);
176 mi = element_index(n);
177 }
178 a->num_elements = i;
179 a->max_index = mi;
180 return a;
181 }
182
183 /*
184 * Walk the array, calling FUNC once for each element, with the array
185 * element as the argument.
186 */
187 void
188 array_walk(a, func, udata)
189 ARRAY *a;
190 sh_ae_map_func_t *func;
191 void *udata;
192 {
193 register ARRAY_ELEMENT *ae;
194
195 if (a == 0 || array_empty(a))
196 return;
197 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae))
198 if ((*func)(ae, udata) < 0)
199 return;
200 }
201
202 /*
203 * Shift the array A N elements to the left. Delete the first N elements
204 * and subtract N from the indices of the remaining elements. If FLAGS
205 * does not include AS_DISPOSE, this returns a singly-linked null-terminated
206 * list of elements so the caller can dispose of the chain. If FLAGS
207 * includes AS_DISPOSE, this function disposes of the shifted-out elements
208 * and returns NULL.
209 */
210 ARRAY_ELEMENT *
211 array_shift(a, n, flags)
212 ARRAY *a;
213 int n, flags;
214 {
215 register ARRAY_ELEMENT *ae, *ret;
216 register int i;
217
218 if (a == 0 || array_empty(a) || n <= 0)
219 return ((ARRAY_ELEMENT *)NULL);
220
221 INVALIDATE_LASTREF(a);
222 for (i = 0, ret = ae = element_forw(a->head); ae != a->head && i < n; ae = element_forw(ae), i++)
223 ;
224 if (ae == a->head) {
225 /* Easy case; shifting out all of the elements */
226 if (flags & AS_DISPOSE) {
227 array_flush (a);
228 return ((ARRAY_ELEMENT *)NULL);
229 }
230 for (ae = ret; element_forw(ae) != a->head; ae = element_forw(ae))
231 ;
232 element_forw(ae) = (ARRAY_ELEMENT *)NULL;
233 a->head->next = a->head->prev = a->head;
234 a->max_index = -1;
235 a->num_elements = 0;
236 return ret;
237 }
238 /*
239 * ae now points to the list of elements we want to retain.
240 * ret points to the list we want to either destroy or return.
241 */
242 ae->prev->next = (ARRAY_ELEMENT *)NULL; /* null-terminate RET */
243
244 a->head->next = ae; /* slice RET out of the array */
245 ae->prev = a->head;
246
247 for ( ; ae != a->head; ae = element_forw(ae))
248 element_index(ae) -= n; /* renumber retained indices */
249
250 a->num_elements -= n; /* modify bookkeeping information */
251 a->max_index = element_index(a->head->prev);
252
253 if (flags & AS_DISPOSE) {
254 for (ae = ret; ae; ) {
255 ret = element_forw(ae);
256 array_dispose_element(ae);
257 ae = ret;
258 }
259 return ((ARRAY_ELEMENT *)NULL);
260 }
261
262 return ret;
263 }
264
265 /*
266 * Shift array A right N indices. If S is non-null, it becomes the value of
267 * the new element 0. Returns the number of elements in the array after the
268 * shift.
269 */
270 int
271 array_rshift (a, n, s)
272 ARRAY *a;
273 int n;
274 char *s;
275 {
276 register ARRAY_ELEMENT *ae, *new;
277
278 if (a == 0 || (array_empty(a) && s == 0))
279 return 0;
280 else if (n <= 0)
281 return (a->num_elements);
282
283 ae = element_forw(a->head);
284 if (s) {
285 new = array_create_element(0, s);
286 ADD_BEFORE(ae, new);
287 a->num_elements++;
288 if (array_num_elements(a) == 1) { /* array was empty */
289 a->max_index = 0;
290 return 1;
291 }
292 }
293
294 /*
295 * Renumber all elements in the array except the one we just added.
296 */
297 for ( ; ae != a->head; ae = element_forw(ae))
298 element_index(ae) += n;
299
300 a->max_index = element_index(a->head->prev);
301
302 INVALIDATE_LASTREF(a);
303 return (a->num_elements);
304 }
305
306 ARRAY_ELEMENT *
307 array_unshift_element(a)
308 ARRAY *a;
309 {
310 return (array_shift (a, 1, 0));
311 }
312
313 int
314 array_shift_element(a, v)
315 ARRAY *a;
316 char *v;
317 {
318 return (array_rshift (a, 1, v));
319 }
320
321 ARRAY *
322 array_quote(array)
323 ARRAY *array;
324 {
325 ARRAY_ELEMENT *a;
326 char *t;
327
328 if (array == 0 || array_head(array) == 0 || array_empty(array))
329 return (ARRAY *)NULL;
330 for (a = element_forw(array->head); a != array->head; a = element_forw(a)) {
331 t = quote_string (a->value);
332 FREE(a->value);
333 a->value = t;
334 }
335 return array;
336 }
337
338 ARRAY *
339 array_quote_escapes(array)
340 ARRAY *array;
341 {
342 ARRAY_ELEMENT *a;
343 char *t;
344
345 if (array == 0 || array_head(array) == 0 || array_empty(array))
346 return (ARRAY *)NULL;
347 for (a = element_forw(array->head); a != array->head; a = element_forw(a)) {
348 t = quote_escapes (a->value);
349 FREE(a->value);
350 a->value = t;
351 }
352 return array;
353 }
354
355 ARRAY *
356 array_dequote(array)
357 ARRAY *array;
358 {
359 ARRAY_ELEMENT *a;
360 char *t;
361
362 if (array == 0 || array_head(array) == 0 || array_empty(array))
363 return (ARRAY *)NULL;
364 for (a = element_forw(array->head); a != array->head; a = element_forw(a)) {
365 t = dequote_string (a->value);
366 FREE(a->value);
367 a->value = t;
368 }
369 return array;
370 }
371
372 ARRAY *
373 array_dequote_escapes(array)
374 ARRAY *array;
375 {
376 ARRAY_ELEMENT *a;
377 char *t;
378
379 if (array == 0 || array_head(array) == 0 || array_empty(array))
380 return (ARRAY *)NULL;
381 for (a = element_forw(array->head); a != array->head; a = element_forw(a)) {
382 t = dequote_escapes (a->value);
383 FREE(a->value);
384 a->value = t;
385 }
386 return array;
387 }
388
389 ARRAY *
390 array_remove_quoted_nulls(array)
391 ARRAY *array;
392 {
393 ARRAY_ELEMENT *a;
394 char *t;
395
396 if (array == 0 || array_head(array) == 0 || array_empty(array))
397 return (ARRAY *)NULL;
398 for (a = element_forw(array->head); a != array->head; a = element_forw(a))
399 a->value = remove_quoted_nulls (a->value);
400 return array;
401 }
402
403 /*
404 * Return a string whose elements are the members of array A beginning at
405 * index START and spanning NELEM members. Null elements are counted.
406 * Since arrays are sparse, unset array elements are not counted.
407 */
408 char *
409 array_subrange (a, start, nelem, starsub, quoted)
410 ARRAY *a;
411 arrayind_t start, nelem;
412 int starsub, quoted;
413 {
414 ARRAY *a2;
415 ARRAY_ELEMENT *h, *p;
416 arrayind_t i;
417 char *ifs, *sifs, *t;
418 int slen;
419
420 p = a ? array_head (a) : 0;
421 if (p == 0 || array_empty (a) || start > array_max_index(a))
422 return ((char *)NULL);
423
424 /*
425 * Find element with index START. If START corresponds to an unset
426 * element (arrays can be sparse), use the first element whose index
427 * is >= START. If START is < 0, we count START indices back from
428 * the end of A (not elements, even with sparse arrays -- START is an
429 * index).
430 */
431 for (p = element_forw(p); p != array_head(a) && start > element_index(p); p = element_forw(p))
432 ;
433
434 if (p == a->head)
435 return ((char *)NULL);
436
437 /* Starting at P, take NELEM elements, inclusive. */
438 for (i = 0, h = p; p != a->head && i < nelem; i++, p = element_forw(p))
439 ;
440
441 a2 = array_slice(a, h, p);
442
443 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
444 array_quote(a2);
445 else
446 array_quote_escapes(a2);
447
448 if (starsub && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) {
449 /* ${array[*]} */
450 array_remove_quoted_nulls (a2);
451 sifs = ifs_firstchar ((int *)NULL);
452 t = array_to_string (a2, sifs, 0);
453 free (sifs);
454 } else if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) {
455 /* ${array[@]} */
456 sifs = ifs_firstchar (&slen);
457 ifs = getifs ();
458 if (ifs == 0 || *ifs == 0) {
459 if (slen < 2)
460 sifs = xrealloc(sifs, 2);
461 sifs[0] = ' ';
462 sifs[1] = '\0';
463 }
464 t = array_to_string (a2, sifs, 0);
465 free (sifs);
466 } else
467 t = array_to_string (a2, " ", 0);
468 array_dispose(a2);
469
470 return t;
471 }
472
473 char *
474 array_patsub (a, pat, rep, mflags)
475 ARRAY *a;
476 char *pat, *rep;
477 int mflags;
478 {
479 ARRAY *a2;
480 ARRAY_ELEMENT *e;
481 char *t, *sifs, *ifs;
482 int slen;
483
484 if (a == 0 || array_head(a) == 0 || array_empty(a))
485 return ((char *)NULL);
486
487 a2 = array_copy(a);
488 for (e = element_forw(a2->head); e != a2->head; e = element_forw(e)) {
489 t = pat_subst(element_value(e), pat, rep, mflags);
490 FREE(element_value(e));
491 e->value = t;
492 }
493
494 if (mflags & MATCH_QUOTED)
495 array_quote(a2);
496 else
497 array_quote_escapes(a2);
498
499 if (mflags & MATCH_STARSUB) {
500 array_remove_quoted_nulls (a2);
501 sifs = ifs_firstchar((int *)NULL);
502 t = array_to_string (a2, sifs, 0);
503 free(sifs);
504 } else if (mflags & MATCH_QUOTED) {
505 /* ${array[@]} */
506 sifs = ifs_firstchar (&slen);
507 ifs = getifs ();
508 if (ifs == 0 || *ifs == 0) {
509 if (slen < 2)
510 sifs = xrealloc (sifs, 2);
511 sifs[0] = ' ';
512 sifs[1] = '\0';
513 }
514 t = array_to_string (a2, sifs, 0);
515 free(sifs);
516 } else
517 t = array_to_string (a2, " ", 0);
518 array_dispose (a2);
519
520 return t;
521 }
522
523 char *
524 array_modcase (a, pat, modop, mflags)
525 ARRAY *a;
526 char *pat;
527 int modop;
528 int mflags;
529 {
530 ARRAY *a2;
531 ARRAY_ELEMENT *e;
532 char *t, *sifs, *ifs;
533 int slen;
534
535 if (a == 0 || array_head(a) == 0 || array_empty(a))
536 return ((char *)NULL);
537
538 a2 = array_copy(a);
539 for (e = element_forw(a2->head); e != a2->head; e = element_forw(e)) {
540 t = sh_modcase(element_value(e), pat, modop);
541 FREE(element_value(e));
542 e->value = t;
543 }
544
545 if (mflags & MATCH_QUOTED)
546 array_quote(a2);
547 else
548 array_quote_escapes(a2);
549
550 if (mflags & MATCH_STARSUB) {
551 array_remove_quoted_nulls (a2);
552 sifs = ifs_firstchar((int *)NULL);
553 t = array_to_string (a2, sifs, 0);
554 free(sifs);
555 } else if (mflags & MATCH_QUOTED) {
556 /* ${array[@]} */
557 sifs = ifs_firstchar (&slen);
558 ifs = getifs ();
559 if (ifs == 0 || *ifs == 0) {
560 if (slen < 2)
561 sifs = xrealloc (sifs, 2);
562 sifs[0] = ' ';
563 sifs[1] = '\0';
564 }
565 t = array_to_string (a2, sifs, 0);
566 free(sifs);
567 } else
568 t = array_to_string (a2, " ", 0);
569 array_dispose (a2);
570
571 return t;
572 }
573 /*
574 * Allocate and return a new array element with index INDEX and value
575 * VALUE.
576 */
577 ARRAY_ELEMENT *
578 array_create_element(indx, value)
579 arrayind_t indx;
580 char *value;
581 {
582 ARRAY_ELEMENT *r;
583
584 r = (ARRAY_ELEMENT *)xmalloc(sizeof(ARRAY_ELEMENT));
585 r->ind = indx;
586 r->value = value ? savestring(value) : (char *)NULL;
587 r->next = r->prev = (ARRAY_ELEMENT *) NULL;
588 return(r);
589 }
590
591 #ifdef INCLUDE_UNUSED
592 ARRAY_ELEMENT *
593 array_copy_element(ae)
594 ARRAY_ELEMENT *ae;
595 {
596 return(ae ? array_create_element(element_index(ae), element_value(ae))
597 : (ARRAY_ELEMENT *) NULL);
598 }
599 #endif
600
601 void
602 array_dispose_element(ae)
603 ARRAY_ELEMENT *ae;
604 {
605 if (ae) {
606 FREE(ae->value);
607 free(ae);
608 }
609 }
610
611 /*
612 * Add a new element with index I and value V to array A (a[i] = v).
613 */
614 int
615 array_insert(a, i, v)
616 ARRAY *a;
617 arrayind_t i;
618 char *v;
619 {
620 register ARRAY_ELEMENT *new, *ae, *start;
621
622 if (a == 0)
623 return(-1);
624 new = array_create_element(i, v);
625 if (i > array_max_index(a)) {
626 /*
627 * Hook onto the end. This also works for an empty array.
628 * Fast path for the common case of allocating arrays
629 * sequentially.
630 */
631 ADD_BEFORE(a->head, new);
632 a->max_index = i;
633 a->num_elements++;
634 SET_LASTREF(a, new);
635 return(0);
636 }
637 #if OPTIMIZE_SEQUENTIAL_ARRAY_ASSIGNMENT
638 /*
639 * Otherwise we search for the spot to insert it. The lastref
640 * handle optimizes the case of sequential or almost-sequential
641 * assignments that are not at the end of the array.
642 */
643 start = LASTREF_START(a, i);
644 #else
645 start = element_forw(ae->head);
646 #endif
647 for (ae = start; ae != a->head; ae = element_forw(ae)) {
648 if (element_index(ae) == i) {
649 /*
650 * Replacing an existing element.
651 */
652 array_dispose_element(new);
653 free(element_value(ae));
654 ae->value = v ? savestring(v) : (char *)NULL;
655 SET_LASTREF(a, ae);
656 return(0);
657 } else if (element_index(ae) > i) {
658 ADD_BEFORE(ae, new);
659 a->num_elements++;
660 SET_LASTREF(a, new);
661 return(0);
662 }
663 }
664 array_dispose_element(new);
665 INVALIDATE_LASTREF(a);
666 return (-1); /* problem */
667 }
668
669 /*
670 * Delete the element with index I from array A and return it so the
671 * caller can dispose of it.
672 */
673 ARRAY_ELEMENT *
674 array_remove(a, i)
675 ARRAY *a;
676 arrayind_t i;
677 {
678 register ARRAY_ELEMENT *ae, *start;
679
680 if (a == 0 || array_empty(a))
681 return((ARRAY_ELEMENT *) NULL);
682 start = LASTREF_START(a, i);
683 for (ae = start; ae != a->head; ae = element_forw(ae))
684 if (element_index(ae) == i) {
685 ae->next->prev = ae->prev;
686 ae->prev->next = ae->next;
687 a->num_elements--;
688 if (i == array_max_index(a))
689 a->max_index = element_index(ae->prev);
690 #if 0
691 INVALIDATE_LASTREF(a);
692 #else
693 if (ae->next != a->head)
694 SET_LASTREF(a, ae->next);
695 else if (ae->prev != a->head)
696 SET_LASTREF(a, ae->prev);
697 else
698 INVALIDATE_LASTREF(a);
699 #endif
700 return(ae);
701 }
702 return((ARRAY_ELEMENT *) NULL);
703 }
704
705 /*
706 * Return the value of a[i].
707 */
708 char *
709 array_reference(a, i)
710 ARRAY *a;
711 arrayind_t i;
712 {
713 register ARRAY_ELEMENT *ae, *start;
714
715 if (a == 0 || array_empty(a))
716 return((char *) NULL);
717 if (i > array_max_index(a))
718 return((char *)NULL); /* Keep roving pointer into array to optimize sequential access */
719 start = LASTREF_START(a, i);
720 for (ae = start; ae != a->head; ae = element_forw(ae))
721 if (element_index(ae) == i) {
722 SET_LASTREF(a, ae);
723 return(element_value(ae));
724 }
725 UNSET_LASTREF(); /* XXX SET_LASTREF(a, start) ? */
726 return((char *) NULL);
727 }
728
729 /* Convenience routines for the shell to translate to and from the form used
730 by the rest of the code. */
731
732 WORD_LIST *
733 array_to_word_list(a)
734 ARRAY *a;
735 {
736 WORD_LIST *list;
737 ARRAY_ELEMENT *ae;
738
739 if (a == 0 || array_empty(a))
740 return((WORD_LIST *)NULL);
741 list = (WORD_LIST *)NULL;
742 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae))
743 list = make_word_list (make_bare_word(element_value(ae)), list);
744 return (REVERSE_LIST(list, WORD_LIST *));
745 }
746
747 ARRAY *
748 array_from_word_list (list)
749 WORD_LIST *list;
750 {
751 ARRAY *a;
752
753 if (list == 0)
754 return((ARRAY *)NULL);
755 a = array_create();
756 return (array_assign_list (a, list));
757 }
758
759 WORD_LIST *
760 array_keys_to_word_list(a)
761 ARRAY *a;
762 {
763 WORD_LIST *list;
764 ARRAY_ELEMENT *ae;
765 char *t;
766
767 if (a == 0 || array_empty(a))
768 return((WORD_LIST *)NULL);
769 list = (WORD_LIST *)NULL;
770 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
771 t = itos(element_index(ae));
772 list = make_word_list (make_bare_word(t), list);
773 free(t);
774 }
775 return (REVERSE_LIST(list, WORD_LIST *));
776 }
777
778 ARRAY *
779 array_assign_list (array, list)
780 ARRAY *array;
781 WORD_LIST *list;
782 {
783 register WORD_LIST *l;
784 register arrayind_t i;
785
786 for (l = list, i = 0; l; l = l->next, i++)
787 array_insert(array, i, l->word->word);
788 return array;
789 }
790
791 char **
792 array_to_argv (a)
793 ARRAY *a;
794 {
795 char **ret, *t;
796 int i;
797 ARRAY_ELEMENT *ae;
798
799 if (a == 0 || array_empty(a))
800 return ((char **)NULL);
801 ret = strvec_create (array_num_elements (a) + 1);
802 i = 0;
803 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
804 t = element_value (ae);
805 ret[i++] = t ? savestring (t) : (char *)NULL;
806 }
807 ret[i] = (char *)NULL;
808 return (ret);
809 }
810
811 /*
812 * Return a string that is the concatenation of the elements in A from START
813 * to END, separated by SEP.
814 */
815 static char *
816 array_to_string_internal (start, end, sep, quoted)
817 ARRAY_ELEMENT *start, *end;
818 char *sep;
819 int quoted;
820 {
821 char *result, *t;
822 ARRAY_ELEMENT *ae;
823 int slen, rsize, rlen, reg;
824
825 if (start == end) /* XXX - should not happen */
826 return ((char *)NULL);
827
828 slen = strlen(sep);
829 result = NULL;
830 for (rsize = rlen = 0, ae = start; ae != end; ae = element_forw(ae)) {
831 if (rsize == 0)
832 result = (char *)xmalloc (rsize = 64);
833 if (element_value(ae)) {
834 t = quoted ? quote_string(element_value(ae)) : element_value(ae);
835 reg = strlen(t);
836 RESIZE_MALLOCED_BUFFER (result, rlen, (reg + slen + 2),
837 rsize, rsize);
838 strcpy(result + rlen, t);
839 rlen += reg;
840 if (quoted)
841 free(t);
842 /*
843 * Add a separator only after non-null elements.
844 */
845 if (element_forw(ae) != end) {
846 strcpy(result + rlen, sep);
847 rlen += slen;
848 }
849 }
850 }
851 if (result)
852 result[rlen] = '\0'; /* XXX */
853 return(result);
854 }
855
856 char *
857 array_to_assign (a, quoted)
858 ARRAY *a;
859 int quoted;
860 {
861 char *result, *valstr, *is;
862 char indstr[INT_STRLEN_BOUND(intmax_t) + 1];
863 ARRAY_ELEMENT *ae;
864 int rsize, rlen, elen;
865
866 if (a == 0 || array_empty (a))
867 return((char *)NULL);
868
869 result = (char *)xmalloc (rsize = 128);
870 result[0] = '(';
871 rlen = 1;
872
873 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
874 is = inttostr (element_index(ae), indstr, sizeof(indstr));
875 valstr = element_value (ae) ?
876 (ansic_shouldquote (element_value (ae)) ?
877 ansic_quote (element_value(ae), 0, (int *)0) :
878 sh_double_quote (element_value (ae)))
879 : (char *)NULL;
880 elen = STRLEN (is) + 8 + STRLEN (valstr);
881 RESIZE_MALLOCED_BUFFER (result, rlen, (elen + 1), rsize, rsize);
882
883 result[rlen++] = '[';
884 strcpy (result + rlen, is);
885 rlen += STRLEN (is);
886 result[rlen++] = ']';
887 result[rlen++] = '=';
888 if (valstr) {
889 strcpy (result + rlen, valstr);
890 rlen += STRLEN (valstr);
891 }
892
893 if (element_forw(ae) != a->head)
894 result[rlen++] = ' ';
895
896 FREE (valstr);
897 }
898 RESIZE_MALLOCED_BUFFER (result, rlen, 1, rsize, 8);
899 result[rlen++] = ')';
900 result[rlen] = '\0';
901 if (quoted) {
902 /* This is not as efficient as it could be... */
903 valstr = sh_single_quote (result);
904 free (result);
905 result = valstr;
906 }
907 return(result);
908 }
909
910 char *
911 array_to_string (a, sep, quoted)
912 ARRAY *a;
913 char *sep;
914 int quoted;
915 {
916 if (a == 0)
917 return((char *)NULL);
918 if (array_empty(a))
919 return(savestring(""));
920 return (array_to_string_internal (element_forw(a->head), a->head, sep, quoted));
921 }
922
923 #if defined (INCLUDE_UNUSED) || defined (TEST_ARRAY)
924 /*
925 * Return an array consisting of elements in S, separated by SEP
926 */
927 ARRAY *
928 array_from_string(s, sep)
929 char *s, *sep;
930 {
931 ARRAY *a;
932 WORD_LIST *w;
933
934 if (s == 0)
935 return((ARRAY *)NULL);
936 w = list_string (s, sep, 0);
937 if (w == 0)
938 return((ARRAY *)NULL);
939 a = array_from_word_list (w);
940 return (a);
941 }
942 #endif
943
944 #if defined (TEST_ARRAY)
945 /*
946 * To make a running version, compile -DTEST_ARRAY and link with:
947 * xmalloc.o syntax.o lib/malloc/libmalloc.a lib/sh/libsh.a
948 */
949 int interrupt_immediately = 0;
950
951 int
952 signal_is_trapped(s)
953 int s;
954 {
955 return 0;
956 }
957
958 void
959 fatal_error(const char *s, ...)
960 {
961 fprintf(stderr, "array_test: fatal memory error\n");
962 abort();
963 }
964
965 void
966 programming_error(const char *s, ...)
967 {
968 fprintf(stderr, "array_test: fatal programming error\n");
969 abort();
970 }
971
972 WORD_DESC *
973 make_bare_word (s)
974 const char *s;
975 {
976 WORD_DESC *w;
977
978 w = (WORD_DESC *)xmalloc(sizeof(WORD_DESC));
979 w->word = s ? savestring(s) : savestring ("");
980 w->flags = 0;
981 return w;
982 }
983
984 WORD_LIST *
985 make_word_list(x, l)
986 WORD_DESC *x;
987 WORD_LIST *l;
988 {
989 WORD_LIST *w;
990
991 w = (WORD_LIST *)xmalloc(sizeof(WORD_LIST));
992 w->word = x;
993 w->next = l;
994 return w;
995 }
996
997 WORD_LIST *
998 list_string(s, t, i)
999 char *s, *t;
1000 int i;
1001 {
1002 char *r, *a;
1003 WORD_LIST *wl;
1004
1005 if (s == 0)
1006 return (WORD_LIST *)NULL;
1007 r = savestring(s);
1008 wl = (WORD_LIST *)NULL;
1009 a = strtok(r, t);
1010 while (a) {
1011 wl = make_word_list (make_bare_word(a), wl);
1012 a = strtok((char *)NULL, t);
1013 }
1014 return (REVERSE_LIST (wl, WORD_LIST *));
1015 }
1016
1017 GENERIC_LIST *
1018 list_reverse (list)
1019 GENERIC_LIST *list;
1020 {
1021 register GENERIC_LIST *next, *prev;
1022
1023 for (prev = 0; list; ) {
1024 next = list->next;
1025 list->next = prev;
1026 prev = list;
1027 list = next;
1028 }
1029 return prev;
1030 }
1031
1032 char *
1033 pat_subst(s, t, u, i)
1034 char *s, *t, *u;
1035 int i;
1036 {
1037 return ((char *)NULL);
1038 }
1039
1040 char *
1041 quote_string(s)
1042 char *s;
1043 {
1044 return savestring(s);
1045 }
1046
1047 print_element(ae)
1048 ARRAY_ELEMENT *ae;
1049 {
1050 char lbuf[INT_STRLEN_BOUND (intmax_t) + 1];
1051
1052 printf("array[%s] = %s\n",
1053 inttostr (element_index(ae), lbuf, sizeof (lbuf)),
1054 element_value(ae));
1055 }
1056
1057 print_array(a)
1058 ARRAY *a;
1059 {
1060 printf("\n");
1061 array_walk(a, print_element, (void *)NULL);
1062 }
1063
1064 main()
1065 {
1066 ARRAY *a, *new_a, *copy_of_a;
1067 ARRAY_ELEMENT *ae, *aew;
1068 char *s;
1069
1070 a = array_create();
1071 array_insert(a, 1, "one");
1072 array_insert(a, 7, "seven");
1073 array_insert(a, 4, "four");
1074 array_insert(a, 1029, "one thousand twenty-nine");
1075 array_insert(a, 12, "twelve");
1076 array_insert(a, 42, "forty-two");
1077 print_array(a);
1078 s = array_to_string (a, " ", 0);
1079 printf("s = %s\n", s);
1080 copy_of_a = array_from_string(s, " ");
1081 printf("copy_of_a:");
1082 print_array(copy_of_a);
1083 array_dispose(copy_of_a);
1084 printf("\n");
1085 free(s);
1086 ae = array_remove(a, 4);
1087 array_dispose_element(ae);
1088 ae = array_remove(a, 1029);
1089 array_dispose_element(ae);
1090 array_insert(a, 16, "sixteen");
1091 print_array(a);
1092 s = array_to_string (a, " ", 0);
1093 printf("s = %s\n", s);
1094 copy_of_a = array_from_string(s, " ");
1095 printf("copy_of_a:");
1096 print_array(copy_of_a);
1097 array_dispose(copy_of_a);
1098 printf("\n");
1099 free(s);
1100 array_insert(a, 2, "two");
1101 array_insert(a, 1029, "new one thousand twenty-nine");
1102 array_insert(a, 0, "zero");
1103 array_insert(a, 134, "");
1104 print_array(a);
1105 s = array_to_string (a, ":", 0);
1106 printf("s = %s\n", s);
1107 copy_of_a = array_from_string(s, ":");
1108 printf("copy_of_a:");
1109 print_array(copy_of_a);
1110 array_dispose(copy_of_a);
1111 printf("\n");
1112 free(s);
1113 new_a = array_copy(a);
1114 print_array(new_a);
1115 s = array_to_string (new_a, ":", 0);
1116 printf("s = %s\n", s);
1117 copy_of_a = array_from_string(s, ":");
1118 free(s);
1119 printf("copy_of_a:");
1120 print_array(copy_of_a);
1121 array_shift(copy_of_a, 2, AS_DISPOSE);
1122 printf("copy_of_a shifted by two:");
1123 print_array(copy_of_a);
1124 ae = array_shift(copy_of_a, 2, 0);
1125 printf("copy_of_a shifted by two:");
1126 print_array(copy_of_a);
1127 for ( ; ae; ) {
1128 aew = element_forw(ae);
1129 array_dispose_element(ae);
1130 ae = aew;
1131 }
1132 array_rshift(copy_of_a, 1, (char *)0);
1133 printf("copy_of_a rshift by 1:");
1134 print_array(copy_of_a);
1135 array_rshift(copy_of_a, 2, "new element zero");
1136 printf("copy_of_a rshift again by 2 with new element zero:");
1137 print_array(copy_of_a);
1138 s = array_to_assign(copy_of_a, 0);
1139 printf("copy_of_a=%s\n", s);
1140 free(s);
1141 ae = array_shift(copy_of_a, array_num_elements(copy_of_a), 0);
1142 for ( ; ae; ) {
1143 aew = element_forw(ae);
1144 array_dispose_element(ae);
1145 ae = aew;
1146 }
1147 array_dispose(copy_of_a);
1148 printf("\n");
1149 array_dispose(a);
1150 array_dispose(new_a);
1151 }
1152
1153 #endif /* TEST_ARRAY */
1154 #endif /* ARRAY_VARS */