curl: fix symbolic names for CURLUSESSL_* enum in --libcurl output
[platform/upstream/curl.git] / src / tool_setopt.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2013, 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 http://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
34 #include "memdebug.h" /* keep this as LAST include */
35
36 /* Lookup tables for converting setopt values back to symbols */
37 /* For enums, values may be in any order. */
38 /* For bit masks, put combinations first, then single bits, */
39 /* and finally any "NONE" value. */
40
41 #define NV(e) {#e, e}
42 #define NV1(e, v) {#e, (v)}
43 #define NVEND {NULL, 0}         /* sentinel to mark end of list */
44
45 const NameValue setopt_nv_CURLPROXY[] = {
46   NV(CURLPROXY_HTTP),
47   NV(CURLPROXY_HTTP_1_0),
48   NV(CURLPROXY_SOCKS4),
49   NV(CURLPROXY_SOCKS5),
50   NV(CURLPROXY_SOCKS4A),
51   NV(CURLPROXY_SOCKS5_HOSTNAME),
52   NVEND,
53 };
54
55 const NameValueUnsigned setopt_nv_CURLAUTH[] = {
56   NV(CURLAUTH_ANY),             /* combination */
57   NV(CURLAUTH_ANYSAFE),         /* combination */
58   NV(CURLAUTH_BASIC),
59   NV(CURLAUTH_DIGEST),
60   NV(CURLAUTH_GSSNEGOTIATE),
61   NV(CURLAUTH_NTLM),
62   NV(CURLAUTH_DIGEST_IE),
63   NV(CURLAUTH_NTLM_WB),
64   NV(CURLAUTH_ONLY),
65   NV(CURLAUTH_NONE),
66   NVEND,
67 };
68
69 const NameValue setopt_nv_CURL_HTTP_VERSION[] = {
70   NV(CURL_HTTP_VERSION_NONE),
71   NV(CURL_HTTP_VERSION_1_0),
72   NV(CURL_HTTP_VERSION_1_1),
73   NVEND,
74 };
75
76 const NameValue setopt_nv_CURL_SSLVERSION[] = {
77   NV(CURL_SSLVERSION_DEFAULT),
78   NV(CURL_SSLVERSION_TLSv1),
79   NV(CURL_SSLVERSION_SSLv2),
80   NV(CURL_SSLVERSION_SSLv3),
81   NVEND,
82 };
83
84 const NameValue setopt_nv_CURL_TIMECOND[] = {
85   NV(CURL_TIMECOND_IFMODSINCE),
86   NV(CURL_TIMECOND_IFUNMODSINCE),
87   NV(CURL_TIMECOND_LASTMOD),
88   NV(CURL_TIMECOND_NONE),
89   NVEND,
90 };
91
92 const NameValue setopt_nv_CURLFTPSSL_CCC[] = {
93   NV(CURLFTPSSL_CCC_NONE),
94   NV(CURLFTPSSL_CCC_PASSIVE),
95   NV(CURLFTPSSL_CCC_ACTIVE),
96   NVEND,
97 };
98
99 const NameValue setopt_nv_CURLUSESSL[] = {
100   NV(CURLUSESSL_NONE),
101   NV(CURLUSESSL_TRY),
102   NV(CURLUSESSL_CONTROL),
103   NV(CURLUSESSL_ALL),
104   NVEND,
105 };
106
107 /* These mappings essentially triplicated - see
108  * tool_libinfo.c and tool_paramhlp.c */
109 const NameValue setopt_nv_CURLPROTO[] = {
110   NV(CURLPROTO_ALL),            /* combination */
111   NV(CURLPROTO_DICT),
112   NV(CURLPROTO_FILE),
113   NV(CURLPROTO_FTP),
114   NV(CURLPROTO_FTPS),
115   NV(CURLPROTO_GOPHER),
116   NV(CURLPROTO_HTTP),
117   NV(CURLPROTO_HTTPS),
118   NV(CURLPROTO_IMAP),
119   NV(CURLPROTO_IMAPS),
120   NV(CURLPROTO_LDAP),
121   NV(CURLPROTO_LDAPS),
122   NV(CURLPROTO_POP3),
123   NV(CURLPROTO_POP3S),
124   NV(CURLPROTO_RTSP),
125   NV(CURLPROTO_SCP),
126   NV(CURLPROTO_SFTP),
127   NV(CURLPROTO_SMTP),
128   NV(CURLPROTO_SMTPS),
129   NV(CURLPROTO_TELNET),
130   NV(CURLPROTO_TFTP),
131   NVEND,
132 };
133
134 /* These options have non-zero default values. */
135 static const NameValue setopt_nv_CURLNONZERODEFAULTS[] = {
136   NV1(CURLOPT_SSL_VERIFYPEER, 1),
137   NV1(CURLOPT_SSL_VERIFYHOST, 1),
138   NVEND
139 };
140
141 /* Format and add code; jump to nomem on malloc error */
142 #define ADD(args) do { \
143   ret = easysrc_add args; \
144   if(ret) \
145     goto nomem; \
146 } WHILE_FALSE
147 #define ADDF(args) do { \
148   ret = easysrc_addf args; \
149   if(ret) \
150     goto nomem; \
151 } WHILE_FALSE
152
153 #define DECL0(s) ADD((&easysrc_decl, s))
154 #define DECL1(f,a) ADDF((&easysrc_decl, f,a))
155
156 #define DATA0(s) ADD((&easysrc_data, s))
157 #define DATA1(f,a) ADDF((&easysrc_data, f,a))
158 #define DATA2(f,a,b) ADDF((&easysrc_data, f,a,b))
159 #define DATA3(f,a,b,c) ADDF((&easysrc_data, f,a,b,c))
160
161 #define CODE0(s) ADD((&easysrc_code, s))
162 #define CODE1(f,a) ADDF((&easysrc_code, f,a))
163 #define CODE2(f,a,b) ADDF((&easysrc_code, f,a,b))
164 #define CODE3(f,a,b,c) ADDF((&easysrc_code, f,a,b,c))
165
166 #define CLEAN0(s) ADD((&easysrc_clean, s))
167 #define CLEAN1(f,a) ADDF((&easysrc_clean, f,a))
168
169 #define REM0(s) ADD((&easysrc_toohard, s))
170 #define REM1(f,a) ADDF((&easysrc_toohard, f,a))
171 #define REM2(f,a,b) ADDF((&easysrc_toohard, f,a,b))
172
173 /* Escape string to C string syntax.  Return NULL if out of memory.
174  * Is this correct for those wacky EBCDIC guys? */
175 static char *c_escape(const char *str)
176 {
177   size_t len = 0;
178   const char *s;
179   unsigned char c;
180   char *escaped, *e;
181   /* Allocate space based on worst-case */
182   len = strlen(str);
183   escaped = malloc(4 * len + 1);
184   if(!escaped)
185     return NULL;
186
187   e = escaped;
188   for(s=str; (c=*s) != '\0'; s++) {
189     if(c=='\n') {
190       strcpy(e, "\\n");
191       e += 2;
192     }
193     else if(c=='\r') {
194       strcpy(e, "\\r");
195       e += 2;
196     }
197     else if(c=='\t') {
198       strcpy(e, "\\t");
199       e += 2;
200     }
201     else if(c=='\\') {
202       strcpy(e, "\\\\");
203       e += 2;
204     }
205     else if(c=='"') {
206       strcpy(e, "\\\"");
207       e += 2;
208     }
209     else if(! isprint(c)) {
210       snprintf(e, 4, "\\%03o", c);
211       e += 4;
212     }
213     else
214       *e++ = c;
215   }
216   *e = '\0';
217   return escaped;
218 }
219
220 /* setopt wrapper for enum types */
221 CURLcode tool_setopt_enum(CURL *curl, struct Configurable *config,
222                           const char *name, CURLoption tag,
223                           const NameValue *nvlist, long lval)
224 {
225   CURLcode ret = CURLE_OK;
226   bool skip = FALSE;
227
228   ret = curl_easy_setopt(curl, tag, lval);
229   if(!lval)
230     skip = TRUE;
231
232   if(config->libcurl && !skip && !ret) {
233     /* we only use this for real if --libcurl was used */
234     const NameValue *nv = NULL;
235     for(nv=nvlist; nv->name; nv++) {
236       if(nv->value == lval) break; /* found it */
237     }
238     if(! nv->name) {
239       /* If no definition was found, output an explicit value.
240        * This could happen if new values are defined and used
241        * but the NameValue list is not updated. */
242       CODE2("curl_easy_setopt(hnd, %s, %ldL);", name, lval);
243     }
244     else {
245       CODE2("curl_easy_setopt(hnd, %s, (long)%s);", name, nv->name);
246     }
247   }
248
249  nomem:
250   return ret;
251 }
252
253 /* setopt wrapper for flags */
254 CURLcode tool_setopt_flags(CURL *curl, struct Configurable *config,
255                            const char *name, CURLoption tag,
256                            const NameValue *nvlist, long lval)
257 {
258   CURLcode ret = CURLE_OK;
259   bool skip = FALSE;
260
261   ret = curl_easy_setopt(curl, tag, lval);
262   if(!lval)
263     skip = TRUE;
264
265   if(config->libcurl && !skip && !ret) {
266     /* we only use this for real if --libcurl was used */
267     char preamble[80];          /* should accommodate any symbol name */
268     long rest = lval;           /* bits not handled yet */
269     const NameValue *nv = NULL;
270     snprintf(preamble, sizeof(preamble),
271              "curl_easy_setopt(hnd, %s, ", name);
272     for(nv=nvlist; nv->name; nv++) {
273       if((nv->value & ~ rest) == 0) {
274         /* all value flags contained in rest */
275         rest &= ~ nv->value;    /* remove bits handled here */
276         CODE3("%s(long)%s%s",
277               preamble, nv->name, rest ? " |" : ");");
278         if(!rest)
279           break;                /* handled them all */
280         /* replace with all spaces for continuation line */
281         snprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), "");
282       }
283     }
284     /* If any bits have no definition, output an explicit value.
285      * This could happen if new bits are defined and used
286      * but the NameValue list is not updated. */
287     if(rest)
288       CODE2("%s%ldL);", preamble, rest);
289   }
290
291  nomem:
292   return ret;
293 }
294
295 /* setopt wrapper for bitmasks */
296 CURLcode tool_setopt_bitmask(CURL *curl, struct Configurable *config,
297                              const char *name, CURLoption tag,
298                              const NameValueUnsigned *nvlist,
299                              long lval)
300 {
301   CURLcode ret = CURLE_OK;
302   bool skip = FALSE;
303
304   ret = curl_easy_setopt(curl, tag, lval);
305   if(!lval)
306     skip = TRUE;
307
308   if(config->libcurl && !skip && !ret) {
309     /* we only use this for real if --libcurl was used */
310     char preamble[80];
311     unsigned long rest = (unsigned long)lval;
312     const NameValueUnsigned *nv = NULL;
313     snprintf(preamble, sizeof(preamble),
314              "curl_easy_setopt(hnd, %s, ", name);
315     for(nv=nvlist; nv->name; nv++) {
316       if((nv->value & ~ rest) == 0) {
317         /* all value flags contained in rest */
318         rest &= ~ nv->value;    /* remove bits handled here */
319         CODE3("%s(long)%s%s",
320               preamble, nv->name, rest ? " |" : ");");
321         if(!rest)
322           break;                /* handled them all */
323         /* replace with all spaces for continuation line */
324         snprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), "");
325       }
326     }
327     /* If any bits have no definition, output an explicit value.
328      * This could happen if new bits are defined and used
329      * but the NameValue list is not updated. */
330     if(rest)
331       CODE2("%s%luUL);", preamble, rest);
332   }
333
334  nomem:
335   return ret;
336 }
337
338 /* setopt wrapper for CURLOPT_HTTPPOST */
339 CURLcode tool_setopt_httppost(CURL *curl, struct Configurable *config,
340                               const char *name, CURLoption tag,
341                               struct curl_httppost *post)
342 {
343   CURLcode ret = CURLE_OK;
344   char *escaped = NULL;
345   bool skip = FALSE;
346
347   ret = curl_easy_setopt(curl, tag, post);
348   if(!post)
349     skip = TRUE;
350
351   if(config->libcurl && !skip && !ret) {
352     struct curl_httppost *pp, *p;
353     int i;
354     /* May use several httppost lists, if multiple POST actions */
355     i = ++ easysrc_form_count;
356     DECL1("struct curl_httppost *post%d;", i);
357     DATA1("post%d = NULL;", i);
358     CLEAN1("curl_formfree(post%d);", i);
359     CLEAN1("post%d = NULL;", i);
360     if(i == 1)
361       DECL0("struct curl_httppost *postend;");
362     DATA0("postend = NULL;");
363     for(p=post; p; p=p->next) {
364       DATA1("curl_formadd(&post%d, &postend,", i);
365       DATA1("             CURLFORM_COPYNAME, \"%s\",", p->name);
366       for(pp=p; pp; pp=pp->more) {
367         /* May be several files uploaded for one name;
368          * these are linked through the 'more' pointer */
369         Curl_safefree(escaped);
370         escaped = c_escape(pp->contents);
371         if(!escaped) {
372           ret = CURLE_OUT_OF_MEMORY;
373           goto nomem;
374         }
375         if(pp->flags & HTTPPOST_FILENAME) {
376           /* file upload as for -F @filename */
377           DATA1("             CURLFORM_FILE, \"%s\",", escaped);
378         }
379         else if(pp->flags & HTTPPOST_READFILE) {
380           /* content from file as for -F <filename */
381           DATA1("             CURLFORM_FILECONTENT, \"%s\",", escaped);
382         }
383         else
384           DATA1("             CURLFORM_COPYCONTENTS, \"%s\",", escaped);
385         if(pp->showfilename) {
386           Curl_safefree(escaped);
387           escaped = c_escape(pp->showfilename);
388           if(!escaped) {
389             ret = CURLE_OUT_OF_MEMORY;
390             goto nomem;
391           }
392           DATA1("             CURLFORM_FILENAME, \"%s\",", escaped);
393         }
394         if(pp->contenttype) {
395           Curl_safefree(escaped);
396           escaped = c_escape(pp->contenttype);
397           if(!escaped) {
398             ret = CURLE_OUT_OF_MEMORY;
399             goto nomem;
400           }
401           DATA1("             CURLFORM_CONTENTTYPE, \"%s\",", escaped);
402         }
403       }
404       DATA0("             CURLFORM_END);");
405     }
406     CODE2("curl_easy_setopt(hnd, %s, post%d);", name, i);
407   }
408
409  nomem:
410   Curl_safefree(escaped);
411   return ret;
412 }
413
414 /* setopt wrapper for curl_slist options */
415 CURLcode tool_setopt_slist(CURL *curl, struct Configurable *config,
416                            const char *name, CURLoption tag,
417                            struct curl_slist *list)
418 {
419   CURLcode ret = CURLE_OK;
420   char *escaped = NULL;
421   bool skip = FALSE;
422
423   ret = curl_easy_setopt(curl, tag, list);
424   if(!list)
425     skip = TRUE;
426
427   if(config->libcurl && !skip && !ret) {
428     struct curl_slist *s;
429     int i;
430     /* May need several slist variables, so invent name */
431     i = ++ easysrc_slist_count;
432     DECL1("struct curl_slist *slist%d;", i);
433     DATA1("slist%d = NULL;", i);
434     CLEAN1("curl_slist_free_all(slist%d);", i);
435     CLEAN1("slist%d = NULL;", i);
436     for(s=list; s; s=s->next) {
437       Curl_safefree(escaped);
438       escaped = c_escape(s->data);
439       if(!escaped) {
440         ret = CURLE_OUT_OF_MEMORY;
441         goto nomem;
442       }
443       DATA3("slist%d = curl_slist_append(slist%d, \"%s\");", i, i, escaped);
444     }
445     CODE2("curl_easy_setopt(hnd, %s, slist%d);", name, i);
446   }
447
448  nomem:
449   Curl_safefree(escaped);
450   return ret;
451 }
452
453 /* generic setopt wrapper for all other options.
454  * Some type information is encoded in the tag value. */
455 CURLcode tool_setopt(CURL *curl, bool str, struct Configurable *config,
456                      const char *name, CURLoption tag, ...)
457 {
458   va_list arg;
459   char buf[256];
460   const char *value = NULL;
461   bool remark = FALSE;
462   bool skip = FALSE;
463   bool escape = FALSE;
464   char *escaped = NULL;
465   CURLcode ret = CURLE_OK;
466
467   va_start(arg, tag);
468
469   if(tag < CURLOPTTYPE_OBJECTPOINT) {
470     /* Value is expected to be a long */
471     long lval = va_arg(arg, long);
472     long defval = 0L;
473     const NameValue *nv = NULL;
474     for(nv=setopt_nv_CURLNONZERODEFAULTS; nv->name; nv++) {
475       if(!strcmp(name, nv->name)) {
476         defval = nv->value;
477         break; /* found it */
478       }
479     }
480
481     snprintf(buf, sizeof(buf), "%ldL", lval);
482     value = buf;
483     ret = curl_easy_setopt(curl, tag, lval);
484     if(lval == defval)
485       skip = TRUE;
486   }
487   else if(tag < CURLOPTTYPE_OFF_T) {
488     /* Value is some sort of object pointer */
489     void *pval = va_arg(arg, void *);
490
491     /* function pointers are never printable */
492     if(tag >= CURLOPTTYPE_FUNCTIONPOINT) {
493       if(pval) {
494         value = "functionpointer";
495         remark = TRUE;
496       }
497       else
498         skip = TRUE;
499     }
500
501     else if(pval && str) {
502       value = (char *)pval;
503       escape = TRUE;
504     }
505     else if(pval) {
506       value = "objectpointer";
507       remark = TRUE;
508     }
509     else
510       skip = TRUE;
511
512     ret = curl_easy_setopt(curl, tag, pval);
513
514   }
515   else {
516     /* Value is expected to be curl_off_t */
517     curl_off_t oval = va_arg(arg, curl_off_t);
518     snprintf(buf, sizeof(buf),
519              "(curl_off_t)%" CURL_FORMAT_CURL_OFF_T, oval);
520     value = buf;
521     ret = curl_easy_setopt(curl, tag, oval);
522
523     if(!oval)
524       skip = TRUE;
525   }
526
527   va_end(arg);
528
529   if(config->libcurl && !skip && !ret) {
530     /* we only use this for real if --libcurl was used */
531
532     if(remark)
533       REM2("%s set to a %s", name, value);
534     else {
535       if(escape) {
536         escaped = c_escape(value);
537         if(!escaped) {
538           ret = CURLE_OUT_OF_MEMORY;
539           goto nomem;
540         }
541         CODE2("curl_easy_setopt(hnd, %s, \"%s\");", name, escaped);
542       }
543       else
544         CODE2("curl_easy_setopt(hnd, %s, %s);", name, value);
545     }
546   }
547
548  nomem:
549   Curl_safefree(escaped);
550   return ret;
551 }
552
553 #endif /* CURL_DISABLE_LIBCURL_OPTION */