blob: bca18c5441a11b7f1e4cc9094b3ae7f2361bbeaf [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 Rameyd233b482019-01-07 09:27:52 -050012/* Copyright (C) 1997-2016 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
Jari Aalto7117c2d2002-07-17 14:10:11 +000064static char *array_to_string_internal __P((ARRAY_ELEMENT *, ARRAY_ELEMENT *, char *, int));
65
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 *
Jari Aaltob80f6442004-07-27 13:29:18 +0000401array_subrange (a, start, nelem, starsub, quoted)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000402ARRAY *a;
Jari Aaltob80f6442004-07-27 13:29:18 +0000403arrayind_t start, nelem;
404int starsub, quoted;
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;
439 t = string_list_pos_params(starsub ? '*' : '@', wl, quoted);
440 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;
452 int pchar, qflags;
453 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;
Jari Aalto31859422009-01-12 13:36:28 +0000470
Chet Rameyd233b482019-01-07 09:27:52 -0500471 t = string_list_pos_params (pchar, save, qflags);
472 dispose_words(save);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000473
474 return t;
475}
476
Jari Aalto31859422009-01-12 13:36:28 +0000477char *
478array_modcase (a, pat, modop, mflags)
479ARRAY *a;
480char *pat;
481int modop;
482int mflags;
483{
Chet Rameyd233b482019-01-07 09:27:52 -0500484 char *t;
485 int pchar, qflags;
486 WORD_LIST *wl, *save;
Jari Aalto31859422009-01-12 13:36:28 +0000487
488 if (a == 0 || array_head(a) == 0 || array_empty(a))
489 return ((char *)NULL);
490
Chet Rameyd233b482019-01-07 09:27:52 -0500491 wl = array_to_word_list(a);
492 if (wl == 0)
493 return ((char *)NULL);
494
495 for (save = wl; wl; wl = wl->next) {
496 t = sh_modcase(wl->word->word, pat, modop);
497 FREE(wl->word->word);
498 wl->word->word = t;
Jari Aalto31859422009-01-12 13:36:28 +0000499 }
500
Chet Rameyd233b482019-01-07 09:27:52 -0500501 pchar = (mflags & MATCH_STARSUB) == MATCH_STARSUB ? '*' : '@';
502 qflags = (mflags & MATCH_QUOTED) == MATCH_QUOTED ? Q_DOUBLE_QUOTES : 0;
Jari Aalto31859422009-01-12 13:36:28 +0000503
Chet Rameyd233b482019-01-07 09:27:52 -0500504 t = string_list_pos_params (pchar, save, qflags);
505 dispose_words(save);
Jari Aalto31859422009-01-12 13:36:28 +0000506
507 return t;
508}
Chet Rameyd233b482019-01-07 09:27:52 -0500509
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000510/*
511 * Allocate and return a new array element with index INDEX and value
512 * VALUE.
513 */
514ARRAY_ELEMENT *
Jari Aalto7117c2d2002-07-17 14:10:11 +0000515array_create_element(indx, value)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000516arrayind_t indx;
517char *value;
518{
519 ARRAY_ELEMENT *r;
520
Jari Aaltof73dda02001-11-13 17:56:06 +0000521 r = (ARRAY_ELEMENT *)xmalloc(sizeof(ARRAY_ELEMENT));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000522 r->ind = indx;
523 r->value = value ? savestring(value) : (char *)NULL;
524 r->next = r->prev = (ARRAY_ELEMENT *) NULL;
525 return(r);
526}
527
Jari Aalto7117c2d2002-07-17 14:10:11 +0000528#ifdef INCLUDE_UNUSED
529ARRAY_ELEMENT *
530array_copy_element(ae)
531ARRAY_ELEMENT *ae;
532{
533 return(ae ? array_create_element(element_index(ae), element_value(ae))
534 : (ARRAY_ELEMENT *) NULL);
535}
536#endif
537
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000538void
Jari Aalto7117c2d2002-07-17 14:10:11 +0000539array_dispose_element(ae)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000540ARRAY_ELEMENT *ae;
541{
Jari Aaltob80f6442004-07-27 13:29:18 +0000542 if (ae) {
543 FREE(ae->value);
544 free(ae);
545 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000546}
547
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000548/*
549 * Add a new element with index I and value V to array A (a[i] = v).
550 */
551int
Jari Aalto7117c2d2002-07-17 14:10:11 +0000552array_insert(a, i, v)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000553ARRAY *a;
554arrayind_t i;
555char *v;
556{
Chet Rameyac50fba2014-02-26 09:36:43 -0500557 register ARRAY_ELEMENT *new, *ae, *start;
Chet Rameyd233b482019-01-07 09:27:52 -0500558 arrayind_t startind;
559 int direction;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000560
Jari Aalto95732b42005-12-07 14:08:12 +0000561 if (a == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000562 return(-1);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000563 new = array_create_element(i, v);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000564 if (i > array_max_index(a)) {
565 /*
566 * Hook onto the end. This also works for an empty array.
567 * Fast path for the common case of allocating arrays
568 * sequentially.
569 */
570 ADD_BEFORE(a->head, new);
571 a->max_index = i;
572 a->num_elements++;
Chet Ramey00018032011-11-21 20:51:19 -0500573 SET_LASTREF(a, new);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000574 return(0);
Chet Rameyd233b482019-01-07 09:27:52 -0500575 } else if (i < array_first_index(a)) {
576 /* Hook at the beginning */
577 ADD_AFTER(a->head, new);
578 a->num_elements++;
579 SET_LASTREF(a, new);
580 return(0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000581 }
Chet Rameyac50fba2014-02-26 09:36:43 -0500582#if OPTIMIZE_SEQUENTIAL_ARRAY_ASSIGNMENT
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000583 /*
Chet Rameyac50fba2014-02-26 09:36:43 -0500584 * Otherwise we search for the spot to insert it. The lastref
585 * handle optimizes the case of sequential or almost-sequential
586 * assignments that are not at the end of the array.
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000587 */
Chet Rameyd233b482019-01-07 09:27:52 -0500588 start = LASTREF(a);
589 /* Use same strategy as array_reference to avoid paying large penalty
590 for semi-random assignment pattern. */
591 startind = element_index(start);
592 if (i < startind/2) {
593 start = element_forw(a->head);
594 startind = element_index(start);
595 direction = 1;
596 } else if (i >= startind) {
597 direction = 1;
598 } else {
599 direction = -1;
600 }
Chet Rameyac50fba2014-02-26 09:36:43 -0500601#else
602 start = element_forw(ae->head);
Chet Rameyd233b482019-01-07 09:27:52 -0500603 startind = element_index(start);
604 direction = 1;
Chet Rameyac50fba2014-02-26 09:36:43 -0500605#endif
Chet Rameyd233b482019-01-07 09:27:52 -0500606 for (ae = start; ae != a->head; ) {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000607 if (element_index(ae) == i) {
608 /*
609 * Replacing an existing element.
610 */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000611 free(element_value(ae));
Chet Rameyd233b482019-01-07 09:27:52 -0500612 /* Just swap in the new value */
613 ae->value = new->value;
614 new->value = 0;
615 array_dispose_element(new);
Chet Ramey00018032011-11-21 20:51:19 -0500616 SET_LASTREF(a, ae);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000617 return(0);
Chet Rameyd233b482019-01-07 09:27:52 -0500618 } else if (direction == 1 && element_index(ae) > i) {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000619 ADD_BEFORE(ae, new);
620 a->num_elements++;
Chet Ramey00018032011-11-21 20:51:19 -0500621 SET_LASTREF(a, new);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000622 return(0);
Chet Rameyd233b482019-01-07 09:27:52 -0500623 } else if (direction == -1 && element_index(ae) < i) {
624 ADD_AFTER(ae, new);
625 a->num_elements++;
626 SET_LASTREF(a, new);
627 return(0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000628 }
Chet Rameyd233b482019-01-07 09:27:52 -0500629 ae = direction == 1 ? element_forw(ae) : element_back(ae);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000630 }
Chet Rameyac50fba2014-02-26 09:36:43 -0500631 array_dispose_element(new);
Chet Ramey00018032011-11-21 20:51:19 -0500632 INVALIDATE_LASTREF(a);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000633 return (-1); /* problem */
634}
635
636/*
637 * Delete the element with index I from array A and return it so the
638 * caller can dispose of it.
639 */
640ARRAY_ELEMENT *
Jari Aalto7117c2d2002-07-17 14:10:11 +0000641array_remove(a, i)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000642ARRAY *a;
643arrayind_t i;
644{
Chet Rameyac50fba2014-02-26 09:36:43 -0500645 register ARRAY_ELEMENT *ae, *start;
Chet Rameyd233b482019-01-07 09:27:52 -0500646 arrayind_t startind;
647 int direction;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000648
Jari Aalto95732b42005-12-07 14:08:12 +0000649 if (a == 0 || array_empty(a))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000650 return((ARRAY_ELEMENT *) NULL);
Chet Rameyd233b482019-01-07 09:27:52 -0500651 if (i > array_max_index(a) || i < array_first_index(a))
652 return((ARRAY_ELEMENT *)NULL); /* Keep roving pointer into array to optimize sequential access */
653 start = LASTREF(a);
654 /* Use same strategy as array_reference to avoid paying large penalty
655 for semi-random assignment pattern. */
656 startind = element_index(start);
657 if (i < startind/2) {
658 start = element_forw(a->head);
659 startind = element_index(start);
660 direction = 1;
661 } else if (i >= startind) {
662 direction = 1;
663 } else {
664 direction = -1;
665 }
666 for (ae = start; ae != a->head; ) {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000667 if (element_index(ae) == i) {
668 ae->next->prev = ae->prev;
669 ae->prev->next = ae->next;
670 a->num_elements--;
671 if (i == array_max_index(a))
672 a->max_index = element_index(ae->prev);
Chet Rameyac50fba2014-02-26 09:36:43 -0500673#if 0
Chet Ramey00018032011-11-21 20:51:19 -0500674 INVALIDATE_LASTREF(a);
Chet Rameyac50fba2014-02-26 09:36:43 -0500675#else
676 if (ae->next != a->head)
677 SET_LASTREF(a, ae->next);
678 else if (ae->prev != a->head)
679 SET_LASTREF(a, ae->prev);
680 else
681 INVALIDATE_LASTREF(a);
682#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000683 return(ae);
684 }
Chet Rameyd233b482019-01-07 09:27:52 -0500685 ae = (direction == 1) ? element_forw(ae) : element_back(ae);
686 if (direction == 1 && element_index(ae) > i)
687 break;
688 else if (direction == -1 && element_index(ae) < i)
689 break;
690 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000691 return((ARRAY_ELEMENT *) NULL);
692}
693
694/*
695 * Return the value of a[i].
696 */
697char *
698array_reference(a, i)
699ARRAY *a;
700arrayind_t i;
701{
Chet Rameyac50fba2014-02-26 09:36:43 -0500702 register ARRAY_ELEMENT *ae, *start;
Chet Rameyd233b482019-01-07 09:27:52 -0500703 arrayind_t startind;
704 int direction;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000705
706 if (a == 0 || array_empty(a))
707 return((char *) NULL);
Chet Rameyd233b482019-01-07 09:27:52 -0500708 if (i > array_max_index(a) || i < array_first_index(a))
Chet Rameyac50fba2014-02-26 09:36:43 -0500709 return((char *)NULL); /* Keep roving pointer into array to optimize sequential access */
Chet Rameyd233b482019-01-07 09:27:52 -0500710 start = LASTREF(a); /* lastref pointer */
711 startind = element_index(start);
712 if (i < startind/2) { /* XXX - guess */
713 start = element_forw(a->head);
714 startind = element_index(start);
715 direction = 1;
716 } else if (i >= startind) {
717 direction = 1;
718 } else {
719 direction = -1;
720 }
721 for (ae = start; ae != a->head; ) {
Chet Ramey00018032011-11-21 20:51:19 -0500722 if (element_index(ae) == i) {
723 SET_LASTREF(a, ae);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000724 return(element_value(ae));
Chet Ramey00018032011-11-21 20:51:19 -0500725 }
Chet Rameyd233b482019-01-07 09:27:52 -0500726 ae = (direction == 1) ? element_forw(ae) : element_back(ae);
727 /* Take advantage of index ordering to short-circuit */
728 /* If we don't find it, set the lastref pointer to the element
729 that's `closest', assuming that the unsuccessful reference
730 will quickly be followed by an assignment. No worse than
731 not changing it from the previous value or resetting it. */
732 if (direction == 1 && element_index(ae) > i) {
733 start = ae; /* use for SET_LASTREF below */
734 break;
735 } else if (direction == -1 && element_index(ae) < i) {
736 start = ae; /* use for SET_LASTREF below */
737 break;
738 }
739 }
740#if 0
741 UNSET_LASTREF(a);
742#else
743 SET_LASTREF(a, start);
744#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000745 return((char *) NULL);
746}
747
Jari Aalto7117c2d2002-07-17 14:10:11 +0000748/* Convenience routines for the shell to translate to and from the form used
749 by the rest of the code. */
Jari Aaltob80f6442004-07-27 13:29:18 +0000750
Jari Aalto7117c2d2002-07-17 14:10:11 +0000751WORD_LIST *
752array_to_word_list(a)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000753ARRAY *a;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000754{
Jari Aalto7117c2d2002-07-17 14:10:11 +0000755 WORD_LIST *list;
756 ARRAY_ELEMENT *ae;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000757
758 if (a == 0 || array_empty(a))
Jari Aalto7117c2d2002-07-17 14:10:11 +0000759 return((WORD_LIST *)NULL);
760 list = (WORD_LIST *)NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000761 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae))
Jari Aalto7117c2d2002-07-17 14:10:11 +0000762 list = make_word_list (make_bare_word(element_value(ae)), list);
763 return (REVERSE_LIST(list, WORD_LIST *));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000764}
765
Jari Aalto7117c2d2002-07-17 14:10:11 +0000766ARRAY *
767array_from_word_list (list)
768WORD_LIST *list;
769{
770 ARRAY *a;
771
772 if (list == 0)
773 return((ARRAY *)NULL);
774 a = array_create();
775 return (array_assign_list (a, list));
776}
777
Jari Aaltob80f6442004-07-27 13:29:18 +0000778WORD_LIST *
779array_keys_to_word_list(a)
780ARRAY *a;
781{
782 WORD_LIST *list;
783 ARRAY_ELEMENT *ae;
784 char *t;
785
786 if (a == 0 || array_empty(a))
787 return((WORD_LIST *)NULL);
788 list = (WORD_LIST *)NULL;
789 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
790 t = itos(element_index(ae));
791 list = make_word_list (make_bare_word(t), list);
792 free(t);
793 }
794 return (REVERSE_LIST(list, WORD_LIST *));
795}
796
Jari Aalto7117c2d2002-07-17 14:10:11 +0000797ARRAY *
798array_assign_list (array, list)
799ARRAY *array;
800WORD_LIST *list;
801{
802 register WORD_LIST *l;
803 register arrayind_t i;
804
805 for (l = list, i = 0; l; l = l->next, i++)
806 array_insert(array, i, l->word->word);
807 return array;
808}
809
810char **
811array_to_argv (a)
812ARRAY *a;
813{
814 char **ret, *t;
815 int i;
816 ARRAY_ELEMENT *ae;
817
818 if (a == 0 || array_empty(a))
819 return ((char **)NULL);
820 ret = strvec_create (array_num_elements (a) + 1);
821 i = 0;
822 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
823 t = element_value (ae);
824 ret[i++] = t ? savestring (t) : (char *)NULL;
825 }
826 ret[i] = (char *)NULL;
827 return (ret);
828}
829
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000830/*
Jari Aalto31859422009-01-12 13:36:28 +0000831 * Return a string that is the concatenation of the elements in A from START
832 * to END, separated by SEP.
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000833 */
834static char *
835array_to_string_internal (start, end, sep, quoted)
836ARRAY_ELEMENT *start, *end;
837char *sep;
838int quoted;
839{
840 char *result, *t;
841 ARRAY_ELEMENT *ae;
842 int slen, rsize, rlen, reg;
843
844 if (start == end) /* XXX - should not happen */
845 return ((char *)NULL);
846
847 slen = strlen(sep);
Jari Aaltof73dda02001-11-13 17:56:06 +0000848 result = NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000849 for (rsize = rlen = 0, ae = start; ae != end; ae = element_forw(ae)) {
850 if (rsize == 0)
Jari Aaltof73dda02001-11-13 17:56:06 +0000851 result = (char *)xmalloc (rsize = 64);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000852 if (element_value(ae)) {
853 t = quoted ? quote_string(element_value(ae)) : element_value(ae);
854 reg = strlen(t);
855 RESIZE_MALLOCED_BUFFER (result, rlen, (reg + slen + 2),
856 rsize, rsize);
857 strcpy(result + rlen, t);
858 rlen += reg;
Chet Rameya0c0a002016-09-15 16:59:08 -0400859 if (quoted)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000860 free(t);
861 /*
862 * Add a separator only after non-null elements.
863 */
864 if (element_forw(ae) != end) {
865 strcpy(result + rlen, sep);
866 rlen += slen;
867 }
868 }
869 }
Jari Aaltof73dda02001-11-13 17:56:06 +0000870 if (result)
871 result[rlen] = '\0'; /* XXX */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000872 return(result);
873}
874
875char *
Jari Aalto7117c2d2002-07-17 14:10:11 +0000876array_to_assign (a, quoted)
877ARRAY *a;
878int quoted;
879{
880 char *result, *valstr, *is;
881 char indstr[INT_STRLEN_BOUND(intmax_t) + 1];
882 ARRAY_ELEMENT *ae;
883 int rsize, rlen, elen;
884
885 if (a == 0 || array_empty (a))
886 return((char *)NULL);
887
888 result = (char *)xmalloc (rsize = 128);
889 result[0] = '(';
890 rlen = 1;
891
892 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
893 is = inttostr (element_index(ae), indstr, sizeof(indstr));
Chet Rameya0c0a002016-09-15 16:59:08 -0400894 valstr = element_value (ae) ?
895 (ansic_shouldquote (element_value (ae)) ?
896 ansic_quote (element_value(ae), 0, (int *)0) :
897 sh_double_quote (element_value (ae)))
Jari Aalto7117c2d2002-07-17 14:10:11 +0000898 : (char *)NULL;
Jari Aaltof1be6662008-11-18 13:15:12 +0000899 elen = STRLEN (is) + 8 + STRLEN (valstr);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000900 RESIZE_MALLOCED_BUFFER (result, rlen, (elen + 1), rsize, rsize);
901
902 result[rlen++] = '[';
903 strcpy (result + rlen, is);
904 rlen += STRLEN (is);
905 result[rlen++] = ']';
906 result[rlen++] = '=';
907 if (valstr) {
908 strcpy (result + rlen, valstr);
909 rlen += STRLEN (valstr);
910 }
911
912 if (element_forw(ae) != a->head)
913 result[rlen++] = ' ';
914
915 FREE (valstr);
916 }
917 RESIZE_MALLOCED_BUFFER (result, rlen, 1, rsize, 8);
918 result[rlen++] = ')';
919 result[rlen] = '\0';
920 if (quoted) {
921 /* This is not as efficient as it could be... */
922 valstr = sh_single_quote (result);
923 free (result);
924 result = valstr;
925 }
926 return(result);
927}
928
929char *
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000930array_to_string (a, sep, quoted)
931ARRAY *a;
932char *sep;
933int quoted;
934{
935 if (a == 0)
936 return((char *)NULL);
937 if (array_empty(a))
938 return(savestring(""));
939 return (array_to_string_internal (element_forw(a->head), a->head, sep, quoted));
940}
941
Jari Aaltod166f041997-06-05 14:59:13 +0000942#if defined (INCLUDE_UNUSED) || defined (TEST_ARRAY)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000943/*
944 * Return an array consisting of elements in S, separated by SEP
945 */
946ARRAY *
Jari Aalto7117c2d2002-07-17 14:10:11 +0000947array_from_string(s, sep)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000948char *s, *sep;
949{
950 ARRAY *a;
951 WORD_LIST *w;
952
953 if (s == 0)
954 return((ARRAY *)NULL);
955 w = list_string (s, sep, 0);
956 if (w == 0)
957 return((ARRAY *)NULL);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000958 a = array_from_word_list (w);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000959 return (a);
960}
Jari Aaltod166f041997-06-05 14:59:13 +0000961#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000962
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000963#if defined (TEST_ARRAY)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000964/*
965 * To make a running version, compile -DTEST_ARRAY and link with:
966 * xmalloc.o syntax.o lib/malloc/libmalloc.a lib/sh/libsh.a
967 */
968int interrupt_immediately = 0;
969
970int
971signal_is_trapped(s)
972int s;
973{
974 return 0;
975}
976
977void
978fatal_error(const char *s, ...)
979{
980 fprintf(stderr, "array_test: fatal memory error\n");
981 abort();
982}
983
984void
985programming_error(const char *s, ...)
986{
987 fprintf(stderr, "array_test: fatal programming error\n");
988 abort();
989}
990
991WORD_DESC *
992make_bare_word (s)
993const char *s;
994{
995 WORD_DESC *w;
996
997 w = (WORD_DESC *)xmalloc(sizeof(WORD_DESC));
998 w->word = s ? savestring(s) : savestring ("");
999 w->flags = 0;
1000 return w;
1001}
1002
1003WORD_LIST *
1004make_word_list(x, l)
1005WORD_DESC *x;
1006WORD_LIST *l;
1007{
1008 WORD_LIST *w;
1009
1010 w = (WORD_LIST *)xmalloc(sizeof(WORD_LIST));
1011 w->word = x;
1012 w->next = l;
1013 return w;
1014}
1015
1016WORD_LIST *
1017list_string(s, t, i)
1018char *s, *t;
1019int i;
1020{
1021 char *r, *a;
1022 WORD_LIST *wl;
1023
1024 if (s == 0)
1025 return (WORD_LIST *)NULL;
1026 r = savestring(s);
1027 wl = (WORD_LIST *)NULL;
1028 a = strtok(r, t);
1029 while (a) {
1030 wl = make_word_list (make_bare_word(a), wl);
1031 a = strtok((char *)NULL, t);
1032 }
1033 return (REVERSE_LIST (wl, WORD_LIST *));
1034}
1035
1036GENERIC_LIST *
1037list_reverse (list)
1038GENERIC_LIST *list;
1039{
1040 register GENERIC_LIST *next, *prev;
1041
1042 for (prev = 0; list; ) {
1043 next = list->next;
1044 list->next = prev;
1045 prev = list;
1046 list = next;
1047 }
1048 return prev;
1049}
1050
1051char *
1052pat_subst(s, t, u, i)
1053char *s, *t, *u;
1054int i;
1055{
1056 return ((char *)NULL);
1057}
1058
1059char *
1060quote_string(s)
1061char *s;
1062{
1063 return savestring(s);
1064}
1065
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001066print_element(ae)
1067ARRAY_ELEMENT *ae;
1068{
Jari Aalto7117c2d2002-07-17 14:10:11 +00001069 char lbuf[INT_STRLEN_BOUND (intmax_t) + 1];
1070
1071 printf("array[%s] = %s\n",
1072 inttostr (element_index(ae), lbuf, sizeof (lbuf)),
1073 element_value(ae));
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001074}
1075
1076print_array(a)
1077ARRAY *a;
1078{
1079 printf("\n");
Jari Aaltob80f6442004-07-27 13:29:18 +00001080 array_walk(a, print_element, (void *)NULL);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001081}
1082
1083main()
1084{
1085 ARRAY *a, *new_a, *copy_of_a;
Jari Aalto7117c2d2002-07-17 14:10:11 +00001086 ARRAY_ELEMENT *ae, *aew;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001087 char *s;
1088
Jari Aalto7117c2d2002-07-17 14:10:11 +00001089 a = array_create();
1090 array_insert(a, 1, "one");
1091 array_insert(a, 7, "seven");
1092 array_insert(a, 4, "four");
1093 array_insert(a, 1029, "one thousand twenty-nine");
1094 array_insert(a, 12, "twelve");
1095 array_insert(a, 42, "forty-two");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001096 print_array(a);
Jari Aaltod166f041997-06-05 14:59:13 +00001097 s = array_to_string (a, " ", 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001098 printf("s = %s\n", s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001099 copy_of_a = array_from_string(s, " ");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001100 printf("copy_of_a:");
1101 print_array(copy_of_a);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001102 array_dispose(copy_of_a);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001103 printf("\n");
1104 free(s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001105 ae = array_remove(a, 4);
1106 array_dispose_element(ae);
1107 ae = array_remove(a, 1029);
1108 array_dispose_element(ae);
1109 array_insert(a, 16, "sixteen");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001110 print_array(a);
Jari Aaltod166f041997-06-05 14:59:13 +00001111 s = array_to_string (a, " ", 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001112 printf("s = %s\n", s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001113 copy_of_a = array_from_string(s, " ");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001114 printf("copy_of_a:");
1115 print_array(copy_of_a);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001116 array_dispose(copy_of_a);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001117 printf("\n");
1118 free(s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001119 array_insert(a, 2, "two");
1120 array_insert(a, 1029, "new one thousand twenty-nine");
1121 array_insert(a, 0, "zero");
1122 array_insert(a, 134, "");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001123 print_array(a);
Jari Aaltod166f041997-06-05 14:59:13 +00001124 s = array_to_string (a, ":", 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001125 printf("s = %s\n", s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001126 copy_of_a = array_from_string(s, ":");
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001127 printf("copy_of_a:");
1128 print_array(copy_of_a);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001129 array_dispose(copy_of_a);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001130 printf("\n");
1131 free(s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001132 new_a = array_copy(a);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001133 print_array(new_a);
Jari Aaltod166f041997-06-05 14:59:13 +00001134 s = array_to_string (new_a, ":", 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001135 printf("s = %s\n", s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001136 copy_of_a = array_from_string(s, ":");
1137 free(s);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001138 printf("copy_of_a:");
1139 print_array(copy_of_a);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001140 array_shift(copy_of_a, 2, AS_DISPOSE);
1141 printf("copy_of_a shifted by two:");
1142 print_array(copy_of_a);
1143 ae = array_shift(copy_of_a, 2, 0);
1144 printf("copy_of_a shifted by two:");
1145 print_array(copy_of_a);
1146 for ( ; ae; ) {
1147 aew = element_forw(ae);
1148 array_dispose_element(ae);
1149 ae = aew;
1150 }
1151 array_rshift(copy_of_a, 1, (char *)0);
1152 printf("copy_of_a rshift by 1:");
1153 print_array(copy_of_a);
1154 array_rshift(copy_of_a, 2, "new element zero");
1155 printf("copy_of_a rshift again by 2 with new element zero:");
1156 print_array(copy_of_a);
1157 s = array_to_assign(copy_of_a, 0);
1158 printf("copy_of_a=%s\n", s);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001159 free(s);
Jari Aalto7117c2d2002-07-17 14:10:11 +00001160 ae = array_shift(copy_of_a, array_num_elements(copy_of_a), 0);
1161 for ( ; ae; ) {
1162 aew = element_forw(ae);
1163 array_dispose_element(ae);
1164 ae = aew;
1165 }
1166 array_dispose(copy_of_a);
1167 printf("\n");
1168 array_dispose(a);
1169 array_dispose(new_a);
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001170}
1171
1172#endif /* TEST_ARRAY */
1173#endif /* ARRAY_VARS */