blob: 0239e398c2f98e05e5b1435165003a386d34b366 [file] [log] [blame]
Yang Tse9ecf53e2011-09-21 01:54:14 +02001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
Daniel Stenberg2bc1d772023-01-02 13:51:48 +01008 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
Yang Tse9ecf53e2011-09-21 01:54:14 +02009 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
Daniel Stenberg4d2f8002020-11-04 14:02:01 +010012 * are also available at https://curl.se/docs/copyright.html.
Yang Tse9ecf53e2011-09-21 01:54:14 +020013 *
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 *
max.mehlad9bc592022-05-17 11:16:50 +020021 * SPDX-License-Identifier: curl
22 *
Yang Tse9ecf53e2011-09-21 01:54:14 +020023 ***************************************************************************/
Yang Tse919c97f2012-04-06 23:35:15 +020024#include "tool_setup.h"
Yang Tse9ecf53e2011-09-21 01:54:14 +020025
Daniel Hwang19cb0c42015-10-17 23:57:58 +020026#include "slist_wc.h"
27
Colin Hogben2b26eb92012-02-05 17:44:22 +000028#ifndef CURL_DISABLE_LIBCURL_OPTION
29
Yang Tse9ecf53e2011-09-21 01:54:14 +020030#define ENABLE_CURLX_PRINTF
31/* use our own printf() functions */
32#include "curlx.h"
33
34#include "tool_cfgable.h"
35#include "tool_easysrc.h"
36#include "tool_msgs.h"
37
Yang Tse4a5aa662013-01-04 02:50:28 +010038#include "memdebug.h" /* keep this as LAST include */
Yang Tse9ecf53e2011-09-21 01:54:14 +020039
40/* global variable definitions, for easy-interface source code generation */
41
Daniel Hwang19cb0c42015-10-17 23:57:58 +020042struct slist_wc *easysrc_decl = NULL; /* Variable declarations */
43struct slist_wc *easysrc_data = NULL; /* Build slists, forms etc. */
44struct slist_wc *easysrc_code = NULL; /* Setopt calls */
45struct slist_wc *easysrc_toohard = NULL; /* Unconvertible setopt */
46struct slist_wc *easysrc_clean = NULL; /* Clean up allocated data */
Patrick Monneratfec7a852017-09-02 18:17:33 +010047int easysrc_mime_count = 0;
Colin Hogben99542422012-02-23 09:43:37 +000048int easysrc_slist_count = 0;
Yang Tse9ecf53e2011-09-21 01:54:14 +020049
50static const char *const srchead[]={
51 "/********* Sample code generated by the curl command line tool **********",
52 " * All curl_easy_setopt() options are documented at:",
Daniel Stenberg4d2f8002020-11-04 14:02:01 +010053 " * https://curl.se/libcurl/c/curl_easy_setopt.html",
Yang Tse9ecf53e2011-09-21 01:54:14 +020054 " ************************************************************************/",
55 "#include <curl/curl.h>",
56 "",
57 "int main(int argc, char *argv[])",
58 "{",
59 " CURLcode ret;",
Colin Hogben99542422012-02-23 09:43:37 +000060 " CURL *hnd;",
Yang Tse9ecf53e2011-09-21 01:54:14 +020061 NULL
62};
Colin Hogben99542422012-02-23 09:43:37 +000063/* easysrc_decl declarations come here */
Daniel Stenberg4a8f6862022-04-05 13:32:26 +020064/* easysrc_data initialization come here */
Colin Hogben99542422012-02-23 09:43:37 +000065/* easysrc_code statements come here */
66static const char *const srchard[]={
67 "/* Here is a list of options the curl code used that cannot get generated",
Борис Верховский3aead052021-10-01 08:57:00 -060068 " as source easily. You may choose to either not use them or implement",
Colin Hogben99542422012-02-23 09:43:37 +000069 " them yourself.",
70 "",
71 NULL
72};
73static const char *const srcend[]={
74 "",
75 " return (int)ret;",
76 "}",
77 "/**** End of sample code ****/",
78 NULL
79};
80
81/* Clean up all source code if we run out of memory */
82static void easysrc_free(void)
83{
Daniel Hwang19cb0c42015-10-17 23:57:58 +020084 slist_wc_free_all(easysrc_decl);
Colin Hogben99542422012-02-23 09:43:37 +000085 easysrc_decl = NULL;
Daniel Hwang19cb0c42015-10-17 23:57:58 +020086 slist_wc_free_all(easysrc_data);
Colin Hogben99542422012-02-23 09:43:37 +000087 easysrc_data = NULL;
Daniel Hwang19cb0c42015-10-17 23:57:58 +020088 slist_wc_free_all(easysrc_code);
Colin Hogben99542422012-02-23 09:43:37 +000089 easysrc_code = NULL;
Daniel Hwang19cb0c42015-10-17 23:57:58 +020090 slist_wc_free_all(easysrc_toohard);
Colin Hogben99542422012-02-23 09:43:37 +000091 easysrc_toohard = NULL;
Daniel Hwang19cb0c42015-10-17 23:57:58 +020092 slist_wc_free_all(easysrc_clean);
Colin Hogben99542422012-02-23 09:43:37 +000093 easysrc_clean = NULL;
94}
95
96/* Add a source line to the main code or remarks */
Daniel Hwang19cb0c42015-10-17 23:57:58 +020097CURLcode easysrc_add(struct slist_wc **plist, const char *line)
Colin Hogben99542422012-02-23 09:43:37 +000098{
99 CURLcode ret = CURLE_OK;
Daniel Hwang19cb0c42015-10-17 23:57:58 +0200100 struct slist_wc *list = slist_wc_append(*plist, line);
Colin Hogben99542422012-02-23 09:43:37 +0000101 if(!list) {
102 easysrc_free();
103 ret = CURLE_OUT_OF_MEMORY;
104 }
105 else
106 *plist = list;
107 return ret;
108}
109
Daniel Hwang19cb0c42015-10-17 23:57:58 +0200110CURLcode easysrc_addf(struct slist_wc **plist, const char *fmt, ...)
Colin Hogben99542422012-02-23 09:43:37 +0000111{
112 CURLcode ret;
113 char *bufp;
114 va_list ap;
115 va_start(ap, fmt);
116 bufp = curlx_mvaprintf(fmt, ap);
117 va_end(ap);
Daniel Stenberg26a7d512020-10-02 10:58:52 +0200118 if(!bufp) {
Colin Hogben99542422012-02-23 09:43:37 +0000119 ret = CURLE_OUT_OF_MEMORY;
120 }
121 else {
122 ret = easysrc_add(plist, bufp);
123 curl_free(bufp);
124 }
125 return ret;
126}
127
Jay Satiro9c1806a2019-11-30 03:29:36 -0500128#define CHKRET(v) do {CURLcode ret = (v); if(ret) return ret;} while(0)
Colin Hogben99542422012-02-23 09:43:37 +0000129
130CURLcode easysrc_init(void)
131{
132 CHKRET(easysrc_add(&easysrc_code,
133 "hnd = curl_easy_init();"));
134 return CURLE_OK;
135}
136
137CURLcode easysrc_perform(void)
138{
139 /* Note any setopt calls which we could not convert */
140 if(easysrc_toohard) {
141 int i;
142 struct curl_slist *ptr;
143 const char *c;
144 CHKRET(easysrc_add(&easysrc_code, ""));
145 /* Preamble comment */
Daniel Stenberg6b844382017-09-09 23:09:06 +0200146 for(i = 0; ((c = srchard[i]) != NULL); i++)
Colin Hogben99542422012-02-23 09:43:37 +0000147 CHKRET(easysrc_add(&easysrc_code, c));
148 /* Each unconverted option */
Daniel Hwang19cb0c42015-10-17 23:57:58 +0200149 if(easysrc_toohard) {
Daniel Stenberg6b844382017-09-09 23:09:06 +0200150 for(ptr = easysrc_toohard->first; ptr; ptr = ptr->next)
Daniel Hwang19cb0c42015-10-17 23:57:58 +0200151 CHKRET(easysrc_add(&easysrc_code, ptr->data));
152 }
Colin Hogben99542422012-02-23 09:43:37 +0000153 CHKRET(easysrc_add(&easysrc_code, ""));
154 CHKRET(easysrc_add(&easysrc_code, "*/"));
155
Daniel Hwang19cb0c42015-10-17 23:57:58 +0200156 slist_wc_free_all(easysrc_toohard);
Colin Hogben99542422012-02-23 09:43:37 +0000157 easysrc_toohard = NULL;
158 }
159
160 CHKRET(easysrc_add(&easysrc_code, ""));
161 CHKRET(easysrc_add(&easysrc_code, "ret = curl_easy_perform(hnd);"));
Steve Holme68920b62014-03-02 09:28:17 +0000162 CHKRET(easysrc_add(&easysrc_code, ""));
163
Colin Hogben99542422012-02-23 09:43:37 +0000164 return CURLE_OK;
165}
166
167CURLcode easysrc_cleanup(void)
168{
Colin Hogben99542422012-02-23 09:43:37 +0000169 CHKRET(easysrc_add(&easysrc_code, "curl_easy_cleanup(hnd);"));
170 CHKRET(easysrc_add(&easysrc_code, "hnd = NULL;"));
Steve Holme68920b62014-03-02 09:28:17 +0000171
Colin Hogben99542422012-02-23 09:43:37 +0000172 return CURLE_OK;
173}
Yang Tse9ecf53e2011-09-21 01:54:14 +0200174
Steve Holmef1a9e682014-03-01 13:38:00 +0000175void dumpeasysrc(struct GlobalConfig *config)
Yang Tse9ecf53e2011-09-21 01:54:14 +0200176{
177 struct curl_slist *ptr;
178 char *o = config->libcurl;
179
Daniel Hwang1467dec2015-09-21 21:06:42 -0700180 FILE *out;
181 bool fopened = FALSE;
182 if(strcmp(o, "-")) {
183 out = fopen(o, FOPEN_WRITETEXT);
184 fopened = TRUE;
185 }
186 else
187 out = stdout;
188 if(!out)
189 warnf(config, "Failed to open %s to write libcurl code!\n", o);
190 else {
191 int i;
192 const char *c;
Yang Tse9ecf53e2011-09-21 01:54:14 +0200193
Daniel Stenberg6b844382017-09-09 23:09:06 +0200194 for(i = 0; ((c = srchead[i]) != NULL); i++)
Daniel Hwang1467dec2015-09-21 21:06:42 -0700195 fprintf(out, "%s\n", c);
Yang Tse9ecf53e2011-09-21 01:54:14 +0200196
Daniel Hwang1467dec2015-09-21 21:06:42 -0700197 /* Declare variables used for complex setopt values */
Daniel Hwang19cb0c42015-10-17 23:57:58 +0200198 if(easysrc_decl) {
Daniel Stenberg6b844382017-09-09 23:09:06 +0200199 for(ptr = easysrc_decl->first; ptr; ptr = ptr->next)
Daniel Hwang19cb0c42015-10-17 23:57:58 +0200200 fprintf(out, " %s\n", ptr->data);
201 }
Yang Tse9ecf53e2011-09-21 01:54:14 +0200202
Daniel Hwang1467dec2015-09-21 21:06:42 -0700203 /* Set up complex values for setopt calls */
204 if(easysrc_data) {
Colin Hogben99542422012-02-23 09:43:37 +0000205 fprintf(out, "\n");
Colin Hogben99542422012-02-23 09:43:37 +0000206
Daniel Stenberg6b844382017-09-09 23:09:06 +0200207 for(ptr = easysrc_data->first; ptr; ptr = ptr->next)
Colin Hogben99542422012-02-23 09:43:37 +0000208 fprintf(out, " %s\n", ptr->data);
Yang Tse9ecf53e2011-09-21 01:54:14 +0200209 }
Daniel Hwang1467dec2015-09-21 21:06:42 -0700210
211 fprintf(out, "\n");
Daniel Hwang19cb0c42015-10-17 23:57:58 +0200212 if(easysrc_code) {
Daniel Stenberg6b844382017-09-09 23:09:06 +0200213 for(ptr = easysrc_code->first; ptr; ptr = ptr->next) {
Daniel Hwang19cb0c42015-10-17 23:57:58 +0200214 if(ptr->data[0]) {
215 fprintf(out, " %s\n", ptr->data);
216 }
217 else {
218 fprintf(out, "\n");
219 }
Daniel Hwang1467dec2015-09-21 21:06:42 -0700220 }
221 }
222
Daniel Hwang19cb0c42015-10-17 23:57:58 +0200223 if(easysrc_clean) {
Daniel Stenberg6b844382017-09-09 23:09:06 +0200224 for(ptr = easysrc_clean->first; ptr; ptr = ptr->next)
Daniel Hwang19cb0c42015-10-17 23:57:58 +0200225 fprintf(out, " %s\n", ptr->data);
226 }
Daniel Hwang1467dec2015-09-21 21:06:42 -0700227
Daniel Stenberg6b844382017-09-09 23:09:06 +0200228 for(i = 0; ((c = srcend[i]) != NULL); i++)
Daniel Hwang1467dec2015-09-21 21:06:42 -0700229 fprintf(out, "%s\n", c);
230
231 if(fopened)
232 fclose(out);
Yang Tse9ecf53e2011-09-21 01:54:14 +0200233 }
234
Colin Hogben99542422012-02-23 09:43:37 +0000235 easysrc_free();
Yang Tse9ecf53e2011-09-21 01:54:14 +0200236}
237
Colin Hogben2b26eb92012-02-05 17:44:22 +0000238#endif /* CURL_DISABLE_LIBCURL_OPTION */