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