📄 main.c
字号:
exec_name = argv[0]; else ++exec_name;#ifdef WINDOWS /* Drop extension (typically .EXE) from executable filename. */ windows_main (&argc, (char **) argv, (char **) &exec_name);#endif /* Set option defaults; read the system wgetrc and ~/.wgetrc. */ initialize (); init_switches (); longindex = -1; while ((ret = getopt_long (argc, argv, short_options, long_options, &longindex)) != -1) { int val; struct cmdline_option *opt; /* If LONGINDEX is unchanged, it means RET is referring a short option. */ if (longindex == -1) { if (ret == '?') { print_usage (); printf ("\n"); printf (_("Try `%s --help' for more options.\n"), exec_name); exit (2); } /* Find the short option character in the mapping. */ longindex = optmap[ret - 32]; } val = long_options[longindex].val; /* Use the retrieved value to locate the option in the option_data array, and to see if we're dealing with the negated "--no-FOO" variant of the boolean option "--foo". */ opt = &option_data[val & ~BOOLEAN_NEG_MARKER]; switch (opt->type) { case OPT_VALUE: setoptval (opt->data, optarg, opt->long_name); break; case OPT_BOOLEAN: if (optarg) /* The user has specified a value -- use it. */ setoptval (opt->data, optarg, opt->long_name); else { /* NEG is true for `--no-FOO' style boolean options. */ int neg = val & BOOLEAN_NEG_MARKER; setoptval (opt->data, neg ? "0" : "1", opt->long_name); } break; case OPT_FUNCALL: { void (*func) PARAMS ((void)) = (void (*) PARAMS ((void))) opt->data; func (); } break; case OPT__APPEND_OUTPUT: setoptval ("logfile", optarg, opt->long_name); append_to_log = 1; break; case OPT__EXECUTE: run_command (optarg); break; case OPT__NO: { /* We support real --no-FOO flags now, but keep these short options for convenience and backward compatibility. */ char *p; for (p = optarg; *p; p++) switch (*p) { case 'v': setoptval ("verbose", "0", opt->long_name); break; case 'H': setoptval ("addhostdir", "0", opt->long_name); break; case 'd': setoptval ("dirstruct", "0", opt->long_name); break; case 'c': setoptval ("noclobber", "1", opt->long_name); break; case 'p': setoptval ("noparent", "1", opt->long_name); break; default: printf (_("%s: illegal option -- `-n%c'\n"), exec_name, *p); print_usage (); printf ("\n"); printf (_("Try `%s --help' for more options.\n"), exec_name); exit (1); } break; } case OPT__PARENT: case OPT__CLOBBER: { /* The wgetrc commands are named noparent and noclobber, so we must revert the meaning of the cmdline options before passing the value to setoptval. */ int flag = 1; if (optarg) flag = (*optarg == '1' || TOLOWER (*optarg) == 'y' || (TOLOWER (optarg[0]) == 'o' && TOLOWER (optarg[1]) == 'n')); setoptval (opt->type == OPT__PARENT ? "noparent" : "noclobber", flag ? "0" : "1", opt->long_name); break; } case OPT__DONT_REMOVE_LISTING: setoptval ("removelisting", "0", opt->long_name); break; } longindex = -1; } /* All user options have now been processed, so it's now safe to do interoption dependency checks. */ if (opt.reclevel == 0) opt.reclevel = INFINITE_RECURSION; /* see recur.h for commentary on this */ if (opt.page_requisites && !opt.recursive) { /* Don't set opt.recursive here because it would confuse the FTP code. Instead, call retrieve_tree below when either page_requisites or recursive is requested. */ opt.reclevel = 0; if (!opt.no_dirstruct) opt.dirstruct = 1; /* normally handled by cmd_spec_recursive() */ } if (opt.verbose == -1) opt.verbose = !opt.quiet; /* Sanity checks. */ if (opt.verbose && opt.quiet) { printf (_("Can't be verbose and quiet at the same time.\n")); print_usage (); exit (1); } if (opt.timestamping && opt.noclobber) { printf (_("\Can't timestamp and not clobber old files at the same time.\n")); print_usage (); exit (1); }#ifdef ENABLE_IPV6 if (opt.ipv4_only && opt.ipv6_only) { printf (_("Cannot specify both --inet4-only and --inet6-only.\n")); print_usage (); exit (1); }#endif nurl = argc - optind; if (!nurl && !opt.input_filename) { /* No URL specified. */ printf (_("%s: missing URL\n"), exec_name); print_usage (); printf ("\n"); /* #### Something nicer should be printed here -- similar to the pre-1.5 `--help' page. */ printf (_("Try `%s --help' for more options.\n"), exec_name); exit (1); } if (opt.background) fork_to_background (); /* Initialize progress. Have to do this after the options are processed so we know where the log file is. */ if (opt.verbose) set_progress_implementation (opt.progress_type); /* Fill in the arguments. */ url = alloca_array (char *, nurl + 1); for (i = 0; i < nurl; i++, optind++) { char *rewritten = rewrite_shorthand_url (argv[optind]); if (rewritten) url[i] = rewritten; else url[i] = xstrdup (argv[optind]); } url[i] = NULL; /* Initialize logging. */ log_init (opt.lfilename, append_to_log); DEBUGP (("DEBUG output created by Wget %s on %s.\n\n", version_string, OS_TYPE)); /* Open the output filename if necessary. */ if (opt.output_document) { extern FILE *output_stream; extern int output_stream_regular; if (HYPHENP (opt.output_document)) output_stream = stdout; else { struct_fstat st; output_stream = fopen (opt.output_document, opt.always_rest ? "ab" : "wb"); if (output_stream == NULL) { perror (opt.output_document); exit (1); } if (fstat (fileno (output_stream), &st) == 0 && S_ISREG (st.st_mode)) output_stream_regular = 1; } }#ifdef WINDOWS ws_startup ();#endif /* Setup the signal handler to redirect output when hangup is received. */#ifdef HAVE_SIGNAL if (signal(SIGHUP, SIG_IGN) != SIG_IGN) signal(SIGHUP, redirect_output_signal); /* ...and do the same for SIGUSR1. */ signal (SIGUSR1, redirect_output_signal); /* Writing to a closed socket normally signals SIGPIPE, and the process exits. What we want is to ignore SIGPIPE and just check for the return value of write(). */ signal (SIGPIPE, SIG_IGN);#ifdef SIGWINCH signal (SIGWINCH, progress_handle_sigwinch);#endif#endif /* HAVE_SIGNAL */ status = RETROK; /* initialize it, just-in-case */ /* Retrieve the URLs from argument list. */ for (t = url; *t; t++) { char *filename = NULL, *redirected_URL = NULL; int dt; if ((opt.recursive || opt.page_requisites) && url_scheme (*t) != SCHEME_FTP) status = retrieve_tree (*t); else status = retrieve_url (*t, &filename, &redirected_URL, NULL, &dt); if (opt.delete_after && file_exists_p(filename)) { DEBUGP (("Removing file due to --delete-after in main():\n")); logprintf (LOG_VERBOSE, _("Removing %s.\n"), filename); if (unlink (filename)) logprintf (LOG_NOTQUIET, "unlink: %s\n", strerror (errno)); } xfree_null (redirected_URL); xfree_null (filename); } /* And then from the input file, if any. */ if (opt.input_filename) { int count; status = retrieve_from_file (opt.input_filename, opt.force_html, &count); if (!count) logprintf (LOG_NOTQUIET, _("No URLs found in %s.\n"), opt.input_filename); } /* Print the downloaded sum. */ if (opt.recursive || opt.page_requisites || nurl > 1 || (opt.input_filename && total_downloaded_bytes != 0)) { logprintf (LOG_NOTQUIET, _("\nFINISHED --%s--\nDownloaded: %s bytes in %d files\n"), time_str (NULL), with_thousand_seps_sum (total_downloaded_bytes), opt.numurls); /* Print quota warning, if exceeded. */ if (opt.quota && total_downloaded_bytes > opt.quota) logprintf (LOG_NOTQUIET, _("Download quota (%s bytes) EXCEEDED!\n"), with_thousand_seps_sum (opt.quota)); } if (opt.cookies_output) save_cookies (); if (opt.convert_links && !opt.delete_after) convert_all_links (); log_close (); for (i = 0; i < nurl; i++) xfree (url[i]); cleanup ();#ifdef DEBUG_MALLOC print_malloc_debug_stats ();#endif if (status == RETROK) return 0; else return 1;}#ifdef HAVE_SIGNAL/* Hangup signal handler. When wget receives SIGHUP or SIGUSR1, it will proceed operation as usual, trying to write into a log file. If that is impossible, the output will be turned off. #### It is unsafe to do call libc functions from a signal handler. What we should do is, set a global variable, and have the code in log.c pick it up. */static RETSIGTYPEredirect_output_signal (int sig){ const char *signal_name = (sig == SIGHUP ? "SIGHUP" : (sig == SIGUSR1 ? "SIGUSR1" : "WTF?!")); log_request_redirect_output (signal_name); progress_schedule_redirect (); signal (sig, redirect_output_signal);}#endif /* HAVE_SIGNAL */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -