| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * array.c - functions to create, destroy, access, and manipulate arrays | 
					
						
							|  |  |  |  *	     of strings. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Arrays are sparse doubly-linked lists.  An element's index is stored | 
					
						
							|  |  |  |  * with it. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Chet Ramey | 
					
						
							|  |  |  |  * chet@ins.cwru.edu | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | /* Copyright (C) 1997-2009 Free Software Foundation, Inc.
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    This file is part of GNU Bash, the Bourne Again SHell. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |    Bash is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |    it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |    the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  |    (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |    Bash is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |    GNU General Public License for more details. | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |    You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |    along with Bash.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | #include "config.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined (ARRAY_VARS)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined (HAVE_UNISTD_H)
 | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  | #  ifdef _MINIX
 | 
					
						
							|  |  |  | #    include <sys/types.h>
 | 
					
						
							|  |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | #  include <unistd.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  | #include "bashansi.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | #include "shell.h"
 | 
					
						
							|  |  |  | #include "array.h"
 | 
					
						
							|  |  |  | #include "builtins/common.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define ADD_BEFORE(ae, new) \
 | 
					
						
							|  |  |  | 	do { \ | 
					
						
							|  |  |  | 		ae->prev->next = new; \ | 
					
						
							|  |  |  | 		new->prev = ae->prev; \ | 
					
						
							|  |  |  | 		ae->prev = new; \ | 
					
						
							|  |  |  | 		new->next = ae; \ | 
					
						
							|  |  |  | 	} while(0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | static char *array_to_string_internal __P((ARRAY_ELEMENT *, ARRAY_ELEMENT *, char *, int)); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-21 20:51:19 -05:00
										 |  |  | static ARRAY *lastarray = 0; | 
					
						
							|  |  |  | static ARRAY_ELEMENT *lastref = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define IS_LASTREF(a)	((a) == lastarray)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define INVALIDATE_LASTREF(a) \
 | 
					
						
							|  |  |  | do { \ | 
					
						
							|  |  |  | 	if ((a) == lastarray) { \ | 
					
						
							|  |  |  | 		lastarray = 0; \ | 
					
						
							|  |  |  | 		lastref = 0; \ | 
					
						
							|  |  |  | 	} \ | 
					
						
							|  |  |  | } while (0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define SET_LASTREF(a, e) \
 | 
					
						
							|  |  |  | do { \ | 
					
						
							|  |  |  | 	lastarray = (a); \ | 
					
						
							|  |  |  | 	lastref = (e); \ | 
					
						
							|  |  |  | } while (0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define UNSET_LASTREF() \
 | 
					
						
							|  |  |  | do { \ | 
					
						
							|  |  |  | 	lastarray = 0; \ | 
					
						
							|  |  |  | 	lastref = 0; \ | 
					
						
							|  |  |  | } while (0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | ARRAY * | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | array_create() | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	ARRAY	*r; | 
					
						
							|  |  |  | 	ARRAY_ELEMENT	*head; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 	r =(ARRAY *)xmalloc(sizeof(ARRAY)); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	r->type = array_indexed; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	r->max_index = -1; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	r->num_elements = 0; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	head = array_create_element(-1, (char *)NULL);	/* dummy head */ | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	head->prev = head->next = head; | 
					
						
							|  |  |  | 	r->head = head; | 
					
						
							|  |  |  | 	return(r); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | array_flush (a) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | ARRAY	*a; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	register ARRAY_ELEMENT *r, *r1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (a == 0) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	for (r = element_forw(a->head); r != a->head; ) { | 
					
						
							|  |  |  | 		r1 = element_forw(r); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 		array_dispose_element(r); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 		r = r1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	a->head->next = a->head->prev = a->head; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	a->max_index = -1; | 
					
						
							|  |  |  | 	a->num_elements = 0; | 
					
						
							| 
									
										
										
										
											2011-11-21 20:51:19 -05:00
										 |  |  | 	INVALIDATE_LASTREF(a); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | array_dispose(a) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | ARRAY	*a; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (a == 0) | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	array_flush (a); | 
					
						
							|  |  |  | 	array_dispose_element(a->head); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	free(a); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ARRAY * | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | array_copy(a) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | ARRAY	*a; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ARRAY	*a1; | 
					
						
							|  |  |  | 	ARRAY_ELEMENT	*ae, *new; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | 	if (a == 0) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 		return((ARRAY *) NULL); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	a1 = array_create(); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	a1->type = a->type; | 
					
						
							|  |  |  | 	a1->max_index = a->max_index; | 
					
						
							|  |  |  | 	a1->num_elements = a->num_elements; | 
					
						
							|  |  |  | 	for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 		new = array_create_element(element_index(ae), element_value(ae)); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 		ADD_BEFORE(a1->head, new); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return(a1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Make and return a new array composed of the elements in array A from | 
					
						
							|  |  |  |  * S to E, inclusive. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | ARRAY * | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | array_slice(array, s, e) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | ARRAY		*array; | 
					
						
							|  |  |  | ARRAY_ELEMENT	*s, *e; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ARRAY	*a; | 
					
						
							|  |  |  | 	ARRAY_ELEMENT *p, *n; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	int	i; | 
					
						
							|  |  |  | 	arrayind_t mi; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	a = array_create (); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	a->type = array->type; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 	for (mi = 0, p = s, i = 0; p != e; p = element_forw(p), i++) { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 		n = array_create_element (element_index(p), element_value(p)); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 		ADD_BEFORE(a->head, n); | 
					
						
							| 
									
										
										
										
											2008-11-18 13:15:12 +00:00
										 |  |  | 		mi = element_index(n); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	a->num_elements = i; | 
					
						
							|  |  |  | 	a->max_index = mi; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	return a; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Walk the array, calling FUNC once for each element, with the array | 
					
						
							|  |  |  |  * element as the argument. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | array_walk(a, func, udata) | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | ARRAY	*a; | 
					
						
							|  |  |  | sh_ae_map_func_t *func; | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | void	*udata; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	register ARRAY_ELEMENT *ae; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (a == 0 || array_empty(a)) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | 		if ((*func)(ae, udata) < 0) | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 			return; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Shift the array A N elements to the left.  Delete the first N elements | 
					
						
							|  |  |  |  * and subtract N from the indices of the remaining elements.  If FLAGS | 
					
						
							|  |  |  |  * does not include AS_DISPOSE, this returns a singly-linked null-terminated | 
					
						
							|  |  |  |  * list of elements so the caller can dispose of the chain.  If FLAGS | 
					
						
							|  |  |  |  * includes AS_DISPOSE, this function disposes of the shifted-out elements | 
					
						
							|  |  |  |  * and returns NULL. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | ARRAY_ELEMENT * | 
					
						
							|  |  |  | array_shift(a, n, flags) | 
					
						
							|  |  |  | ARRAY	*a; | 
					
						
							|  |  |  | int	n, flags; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	register ARRAY_ELEMENT *ae, *ret; | 
					
						
							|  |  |  | 	register int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (a == 0 || array_empty(a) || n <= 0) | 
					
						
							|  |  |  | 		return ((ARRAY_ELEMENT *)NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-21 20:51:19 -05:00
										 |  |  | 	INVALIDATE_LASTREF(a); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	for (i = 0, ret = ae = element_forw(a->head); ae != a->head && i < n; ae = element_forw(ae), i++) | 
					
						
							|  |  |  | 		; | 
					
						
							|  |  |  | 	if (ae == a->head) { | 
					
						
							|  |  |  | 		/* Easy case; shifting out all of the elements */ | 
					
						
							|  |  |  | 		if (flags & AS_DISPOSE) { | 
					
						
							|  |  |  | 			array_flush (a); | 
					
						
							|  |  |  | 			return ((ARRAY_ELEMENT *)NULL); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		for (ae = ret; element_forw(ae) != a->head; ae = element_forw(ae)) | 
					
						
							|  |  |  | 			; | 
					
						
							|  |  |  | 		element_forw(ae) = (ARRAY_ELEMENT *)NULL; | 
					
						
							|  |  |  | 		a->head->next = a->head->prev = a->head; | 
					
						
							|  |  |  | 		a->max_index = -1; | 
					
						
							|  |  |  | 		a->num_elements = 0; | 
					
						
							|  |  |  | 		return ret; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * ae now points to the list of elements we want to retain. | 
					
						
							|  |  |  | 	 * ret points to the list we want to either destroy or return. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	ae->prev->next = (ARRAY_ELEMENT *)NULL;		/* null-terminate RET */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	a->head->next = ae;		/* slice RET out of the array */ | 
					
						
							|  |  |  | 	ae->prev = a->head; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for ( ; ae != a->head; ae = element_forw(ae)) | 
					
						
							|  |  |  | 		element_index(ae) -= n;	/* renumber retained indices */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	a->num_elements -= n;		/* modify bookkeeping information */ | 
					
						
							| 
									
										
										
										
											2011-11-21 20:51:19 -05:00
										 |  |  | 	a->max_index = element_index(a->head->prev); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (flags & AS_DISPOSE) { | 
					
						
							|  |  |  | 		for (ae = ret; ae; ) { | 
					
						
							|  |  |  | 			ret = element_forw(ae); | 
					
						
							|  |  |  | 			array_dispose_element(ae); | 
					
						
							|  |  |  | 			ae = ret; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return ((ARRAY_ELEMENT *)NULL); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Shift array A right N indices.  If S is non-null, it becomes the value of | 
					
						
							|  |  |  |  * the new element 0.  Returns the number of elements in the array after the | 
					
						
							|  |  |  |  * shift. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | array_rshift (a, n, s) | 
					
						
							|  |  |  | ARRAY	*a; | 
					
						
							|  |  |  | int	n; | 
					
						
							|  |  |  | char	*s; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	register ARRAY_ELEMENT	*ae, *new; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | 	if (a == 0 || (array_empty(a) && s == 0)) | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | 	else if (n <= 0) | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 		return (a->num_elements); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ae = element_forw(a->head); | 
					
						
							|  |  |  | 	if (s) { | 
					
						
							|  |  |  | 		new = array_create_element(0, s); | 
					
						
							|  |  |  | 		ADD_BEFORE(ae, new); | 
					
						
							|  |  |  | 		a->num_elements++; | 
					
						
							| 
									
										
										
										
											2011-11-21 20:51:19 -05:00
										 |  |  | 		if (array_num_elements(a) == 1)	{	/* array was empty */ | 
					
						
							|  |  |  | 			a->max_index = 0; | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | 			return 1; | 
					
						
							| 
									
										
										
										
											2011-11-21 20:51:19 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * Renumber all elements in the array except the one we just added. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	for ( ; ae != a->head; ae = element_forw(ae)) | 
					
						
							|  |  |  | 		element_index(ae) += n; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | 	a->max_index = element_index(a->head->prev); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-21 20:51:19 -05:00
										 |  |  | 	INVALIDATE_LASTREF(a); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	return (a->num_elements); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | ARRAY_ELEMENT * | 
					
						
							|  |  |  | array_unshift_element(a) | 
					
						
							|  |  |  | ARRAY	*a; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return (array_shift (a, 1, 0)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | array_shift_element(a, v) | 
					
						
							|  |  |  | ARRAY	*a; | 
					
						
							|  |  |  | char	*v; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return (array_rshift (a, 1, v)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | ARRAY * | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | array_quote(array) | 
					
						
							|  |  |  | ARRAY	*array; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ARRAY_ELEMENT	*a; | 
					
						
							|  |  |  | 	char	*t; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | 	if (array == 0 || array_head(array) == 0 || array_empty(array)) | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 		return (ARRAY *)NULL; | 
					
						
							|  |  |  | 	for (a = element_forw(array->head); a != array->head; a = element_forw(a)) { | 
					
						
							|  |  |  | 		t = quote_string (a->value); | 
					
						
							|  |  |  | 		FREE(a->value); | 
					
						
							|  |  |  | 		a->value = t; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return array; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | ARRAY * | 
					
						
							| 
									
										
										
										
											2008-11-18 13:15:12 +00:00
										 |  |  | array_quote_escapes(array) | 
					
						
							|  |  |  | ARRAY	*array; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ARRAY_ELEMENT	*a; | 
					
						
							|  |  |  | 	char	*t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (array == 0 || array_head(array) == 0 || array_empty(array)) | 
					
						
							|  |  |  | 		return (ARRAY *)NULL; | 
					
						
							|  |  |  | 	for (a = element_forw(array->head); a != array->head; a = element_forw(a)) { | 
					
						
							|  |  |  | 		t = quote_escapes (a->value); | 
					
						
							|  |  |  | 		FREE(a->value); | 
					
						
							|  |  |  | 		a->value = t; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return array; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | ARRAY * | 
					
						
							|  |  |  | array_dequote(array) | 
					
						
							|  |  |  | ARRAY	*array; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ARRAY_ELEMENT	*a; | 
					
						
							|  |  |  | 	char	*t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (array == 0 || array_head(array) == 0 || array_empty(array)) | 
					
						
							|  |  |  | 		return (ARRAY *)NULL; | 
					
						
							|  |  |  | 	for (a = element_forw(array->head); a != array->head; a = element_forw(a)) { | 
					
						
							|  |  |  | 		t = dequote_string (a->value); | 
					
						
							|  |  |  | 		FREE(a->value); | 
					
						
							|  |  |  | 		a->value = t; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return array; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ARRAY * | 
					
						
							|  |  |  | array_dequote_escapes(array) | 
					
						
							|  |  |  | ARRAY	*array; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ARRAY_ELEMENT	*a; | 
					
						
							|  |  |  | 	char	*t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (array == 0 || array_head(array) == 0 || array_empty(array)) | 
					
						
							|  |  |  | 		return (ARRAY *)NULL; | 
					
						
							|  |  |  | 	for (a = element_forw(array->head); a != array->head; a = element_forw(a)) { | 
					
						
							|  |  |  | 		t = dequote_escapes (a->value); | 
					
						
							|  |  |  | 		FREE(a->value); | 
					
						
							|  |  |  | 		a->value = t; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return array; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ARRAY * | 
					
						
							|  |  |  | array_remove_quoted_nulls(array) | 
					
						
							|  |  |  | ARRAY	*array; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ARRAY_ELEMENT	*a; | 
					
						
							|  |  |  | 	char	*t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (array == 0 || array_head(array) == 0 || array_empty(array)) | 
					
						
							|  |  |  | 		return (ARRAY *)NULL; | 
					
						
							|  |  |  | 	for (a = element_forw(array->head); a != array->head; a = element_forw(a)) | 
					
						
							|  |  |  | 		a->value = remove_quoted_nulls (a->value); | 
					
						
							|  |  |  | 	return array; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Return a string whose elements are the members of array A beginning at | 
					
						
							|  |  |  |  * index START and spanning NELEM members.  Null elements are counted. | 
					
						
							|  |  |  |  * Since arrays are sparse, unset array elements are not counted. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | char * | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | array_subrange (a, start, nelem, starsub, quoted) | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | ARRAY	*a; | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | arrayind_t	start, nelem; | 
					
						
							|  |  |  | int	starsub, quoted; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-11-18 13:15:12 +00:00
										 |  |  | 	ARRAY		*a2; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	ARRAY_ELEMENT	*h, *p; | 
					
						
							|  |  |  | 	arrayind_t	i; | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 	char		*ifs, *sifs, *t; | 
					
						
							|  |  |  | 	int		slen; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | 	p = a ? array_head (a) : 0; | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | 	if (p == 0 || array_empty (a) || start > array_max_index(a)) | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 		return ((char *)NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * Find element with index START.  If START corresponds to an unset | 
					
						
							|  |  |  | 	 * element (arrays can be sparse), use the first element whose index | 
					
						
							|  |  |  | 	 * is >= START.  If START is < 0, we count START indices back from | 
					
						
							|  |  |  | 	 * the end of A (not elements, even with sparse arrays -- START is an | 
					
						
							|  |  |  | 	 * index). | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	for (p = element_forw(p); p != array_head(a) && start > element_index(p); p = element_forw(p)) | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 		; | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	if (p == a->head) | 
					
						
							|  |  |  | 		return ((char *)NULL); | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* Starting at P, take NELEM elements, inclusive. */ | 
					
						
							|  |  |  | 	for (i = 0, h = p; p != a->head && i < nelem; i++, p = element_forw(p)) | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 		; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-18 13:15:12 +00:00
										 |  |  | 	a2 = array_slice(a, h, p); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) | 
					
						
							|  |  |  | 		array_quote(a2); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		array_quote_escapes(a2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | 	if (starsub && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) { | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 		/* ${array[*]} */ | 
					
						
							|  |  |  | 		array_remove_quoted_nulls (a2); | 
					
						
							|  |  |  | 		sifs = ifs_firstchar ((int *)NULL); | 
					
						
							|  |  |  | 		t = array_to_string (a2, sifs, 0); | 
					
						
							|  |  |  | 		free (sifs); | 
					
						
							|  |  |  | 	} else if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) { | 
					
						
							|  |  |  | 		/* ${array[@]} */ | 
					
						
							|  |  |  | 		sifs = ifs_firstchar (&slen); | 
					
						
							|  |  |  | 		ifs = getifs (); | 
					
						
							|  |  |  | 		if (ifs == 0 || *ifs == 0) { | 
					
						
							|  |  |  | 			if (slen < 2) | 
					
						
							|  |  |  | 				sifs = xrealloc(sifs, 2); | 
					
						
							|  |  |  | 			sifs[0] = ' '; | 
					
						
							|  |  |  | 			sifs[1] = '\0'; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		t = array_to_string (a2, sifs, 0); | 
					
						
							|  |  |  | 		free (sifs); | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | 	} else | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 		t = array_to_string (a2, " ", 0); | 
					
						
							| 
									
										
										
										
											2008-11-18 13:15:12 +00:00
										 |  |  | 	array_dispose(a2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return t; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | char * | 
					
						
							|  |  |  | array_patsub (a, pat, rep, mflags) | 
					
						
							|  |  |  | ARRAY	*a; | 
					
						
							|  |  |  | char	*pat, *rep; | 
					
						
							|  |  |  | int	mflags; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ARRAY		*a2; | 
					
						
							|  |  |  | 	ARRAY_ELEMENT	*e; | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 	char	*t, *sifs, *ifs; | 
					
						
							|  |  |  | 	int	slen; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | 	if (a == 0 || array_head(a) == 0 || array_empty(a)) | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 		return ((char *)NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | 	a2 = array_copy(a); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	for (e = element_forw(a2->head); e != a2->head; e = element_forw(e)) { | 
					
						
							|  |  |  | 		t = pat_subst(element_value(e), pat, rep, mflags); | 
					
						
							|  |  |  | 		FREE(element_value(e)); | 
					
						
							|  |  |  | 		e->value = t; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (mflags & MATCH_QUOTED) | 
					
						
							| 
									
										
										
										
											2008-11-18 13:15:12 +00:00
										 |  |  | 		array_quote(a2); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		array_quote_escapes(a2); | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | 	if (mflags & MATCH_STARSUB) { | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 		array_remove_quoted_nulls (a2); | 
					
						
							|  |  |  | 		sifs = ifs_firstchar((int *)NULL); | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | 		t = array_to_string (a2, sifs, 0); | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 		free(sifs); | 
					
						
							|  |  |  | 	} else if (mflags & MATCH_QUOTED) { | 
					
						
							|  |  |  | 		/* ${array[@]} */ | 
					
						
							|  |  |  | 		sifs = ifs_firstchar (&slen); | 
					
						
							|  |  |  | 		ifs = getifs (); | 
					
						
							|  |  |  | 		if (ifs == 0 || *ifs == 0) { | 
					
						
							|  |  |  | 			if (slen < 2) | 
					
						
							|  |  |  | 				sifs = xrealloc (sifs, 2); | 
					
						
							|  |  |  | 			sifs[0] = ' '; | 
					
						
							|  |  |  | 			sifs[1] = '\0'; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		t = array_to_string (a2, sifs, 0); | 
					
						
							|  |  |  | 		free(sifs); | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | 	} else | 
					
						
							|  |  |  | 		t = array_to_string (a2, " ", 0); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	array_dispose (a2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return t; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | char * | 
					
						
							|  |  |  | array_modcase (a, pat, modop, mflags) | 
					
						
							|  |  |  | ARRAY	*a; | 
					
						
							|  |  |  | char	*pat; | 
					
						
							|  |  |  | int	modop; | 
					
						
							|  |  |  | int	mflags; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ARRAY		*a2; | 
					
						
							|  |  |  | 	ARRAY_ELEMENT	*e; | 
					
						
							|  |  |  | 	char	*t, *sifs, *ifs; | 
					
						
							|  |  |  | 	int	slen; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (a == 0 || array_head(a) == 0 || array_empty(a)) | 
					
						
							|  |  |  | 		return ((char *)NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	a2 = array_copy(a); | 
					
						
							|  |  |  | 	for (e = element_forw(a2->head); e != a2->head; e = element_forw(e)) { | 
					
						
							|  |  |  | 		t = sh_modcase(element_value(e), pat, modop); | 
					
						
							|  |  |  | 		FREE(element_value(e)); | 
					
						
							|  |  |  | 		e->value = t; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (mflags & MATCH_QUOTED) | 
					
						
							|  |  |  | 		array_quote(a2); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		array_quote_escapes(a2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (mflags & MATCH_STARSUB) { | 
					
						
							|  |  |  | 		array_remove_quoted_nulls (a2); | 
					
						
							|  |  |  | 		sifs = ifs_firstchar((int *)NULL); | 
					
						
							|  |  |  | 		t = array_to_string (a2, sifs, 0); | 
					
						
							|  |  |  | 		free(sifs); | 
					
						
							|  |  |  | 	} else if (mflags & MATCH_QUOTED) { | 
					
						
							|  |  |  | 		/* ${array[@]} */ | 
					
						
							|  |  |  | 		sifs = ifs_firstchar (&slen); | 
					
						
							|  |  |  | 		ifs = getifs (); | 
					
						
							|  |  |  | 		if (ifs == 0 || *ifs == 0) { | 
					
						
							|  |  |  | 			if (slen < 2) | 
					
						
							|  |  |  | 				sifs = xrealloc (sifs, 2); | 
					
						
							|  |  |  | 			sifs[0] = ' '; | 
					
						
							|  |  |  | 			sifs[1] = '\0'; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		t = array_to_string (a2, sifs, 0); | 
					
						
							|  |  |  | 		free(sifs); | 
					
						
							|  |  |  | 	} else | 
					
						
							|  |  |  | 		t = array_to_string (a2, " ", 0); | 
					
						
							|  |  |  | 	array_dispose (a2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return t; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Allocate and return a new array element with index INDEX and value | 
					
						
							|  |  |  |  * VALUE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | ARRAY_ELEMENT * | 
					
						
							|  |  |  | array_create_element(indx, value) | 
					
						
							|  |  |  | arrayind_t	indx; | 
					
						
							|  |  |  | char	*value; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ARRAY_ELEMENT *r; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	r = (ARRAY_ELEMENT *)xmalloc(sizeof(ARRAY_ELEMENT)); | 
					
						
							|  |  |  | 	r->ind = indx; | 
					
						
							|  |  |  | 	r->value = value ? savestring(value) : (char *)NULL; | 
					
						
							|  |  |  | 	r->next = r->prev = (ARRAY_ELEMENT *) NULL; | 
					
						
							|  |  |  | 	return(r); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  | #ifdef INCLUDE_UNUSED
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | ARRAY_ELEMENT * | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | array_copy_element(ae) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | ARRAY_ELEMENT	*ae; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	return(ae ? array_create_element(element_index(ae), element_value(ae)) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 		  : (ARRAY_ELEMENT *) NULL); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | void | 
					
						
							|  |  |  | array_dispose_element(ae) | 
					
						
							|  |  |  | ARRAY_ELEMENT	*ae; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | 	if (ae) { | 
					
						
							|  |  |  | 		FREE(ae->value); | 
					
						
							|  |  |  | 		free(ae); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Add a new element with index I and value V to array A (a[i] = v). | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | int | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | array_insert(a, i, v) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | ARRAY	*a; | 
					
						
							|  |  |  | arrayind_t	i; | 
					
						
							|  |  |  | char	*v; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	register ARRAY_ELEMENT *new, *ae; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | 	if (a == 0) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 		return(-1); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	new = array_create_element(i, v); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	if (i > array_max_index(a)) { | 
					
						
							|  |  |  | 		/*
 | 
					
						
							|  |  |  | 		 * Hook onto the end.  This also works for an empty array. | 
					
						
							|  |  |  | 		 * Fast path for the common case of allocating arrays | 
					
						
							|  |  |  | 		 * sequentially. | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		ADD_BEFORE(a->head, new); | 
					
						
							|  |  |  | 		a->max_index = i; | 
					
						
							|  |  |  | 		a->num_elements++; | 
					
						
							| 
									
										
										
										
											2011-11-21 20:51:19 -05:00
										 |  |  | 		SET_LASTREF(a, new); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 		return(0); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * Otherwise we search for the spot to insert it. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) { | 
					
						
							|  |  |  | 		if (element_index(ae) == i) { | 
					
						
							|  |  |  | 			/*
 | 
					
						
							|  |  |  | 			 * Replacing an existing element. | 
					
						
							|  |  |  | 			 */ | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 			array_dispose_element(new); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 			free(element_value(ae)); | 
					
						
							| 
									
										
										
										
											2004-11-09 21:37:25 +00:00
										 |  |  | 			ae->value = v ? savestring(v) : (char *)NULL; | 
					
						
							| 
									
										
										
										
											2011-11-21 20:51:19 -05:00
										 |  |  | 			SET_LASTREF(a, ae); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 			return(0); | 
					
						
							|  |  |  | 		} else if (element_index(ae) > i) { | 
					
						
							|  |  |  | 			ADD_BEFORE(ae, new); | 
					
						
							|  |  |  | 			a->num_elements++; | 
					
						
							| 
									
										
										
										
											2011-11-21 20:51:19 -05:00
										 |  |  | 			SET_LASTREF(a, new); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 			return(0); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-11-21 20:51:19 -05:00
										 |  |  | 	INVALIDATE_LASTREF(a); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	return (-1);		/* problem */ | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Delete the element with index I from array A and return it so the | 
					
						
							|  |  |  |  * caller can dispose of it. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | ARRAY_ELEMENT * | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | array_remove(a, i) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | ARRAY	*a; | 
					
						
							|  |  |  | arrayind_t	i; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	register ARRAY_ELEMENT *ae; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | 	if (a == 0 || array_empty(a)) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 		return((ARRAY_ELEMENT *) NULL); | 
					
						
							|  |  |  | 	for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) | 
					
						
							|  |  |  | 		if (element_index(ae) == i) { | 
					
						
							|  |  |  | 			ae->next->prev = ae->prev; | 
					
						
							|  |  |  | 			ae->prev->next = ae->next; | 
					
						
							|  |  |  | 			a->num_elements--; | 
					
						
							|  |  |  | 			if (i == array_max_index(a)) | 
					
						
							|  |  |  | 				a->max_index = element_index(ae->prev); | 
					
						
							| 
									
										
										
										
											2011-11-21 20:51:19 -05:00
										 |  |  | 			INVALIDATE_LASTREF(a); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 			return(ae); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	return((ARRAY_ELEMENT *) NULL); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Return the value of a[i]. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | char * | 
					
						
							|  |  |  | array_reference(a, i) | 
					
						
							|  |  |  | ARRAY	*a; | 
					
						
							|  |  |  | arrayind_t	i; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	register ARRAY_ELEMENT *ae; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (a == 0 || array_empty(a)) | 
					
						
							|  |  |  | 		return((char *) NULL); | 
					
						
							| 
									
										
										
										
											2011-11-21 20:51:19 -05:00
										 |  |  | 	if (i > array_max_index(a)) | 
					
						
							|  |  |  | 		return((char *)NULL); | 
					
						
							|  |  |  | 	/* Keep roving pointer into array to optimize sequential access */ | 
					
						
							|  |  |  | 	if (lastref && IS_LASTREF(a)) | 
					
						
							|  |  |  | 		ae = (i >= element_index(lastref)) ? lastref : element_forw(a->head); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		ae = element_forw(a->head); | 
					
						
							|  |  |  | 	for ( ; ae != a->head; ae = element_forw(ae)) | 
					
						
							|  |  |  | 		if (element_index(ae) == i) { | 
					
						
							|  |  |  | 			SET_LASTREF(a, ae); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 			return(element_value(ae)); | 
					
						
							| 
									
										
										
										
											2011-11-21 20:51:19 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	UNSET_LASTREF(); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	return((char *) NULL); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | /* Convenience routines for the shell to translate to and from the form used
 | 
					
						
							|  |  |  |    by the rest of the code. */ | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | WORD_LIST * | 
					
						
							|  |  |  | array_to_word_list(a) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | ARRAY	*a; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	WORD_LIST	*list; | 
					
						
							|  |  |  | 	ARRAY_ELEMENT	*ae; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (a == 0 || array_empty(a)) | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 		return((WORD_LIST *)NULL); | 
					
						
							|  |  |  | 	list = (WORD_LIST *)NULL; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 		list = make_word_list (make_bare_word(element_value(ae)), list); | 
					
						
							|  |  |  | 	return (REVERSE_LIST(list, WORD_LIST *)); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | ARRAY * | 
					
						
							|  |  |  | array_from_word_list (list) | 
					
						
							|  |  |  | WORD_LIST	*list; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ARRAY	*a; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (list == 0) | 
					
						
							|  |  |  | 		return((ARRAY *)NULL); | 
					
						
							|  |  |  | 	a = array_create(); | 
					
						
							|  |  |  | 	return (array_assign_list (a, list)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | WORD_LIST * | 
					
						
							|  |  |  | array_keys_to_word_list(a) | 
					
						
							|  |  |  | ARRAY	*a; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	WORD_LIST	*list; | 
					
						
							|  |  |  | 	ARRAY_ELEMENT	*ae; | 
					
						
							|  |  |  | 	char		*t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (a == 0 || array_empty(a)) | 
					
						
							|  |  |  | 		return((WORD_LIST *)NULL); | 
					
						
							|  |  |  | 	list = (WORD_LIST *)NULL; | 
					
						
							|  |  |  | 	for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) { | 
					
						
							|  |  |  | 		t = itos(element_index(ae)); | 
					
						
							|  |  |  | 		list = make_word_list (make_bare_word(t), list); | 
					
						
							|  |  |  | 		free(t); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return (REVERSE_LIST(list, WORD_LIST *)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | ARRAY * | 
					
						
							|  |  |  | array_assign_list (array, list) | 
					
						
							|  |  |  | ARRAY	*array; | 
					
						
							|  |  |  | WORD_LIST	*list; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	register WORD_LIST *l; | 
					
						
							|  |  |  | 	register arrayind_t i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (l = list, i = 0; l; l = l->next, i++) | 
					
						
							|  |  |  | 		array_insert(array, i, l->word->word); | 
					
						
							|  |  |  | 	return array; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | char ** | 
					
						
							|  |  |  | array_to_argv (a) | 
					
						
							|  |  |  | ARRAY	*a; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char		**ret, *t; | 
					
						
							|  |  |  | 	int		i; | 
					
						
							|  |  |  | 	ARRAY_ELEMENT	*ae; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (a == 0 || array_empty(a)) | 
					
						
							|  |  |  | 		return ((char **)NULL); | 
					
						
							|  |  |  | 	ret = strvec_create (array_num_elements (a) + 1); | 
					
						
							|  |  |  | 	i = 0; | 
					
						
							|  |  |  | 	for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) { | 
					
						
							|  |  |  | 		t = element_value (ae); | 
					
						
							|  |  |  | 		ret[i++] = t ? savestring (t) : (char *)NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ret[i] = (char *)NULL; | 
					
						
							|  |  |  | 	return (ret); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |  * Return a string that is the concatenation of the elements in A from START | 
					
						
							|  |  |  |  * to END, separated by SEP. | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | static char * | 
					
						
							|  |  |  | array_to_string_internal (start, end, sep, quoted) | 
					
						
							|  |  |  | ARRAY_ELEMENT	*start, *end; | 
					
						
							|  |  |  | char	*sep; | 
					
						
							|  |  |  | int	quoted; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char	*result, *t; | 
					
						
							|  |  |  | 	ARRAY_ELEMENT *ae; | 
					
						
							|  |  |  | 	int	slen, rsize, rlen, reg; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (start == end)	/* XXX - should not happen */ | 
					
						
							|  |  |  | 		return ((char *)NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	slen = strlen(sep); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 	result = NULL; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	for (rsize = rlen = 0, ae = start; ae != end; ae = element_forw(ae)) { | 
					
						
							|  |  |  | 		if (rsize == 0) | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 			result = (char *)xmalloc (rsize = 64); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 		if (element_value(ae)) { | 
					
						
							|  |  |  | 			t = quoted ? quote_string(element_value(ae)) : element_value(ae); | 
					
						
							|  |  |  | 			reg = strlen(t); | 
					
						
							|  |  |  | 			RESIZE_MALLOCED_BUFFER (result, rlen, (reg + slen + 2), | 
					
						
							|  |  |  | 						rsize, rsize); | 
					
						
							|  |  |  | 			strcpy(result + rlen, t); | 
					
						
							|  |  |  | 			rlen += reg; | 
					
						
							|  |  |  | 			if (quoted && t) | 
					
						
							|  |  |  | 				free(t); | 
					
						
							|  |  |  | 			/*
 | 
					
						
							|  |  |  | 			 * Add a separator only after non-null elements. | 
					
						
							|  |  |  | 			 */ | 
					
						
							|  |  |  | 			if (element_forw(ae) != end) { | 
					
						
							|  |  |  | 				strcpy(result + rlen, sep); | 
					
						
							|  |  |  | 				rlen += slen; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 	if (result) | 
					
						
							|  |  |  | 	  result[rlen] = '\0';	/* XXX */ | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	return(result); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | char * | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | array_to_assign (a, quoted) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | ARRAY	*a; | 
					
						
							|  |  |  | int	quoted; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	char	*result, *valstr, *is; | 
					
						
							|  |  |  | 	char	indstr[INT_STRLEN_BOUND(intmax_t) + 1]; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	ARRAY_ELEMENT *ae; | 
					
						
							|  |  |  | 	int	rsize, rlen, elen; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (a == 0 || array_empty (a)) | 
					
						
							|  |  |  | 		return((char *)NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 	result = (char *)xmalloc (rsize = 128); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	result[0] = '('; | 
					
						
							|  |  |  | 	rlen = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 		is = inttostr (element_index(ae), indstr, sizeof(indstr)); | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  | 		valstr = element_value (ae) ? sh_double_quote (element_value(ae)) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 					    : (char *)NULL; | 
					
						
							| 
									
										
										
										
											2008-11-18 13:15:12 +00:00
										 |  |  | 		elen = STRLEN (is) + 8 + STRLEN (valstr); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 		RESIZE_MALLOCED_BUFFER (result, rlen, (elen + 1), rsize, rsize); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		result[rlen++] = '['; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 		strcpy (result + rlen, is); | 
					
						
							|  |  |  | 		rlen += STRLEN (is); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 		result[rlen++] = ']'; | 
					
						
							|  |  |  | 		result[rlen++] = '='; | 
					
						
							|  |  |  | 		if (valstr) { | 
					
						
							|  |  |  | 			strcpy (result + rlen, valstr); | 
					
						
							|  |  |  | 			rlen += STRLEN (valstr); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (element_forw(ae) != a->head) | 
					
						
							|  |  |  | 		  result[rlen++] = ' '; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		FREE (valstr); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	RESIZE_MALLOCED_BUFFER (result, rlen, 1, rsize, 8); | 
					
						
							|  |  |  | 	result[rlen++] = ')'; | 
					
						
							|  |  |  | 	result[rlen] = '\0'; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	if (quoted) { | 
					
						
							|  |  |  | 		/* This is not as efficient as it could be... */ | 
					
						
							|  |  |  | 		valstr = sh_single_quote (result); | 
					
						
							|  |  |  | 		free (result); | 
					
						
							|  |  |  | 		result = valstr; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	return(result); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | char * | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | array_to_string (a, sep, quoted) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | ARRAY	*a; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | char	*sep; | 
					
						
							|  |  |  | int	quoted; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	if (a == 0) | 
					
						
							|  |  |  | 		return((char *)NULL); | 
					
						
							|  |  |  | 	if (array_empty(a)) | 
					
						
							|  |  |  | 		return(savestring("")); | 
					
						
							|  |  |  | 	return (array_to_string_internal (element_forw(a->head), a->head, sep, quoted)); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  | #if defined (INCLUDE_UNUSED) || defined (TEST_ARRAY)
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Return an array consisting of elements in S, separated by SEP | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | ARRAY * | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | array_from_string(s, sep) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | char	*s, *sep; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ARRAY	*a; | 
					
						
							|  |  |  | 	WORD_LIST *w; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (s == 0) | 
					
						
							|  |  |  | 		return((ARRAY *)NULL); | 
					
						
							|  |  |  | 	w = list_string (s, sep, 0); | 
					
						
							|  |  |  | 	if (w == 0) | 
					
						
							|  |  |  | 		return((ARRAY *)NULL); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	a = array_from_word_list (w); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	return (a); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | #if defined (TEST_ARRAY)
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * To make a running version, compile -DTEST_ARRAY and link with: | 
					
						
							|  |  |  |  * 	xmalloc.o syntax.o lib/malloc/libmalloc.a lib/sh/libsh.a | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | int interrupt_immediately = 0; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | int | 
					
						
							|  |  |  | signal_is_trapped(s) | 
					
						
							|  |  |  | int	s; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | void | 
					
						
							|  |  |  | fatal_error(const char *s, ...) | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	fprintf(stderr, "array_test: fatal memory error\n"); | 
					
						
							|  |  |  | 	abort(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | void | 
					
						
							|  |  |  | programming_error(const char *s, ...) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	fprintf(stderr, "array_test: fatal programming error\n"); | 
					
						
							|  |  |  | 	abort(); | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | WORD_DESC * | 
					
						
							|  |  |  | make_bare_word (s) | 
					
						
							|  |  |  | const char	*s; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	WORD_DESC *w; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	w = (WORD_DESC *)xmalloc(sizeof(WORD_DESC)); | 
					
						
							|  |  |  | 	w->word = s ? savestring(s) : savestring (""); | 
					
						
							|  |  |  | 	w->flags = 0; | 
					
						
							|  |  |  | 	return w; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | WORD_LIST * | 
					
						
							|  |  |  | make_word_list(x, l) | 
					
						
							|  |  |  | WORD_DESC	*x; | 
					
						
							|  |  |  | WORD_LIST	*l; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	WORD_LIST *w; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	w = (WORD_LIST *)xmalloc(sizeof(WORD_LIST)); | 
					
						
							|  |  |  | 	w->word = x; | 
					
						
							|  |  |  | 	w->next = l; | 
					
						
							|  |  |  | 	return w; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | WORD_LIST * | 
					
						
							|  |  |  | list_string(s, t, i) | 
					
						
							|  |  |  | char	*s, *t; | 
					
						
							|  |  |  | int	i; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	char	*r, *a; | 
					
						
							|  |  |  | 	WORD_LIST	*wl; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	if (s == 0) | 
					
						
							|  |  |  | 		return (WORD_LIST *)NULL; | 
					
						
							|  |  |  | 	r = savestring(s); | 
					
						
							|  |  |  | 	wl = (WORD_LIST *)NULL; | 
					
						
							|  |  |  | 	a = strtok(r, t); | 
					
						
							|  |  |  | 	while (a) { | 
					
						
							|  |  |  | 		wl = make_word_list (make_bare_word(a), wl); | 
					
						
							|  |  |  | 		a = strtok((char *)NULL, t); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	return (REVERSE_LIST (wl, WORD_LIST *)); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | GENERIC_LIST * | 
					
						
							|  |  |  | list_reverse (list) | 
					
						
							|  |  |  | GENERIC_LIST	*list; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	register GENERIC_LIST *next, *prev; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	for (prev = 0; list; ) { | 
					
						
							|  |  |  | 		next = list->next; | 
					
						
							|  |  |  | 		list->next = prev; | 
					
						
							|  |  |  | 		prev = list; | 
					
						
							|  |  |  | 		list = next; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return prev; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | char * | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | pat_subst(s, t, u, i) | 
					
						
							|  |  |  | char	*s, *t, *u; | 
					
						
							|  |  |  | int	i; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	return ((char *)NULL); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | char * | 
					
						
							|  |  |  | quote_string(s) | 
					
						
							|  |  |  | char	*s; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return savestring(s); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | print_element(ae) | 
					
						
							|  |  |  | ARRAY_ELEMENT	*ae; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	char	lbuf[INT_STRLEN_BOUND (intmax_t) + 1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	printf("array[%s] = %s\n", | 
					
						
							|  |  |  | 		inttostr (element_index(ae), lbuf, sizeof (lbuf)), | 
					
						
							|  |  |  | 		element_value(ae)); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | print_array(a) | 
					
						
							|  |  |  | ARRAY	*a; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	printf("\n"); | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | 	array_walk(a, print_element, (void *)NULL); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | main() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ARRAY	*a, *new_a, *copy_of_a; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	ARRAY_ELEMENT	*ae, *aew; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	char	*s; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	a = array_create(); | 
					
						
							|  |  |  | 	array_insert(a, 1, "one"); | 
					
						
							|  |  |  | 	array_insert(a, 7, "seven"); | 
					
						
							|  |  |  | 	array_insert(a, 4, "four"); | 
					
						
							|  |  |  | 	array_insert(a, 1029, "one thousand twenty-nine"); | 
					
						
							|  |  |  | 	array_insert(a, 12, "twelve"); | 
					
						
							|  |  |  | 	array_insert(a, 42, "forty-two"); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	print_array(a); | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  | 	s = array_to_string (a, " ", 0); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	printf("s = %s\n", s); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	copy_of_a = array_from_string(s, " "); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	printf("copy_of_a:"); | 
					
						
							|  |  |  | 	print_array(copy_of_a); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	array_dispose(copy_of_a); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	printf("\n"); | 
					
						
							|  |  |  | 	free(s); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	ae = array_remove(a, 4); | 
					
						
							|  |  |  | 	array_dispose_element(ae); | 
					
						
							|  |  |  | 	ae = array_remove(a, 1029); | 
					
						
							|  |  |  | 	array_dispose_element(ae); | 
					
						
							|  |  |  | 	array_insert(a, 16, "sixteen"); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	print_array(a); | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  | 	s = array_to_string (a, " ", 0); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	printf("s = %s\n", s); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	copy_of_a = array_from_string(s, " "); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	printf("copy_of_a:"); | 
					
						
							|  |  |  | 	print_array(copy_of_a); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	array_dispose(copy_of_a); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	printf("\n"); | 
					
						
							|  |  |  | 	free(s); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	array_insert(a, 2, "two"); | 
					
						
							|  |  |  | 	array_insert(a, 1029, "new one thousand twenty-nine"); | 
					
						
							|  |  |  | 	array_insert(a, 0, "zero"); | 
					
						
							|  |  |  | 	array_insert(a, 134, ""); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	print_array(a); | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  | 	s = array_to_string (a, ":", 0); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	printf("s = %s\n", s); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	copy_of_a = array_from_string(s, ":"); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	printf("copy_of_a:"); | 
					
						
							|  |  |  | 	print_array(copy_of_a); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	array_dispose(copy_of_a); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	printf("\n"); | 
					
						
							|  |  |  | 	free(s); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	new_a = array_copy(a); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	print_array(new_a); | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  | 	s = array_to_string (new_a, ":", 0); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	printf("s = %s\n", s); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	copy_of_a = array_from_string(s, ":"); | 
					
						
							|  |  |  | 	free(s); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	printf("copy_of_a:"); | 
					
						
							|  |  |  | 	print_array(copy_of_a); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	array_shift(copy_of_a, 2, AS_DISPOSE); | 
					
						
							|  |  |  | 	printf("copy_of_a shifted by two:"); | 
					
						
							|  |  |  | 	print_array(copy_of_a); | 
					
						
							|  |  |  | 	ae = array_shift(copy_of_a, 2, 0); | 
					
						
							|  |  |  | 	printf("copy_of_a shifted by two:"); | 
					
						
							|  |  |  | 	print_array(copy_of_a); | 
					
						
							|  |  |  | 	for ( ; ae; ) { | 
					
						
							|  |  |  | 		aew = element_forw(ae); | 
					
						
							|  |  |  | 		array_dispose_element(ae); | 
					
						
							|  |  |  | 		ae = aew; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	array_rshift(copy_of_a, 1, (char *)0); | 
					
						
							|  |  |  | 	printf("copy_of_a rshift by 1:"); | 
					
						
							|  |  |  | 	print_array(copy_of_a); | 
					
						
							|  |  |  | 	array_rshift(copy_of_a, 2, "new element zero"); | 
					
						
							|  |  |  | 	printf("copy_of_a rshift again by 2 with new element zero:"); | 
					
						
							|  |  |  | 	print_array(copy_of_a); | 
					
						
							|  |  |  | 	s = array_to_assign(copy_of_a, 0); | 
					
						
							|  |  |  | 	printf("copy_of_a=%s\n", s); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	free(s); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	ae = array_shift(copy_of_a, array_num_elements(copy_of_a), 0); | 
					
						
							|  |  |  | 	for ( ; ae; ) { | 
					
						
							|  |  |  | 		aew = element_forw(ae); | 
					
						
							|  |  |  | 		array_dispose_element(ae); | 
					
						
							|  |  |  | 		ae = aew; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	array_dispose(copy_of_a); | 
					
						
							|  |  |  | 	printf("\n"); | 
					
						
							|  |  |  | 	array_dispose(a); | 
					
						
							|  |  |  | 	array_dispose(new_a); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif /* TEST_ARRAY */
 | 
					
						
							|  |  |  | #endif /* ARRAY_VARS */
 |