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