113498266Sopenharmony_ci/*************************************************************************** 213498266Sopenharmony_ci * _ _ ____ _ 313498266Sopenharmony_ci * Project ___| | | | _ \| | 413498266Sopenharmony_ci * / __| | | | |_) | | 513498266Sopenharmony_ci * | (__| |_| | _ <| |___ 613498266Sopenharmony_ci * \___|\___/|_| \_\_____| 713498266Sopenharmony_ci * 813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 913498266Sopenharmony_ci * 1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which 1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms 1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html. 1313498266Sopenharmony_ci * 1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell 1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is 1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file. 1713498266Sopenharmony_ci * 1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 1913498266Sopenharmony_ci * KIND, either express or implied. 2013498266Sopenharmony_ci * 2113498266Sopenharmony_ci * SPDX-License-Identifier: curl 2213498266Sopenharmony_ci * 2313498266Sopenharmony_ci ***************************************************************************/ 2413498266Sopenharmony_ci#include "tool_setup.h" 2513498266Sopenharmony_ci 2613498266Sopenharmony_ci#include "slist_wc.h" 2713498266Sopenharmony_ci 2813498266Sopenharmony_ci#ifndef CURL_DISABLE_LIBCURL_OPTION 2913498266Sopenharmony_ci 3013498266Sopenharmony_ci#define ENABLE_CURLX_PRINTF 3113498266Sopenharmony_ci/* use our own printf() functions */ 3213498266Sopenharmony_ci#include "curlx.h" 3313498266Sopenharmony_ci 3413498266Sopenharmony_ci#include "tool_cfgable.h" 3513498266Sopenharmony_ci#include "tool_easysrc.h" 3613498266Sopenharmony_ci#include "tool_msgs.h" 3713498266Sopenharmony_ci 3813498266Sopenharmony_ci#include "memdebug.h" /* keep this as LAST include */ 3913498266Sopenharmony_ci 4013498266Sopenharmony_ci/* global variable definitions, for easy-interface source code generation */ 4113498266Sopenharmony_ci 4213498266Sopenharmony_cistruct slist_wc *easysrc_decl = NULL; /* Variable declarations */ 4313498266Sopenharmony_cistruct slist_wc *easysrc_data = NULL; /* Build slists, forms etc. */ 4413498266Sopenharmony_cistruct slist_wc *easysrc_code = NULL; /* Setopt calls */ 4513498266Sopenharmony_cistruct slist_wc *easysrc_toohard = NULL; /* Unconvertible setopt */ 4613498266Sopenharmony_cistruct slist_wc *easysrc_clean = NULL; /* Clean up allocated data */ 4713498266Sopenharmony_ciint easysrc_mime_count = 0; 4813498266Sopenharmony_ciint easysrc_slist_count = 0; 4913498266Sopenharmony_ci 5013498266Sopenharmony_cistatic const char *const srchead[]={ 5113498266Sopenharmony_ci "/********* Sample code generated by the curl command line tool **********", 5213498266Sopenharmony_ci " * All curl_easy_setopt() options are documented at:", 5313498266Sopenharmony_ci " * https://curl.se/libcurl/c/curl_easy_setopt.html", 5413498266Sopenharmony_ci " ************************************************************************/", 5513498266Sopenharmony_ci "#include <curl/curl.h>", 5613498266Sopenharmony_ci "", 5713498266Sopenharmony_ci "int main(int argc, char *argv[])", 5813498266Sopenharmony_ci "{", 5913498266Sopenharmony_ci " CURLcode ret;", 6013498266Sopenharmony_ci " CURL *hnd;", 6113498266Sopenharmony_ci NULL 6213498266Sopenharmony_ci}; 6313498266Sopenharmony_ci/* easysrc_decl declarations come here */ 6413498266Sopenharmony_ci/* easysrc_data initialization come here */ 6513498266Sopenharmony_ci/* easysrc_code statements come here */ 6613498266Sopenharmony_cistatic const char *const srchard[]={ 6713498266Sopenharmony_ci "/* Here is a list of options the curl code used that cannot get generated", 6813498266Sopenharmony_ci " as source easily. You may choose to either not use them or implement", 6913498266Sopenharmony_ci " them yourself.", 7013498266Sopenharmony_ci "", 7113498266Sopenharmony_ci NULL 7213498266Sopenharmony_ci}; 7313498266Sopenharmony_cistatic const char *const srcend[]={ 7413498266Sopenharmony_ci "", 7513498266Sopenharmony_ci " return (int)ret;", 7613498266Sopenharmony_ci "}", 7713498266Sopenharmony_ci "/**** End of sample code ****/", 7813498266Sopenharmony_ci NULL 7913498266Sopenharmony_ci}; 8013498266Sopenharmony_ci 8113498266Sopenharmony_ci/* Clean up all source code if we run out of memory */ 8213498266Sopenharmony_cistatic void easysrc_free(void) 8313498266Sopenharmony_ci{ 8413498266Sopenharmony_ci slist_wc_free_all(easysrc_decl); 8513498266Sopenharmony_ci easysrc_decl = NULL; 8613498266Sopenharmony_ci slist_wc_free_all(easysrc_data); 8713498266Sopenharmony_ci easysrc_data = NULL; 8813498266Sopenharmony_ci slist_wc_free_all(easysrc_code); 8913498266Sopenharmony_ci easysrc_code = NULL; 9013498266Sopenharmony_ci slist_wc_free_all(easysrc_toohard); 9113498266Sopenharmony_ci easysrc_toohard = NULL; 9213498266Sopenharmony_ci slist_wc_free_all(easysrc_clean); 9313498266Sopenharmony_ci easysrc_clean = NULL; 9413498266Sopenharmony_ci} 9513498266Sopenharmony_ci 9613498266Sopenharmony_ci/* Add a source line to the main code or remarks */ 9713498266Sopenharmony_ciCURLcode easysrc_add(struct slist_wc **plist, const char *line) 9813498266Sopenharmony_ci{ 9913498266Sopenharmony_ci CURLcode ret = CURLE_OK; 10013498266Sopenharmony_ci struct slist_wc *list = slist_wc_append(*plist, line); 10113498266Sopenharmony_ci if(!list) { 10213498266Sopenharmony_ci easysrc_free(); 10313498266Sopenharmony_ci ret = CURLE_OUT_OF_MEMORY; 10413498266Sopenharmony_ci } 10513498266Sopenharmony_ci else 10613498266Sopenharmony_ci *plist = list; 10713498266Sopenharmony_ci return ret; 10813498266Sopenharmony_ci} 10913498266Sopenharmony_ci 11013498266Sopenharmony_ciCURLcode easysrc_addf(struct slist_wc **plist, const char *fmt, ...) 11113498266Sopenharmony_ci{ 11213498266Sopenharmony_ci CURLcode ret; 11313498266Sopenharmony_ci char *bufp; 11413498266Sopenharmony_ci va_list ap; 11513498266Sopenharmony_ci va_start(ap, fmt); 11613498266Sopenharmony_ci bufp = curlx_mvaprintf(fmt, ap); 11713498266Sopenharmony_ci va_end(ap); 11813498266Sopenharmony_ci if(!bufp) { 11913498266Sopenharmony_ci ret = CURLE_OUT_OF_MEMORY; 12013498266Sopenharmony_ci } 12113498266Sopenharmony_ci else { 12213498266Sopenharmony_ci ret = easysrc_add(plist, bufp); 12313498266Sopenharmony_ci curl_free(bufp); 12413498266Sopenharmony_ci } 12513498266Sopenharmony_ci return ret; 12613498266Sopenharmony_ci} 12713498266Sopenharmony_ci 12813498266Sopenharmony_ci#define CHKRET(v) do {CURLcode ret = (v); if(ret) return ret;} while(0) 12913498266Sopenharmony_ci 13013498266Sopenharmony_ciCURLcode easysrc_init(void) 13113498266Sopenharmony_ci{ 13213498266Sopenharmony_ci CHKRET(easysrc_add(&easysrc_code, 13313498266Sopenharmony_ci "hnd = curl_easy_init();")); 13413498266Sopenharmony_ci return CURLE_OK; 13513498266Sopenharmony_ci} 13613498266Sopenharmony_ci 13713498266Sopenharmony_ciCURLcode easysrc_perform(void) 13813498266Sopenharmony_ci{ 13913498266Sopenharmony_ci /* Note any setopt calls which we could not convert */ 14013498266Sopenharmony_ci if(easysrc_toohard) { 14113498266Sopenharmony_ci int i; 14213498266Sopenharmony_ci struct curl_slist *ptr; 14313498266Sopenharmony_ci const char *c; 14413498266Sopenharmony_ci CHKRET(easysrc_add(&easysrc_code, "")); 14513498266Sopenharmony_ci /* Preamble comment */ 14613498266Sopenharmony_ci for(i = 0; ((c = srchard[i]) != NULL); i++) 14713498266Sopenharmony_ci CHKRET(easysrc_add(&easysrc_code, c)); 14813498266Sopenharmony_ci /* Each unconverted option */ 14913498266Sopenharmony_ci if(easysrc_toohard) { 15013498266Sopenharmony_ci for(ptr = easysrc_toohard->first; ptr; ptr = ptr->next) 15113498266Sopenharmony_ci CHKRET(easysrc_add(&easysrc_code, ptr->data)); 15213498266Sopenharmony_ci } 15313498266Sopenharmony_ci CHKRET(easysrc_add(&easysrc_code, "")); 15413498266Sopenharmony_ci CHKRET(easysrc_add(&easysrc_code, "*/")); 15513498266Sopenharmony_ci 15613498266Sopenharmony_ci slist_wc_free_all(easysrc_toohard); 15713498266Sopenharmony_ci easysrc_toohard = NULL; 15813498266Sopenharmony_ci } 15913498266Sopenharmony_ci 16013498266Sopenharmony_ci CHKRET(easysrc_add(&easysrc_code, "")); 16113498266Sopenharmony_ci CHKRET(easysrc_add(&easysrc_code, "ret = curl_easy_perform(hnd);")); 16213498266Sopenharmony_ci CHKRET(easysrc_add(&easysrc_code, "")); 16313498266Sopenharmony_ci 16413498266Sopenharmony_ci return CURLE_OK; 16513498266Sopenharmony_ci} 16613498266Sopenharmony_ci 16713498266Sopenharmony_ciCURLcode easysrc_cleanup(void) 16813498266Sopenharmony_ci{ 16913498266Sopenharmony_ci CHKRET(easysrc_add(&easysrc_code, "curl_easy_cleanup(hnd);")); 17013498266Sopenharmony_ci CHKRET(easysrc_add(&easysrc_code, "hnd = NULL;")); 17113498266Sopenharmony_ci 17213498266Sopenharmony_ci return CURLE_OK; 17313498266Sopenharmony_ci} 17413498266Sopenharmony_ci 17513498266Sopenharmony_civoid dumpeasysrc(struct GlobalConfig *config) 17613498266Sopenharmony_ci{ 17713498266Sopenharmony_ci struct curl_slist *ptr; 17813498266Sopenharmony_ci char *o = config->libcurl; 17913498266Sopenharmony_ci 18013498266Sopenharmony_ci FILE *out; 18113498266Sopenharmony_ci bool fopened = FALSE; 18213498266Sopenharmony_ci if(strcmp(o, "-")) { 18313498266Sopenharmony_ci out = fopen(o, FOPEN_WRITETEXT); 18413498266Sopenharmony_ci fopened = TRUE; 18513498266Sopenharmony_ci } 18613498266Sopenharmony_ci else 18713498266Sopenharmony_ci out = stdout; 18813498266Sopenharmony_ci if(!out) 18913498266Sopenharmony_ci warnf(config, "Failed to open %s to write libcurl code", o); 19013498266Sopenharmony_ci else { 19113498266Sopenharmony_ci int i; 19213498266Sopenharmony_ci const char *c; 19313498266Sopenharmony_ci 19413498266Sopenharmony_ci for(i = 0; ((c = srchead[i]) != NULL); i++) 19513498266Sopenharmony_ci fprintf(out, "%s\n", c); 19613498266Sopenharmony_ci 19713498266Sopenharmony_ci /* Declare variables used for complex setopt values */ 19813498266Sopenharmony_ci if(easysrc_decl) { 19913498266Sopenharmony_ci for(ptr = easysrc_decl->first; ptr; ptr = ptr->next) 20013498266Sopenharmony_ci fprintf(out, " %s\n", ptr->data); 20113498266Sopenharmony_ci } 20213498266Sopenharmony_ci 20313498266Sopenharmony_ci /* Set up complex values for setopt calls */ 20413498266Sopenharmony_ci if(easysrc_data) { 20513498266Sopenharmony_ci fprintf(out, "\n"); 20613498266Sopenharmony_ci 20713498266Sopenharmony_ci for(ptr = easysrc_data->first; ptr; ptr = ptr->next) 20813498266Sopenharmony_ci fprintf(out, " %s\n", ptr->data); 20913498266Sopenharmony_ci } 21013498266Sopenharmony_ci 21113498266Sopenharmony_ci fprintf(out, "\n"); 21213498266Sopenharmony_ci if(easysrc_code) { 21313498266Sopenharmony_ci for(ptr = easysrc_code->first; ptr; ptr = ptr->next) { 21413498266Sopenharmony_ci if(ptr->data[0]) { 21513498266Sopenharmony_ci fprintf(out, " %s\n", ptr->data); 21613498266Sopenharmony_ci } 21713498266Sopenharmony_ci else { 21813498266Sopenharmony_ci fprintf(out, "\n"); 21913498266Sopenharmony_ci } 22013498266Sopenharmony_ci } 22113498266Sopenharmony_ci } 22213498266Sopenharmony_ci 22313498266Sopenharmony_ci if(easysrc_clean) { 22413498266Sopenharmony_ci for(ptr = easysrc_clean->first; ptr; ptr = ptr->next) 22513498266Sopenharmony_ci fprintf(out, " %s\n", ptr->data); 22613498266Sopenharmony_ci } 22713498266Sopenharmony_ci 22813498266Sopenharmony_ci for(i = 0; ((c = srcend[i]) != NULL); i++) 22913498266Sopenharmony_ci fprintf(out, "%s\n", c); 23013498266Sopenharmony_ci 23113498266Sopenharmony_ci if(fopened) 23213498266Sopenharmony_ci fclose(out); 23313498266Sopenharmony_ci } 23413498266Sopenharmony_ci 23513498266Sopenharmony_ci easysrc_free(); 23613498266Sopenharmony_ci} 23713498266Sopenharmony_ci 23813498266Sopenharmony_ci#endif /* CURL_DISABLE_LIBCURL_OPTION */ 239