tool_easysrc.c (6254B)
1 /*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 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 https://curl.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 * SPDX-License-Identifier: curl 22 * 23 ***************************************************************************/ 24 #include "tool_setup.h" 25 26 #include "slist_wc.h" 27 28 #ifndef CURL_DISABLE_LIBCURL_OPTION 29 30 #include "tool_cfgable.h" 31 #include "tool_easysrc.h" 32 #include "tool_msgs.h" 33 34 #include "memdebug.h" /* keep this as LAST include */ 35 36 /* global variable definitions, for easy-interface source code generation */ 37 38 struct slist_wc *easysrc_decl; /* Variable declarations */ 39 struct slist_wc *easysrc_data; /* Build slists, forms etc. */ 40 struct slist_wc *easysrc_code; /* Setopt calls */ 41 struct slist_wc *easysrc_toohard; /* Unconvertible setopt */ 42 struct slist_wc *easysrc_clean; /* Clean up allocated data */ 43 int easysrc_mime_count; 44 int easysrc_slist_count; 45 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 " * https://curl.se/libcurl/c/curl_easy_setopt.html", 50 " ************************************************************************/", 51 "#include <curl/curl.h>", 52 "", 53 "int main(int argc, char *argv[])", 54 "{", 55 " CURLcode ret;", 56 " CURL *hnd;", 57 NULL 58 }; 59 /* easysrc_decl declarations come here */ 60 /* easysrc_data initialization 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 choose to either not use them or implement", 65 " them yourself.", 66 "", 67 NULL 68 }; 69 static const char *const srcend[]={ 70 "", 71 " return (int)ret;", 72 "}", 73 "/**** End of sample code ****/", 74 NULL 75 }; 76 77 /* Clean up all source code if we run out of memory */ 78 static void easysrc_free(void) 79 { 80 slist_wc_free_all(easysrc_decl); 81 easysrc_decl = NULL; 82 slist_wc_free_all(easysrc_data); 83 easysrc_data = NULL; 84 slist_wc_free_all(easysrc_code); 85 easysrc_code = NULL; 86 slist_wc_free_all(easysrc_toohard); 87 easysrc_toohard = NULL; 88 slist_wc_free_all(easysrc_clean); 89 easysrc_clean = NULL; 90 } 91 92 /* Add a source line to the main code or remarks */ 93 CURLcode easysrc_add(struct slist_wc **plist, const char *line) 94 { 95 CURLcode ret = CURLE_OK; 96 struct slist_wc *list = slist_wc_append(*plist, line); 97 if(!list) { 98 easysrc_free(); 99 ret = CURLE_OUT_OF_MEMORY; 100 } 101 else 102 *plist = list; 103 return ret; 104 } 105 106 CURLcode easysrc_addf(struct slist_wc **plist, const char *fmt, ...) 107 { 108 CURLcode ret; 109 char *bufp; 110 va_list ap; 111 va_start(ap, fmt); 112 bufp = vaprintf(fmt, ap); 113 va_end(ap); 114 if(!bufp) { 115 ret = CURLE_OUT_OF_MEMORY; 116 } 117 else { 118 ret = easysrc_add(plist, bufp); 119 curl_free(bufp); 120 } 121 return ret; 122 } 123 124 CURLcode easysrc_init(void) 125 { 126 return easysrc_add(&easysrc_code, "hnd = curl_easy_init();"); 127 } 128 129 CURLcode easysrc_perform(void) 130 { 131 CURLcode ret = CURLE_OK; 132 /* Note any setopt calls which we could not convert */ 133 if(easysrc_toohard) { 134 int i; 135 struct curl_slist *ptr; 136 ret = easysrc_add(&easysrc_code, ""); 137 /* Preamble comment */ 138 for(i = 0; srchard[i] && !ret; i++) 139 ret = easysrc_add(&easysrc_code, srchard[i]); 140 /* Each unconverted option */ 141 if(easysrc_toohard && !ret) { 142 for(ptr = easysrc_toohard->first; ptr && !ret; ptr = ptr->next) 143 ret = easysrc_add(&easysrc_code, ptr->data); 144 } 145 if(!ret) 146 ret = easysrc_add(&easysrc_code, ""); 147 if(!ret) 148 ret = easysrc_add(&easysrc_code, "*/"); 149 150 slist_wc_free_all(easysrc_toohard); 151 easysrc_toohard = NULL; 152 } 153 154 if(!ret) 155 ret = easysrc_add(&easysrc_code, ""); 156 if(!ret) 157 ret = easysrc_add(&easysrc_code, "ret = curl_easy_perform(hnd);"); 158 if(!ret) 159 ret = easysrc_add(&easysrc_code, ""); 160 161 return ret; 162 } 163 164 CURLcode easysrc_cleanup(void) 165 { 166 CURLcode ret = easysrc_add(&easysrc_code, "curl_easy_cleanup(hnd);"); 167 if(!ret) 168 ret = easysrc_add(&easysrc_code, "hnd = NULL;"); 169 170 return ret; 171 } 172 173 void dumpeasysrc(struct GlobalConfig *global) 174 { 175 struct curl_slist *ptr; 176 char *o = global->libcurl; 177 178 FILE *out; 179 bool fopened = FALSE; 180 if(strcmp(o, "-")) { 181 out = fopen(o, FOPEN_WRITETEXT); 182 fopened = TRUE; 183 } 184 else 185 out = stdout; 186 if(!out) 187 warnf(global, "Failed to open %s to write libcurl code", o); 188 else { 189 int i; 190 const char *c; 191 192 for(i = 0; ((c = srchead[i]) != NULL); i++) 193 fprintf(out, "%s\n", c); 194 195 /* Declare variables used for complex setopt values */ 196 if(easysrc_decl) { 197 for(ptr = easysrc_decl->first; ptr; ptr = ptr->next) 198 fprintf(out, " %s\n", ptr->data); 199 } 200 201 /* Set up complex values for setopt calls */ 202 if(easysrc_data) { 203 fprintf(out, "\n"); 204 205 for(ptr = easysrc_data->first; ptr; ptr = ptr->next) 206 fprintf(out, " %s\n", ptr->data); 207 } 208 209 fprintf(out, "\n"); 210 if(easysrc_code) { 211 for(ptr = easysrc_code->first; ptr; ptr = ptr->next) { 212 if(ptr->data[0]) { 213 fprintf(out, " %s\n", ptr->data); 214 } 215 else { 216 fprintf(out, "\n"); 217 } 218 } 219 } 220 221 if(easysrc_clean) { 222 for(ptr = easysrc_clean->first; ptr; ptr = ptr->next) 223 fprintf(out, " %s\n", ptr->data); 224 } 225 226 for(i = 0; ((c = srcend[i]) != NULL); i++) 227 fprintf(out, "%s\n", c); 228 229 if(fopened) 230 fclose(out); 231 } 232 233 easysrc_free(); 234 } 235 236 #endif /* CURL_DISABLE_LIBCURL_OPTION */