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