blob: 910bbaad815d604ca32d9b8169bb79b98d61e690 [file] [log] [blame]
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001/*
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 */
Jari Aaltobb706242000-03-17 21:46:59 +000011
Chet Ramey74091dd2022-09-26 11:49:46 -040012/* Copyright (C) 1997-2021 Free Software Foundation, Inc.
Jari Aaltobb706242000-03-17 21:46:59 +000013
14 This file is part of GNU Bash, the Bourne Again SHell.
15
Jari Aalto31859422009-01-12 13:36:28 +000016 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.
Jari Aaltobb706242000-03-17 21:46:59 +000020
Jari Aalto31859422009-01-12 13:36:28 +000021 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.
Jari Aaltobb706242000-03-17 21:46:59 +000025
Jari Aalto31859422009-01-12 13:36:28 +000026 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*/
Jari Aaltobb706242000-03-17 21:46:59 +000029
Jari Aaltoccc6cda1996-12-23 17:02:34 +000030#include "config.h"
31
32#if defined (ARRAY_VARS)
33
34#if defined (HAVE_UNISTD_H)
Jari Aaltocce855b1998-04-17 19:52:44 +000035# ifdef _MINIX
36# include <sys/types.h>
37# endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +000038# include <unistd.h>
39#endif
40
41#include <stdio.h>
Jari Aaltod166f041997-06-05 14:59:13 +000042#include "bashansi.h"
43
Jari Aaltoccc6cda1996-12-23 17:02:34 +000044#include "shell.h"
45#include "array.h"
46#include "builtins/common.h"
47
Jari Aaltoccc6cda1996-12-23 17:02:34 +000048#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)
Chet Rameyd233b482019-01-07 09:27:52 -050055
56#define ADD_AFTER(ae, new) \
57 do { \
58 ae->next->prev = new; \
59 new->next = ae->next; \
60 new->prev = ae; \
61 ae->next = new; \
62 } while (0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +000063
Chet Ramey8868eda2020-12-06 15:51:17 -050064static char *array_to_string_internal PARAMS((ARRAY_ELEMENT *, ARRAY_ELEMENT *, char *, int));
Jari Aalto7117c2d2002-07-17 14:10:11 +000065
Chet Rameyd233b482019-01-07 09:27:52 -050066static char *spacesep = " ";
Chet Rameya0c0a002016-09-15 16:59:08 -040067
Chet Rameyd233b482019-01-07 09:27:52 -050068#define IS_LASTREF(a) (a->lastref)
Chet Rameyac50fba2014-02-26 09:36:43 -050069
70#define LASTREF_START(a, i) \
Chet Rameyd233b482019-01-07 09:27:52 -050071 (IS_LASTREF(a) && i >= element_index(a->lastref)) ? a->lastref \
72 : element_forw(a->head)
Chet Ramey00018032011-11-21 20:51:19 -050073
Chet Rameyd233b482019-01-07 09:27:52 -050074#define LASTREF(a) (a->lastref ? a->lastref : element_forw(a->head))
Chet Ramey00018032011-11-21 20:51:19 -050075
Chet Rameyd233b482019-01-07 09:27:52 -050076#define INVALIDATE_LASTREF(a) a->lastref = 0
77#define SET_LASTREF(a, e) a->lastref = (e)
78#define UNSET_LASTREF(a) a->lastref = 0;
Chet Ramey00018032011-11-21 20:51:19 -050079
Jari Aalto7117c2d2002-07-17 14:10:11 +000080ARRAY *
81array_create()
82{
83 ARRAY *r;
84 ARRAY_ELEMENT *head;
85
Chet Rameyd233b482019-01-07 09:27:52 -050086 r = (ARRAY *)xmalloc(sizeof(ARRAY));
Jari Aalto7117c2d2002-07-17 14:10:11 +000087 r->max_index = -1;
88 r->num_elements = 0;
Chet Rameyd233b482019-01-07 09:27:52 -050089 r->lastref = (ARRAY_ELEMENT *)0;
Jari Aalto7117c2d2002-07-17 14:10:11 +000090 head = array_create_element(-1, (char *)NULL); /* dummy head */
91 head->prev = head->next = head;
92 r->head = head;
93 return(r);
94}
95
96void
97array_flush (a)
98ARRAY *a;
99{
100 register ARRAY_ELEMENT *r, *r1;
101
102 if (a == 0)
103 return;
104 for (r = element_forw(a->head); r != a->head; ) {
105 r1 = element_forw(r);
106 array_dispose_element(r);
107 r = r1;
108 }
109 a->head->next = a->head->prev = a->head;
110 a->max_index = -1;
111 a->num_elements = 0;
Chet Ramey00018032011-11-21 20:51:19 -0500112 INVALIDATE_LASTREF(a);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000113}
114
115void
116array_dispose(a)
117ARRAY *a;
118{
119 if (a == 0)
120 return;
121 array_flush (a);
122 array_dispose_element(a->head);
123 free(a);
124}
125
126ARRAY *
127array_copy(a)
128ARRAY *a;
129{
130 ARRAY *a1;
131 ARRAY_ELEMENT *ae, *new;
132
Jari Aalto95732b42005-12-07 14:08:12 +0000133 if (a == 0)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000134 return((ARRAY *) NULL);
135 a1 = array_create();
Jari Aalto7117c2d2002-07-17 14:10:11 +0000136 a1->max_index = a->max_index;
137 a1->num_elements = a->num_elements;
138 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
139 new = array_create_element(element_index(ae), element_value(ae));
140 ADD_BEFORE(a1->head, new);
Chet Rameyd233b482019-01-07 09:27:52 -0500141 if (ae == LASTREF(a))
142 SET_LASTREF(a1, new);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000143 }
144 return(a1);
145}
146
Jari Aalto7117c2d2002-07-17 14:10:11 +0000147/*
148 * Make and return a new array composed of the elements in array A from
149 * S to E, inclusive.
150 */
151ARRAY *
152array_slice(array, s, e)
153ARRAY *array;
154ARRAY_ELEMENT *s, *e;
155{
156 ARRAY *a;
157 ARRAY_ELEMENT *p, *n;
158 int i;
159 arrayind_t mi;
160
161 a = array_create ();
Jari Aalto7117c2d2002-07-17 14:10:11 +0000162
Jari Aalto31859422009-01-12 13:36:28 +0000163 for (mi = 0, p = s, i = 0; p != e; p = element_forw(p), i++) {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000164 n = array_create_element (element_index(p), element_value(p));
165 ADD_BEFORE(a->head, n);
Jari Aaltof1be6662008-11-18 13:15:12 +0000166 mi = element_index(n);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000167 }
168 a->num_elements = i;
169 a->max_index = mi;
170 return a;
171}
Jari Aalto7117c2d2002-07-17 14:10:11 +0000172
173/*
174 * Walk the array, calling FUNC once for each element, with the array
175 * element as the argument.
176 */
177void
Jari Aaltob80f6442004-07-27 13:29:18 +0000178array_walk(a, func, udata)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000179ARRAY *a;
180sh_ae_map_func_t *func;
Jari Aaltob80f6442004-07-27 13:29:18 +0000181void *udata;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000182{
183 register ARRAY_ELEMENT *ae;
184
185 if (a == 0 || array_empty(a))
186 return;
187 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae))
Jari Aaltob80f6442004-07-27 13:29:18 +0000188 if ((*func)(ae, udata) < 0)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000189 return;
190}
191
192/*
193 * Shift the array A N elements to the left. Delete the first N elements
194 * and subtract N from the indices of the remaining elements. If FLAGS
195 * does not include AS_DISPOSE, this returns a singly-linked null-terminated
196 * list of elements so the caller can dispose of the chain. If FLAGS
197 * includes AS_DISPOSE, this function disposes of the shifted-out elements
198 * and returns NULL.
199 */
200ARRAY_ELEMENT *
201array_shift(a, n, flags)
202ARRAY *a;
203int n, flags;
204{
205 register ARRAY_ELEMENT *ae, *ret;
206 register int i;
207
208 if (a == 0 || array_empty(a) || n <= 0)
209 return ((ARRAY_ELEMENT *)NULL);
210
Chet Ramey00018032011-11-21 20:51:19 -0500211 INVALIDATE_LASTREF(a);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000212 for (i = 0, ret = ae = element_forw(a->head); ae != a->head && i < n; ae = element_forw(ae), i++)
213 ;
214 if (ae == a->head) {
215 /* Easy case; shifting out all of the elements */
216 if (flags & AS_DISPOSE) {
217 array_flush (a);
218 return ((ARRAY_ELEMENT *)NULL);
219 }
220 for (ae = ret; element_forw(ae) != a->head; ae = element_forw(ae))
221 ;
222 element_forw(ae) = (ARRAY_ELEMENT *)NULL;
223 a->head->next = a->head->prev = a->head;
224 a->max_index = -1;
225 a->num_elements = 0;
226 return ret;
227 }
228 /*
229 * ae now points to the list of elements we want to retain.
230 * ret points to the list we want to either destroy or return.
231 */
232 ae->prev->next = (ARRAY_ELEMENT *)NULL; /* null-terminate RET */
233
234 a->head->next = ae; /* slice RET out of the array */
235 ae->prev = a->head;
236
237 for ( ; ae != a->head; ae = element_forw(ae))
238 element_index(ae) -= n; /* renumber retained indices */
239
240 a->num_elements -= n; /* modify bookkeeping information */
Chet Ramey00018032011-11-21 20:51:19 -0500241 a->max_index = element_index(a->head->prev);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000242
243 if (flags & AS_DISPOSE) {
244 for (ae = ret; ae; ) {
245 ret = element_forw(ae);
246 array_dispose_element(ae);
247 ae = ret;
248 }
249 return ((ARRAY_ELEMENT *)NULL);
250 }
251
252 return ret;
253}
254
255/*
256 * Shift array A right N indices. If S is non-null, it becomes the value of
257 * the new element 0. Returns the number of elements in the array after the
258 * shift.
259 */
260int
261array_rshift (a, n, s)
262ARRAY *a;
263int n;
264char *s;
265{
266 register ARRAY_ELEMENT *ae, *new;
267
Jari Aalto95732b42005-12-07 14:08:12 +0000268 if (a == 0 || (array_empty(a) && s == 0))
Jari Aalto7117c2d2002-07-17 14:10:11 +0000269 return 0;
Jari Aalto95732b42005-12-07 14:08:12 +0000270 else if (n <= 0)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000271 return (a->num_elements);
272
273 ae = element_forw(a->head);
274 if (s) {
275 new = array_create_element(0, s);
276 ADD_BEFORE(ae, new);
277 a->num_elements++;
Chet Ramey00018032011-11-21 20:51:19 -0500278 if (array_num_elements(a) == 1) { /* array was empty */
279 a->max_index = 0;
Jari Aalto95732b42005-12-07 14:08:12 +0000280 return 1;
Chet Ramey00018032011-11-21 20:51:19 -0500281 }
Jari Aalto7117c2d2002-07-17 14:10:11 +0000282 }
283
Jari Aalto7117c2d2002-07-17 14:10:11 +0000284 /*
285 * Renumber all elements in the array except the one we just added.
286 */
287 for ( ; ae != a->head; ae = element_forw(ae))
288 element_index(ae) += n;
289
Jari Aalto95732b42005-12-07 14:08:12 +0000290 a->max_index = element_index(a->head->prev);
291
Chet Ramey00018032011-11-21 20:51:19 -0500292 INVALIDATE_LASTREF(a);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000293 return (a->num_elements);
294}
295
Jari Aaltob80f6442004-07-27 13:29:18 +0000296ARRAY_ELEMENT *
297array_unshift_element(a)
298ARRAY *a;
299{
300 return (array_shift (a, 1, 0));
301}
302
303int
304array_shift_element(a, v)
305ARRAY *a;
306char *v;
307{
308 return (array_rshift (a, 1, v));
309}
310
Jari Aalto31859422009-01-12 13:36:28 +0000311ARRAY *
Jari Aalto7117c2d2002-07-17 14:10:11 +0000312array_quote(array)
313ARRAY *array;
314{
315 ARRAY_ELEMENT *a;
316 char *t;
317
Jari Aalto95732b42005-12-07 14:08:12 +0000318 if (array == 0 || array_head(array) == 0 || array_empty(array))
Jari Aalto7117c2d2002-07-17 14:10:11 +0000319 return (ARRAY *)NULL;
320 for (a = element_forw(array->head); a != array->head; a = element_forw(a)) {
321 t = quote_string (a->value);
322 FREE(a->value);
323 a->value = t;
324 }
325 return array;
326}
327
Jari Aalto31859422009-01-12 13:36:28 +0000328ARRAY *
Jari Aaltof1be6662008-11-18 13:15:12 +0000329array_quote_escapes(array)
330ARRAY *array;
331{
332 ARRAY_ELEMENT *a;
333 char *t;
334
335 if (array == 0 || array_head(array) == 0 || array_empty(array))
336 return (ARRAY *)NULL;
337 for (a = element_forw(array->head); a != array->head; a = element_forw(a)) {
338 t = quote_escapes (a->value);
339 FREE(a->value);
340 a->value = t;
341 }
342 return array;
343}
344
Jari Aalto31859422009-01-12 13:36:28 +0000345ARRAY *
346array_dequote(array)
347ARRAY *array;
348{
349 ARRAY_ELEMENT *a;
350 char *t;
351
352 if (array == 0 || array_head(array) == 0 || array_empty(array))
353 return (ARRAY *)NULL;
354 for (a = element_forw(array->head); a != array->head; a = element_forw(a)) {
355 t = dequote_string (a->value);
356 FREE(a->value);
357 a->value = t;
358 }
359 return array;
360}
361
362ARRAY *
363array_dequote_escapes(array)
364ARRAY *array;
365{
366 ARRAY_ELEMENT *a;
367 char *t;
368
369 if (array == 0 || array_head(array) == 0 || array_empty(array))
370 return (ARRAY *)NULL;
371 for (a = element_forw(array->head); a != array->head; a = element_forw(a)) {
372 t = dequote_escapes (a->value);
373 FREE(a->value);
374 a->value = t;
375 }
376 return array;
377}
378
379ARRAY *
380array_remove_quoted_nulls(array)
381ARRAY *array;
382{
383 ARRAY_ELEMENT *a;
Jari Aalto31859422009-01-12 13:36:28 +0000384
385 if (array == 0 || array_head(array) == 0 || array_empty(array))
386 return (ARRAY *)NULL;
387 for (a = element_forw(array->head); a != array->head; a = element_forw(a))
388 a->value = remove_quoted_nulls (a->value);
389 return array;
390}
391
Jari Aaltob80f6442004-07-27 13:29:18 +0000392/*
393 * Return a string whose elements are the members of array A beginning at
394 * index START and spanning NELEM members. Null elements are counted.
395 * Since arrays are sparse, unset array elements are not counted.
396 */
Jari Aalto7117c2d2002-07-17 14:10:11 +0000397char *
Chet Ramey8868eda2020-12-06 15:51:17 -0500398array_subrange (a, start, nelem, starsub, quoted, pflags)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000399ARRAY *a;
Jari Aaltob80f6442004-07-27 13:29:18 +0000400arrayind_t start, nelem;
Chet Ramey8868eda2020-12-06 15:51:17 -0500401int starsub, quoted, pflags;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000402{
Jari Aaltof1be6662008-11-18 13:15:12 +0000403 ARRAY *a2;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000404 ARRAY_ELEMENT *h, *p;
405 arrayind_t i;
Chet Rameyd233b482019-01-07 09:27:52 -0500406 char *t;
407 WORD_LIST *wl;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000408
Jari Aalto95732b42005-12-07 14:08:12 +0000409 p = a ? array_head (a) : 0;
Jari Aaltob80f6442004-07-27 13:29:18 +0000410 if (p == 0 || array_empty (a) || start > array_max_index(a))
Jari Aalto7117c2d2002-07-17 14:10:11 +0000411 return ((char *)NULL);
412
Jari Aaltob80f6442004-07-27 13:29:18 +0000413 /*
414 * Find element with index START. If START corresponds to an unset
415 * element (arrays can be sparse), use the first element whose index
416 * is >= START. If START is < 0, we count START indices back from
417 * the end of A (not elements, even with sparse arrays -- START is an
418 * index).
419 */
420 for (p = element_forw(p); p != array_head(a) && start > element_index(p); p = element_forw(p))
Jari Aalto7117c2d2002-07-17 14:10:11 +0000421 ;
Jari Aaltob80f6442004-07-27 13:29:18 +0000422
Jari Aalto7117c2d2002-07-17 14:10:11 +0000423 if (p == a->head)
424 return ((char *)NULL);
Jari Aaltob80f6442004-07-27 13:29:18 +0000425
426 /* Starting at P, take NELEM elements, inclusive. */
427 for (i = 0, h = p; p != a->head && i < nelem; i++, p = element_forw(p))
Jari Aalto7117c2d2002-07-17 14:10:11 +0000428 ;
429
Jari Aaltof1be6662008-11-18 13:15:12 +0000430 a2 = array_slice(a, h, p);
431
Chet Rameyd233b482019-01-07 09:27:52 -0500432 wl = array_to_word_list(a2);
Jari Aaltof1be6662008-11-18 13:15:12 +0000433 array_dispose(a2);
Chet Rameyd233b482019-01-07 09:27:52 -0500434 if (wl == 0)
435 return (char *)NULL;
Chet Ramey8868eda2020-12-06 15:51:17 -0500436 t = string_list_pos_params(starsub ? '*' : '@', wl, quoted, pflags); /* XXX */
Chet Rameyd233b482019-01-07 09:27:52 -0500437 dispose_words(wl);
Jari Aaltof1be6662008-11-18 13:15:12 +0000438
439 return t;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000440}
441
442char *
443array_patsub (a, pat, rep, mflags)
444ARRAY *a;
445char *pat, *rep;
446int mflags;
447{
Chet Rameyd233b482019-01-07 09:27:52 -0500448 char *t;
Chet Ramey8868eda2020-12-06 15:51:17 -0500449 int pchar, qflags, pflags;
Chet Rameyd233b482019-01-07 09:27:52 -0500450 WORD_LIST *wl, *save;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000451
Jari Aalto95732b42005-12-07 14:08:12 +0000452 if (a == 0 || array_head(a) == 0 || array_empty(a))
Jari Aalto7117c2d2002-07-17 14:10:11 +0000453 return ((char *)NULL);
454
Chet Rameyd233b482019-01-07 09:27:52 -0500455 wl = array_to_word_list(a);
456 if (wl == 0)
457 return (char *)NULL;
458
459 for (save = wl; wl; wl = wl->next) {
460 t = pat_subst (wl->word->word, pat, rep, mflags);
461 FREE (wl->word->word);
462 wl->word->word = t;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000463 }
464
Chet Rameyd233b482019-01-07 09:27:52 -0500465 pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@';
466 qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0;
Chet Ramey8868eda2020-12-06 15:51:17 -0500467 pflags = (mflags & MATCH_ASSIGNRHS) ? PF_ASSIGNRHS : 0;
Jari Aalto31859422009-01-12 13:36:28 +0000468
Chet Ramey8868eda2020-12-06 15:51:17 -0500469 t = string_list_pos_params (pchar, save, qflags, pflags);
Chet Rameyd233b482019-01-07 09:27:52 -0500470 dispose_words(save);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000471
472 return t;
473}
474
Jari Aalto31859422009-01-12 13:36:28 +0000475char *
476array_modcase (a, pat, modop, mflags)
477ARRAY *a;
478char *pat;
479int modop;
480int mflags;
481{
Chet Rameyd233b482019-01-07 09:27:52 -0500482 char *t;
Chet Ramey8868eda2020-12-06 15:51:17 -0500483 int pchar, qflags, pflags;
Chet Rameyd233b482019-01-07 09:27:52 -0500484 WORD_LIST *wl, *save;
Jari Aalto31859422009-01-12 13:36:28 +0000485
486 if (a == 0 || array_head(a) == 0 || array_empty(a))
487 return ((char *)NULL);
488
Chet Rameyd233b482019-01-07 09:27:52 -0500489 wl = array_to_word_list(a);
490 if (wl == 0)
491 return ((char *)NULL);
492
493 for (save = wl; wl; wl = wl->next) {
494 t = sh_modcase(wl->word->word, pat, modop);
495 FREE(wl->word->word);
496 wl->word->word = t;
Jari Aalto31859422009-01-12 13:36:28 +0000497 }
498
Chet Rameyd233b482019-01-07 09:27:52 -0500499 pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@';
500 qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0;
Chet Ramey8868eda2020-12-06 15:51:17 -0500501 pflags = (mflags & MATCH_ASSIGNRHS) ? PF_ASSIGNRHS : 0;
Jari Aalto31859422009-01-12 13:36:28 +0000502
Chet Ramey8868eda2020-12-06 15:51:17 -0500503 t = string_list_pos_params (pchar, save, qflags, pflags);
Chet Rameyd233b482019-01-07 09:27:52 -0500504 dispose_words(save);
Jari Aalto31859422009-01-12 13:36:28 +0000505
506 return t;
507}
Chet Rameyd233b482019-01-07 09:27:52 -0500508
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000509/*
510 * Allocate and return a new array element with index INDEX and value
511 * VALUE.
512 */
513ARRAY_ELEMENT *
Jari Aalto7117c2d2002-07-17 14:10:11 +0000514array_create_element(indx, value)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000515arrayind_t indx;
516char *value;
517{
518 ARRAY_ELEMENT *r;
519
Jari Aaltof73dda02001-11-13 17:56:06 +0000520 r = (ARRAY_ELEMENT *)xmalloc(sizeof(ARRAY_ELEMENT));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000521 r->ind = indx;
522 r->value = value ? savestring(value) : (char *)NULL;
523 r->next = r->prev = (ARRAY_ELEMENT *) NULL;
524 return(r);
525}
526
Jari Aalto7117c2d2002-07-17 14:10:11 +0000527#ifdef INCLUDE_UNUSED
528ARRAY_ELEMENT *
529array_copy_element(ae)
530ARRAY_ELEMENT *ae;
531{
532 return(ae ? array_create_element(element_index(ae), element_value(ae))
533 : (ARRAY_ELEMENT *) NULL);
534}
535#endif
536
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000537void
Jari Aalto7117c2d2002-07-17 14:10:11 +0000538array_dispose_element(ae)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000539ARRAY_ELEMENT *ae;
540{
Jari Aaltob80f6442004-07-27 13:29:18 +0000541 if (ae) {
542 FREE(ae->value);
543 free(ae);
544 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000545}
546
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000547/*
548 * Add a new element with index I and value V to array A (a[i] = v).
549 */
550int
Jari Aalto7117c2d2002-07-17 14:10:11 +0000551array_insert(a, i, v)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000552ARRAY *a;
553arrayind_t i;
554char *v;
555{
Chet Rameyac50fba2014-02-26 09:36:43 -0500556 register ARRAY_ELEMENT *new, *ae, *start;
Chet Rameyd233b482019-01-07 09:27:52 -0500557 arrayind_t startind;
558 int direction;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000559
Jari Aalto95732b42005-12-07 14:08:12 +0000560 if (a == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000561 return(-1);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000562 new = array_create_element(i, v);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000563 if (i > array_max_index(a)) {
564 /*
565 * Hook onto the end. This also works for an empty array.
566 * Fast path for the common case of allocating arrays
567 * sequentially.
568 */
569 ADD_BEFORE(a->head, new);
570 a->max_index = i;
571 a->num_elements++;
Chet Ramey00018032011-11-21 20:51:19 -0500572 SET_LASTREF(a, new);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000573 return(0);
Chet Rameyd233b482019-01-07 09:27:52 -0500574 } else if (i < array_first_index(a)) {
575 /* Hook at the beginning */
576 ADD_AFTER(a->head, new);
577 a->num_elements++;
578 SET_LASTREF(a, new);
579 return(0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000580 }
Chet Rameyac50fba2014-02-26 09:36:43 -0500581#if OPTIMIZE_SEQUENTIAL_ARRAY_ASSIGNMENT
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000582 /*
Chet Rameyac50fba2014-02-26 09:36:43 -0500583 * Otherwise we search for the spot to insert it. The lastref
584 * handle optimizes the case of sequential or almost-sequential
585 * assignments that are not at the end of the array.
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000586 */
Chet Rameyd233b482019-01-07 09:27:52 -0500587 start = LASTREF(a);
588 /* Use same strategy as array_reference to avoid paying large penalty
589 for semi-random assignment pattern. */
590 startind = element_index(start);
591 if (i < startind/2) {
592 start = element_forw(a->head);
593 startind = element_index(start);
594 direction = 1;
595 } else if (i >= startind) {
596 direction = 1;
597 } else {
598 direction = -1;
599 }
Chet Rameyac50fba2014-02-26 09:36:43 -0500600#else
601 start = element_forw(ae->head);
Chet Rameyd233b482019-01-07 09:27:52 -0500602 startind = element_index(start);
603 direction = 1;
Chet Rameyac50fba2014-02-26 09:36:43 -0500604#endif
Chet Rameyd233b482019-01-07 09:27:52 -0500605 for (ae = start; ae != a->head; ) {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000606 if (element_index(ae) == i) {
607 /*
608 * Replacing an existing element.
609 */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000610 free(element_value(ae));
Chet Rameyd233b482019-01-07 09:27:52 -0500611 /* Just swap in the new value */
612 ae->value = new->value;
613 new->value = 0;
614 array_dispose_element(new);
Chet Ramey00018032011-11-21 20:51:19 -0500615 SET_LASTREF(a, ae);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000616 return(0);
Chet Rameyd233b482019-01-07 09:27:52 -0500617 } else if (direction == 1 && element_index(ae) > i) {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000618 ADD_BEFORE(ae, new);
619 a->num_elements++;
Chet Ramey00018032011-11-21 20:51:19 -0500620 SET_LASTREF(a, new);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000621 return(0);
Chet Rameyd233b482019-01-07 09:27:52 -0500622 } else if (direction == -1 && element_index(ae) < i) {
623 ADD_AFTER(ae, new);
624 a->num_elements++;
625 SET_LASTREF(a, new);
626 return(0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000627 }
Chet Rameyd233b482019-01-07 09:27:52 -0500628 ae = direction == 1 ? element_forw(ae) : element_back(ae);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000629 }
Chet Rameyac50fba2014-02-26 09:36:43 -0500630 array_dispose_element(new);
Chet Ramey00018032011-11-21 20:51:19 -0500631 INVALIDATE_LASTREF(a);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000632 return (-1); /* problem */
633}
634
635/*
636 * Delete the element with index I from array A and return it so the
637 * caller can dispose of it.
638 */
639ARRAY_ELEMENT *
Jari Aalto7117c2d2002-07-17 14:10:11 +0000640array_remove(a, i)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000641ARRAY *a;
642arrayind_t i;
643{
Chet Rameyac50fba2014-02-26 09:36:43 -0500644 register ARRAY_ELEMENT *ae, *start;
Chet Rameyd233b482019-01-07 09:27:52 -0500645 arrayind_t startind;
646 int direction;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000647
Jari Aalto95732b42005-12-07 14:08:12 +0000648 if (a == 0 || array_empty(a))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000649 return((ARRAY_ELEMENT *) NULL);
Chet Rameyd233b482019-01-07 09:27:52 -0500650 if (i > array_max_index(a) || i < array_first_index(a))
651 return((ARRAY_ELEMENT *)NULL); /* Keep roving pointer into array to optimize sequential access */
652 start = LASTREF(a);
653 /* Use same strategy as array_reference to avoid paying large penalty
654 for semi-random assignment pattern. */
655 startind = element_index(start);
656 if (i < startind/2) {
657 start = element_forw(a->head);
658 startind = element_index(start);
659 direction = 1;
660 } else if (i >= startind) {
661 direction = 1;
662 } else {
663 direction = -1;
664 }
665 for (ae = start; ae != a->head; ) {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000666 if (element_index(ae) == i) {
667 ae->next->prev = ae->prev;
668 ae->prev->next = ae->next;
669 a->num_elements--;
670 if (i == array_max_index(a))
671 a->max_index = element_index(ae->prev);
Chet Rameyac50fba2014-02-26 09:36:43 -0500672#if 0
Chet Ramey00018032011-11-21 20:51:19 -0500673 INVALIDATE_LASTREF(a);
Chet Rameyac50fba2014-02-26 09:36:43 -0500674#else
675 if (ae->next != a->head)
676 SET_LASTREF(a, ae->next);
677 else if (ae->prev != a->head)
678 SET_LASTREF(a, ae->prev);
679 else
680 INVALIDATE_LASTREF(a);
681#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000682 return(ae);
683 }
Chet Rameyd233b482019-01-07 09:27:52 -0500684 ae = (direction == 1) ? element_forw(ae) : element_back(ae);
685 if (direction == 1 && element_index(ae) > i)
686 break;
687 else if (direction == -1 && element_index(ae) < i)
688 break;
689 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000690 return((ARRAY_ELEMENT *) NULL);
691}
692
693/*
694 * Return the value of a[i].
695 */
696char *
697array_reference(a, i)
698ARRAY *a;
699arrayind_t i;
700{
Chet Rameyac50fba2014-02-26 09:36:43 -0500701 register ARRAY_ELEMENT *ae, *start;
Chet Rameyd233b482019-01-07 09:27:52 -0500702 arrayind_t startind;
703 int direction;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000704
705 if (a == 0 || array_empty(a))
706 return((char *) NULL);
Chet Rameyd233b482019-01-07 09:27:52 -0500707 if (i > array_max_index(a) || i < array_first_index(a))
Chet Rameyac50fba2014-02-26 09:36:43 -0500708 return((char *)NULL); /* Keep roving pointer into array to optimize sequential access */
Chet Rameyd233b482019-01-07 09:27:52 -0500709 start = LASTREF(a); /* lastref pointer */
710 startind = element_index(start);
711 if (i < startind/2) { /* XXX - guess */
712 start = element_forw(a->head);
713 startind = element_index(start);
714 direction = 1;
715 } else if (i >= startind) {
716 direction = 1;
717 } else {
718 direction = -1;
719 }
720 for (ae = start; ae != a->head; ) {
Chet Ramey00018032011-11-21 20:51:19 -0500721 if (element_index(ae) == i) {
722 SET_LASTREF(a, ae);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000723 return(element_value(ae));
Chet Ramey00018032011-11-21 20:51:19 -0500724 }
Chet Rameyd233b482019-01-07 09:27:52 -0500725 ae = (direction == 1) ? element_forw(ae) : element_back(ae);
726 /* Take advantage of index ordering to short-circuit */
727 /* If we don't find it, set the lastref pointer to the element
728 that's `closest', assuming that the unsuccessful reference
729 will quickly be followed by an assignment. No worse than
730 not changing it from the previous value or resetting it. */
731 if (direction == 1 && element_index(ae) > i) {
732 start = ae; /* use for SET_LASTREF below */
733 break;
734 } else if (direction == -1 && element_index(ae) < i) {
735 start = ae; /* use for SET_LASTREF below */
736 break;
737 }
738 }
739#if 0
740 UNSET_LASTREF(a);
741#else
742 SET_LASTREF(a, start);
743#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000744 return((char *) NULL);
745}
746
Jari Aalto7117c2d2002-07-17 14:10:11 +0000747/* Convenience routines for the shell to translate to and from the form used
748 by the rest of the code. */
Jari Aaltob80f6442004-07-27 13:29:18 +0000749
Jari Aalto7117c2d2002-07-17 14:10:11 +0000750WORD_LIST *
751array_to_word_list(a)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000752ARRAY *a;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000753{
Jari Aalto7117c2d2002-07-17 14:10:11 +0000754 WORD_LIST *list;
755 ARRAY_ELEMENT *ae;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000756
757 if (a == 0 || array_empty(a))
Jari Aalto7117c2d2002-07-17 14:10:11 +0000758 return((WORD_LIST *)NULL);
759 list = (WORD_LIST *)NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000760 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae))
Jari Aalto7117c2d2002-07-17 14:10:11 +0000761 list = make_word_list (make_bare_word(element_value(ae)), list);
762 return (REVERSE_LIST(list, WORD_LIST *));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000763}
764
Jari Aalto7117c2d2002-07-17 14:10:11 +0000765ARRAY *
766array_from_word_list (list)
767WORD_LIST *list;
768{
769 ARRAY *a;
770
771 if (list == 0)
772 return((ARRAY *)NULL);
773 a = array_create();
774 return (array_assign_list (a, list));
775}
776
Jari Aaltob80f6442004-07-27 13:29:18 +0000777WORD_LIST *
778array_keys_to_word_list(a)
779ARRAY *a;
780{
781 WORD_LIST *list;
782 ARRAY_ELEMENT *ae;
783 char *t;
784
785 if (a == 0 || array_empty(a))
786 return((WORD_LIST *)NULL);
787 list = (WORD_LIST *)NULL;
788 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
789 t = itos(element_index(ae));
790 list = make_word_list (make_bare_word(t), list);
791 free(t);
792 }
793 return (REVERSE_LIST(list, WORD_LIST *));
794}
795
Chet Ramey74091dd2022-09-26 11:49:46 -0400796WORD_LIST *
797array_to_kvpair_list(a)
798ARRAY *a;
799{
800 WORD_LIST *list;
801 ARRAY_ELEMENT *ae;
802 char *k, *v;
803
804 if (a == 0 || array_empty(a))
805 return((WORD_LIST *)NULL);
806 list = (WORD_LIST *)NULL;
807 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
808 k = itos(element_index(ae));
809 v = element_value(ae);
810 list = make_word_list (make_bare_word(k), list);
811 list = make_word_list (make_bare_word(v), list);
812 free(k);
813 }
814 return (REVERSE_LIST(list, WORD_LIST *));
815}
816
Jari Aalto7117c2d2002-07-17 14:10:11 +0000817ARRAY *
818array_assign_list (array, list)
819ARRAY *array;
820WORD_LIST *list;
821{
822 register WORD_LIST *l;
823 register arrayind_t i;
824
825 for (l = list, i = 0; l; l = l->next, i++)
826 array_insert(array, i, l->word->word);
827 return array;
828}
829
830char **
Chet Ramey8868eda2020-12-06 15:51:17 -0500831array_to_argv (a, countp)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000832ARRAY *a;
Chet Ramey8868eda2020-12-06 15:51:17 -0500833int *countp;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000834{
835 char **ret, *t;
836 int i;
837 ARRAY_ELEMENT *ae;
838
Chet Ramey8868eda2020-12-06 15:51:17 -0500839 if (a == 0 || array_empty(a)) {
840 if (countp)
841 *countp = 0;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000842 return ((char **)NULL);
Chet Ramey8868eda2020-12-06 15:51:17 -0500843 }
Jari Aalto7117c2d2002-07-17 14:10:11 +0000844 ret = strvec_create (array_num_elements (a) + 1);
845 i = 0;
846 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
847 t = element_value (ae);
Chet Ramey8868eda2020-12-06 15:51:17 -0500848 if (t)
849 ret[i++] = savestring (t);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000850 }
851 ret[i] = (char *)NULL;
Chet Ramey8868eda2020-12-06 15:51:17 -0500852 if (countp)
853 *countp = i;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000854 return (ret);
855}
Chet Ramey74091dd2022-09-26 11:49:46 -0400856
857ARRAY *
858array_from_argv(a, vec, count)
859ARRAY *a;
860char **vec;
861int count;
862{
863 arrayind_t i;
864 ARRAY_ELEMENT *ae;
865 char *t;
866
867 if (a == 0 || array_num_elements (a) == 0)
868 {
869 for (i = 0; i < count; i++)
870 array_insert (a, i, t);
871 return a;
872 }
873
874 /* Fast case */
875 if (array_num_elements (a) == count && count == 1)
876 {
877 ae = element_forw (a->head);
878 t = vec[0] ? savestring (vec[0]) : 0;
879 ARRAY_ELEMENT_REPLACE (ae, t);
880 }
881 else if (array_num_elements (a) <= count)
882 {
883 /* modify in array_num_elements members in place, then add */
884 ae = a->head;
885 for (i = 0; i < array_num_elements (a); i++)
886 {
887 ae = element_forw (ae);
888 t = vec[0] ? savestring (vec[0]) : 0;
889 ARRAY_ELEMENT_REPLACE (ae, t);
890 }
891 /* add any more */
892 for ( ; i < count; i++)
893 array_insert (a, i, vec[i]);
894 }
895 else
896 {
897 /* deleting elements. it's faster to rebuild the array. */
898 array_flush (a);
899 for (i = 0; i < count; i++)
900 array_insert (a, i, vec[i]);
901 }
902
903 return a;
904}
Jari Aalto7117c2d2002-07-17 14:10:11 +0000905
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000906/*
Jari Aalto31859422009-01-12 13:36:28 +0000907 * Return a string that is the concatenation of the elements in A from START
908 * to END, separated by SEP.
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000909 */
910static char *
911array_to_string_internal (start, end, sep, quoted)
912ARRAY_ELEMENT *start, *end;
913char *sep;
914int quoted;
915{
916 char *result, *t;
917 ARRAY_ELEMENT *ae;
918 int slen, rsize, rlen, reg;
919
920 if (start == end) /* XXX - should not happen */
921 return ((char *)NULL);
922
923 slen = strlen(sep);
Jari Aaltof73dda02001-11-13 17:56:06 +0000924 result = NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000925 for (rsize = rlen = 0, ae = start; ae != end; ae = element_forw(ae)) {
926 if (rsize == 0)
Jari Aaltof73dda02001-11-13 17:56:06 +0000927 result = (char *)xmalloc (rsize = 64);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000928 if (element_value(ae)) {
929 t = quoted ? quote_string(element_value(ae)) : element_value(ae);
930 reg = strlen(t);
931 RESIZE_MALLOCED_BUFFER (result, rlen, (reg + slen + 2),
932 rsize, rsize);
933 strcpy(result + rlen, t);
934 rlen += reg;
Chet Rameya0c0a002016-09-15 16:59:08 -0400935 if (quoted)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000936 free(t);
937 /*
938 * Add a separator only after non-null elements.
939 */
940 if (element_forw(ae) != end) {
941 strcpy(result + rlen, sep);
942 rlen += slen;
943 }
944 }
945 }
Jari Aaltof73dda02001-11-13 17:56:06 +0000946 if (result)
947 result[rlen] = '\0'; /* XXX */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000948 return(result);
949}
950
951char *
Chet Ramey8868eda2020-12-06 15:51:17 -0500952array_to_kvpair (a, quoted)
953ARRAY *a;
954int quoted;
955{
956 char *result, *valstr, *is;
957 char indstr[INT_STRLEN_BOUND(intmax_t) + 1];
958 ARRAY_ELEMENT *ae;
959 int rsize, rlen, elen;
960
961 if (a == 0 || array_empty (a))
962 return((char *)NULL);
963
964 result = (char *)xmalloc (rsize = 128);
965 result[rlen = 0] = '\0';
966
967 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
968 is = inttostr (element_index(ae), indstr, sizeof(indstr));
969 valstr = element_value (ae) ?
970 (ansic_shouldquote (element_value (ae)) ?
971 ansic_quote (element_value(ae), 0, (int *)0) :
972 sh_double_quote (element_value (ae)))
973 : (char *)NULL;
974 elen = STRLEN (is) + 8 + STRLEN (valstr);
975 RESIZE_MALLOCED_BUFFER (result, rlen, (elen + 1), rsize, rsize);
976
977 strcpy (result + rlen, is);
978 rlen += STRLEN (is);
979 result[rlen++] = ' ';
980 if (valstr) {
981 strcpy (result + rlen, valstr);
982 rlen += STRLEN (valstr);
983 } else {
984 strcpy (result + rlen, "\"\"");
985 rlen += 2;
986 }
987
988 if (element_forw(ae) != a->head)
989 result[rlen++] = ' ';
990
991 FREE (valstr);
992 }
993 RESIZE_MALLOCED_BUFFER (result, rlen, 1, rsize, 8);
994 result[rlen] = '\0';
995
996 if (quoted) {
997 /* This is not as efficient as it could be... */
998 valstr = sh_single_quote (result);
999 free (result);
1000 result = valstr;
1001 }
1002 return(result);
1003}
1004
1005char *
Jari Aalto7117c2d2002-07-17 14:10:11 +00001006array_to_assign (a, quoted)
1007ARRAY *a;
1008int quoted;
1009{
1010 char *result, *valstr, *is;
1011 char indstr[INT_STRLEN_BOUND(intmax_t) + 1];
1012 ARRAY_ELEMENT *ae;
1013 int rsize, rlen, elen;
1014
1015 if (a == 0 || array_empty (a))
1016 return((char *)NULL);
1017
1018 result = (char *)xmalloc (rsize = 128);
1019 result[0] = '(';
1020 rlen = 1;
1021
1022 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
1023 is = inttostr (element_index(ae), indstr, sizeof(indstr));
Chet Rameya0c0a002016-09-15 16:59:08 -04001024 valstr = element_value (ae) ?
1025 (ansic_shouldquote (element_value (ae)) ?
1026 ansic_quote (element_value(ae), 0, (int *)0) :
1027 sh_double_quote (element_value (ae)))
Jari Aalto7117c2d2002-07-17 14:10:11 +00001028 : (char *)NULL;
Jari Aaltof1be6662008-11-18 13:15:12 +00001029 elen = STRLEN (is) + 8 + STRLEN (valstr);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001030 RESIZE_MALLOCED_BUFFER (result, rlen, (elen + 1), rsize, rsize);
1031
1032 result[rlen++] = '[';
1033 strcpy (result + rlen, is);
1034 rlen += STRLEN (is);
1035 result[rlen++] = ']';
1036 result[rlen++] = '=';
1037 if (valstr) {
1038 strcpy (result + rlen, valstr);
1039 rlen += STRLEN (valstr);
1040 }
1041
1042 if (element_forw(ae) != a->head)
1043 result[rlen++] = ' ';
1044
1045 FREE (valstr);
1046 }
1047 RESIZE_MALLOCED_BUFFER (result, rlen, 1, rsize, 8);
1048 result[rlen++] = ')';
1049 result[rlen] = '\0';
1050 if (quoted) {
1051 /* This is not as efficient as it could be... */
1052 valstr = sh_single_quote (result);
1053 free (result);
1054 result = valstr;
1055 }
1056 return(result);
1057}
1058
1059char *
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001060array_to_string (a, sep, quoted)
1061ARRAY *a;
1062char *sep;
1063int quoted;
1064{
1065 if (a == 0)
1066 return((char *)NULL);
1067 if (array_empty(a))
1068 return(savestring(""));
1069 return (array_to_string_internal (element_forw(a->head), a->head, sep, quoted));
1070}
1071
Jari Aaltod166f041997-06-05 14:59:13 +00001072#if defined (INCLUDE_UNUSED) || defined (TEST_ARRAY)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001073/*
1074 * Return an array consisting of elements in S, separated by SEP
1075 */
1076ARRAY *
Jari Aalto7117c2d2002-07-17 14:10:11 +00001077array_from_string(s, sep)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001078char *s, *sep;
1079{
1080 ARRAY *a;
1081 WORD_LIST *w;
1082
1083 if (s == 0)
1084 return((ARRAY *)NULL);
1085 w = list_string (s, sep, 0);
1086 if (w == 0)
1087 return((ARRAY *)NULL);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001088 a = array_from_word_list (w);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001089 return (a);
1090}
Jari Aaltod166f041997-06-05 14:59:13 +00001091#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001092
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001093#if defined (TEST_ARRAY)
Jari Aalto7117c2d2002-07-17 14:10:11 +00001094/*
1095 * To make a running version, compile -DTEST_ARRAY and link with:
1096 * xmalloc.o syntax.o lib/malloc/libmalloc.a lib/sh/libsh.a
1097 */
1098int interrupt_immediately = 0;
1099
1100int
1101signal_is_trapped(s)
1102int s;
1103{
1104 return 0;
1105}
1106
1107void
1108fatal_error(const char *s, ...)
1109{
1110 fprintf(stderr, "array_test: fatal memory error\n");
1111 abort();
1112}
1113
1114void
1115programming_error(const char *s, ...)
1116{
1117 fprintf(stderr, "array_test: fatal programming error\n");
1118 abort();
1119}
1120
1121WORD_DESC *
1122make_bare_word (s)
1123const char *s;
1124{
1125 WORD_DESC *w;
1126
1127 w = (WORD_DESC *)xmalloc(sizeof(WORD_DESC));
1128 w->word = s ? savestring(s) : savestring ("");
1129 w->flags = 0;
1130 return w;
1131}
1132
1133WORD_LIST *
1134make_word_list(x, l)
1135WORD_DESC *x;
1136WORD_LIST *l;
1137{
1138 WORD_LIST *w;
1139
1140 w = (WORD_LIST *)xmalloc(sizeof(WORD_LIST));
1141 w->word = x;
1142 w->next = l;
1143 return w;
1144}
1145
1146WORD_LIST *
1147list_string(s, t, i)
1148char *s, *t;
1149int i;
1150{
1151 char *r, *a;
1152 WORD_LIST *wl;
1153
1154 if (s == 0)
1155 return (WORD_LIST *)NULL;
1156 r = savestring(s);
1157 wl = (WORD_LIST *)NULL;
1158 a = strtok(r, t);
1159 while (a) {
1160 wl = make_word_list (make_bare_word(a), wl);
1161 a = strtok((char *)NULL, t);
1162 }
1163 return (REVERSE_LIST (wl, WORD_LIST *));
1164}
1165
1166GENERIC_LIST *
1167list_reverse (list)
1168GENERIC_LIST *list;
1169{
1170 register GENERIC_LIST *next, *prev;
1171
1172 for (prev = 0; list; ) {
1173 next = list->next;
1174 list->next = prev;
1175 prev = list;
1176 list = next;
1177 }
1178 return prev;
1179}
1180
1181char *
1182pat_subst(s, t, u, i)
1183char *s, *t, *u;
1184int i;
1185{
1186 return ((char *)NULL);
1187}
1188
1189char *
1190quote_string(s)
1191char *s;
1192{
1193 return savestring(s);
1194}
1195
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001196print_element(ae)
1197ARRAY_ELEMENT *ae;
1198{
Jari Aalto7117c2d2002-07-17 14:10:11 +00001199 char lbuf[INT_STRLEN_BOUND (intmax_t) + 1];
1200
1201 printf("array[%s] = %s\n",
1202 inttostr (element_index(ae), lbuf, sizeof (lbuf)),
1203 element_value(ae));
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001204}
1205
1206print_array(a)
1207ARRAY *a;
1208{
1209 printf("\n");
Jari Aaltob80f6442004-07-27 13:29:18 +00001210 array_walk(a, print_element, (void *)NULL);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001211}
1212
1213main()
1214{
1215 ARRAY *a, *new_a, *copy_of_a;
Jari Aalto7117c2d2002-07-17 14:10:11 +00001216 ARRAY_ELEMENT *ae, *aew;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001217 char *s;
1218
Jari Aalto7117c2d2002-07-17 14:10:11 +00001219 a = array_create();
1220 array_insert(a, 1, "one");
1221 array_insert(a, 7, "seven");
1222 array_insert(a, 4, "four");
1223 array_insert(a, 1029, "one thousand twenty-nine");
1224 array_insert(a, 12, "twelve");
1225 array_insert(a, 42, "forty-two");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001226 print_array(a);
Jari Aaltod166f041997-06-05 14:59:13 +00001227 s = array_to_string (a, " ", 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001228 printf("s = %s\n", s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001229 copy_of_a = array_from_string(s, " ");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001230 printf("copy_of_a:");
1231 print_array(copy_of_a);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001232 array_dispose(copy_of_a);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001233 printf("\n");
1234 free(s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001235 ae = array_remove(a, 4);
1236 array_dispose_element(ae);
1237 ae = array_remove(a, 1029);
1238 array_dispose_element(ae);
1239 array_insert(a, 16, "sixteen");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001240 print_array(a);
Jari Aaltod166f041997-06-05 14:59:13 +00001241 s = array_to_string (a, " ", 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001242 printf("s = %s\n", s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001243 copy_of_a = array_from_string(s, " ");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001244 printf("copy_of_a:");
1245 print_array(copy_of_a);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001246 array_dispose(copy_of_a);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001247 printf("\n");
1248 free(s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001249 array_insert(a, 2, "two");
1250 array_insert(a, 1029, "new one thousand twenty-nine");
1251 array_insert(a, 0, "zero");
1252 array_insert(a, 134, "");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001253 print_array(a);
Jari Aaltod166f041997-06-05 14:59:13 +00001254 s = array_to_string (a, ":", 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001255 printf("s = %s\n", s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001256 copy_of_a = array_from_string(s, ":");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001257 printf("copy_of_a:");
1258 print_array(copy_of_a);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001259 array_dispose(copy_of_a);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001260 printf("\n");
1261 free(s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001262 new_a = array_copy(a);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001263 print_array(new_a);
Jari Aaltod166f041997-06-05 14:59:13 +00001264 s = array_to_string (new_a, ":", 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001265 printf("s = %s\n", s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001266 copy_of_a = array_from_string(s, ":");
1267 free(s);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001268 printf("copy_of_a:");
1269 print_array(copy_of_a);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001270 array_shift(copy_of_a, 2, AS_DISPOSE);
1271 printf("copy_of_a shifted by two:");
1272 print_array(copy_of_a);
1273 ae = array_shift(copy_of_a, 2, 0);
1274 printf("copy_of_a shifted by two:");
1275 print_array(copy_of_a);
1276 for ( ; ae; ) {
1277 aew = element_forw(ae);
1278 array_dispose_element(ae);
1279 ae = aew;
1280 }
1281 array_rshift(copy_of_a, 1, (char *)0);
1282 printf("copy_of_a rshift by 1:");
1283 print_array(copy_of_a);
1284 array_rshift(copy_of_a, 2, "new element zero");
1285 printf("copy_of_a rshift again by 2 with new element zero:");
1286 print_array(copy_of_a);
1287 s = array_to_assign(copy_of_a, 0);
1288 printf("copy_of_a=%s\n", s);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001289 free(s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001290 ae = array_shift(copy_of_a, array_num_elements(copy_of_a), 0);
1291 for ( ; ae; ) {
1292 aew = element_forw(ae);
1293 array_dispose_element(ae);
1294 ae = aew;
1295 }
1296 array_dispose(copy_of_a);
1297 printf("\n");
1298 array_dispose(a);
1299 array_dispose(new_a);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001300}
1301
1302#endif /* TEST_ARRAY */
1303#endif /* ARRAY_VARS */