Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/curl/src/tool_easysrc.c
2065 views
1
/***************************************************************************
2
* _ _ ____ _
3
* Project ___| | | | _ \| |
4
* / __| | | | |_) | |
5
* | (__| |_| | _ <| |___
6
* \___|\___/|_| \_\_____|
7
*
8
* Copyright (C) Daniel Stenberg, <[email protected]>, 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 https://curl.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
* SPDX-License-Identifier: curl
22
*
23
***************************************************************************/
24
#include "tool_setup.h"
25
26
#include "slist_wc.h"
27
28
#ifndef CURL_DISABLE_LIBCURL_OPTION
29
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 slist_wc *easysrc_decl; /* Variable declarations */
41
struct slist_wc *easysrc_data; /* Build slists, forms etc. */
42
struct slist_wc *easysrc_code; /* Setopt calls */
43
struct slist_wc *easysrc_toohard; /* Unconvertible setopt */
44
struct slist_wc *easysrc_clean; /* Clean up allocated data */
45
int easysrc_mime_count;
46
int easysrc_slist_count;
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
" * https://curl.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 initialization 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 choose 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
slist_wc_free_all(easysrc_decl);
83
easysrc_decl = NULL;
84
slist_wc_free_all(easysrc_data);
85
easysrc_data = NULL;
86
slist_wc_free_all(easysrc_code);
87
easysrc_code = NULL;
88
slist_wc_free_all(easysrc_toohard);
89
easysrc_toohard = NULL;
90
slist_wc_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 slist_wc **plist, const char *line)
96
{
97
CURLcode ret = CURLE_OK;
98
struct slist_wc *list = slist_wc_append(*plist, line);
99
if(!list) {
100
easysrc_free();
101
ret = CURLE_OUT_OF_MEMORY;
102
}
103
else
104
*plist = list;
105
return ret;
106
}
107
108
CURLcode easysrc_addf(struct slist_wc **plist, const char *fmt, ...)
109
{
110
CURLcode ret;
111
char *bufp;
112
va_list ap;
113
va_start(ap, fmt);
114
bufp = vaprintf(fmt, ap);
115
va_end(ap);
116
if(!bufp) {
117
ret = CURLE_OUT_OF_MEMORY;
118
}
119
else {
120
ret = easysrc_add(plist, bufp);
121
curl_free(bufp);
122
}
123
return ret;
124
}
125
126
CURLcode easysrc_init(void)
127
{
128
return easysrc_add(&easysrc_code, "hnd = curl_easy_init();");
129
}
130
131
CURLcode easysrc_perform(void)
132
{
133
CURLcode ret = CURLE_OK;
134
/* Note any setopt calls which we could not convert */
135
if(easysrc_toohard) {
136
int i;
137
struct curl_slist *ptr;
138
ret = easysrc_add(&easysrc_code, "");
139
/* Preamble comment */
140
for(i = 0; srchard[i] && !ret; i++)
141
ret = easysrc_add(&easysrc_code, srchard[i]);
142
/* Each unconverted option */
143
if(easysrc_toohard && !ret) {
144
for(ptr = easysrc_toohard->first; ptr && !ret; ptr = ptr->next)
145
ret = easysrc_add(&easysrc_code, ptr->data);
146
}
147
if(!ret)
148
ret = easysrc_add(&easysrc_code, "");
149
if(!ret)
150
ret = easysrc_add(&easysrc_code, "*/");
151
152
slist_wc_free_all(easysrc_toohard);
153
easysrc_toohard = NULL;
154
}
155
156
if(!ret)
157
ret = easysrc_add(&easysrc_code, "");
158
if(!ret)
159
ret = easysrc_add(&easysrc_code, "ret = curl_easy_perform(hnd);");
160
if(!ret)
161
ret = easysrc_add(&easysrc_code, "");
162
163
return ret;
164
}
165
166
CURLcode easysrc_cleanup(void)
167
{
168
CURLcode ret = easysrc_add(&easysrc_code, "curl_easy_cleanup(hnd);");
169
if(!ret)
170
ret = easysrc_add(&easysrc_code, "hnd = NULL;");
171
172
return ret;
173
}
174
175
void dumpeasysrc(struct GlobalConfig *config)
176
{
177
struct curl_slist *ptr;
178
char *o = config->libcurl;
179
180
FILE *out;
181
bool fopened = FALSE;
182
if(strcmp(o, "-")) {
183
out = fopen(o, FOPEN_WRITETEXT);
184
fopened = TRUE;
185
}
186
else
187
out = stdout;
188
if(!out)
189
warnf(config, "Failed to open %s to write libcurl code", o);
190
else {
191
int i;
192
const char *c;
193
194
for(i = 0; ((c = srchead[i]) != NULL); i++)
195
fprintf(out, "%s\n", c);
196
197
/* Declare variables used for complex setopt values */
198
if(easysrc_decl) {
199
for(ptr = easysrc_decl->first; ptr; ptr = ptr->next)
200
fprintf(out, " %s\n", ptr->data);
201
}
202
203
/* Set up complex values for setopt calls */
204
if(easysrc_data) {
205
fprintf(out, "\n");
206
207
for(ptr = easysrc_data->first; ptr; ptr = ptr->next)
208
fprintf(out, " %s\n", ptr->data);
209
}
210
211
fprintf(out, "\n");
212
if(easysrc_code) {
213
for(ptr = easysrc_code->first; ptr; ptr = ptr->next) {
214
if(ptr->data[0]) {
215
fprintf(out, " %s\n", ptr->data);
216
}
217
else {
218
fprintf(out, "\n");
219
}
220
}
221
}
222
223
if(easysrc_clean) {
224
for(ptr = easysrc_clean->first; ptr; ptr = ptr->next)
225
fprintf(out, " %s\n", ptr->data);
226
}
227
228
for(i = 0; ((c = srcend[i]) != NULL); i++)
229
fprintf(out, "%s\n", c);
230
231
if(fopened)
232
fclose(out);
233
}
234
235
easysrc_free();
236
}
237
238
#endif /* CURL_DISABLE_LIBCURL_OPTION */
239
240