📄 main.c
字号:
/* Command line parsing. Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Wget.GNU Wget is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.GNU Wget is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with Wget; if not, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.In addition, as a special exception, the Free Software Foundationgives permission to link the code of its release of Wget with theOpenSSL project's "OpenSSL" library (or with modified versions of itthat use the same license as the "OpenSSL" library), and distributethe linked executables. You must obey the GNU General Public Licensein all respects for all of the code used other than "OpenSSL". If youmodify this file, you may extend this exception to your version of thefile, but you are not obligated to do so. If you do not wish to doso, delete this exception statement from your version. */#include <config.h>#include <stdio.h>#include <stdlib.h>#ifdef HAVE_UNISTD_H# include <unistd.h>#endif /* HAVE_UNISTD_H */#include <sys/types.h>#ifdef HAVE_STRING_H# include <string.h>#else# include <strings.h>#endif /* HAVE_STRING_H */#ifdef HAVE_SIGNAL_H# include <signal.h>#endif#ifdef HAVE_NLS#ifdef HAVE_LOCALE_H# include <locale.h>#endif /* HAVE_LOCALE_H */#endif /* HAVE_NLS */#include <assert.h>#include <errno.h>#ifndef errnoextern int errno;#endif#include "wget.h"#include "utils.h"#include "init.h"#include "retr.h"#include "recur.h"#include "host.h"#include "url.h"#include "progress.h" /* for progress_handle_sigwinch */#include "convert.h"/* On GNU system this will include system-wide getopt.h. */#include "getopt.h"#ifndef PATH_SEPARATOR# define PATH_SEPARATOR '/'#endifstruct options opt;extern SUM_SIZE_INT total_downloaded_bytes;extern char *version_string;extern struct cookie_jar *wget_cookie_jar;static RETSIGTYPE redirect_output_signal PARAMS ((int));const char *exec_name;/* Initialize I18N/L10N. That amounts to invoking setlocale, and setting up gettext's message catalog using bindtextdomain and textdomain. Does nothing if NLS is disabled or missing. */static voidi18n_initialize (void){ /* HAVE_NLS implies existence of functions invoked here. */#ifdef HAVE_NLS /* Set the current locale. */ /* Where possible, sets only LC_MESSAGES and LC_CTYPE. Other categories, such as numeric, time, or collation, break code that parses data received from the network and relies on C-locale behavior of libc functions. For example, Solaris strptime fails to recognize English month names in non-English locales, which breaks http_atotm. Some implementations of fnmatch perform unwanted case folding in non-C locales. ctype macros, while they were used, provided another example against LC_ALL. */#if defined(LC_MESSAGES) && defined(LC_CTYPE) setlocale (LC_MESSAGES, ""); setlocale (LC_CTYPE, ""); /* safe because we use safe-ctype */#else setlocale (LC_ALL, "");#endif /* Set the text message domain. */ bindtextdomain ("wget", LOCALEDIR); textdomain ("wget");#endif /* HAVE_NLS */}/* Definition of command-line options. */static void print_help PARAMS ((void));static void print_version PARAMS ((void));#ifdef HAVE_SSL# define IF_SSL(x) x#else# define IF_SSL(x) NULL#endif#ifdef ENABLE_DEBUG# define IF_DEBUG(x) x#else# define IF_DEBUG(x) NULL#endifstruct cmdline_option { const char *long_name; char short_name; enum { OPT_VALUE, OPT_BOOLEAN, OPT_FUNCALL, /* Non-standard options that have to be handled specially in main(). */ OPT__APPEND_OUTPUT, OPT__CLOBBER, OPT__DONT_REMOVE_LISTING, OPT__EXECUTE, OPT__NO, OPT__PARENT } type; const void *data; /* for standard options */ int argtype; /* for non-standard options */};struct cmdline_option option_data[] = { { "accept", 'A', OPT_VALUE, "accept", -1 }, { "append-output", 'a', OPT__APPEND_OUTPUT, NULL, required_argument }, { "background", 'b', OPT_BOOLEAN, "background", -1 }, { "backup-converted", 'K', OPT_BOOLEAN, "backupconverted", -1 }, { "backups", 0, OPT_BOOLEAN, "backups", -1 }, { "base", 'B', OPT_VALUE, "base", -1 }, { "bind-address", 0, OPT_VALUE, "bindaddress", -1 }, { IF_SSL ("ca-certificate"), 0, OPT_VALUE, "cacertificate", -1 }, { IF_SSL ("ca-directory"), 0, OPT_VALUE, "cadirectory", -1 }, { "cache", 0, OPT_BOOLEAN, "cache", -1 }, { IF_SSL ("certificate"), 0, OPT_VALUE, "certificate", -1 }, { IF_SSL ("certificate-type"), 0, OPT_VALUE, "certificatetype", -1 }, { IF_SSL ("check-certificate"), 0, OPT_BOOLEAN, "checkcertificate", -1 }, { "clobber", 0, OPT__CLOBBER, NULL, optional_argument }, { "connect-timeout", 0, OPT_VALUE, "connecttimeout", -1 }, { "continue", 'c', OPT_BOOLEAN, "continue", -1 }, { "convert-links", 'k', OPT_BOOLEAN, "convertlinks", -1 }, { "cookies", 0, OPT_BOOLEAN, "cookies", -1 }, { "cut-dirs", 0, OPT_VALUE, "cutdirs", -1 }, { IF_DEBUG ("debug"), 'd', OPT_BOOLEAN, "debug", -1 }, { "delete-after", 0, OPT_BOOLEAN, "deleteafter", -1 }, { "directories", 0, OPT_BOOLEAN, "dirstruct", -1 }, { "directory-prefix", 'P', OPT_VALUE, "dirprefix", -1 }, { "dns-cache", 0, OPT_BOOLEAN, "dnscache", -1 }, { "dns-timeout", 0, OPT_VALUE, "dnstimeout", -1 }, { "domains", 'D', OPT_VALUE, "domains", -1 }, { "dont-remove-listing", 0, OPT__DONT_REMOVE_LISTING, NULL, no_argument }, { "dot-style", 0, OPT_VALUE, "dotstyle", -1 }, { "egd-file", 0, OPT_VALUE, "egdfile", -1 }, { "exclude-directories", 'X', OPT_VALUE, "excludedirectories", -1 }, { "exclude-domains", 0, OPT_VALUE, "excludedomains", -1 }, { "execute", 'e', OPT__EXECUTE, NULL, required_argument }, { "follow-ftp", 0, OPT_BOOLEAN, "followftp", -1 }, { "follow-tags", 0, OPT_VALUE, "followtags", -1 }, { "force-directories", 'x', OPT_BOOLEAN, "dirstruct", -1 }, { "force-html", 'F', OPT_BOOLEAN, "forcehtml", -1 }, { "ftp-password", 0, OPT_VALUE, "ftppassword", -1 }, { "ftp-user", 0, OPT_VALUE, "ftpuser", -1 }, { "glob", 0, OPT_BOOLEAN, "glob", -1 }, { "header", 0, OPT_VALUE, "header", -1 }, { "help", 'h', OPT_FUNCALL, (void *)print_help, no_argument }, { "host-directories", 0, OPT_BOOLEAN, "addhostdir", -1 }, { "html-extension", 'E', OPT_BOOLEAN, "htmlextension", -1 }, { "htmlify", 0, OPT_BOOLEAN, "htmlify", -1 }, { "http-keep-alive", 0, OPT_BOOLEAN, "httpkeepalive", -1 }, { "http-passwd", 0, OPT_VALUE, "httppassword", -1 }, /* deprecated */ { "http-password", 0, OPT_VALUE, "httppassword", -1 }, { "http-user", 0, OPT_VALUE, "httpuser", -1 }, { "ignore-length", 0, OPT_BOOLEAN, "ignorelength", -1 }, { "ignore-tags", 0, OPT_VALUE, "ignoretags", -1 }, { "include-directories", 'I', OPT_VALUE, "includedirectories", -1 },#ifdef ENABLE_IPV6 { "inet4-only", '4', OPT_BOOLEAN, "inet4only", -1 }, { "inet6-only", '6', OPT_BOOLEAN, "inet6only", -1 },#endif { "input-file", 'i', OPT_VALUE, "input", -1 }, { "keep-session-cookies", 0, OPT_BOOLEAN, "keepsessioncookies", -1 }, { "level", 'l', OPT_VALUE, "reclevel", -1 }, { "limit-rate", 0, OPT_VALUE, "limitrate", -1 }, { "load-cookies", 0, OPT_VALUE, "loadcookies", -1 }, { "mirror", 'm', OPT_BOOLEAN, "mirror", -1 }, { "no", 'n', OPT__NO, NULL, required_argument }, { "no-clobber", 0, OPT_BOOLEAN, "noclobber", -1 }, { "no-parent", 0, OPT_BOOLEAN, "noparent", -1 }, { "output-document", 'O', OPT_VALUE, "outputdocument", -1 }, { "output-file", 'o', OPT_VALUE, "logfile", -1 }, { "page-requisites", 'p', OPT_BOOLEAN, "pagerequisites", -1 }, { "parent", 0, OPT__PARENT, NULL, optional_argument }, { "passive-ftp", 0, OPT_BOOLEAN, "passiveftp", -1 }, { "password", 0, OPT_VALUE, "password", -1 }, { "post-data", 0, OPT_VALUE, "postdata", -1 }, { "post-file", 0, OPT_VALUE, "postfile", -1 }, { "prefer-family", 0, OPT_VALUE, "preferfamily", -1 }, { "preserve-permissions", 0, OPT_BOOLEAN, "preservepermissions", -1 }, { IF_SSL ("private-key"), 0, OPT_VALUE, "privatekey", -1 }, { IF_SSL ("private-key-type"), 0, OPT_VALUE, "privatekeytype", -1 }, { "progress", 0, OPT_VALUE, "progress", -1 }, { "protocol-directories", 0, OPT_BOOLEAN, "protocoldirectories", -1 }, { "proxy", 0, OPT_BOOLEAN, "useproxy", -1 }, { "proxy__compat", 'Y', OPT_VALUE, "useproxy", -1 }, /* back-compatible */ { "proxy-passwd", 0, OPT_VALUE, "proxypassword", -1 }, /* deprecated */ { "proxy-password", 0, OPT_VALUE, "proxypassword", -1 }, { "proxy-user", 0, OPT_VALUE, "proxyuser", -1 }, { "quiet", 'q', OPT_BOOLEAN, "quiet", -1 }, { "quota", 'Q', OPT_VALUE, "quota", -1 }, { "random-file", 0, OPT_VALUE, "randomfile", -1 }, { "random-wait", 0, OPT_BOOLEAN, "randomwait", -1 }, { "read-timeout", 0, OPT_VALUE, "readtimeout", -1 }, { "recursive", 'r', OPT_BOOLEAN, "recursive", -1 }, { "referer", 0, OPT_VALUE, "referer", -1 }, { "reject", 'R', OPT_VALUE, "reject", -1 }, { "relative", 'L', OPT_BOOLEAN, "relativeonly", -1 }, { "remove-listing", 0, OPT_BOOLEAN, "removelisting", -1 }, { "restrict-file-names", 0, OPT_BOOLEAN, "restrictfilenames", -1 }, { "retr-symlinks", 0, OPT_BOOLEAN, "retrsymlinks", -1 }, { "retry-connrefused", 0, OPT_BOOLEAN, "retryconnrefused", -1 }, { "save-cookies", 0, OPT_VALUE, "savecookies", -1 }, { "save-headers", 0, OPT_BOOLEAN, "saveheaders", -1 }, { IF_SSL ("secure-protocol"), 0, OPT_VALUE, "secureprotocol", -1 }, { "server-response", 'S', OPT_BOOLEAN, "serverresponse", -1 }, { "span-hosts", 'H', OPT_BOOLEAN, "spanhosts", -1 }, { "spider", 0, OPT_BOOLEAN, "spider", -1 }, { "strict-comments", 0, OPT_BOOLEAN, "strictcomments", -1 }, { "timeout", 'T', OPT_VALUE, "timeout", -1 }, { "timestamping", 'N', OPT_BOOLEAN, "timestamping", -1 }, { "tries", 't', OPT_VALUE, "tries", -1 }, { "user", 0, OPT_VALUE, "user", -1 }, { "user-agent", 'U', OPT_VALUE, "useragent", -1 }, { "verbose", 'v', OPT_BOOLEAN, "verbose", -1 }, { "verbose", 0, OPT_BOOLEAN, "verbose", -1 }, { "version", 'V', OPT_FUNCALL, (void *) print_version, no_argument }, { "wait", 'w', OPT_VALUE, "wait", -1 }, { "waitretry", 0, OPT_VALUE, "waitretry", -1 }, };#undef IF_DEBUG#undef IF_SSL/* Return a string that contains S with "no-" prepended. The string is NUL-terminated and allocated off static storage at Wget startup. */static char *no_prefix (const char *s){ static char buffer[1024]; static char *p = buffer; char *cp = p; int size = 3 + strlen (s) + 1; /* "no-STRING\0" */ if (p + size >= buffer + sizeof (buffer)) abort (); cp[0] = 'n', cp[1] = 'o', cp[2] = '-'; strcpy (cp + 3, s); p += size; return cp;}/* The arguments that that main passes to getopt_long. */static struct option long_options[2 * countof (option_data) + 1];static char short_options[128];/* Mapping between short option chars and option_data indices. */static unsigned char optmap[96];/* Marker for `--no-FOO' values in long_options. */#define BOOLEAN_NEG_MARKER 1024/* Initialize the long_options array used by getopt_long from the data in option_data. */static voidinit_switches (void){ char *p = short_options; int i, o = 0; for (i = 0; i < countof (option_data); i++) { struct cmdline_option *opt = &option_data[i]; struct option *longopt; if (!opt->long_name) /* The option is disabled. */ continue; longopt = &long_options[o++]; longopt->name = opt->long_name; longopt->val = i; if (opt->short_name) { *p++ = opt->short_name; optmap[opt->short_name - 32] = longopt - long_options; } switch (opt->type) { case OPT_VALUE: longopt->has_arg = required_argument; if (opt->short_name) *p++ = ':'; break; case OPT_BOOLEAN: /* Specify an optional argument for long options, so that --option=off works the same as --no-option, for compatibility with pre-1.10 Wget. However, don't specify optional arguments short-option booleans because they prevent combining of short options. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -