#include "tool_setup.h"
#ifndef CURL_DISABLE_LIBCURL_OPTION
#define ENABLE_CURLX_PRINTF
#include "curlx.h"
#include "tool_cfgable.h"
#include "tool_easysrc.h"
#include "tool_msgs.h"
#include "memdebug.h"
struct curl_slist *easysrc_decl = NULL;
struct curl_slist *easysrc_data = NULL;
struct curl_slist *easysrc_code = NULL;
struct curl_slist *easysrc_toohard = NULL;
struct curl_slist *easysrc_clean = NULL;
int easysrc_form_count = 0;
int easysrc_slist_count = 0;
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;",
" CURL *hnd;",
NULL
};
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
};
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;
}
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;
}
#define CHKRET(v) do {CURLcode ret = (v); if(ret) return ret;} WHILE_FALSE
CURLcode easysrc_init(void)
{
CHKRET(easysrc_add(&easysrc_code,
"hnd = curl_easy_init();"));
return CURLE_OK;
}
CURLcode easysrc_perform(void)
{
if(easysrc_toohard) {
int i;
struct curl_slist *ptr;
const char *c;
CHKRET(easysrc_add(&easysrc_code, ""));
for(i=0; ((c = srchard[i]) != NULL); i++)
CHKRET(easysrc_add(&easysrc_code, c));
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);"));
CHKRET(easysrc_add(&easysrc_code, ""));
return CURLE_OK;
}
CURLcode easysrc_cleanup(void)
{
CHKRET(easysrc_add(&easysrc_code, "curl_easy_cleanup(hnd);"));
CHKRET(easysrc_add(&easysrc_code, "hnd = NULL;"));
return CURLE_OK;
}
void dumpeasysrc(struct GlobalConfig *config)
{
struct curl_slist *ptr;
char *o = config->libcurl;
if(o) {
FILE *out;
bool fopened = FALSE;
if(strcmp(o, "-")) {
out = fopen(o, "w");
fopened = TRUE;
}
else
out = stdout;
if(!out)
warnf(config->current, "Failed to open %s to write libcurl code!\n", o);
else {
int i;
const char *c;
for(i=0; ((c = srchead[i]) != NULL); i++)
fprintf(out, "%s\n", c);
for(ptr=easysrc_decl; ptr; ptr = ptr->next)
fprintf(out, " %s\n", ptr->data);
if(easysrc_data) {
fprintf(out, "\n");
for(ptr=easysrc_data; ptr; ptr = ptr->next)
fprintf(out, " %s\n", ptr->data);
}
fprintf(out, "\n");
for(ptr=easysrc_code; ptr; ptr = ptr->next) {
if(ptr->data[0]) {
fprintf(out, " %s\n", ptr->data);
}
else {
fprintf(out, "\n");
}
}
for(ptr=easysrc_clean; ptr; ptr = ptr->next)
fprintf(out, " %s\n", ptr->data);
for(i=0; ((c = srcend[i]) != NULL); i++)
fprintf(out, "%s\n", c);
if(fopened)
fclose(out);
}
}
easysrc_free();
}
#endif