1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24 #include "tool_setup.h"
25
26 #ifndef CURL_DISABLE_LIBCURL_OPTION
27
28 #define ENABLE_CURLX_PRINTF
29 /* use our own printf() functions */
30 #include "curlx.h"
31
32 #include "tool_cfgable.h"
33 #include "tool_easysrc.h"
34 #include "tool_setopt.h"
35 #include "tool_msgs.h"
36 #include "dynbuf.h"
37
38 #include "memdebug.h" /* keep this as LAST include */
39
40 /* Lookup tables for converting setopt values back to symbols */
41 /* For enums, values may be in any order. */
42 /* For bit masks, put combinations first, then single bits, */
43 /* and finally any "NONE" value. */
44
45 #define NV(e) {#e, e}
46 #define NV1(e, v) {#e, (v)}
47 #define NVEND {NULL, 0} /* sentinel to mark end of list */
48
49 const struct NameValue setopt_nv_CURLPROXY[] = {
50 NV(CURLPROXY_HTTP),
51 NV(CURLPROXY_HTTP_1_0),
52 NV(CURLPROXY_HTTPS),
53 NV(CURLPROXY_SOCKS4),
54 NV(CURLPROXY_SOCKS5),
55 NV(CURLPROXY_SOCKS4A),
56 NV(CURLPROXY_SOCKS5_HOSTNAME),
57 NVEND,
58 };
59
60 const struct NameValue setopt_nv_CURL_SOCKS_PROXY[] = {
61 NV(CURLPROXY_SOCKS4),
62 NV(CURLPROXY_SOCKS5),
63 NV(CURLPROXY_SOCKS4A),
64 NV(CURLPROXY_SOCKS5_HOSTNAME),
65 NVEND,
66 };
67
68 const struct NameValueUnsigned setopt_nv_CURLHSTS[] = {
69 NV(CURLHSTS_ENABLE),
70 NVEND,
71 };
72
73 const struct NameValueUnsigned setopt_nv_CURLAUTH[] = {
74 NV(CURLAUTH_ANY), /* combination */
75 NV(CURLAUTH_ANYSAFE), /* combination */
76 NV(CURLAUTH_BASIC),
77 NV(CURLAUTH_DIGEST),
78 NV(CURLAUTH_GSSNEGOTIATE),
79 NV(CURLAUTH_NTLM),
80 NV(CURLAUTH_DIGEST_IE),
81 NV(CURLAUTH_NTLM_WB),
82 NV(CURLAUTH_ONLY),
83 NV(CURLAUTH_NONE),
84 NVEND,
85 };
86
87 const struct NameValue setopt_nv_CURL_HTTP_VERSION[] = {
88 NV(CURL_HTTP_VERSION_NONE),
89 NV(CURL_HTTP_VERSION_1_0),
90 NV(CURL_HTTP_VERSION_1_1),
91 NV(CURL_HTTP_VERSION_2_0),
92 NV(CURL_HTTP_VERSION_2TLS),
93 NV(CURL_HTTP_VERSION_3),
94 NV(CURL_HTTP_VERSION_3ONLY),
95 NVEND,
96 };
97
98 const struct NameValue setopt_nv_CURL_SSLVERSION[] = {
99 NV(CURL_SSLVERSION_DEFAULT),
100 NV(CURL_SSLVERSION_TLSv1),
101 NV(CURL_SSLVERSION_SSLv2),
102 NV(CURL_SSLVERSION_SSLv3),
103 NV(CURL_SSLVERSION_TLSv1_0),
104 NV(CURL_SSLVERSION_TLSv1_1),
105 NV(CURL_SSLVERSION_TLSv1_2),
106 NV(CURL_SSLVERSION_TLSv1_3),
107 NVEND,
108 };
109
110 const struct NameValue setopt_nv_CURL_TIMECOND[] = {
111 NV(CURL_TIMECOND_IFMODSINCE),
112 NV(CURL_TIMECOND_IFUNMODSINCE),
113 NV(CURL_TIMECOND_LASTMOD),
114 NV(CURL_TIMECOND_NONE),
115 NVEND,
116 };
117
118 const struct NameValue setopt_nv_CURLFTPSSL_CCC[] = {
119 NV(CURLFTPSSL_CCC_NONE),
120 NV(CURLFTPSSL_CCC_PASSIVE),
121 NV(CURLFTPSSL_CCC_ACTIVE),
122 NVEND,
123 };
124
125 const struct NameValue setopt_nv_CURLUSESSL[] = {
126 NV(CURLUSESSL_NONE),
127 NV(CURLUSESSL_TRY),
128 NV(CURLUSESSL_CONTROL),
129 NV(CURLUSESSL_ALL),
130 NVEND,
131 };
132
133 const struct NameValueUnsigned setopt_nv_CURLSSLOPT[] = {
134 NV(CURLSSLOPT_ALLOW_BEAST),
135 NV(CURLSSLOPT_NO_REVOKE),
136 NV(CURLSSLOPT_NO_PARTIALCHAIN),
137 NV(CURLSSLOPT_REVOKE_BEST_EFFORT),
138 NV(CURLSSLOPT_NATIVE_CA),
139 NV(CURLSSLOPT_AUTO_CLIENT_CERT),
140 NVEND,
141 };
142
143 const struct NameValue setopt_nv_CURL_NETRC[] = {
144 NV(CURL_NETRC_IGNORED),
145 NV(CURL_NETRC_OPTIONAL),
146 NV(CURL_NETRC_REQUIRED),
147 NVEND,
148 };
149
150 /* These options have non-zero default values. */
151 static const struct NameValue setopt_nv_CURLNONZERODEFAULTS[] = {
152 NV1(CURLOPT_SSL_VERIFYPEER, 1),
153 NV1(CURLOPT_SSL_VERIFYHOST, 1),
154 NV1(CURLOPT_SSL_ENABLE_NPN, 1),
155 NV1(CURLOPT_SSL_ENABLE_ALPN, 1),
156 NV1(CURLOPT_TCP_NODELAY, 1),
157 NV1(CURLOPT_PROXY_SSL_VERIFYPEER, 1),
158 NV1(CURLOPT_PROXY_SSL_VERIFYHOST, 1),
159 NV1(CURLOPT_SOCKS5_AUTH, 1),
160 NVEND
161 };
162
163 /* Format and add code; jump to nomem on malloc error */
164 #define ADD(args) do { \
165 ret = easysrc_add args; \
166 if(ret) \
167 goto nomem; \
168 } while(0)
169 #define ADDF(args) do { \
170 ret = easysrc_addf args; \
171 if(ret) \
172 goto nomem; \
173 } while(0)
174 #define NULL_CHECK(p) do { \
175 if(!p) { \
176 ret = CURLE_OUT_OF_MEMORY; \
177 goto nomem; \
178 } \
179 } while(0)
180
181 #define DECL0(s) ADD((&easysrc_decl, s))
182 #define DECL1(f,a) ADDF((&easysrc_decl, f,a))
183
184 #define DATA0(s) ADD((&easysrc_data, s))
185 #define DATA1(f,a) ADDF((&easysrc_data, f,a))
186 #define DATA2(f,a,b) ADDF((&easysrc_data, f,a,b))
187 #define DATA3(f,a,b,c) ADDF((&easysrc_data, f,a,b,c))
188
189 #define CODE0(s) ADD((&easysrc_code, s))
190 #define CODE1(f,a) ADDF((&easysrc_code, f,a))
191 #define CODE2(f,a,b) ADDF((&easysrc_code, f,a,b))
192 #define CODE3(f,a,b,c) ADDF((&easysrc_code, f,a,b,c))
193
194 #define CLEAN0(s) ADD((&easysrc_clean, s))
195 #define CLEAN1(f,a) ADDF((&easysrc_clean, f,a))
196
197 #define REM0(s) ADD((&easysrc_toohard, s))
198 #define REM1(f,a) ADDF((&easysrc_toohard, f,a))
199 #define REM3(f,a,b,c) ADDF((&easysrc_toohard, f,a,b,c))
200
201 /* Escape string to C string syntax. Return NULL if out of memory.
202 * Is this correct for those wacky EBCDIC guys? */
203
204 #define MAX_STRING_LENGTH_OUTPUT 2000
205 #define ZERO_TERMINATED -1
206
c_escape(const char * str,curl_off_t len)207 static char *c_escape(const char *str, curl_off_t len)
208 {
209 const char *s;
210 unsigned int cutoff = 0;
211 CURLcode result;
212 struct curlx_dynbuf escaped;
213
214 curlx_dyn_init(&escaped, 4 * MAX_STRING_LENGTH_OUTPUT + 3);
215
216 if(len == ZERO_TERMINATED)
217 len = strlen(str);
218
219 if(len > MAX_STRING_LENGTH_OUTPUT) {
220 /* cap ridiculously long strings */
221 len = MAX_STRING_LENGTH_OUTPUT;
222 cutoff = 3;
223 }
224
225 result = curlx_dyn_addn(&escaped, STRCONST(""));
226 for(s = str; !result && len; s++, len--) {
227 /* escape question marks as well, to prevent generating accidental
228 trigraphs */
229 static const char from[] = "\t\r\n?\"\\";
230 static const char to[] = "\\t\\r\\n\\?\\\"\\\\";
231 const char *p = strchr(from, *s);
232
233 if(!p && ISPRINT(*s))
234 continue;
235
236 result = curlx_dyn_addn(&escaped, str, s - str);
237 str = s + 1;
238
239 if(!result) {
240 if(p && *p)
241 result = curlx_dyn_addn(&escaped, to + 2 * (p - from), 2);
242 else {
243 const char *format = "\\x%02x";
244
245 if(len > 1 && ISXDIGIT(s[1])) {
246 /* Octal escape to avoid >2 digit hex. */
247 format = "\\%03o";
248 }
249
250 result = curlx_dyn_addf(&escaped, format,
251 (unsigned int) *(unsigned char *) s);
252 }
253 }
254 }
255
256 if(!result)
257 result = curlx_dyn_addn(&escaped, str, s - str);
258
259 if(!result)
260 (void) !curlx_dyn_addn(&escaped, "...", cutoff);
261
262 return curlx_dyn_ptr(&escaped);
263 }
264
265 /* setopt wrapper for enum types */
tool_setopt_enum(CURL * curl,struct GlobalConfig * config,const char * name,CURLoption tag,const struct NameValue * nvlist,long lval)266 CURLcode tool_setopt_enum(CURL *curl, struct GlobalConfig *config,
267 const char *name, CURLoption tag,
268 const struct NameValue *nvlist, long lval)
269 {
270 CURLcode ret = CURLE_OK;
271 bool skip = FALSE;
272
273 ret = curl_easy_setopt(curl, tag, lval);
274 if(!lval)
275 skip = TRUE;
276
277 if(config->libcurl && !skip && !ret) {
278 /* we only use this for real if --libcurl was used */
279 const struct NameValue *nv = NULL;
280 for(nv = nvlist; nv->name; nv++) {
281 if(nv->value == lval)
282 break; /* found it */
283 }
284 if(!nv->name) {
285 /* If no definition was found, output an explicit value.
286 * This could happen if new values are defined and used
287 * but the NameValue list is not updated. */
288 CODE2("curl_easy_setopt(hnd, %s, %ldL);", name, lval);
289 }
290 else {
291 CODE2("curl_easy_setopt(hnd, %s, (long)%s);", name, nv->name);
292 }
293 }
294
295 #ifdef DEBUGBUILD
296 if(ret)
297 warnf(config, "option %s returned error (%d)\n", name, (int)ret);
298 #endif
299 nomem:
300 return ret;
301 }
302
303 /* setopt wrapper for flags */
tool_setopt_flags(CURL * curl,struct GlobalConfig * config,const char * name,CURLoption tag,const struct NameValue * nvlist,long lval)304 CURLcode tool_setopt_flags(CURL *curl, struct GlobalConfig *config,
305 const char *name, CURLoption tag,
306 const struct NameValue *nvlist, long lval)
307 {
308 CURLcode ret = CURLE_OK;
309 bool skip = FALSE;
310
311 ret = curl_easy_setopt(curl, tag, lval);
312 if(!lval)
313 skip = TRUE;
314
315 if(config->libcurl && !skip && !ret) {
316 /* we only use this for real if --libcurl was used */
317 char preamble[80]; /* should accommodate any symbol name */
318 long rest = lval; /* bits not handled yet */
319 const struct NameValue *nv = NULL;
320 msnprintf(preamble, sizeof(preamble),
321 "curl_easy_setopt(hnd, %s, ", name);
322 for(nv = nvlist; nv->name; nv++) {
323 if((nv->value & ~ rest) == 0) {
324 /* all value flags contained in rest */
325 rest &= ~ nv->value; /* remove bits handled here */
326 CODE3("%s(long)%s%s",
327 preamble, nv->name, rest ? " |" : ");");
328 if(!rest)
329 break; /* handled them all */
330 /* replace with all spaces for continuation line */
331 msnprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), "");
332 }
333 }
334 /* If any bits have no definition, output an explicit value.
335 * This could happen if new bits are defined and used
336 * but the NameValue list is not updated. */
337 if(rest)
338 CODE2("%s%ldL);", preamble, rest);
339 }
340
341 nomem:
342 return ret;
343 }
344
345 /* setopt wrapper for bitmasks */
tool_setopt_bitmask(CURL * curl,struct GlobalConfig * config,const char * name,CURLoption tag,const struct NameValueUnsigned * nvlist,long lval)346 CURLcode tool_setopt_bitmask(CURL *curl, struct GlobalConfig *config,
347 const char *name, CURLoption tag,
348 const struct NameValueUnsigned *nvlist,
349 long lval)
350 {
351 CURLcode ret = CURLE_OK;
352 bool skip = FALSE;
353
354 ret = curl_easy_setopt(curl, tag, lval);
355 if(!lval)
356 skip = TRUE;
357
358 if(config->libcurl && !skip && !ret) {
359 /* we only use this for real if --libcurl was used */
360 char preamble[80];
361 unsigned long rest = (unsigned long)lval;
362 const struct NameValueUnsigned *nv = NULL;
363 msnprintf(preamble, sizeof(preamble),
364 "curl_easy_setopt(hnd, %s, ", name);
365 for(nv = nvlist; nv->name; nv++) {
366 if((nv->value & ~ rest) == 0) {
367 /* all value flags contained in rest */
368 rest &= ~ nv->value; /* remove bits handled here */
369 CODE3("%s(long)%s%s",
370 preamble, nv->name, rest ? " |" : ");");
371 if(!rest)
372 break; /* handled them all */
373 /* replace with all spaces for continuation line */
374 msnprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), "");
375 }
376 }
377 /* If any bits have no definition, output an explicit value.
378 * This could happen if new bits are defined and used
379 * but the NameValue list is not updated. */
380 if(rest)
381 CODE2("%s%luUL);", preamble, rest);
382 }
383
384 nomem:
385 return ret;
386 }
387
388 /* Generate code for a struct curl_slist. */
libcurl_generate_slist(struct curl_slist * slist,int * slistno)389 static CURLcode libcurl_generate_slist(struct curl_slist *slist, int *slistno)
390 {
391 CURLcode ret = CURLE_OK;
392 char *escaped = NULL;
393
394 /* May need several slist variables, so invent name */
395 *slistno = ++easysrc_slist_count;
396
397 DECL1("struct curl_slist *slist%d;", *slistno);
398 DATA1("slist%d = NULL;", *slistno);
399 CLEAN1("curl_slist_free_all(slist%d);", *slistno);
400 CLEAN1("slist%d = NULL;", *slistno);
401 for(; slist; slist = slist->next) {
402 Curl_safefree(escaped);
403 escaped = c_escape(slist->data, ZERO_TERMINATED);
404 if(!escaped)
405 return CURLE_OUT_OF_MEMORY;
406 DATA3("slist%d = curl_slist_append(slist%d, \"%s\");",
407 *slistno, *slistno, escaped);
408 }
409
410 nomem:
411 Curl_safefree(escaped);
412 return ret;
413 }
414
415 static CURLcode libcurl_generate_mime(CURL *curl,
416 struct GlobalConfig *config,
417 struct tool_mime *toolmime,
418 int *mimeno); /* Forward. */
419
420 /* Wrapper to generate source code for a mime part. */
libcurl_generate_mime_part(CURL * curl,struct GlobalConfig * config,struct tool_mime * part,int mimeno)421 static CURLcode libcurl_generate_mime_part(CURL *curl,
422 struct GlobalConfig *config,
423 struct tool_mime *part,
424 int mimeno)
425 {
426 CURLcode ret = CURLE_OK;
427 int submimeno = 0;
428 char *escaped = NULL;
429 const char *data = NULL;
430 const char *filename = part->filename;
431
432 /* Parts are linked in reverse order. */
433 if(part->prev) {
434 ret = libcurl_generate_mime_part(curl, config, part->prev, mimeno);
435 if(ret)
436 return ret;
437 }
438
439 /* Create the part. */
440 CODE2("part%d = curl_mime_addpart(mime%d);", mimeno, mimeno);
441
442 switch(part->kind) {
443 case TOOLMIME_PARTS:
444 ret = libcurl_generate_mime(curl, config, part, &submimeno);
445 if(!ret) {
446 CODE2("curl_mime_subparts(part%d, mime%d);", mimeno, submimeno);
447 CODE1("mime%d = NULL;", submimeno); /* Avoid freeing in CLEAN. */
448 }
449 break;
450
451 case TOOLMIME_DATA:
452 data = part->data;
453 if(!ret) {
454 Curl_safefree(escaped);
455 escaped = c_escape(data, ZERO_TERMINATED);
456 NULL_CHECK(escaped);
457 CODE2("curl_mime_data(part%d, \"%s\", CURL_ZERO_TERMINATED);",
458 mimeno, escaped);
459 }
460 break;
461
462 case TOOLMIME_FILE:
463 case TOOLMIME_FILEDATA:
464 escaped = c_escape(part->data, ZERO_TERMINATED);
465 NULL_CHECK(escaped);
466 CODE2("curl_mime_filedata(part%d, \"%s\");", mimeno, escaped);
467 if(part->kind == TOOLMIME_FILEDATA && !filename) {
468 CODE1("curl_mime_filename(part%d, NULL);", mimeno);
469 }
470 break;
471
472 case TOOLMIME_STDIN:
473 if(!filename)
474 filename = "-";
475 /* FALLTHROUGH */
476 case TOOLMIME_STDINDATA:
477 /* Can only be reading stdin in the current context. */
478 CODE1("curl_mime_data_cb(part%d, -1, (curl_read_callback) fread, \\",
479 mimeno);
480 CODE0(" (curl_seek_callback) fseek, NULL, stdin);");
481 break;
482 default:
483 /* Other cases not possible in this context. */
484 break;
485 }
486
487 if(!ret && part->encoder) {
488 Curl_safefree(escaped);
489 escaped = c_escape(part->encoder, ZERO_TERMINATED);
490 NULL_CHECK(escaped);
491 CODE2("curl_mime_encoder(part%d, \"%s\");", mimeno, escaped);
492 }
493
494 if(!ret && filename) {
495 Curl_safefree(escaped);
496 escaped = c_escape(filename, ZERO_TERMINATED);
497 NULL_CHECK(escaped);
498 CODE2("curl_mime_filename(part%d, \"%s\");", mimeno, escaped);
499 }
500
501 if(!ret && part->name) {
502 Curl_safefree(escaped);
503 escaped = c_escape(part->name, ZERO_TERMINATED);
504 NULL_CHECK(escaped);
505 CODE2("curl_mime_name(part%d, \"%s\");", mimeno, escaped);
506 }
507
508 if(!ret && part->type) {
509 Curl_safefree(escaped);
510 escaped = c_escape(part->type, ZERO_TERMINATED);
511 NULL_CHECK(escaped);
512 CODE2("curl_mime_type(part%d, \"%s\");", mimeno, escaped);
513 }
514
515 if(!ret && part->headers) {
516 int slistno;
517
518 ret = libcurl_generate_slist(part->headers, &slistno);
519 if(!ret) {
520 CODE2("curl_mime_headers(part%d, slist%d, 1);", mimeno, slistno);
521 CODE1("slist%d = NULL;", slistno); /* Prevent CLEANing. */
522 }
523 }
524
525 nomem:
526 Curl_safefree(escaped);
527 return ret;
528 }
529
530 /* Wrapper to generate source code for a mime structure. */
libcurl_generate_mime(CURL * curl,struct GlobalConfig * config,struct tool_mime * toolmime,int * mimeno)531 static CURLcode libcurl_generate_mime(CURL *curl,
532 struct GlobalConfig *config,
533 struct tool_mime *toolmime,
534 int *mimeno)
535 {
536 CURLcode ret = CURLE_OK;
537
538 /* May need several mime variables, so invent name. */
539 *mimeno = ++easysrc_mime_count;
540 DECL1("curl_mime *mime%d;", *mimeno);
541 DATA1("mime%d = NULL;", *mimeno);
542 CODE1("mime%d = curl_mime_init(hnd);", *mimeno);
543 CLEAN1("curl_mime_free(mime%d);", *mimeno);
544 CLEAN1("mime%d = NULL;", *mimeno);
545
546 if(toolmime->subparts) {
547 DECL1("curl_mimepart *part%d;", *mimeno);
548 ret = libcurl_generate_mime_part(curl, config,
549 toolmime->subparts, *mimeno);
550 }
551
552 nomem:
553 return ret;
554 }
555
556 /* setopt wrapper for CURLOPT_MIMEPOST */
tool_setopt_mimepost(CURL * curl,struct GlobalConfig * config,const char * name,CURLoption tag,curl_mime * mimepost)557 CURLcode tool_setopt_mimepost(CURL *curl, struct GlobalConfig *config,
558 const char *name, CURLoption tag,
559 curl_mime *mimepost)
560 {
561 CURLcode ret = curl_easy_setopt(curl, tag, mimepost);
562 int mimeno = 0;
563
564 if(!ret && config->libcurl) {
565 ret = libcurl_generate_mime(curl, config,
566 config->current->mimeroot, &mimeno);
567
568 if(!ret)
569 CODE2("curl_easy_setopt(hnd, %s, mime%d);", name, mimeno);
570 }
571
572 nomem:
573 return ret;
574 }
575
576 /* setopt wrapper for curl_slist options */
tool_setopt_slist(CURL * curl,struct GlobalConfig * config,const char * name,CURLoption tag,struct curl_slist * list)577 CURLcode tool_setopt_slist(CURL *curl, struct GlobalConfig *config,
578 const char *name, CURLoption tag,
579 struct curl_slist *list)
580 {
581 CURLcode ret = CURLE_OK;
582
583 ret = curl_easy_setopt(curl, tag, list);
584
585 if(config->libcurl && list && !ret) {
586 int i;
587
588 ret = libcurl_generate_slist(list, &i);
589 if(!ret)
590 CODE2("curl_easy_setopt(hnd, %s, slist%d);", name, i);
591 }
592
593 nomem:
594 return ret;
595 }
596
597 /* generic setopt wrapper for all other options.
598 * Some type information is encoded in the tag value. */
tool_setopt(CURL * curl,bool str,struct GlobalConfig * global,struct OperationConfig * config,const char * name,CURLoption tag,...)599 CURLcode tool_setopt(CURL *curl, bool str, struct GlobalConfig *global,
600 struct OperationConfig *config,
601 const char *name, CURLoption tag, ...)
602 {
603 va_list arg;
604 char buf[256];
605 const char *value = NULL;
606 bool remark = FALSE;
607 bool skip = FALSE;
608 bool escape = FALSE;
609 char *escaped = NULL;
610 CURLcode ret = CURLE_OK;
611
612 va_start(arg, tag);
613
614 if(tag < CURLOPTTYPE_OBJECTPOINT) {
615 /* Value is expected to be a long */
616 long lval = va_arg(arg, long);
617 long defval = 0L;
618 const struct NameValue *nv = NULL;
619 for(nv = setopt_nv_CURLNONZERODEFAULTS; nv->name; nv++) {
620 if(!strcmp(name, nv->name)) {
621 defval = nv->value;
622 break; /* found it */
623 }
624 }
625
626 msnprintf(buf, sizeof(buf), "%ldL", lval);
627 value = buf;
628 ret = curl_easy_setopt(curl, tag, lval);
629 if(lval == defval)
630 skip = TRUE;
631 }
632 else if(tag < CURLOPTTYPE_OFF_T) {
633 /* Value is some sort of object pointer */
634 void *pval = va_arg(arg, void *);
635
636 /* function pointers are never printable */
637 if(tag >= CURLOPTTYPE_FUNCTIONPOINT) {
638 if(pval) {
639 value = "function pointer";
640 remark = TRUE;
641 }
642 else
643 skip = TRUE;
644 }
645
646 else if(pval && str) {
647 value = (char *)pval;
648 escape = TRUE;
649 }
650 else if(pval) {
651 value = "object pointer";
652 remark = TRUE;
653 }
654 else
655 skip = TRUE;
656
657 ret = curl_easy_setopt(curl, tag, pval);
658
659 }
660 else if(tag < CURLOPTTYPE_BLOB) {
661 /* Value is expected to be curl_off_t */
662 curl_off_t oval = va_arg(arg, curl_off_t);
663 msnprintf(buf, sizeof(buf),
664 "(curl_off_t)%" CURL_FORMAT_CURL_OFF_T, oval);
665 value = buf;
666 ret = curl_easy_setopt(curl, tag, oval);
667
668 if(!oval)
669 skip = TRUE;
670 }
671 else {
672 /* Value is a blob */
673 void *pblob = va_arg(arg, void *);
674
675 /* blobs are never printable */
676 if(pblob) {
677 value = "blob pointer";
678 remark = TRUE;
679 }
680 else
681 skip = TRUE;
682
683 ret = curl_easy_setopt(curl, tag, pblob);
684 }
685
686 va_end(arg);
687
688 if(global->libcurl && !skip && !ret) {
689 /* we only use this for real if --libcurl was used */
690
691 if(remark)
692 REM3("%s was set to a%s %s", name, (*value == 'o' ? "n" : ""), value);
693 else {
694 if(escape) {
695 curl_off_t len = ZERO_TERMINATED;
696 if(tag == CURLOPT_POSTFIELDS)
697 len = config->postfieldsize;
698 escaped = c_escape(value, len);
699 NULL_CHECK(escaped);
700 CODE2("curl_easy_setopt(hnd, %s, \"%s\");", name, escaped);
701 }
702 else
703 CODE2("curl_easy_setopt(hnd, %s, %s);", name, value);
704 }
705 }
706
707 nomem:
708 Curl_safefree(escaped);
709 return ret;
710 }
711
712 #else /* CURL_DISABLE_LIBCURL_OPTION */
713
714 #endif /* CURL_DISABLE_LIBCURL_OPTION */
715