]> git.ipfire.org Git - thirdparty/bash.git/blob - array.h
4214e8b414933d5b4fd950f5bedd9d75486a8645
[thirdparty/bash.git] / array.h
1 /* array.h -- definitions for the interface exported by array.c that allows
2 the rest of the shell to manipulate array variables. */
3
4 /* Copyright (C) 1997-2021 Free Software Foundation, Inc.
5
6 This file is part of GNU Bash, the Bourne Again SHell.
7
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.
12
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.
17
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/>.
20 */
21
22
23 #ifndef _ARRAY_H_
24 #define _ARRAY_H_
25
26 #include "stdc.h"
27
28 typedef intmax_t arrayind_t;
29
30 typedef struct array {
31 arrayind_t max_index;
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;
37 #else
38 struct array_element *head;
39 struct array_element *lastref;
40 #endif
41 } ARRAY;
42
43 typedef struct array_element {
44 arrayind_t ind;
45 char *value;
46 #ifndef ALT_ARRAY_IMPLEMENTATION
47 struct array_element *next, *prev;
48 #endif
49 } ARRAY_ELEMENT;
50
51 #define ARRAY_DEFAULT_SIZE 1024
52
53 typedef int sh_ae_map_func_t PARAMS((ARRAY_ELEMENT *, void *));
54
55 /* Basic operations on entire arrays */
56 #ifdef ALT_ARRAY_IMPLEMENTATION
57 extern void array_alloc PARAMS((ARRAY *, arrayind_t));
58 extern void array_resize PARAMS((ARRAY *, arrayind_t));
59 extern void array_expand PARAMS((ARRAY *, arrayind_t));
60 extern void array_dispose_elements PARAMS((ARRAY_ELEMENT **));
61 #endif
62 extern ARRAY *array_create PARAMS((void));
63 extern void array_flush PARAMS((ARRAY *));
64 extern void array_dispose PARAMS((ARRAY *));
65 extern ARRAY *array_copy PARAMS((ARRAY *));
66 #ifndef ALT_ARRAY_IMPLEMENTATION
67 extern ARRAY *array_slice PARAMS((ARRAY *, ARRAY_ELEMENT *, ARRAY_ELEMENT *));
68 #else
69 extern ARRAY *array_slice PARAMS((ARRAY *, arrayind_t, arrayind_t));
70 #endif
71
72 extern void array_walk PARAMS((ARRAY *, sh_ae_map_func_t *, void *));
73
74 #ifndef ALT_ARRAY_IMPLEMENTATION
75 extern ARRAY_ELEMENT *array_shift PARAMS((ARRAY *, int, int));
76 #else
77 extern ARRAY_ELEMENT **array_shift PARAMS((ARRAY *, int, int));
78 #endif
79 extern int array_rshift PARAMS((ARRAY *, int, char *));
80 extern ARRAY_ELEMENT *array_unshift_element PARAMS((ARRAY *));
81 extern int array_shift_element PARAMS((ARRAY *, char *));
82
83 extern ARRAY *array_quote PARAMS((ARRAY *));
84 extern ARRAY *array_quote_escapes PARAMS((ARRAY *));
85 extern ARRAY *array_dequote PARAMS((ARRAY *));
86 extern ARRAY *array_dequote_escapes PARAMS((ARRAY *));
87 extern ARRAY *array_remove_quoted_nulls PARAMS((ARRAY *));
88
89 extern char *array_subrange PARAMS((ARRAY *, arrayind_t, arrayind_t, int, int, int));
90 extern char *array_patsub PARAMS((ARRAY *, char *, char *, int));
91 extern char *array_modcase PARAMS((ARRAY *, char *, int, int));
92
93 /* Basic operations on array elements. */
94 extern ARRAY_ELEMENT *array_create_element PARAMS((arrayind_t, char *));
95 extern ARRAY_ELEMENT *array_copy_element PARAMS((ARRAY_ELEMENT *));
96 extern void array_dispose_element PARAMS((ARRAY_ELEMENT *));
97
98 extern int array_insert PARAMS((ARRAY *, arrayind_t, char *));
99 extern ARRAY_ELEMENT *array_remove PARAMS((ARRAY *, arrayind_t));
100 extern char *array_reference PARAMS((ARRAY *, arrayind_t));
101
102 /* Converting to and from arrays */
103 extern WORD_LIST *array_to_word_list PARAMS((ARRAY *));
104 extern ARRAY *array_from_word_list PARAMS((WORD_LIST *));
105 extern WORD_LIST *array_keys_to_word_list PARAMS((ARRAY *));
106 extern WORD_LIST *array_to_kvpair_list PARAMS((ARRAY *));
107
108 extern ARRAY *array_assign_list PARAMS((ARRAY *, WORD_LIST *));
109
110 extern char **array_to_argv PARAMS((ARRAY *, int *));
111 extern ARRAY *array_from_argv PARAMS((ARRAY *, char **, int));
112
113 extern char *array_to_kvpair PARAMS((ARRAY *, int));
114 extern char *array_to_assign PARAMS((ARRAY *, int));
115 extern char *array_to_string PARAMS((ARRAY *, char *, int));
116 extern ARRAY *array_from_string PARAMS((char *, char *));
117
118 /* Flags for array_shift */
119 #define AS_DISPOSE 0x01
120
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)
127 #else
128 #define array_first_index(a) ((a)->first_index)
129 #define array_head(a) ((a)->elements)
130 #endif
131 #define array_empty(a) ((a)->num_elements == 0)
132
133 #define element_value(ae) ((ae)->value)
134 #define element_index(ae) ((ae)->ind)
135
136 #ifndef ALT_ARRAY_IMPLEMENTATION
137 #define element_forw(ae) ((ae)->next)
138 #define element_back(ae) ((ae)->prev)
139 #else
140 extern arrayind_t element_forw PARAMS((ARRAY *, arrayind_t));
141 extern arrayind_t element_back PARAMS((ARRAY *, arrayind_t));
142 #endif
143
144
145 #define set_element_value(ae, val) ((ae)->value = (val))
146
147 #ifdef ALT_ARRAY_IMPLEMENTATION
148 #define set_first_index(a, i) ((a)->first_index = (i))
149 #endif
150
151 #define set_max_index(a, i) ((a)->max_index = (i))
152 #define set_num_elements(a, n) ((a)->num_elements = (n))
153
154 /* Convenience */
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)
159
160 #define GET_ARRAY_FROM_VAR(n, v, a) \
161 do { \
162 (v) = find_variable (n); \
163 (a) = ((v) && array_p ((v))) ? array_cell (v) : (ARRAY *)0; \
164 } while (0)
165
166 #define ARRAY_ELEMENT_REPLACE(ae, v) \
167 do { \
168 free ((ae)->value); \
169 (ae)->value = (v); \
170 } while (0)
171
172 #ifdef ALT_ARRAY_IMPLEMENTATION
173 #define ARRAY_VALUE_REPLACE(a, i, v) \
174 ARRAY_ELEMENT_REPLACE((a)->elements[(i)], (v))
175 #endif
176
177 #define ALL_ELEMENT_SUB(c) ((c) == '@' || (c) == '*')
178
179 /* In eval.c, but uses ARRAY * */
180 extern int execute_array_command PARAMS((ARRAY *, void *));
181
182 #endif /* _ARRAY_H_ */