view rcg.c @ 21:ebfc52615768

fix segfault with long input lines
author paulo
date Tue, 29 Jul 2014 23:42:57 -0700
parents e618a3ff2027
children b18d74b37e9e
line source
1 // rcg.c -- regex colored grep
2 // Copyright (C) 2010 by Paulo Ang (pbba13@gmail.com)
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <regex.h>
22 #include <string.h>
24 #include <unistd.h>
26 #define _GNU_SOURCE
27 #include <getopt.h>
29 const size_t BUFSIZE = 5000;
31 const char *_CLR_START = "\x1B[";
32 const char *_CLR_BOLD = "1;";
33 char *CLR_START;
34 const char *CLR_END = "m";
35 const char *CLR_CLEAR = "\x1B[0m";
37 typedef struct _colors {
38 char *name;
39 char *fg;
40 char *bg;
41 } Colors;
43 const Colors COLORS[] = {
44 { "black", "30", "40" },
45 { "red", "31", "41" },
46 { "green", "32", "42" },
47 { "brown", "33", "43" },
48 { "blue", "34", "44" },
49 { "magenta", "35", "45" },
50 { "cyan", "36", "46" },
51 { "white", "37", "47" },
52 { "default", "39", "49" },
53 { NULL },
54 };
56 char *getFGColor(char *cname)
57 {
58 Colors *c;
59 for (c = COLORS; c->name; c++)
60 {
61 if (strncmp(cname, c->name, strlen(c->name)) == 0)
62 return c->fg;
63 }
64 return NULL;
65 }
67 char *getBGColor(char *cname)
68 {
69 Colors *c;
70 for (c = COLORS; c->name; c++)
71 {
72 if (strncmp(cname, c->name, strlen(c->name)) == 0)
73 return c->bg;
74 }
75 return NULL;
76 }
78 int colorLine = 0;
79 int embolden = 0;
80 char *g_fg = "";
81 char *g_bg = "";
83 typedef enum _exit_code {
84 EXIT_OK,
85 EXIT_REALLOC_ERROR,
86 EXIT_ARGS_ERROR,
87 } Exit_code;
89 int re_error(int errcode, const regex_t *re)
90 {
91 char *err_string = calloc(BUFSIZE, sizeof(char));
92 regerror(errcode, re, err_string, BUFSIZE*sizeof(char));
93 fprintf(stderr, "%s \n", err_string);
94 free(err_string);
95 return errcode;
96 }
98 Exit_code realloc_error()
99 {
100 fprintf(stderr, "realloc() failure \n");
101 return EXIT_REALLOC_ERROR;
102 }
104 void *realloc_buffer(void *buffer, size_t buffer_len)
105 {
106 void *new_buffer = realloc(buffer, buffer_len);
107 if (!new_buffer)
108 exit(realloc_error());
109 return new_buffer;
110 }
112 Exit_code args_error()
113 {
114 char *usage = "\
115 Usage: rcg [options] <PATTERN> \n\
116 \n\
117 Options: \n\
118 -l, --line Highlight whole line \n\
119 -B, --bold Bold \n\
120 -b <color> Background color \n\
121 -f <color> Foreground color \n\
122 \n\
123 <color> can be one of the following: \n\
124 ";
126 fprintf(stdout, usage);
128 Colors *c;
129 for (c = COLORS; c->name; c++)
130 fprintf(stdout, " %s \n", c->name);
132 return EXIT_ARGS_ERROR;
133 }
135 // returns regular expression argument
136 char *parseArgs(int argc, char **argv)
137 {
138 static struct option long_options[] =
139 {
140 { "line", 0, 0, 'l' },
141 { "bold", 0, 0, 'B' },
142 { 0, 0, 0, 0 },
143 };
145 int c;
146 int l;
147 while ((c = getopt_long(argc, argv, "lBf:b:", long_options, &l)) >= 0)
148 {
149 if (c == 'l')
150 colorLine = 1;
151 else if (c == 'B')
152 embolden = 1;
153 else if (c == 'f')
154 g_fg = strdup(optarg);
155 else if (c == 'b')
156 g_bg = strdup(optarg);
157 }
159 if (optind >= argc)
160 exit(args_error());
162 return argv[optind];
163 }
165 int main(int argc, char **argv)
166 {
167 char *re_expression = parseArgs(argc, argv);
169 //fprintf(stderr, "re_expression = %s \n", re_expression); //d/ 20100405 PBA
171 char *buf = calloc(BUFSIZE, sizeof(char));
173 int re_err;
174 regex_t *re = calloc(1, sizeof(regex_t));
175 re_err = regcomp(re, re_expression, REG_EXTENDED | REG_NEWLINE);
176 if (re_err != 0)
177 exit(re_error(re_err, re));
179 regmatch_t *rem = calloc(1, sizeof(regmatch_t));
181 if (embolden)
182 {
183 CLR_START = calloc(strlen(_CLR_START) + strlen(_CLR_BOLD) + 1, sizeof(char));
184 strcpy(CLR_START, _CLR_START);
185 strcat(CLR_START, _CLR_BOLD);
186 }
187 else
188 CLR_START = strdup(_CLR_START);
190 if (strlen(g_fg) == 0 && strlen(g_bg) == 0)
191 g_fg = "red";
192 else if (strlen(g_fg) == 0 && strlen(g_bg) > 0)
193 g_fg = "default";
195 char *fgcolor = getFGColor(g_fg);
196 if (!fgcolor)
197 exit(args_error());
199 char *clr = strdup(fgcolor);
200 if (strlen(g_bg) > 0)
201 {
202 char *bgcolor = getBGColor(g_bg);
203 if (bgcolor)
204 {
205 size_t l = strlen(clr) + 1 + strlen(bgcolor);
206 clr = realloc_buffer(clr, (l + 1)*sizeof(char));
207 strcat(clr, ";");
208 strcat(clr, bgcolor);
209 }
210 else
211 exit(args_error());
212 }
214 char *out = NULL;
216 while (fgets(buf, BUFSIZE, stdin))
217 {
218 int so = -1;
219 int eo = -1;
221 size_t out_len = strlen(buf) + 1;
222 unsigned int out_pos = 0;
223 unsigned int buf_pos = 0;
225 for (re_err = 0; re_err != REG_NOMATCH;)
226 {
227 re_err = regexec(re, &buf[buf_pos], 1, rem, 0);
229 so = rem[0].rm_so;
230 eo = rem[0].rm_eo;
232 int match_len = eo - so;
234 if (colorLine && match_len > 0)
235 {
236 so = 0;
237 eo = out_len - 1;
238 match_len = eo - so;
239 }
241 if (re_err != REG_NOMATCH && so >= 0 && eo >= 0 && match_len > 0)
242 {
243 size_t CLR_STRING_len = strlen(CLR_START) + strlen(clr) + strlen(CLR_END) + strlen(CLR_CLEAR);
245 out_len += CLR_STRING_len;
246 out = realloc_buffer(out, out_len*sizeof(char));
248 snprintf(&out[out_pos], out_len, "%.*s%s%s%s%.*s%s", so, &buf[buf_pos], CLR_START, clr, CLR_END, match_len, &buf[buf_pos + so], CLR_CLEAR);
250 buf_pos += match_len + so;
251 out_pos += CLR_STRING_len + match_len + so;
252 }
253 else
254 {
255 out = realloc_buffer(out, out_len*sizeof(char));
256 strncpy(&out[out_pos], &buf[buf_pos], out_len - out_pos);
257 out[out_len - 1] = '\0';
258 re_err = REG_NOMATCH;
259 }
260 }
262 fputs(out, stdout);
263 fflush(stdout);
264 }
266 return EXIT_OK;
267 }