2011-10-04 18:03:20 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2014-02-01 08:26:09 -05:00
|
|
|
* Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-10-04 18:03:20 -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-10-04 18:03:20 -04:00
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2013-01-08 17:10:26 -05:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
|
|
|
|
2011-10-04 18:03:20 -04:00
|
|
|
#define ENABLE_CURLX_PRINTF
|
|
|
|
/* use our own printf() functions */
|
|
|
|
#include "curlx.h"
|
|
|
|
|
|
|
|
#include "tool_cfgable.h"
|
|
|
|
#include "tool_convert.h"
|
2014-02-02 08:58:35 -05:00
|
|
|
#include "tool_msgs.h"
|
2011-10-04 18:03:20 -04:00
|
|
|
#include "tool_operate.h"
|
|
|
|
#include "tool_panykey.h"
|
|
|
|
#include "tool_vms.h"
|
|
|
|
#include "tool_main.h"
|
2014-02-02 10:11:22 -05:00
|
|
|
#include "tool_libinfo.h"
|
2011-10-04 18:03:20 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This is low-level hard-hacking memory leak tracking and similar. Using
|
|
|
|
* the library level code from this client-side is ugly, but we do this
|
|
|
|
* anyway for convenience.
|
|
|
|
*/
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h" /* keep this as LAST include */
|
2011-10-04 18:03:20 -04:00
|
|
|
|
2013-02-05 22:51:55 -05:00
|
|
|
#ifdef __VMS
|
|
|
|
/*
|
|
|
|
* vms_show is a global variable, used in main() as parameter for
|
|
|
|
* function vms_special_exit() to allow proper curl tool exiting.
|
|
|
|
* Its value may be set in other tool_*.c source files thanks to
|
|
|
|
* forward declaration present in tool_vms.h
|
|
|
|
*/
|
2013-03-13 18:35:24 -04:00
|
|
|
int vms_show = 0;
|
2013-02-05 22:51:55 -05:00
|
|
|
#endif
|
|
|
|
|
2013-05-03 17:03:58 -04:00
|
|
|
/* if we build a static library for unit tests, there is no main() function */
|
|
|
|
#ifndef UNITTESTS
|
|
|
|
|
2011-10-04 18:03:20 -04:00
|
|
|
/*
|
|
|
|
* Ensure that file descriptors 0, 1 and 2 (stdin, stdout, stderr) are
|
|
|
|
* open before starting to run. Otherwise, the first three network
|
|
|
|
* sockets opened by curl could be used for input sources, downloaded data
|
|
|
|
* or error logs as they will effectively be stdin, stdout and/or stderr.
|
|
|
|
*/
|
|
|
|
static void main_checkfds(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_PIPE
|
|
|
|
int fd[2] = { STDIN_FILENO, STDIN_FILENO };
|
|
|
|
while(fd[0] == STDIN_FILENO ||
|
|
|
|
fd[0] == STDOUT_FILENO ||
|
|
|
|
fd[0] == STDERR_FILENO ||
|
|
|
|
fd[1] == STDIN_FILENO ||
|
|
|
|
fd[1] == STDOUT_FILENO ||
|
|
|
|
fd[1] == STDERR_FILENO)
|
|
|
|
if(pipe(fd) < 0)
|
|
|
|
return; /* Out of handles. This isn't really a big problem now, but
|
|
|
|
will be when we try to create a socket later. */
|
|
|
|
close(fd[0]);
|
|
|
|
close(fd[1]);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-02-02 08:45:35 -05:00
|
|
|
#ifdef CURLDEBUG
|
2014-02-03 07:16:22 -05:00
|
|
|
static void memory_tracking_init(void)
|
2014-02-02 08:45:35 -05:00
|
|
|
{
|
|
|
|
char *env;
|
|
|
|
/* if CURL_MEMDEBUG is set, this starts memory tracking message logging */
|
|
|
|
env = curlx_getenv("CURL_MEMDEBUG");
|
|
|
|
if(env) {
|
|
|
|
/* use the value as file name */
|
|
|
|
char fname[CURL_MT_LOGFNAME_BUFSIZE];
|
|
|
|
if(strlen(env) >= CURL_MT_LOGFNAME_BUFSIZE)
|
|
|
|
env[CURL_MT_LOGFNAME_BUFSIZE-1] = '\0';
|
|
|
|
strcpy(fname, env);
|
|
|
|
curl_free(env);
|
|
|
|
curl_memdebug(fname);
|
|
|
|
/* this weird stuff here is to make curl_free() get called
|
|
|
|
before curl_memdebug() as otherwise memory tracking will
|
|
|
|
log a free() without an alloc! */
|
|
|
|
}
|
|
|
|
/* if CURL_MEMLIMIT is set, this enables fail-on-alloc-number-N feature */
|
|
|
|
env = curlx_getenv("CURL_MEMLIMIT");
|
|
|
|
if(env) {
|
|
|
|
char *endptr;
|
|
|
|
long num = strtol(env, &endptr, 10);
|
|
|
|
if((endptr != env) && (endptr == env + strlen(env)) && (num > 0))
|
|
|
|
curl_memlimit(num);
|
|
|
|
curl_free(env);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define memory_tracking_init() Curl_nop_stmt
|
|
|
|
#endif
|
|
|
|
|
2014-02-02 09:21:53 -05:00
|
|
|
/*
|
|
|
|
* This is the main global constructor for the app. Call this before
|
|
|
|
* _any_ libcurl usage. If this fails, *NO* libcurl functions may be
|
|
|
|
* used, or havoc may be the result.
|
|
|
|
*/
|
2014-02-02 10:11:22 -05:00
|
|
|
static CURLcode main_init(struct Configurable *config)
|
2014-02-02 09:21:53 -05:00
|
|
|
{
|
2014-02-02 10:11:22 -05:00
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
|
2014-02-02 09:21:53 -05:00
|
|
|
#if defined(__DJGPP__) || defined(__GO32__)
|
|
|
|
/* stop stat() wasting time */
|
|
|
|
_djstat_flags |= _STAT_INODE | _STAT_EXEC_MAGIC | _STAT_DIRSIZE;
|
|
|
|
#endif
|
|
|
|
|
2014-02-02 10:11:22 -05:00
|
|
|
/* Perform the libcurl initialization */
|
|
|
|
result = curl_global_init(CURL_GLOBAL_DEFAULT);
|
|
|
|
if(!result) {
|
|
|
|
/* Get information about libcurl */
|
|
|
|
result = get_libcurl_info();
|
|
|
|
|
|
|
|
if(result)
|
|
|
|
helpf(config->errors, "error retrieving curl library information\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
helpf(config->errors, "error initializing curl library\n");
|
|
|
|
|
|
|
|
return result;
|
2014-02-02 09:21:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the main global destructor for the app. Call this after
|
|
|
|
* _all_ libcurl usage is done.
|
|
|
|
*/
|
|
|
|
static void main_free(void)
|
|
|
|
{
|
|
|
|
curl_global_cleanup();
|
|
|
|
convert_cleanup();
|
|
|
|
metalink_cleanup();
|
|
|
|
}
|
|
|
|
|
2011-10-04 18:03:20 -04:00
|
|
|
/*
|
|
|
|
** curl tool main function.
|
|
|
|
*/
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int res;
|
2014-02-02 08:58:35 -05:00
|
|
|
struct Configurable *config;
|
2011-10-04 18:03:20 -04:00
|
|
|
|
|
|
|
main_checkfds();
|
|
|
|
|
2013-01-09 16:47:41 -05:00
|
|
|
#if defined(HAVE_SIGNAL) && defined(SIGPIPE)
|
2013-01-08 19:30:08 -05:00
|
|
|
(void)signal(SIGPIPE, SIG_IGN);
|
2013-01-08 17:10:26 -05:00
|
|
|
#endif
|
|
|
|
|
2014-02-02 08:45:35 -05:00
|
|
|
/* Initialize memory tracking */
|
|
|
|
memory_tracking_init();
|
|
|
|
|
2014-02-02 08:58:35 -05:00
|
|
|
/* Allocate the initial config */
|
|
|
|
config = malloc(sizeof(struct Configurable));
|
|
|
|
|
|
|
|
if(config) {
|
|
|
|
/* Initialise the config */
|
|
|
|
init_config(config);
|
|
|
|
|
2014-02-02 09:21:53 -05:00
|
|
|
/* Initialize the curl library - do not call any libcurl functions before
|
|
|
|
this point */
|
2014-02-02 10:11:22 -05:00
|
|
|
res = main_init(config);
|
|
|
|
if(!res) {
|
2014-02-02 09:21:53 -05:00
|
|
|
/* Start our curl operation */
|
|
|
|
res = operate(config, argc, argv);
|
|
|
|
|
|
|
|
/* Perform the main cleanup */
|
|
|
|
main_free();
|
|
|
|
}
|
2011-10-04 18:03:20 -04:00
|
|
|
|
|
|
|
#ifdef __SYMBIAN32__
|
2014-02-02 08:58:35 -05:00
|
|
|
if(config->showerror)
|
|
|
|
tool_pressanykey();
|
2011-10-04 18:03:20 -04:00
|
|
|
#endif
|
|
|
|
|
2014-02-02 08:58:35 -05:00
|
|
|
/* Free the config structure */
|
2014-02-07 14:29:46 -05:00
|
|
|
config_free(config);
|
|
|
|
config = NULL;
|
2014-02-02 08:58:35 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
helpf(stderr, "error initializing curl\n");
|
|
|
|
res = CURLE_FAILED_INIT;
|
|
|
|
}
|
2011-10-04 18:03:20 -04:00
|
|
|
|
|
|
|
#ifdef __NOVELL_LIBC__
|
|
|
|
if(getenv("_IN_NETWARE_BASH_") == NULL)
|
|
|
|
tool_pressanykey();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __VMS
|
|
|
|
vms_special_exit(res, vms_show);
|
|
|
|
#else
|
|
|
|
return res;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-05-03 17:03:58 -04:00
|
|
|
#endif /* ndef UNITTESTS */
|