Imported from ../bash-2.05a.tar.gz.
[platform/upstream/bash.git] / array.c
diff --git a/array.c b/array.c
index 0de505c..c1d862e 100644 (file)
--- a/array.c
+++ b/array.c
@@ -8,21 +8,43 @@
  * 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; \
@@ -42,7 +64,7 @@ char  *value;
 {
        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;
@@ -63,7 +85,7 @@ new_array()
        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;
@@ -123,6 +145,7 @@ ARRAY       *a;
        return(a1);
 }
 
+#ifdef INCLUDE_UNUSED
 /*
  * Make and return a new array composed of the elements in array A from
  * S to E, inclusive.
@@ -134,7 +157,7 @@ ARRAY_ELEMENT       *s, *e;
 {
        ARRAY   *a;
        ARRAY_ELEMENT *p, *n;
-       int     i;
+       arrayind_t i;
 
        a = new_array ();
        a->type = array->type;
@@ -146,7 +169,9 @@ ARRAY_ELEMENT       *s, *e;
        a->num_elements = a->max_index = i;
        return a;
 }
+#endif
 
+#ifdef INCLUDE_UNUSED
 ARRAY_ELEMENT *
 copy_array_element(ae)
 ARRAY_ELEMENT  *ae;
@@ -154,6 +179,7 @@ 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).
@@ -244,6 +270,7 @@ arrayind_t  i;
        return((char *) NULL);
 }
 
+#ifdef TEST_ARRAY
 /*
  * Walk the array, calling FUNC once for each element, with the array
  * element as the argument.
@@ -251,7 +278,7 @@ arrayind_t  i;
 void
 array_walk(a, func)
 ARRAY  *a;
-Function *func;
+sh_ae_map_func_t *func;
 {
        register ARRAY_ELEMENT *ae;
 
@@ -260,6 +287,7 @@ Function *func;
        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,
@@ -279,9 +307,10 @@ int        quoted;
                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);
@@ -300,7 +329,8 @@ int quoted;
                        }
                }
        }
-       result[rlen] = '\0';    /* XXX */
+       if (result)
+         result[rlen] = '\0';  /* XXX */
        return(result);
 }
 
@@ -328,13 +358,13 @@ ARRAY     *a;
        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);
@@ -371,7 +401,7 @@ ARRAY       *a;
        if (sv == 0)
                return ((char *)NULL);
 
-       vstr = single_quote (sv);
+       vstr = sh_single_quote (sv);
        free (sv);
        return (vstr);
 }
@@ -392,6 +422,7 @@ register char *s1, *s2;
 }
 #endif
 
+#if defined (INCLUDE_UNUSED) || defined (TEST_ARRAY)
 /*
  * Return an array consisting of elements in S, separated by SEP
  */
@@ -410,6 +441,7 @@ char        *s, *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. */
@@ -428,6 +460,26 @@ ARRAY      *a;
        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;
@@ -473,10 +525,11 @@ 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))
@@ -525,7 +578,7 @@ int mflags;
 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)
@@ -549,7 +602,7 @@ main()
        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:");
@@ -563,7 +616,7 @@ main()
        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:");
@@ -576,7 +629,7 @@ main()
        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:");
@@ -586,9 +639,9 @@ main()
        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);