Commit | Line | Data |
---|---|---|
7263c7a9 PP |
1 | #ifndef BABELTRACE_ARGPAR_H |
2 | #define BABELTRACE_ARGPAR_H | |
3 | ||
4 | /* | |
5 | * Copyright 2019 Philippe Proulx <pproulx@efficios.com> | |
6 | * | |
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
8 | * of this software and associated documentation files (the "Software"), to deal | |
9 | * in the Software without restriction, including without limitation the rights | |
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the Software is | |
12 | * furnished to do so, subject to the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
23 | * SOFTWARE. | |
24 | */ | |
25 | ||
7263c7a9 PP |
26 | #include <stdbool.h> |
27 | ||
7263c7a9 | 28 | /* Sentinel for an option descriptor array */ |
cf8da540 SM |
29 | #define ARGPAR_OPT_DESCR_SENTINEL { -1, '\0', NULL, false } |
30 | ||
31 | /* | |
32 | * ARGPAR_HIDDEN: if argpar is used in some shared library, we don't want them | |
33 | * to be exported by that library, so mark them as "hidden". | |
34 | * | |
35 | * On Windows, symbols are local unless explicitly exported, | |
36 | * see https://gcc.gnu.org/wiki/Visibility | |
37 | */ | |
38 | #if defined(_WIN32) || defined(__CYGWIN__) | |
39 | #define ARGPAR_HIDDEN | |
40 | #else | |
41 | #define ARGPAR_HIDDEN __attribute__((visibility("hidden"))) | |
42 | #endif | |
7263c7a9 PP |
43 | |
44 | /* Option descriptor */ | |
cf8da540 | 45 | struct argpar_opt_descr { |
7263c7a9 PP |
46 | /* Numeric ID for this option */ |
47 | const int id; | |
48 | ||
49 | /* Short option character, or `\0` */ | |
50 | const char short_name; | |
51 | ||
52 | /* Long option name (without `--`), or `NULL` */ | |
53 | const char * const long_name; | |
54 | ||
55 | /* True if this option has an argument */ | |
56 | const bool with_arg; | |
57 | }; | |
58 | ||
59 | /* Item type */ | |
cf8da540 | 60 | enum argpar_item_type { |
7263c7a9 | 61 | /* Option */ |
cf8da540 | 62 | ARGPAR_ITEM_TYPE_OPT, |
7263c7a9 PP |
63 | |
64 | /* Non-option */ | |
cf8da540 | 65 | ARGPAR_ITEM_TYPE_NON_OPT, |
7263c7a9 PP |
66 | }; |
67 | ||
68 | /* Base item */ | |
cf8da540 SM |
69 | struct argpar_item { |
70 | enum argpar_item_type type; | |
7263c7a9 PP |
71 | }; |
72 | ||
73 | /* Option item */ | |
cf8da540 SM |
74 | struct argpar_item_opt { |
75 | struct argpar_item base; | |
7263c7a9 PP |
76 | |
77 | /* Corresponding descriptor */ | |
cf8da540 | 78 | const struct argpar_opt_descr *descr; |
7263c7a9 PP |
79 | |
80 | /* Argument, or `NULL` if none */ | |
81 | const char *arg; | |
82 | }; | |
83 | ||
84 | /* Non-option item */ | |
cf8da540 SM |
85 | struct argpar_item_non_opt { |
86 | struct argpar_item base; | |
7263c7a9 PP |
87 | |
88 | /* | |
89 | * Complete argument, pointing to one of the entries of the | |
90 | * original arguments (`argv`). | |
91 | */ | |
92 | const char *arg; | |
93 | ||
94 | /* Index of this argument amongst all original arguments (`argv`) */ | |
95 | unsigned int orig_index; | |
96 | ||
97 | /* Index of this argument amongst other non-option arguments */ | |
98 | unsigned int non_opt_index; | |
99 | }; | |
100 | ||
cf8da540 SM |
101 | struct argpar_item_array { |
102 | /* Array of `struct argpar_item *`, or `NULL` on error */ | |
103 | struct argpar_item **items; | |
104 | ||
105 | /* Number of used slots in `items`. */ | |
106 | unsigned int n_items; | |
107 | ||
108 | /* Number of allocated slots in `items`. */ | |
109 | unsigned int n_alloc; | |
110 | }; | |
111 | ||
112 | /* What is returned by argpar_parse() */ | |
113 | struct argpar_parse_ret { | |
114 | /* Array of `struct argpar_item *`, or `NULL` on error */ | |
115 | struct argpar_item_array *items; | |
7263c7a9 PP |
116 | |
117 | /* Error string, or `NULL` if none */ | |
cf8da540 | 118 | char *error; |
7263c7a9 PP |
119 | |
120 | /* Number of original arguments (`argv`) ingested */ | |
121 | unsigned int ingested_orig_args; | |
122 | }; | |
123 | ||
124 | /* | |
125 | * Parses the arguments `argv` of which the count is `argc` using the | |
cf8da540 | 126 | * sentinel-terminated (use `ARGPAR_OPT_DESCR_SENTINEL`) option |
7263c7a9 PP |
127 | * descriptor array `descrs`. |
128 | * | |
129 | * This function considers ALL the elements of `argv`, including the | |
130 | * first one, so that you would typically pass `argc - 1` and | |
131 | * `&argv[1]` from what main() receives. | |
132 | * | |
133 | * This argument parser supports: | |
134 | * | |
135 | * * Short options without an argument, possibly tied together: | |
136 | * | |
137 | * -f -auf -n | |
138 | * | |
139 | * * Short options with argument: | |
140 | * | |
141 | * -b 45 -f/mein/file -xyzhello | |
142 | * | |
143 | * * Long options without an argument: | |
144 | * | |
145 | * --five-guys --burger-king --pizza-hut --subway | |
146 | * | |
147 | * * Long options with arguments: | |
148 | * | |
149 | * --security enable --time=18.56 | |
150 | * | |
151 | * * Non-option arguments (anything else). | |
152 | * | |
153 | * This function does not accept `-` or `--` as arguments. The latter | |
154 | * means "end of options" for many command-line tools, but this function | |
155 | * is all about keeping the order of the arguments, so it does not mean | |
156 | * much to put them at the end. This has the side effect that a | |
157 | * non-option argument cannot have the form of an option, for example if | |
158 | * you need to pass the exact relative path `--component`. In that case, | |
159 | * you would need to pass `./--component`. There's no generic way to | |
160 | * escape `-` for the moment. | |
161 | * | |
162 | * This function accepts duplicate options (the resulting array of items | |
163 | * contains one entry for each instance). | |
164 | * | |
165 | * On success, this function returns an array of items | |
cf8da540 SM |
166 | * (`struct argpar_item *`). Each item is to be casted to the |
167 | * appropriate type (`struct argpar_item_opt *` or | |
168 | * `struct argpar_item_non_opt *`) depending on its type. | |
7263c7a9 PP |
169 | * |
170 | * The returned array contains the items in the same order that the | |
171 | * arguments were parsed, including non-option arguments. This means, | |
172 | * for example, that for | |
173 | * | |
174 | * --hello --meow=23 /path/to/file -b | |
175 | * | |
176 | * the function returns an array of four items: two options, one | |
177 | * non-option, and one option. | |
178 | * | |
179 | * In the returned structure, `ingested_orig_args` is the number of | |
180 | * ingested arguments within `argv` to produce the resulting array of | |
181 | * items. If `fail_on_unknown_opt` is true, then on success | |
182 | * `ingested_orig_args` is equal to `argc`. Otherwise, | |
183 | * `ingested_orig_args` contains the number of original arguments until | |
184 | * an unknown _option_ occurs. For example, with | |
185 | * | |
186 | * --great --white contact nuance --shark nuclear | |
187 | * | |
188 | * if `--shark` is not described within `descrs` and | |
189 | * `fail_on_unknown_opt` is false, then `ingested_orig_args` is 4 (two | |
190 | * options, two non-options), whereas `argc` is 6. | |
191 | * | |
192 | * This makes it possible to know where a command name is, for example. | |
193 | * With those arguments: | |
194 | * | |
195 | * --verbose --stuff=23 do-something --specific-opt -f -b | |
196 | * | |
197 | * and the descriptors for `--verbose` and `--stuff` only, the function | |
198 | * returns the `--verbose` and `--stuff` option items, the | |
199 | * `do-something` non-option item, and that three original arguments | |
200 | * were ingested. This means you can start the next argument parsing | |
201 | * stage, with option descriptors depending on the command name, at | |
202 | * `&argv[3]`. | |
203 | * | |
204 | * Note that `ingested_orig_args` is not always equal to the number of | |
205 | * returned items, as | |
206 | * | |
207 | * --hello -fdw | |
208 | * | |
209 | * for example contains two ingested original arguments, but four | |
210 | * resulting items. | |
211 | * | |
212 | * On failure, the returned structure's `items` member is `NULL`, and | |
213 | * the `error` string member contains details about the error. | |
214 | * | |
215 | * You can finalize the returned structure with | |
cf8da540 | 216 | * argpar_parse_ret_fini(). |
7263c7a9 | 217 | */ |
cf8da540 SM |
218 | ARGPAR_HIDDEN |
219 | struct argpar_parse_ret argpar_parse(unsigned int argc, | |
7263c7a9 | 220 | const char * const *argv, |
cf8da540 | 221 | const struct argpar_opt_descr *descrs, |
7263c7a9 PP |
222 | bool fail_on_unknown_opt); |
223 | ||
224 | /* | |
cf8da540 | 225 | * Finalizes what is returned by argpar_parse(). |
7263c7a9 | 226 | * |
cf8da540 | 227 | * It is safe to call argpar_parse() multiple times with the same |
7263c7a9 PP |
228 | * structure. |
229 | */ | |
cf8da540 SM |
230 | ARGPAR_HIDDEN |
231 | void argpar_parse_ret_fini(struct argpar_parse_ret *ret); | |
7263c7a9 PP |
232 | |
233 | #endif /* BABELTRACE_ARGPAR_H */ |