blob: fb4f789f3f8879002bc02d62342c48c432acd168 [file] [log] [blame]
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001/* array.h -- definitions for the interface exported by array.c that allows
2 the rest of the shell to manipulate array variables. */
Jari Aaltobb706242000-03-17 21:46:59 +00003
Jari Aalto31859422009-01-12 13:36:28 +00004/* Copyright (C) 1997-2009 Free Software Foundation, Inc.
Jari Aaltobb706242000-03-17 21:46:59 +00005
6 This file is part of GNU Bash, the Bourne Again SHell.
7
Jari Aalto31859422009-01-12 13:36:28 +00008 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.
Jari Aaltobb706242000-03-17 21:46:59 +000012
Jari Aalto31859422009-01-12 13:36:28 +000013 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.
Jari Aaltobb706242000-03-17 21:46:59 +000017
Jari Aalto31859422009-01-12 13:36:28 +000018 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
Jari Aaltobb706242000-03-17 21:46:59 +000022
Jari Aaltoccc6cda1996-12-23 17:02:34 +000023#ifndef _ARRAY_H_
24#define _ARRAY_H_
25
26#include "stdc.h"
27
Jari Aalto7117c2d2002-07-17 14:10:11 +000028typedef intmax_t arrayind_t;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000029
30enum atype {array_indexed, array_assoc};
31
32typedef struct array {
33 enum atype type;
Chet Ramey00018032011-11-21 20:51:19 -050034 arrayind_t max_index;
35 int num_elements;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000036 struct array_element *head;
37} ARRAY;
38
39typedef struct array_element {
40 arrayind_t ind;
41 char *value;
42 struct array_element *next, *prev;
43} ARRAY_ELEMENT;
44
Jari Aaltob80f6442004-07-27 13:29:18 +000045typedef int sh_ae_map_func_t __P((ARRAY_ELEMENT *, void *));
Jari Aaltof73dda02001-11-13 17:56:06 +000046
Jari Aalto7117c2d2002-07-17 14:10:11 +000047/* Basic operations on entire arrays */
48extern ARRAY *array_create __P((void));
49extern void array_flush __P((ARRAY *));
50extern void array_dispose __P((ARRAY *));
51extern ARRAY *array_copy __P((ARRAY *));
52extern ARRAY *array_slice __P((ARRAY *, ARRAY_ELEMENT *, ARRAY_ELEMENT *));
Jari Aaltob80f6442004-07-27 13:29:18 +000053extern void array_walk __P((ARRAY *, sh_ae_map_func_t *, void *));
Jari Aaltoccc6cda1996-12-23 17:02:34 +000054
Jari Aalto7117c2d2002-07-17 14:10:11 +000055extern ARRAY_ELEMENT *array_shift __P((ARRAY *, int, int));
56extern int array_rshift __P((ARRAY *, int, char *));
Jari Aaltob80f6442004-07-27 13:29:18 +000057extern ARRAY_ELEMENT *array_unshift_element __P((ARRAY *));
58extern int array_shift_element __P((ARRAY *, char *));
Jari Aalto31859422009-01-12 13:36:28 +000059
Jari Aalto7117c2d2002-07-17 14:10:11 +000060extern ARRAY *array_quote __P((ARRAY *));
Jari Aaltof1be6662008-11-18 13:15:12 +000061extern ARRAY *array_quote_escapes __P((ARRAY *));
Jari Aalto31859422009-01-12 13:36:28 +000062extern ARRAY *array_dequote __P((ARRAY *));
63extern ARRAY *array_dequote_escapes __P((ARRAY *));
64extern ARRAY *array_remove_quoted_nulls __P((ARRAY *));
Jari Aaltoccc6cda1996-12-23 17:02:34 +000065
Jari Aaltob80f6442004-07-27 13:29:18 +000066extern char *array_subrange __P((ARRAY *, arrayind_t, arrayind_t, int, int));
Jari Aalto7117c2d2002-07-17 14:10:11 +000067extern char *array_patsub __P((ARRAY *, char *, char *, int));
Jari Aalto31859422009-01-12 13:36:28 +000068extern char *array_modcase __P((ARRAY *, char *, int, int));
Jari Aaltoccc6cda1996-12-23 17:02:34 +000069
Jari Aalto7117c2d2002-07-17 14:10:11 +000070/* Basic operations on array elements. */
71extern ARRAY_ELEMENT *array_create_element __P((arrayind_t, char *));
72extern ARRAY_ELEMENT *array_copy_element __P((ARRAY_ELEMENT *));
73extern void array_dispose_element __P((ARRAY_ELEMENT *));
Jari Aaltoccc6cda1996-12-23 17:02:34 +000074
Jari Aalto7117c2d2002-07-17 14:10:11 +000075extern int array_insert __P((ARRAY *, arrayind_t, char *));
76extern ARRAY_ELEMENT *array_remove __P((ARRAY *, arrayind_t));
77extern char *array_reference __P((ARRAY *, arrayind_t));
78
79/* Converting to and from arrays */
Jari Aaltoccc6cda1996-12-23 17:02:34 +000080extern WORD_LIST *array_to_word_list __P((ARRAY *));
Jari Aalto7117c2d2002-07-17 14:10:11 +000081extern ARRAY *array_from_word_list __P((WORD_LIST *));
Jari Aaltob80f6442004-07-27 13:29:18 +000082extern WORD_LIST *array_keys_to_word_list __P((ARRAY *));
83
Jari Aalto7117c2d2002-07-17 14:10:11 +000084extern ARRAY *array_assign_list __P((ARRAY *, WORD_LIST *));
Jari Aaltoccc6cda1996-12-23 17:02:34 +000085
Jari Aaltobb706242000-03-17 21:46:59 +000086extern char **array_to_argv __P((ARRAY *));
87
Jari Aalto7117c2d2002-07-17 14:10:11 +000088extern char *array_to_assign __P((ARRAY *, int));
Jari Aaltoccc6cda1996-12-23 17:02:34 +000089extern char *array_to_string __P((ARRAY *, char *, int));
Jari Aalto7117c2d2002-07-17 14:10:11 +000090extern ARRAY *array_from_string __P((char *, char *));
Jari Aaltoccc6cda1996-12-23 17:02:34 +000091
Jari Aalto7117c2d2002-07-17 14:10:11 +000092/* Flags for array_shift */
93#define AS_DISPOSE 0x01
Jari Aaltoccc6cda1996-12-23 17:02:34 +000094
95#define array_num_elements(a) ((a)->num_elements)
96#define array_max_index(a) ((a)->max_index)
97#define array_head(a) ((a)->head)
98#define array_empty(a) ((a)->num_elements == 0)
99
100#define element_value(ae) ((ae)->value)
101#define element_index(ae) ((ae)->ind)
102#define element_forw(ae) ((ae)->next)
103#define element_back(ae) ((ae)->prev)
104
Jari Aaltob80f6442004-07-27 13:29:18 +0000105/* Convenience */
106#define array_push(a,v) \
107 do { array_rshift ((a), 1, (v)); } while (0)
108#define array_pop(a) \
109 do { array_dispose_element (array_shift ((a), 1, 0)); } while (0)
110
111#define GET_ARRAY_FROM_VAR(n, v, a) \
112 do { \
113 (v) = find_variable (n); \
114 (a) = ((v) && array_p ((v))) ? array_cell (v) : (ARRAY *)0; \
115 } while (0)
116
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000117#define ALL_ELEMENT_SUB(c) ((c) == '@' || (c) == '*')
118
119#endif /* _ARRAY_H_ */