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