curl tool: reviewed code moved to tool_*.[ch] files
[platform/upstream/curl.git] / src / tool_setopt.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2011, 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 #include <curl/curl.h>
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 CURLcode tool_setopt(CURL *curl, bool str, struct Configurable *config,
37                      const char *name, CURLoption tag, ...)
38 {
39   va_list arg;
40   char *bufp;
41   char value[256];
42   bool remark = FALSE;
43   bool skip = FALSE;
44   CURLcode ret = CURLE_OK;
45
46   va_start(arg, tag);
47
48   if(tag < CURLOPTTYPE_OBJECTPOINT) {
49     long lval = va_arg(arg, long);
50     snprintf(value, sizeof(value), "%ldL", lval);
51     ret = curl_easy_setopt(curl, tag, lval);
52     if(!lval)
53       skip = TRUE;
54   }
55   else if(tag < CURLOPTTYPE_OFF_T) {
56     void *pval = va_arg(arg, void *);
57     unsigned char *ptr = (unsigned char *)pval;
58
59     /* function pointers are never printable */
60     if(tag >= CURLOPTTYPE_FUNCTIONPOINT) {
61       if(pval) {
62         strcpy(value, "functionpointer"); /* 'value' fits 256 bytes */
63         remark = TRUE;
64       }
65       else
66         skip = TRUE;
67     }
68
69     else if(pval && str)
70       snprintf(value, sizeof(value), "\"%s\"", (char *)ptr);
71     else if(pval) {
72       strcpy(value, "objectpointer"); /* 'value' fits 256 bytes */
73       remark = TRUE;
74     }
75     else
76       skip = TRUE;
77
78     ret = curl_easy_setopt(curl, tag, pval);
79
80   }
81   else {
82     curl_off_t oval = va_arg(arg, curl_off_t);
83     snprintf(value, sizeof(value),
84              "(curl_off_t)%" CURL_FORMAT_CURL_OFF_T, oval);
85     ret = curl_easy_setopt(curl, tag, oval);
86
87     if(!oval)
88       skip = TRUE;
89   }
90
91   va_end(arg);
92
93   if(config->libcurl && !skip && !ret) {
94     /* we only use this for real if --libcurl was used */
95
96     if(remark)
97       bufp = curlx_maprintf("%s set to a %s", name, value);
98     else
99       bufp = curlx_maprintf("curl_easy_setopt(hnd, %s, %s);", name, value);
100
101     if(!bufp)
102       ret = CURLE_OUT_OF_MEMORY;
103     else {
104       struct curl_slist *list =
105         curl_slist_append(remark?easysrc_remarks:easysrc, bufp);
106
107       curl_free(bufp);
108
109       if(!list) {
110         curl_slist_free_all(easysrc_remarks);
111         curl_slist_free_all(easysrc);
112         easysrc_remarks = NULL;
113         easysrc = NULL;
114         ret = CURLE_OUT_OF_MEMORY;
115       }
116       else if(remark)
117         easysrc_remarks = list;
118       else
119         easysrc = list;
120     }
121   }
122
123   return ret;
124 }
125