📄 init.c
字号:
if (!simple_atof (val, end, &number)) goto err; *(double *)place = number * mult; return true;}#ifdef HAVE_SSLstatic boolcmd_cert_type (const char *com, const char *val, void *place){ static const struct decode_item choices[] = { { "pem", keyfile_pem }, { "der", keyfile_asn1 }, { "asn1", keyfile_asn1 }, }; int ok = decode_string (val, choices, countof (choices), place); if (!ok) fprintf (stderr, _("%s: %s: Invalid value `%s'.\n"), exec_name, com, val); return ok;}#endif/* Specialized helper functions, used by `commands' to handle some options specially. */static bool check_user_specified_header (const char *);static boolcmd_spec_dirstruct (const char *com, const char *val, void *place_ignored){ if (!cmd_boolean (com, val, &opt.dirstruct)) return false; /* Since dirstruct behaviour is explicitly changed, no_dirstruct must be affected inversely. */ if (opt.dirstruct) opt.no_dirstruct = false; else opt.no_dirstruct = true; return true;}static boolcmd_spec_header (const char *com, const char *val, void *place_ignored){ /* Empty value means reset the list of headers. */ if (*val == '\0') { free_vec (opt.user_headers); opt.user_headers = NULL; return true; } if (!check_user_specified_header (val)) { fprintf (stderr, _("%s: %s: Invalid header `%s'.\n"), exec_name, com, val); return false; } opt.user_headers = vec_append (opt.user_headers, val); return true;}static boolcmd_spec_htmlify (const char *com, const char *val, void *place_ignored){ int flag = cmd_boolean (com, val, &opt.htmlify); if (flag && !opt.htmlify) opt.remove_listing = false; return flag;}/* Set the "mirror" mode. It means: recursive download, timestamping, no limit on max. recursion depth, and don't remove listings. */static boolcmd_spec_mirror (const char *com, const char *val, void *place_ignored){ int mirror; if (!cmd_boolean (com, val, &mirror)) return false; if (mirror) { opt.recursive = true; if (!opt.no_dirstruct) opt.dirstruct = true; opt.timestamping = true; opt.reclevel = INFINITE_RECURSION; opt.remove_listing = false; } return true;}/* Validate --prefer-family and set the choice. Allowed values are "IPv4", "IPv6", and "none". */static boolcmd_spec_prefer_family (const char *com, const char *val, void *place_ignored){ static const struct decode_item choices[] = { { "IPv4", prefer_ipv4 }, { "IPv6", prefer_ipv6 }, { "none", prefer_none }, }; int prefer_family = prefer_ipv4; int ok = decode_string (val, choices, countof (choices), &prefer_family); if (!ok) fprintf (stderr, _("%s: %s: Invalid value `%s'.\n"), exec_name, com, val); opt.prefer_family = prefer_family; return ok;}/* Set progress.type to VAL, but verify that it's a valid progress implementation before that. */static boolcmd_spec_progress (const char *com, const char *val, void *place_ignored){ if (!valid_progress_implementation_p (val)) { fprintf (stderr, _("%s: %s: Invalid progress type `%s'.\n"), exec_name, com, val); return false; } xfree_null (opt.progress_type); /* Don't call set_progress_implementation here. It will be called in main() when it becomes clear what the log output is. */ opt.progress_type = xstrdup (val); return true;}/* Set opt.recursive to VAL as with cmd_boolean. If opt.recursive is set to true, also set opt.dirstruct to true, unless opt.no_dirstruct is specified. */static boolcmd_spec_recursive (const char *com, const char *val, void *place_ignored){ if (!cmd_boolean (com, val, &opt.recursive)) return false; else { if (opt.recursive && !opt.no_dirstruct) opt.dirstruct = true; } return true;}static boolcmd_spec_restrict_file_names (const char *com, const char *val, void *place_ignored){ int restrict_os = opt.restrict_files_os; int restrict_ctrl = opt.restrict_files_ctrl; int restrict_case = opt.restrict_files_case; const char *end;#define VAL_IS(string_literal) BOUNDED_EQUAL (val, end, string_literal) do { end = strchr (val, ','); if (!end) end = val + strlen (val); if (VAL_IS ("unix")) restrict_os = restrict_unix; else if (VAL_IS ("windows")) restrict_os = restrict_windows; else if (VAL_IS ("lowercase")) restrict_case = restrict_lowercase; else if (VAL_IS ("uppercase")) restrict_case = restrict_uppercase; else if (VAL_IS ("nocontrol")) restrict_ctrl = false; else { fprintf (stderr, _("%s: %s: Invalid restriction `%s', use [unix|windows],[lowercase|uppercase],[nocontrol].\n"), exec_name, com, val); return false; } if (*end) val = end + 1; } while (*val && *end);#undef VAL_IS opt.restrict_files_os = restrict_os; opt.restrict_files_ctrl = restrict_ctrl; opt.restrict_files_case = restrict_case; return true;}#ifdef HAVE_SSLstatic boolcmd_spec_secure_protocol (const char *com, const char *val, void *place){ static const struct decode_item choices[] = { { "auto", secure_protocol_auto }, { "sslv2", secure_protocol_sslv2 }, { "sslv3", secure_protocol_sslv3 }, { "tlsv1", secure_protocol_tlsv1 }, }; int ok = decode_string (val, choices, countof (choices), place); if (!ok) fprintf (stderr, _("%s: %s: Invalid value `%s'.\n"), exec_name, com, val); return ok;}#endif/* Set all three timeout values. */static boolcmd_spec_timeout (const char *com, const char *val, void *place_ignored){ double value; if (!cmd_time (com, val, &value)) return false; opt.read_timeout = value; opt.connect_timeout = value; opt.dns_timeout = value; return true;}static boolcmd_spec_useragent (const char *com, const char *val, void *place_ignored){ /* Disallow embedded newlines. */ if (strchr (val, '\n')) { fprintf (stderr, _("%s: %s: Invalid value `%s'.\n"), exec_name, com, val); return false; } xfree_null (opt.useragent); opt.useragent = xstrdup (val); return true;}/* The "verbose" option cannot be cmd_boolean because the variable is not bool -- it's of type int (-1 means uninitialized because of some random hackery for disallowing -q -v). */static boolcmd_spec_verbose (const char *com, const char *val, void *place_ignored){ bool flag; if (cmd_boolean (com, val, &flag)) { opt.verbose = flag; return true; } return false;}/* Miscellaneous useful routines. *//* A very simple atoi clone, more useful than atoi because it works on delimited strings, and has error reportage. Returns true on success, false on failure. If successful, stores result to *DEST. */static boolsimple_atoi (const char *beg, const char *end, int *dest){ int result = 0; bool negative = false; const char *p = beg; while (p < end && ISSPACE (*p)) ++p; if (p < end && (*p == '-' || *p == '+')) { negative = (*p == '-'); ++p; } if (p == end) return false; /* Read negative numbers in a separate loop because the most negative integer cannot be represented as a positive number. */ if (!negative) for (; p < end && ISDIGIT (*p); p++) { int next = (10 * result) + (*p - '0'); if (next < result) return false; /* overflow */ result = next; } else for (; p < end && ISDIGIT (*p); p++) { int next = (10 * result) - (*p - '0'); if (next > result) return false; /* underflow */ result = next; } if (p != end) return false; *dest = result; return true;}/* Trivial atof, with error reporting. Handles "<digits>[.<digits>]", doesn't handle exponential notation. Returns true on success, false on failure. In case of success, stores its result to *DEST. */static boolsimple_atof (const char *beg, const char *end, double *dest){ double result = 0; bool negative = false; bool seen_dot = false; bool seen_digit = false; double divider = 1; const char *p = beg; while (p < end && ISSPACE (*p)) ++p; if (p < end && (*p == '-' || *p == '+')) { negative = (*p == '-'); ++p; } for (; p < end; p++) { char ch = *p; if (ISDIGIT (ch)) { if (!seen_dot) result = (10 * result) + (ch - '0'); else result += (ch - '0') / (divider *= 10); seen_digit = true; } else if (ch == '.') { if (!seen_dot) seen_dot = true; else return false; } else return false; } if (!seen_digit) return false; if (negative) result = -result; *dest = result; return true;}/* Verify that the user-specified header in S is valid. It must contain a colon preceded by non-white-space characters and must not contain newlines. */static boolcheck_user_specified_header (const char *s){ const char *p; for (p = s; *p && *p != ':' && !ISSPACE (*p); p++) ; /* The header MUST contain `:' preceded by at least one non-whitespace character. */ if (*p != ':' || p == s) return false; /* The header MUST NOT contain newlines. */ if (strchr (s, '\n')) return false; return true;}/* Decode VAL into a number, according to ITEMS. */static booldecode_string (const char *val, const struct decode_item *items, int itemcount, int *place){ int i; for (i = 0; i < itemcount; i++) if (0 == strcasecmp (val, items[i].name)) { *place = items[i].code; return true; } return false;}void cleanup_html_url (void);/* Free the memory allocated by global variables. */voidcleanup (void){ /* Free external resources, close files, etc. */ if (output_stream) fclose (output_stream); /* No need to check for error because Wget flushes its output (and checks for errors) after any data arrives. */ /* We're exiting anyway so there's no real need to call free() hundreds of times. Skipping the frees will make Wget exit faster. However, when detecting leaks, it's crucial to free() everything because then you can find the real leaks, i.e. the allocated memory which grows with the size of the program. */#ifdef DEBUG_MALLOC convert_cleanup (); res_cleanup (); http_cleanup (); cleanup_html_url (); host_cleanup (); log_cleanup (); { extern acc_t *netrc_list; free_netrc (netrc_list); } xfree_null (opt.lfilename); xfree_null (opt.dir_prefix); xfree_null (opt.input_filename); xfree_null (opt.output_document); free_vec (opt.accepts); free_vec (opt.rejects); free_vec (opt.excludes); free_vec (opt.includes); free_vec (opt.domains); free_vec (opt.follow_tags); free_vec (opt.ignore_tags); xfree_null (opt.progress_type); xfree_null (opt.ftp_user); xfree_null (opt.ftp_passwd); xfree_null (opt.ftp_proxy); xfree_null (opt.https_proxy); xfree_null (opt.http_proxy); free_vec (opt.no_proxy); xfree_null (opt.useragent); xfree_null (opt.referer); xfree_null (opt.http_user); xfree_null (opt.http_passwd); free_vec (opt.user_headers);# ifdef HAVE_SSL xfree_null (opt.cert_file); xfree_null (opt.private_key); xfree_null (opt.ca_directory); xfree_null (opt.ca_cert); xfree_null (opt.random_file); xfree_null (opt.egd_file);# endif xfree_null (opt.bind_address); xfree_null (opt.cookies_input); xfree_null (opt.cookies_output); xfree_null (opt.user); xfree_null (opt.passwd);#endif /* DEBUG_MALLOC */}/* Unit testing routines. */#ifdef TESTINGconst char *test_cmd_spec_restrict_file_names(){ int i; struct { char *val; int expected_restrict_files_os; int expected_restrict_files_ctrl; int expected_restrict_files_case; bool result; } test_array[] = { { "windows", restrict_windows, true, restrict_no_case_restriction, true }, { "windows,", restrict_windows, true, restrict_no_case_restriction, true }, { "windows,lowercase", restrict_windows, true, restrict_lowercase, true }, { "unix,nocontrol,lowercase,", restrict_unix, false, restrict_lowercase, true }, }; for (i = 0; i < sizeof(test_array)/sizeof(test_array[0]); ++i) { bool res; defaults(); res = cmd_spec_restrict_file_names ("dummy", test_array[i].val, NULL); /* fprintf (stderr, "test_cmd_spec_restrict_file_names: TEST %d\n", i); fflush (stderr); fprintf (stderr, "opt.restrict_files_os: %d\n", opt.restrict_files_os); fflush (stderr); fprintf (stderr, "opt.restrict_files_ctrl: %d\n", opt.restrict_files_ctrl); fflush (stderr); fprintf (stderr, "opt.restrict_files_case: %d\n", opt.restrict_files_case); fflush (stderr); */ mu_assert ("test_cmd_spec_restrict_file_names: wrong result", res == test_array[i].result && opt.restrict_files_os == test_array[i].expected_restrict_files_os && opt.restrict_files_ctrl == test_array[i].expected_restrict_files_ctrl && opt.restrict_files_case == test_array[i].expected_restrict_files_case); } return NULL;}#endif /* TESTING */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -