]>
| 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 | |
| b8c60bc9 | 4 | /* Copyright (C) 1997-2022 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 | ||
| b8c60bc9 | 53 | typedef int sh_ae_map_func_t (ARRAY_ELEMENT *, void *); |
| f73dda09 | 54 | |
| 7117c2d2 | 55 | /* Basic operations on entire arrays */ |
| 74091dd4 | 56 | #ifdef ALT_ARRAY_IMPLEMENTATION |
| b8c60bc9 CR |
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 **); | |
| 74091dd4 | 61 | #endif |
| b8c60bc9 CR |
62 | extern ARRAY *array_create (void); |
| 63 | extern void array_flush (ARRAY *); | |
| 64 | extern void array_dispose (ARRAY *); | |
| 65 | extern ARRAY *array_copy (ARRAY *); | |
| 74091dd4 | 66 | #ifndef ALT_ARRAY_IMPLEMENTATION |
| b8c60bc9 | 67 | extern ARRAY *array_slice (ARRAY *, ARRAY_ELEMENT *, ARRAY_ELEMENT *); |
| 74091dd4 | 68 | #else |
| b8c60bc9 | 69 | extern ARRAY *array_slice (ARRAY *, arrayind_t, arrayind_t); |
| 74091dd4 CR |
70 | #endif |
| 71 | ||
| b8c60bc9 | 72 | extern void array_walk (ARRAY *, sh_ae_map_func_t *, void *); |
| 8868edaf | 73 | |
| 74091dd4 | 74 | #ifndef ALT_ARRAY_IMPLEMENTATION |
| b8c60bc9 | 75 | extern ARRAY_ELEMENT *array_shift (ARRAY *, int, int); |
| 74091dd4 | 76 | #else |
| b8c60bc9 | 77 | extern ARRAY_ELEMENT **array_shift (ARRAY *, int, int); |
| 74091dd4 | 78 | #endif |
| b8c60bc9 CR |
79 | extern int array_rshift (ARRAY *, int, char *); |
| 80 | extern ARRAY_ELEMENT *array_unshift_element (ARRAY *); | |
| 81 | extern int array_shift_element (ARRAY *, char *); | |
| 8868edaf | 82 | |
| b8c60bc9 CR |
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 *); | |
| 8868edaf | 88 | |
| b8c60bc9 CR |
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); | |
| ccc6cda3 | 92 | |
| 7117c2d2 | 93 | /* Basic operations on array elements. */ |
| b8c60bc9 CR |
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 *); | |
| ccc6cda3 | 97 | |
| b8c60bc9 CR |
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); | |
| 7117c2d2 JA |
101 | |
| 102 | /* Converting to and from arrays */ | |
| b8c60bc9 CR |
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 *); | |
| b80f6443 | 107 | |
| b8c60bc9 | 108 | extern ARRAY *array_assign_list (ARRAY *, WORD_LIST *); |
| ccc6cda3 | 109 | |
| b8c60bc9 CR |
110 | extern char **array_to_argv (ARRAY *, int *); |
| 111 | extern ARRAY *array_from_argv (ARRAY *, char **, int); | |
| bb70624e | 112 | |
| b8c60bc9 CR |
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 *); | |
| 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 | 139 | #else |
| b8c60bc9 CR |
140 | extern arrayind_t element_forw (ARRAY *, arrayind_t); |
| 141 | extern arrayind_t element_back (ARRAY *, arrayind_t); | |
| 74091dd4 CR |
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 | 179 | /* In eval.c, but uses ARRAY * */ |
| b8c60bc9 | 180 | extern int execute_array_command (ARRAY *, void *); |
| 8868edaf | 181 | |
| ccc6cda3 | 182 | #endif /* _ARRAY_H_ */ |