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