[Pcre-svn] [691] code/trunk: Fix pcre2grep Windows problem f…

Top Page
Delete this message
Author: Subversion repository
Date:  
To: pcre-svn
Subject: [Pcre-svn] [691] code/trunk: Fix pcre2grep Windows problem for new output-colouring code when not under
Revision: 691
          http://www.exim.org/viewvc/pcre2?view=rev&revision=691
Author:   ph10
Date:     2017-03-21 16:09:57 +0000 (Tue, 21 Mar 2017)
Log Message:
-----------
Fix pcre2grep Windows problem for new output-colouring code when not under 
mingw (Bugzilla 2067).


Modified Paths:
--------------
    code/trunk/ChangeLog
    code/trunk/src/pcre2grep.c


Modified: code/trunk/ChangeLog
===================================================================
--- code/trunk/ChangeLog    2017-03-21 12:22:58 UTC (rev 690)
+++ code/trunk/ChangeLog    2017-03-21 16:09:57 UTC (rev 691)
@@ -64,7 +64,11 @@
 mode, the pattern //g,zero_terminate read random memory when matched against an 
 empty string with zero_terminate. This was a bug in pcre2test, not the library.


+9. Moved some Windows-specific code in pcre2grep (introduced in 10.23/13) out
+of the section that is compiled when Unix-style directory scanning is
+available, and into a new section that is always compiled for Windows.

+
Version 10.23 14-February-2017
------------------------------


Modified: code/trunk/src/pcre2grep.c
===================================================================
--- code/trunk/src/pcre2grep.c    2017-03-21 12:22:58 UTC (rev 690)
+++ code/trunk/src/pcre2grep.c    2017-03-21 16:09:57 UTC (rev 691)
@@ -622,11 +622,72 @@
 *            OS-specific functions               *
 *************************************************/


-/* These functions are defined so that they can be made system specific.
-At present there are versions for Unix-style environments, Windows, native
-z/OS, and "no support". */
+/* These definitions are needed in all Windows environments, even those where
+Unix-style directory scanning can be used (see below). */

+#ifdef WIN32

+#define iswild(name) (strpbrk(name, "*?") != NULL)
+
+/* Convert ANSI BGR format to RGB used by Windows */
+#define BGR_RGB(x) ((x & 1 ? 4 : 0) | (x & 2) | (x & 4 ? 1 : 0))
+
+static WORD
+decode_ANSI_colour(const char *cs)
+{
+WORD result = csbi.wAttributes;
+while (*cs)
+  {
+  if (isdigit(*cs))
+    {
+    int code = atoi(cs);
+    if (code == 1) result |= 0x08;
+    else if (code == 4) result |= 0x8000;
+    else if (code == 5) result |= 0x80;
+    else if (code >= 30 && code <= 37) result = (result & 0xF8) | BGR_RGB(code - 30);
+    else if (code == 39) result = (result & 0xF0) | (csbi.wAttributes & 0x0F);
+    else if (code >= 40 && code <= 47) result = (result & 0x8F) | (BGR_RGB(code - 40) << 4);
+    else if (code == 49) result = (result & 0x0F) | (csbi.wAttributes & 0xF0);
+    /* aixterm high intensity colour codes */
+    else if (code >= 90 && code <= 97) result = (result & 0xF0) | BGR_RGB(code - 90) | 0x08;
+    else if (code >= 100 && code <= 107) result = (result & 0x0F) | (BGR_RGB(code - 100) << 4) | 0x80;
+
+    while (isdigit(*cs)) cs++;
+    }
+  if (*cs) cs++;
+  }
+return result;
+}
+
+
+static void
+init_colour_output()
+{
+if (do_colour)
+  {
+  hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
+  /* This fails when redirected to con; try again if so. */
+  if (!GetConsoleScreenBufferInfo(hstdout, &csbi) && !do_ansi)
+    {
+    HANDLE hcon = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE,
+      FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
+    GetConsoleScreenBufferInfo(hcon, &csbi);
+    CloseHandle(hcon);
+    }
+  match_colour = decode_ANSI_colour(colour_string);
+  /* No valid colour found - turn off colouring */
+  if (!match_colour) do_colour = FALSE;
+  }
+}
+
+#endif  /* WIN32 */
+
+
+/* The following sets of functions are defined so that they can be made system
+specific. At present there are versions for Unix-style environments, Windows,
+native z/OS, and "no support". */
+
+
 /************* Directory scanning Unix-style and z/OS ***********/


#if (defined HAVE_SYS_STAT_H && defined HAVE_DIRENT_H && defined HAVE_SYS_TYPES_H) || defined NATIVE_ZOS
@@ -748,7 +809,8 @@
/* I (Philip Hazel) have no means of testing this code. It was contributed by
Lionel Fourquaux. David Burgess added a patch to define INVALID_FILE_ATTRIBUTES
when it did not exist. David Byron added a patch that moved the #include of
-<windows.h> to before the INVALID_FILE_ATTRIBUTES definition rather than after. */
+<windows.h> to before the INVALID_FILE_ATTRIBUTES definition rather than after.
+*/

#elif defined WIN32

@@ -765,11 +827,6 @@
#define INVALID_FILE_ATTRIBUTES 0xFFFFFFFF
#endif

-/* Allow opendirectory to provide globbing, since Microsoft started doing it
-wrong (expanding quoted arguments). */
-
-#define iswild(name) (strpbrk(name, "*?") != NULL)
-
typedef struct directory_type
{
HANDLE handle;
@@ -901,58 +958,6 @@
}
}

-/* Convert ANSI BGR format to RGB used by Windows */
-#define BGR_RGB(x) ((x & 1 ? 4 : 0) | (x & 2) | (x & 4 ? 1 : 0))
-
-static WORD
-decode_ANSI_colour(const char *cs)
-{
-WORD result = csbi.wAttributes;
-while (*cs)
-  {
-  if (isdigit(*cs))
-    {
-    int code = atoi(cs);
-    if (code == 1) result |= 0x08;
-    else if (code == 4) result |= 0x8000;
-    else if (code == 5) result |= 0x80;
-    else if (code >= 30 && code <= 37) result = (result & 0xF8) | BGR_RGB(code - 30);
-    else if (code == 39) result = (result & 0xF0) | (csbi.wAttributes & 0x0F);
-    else if (code >= 40 && code <= 47) result = (result & 0x8F) | (BGR_RGB(code - 40) << 4);
-    else if (code == 49) result = (result & 0x0F) | (csbi.wAttributes & 0xF0);
-    /* aixterm high intensity colour codes */
-    else if (code >= 90 && code <= 97) result = (result & 0xF0) | BGR_RGB(code - 90) | 0x08;
-    else if (code >= 100 && code <= 107) result = (result & 0x0F) | (BGR_RGB(code - 100) << 4) | 0x80;
-
-    while (isdigit(*cs)) cs++;
-    }
-
-  if (*cs) cs++;
-  }
-
-return result;
-}
-
-static void
-init_colour_output()
-{
-if (do_colour)
-  {
-  hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
-  /* This fails when redirected to con; try again if so. */
-  if (!GetConsoleScreenBufferInfo(hstdout, &csbi) && !do_ansi)
-    {
-    HANDLE hcon = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE,
-      FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
-    GetConsoleScreenBufferInfo(hcon, &csbi);
-    CloseHandle(hcon);
-    }
-  match_colour = decode_ANSI_colour(colour_string);
-  /* No valid colour found - turn off colouring */
-  if (!match_colour) do_colour = FALSE;
-  }
-}
-
 /* End of Windows functions */