Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 263 lines (230 sloc) 7.335 kb
49b79b76 »
2011-10-05 curl tool: code moved to tool_*.[ch] files
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
83dbd069 »
2014-02-01 tool_main: Moved config struct initialisation into a separate function
8 * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
49b79b76 »
2011-10-05 curl tool: code moved to tool_*.[ch] files
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 ***************************************************************************/
919c97fa »
2012-04-06 curl tool: use configuration files from lib directory
22 #include "tool_setup.h"
49b79b76 »
2011-10-05 curl tool: code moved to tool_*.[ch] files
23
24 #include <sys/stat.h>
25
e2bcd2ab »
2013-01-08 curl: ignore SIGPIPE
26 #ifdef HAVE_SIGNAL_H
27 #include <signal.h>
28 #endif
29
49b79b76 »
2011-10-05 curl tool: code moved to tool_*.[ch] files
30 #define ENABLE_CURLX_PRINTF
31 /* use our own printf() functions */
32 #include "curlx.h"
33
34 #include "tool_cfgable.h"
35 #include "tool_convert.h"
cf80b85b »
2014-02-02 tool_main: Changed stack based config struct to be heap based
36 #include "tool_msgs.h"
49b79b76 »
2011-10-05 curl tool: code moved to tool_*.[ch] files
37 #include "tool_operate.h"
38 #include "tool_panykey.h"
39 #include "tool_vms.h"
40 #include "tool_main.h"
6127e54f »
2014-02-02 tool_operate: Moved libcurl information gathering to tool_main
41 #include "tool_libinfo.h"
49b79b76 »
2011-10-05 curl tool: code moved to tool_*.[ch] files
42
43 /*
44 * This is low-level hard-hacking memory leak tracking and similar. Using
45 * the library level code from this client-side is ugly, but we do this
46 * anyway for convenience.
47 */
4a5aa668 »
2013-01-04 Revert changes relative to lib/*.[ch] recent renaming
48 #include "memdebug.h" /* keep this as LAST include */
49b79b76 »
2011-10-05 curl tool: code moved to tool_*.[ch] files
49
453e821a »
2013-02-06 vms_show: post VMS patch cleanup - II
50 #ifdef __VMS
51 /*
52 * vms_show is a global variable, used in main() as parameter for
53 * function vms_special_exit() to allow proper curl tool exiting.
54 * Its value may be set in other tool_*.c source files thanks to
55 * forward declaration present in tool_vms.h
56 */
911b2d3f »
2013-03-13 tool_main.c: remove redundant vms_show storage-class specifier
57 int vms_show = 0;
453e821a »
2013-02-06 vms_show: post VMS patch cleanup - II
58 #endif
59
683f2b83 »
2013-05-03 src/Makefile.am: build static lib for unit tests if enabled
60 /* if we build a static library for unit tests, there is no main() function */
61 #ifndef UNITTESTS
62
49b79b76 »
2011-10-05 curl tool: code moved to tool_*.[ch] files
63 /*
64 * Ensure that file descriptors 0, 1 and 2 (stdin, stdout, stderr) are
65 * open before starting to run. Otherwise, the first three network
66 * sockets opened by curl could be used for input sources, downloaded data
67 * or error logs as they will effectively be stdin, stdout and/or stderr.
68 */
69 static void main_checkfds(void)
70 {
71 #ifdef HAVE_PIPE
72 int fd[2] = { STDIN_FILENO, STDIN_FILENO };
73 while(fd[0] == STDIN_FILENO ||
74 fd[0] == STDOUT_FILENO ||
75 fd[0] == STDERR_FILENO ||
76 fd[1] == STDIN_FILENO ||
77 fd[1] == STDOUT_FILENO ||
78 fd[1] == STDERR_FILENO)
79 if(pipe(fd) < 0)
80 return; /* Out of handles. This isn't really a big problem now, but
81 will be when we try to create a socket later. */
82 close(fd[0]);
83 close(fd[1]);
84 #endif
85 }
86
0104678c »
2014-02-02 tool_operate: Moved memory tracking initialisation into tool_main
87 #ifdef CURLDEBUG
c1daf6c0 »
2014-02-03 tool_main: Fixed compilation warning from commit 0104678
88 static void memory_tracking_init(void)
0104678c »
2014-02-02 tool_operate: Moved memory tracking initialisation into tool_main
89 {
90 char *env;
91 /* if CURL_MEMDEBUG is set, this starts memory tracking message logging */
92 env = curlx_getenv("CURL_MEMDEBUG");
93 if(env) {
94 /* use the value as file name */
95 char fname[CURL_MT_LOGFNAME_BUFSIZE];
96 if(strlen(env) >= CURL_MT_LOGFNAME_BUFSIZE)
97 env[CURL_MT_LOGFNAME_BUFSIZE-1] = '\0';
98 strcpy(fname, env);
99 curl_free(env);
100 curl_memdebug(fname);
101 /* this weird stuff here is to make curl_free() get called
102 before curl_memdebug() as otherwise memory tracking will
103 log a free() without an alloc! */
104 }
105 /* if CURL_MEMLIMIT is set, this enables fail-on-alloc-number-N feature */
106 env = curlx_getenv("CURL_MEMLIMIT");
107 if(env) {
108 char *endptr;
109 long num = strtol(env, &endptr, 10);
110 if((endptr != env) && (endptr == env + strlen(env)) && (num > 0))
111 curl_memlimit(num);
112 curl_free(env);
113 }
114 }
115 #else
116 # define memory_tracking_init() Curl_nop_stmt
117 #endif
118
49b79b76 »
2011-10-05 curl tool: code moved to tool_*.[ch] files
119 /*
b811200f »
2014-02-02 tool_operate: Moved main initialisation and cleanup code into tool_main
120 * This is the main global constructor for the app. Call this before
121 * _any_ libcurl usage. If this fails, *NO* libcurl functions may be
122 * used, or havoc may be the result.
123 */
d6b9f054 »
2014-02-23 tool_main: Moved initial OperateConfig creation into main_init()
124 static CURLcode main_init(struct GlobalConfig *config)
b811200f »
2014-02-02 tool_operate: Moved main initialisation and cleanup code into tool_main
125 {
6127e54f »
2014-02-02 tool_operate: Moved libcurl information gathering to tool_main
126 CURLcode result = CURLE_OK;
127
b811200f »
2014-02-02 tool_operate: Moved main initialisation and cleanup code into tool_main
128 #if defined(__DJGPP__) || defined(__GO32__)
129 /* stop stat() wasting time */
130 _djstat_flags |= _STAT_INODE | _STAT_EXEC_MAGIC | _STAT_DIRSIZE;
131 #endif
132
249dc835 »
2014-02-23 tool: Moved --showerror to the global config
133 /* Initialise the global config */
134 config->showerror = -1; /* Will show errors */
5513bbd5 »
2014-03-01 tool: Moved --stderr to the global config
135 config->errors = stderr; /* Default errors to stderr */
249dc835 »
2014-02-23 tool: Moved --showerror to the global config
136
d6b9f054 »
2014-02-23 tool_main: Moved initial OperateConfig creation into main_init()
137 /* Allocate the initial operate config */
59b5ef44 »
2014-02-23 tool_main: Moved easy handle into global config structure
138 config->first = config->last = malloc(sizeof(struct OperationConfig));
d6b9f054 »
2014-02-23 tool_main: Moved initial OperateConfig creation into main_init()
139 if(config->first) {
140 /* Perform the libcurl initialization */
141 result = curl_global_init(CURL_GLOBAL_DEFAULT);
142 if(!result) {
143 /* Get information about libcurl */
144 result = get_libcurl_info();
145
146 if(!result) {
59b5ef44 »
2014-02-23 tool_main: Moved easy handle into global config structure
147 /* Get a curl handle to use for all forthcoming curl transfers */
148 config->easy = curl_easy_init();
149 if(config->easy) {
150 /* Initialise the config */
151 config_init(config->first);
152 config->first->easy = config->easy;
fd97c17b »
2014-02-27 tool_main: Forgot to initialise the first operation's global pointer
153 config->first->global = config;
59b5ef44 »
2014-02-23 tool_main: Moved easy handle into global config structure
154 }
155 else {
156 helpf(stderr, "error initializing curl easy handle\n");
157 result = CURLE_FAILED_INIT;
bd248a0b »
2014-02-28 tool_main: Fixed a memory leak on main_init error
158 free(config->first);
59b5ef44 »
2014-02-23 tool_main: Moved easy handle into global config structure
159 }
d6b9f054 »
2014-02-23 tool_main: Moved initial OperateConfig creation into main_init()
160 }
bd248a0b »
2014-02-28 tool_main: Fixed a memory leak on main_init error
161 else {
d6b9f054 »
2014-02-23 tool_main: Moved initial OperateConfig creation into main_init()
162 helpf(stderr, "error retrieving curl library information\n");
bd248a0b »
2014-02-28 tool_main: Fixed a memory leak on main_init error
163 free(config->first);
164 }
d6b9f054 »
2014-02-23 tool_main: Moved initial OperateConfig creation into main_init()
165 }
bd248a0b »
2014-02-28 tool_main: Fixed a memory leak on main_init error
166 else {
d6b9f054 »
2014-02-23 tool_main: Moved initial OperateConfig creation into main_init()
167 helpf(stderr, "error initializing curl library\n");
bd248a0b »
2014-02-28 tool_main: Fixed a memory leak on main_init error
168 free(config->first);
169 }
d6b9f054 »
2014-02-23 tool_main: Moved initial OperateConfig creation into main_init()
170 }
171 else {
172 helpf(stderr, "error initializing curl\n");
173 result = CURLE_FAILED_INIT;
6127e54f »
2014-02-02 tool_operate: Moved libcurl information gathering to tool_main
174 }
175
176 return result;
b811200f »
2014-02-02 tool_operate: Moved main initialisation and cleanup code into tool_main
177 }
178
4efa1d29 »
2014-02-27 tool: Moved --trace and --verbose to the global config
179 static void free_config_fields(struct GlobalConfig *config)
180 {
181 Curl_safefree(config->trace_dump);
182
5513bbd5 »
2014-03-01 tool: Moved --stderr to the global config
183 if(config->errors_fopened && config->errors)
184 fclose(config->errors);
185 config->errors = NULL;
186
4efa1d29 »
2014-02-27 tool: Moved --trace and --verbose to the global config
187 if(config->trace_fopened && config->trace_stream)
188 fclose(config->trace_stream);
189 config->trace_stream = NULL;
f1a9e685 »
2014-03-01 tool: Moved --libcurl to the global config
190
191 Curl_safefree(config->libcurl);
4efa1d29 »
2014-02-27 tool: Moved --trace and --verbose to the global config
192 }
193
b811200f »
2014-02-02 tool_operate: Moved main initialisation and cleanup code into tool_main
194 /*
195 * This is the main global destructor for the app. Call this after
196 * _all_ libcurl usage is done.
197 */
665096e2 »
2014-02-23 tool_main: Moved OperateConfig cleanup into main_free()
198 static void main_free(struct GlobalConfig *config)
b811200f »
2014-02-02 tool_operate: Moved main initialisation and cleanup code into tool_main
199 {
c27cc688 »
2014-02-23 tool_operate: Moved easy handle cleanup into tool_main
200 /* Cleanup the easy handle */
201 curl_easy_cleanup(config->easy);
202 config->easy = NULL;
203
665096e2 »
2014-02-23 tool_main: Moved OperateConfig cleanup into main_free()
204 /* Main cleanup */
b811200f »
2014-02-02 tool_operate: Moved main initialisation and cleanup code into tool_main
205 curl_global_cleanup();
206 convert_cleanup();
207 metalink_cleanup();
4efa1d29 »
2014-02-27 tool: Moved --trace and --verbose to the global config
208 free_config_fields(config);
665096e2 »
2014-02-23 tool_main: Moved OperateConfig cleanup into main_free()
209
210 /* Free the config structures */
c27cc688 »
2014-02-23 tool_operate: Moved easy handle cleanup into tool_main
211 config_free(config->last);
665096e2 »
2014-02-23 tool_main: Moved OperateConfig cleanup into main_free()
212 config->first = NULL;
213 config->last = NULL;
b811200f »
2014-02-02 tool_operate: Moved main initialisation and cleanup code into tool_main
214 }
215
216 /*
49b79b76 »
2011-10-05 curl tool: code moved to tool_*.[ch] files
217 ** curl tool main function.
218 */
219 int main(int argc, char *argv[])
220 {
b914e7ed »
2014-02-22 tool_operate: Start to use CURLcode rather than int for return codes
221 CURLcode result = CURLE_OK;
2249f7fe »
2014-02-23 tool_cfgable: Added global config structure
222 struct GlobalConfig global;
223 memset(&global, 0, sizeof(global));
224
49b79b76 »
2011-10-05 curl tool: code moved to tool_*.[ch] files
225 main_checkfds();
226
a7db42e4 »
2013-01-09 curl: ignore SIGPIPE - compilation fix - follow-up
227 #if defined(HAVE_SIGNAL) && defined(SIGPIPE)
dd73c924 »
2013-01-09 curl: ignore SIGPIPE - compilation fix
228 (void)signal(SIGPIPE, SIG_IGN);
e2bcd2ab »
2013-01-08 curl: ignore SIGPIPE
229 #endif
230
0104678c »
2014-02-02 tool_operate: Moved memory tracking initialisation into tool_main
231 /* Initialize memory tracking */
232 memory_tracking_init();
233
d6b9f054 »
2014-02-23 tool_main: Moved initial OperateConfig creation into main_init()
234 /* Initialize the curl library - do not call any libcurl functions before
235 this point */
236 result = main_init(&global);
237 if(!result) {
238 /* Start our curl operation */
59b5ef44 »
2014-02-23 tool_main: Moved easy handle into global config structure
239 result = operate(&global, argc, argv);
b811200f »
2014-02-02 tool_operate: Moved main initialisation and cleanup code into tool_main
240
49b79b76 »
2011-10-05 curl tool: code moved to tool_*.[ch] files
241 #ifdef __SYMBIAN32__
249dc835 »
2014-02-23 tool: Moved --showerror to the global config
242 if(global.showerror)
cf80b85b »
2014-02-02 tool_main: Changed stack based config struct to be heap based
243 tool_pressanykey();
49b79b76 »
2011-10-05 curl tool: code moved to tool_*.[ch] files
244 #endif
245
665096e2 »
2014-02-23 tool_main: Moved OperateConfig cleanup into main_free()
246 /* Perform the main cleanup */
247 main_free(&global);
cf80b85b »
2014-02-02 tool_main: Changed stack based config struct to be heap based
248 }
49b79b76 »
2011-10-05 curl tool: code moved to tool_*.[ch] files
249
250 #ifdef __NOVELL_LIBC__
251 if(getenv("_IN_NETWARE_BASH_") == NULL)
252 tool_pressanykey();
253 #endif
254
255 #ifdef __VMS
256 vms_special_exit(res, vms_show);
257 #else
b914e7ed »
2014-02-22 tool_operate: Start to use CURLcode rather than int for return codes
258 return (int)result;
49b79b76 »
2011-10-05 curl tool: code moved to tool_*.[ch] files
259 #endif
260 }
261
683f2b83 »
2013-05-03 src/Makefile.am: build static lib for unit tests if enabled
262 #endif /* ndef UNITTESTS */
Something went wrong with that request. Please try again.