Commit | Line | Data |
---|---|---|
d50d200a SM |
1 | /* |
2 | * Copyright (C) 2021 Simon Marchi <simon.marchi@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0-only | |
5 | * | |
6 | */ | |
7 | ||
8 | #include "argpar-utils.h" | |
9 | ||
10 | #include <stdio.h> | |
11 | ||
12 | #include <common/error.h> | |
13 | #include <common/string-utils/string-utils.h> | |
14 | ||
15 | #define WHILE_PARSING_ARG_N_ARG_FMT "While parsing argument #%d (`%s`): " | |
16 | ||
17 | /* | |
18 | * Given argpar error status `status` and error `error`, return a formatted | |
19 | * error message describing the error. | |
20 | * | |
21 | * `argv` is the argument vector that was being parsed. | |
22 | * | |
23 | * `context_fmt`, if non-NULL, is formatted using `args` and prepended to the | |
24 | * error message. | |
25 | * | |
26 | * The returned string must be freed by the caller. | |
27 | */ | |
28 | static ATTR_FORMAT_PRINTF(3, 0) | |
29 | char *format_arg_error_v(const struct argpar_error *error, | |
30 | const char **argv, const char *context_fmt, va_list args) | |
31 | { | |
32 | char *str = NULL; | |
33 | char *str_ret = NULL; | |
34 | int ret; | |
35 | ||
36 | if (context_fmt) { | |
37 | ret = vasprintf(&str, context_fmt, args); | |
38 | if (ret == -1) { | |
39 | /* | |
40 | * If vasprintf fails, the content of str is undefined, | |
41 | * and we shouldn't try to free it. | |
42 | */ | |
43 | str = NULL; | |
44 | goto end; | |
45 | } | |
46 | ||
47 | ret = strutils_append_str(&str, ": "); | |
48 | if (ret < 0) { | |
49 | goto end; | |
50 | } | |
51 | } | |
52 | ||
53 | switch (argpar_error_type(error)) | |
54 | { | |
55 | case ARGPAR_ERROR_TYPE_MISSING_OPT_ARG: | |
56 | { | |
57 | int orig_index = argpar_error_orig_index(error); | |
58 | const char *arg = argv[orig_index]; | |
59 | ||
60 | ret = strutils_appendf(&str, | |
61 | WHILE_PARSING_ARG_N_ARG_FMT "Missing required argument for option `%s`", | |
62 | orig_index + 1, arg, arg); | |
63 | if (ret < 0) { | |
64 | goto end; | |
65 | } | |
66 | ||
67 | break; | |
68 | } | |
69 | case ARGPAR_ERROR_TYPE_UNEXPECTED_OPT_ARG: | |
70 | { | |
71 | bool is_short; | |
72 | const struct argpar_opt_descr *descr = | |
73 | argpar_error_opt_descr(error, &is_short); | |
74 | int orig_index = argpar_error_orig_index(error); | |
75 | const char *arg = argv[orig_index]; | |
76 | ||
77 | if (is_short) { | |
78 | ret = strutils_appendf(&str, | |
79 | WHILE_PARSING_ARG_N_ARG_FMT "Unexpected argument for option `-%c`", | |
80 | orig_index + 1, arg, descr->short_name); | |
81 | } else { | |
82 | ret = strutils_appendf(&str, | |
83 | WHILE_PARSING_ARG_N_ARG_FMT "Unexpected argument for option `--%s`", | |
84 | orig_index + 1, arg, descr->long_name); | |
85 | } | |
86 | ||
87 | if (ret < 0) { | |
88 | goto end; | |
89 | } | |
90 | ||
91 | break; | |
92 | } | |
93 | case ARGPAR_ERROR_TYPE_UNKNOWN_OPT: | |
94 | { | |
95 | const char *unknown_opt = argpar_error_unknown_opt_name(error); | |
96 | ||
97 | ret = strutils_appendf(&str, | |
98 | "Unknown option `%s`", unknown_opt); | |
99 | ||
100 | if (ret < 0) { | |
101 | goto end; | |
102 | } | |
103 | ||
104 | break; | |
105 | } | |
106 | default: | |
107 | abort (); | |
108 | } | |
109 | ||
110 | str_ret = str; | |
111 | str = NULL; | |
112 | ||
113 | end: | |
114 | free(str); | |
115 | return str_ret; | |
116 | } | |
117 | ||
118 | enum parse_next_item_status parse_next_item(struct argpar_iter *iter, | |
119 | const struct argpar_item **item, const char **argv, | |
120 | bool unknown_opt_is_error, const char *context_fmt, ...) | |
121 | { | |
122 | enum argpar_iter_next_status status; | |
123 | const struct argpar_error *error = NULL; | |
124 | enum parse_next_item_status ret; | |
125 | ||
126 | ARGPAR_ITEM_DESTROY_AND_RESET(*item); | |
127 | status = argpar_iter_next(iter, item, &error); | |
128 | ||
129 | switch (status) { | |
130 | case ARGPAR_ITER_NEXT_STATUS_ERROR_MEMORY: | |
131 | ERR("Failed to get next argpar item."); | |
132 | ret = PARSE_NEXT_ITEM_STATUS_ERROR; | |
133 | break; | |
134 | case ARGPAR_ITER_NEXT_STATUS_ERROR: | |
135 | { | |
136 | va_list args; | |
137 | char *err_str; | |
138 | ||
139 | if (argpar_error_type(error) == ARGPAR_ERROR_TYPE_UNKNOWN_OPT && | |
140 | !unknown_opt_is_error) { | |
141 | ret = PARSE_NEXT_ITEM_STATUS_END; | |
142 | break; | |
143 | } | |
144 | ||
145 | va_start(args, context_fmt); | |
146 | err_str = format_arg_error_v(error, argv, context_fmt, args); | |
147 | va_end(args); | |
148 | ||
149 | if (err_str) { | |
150 | ERR("%s", err_str); | |
151 | free(err_str); | |
152 | } else { | |
153 | ERR("%s", "Failed to format argpar error."); | |
154 | } | |
155 | ||
156 | ret = PARSE_NEXT_ITEM_STATUS_ERROR; | |
157 | break; | |
158 | } | |
159 | case ARGPAR_ITER_NEXT_STATUS_END: | |
160 | ret = PARSE_NEXT_ITEM_STATUS_END; | |
161 | break; | |
162 | case ARGPAR_ITER_NEXT_STATUS_OK: | |
163 | ret = PARSE_NEXT_ITEM_STATUS_OK; | |
164 | break; | |
165 | default: | |
166 | abort(); | |
167 | } | |
168 | ||
169 | argpar_error_destroy(error); | |
170 | ||
171 | return ret; | |
172 | } |