Imported from ../bash-2.05.tar.gz.
[platform/upstream/bash.git] / array.c
diff --git a/array.c b/array.c
index 0de505c..a34fcf0 100644 (file)
--- a/array.c
+++ b/array.c
@@ -8,15 +8,39 @@
  * 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"
@@ -123,6 +147,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.
@@ -146,7 +171,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 +181,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 +272,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.
@@ -260,6 +289,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,
@@ -334,7 +364,7 @@ ARRAY       *a;
 
        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;
@@ -549,7 +601,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 +615,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 +628,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 +638,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);