blob: 6d3554bf4cc5c4ef303fe402fe0dc3ea442b1c83 [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 Ramey8868eda2020-12-06 15:51:17 -050012/* Copyright (C) 1997-2020 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->type = array_indexed;
88 r->max_index = -1;
89 r->num_elements = 0;
Chet Rameyd233b482019-01-07 09:27:52 -050090 r->lastref = (ARRAY_ELEMENT *)0;
Jari Aalto7117c2d2002-07-17 14:10:11 +000091 head = array_create_element(-1, (char *)NULL); /* dummy head */
92 head->prev = head->next = head;
93 r->head = head;
94 return(r);
95}
96
97void
98array_flush (a)
99ARRAY *a;
100{
101 register ARRAY_ELEMENT *r, *r1;
102
103 if (a == 0)
104 return;
105 for (r = element_forw(a->head); r != a->head; ) {
106 r1 = element_forw(r);
107 array_dispose_element(r);
108 r = r1;
109 }
110 a->head->next = a->head->prev = a->head;
111 a->max_index = -1;
112 a->num_elements = 0;
Chet Ramey00018032011-11-21 20:51:19 -0500113 INVALIDATE_LASTREF(a);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000114}
115
116void
117array_dispose(a)
118ARRAY *a;
119{
120 if (a == 0)
121 return;
122 array_flush (a);
123 array_dispose_element(a->head);
124 free(a);
125}
126
127ARRAY *
128array_copy(a)
129ARRAY *a;
130{
131 ARRAY *a1;
132 ARRAY_ELEMENT *ae, *new;
133
Jari Aalto95732b42005-12-07 14:08:12 +0000134 if (a == 0)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000135 return((ARRAY *) NULL);
136 a1 = array_create();
137 a1->type = a->type;
138 a1->max_index = a->max_index;
139 a1->num_elements = a->num_elements;
140 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
141 new = array_create_element(element_index(ae), element_value(ae));
142 ADD_BEFORE(a1->head, new);
Chet Rameyd233b482019-01-07 09:27:52 -0500143 if (ae == LASTREF(a))
144 SET_LASTREF(a1, new);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000145 }
146 return(a1);
147}
148
Jari Aalto7117c2d2002-07-17 14:10:11 +0000149/*
150 * Make and return a new array composed of the elements in array A from
151 * S to E, inclusive.
152 */
153ARRAY *
154array_slice(array, s, e)
155ARRAY *array;
156ARRAY_ELEMENT *s, *e;
157{
158 ARRAY *a;
159 ARRAY_ELEMENT *p, *n;
160 int i;
161 arrayind_t mi;
162
163 a = array_create ();
164 a->type = array->type;
165
Jari Aalto31859422009-01-12 13:36:28 +0000166 for (mi = 0, p = s, i = 0; p != e; p = element_forw(p), i++) {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000167 n = array_create_element (element_index(p), element_value(p));
168 ADD_BEFORE(a->head, n);
Jari Aaltof1be6662008-11-18 13:15:12 +0000169 mi = element_index(n);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000170 }
171 a->num_elements = i;
172 a->max_index = mi;
173 return a;
174}
Jari Aalto7117c2d2002-07-17 14:10:11 +0000175
176/*
177 * Walk the array, calling FUNC once for each element, with the array
178 * element as the argument.
179 */
180void
Jari Aaltob80f6442004-07-27 13:29:18 +0000181array_walk(a, func, udata)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000182ARRAY *a;
183sh_ae_map_func_t *func;
Jari Aaltob80f6442004-07-27 13:29:18 +0000184void *udata;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000185{
186 register ARRAY_ELEMENT *ae;
187
188 if (a == 0 || array_empty(a))
189 return;
190 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae))
Jari Aaltob80f6442004-07-27 13:29:18 +0000191 if ((*func)(ae, udata) < 0)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000192 return;
193}
194
195/*
196 * Shift the array A N elements to the left. Delete the first N elements
197 * and subtract N from the indices of the remaining elements. If FLAGS
198 * does not include AS_DISPOSE, this returns a singly-linked null-terminated
199 * list of elements so the caller can dispose of the chain. If FLAGS
200 * includes AS_DISPOSE, this function disposes of the shifted-out elements
201 * and returns NULL.
202 */
203ARRAY_ELEMENT *
204array_shift(a, n, flags)
205ARRAY *a;
206int n, flags;
207{
208 register ARRAY_ELEMENT *ae, *ret;
209 register int i;
210
211 if (a == 0 || array_empty(a) || n <= 0)
212 return ((ARRAY_ELEMENT *)NULL);
213
Chet Ramey00018032011-11-21 20:51:19 -0500214 INVALIDATE_LASTREF(a);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000215 for (i = 0, ret = ae = element_forw(a->head); ae != a->head && i < n; ae = element_forw(ae), i++)
216 ;
217 if (ae == a->head) {
218 /* Easy case; shifting out all of the elements */
219 if (flags & AS_DISPOSE) {
220 array_flush (a);
221 return ((ARRAY_ELEMENT *)NULL);
222 }
223 for (ae = ret; element_forw(ae) != a->head; ae = element_forw(ae))
224 ;
225 element_forw(ae) = (ARRAY_ELEMENT *)NULL;
226 a->head->next = a->head->prev = a->head;
227 a->max_index = -1;
228 a->num_elements = 0;
229 return ret;
230 }
231 /*
232 * ae now points to the list of elements we want to retain.
233 * ret points to the list we want to either destroy or return.
234 */
235 ae->prev->next = (ARRAY_ELEMENT *)NULL; /* null-terminate RET */
236
237 a->head->next = ae; /* slice RET out of the array */
238 ae->prev = a->head;
239
240 for ( ; ae != a->head; ae = element_forw(ae))
241 element_index(ae) -= n; /* renumber retained indices */
242
243 a->num_elements -= n; /* modify bookkeeping information */
Chet Ramey00018032011-11-21 20:51:19 -0500244 a->max_index = element_index(a->head->prev);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000245
246 if (flags & AS_DISPOSE) {
247 for (ae = ret; ae; ) {
248 ret = element_forw(ae);
249 array_dispose_element(ae);
250 ae = ret;
251 }
252 return ((ARRAY_ELEMENT *)NULL);
253 }
254
255 return ret;
256}
257
258/*
259 * Shift array A right N indices. If S is non-null, it becomes the value of
260 * the new element 0. Returns the number of elements in the array after the
261 * shift.
262 */
263int
264array_rshift (a, n, s)
265ARRAY *a;
266int n;
267char *s;
268{
269 register ARRAY_ELEMENT *ae, *new;
270
Jari Aalto95732b42005-12-07 14:08:12 +0000271 if (a == 0 || (array_empty(a) && s == 0))
Jari Aalto7117c2d2002-07-17 14:10:11 +0000272 return 0;
Jari Aalto95732b42005-12-07 14:08:12 +0000273 else if (n <= 0)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000274 return (a->num_elements);
275
276 ae = element_forw(a->head);
277 if (s) {
278 new = array_create_element(0, s);
279 ADD_BEFORE(ae, new);
280 a->num_elements++;
Chet Ramey00018032011-11-21 20:51:19 -0500281 if (array_num_elements(a) == 1) { /* array was empty */
282 a->max_index = 0;
Jari Aalto95732b42005-12-07 14:08:12 +0000283 return 1;
Chet Ramey00018032011-11-21 20:51:19 -0500284 }
Jari Aalto7117c2d2002-07-17 14:10:11 +0000285 }
286
Jari Aalto7117c2d2002-07-17 14:10:11 +0000287 /*
288 * Renumber all elements in the array except the one we just added.
289 */
290 for ( ; ae != a->head; ae = element_forw(ae))
291 element_index(ae) += n;
292
Jari Aalto95732b42005-12-07 14:08:12 +0000293 a->max_index = element_index(a->head->prev);
294
Chet Ramey00018032011-11-21 20:51:19 -0500295 INVALIDATE_LASTREF(a);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000296 return (a->num_elements);
297}
298
Jari Aaltob80f6442004-07-27 13:29:18 +0000299ARRAY_ELEMENT *
300array_unshift_element(a)
301ARRAY *a;
302{
303 return (array_shift (a, 1, 0));
304}
305
306int
307array_shift_element(a, v)
308ARRAY *a;
309char *v;
310{
311 return (array_rshift (a, 1, v));
312}
313
Jari Aalto31859422009-01-12 13:36:28 +0000314ARRAY *
Jari Aalto7117c2d2002-07-17 14:10:11 +0000315array_quote(array)
316ARRAY *array;
317{
318 ARRAY_ELEMENT *a;
319 char *t;
320
Jari Aalto95732b42005-12-07 14:08:12 +0000321 if (array == 0 || array_head(array) == 0 || array_empty(array))
Jari Aalto7117c2d2002-07-17 14:10:11 +0000322 return (ARRAY *)NULL;
323 for (a = element_forw(array->head); a != array->head; a = element_forw(a)) {
324 t = quote_string (a->value);
325 FREE(a->value);
326 a->value = t;
327 }
328 return array;
329}
330
Jari Aalto31859422009-01-12 13:36:28 +0000331ARRAY *
Jari Aaltof1be6662008-11-18 13:15:12 +0000332array_quote_escapes(array)
333ARRAY *array;
334{
335 ARRAY_ELEMENT *a;
336 char *t;
337
338 if (array == 0 || array_head(array) == 0 || array_empty(array))
339 return (ARRAY *)NULL;
340 for (a = element_forw(array->head); a != array->head; a = element_forw(a)) {
341 t = quote_escapes (a->value);
342 FREE(a->value);
343 a->value = t;
344 }
345 return array;
346}
347
Jari Aalto31859422009-01-12 13:36:28 +0000348ARRAY *
349array_dequote(array)
350ARRAY *array;
351{
352 ARRAY_ELEMENT *a;
353 char *t;
354
355 if (array == 0 || array_head(array) == 0 || array_empty(array))
356 return (ARRAY *)NULL;
357 for (a = element_forw(array->head); a != array->head; a = element_forw(a)) {
358 t = dequote_string (a->value);
359 FREE(a->value);
360 a->value = t;
361 }
362 return array;
363}
364
365ARRAY *
366array_dequote_escapes(array)
367ARRAY *array;
368{
369 ARRAY_ELEMENT *a;
370 char *t;
371
372 if (array == 0 || array_head(array) == 0 || array_empty(array))
373 return (ARRAY *)NULL;
374 for (a = element_forw(array->head); a != array->head; a = element_forw(a)) {
375 t = dequote_escapes (a->value);
376 FREE(a->value);
377 a->value = t;
378 }
379 return array;
380}
381
382ARRAY *
383array_remove_quoted_nulls(array)
384ARRAY *array;
385{
386 ARRAY_ELEMENT *a;
Jari Aalto31859422009-01-12 13:36:28 +0000387
388 if (array == 0 || array_head(array) == 0 || array_empty(array))
389 return (ARRAY *)NULL;
390 for (a = element_forw(array->head); a != array->head; a = element_forw(a))
391 a->value = remove_quoted_nulls (a->value);
392 return array;
393}
394
Jari Aaltob80f6442004-07-27 13:29:18 +0000395/*
396 * Return a string whose elements are the members of array A beginning at
397 * index START and spanning NELEM members. Null elements are counted.
398 * Since arrays are sparse, unset array elements are not counted.
399 */
Jari Aalto7117c2d2002-07-17 14:10:11 +0000400char *
Chet Ramey8868eda2020-12-06 15:51:17 -0500401array_subrange (a, start, nelem, starsub, quoted, pflags)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000402ARRAY *a;
Jari Aaltob80f6442004-07-27 13:29:18 +0000403arrayind_t start, nelem;
Chet Ramey8868eda2020-12-06 15:51:17 -0500404int starsub, quoted, pflags;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000405{
Jari Aaltof1be6662008-11-18 13:15:12 +0000406 ARRAY *a2;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000407 ARRAY_ELEMENT *h, *p;
408 arrayind_t i;
Chet Rameyd233b482019-01-07 09:27:52 -0500409 char *t;
410 WORD_LIST *wl;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000411
Jari Aalto95732b42005-12-07 14:08:12 +0000412 p = a ? array_head (a) : 0;
Jari Aaltob80f6442004-07-27 13:29:18 +0000413 if (p == 0 || array_empty (a) || start > array_max_index(a))
Jari Aalto7117c2d2002-07-17 14:10:11 +0000414 return ((char *)NULL);
415
Jari Aaltob80f6442004-07-27 13:29:18 +0000416 /*
417 * Find element with index START. If START corresponds to an unset
418 * element (arrays can be sparse), use the first element whose index
419 * is >= START. If START is < 0, we count START indices back from
420 * the end of A (not elements, even with sparse arrays -- START is an
421 * index).
422 */
423 for (p = element_forw(p); p != array_head(a) && start > element_index(p); p = element_forw(p))
Jari Aalto7117c2d2002-07-17 14:10:11 +0000424 ;
Jari Aaltob80f6442004-07-27 13:29:18 +0000425
Jari Aalto7117c2d2002-07-17 14:10:11 +0000426 if (p == a->head)
427 return ((char *)NULL);
Jari Aaltob80f6442004-07-27 13:29:18 +0000428
429 /* Starting at P, take NELEM elements, inclusive. */
430 for (i = 0, h = p; p != a->head && i < nelem; i++, p = element_forw(p))
Jari Aalto7117c2d2002-07-17 14:10:11 +0000431 ;
432
Jari Aaltof1be6662008-11-18 13:15:12 +0000433 a2 = array_slice(a, h, p);
434
Chet Rameyd233b482019-01-07 09:27:52 -0500435 wl = array_to_word_list(a2);
Jari Aaltof1be6662008-11-18 13:15:12 +0000436 array_dispose(a2);
Chet Rameyd233b482019-01-07 09:27:52 -0500437 if (wl == 0)
438 return (char *)NULL;
Chet Ramey8868eda2020-12-06 15:51:17 -0500439 t = string_list_pos_params(starsub ? '*' : '@', wl, quoted, pflags); /* XXX */
Chet Rameyd233b482019-01-07 09:27:52 -0500440 dispose_words(wl);
Jari Aaltof1be6662008-11-18 13:15:12 +0000441
442 return t;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000443}
444
445char *
446array_patsub (a, pat, rep, mflags)
447ARRAY *a;
448char *pat, *rep;
449int mflags;
450{
Chet Rameyd233b482019-01-07 09:27:52 -0500451 char *t;
Chet Ramey8868eda2020-12-06 15:51:17 -0500452 int pchar, qflags, pflags;
Chet Rameyd233b482019-01-07 09:27:52 -0500453 WORD_LIST *wl, *save;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000454
Jari Aalto95732b42005-12-07 14:08:12 +0000455 if (a == 0 || array_head(a) == 0 || array_empty(a))
Jari Aalto7117c2d2002-07-17 14:10:11 +0000456 return ((char *)NULL);
457
Chet Rameyd233b482019-01-07 09:27:52 -0500458 wl = array_to_word_list(a);
459 if (wl == 0)
460 return (char *)NULL;
461
462 for (save = wl; wl; wl = wl->next) {
463 t = pat_subst (wl->word->word, pat, rep, mflags);
464 FREE (wl->word->word);
465 wl->word->word = t;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000466 }
467
Chet Rameyd233b482019-01-07 09:27:52 -0500468 pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@';
469 qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0;
Chet Ramey8868eda2020-12-06 15:51:17 -0500470 pflags = (mflags & MATCH_ASSIGNRHS) ? PF_ASSIGNRHS : 0;
Jari Aalto31859422009-01-12 13:36:28 +0000471
Chet Ramey8868eda2020-12-06 15:51:17 -0500472 t = string_list_pos_params (pchar, save, qflags, pflags);
Chet Rameyd233b482019-01-07 09:27:52 -0500473 dispose_words(save);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000474
475 return t;
476}
477
Jari Aalto31859422009-01-12 13:36:28 +0000478char *
479array_modcase (a, pat, modop, mflags)
480ARRAY *a;
481char *pat;
482int modop;
483int mflags;
484{
Chet Rameyd233b482019-01-07 09:27:52 -0500485 char *t;
Chet Ramey8868eda2020-12-06 15:51:17 -0500486 int pchar, qflags, pflags;
Chet Rameyd233b482019-01-07 09:27:52 -0500487 WORD_LIST *wl, *save;
Jari Aalto31859422009-01-12 13:36:28 +0000488
489 if (a == 0 || array_head(a) == 0 || array_empty(a))
490 return ((char *)NULL);
491
Chet Rameyd233b482019-01-07 09:27:52 -0500492 wl = array_to_word_list(a);
493 if (wl == 0)
494 return ((char *)NULL);
495
496 for (save = wl; wl; wl = wl->next) {
497 t = sh_modcase(wl->word->word, pat, modop);
498 FREE(wl->word->word);
499 wl->word->word = t;
Jari Aalto31859422009-01-12 13:36:28 +0000500 }
501
Chet Rameyd233b482019-01-07 09:27:52 -0500502 pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@';
503 qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0;
Chet Ramey8868eda2020-12-06 15:51:17 -0500504 pflags = (mflags & MATCH_ASSIGNRHS) ? PF_ASSIGNRHS : 0;
Jari Aalto31859422009-01-12 13:36:28 +0000505
Chet Ramey8868eda2020-12-06 15:51:17 -0500506 t = string_list_pos_params (pchar, save, qflags, pflags);
Chet Rameyd233b482019-01-07 09:27:52 -0500507 dispose_words(save);
Jari Aalto31859422009-01-12 13:36:28 +0000508
509 return t;
510}
Chet Rameyd233b482019-01-07 09:27:52 -0500511
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000512/*
513 * Allocate and return a new array element with index INDEX and value
514 * VALUE.
515 */
516ARRAY_ELEMENT *
Jari Aalto7117c2d2002-07-17 14:10:11 +0000517array_create_element(indx, value)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000518arrayind_t indx;
519char *value;
520{
521 ARRAY_ELEMENT *r;
522
Jari Aaltof73dda02001-11-13 17:56:06 +0000523 r = (ARRAY_ELEMENT *)xmalloc(sizeof(ARRAY_ELEMENT));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000524 r->ind = indx;
525 r->value = value ? savestring(value) : (char *)NULL;
526 r->next = r->prev = (ARRAY_ELEMENT *) NULL;
527 return(r);
528}
529
Jari Aalto7117c2d2002-07-17 14:10:11 +0000530#ifdef INCLUDE_UNUSED
531ARRAY_ELEMENT *
532array_copy_element(ae)
533ARRAY_ELEMENT *ae;
534{
535 return(ae ? array_create_element(element_index(ae), element_value(ae))
536 : (ARRAY_ELEMENT *) NULL);
537}
538#endif
539
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000540void
Jari Aalto7117c2d2002-07-17 14:10:11 +0000541array_dispose_element(ae)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000542ARRAY_ELEMENT *ae;
543{
Jari Aaltob80f6442004-07-27 13:29:18 +0000544 if (ae) {
545 FREE(ae->value);
546 free(ae);
547 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000548}
549
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000550/*
551 * Add a new element with index I and value V to array A (a[i] = v).
552 */
553int
Jari Aalto7117c2d2002-07-17 14:10:11 +0000554array_insert(a, i, v)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000555ARRAY *a;
556arrayind_t i;
557char *v;
558{
Chet Rameyac50fba2014-02-26 09:36:43 -0500559 register ARRAY_ELEMENT *new, *ae, *start;
Chet Rameyd233b482019-01-07 09:27:52 -0500560 arrayind_t startind;
561 int direction;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000562
Jari Aalto95732b42005-12-07 14:08:12 +0000563 if (a == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000564 return(-1);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000565 new = array_create_element(i, v);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000566 if (i > array_max_index(a)) {
567 /*
568 * Hook onto the end. This also works for an empty array.
569 * Fast path for the common case of allocating arrays
570 * sequentially.
571 */
572 ADD_BEFORE(a->head, new);
573 a->max_index = i;
574 a->num_elements++;
Chet Ramey00018032011-11-21 20:51:19 -0500575 SET_LASTREF(a, new);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000576 return(0);
Chet Rameyd233b482019-01-07 09:27:52 -0500577 } else if (i < array_first_index(a)) {
578 /* Hook at the beginning */
579 ADD_AFTER(a->head, new);
580 a->num_elements++;
581 SET_LASTREF(a, new);
582 return(0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000583 }
Chet Rameyac50fba2014-02-26 09:36:43 -0500584#if OPTIMIZE_SEQUENTIAL_ARRAY_ASSIGNMENT
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000585 /*
Chet Rameyac50fba2014-02-26 09:36:43 -0500586 * Otherwise we search for the spot to insert it. The lastref
587 * handle optimizes the case of sequential or almost-sequential
588 * assignments that are not at the end of the array.
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000589 */
Chet Rameyd233b482019-01-07 09:27:52 -0500590 start = LASTREF(a);
591 /* Use same strategy as array_reference to avoid paying large penalty
592 for semi-random assignment pattern. */
593 startind = element_index(start);
594 if (i < startind/2) {
595 start = element_forw(a->head);
596 startind = element_index(start);
597 direction = 1;
598 } else if (i >= startind) {
599 direction = 1;
600 } else {
601 direction = -1;
602 }
Chet Rameyac50fba2014-02-26 09:36:43 -0500603#else
604 start = element_forw(ae->head);
Chet Rameyd233b482019-01-07 09:27:52 -0500605 startind = element_index(start);
606 direction = 1;
Chet Rameyac50fba2014-02-26 09:36:43 -0500607#endif
Chet Rameyd233b482019-01-07 09:27:52 -0500608 for (ae = start; ae != a->head; ) {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000609 if (element_index(ae) == i) {
610 /*
611 * Replacing an existing element.
612 */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000613 free(element_value(ae));
Chet Rameyd233b482019-01-07 09:27:52 -0500614 /* Just swap in the new value */
615 ae->value = new->value;
616 new->value = 0;
617 array_dispose_element(new);
Chet Ramey00018032011-11-21 20:51:19 -0500618 SET_LASTREF(a, ae);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000619 return(0);
Chet Rameyd233b482019-01-07 09:27:52 -0500620 } else if (direction == 1 && element_index(ae) > i) {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000621 ADD_BEFORE(ae, new);
622 a->num_elements++;
Chet Ramey00018032011-11-21 20:51:19 -0500623 SET_LASTREF(a, new);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000624 return(0);
Chet Rameyd233b482019-01-07 09:27:52 -0500625 } else if (direction == -1 && element_index(ae) < i) {
626 ADD_AFTER(ae, new);
627 a->num_elements++;
628 SET_LASTREF(a, new);
629 return(0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000630 }
Chet Rameyd233b482019-01-07 09:27:52 -0500631 ae = direction == 1 ? element_forw(ae) : element_back(ae);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000632 }
Chet Rameyac50fba2014-02-26 09:36:43 -0500633 array_dispose_element(new);
Chet Ramey00018032011-11-21 20:51:19 -0500634 INVALIDATE_LASTREF(a);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000635 return (-1); /* problem */
636}
637
638/*
639 * Delete the element with index I from array A and return it so the
640 * caller can dispose of it.
641 */
642ARRAY_ELEMENT *
Jari Aalto7117c2d2002-07-17 14:10:11 +0000643array_remove(a, i)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000644ARRAY *a;
645arrayind_t i;
646{
Chet Rameyac50fba2014-02-26 09:36:43 -0500647 register ARRAY_ELEMENT *ae, *start;
Chet Rameyd233b482019-01-07 09:27:52 -0500648 arrayind_t startind;
649 int direction;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000650
Jari Aalto95732b42005-12-07 14:08:12 +0000651 if (a == 0 || array_empty(a))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000652 return((ARRAY_ELEMENT *) NULL);
Chet Rameyd233b482019-01-07 09:27:52 -0500653 if (i > array_max_index(a) || i < array_first_index(a))
654 return((ARRAY_ELEMENT *)NULL); /* Keep roving pointer into array to optimize sequential access */
655 start = LASTREF(a);
656 /* Use same strategy as array_reference to avoid paying large penalty
657 for semi-random assignment pattern. */
658 startind = element_index(start);
659 if (i < startind/2) {
660 start = element_forw(a->head);
661 startind = element_index(start);
662 direction = 1;
663 } else if (i >= startind) {
664 direction = 1;
665 } else {
666 direction = -1;
667 }
668 for (ae = start; ae != a->head; ) {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000669 if (element_index(ae) == i) {
670 ae->next->prev = ae->prev;
671 ae->prev->next = ae->next;
672 a->num_elements--;
673 if (i == array_max_index(a))
674 a->max_index = element_index(ae->prev);
Chet Rameyac50fba2014-02-26 09:36:43 -0500675#if 0
Chet Ramey00018032011-11-21 20:51:19 -0500676 INVALIDATE_LASTREF(a);
Chet Rameyac50fba2014-02-26 09:36:43 -0500677#else
678 if (ae->next != a->head)
679 SET_LASTREF(a, ae->next);
680 else if (ae->prev != a->head)
681 SET_LASTREF(a, ae->prev);
682 else
683 INVALIDATE_LASTREF(a);
684#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000685 return(ae);
686 }
Chet Rameyd233b482019-01-07 09:27:52 -0500687 ae = (direction == 1) ? element_forw(ae) : element_back(ae);
688 if (direction == 1 && element_index(ae) > i)
689 break;
690 else if (direction == -1 && element_index(ae) < i)
691 break;
692 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000693 return((ARRAY_ELEMENT *) NULL);
694}
695
696/*
697 * Return the value of a[i].
698 */
699char *
700array_reference(a, i)
701ARRAY *a;
702arrayind_t i;
703{
Chet Rameyac50fba2014-02-26 09:36:43 -0500704 register ARRAY_ELEMENT *ae, *start;
Chet Rameyd233b482019-01-07 09:27:52 -0500705 arrayind_t startind;
706 int direction;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000707
708 if (a == 0 || array_empty(a))
709 return((char *) NULL);
Chet Rameyd233b482019-01-07 09:27:52 -0500710 if (i > array_max_index(a) || i < array_first_index(a))
Chet Rameyac50fba2014-02-26 09:36:43 -0500711 return((char *)NULL); /* Keep roving pointer into array to optimize sequential access */
Chet Rameyd233b482019-01-07 09:27:52 -0500712 start = LASTREF(a); /* lastref pointer */
713 startind = element_index(start);
714 if (i < startind/2) { /* XXX - guess */
715 start = element_forw(a->head);
716 startind = element_index(start);
717 direction = 1;
718 } else if (i >= startind) {
719 direction = 1;
720 } else {
721 direction = -1;
722 }
723 for (ae = start; ae != a->head; ) {
Chet Ramey00018032011-11-21 20:51:19 -0500724 if (element_index(ae) == i) {
725 SET_LASTREF(a, ae);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000726 return(element_value(ae));
Chet Ramey00018032011-11-21 20:51:19 -0500727 }
Chet Rameyd233b482019-01-07 09:27:52 -0500728 ae = (direction == 1) ? element_forw(ae) : element_back(ae);
729 /* Take advantage of index ordering to short-circuit */
730 /* If we don't find it, set the lastref pointer to the element
731 that's `closest', assuming that the unsuccessful reference
732 will quickly be followed by an assignment. No worse than
733 not changing it from the previous value or resetting it. */
734 if (direction == 1 && element_index(ae) > i) {
735 start = ae; /* use for SET_LASTREF below */
736 break;
737 } else if (direction == -1 && element_index(ae) < i) {
738 start = ae; /* use for SET_LASTREF below */
739 break;
740 }
741 }
742#if 0
743 UNSET_LASTREF(a);
744#else
745 SET_LASTREF(a, start);
746#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000747 return((char *) NULL);
748}
749
Jari Aalto7117c2d2002-07-17 14:10:11 +0000750/* Convenience routines for the shell to translate to and from the form used
751 by the rest of the code. */
Jari Aaltob80f6442004-07-27 13:29:18 +0000752
Jari Aalto7117c2d2002-07-17 14:10:11 +0000753WORD_LIST *
754array_to_word_list(a)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000755ARRAY *a;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000756{
Jari Aalto7117c2d2002-07-17 14:10:11 +0000757 WORD_LIST *list;
758 ARRAY_ELEMENT *ae;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000759
760 if (a == 0 || array_empty(a))
Jari Aalto7117c2d2002-07-17 14:10:11 +0000761 return((WORD_LIST *)NULL);
762 list = (WORD_LIST *)NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000763 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae))
Jari Aalto7117c2d2002-07-17 14:10:11 +0000764 list = make_word_list (make_bare_word(element_value(ae)), list);
765 return (REVERSE_LIST(list, WORD_LIST *));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000766}
767
Jari Aalto7117c2d2002-07-17 14:10:11 +0000768ARRAY *
769array_from_word_list (list)
770WORD_LIST *list;
771{
772 ARRAY *a;
773
774 if (list == 0)
775 return((ARRAY *)NULL);
776 a = array_create();
777 return (array_assign_list (a, list));
778}
779
Jari Aaltob80f6442004-07-27 13:29:18 +0000780WORD_LIST *
781array_keys_to_word_list(a)
782ARRAY *a;
783{
784 WORD_LIST *list;
785 ARRAY_ELEMENT *ae;
786 char *t;
787
788 if (a == 0 || array_empty(a))
789 return((WORD_LIST *)NULL);
790 list = (WORD_LIST *)NULL;
791 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
792 t = itos(element_index(ae));
793 list = make_word_list (make_bare_word(t), list);
794 free(t);
795 }
796 return (REVERSE_LIST(list, WORD_LIST *));
797}
798
Jari Aalto7117c2d2002-07-17 14:10:11 +0000799ARRAY *
800array_assign_list (array, list)
801ARRAY *array;
802WORD_LIST *list;
803{
804 register WORD_LIST *l;
805 register arrayind_t i;
806
807 for (l = list, i = 0; l; l = l->next, i++)
808 array_insert(array, i, l->word->word);
809 return array;
810}
811
812char **
Chet Ramey8868eda2020-12-06 15:51:17 -0500813array_to_argv (a, countp)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000814ARRAY *a;
Chet Ramey8868eda2020-12-06 15:51:17 -0500815int *countp;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000816{
817 char **ret, *t;
818 int i;
819 ARRAY_ELEMENT *ae;
820
Chet Ramey8868eda2020-12-06 15:51:17 -0500821 if (a == 0 || array_empty(a)) {
822 if (countp)
823 *countp = 0;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000824 return ((char **)NULL);
Chet Ramey8868eda2020-12-06 15:51:17 -0500825 }
Jari Aalto7117c2d2002-07-17 14:10:11 +0000826 ret = strvec_create (array_num_elements (a) + 1);
827 i = 0;
828 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
829 t = element_value (ae);
Chet Ramey8868eda2020-12-06 15:51:17 -0500830 if (t)
831 ret[i++] = savestring (t);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000832 }
833 ret[i] = (char *)NULL;
Chet Ramey8868eda2020-12-06 15:51:17 -0500834 if (countp)
835 *countp = i;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000836 return (ret);
837}
838
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000839/*
Jari Aalto31859422009-01-12 13:36:28 +0000840 * Return a string that is the concatenation of the elements in A from START
841 * to END, separated by SEP.
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000842 */
843static char *
844array_to_string_internal (start, end, sep, quoted)
845ARRAY_ELEMENT *start, *end;
846char *sep;
847int quoted;
848{
849 char *result, *t;
850 ARRAY_ELEMENT *ae;
851 int slen, rsize, rlen, reg;
852
853 if (start == end) /* XXX - should not happen */
854 return ((char *)NULL);
855
856 slen = strlen(sep);
Jari Aaltof73dda02001-11-13 17:56:06 +0000857 result = NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000858 for (rsize = rlen = 0, ae = start; ae != end; ae = element_forw(ae)) {
859 if (rsize == 0)
Jari Aaltof73dda02001-11-13 17:56:06 +0000860 result = (char *)xmalloc (rsize = 64);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000861 if (element_value(ae)) {
862 t = quoted ? quote_string(element_value(ae)) : element_value(ae);
863 reg = strlen(t);
864 RESIZE_MALLOCED_BUFFER (result, rlen, (reg + slen + 2),
865 rsize, rsize);
866 strcpy(result + rlen, t);
867 rlen += reg;
Chet Rameya0c0a002016-09-15 16:59:08 -0400868 if (quoted)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000869 free(t);
870 /*
871 * Add a separator only after non-null elements.
872 */
873 if (element_forw(ae) != end) {
874 strcpy(result + rlen, sep);
875 rlen += slen;
876 }
877 }
878 }
Jari Aaltof73dda02001-11-13 17:56:06 +0000879 if (result)
880 result[rlen] = '\0'; /* XXX */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000881 return(result);
882}
883
884char *
Chet Ramey8868eda2020-12-06 15:51:17 -0500885array_to_kvpair (a, quoted)
886ARRAY *a;
887int quoted;
888{
889 char *result, *valstr, *is;
890 char indstr[INT_STRLEN_BOUND(intmax_t) + 1];
891 ARRAY_ELEMENT *ae;
892 int rsize, rlen, elen;
893
894 if (a == 0 || array_empty (a))
895 return((char *)NULL);
896
897 result = (char *)xmalloc (rsize = 128);
898 result[rlen = 0] = '\0';
899
900 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
901 is = inttostr (element_index(ae), indstr, sizeof(indstr));
902 valstr = element_value (ae) ?
903 (ansic_shouldquote (element_value (ae)) ?
904 ansic_quote (element_value(ae), 0, (int *)0) :
905 sh_double_quote (element_value (ae)))
906 : (char *)NULL;
907 elen = STRLEN (is) + 8 + STRLEN (valstr);
908 RESIZE_MALLOCED_BUFFER (result, rlen, (elen + 1), rsize, rsize);
909
910 strcpy (result + rlen, is);
911 rlen += STRLEN (is);
912 result[rlen++] = ' ';
913 if (valstr) {
914 strcpy (result + rlen, valstr);
915 rlen += STRLEN (valstr);
916 } else {
917 strcpy (result + rlen, "\"\"");
918 rlen += 2;
919 }
920
921 if (element_forw(ae) != a->head)
922 result[rlen++] = ' ';
923
924 FREE (valstr);
925 }
926 RESIZE_MALLOCED_BUFFER (result, rlen, 1, rsize, 8);
927 result[rlen] = '\0';
928
929 if (quoted) {
930 /* This is not as efficient as it could be... */
931 valstr = sh_single_quote (result);
932 free (result);
933 result = valstr;
934 }
935 return(result);
936}
937
938char *
Jari Aalto7117c2d2002-07-17 14:10:11 +0000939array_to_assign (a, quoted)
940ARRAY *a;
941int quoted;
942{
943 char *result, *valstr, *is;
944 char indstr[INT_STRLEN_BOUND(intmax_t) + 1];
945 ARRAY_ELEMENT *ae;
946 int rsize, rlen, elen;
947
948 if (a == 0 || array_empty (a))
949 return((char *)NULL);
950
951 result = (char *)xmalloc (rsize = 128);
952 result[0] = '(';
953 rlen = 1;
954
955 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
956 is = inttostr (element_index(ae), indstr, sizeof(indstr));
Chet Rameya0c0a002016-09-15 16:59:08 -0400957 valstr = element_value (ae) ?
958 (ansic_shouldquote (element_value (ae)) ?
959 ansic_quote (element_value(ae), 0, (int *)0) :
960 sh_double_quote (element_value (ae)))
Jari Aalto7117c2d2002-07-17 14:10:11 +0000961 : (char *)NULL;
Jari Aaltof1be6662008-11-18 13:15:12 +0000962 elen = STRLEN (is) + 8 + STRLEN (valstr);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000963 RESIZE_MALLOCED_BUFFER (result, rlen, (elen + 1), rsize, rsize);
964
965 result[rlen++] = '[';
966 strcpy (result + rlen, is);
967 rlen += STRLEN (is);
968 result[rlen++] = ']';
969 result[rlen++] = '=';
970 if (valstr) {
971 strcpy (result + rlen, valstr);
972 rlen += STRLEN (valstr);
973 }
974
975 if (element_forw(ae) != a->head)
976 result[rlen++] = ' ';
977
978 FREE (valstr);
979 }
980 RESIZE_MALLOCED_BUFFER (result, rlen, 1, rsize, 8);
981 result[rlen++] = ')';
982 result[rlen] = '\0';
983 if (quoted) {
984 /* This is not as efficient as it could be... */
985 valstr = sh_single_quote (result);
986 free (result);
987 result = valstr;
988 }
989 return(result);
990}
991
992char *
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000993array_to_string (a, sep, quoted)
994ARRAY *a;
995char *sep;
996int quoted;
997{
998 if (a == 0)
999 return((char *)NULL);
1000 if (array_empty(a))
1001 return(savestring(""));
1002 return (array_to_string_internal (element_forw(a->head), a->head, sep, quoted));
1003}
1004
Jari Aaltod166f041997-06-05 14:59:13 +00001005#if defined (INCLUDE_UNUSED) || defined (TEST_ARRAY)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001006/*
1007 * Return an array consisting of elements in S, separated by SEP
1008 */
1009ARRAY *
Jari Aalto7117c2d2002-07-17 14:10:11 +00001010array_from_string(s, sep)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001011char *s, *sep;
1012{
1013 ARRAY *a;
1014 WORD_LIST *w;
1015
1016 if (s == 0)
1017 return((ARRAY *)NULL);
1018 w = list_string (s, sep, 0);
1019 if (w == 0)
1020 return((ARRAY *)NULL);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001021 a = array_from_word_list (w);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001022 return (a);
1023}
Jari Aaltod166f041997-06-05 14:59:13 +00001024#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001025
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001026#if defined (TEST_ARRAY)
Jari Aalto7117c2d2002-07-17 14:10:11 +00001027/*
1028 * To make a running version, compile -DTEST_ARRAY and link with:
1029 * xmalloc.o syntax.o lib/malloc/libmalloc.a lib/sh/libsh.a
1030 */
1031int interrupt_immediately = 0;
1032
1033int
1034signal_is_trapped(s)
1035int s;
1036{
1037 return 0;
1038}
1039
1040void
1041fatal_error(const char *s, ...)
1042{
1043 fprintf(stderr, "array_test: fatal memory error\n");
1044 abort();
1045}
1046
1047void
1048programming_error(const char *s, ...)
1049{
1050 fprintf(stderr, "array_test: fatal programming error\n");
1051 abort();
1052}
1053
1054WORD_DESC *
1055make_bare_word (s)
1056const char *s;
1057{
1058 WORD_DESC *w;
1059
1060 w = (WORD_DESC *)xmalloc(sizeof(WORD_DESC));
1061 w->word = s ? savestring(s) : savestring ("");
1062 w->flags = 0;
1063 return w;
1064}
1065
1066WORD_LIST *
1067make_word_list(x, l)
1068WORD_DESC *x;
1069WORD_LIST *l;
1070{
1071 WORD_LIST *w;
1072
1073 w = (WORD_LIST *)xmalloc(sizeof(WORD_LIST));
1074 w->word = x;
1075 w->next = l;
1076 return w;
1077}
1078
1079WORD_LIST *
1080list_string(s, t, i)
1081char *s, *t;
1082int i;
1083{
1084 char *r, *a;
1085 WORD_LIST *wl;
1086
1087 if (s == 0)
1088 return (WORD_LIST *)NULL;
1089 r = savestring(s);
1090 wl = (WORD_LIST *)NULL;
1091 a = strtok(r, t);
1092 while (a) {
1093 wl = make_word_list (make_bare_word(a), wl);
1094 a = strtok((char *)NULL, t);
1095 }
1096 return (REVERSE_LIST (wl, WORD_LIST *));
1097}
1098
1099GENERIC_LIST *
1100list_reverse (list)
1101GENERIC_LIST *list;
1102{
1103 register GENERIC_LIST *next, *prev;
1104
1105 for (prev = 0; list; ) {
1106 next = list->next;
1107 list->next = prev;
1108 prev = list;
1109 list = next;
1110 }
1111 return prev;
1112}
1113
1114char *
1115pat_subst(s, t, u, i)
1116char *s, *t, *u;
1117int i;
1118{
1119 return ((char *)NULL);
1120}
1121
1122char *
1123quote_string(s)
1124char *s;
1125{
1126 return savestring(s);
1127}
1128
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001129print_element(ae)
1130ARRAY_ELEMENT *ae;
1131{
Jari Aalto7117c2d2002-07-17 14:10:11 +00001132 char lbuf[INT_STRLEN_BOUND (intmax_t) + 1];
1133
1134 printf("array[%s] = %s\n",
1135 inttostr (element_index(ae), lbuf, sizeof (lbuf)),
1136 element_value(ae));
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001137}
1138
1139print_array(a)
1140ARRAY *a;
1141{
1142 printf("\n");
Jari Aaltob80f6442004-07-27 13:29:18 +00001143 array_walk(a, print_element, (void *)NULL);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001144}
1145
1146main()
1147{
1148 ARRAY *a, *new_a, *copy_of_a;
Jari Aalto7117c2d2002-07-17 14:10:11 +00001149 ARRAY_ELEMENT *ae, *aew;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001150 char *s;
1151
Jari Aalto7117c2d2002-07-17 14:10:11 +00001152 a = array_create();
1153 array_insert(a, 1, "one");
1154 array_insert(a, 7, "seven");
1155 array_insert(a, 4, "four");
1156 array_insert(a, 1029, "one thousand twenty-nine");
1157 array_insert(a, 12, "twelve");
1158 array_insert(a, 42, "forty-two");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001159 print_array(a);
Jari Aaltod166f041997-06-05 14:59:13 +00001160 s = array_to_string (a, " ", 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001161 printf("s = %s\n", s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001162 copy_of_a = array_from_string(s, " ");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001163 printf("copy_of_a:");
1164 print_array(copy_of_a);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001165 array_dispose(copy_of_a);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001166 printf("\n");
1167 free(s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001168 ae = array_remove(a, 4);
1169 array_dispose_element(ae);
1170 ae = array_remove(a, 1029);
1171 array_dispose_element(ae);
1172 array_insert(a, 16, "sixteen");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001173 print_array(a);
Jari Aaltod166f041997-06-05 14:59:13 +00001174 s = array_to_string (a, " ", 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001175 printf("s = %s\n", s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001176 copy_of_a = array_from_string(s, " ");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001177 printf("copy_of_a:");
1178 print_array(copy_of_a);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001179 array_dispose(copy_of_a);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001180 printf("\n");
1181 free(s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001182 array_insert(a, 2, "two");
1183 array_insert(a, 1029, "new one thousand twenty-nine");
1184 array_insert(a, 0, "zero");
1185 array_insert(a, 134, "");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001186 print_array(a);
Jari Aaltod166f041997-06-05 14:59:13 +00001187 s = array_to_string (a, ":", 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001188 printf("s = %s\n", s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001189 copy_of_a = array_from_string(s, ":");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001190 printf("copy_of_a:");
1191 print_array(copy_of_a);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001192 array_dispose(copy_of_a);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001193 printf("\n");
1194 free(s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001195 new_a = array_copy(a);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001196 print_array(new_a);
Jari Aaltod166f041997-06-05 14:59:13 +00001197 s = array_to_string (new_a, ":", 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001198 printf("s = %s\n", s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001199 copy_of_a = array_from_string(s, ":");
1200 free(s);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001201 printf("copy_of_a:");
1202 print_array(copy_of_a);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001203 array_shift(copy_of_a, 2, AS_DISPOSE);
1204 printf("copy_of_a shifted by two:");
1205 print_array(copy_of_a);
1206 ae = array_shift(copy_of_a, 2, 0);
1207 printf("copy_of_a shifted by two:");
1208 print_array(copy_of_a);
1209 for ( ; ae; ) {
1210 aew = element_forw(ae);
1211 array_dispose_element(ae);
1212 ae = aew;
1213 }
1214 array_rshift(copy_of_a, 1, (char *)0);
1215 printf("copy_of_a rshift by 1:");
1216 print_array(copy_of_a);
1217 array_rshift(copy_of_a, 2, "new element zero");
1218 printf("copy_of_a rshift again by 2 with new element zero:");
1219 print_array(copy_of_a);
1220 s = array_to_assign(copy_of_a, 0);
1221 printf("copy_of_a=%s\n", s);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001222 free(s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001223 ae = array_shift(copy_of_a, array_num_elements(copy_of_a), 0);
1224 for ( ; ae; ) {
1225 aew = element_forw(ae);
1226 array_dispose_element(ae);
1227 ae = aew;
1228 }
1229 array_dispose(copy_of_a);
1230 printf("\n");
1231 array_dispose(a);
1232 array_dispose(new_a);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001233}
1234
1235#endif /* TEST_ARRAY */
1236#endif /* ARRAY_VARS */