1 /* array.h -- definitions for the interface exported by array.c that allows
2 the rest of the shell to manipulate array variables. */
4 /* Copyright (C) 1997-2009 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Bash is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
28 typedef intmax_t arrayind_t
;
30 enum atype
{array_indexed
, array_assoc
};
32 typedef struct array
{
36 struct array_element
*lastref
;
37 struct array_element
*head
;
40 typedef struct array_element
{
43 struct array_element
*next
, *prev
;
46 typedef int sh_ae_map_func_t
__P((ARRAY_ELEMENT
*, void *));
48 /* Basic operations on entire arrays */
49 extern ARRAY
*array_create
__P((void));
50 extern void array_flush
__P((ARRAY
*));
51 extern void array_dispose
__P((ARRAY
*));
52 extern ARRAY
*array_copy
__P((ARRAY
*));
53 extern ARRAY
*array_slice
__P((ARRAY
*, ARRAY_ELEMENT
*, ARRAY_ELEMENT
*));
54 extern void array_walk
__P((ARRAY
*, sh_ae_map_func_t
*, void *));
56 extern ARRAY_ELEMENT
*array_shift
__P((ARRAY
*, int, int));
57 extern int array_rshift
__P((ARRAY
*, int, char *));
58 extern ARRAY_ELEMENT
*array_unshift_element
__P((ARRAY
*));
59 extern int array_shift_element
__P((ARRAY
*, char *));
61 extern ARRAY
*array_quote
__P((ARRAY
*));
62 extern ARRAY
*array_quote_escapes
__P((ARRAY
*));
63 extern ARRAY
*array_dequote
__P((ARRAY
*));
64 extern ARRAY
*array_dequote_escapes
__P((ARRAY
*));
65 extern ARRAY
*array_remove_quoted_nulls
__P((ARRAY
*));
67 extern char *array_subrange
__P((ARRAY
*, arrayind_t
, arrayind_t
, int, int));
68 extern char *array_patsub
__P((ARRAY
*, char *, char *, int));
69 extern char *array_modcase
__P((ARRAY
*, char *, int, int));
71 /* Basic operations on array elements. */
72 extern ARRAY_ELEMENT
*array_create_element
__P((arrayind_t
, char *));
73 extern ARRAY_ELEMENT
*array_copy_element
__P((ARRAY_ELEMENT
*));
74 extern void array_dispose_element
__P((ARRAY_ELEMENT
*));
76 extern int array_insert
__P((ARRAY
*, arrayind_t
, char *));
77 extern ARRAY_ELEMENT
*array_remove
__P((ARRAY
*, arrayind_t
));
78 extern char *array_reference
__P((ARRAY
*, arrayind_t
));
80 /* Converting to and from arrays */
81 extern WORD_LIST
*array_to_word_list
__P((ARRAY
*));
82 extern ARRAY
*array_from_word_list
__P((WORD_LIST
*));
83 extern WORD_LIST
*array_keys_to_word_list
__P((ARRAY
*));
85 extern ARRAY
*array_assign_list
__P((ARRAY
*, WORD_LIST
*));
87 extern char **array_to_argv
__P((ARRAY
*));
89 extern char *array_to_assign
__P((ARRAY
*, int));
90 extern char *array_to_string
__P((ARRAY
*, char *, int));
91 extern ARRAY
*array_from_string
__P((char *, char *));
93 /* Flags for array_shift */
94 #define AS_DISPOSE 0x01
96 #define array_num_elements(a) ((a)->num_elements)
97 #define array_max_index(a) ((a)->max_index)
98 #define array_first_index(a) ((a)->head->next->ind)
99 #define array_head(a) ((a)->head)
100 #define array_empty(a) ((a)->num_elements == 0)
102 #define element_value(ae) ((ae)->value)
103 #define element_index(ae) ((ae)->ind)
104 #define element_forw(ae) ((ae)->next)
105 #define element_back(ae) ((ae)->prev)
107 #define set_element_value(ae, val) ((ae)->value = (val))
110 #define array_push(a,v) \
111 do { array_rshift ((a), 1, (v)); } while (0)
112 #define array_pop(a) \
113 do { array_dispose_element (array_shift ((a), 1, 0)); } while (0)
115 #define GET_ARRAY_FROM_VAR(n, v, a) \
117 (v) = find_variable (n); \
118 (a) = ((v) && array_p ((v))) ? array_cell (v) : (ARRAY *)0; \
121 #define ALL_ELEMENT_SUB(c) ((c) == '@' || (c) == '*')
123 #endif /* _ARRAY_H_ */