]>
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 | |
8868edaf | 4 | /* Copyright (C) 1997-2020 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 | |
8868edaf | 30 | enum atype {array_indexed, array_assoc}; /* only array_indexed used */ |
ccc6cda3 JA |
31 | |
32 | typedef struct array { | |
33 | enum atype type; | |
0001803f CR |
34 | arrayind_t max_index; |
35 | int num_elements; | |
ccc6cda3 | 36 | struct array_element *head; |
8868edaf | 37 | struct array_element *lastref; |
ccc6cda3 JA |
38 | } ARRAY; |
39 | ||
40 | typedef struct array_element { | |
41 | arrayind_t ind; | |
42 | char *value; | |
43 | struct array_element *next, *prev; | |
44 | } ARRAY_ELEMENT; | |
45 | ||
8868edaf | 46 | typedef int sh_ae_map_func_t PARAMS((ARRAY_ELEMENT *, void *)); |
f73dda09 | 47 | |
7117c2d2 | 48 | /* Basic operations on entire arrays */ |
8868edaf CR |
49 | extern ARRAY *array_create PARAMS((void)); |
50 | extern void array_flush PARAMS((ARRAY *)); | |
51 | extern void array_dispose PARAMS((ARRAY *)); | |
52 | extern ARRAY *array_copy PARAMS((ARRAY *)); | |
53 | extern ARRAY *array_slice PARAMS((ARRAY *, ARRAY_ELEMENT *, ARRAY_ELEMENT *)); | |
54 | extern void array_walk PARAMS((ARRAY *, sh_ae_map_func_t *, void *)); | |
55 | ||
56 | extern ARRAY_ELEMENT *array_shift PARAMS((ARRAY *, int, int)); | |
57 | extern int array_rshift PARAMS((ARRAY *, int, char *)); | |
58 | extern ARRAY_ELEMENT *array_unshift_element PARAMS((ARRAY *)); | |
59 | extern int array_shift_element PARAMS((ARRAY *, char *)); | |
60 | ||
61 | extern ARRAY *array_quote PARAMS((ARRAY *)); | |
62 | extern ARRAY *array_quote_escapes PARAMS((ARRAY *)); | |
63 | extern ARRAY *array_dequote PARAMS((ARRAY *)); | |
64 | extern ARRAY *array_dequote_escapes PARAMS((ARRAY *)); | |
65 | extern ARRAY *array_remove_quoted_nulls PARAMS((ARRAY *)); | |
66 | ||
67 | extern char *array_subrange PARAMS((ARRAY *, arrayind_t, arrayind_t, int, int, int)); | |
68 | extern char *array_patsub PARAMS((ARRAY *, char *, char *, int)); | |
69 | extern char *array_modcase PARAMS((ARRAY *, char *, int, int)); | |
ccc6cda3 | 70 | |
7117c2d2 | 71 | /* Basic operations on array elements. */ |
8868edaf CR |
72 | extern ARRAY_ELEMENT *array_create_element PARAMS((arrayind_t, char *)); |
73 | extern ARRAY_ELEMENT *array_copy_element PARAMS((ARRAY_ELEMENT *)); | |
74 | extern void array_dispose_element PARAMS((ARRAY_ELEMENT *)); | |
ccc6cda3 | 75 | |
8868edaf CR |
76 | extern int array_insert PARAMS((ARRAY *, arrayind_t, char *)); |
77 | extern ARRAY_ELEMENT *array_remove PARAMS((ARRAY *, arrayind_t)); | |
78 | extern char *array_reference PARAMS((ARRAY *, arrayind_t)); | |
7117c2d2 JA |
79 | |
80 | /* Converting to and from arrays */ | |
8868edaf CR |
81 | extern WORD_LIST *array_to_word_list PARAMS((ARRAY *)); |
82 | extern ARRAY *array_from_word_list PARAMS((WORD_LIST *)); | |
83 | extern WORD_LIST *array_keys_to_word_list PARAMS((ARRAY *)); | |
b80f6443 | 84 | |
8868edaf | 85 | extern ARRAY *array_assign_list PARAMS((ARRAY *, WORD_LIST *)); |
ccc6cda3 | 86 | |
8868edaf | 87 | extern char **array_to_argv PARAMS((ARRAY *, int *)); |
bb70624e | 88 | |
8868edaf CR |
89 | extern char *array_to_kvpair PARAMS((ARRAY *, int)); |
90 | extern char *array_to_assign PARAMS((ARRAY *, int)); | |
91 | extern char *array_to_string PARAMS((ARRAY *, char *, int)); | |
92 | extern ARRAY *array_from_string PARAMS((char *, char *)); | |
ccc6cda3 | 93 | |
7117c2d2 JA |
94 | /* Flags for array_shift */ |
95 | #define AS_DISPOSE 0x01 | |
ccc6cda3 JA |
96 | |
97 | #define array_num_elements(a) ((a)->num_elements) | |
98 | #define array_max_index(a) ((a)->max_index) | |
d233b485 | 99 | #define array_first_index(a) ((a)->head->next->ind) |
ccc6cda3 JA |
100 | #define array_head(a) ((a)->head) |
101 | #define array_empty(a) ((a)->num_elements == 0) | |
102 | ||
103 | #define element_value(ae) ((ae)->value) | |
104 | #define element_index(ae) ((ae)->ind) | |
105 | #define element_forw(ae) ((ae)->next) | |
106 | #define element_back(ae) ((ae)->prev) | |
107 | ||
d233b485 CR |
108 | #define set_element_value(ae, val) ((ae)->value = (val)) |
109 | ||
b80f6443 JA |
110 | /* Convenience */ |
111 | #define array_push(a,v) \ | |
112 | do { array_rshift ((a), 1, (v)); } while (0) | |
113 | #define array_pop(a) \ | |
114 | do { array_dispose_element (array_shift ((a), 1, 0)); } while (0) | |
115 | ||
116 | #define GET_ARRAY_FROM_VAR(n, v, a) \ | |
117 | do { \ | |
118 | (v) = find_variable (n); \ | |
119 | (a) = ((v) && array_p ((v))) ? array_cell (v) : (ARRAY *)0; \ | |
120 | } while (0) | |
121 | ||
ccc6cda3 JA |
122 | #define ALL_ELEMENT_SUB(c) ((c) == '@' || (c) == '*') |
123 | ||
8868edaf CR |
124 | /* In eval.c, but uses ARRAY * */ |
125 | extern int execute_array_command PARAMS((ARRAY *, void *)); | |
126 | ||
ccc6cda3 | 127 | #endif /* _ARRAY_H_ */ |