Blame SOURCES/0001-Add-wide-char-support-to-pinentry-curses.patch

8461b8
From e2b89cbdcba7475047ca3c46fc7d03825355b3ff Mon Sep 17 00:00:00 2001
8461b8
From: Daiki Ueno <ueno@unixuser.org>
8461b8
Date: Wed, 10 Aug 2011 12:50:43 +0900
8461b8
Subject: [PATCH] Add wide-char support to pinentry-curses.
8461b8
8461b8
---
8461b8
 configure.ac               |   2 +-
8461b8
 m4/curses.m4               |  20 +++++-
8461b8
 pinentry/pinentry-curses.c | 150 +++++++++++++++++++++++++++++++++++++--------
8461b8
 3 files changed, 143 insertions(+), 29 deletions(-)
8461b8
8461b8
diff --git a/configure.ac b/configure.ac
8461b8
index 4b8ed79..bcbe26e 100644
8461b8
--- a/configure.ac
8461b8
+++ b/configure.ac
8461b8
@@ -158,7 +158,7 @@ fi
8461b8
 
8461b8
 # Checks for header files.
8461b8
 AC_HEADER_STDC
8461b8
-AC_CHECK_HEADERS(string.h unistd.h langinfo.h termio.h locale.h utime.h)
8461b8
+AC_CHECK_HEADERS(string.h unistd.h langinfo.h termio.h locale.h utime.h wchar.h)
8461b8
 
8461b8
 dnl Checks for library functions.
8461b8
 AC_CHECK_FUNCS(seteuid stpcpy mmap)
