]>
git.ipfire.org Git - thirdparty/bash.git/blob - array.h
47712e88ad24174f5bde0550eb3f8b5942a69013
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-2022 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 typedef struct array
{
32 arrayind_t num_elements
;
33 #ifdef ALT_ARRAY_IMPLEMENTATION
34 arrayind_t first_index
;
35 arrayind_t alloc_size
;
36 struct array_element
**elements
;
38 struct array_element
*head
;
39 struct array_element
*lastref
;
43 typedef struct array_element
{
46 #ifndef ALT_ARRAY_IMPLEMENTATION
47 struct array_element
*next
, *prev
;
51 #define ARRAY_DEFAULT_SIZE 1024
53 typedef int sh_ae_map_func_t (ARRAY_ELEMENT
*, void *);
55 /* Basic operations on entire arrays */
56 #ifdef ALT_ARRAY_IMPLEMENTATION
57 extern void array_alloc (ARRAY
*, arrayind_t
);
58 extern void array_resize (ARRAY
*, arrayind_t
);
59 extern void array_expand (ARRAY
*, arrayind_t
);
60 extern void array_dispose_elements (ARRAY_ELEMENT
**);
62 extern ARRAY
*array_create (void);
63 extern void array_flush (ARRAY
*);
64 extern void array_dispose (ARRAY
*);
65 extern ARRAY
*array_copy (ARRAY
*);
66 #ifndef ALT_ARRAY_IMPLEMENTATION
67 extern ARRAY
*array_slice (ARRAY
*, ARRAY_ELEMENT
*, ARRAY_ELEMENT
*);
69 extern ARRAY
*array_slice (ARRAY
*, arrayind_t
, arrayind_t
);
72 extern void array_walk (ARRAY
*, sh_ae_map_func_t
*, void *);
74 #ifndef ALT_ARRAY_IMPLEMENTATION
75 extern ARRAY_ELEMENT
*array_shift (ARRAY
*, int, int);
77 extern ARRAY_ELEMENT
**array_shift (ARRAY
*, int, int);
79 extern int array_rshift (ARRAY
*, int, char *);
80 extern ARRAY_ELEMENT
*array_unshift_element (ARRAY
*);
81 extern int array_shift_element (ARRAY
*, char *);
83 extern ARRAY
*array_quote (ARRAY
*);
84 extern ARRAY
*array_quote_escapes (ARRAY
*);
85 extern ARRAY
*array_dequote (ARRAY
*);
86 extern ARRAY
*array_dequote_escapes (ARRAY
*);
87 extern ARRAY
*array_remove_quoted_nulls (ARRAY
*);
89 extern char *array_subrange (ARRAY
*, arrayind_t
, arrayind_t
, int, int, int);
90 extern char *array_patsub (ARRAY
*, char *, char *, int);
91 extern char *array_modcase (ARRAY
*, char *, int, int);
93 /* Basic operations on array elements. */
94 extern ARRAY_ELEMENT
*array_create_element (arrayind_t
, char *);
95 extern ARRAY_ELEMENT
*array_copy_element (ARRAY_ELEMENT
*);
96 extern void array_dispose_element (ARRAY_ELEMENT
*);
98 extern int array_insert (ARRAY
*, arrayind_t
, char *);
99 extern ARRAY_ELEMENT
*array_remove (ARRAY
*, arrayind_t
);
100 extern char *array_reference (ARRAY
*, arrayind_t
);
102 /* Converting to and from arrays */
103 extern WORD_LIST
*array_to_word_list (ARRAY
*);
104 extern ARRAY
*array_from_word_list (WORD_LIST
*);
105 extern WORD_LIST
*array_keys_to_word_list (ARRAY
*);
106 extern WORD_LIST
*array_to_kvpair_list (ARRAY
*);
108 extern ARRAY
*array_assign_list (ARRAY
*, WORD_LIST
*);
110 extern char **array_to_argv (ARRAY
*, int *);
111 extern ARRAY
*array_from_argv (ARRAY
*, char **, int);
113 extern char *array_to_kvpair (ARRAY
*, int);
114 extern char *array_to_assign (ARRAY
*, int);
115 extern char *array_to_string (ARRAY
*, char *, int);
116 extern ARRAY
*array_from_string (char *, char *);
118 /* Flags for array_shift */
119 #define AS_DISPOSE 0x01
121 #define array_num_elements(a) ((a)->num_elements)
122 #define array_max_index(a) ((a)->max_index)
123 #ifndef ALT_ARRAY_IMPLEMENTATION
124 #define array_first_index(a) ((a)->head->next->ind)
125 #define array_head(a) ((a)->head)
126 #define array_alloc_size(a) ((a)->alloc_size)
128 #define array_first_index(a) ((a)->first_index)
129 #define array_head(a) ((a)->elements)
131 #define array_empty(a) ((a)->num_elements == 0)
133 #define element_value(ae) ((ae)->value)
134 #define element_index(ae) ((ae)->ind)
136 #ifndef ALT_ARRAY_IMPLEMENTATION
137 #define element_forw(ae) ((ae)->next)
138 #define element_back(ae) ((ae)->prev)
140 extern arrayind_t
element_forw (ARRAY
*, arrayind_t
);
141 extern arrayind_t
element_back (ARRAY
*, arrayind_t
);
145 #define set_element_value(ae, val) ((ae)->value = (val))
147 #ifdef ALT_ARRAY_IMPLEMENTATION
148 #define set_first_index(a, i) ((a)->first_index = (i))
151 #define set_max_index(a, i) ((a)->max_index = (i))
152 #define set_num_elements(a, n) ((a)->num_elements = (n))
155 #define array_push(a,v) \
156 do { array_rshift ((a), 1, (v)); } while (0)
157 #define array_pop(a) \
158 do { array_shift ((a), 1, AS_DISPOSE); } while (0)
160 #define GET_ARRAY_FROM_VAR(n, v, a) \
162 (v) = find_variable (n); \
163 (a) = ((v) && array_p ((v))) ? array_cell (v) : (ARRAY *)0; \
166 #define ARRAY_ELEMENT_REPLACE(ae, v) \
168 free ((ae)->value); \
172 #ifdef ALT_ARRAY_IMPLEMENTATION
173 #define ARRAY_VALUE_REPLACE(a, i, v) \
174 ARRAY_ELEMENT_REPLACE((a)->elements[(i)], (v))
177 #define ALL_ELEMENT_SUB(c) ((c) == '@' || (c) == '*')
179 /* In eval.c, but uses ARRAY * */
180 extern int execute_array_command (ARRAY
*, void *);
182 #endif /* _ARRAY_H_ */