]> sjero.net Git - wget/blob - src/main.c
[svn] Enabled separation of different timeout values.
[wget] / src / main.c
1 /* Command line parsing.
2    Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001, 2002
3    Free Software Foundation, Inc.
4
5 This file is part of GNU Wget.
6
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.
11
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.
16
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.
20
21 In addition, as a special exception, the Free Software Foundation
22 gives permission to link the code of its release of Wget with the
23 OpenSSL project's "OpenSSL" library (or with modified versions of it
24 that use the same license as the "OpenSSL" library), and distribute
25 the linked executables.  You must obey the GNU General Public License
26 in all respects for all of the code used other than "OpenSSL".  If you
27 modify this file, you may extend this exception to your version of the
28 file, but you are not obligated to do so.  If you do not wish to do
29 so, delete this exception statement from your version.  */
30
31 #include <config.h>
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif /* HAVE_UNISTD_H */
38 #include <sys/types.h>
39 #ifdef HAVE_STRING_H
40 # include <string.h>
41 #else
42 # include <strings.h>
43 #endif /* HAVE_STRING_H */
44 #ifdef HAVE_SIGNAL_H
45 # include <signal.h>
46 #endif
47 #ifdef HAVE_NLS
48 #ifdef HAVE_LOCALE_H
49 # include <locale.h>
50 #endif /* HAVE_LOCALE_H */
51 #endif /* HAVE_NLS */
52 #include <errno.h>
53
54 #include "wget.h"
55 #include "utils.h"
56 #include "init.h"
57 #include "retr.h"
58 #include "recur.h"
59 #include "host.h"
60 #include "cookies.h"
61 #include "url.h"
62 #include "progress.h"           /* for progress_handle_sigwinch */
63
64 #ifdef HAVE_SSL
65 # include "gen_sslfunc.h"
66 #endif
67
68 /* On GNU system this will include system-wide getopt.h. */
69 #include "getopt.h"
70
71 #ifndef PATH_SEPARATOR
72 # define PATH_SEPARATOR '/'
73 #endif
74
75 extern char *version_string;
76
77 #ifndef errno
78 extern int errno;
79 #endif
80
81 struct options opt;
82
83 extern struct cookie_jar *wget_cookie_jar;
84
85 /* From log.c.  */
86 void log_init PARAMS ((const char *, int));
87 void log_close PARAMS ((void));
88 void log_request_redirect_output PARAMS ((const char *));
89
90 static RETSIGTYPE redirect_output_signal PARAMS ((int));
91
92 const char *exec_name;
93 \f
94 /* Initialize I18N.  The initialization amounts to invoking
95    setlocale(), bindtextdomain() and textdomain().
96    Does nothing if NLS is disabled or missing.  */
97 static void
98 i18n_initialize (void)
99 {
100   /* If HAVE_NLS is defined, assume the existence of the three
101      functions invoked here.  */
102 #ifdef HAVE_NLS
103   /* Set the current locale.  */
104   /* Here we use LC_MESSAGES instead of LC_ALL, for two reasons.
105      First, message catalogs are all of I18N Wget uses anyway.
106      Second, setting LC_ALL has a dangerous potential of messing
107      things up.  For example, when in a foreign locale, Solaris
108      strptime() fails to handle international dates correctly, which
109      makes http_atotm() malfunction.  */
110 #ifdef LC_MESSAGES
111   setlocale (LC_MESSAGES, "");
112   setlocale (LC_CTYPE, "");
113 #else
114   setlocale (LC_ALL, "");
115 #endif
116   /* Set the text message domain.  */
117   bindtextdomain ("wget", LOCALEDIR);
118   textdomain ("wget");
119 #endif /* HAVE_NLS */
120 }
121 \f
122 /* Print the usage message.  */
123 static void
124 print_usage (void)
125 {
126   printf (_("Usage: %s [OPTION]... [URL]...\n"), exec_name);
127 }
128
129 /* Print the help message, describing all the available options.  If
130    you add an option, be sure to update this list.  */
131 static void
132 print_help (void)
133 {
134   printf (_("GNU Wget %s, a non-interactive network retriever.\n"),
135           version_string);
136   print_usage ();
137   /* Had to split this in parts, so the #@@#%# Ultrix compiler and cpp
138      don't bitch.  Also, it makes translation much easier.  */
139   fputs (_("\
140 \n\
141 Mandatory arguments to long options are mandatory for short options too.\n\
142 \n"), stdout);
143   fputs (_("\
144 Startup:\n\
145   -V,  --version           display the version of Wget and exit.\n\
146   -h,  --help              print this help.\n\
147   -b,  --background        go to background after startup.\n\
148   -e,  --execute=COMMAND   execute a `.wgetrc\'-style command.\n\
149 \n"), stdout);
150   fputs (_("\
151 Logging and input file:\n\
152   -o,  --output-file=FILE     log messages to FILE.\n\
153   -a,  --append-output=FILE   append messages to FILE.\n\
154   -d,  --debug                print debug output.\n\
155   -q,  --quiet                quiet (no output).\n\
156   -v,  --verbose              be verbose (this is the default).\n\
157   -nv, --non-verbose          turn off verboseness, without being quiet.\n\
158   -i,  --input-file=FILE      download URLs found in FILE.\n\
159   -F,  --force-html           treat input file as HTML.\n\
160   -B,  --base=URL             prepends URL to relative links in -F -i file.\n\
161 \n"),stdout);
162   fputs (_("\
163 Download:\n\
164   -t,  --tries=NUMBER           set number of retries to NUMBER (0 unlimits).\n\
165        --retry-connrefused      retry even if connection is refused.\n\
166   -O   --output-document=FILE   write documents to FILE.\n\
167   -nc, --no-clobber             don\'t clobber existing files or use .# suffixes.\n\
168   -c,  --continue               resume getting a partially-downloaded file.\n\
169        --progress=TYPE          select progress gauge type.\n\
170   -N,  --timestamping           don\'t re-retrieve files unless newer than local.\n\
171   -S,  --server-response        print server response.\n\
172        --spider                 don\'t download anything.\n\
173   -T,  --timeout=SECONDS        set all timeout values to SECONDS.\n\
174        --dns-timeout=SECS       set the DNS lookup timeout to SECS.\n\
175        --connect-timeout=SECS   set the connect timeout to SECS.\n\
176        --read-timeout=SECS      set the read timeout to SECS.\n\
177   -w,  --wait=SECONDS           wait SECONDS between retrievals.\n\
178        --waitretry=SECONDS      wait 1...SECONDS between retries of a retrieval.\n\
179        --random-wait            wait from 0...2*WAIT secs between retrievals.\n\
180   -Y,  --proxy=on/off           turn proxy on or off.\n\
181   -Q,  --quota=NUMBER           set retrieval quota to NUMBER.\n\
182        --bind-address=ADDRESS   bind to ADDRESS (hostname or IP) on local host.\n\
183        --limit-rate=RATE        limit download rate to RATE.\n\
184        --dns-cache=off          disable caching DNS lookups.\n\
185        --restrict-file-names=OS restrict chars in file names to ones OS allows.\n\
186 \n"), stdout);
187   fputs (_("\
188 Directories:\n\
189   -nd, --no-directories            don\'t create directories.\n\
190   -x,  --force-directories         force creation of directories.\n\
191   -nH, --no-host-directories       don\'t create host directories.\n\
192   -P,  --directory-prefix=PREFIX   save files to PREFIX/...\n\
193        --cut-dirs=NUMBER           ignore NUMBER remote directory components.\n\
194 \n"), stdout);
195   fputs (_("\
196 HTTP options:\n\
197        --http-user=USER      set http user to USER.\n\
198        --http-passwd=PASS    set http password to PASS.\n\
199   -C,  --cache=on/off        (dis)allow server-cached data (normally allowed).\n\
200   -E,  --html-extension      save all text/html documents with .html extension.\n\
201        --ignore-length       ignore `Content-Length\' header field.\n\
202        --header=STRING       insert STRING among the headers.\n\
203        --proxy-user=USER     set USER as proxy username.\n\
204        --proxy-passwd=PASS   set PASS as proxy password.\n\
205        --referer=URL         include `Referer: URL\' header in HTTP request.\n\
206   -s,  --save-headers        save the HTTP headers to file.\n\
207   -U,  --user-agent=AGENT    identify as AGENT instead of Wget/VERSION.\n\
208        --no-http-keep-alive  disable HTTP keep-alive (persistent connections).\n\
209        --cookies=off         don't use cookies.\n\
210        --load-cookies=FILE   load cookies from FILE before session.\n\
211        --save-cookies=FILE   save cookies to FILE after session.\n\
212        --post-data=STRING    use the POST method; send STRING as the data.\n\
213        --post-file=FILE      use the POST method; send contents of FILE.\n\
214 \n"), stdout);
215 #ifdef HAVE_SSL
216   fputs (_("\
217 HTTPS (SSL) options:\n\
218        --sslcertfile=FILE     optional client certificate.\n\
219        --sslcertkey=KEYFILE   optional keyfile for this certificate.\n\
220        --egd-file=FILE        file name of the EGD socket.\n\
221        --sslcadir=DIR         dir where hash list of CA's are stured.\n\
222        --sslcafile=FILE       file with bundle of CA's\n\
223        --sslcerttype=0/1      Client-Cert type 0=PEM (default) / 1=ASN1 (DER)\n\
224        --sslcheckcert=0/1     Check the server cert agenst given CA\n\
225        --sslprotocol=0-3      choose SSL protocol; 0=automatic,\n\
226                               1=SSLv2 2=SSLv3 3=TLSv1\n\
227 \n"), stdout);
228 #endif
229   fputs (_("\
230 FTP options:\n\
231   -nr, --dont-remove-listing   don\'t remove `.listing\' files.\n\
232   -g,  --glob=on/off           turn file name globbing on or off.\n\
233        --passive-ftp           use the \"passive\" transfer mode.\n\
234        --retr-symlinks         when recursing, get linked-to files (not dirs).\n\
235 \n"), stdout);
236   fputs (_("\
237 Recursive retrieval:\n\
238   -r,  --recursive          recursive download.\n\
239   -l,  --level=NUMBER       maximum recursion depth (inf or 0 for infinite).\n\
240        --delete-after       delete files locally after downloading them.\n\
241   -k,  --convert-links      convert non-relative links to relative.\n\
242   -K,  --backup-converted   before converting file X, back up as X.orig.\n\
243   -m,  --mirror             shortcut option equivalent to -r -N -l inf -nr.\n\
244   -p,  --page-requisites    get all images, etc. needed to display HTML page.\n\
245        --strict-comments    turn on strict (SGML) handling of HTML comments.\n\
246 \n"), stdout);
247   fputs (_("\
248 Recursive accept/reject:\n\
249   -A,  --accept=LIST                comma-separated list of accepted extensions.\n\
250   -R,  --reject=LIST                comma-separated list of rejected extensions.\n\
251   -D,  --domains=LIST               comma-separated list of accepted domains.\n\
252        --exclude-domains=LIST       comma-separated list of rejected domains.\n\
253        --follow-ftp                 follow FTP links from HTML documents.\n\
254        --follow-tags=LIST           comma-separated list of followed HTML tags.\n\
255   -G,  --ignore-tags=LIST           comma-separated list of ignored HTML tags.\n\
256   -H,  --span-hosts                 go to foreign hosts when recursive.\n\
257   -L,  --relative                   follow relative links only.\n\
258   -I,  --include-directories=LIST   list of allowed directories.\n\
259   -X,  --exclude-directories=LIST   list of excluded directories.\n\
260   -np, --no-parent                  don\'t ascend to the parent directory.\n\
261 \n"), stdout);
262   fputs (_("Mail bug reports and suggestions to <bug-wget@gnu.org>.\n"),
263          stdout);
264 }
265 \f
266 int
267 main (int argc, char *const *argv)
268 {
269   char **url, **t;
270   int i, c, nurl, status, append_to_log;
271
272   static struct option long_options[] =
273   {
274     /* Options without arguments: */
275     { "background", no_argument, NULL, 'b' },
276     { "backup-converted", no_argument, NULL, 'K' },
277     { "continue", no_argument, NULL, 'c' },
278     { "convert-links", no_argument, NULL, 'k' },
279     { "debug", no_argument, NULL, 'd' },
280     { "delete-after", no_argument, NULL, 136 },
281     { "dont-remove-listing", no_argument, NULL, 149 },
282     { "follow-ftp", no_argument, NULL, 142 },
283     { "force-directories", no_argument, NULL, 'x' },
284     { "force-hier", no_argument, NULL, 'x' }, /* obsolete */
285     { "force-html", no_argument, NULL, 'F'},
286     { "help", no_argument, NULL, 'h' },
287     { "html-extension", no_argument, NULL, 'E' },
288     { "ignore-length", no_argument, NULL, 138 },
289     { "mirror", no_argument, NULL, 'm' },
290     { "no-clobber", no_argument, NULL, 141 },
291     { "no-directories", no_argument, NULL, 147 },
292     { "no-host-directories", no_argument, NULL, 148 },
293     { "no-host-lookup", no_argument, NULL, 150 },
294     { "no-http-keep-alive", no_argument, NULL, 156 },
295     { "no-parent", no_argument, NULL, 133 },
296     { "non-verbose", no_argument, NULL, 146 },
297     { "passive-ftp", no_argument, NULL, 139 },
298     { "page-requisites", no_argument, NULL, 'p' },
299     { "quiet", no_argument, NULL, 'q' },
300     { "random-wait", no_argument, NULL, 165 },
301     { "recursive", no_argument, NULL, 'r' },
302     { "relative", no_argument, NULL, 'L' },
303     { "retr-symlinks", no_argument, NULL, 137 },
304     { "retry-connrefused", no_argument, NULL, 174 },
305     { "save-headers", no_argument, NULL, 's' },
306     { "server-response", no_argument, NULL, 'S' },
307     { "span-hosts", no_argument, NULL, 'H' },
308     { "spider", no_argument, NULL, 132 },
309     { "strict-comments", no_argument, NULL, 177 },
310     { "timestamping", no_argument, NULL, 'N' },
311     { "verbose", no_argument, NULL, 'v' },
312     { "version", no_argument, NULL, 'V' },
313
314     /* Options accepting an argument: */
315     { "accept", required_argument, NULL, 'A' },
316     { "append-output", required_argument, NULL, 'a' },
317     { "backups", required_argument, NULL, 151 }, /* undocumented */
318     { "base", required_argument, NULL, 'B' },
319     { "bind-address", required_argument, NULL, 155 },
320     { "cache", required_argument, NULL, 'C' },
321     { "connect-timeout", required_argument, NULL, 180 },
322     { "cookies", required_argument, NULL, 160 },
323     { "cut-dirs", required_argument, NULL, 145 },
324     { "dns-timeout", required_argument, NULL, 178 },
325     { "directory-prefix", required_argument, NULL, 'P' },
326     { "dns-cache", required_argument, NULL, 175 },
327     { "domains", required_argument, NULL, 'D' },
328     { "dot-style", required_argument, NULL, 134 },
329     { "execute", required_argument, NULL, 'e' },
330     { "exclude-directories", required_argument, NULL, 'X' },
331     { "exclude-domains", required_argument, NULL, 140 },
332     { "follow-tags", required_argument, NULL, 153 },
333     { "glob", required_argument, NULL, 'g' },
334     { "header", required_argument, NULL, 131 },
335     { "htmlify", required_argument, NULL, 135 },
336     { "http-passwd", required_argument, NULL, 130 },
337     { "http-user", required_argument, NULL, 129 },
338     { "ignore-tags", required_argument, NULL, 'G' },
339     { "include-directories", required_argument, NULL, 'I' },
340     { "input-file", required_argument, NULL, 'i' },
341     { "level", required_argument, NULL, 'l' },
342     { "limit-rate", required_argument, NULL, 164 },
343     { "load-cookies", required_argument, NULL, 161 },
344     { "no", required_argument, NULL, 'n' },
345     { "output-document", required_argument, NULL, 'O' },
346     { "output-file", required_argument, NULL, 'o' },
347     { "post-data", required_argument, NULL, 167 },
348     { "post-file", required_argument, NULL, 168 },
349     { "progress", required_argument, NULL, 163 },
350     { "proxy", required_argument, NULL, 'Y' },
351     { "proxy-passwd", required_argument, NULL, 144 },
352     { "proxy-user", required_argument, NULL, 143 },
353     { "quota", required_argument, NULL, 'Q' },
354     { "read-timeout", required_argument, NULL, 179 },
355     { "reject", required_argument, NULL, 'R' },
356     { "restrict-file-names", required_argument, NULL, 176 },
357     { "save-cookies", required_argument, NULL, 162 },
358     { "timeout", required_argument, NULL, 'T' },
359     { "tries", required_argument, NULL, 't' },
360     { "user-agent", required_argument, NULL, 'U' },
361     { "referer", required_argument, NULL, 157 },
362     { "use-proxy", required_argument, NULL, 'Y' },
363 #ifdef HAVE_SSL
364     { "sslcertfile", required_argument, NULL, 158 },
365     { "sslcertkey", required_argument, NULL, 159 },
366     { "egd-file", required_argument, NULL, 166 },
367     { "sslcadir",         required_argument, NULL, 169},
368     { "sslcafile",        required_argument, NULL, 170},
369     { "sslcerttype",      required_argument, NULL, 171},
370     { "sslcheckcert",     required_argument, NULL, 172},
371     { "sslprotocol",      required_argument, NULL, 173},
372 #endif /* HAVE_SSL */
373     { "wait", required_argument, NULL, 'w' },
374     { "waitretry", required_argument, NULL, 152 },
375     { 0, 0, 0, 0 }
376   };
377
378   i18n_initialize ();
379
380   append_to_log = 0;
381
382   /* Construct the name of the executable, without the directory part.  */
383   exec_name = strrchr (argv[0], PATH_SEPARATOR);
384   if (!exec_name)
385     exec_name = argv[0];
386   else
387     ++exec_name;
388
389 #ifdef WINDOWS
390   windows_main_junk (&argc, (char **) argv, (char **) &exec_name);
391 #endif
392
393   initialize (); /* sets option defaults; reads the system wgetrc and .wgetrc */
394
395   /* [Is the order of the option letters significant?  If not, they should be
396       alphabetized, like the long_options.  The only thing I know for sure is
397       that the options with required arguments must be followed by a ':'.
398       -- Dan Harkless <wget@harkless.org>] */
399   while ((c = getopt_long (argc, argv, "\
400 hpVqvdkKsxmNWrHSLcFbEY:G:g:T:U:O:l:n:i:o:a:t:D:A:R:P:B:e:Q:X:I:w:C:",
401                            long_options, (int *)0)) != EOF)
402     {
403       switch (c)
404         {
405           /* Options without arguments: */
406         case 132:
407           setoptval ("spider", "on");
408           break;
409         case 133:
410           setoptval ("noparent", "on");
411           break;
412         case 136:
413           setoptval ("deleteafter", "on");
414           break;
415         case 137:
416           setoptval ("retrsymlinks", "on");
417           break;
418         case 138:
419           setoptval ("ignorelength", "on");
420           break;
421         case 139:
422           setoptval ("passiveftp", "on");
423           break;
424         case 141:
425           setoptval ("noclobber", "on");
426           break;
427         case 142:
428           setoptval ("followftp", "on");
429           break;
430         case 145:
431           setoptval ("cutdirs", optarg);
432           break;
433         case 146:
434           setoptval ("verbose", "off");
435           break;
436         case 147:
437           setoptval ("dirstruct", "off");
438           break;
439         case 148:
440           setoptval ("addhostdir", "off");
441           break;
442         case 149:
443           setoptval ("removelisting", "off");
444           break;
445         case 155:
446           setoptval ("bindaddress", optarg);
447           break;
448         case 156:
449           setoptval ("httpkeepalive", "off");
450           break;
451         case 165:
452           setoptval ("randomwait", "on");
453           break;
454         case 'b':
455           setoptval ("background", "on");
456           break;
457         case 'c':
458           setoptval ("continue", "on");
459           break;
460         case 'd':
461 #ifdef DEBUG
462           setoptval ("debug", "on");
463 #else  /* not DEBUG */
464           fprintf (stderr, _("%s: debug support not compiled in.\n"),
465                    exec_name);
466 #endif /* not DEBUG */
467           break;
468         case 'E':
469           setoptval ("htmlextension", "on");
470           break;
471         case 'F':
472           setoptval ("forcehtml", "on");
473           break;
474         case 'H':
475           setoptval ("spanhosts", "on");
476           break;
477         case 'h':
478           print_help ();
479 #ifdef WINDOWS
480           ws_help (exec_name);
481 #endif
482           exit (0);
483           break;
484         case 'K':
485           setoptval ("backupconverted", "on");
486           break;
487         case 'k':
488           setoptval ("convertlinks", "on");
489           break;
490         case 'L':
491           setoptval ("relativeonly", "on");
492           break;
493         case 'm':
494           setoptval ("mirror", "on");
495           break;
496         case 'N':
497           setoptval ("timestamping", "on");
498           break;
499         case 'p':
500           setoptval ("pagerequisites", "on");
501           break;
502         case 'S':
503           setoptval ("serverresponse", "on");
504           break;
505         case 's':
506           setoptval ("saveheaders", "on");
507           break;
508         case 'q':
509           setoptval ("quiet", "on");
510           break;
511         case 'r':
512           setoptval ("recursive", "on");
513           break;
514         case 'V':
515           printf ("GNU Wget %s\n\n", version_string);
516           printf ("%s", _("\
517 Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001 Free Software Foundation, Inc.\n"));
518           printf ("%s", _("\
519 This program is distributed in the hope that it will be useful,\n\
520 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
521 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n\
522 GNU General Public License for more details.\n"));
523           printf (_("\nOriginally written by Hrvoje Niksic <hniksic@arsdigita.com>.\n"));
524           exit (0);
525           break;
526         case 'v':
527           setoptval ("verbose", "on");
528           break;
529         case 'x':
530           setoptval ("dirstruct", "on");
531           break;
532         case 174:
533           setoptval ("retryconnrefused", "on");
534           break;
535         case 177:
536           setoptval ("strictcomments", "on");
537           break;
538
539           /* Options accepting an argument: */
540         case 129:
541           setoptval ("httpuser", optarg);
542           break;
543         case 130:
544           setoptval ("httppasswd", optarg);
545           break;
546         case 131:
547           setoptval ("header", optarg);
548           break;
549         case 134:
550           setoptval ("dotstyle", optarg);
551           break;
552         case 135:
553           setoptval ("htmlify", optarg);
554           break;
555         case 140:
556           setoptval ("excludedomains", optarg);
557           break;
558         case 143:
559           setoptval ("proxyuser", optarg);
560           break;
561         case 144:
562           setoptval ("proxypasswd", optarg);
563           break;
564         case 151:
565           setoptval ("backups", optarg);
566           break;
567         case 152:
568           setoptval ("waitretry", optarg);
569           break;
570         case 153:
571           setoptval ("followtags", optarg);
572           break;
573         case 160:
574           setoptval ("cookies", optarg);
575           break;
576         case 161:
577           setoptval ("loadcookies", optarg);
578           break;
579         case 162:
580           setoptval ("savecookies", optarg);
581           break;
582         case 163:
583           setoptval ("progress", optarg);
584           break;
585         case 164:
586           setoptval ("limitrate", optarg);
587           break;
588         case 157:
589           setoptval ("referer", optarg);
590           break;
591 #ifdef HAVE_SSL
592         case 158:
593           setoptval ("sslcertfile", optarg);
594           break;
595         case 159:
596           setoptval ("sslcertkey", optarg);
597           break;
598         case 166:
599           setoptval ("egdfile", optarg);
600           break;
601         case 169:
602           setoptval ("sslcadir", optarg);
603           break;
604         case 170:
605           setoptval ("sslcafile", optarg);
606           break;
607         case 171:
608           setoptval ("sslcerttype", optarg);
609           break;
610         case 172:
611           setoptval ("sslcheckcert", optarg);
612           break;
613         case 173:
614           setoptval ("sslprotocol", optarg);
615           break;
616 #endif /* HAVE_SSL */
617         case 167:
618           setoptval ("postdata", optarg);
619           break;
620         case 168:
621           setoptval ("postfile", optarg);
622           break;
623         case 175:
624           setoptval ("dnscache", optarg);
625           break;
626         case 176:
627           setoptval ("restrictfilenames", optarg);
628           break;
629         case 178:
630           setoptval ("dnstimeout", optarg);
631           break;
632         case 179:
633           setoptval ("readtimeout", optarg);
634           break;
635         case 180:
636           setoptval ("connecttimeout", optarg);
637           break;
638         case 'A':
639           setoptval ("accept", optarg);
640           break;
641         case 'a':
642           setoptval ("logfile", optarg);
643           append_to_log = 1;
644           break;
645         case 'B':
646           setoptval ("base", optarg);
647           break;
648         case 'C':
649           setoptval ("cache", optarg);
650           break;
651         case 'D':
652           setoptval ("domains", optarg);
653           break;
654         case 'e':
655           run_command (optarg);
656           break;
657         case 'G':
658           setoptval ("ignoretags", optarg);
659           break;
660         case 'g':
661           setoptval ("glob", optarg);
662           break;
663         case 'I':
664           setoptval ("includedirectories", optarg);
665           break;
666         case 'i':
667           setoptval ("input", optarg);
668           break;
669         case 'l':
670           setoptval ("reclevel", optarg);
671           break;
672         case 'n':
673           {
674             /* #### What we really want here is --no-foo. */
675             char *p;
676
677             for (p = optarg; *p; p++)
678               switch (*p)
679                 {
680                 case 'v':
681                   setoptval ("verbose", "off");
682                   break;
683                 case 'H':
684                   setoptval ("addhostdir", "off");
685                   break;
686                 case 'd':
687                   setoptval ("dirstruct", "off");
688                   break;
689                 case 'c':
690                   setoptval ("noclobber", "on");
691                   break;
692                 case 'r':
693                   setoptval ("removelisting", "off");
694                   break;
695                 case 'p':
696                   setoptval ("noparent", "on");
697                   break;
698                 case 'k':
699                   setoptval ("httpkeepalive", "off");
700                   break;
701                 default:
702                   printf (_("%s: illegal option -- `-n%c'\n"), exec_name, *p);
703                   print_usage ();
704                   printf ("\n");
705                   printf (_("Try `%s --help\' for more options.\n"), exec_name);
706                   exit (1);
707                 }
708             break;
709           }
710         case 'O':
711           setoptval ("outputdocument", optarg);
712           break;
713         case 'o':
714           setoptval ("logfile", optarg);
715           break;
716         case 'P':
717           setoptval ("dirprefix", optarg);
718           break;
719         case 'Q':
720           setoptval ("quota", optarg);
721           break;
722         case 'R':
723           setoptval ("reject", optarg);
724           break;
725         case 'T':
726           setoptval ("timeout", optarg);
727           break;
728         case 't':
729           setoptval ("tries", optarg);
730           break;
731         case 'U':
732           setoptval ("useragent", optarg);
733           break;
734         case 'w':
735           setoptval ("wait", optarg);
736           break;
737         case 'X':
738           setoptval ("excludedirectories", optarg);
739           break;
740         case 'Y':
741           setoptval ("useproxy", optarg);
742           break;
743
744         case '?':
745           print_usage ();
746           printf ("\n");
747           printf (_("Try `%s --help' for more options.\n"), exec_name);
748           exit (0);
749           break;
750         }
751     }
752
753   /* All user options have now been processed, so it's now safe to do
754      interoption dependency checks. */
755
756   if (opt.reclevel == 0)
757     opt.reclevel = INFINITE_RECURSION;  /* see wget.h for commentary on this */
758
759   if (opt.page_requisites && !opt.recursive)
760     {
761       opt.recursive = TRUE;
762       opt.reclevel = 0;
763       if (!opt.no_dirstruct)
764         opt.dirstruct = TRUE;  /* usually handled by cmd_spec_recursive() */
765     }
766
767   if (opt.verbose == -1)
768     opt.verbose = !opt.quiet;
769
770   /* Sanity checks.  */
771   if (opt.verbose && opt.quiet)
772     {
773       printf (_("Can't be verbose and quiet at the same time.\n"));
774       print_usage ();
775       exit (1);
776     }
777   if (opt.timestamping && opt.noclobber)
778     {
779       printf (_("\
780 Can't timestamp and not clobber old files at the same time.\n"));
781       print_usage ();
782       exit (1);
783     }
784   nurl = argc - optind;
785   if (!nurl && !opt.input_filename)
786     {
787       /* No URL specified.  */
788       printf (_("%s: missing URL\n"), exec_name);
789       print_usage ();
790       printf ("\n");
791       /* #### Something nicer should be printed here -- similar to the
792          pre-1.5 `--help' page.  */
793       printf (_("Try `%s --help' for more options.\n"), exec_name);
794       exit (1);
795     }
796
797   if (opt.background)
798     fork_to_background ();
799
800   /* Initialize progress.  Have to do this after the options are
801      processed so we know where the log file is.  */
802   if (opt.verbose)
803     set_progress_implementation (opt.progress_type);
804
805   /* Allocate basic pointer.  */
806   url = ALLOCA_ARRAY (char *, nurl + 1);
807   /* Fill in the arguments.  */
808   for (i = 0; i < nurl; i++, optind++)
809     {
810       char *rewritten = rewrite_shorthand_url (argv[optind]);
811       if (rewritten)
812         url[i] = rewritten;
813       else
814         url[i] = xstrdup (argv[optind]);
815     }
816   url[i] = NULL;
817
818   /* Change the title of console window on Windows.  #### I think this
819      statement should belong to retrieve_url().  --hniksic.  */
820 #ifdef WINDOWS
821   ws_changetitle (*url, nurl);
822 #endif
823
824   /* Initialize logging.  */
825   log_init (opt.lfilename, append_to_log);
826
827   DEBUGP (("DEBUG output created by Wget %s on %s.\n\n", version_string,
828            OS_TYPE));
829
830   /* Open the output filename if necessary.  */
831   if (opt.output_document)
832     {
833       if (HYPHENP (opt.output_document))
834         opt.dfp = stdout;
835       else
836         {
837           struct stat st;
838           opt.dfp = fopen (opt.output_document, opt.always_rest ? "ab" : "wb");
839           if (opt.dfp == NULL)
840             {
841               perror (opt.output_document);
842               exit (1);
843             }
844           if (fstat (fileno (opt.dfp), &st) == 0 && S_ISREG (st.st_mode))
845             opt.od_known_regular = 1;
846         }
847     }
848
849 #ifdef WINDOWS
850   ws_startup ();
851 #endif
852
853   /* Setup the signal handler to redirect output when hangup is
854      received.  */
855 #ifdef HAVE_SIGNAL
856   if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
857     signal(SIGHUP, redirect_output_signal);
858   /* ...and do the same for SIGUSR1.  */
859   signal (SIGUSR1, redirect_output_signal);
860   /* Writing to a closed socket normally signals SIGPIPE, and the
861      process exits.  What we want is to ignore SIGPIPE and just check
862      for the return value of write().  */
863   signal (SIGPIPE, SIG_IGN);
864 #ifdef SIGWINCH
865   signal (SIGWINCH, progress_handle_sigwinch);
866 #endif
867 #endif /* HAVE_SIGNAL */
868
869 #ifdef HAVE_SSL
870   /* Must call this before resolving any URLs because it has the power
871      to disable `https'.  */
872   ssl_init_prng ();
873 #endif
874
875   status = RETROK;              /* initialize it, just-in-case */
876   /* Retrieve the URLs from argument list.  */
877   for (t = url; *t; t++)
878     {
879       char *filename = NULL, *redirected_URL = NULL;
880       int dt;
881
882       if (opt.recursive && url_scheme (*t) != SCHEME_FTP)
883         status = retrieve_tree (*t);
884       else
885         status = retrieve_url (*t, &filename, &redirected_URL, NULL, &dt);
886
887       if (opt.delete_after && file_exists_p(filename))
888         {
889           DEBUGP (("Removing file due to --delete-after in main():\n"));
890           logprintf (LOG_VERBOSE, _("Removing %s.\n"), filename);
891           if (unlink (filename))
892             logprintf (LOG_NOTQUIET, "unlink: %s\n", strerror (errno));
893         }
894
895       FREE_MAYBE (redirected_URL);
896       FREE_MAYBE (filename);
897     }
898
899   /* And then from the input file, if any.  */
900   if (opt.input_filename)
901     {
902       int count;
903       status = retrieve_from_file (opt.input_filename, opt.force_html, &count);
904       if (!count)
905         logprintf (LOG_NOTQUIET, _("No URLs found in %s.\n"),
906                    opt.input_filename);
907     }
908   /* Print the downloaded sum.  */
909   if (opt.recursive
910       || nurl > 1
911       || (opt.input_filename && opt.downloaded != 0))
912     {
913       logprintf (LOG_NOTQUIET,
914                  _("\nFINISHED --%s--\nDownloaded: %s bytes in %d files\n"),
915                  time_str (NULL),
916                  (opt.downloaded_overflow ?
917                   "<overflow>" : legible_very_long (opt.downloaded)),
918                  opt.numurls);
919       /* Print quota warning, if exceeded.  */
920       if (downloaded_exceeds_quota ())
921         logprintf (LOG_NOTQUIET,
922                    _("Download quota (%s bytes) EXCEEDED!\n"),
923                    legible (opt.quota));
924     }
925
926   if (opt.cookies_output && wget_cookie_jar)
927     cookie_jar_save (wget_cookie_jar, opt.cookies_output);
928
929   if (opt.convert_links && !opt.delete_after)
930     {
931       convert_all_links ();
932     }
933
934   log_close ();
935   for (i = 0; i < nurl; i++)
936     xfree (url[i]);
937   cleanup ();
938
939 #ifdef DEBUG_MALLOC
940   print_malloc_debug_stats ();
941 #endif
942   if (status == RETROK)
943     return 0;
944   else
945     return 1;
946 }
947 \f
948 #ifdef HAVE_SIGNAL
949 /* Hangup signal handler.  When wget receives SIGHUP or SIGUSR1, it
950    will proceed operation as usual, trying to write into a log file.
951    If that is impossible, the output will be turned off.
952
953    #### It is unsafe to do call libc functions from a signal handler.
954    What we should do is, set a global variable, and have the code in
955    log.c pick it up.  */
956
957 static RETSIGTYPE
958 redirect_output_signal (int sig)
959 {
960   char *signal_name = (sig == SIGHUP ? "SIGHUP" :
961                        (sig == SIGUSR1 ? "SIGUSR1" :
962                         "WTF?!"));
963   log_request_redirect_output (signal_name);
964   progress_schedule_redirect ();
965   signal (sig, redirect_output_signal);
966 }
967 #endif /* HAVE_SIGNAL */