2011-10-04 18:03:20 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2020-02-19 01:53:54 -05:00
|
|
|
* Copyright (C) 1998 - 2020, 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
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2011-10-04 18:03:20 -04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
|
2014-07-02 10:34:48 -04:00
|
|
|
#ifdef USE_NSS
|
|
|
|
#include <nspr.h>
|
2014-07-03 14:19:35 -04:00
|
|
|
#include <plarenas.h>
|
2014-07-02 10:34:48 -04:00
|
|
|
#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"
|
2018-11-01 02:50:40 -04:00
|
|
|
#include "tool_doswin.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
|
|
|
|
|
2017-08-21 15:35:46 -04:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
/*
|
|
|
|
* There seems to be no way to escape "*" in command-line arguments with MinGW
|
|
|
|
* when command-line argument globbing is enabled under the MSYS shell, so turn
|
|
|
|
* it off.
|
|
|
|
*/
|
|
|
|
int _CRT_glob = 0;
|
|
|
|
#endif /* __MINGW32__ */
|
|
|
|
|
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);
|
2019-03-08 04:47:29 -05:00
|
|
|
curl_dbg_memdebug(fname);
|
2019-08-08 08:51:01 -04:00
|
|
|
/* this weird stuff here is to make curl_free() get called before
|
|
|
|
curl_gdb_memdebug() as otherwise memory tracking will log a free()
|
|
|
|
without an alloc! */
|
2014-02-02 08:45:35 -05:00
|
|
|
}
|
|
|
|
/* 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))
|
2019-03-08 04:47:29 -05:00
|
|
|
curl_dbg_memlimit(num);
|
2014-02-02 08:45:35 -05:00
|
|
|
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-23 08:34:29 -05:00
|
|
|
static CURLcode main_init(struct GlobalConfig *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-23 11:37:28 -05:00
|
|
|
/* Initialise the global config */
|
|
|
|
config->showerror = -1; /* Will show errors */
|
2014-03-01 08:00:31 -05:00
|
|
|
config->errors = stderr; /* Default errors to stderr */
|
2018-05-17 08:09:17 -04:00
|
|
|
config->styled_output = TRUE; /* enable detection */
|
2019-07-20 13:14:00 -04:00
|
|
|
config->parallel_max = PARALLEL_DEFAULT;
|
2014-02-23 11:37:28 -05:00
|
|
|
|
2014-02-23 08:34:29 -05:00
|
|
|
/* Allocate the initial operate config */
|
2014-02-23 09:32:22 -05:00
|
|
|
config->first = config->last = malloc(sizeof(struct OperationConfig));
|
2014-02-23 08:34:29 -05:00
|
|
|
if(config->first) {
|
|
|
|
/* Perform the libcurl initialization */
|
|
|
|
result = curl_global_init(CURL_GLOBAL_DEFAULT);
|
|
|
|
if(!result) {
|
|
|
|
/* Get information about libcurl */
|
|
|
|
result = get_libcurl_info();
|
|
|
|
|
|
|
|
if(!result) {
|
2019-07-20 13:14:00 -04:00
|
|
|
/* Initialise the config */
|
|
|
|
config_init(config->first);
|
|
|
|
config->first->global = config;
|
2014-02-23 08:34:29 -05:00
|
|
|
}
|
2014-02-28 15:57:04 -05:00
|
|
|
else {
|
2019-12-09 10:15:37 -05:00
|
|
|
errorf(config, "error retrieving curl library information\n");
|
2014-02-28 15:57:04 -05:00
|
|
|
free(config->first);
|
|
|
|
}
|
2014-02-23 08:34:29 -05:00
|
|
|
}
|
2014-02-28 15:57:04 -05:00
|
|
|
else {
|
2019-12-09 10:15:37 -05:00
|
|
|
errorf(config, "error initializing curl library\n");
|
2014-02-28 15:57:04 -05:00
|
|
|
free(config->first);
|
|
|
|
}
|
2014-02-23 08:34:29 -05:00
|
|
|
}
|
|
|
|
else {
|
2019-12-09 10:15:37 -05:00
|
|
|
errorf(config, "error initializing curl\n");
|
2014-02-23 08:34:29 -05:00
|
|
|
result = CURLE_FAILED_INIT;
|
2014-02-02 10:11:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2014-02-02 09:21:53 -05:00
|
|
|
}
|
|
|
|
|
2018-11-30 05:58:09 -05:00
|
|
|
static void free_globalconfig(struct GlobalConfig *config)
|
2014-02-27 15:51:49 -05:00
|
|
|
{
|
|
|
|
Curl_safefree(config->trace_dump);
|
|
|
|
|
2014-03-01 08:00:31 -05:00
|
|
|
if(config->errors_fopened && config->errors)
|
|
|
|
fclose(config->errors);
|
|
|
|
config->errors = NULL;
|
|
|
|
|
2014-02-27 15:51:49 -05:00
|
|
|
if(config->trace_fopened && config->trace_stream)
|
|
|
|
fclose(config->trace_stream);
|
|
|
|
config->trace_stream = NULL;
|
2014-03-01 08:38:00 -05:00
|
|
|
|
|
|
|
Curl_safefree(config->libcurl);
|
2014-02-27 15:51:49 -05:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2014-02-23 08:44:59 -05:00
|
|
|
static void main_free(struct GlobalConfig *config)
|
2014-02-02 09:21:53 -05:00
|
|
|
{
|
2014-02-23 10:10:18 -05:00
|
|
|
/* Cleanup the easy handle */
|
2014-02-23 08:44:59 -05:00
|
|
|
/* Main cleanup */
|
2014-02-02 09:21:53 -05:00
|
|
|
curl_global_cleanup();
|
|
|
|
convert_cleanup();
|
|
|
|
metalink_cleanup();
|
2014-07-02 10:34:48 -04:00
|
|
|
#ifdef USE_NSS
|
2014-07-03 14:19:35 -04:00
|
|
|
if(PR_Initialized()) {
|
|
|
|
/* prevent valgrind from reporting still reachable mem from NSRP arenas */
|
|
|
|
PL_ArenaFinish();
|
2014-07-02 10:34:48 -04:00
|
|
|
/* prevent valgrind from reporting possibly lost memory (fd cache, ...) */
|
|
|
|
PR_Cleanup();
|
2014-07-03 14:19:35 -04:00
|
|
|
}
|
2014-07-02 10:34:48 -04:00
|
|
|
#endif
|
2018-11-30 05:58:09 -05:00
|
|
|
free_globalconfig(config);
|
2014-02-23 08:44:59 -05:00
|
|
|
|
|
|
|
/* Free the config structures */
|
2014-02-23 10:10:18 -05:00
|
|
|
config_free(config->last);
|
2014-02-23 08:44:59 -05:00
|
|
|
config->first = NULL;
|
|
|
|
config->last = NULL;
|
2014-02-02 09:21:53 -05:00
|
|
|
}
|
|
|
|
|
2019-04-05 13:57:29 -04:00
|
|
|
#ifdef WIN32
|
2018-06-28 18:25:00 -04:00
|
|
|
/* TerminalSettings for Windows */
|
2018-10-23 07:38:48 -04:00
|
|
|
static struct TerminalSettings {
|
2018-06-28 18:25:00 -04:00
|
|
|
HANDLE hStdOut;
|
|
|
|
DWORD dwOutputMode;
|
2018-10-23 07:38:48 -04:00
|
|
|
} TerminalSettings;
|
2018-06-28 18:25:00 -04:00
|
|
|
|
|
|
|
static void configure_terminal(void)
|
|
|
|
{
|
|
|
|
/*
|
2018-11-01 12:04:39 -04:00
|
|
|
* If we're running Windows, enable VT output.
|
2018-06-28 18:25:00 -04:00
|
|
|
* Note: VT mode flag can be set on any version of Windows, but VT
|
|
|
|
* processing only performed on Win10 >= Creators Update)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Define the VT flags in case we're building with an older SDK */
|
|
|
|
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
|
|
|
|
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
|
|
|
|
#endif
|
|
|
|
|
|
|
|
memset(&TerminalSettings, 0, sizeof(TerminalSettings));
|
|
|
|
|
|
|
|
/* Enable VT output */
|
|
|
|
TerminalSettings.hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
if((TerminalSettings.hStdOut != INVALID_HANDLE_VALUE)
|
|
|
|
&& (GetConsoleMode(TerminalSettings.hStdOut,
|
|
|
|
&TerminalSettings.dwOutputMode))) {
|
|
|
|
SetConsoleMode(TerminalSettings.hStdOut,
|
|
|
|
TerminalSettings.dwOutputMode
|
|
|
|
| ENABLE_VIRTUAL_TERMINAL_PROCESSING);
|
|
|
|
}
|
|
|
|
}
|
2018-10-23 07:38:48 -04:00
|
|
|
#else
|
|
|
|
#define configure_terminal()
|
|
|
|
#endif
|
2018-06-28 18:25:00 -04:00
|
|
|
|
|
|
|
static void restore_terminal(void)
|
|
|
|
{
|
2019-04-05 13:57:29 -04:00
|
|
|
#ifdef WIN32
|
2018-06-28 18:25:00 -04:00
|
|
|
/* Restore Console output mode and codepage to whatever they were
|
|
|
|
* when Curl started */
|
|
|
|
SetConsoleMode(TerminalSettings.hStdOut, TerminalSettings.dwOutputMode);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-10-04 18:03:20 -04:00
|
|
|
/*
|
|
|
|
** curl tool main function.
|
|
|
|
*/
|
2019-04-12 16:59:40 -04:00
|
|
|
#ifdef _UNICODE
|
|
|
|
int wmain(int argc, wchar_t *argv[])
|
|
|
|
#else
|
2011-10-04 18:03:20 -04:00
|
|
|
int main(int argc, char *argv[])
|
2019-04-12 16:59:40 -04:00
|
|
|
#endif
|
2011-10-04 18:03:20 -04:00
|
|
|
{
|
2014-02-22 13:57:02 -05:00
|
|
|
CURLcode result = CURLE_OK;
|
2014-02-23 08:15:31 -05:00
|
|
|
struct GlobalConfig global;
|
|
|
|
memset(&global, 0, sizeof(global));
|
|
|
|
|
2020-01-24 03:34:52 -05:00
|
|
|
#ifdef WIN32
|
2019-04-12 16:59:40 -04:00
|
|
|
#ifdef _tcscmp
|
2020-01-24 03:34:52 -05:00
|
|
|
/* Undocumented diagnostic option to list the full paths of all loaded
|
|
|
|
modules. This is purposely pre-init. */
|
2019-04-12 16:59:40 -04:00
|
|
|
if(argc == 2 && !_tcscmp(argv[1], _T("--dump-module-paths"))) {
|
2020-01-24 03:34:52 -05:00
|
|
|
struct curl_slist *item, *head = GetLoadedModulePaths();
|
|
|
|
for(item = head; item; item = item->next)
|
|
|
|
printf("%s\n", item->data);
|
|
|
|
curl_slist_free_all(head);
|
|
|
|
return head ? 0 : 1;
|
|
|
|
}
|
2019-04-12 16:59:40 -04:00
|
|
|
#endif /* _tcscmp */
|
2020-01-24 03:34:52 -05:00
|
|
|
/* win32_init must be called before other init routines. */
|
|
|
|
result = win32_init();
|
|
|
|
if(result) {
|
|
|
|
fprintf(stderr, "curl: (%d) Windows-specific init failed.\n", result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-06-28 18:25:00 -04:00
|
|
|
/* Perform any platform-specific terminal configuration */
|
|
|
|
configure_terminal();
|
|
|
|
|
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-23 08:34:29 -05:00
|
|
|
/* Initialize the curl library - do not call any libcurl functions before
|
|
|
|
this point */
|
|
|
|
result = main_init(&global);
|
|
|
|
if(!result) {
|
|
|
|
/* Start our curl operation */
|
2014-02-23 09:32:22 -05:00
|
|
|
result = operate(&global, argc, argv);
|
2014-02-02 09:21:53 -05:00
|
|
|
|
2011-10-04 18:03:20 -04:00
|
|
|
#ifdef __SYMBIAN32__
|
2014-02-23 11:37:28 -05:00
|
|
|
if(global.showerror)
|
2014-02-02 08:58:35 -05:00
|
|
|
tool_pressanykey();
|
2011-10-04 18:03:20 -04:00
|
|
|
#endif
|
|
|
|
|
2014-02-23 08:44:59 -05:00
|
|
|
/* Perform the main cleanup */
|
|
|
|
main_free(&global);
|
2014-02-02 08:58:35 -05:00
|
|
|
}
|
2011-10-04 18:03:20 -04:00
|
|
|
|
2018-06-28 18:25:00 -04:00
|
|
|
/* Return the terminal to its original state */
|
|
|
|
restore_terminal();
|
|
|
|
|
2011-10-04 18:03:20 -04:00
|
|
|
#ifdef __NOVELL_LIBC__
|
|
|
|
if(getenv("_IN_NETWARE_BASH_") == NULL)
|
|
|
|
tool_pressanykey();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __VMS
|
2014-12-21 10:24:55 -05:00
|
|
|
vms_special_exit(result, vms_show);
|
2011-10-04 18:03:20 -04:00
|
|
|
#else
|
2014-02-22 13:57:02 -05:00
|
|
|
return (int)result;
|
2011-10-04 18:03:20 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-05-03 17:03:58 -04:00
|
|
|
#endif /* ndef UNITTESTS */
|