📄 main.c
字号:
/* Command line parsing. Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 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 3 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, see <http://www.gnu.org/licenses/>.Additional permission under GNU GPL version 3 section 7If you modify this program, or any covered work, by linking orcombining it with the OpenSSL project's OpenSSL library (or amodified version of that library), containing parts covered by theterms of the OpenSSL or SSLeay licenses, the Free Software Foundationgrants you additional permission to convey the resulting work.Corresponding Source for a non-source form of such a combinationshall include the source code for the parts of OpenSSL used as wellas that of the covered work. */#include <config.h>#include <stdio.h>#include <stdlib.h>#ifdef HAVE_UNISTD_H# include <unistd.h>#endif /* HAVE_UNISTD_H */#include <string.h>#include <signal.h>#ifdef HAVE_NLS# include <locale.h>#endif#include <assert.h>#include <errno.h>#include <time.h>#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"#include "spider.h"#include "http.h" /* for save_cookies *//* On GNU system this will include system-wide getopt.h. */#include "getopt.h"#ifndef PATH_SEPARATOR# define PATH_SEPARATOR '/'#endifstruct options opt;extern char *version_string;#if defined(SIGHUP) || defined(SIGUSR1)static void redirect_output_signal (int);#endifconst char *exec_name;#ifndef TESTING/* 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. */ setlocale (LC_ALL, ""); /* Set the text message domain. */ bindtextdomain ("wget", LOCALEDIR); textdomain ("wget");#endif /* HAVE_NLS */}/* Definition of command-line options. */static void print_help (void);static void print_version (void);#ifdef HAVE_SSL# define IF_SSL(x) x#else# define IF_SSL(x) NULL#endif#ifdef ENABLE_DEBUG# define WHEN_DEBUG(x) x#else# define WHEN_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 */};static struct cmdline_option option_data[] = { { "accept", 'A', OPT_VALUE, "accept", -1 }, { "append-output", 'a', OPT__APPEND_OUTPUT, NULL, required_argument }, { "auth-no-challenge", 0, OPT_BOOLEAN, "authnochallenge", -1 }, { "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 }, { "content-disposition", 0, OPT_BOOLEAN, "contentdisposition", -1 }, { "cookies", 0, OPT_BOOLEAN, "cookies", -1 }, { "cut-dirs", 0, OPT_VALUE, "cutdirs", -1 }, { WHEN_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-case", 0, OPT_BOOLEAN, "ignorecase", -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 }, { "max-redirect", 0, OPT_VALUE, "maxredirect", -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 },#ifdef MSDOS { "wdebug", 0, OPT_BOOLEAN, "wdebug", -1 },#endif };#undef WHEN_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. */ longopt->has_arg = optional_argument; /* For Boolean options, add the "--no-FOO" variant, which is identical to "--foo", except it has opposite meaning and it doesn't allow an argument. */ longopt = &long_options[o++]; longopt->name = no_prefix (opt->long_name); longopt->has_arg = no_argument; /* Mask the value so we'll be able to recognize that we're dealing with the false value. */ longopt->val = i | BOOLEAN_NEG_MARKER; break; default: assert (opt->argtype != -1); longopt->has_arg = opt->argtype; if (opt->short_name) { if (longopt->has_arg == required_argument) *p++ = ':'; /* Don't handle optional_argument */ } } } /* Terminate short_options. */ *p = '\0'; /* No need for xzero(long_options[o]) because its storage is static and it will be zeroed by default. */ assert (o <= countof (long_options));}/* Print the usage message. */static voidprint_usage (void){ printf (_("Usage: %s [OPTION]... [URL]...\n"), exec_name);}/* Print the help message, describing all the available options. If you add an option, be sure to update this list. */static voidprint_help (void){ /* We split the help text this way to ease translation of individual entries. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -