]> cygwin.com Git - cygwin-apps/cygutils.git/commitdiff
Add Unicode support to getclip
authorMark Geisert <mark@maxrnd.com>
Tue, 6 Jul 2021 06:53:28 +0000 (23:53 -0700)
committerMark Geisert <mark@maxrnd.com>
Tue, 6 Jul 2021 06:53:28 +0000 (23:53 -0700)
ChangeLog
Makefile.am
NEWS
cygutils.cygport
src/clip/getclip.c

index c67ee007d6d148955b7dadc5c4ef08c0a088e7e9..53c8eef2f71c8b84c76941b9e43d6288c1402cb7 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2021-07-05  Mark Geisert  <mark@maxrnd.com>
+
+       * src/clip/getclip.c: Add Unicode support.
+       * Makefile.am: Add -lntoskrnl to src_clip_getclip_LDADD.
+
 2017-07-03  Mark Geisert  <mark@maxrnd.com>
 
        Release 1.4.16
index 79eb2b1dec33494136928e845c2388d17dfab758..9b2604a0ff710d0c9f2a6d4fe236a64dbe72c627 100644 (file)
@@ -117,7 +117,7 @@ DISTCLEANFILES = \
 
 libuuid = `${CC} -print-file-name=w32api/libuuid.a`
 src_banner_banner_LDADD             = -lpopt -lkernel32 -lgdi32
-src_clip_getclip_LDADD              = -lpopt
+src_clip_getclip_LDADD              = -lpopt -lntoskrnl
 src_clip_putclip_LDADD              = -lpopt
 src_conv_conv_LDADD                 = -lpopt
 src_cygstart_cygstart_LDADD         = -lpopt
diff --git a/NEWS b/NEWS
index dbc98790a4b4bd48202d4bfd5746a51fe563dc41..99914d42339915af7dbd504a3c19bb63b1f5929e 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -3,6 +3,7 @@
   * Minor formatting fixes to quash compiler warnings.
   * Execute bits removed from files not needing them.
   * Temporarily rename /usr/lib/libpopt.la while building package.
+  * Unicode support added to getclip.
 
 1.4.15
   New maintainer Mark Geisert <mark@maxrnd.com>.
index cdbae68a47be560b14943500034c74c80e4bcc2e..6f603aa582cabf2cc2c74a19ea3b5883d1e8ad65 100644 (file)
@@ -1,6 +1,6 @@
 NAME=cygutils
 VERSION=1.4.16
-RELEASE=2
+RELEASE=3
 
 SUMM="A collection of simple utilities"
 DESC="A collection of simple (single source file) utilities, including"
index 4ddbce3c8573247b61b58b6ea9fa0e304c6d47bd..21eef59e013423378a65869d80971a20077f9347 100644 (file)
 #include "common.h"
 
 #include <io.h>
+#include <wchar.h>
 #include <sys/cygwin.h>
+#include <w32api/ntstatus.h>
+/* This Windows 7+ function is not yet present in w32api/winternl.h */
+extern NTSYSAPI NTSTATUS RtlUnicodeToUTF8N(PCHAR, ULONG, PULONG, LPWSTR, ULONG);
+
+#define DEBUGGING 0 // Set 1 to display debug output on stderr
 
 typedef struct
 {
@@ -356,16 +362,31 @@ getclip (FILE * out, flags_struct flags, FILE * f, char *name)
     {
       const char *CYGWIN_NATIVE = "CYGWIN_NATIVE_CLIPBOARD";
       UINT cygnativeformat;
-      UINT formatlist[2];
+      UINT formatlist[3];
       UINT format;
 
       OpenClipboard (NULL);
+#if DEBUGGING
+      {
+        fprintf (stderr, "ThreadLocale: %u\nClipboardFormats:",
+                 GetThreadLocale ());
+        format = 0;
+        do {
+          format = EnumClipboardFormats (format);
+          fprintf (stderr, " %u", format);
+          if (format == CF_LOCALE)
+            fprintf (stderr, "(%u)", *(LCID *) GetClipboardData (CF_LOCALE));
+        } while (format != 0);
+        fprintf (stderr, "\n");
+      }
+#endif
       cygnativeformat = RegisterClipboardFormat (CYGWIN_NATIVE);
 
       formatlist[0] = cygnativeformat;
-      formatlist[1] = CF_TEXT;
+      formatlist[1] = CF_UNICODETEXT;
+      formatlist[2] = CF_TEXT;
 
-      if ((format = GetPriorityClipboardFormat (formatlist, 2)) <= 0)
+      if ((format = GetPriorityClipboardFormat (formatlist, 3)) <= 0)
         {
           CloseClipboard ();
           return (0);
@@ -380,25 +401,47 @@ getclip (FILE * out, flags_struct flags, FILE * f, char *name)
               cygcb_t *clipbuf = (cygcb_t *) GlobalLock (hglb);
               if (clipbuf)
                 {
-                  fwrite (clipbuf->data, sizeof (unsigned char), clipbuf->len, out);
+                  fwrite (clipbuf->data, sizeof (char), clipbuf->len, out);
                   GlobalUnlock (hglb);
                 }
            }
          else
            {
-              unsigned char *nativebuf = (unsigned char *) GlobalLock (hglb);
+              char *nativebuf = (char *) GlobalLock (hglb);
               if (nativebuf)
                 {
                   size_t buflen = (*(size_t *) nativebuf);
                   buf = nativebuf + sizeof (size_t);
                   len = buflen;
 
-                  fwrite (buf, sizeof (unsigned char), len, out);
+                  fwrite (buf, sizeof (char), len, out);
                   GlobalUnlock (hglb);
                 }
            }
         }
-      else
+      else if (format == CF_UNICODETEXT)
+        {
+          LPWSTR lpwstr = (LPWSTR) GlobalLock (hglb);
+          if (lpwstr)
+            {
+              ULONG count;
+              ULONG srclen = wcslen (lpwstr) * sizeof (wchar_t); // byte count
+              ULONG dstlen = srclen + 32;
+              PCHAR dst = (PCHAR) malloc (dstlen);
+              NTSTATUS res = RtlUnicodeToUTF8N (dst, dstlen, &count,
+                                                lpwstr, srclen);
+#if DEBUGGING
+              /* look up hex error code displayed here in w32api/ntstatus.h */
+              fprintf (stderr, "RtlUnicodeToUTF8N returns 0x%X\n", res);
+#endif
+              if (res == STATUS_SUCCESS || res == STATUS_SOME_NOT_MAPPED)
+                if (count > 0)
+                  fwrite (dst, sizeof (char), count, out);
+              free (dst);
+              GlobalUnlock (hglb);
+            }
+        }
+      else /* format == CF_TEXT */
         {
           LPSTR lpstr = (LPSTR) GlobalLock (hglb);
           if (lpstr)
@@ -407,7 +450,7 @@ getclip (FILE * out, flags_struct flags, FILE * f, char *name)
               buf = lpstr;
               len = lplen;
 
-              fwrite (buf, sizeof (unsigned char), len, out);
+              fwrite (buf, sizeof (char), len, out);
               GlobalUnlock (hglb);
             }
         }
@@ -425,7 +468,7 @@ getclip (FILE * out, flags_struct flags, FILE * f, char *name)
       char *pos;
 
       OpenClipboard (0);
-      hglb = GetClipboardData (CF_TEXT);
+      hglb = GetClipboardData (CF_TEXT); //TODO support CF_UNICODETEXT too?
       lpstr = GlobalLock (hglb);
       lplen = strlen (lpstr);
 
@@ -481,7 +524,7 @@ getclip (FILE * out, flags_struct flags, FILE * f, char *name)
       char *pos;
 
       OpenClipboard (0);
-      hglb = GetClipboardData (CF_TEXT);
+      hglb = GetClipboardData (CF_TEXT); //TODO support CF_UNICODETEXT too?
       lpstr = GlobalLock (hglb);
       lplen = strlen (lpstr);
 
This page took 0.038241 seconds and 5 git commands to generate.