forked from StephanTLavavej/mingw-distro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcre-color.patch
259 lines (247 loc) · 8.82 KB
/
pcre-color.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
diff -aurN pcre-8.37/pcregrep.c pcre-8.37-fixed/pcregrep.c
--- pcre-8.37/pcregrep.c 2015-04-14 10:06:44 -0700
+++ pcre-8.37-fixed/pcregrep.c 2015-09-27 12:40:30 -0700
@@ -142,8 +142,9 @@
static int endlinetype;
-static char *colour_string = (char *)"1;31";
-static char *colour_option = NULL;
+static char *colour_string = (char *)"32;1"; /* bold green */
+static char *colour_option = (char *)"always";
+const char * const filename_color = "36"; /* cyan */
static char *dee_option = NULL;
static char *DEE_option = NULL;
static char *locale = NULL;
@@ -817,7 +818,171 @@
#endif /* End of system-specific functions */
+#define NOMINMAX
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+
+void setWinColor_UNLOCKED(const char * const s) {
+ static WORD s_attributes = 0;
+ static BOOL s_saved = FALSE;
+
+ HANDLE hand = NULL;
+ CONSOLE_SCREEN_BUFFER_INFO info;
+
+ hand = GetStdHandle(STD_OUTPUT_HANDLE);
+
+ if (hand == INVALID_HANDLE_VALUE || hand == NULL || GetConsoleScreenBufferInfo(hand, &info) == 0) {
+ return;
+ }
+
+ if (!s_saved) {
+ s_attributes = info.wAttributes;
+ s_saved = TRUE;
+ }
+
+ if (s == NULL) {
+ puts("ERROR: setWinColor() - s is NULL.");
+ exit(EXIT_FAILURE);
+ } else if (strcmp(s, "") == 0) {
+ puts("ERROR: setWinColor() - s is empty.");
+ exit(EXIT_FAILURE);
+ } else {
+ int f_r, f_g, f_b, f_bold, b_r, b_g, b_b, b_bold;
+ const char * p = s;
+ int number = 0;
+
+ f_r = (info.wAttributes & FOREGROUND_RED ) != 0;
+ f_g = (info.wAttributes & FOREGROUND_GREEN ) != 0;
+ f_b = (info.wAttributes & FOREGROUND_BLUE ) != 0;
+ f_bold = -999;
+
+ b_r = (info.wAttributes & BACKGROUND_RED ) != 0;
+ b_g = (info.wAttributes & BACKGROUND_GREEN ) != 0;
+ b_b = (info.wAttributes & BACKGROUND_BLUE ) != 0;
+ b_bold = -999;
+
+ for (;;) {
+ const char c = *p;
+
+ if (c >= '0' && c <= '9') {
+ number *= 10;
+ number += c - '0';
+
+ if (number > 99) {
+ puts("ERROR: setWinColor() - Gigantic number.");
+ exit(EXIT_FAILURE);
+ }
+ } else if (c == ';' || c == '\0') {
+ if (number == 0) {
+ f_r = (s_attributes & FOREGROUND_RED ) != 0;
+ f_g = (s_attributes & FOREGROUND_GREEN ) != 0;
+ f_b = (s_attributes & FOREGROUND_BLUE ) != 0;
+ f_bold = (s_attributes & FOREGROUND_INTENSITY) != 0;
+ b_r = (s_attributes & BACKGROUND_RED ) != 0;
+ b_g = (s_attributes & BACKGROUND_GREEN ) != 0;
+ b_b = (s_attributes & BACKGROUND_BLUE ) != 0;
+ b_bold = (s_attributes & BACKGROUND_INTENSITY) != 0;
+ } else if (number >= 30 && number <= 37) {
+ f_r = ((number % 10) & 1) != 0;
+ f_g = ((number % 10) & 2) != 0;
+ f_b = ((number % 10) & 4) != 0;
+
+ if (f_bold == -999) {
+ f_bold = 0;
+ }
+ } else if (number == 1) {
+ f_bold = 1;
+ } else if (number == 22) {
+ f_bold = 0;
+ } else if (number >= 40 && number <= 47) {
+ b_r = ((number % 10) & 1) != 0;
+ b_g = ((number % 10) & 2) != 0;
+ b_b = ((number % 10) & 4) != 0;
+
+ if (b_bold == -999) {
+ b_bold = 0;
+ }
+ } else if (number == 5) {
+ b_bold = 1;
+ } else if (number == 25) {
+ b_bold = 0;
+ } else {
+ puts("ERROR: setWinColor() - Unrecognized number.");
+ exit(EXIT_FAILURE);
+ }
+
+ number = 0;
+ } else {
+ puts("ERROR: setWinColor() - Unrecognized character.");
+ exit(EXIT_FAILURE);
+ }
+
+ if (c == '\0') {
+ break;
+ } else {
+ ++p;
+ }
+ }
+
+ if (f_bold == -999) {
+ f_bold = (info.wAttributes & FOREGROUND_INTENSITY) != 0;
+ }
+
+ if (b_bold == -999) {
+ b_bold = (info.wAttributes & BACKGROUND_INTENSITY) != 0;
+ }
+
+ if (f_r == b_r && f_g == b_g && f_b == b_b && f_bold == b_bold) {
+ f_r = f_g = f_b = f_bold = 0;
+ }
+
+ if (f_r == b_r && f_g == b_g && f_b == b_b && f_bold == b_bold) {
+ f_r = f_g = f_b = f_bold = 1;
+ }
+
+ SetConsoleTextAttribute(hand, f_r * FOREGROUND_RED | f_g * FOREGROUND_GREEN | f_b * FOREGROUND_BLUE | f_bold * FOREGROUND_INTENSITY
+ | b_r * BACKGROUND_RED | b_g * BACKGROUND_GREEN | b_b * BACKGROUND_BLUE | b_bold * BACKGROUND_INTENSITY);
+ }
+}
+
+CRITICAL_SECTION g_color_lock;
+BOOL g_color_bool = TRUE;
+
+void setWinColor(const char * const s) {
+ EnterCriticalSection(&g_color_lock);
+
+ if (g_color_bool) {
+ setWinColor_UNLOCKED(s);
+ }
+ LeaveCriticalSection(&g_color_lock);
+}
+
+void resetWinColor(void) {
+ setWinColor("0");
+}
+
+BOOL WINAPI colorHandler(const DWORD dwCtrlType) {
+ if (dwCtrlType == CTRL_C_EVENT || dwCtrlType == CTRL_BREAK_EVENT) {
+ EnterCriticalSection(&g_color_lock);
+ resetWinColor();
+ g_color_bool = FALSE;
+ LeaveCriticalSection(&g_color_lock);
+ }
+
+ return FALSE;
+}
+
+void initWinColor(void) {
+ InitializeCriticalSectionAndSpinCount(&g_color_lock, 4000);
+
+ if (SetConsoleCtrlHandler(&colorHandler, TRUE) == 0) {
+ puts("ERROR: initWinColor() - SetConsoleCtrlHandler() failed.");
+ exit(EXIT_FAILURE);
+ }
+
+ atexit(resetWinColor);
+}
#ifndef HAVE_STRERROR
/*************************************************
@@ -1746,7 +1911,7 @@
prevoffsets[0] = offsets[0];
prevoffsets[1] = offsets[1];
- if (printname != NULL) fprintf(stdout, "%s:", printname);
+ if (printname != NULL) setWinColor(filename_color), fprintf(stdout, "%s:", printname), resetWinColor();
if (number) fprintf(stdout, "%d:", linenumber);
/* Handle --line-offsets */
@@ -1778,9 +1943,9 @@
if (plen > 0)
{
if (printed) fprintf(stdout, "%s", om_separator);
- if (do_colour) fprintf(stdout, "%c[%sm", 0x1b, colour_string);
+ if (do_colour) setWinColor(colour_string);
FWRITE(matchptr + offsets[n*2], 1, plen, stdout);
- if (do_colour) fprintf(stdout, "%c[00m", 0x1b);
+ if (do_colour) resetWinColor();
printed = TRUE;
}
}
@@ -1898,7 +2063,7 @@
if (after_context > 0 || before_context > 0)
endhyphenpending = TRUE;
- if (printname != NULL) fprintf(stdout, "%s:", printname);
+ if (printname != NULL) setWinColor(filename_color), fprintf(stdout, "%s:", printname), resetWinColor();
if (number) fprintf(stdout, "%d:", linenumber);
/* In multiline mode, we want to print to the end of the line in which
@@ -1945,9 +2110,9 @@
{
int plength;
FWRITE(ptr, 1, offsets[0], stdout);
- fprintf(stdout, "%c[%sm", 0x1b, colour_string);
+ setWinColor(colour_string);
FWRITE(ptr + offsets[0], 1, offsets[1] - offsets[0], stdout);
- fprintf(stdout, "%c[00m", 0x1b);
+ resetWinColor();
for (;;)
{
startoffset = offsets[1];
@@ -1956,9 +2121,9 @@
&mrc))
break;
FWRITE(matchptr + startoffset, 1, offsets[0] - startoffset, stdout);
- fprintf(stdout, "%c[%sm", 0x1b, colour_string);
+ setWinColor(colour_string);
FWRITE(matchptr + offsets[0], 1, offsets[1] - offsets[0], stdout);
- fprintf(stdout, "%c[00m", 0x1b);
+ resetWinColor();
}
/* In multiline mode, we may have already printed the complete line
@@ -2094,7 +2259,7 @@
if (count > 0 || !omit_zero_count)
{
if (printname != NULL && filenames != FN_NONE)
- fprintf(stdout, "%s:", printname);
+ setWinColor(filename_color), fprintf(stdout, "%s:", printname), resetWinColor();
fprintf(stdout, "%d\n", count);
}
}
@@ -2638,6 +2803,8 @@
pcre_jit_stack *jit_stack = NULL;
#endif
+initWinColor();
+
/* Set the default line ending value from the default in the PCRE library;
"lf", "cr", "crlf", and "any" are supported. Anything else is treated as "lf".
Note that the return values from pcre_config(), though derived from the ASCII