tool_easysrc.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 #include "tool_setup.h"
00023 
00024 #include "slist_wc.h"
00025 
00026 #ifndef CURL_DISABLE_LIBCURL_OPTION
00027 
00028 #define ENABLE_CURLX_PRINTF
00029 /* use our own printf() functions */
00030 #include "curlx.h"
00031 
00032 #include "tool_cfgable.h"
00033 #include "tool_easysrc.h"
00034 #include "tool_msgs.h"
00035 
00036 #include "memdebug.h" /* keep this as LAST include */
00037 
00038 /* global variable definitions, for easy-interface source code generation */
00039 
00040 struct slist_wc *easysrc_decl = NULL; /* Variable declarations */
00041 struct slist_wc *easysrc_data = NULL; /* Build slists, forms etc. */
00042 struct slist_wc *easysrc_code = NULL; /* Setopt calls */
00043 struct slist_wc *easysrc_toohard = NULL; /* Unconvertible setopt */
00044 struct slist_wc *easysrc_clean = NULL;  /* Clean up allocated data */
00045 int easysrc_form_count = 0;
00046 int easysrc_slist_count = 0;
00047 
00048 static const char *const srchead[]={
00049   "/********* Sample code generated by the curl command line tool **********",
00050   " * All curl_easy_setopt() options are documented at:",
00051   " * https://curl.haxx.se/libcurl/c/curl_easy_setopt.html",
00052   " ************************************************************************/",
00053   "#include <curl/curl.h>",
00054   "",
00055   "int main(int argc, char *argv[])",
00056   "{",
00057   "  CURLcode ret;",
00058   "  CURL *hnd;",
00059   NULL
00060 };
00061 /* easysrc_decl declarations come here */
00062 /* easysrc_data initialisations come here */
00063 /* easysrc_code statements come here */
00064 static const char *const srchard[]={
00065   "/* Here is a list of options the curl code used that cannot get generated",
00066   "   as source easily. You may select to either not use them or implement",
00067   "   them yourself.",
00068   "",
00069   NULL
00070 };
00071 static const char *const srcend[]={
00072   "",
00073   "  return (int)ret;",
00074   "}",
00075   "/**** End of sample code ****/",
00076   NULL
00077 };
00078 
00079 /* Clean up all source code if we run out of memory */
00080 static void easysrc_free(void)
00081 {
00082   slist_wc_free_all(easysrc_decl);
00083   easysrc_decl = NULL;
00084   slist_wc_free_all(easysrc_data);
00085   easysrc_data = NULL;
00086   slist_wc_free_all(easysrc_code);
00087   easysrc_code = NULL;
00088   slist_wc_free_all(easysrc_toohard);
00089   easysrc_toohard = NULL;
00090   slist_wc_free_all(easysrc_clean);
00091   easysrc_clean = NULL;
00092 }
00093 
00094 /* Add a source line to the main code or remarks */
00095 CURLcode easysrc_add(struct slist_wc **plist, const char *line)
00096 {
00097   CURLcode ret = CURLE_OK;
00098   struct slist_wc *list = slist_wc_append(*plist, line);
00099   if(!list) {
00100     easysrc_free();
00101     ret = CURLE_OUT_OF_MEMORY;
00102   }
00103   else
00104     *plist = list;
00105   return ret;
00106 }
00107 
00108 CURLcode easysrc_addf(struct slist_wc **plist, const char *fmt, ...)
00109 {
00110   CURLcode ret;
00111   char *bufp;
00112   va_list ap;
00113   va_start(ap, fmt);
00114   bufp = curlx_mvaprintf(fmt, ap);
00115   va_end(ap);
00116   if(! bufp) {
00117     ret = CURLE_OUT_OF_MEMORY;
00118   }
00119   else {
00120     ret = easysrc_add(plist, bufp);
00121     curl_free(bufp);
00122   }
00123   return ret;
00124 }
00125 
00126 #define CHKRET(v) do {CURLcode ret = (v); if(ret) return ret;} WHILE_FALSE
00127 
00128 CURLcode easysrc_init(void)
00129 {
00130   CHKRET(easysrc_add(&easysrc_code,
00131                      "hnd = curl_easy_init();"));
00132   return CURLE_OK;
00133 }
00134 
00135 CURLcode easysrc_perform(void)
00136 {
00137   /* Note any setopt calls which we could not convert */
00138   if(easysrc_toohard) {
00139     int i;
00140     struct curl_slist *ptr;
00141     const char *c;
00142     CHKRET(easysrc_add(&easysrc_code, ""));
00143     /* Preamble comment */
00144     for(i=0; ((c = srchard[i]) != NULL); i++)
00145       CHKRET(easysrc_add(&easysrc_code, c));
00146     /* Each unconverted option */
00147     if(easysrc_toohard) {
00148       for(ptr=easysrc_toohard->first; ptr; ptr = ptr->next)
00149         CHKRET(easysrc_add(&easysrc_code, ptr->data));
00150     }
00151     CHKRET(easysrc_add(&easysrc_code, ""));
00152     CHKRET(easysrc_add(&easysrc_code, "*/"));
00153 
00154     slist_wc_free_all(easysrc_toohard);
00155     easysrc_toohard = NULL;
00156   }
00157 
00158   CHKRET(easysrc_add(&easysrc_code, ""));
00159   CHKRET(easysrc_add(&easysrc_code, "ret = curl_easy_perform(hnd);"));
00160   CHKRET(easysrc_add(&easysrc_code, ""));
00161 
00162   return CURLE_OK;
00163 }
00164 
00165 CURLcode easysrc_cleanup(void)
00166 {
00167   CHKRET(easysrc_add(&easysrc_code, "curl_easy_cleanup(hnd);"));
00168   CHKRET(easysrc_add(&easysrc_code, "hnd = NULL;"));
00169 
00170   return CURLE_OK;
00171 }
00172 
00173 void dumpeasysrc(struct GlobalConfig *config)
00174 {
00175   struct curl_slist *ptr;
00176   char *o = config->libcurl;
00177 
00178   FILE *out;
00179   bool fopened = FALSE;
00180   if(strcmp(o, "-")) {
00181     out = fopen(o, FOPEN_WRITETEXT);
00182     fopened = TRUE;
00183   }
00184   else
00185     out = stdout;
00186   if(!out)
00187     warnf(config, "Failed to open %s to write libcurl code!\n", o);
00188   else {
00189     int i;
00190     const char *c;
00191 
00192     for(i=0; ((c = srchead[i]) != NULL); i++)
00193       fprintf(out, "%s\n", c);
00194 
00195     /* Declare variables used for complex setopt values */
00196     if(easysrc_decl) {
00197       for(ptr=easysrc_decl->first; ptr; ptr = ptr->next)
00198         fprintf(out, "  %s\n", ptr->data);
00199     }
00200 
00201     /* Set up complex values for setopt calls */
00202     if(easysrc_data) {
00203       fprintf(out, "\n");
00204 
00205       for(ptr=easysrc_data->first; ptr; ptr = ptr->next)
00206         fprintf(out, "  %s\n", ptr->data);
00207     }
00208 
00209     fprintf(out, "\n");
00210     if(easysrc_code) {
00211       for(ptr=easysrc_code->first; ptr; ptr = ptr->next) {
00212         if(ptr->data[0]) {
00213           fprintf(out, "  %s\n", ptr->data);
00214         }
00215         else {
00216           fprintf(out, "\n");
00217         }
00218       }
00219     }
00220 
00221     if(easysrc_clean) {
00222       for(ptr=easysrc_clean->first; ptr; ptr = ptr->next)
00223         fprintf(out, "  %s\n", ptr->data);
00224     }
00225 
00226     for(i=0; ((c = srcend[i]) != NULL); i++)
00227       fprintf(out, "%s\n", c);
00228 
00229     if(fopened)
00230       fclose(out);
00231   }
00232 
00233   easysrc_free();
00234 }
00235 
00236 #endif /* CURL_DISABLE_LIBCURL_OPTION */


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:07