]> git.ipfire.org Git - thirdparty/bash.git/blame - array.c
Imported from ../bash-3.2.tar.gz.
[thirdparty/bash.git] / array.c
CommitLineData
ccc6cda3
JA
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 */
bb70624e 11
b80f6443 12/* Copyright (C) 1997-2004 Free Software Foundation, Inc.
bb70624e
JA
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
ccc6cda3
JA
30#include "config.h"
31
32#if defined (ARRAY_VARS)
33
34#if defined (HAVE_UNISTD_H)
cce855bc
JA
35# ifdef _MINIX
36# include <sys/types.h>
37# endif
ccc6cda3
JA
38# include <unistd.h>
39#endif
40
41#include <stdio.h>
d166f048
JA
42#include "bashansi.h"
43
ccc6cda3
JA
44#include "shell.h"
45#include "array.h"
46#include "builtins/common.h"
47
ccc6cda3
JA
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
7117c2d2 56static char *array_to_string_internal __P((ARRAY_ELEMENT *, ARRAY_ELEMENT *, char *, int));
ccc6cda3
JA
57
58ARRAY *
7117c2d2 59array_create()
ccc6cda3
JA
60{
61 ARRAY *r;
62 ARRAY_ELEMENT *head;
63
f73dda09 64 r =(ARRAY *)xmalloc(sizeof(ARRAY));
ccc6cda3 65 r->type = array_indexed;
7117c2d2 66 r->max_index = -1;
ccc6cda3 67 r->num_elements = 0;
7117c2d2 68 head = array_create_element(-1, (char *)NULL); /* dummy head */
ccc6cda3
JA
69 head->prev = head->next = head;
70 r->head = head;
71 return(r);
72}
73
74void
7117c2d2 75array_flush (a)
ccc6cda3
JA
76ARRAY *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);
7117c2d2 84 array_dispose_element(r);
ccc6cda3
JA
85 r = r1;
86 }
87 a->head->next = a->head->prev = a->head;
7117c2d2
JA
88 a->max_index = -1;
89 a->num_elements = 0;
ccc6cda3
JA
90}
91
92void
7117c2d2 93array_dispose(a)
ccc6cda3
JA
94ARRAY *a;
95{
96 if (a == 0)
97 return;
7117c2d2
JA
98 array_flush (a);
99 array_dispose_element(a->head);
ccc6cda3
JA
100 free(a);
101}
102
103ARRAY *
7117c2d2 104array_copy(a)
ccc6cda3
JA
105ARRAY *a;
106{
107 ARRAY *a1;
108 ARRAY_ELEMENT *ae, *new;
109
95732b49 110 if (a == 0)
ccc6cda3 111 return((ARRAY *) NULL);
7117c2d2 112 a1 = array_create();
ccc6cda3
JA
113 a1->type = a->type;
114 a1->max_index = a->max_index;
115 a1->num_elements = a->num_elements;
ccc6cda3 116 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
7117c2d2 117 new = array_create_element(element_index(ae), element_value(ae));
ccc6cda3
JA
118 ADD_BEFORE(a1->head, new);
119 }
120 return(a1);
121}
122
d166f048 123#ifdef INCLUDE_UNUSED
ccc6cda3
JA
124/*
125 * Make and return a new array composed of the elements in array A from
126 * S to E, inclusive.
127 */
128ARRAY *
7117c2d2 129array_slice(array, s, e)
ccc6cda3
JA
130ARRAY *array;
131ARRAY_ELEMENT *s, *e;
132{
133 ARRAY *a;
134 ARRAY_ELEMENT *p, *n;
7117c2d2
JA
135 int i;
136 arrayind_t mi;
ccc6cda3 137
7117c2d2 138 a = array_create ();
ccc6cda3
JA
139 a->type = array->type;
140
141 for (p = s, i = 0; p != e; p = element_forw(p), i++) {
7117c2d2 142 n = array_create_element (element_index(p), element_value(p));
ccc6cda3 143 ADD_BEFORE(a->head, n);
7117c2d2 144 mi = element_index(ae);
ccc6cda3 145 }
7117c2d2
JA
146 a->num_elements = i;
147 a->max_index = mi;
ccc6cda3
JA
148 return a;
149}
d166f048 150#endif
ccc6cda3 151
7117c2d2
JA
152/*
153 * Walk the array, calling FUNC once for each element, with the array
154 * element as the argument.
155 */
156void
b80f6443 157array_walk(a, func, udata)
7117c2d2
JA
158ARRAY *a;
159sh_ae_map_func_t *func;
b80f6443 160void *udata;
7117c2d2
JA
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))
b80f6443 167 if ((*func)(ae, udata) < 0)
7117c2d2
JA
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 */
179ARRAY_ELEMENT *
180array_shift(a, n, flags)
181ARRAY *a;
182int 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 */
238int
239array_rshift (a, n, s)
240ARRAY *a;
241int n;
242char *s;
243{
244 register ARRAY_ELEMENT *ae, *new;
245
95732b49 246 if (a == 0 || (array_empty(a) && s == 0))
7117c2d2 247 return 0;
95732b49 248 else if (n <= 0)
7117c2d2
JA
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++;
95732b49
JA
256 if (array_num_elements(a) == 1) /* array was empty */
257 return 1;
7117c2d2
JA
258 }
259
7117c2d2
JA
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
95732b49
JA
266 a->max_index = element_index(a->head->prev);
267
7117c2d2
JA
268 return (a->num_elements);
269}
270
b80f6443
JA
271ARRAY_ELEMENT *
272array_unshift_element(a)
273ARRAY *a;
274{
275 return (array_shift (a, 1, 0));
276}
277
278int
279array_shift_element(a, v)
280ARRAY *a;
281char *v;
282{
283 return (array_rshift (a, 1, v));
284}
285
7117c2d2
JA
286ARRAY *
287array_quote(array)
288ARRAY *array;
289{
290 ARRAY_ELEMENT *a;
291 char *t;
292
95732b49 293 if (array == 0 || array_head(array) == 0 || array_empty(array))
7117c2d2
JA
294 return (ARRAY *)NULL;
295 for (a = element_forw(array->head); a != array->head; a = element_forw(a)) {
296 t = quote_string (a->value);
297 FREE(a->value);
298 a->value = t;
299 }
300 return array;
301}
302
b80f6443
JA
303/*
304 * Return a string whose elements are the members of array A beginning at
305 * index START and spanning NELEM members. Null elements are counted.
306 * Since arrays are sparse, unset array elements are not counted.
307 */
7117c2d2 308char *
b80f6443 309array_subrange (a, start, nelem, starsub, quoted)
7117c2d2 310ARRAY *a;
b80f6443
JA
311arrayind_t start, nelem;
312int starsub, quoted;
7117c2d2
JA
313{
314 ARRAY_ELEMENT *h, *p;
315 arrayind_t i;
b80f6443 316 char *ifs, sep[2];
7117c2d2 317
95732b49 318 p = a ? array_head (a) : 0;
b80f6443 319 if (p == 0 || array_empty (a) || start > array_max_index(a))
7117c2d2
JA
320 return ((char *)NULL);
321
b80f6443
JA
322 /*
323 * Find element with index START. If START corresponds to an unset
324 * element (arrays can be sparse), use the first element whose index
325 * is >= START. If START is < 0, we count START indices back from
326 * the end of A (not elements, even with sparse arrays -- START is an
327 * index).
328 */
329 for (p = element_forw(p); p != array_head(a) && start > element_index(p); p = element_forw(p))
7117c2d2 330 ;
b80f6443 331
7117c2d2
JA
332 if (p == a->head)
333 return ((char *)NULL);
b80f6443
JA
334
335 /* Starting at P, take NELEM elements, inclusive. */
336 for (i = 0, h = p; p != a->head && i < nelem; i++, p = element_forw(p))
7117c2d2
JA
337 ;
338
b80f6443
JA
339 if (starsub && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) {
340 ifs = getifs();
341 sep[0] = ifs ? *ifs : '\0';
342 } else
343 sep[0] = ' ';
344 sep[1] = '\0';
345
346 return (array_to_string_internal (h, p, sep, quoted));
7117c2d2
JA
347}
348
349char *
350array_patsub (a, pat, rep, mflags)
351ARRAY *a;
352char *pat, *rep;
353int mflags;
354{
355 ARRAY *a2;
356 ARRAY_ELEMENT *e;
b80f6443 357 char *t, *ifs, sifs[2];
7117c2d2 358
95732b49 359 if (a == 0 || array_head(a) == 0 || array_empty(a))
7117c2d2
JA
360 return ((char *)NULL);
361
95732b49 362 a2 = array_copy(a);
7117c2d2
JA
363 for (e = element_forw(a2->head); e != a2->head; e = element_forw(e)) {
364 t = pat_subst(element_value(e), pat, rep, mflags);
365 FREE(element_value(e));
366 e->value = t;
367 }
368
369 if (mflags & MATCH_QUOTED)
370 array_quote (a2);
b80f6443
JA
371 if (mflags & MATCH_STARSUB) {
372 ifs = getifs();
373 sifs[0] = ifs ? *ifs : '\0';
374 sifs[1] = '\0';
375 t = array_to_string (a2, sifs, 0);
376 } else
377 t = array_to_string (a2, " ", 0);
7117c2d2
JA
378 array_dispose (a2);
379
380 return t;
381}
382
383/*
384 * Allocate and return a new array element with index INDEX and value
385 * VALUE.
386 */
387ARRAY_ELEMENT *
388array_create_element(indx, value)
389arrayind_t indx;
390char *value;
391{
392 ARRAY_ELEMENT *r;
393
394 r = (ARRAY_ELEMENT *)xmalloc(sizeof(ARRAY_ELEMENT));
395 r->ind = indx;
396 r->value = value ? savestring(value) : (char *)NULL;
397 r->next = r->prev = (ARRAY_ELEMENT *) NULL;
398 return(r);
399}
400
cce855bc 401#ifdef INCLUDE_UNUSED
ccc6cda3 402ARRAY_ELEMENT *
7117c2d2 403array_copy_element(ae)
ccc6cda3
JA
404ARRAY_ELEMENT *ae;
405{
7117c2d2 406 return(ae ? array_create_element(element_index(ae), element_value(ae))
ccc6cda3
JA
407 : (ARRAY_ELEMENT *) NULL);
408}
cce855bc 409#endif
ccc6cda3 410
7117c2d2
JA
411void
412array_dispose_element(ae)
413ARRAY_ELEMENT *ae;
414{
b80f6443
JA
415 if (ae) {
416 FREE(ae->value);
417 free(ae);
418 }
7117c2d2
JA
419}
420
ccc6cda3
JA
421/*
422 * Add a new element with index I and value V to array A (a[i] = v).
423 */
424int
7117c2d2 425array_insert(a, i, v)
ccc6cda3
JA
426ARRAY *a;
427arrayind_t i;
428char *v;
429{
430 register ARRAY_ELEMENT *new, *ae;
431
95732b49 432 if (a == 0)
ccc6cda3 433 return(-1);
7117c2d2 434 new = array_create_element(i, v);
ccc6cda3
JA
435 if (i > array_max_index(a)) {
436 /*
437 * Hook onto the end. This also works for an empty array.
438 * Fast path for the common case of allocating arrays
439 * sequentially.
440 */
441 ADD_BEFORE(a->head, new);
442 a->max_index = i;
443 a->num_elements++;
444 return(0);
445 }
446 /*
447 * Otherwise we search for the spot to insert it.
448 */
449 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
450 if (element_index(ae) == i) {
451 /*
452 * Replacing an existing element.
453 */
7117c2d2 454 array_dispose_element(new);
ccc6cda3 455 free(element_value(ae));
eb873671 456 ae->value = v ? savestring(v) : (char *)NULL;
ccc6cda3
JA
457 return(0);
458 } else if (element_index(ae) > i) {
459 ADD_BEFORE(ae, new);
460 a->num_elements++;
461 return(0);
462 }
463 }
464 return (-1); /* problem */
465}
466
467/*
468 * Delete the element with index I from array A and return it so the
469 * caller can dispose of it.
470 */
471ARRAY_ELEMENT *
7117c2d2 472array_remove(a, i)
ccc6cda3
JA
473ARRAY *a;
474arrayind_t i;
475{
476 register ARRAY_ELEMENT *ae;
477
95732b49 478 if (a == 0 || array_empty(a))
ccc6cda3
JA
479 return((ARRAY_ELEMENT *) NULL);
480 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae))
481 if (element_index(ae) == i) {
482 ae->next->prev = ae->prev;
483 ae->prev->next = ae->next;
484 a->num_elements--;
485 if (i == array_max_index(a))
486 a->max_index = element_index(ae->prev);
487 return(ae);
488 }
489 return((ARRAY_ELEMENT *) NULL);
490}
491
492/*
493 * Return the value of a[i].
494 */
495char *
496array_reference(a, i)
497ARRAY *a;
498arrayind_t i;
499{
500 register ARRAY_ELEMENT *ae;
501
502 if (a == 0 || array_empty(a))
503 return((char *) NULL);
504 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae))
505 if (element_index(ae) == i)
506 return(element_value(ae));
507 return((char *) NULL);
508}
509
7117c2d2
JA
510/* Convenience routines for the shell to translate to and from the form used
511 by the rest of the code. */
b80f6443 512
7117c2d2
JA
513WORD_LIST *
514array_to_word_list(a)
ccc6cda3 515ARRAY *a;
ccc6cda3 516{
7117c2d2
JA
517 WORD_LIST *list;
518 ARRAY_ELEMENT *ae;
ccc6cda3
JA
519
520 if (a == 0 || array_empty(a))
7117c2d2
JA
521 return((WORD_LIST *)NULL);
522 list = (WORD_LIST *)NULL;
ccc6cda3 523 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae))
7117c2d2
JA
524 list = make_word_list (make_bare_word(element_value(ae)), list);
525 return (REVERSE_LIST(list, WORD_LIST *));
ccc6cda3
JA
526}
527
7117c2d2
JA
528ARRAY *
529array_from_word_list (list)
530WORD_LIST *list;
531{
532 ARRAY *a;
533
534 if (list == 0)
535 return((ARRAY *)NULL);
536 a = array_create();
537 return (array_assign_list (a, list));
538}
539
b80f6443
JA
540WORD_LIST *
541array_keys_to_word_list(a)
542ARRAY *a;
543{
544 WORD_LIST *list;
545 ARRAY_ELEMENT *ae;
546 char *t;
547
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 t = itos(element_index(ae));
553 list = make_word_list (make_bare_word(t), list);
554 free(t);
555 }
556 return (REVERSE_LIST(list, WORD_LIST *));
557}
558
7117c2d2
JA
559ARRAY *
560array_assign_list (array, list)
561ARRAY *array;
562WORD_LIST *list;
563{
564 register WORD_LIST *l;
565 register arrayind_t i;
566
567 for (l = list, i = 0; l; l = l->next, i++)
568 array_insert(array, i, l->word->word);
569 return array;
570}
571
572char **
573array_to_argv (a)
574ARRAY *a;
575{
576 char **ret, *t;
577 int i;
578 ARRAY_ELEMENT *ae;
579
580 if (a == 0 || array_empty(a))
581 return ((char **)NULL);
582 ret = strvec_create (array_num_elements (a) + 1);
583 i = 0;
584 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
585 t = element_value (ae);
586 ret[i++] = t ? savestring (t) : (char *)NULL;
587 }
588 ret[i] = (char *)NULL;
589 return (ret);
590}
591
ccc6cda3
JA
592/*
593 * Return a string that is the concatenation of all the elements in A,
594 * separated by SEP.
595 */
596static char *
597array_to_string_internal (start, end, sep, quoted)
598ARRAY_ELEMENT *start, *end;
599char *sep;
600int quoted;
601{
602 char *result, *t;
603 ARRAY_ELEMENT *ae;
604 int slen, rsize, rlen, reg;
605
606 if (start == end) /* XXX - should not happen */
607 return ((char *)NULL);
608
609 slen = strlen(sep);
f73dda09 610 result = NULL;
ccc6cda3
JA
611 for (rsize = rlen = 0, ae = start; ae != end; ae = element_forw(ae)) {
612 if (rsize == 0)
f73dda09 613 result = (char *)xmalloc (rsize = 64);
ccc6cda3
JA
614 if (element_value(ae)) {
615 t = quoted ? quote_string(element_value(ae)) : element_value(ae);
616 reg = strlen(t);
617 RESIZE_MALLOCED_BUFFER (result, rlen, (reg + slen + 2),
618 rsize, rsize);
619 strcpy(result + rlen, t);
620 rlen += reg;
621 if (quoted && t)
622 free(t);
623 /*
624 * Add a separator only after non-null elements.
625 */
626 if (element_forw(ae) != end) {
627 strcpy(result + rlen, sep);
628 rlen += slen;
629 }
630 }
631 }
f73dda09
JA
632 if (result)
633 result[rlen] = '\0'; /* XXX */
ccc6cda3
JA
634 return(result);
635}
636
637char *
7117c2d2 638array_to_assign (a, quoted)
ccc6cda3 639ARRAY *a;
ccc6cda3
JA
640int quoted;
641{
7117c2d2
JA
642 char *result, *valstr, *is;
643 char indstr[INT_STRLEN_BOUND(intmax_t) + 1];
ccc6cda3
JA
644 ARRAY_ELEMENT *ae;
645 int rsize, rlen, elen;
646
647 if (a == 0 || array_empty (a))
648 return((char *)NULL);
649
f73dda09 650 result = (char *)xmalloc (rsize = 128);
ccc6cda3
JA
651 result[0] = '(';
652 rlen = 1;
653
654 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
7117c2d2 655 is = inttostr (element_index(ae), indstr, sizeof(indstr));
28ef6c31 656 valstr = element_value (ae) ? sh_double_quote (element_value(ae))
ccc6cda3
JA
657 : (char *)NULL;
658 elen = STRLEN (indstr) + 8 + STRLEN (valstr);
659 RESIZE_MALLOCED_BUFFER (result, rlen, (elen + 1), rsize, rsize);
660
661 result[rlen++] = '[';
7117c2d2
JA
662 strcpy (result + rlen, is);
663 rlen += STRLEN (is);
ccc6cda3
JA
664 result[rlen++] = ']';
665 result[rlen++] = '=';
666 if (valstr) {
667 strcpy (result + rlen, valstr);
668 rlen += STRLEN (valstr);
669 }
670
671 if (element_forw(ae) != a->head)
672 result[rlen++] = ' ';
673
ccc6cda3
JA
674 FREE (valstr);
675 }
676 RESIZE_MALLOCED_BUFFER (result, rlen, 1, rsize, 8);
677 result[rlen++] = ')';
678 result[rlen] = '\0';
7117c2d2
JA
679 if (quoted) {
680 /* This is not as efficient as it could be... */
681 valstr = sh_single_quote (result);
682 free (result);
683 result = valstr;
684 }
ccc6cda3
JA
685 return(result);
686}
687
688char *
7117c2d2 689array_to_string (a, sep, quoted)
ccc6cda3 690ARRAY *a;
7117c2d2
JA
691char *sep;
692int quoted;
ccc6cda3 693{
7117c2d2
JA
694 if (a == 0)
695 return((char *)NULL);
696 if (array_empty(a))
697 return(savestring(""));
698 return (array_to_string_internal (element_forw(a->head), a->head, sep, quoted));
ccc6cda3 699}
ccc6cda3 700
d166f048 701#if defined (INCLUDE_UNUSED) || defined (TEST_ARRAY)
ccc6cda3
JA
702/*
703 * Return an array consisting of elements in S, separated by SEP
704 */
705ARRAY *
7117c2d2 706array_from_string(s, sep)
ccc6cda3
JA
707char *s, *sep;
708{
709 ARRAY *a;
710 WORD_LIST *w;
711
712 if (s == 0)
713 return((ARRAY *)NULL);
714 w = list_string (s, sep, 0);
715 if (w == 0)
716 return((ARRAY *)NULL);
7117c2d2 717 a = array_from_word_list (w);
ccc6cda3
JA
718 return (a);
719}
d166f048 720#endif
ccc6cda3 721
7117c2d2
JA
722#if defined (TEST_ARRAY)
723/*
724 * To make a running version, compile -DTEST_ARRAY and link with:
725 * xmalloc.o syntax.o lib/malloc/libmalloc.a lib/sh/libsh.a
726 */
727int interrupt_immediately = 0;
ccc6cda3 728
7117c2d2
JA
729int
730signal_is_trapped(s)
731int s;
732{
733 return 0;
ccc6cda3
JA
734}
735
7117c2d2
JA
736void
737fatal_error(const char *s, ...)
bb70624e 738{
7117c2d2
JA
739 fprintf(stderr, "array_test: fatal memory error\n");
740 abort();
741}
bb70624e 742
7117c2d2
JA
743void
744programming_error(const char *s, ...)
745{
746 fprintf(stderr, "array_test: fatal programming error\n");
747 abort();
bb70624e 748}
7117c2d2
JA
749
750WORD_DESC *
751make_bare_word (s)
752const char *s;
ccc6cda3 753{
7117c2d2 754 WORD_DESC *w;
ccc6cda3 755
7117c2d2
JA
756 w = (WORD_DESC *)xmalloc(sizeof(WORD_DESC));
757 w->word = s ? savestring(s) : savestring ("");
758 w->flags = 0;
759 return w;
ccc6cda3
JA
760}
761
7117c2d2
JA
762WORD_LIST *
763make_word_list(x, l)
764WORD_DESC *x;
765WORD_LIST *l;
ccc6cda3 766{
7117c2d2 767 WORD_LIST *w;
ccc6cda3 768
7117c2d2
JA
769 w = (WORD_LIST *)xmalloc(sizeof(WORD_LIST));
770 w->word = x;
771 w->next = l;
772 return w;
ccc6cda3
JA
773}
774
7117c2d2
JA
775WORD_LIST *
776list_string(s, t, i)
777char *s, *t;
778int i;
ccc6cda3 779{
7117c2d2
JA
780 char *r, *a;
781 WORD_LIST *wl;
ccc6cda3 782
7117c2d2
JA
783 if (s == 0)
784 return (WORD_LIST *)NULL;
785 r = savestring(s);
786 wl = (WORD_LIST *)NULL;
787 a = strtok(r, t);
788 while (a) {
789 wl = make_word_list (make_bare_word(a), wl);
790 a = strtok((char *)NULL, t);
ccc6cda3 791 }
7117c2d2 792 return (REVERSE_LIST (wl, WORD_LIST *));
ccc6cda3
JA
793}
794
7117c2d2
JA
795GENERIC_LIST *
796list_reverse (list)
797GENERIC_LIST *list;
ccc6cda3 798{
7117c2d2 799 register GENERIC_LIST *next, *prev;
ccc6cda3 800
7117c2d2
JA
801 for (prev = 0; list; ) {
802 next = list->next;
803 list->next = prev;
804 prev = list;
805 list = next;
806 }
807 return prev;
ccc6cda3
JA
808}
809
810char *
7117c2d2
JA
811pat_subst(s, t, u, i)
812char *s, *t, *u;
813int i;
ccc6cda3 814{
7117c2d2 815 return ((char *)NULL);
ccc6cda3
JA
816}
817
7117c2d2
JA
818char *
819quote_string(s)
820char *s;
821{
822 return savestring(s);
823}
ccc6cda3 824
ccc6cda3
JA
825print_element(ae)
826ARRAY_ELEMENT *ae;
827{
7117c2d2
JA
828 char lbuf[INT_STRLEN_BOUND (intmax_t) + 1];
829
830 printf("array[%s] = %s\n",
831 inttostr (element_index(ae), lbuf, sizeof (lbuf)),
832 element_value(ae));
ccc6cda3
JA
833}
834
835print_array(a)
836ARRAY *a;
837{
838 printf("\n");
b80f6443 839 array_walk(a, print_element, (void *)NULL);
ccc6cda3
JA
840}
841
842main()
843{
844 ARRAY *a, *new_a, *copy_of_a;
7117c2d2 845 ARRAY_ELEMENT *ae, *aew;
ccc6cda3
JA
846 char *s;
847
7117c2d2
JA
848 a = array_create();
849 array_insert(a, 1, "one");
850 array_insert(a, 7, "seven");
851 array_insert(a, 4, "four");
852 array_insert(a, 1029, "one thousand twenty-nine");
853 array_insert(a, 12, "twelve");
854 array_insert(a, 42, "forty-two");
ccc6cda3 855 print_array(a);
d166f048 856 s = array_to_string (a, " ", 0);
ccc6cda3 857 printf("s = %s\n", s);
7117c2d2 858 copy_of_a = array_from_string(s, " ");
ccc6cda3
JA
859 printf("copy_of_a:");
860 print_array(copy_of_a);
7117c2d2 861 array_dispose(copy_of_a);
ccc6cda3
JA
862 printf("\n");
863 free(s);
7117c2d2
JA
864 ae = array_remove(a, 4);
865 array_dispose_element(ae);
866 ae = array_remove(a, 1029);
867 array_dispose_element(ae);
868 array_insert(a, 16, "sixteen");
ccc6cda3 869 print_array(a);
d166f048 870 s = array_to_string (a, " ", 0);
ccc6cda3 871 printf("s = %s\n", s);
7117c2d2 872 copy_of_a = array_from_string(s, " ");
ccc6cda3
JA
873 printf("copy_of_a:");
874 print_array(copy_of_a);
7117c2d2 875 array_dispose(copy_of_a);
ccc6cda3
JA
876 printf("\n");
877 free(s);
7117c2d2
JA
878 array_insert(a, 2, "two");
879 array_insert(a, 1029, "new one thousand twenty-nine");
880 array_insert(a, 0, "zero");
881 array_insert(a, 134, "");
ccc6cda3 882 print_array(a);
d166f048 883 s = array_to_string (a, ":", 0);
ccc6cda3 884 printf("s = %s\n", s);
7117c2d2 885 copy_of_a = array_from_string(s, ":");
ccc6cda3
JA
886 printf("copy_of_a:");
887 print_array(copy_of_a);
7117c2d2 888 array_dispose(copy_of_a);
ccc6cda3
JA
889 printf("\n");
890 free(s);
7117c2d2 891 new_a = array_copy(a);
ccc6cda3 892 print_array(new_a);
d166f048 893 s = array_to_string (new_a, ":", 0);
ccc6cda3 894 printf("s = %s\n", s);
7117c2d2
JA
895 copy_of_a = array_from_string(s, ":");
896 free(s);
ccc6cda3
JA
897 printf("copy_of_a:");
898 print_array(copy_of_a);
7117c2d2
JA
899 array_shift(copy_of_a, 2, AS_DISPOSE);
900 printf("copy_of_a shifted by two:");
901 print_array(copy_of_a);
902 ae = array_shift(copy_of_a, 2, 0);
903 printf("copy_of_a shifted by two:");
904 print_array(copy_of_a);
905 for ( ; ae; ) {
906 aew = element_forw(ae);
907 array_dispose_element(ae);
908 ae = aew;
909 }
910 array_rshift(copy_of_a, 1, (char *)0);
911 printf("copy_of_a rshift by 1:");
912 print_array(copy_of_a);
913 array_rshift(copy_of_a, 2, "new element zero");
914 printf("copy_of_a rshift again by 2 with new element zero:");
915 print_array(copy_of_a);
916 s = array_to_assign(copy_of_a, 0);
917 printf("copy_of_a=%s\n", s);
ccc6cda3 918 free(s);
7117c2d2
JA
919 ae = array_shift(copy_of_a, array_num_elements(copy_of_a), 0);
920 for ( ; ae; ) {
921 aew = element_forw(ae);
922 array_dispose_element(ae);
923 ae = aew;
924 }
925 array_dispose(copy_of_a);
926 printf("\n");
927 array_dispose(a);
928 array_dispose(new_a);
ccc6cda3
JA
929}
930
931#endif /* TEST_ARRAY */
932#endif /* ARRAY_VARS */