Yang Tse | 9ecf53e | 2011-09-21 01:54:14 +0200 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
Daniel Stenberg | 2bc1d77 | 2023-01-02 13:51:48 +0100 | [diff] [blame] | 8 | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
Yang Tse | 9ecf53e | 2011-09-21 01:54:14 +0200 | [diff] [blame] | 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 |
Daniel Stenberg | 4d2f800 | 2020-11-04 14:02:01 +0100 | [diff] [blame] | 12 | * are also available at https://curl.se/docs/copyright.html. |
Yang Tse | 9ecf53e | 2011-09-21 01:54:14 +0200 | [diff] [blame] | 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 | * |
max.mehl | ad9bc59 | 2022-05-17 11:16:50 +0200 | [diff] [blame] | 21 | * SPDX-License-Identifier: curl |
| 22 | * |
Yang Tse | 9ecf53e | 2011-09-21 01:54:14 +0200 | [diff] [blame] | 23 | ***************************************************************************/ |
Yang Tse | 919c97f | 2012-04-06 23:35:15 +0200 | [diff] [blame] | 24 | #include "tool_setup.h" |
Yang Tse | 9ecf53e | 2011-09-21 01:54:14 +0200 | [diff] [blame] | 25 | |
Daniel Hwang | 19cb0c4 | 2015-10-17 23:57:58 +0200 | [diff] [blame] | 26 | #include "slist_wc.h" |
| 27 | |
Colin Hogben | 2b26eb9 | 2012-02-05 17:44:22 +0000 | [diff] [blame] | 28 | #ifndef CURL_DISABLE_LIBCURL_OPTION |
| 29 | |
Yang Tse | 9ecf53e | 2011-09-21 01:54:14 +0200 | [diff] [blame] | 30 | #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 Tse | 4a5aa66 | 2013-01-04 02:50:28 +0100 | [diff] [blame] | 38 | #include "memdebug.h" /* keep this as LAST include */ |
Yang Tse | 9ecf53e | 2011-09-21 01:54:14 +0200 | [diff] [blame] | 39 | |
| 40 | /* global variable definitions, for easy-interface source code generation */ |
| 41 | |
Daniel Hwang | 19cb0c4 | 2015-10-17 23:57:58 +0200 | [diff] [blame] | 42 | struct slist_wc *easysrc_decl = NULL; /* Variable declarations */ |
| 43 | struct slist_wc *easysrc_data = NULL; /* Build slists, forms etc. */ |
| 44 | struct slist_wc *easysrc_code = NULL; /* Setopt calls */ |
| 45 | struct slist_wc *easysrc_toohard = NULL; /* Unconvertible setopt */ |
| 46 | struct slist_wc *easysrc_clean = NULL; /* Clean up allocated data */ |
Patrick Monnerat | fec7a85 | 2017-09-02 18:17:33 +0100 | [diff] [blame] | 47 | int easysrc_mime_count = 0; |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 48 | int easysrc_slist_count = 0; |
Yang Tse | 9ecf53e | 2011-09-21 01:54:14 +0200 | [diff] [blame] | 49 | |
| 50 | static const char *const srchead[]={ |
| 51 | "/********* Sample code generated by the curl command line tool **********", |
| 52 | " * All curl_easy_setopt() options are documented at:", |
Daniel Stenberg | 4d2f800 | 2020-11-04 14:02:01 +0100 | [diff] [blame] | 53 | " * https://curl.se/libcurl/c/curl_easy_setopt.html", |
Yang Tse | 9ecf53e | 2011-09-21 01:54:14 +0200 | [diff] [blame] | 54 | " ************************************************************************/", |
| 55 | "#include <curl/curl.h>", |
| 56 | "", |
| 57 | "int main(int argc, char *argv[])", |
| 58 | "{", |
| 59 | " CURLcode ret;", |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 60 | " CURL *hnd;", |
Yang Tse | 9ecf53e | 2011-09-21 01:54:14 +0200 | [diff] [blame] | 61 | NULL |
| 62 | }; |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 63 | /* easysrc_decl declarations come here */ |
Daniel Stenberg | 4a8f686 | 2022-04-05 13:32:26 +0200 | [diff] [blame] | 64 | /* easysrc_data initialization come here */ |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 65 | /* easysrc_code statements come here */ |
| 66 | static const char *const srchard[]={ |
| 67 | "/* Here is a list of options the curl code used that cannot get generated", |
Борис Верховский | 3aead05 | 2021-10-01 08:57:00 -0600 | [diff] [blame] | 68 | " as source easily. You may choose to either not use them or implement", |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 69 | " them yourself.", |
| 70 | "", |
| 71 | NULL |
| 72 | }; |
| 73 | static 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 */ |
| 82 | static void easysrc_free(void) |
| 83 | { |
Daniel Hwang | 19cb0c4 | 2015-10-17 23:57:58 +0200 | [diff] [blame] | 84 | slist_wc_free_all(easysrc_decl); |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 85 | easysrc_decl = NULL; |
Daniel Hwang | 19cb0c4 | 2015-10-17 23:57:58 +0200 | [diff] [blame] | 86 | slist_wc_free_all(easysrc_data); |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 87 | easysrc_data = NULL; |
Daniel Hwang | 19cb0c4 | 2015-10-17 23:57:58 +0200 | [diff] [blame] | 88 | slist_wc_free_all(easysrc_code); |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 89 | easysrc_code = NULL; |
Daniel Hwang | 19cb0c4 | 2015-10-17 23:57:58 +0200 | [diff] [blame] | 90 | slist_wc_free_all(easysrc_toohard); |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 91 | easysrc_toohard = NULL; |
Daniel Hwang | 19cb0c4 | 2015-10-17 23:57:58 +0200 | [diff] [blame] | 92 | slist_wc_free_all(easysrc_clean); |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 93 | easysrc_clean = NULL; |
| 94 | } |
| 95 | |
| 96 | /* Add a source line to the main code or remarks */ |
Daniel Hwang | 19cb0c4 | 2015-10-17 23:57:58 +0200 | [diff] [blame] | 97 | CURLcode easysrc_add(struct slist_wc **plist, const char *line) |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 98 | { |
| 99 | CURLcode ret = CURLE_OK; |
Daniel Hwang | 19cb0c4 | 2015-10-17 23:57:58 +0200 | [diff] [blame] | 100 | struct slist_wc *list = slist_wc_append(*plist, line); |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 101 | if(!list) { |
| 102 | easysrc_free(); |
| 103 | ret = CURLE_OUT_OF_MEMORY; |
| 104 | } |
| 105 | else |
| 106 | *plist = list; |
| 107 | return ret; |
| 108 | } |
| 109 | |
Daniel Hwang | 19cb0c4 | 2015-10-17 23:57:58 +0200 | [diff] [blame] | 110 | CURLcode easysrc_addf(struct slist_wc **plist, const char *fmt, ...) |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 111 | { |
| 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 Stenberg | 26a7d51 | 2020-10-02 10:58:52 +0200 | [diff] [blame] | 118 | if(!bufp) { |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 119 | 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 Satiro | 9c1806a | 2019-11-30 03:29:36 -0500 | [diff] [blame] | 128 | #define CHKRET(v) do {CURLcode ret = (v); if(ret) return ret;} while(0) |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 129 | |
| 130 | CURLcode easysrc_init(void) |
| 131 | { |
| 132 | CHKRET(easysrc_add(&easysrc_code, |
| 133 | "hnd = curl_easy_init();")); |
| 134 | return CURLE_OK; |
| 135 | } |
| 136 | |
| 137 | CURLcode 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 Stenberg | 6b84438 | 2017-09-09 23:09:06 +0200 | [diff] [blame] | 146 | for(i = 0; ((c = srchard[i]) != NULL); i++) |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 147 | CHKRET(easysrc_add(&easysrc_code, c)); |
| 148 | /* Each unconverted option */ |
Daniel Hwang | 19cb0c4 | 2015-10-17 23:57:58 +0200 | [diff] [blame] | 149 | if(easysrc_toohard) { |
Daniel Stenberg | 6b84438 | 2017-09-09 23:09:06 +0200 | [diff] [blame] | 150 | for(ptr = easysrc_toohard->first; ptr; ptr = ptr->next) |
Daniel Hwang | 19cb0c4 | 2015-10-17 23:57:58 +0200 | [diff] [blame] | 151 | CHKRET(easysrc_add(&easysrc_code, ptr->data)); |
| 152 | } |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 153 | CHKRET(easysrc_add(&easysrc_code, "")); |
| 154 | CHKRET(easysrc_add(&easysrc_code, "*/")); |
| 155 | |
Daniel Hwang | 19cb0c4 | 2015-10-17 23:57:58 +0200 | [diff] [blame] | 156 | slist_wc_free_all(easysrc_toohard); |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 157 | easysrc_toohard = NULL; |
| 158 | } |
| 159 | |
| 160 | CHKRET(easysrc_add(&easysrc_code, "")); |
| 161 | CHKRET(easysrc_add(&easysrc_code, "ret = curl_easy_perform(hnd);")); |
Steve Holme | 68920b6 | 2014-03-02 09:28:17 +0000 | [diff] [blame] | 162 | CHKRET(easysrc_add(&easysrc_code, "")); |
| 163 | |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 164 | return CURLE_OK; |
| 165 | } |
| 166 | |
| 167 | CURLcode easysrc_cleanup(void) |
| 168 | { |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 169 | CHKRET(easysrc_add(&easysrc_code, "curl_easy_cleanup(hnd);")); |
| 170 | CHKRET(easysrc_add(&easysrc_code, "hnd = NULL;")); |
Steve Holme | 68920b6 | 2014-03-02 09:28:17 +0000 | [diff] [blame] | 171 | |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 172 | return CURLE_OK; |
| 173 | } |
Yang Tse | 9ecf53e | 2011-09-21 01:54:14 +0200 | [diff] [blame] | 174 | |
Steve Holme | f1a9e68 | 2014-03-01 13:38:00 +0000 | [diff] [blame] | 175 | void dumpeasysrc(struct GlobalConfig *config) |
Yang Tse | 9ecf53e | 2011-09-21 01:54:14 +0200 | [diff] [blame] | 176 | { |
| 177 | struct curl_slist *ptr; |
| 178 | char *o = config->libcurl; |
| 179 | |
Daniel Hwang | 1467dec | 2015-09-21 21:06:42 -0700 | [diff] [blame] | 180 | 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 Tse | 9ecf53e | 2011-09-21 01:54:14 +0200 | [diff] [blame] | 193 | |
Daniel Stenberg | 6b84438 | 2017-09-09 23:09:06 +0200 | [diff] [blame] | 194 | for(i = 0; ((c = srchead[i]) != NULL); i++) |
Daniel Hwang | 1467dec | 2015-09-21 21:06:42 -0700 | [diff] [blame] | 195 | fprintf(out, "%s\n", c); |
Yang Tse | 9ecf53e | 2011-09-21 01:54:14 +0200 | [diff] [blame] | 196 | |
Daniel Hwang | 1467dec | 2015-09-21 21:06:42 -0700 | [diff] [blame] | 197 | /* Declare variables used for complex setopt values */ |
Daniel Hwang | 19cb0c4 | 2015-10-17 23:57:58 +0200 | [diff] [blame] | 198 | if(easysrc_decl) { |
Daniel Stenberg | 6b84438 | 2017-09-09 23:09:06 +0200 | [diff] [blame] | 199 | for(ptr = easysrc_decl->first; ptr; ptr = ptr->next) |
Daniel Hwang | 19cb0c4 | 2015-10-17 23:57:58 +0200 | [diff] [blame] | 200 | fprintf(out, " %s\n", ptr->data); |
| 201 | } |
Yang Tse | 9ecf53e | 2011-09-21 01:54:14 +0200 | [diff] [blame] | 202 | |
Daniel Hwang | 1467dec | 2015-09-21 21:06:42 -0700 | [diff] [blame] | 203 | /* Set up complex values for setopt calls */ |
| 204 | if(easysrc_data) { |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 205 | fprintf(out, "\n"); |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 206 | |
Daniel Stenberg | 6b84438 | 2017-09-09 23:09:06 +0200 | [diff] [blame] | 207 | for(ptr = easysrc_data->first; ptr; ptr = ptr->next) |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 208 | fprintf(out, " %s\n", ptr->data); |
Yang Tse | 9ecf53e | 2011-09-21 01:54:14 +0200 | [diff] [blame] | 209 | } |
Daniel Hwang | 1467dec | 2015-09-21 21:06:42 -0700 | [diff] [blame] | 210 | |
| 211 | fprintf(out, "\n"); |
Daniel Hwang | 19cb0c4 | 2015-10-17 23:57:58 +0200 | [diff] [blame] | 212 | if(easysrc_code) { |
Daniel Stenberg | 6b84438 | 2017-09-09 23:09:06 +0200 | [diff] [blame] | 213 | for(ptr = easysrc_code->first; ptr; ptr = ptr->next) { |
Daniel Hwang | 19cb0c4 | 2015-10-17 23:57:58 +0200 | [diff] [blame] | 214 | if(ptr->data[0]) { |
| 215 | fprintf(out, " %s\n", ptr->data); |
| 216 | } |
| 217 | else { |
| 218 | fprintf(out, "\n"); |
| 219 | } |
Daniel Hwang | 1467dec | 2015-09-21 21:06:42 -0700 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
Daniel Hwang | 19cb0c4 | 2015-10-17 23:57:58 +0200 | [diff] [blame] | 223 | if(easysrc_clean) { |
Daniel Stenberg | 6b84438 | 2017-09-09 23:09:06 +0200 | [diff] [blame] | 224 | for(ptr = easysrc_clean->first; ptr; ptr = ptr->next) |
Daniel Hwang | 19cb0c4 | 2015-10-17 23:57:58 +0200 | [diff] [blame] | 225 | fprintf(out, " %s\n", ptr->data); |
| 226 | } |
Daniel Hwang | 1467dec | 2015-09-21 21:06:42 -0700 | [diff] [blame] | 227 | |
Daniel Stenberg | 6b84438 | 2017-09-09 23:09:06 +0200 | [diff] [blame] | 228 | for(i = 0; ((c = srcend[i]) != NULL); i++) |
Daniel Hwang | 1467dec | 2015-09-21 21:06:42 -0700 | [diff] [blame] | 229 | fprintf(out, "%s\n", c); |
| 230 | |
| 231 | if(fopened) |
| 232 | fclose(out); |
Yang Tse | 9ecf53e | 2011-09-21 01:54:14 +0200 | [diff] [blame] | 233 | } |
| 234 | |
Colin Hogben | 9954242 | 2012-02-23 09:43:37 +0000 | [diff] [blame] | 235 | easysrc_free(); |
Yang Tse | 9ecf53e | 2011-09-21 01:54:14 +0200 | [diff] [blame] | 236 | } |
| 237 | |
Colin Hogben | 2b26eb9 | 2012-02-05 17:44:22 +0000 | [diff] [blame] | 238 | #endif /* CURL_DISABLE_LIBCURL_OPTION */ |