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