2011-09-20 19:54:14 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2012-02-05 12:44:22 -05:00
|
|
|
* Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-09-20 19:54:14 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
|
|
|
* are also available at http://curl.haxx.se/docs/copyright.html.
|
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2012-04-06 17:35:15 -04:00
|
|
|
#include "tool_setup.h"
|
2011-09-20 19:54:14 -04:00
|
|
|
|
2012-02-05 12:44:22 -05:00
|
|
|
#ifndef CURL_DISABLE_LIBCURL_OPTION
|
|
|
|
|
2011-09-20 19:54:14 -04:00
|
|
|
#define ENABLE_CURLX_PRINTF
|
|
|
|
/* use our own printf() functions */
|
|
|
|
#include "curlx.h"
|
|
|
|
|
|
|
|
#include "tool_cfgable.h"
|
|
|
|
#include "tool_easysrc.h"
|
|
|
|
#include "tool_msgs.h"
|
|
|
|
|
|
|
|
#include "memdebug.h" /* keep this as LAST include */
|
|
|
|
|
|
|
|
/* global variable definitions, for easy-interface source code generation */
|
|
|
|
|
2012-02-23 04:43:37 -05:00
|
|
|
struct curl_slist *easysrc_decl = NULL; /* Variable declarations */
|
|
|
|
struct curl_slist *easysrc_data = NULL; /* Build slists, forms etc. */
|
|
|
|
struct curl_slist *easysrc_code = NULL; /* Setopt calls */
|
|
|
|
struct curl_slist *easysrc_toohard = NULL; /* Unconvertible setopt */
|
|
|
|
struct curl_slist *easysrc_clean = NULL; /* Clean up allocated data */
|
|
|
|
int easysrc_form_count = 0;
|
|
|
|
int easysrc_slist_count = 0;
|
2011-09-20 19:54:14 -04:00
|
|
|
|
|
|
|
static const char *const srchead[]={
|
|
|
|
"/********* Sample code generated by the curl command line tool **********",
|
|
|
|
" * All curl_easy_setopt() options are documented at:",
|
|
|
|
" * http://curl.haxx.se/libcurl/c/curl_easy_setopt.html",
|
|
|
|
" ************************************************************************/",
|
|
|
|
"#include <curl/curl.h>",
|
|
|
|
"",
|
|
|
|
"int main(int argc, char *argv[])",
|
|
|
|
"{",
|
|
|
|
" CURLcode ret;",
|
2012-02-23 04:43:37 -05:00
|
|
|
" CURL *hnd;",
|
2011-09-20 19:54:14 -04:00
|
|
|
NULL
|
|
|
|
};
|
2012-02-23 04:43:37 -05:00
|
|
|
/* easysrc_decl declarations come here */
|
|
|
|
/* easysrc_data initialisations come here */
|
|
|
|
/* easysrc_code statements come here */
|
|
|
|
static const char *const srchard[]={
|
|
|
|
"/* Here is a list of options the curl code used that cannot get generated",
|
|
|
|
" as source easily. You may select to either not use them or implement",
|
|
|
|
" them yourself.",
|
|
|
|
"",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
static const char *const srcend[]={
|
|
|
|
"",
|
|
|
|
" return (int)ret;",
|
|
|
|
"}",
|
|
|
|
"/**** End of sample code ****/",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Clean up all source code if we run out of memory */
|
|
|
|
static void easysrc_free(void)
|
|
|
|
{
|
|
|
|
curl_slist_free_all(easysrc_decl);
|
|
|
|
easysrc_decl = NULL;
|
|
|
|
curl_slist_free_all(easysrc_data);
|
|
|
|
easysrc_data = NULL;
|
|
|
|
curl_slist_free_all(easysrc_code);
|
|
|
|
easysrc_code = NULL;
|
|
|
|
curl_slist_free_all(easysrc_toohard);
|
|
|
|
easysrc_toohard = NULL;
|
|
|
|
curl_slist_free_all(easysrc_clean);
|
|
|
|
easysrc_clean = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a source line to the main code or remarks */
|
|
|
|
CURLcode easysrc_add(struct curl_slist **plist, const char *line)
|
|
|
|
{
|
|
|
|
CURLcode ret = CURLE_OK;
|
|
|
|
struct curl_slist *list =
|
|
|
|
curl_slist_append(*plist, line);
|
|
|
|
if(!list) {
|
|
|
|
easysrc_free();
|
|
|
|
ret = CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*plist = list;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
CURLcode easysrc_addf(struct curl_slist **plist, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
CURLcode ret;
|
|
|
|
char *bufp;
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
bufp = curlx_mvaprintf(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if(! bufp) {
|
|
|
|
ret = CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret = easysrc_add(plist, bufp);
|
|
|
|
curl_free(bufp);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-03-16 15:06:18 -04:00
|
|
|
#define CHKRET(v) do {CURLcode ret = (v); if(ret) return ret;} WHILE_FALSE
|
2012-02-23 04:43:37 -05:00
|
|
|
|
|
|
|
CURLcode easysrc_init(void)
|
|
|
|
{
|
|
|
|
CHKRET(easysrc_add(&easysrc_code,
|
|
|
|
"hnd = curl_easy_init();"));
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
CURLcode easysrc_perform(void)
|
|
|
|
{
|
|
|
|
/* Note any setopt calls which we could not convert */
|
|
|
|
if(easysrc_toohard) {
|
|
|
|
int i;
|
|
|
|
struct curl_slist *ptr;
|
|
|
|
const char *c;
|
|
|
|
CHKRET(easysrc_add(&easysrc_code, ""));
|
|
|
|
/* Preamble comment */
|
|
|
|
for(i=0; ((c = srchard[i]) != '\0'); i++)
|
|
|
|
CHKRET(easysrc_add(&easysrc_code, c));
|
|
|
|
/* Each unconverted option */
|
|
|
|
for(ptr=easysrc_toohard; ptr; ptr = ptr->next)
|
|
|
|
CHKRET(easysrc_add(&easysrc_code, ptr->data));
|
|
|
|
CHKRET(easysrc_add(&easysrc_code, ""));
|
|
|
|
CHKRET(easysrc_add(&easysrc_code, "*/"));
|
|
|
|
|
|
|
|
curl_slist_free_all(easysrc_toohard);
|
|
|
|
easysrc_toohard = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHKRET(easysrc_add(&easysrc_code, ""));
|
|
|
|
CHKRET(easysrc_add(&easysrc_code, "ret = curl_easy_perform(hnd);"));
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
CURLcode easysrc_cleanup(void)
|
|
|
|
{
|
|
|
|
CHKRET(easysrc_add(&easysrc_code, ""));
|
|
|
|
CHKRET(easysrc_add(&easysrc_code, "curl_easy_cleanup(hnd);"));
|
|
|
|
CHKRET(easysrc_add(&easysrc_code, "hnd = NULL;"));
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2011-09-20 19:54:14 -04:00
|
|
|
|
|
|
|
void dumpeasysrc(struct Configurable *config)
|
|
|
|
{
|
|
|
|
struct curl_slist *ptr;
|
|
|
|
char *o = config->libcurl;
|
|
|
|
|
|
|
|
if(o) {
|
|
|
|
FILE *out;
|
|
|
|
bool fopened = FALSE;
|
|
|
|
if(strcmp(o, "-")) {
|
2012-03-18 00:54:10 -04:00
|
|
|
out = fopen(o, "w");
|
2011-09-20 19:54:14 -04:00
|
|
|
fopened = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
out = stdout;
|
|
|
|
if(!out)
|
|
|
|
warnf(config, "Failed to open %s to write libcurl code!\n", o);
|
|
|
|
else {
|
|
|
|
int i;
|
|
|
|
const char *c;
|
|
|
|
|
|
|
|
for(i=0; ((c = srchead[i]) != '\0'); i++)
|
|
|
|
fprintf(out, "%s\n", c);
|
|
|
|
|
2012-02-23 04:43:37 -05:00
|
|
|
/* Declare variables used for complex setopt values */
|
|
|
|
for(ptr=easysrc_decl; ptr; ptr = ptr->next)
|
2011-09-20 19:54:14 -04:00
|
|
|
fprintf(out, " %s\n", ptr->data);
|
2012-02-23 04:43:37 -05:00
|
|
|
|
|
|
|
/* Set up complex values for setopt calls */
|
|
|
|
if(easysrc_data) {
|
|
|
|
fprintf(out, "\n");
|
|
|
|
|
|
|
|
for(ptr=easysrc_data; ptr; ptr = ptr->next)
|
|
|
|
fprintf(out, " %s\n", ptr->data);
|
2011-09-20 19:54:14 -04:00
|
|
|
}
|
|
|
|
|
2012-02-23 04:43:37 -05:00
|
|
|
fprintf(out, "\n");
|
|
|
|
for(ptr=easysrc_code; ptr; ptr = ptr->next) {
|
|
|
|
if(ptr->data[0]) {
|
2011-09-20 19:54:14 -04:00
|
|
|
fprintf(out, " %s\n", ptr->data);
|
|
|
|
}
|
2012-02-23 04:43:37 -05:00
|
|
|
else {
|
|
|
|
fprintf(out, "\n");
|
|
|
|
}
|
2011-09-20 19:54:14 -04:00
|
|
|
}
|
|
|
|
|
2012-02-23 04:43:37 -05:00
|
|
|
for(ptr=easysrc_clean; ptr; ptr = ptr->next)
|
|
|
|
fprintf(out, " %s\n", ptr->data);
|
|
|
|
|
|
|
|
for(i=0; ((c = srcend[i]) != '\0'); i++)
|
|
|
|
fprintf(out, "%s\n", c);
|
|
|
|
|
2011-09-20 19:54:14 -04:00
|
|
|
if(fopened)
|
|
|
|
fclose(out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-23 04:43:37 -05:00
|
|
|
easysrc_free();
|
2011-09-20 19:54:14 -04:00
|
|
|
}
|
|
|
|
|
2012-02-05 12:44:22 -05:00
|
|
|
#endif /* CURL_DISABLE_LIBCURL_OPTION */
|