1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 #include "tool_setup.h"
24 #ifndef CURL_DISABLE_LIBCURL_OPTION
26 #define ENABLE_CURLX_PRINTF
27 /* use our own printf() functions */
30 #include "tool_cfgable.h"
31 #include "tool_easysrc.h"
32 #include "tool_msgs.h"
34 #include "memdebug.h" /* keep this as LAST include */
36 /* global variable definitions, for easy-interface source code generation */
38 struct curl_slist *easysrc_decl = NULL; /* Variable declarations */
39 struct curl_slist *easysrc_data = NULL; /* Build slists, forms etc. */
40 struct curl_slist *easysrc_code = NULL; /* Setopt calls */
41 struct curl_slist *easysrc_toohard = NULL; /* Unconvertible setopt */
42 struct curl_slist *easysrc_clean = NULL; /* Clean up allocated data */
43 int easysrc_form_count = 0;
44 int easysrc_slist_count = 0;
46 static const char *const srchead[]={
47 "/********* Sample code generated by the curl command line tool **********",
48 " * All curl_easy_setopt() options are documented at:",
49 " * http://curl.haxx.se/libcurl/c/curl_easy_setopt.html",
50 " ************************************************************************/",
51 "#include <curl/curl.h>",
53 "int main(int argc, char *argv[])",
59 /* easysrc_decl declarations come here */
60 /* easysrc_data initialisations come here */
61 /* easysrc_code statements come here */
62 static const char *const srchard[]={
63 "/* Here is a list of options the curl code used that cannot get generated",
64 " as source easily. You may select to either not use them or implement",
69 static const char *const srcend[]={
73 "/**** End of sample code ****/",
77 /* Clean up all source code if we run out of memory */
78 static void easysrc_free(void)
80 curl_slist_free_all(easysrc_decl);
82 curl_slist_free_all(easysrc_data);
84 curl_slist_free_all(easysrc_code);
86 curl_slist_free_all(easysrc_toohard);
87 easysrc_toohard = NULL;
88 curl_slist_free_all(easysrc_clean);
92 /* Add a source line to the main code or remarks */
93 CURLcode easysrc_add(struct curl_slist **plist, const char *line)
95 CURLcode ret = CURLE_OK;
96 struct curl_slist *list =
97 curl_slist_append(*plist, line);
100 ret = CURLE_OUT_OF_MEMORY;
107 CURLcode easysrc_addf(struct curl_slist **plist, const char *fmt, ...)
113 bufp = curlx_mvaprintf(fmt, ap);
116 ret = CURLE_OUT_OF_MEMORY;
119 ret = easysrc_add(plist, bufp);
125 #define CHKRET(v) do {CURLcode ret = (v); if(ret) return ret;} WHILE_FALSE
127 CURLcode easysrc_init(void)
129 CHKRET(easysrc_add(&easysrc_code,
130 "hnd = curl_easy_init();"));
134 CURLcode easysrc_perform(void)
136 /* Note any setopt calls which we could not convert */
137 if(easysrc_toohard) {
139 struct curl_slist *ptr;
141 CHKRET(easysrc_add(&easysrc_code, ""));
142 /* Preamble comment */
143 for(i=0; ((c = srchard[i]) != NULL); i++)
144 CHKRET(easysrc_add(&easysrc_code, c));
145 /* Each unconverted option */
146 for(ptr=easysrc_toohard; ptr; ptr = ptr->next)
147 CHKRET(easysrc_add(&easysrc_code, ptr->data));
148 CHKRET(easysrc_add(&easysrc_code, ""));
149 CHKRET(easysrc_add(&easysrc_code, "*/"));
151 curl_slist_free_all(easysrc_toohard);
152 easysrc_toohard = NULL;
155 CHKRET(easysrc_add(&easysrc_code, ""));
156 CHKRET(easysrc_add(&easysrc_code, "ret = curl_easy_perform(hnd);"));
160 CURLcode easysrc_cleanup(void)
162 CHKRET(easysrc_add(&easysrc_code, ""));
163 CHKRET(easysrc_add(&easysrc_code, "curl_easy_cleanup(hnd);"));
164 CHKRET(easysrc_add(&easysrc_code, "hnd = NULL;"));
168 void dumpeasysrc(struct Configurable *config)
170 struct curl_slist *ptr;
171 char *o = config->libcurl;
175 bool fopened = FALSE;
183 warnf(config, "Failed to open %s to write libcurl code!\n", o);
188 for(i=0; ((c = srchead[i]) != NULL); i++)
189 fprintf(out, "%s\n", c);
191 /* Declare variables used for complex setopt values */
192 for(ptr=easysrc_decl; ptr; ptr = ptr->next)
193 fprintf(out, " %s\n", ptr->data);
195 /* Set up complex values for setopt calls */
199 for(ptr=easysrc_data; ptr; ptr = ptr->next)
200 fprintf(out, " %s\n", ptr->data);
204 for(ptr=easysrc_code; ptr; ptr = ptr->next) {
206 fprintf(out, " %s\n", ptr->data);
213 for(ptr=easysrc_clean; ptr; ptr = ptr->next)
214 fprintf(out, " %s\n", ptr->data);
216 for(i=0; ((c = srcend[i]) != NULL); i++)
217 fprintf(out, "%s\n", c);
227 #endif /* CURL_DISABLE_LIBCURL_OPTION */