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