8461b8
diff --git a/m4/curses.m4 b/m4/curses.m4
8461b8
index 1e1cb4f..3a01881 100644
8461b8
--- a/m4/curses.m4
8461b8
+++ b/m4/curses.m4
8461b8
@@ -28,7 +28,13 @@ AC_DEFUN([IU_LIB_NCURSES], [
8461b8
   AC_ARG_ENABLE(ncurses,    [  --disable-ncurses       don't prefer -lncurses over -lcurses],
8461b8
               , enable_ncurses=yes)
8461b8
   if test "$enable_ncurses" = yes; then
8461b8
-    AC_CHECK_LIB(ncurses, initscr, LIBNCURSES="-lncurses")
8461b8
+    AC_CHECK_LIB(ncursesw, initscr, LIBNCURSES="-lncursesw",
8461b8
+      AC_CHECK_LIB(ncurses, initscr, LIBNCURSES="-lncurses"))
8461b8
+    if test "$ac_cv_lib_ncursesw_initscr" = yes; then
8461b8
+      have_ncursesw=yes
8461b8
+    else
8461b8
+      have_ncursesw=no
8461b8
+    fi
8461b8
     if test "$LIBNCURSES"; then
8461b8
       # Use ncurses header files instead of the ordinary ones, if possible;
8461b8
       # is there a better way of doing this, that avoids looking in specific
8461b8
@@ -53,9 +59,14 @@ AC_DEFUN([IU_LIB_NCURSES], [
8461b8
       else
8461b8
 	AC_CACHE_CHECK(for ncurses include dir,
8461b8
 		       inetutils_cv_includedir_ncurses,
8461b8
+          if test "$have_ncursesw" = yes; then
8461b8
+            ncursesdir=ncursesw
8461b8
+          else
8461b8
+            ncursesdir=ncurses
8461b8
+          fi
8461b8
 	  for D in $includedir $prefix/include /local/include /usr/local/include /include /usr/include; do
8461b8
-	    if test -d $D/ncurses; then
8461b8
-	      inetutils_cv_includedir_ncurses="$D/ncurses"
8461b8
+	    if test -d $D/$ncursesdir; then
8461b8
+	      inetutils_cv_includedir_ncurses="$D/$ncursesdir"
8461b8
 	      break
8461b8
 	    fi
8461b8
 	    test "$inetutils_cv_includedir_ncurses" \
8461b8
@@ -68,6 +79,9 @@ AC_DEFUN([IU_LIB_NCURSES], [
8461b8
         NCURSES_INCLUDE="-I$inetutils_cv_includedir_ncurses"
8461b8
       fi
8461b8
     fi
8461b8
+    if test $have_ncursesw = yes; then
8461b8
+      AC_DEFINE(HAVE_NCURSESW, 1, [Define if you have working ncursesw])
8461b8
+    fi
8461b8
   fi
8461b8
   AC_SUBST(NCURSES_INCLUDE)
8461b8
   AC_SUBST(LIBNCURSES)])dnl
8461b8
diff --git a/pinentry/pinentry-curses.c b/pinentry/pinentry-curses.c
8461b8
index 76ddbdd..585059f 100644
8461b8
--- a/pinentry/pinentry-curses.c
8461b8
+++ b/pinentry/pinentry-curses.c
8461b8
@@ -42,6 +42,10 @@
8461b8
 
8461b8
 #include <memory.h>
8461b8
 
8461b8
+#ifdef HAVE_WCHAR_H
8461b8
+#include <wchar.h>
8461b8
+#endif /*HAVE_WCHAR_H*/
8461b8
+
8461b8
 #include "pinentry.h"
8461b8
 
8461b8
 /* FIXME: We should allow configuration of these button labels and in
8461b8
@@ -94,6 +98,24 @@ struct dialog
8461b8
 typedef struct dialog *dialog_t;
8461b8
 
8461b8
 
8461b8
+#ifdef HAVE_NCURSESW
8461b8
+typedef wchar_t CH;
8461b8
+#define STRLEN(x) wcslen (x)
8461b8
+#define ADDCH(x) addnwstr (&x, 1);
8461b8
+#define CHWIDTH(x) wcwidth (x)
8461b8
+#define NULLCH L'\0'
8461b8
+#define NLCH L'\n'
8461b8
+#define SPCH L' '
8461b8
+#else
8461b8
+typedef char CH;
8461b8
+#define STRLEN(x) strlen (x)
8461b8
+#define ADDCH(x) addch ((unsigned char) x)
8461b8
+#define CHWIDTH(x) 1
8461b8
+#define NULLCH '\0'
8461b8
+#define NLCH '\n'
8461b8
+#define SPCH ' '
8461b8
+#endif
8461b8
+
8461b8
 /* Return the next line up to MAXLEN columns wide in START and LEN.
8461b8
    The first invocation should have 0 as *LEN.  If the line ends with
8461b8
    a \n, it is a normal line that will be continued.  If it is a '\0'
8461b8
@@ -101,40 +123,95 @@ typedef struct dialog *dialog_t;
8461b8
    there is a forced line break.  A full line is returned and will be
8461b8
    continued in the next line.  */
8461b8
 static void
8461b8
-collect_line (int maxlen, char **start_p, int *len_p)
8461b8
+collect_line (int maxwidth, wchar_t **start_p, int *len_p)
8461b8
 {
8461b8
   int last_space = 0;
8461b8
   int len = *len_p;
8461b8
-  char *end;
8461b8
+  int width = 0;
8461b8
+  CH *end;
8461b8
 
8461b8
   /* Skip to next line.  */
8461b8
   *start_p += len;
8461b8
   /* Skip leading space.  */
8461b8
-  while (**start_p == ' ')
8461b8
+  while (**start_p == SPCH)
8461b8
     (*start_p)++;
8461b8
 
8461b8
   end = *start_p;
8461b8
   len = 0;
8461b8
 
8461b8
-  while (len < maxlen - 1 && *end && *end != '\n')
8461b8
+  while (width < maxwidth - 1 && *end != NULLCH && *end != NLCH)
8461b8
     {
8461b8
       len++;
8461b8
       end++;
8461b8
-      if (*end == ' ')
8461b8
+      if (*end == SPCH)
8461b8
 	last_space = len;
8461b8
+      width += CHWIDTH (*end);
8461b8
     }
8461b8
 
8461b8
-  if (*end && *end != '\n' && last_space != 0)
8461b8
+  if (*end != NULLCH && *end != NLCH && last_space != 0)
8461b8
     {
8461b8
       /* We reached the end of the available space, but still have
8461b8
 	 characters to go in this line.  We can break the line into
8461b8
 	 two parts at a space.  */
8461b8
       len = last_space;
8461b8
-      (*start_p)[len] = '\n';
8461b8
+      (*start_p)[len] = NLCH;
8461b8
     }
8461b8
   *len_p = len + 1;
8461b8
 }
8461b8
 
8461b8
+#ifdef HAVE_NCURSESW
8461b8
+static CH *
8461b8
+utf8_to_local (char *lc_ctype, char *string)
8461b8
+{
8461b8
+  mbstate_t ps;
8461b8
+  size_t len;
8461b8
+  char *local;
8461b8
+  const char *p;
8461b8
+  wchar_t *wcs = NULL;
8461b8
+  char *old_ctype = NULL;
8461b8
+
8461b8
+  local = pinentry_utf8_to_local (lc_ctype, string);
8461b8
+  if (!local)
8461b8
+    return NULL;
8461b8
+
8461b8
+  old_ctype = strdup (setlocale (LC_CTYPE, NULL));
8461b8
+  setlocale (LC_CTYPE, lc_ctype? lc_ctype : "");
8461b8
+
8461b8
+  p = local;
8461b8
+  memset (&ps, 0, sizeof(mbstate_t));
8461b8
+  len = mbsrtowcs (NULL, &p, strlen (string), &ps);
8461b8
+  if (len == (size_t)-1)
8461b8
+    {
8461b8
+      free (local);
8461b8
+      goto leave;
8461b8
+    }
8461b8
+  wcs = calloc (len + 1, sizeof(wchar_t));
8461b8
+  if (!wcs)
8461b8
+    {
8461b8
+      free (local);
8461b8
+      goto leave;
8461b8
+    }
8461b8
+
8461b8
+  p = local;
8461b8
+  memset (&ps, 0, sizeof(mbstate_t));
8461b8
+  mbsrtowcs (wcs, &p, len, &ps);
8461b8
+
8461b8
+ leave:
8461b8
+  if (old_ctype)
8461b8
+    {
8461b8
+      setlocale (LC_CTYPE, old_ctype);
8461b8
+      free (old_ctype);
8461b8
+    }
8461b8
+
8461b8
+  return wcs;
8461b8
+}
8461b8
+#else
8461b8
+static CH *
8461b8
+utf8_to_local (const char *lc_ctype, const char *string)
8461b8
+{
8461b8
+  return pinentry_utf8_to_local (lc_ctype, string);
8461b8
+}
8461b8
+#endif
8461b8
 
8461b8
 static int
8461b8
 dialog_create (pinentry_t pinentry, dialog_t dialog)
8461b8
@@ -148,16 +225,15 @@ dialog_create (pinentry_t pinentry, dialog_t dialog)
8461b8
   int xpos;
8461b8
   int description_x = 0;
8461b8
   int error_x = 0;
8461b8
-  char *description = NULL;
8461b8
-  char *error = NULL;
8461b8
-  char *prompt = NULL;
8461b8
+  CH *description = NULL;
8461b8
+  CH *error = NULL;
8461b8
+  CH *prompt = NULL;
8461b8
 
8461b8
 #define COPY_OUT(what)							\
8461b8
   do									\
8461b8
     if (pinentry->what)							\
8461b8
       {									\
8461b8
-        what = pinentry_utf8_to_local (pinentry->lc_ctype,		\
8461b8
-				       pinentry->what);			\
8461b8
+        what = utf8_to_local (pinentry->lc_ctype, pinentry->what);	\
8461b8
         if (!what)							\
8461b8
 	  {								\
8461b8
 	    err = 1;							\
8461b8
@@ -214,7 +290,7 @@ dialog_create (pinentry_t pinentry, dialog_t dialog)
8461b8
   y = 1;		/* Top frame.  */
8461b8
   if (description)
8461b8
     {
8461b8
-      char *start = description;
8461b8
+      CH *start = description;
8461b8
       int len = 0;
8461b8
 
8461b8
       do
8461b8
@@ -232,7 +308,7 @@ dialog_create (pinentry_t pinentry, dialog_t dialog)
8461b8
     {
8461b8
       if (error)
8461b8
 	{
8461b8
-	  char *p = error;
8461b8
+	  CH *p = error;
8461b8
 	  int err_x = 0;
8461b8
 
8461b8
 	  while (*p)
8461b8
@@ -287,7 +363,9 @@ dialog_create (pinentry_t pinentry, dialog_t dialog)
8461b8
 
8461b8
       new_x = MIN_PINENTRY_LENGTH;
8461b8
       if (prompt)
8461b8
-	new_x += strlen (prompt) + 1;	/* One space after prompt.  */
8461b8
+	{
8461b8
+	  new_x += STRLEN (prompt) + 1;	/* One space after prompt.  */
8461b8
+	}
8461b8
       if (new_x > size_x - 4)
8461b8
 	new_x = size_x - 4;
8461b8
       if (new_x > x)
8461b8
@@ -335,7 +413,7 @@ dialog_create (pinentry_t pinentry, dialog_t dialog)
8461b8
   ypos++;
8461b8
   if (description)
8461b8
     {
8461b8
-      char *start = description;
8461b8
+      CH *start = description;
8461b8
       int len = 0;
8461b8
 
8461b8
       do
8461b8
@@ -347,9 +425,11 @@ dialog_create (pinentry_t pinentry, dialog_t dialog)
8461b8
 	  addch (' ');
8461b8
 	  collect_line (size_x - 4, &start, &len;;
8461b8
 	  for (i = 0; i < len - 1; i++)
8461b8
-	    addch ((unsigned char) start[i]);
8461b8
-	  if (start[len - 1] && start[len - 1] != '\n')
8461b8
-	    addch ((unsigned char) start[len - 1]);
8461b8
+	    {
8461b8
+	      ADDCH (start[i]);
8461b8
+	    }
8461b8
+	  if (start[len - 1] != NULLCH && start[len - 1] != NLCH)
8461b8
+	    ADDCH (start[len - 1]);
8461b8
 	  ypos++;
8461b8
 	}
8461b8
       while (start[len - 1]);
8461b8
@@ -363,7 +443,7 @@ dialog_create (pinentry_t pinentry, dialog_t dialog)
8461b8
 
8461b8
       if (error)
8461b8
 	{
8461b8
-	  char *p = error;
8461b8
+	  CH *p = error;
8461b8
 	  i = 0;
8461b8
 
8461b8
 	  while (*p)
8461b8
@@ -378,11 +458,11 @@ dialog_create (pinentry_t pinentry, dialog_t dialog)
8461b8
 		}
8461b8
 	      else
8461b8
 		standout ();
8461b8
-	      for (;*p && *p != '\n'; p++)
8461b8
+	      for (;*p && *p != NLCH; p++)
8461b8
 		if (i < x - 4)
8461b8
 		  {
8461b8
 		    i++;
8461b8
-		    addch ((unsigned char) *p);
8461b8
+		    ADDCH (*p);
8461b8
 		  }
8461b8
 	      if (USE_COLORS && pinentry->color_so != PINENTRY_COLOR_NONE)
8461b8
 		{
8461b8
@@ -410,14 +490,16 @@ dialog_create (pinentry_t pinentry, dialog_t dialog)
8461b8
       dialog->pin_size = x - 4;
8461b8
       if (prompt)
8461b8
 	{
8461b8
-	  char *p = prompt;
8461b8
-	  i = strlen (prompt);
8461b8
+	  CH *p = prompt;
8461b8
+	  i = STRLEN (prompt);
8461b8
 	  if (i > x - 4 - MIN_PINENTRY_LENGTH)
8461b8
 	    i = x - 4 - MIN_PINENTRY_LENGTH;
8461b8
 	  dialog->pin_x += i + 1;
8461b8
 	  dialog->pin_size -= i + 1;
8461b8
 	  while (i-- > 0)
8461b8
-	    addch ((unsigned char) *(p++));
8461b8
+	    {
8461b8
+	      ADDCH (*(p++));
8461b8
+	    }
8461b8
 	  addch (' ');
8461b8
 	}
8461b8
       for (i = 0; i < dialog->pin_size; i++)
8461b8
@@ -631,6 +713,17 @@ dialog_run (pinentry_t pinentry, const char *tty_name, const char *tty_type)
8461b8
   SCREEN *screen = 0;
8461b8
   int done = 0;
8461b8
   char *pin_utf8;
8461b8
+#ifdef HAVE_NCURSESW
8461b8
+  char *old_ctype = NULL;
8461b8
+
8461b8
+  if (pinentry->lc_ctype)
8461b8
+    {
8461b8
+      old_ctype = strdup (setlocale (LC_CTYPE, NULL));
8461b8
+      setlocale (LC_CTYPE, pinentry->lc_ctype);
8461b8
+    }
8461b8
+  else
8461b8
+    setlocale (LC_CTYPE, "");
8461b8
+#endif
8461b8
 
8461b8
   /* Open the desired terminal if necessary.  */
8461b8
   if (tty_name)
8461b8
@@ -804,6 +897,13 @@ dialog_run (pinentry_t pinentry, const char *tty_name, const char *tty_type)
8461b8
   if (screen)
8461b8
     delscreen (screen);
8461b8
 
8461b8
+#ifdef HAVE_NCURSESW
8461b8
+  if (old_ctype)
8461b8
+    {
8461b8
+      setlocale (LC_CTYPE, old_ctype);
8461b8
+      free (old_ctype);
8461b8
+    }
8461b8
+#endif
8461b8
   if (ttyfi)
8461b8
     fclose (ttyfi);
8461b8
   if (ttyfo)
8461b8
-- 
8461b8
1.8.3.1
8461b8