forked from StephanTLavavej/mingw-distro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrep.patch
308 lines (292 loc) · 10.6 KB
/
grep.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
diff -aurN grep-2.10/lib/msvc-inval.c grep-2.10-fixed/lib/msvc-inval.c
--- grep-2.10/lib/msvc-inval.c 2011-09-28 02:54:46 -0700
+++ grep-2.10-fixed/lib/msvc-inval.c 2013-08-07 22:19:41 -0700
@@ -31,7 +31,7 @@
# if MSVC_INVALID_PARAMETER_HANDLING == DEFAULT_HANDLING
-static void cdecl
+static void __cdecl
gl_msvc_invalid_parameter_handler (const wchar_t *expression,
const wchar_t *function,
const wchar_t *file,
@@ -48,7 +48,7 @@
# if defined _MSC_VER
-static void cdecl
+static void __cdecl
gl_msvc_invalid_parameter_handler (const wchar_t *expression,
const wchar_t *function,
const wchar_t *file,
@@ -97,7 +97,7 @@
}
}
-static void cdecl
+static void __cdecl
gl_msvc_invalid_parameter_handler (const wchar_t *expression,
const wchar_t *function,
const wchar_t *file,
diff -aurN grep-2.10/src/main.c grep-2.10-fixed/src/main.c
--- grep-2.10/src/main.c 2011-09-10 00:17:04 -0700
+++ grep-2.10-fixed/src/main.c 2013-08-07 22:19:13 -0700
@@ -50,6 +50,10 @@
#include "xalloc.h"
#include "xstrtol.h"
+#define NOMINMAX
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+
#define SEP_CHAR_SELECTED ':'
#define SEP_CHAR_REJECTED '-'
#define SEP_STR_GROUP "--"
@@ -84,7 +88,7 @@
static int suppress_errors;
/* If nonzero, use color markers. */
-static int color_option;
+static int color_option = 1;
/* If nonzero, show only the part of a line matching the expression. */
static int only_matching;
@@ -140,14 +144,14 @@
/* The color strings used for matched text.
The user can overwrite them using the deprecated
environment variable GREP_COLOR or the new GREP_COLORS. */
-static const char *selected_match_color = "01;31"; /* bold red */
-static const char *context_match_color = "01;31"; /* bold red */
+static const char *selected_match_color = "32;1"; /* bold green */
+static const char *context_match_color = "32;1"; /* bold green */
/* Other colors. Defaults look damn good. */
-static const char *filename_color = "35"; /* magenta */
-static const char *line_num_color = "32"; /* green */
-static const char *byte_num_color = "32"; /* green */
-static const char *sep_color = "36"; /* cyan */
+static const char *filename_color = "36"; /* cyan */
+static const char *line_num_color = "33"; /* yellow */
+static const char *byte_num_color = "31"; /* red */
+static const char *sep_color = "35"; /* magenta */
static const char *selected_line_color = ""; /* default color pair */
static const char *context_line_color = ""; /* default color pair */
@@ -213,10 +217,167 @@
static const char *sgr_end = "\33[m\33[K";
#define SGR_END sgr_end
+void setWinColor_UNLOCKED(const char * const fmt, 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) {
+ printf("ERROR: setWinColor() - s is NULL.\n");
+ exit(EXIT_FAILURE);
+ } else if (strcmp(s, "") == 0) {
+ printf("ERROR: setWinColor() - s is empty.\n");
+ exit(EXIT_FAILURE);
+ } else if (strcmp(fmt, SGR_START) == 0) {
+ 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) {
+ printf("ERROR: setWinColor() - Gigantic number.\n");
+ 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 {
+ printf("ERROR: setWinColor() - Unrecognized number.\n");
+ exit(EXIT_FAILURE);
+ }
+
+ number = 0;
+ } else {
+ printf("ERROR: setWinColor() - Unrecognized character.\n");
+ 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);
+
+ } else if (strcmp(fmt, SGR_END) == 0) {
+ SetConsoleTextAttribute(hand, s_attributes);
+ } else {
+ printf("ERROR: setWinColor() - Unrecognized fmt.\n");
+ exit(EXIT_FAILURE);
+ }
+}
+
+CRITICAL_SECTION g_color_lock;
+BOOL g_color_bool = TRUE;
+
+void setWinColor(const char * const fmt, const char * const s) {
+ EnterCriticalSection(&g_color_lock);
+
+ if (g_color_bool) {
+ setWinColor_UNLOCKED(fmt, s);
+ }
+
+ LeaveCriticalSection(&g_color_lock);
+}
+
+void resetColor(void) {
+ setWinColor(SGR_END, "meow");
+}
+
+BOOL WINAPI colorHandler(const DWORD dwCtrlType) {
+ if (dwCtrlType == CTRL_C_EVENT || dwCtrlType == CTRL_BREAK_EVENT) {
+ EnterCriticalSection(&g_color_lock);
+ resetColor();
+ g_color_bool = FALSE;
+ LeaveCriticalSection(&g_color_lock);
+ }
+
+ return FALSE;
+}
+
/* SGR utility macros. */
-#define PR_SGR_FMT(fmt, s) do { if (*(s)) printf((fmt), (s)); } while (0)
+#define PR_SGR_FMT(fmt, s) do { if (*(s)) setWinColor((fmt), (s)); } while (0)
#define PR_SGR_FMT_IF(fmt, s) \
- do { if (color_option && *(s)) printf((fmt), (s)); } while (0)
+ do { if (color_option) { PR_SGR_FMT(fmt, s); } } while (0)
#define PR_SGR_START(s) PR_SGR_FMT( SGR_START, (s))
#define PR_SGR_END(s) PR_SGR_FMT( SGR_END, (s))
#define PR_SGR_START_IF(s) PR_SGR_FMT_IF(SGR_START, (s))
@@ -432,7 +593,8 @@
static char *buffer; /* Base of buffer. */
static size_t bufalloc; /* Allocated buffer size, counting slop. */
-#define INITIAL_BUFSIZE 32768 /* Initial buffer size, not counting slop. */
+/* http://savannah.gnu.org/bugs/?25414 */
+#define INITIAL_BUFSIZE 16384 /* Initial buffer size, not counting slop. */
static int bufdesc; /* File descriptor. */
static char *bufbeg; /* Beginning of user-visible stuff. */
static char *buflim; /* Limit of user-visible stuff. */
@@ -1779,6 +1941,15 @@
int default_context;
FILE *fp;
+ InitializeCriticalSectionAndSpinCount(&g_color_lock, 4000);
+
+ if (SetConsoleCtrlHandler(&colorHandler, TRUE) == 0) {
+ printf("ERROR: main() - SetConsoleCtrlHandler() failed.\n");
+ exit(EXIT_FAILURE);
+ }
+
+ atexit(resetColor);
+
exit_failure = EXIT_TROUBLE;
initialize_main (&argc, &argv);
set_program_name (argv[0]);
@@ -2039,20 +2210,11 @@
color_option = 0;
else if(!strcasecmp(optarg, "auto") || !strcasecmp(optarg, "tty") ||
!strcasecmp(optarg, "if-tty"))
- color_option = 2;
+ color_option = 1;
else
show_help = 1;
} else
- color_option = 2;
- if (color_option == 2)
- {
- char const *t;
- if (isatty (STDOUT_FILENO) && (t = getenv ("TERM"))
- && !STREQ (t, "dumb"))
- color_option = 1;
- else
- color_option = 0;
- }
+ color_option = 1;
break;
case EXCLUDE_OPTION:
diff -aurN grep-2.10/src/system.h grep-2.10-fixed/src/system.h
--- grep-2.10/src/system.h 2011-11-12 00:59:34 -0800
+++ grep-2.10-fixed/src/system.h 2013-08-07 22:19:13 -0700
@@ -34,9 +34,9 @@
#endif
#ifdef EISDIR
-# define is_EISDIR(e, f) ((e) == EISDIR)
+# define is_EISDIR(e, f) ((e) == EISDIR || ((e) == EACCES && isdir (f) && ((e) = EISDIR, 1)))
#else
-# define is_EISDIR(e, f) 0
+# define is_EISDIR(e, f) ((e) == EACCES && isdir (f))
#endif
#include <stdlib.h>