1 /* Command line parsing.
2 Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Wget.
7 GNU Wget is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GNU Wget is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Wget; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
27 #endif /* HAVE_UNISTD_H */
28 #include <sys/types.h>
33 #endif /* HAVE_STRING_H */
40 #endif /* HAVE_LOCALE_H */
52 #include "progress.h" /* for progress_handle_sigwinch */
55 # include "gen_sslfunc.h"
58 /* On GNU system this will include system-wide getopt.h. */
61 #ifndef PATH_SEPARATOR
62 # define PATH_SEPARATOR '/'
65 extern char *version_string;
74 void log_init PARAMS ((const char *, int));
75 void log_close PARAMS ((void));
76 void log_request_redirect_output PARAMS ((const char *));
78 static RETSIGTYPE redirect_output_signal PARAMS ((int));
80 const char *exec_name;
82 /* Initialize I18N. The initialization amounts to invoking
83 setlocale(), bindtextdomain() and textdomain().
84 Does nothing if NLS is disabled or missing. */
86 i18n_initialize (void)
88 /* If HAVE_NLS is defined, assume the existence of the three
89 functions invoked here. */
91 /* Set the current locale. */
92 /* Here we use LC_MESSAGES instead of LC_ALL, for two reasons.
93 First, message catalogs are all of I18N Wget uses anyway.
94 Second, setting LC_ALL has a dangerous potential of messing
95 things up. For example, when in a foreign locale, Solaris
96 strptime() fails to handle international dates correctly, which
97 makes http_atotm() malfunction. */
99 setlocale (LC_MESSAGES, "");
100 setlocale (LC_CTYPE, "");
102 setlocale (LC_ALL, "");
104 /* Set the text message domain. */
105 bindtextdomain ("wget", LOCALEDIR);
107 #endif /* HAVE_NLS */
110 /* Print the usage message. */
114 printf (_("Usage: %s [OPTION]... [URL]...\n"), exec_name);
117 /* Print the help message, describing all the available options. If
118 you add an option, be sure to update this list. */
122 printf (_("GNU Wget %s, a non-interactive network retriever.\n"),
125 /* Had to split this in parts, so the #@@#%# Ultrix compiler and cpp
126 don't bitch. Also, it makes translation much easier. */
129 Mandatory arguments to long options are mandatory for short options too.\n\
133 -V, --version display the version of Wget and exit.\n\
134 -h, --help print this help.\n\
135 -b, --background go to background after startup.\n\
136 -e, --execute=COMMAND execute a `.wgetrc\'-style command.\n\
139 Logging and input file:\n\
140 -o, --output-file=FILE log messages to FILE.\n\
141 -a, --append-output=FILE append messages to FILE.\n\
142 -d, --debug print debug output.\n\
143 -q, --quiet quiet (no output).\n\
144 -v, --verbose be verbose (this is the default).\n\
145 -nv, --non-verbose turn off verboseness, without being quiet.\n\
146 -i, --input-file=FILE download URLs found in FILE.\n\
147 -F, --force-html treat input file as HTML.\n\
148 -B, --base=URL prepends URL to relative links in -F -i file.\n\
149 --sslcertfile=FILE optional client certificate.\n\
150 --sslcertkey=KEYFILE optional keyfile for this certificate.\n\
151 --egd-file=FILE file name of the EGD socket.\n\
155 --bind-address=ADDRESS bind to ADDRESS (hostname or IP) on local host.\n\
156 -t, --tries=NUMBER set number of retries to NUMBER (0 unlimits).\n\
157 -O --output-document=FILE write documents to FILE.\n\
158 -nc, --no-clobber don\'t clobber existing files or use .# suffixes.\n\
159 -c, --continue resume getting a partially-downloaded file.\n\
160 --progress=TYPE select progress gauge type.\n\
161 -N, --timestamping don\'t re-retrieve files unless newer than local.\n\
162 -S, --server-response print server response.\n\
163 --spider don\'t download anything.\n\
164 -T, --timeout=SECONDS set the read timeout to SECONDS.\n\
165 -w, --wait=SECONDS wait SECONDS between retrievals.\n\
166 --waitretry=SECONDS wait 1...SECONDS between retries of a retrieval.\n\
167 --random-wait wait from 0...2*WAIT secs between retrievals.\n\
168 -Y, --proxy=on/off turn proxy on or off.\n\
169 -Q, --quota=NUMBER set retrieval quota to NUMBER.\n\
170 --limit-rate=RATE limit download rate to RATE.\n\
174 -nd --no-directories don\'t create directories.\n\
175 -x, --force-directories force creation of directories.\n\
176 -nH, --no-host-directories don\'t create host directories.\n\
177 -P, --directory-prefix=PREFIX save files to PREFIX/...\n\
178 --cut-dirs=NUMBER ignore NUMBER remote directory components.\n\
182 --http-user=USER set http user to USER.\n\
183 --http-passwd=PASS set http password to PASS.\n\
184 -C, --cache=on/off (dis)allow server-cached data (normally allowed).\n\
185 -E, --html-extension save all text/html documents with .html extension.\n\
186 --ignore-length ignore `Content-Length\' header field.\n\
187 --header=STRING insert STRING among the headers.\n\
188 --proxy-user=USER set USER as proxy username.\n\
189 --proxy-passwd=PASS set PASS as proxy password.\n\
190 --referer=URL include `Referer: URL\' header in HTTP request.\n\
191 -s, --save-headers save the HTTP headers to file.\n\
192 -U, --user-agent=AGENT identify as AGENT instead of Wget/VERSION.\n\
193 --no-http-keep-alive disable HTTP keep-alive (persistent connections).\n\
194 --cookies=off don't use cookies.\n\
195 --load-cookies=FILE load cookies from FILE before session.\n\
196 --save-cookies=FILE save cookies to FILE after session.\n\
200 -nr, --dont-remove-listing don\'t remove `.listing\' files.\n\
201 -g, --glob=on/off turn file name globbing on or off.\n\
202 --passive-ftp use the \"passive\" transfer mode.\n\
203 --retr-symlinks when recursing, get linked-to files (not dirs).\n\
206 Recursive retrieval:\n\
207 -r, --recursive recursive web-suck -- use with care!\n\
208 -l, --level=NUMBER maximum recursion depth (inf or 0 for infinite).\n\
209 --delete-after delete files locally after downloading them.\n\
210 -k, --convert-links convert non-relative links to relative.\n\
211 -K, --backup-converted before converting file X, back up as X.orig.\n\
212 -m, --mirror shortcut option equivalent to -r -N -l inf -nr.\n\
213 -p, --page-requisites get all images, etc. needed to display HTML page.\n\
216 Recursive accept/reject:\n\
217 -A, --accept=LIST comma-separated list of accepted extensions.\n\
218 -R, --reject=LIST comma-separated list of rejected extensions.\n\
219 -D, --domains=LIST comma-separated list of accepted domains.\n\
220 --exclude-domains=LIST comma-separated list of rejected domains.\n\
221 --follow-ftp follow FTP links from HTML documents.\n\
222 --follow-tags=LIST comma-separated list of followed HTML tags.\n\
223 -G, --ignore-tags=LIST comma-separated list of ignored HTML tags.\n\
224 -H, --span-hosts go to foreign hosts when recursive.\n\
225 -L, --relative follow relative links only.\n\
226 -I, --include-directories=LIST list of allowed directories.\n\
227 -X, --exclude-directories=LIST list of excluded directories.\n\
228 -np, --no-parent don\'t ascend to the parent directory.\n\
230 fputs (_("Mail bug reports and suggestions to <bug-wget@gnu.org>.\n"),
235 main (int argc, char *const *argv)
238 int i, c, nurl, status, append_to_log;
240 static struct option long_options[] =
242 /* Options without arguments: */
243 { "background", no_argument, NULL, 'b' },
244 { "backup-converted", no_argument, NULL, 'K' },
245 { "continue", no_argument, NULL, 'c' },
246 { "convert-links", no_argument, NULL, 'k' },
247 { "debug", no_argument, NULL, 'd' },
248 { "delete-after", no_argument, NULL, 136 },
249 { "dont-remove-listing", no_argument, NULL, 149 },
250 { "follow-ftp", no_argument, NULL, 142 },
251 { "force-directories", no_argument, NULL, 'x' },
252 { "force-hier", no_argument, NULL, 'x' }, /* obsolete */
253 { "force-html", no_argument, NULL, 'F'},
254 { "help", no_argument, NULL, 'h' },
255 { "html-extension", no_argument, NULL, 'E' },
256 { "ignore-length", no_argument, NULL, 138 },
257 { "mirror", no_argument, NULL, 'm' },
258 { "no-clobber", no_argument, NULL, 141 },
259 { "no-directories", no_argument, NULL, 147 },
260 { "no-host-directories", no_argument, NULL, 148 },
261 { "no-host-lookup", no_argument, NULL, 150 },
262 { "no-http-keep-alive", no_argument, NULL, 156 },
263 { "no-parent", no_argument, NULL, 133 },
264 { "non-verbose", no_argument, NULL, 146 },
265 { "passive-ftp", no_argument, NULL, 139 },
266 { "page-requisites", no_argument, NULL, 'p' },
267 { "quiet", no_argument, NULL, 'q' },
268 { "random-wait", no_argument, NULL, 165 },
269 { "recursive", no_argument, NULL, 'r' },
270 { "relative", no_argument, NULL, 'L' },
271 { "retr-symlinks", no_argument, NULL, 137 },
272 { "save-headers", no_argument, NULL, 's' },
273 { "server-response", no_argument, NULL, 'S' },
274 { "span-hosts", no_argument, NULL, 'H' },
275 { "spider", no_argument, NULL, 132 },
276 { "timestamping", no_argument, NULL, 'N' },
277 { "verbose", no_argument, NULL, 'v' },
278 { "version", no_argument, NULL, 'V' },
280 /* Options accepting an argument: */
281 { "accept", required_argument, NULL, 'A' },
282 { "append-output", required_argument, NULL, 'a' },
283 { "backups", required_argument, NULL, 151 }, /* undocumented */
284 { "base", required_argument, NULL, 'B' },
285 { "bind-address", required_argument, NULL, 155 },
286 { "cache", required_argument, NULL, 'C' },
287 { "cookies", required_argument, NULL, 160 },
288 { "cut-dirs", required_argument, NULL, 145 },
289 { "directory-prefix", required_argument, NULL, 'P' },
290 { "domains", required_argument, NULL, 'D' },
291 { "dot-style", required_argument, NULL, 134 },
292 { "execute", required_argument, NULL, 'e' },
293 { "exclude-directories", required_argument, NULL, 'X' },
294 { "exclude-domains", required_argument, NULL, 140 },
295 { "follow-tags", required_argument, NULL, 153 },
296 { "glob", required_argument, NULL, 'g' },
297 { "header", required_argument, NULL, 131 },
298 { "htmlify", required_argument, NULL, 135 },
299 { "http-passwd", required_argument, NULL, 130 },
300 { "http-user", required_argument, NULL, 129 },
301 { "ignore-tags", required_argument, NULL, 'G' },
302 { "include-directories", required_argument, NULL, 'I' },
303 { "input-file", required_argument, NULL, 'i' },
304 { "level", required_argument, NULL, 'l' },
305 { "limit-rate", required_argument, NULL, 164 },
306 { "load-cookies", required_argument, NULL, 161 },
307 { "no", required_argument, NULL, 'n' },
308 { "output-document", required_argument, NULL, 'O' },
309 { "output-file", required_argument, NULL, 'o' },
310 { "progress", required_argument, NULL, 163 },
311 { "proxy", required_argument, NULL, 'Y' },
312 { "proxy-passwd", required_argument, NULL, 144 },
313 { "proxy-user", required_argument, NULL, 143 },
314 { "quota", required_argument, NULL, 'Q' },
315 { "reject", required_argument, NULL, 'R' },
316 { "save-cookies", required_argument, NULL, 162 },
317 { "timeout", required_argument, NULL, 'T' },
318 { "tries", required_argument, NULL, 't' },
319 { "user-agent", required_argument, NULL, 'U' },
320 { "referer", required_argument, NULL, 157 },
321 { "use-proxy", required_argument, NULL, 'Y' },
323 { "sslcertfile", required_argument, NULL, 158 },
324 { "sslcertkey", required_argument, NULL, 159 },
325 { "egd-file", required_argument, NULL, 166 },
326 #endif /* HAVE_SSL */
327 { "wait", required_argument, NULL, 'w' },
328 { "waitretry", required_argument, NULL, 152 },
336 /* Construct the name of the executable, without the directory part. */
337 exec_name = strrchr (argv[0], PATH_SEPARATOR);
344 windows_main_junk (&argc, (char **) argv, (char **) &exec_name);
347 initialize (); /* sets option defaults; reads the system wgetrc and .wgetrc */
349 /* [Is the order of the option letters significant? If not, they should be
350 alphabetized, like the long_options. The only thing I know for sure is
351 that the options with required arguments must be followed by a ':'.
352 -- Dan Harkless <wget@harkless.org>] */
353 while ((c = getopt_long (argc, argv, "\
354 hpVqvdkKsxmNWrHSLcFbEY:G:g:T:U:O:l:n:i:o:a:t:D:A:R:P:B:e:Q:X:I:w:C:",
355 long_options, (int *)0)) != EOF)
359 /* Options without arguments: */
361 setval ("spider", "on");
364 setval ("noparent", "on");
367 setval ("deleteafter", "on");
370 setval ("retrsymlinks", "on");
373 setval ("ignorelength", "on");
376 setval ("passiveftp", "on");
379 setval ("noclobber", "on");
382 setval ("followftp", "on");
385 setval ("cutdirs", optarg);
388 setval ("verbose", "off");
391 setval ("dirstruct", "off");
394 setval ("addhostdir", "off");
397 setval ("removelisting", "off");
400 setval ("bindaddress", optarg);
403 setval ("httpkeepalive", "off");
406 setval ("randomwait", "on");
409 setval ("background", "on");
412 setval ("continue", "on");
416 setval ("debug", "on");
417 #else /* not DEBUG */
418 fprintf (stderr, _("%s: debug support not compiled in.\n"),
420 #endif /* not DEBUG */
423 setval ("htmlextension", "on");
426 setval ("forcehtml", "on");
429 setval ("spanhosts", "on");
439 setval ("backupconverted", "on");
442 setval ("convertlinks", "on");
445 setval ("relativeonly", "on");
448 setval ("mirror", "on");
451 setval ("timestamping", "on");
454 setval ("pagerequisites", "on");
457 setval ("serverresponse", "on");
460 setval ("saveheaders", "on");
463 setval ("quiet", "on");
466 setval ("recursive", "on");
469 printf ("GNU Wget %s\n\n", version_string);
471 Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001 Free Software Foundation, Inc.\n"));
473 This program is distributed in the hope that it will be useful,\n\
474 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
475 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
476 GNU General Public License for more details.\n"));
477 printf (_("\nOriginally written by Hrvoje Niksic <hniksic@arsdigita.com>.\n"));
481 setval ("verbose", "on");
484 setval ("dirstruct", "on");
487 /* Options accepting an argument: */
489 setval ("httpuser", optarg);
492 setval ("httppasswd", optarg);
495 setval ("header", optarg);
498 setval ("dotstyle", optarg);
501 setval ("htmlify", optarg);
504 setval ("excludedomains", optarg);
507 setval ("proxyuser", optarg);
510 setval ("proxypasswd", optarg);
513 setval ("backups", optarg);
516 setval ("waitretry", optarg);
519 setval ("followtags", optarg);
522 setval ("cookies", optarg);
525 setval ("loadcookies", optarg);
528 setval ("savecookies", optarg);
531 setval ("progress", optarg);
534 setval ("limitrate", optarg);
537 setval ("referer", optarg);
541 setval ("sslcertfile", optarg);
544 setval ("sslcertkey", optarg);
547 setval ("egdfile", optarg);
549 #endif /* HAVE_SSL */
551 setval ("accept", optarg);
554 setval ("logfile", optarg);
558 setval ("base", optarg);
561 setval ("cache", optarg);
564 setval ("domains", optarg);
569 if (parse_line (optarg, &com, &val))
571 if (!setval (com, val))
576 fprintf (stderr, _("%s: %s: invalid command\n"), exec_name,
585 setval ("ignoretags", optarg);
588 setval ("glob", optarg);
591 setval ("includedirectories", optarg);
594 setval ("input", optarg);
597 setval ("reclevel", optarg);
601 /* #### What we really want here is --no-foo. */
604 for (p = optarg; *p; p++)
608 setval ("verbose", "off");
611 setval ("addhostdir", "off");
614 setval ("dirstruct", "off");
617 setval ("noclobber", "on");
620 setval ("removelisting", "off");
623 setval ("noparent", "on");
626 setval ("httpkeepalive", "off");
629 printf (_("%s: illegal option -- `-n%c'\n"), exec_name, *p);
632 printf (_("Try `%s --help\' for more options.\n"), exec_name);
638 setval ("outputdocument", optarg);
641 setval ("logfile", optarg);
644 setval ("dirprefix", optarg);
647 setval ("quota", optarg);
650 setval ("reject", optarg);
653 setval ("timeout", optarg);
656 setval ("tries", optarg);
659 setval ("useragent", optarg);
662 setval ("wait", optarg);
665 setval ("excludedirectories", optarg);
668 setval ("useproxy", optarg);
674 printf (_("Try `%s --help' for more options.\n"), exec_name);
680 /* All user options have now been processed, so it's now safe to do
681 interoption dependency checks. */
683 if (opt.reclevel == 0)
684 opt.reclevel = INFINITE_RECURSION; /* see wget.h for commentary on this */
686 if (opt.page_requisites && !opt.recursive)
688 opt.recursive = TRUE;
690 if (!opt.no_dirstruct)
691 opt.dirstruct = TRUE; /* usually handled by cmd_spec_recursive() */
694 if (opt.verbose == -1)
695 opt.verbose = !opt.quiet;
698 if (opt.verbose && opt.quiet)
700 printf (_("Can't be verbose and quiet at the same time.\n"));
704 if (opt.timestamping && opt.noclobber)
707 Can't timestamp and not clobber old files at the same time.\n"));
711 nurl = argc - optind;
712 if (!nurl && !opt.input_filename)
714 /* No URL specified. */
715 printf (_("%s: missing URL\n"), exec_name);
718 /* #### Something nicer should be printed here -- similar to the
719 pre-1.5 `--help' page. */
720 printf (_("Try `%s --help' for more options.\n"), exec_name);
725 fork_to_background ();
727 /* Initialize progress. Have to do this after the options are
728 processed so we know where the log file is. */
730 set_progress_implementation (opt.progress_type);
732 /* Allocate basic pointer. */
733 url = ALLOCA_ARRAY (char *, nurl + 1);
734 /* Fill in the arguments. */
735 for (i = 0; i < nurl; i++, optind++)
737 char *rewritten = rewrite_shorthand_url (argv[optind]);
741 url[i] = xstrdup (argv[optind]);
745 /* Change the title of console window on Windows. #### I think this
746 statement should belong to retrieve_url(). --hniksic. */
748 ws_changetitle (*url, nurl);
751 /* Initialize logging. */
752 log_init (opt.lfilename, append_to_log);
754 DEBUGP (("DEBUG output created by Wget %s on %s.\n\n", version_string,
757 /* Open the output filename if necessary. */
758 if (opt.output_document)
760 if (HYPHENP (opt.output_document))
765 opt.dfp = fopen (opt.output_document, opt.always_rest ? "ab" : "wb");
768 perror (opt.output_document);
771 if (fstat (fileno (opt.dfp), &st) == 0 && S_ISREG (st.st_mode))
772 opt.od_known_regular = 1;
780 /* Setup the signal handler to redirect output when hangup is
783 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
784 signal(SIGHUP, redirect_output_signal);
785 /* ...and do the same for SIGUSR1. */
786 signal (SIGUSR1, redirect_output_signal);
787 /* Writing to a closed socket normally signals SIGPIPE, and the
788 process exits. What we want is to ignore SIGPIPE and just check
789 for the return value of write(). */
790 signal (SIGPIPE, SIG_IGN);
792 signal (SIGWINCH, progress_handle_sigwinch);
794 #endif /* HAVE_SIGNAL */
797 /* Must call this before resolving any URLs because it has the power
798 to disable `https'. */
802 status = RETROK; /* initialize it, just-in-case */
803 /* Retrieve the URLs from argument list. */
804 for (t = url; *t; t++)
806 char *filename = NULL, *redirected_URL = NULL;
809 if (opt.recursive && url_scheme (*t) != SCHEME_FTP)
810 status = retrieve_tree (*t);
812 status = retrieve_url (*t, &filename, &redirected_URL, NULL, &dt);
814 if (opt.delete_after && file_exists_p(filename))
816 DEBUGP (("Removing file due to --delete-after in main():\n"));
817 logprintf (LOG_VERBOSE, _("Removing %s.\n"), filename);
818 if (unlink (filename))
819 logprintf (LOG_NOTQUIET, "unlink: %s\n", strerror (errno));
822 FREE_MAYBE (redirected_URL);
823 FREE_MAYBE (filename);
826 /* And then from the input file, if any. */
827 if (opt.input_filename)
830 status = retrieve_from_file (opt.input_filename, opt.force_html, &count);
832 logprintf (LOG_NOTQUIET, _("No URLs found in %s.\n"),
835 /* Print the downloaded sum. */
838 || (opt.input_filename && opt.downloaded != 0))
840 logprintf (LOG_NOTQUIET,
841 _("\nFINISHED --%s--\nDownloaded: %s bytes in %d files\n"),
843 (opt.downloaded_overflow ?
844 "<overflow>" : legible_very_long (opt.downloaded)),
846 /* Print quota warning, if exceeded. */
847 if (downloaded_exceeds_quota ())
848 logprintf (LOG_NOTQUIET,
849 _("Download quota (%s bytes) EXCEEDED!\n"),
850 legible (opt.quota));
853 if (opt.cookies_output)
854 save_cookies (opt.cookies_output);
856 if (opt.convert_links && !opt.delete_after)
858 convert_all_links ();
862 for (i = 0; i < nurl; i++)
867 print_malloc_debug_stats ();
869 if (status == RETROK)
876 /* Hangup signal handler. When wget receives SIGHUP or SIGUSR1, it
877 will proceed operation as usual, trying to write into a log file.
878 If that is impossible, the output will be turned off.
880 #### It is unsafe to do call libc functions from a signal handler.
881 What we should do is, set a global variable, and have the code in
885 redirect_output_signal (int sig)
887 char *signal_name = (sig == SIGHUP ? "SIGHUP" :
888 (sig == SIGUSR1 ? "SIGUSR1" :
890 log_request_redirect_output (signal_name);
891 progress_schedule_redirect ();
892 signal (sig, redirect_output_signal);
894 #endif /* HAVE_SIGNAL */