* Chet Ramey
* chet@ins.cwru.edu
*/
+
+/* Copyright (C) 1997 Free Software Foundation, Inc.
+
+ This file is part of GNU Bash, the Bourne Again SHell.
+
+ 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 2, or (at your option) any later
+ version.
+
+ 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.
+
+ You should have received a copy of the GNU General Public License along
+ with Bash; see the file COPYING. If not, write to the Free Software
+ Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+
#include "config.h"
#if defined (ARRAY_VARS)
#if defined (HAVE_UNISTD_H)
+# ifdef _MINIX
+# include <sys/types.h>
+# endif
# include <unistd.h>
#endif
#include <stdio.h>
+#include "bashansi.h"
+
#include "shell.h"
#include "array.h"
#include "builtins/common.h"
-extern char *quote_string (); /* XXX */
-
#define ADD_BEFORE(ae, new) \
do { \
ae->prev->next = new; \
{
ARRAY_ELEMENT *r;
- r = (ARRAY_ELEMENT *) xmalloc(sizeof(ARRAY_ELEMENT));
+ r = (ARRAY_ELEMENT *)xmalloc(sizeof(ARRAY_ELEMENT));
r->ind = indx;
r->value = value ? savestring(value) : (char *)NULL;
r->next = r->prev = (ARRAY_ELEMENT *) NULL;
ARRAY *r;
ARRAY_ELEMENT *head;
- r =(ARRAY *) xmalloc(sizeof(ARRAY));
+ r =(ARRAY *)xmalloc(sizeof(ARRAY));
r->type = array_indexed;
r->max_index = r->max_size = -1;
r->num_elements = 0;
return(a1);
}
+#ifdef INCLUDE_UNUSED
/*
* Make and return a new array composed of the elements in array A from
* S to E, inclusive.
{
ARRAY *a;
ARRAY_ELEMENT *p, *n;
- int i;
+ arrayind_t i;
a = new_array ();
a->type = array->type;
a->num_elements = a->max_index = i;
return a;
}
+#endif
+#ifdef INCLUDE_UNUSED
ARRAY_ELEMENT *
copy_array_element(ae)
ARRAY_ELEMENT *ae;
return(ae ? new_array_element(element_index(ae), element_value(ae))
: (ARRAY_ELEMENT *) NULL);
}
+#endif
/*
* Add a new element with index I and value V to array A (a[i] = v).
return((char *) NULL);
}
+#ifdef TEST_ARRAY
/*
* Walk the array, calling FUNC once for each element, with the array
* element as the argument.
void
array_walk(a, func)
ARRAY *a;
-Function *func;
+sh_ae_map_func_t *func;
{
register ARRAY_ELEMENT *ae;
for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae))
(*func)(ae);
}
+#endif
/*
* Return a string that is the concatenation of all the elements in A,
return ((char *)NULL);
slen = strlen(sep);
+ result = NULL;
for (rsize = rlen = 0, ae = start; ae != end; ae = element_forw(ae)) {
if (rsize == 0)
- result = xmalloc (rsize = 64);
+ result = (char *)xmalloc (rsize = 64);
if (element_value(ae)) {
t = quoted ? quote_string(element_value(ae)) : element_value(ae);
reg = strlen(t);
}
}
}
- result[rlen] = '\0'; /* XXX */
+ if (result)
+ result[rlen] = '\0'; /* XXX */
return(result);
}
if (a == 0 || array_empty (a))
return((char *)NULL);
- result = xmalloc (rsize = 128);
+ result = (char *)xmalloc (rsize = 128);
result[0] = '(';
rlen = 1;
for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
indstr = itos (element_index(ae));
- valstr = element_value (ae) ? double_quote (element_value(ae))
+ valstr = element_value (ae) ? sh_double_quote (element_value(ae))
: (char *)NULL;
elen = STRLEN (indstr) + 8 + STRLEN (valstr);
RESIZE_MALLOCED_BUFFER (result, rlen, (elen + 1), rsize, rsize);
if (sv == 0)
return ((char *)NULL);
- vstr = single_quote (sv);
+ vstr = sh_single_quote (sv);
free (sv);
return (vstr);
}
}
#endif
+#if defined (INCLUDE_UNUSED) || defined (TEST_ARRAY)
/*
* Return an array consisting of elements in S, separated by SEP
*/
a = word_list_to_array (w);
return (a);
}
+#endif
/* Convenience routines for the shell to translate to and from the form used
by the rest of the code. */
return (REVERSE_LIST(list, WORD_LIST *));
}
+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 = alloc_array (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);
+}
+
ARRAY *
assign_word_list (array, list)
ARRAY *array;
char *
array_subrange (a, start, end, quoted)
ARRAY *a;
-int start, end, quoted;
+arrayind_t start, end;
+int quoted;
{
ARRAY_ELEMENT *h, *p;
- int i;
+ arrayind_t i;
p = array_head (a);
if (p == 0 || array_empty (a) || start > array_num_elements (a))
print_element(ae)
ARRAY_ELEMENT *ae;
{
- printf("array[%d] = %s\n",(int)element_index(ae), element_value(ae));
+ printf("array[%ld] = %s\n", element_index(ae), element_value(ae));
}
print_array(a)
array_add_element(a, 12, "twelve");
array_add_element(a, 42, "forty-two");
print_array(a);
- s = array_to_string (a, " ");
+ s = array_to_string (a, " ", 0);
printf("s = %s\n", s);
copy_of_a = string_to_array(s, " ");
printf("copy_of_a:");
destroy_array_element(ae);
array_add_element(a, 16, "sixteen");
print_array(a);
- s = array_to_string (a, " ");
+ s = array_to_string (a, " ", 0);
printf("s = %s\n", s);
copy_of_a = string_to_array(s, " ");
printf("copy_of_a:");
array_add_element(a, 0, "zero");
array_add_element(a, 134, "");
print_array(a);
- s = array_to_string (a, ":");
+ s = array_to_string (a, ":", 0);
printf("s = %s\n", s);
copy_of_a = string_to_array(s, ":");
printf("copy_of_a:");
free(s);
new_a = copy_array(a);
print_array(new_a);
- s = array_to_string (new_a, ":");
+ s = array_to_string (new_a, ":", 0);
printf("s = %s\n", s);
- copy_of_a = string_to_array(s, ":");
+ copy_of_a = string_to_array(s, ":", 0);
printf("copy_of_a:");
print_array(copy_of_a);
dispose_array(copy_of_a);