]>
Commit | Line | Data |
---|---|---|
ccc6cda3 JA |
1 | /* array.h -- definitions for the interface exported by array.c that allows |
2 | the rest of the shell to manipulate array variables. */ | |
bb70624e | 3 | |
74091dd4 | 4 | /* Copyright (C) 1997-2021 Free Software Foundation, Inc. |
bb70624e JA |
5 | |
6 | This file is part of GNU Bash, the Bourne Again SHell. | |
7 | ||
3185942a JA |
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. | |
bb70624e | 12 | |
3185942a JA |
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 | */ | |
bb70624e | 21 | |
bb70624e | 22 | |
ccc6cda3 JA |
23 | #ifndef _ARRAY_H_ |
24 | #define _ARRAY_H_ | |
25 | ||
26 | #include "stdc.h" | |
27 | ||
7117c2d2 | 28 | typedef intmax_t arrayind_t; |
ccc6cda3 | 29 | |
ccc6cda3 | 30 | typedef struct array { |
0001803f | 31 | arrayind_t max_index; |
74091dd4 CR |
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 | |
ccc6cda3 | 38 | struct array_element *head; |
8868edaf | 39 | struct array_element *lastref; |
74091dd4 | 40 | #endif |
ccc6cda3 JA |
41 | } ARRAY; |
42 | ||
43 | typedef struct array_element { | |
44 | arrayind_t ind; | |
45 | char *value; | |
74091dd4 | 46 | #ifndef ALT_ARRAY_IMPLEMENTATION |
ccc6cda3 | 47 | struct array_element *next, *prev; |
74091dd4 | 48 | #endif |
ccc6cda3 JA |
49 | } ARRAY_ELEMENT; |
50 | ||
74091dd4 CR |
51 | #define ARRAY_DEFAULT_SIZE 1024 |
52 | ||
8868edaf | 53 | typedef int sh_ae_map_func_t PARAMS((ARRAY_ELEMENT *, void *)); |
f73dda09 | 54 | |
7117c2d2 | 55 | /* Basic operations on entire arrays */ |
74091dd4 CR |
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 | |
8868edaf CR |
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 *)); | |
74091dd4 | 66 | #ifndef ALT_ARRAY_IMPLEMENTATION |
8868edaf | 67 | extern ARRAY *array_slice PARAMS((ARRAY *, ARRAY_ELEMENT *, ARRAY_ELEMENT *)); |
74091dd4 CR |
68 | #else |
69 | extern ARRAY *array_slice PARAMS((ARRAY *, arrayind_t, arrayind_t)); | |
70 | #endif | |
71 | ||
8868edaf CR |
72 | extern void array_walk PARAMS((ARRAY *, sh_ae_map_func_t *, void *)); |
73 | ||
74091dd4 | 74 | #ifndef ALT_ARRAY_IMPLEMENTATION |
8868edaf | 75 | extern ARRAY_ELEMENT *array_shift PARAMS((ARRAY *, int, int)); |
74091dd4 CR |
76 | #else |
77 | extern ARRAY_ELEMENT **array_shift PARAMS((ARRAY *, int, int)); | |
78 | #endif | |
8868edaf CR |
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)); | |
ccc6cda3 | 92 | |
7117c2d2 | 93 | /* Basic operations on array elements. */ |
8868edaf CR |
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 *)); | |
ccc6cda3 | 97 | |
8868edaf CR |
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)); | |
7117c2d2 JA |
101 | |
102 | /* Converting to and from arrays */ | |
8868edaf CR |
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 *)); | |
74091dd4 | 106 | extern WORD_LIST *array_to_kvpair_list PARAMS((ARRAY *)); |
b80f6443 | 107 | |
8868edaf | 108 | extern ARRAY *array_assign_list PARAMS((ARRAY *, WORD_LIST *)); |
ccc6cda3 | 109 | |
8868edaf | 110 | extern char **array_to_argv PARAMS((ARRAY *, int *)); |
74091dd4 | 111 | extern ARRAY *array_from_argv PARAMS((ARRAY *, char **, int)); |
bb70624e | 112 | |
8868edaf CR |
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 *)); | |
ccc6cda3 | 117 | |
7117c2d2 JA |
118 | /* Flags for array_shift */ |
119 | #define AS_DISPOSE 0x01 | |
ccc6cda3 JA |
120 | |
121 | #define array_num_elements(a) ((a)->num_elements) | |
122 | #define array_max_index(a) ((a)->max_index) | |
74091dd4 | 123 | #ifndef ALT_ARRAY_IMPLEMENTATION |
d233b485 | 124 | #define array_first_index(a) ((a)->head->next->ind) |
ccc6cda3 | 125 | #define array_head(a) ((a)->head) |
74091dd4 CR |
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 | |
ccc6cda3 JA |
131 | #define array_empty(a) ((a)->num_elements == 0) |
132 | ||
133 | #define element_value(ae) ((ae)->value) | |
134 | #define element_index(ae) ((ae)->ind) | |
74091dd4 CR |
135 | |
136 | #ifndef ALT_ARRAY_IMPLEMENTATION | |
ccc6cda3 JA |
137 | #define element_forw(ae) ((ae)->next) |
138 | #define element_back(ae) ((ae)->prev) | |
74091dd4 CR |
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 | ||
ccc6cda3 | 144 | |
d233b485 CR |
145 | #define set_element_value(ae, val) ((ae)->value = (val)) |
146 | ||
74091dd4 CR |
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 | ||
b80f6443 JA |
154 | /* Convenience */ |
155 | #define array_push(a,v) \ | |
156 | do { array_rshift ((a), 1, (v)); } while (0) | |
157 | #define array_pop(a) \ | |
74091dd4 | 158 | do { array_shift ((a), 1, AS_DISPOSE); } while (0) |
b80f6443 JA |
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 | ||
74091dd4 CR |
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 | ||
ccc6cda3 JA |
177 | #define ALL_ELEMENT_SUB(c) ((c) == '@' || (c) == '*') |
178 | ||
8868edaf CR |
179 | /* In eval.c, but uses ARRAY * */ |
180 | extern int execute_array_command PARAMS((ARRAY *, void *)); | |
181 | ||
ccc6cda3 | 182 | #endif /* _ARRAY_H_ */ |