1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, 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 https://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"
26 #ifndef CURL_DISABLE_LIBCURL_OPTION
28 #define ENABLE_CURLX_PRINTF
29 /* use our own printf() functions */
32 #include "tool_cfgable.h"
33 #include "tool_easysrc.h"
34 #include "tool_msgs.h"
36 #include "memdebug.h" /* keep this as LAST include */
38 /* global variable definitions, for easy-interface source code generation */
40 struct slist_wc *easysrc_decl = NULL; /* Variable declarations */
41 struct slist_wc *easysrc_data = NULL; /* Build slists, forms etc. */
42 struct slist_wc *easysrc_code = NULL; /* Setopt calls */
43 struct slist_wc *easysrc_toohard = NULL; /* Unconvertible setopt */
44 struct slist_wc *easysrc_clean = NULL; /* Clean up allocated data */
45 int easysrc_form_count = 0;
46 int easysrc_slist_count = 0;
48 static const char *const srchead[]={
49 "/********* Sample code generated by the curl command line tool **********",
50 " * All curl_easy_setopt() options are documented at:",
51 " * https://curl.haxx.se/libcurl/c/curl_easy_setopt.html",
52 " ************************************************************************/",
53 "#include <curl/curl.h>",
55 "int main(int argc, char *argv[])",
61 /* easysrc_decl declarations come here */
62 /* easysrc_data initialisations come here */
63 /* easysrc_code statements come here */
64 static const char *const srchard[]={
65 "/* Here is a list of options the curl code used that cannot get generated",
66 " as source easily. You may select to either not use them or implement",
71 static const char *const srcend[]={
75 "/**** End of sample code ****/",
79 /* Clean up all source code if we run out of memory */
80 static void easysrc_free(void)
82 slist_wc_free_all(easysrc_decl);
84 slist_wc_free_all(easysrc_data);
86 slist_wc_free_all(easysrc_code);
88 slist_wc_free_all(easysrc_toohard);
89 easysrc_toohard = NULL;
90 slist_wc_free_all(easysrc_clean);
94 /* Add a source line to the main code or remarks */
95 CURLcode easysrc_add(struct slist_wc **plist, const char *line)
97 CURLcode ret = CURLE_OK;
98 struct slist_wc *list = slist_wc_append(*plist, line);
101 ret = CURLE_OUT_OF_MEMORY;
108 CURLcode easysrc_addf(struct slist_wc **plist, const char *fmt, ...)
114 bufp = curlx_mvaprintf(fmt, ap);
117 ret = CURLE_OUT_OF_MEMORY;
120 ret = easysrc_add(plist, bufp);
126 #define CHKRET(v) do {CURLcode ret = (v); if(ret) return ret;} WHILE_FALSE
128 CURLcode easysrc_init(void)
130 CHKRET(easysrc_add(&easysrc_code,
131 "hnd = curl_easy_init();"));
135 CURLcode easysrc_perform(void)
137 /* Note any setopt calls which we could not convert */
138 if(easysrc_toohard) {
140 struct curl_slist *ptr;
142 CHKRET(easysrc_add(&easysrc_code, ""));
143 /* Preamble comment */
144 for(i=0; ((c = srchard[i]) != NULL); i++)
145 CHKRET(easysrc_add(&easysrc_code, c));
146 /* Each unconverted option */
147 if(easysrc_toohard) {
148 for(ptr=easysrc_toohard->first; ptr; ptr = ptr->next)
149 CHKRET(easysrc_add(&easysrc_code, ptr->data));
151 CHKRET(easysrc_add(&easysrc_code, ""));
152 CHKRET(easysrc_add(&easysrc_code, "*/"));
154 slist_wc_free_all(easysrc_toohard);
155 easysrc_toohard = NULL;
158 CHKRET(easysrc_add(&easysrc_code, ""));
159 CHKRET(easysrc_add(&easysrc_code, "ret = curl_easy_perform(hnd);"));
160 CHKRET(easysrc_add(&easysrc_code, ""));
165 CURLcode easysrc_cleanup(void)
167 CHKRET(easysrc_add(&easysrc_code, "curl_easy_cleanup(hnd);"));
168 CHKRET(easysrc_add(&easysrc_code, "hnd = NULL;"));
173 void dumpeasysrc(struct GlobalConfig *config)
175 struct curl_slist *ptr;
176 char *o = config->libcurl;
179 bool fopened = FALSE;
181 out = fopen(o, FOPEN_WRITETEXT);
187 warnf(config, "Failed to open %s to write libcurl code!\n", o);
192 for(i=0; ((c = srchead[i]) != NULL); i++)
193 fprintf(out, "%s\n", c);
195 /* Declare variables used for complex setopt values */
197 for(ptr=easysrc_decl->first; ptr; ptr = ptr->next)
198 fprintf(out, " %s\n", ptr->data);
201 /* Set up complex values for setopt calls */
205 for(ptr=easysrc_data->first; ptr; ptr = ptr->next)
206 fprintf(out, " %s\n", ptr->data);
211 for(ptr=easysrc_code->first; ptr; ptr = ptr->next) {
213 fprintf(out, " %s\n", ptr->data);
222 for(ptr=easysrc_clean->first; ptr; ptr = ptr->next)
223 fprintf(out, " %s\n", ptr->data);
226 for(i=0; ((c = srcend[i]) != NULL); i++)
227 fprintf(out, "%s\n", c);
236 #endif /* CURL_DISABLE_LIBCURL_OPTION */