From 99396653c22e54e13d9be63f6c333a4b33c6cbda Mon Sep 17 00:00:00 2001
From: Saint Xavier <wget@sxav.eu>
Date: Wed, 2 Jul 2008 16:37:28 +0200
Subject: [PATCH] Show also the hostname in the locale when possible

---
 src/ChangeLog |  8 ++++++++
 src/host.c    | 21 +++++++++++++++++++--
 src/iri.c     | 21 ++++++++++++++++++++-
 src/iri.h     |  2 ++
 4 files changed, 49 insertions(+), 3 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 9e31b1c4..7aca0527 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,11 @@
+2008-07-02  Xavier Saint  <wget@sxav.eu>
+
+	* iri.c, iri.h  : New function idn_decode() to decode ASCII
+	encoded hostname to the locale.
+
+	* host.c : Show hostname to be resolved both in locale and
+	ASCII encoded.
+
 2008-06-26  Xavier Saint  <wget@sxav.eu>
 
 	* iri.c, iri.h : New functions locale_to_utf8() and
diff --git a/src/host.c b/src/host.c
index fdb35b1c..8a1495f0 100644
--- a/src/host.c
+++ b/src/host.c
@@ -53,6 +53,7 @@ as that of the covered work.  */
 #include "host.h"
 #include "url.h"
 #include "hash.h"
+#include "iri.h"
 
 #ifndef NO_ADDRESS
 # define NO_ADDRESS NO_DATA
@@ -712,8 +713,24 @@ lookup_host (const char *host, int flags)
   /* No luck with the cache; resolve HOST. */
 
   if (!silent && !numeric_address)
-    logprintf (LOG_VERBOSE, _("Resolving %s... "), 
-               quotearg_style (escape_quoting_style, host));
+    {
+      char *str = NULL, *name = NULL;
+
+      if (opt.enable_iri && (name = idn_decode (host)) != NULL)
+        {
+          int len = strlen (host) + strlen (name) + 4;
+          str = xmalloc (len);
+          snprintf (str, len, "%s (%s)", name, host);
+          str[len-1] = '\0';
+          xfree (name);
+        }
+
+      logprintf (LOG_VERBOSE, _("Resolving %s... "), 
+                 quotearg_style (escape_quoting_style, str ? str : host));
+
+      if (str)
+        xfree (str);
+    }
 
 #ifdef ENABLE_IPV6
   {
diff --git a/src/iri.c b/src/iri.c
index dfcb0578..000f6550 100644
--- a/src/iri.c
+++ b/src/iri.c
@@ -220,7 +220,7 @@ do_conversion (iconv_t cd, char *in, size_t inlen, char **out)
     return false;
 }
 
-/* Try to encode UTF-8 host to ASCII. Return the new domain on success or NULL
+/* Try to ASCII encode UTF-8 host. Return the new domain on success or NULL
    on error. */
 char *idn_encode (char *host)
 {
@@ -239,3 +239,22 @@ char *idn_encode (char *host)
   return new;
 }
 
+/* Try to decode an ASCII encoded host. Return the new domain in the locale on
+   success or NULL on error. */
+char *idn_decode (char *host)
+{
+  char *new;
+  int ret;
+
+  ret = idna_to_unicode_8zlz (host, &new, 0);
+  if (ret != IDNA_SUCCESS)
+    {
+      logprintf (LOG_VERBOSE, "idn_decode failed (%d): %s\n", ret,
+                 quote (idna_strerror (ret)));
+      return NULL;
+    }
+
+  return new;
+}
+
+
diff --git a/src/iri.h b/src/iri.h
index 64858476..3992d76d 100644
--- a/src/iri.h
+++ b/src/iri.h
@@ -37,6 +37,7 @@ char *find_locale (void);
 bool check_encoding_name (char *encoding);
 const char *locale_to_utf8 (const char *str);
 char *idn_encode (char *host);
+char *idn_decode (char *host);
 
 #else /* ENABLE_IRI */
 
@@ -45,6 +46,7 @@ char *idn_encode (char *host);
 #define check_encoding_name(str)    false
 #define locale_to_utf8(str)         (str)
 #define idn_encode(str)             NULL
+#define idn_decode(str)             NULL
 
 #endif /* ENABLE_IRI */
 #endif /* IRI_H */