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