diff options
Diffstat (limited to 'src/lib/libcrypto/ui/ui_lib.c')
-rw-r--r-- | src/lib/libcrypto/ui/ui_lib.c | 1022 |
1 files changed, 498 insertions, 524 deletions
diff --git a/src/lib/libcrypto/ui/ui_lib.c b/src/lib/libcrypto/ui/ui_lib.c index d3cadd51f6..32fb9d7dbb 100644 --- a/src/lib/libcrypto/ui/ui_lib.c +++ b/src/lib/libcrypto/ui/ui_lib.c | |||
@@ -10,7 +10,7 @@ | |||
10 | * are met: | 10 | * are met: |
11 | * | 11 | * |
12 | * 1. Redistributions of source code must retain the above copyright | 12 | * 1. Redistributions of source code must retain the above copyright |
13 | * notice, this list of conditions and the following disclaimer. | 13 | * notice, this list of conditions and the following disclaimer. |
14 | * | 14 | * |
15 | * 2. Redistributions in binary form must reproduce the above copyright | 15 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in | 16 | * notice, this list of conditions and the following disclaimer in |
@@ -65,359 +65,337 @@ | |||
65 | #include "ui_locl.h" | 65 | #include "ui_locl.h" |
66 | 66 | ||
67 | IMPLEMENT_STACK_OF(UI_STRING_ST) | 67 | IMPLEMENT_STACK_OF(UI_STRING_ST) |
68 | static const UI_METHOD *default_UI_meth = NULL; | ||
68 | 69 | ||
69 | static const UI_METHOD *default_UI_meth=NULL; | 70 | UI *UI_new(void) |
71 | { | ||
72 | return (UI_new_method(NULL)); | ||
73 | } | ||
70 | 74 | ||
71 | UI *UI_new(void) | 75 | UI * |
72 | { | 76 | UI_new_method(const UI_METHOD * method) |
73 | return(UI_new_method(NULL)); | 77 | { |
74 | } | ||
75 | |||
76 | UI *UI_new_method(const UI_METHOD *method) | ||
77 | { | ||
78 | UI *ret; | 78 | UI *ret; |
79 | 79 | ||
80 | ret=(UI *)malloc(sizeof(UI)); | 80 | ret = (UI *) malloc(sizeof(UI)); |
81 | if (ret == NULL) | 81 | if (ret == NULL) { |
82 | { | 82 | UIerr(UI_F_UI_NEW_METHOD, ERR_R_MALLOC_FAILURE); |
83 | UIerr(UI_F_UI_NEW_METHOD,ERR_R_MALLOC_FAILURE); | ||
84 | return NULL; | 83 | return NULL; |
85 | } | 84 | } |
86 | if (method == NULL) | 85 | if (method == NULL) |
87 | ret->meth=UI_get_default_method(); | 86 | ret->meth = UI_get_default_method(); |
88 | else | 87 | else |
89 | ret->meth=method; | 88 | ret->meth = method; |
90 | 89 | ||
91 | ret->strings=NULL; | 90 | ret->strings = NULL; |
92 | ret->user_data=NULL; | 91 | ret->user_data = NULL; |
93 | ret->flags=0; | 92 | ret->flags = 0; |
94 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data); | 93 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data); |
95 | return ret; | 94 | return ret; |
96 | } | 95 | } |
97 | 96 | ||
98 | static void free_string(UI_STRING *uis) | 97 | static void |
99 | { | 98 | free_string(UI_STRING * uis) |
100 | if (uis->flags & OUT_STRING_FREEABLE) | 99 | { |
101 | { | 100 | if (uis->flags & OUT_STRING_FREEABLE) { |
102 | free((char *)uis->out_string); | 101 | free((char *) uis->out_string); |
103 | switch(uis->type) | 102 | switch (uis->type) { |
104 | { | ||
105 | case UIT_BOOLEAN: | 103 | case UIT_BOOLEAN: |
106 | free((char *)uis->_.boolean_data.action_desc); | 104 | free((char *) uis->_.boolean_data.action_desc); |
107 | free((char *)uis->_.boolean_data.ok_chars); | 105 | free((char *) uis->_.boolean_data.ok_chars); |
108 | free((char *)uis->_.boolean_data.cancel_chars); | 106 | free((char *) uis->_.boolean_data.cancel_chars); |
109 | break; | 107 | break; |
110 | default: | 108 | default: |
111 | break; | 109 | break; |
112 | } | ||
113 | } | 110 | } |
114 | free(uis); | ||
115 | } | 111 | } |
112 | free(uis); | ||
113 | } | ||
116 | 114 | ||
117 | void UI_free(UI *ui) | 115 | void |
118 | { | 116 | UI_free(UI * ui) |
117 | { | ||
119 | if (ui == NULL) | 118 | if (ui == NULL) |
120 | return; | 119 | return; |
121 | sk_UI_STRING_pop_free(ui->strings,free_string); | 120 | sk_UI_STRING_pop_free(ui->strings, free_string); |
122 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI, ui, &ui->ex_data); | 121 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI, ui, &ui->ex_data); |
123 | free(ui); | 122 | free(ui); |
124 | } | 123 | } |
125 | 124 | ||
126 | static int allocate_string_stack(UI *ui) | 125 | static int |
127 | { | 126 | allocate_string_stack(UI * ui) |
128 | if (ui->strings == NULL) | 127 | { |
129 | { | 128 | if (ui->strings == NULL) { |
130 | ui->strings=sk_UI_STRING_new_null(); | 129 | ui->strings = sk_UI_STRING_new_null(); |
131 | if (ui->strings == NULL) | 130 | if (ui->strings == NULL) { |
132 | { | ||
133 | return -1; | 131 | return -1; |
134 | } | ||
135 | } | 132 | } |
136 | return 0; | ||
137 | } | 133 | } |
134 | return 0; | ||
135 | } | ||
138 | 136 | ||
139 | static UI_STRING *general_allocate_prompt(UI *ui, const char *prompt, | 137 | static UI_STRING * |
140 | int prompt_freeable, enum UI_string_types type, int input_flags, | 138 | general_allocate_prompt(UI * ui, const char *prompt, |
141 | char *result_buf) | 139 | int prompt_freeable, enum UI_string_types type, int input_flags, |
142 | { | 140 | char *result_buf) |
141 | { | ||
143 | UI_STRING *ret = NULL; | 142 | UI_STRING *ret = NULL; |
144 | 143 | ||
145 | if (prompt == NULL) | 144 | if (prompt == NULL) { |
146 | { | 145 | UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, ERR_R_PASSED_NULL_PARAMETER); |
147 | UIerr(UI_F_GENERAL_ALLOCATE_PROMPT,ERR_R_PASSED_NULL_PARAMETER); | 146 | } else if ((type == UIT_PROMPT || type == UIT_VERIFY |
148 | } | 147 | || type == UIT_BOOLEAN) && result_buf == NULL) { |
149 | else if ((type == UIT_PROMPT || type == UIT_VERIFY | 148 | UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, UI_R_NO_RESULT_BUFFER); |
150 | || type == UIT_BOOLEAN) && result_buf == NULL) | 149 | } else if ((ret = (UI_STRING *) malloc(sizeof(UI_STRING)))) { |
151 | { | 150 | ret->out_string = prompt; |
152 | UIerr(UI_F_GENERAL_ALLOCATE_PROMPT,UI_R_NO_RESULT_BUFFER); | 151 | ret->flags = prompt_freeable ? OUT_STRING_FREEABLE : 0; |
153 | } | 152 | ret->input_flags = input_flags; |
154 | else if ((ret = (UI_STRING *)malloc(sizeof(UI_STRING)))) | 153 | ret->type = type; |
155 | { | 154 | ret->result_buf = result_buf; |
156 | ret->out_string=prompt; | ||
157 | ret->flags=prompt_freeable ? OUT_STRING_FREEABLE : 0; | ||
158 | ret->input_flags=input_flags; | ||
159 | ret->type=type; | ||
160 | ret->result_buf=result_buf; | ||
161 | } | ||
162 | return ret; | ||
163 | } | 155 | } |
156 | return ret; | ||
157 | } | ||
164 | 158 | ||
165 | static int general_allocate_string(UI *ui, const char *prompt, | 159 | static int |
166 | int prompt_freeable, enum UI_string_types type, int input_flags, | 160 | general_allocate_string(UI * ui, const char *prompt, |
167 | char *result_buf, int minsize, int maxsize, const char *test_buf) | 161 | int prompt_freeable, enum UI_string_types type, int input_flags, |
168 | { | 162 | char *result_buf, int minsize, int maxsize, const char *test_buf) |
163 | { | ||
169 | int ret = -1; | 164 | int ret = -1; |
170 | UI_STRING *s = general_allocate_prompt(ui, prompt, prompt_freeable, | 165 | UI_STRING *s = general_allocate_prompt(ui, prompt, prompt_freeable, |
171 | type, input_flags, result_buf); | 166 | type, input_flags, result_buf); |
172 | 167 | ||
173 | if (s) | 168 | if (s) { |
174 | { | 169 | if (allocate_string_stack(ui) >= 0) { |
175 | if (allocate_string_stack(ui) >= 0) | 170 | s->_.string_data.result_minsize = minsize; |
176 | { | 171 | s->_.string_data.result_maxsize = maxsize; |
177 | s->_.string_data.result_minsize=minsize; | 172 | s->_.string_data.test_buf = test_buf; |
178 | s->_.string_data.result_maxsize=maxsize; | 173 | ret = sk_UI_STRING_push(ui->strings, s); |
179 | s->_.string_data.test_buf=test_buf; | ||
180 | ret=sk_UI_STRING_push(ui->strings, s); | ||
181 | /* sk_push() returns 0 on error. Let's addapt that */ | 174 | /* sk_push() returns 0 on error. Let's addapt that */ |
182 | if (ret <= 0) ret--; | 175 | if (ret <= 0) |
183 | } | 176 | ret--; |
184 | else | 177 | } else |
185 | free_string(s); | 178 | free_string(s); |
186 | } | ||
187 | return ret; | ||
188 | } | 179 | } |
189 | 180 | return ret; | |
190 | static int general_allocate_boolean(UI *ui, | 181 | } |
191 | const char *prompt, const char *action_desc, | 182 | |
192 | const char *ok_chars, const char *cancel_chars, | 183 | static int |
193 | int prompt_freeable, enum UI_string_types type, int input_flags, | 184 | general_allocate_boolean(UI * ui, |
194 | char *result_buf) | 185 | const char *prompt, const char *action_desc, |
195 | { | 186 | const char *ok_chars, const char *cancel_chars, |
187 | int prompt_freeable, enum UI_string_types type, int input_flags, | ||
188 | char *result_buf) | ||
189 | { | ||
196 | int ret = -1; | 190 | int ret = -1; |
197 | UI_STRING *s; | 191 | UI_STRING *s; |
198 | const char *p; | 192 | const char *p; |
199 | 193 | ||
200 | if (ok_chars == NULL) | 194 | if (ok_chars == NULL) { |
201 | { | 195 | UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER); |
202 | UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,ERR_R_PASSED_NULL_PARAMETER); | 196 | } else if (cancel_chars == NULL) { |
203 | } | 197 | UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER); |
204 | else if (cancel_chars == NULL) | 198 | } else { |
205 | { | 199 | for (p = ok_chars; *p; p++) { |
206 | UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,ERR_R_PASSED_NULL_PARAMETER); | 200 | if (strchr(cancel_chars, *p)) { |
207 | } | ||
208 | else | ||
209 | { | ||
210 | for(p = ok_chars; *p; p++) | ||
211 | { | ||
212 | if (strchr(cancel_chars, *p)) | ||
213 | { | ||
214 | UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, | 201 | UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, |
215 | UI_R_COMMON_OK_AND_CANCEL_CHARACTERS); | 202 | UI_R_COMMON_OK_AND_CANCEL_CHARACTERS); |
216 | } | ||
217 | } | 203 | } |
204 | } | ||
218 | 205 | ||
219 | s = general_allocate_prompt(ui, prompt, prompt_freeable, | 206 | s = general_allocate_prompt(ui, prompt, prompt_freeable, |
220 | type, input_flags, result_buf); | 207 | type, input_flags, result_buf); |
221 | 208 | ||
222 | if (s) | 209 | if (s) { |
223 | { | 210 | if (allocate_string_stack(ui) >= 0) { |
224 | if (allocate_string_stack(ui) >= 0) | ||
225 | { | ||
226 | s->_.boolean_data.action_desc = action_desc; | 211 | s->_.boolean_data.action_desc = action_desc; |
227 | s->_.boolean_data.ok_chars = ok_chars; | 212 | s->_.boolean_data.ok_chars = ok_chars; |
228 | s->_.boolean_data.cancel_chars = cancel_chars; | 213 | s->_.boolean_data.cancel_chars = cancel_chars; |
229 | ret=sk_UI_STRING_push(ui->strings, s); | 214 | ret = sk_UI_STRING_push(ui->strings, s); |
230 | /* sk_push() returns 0 on error. | 215 | /* |
231 | Let's addapt that */ | 216 | * sk_push() returns 0 on error. Let's addapt |
232 | if (ret <= 0) ret--; | 217 | * that |
233 | } | 218 | */ |
234 | else | 219 | if (ret <= 0) |
220 | ret--; | ||
221 | } else | ||
235 | free_string(s); | 222 | free_string(s); |
236 | } | ||
237 | } | 223 | } |
238 | return ret; | ||
239 | } | 224 | } |
225 | return ret; | ||
226 | } | ||
240 | 227 | ||
241 | /* Returns the index to the place in the stack or -1 for error. Uses a | 228 | /* Returns the index to the place in the stack or -1 for error. Uses a |
242 | direct reference to the prompt. */ | 229 | direct reference to the prompt. */ |
243 | int UI_add_input_string(UI *ui, const char *prompt, int flags, | 230 | int |
244 | char *result_buf, int minsize, int maxsize) | 231 | UI_add_input_string(UI * ui, const char *prompt, int flags, |
245 | { | 232 | char *result_buf, int minsize, int maxsize) |
233 | { | ||
246 | return general_allocate_string(ui, prompt, 0, | 234 | return general_allocate_string(ui, prompt, 0, |
247 | UIT_PROMPT, flags, result_buf, minsize, maxsize, NULL); | 235 | UIT_PROMPT, flags, result_buf, minsize, maxsize, NULL); |
248 | } | 236 | } |
249 | 237 | ||
250 | /* Same as UI_add_input_string(), excepts it takes a copy of the prompt */ | 238 | /* Same as UI_add_input_string(), excepts it takes a copy of the prompt */ |
251 | int UI_dup_input_string(UI *ui, const char *prompt, int flags, | 239 | int |
252 | char *result_buf, int minsize, int maxsize) | 240 | UI_dup_input_string(UI * ui, const char *prompt, int flags, |
253 | { | 241 | char *result_buf, int minsize, int maxsize) |
254 | char *prompt_copy=NULL; | 242 | { |
243 | char *prompt_copy = NULL; | ||
255 | 244 | ||
256 | if (prompt) | 245 | if (prompt) { |
257 | { | 246 | prompt_copy = BUF_strdup(prompt); |
258 | prompt_copy=BUF_strdup(prompt); | 247 | if (prompt_copy == NULL) { |
259 | if (prompt_copy == NULL) | 248 | UIerr(UI_F_UI_DUP_INPUT_STRING, ERR_R_MALLOC_FAILURE); |
260 | { | ||
261 | UIerr(UI_F_UI_DUP_INPUT_STRING,ERR_R_MALLOC_FAILURE); | ||
262 | return 0; | 249 | return 0; |
263 | } | ||
264 | } | 250 | } |
265 | |||
266 | return general_allocate_string(ui, prompt_copy, 1, | ||
267 | UIT_PROMPT, flags, result_buf, minsize, maxsize, NULL); | ||
268 | } | 251 | } |
252 | return general_allocate_string(ui, prompt_copy, 1, | ||
253 | UIT_PROMPT, flags, result_buf, minsize, maxsize, NULL); | ||
254 | } | ||
269 | 255 | ||
270 | int UI_add_verify_string(UI *ui, const char *prompt, int flags, | 256 | int |
271 | char *result_buf, int minsize, int maxsize, const char *test_buf) | 257 | UI_add_verify_string(UI * ui, const char *prompt, int flags, |
272 | { | 258 | char *result_buf, int minsize, int maxsize, const char *test_buf) |
259 | { | ||
273 | return general_allocate_string(ui, prompt, 0, | 260 | return general_allocate_string(ui, prompt, 0, |
274 | UIT_VERIFY, flags, result_buf, minsize, maxsize, test_buf); | 261 | UIT_VERIFY, flags, result_buf, minsize, maxsize, test_buf); |
275 | } | 262 | } |
276 | 263 | ||
277 | int UI_dup_verify_string(UI *ui, const char *prompt, int flags, | 264 | int |
278 | char *result_buf, int minsize, int maxsize, const char *test_buf) | 265 | UI_dup_verify_string(UI * ui, const char *prompt, int flags, |
279 | { | 266 | char *result_buf, int minsize, int maxsize, const char *test_buf) |
280 | char *prompt_copy=NULL; | 267 | { |
268 | char *prompt_copy = NULL; | ||
281 | 269 | ||
282 | if (prompt) | 270 | if (prompt) { |
283 | { | 271 | prompt_copy = BUF_strdup(prompt); |
284 | prompt_copy=BUF_strdup(prompt); | 272 | if (prompt_copy == NULL) { |
285 | if (prompt_copy == NULL) | 273 | UIerr(UI_F_UI_DUP_VERIFY_STRING, ERR_R_MALLOC_FAILURE); |
286 | { | ||
287 | UIerr(UI_F_UI_DUP_VERIFY_STRING,ERR_R_MALLOC_FAILURE); | ||
288 | return -1; | 274 | return -1; |
289 | } | ||
290 | } | 275 | } |
291 | |||
292 | return general_allocate_string(ui, prompt_copy, 1, | ||
293 | UIT_VERIFY, flags, result_buf, minsize, maxsize, test_buf); | ||
294 | } | 276 | } |
295 | 277 | return general_allocate_string(ui, prompt_copy, 1, | |
296 | int UI_add_input_boolean(UI *ui, const char *prompt, const char *action_desc, | 278 | UIT_VERIFY, flags, result_buf, minsize, maxsize, test_buf); |
297 | const char *ok_chars, const char *cancel_chars, | 279 | } |
298 | int flags, char *result_buf) | 280 | |
299 | { | 281 | int |
282 | UI_add_input_boolean(UI * ui, const char *prompt, const char *action_desc, | ||
283 | const char *ok_chars, const char *cancel_chars, | ||
284 | int flags, char *result_buf) | ||
285 | { | ||
300 | return general_allocate_boolean(ui, prompt, action_desc, | 286 | return general_allocate_boolean(ui, prompt, action_desc, |
301 | ok_chars, cancel_chars, 0, UIT_BOOLEAN, flags, result_buf); | 287 | ok_chars, cancel_chars, 0, UIT_BOOLEAN, flags, result_buf); |
302 | } | 288 | } |
303 | 289 | ||
304 | int UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc, | 290 | int |
305 | const char *ok_chars, const char *cancel_chars, | 291 | UI_dup_input_boolean(UI * ui, const char *prompt, const char *action_desc, |
306 | int flags, char *result_buf) | 292 | const char *ok_chars, const char *cancel_chars, |
307 | { | 293 | int flags, char *result_buf) |
294 | { | ||
308 | char *prompt_copy = NULL; | 295 | char *prompt_copy = NULL; |
309 | char *action_desc_copy = NULL; | 296 | char *action_desc_copy = NULL; |
310 | char *ok_chars_copy = NULL; | 297 | char *ok_chars_copy = NULL; |
311 | char *cancel_chars_copy = NULL; | 298 | char *cancel_chars_copy = NULL; |
312 | 299 | ||
313 | if (prompt) | 300 | if (prompt) { |
314 | { | 301 | prompt_copy = BUF_strdup(prompt); |
315 | prompt_copy=BUF_strdup(prompt); | 302 | if (prompt_copy == NULL) { |
316 | if (prompt_copy == NULL) | 303 | UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE); |
317 | { | ||
318 | UIerr(UI_F_UI_DUP_INPUT_BOOLEAN,ERR_R_MALLOC_FAILURE); | ||
319 | goto err; | 304 | goto err; |
320 | } | ||
321 | } | 305 | } |
322 | 306 | } | |
323 | if (action_desc) | 307 | if (action_desc) { |
324 | { | 308 | action_desc_copy = BUF_strdup(action_desc); |
325 | action_desc_copy=BUF_strdup(action_desc); | 309 | if (action_desc_copy == NULL) { |
326 | if (action_desc_copy == NULL) | 310 | UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE); |
327 | { | ||
328 | UIerr(UI_F_UI_DUP_INPUT_BOOLEAN,ERR_R_MALLOC_FAILURE); | ||
329 | goto err; | 311 | goto err; |
330 | } | ||
331 | } | 312 | } |
332 | 313 | } | |
333 | if (ok_chars) | 314 | if (ok_chars) { |
334 | { | 315 | ok_chars_copy = BUF_strdup(ok_chars); |
335 | ok_chars_copy=BUF_strdup(ok_chars); | 316 | if (ok_chars_copy == NULL) { |
336 | if (ok_chars_copy == NULL) | 317 | UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE); |
337 | { | ||
338 | UIerr(UI_F_UI_DUP_INPUT_BOOLEAN,ERR_R_MALLOC_FAILURE); | ||
339 | goto err; | 318 | goto err; |
340 | } | ||
341 | } | 319 | } |
342 | 320 | } | |
343 | if (cancel_chars) | 321 | if (cancel_chars) { |
344 | { | 322 | cancel_chars_copy = BUF_strdup(cancel_chars); |
345 | cancel_chars_copy=BUF_strdup(cancel_chars); | 323 | if (cancel_chars_copy == NULL) { |
346 | if (cancel_chars_copy == NULL) | 324 | UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE); |
347 | { | ||
348 | UIerr(UI_F_UI_DUP_INPUT_BOOLEAN,ERR_R_MALLOC_FAILURE); | ||
349 | goto err; | 325 | goto err; |
350 | } | ||
351 | } | 326 | } |
352 | 327 | } | |
353 | return general_allocate_boolean(ui, prompt_copy, action_desc_copy, | 328 | return general_allocate_boolean(ui, prompt_copy, action_desc_copy, |
354 | ok_chars_copy, cancel_chars_copy, 1, UIT_BOOLEAN, flags, | 329 | ok_chars_copy, cancel_chars_copy, 1, UIT_BOOLEAN, flags, |
355 | result_buf); | 330 | result_buf); |
356 | err: | 331 | err: |
357 | if (prompt_copy) free(prompt_copy); | 332 | if (prompt_copy) |
358 | if (action_desc_copy) free(action_desc_copy); | 333 | free(prompt_copy); |
359 | if (ok_chars_copy) free(ok_chars_copy); | 334 | if (action_desc_copy) |
360 | if (cancel_chars_copy) free(cancel_chars_copy); | 335 | free(action_desc_copy); |
336 | if (ok_chars_copy) | ||
337 | free(ok_chars_copy); | ||
338 | if (cancel_chars_copy) | ||
339 | free(cancel_chars_copy); | ||
361 | return -1; | 340 | return -1; |
362 | } | 341 | } |
363 | 342 | ||
364 | int UI_add_info_string(UI *ui, const char *text) | 343 | int |
365 | { | 344 | UI_add_info_string(UI * ui, const char *text) |
345 | { | ||
366 | return general_allocate_string(ui, text, 0, UIT_INFO, 0, NULL, 0, 0, | 346 | return general_allocate_string(ui, text, 0, UIT_INFO, 0, NULL, 0, 0, |
367 | NULL); | 347 | NULL); |
368 | } | 348 | } |
369 | 349 | ||
370 | int UI_dup_info_string(UI *ui, const char *text) | 350 | int |
371 | { | 351 | UI_dup_info_string(UI * ui, const char *text) |
372 | char *text_copy=NULL; | 352 | { |
373 | 353 | char *text_copy = NULL; | |
374 | if (text) | 354 | |
375 | { | 355 | if (text) { |
376 | text_copy=BUF_strdup(text); | 356 | text_copy = BUF_strdup(text); |
377 | if (text_copy == NULL) | 357 | if (text_copy == NULL) { |
378 | { | 358 | UIerr(UI_F_UI_DUP_INFO_STRING, ERR_R_MALLOC_FAILURE); |
379 | UIerr(UI_F_UI_DUP_INFO_STRING,ERR_R_MALLOC_FAILURE); | ||
380 | return -1; | 359 | return -1; |
381 | } | ||
382 | } | 360 | } |
383 | |||
384 | return general_allocate_string(ui, text_copy, 1, UIT_INFO, 0, NULL, | ||
385 | 0, 0, NULL); | ||
386 | } | 361 | } |
362 | return general_allocate_string(ui, text_copy, 1, UIT_INFO, 0, NULL, | ||
363 | 0, 0, NULL); | ||
364 | } | ||
387 | 365 | ||
388 | int UI_add_error_string(UI *ui, const char *text) | 366 | int |
389 | { | 367 | UI_add_error_string(UI * ui, const char *text) |
368 | { | ||
390 | return general_allocate_string(ui, text, 0, UIT_ERROR, 0, NULL, 0, 0, | 369 | return general_allocate_string(ui, text, 0, UIT_ERROR, 0, NULL, 0, 0, |
391 | NULL); | 370 | NULL); |
392 | } | 371 | } |
393 | 372 | ||
394 | int UI_dup_error_string(UI *ui, const char *text) | 373 | int |
395 | { | 374 | UI_dup_error_string(UI * ui, const char *text) |
396 | char *text_copy=NULL; | 375 | { |
397 | 376 | char *text_copy = NULL; | |
398 | if (text) | 377 | |
399 | { | 378 | if (text) { |
400 | text_copy=BUF_strdup(text); | 379 | text_copy = BUF_strdup(text); |
401 | if (text_copy == NULL) | 380 | if (text_copy == NULL) { |
402 | { | 381 | UIerr(UI_F_UI_DUP_ERROR_STRING, ERR_R_MALLOC_FAILURE); |
403 | UIerr(UI_F_UI_DUP_ERROR_STRING,ERR_R_MALLOC_FAILURE); | ||
404 | return -1; | 382 | return -1; |
405 | } | ||
406 | } | 383 | } |
407 | return general_allocate_string(ui, text_copy, 1, UIT_ERROR, 0, NULL, | ||
408 | 0, 0, NULL); | ||
409 | } | 384 | } |
385 | return general_allocate_string(ui, text_copy, 1, UIT_ERROR, 0, NULL, | ||
386 | 0, 0, NULL); | ||
387 | } | ||
410 | 388 | ||
411 | char *UI_construct_prompt(UI *ui, const char *object_desc, | 389 | char * |
412 | const char *object_name) | 390 | UI_construct_prompt(UI * ui, const char *object_desc, |
413 | { | 391 | const char *object_name) |
392 | { | ||
414 | char *prompt = NULL; | 393 | char *prompt = NULL; |
415 | 394 | ||
416 | if (ui->meth->ui_construct_prompt) | 395 | if (ui->meth->ui_construct_prompt) |
417 | prompt = ui->meth->ui_construct_prompt(ui, | 396 | prompt = ui->meth->ui_construct_prompt(ui, |
418 | object_desc, object_name); | 397 | object_desc, object_name); |
419 | else | 398 | else { |
420 | { | ||
421 | char prompt1[] = "Enter "; | 399 | char prompt1[] = "Enter "; |
422 | char prompt2[] = " for "; | 400 | char prompt2[] = " for "; |
423 | char prompt3[] = ":"; | 401 | char prompt3[] = ":"; |
@@ -430,48 +408,49 @@ char *UI_construct_prompt(UI *ui, const char *object_desc, | |||
430 | len += sizeof(prompt2) - 1 + strlen(object_name); | 408 | len += sizeof(prompt2) - 1 + strlen(object_name); |
431 | len += sizeof(prompt3) - 1; | 409 | len += sizeof(prompt3) - 1; |
432 | 410 | ||
433 | prompt = (char *)malloc(len + 1); | 411 | prompt = (char *) malloc(len + 1); |
434 | BUF_strlcpy(prompt, prompt1, len + 1); | 412 | BUF_strlcpy(prompt, prompt1, len + 1); |
435 | BUF_strlcat(prompt, object_desc, len + 1); | 413 | BUF_strlcat(prompt, object_desc, len + 1); |
436 | if (object_name) | 414 | if (object_name) { |
437 | { | ||
438 | BUF_strlcat(prompt, prompt2, len + 1); | 415 | BUF_strlcat(prompt, prompt2, len + 1); |
439 | BUF_strlcat(prompt, object_name, len + 1); | 416 | BUF_strlcat(prompt, object_name, len + 1); |
440 | } | ||
441 | BUF_strlcat(prompt, prompt3, len + 1); | ||
442 | } | 417 | } |
443 | return prompt; | 418 | BUF_strlcat(prompt, prompt3, len + 1); |
444 | } | 419 | } |
420 | return prompt; | ||
421 | } | ||
445 | 422 | ||
446 | void *UI_add_user_data(UI *ui, void *user_data) | 423 | void * |
447 | { | 424 | UI_add_user_data(UI * ui, void *user_data) |
425 | { | ||
448 | void *old_data = ui->user_data; | 426 | void *old_data = ui->user_data; |
449 | ui->user_data = user_data; | 427 | ui->user_data = user_data; |
450 | return old_data; | 428 | return old_data; |
451 | } | 429 | } |
452 | 430 | ||
453 | void *UI_get0_user_data(UI *ui) | 431 | void * |
454 | { | 432 | UI_get0_user_data(UI * ui) |
433 | { | ||
455 | return ui->user_data; | 434 | return ui->user_data; |
456 | } | 435 | } |
457 | 436 | ||
458 | const char *UI_get0_result(UI *ui, int i) | 437 | const char * |
459 | { | 438 | UI_get0_result(UI * ui, int i) |
460 | if (i < 0) | 439 | { |
461 | { | 440 | if (i < 0) { |
462 | UIerr(UI_F_UI_GET0_RESULT,UI_R_INDEX_TOO_SMALL); | 441 | UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_SMALL); |
463 | return NULL; | 442 | return NULL; |
464 | } | 443 | } |
465 | if (i >= sk_UI_STRING_num(ui->strings)) | 444 | if (i >= sk_UI_STRING_num(ui->strings)) { |
466 | { | 445 | UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_LARGE); |
467 | UIerr(UI_F_UI_GET0_RESULT,UI_R_INDEX_TOO_LARGE); | ||
468 | return NULL; | 446 | return NULL; |
469 | } | ||
470 | return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i)); | ||
471 | } | 447 | } |
448 | return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i)); | ||
449 | } | ||
472 | 450 | ||
473 | static int print_error(const char *str, size_t len, UI *ui) | 451 | static int |
474 | { | 452 | print_error(const char *str, size_t len, UI * ui) |
453 | { | ||
475 | UI_STRING uis; | 454 | UI_STRING uis; |
476 | 455 | ||
477 | memset(&uis, 0, sizeof(uis)); | 456 | memset(&uis, 0, sizeof(uis)); |
@@ -479,446 +458,441 @@ static int print_error(const char *str, size_t len, UI *ui) | |||
479 | uis.out_string = str; | 458 | uis.out_string = str; |
480 | 459 | ||
481 | if (ui->meth->ui_write_string | 460 | if (ui->meth->ui_write_string |
482 | && !ui->meth->ui_write_string(ui, &uis)) | 461 | && !ui->meth->ui_write_string(ui, &uis)) |
483 | return -1; | 462 | return -1; |
484 | return 0; | 463 | return 0; |
485 | } | 464 | } |
486 | 465 | ||
487 | int UI_process(UI *ui) | 466 | int |
488 | { | 467 | UI_process(UI * ui) |
489 | int i, ok=0; | 468 | { |
469 | int i, ok = 0; | ||
490 | 470 | ||
491 | if (ui->meth->ui_open_session && !ui->meth->ui_open_session(ui)) | 471 | if (ui->meth->ui_open_session && !ui->meth->ui_open_session(ui)) |
492 | return -1; | 472 | return -1; |
493 | 473 | ||
494 | if (ui->flags & UI_FLAG_PRINT_ERRORS) | 474 | if (ui->flags & UI_FLAG_PRINT_ERRORS) |
495 | ERR_print_errors_cb( | 475 | ERR_print_errors_cb( |
496 | (int (*)(const char *, size_t, void *))print_error, | 476 | (int (*) (const char *, size_t, void *)) print_error, |
497 | (void *)ui); | 477 | (void *) ui); |
498 | 478 | ||
499 | for(i=0; i<sk_UI_STRING_num(ui->strings); i++) | 479 | for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) { |
500 | { | ||
501 | if (ui->meth->ui_write_string | 480 | if (ui->meth->ui_write_string |
502 | && !ui->meth->ui_write_string(ui, | 481 | && !ui->meth->ui_write_string(ui, |
503 | sk_UI_STRING_value(ui->strings, i))) | 482 | sk_UI_STRING_value(ui->strings, i))) { |
504 | { | 483 | ok = -1; |
505 | ok=-1; | ||
506 | goto err; | 484 | goto err; |
507 | } | ||
508 | } | 485 | } |
486 | } | ||
509 | 487 | ||
510 | if (ui->meth->ui_flush) | 488 | if (ui->meth->ui_flush) |
511 | switch(ui->meth->ui_flush(ui)) | 489 | switch (ui->meth->ui_flush(ui)) { |
512 | { | 490 | case -1: /* Interrupt/Cancel/something... */ |
513 | case -1: /* Interrupt/Cancel/something... */ | ||
514 | ok = -2; | 491 | ok = -2; |
515 | goto err; | 492 | goto err; |
516 | case 0: /* Errors */ | 493 | case 0: /* Errors */ |
517 | ok = -1; | 494 | ok = -1; |
518 | goto err; | 495 | goto err; |
519 | default: /* Success */ | 496 | default: /* Success */ |
520 | ok = 0; | 497 | ok = 0; |
521 | break; | 498 | break; |
522 | } | 499 | } |
523 | 500 | ||
524 | for(i=0; i<sk_UI_STRING_num(ui->strings); i++) | 501 | for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) { |
525 | { | 502 | if (ui->meth->ui_read_string) { |
526 | if (ui->meth->ui_read_string) | 503 | switch (ui->meth->ui_read_string(ui, |
527 | { | 504 | sk_UI_STRING_value(ui->strings, i))) { |
528 | switch(ui->meth->ui_read_string(ui, | 505 | case -1: /* Interrupt/Cancel/something... */ |
529 | sk_UI_STRING_value(ui->strings, i))) | ||
530 | { | ||
531 | case -1: /* Interrupt/Cancel/something... */ | ||
532 | ok = -2; | 506 | ok = -2; |
533 | goto err; | 507 | goto err; |
534 | case 0: /* Errors */ | 508 | case 0:/* Errors */ |
535 | ok = -1; | 509 | ok = -1; |
536 | goto err; | 510 | goto err; |
537 | default: /* Success */ | 511 | default: /* Success */ |
538 | ok = 0; | 512 | ok = 0; |
539 | break; | 513 | break; |
540 | } | ||
541 | } | 514 | } |
542 | } | 515 | } |
543 | err: | 516 | } |
517 | err: | ||
544 | if (ui->meth->ui_close_session && !ui->meth->ui_close_session(ui)) | 518 | if (ui->meth->ui_close_session && !ui->meth->ui_close_session(ui)) |
545 | return -1; | 519 | return -1; |
546 | return ok; | 520 | return ok; |
547 | } | 521 | } |
548 | 522 | ||
549 | int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f)(void)) | 523 | int |
550 | { | 524 | UI_ctrl(UI * ui, int cmd, long i, void *p, void (*f) (void)) |
551 | if (ui == NULL) | 525 | { |
552 | { | 526 | if (ui == NULL) { |
553 | UIerr(UI_F_UI_CTRL,ERR_R_PASSED_NULL_PARAMETER); | 527 | UIerr(UI_F_UI_CTRL, ERR_R_PASSED_NULL_PARAMETER); |
554 | return -1; | 528 | return -1; |
555 | } | 529 | } |
556 | switch(cmd) | 530 | switch (cmd) { |
557 | { | ||
558 | case UI_CTRL_PRINT_ERRORS: | 531 | case UI_CTRL_PRINT_ERRORS: |
559 | { | 532 | { |
560 | int save_flag = !!(ui->flags & UI_FLAG_PRINT_ERRORS); | 533 | int save_flag = !!(ui->flags & UI_FLAG_PRINT_ERRORS); |
561 | if (i) | 534 | if (i) |
562 | ui->flags |= UI_FLAG_PRINT_ERRORS; | 535 | ui->flags |= UI_FLAG_PRINT_ERRORS; |
563 | else | 536 | else |
564 | ui->flags &= ~UI_FLAG_PRINT_ERRORS; | 537 | ui->flags &= ~UI_FLAG_PRINT_ERRORS; |
565 | return save_flag; | 538 | return save_flag; |
566 | } | 539 | } |
567 | case UI_CTRL_IS_REDOABLE: | 540 | case UI_CTRL_IS_REDOABLE: |
568 | return !!(ui->flags & UI_FLAG_REDOABLE); | 541 | return !!(ui->flags & UI_FLAG_REDOABLE); |
569 | default: | 542 | default: |
570 | break; | 543 | break; |
571 | } | ||
572 | UIerr(UI_F_UI_CTRL,UI_R_UNKNOWN_CONTROL_COMMAND); | ||
573 | return -1; | ||
574 | } | 544 | } |
545 | UIerr(UI_F_UI_CTRL, UI_R_UNKNOWN_CONTROL_COMMAND); | ||
546 | return -1; | ||
547 | } | ||
575 | 548 | ||
576 | int UI_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | 549 | int |
577 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) | 550 | UI_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new * new_func, |
578 | { | 551 | CRYPTO_EX_dup * dup_func, CRYPTO_EX_free * free_func) |
552 | { | ||
579 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI, argl, argp, | 553 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI, argl, argp, |
580 | new_func, dup_func, free_func); | 554 | new_func, dup_func, free_func); |
581 | } | 555 | } |
582 | 556 | ||
583 | int UI_set_ex_data(UI *r, int idx, void *arg) | 557 | int |
584 | { | 558 | UI_set_ex_data(UI * r, int idx, void *arg) |
585 | return(CRYPTO_set_ex_data(&r->ex_data,idx,arg)); | 559 | { |
586 | } | 560 | return (CRYPTO_set_ex_data(&r->ex_data, idx, arg)); |
587 | 561 | } | |
588 | void *UI_get_ex_data(UI *r, int idx) | 562 | |
589 | { | 563 | void * |
590 | return(CRYPTO_get_ex_data(&r->ex_data,idx)); | 564 | UI_get_ex_data(UI * r, int idx) |
591 | } | 565 | { |
592 | 566 | return (CRYPTO_get_ex_data(&r->ex_data, idx)); | |
593 | void UI_set_default_method(const UI_METHOD *meth) | 567 | } |
594 | { | 568 | |
595 | default_UI_meth=meth; | 569 | void |
570 | UI_set_default_method(const UI_METHOD * meth) | ||
571 | { | ||
572 | default_UI_meth = meth; | ||
573 | } | ||
574 | |||
575 | const UI_METHOD * | ||
576 | UI_get_default_method(void) | ||
577 | { | ||
578 | if (default_UI_meth == NULL) { | ||
579 | default_UI_meth = UI_OpenSSL(); | ||
596 | } | 580 | } |
597 | |||
598 | const UI_METHOD *UI_get_default_method(void) | ||
599 | { | ||
600 | if (default_UI_meth == NULL) | ||
601 | { | ||
602 | default_UI_meth=UI_OpenSSL(); | ||
603 | } | ||
604 | return default_UI_meth; | 581 | return default_UI_meth; |
605 | } | 582 | } |
606 | 583 | ||
607 | const UI_METHOD *UI_get_method(UI *ui) | 584 | const UI_METHOD * |
608 | { | 585 | UI_get_method(UI * ui) |
586 | { | ||
609 | return ui->meth; | 587 | return ui->meth; |
610 | } | 588 | } |
611 | 589 | ||
612 | const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth) | 590 | const UI_METHOD * |
613 | { | 591 | UI_set_method(UI * ui, const UI_METHOD * meth) |
614 | ui->meth=meth; | 592 | { |
593 | ui->meth = meth; | ||
615 | return ui->meth; | 594 | return ui->meth; |
616 | } | 595 | } |
617 | 596 | ||
618 | 597 | ||
619 | UI_METHOD *UI_create_method(char *name) | 598 | UI_METHOD * |
620 | { | 599 | UI_create_method(char *name) |
621 | UI_METHOD *ui_method = (UI_METHOD *)malloc(sizeof(UI_METHOD)); | 600 | { |
601 | UI_METHOD *ui_method = (UI_METHOD *) malloc(sizeof(UI_METHOD)); | ||
622 | 602 | ||
623 | if (ui_method) | 603 | if (ui_method) { |
624 | { | ||
625 | memset(ui_method, 0, sizeof(*ui_method)); | 604 | memset(ui_method, 0, sizeof(*ui_method)); |
626 | ui_method->name = BUF_strdup(name); | 605 | ui_method->name = BUF_strdup(name); |
627 | } | ||
628 | return ui_method; | ||
629 | } | 606 | } |
607 | return ui_method; | ||
608 | } | ||
630 | 609 | ||
631 | /* BIG FSCKING WARNING!!!! If you use this on a statically allocated method | 610 | /* BIG FSCKING WARNING!!!! If you use this on a statically allocated method |
632 | (that is, it hasn't been allocated using UI_create_method(), you deserve | 611 | (that is, it hasn't been allocated using UI_create_method(), you deserve |
633 | anything Murphy can throw at you and more! You have been warned. */ | 612 | anything Murphy can throw at you and more! You have been warned. */ |
634 | void UI_destroy_method(UI_METHOD *ui_method) | 613 | void |
635 | { | 614 | UI_destroy_method(UI_METHOD * ui_method) |
615 | { | ||
636 | free(ui_method->name); | 616 | free(ui_method->name); |
637 | ui_method->name = NULL; | 617 | ui_method->name = NULL; |
638 | free(ui_method); | 618 | free(ui_method); |
639 | } | 619 | } |
640 | 620 | ||
641 | int UI_method_set_opener(UI_METHOD *method, int (*opener)(UI *ui)) | 621 | int |
642 | { | 622 | UI_method_set_opener(UI_METHOD * method, int (*opener) (UI * ui)) |
643 | if (method) | 623 | { |
644 | { | 624 | if (method) { |
645 | method->ui_open_session = opener; | 625 | method->ui_open_session = opener; |
646 | return 0; | 626 | return 0; |
647 | } | 627 | } else |
648 | else | ||
649 | return -1; | 628 | return -1; |
650 | } | 629 | } |
651 | 630 | ||
652 | int UI_method_set_writer(UI_METHOD *method, int (*writer)(UI *ui, UI_STRING *uis)) | 631 | int |
653 | { | 632 | UI_method_set_writer(UI_METHOD * method, int (*writer) (UI * ui, UI_STRING * uis)) |
654 | if (method) | 633 | { |
655 | { | 634 | if (method) { |
656 | method->ui_write_string = writer; | 635 | method->ui_write_string = writer; |
657 | return 0; | 636 | return 0; |
658 | } | 637 | } else |
659 | else | ||
660 | return -1; | 638 | return -1; |
661 | } | 639 | } |
662 | 640 | ||
663 | int UI_method_set_flusher(UI_METHOD *method, int (*flusher)(UI *ui)) | 641 | int |
664 | { | 642 | UI_method_set_flusher(UI_METHOD * method, int (*flusher) (UI * ui)) |
665 | if (method) | 643 | { |
666 | { | 644 | if (method) { |
667 | method->ui_flush = flusher; | 645 | method->ui_flush = flusher; |
668 | return 0; | 646 | return 0; |
669 | } | 647 | } else |
670 | else | ||
671 | return -1; | 648 | return -1; |
672 | } | 649 | } |
673 | 650 | ||
674 | int UI_method_set_reader(UI_METHOD *method, int (*reader)(UI *ui, UI_STRING *uis)) | 651 | int |
675 | { | 652 | UI_method_set_reader(UI_METHOD * method, int (*reader) (UI * ui, UI_STRING * uis)) |
676 | if (method) | 653 | { |
677 | { | 654 | if (method) { |
678 | method->ui_read_string = reader; | 655 | method->ui_read_string = reader; |
679 | return 0; | 656 | return 0; |
680 | } | 657 | } else |
681 | else | ||
682 | return -1; | 658 | return -1; |
683 | } | 659 | } |
684 | 660 | ||
685 | int UI_method_set_closer(UI_METHOD *method, int (*closer)(UI *ui)) | 661 | int |
686 | { | 662 | UI_method_set_closer(UI_METHOD * method, int (*closer) (UI * ui)) |
687 | if (method) | 663 | { |
688 | { | 664 | if (method) { |
689 | method->ui_close_session = closer; | 665 | method->ui_close_session = closer; |
690 | return 0; | 666 | return 0; |
691 | } | 667 | } else |
692 | else | ||
693 | return -1; | 668 | return -1; |
694 | } | 669 | } |
695 | 670 | ||
696 | int UI_method_set_prompt_constructor(UI_METHOD *method, char *(*prompt_constructor)(UI* ui, const char* object_desc, const char* object_name)) | 671 | int |
697 | { | 672 | UI_method_set_prompt_constructor(UI_METHOD * method, char *(*prompt_constructor) (UI * ui, const char *object_desc, const char *object_name)) |
698 | if (method) | 673 | { |
699 | { | 674 | if (method) { |
700 | method->ui_construct_prompt = prompt_constructor; | 675 | method->ui_construct_prompt = prompt_constructor; |
701 | return 0; | 676 | return 0; |
702 | } | 677 | } else |
703 | else | ||
704 | return -1; | 678 | return -1; |
705 | } | 679 | } |
706 | 680 | ||
707 | int (*UI_method_get_opener(UI_METHOD *method))(UI*) | 681 | int (* |
708 | { | 682 | UI_method_get_opener(UI_METHOD * method)) (UI *) |
683 | { | ||
709 | if (method) | 684 | if (method) |
710 | return method->ui_open_session; | 685 | return method->ui_open_session; |
711 | else | 686 | else |
712 | return NULL; | 687 | return NULL; |
713 | } | 688 | } |
714 | 689 | ||
715 | int (*UI_method_get_writer(UI_METHOD *method))(UI*,UI_STRING*) | 690 | int (* |
716 | { | 691 | UI_method_get_writer(UI_METHOD * method)) (UI *, UI_STRING *) |
692 | { | ||
717 | if (method) | 693 | if (method) |
718 | return method->ui_write_string; | 694 | return method->ui_write_string; |
719 | else | 695 | else |
720 | return NULL; | 696 | return NULL; |
721 | } | 697 | } |
722 | 698 | ||
723 | int (*UI_method_get_flusher(UI_METHOD *method))(UI*) | 699 | int (* |
724 | { | 700 | UI_method_get_flusher(UI_METHOD * method)) (UI *) |
701 | { | ||
725 | if (method) | 702 | if (method) |
726 | return method->ui_flush; | 703 | return method->ui_flush; |
727 | else | 704 | else |
728 | return NULL; | 705 | return NULL; |
729 | } | 706 | } |
730 | 707 | ||
731 | int (*UI_method_get_reader(UI_METHOD *method))(UI*,UI_STRING*) | 708 | int (* |
732 | { | 709 | UI_method_get_reader(UI_METHOD * method)) (UI *, UI_STRING *) |
710 | { | ||
733 | if (method) | 711 | if (method) |
734 | return method->ui_read_string; | 712 | return method->ui_read_string; |
735 | else | 713 | else |
736 | return NULL; | 714 | return NULL; |
737 | } | 715 | } |
738 | 716 | ||
739 | int (*UI_method_get_closer(UI_METHOD *method))(UI*) | 717 | int (* |
740 | { | 718 | UI_method_get_closer(UI_METHOD * method)) (UI *) |
719 | { | ||
741 | if (method) | 720 | if (method) |
742 | return method->ui_close_session; | 721 | return method->ui_close_session; |
743 | else | 722 | else |
744 | return NULL; | 723 | return NULL; |
745 | } | 724 | } |
746 | 725 | ||
747 | char* (*UI_method_get_prompt_constructor(UI_METHOD *method))(UI*, const char*, const char*) | 726 | char *(* |
748 | { | 727 | UI_method_get_prompt_constructor(UI_METHOD * method)) (UI *, const char *, const char *) |
728 | { | ||
749 | if (method) | 729 | if (method) |
750 | return method->ui_construct_prompt; | 730 | return method->ui_construct_prompt; |
751 | else | 731 | else |
752 | return NULL; | 732 | return NULL; |
753 | } | 733 | } |
754 | 734 | ||
755 | enum UI_string_types UI_get_string_type(UI_STRING *uis) | 735 | enum UI_string_types |
756 | { | 736 | UI_get_string_type(UI_STRING * uis) |
737 | { | ||
757 | if (!uis) | 738 | if (!uis) |
758 | return UIT_NONE; | 739 | return UIT_NONE; |
759 | return uis->type; | 740 | return uis->type; |
760 | } | 741 | } |
761 | 742 | ||
762 | int UI_get_input_flags(UI_STRING *uis) | 743 | int |
763 | { | 744 | UI_get_input_flags(UI_STRING * uis) |
745 | { | ||
764 | if (!uis) | 746 | if (!uis) |
765 | return 0; | 747 | return 0; |
766 | return uis->input_flags; | 748 | return uis->input_flags; |
767 | } | 749 | } |
768 | 750 | ||
769 | const char *UI_get0_output_string(UI_STRING *uis) | 751 | const char * |
770 | { | 752 | UI_get0_output_string(UI_STRING * uis) |
753 | { | ||
771 | if (!uis) | 754 | if (!uis) |
772 | return NULL; | 755 | return NULL; |
773 | return uis->out_string; | 756 | return uis->out_string; |
774 | } | 757 | } |
775 | 758 | ||
776 | const char *UI_get0_action_string(UI_STRING *uis) | 759 | const char * |
777 | { | 760 | UI_get0_action_string(UI_STRING * uis) |
761 | { | ||
778 | if (!uis) | 762 | if (!uis) |
779 | return NULL; | 763 | return NULL; |
780 | switch(uis->type) | 764 | switch (uis->type) { |
781 | { | ||
782 | case UIT_PROMPT: | 765 | case UIT_PROMPT: |
783 | case UIT_BOOLEAN: | 766 | case UIT_BOOLEAN: |
784 | return uis->_.boolean_data.action_desc; | 767 | return uis->_.boolean_data.action_desc; |
785 | default: | 768 | default: |
786 | return NULL; | 769 | return NULL; |
787 | } | ||
788 | } | 770 | } |
771 | } | ||
789 | 772 | ||
790 | const char *UI_get0_result_string(UI_STRING *uis) | 773 | const char * |
791 | { | 774 | UI_get0_result_string(UI_STRING * uis) |
775 | { | ||
792 | if (!uis) | 776 | if (!uis) |
793 | return NULL; | 777 | return NULL; |
794 | switch(uis->type) | 778 | switch (uis->type) { |
795 | { | ||
796 | case UIT_PROMPT: | 779 | case UIT_PROMPT: |
797 | case UIT_VERIFY: | 780 | case UIT_VERIFY: |
798 | return uis->result_buf; | 781 | return uis->result_buf; |
799 | default: | 782 | default: |
800 | return NULL; | 783 | return NULL; |
801 | } | ||
802 | } | 784 | } |
785 | } | ||
803 | 786 | ||
804 | const char *UI_get0_test_string(UI_STRING *uis) | 787 | const char * |
805 | { | 788 | UI_get0_test_string(UI_STRING * uis) |
789 | { | ||
806 | if (!uis) | 790 | if (!uis) |
807 | return NULL; | 791 | return NULL; |
808 | switch(uis->type) | 792 | switch (uis->type) { |
809 | { | ||
810 | case UIT_VERIFY: | 793 | case UIT_VERIFY: |
811 | return uis->_.string_data.test_buf; | 794 | return uis->_.string_data.test_buf; |
812 | default: | 795 | default: |
813 | return NULL; | 796 | return NULL; |
814 | } | ||
815 | } | 797 | } |
798 | } | ||
816 | 799 | ||
817 | int UI_get_result_minsize(UI_STRING *uis) | 800 | int |
818 | { | 801 | UI_get_result_minsize(UI_STRING * uis) |
802 | { | ||
819 | if (!uis) | 803 | if (!uis) |
820 | return -1; | 804 | return -1; |
821 | switch(uis->type) | 805 | switch (uis->type) { |
822 | { | ||
823 | case UIT_PROMPT: | 806 | case UIT_PROMPT: |
824 | case UIT_VERIFY: | 807 | case UIT_VERIFY: |
825 | return uis->_.string_data.result_minsize; | 808 | return uis->_.string_data.result_minsize; |
826 | default: | 809 | default: |
827 | return -1; | 810 | return -1; |
828 | } | ||
829 | } | 811 | } |
812 | } | ||
830 | 813 | ||
831 | int UI_get_result_maxsize(UI_STRING *uis) | 814 | int |
832 | { | 815 | UI_get_result_maxsize(UI_STRING * uis) |
816 | { | ||
833 | if (!uis) | 817 | if (!uis) |
834 | return -1; | 818 | return -1; |
835 | switch(uis->type) | 819 | switch (uis->type) { |
836 | { | ||
837 | case UIT_PROMPT: | 820 | case UIT_PROMPT: |
838 | case UIT_VERIFY: | 821 | case UIT_VERIFY: |
839 | return uis->_.string_data.result_maxsize; | 822 | return uis->_.string_data.result_maxsize; |
840 | default: | 823 | default: |
841 | return -1; | 824 | return -1; |
842 | } | ||
843 | } | 825 | } |
826 | } | ||
844 | 827 | ||
845 | int UI_set_result(UI *ui, UI_STRING *uis, const char *result) | 828 | int |
846 | { | 829 | UI_set_result(UI * ui, UI_STRING * uis, const char *result) |
830 | { | ||
847 | int l = strlen(result); | 831 | int l = strlen(result); |
848 | 832 | ||
849 | ui->flags &= ~UI_FLAG_REDOABLE; | 833 | ui->flags &= ~UI_FLAG_REDOABLE; |
850 | 834 | ||
851 | if (!uis) | 835 | if (!uis) |
852 | return -1; | 836 | return -1; |
853 | switch (uis->type) | 837 | switch (uis->type) { |
854 | { | ||
855 | case UIT_PROMPT: | 838 | case UIT_PROMPT: |
856 | case UIT_VERIFY: | 839 | case UIT_VERIFY: |
857 | { | 840 | { |
858 | char number1[DECIMAL_SIZE(uis->_.string_data.result_minsize)+1]; | 841 | char number1[DECIMAL_SIZE(uis->_.string_data.result_minsize) + 1]; |
859 | char number2[DECIMAL_SIZE(uis->_.string_data.result_maxsize)+1]; | 842 | char number2[DECIMAL_SIZE(uis->_.string_data.result_maxsize) + 1]; |
860 | 843 | ||
861 | (void) snprintf(number1, sizeof(number1), "%d", | 844 | (void) snprintf(number1, sizeof(number1), "%d", |
862 | uis->_.string_data.result_minsize); | 845 | uis->_.string_data.result_minsize); |
863 | (void) snprintf(number2, sizeof(number2), "%d", | 846 | (void) snprintf(number2, sizeof(number2), "%d", |
864 | uis->_.string_data.result_maxsize); | 847 | uis->_.string_data.result_maxsize); |
865 | 848 | ||
866 | if (l < uis->_.string_data.result_minsize) | 849 | if (l < uis->_.string_data.result_minsize) { |
867 | { | 850 | ui->flags |= UI_FLAG_REDOABLE; |
868 | ui->flags |= UI_FLAG_REDOABLE; | 851 | UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_SMALL); |
869 | UIerr(UI_F_UI_SET_RESULT,UI_R_RESULT_TOO_SMALL); | 852 | ERR_add_error_data(5, "You must type in ", |
870 | ERR_add_error_data(5,"You must type in ", | 853 | number1, " to ", number2, " characters"); |
871 | number1," to ",number2," characters"); | 854 | return -1; |
872 | return -1; | ||
873 | } | 855 | } |
874 | if (l > uis->_.string_data.result_maxsize) | 856 | if (l > uis->_.string_data.result_maxsize) { |
875 | { | 857 | ui->flags |= UI_FLAG_REDOABLE; |
876 | ui->flags |= UI_FLAG_REDOABLE; | 858 | UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_LARGE); |
877 | UIerr(UI_F_UI_SET_RESULT,UI_R_RESULT_TOO_LARGE); | 859 | ERR_add_error_data(5, "You must type in ", |
878 | ERR_add_error_data(5,"You must type in ", | 860 | number1, " to ", number2, " characters"); |
879 | number1," to ",number2," characters"); | 861 | return -1; |
880 | return -1; | ||
881 | } | 862 | } |
882 | } | 863 | } |
883 | 864 | ||
884 | if (!uis->result_buf) | 865 | if (!uis->result_buf) { |
885 | { | 866 | UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER); |
886 | UIerr(UI_F_UI_SET_RESULT,UI_R_NO_RESULT_BUFFER); | ||
887 | return -1; | 867 | return -1; |
888 | } | 868 | } |
889 | |||
890 | BUF_strlcpy(uis->result_buf, result, | 869 | BUF_strlcpy(uis->result_buf, result, |
891 | uis->_.string_data.result_maxsize + 1); | 870 | uis->_.string_data.result_maxsize + 1); |
892 | break; | 871 | break; |
893 | case UIT_BOOLEAN: | 872 | case UIT_BOOLEAN: |
894 | { | 873 | { |
895 | const char *p; | 874 | const char *p; |
896 | 875 | ||
897 | if (!uis->result_buf) | 876 | if (!uis->result_buf) { |
898 | { | 877 | UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER); |
899 | UIerr(UI_F_UI_SET_RESULT,UI_R_NO_RESULT_BUFFER); | 878 | return -1; |
900 | return -1; | ||
901 | } | 879 | } |
902 | 880 | uis->result_buf[0] = '\0'; | |
903 | uis->result_buf[0] = '\0'; | 881 | for (p = result; *p; p++) { |
904 | for(p = result; *p; p++) | 882 | if (strchr(uis->_.boolean_data.ok_chars, *p)) { |
905 | { | 883 | uis->result_buf[0] = |
906 | if (strchr(uis->_.boolean_data.ok_chars, *p)) | 884 | uis->_.boolean_data.ok_chars[0]; |
907 | { | 885 | break; |
908 | uis->result_buf[0] = | ||
909 | uis->_.boolean_data.ok_chars[0]; | ||
910 | break; | ||
911 | } | 886 | } |
912 | if (strchr(uis->_.boolean_data.cancel_chars, *p)) | 887 | if (strchr(uis->_.boolean_data.cancel_chars, *p)) { |
913 | { | 888 | uis->result_buf[0] = |
914 | uis->result_buf[0] = | 889 | uis->_.boolean_data.cancel_chars[0]; |
915 | uis->_.boolean_data.cancel_chars[0]; | 890 | break; |
916 | break; | ||
917 | } | 891 | } |
918 | } | 892 | } |
919 | default: | 893 | default: |
920 | break; | 894 | break; |
921 | } | ||
922 | } | 895 | } |
923 | return 0; | ||
924 | } | 896 | } |
897 | return 0; | ||
898 | } | ||