-
Notifications
You must be signed in to change notification settings - Fork 0
/
hl.c
184 lines (161 loc) · 4.06 KB
/
hl.c
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
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ESC_COLOR_INDEX_MAX 6
typedef struct hl_option {
int cflags;
int color_index;
int is_bright;
char *pattern;
int max_width;
} hl_option_t;
int esc_colors[ESC_COLOR_INDEX_MAX] = {
31, // red
32, // green
33, // yello
34, // blue
35, // magenta
36, // cyan
};
int process_option(char option, hl_option_t *hl_opt) {
int result = -1;
switch (option) {
case 's': // make short width, ignore long line
hl_opt->max_width = 72;
break;
case 'w': // capture warning
hl_opt->cflags |= REG_ICASE;
hl_opt->cflags |= REG_EXTENDED;
hl_opt->pattern = "warn|warning";
hl_opt->color_index = 2;
break;
case 'e': // capture error
hl_opt->cflags |= REG_ICASE;
hl_opt->cflags |= REG_EXTENDED;
hl_opt->pattern = "err|error|fail|abort";
hl_opt->color_index = 0;
break;
case 'o': // capture error
hl_opt->cflags |= REG_ICASE;
hl_opt->cflags |= REG_EXTENDED;
hl_opt->pattern = "ok|good|nice|fine|success";
hl_opt->color_index = 1;
break;
case 'i': // ignore string case
hl_opt->cflags |= REG_ICASE;
break;
case 'E': // support extended
hl_opt->cflags |= REG_EXTENDED;
break;
case 'c': // change color index
hl_opt->color_index += 1;
hl_opt->color_index = hl_opt->color_index % ESC_COLOR_INDEX_MAX;
break;
case 'b': // set bright text color
hl_opt->is_bright = 1;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
hl_opt->color_index = option - '0';
break;
case 'h': // print help
printf("i : ignore upper/lower case\n"
"E : support extended regex\n"
"c : change to next color index\n"
"0~5 : change color index\n"
"h : help\n"
"");
result = 0; // normal option but need to quit
break;
default:
fprintf(stderr, "unknown option %c \n", option);
result = 1;
break;
}
return result;
}
void print_esc(int code) { printf("\033[%dm", code); }
int parse_options(char *options, hl_option_t *hl_opt) {
int result = -1;
for (int i = 1; i < strlen(options); i++) {
result = process_option(options[i], hl_opt);
if (result != -1) {
break;
}
}
return result;
}
int main(int argc, char *argv[]) {
char *buffer = NULL;
size_t bufferLen = 0;
ssize_t read;
regex_t regex;
int cflags = 0;
hl_option_t hl_opt = {
0,
};
hl_opt.max_width = -1;
hl_opt.pattern = NULL;
if (argc < 2) {
fprintf(stderr, "argument not founded\n");
return 1;
}
for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
int result = parse_options(argv[i], &hl_opt);
if (result != -1) {
return result;
}
} else if (hl_opt.pattern == NULL) {
hl_opt.pattern = argv[i];
break; // ignore after arguments
}
}
if (hl_opt.pattern == NULL) {
fprintf(stderr, "pattern not found\n");
return 1;
}
if (regcomp(®ex, hl_opt.pattern, hl_opt.cflags) != 0) {
fprintf(stderr, "regex compilation error\n");
return 1;
}
while ((read = getline(&buffer, &bufferLen, stdin)) != -1) {
int start = 0;
regmatch_t match;
while (regexec(®ex, buffer + start, 1, &match, 0) == 0) {
int match_start = match.rm_so + start;
int match_end = match.rm_eo + start;
print_esc(1); // bold
for (int i = start; i < match_start; i++) {
putchar(buffer[i]);
}
print_esc(esc_colors[hl_opt.color_index] +
(hl_opt.is_bright == 1 ? 60 : 0));
for (int i = match_start; i < match_end; i++) {
putchar(buffer[i]);
}
print_esc(0); // reset
print_esc(1); // bold
start = match_end;
}
for (int i = start; i < read; i++) {
if (i == read - 1) {
print_esc(0); // reset
} else if (hl_opt.max_width != -1 && i > hl_opt.max_width) {
print_esc(0);
printf("(....)\n");
i = read;
}
putchar(buffer[i]);
}
fflush(stdout);
}
regfree(®ex);
free(buffer);
return 0;
}