⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 init.c

📁 一个从网络上自动下载文件的自由工具
💻 C
📖 第 1 页 / 共 3 页
字号:
/* Reading/parsing the initialization file.   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#include <string.h>#include <errno.h>#ifdef HAVE_PWD_H# include <pwd.h>#endif#include <assert.h>#include "wget.h"#include "utils.h"#include "init.h"#include "host.h"#include "netrc.h"#include "progress.h"#include "recur.h"              /* for INFINITE_RECURSION */#include "convert.h"            /* for convert_cleanup */#include "res.h"                /* for res_cleanup */#include "http.h"               /* for http_cleanup */#include "retr.h"               /* for output_stream */#ifdef TESTING#include "test.h"#endif/* We want tilde expansion enabled only when reading `.wgetrc' lines;   otherwise, it will be performed by the shell.  This variable will   be set by the wgetrc-reading function.  */static bool enable_tilde_expansion;#define CMD_DECLARE(func) static bool func (const char *, const char *, void *)CMD_DECLARE (cmd_boolean);CMD_DECLARE (cmd_bytes);CMD_DECLARE (cmd_bytes_sum);#ifdef HAVE_SSLCMD_DECLARE (cmd_cert_type);#endifCMD_DECLARE (cmd_directory_vector);CMD_DECLARE (cmd_number);CMD_DECLARE (cmd_number_inf);CMD_DECLARE (cmd_string);CMD_DECLARE (cmd_file);CMD_DECLARE (cmd_directory);CMD_DECLARE (cmd_time);CMD_DECLARE (cmd_vector);CMD_DECLARE (cmd_spec_dirstruct);CMD_DECLARE (cmd_spec_header);CMD_DECLARE (cmd_spec_htmlify);CMD_DECLARE (cmd_spec_mirror);CMD_DECLARE (cmd_spec_prefer_family);CMD_DECLARE (cmd_spec_progress);CMD_DECLARE (cmd_spec_recursive);CMD_DECLARE (cmd_spec_restrict_file_names);#ifdef HAVE_SSLCMD_DECLARE (cmd_spec_secure_protocol);#endifCMD_DECLARE (cmd_spec_timeout);CMD_DECLARE (cmd_spec_useragent);CMD_DECLARE (cmd_spec_verbose);/* List of recognized commands, each consisting of name, place and   function.  When adding a new command, simply add it to the list,   but be sure to keep the list sorted alphabetically, as   command_by_name's binary search depends on it.  Also, be sure to   add any entries that allocate memory (e.g. cmd_string and   cmd_vector) to the cleanup() function below. */static const struct {  const char *name;  void *place;  bool (*action) (const char *, const char *, void *);} commands[] = {  /* KEEP THIS LIST ALPHABETICALLY SORTED */  { "accept",           &opt.accepts,           cmd_vector },  { "addhostdir",       &opt.add_hostdir,       cmd_boolean },  { "alwaysrest",       &opt.always_rest,       cmd_boolean }, /* deprecated */  { "background",       &opt.background,        cmd_boolean },  { "backupconverted",  &opt.backup_converted,  cmd_boolean },  { "backups",          &opt.backups,           cmd_number },  { "base",             &opt.base_href,         cmd_string },  { "bindaddress",      &opt.bind_address,      cmd_string },#ifdef HAVE_SSL  { "cacertificate",    &opt.ca_cert,           cmd_file },#endif  { "cache",            &opt.allow_cache,       cmd_boolean },#ifdef HAVE_SSL  { "cadirectory",      &opt.ca_directory,      cmd_directory },  { "certificate",      &opt.cert_file,         cmd_file },  { "certificatetype",  &opt.cert_type,         cmd_cert_type },  { "checkcertificate", &opt.check_cert,        cmd_boolean },#endif  { "connecttimeout",   &opt.connect_timeout,   cmd_time },  { "contentdisposition", &opt.content_disposition, cmd_boolean },  { "continue",         &opt.always_rest,       cmd_boolean },  { "convertlinks",     &opt.convert_links,     cmd_boolean },  { "cookies",          &opt.cookies,           cmd_boolean },  { "cutdirs",          &opt.cut_dirs,          cmd_number },#ifdef ENABLE_DEBUG  { "debug",            &opt.debug,             cmd_boolean },#endif  { "deleteafter",      &opt.delete_after,      cmd_boolean },  { "dirprefix",        &opt.dir_prefix,        cmd_directory },  { "dirstruct",        NULL,                   cmd_spec_dirstruct },  { "dnscache",         &opt.dns_cache,         cmd_boolean },  { "dnstimeout",       &opt.dns_timeout,       cmd_time },  { "domains",          &opt.domains,           cmd_vector },  { "dotbytes",         &opt.dot_bytes,         cmd_bytes },  { "dotsinline",       &opt.dots_in_line,      cmd_number },  { "dotspacing",       &opt.dot_spacing,       cmd_number },  { "dotstyle",         &opt.dot_style,         cmd_string },#ifdef HAVE_SSL  { "egdfile",          &opt.egd_file,          cmd_file },#endif  { "excludedirectories", &opt.excludes,        cmd_directory_vector },  { "excludedomains",   &opt.exclude_domains,   cmd_vector },  { "followftp",        &opt.follow_ftp,        cmd_boolean },  { "followtags",       &opt.follow_tags,       cmd_vector },  { "forcehtml",        &opt.force_html,        cmd_boolean },  { "ftppasswd",        &opt.ftp_passwd,        cmd_string }, /* deprecated */  { "ftppassword",      &opt.ftp_passwd,        cmd_string },  { "ftpproxy",         &opt.ftp_proxy,         cmd_string },  { "ftpuser",          &opt.ftp_user,          cmd_string },  { "glob",             &opt.ftp_glob,          cmd_boolean },  { "header",           NULL,                   cmd_spec_header },  { "htmlextension",    &opt.html_extension,    cmd_boolean },  { "htmlify",          NULL,                   cmd_spec_htmlify },  { "httpkeepalive",    &opt.http_keep_alive,   cmd_boolean },  { "httppasswd",       &opt.http_passwd,       cmd_string }, /* deprecated */  { "httppassword",     &opt.http_passwd,       cmd_string },  { "httpproxy",        &opt.http_proxy,        cmd_string },  { "httpsproxy",       &opt.https_proxy,       cmd_string },  { "httpuser",         &opt.http_user,         cmd_string },  { "ignorecase",       &opt.ignore_case,       cmd_boolean },  { "ignorelength",     &opt.ignore_length,     cmd_boolean },  { "ignoretags",       &opt.ignore_tags,       cmd_vector },  { "includedirectories", &opt.includes,        cmd_directory_vector },#ifdef ENABLE_IPV6  { "inet4only",        &opt.ipv4_only,         cmd_boolean },  { "inet6only",        &opt.ipv6_only,         cmd_boolean },#endif  { "input",            &opt.input_filename,    cmd_file },  { "keepsessioncookies", &opt.keep_session_cookies, cmd_boolean },  { "limitrate",        &opt.limit_rate,        cmd_bytes },  { "loadcookies",      &opt.cookies_input,     cmd_file },  { "logfile",          &opt.lfilename,         cmd_file },  { "login",            &opt.ftp_user,          cmd_string },/* deprecated*/  { "maxredirect",      &opt.max_redirect,      cmd_number },  { "mirror",           NULL,                   cmd_spec_mirror },  { "netrc",            &opt.netrc,             cmd_boolean },  { "noclobber",        &opt.noclobber,         cmd_boolean },  { "noparent",         &opt.no_parent,         cmd_boolean },  { "noproxy",          &opt.no_proxy,          cmd_vector },  { "numtries",         &opt.ntry,              cmd_number_inf },/* deprecated*/  { "outputdocument",   &opt.output_document,   cmd_file },  { "pagerequisites",   &opt.page_requisites,   cmd_boolean },  { "passiveftp",       &opt.ftp_pasv,          cmd_boolean },  { "passwd",           &opt.ftp_passwd,        cmd_string },/* deprecated*/  { "password",         &opt.passwd,            cmd_string },  { "postdata",         &opt.post_data,         cmd_string },  { "postfile",         &opt.post_file_name,    cmd_file },  { "preferfamily",     NULL,                   cmd_spec_prefer_family },  { "preservepermissions", &opt.preserve_perm,  cmd_boolean },#ifdef HAVE_SSL  { "privatekey",       &opt.private_key,       cmd_file },  { "privatekeytype",   &opt.private_key_type,  cmd_cert_type },#endif  { "progress",         &opt.progress_type,     cmd_spec_progress },  { "protocoldirectories", &opt.protocol_directories, cmd_boolean },  { "proxypasswd",      &opt.proxy_passwd,      cmd_string }, /* deprecated */  { "proxypassword",    &opt.proxy_passwd,      cmd_string },  { "proxyuser",        &opt.proxy_user,        cmd_string },  { "quiet",            &opt.quiet,             cmd_boolean },  { "quota",            &opt.quota,             cmd_bytes_sum },#ifdef HAVE_SSL  { "randomfile",       &opt.random_file,       cmd_file },#endif  { "randomwait",       &opt.random_wait,       cmd_boolean },  { "readtimeout",      &opt.read_timeout,      cmd_time },  { "reclevel",         &opt.reclevel,          cmd_number_inf },  { "recursive",        NULL,                   cmd_spec_recursive },  { "referer",          &opt.referer,           cmd_string },  { "reject",           &opt.rejects,           cmd_vector },  { "relativeonly",     &opt.relative_only,     cmd_boolean },  { "removelisting",    &opt.remove_listing,    cmd_boolean },  { "restrictfilenames", NULL,                  cmd_spec_restrict_file_names },  { "retrsymlinks",     &opt.retr_symlinks,     cmd_boolean },  { "retryconnrefused", &opt.retry_connrefused, cmd_boolean },  { "robots",           &opt.use_robots,        cmd_boolean },  { "savecookies",      &opt.cookies_output,    cmd_file },  { "saveheaders",      &opt.save_headers,      cmd_boolean },#ifdef HAVE_SSL  { "secureprotocol",   &opt.secure_protocol,   cmd_spec_secure_protocol },#endif  { "serverresponse",   &opt.server_response,   cmd_boolean },  { "spanhosts",        &opt.spanhost,          cmd_boolean },  { "spider",           &opt.spider,            cmd_boolean },  { "strictcomments",   &opt.strict_comments,   cmd_boolean },  { "timeout",          NULL,                   cmd_spec_timeout },  { "timestamping",     &opt.timestamping,      cmd_boolean },  { "tries",            &opt.ntry,              cmd_number_inf },  { "useproxy",         &opt.use_proxy,         cmd_boolean },  { "user",             &opt.user,              cmd_string },  { "useragent",        NULL,                   cmd_spec_useragent },  { "verbose",          NULL,                   cmd_spec_verbose },  { "wait",             &opt.wait,              cmd_time },  { "waitretry",        &opt.waitretry,         cmd_time },#ifdef MSDOS  { "wdebug",           &opt.wdebug,            cmd_boolean },#endif};/* Look up CMDNAME in the commands[] and return its position in the   array.  If CMDNAME is not found, return -1.  */static intcommand_by_name (const char *cmdname){  /* Use binary search for speed.  Wget has ~100 commands, which     guarantees a worst case performance of 7 string comparisons.  */  int lo = 0, hi = countof (commands) - 1;  while (lo <= hi)    {      int mid = (lo + hi) >> 1;      int cmp = strcasecmp (cmdname, commands[mid].name);      if (cmp < 0)        hi = mid - 1;      else if (cmp > 0)        lo = mid + 1;      else        return mid;    }  return -1;}/* Reset the variables to default values.  */static voiddefaults (void){  char *tmp;  /* Most of the default values are 0 (and 0.0, NULL, and false).     Just reset everything, and fill in the non-zero values.  Note     that initializing pointers to NULL this way is technically     illegal, but porting Wget to a machine where NULL is not all-zero     bit pattern will be the least of the implementors' worries.  */  xzero (opt);  opt.cookies = true;  opt.verbose = -1;  opt.ntry = 20;  opt.reclevel = 5;  opt.add_hostdir = true;  opt.netrc = true;  opt.ftp_glob = true;  opt.htmlify = true;  opt.http_keep_alive = true;  opt.use_proxy = true;  tmp = getenv ("no_proxy");  if (tmp)    opt.no_proxy = sepstring (tmp);  opt.allow_cache = true;  opt.read_timeout = 900;  opt.use_robots = true;  opt.remove_listing = true;  opt.dot_bytes = 1024;  opt.dot_spacing = 10;  opt.dots_in_line = 50;  opt.dns_cache = true;  opt.ftp_pasv = true;#ifdef HAVE_SSL  opt.check_cert = true;#endif  /* The default for file name restriction defaults to the OS type. */#if defined(WINDOWS) || defined(MSDOS) || defined(__CYGWIN__)  opt.restrict_files_os = restrict_windows;#else  opt.restrict_files_os = restrict_unix;#endif  opt.restrict_files_ctrl = true;  opt.restrict_files_case = restrict_no_case_restriction;  opt.max_redirect = 20;}/* Return the user's home directory (strdup-ed), or NULL if none is   found.  */char *home_dir (void){  char *home = getenv ("HOME");  if (!home)    {#if defined(MSDOS)      /* Under MSDOS, if $HOME isn't defined, use the directory where         `wget.exe' resides.  */      const char *_w32_get_argv0 (void); /* in libwatt.a/pcconfig.c */      char *p, buf[PATH_MAX];      strcpy (buf, _w32_get_argv0 ());      p = strrchr (buf, '/');            /* djgpp */      if (!p)        p = strrchr (buf, '\\');          /* others */      assert (p);      *p = '\0';      home = buf;#elif !defined(WINDOWS)      /* If HOME is not defined, try getting it from the password         file.  */      struct passwd *pwd = getpwuid (getuid ());      if (!pwd || !pwd->pw_dir)        return NULL;      home = pwd->pw_dir;#else  /* !WINDOWS */      /* Under Windows, if $HOME isn't defined, use the directory where         `wget.exe' resides.  */      home = ws_mypath ();#endif /* WINDOWS */    }  return home ? xstrdup (home) : NULL;}/* Return the path to the user's .wgetrc.  This is either the value of   `WGETRC' environment variable, or `$HOME/.wgetrc'.   If the `WGETRC' variable exists but the file does not exist, the   function will exit().  */static char *wgetrc_file_name (void){  char *env, *home;  char *file = NULL;  /* Try the environment.  */  env = getenv ("WGETRC");  if (env && *env)    {      if (!file_exists_p (env))        {          fprintf (stderr, _("%s: WGETRC points to %s, which doesn't exist.\n"),                   exec_name, env);          exit (1);        }      return xstrdup (env);    }  /* If that failed, try $HOME/.wgetrc.  */  home = home_dir ();  if (home)    file = aprintf ("%s/.wgetrc", home);  xfree_null (home);#ifdef WINDOWS  /* Under Windows, if we still haven't found .wgetrc, look for the file     `wget.ini' in the directory where `wget.exe' resides; we do this for     backward compatibility with previous versions of Wget.     SYSTEM_WGETRC should not be defined under WINDOWS.  */  if (!file || !file_exists_p (file))    {      xfree_null (file);      file = NULL;      home = ws_mypath ();      if (home)        file = aprintf ("%s/wget.ini", home);    }#endif /* WINDOWS */  if (!file)    return NULL;  if (!file_exists_p (file))    {      xfree (file);      return NULL;    }  return file;}/* Return values of parse_line. */enum parse_line {  line_ok,  line_empty,  line_syntax_error,  line_unknown_command};static enum parse_line parse_line (const char *, char **, char **, int *);static bool setval_internal (int, const char *, const char *);/* Initialize variables from a wgetrc file.  Returns zero (failure) if   there were errors in the file.  */static boolrun_wgetrc (const char *file){  FILE *fp;  char *line;  int ln;  int errcnt = 0;  fp = fopen (file, "rb");  if (!fp)    {      fprintf (stderr, _("%s: Cannot read %s (%s).\n"), exec_name,               file, strerror (errno));      return true;                      /* not a fatal error */    }  enable_tilde_expansion = true;  ln = 1;  while ((line = read_whole_line (fp)) != NULL)    {      char *com = NULL, *val = NULL;      int comind;      /* Parse the line.  */      switch (parse_line (line, &com, &val, &comind))        {        case line_ok:          /* If everything is OK, set the value.  */          if (!setval_internal (comind, com, val))            {              fprintf (stderr, _("%s: Error in %s at line %d.\n"),                       exec_name, file, ln);              ++errcnt;            }          break;        case line_syntax_error:          fprintf (stderr, _("%s: Syntax error in %s at line %d.\n"),                   exec_name, file, ln);          ++errcnt;          break;        case line_unknown_command:          fprintf (stderr, _("%s: Unknown command `%s' in %s at line %d.\n"),                   exec_name, com, file, ln);          ++errcnt;          break;        case line_empty:          break;        default:          abort ();        }      xfree_null (com);      xfree_null (val);      xfree (line);      ++ln;    }  enable_tilde_expansion = false;  fclose (fp);  return errcnt == 0;}/* Initialize the defaults and run the system wgetrc and user's own   wgetrc.  */voidinitialize (void){  char *file;  int ok = true;  /* Load the hard-coded defaults.  */  defaults ();  /* If SYSTEM_WGETRC is defined, use it.  */#ifdef SYSTEM_WGETRC  if (file_exists_p (SYSTEM_WGETRC))    ok &= run_wgetrc (SYSTEM_WGETRC);#endif  /* Override it with your own, if one exists.  */  file = wgetrc_file_name ();  if (!file)    return;  /* #### We should canonicalize `file' and SYSTEM_WGETRC with     something like realpath() before comparing them with `strcmp'  */#ifdef SYSTEM_WGETRC  if (!strcmp (file, SYSTEM_WGETRC))    {      fprintf (stderr, _("\

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -