]> sjero.net Git - wget/blobdiff - src/main.c
Fixes to restore Test-iri and Test-iri-forced-remote
[wget] / src / main.c
index 46e5444531ca8962853ebb8596ec5b0421b46bd8..8b9c11057b01091755732d381f351303b24126d1 100644 (file)
@@ -1,6 +1,6 @@
 /* Command line parsing.
    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
-   2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+   2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
 
 This file is part of GNU Wget.
 
@@ -80,6 +80,9 @@ static void redirect_output_signal (int);
 #endif
 
 const char *exec_name;
+
+/* Number of successfully downloaded URLs */
+int numurls = 0;
 \f
 #ifndef TESTING
 /* Initialize I18N/L10N.  That amounts to invoking setlocale, and
@@ -199,10 +202,12 @@ static struct cmdline_option option_data[] =
     { "inet6-only", '6', OPT_BOOLEAN, "inet6only", -1 },
 #endif
     { "input-file", 'i', OPT_VALUE, "input", -1 },
+    { "iri", 0, OPT_BOOLEAN, "iri", -1 },
     { "keep-session-cookies", 0, OPT_BOOLEAN, "keepsessioncookies", -1 },
     { "level", 'l', OPT_VALUE, "reclevel", -1 },
     { "limit-rate", 0, OPT_VALUE, "limitrate", -1 },
     { "load-cookies", 0, OPT_VALUE, "loadcookies", -1 },
+    { "locale", 0, OPT_VALUE, "locale", -1 },
     { "max-redirect", 0, OPT_VALUE, "maxredirect", -1 },
     { "mirror", 'm', OPT_BOOLEAN, "mirror", -1 },
     { "no", 'n', OPT__NO, NULL, required_argument },
@@ -236,6 +241,7 @@ static struct cmdline_option option_data[] =
     { "referer", 0, OPT_VALUE, "referer", -1 },
     { "reject", 'R', OPT_VALUE, "reject", -1 },
     { "relative", 'L', OPT_BOOLEAN, "relativeonly", -1 },
+    { "remote-encoding", 0, OPT_VALUE, "remoteencoding", -1},
     { "remove-listing", 0, OPT_BOOLEAN, "removelisting", -1 },
     { "restrict-file-names", 0, OPT_BOOLEAN, "restrictfilenames", -1 },
     { "retr-symlinks", 0, OPT_BOOLEAN, "retrsymlinks", -1 },
@@ -706,16 +712,18 @@ prompt_for_password (void)
    and an appropriate number of spaces are added on subsequent
    lines.*/
 static void
-format_and_print_line (char* prefix, char* line,
-                      int line_length) 
+format_and_print_line (const char *prefix, const char *line,
+                       int line_length) 
 {
   int leading_spaces;
   int remaining_chars;
-  char *token;
+  char *line_dup, *token;
   
   assert (prefix != NULL);
   assert (line != NULL);
 
+  line_dup = xstrdup (line);
+
   if (line_length <= 0)
     line_length = max_chars_per_line;
 
@@ -723,7 +731,7 @@ format_and_print_line (char* prefix, char* line,
   printf ("%s", prefix);
   remaining_chars = line_length - leading_spaces;
   /* We break on spaces. */
-  token = strtok (line, " ");
+  token = strtok (line_dup, " ");
   while (token != NULL) 
     {
       /* If however a token is much larger than the maximum
@@ -731,13 +739,7 @@ format_and_print_line (char* prefix, char* line,
          token on the next line. */
       if (remaining_chars <= strlen (token)) 
         {
-          int j;
-          printf ("\n");
-          j = 0;
-          for (j = 0; j < leading_spaces; j++) 
-            {
-              printf (" ");
-            }
+          printf ("\n%*c", leading_spaces, ' ');
           remaining_chars = line_length - leading_spaces;
         }
       printf ("%s ", token);
@@ -747,11 +749,7 @@ format_and_print_line (char* prefix, char* line,
 
   printf ("\n");
 
-  /* FIXME: Responsibility for deallocation should be handled by
-     whatever allocated it, wherever possible. These two lines result
-     in unnecessary strdup calls in the print_version function. */
-  xfree (prefix);
-  xfree (line);
+  xfree (line_dup);
 }
 
 static void
@@ -764,6 +762,7 @@ print_version (void)
   const char *link_title    = "Link       : ";
   const char *prefix_spaces = "             ";
   const int prefix_space_length = strlen (prefix_spaces);
+  char *line;
   char *env_wgetrc, *user_wgetrc;
   int i;
 
@@ -808,17 +807,18 @@ print_version (void)
   putchar ('\n');
 #endif
 
-  format_and_print_line (strdup (locale_title),
-                        strdup (LOCALEDIR)
+  format_and_print_line (locale_title,
+                        LOCALEDIR
                         max_chars_per_line);
   
-  format_and_print_line (strdup (compile_title),
-                        strdup (compilation_string),
+  format_and_print_line (compile_title,
+                        compilation_string,
                         max_chars_per_line);
 
-  format_and_print_line (strdup (link_title),
-                        strdup (link_string),
+  format_and_print_line (link_title,
+                        link_string,
                         max_chars_per_line);
+
   printf ("\n");
   /* TRANSLATORS: When available, an actual copyright character
      (cirle-c) should be used in preference to "(C)". */
@@ -835,6 +835,8 @@ There is NO WARRANTY, to the extent permitted by law.\n"), stdout);
          stdout);
   fputs (_("Currently maintained by Micah Cowan <micah@cowan.name>.\n"),
          stdout);
+  fputs (_("Please send bug reports and questions to <bug-wget@gnu.org>.\n"),
+         stdout);
   exit (0);
 }
 
@@ -1078,6 +1080,27 @@ for details.\n\n"));
       exit (1);
     }
 
+#ifdef ENABLE_IRI
+  if (opt.enable_iri)
+    {
+      if (opt.locale && !check_encoding_name (opt.locale))
+        opt.locale = NULL;
+
+      if (!opt.locale)
+        opt.locale = find_locale ();
+
+      if (opt.encoding_remote && !check_encoding_name (opt.encoding_remote))
+        opt.encoding_remote = NULL;
+    }
+#else
+  if (opt.enable_iri || opt.locale || opt.encoding_remote)
+    {
+      /* sXXXav : be more specific... */
+      printf(_("This version does not have support for IRIs\n"));
+      exit(1);
+    }
+#endif
+
   if (opt.ask_passwd)
     {
       opt.passwd = prompt_for_password ();
@@ -1125,7 +1148,7 @@ for details.\n\n"));
         {
 #ifdef WINDOWS
           FILE *result;
-          result = freopen (NULL, "wb", stdout);
+          result = freopen ("CONOUT$", "wb", stdout);
           if (result == NULL)
             {
               logputs (LOG_NOTQUIET, _("\
@@ -1179,34 +1202,55 @@ WARNING: Can't reopen standard output in binary mode;\n\
   for (t = url; *t; t++)
     {
       char *filename = NULL, *redirected_URL = NULL;
-      int dt;
+      int dt, url_err;
+      /* Need to do a new struct iri every time, because
+       * retrieve_url may modify it in some circumstances,
+       * currently. */
+      struct iri *iri = iri_new ();
+      struct url *url_parsed = url_parse (*t, &url_err, iri, true);
 
-      if ((opt.recursive || opt.page_requisites)
-          && (url_scheme (*t) != SCHEME_FTP || url_uses_proxy (*t)))
+      set_uri_encoding (iri, opt.locale, true);
+
+      if (!url_parsed)
+        {
+          char *error = url_error (*t, url_err);
+          logprintf (LOG_NOTQUIET, "%s: %s.\n",*t, error);
+          xfree (error);
+          status = URLERROR;
+        }
+      else
         {
-          int old_follow_ftp = opt.follow_ftp;
+          if ((opt.recursive || opt.page_requisites)
+              && (url_scheme (*t) != SCHEME_FTP || url_uses_proxy (url_parsed)))
+            {
+              int old_follow_ftp = opt.follow_ftp;
 
-          /* Turn opt.follow_ftp on in case of recursive FTP retrieval */
-          if (url_scheme (*t) == SCHEME_FTP) 
-            opt.follow_ftp = 1;
+              /* Turn opt.follow_ftp on in case of recursive FTP retrieval */
+              if (url_scheme (*t) == SCHEME_FTP) 
+                opt.follow_ftp = 1;
           
-          status = retrieve_tree (*t);
+              status = retrieve_tree (url_parsed, NULL);
 
-          opt.follow_ftp = old_follow_ftp;
-        }
-      else
-        status = retrieve_url (*t, &filename, &redirected_URL, NULL, &dt, opt.recursive);
+              opt.follow_ftp = old_follow_ftp;
+            }
+          else
+          {
+            status = retrieve_url (url_parsed, *t, &filename, &redirected_URL,
+                                   NULL, &dt, opt.recursive, iri);
+          }
 
-      if (opt.delete_after && file_exists_p(filename))
-        {
-          DEBUGP (("Removing file due to --delete-after in main():\n"));
-          logprintf (LOG_VERBOSE, _("Removing %s.\n"), filename);
-          if (unlink (filename))
-            logprintf (LOG_NOTQUIET, "unlink: %s\n", strerror (errno));
+          if (opt.delete_after && file_exists_p(filename))
+            {
+              DEBUGP (("Removing file due to --delete-after in main():\n"));
+              logprintf (LOG_VERBOSE, _("Removing %s.\n"), filename);
+              if (unlink (filename))
+                logprintf (LOG_NOTQUIET, "unlink: %s\n", strerror (errno));
+            }
+          xfree_null (redirected_URL);
+          xfree_null (filename);
+          url_free (url_parsed);
         }
-
-      xfree_null (redirected_URL);
-      xfree_null (filename);
+      iri_free (iri);
     }
 
   /* And then from the input file, if any.  */
@@ -1235,7 +1279,7 @@ WARNING: Can't reopen standard output in binary mode;\n\
       logprintf (LOG_NOTQUIET,
                  _("FINISHED --%s--\nDownloaded: %d files, %s in %s (%s)\n"),
                  datetime_str (time (NULL)),
-                 opt.numurls,
+                 numurls,
                  human_readable (total_downloaded_bytes),
                  secs_to_human_time (total_download_time),
                  retr_rate (total_downloaded_bytes, total_download_time));