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