view rcg.c @ 9:a367f80b847b

add color and bold options
author paulo@hit-nxdomain.opendns.com
date Fri, 07 May 2010 01:02:18 -0700
parents 5ab8d6c1a37c
children 4ef47c3bdce6
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <regex.h>
5 #define __USE_GNU
6 #include <string.h>
8 #include <unistd.h>
10 #define _GNU_SOURCE
11 #include <getopt.h>
13 const size_t BUFSIZE = 5000;
15 const char *_CLR_START = "\x1B[";
16 const char *_CLR_BOLD = "1;";
17 char *CLR_START;
18 const char *CLR_END = "m";
19 const char *CLR_CLEAR = "\x1B[0m";
21 char *CLR_CLR;
23 const char *CLR_FG_RED = "31";
25 typedef struct _colors {
26 char *name;
27 char *fg;
28 char *bg;
29 } Colors;
31 const Colors COLORS[] = {
32 { "black", "30", "40" },
33 { "red", "31", "41" },
34 { "green", "32", "42" },
35 { "brown", "33", "43" },
36 { "blue", "34", "44" },
37 { "magenta", "35", "45" },
38 { "cyan", "36", "46" },
39 { "white", "37", "47" },
40 { "default", "39", "49" },
41 { NULL },
42 };
44 char *getFGColor(char *cname)
45 {
46 Colors *c;
47 for (c = COLORS; c->name; c++)
48 {
49 if (strncmp(cname, c->name, strlen(c->name)) == 0)
50 return c->fg;
51 }
52 return NULL;
53 }
55 char *getBGColor(char *cname)
56 {
57 Colors *c;
58 for (c = COLORS; c->name; c++)
59 {
60 if (strncmp(cname, c->name, strlen(c->name)) == 0)
61 return c->bg;
62 }
63 return NULL;
64 }
66 int colorLine = 0;
67 int embolden = 0;
68 char *g_fg = "red";
69 char *g_bg = "";
71 typedef enum _exit_code {
72 EXIT_OK,
73 EXIT_REALLOC_ERROR,
74 EXIT_ARGS_ERROR,
75 } Exit_code;
77 int re_error(int errcode, const regex_t *re)
78 {
79 char *err_string = calloc(BUFSIZE, sizeof(char));
80 regerror(errcode, re, err_string, BUFSIZE*sizeof(char));
81 fprintf(stderr, "%s \n", err_string);
82 free(err_string);
83 return errcode;
84 }
86 Exit_code realloc_error()
87 {
88 fprintf(stderr, "realloc() failure \n");
89 return EXIT_REALLOC_ERROR;
90 }
92 Exit_code args_error()
93 {
94 fprintf(stderr, "Invalid or nonexistent argument. \n");
95 return EXIT_ARGS_ERROR;
96 }
98 // returns regular expression argument
99 char *parseArgs(int argc, char **argv)
100 {
101 static struct option long_options[] =
102 {
103 { "line", 0, 0, 'l' },
104 { "bold", 0, 0, 'B' },
105 { 0, 0, 0, 0 },
106 };
108 int c;
109 int l;
110 while ((c = getopt_long(argc, argv, "lBf:b:", long_options, &l)) >= 0)
111 {
112 if (c == 'l')
113 colorLine = 1;
114 else if (c == 'B')
115 embolden = 1;
116 else if (c == 'f')
117 g_fg = strdup(optarg);
118 else if (c == 'b')
119 g_bg = strdup(optarg);
120 }
122 if (optind >= argc)
123 exit(args_error());
125 return argv[optind];
126 }
128 int main(int argc, char **argv)
129 {
130 char *re_expression = parseArgs(argc, argv);
132 //fprintf(stderr, "re_expression = %s \n", re_expression); //d/ 20100405 PBA
134 char *buf = calloc(BUFSIZE, sizeof(char));
136 int re_err;
137 regex_t *re = calloc(1, sizeof(regex_t));
138 re_err = regcomp(re, re_expression, REG_EXTENDED);
139 if (re_err != 0)
140 exit(re_error(re_err, re));
142 regmatch_t *rem = calloc(1, sizeof(regmatch_t));
144 if (embolden)
145 {
146 CLR_START = calloc(strlen(_CLR_START) + strlen(_CLR_BOLD) + 1, sizeof(char));
147 strcpy(CLR_START, _CLR_START);
148 strcat(CLR_START, _CLR_BOLD);
149 }
150 else
151 CLR_START = strdup(_CLR_START);
153 CLR_CLR = getFGColor(g_fg);
154 if (!CLR_CLR)
155 exit(args_error());
156 if (strlen(g_bg) > 0)
157 {
158 char *fgcolor = getFGColor(g_fg);
159 char *bgcolor = getBGColor(g_bg);
160 if (bgcolor)
161 {
162 size_t l = strlen(fgcolor) + 1 + strlen(bgcolor);
163 CLR_CLR = calloc((l + 1), sizeof(char));
164 if (!CLR_CLR)
165 exit(realloc_error());
166 strcpy(CLR_CLR, fgcolor);
167 strcat(CLR_CLR, ";");
168 strcat(CLR_CLR, bgcolor);
169 }
170 else
171 exit(args_error());
172 }
174 size_t CLR_START_len = strlen(CLR_START);
175 size_t CLR_CLR_len = strlen(CLR_CLR);
176 size_t CLR_END_len = strlen(CLR_END);
177 size_t CLR_CLEAR_len = strlen(CLR_CLEAR);
179 char *out = NULL;
181 while (fgets(buf, BUFSIZE, stdin))
182 {
183 int so = -1;
184 int eo = -1;
186 size_t out_len = strlen(buf) + 1;
187 unsigned int out_pos = 0;
188 unsigned int buf_pos = 0;
190 for (re_err = 0; re_err != REG_NOMATCH;)
191 {
192 re_err = regexec(re, &buf[buf_pos], 1, rem, 0);
194 //fprintf(stderr, "%s", &buf[buf_pos]); //d// 20100405 PBA
196 if (out_len >= (BUFSIZE - buf_pos))
197 out_len = (BUFSIZE - buf_pos) - 1;
199 out = realloc(out, out_len*sizeof(char));
200 if (!out)
201 exit(realloc_error());
203 if (colorLine)
204 {
205 so = 0;
206 eo = out_len - 1;
207 }
208 else
209 {
210 so = rem[0].rm_so;
211 eo = rem[0].rm_eo;
212 }
214 int match_len = eo - so;
216 //fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA
218 if (re_err != REG_NOMATCH && so >= 0 && eo >= 0 && match_len > 0)
219 {
220 out_len += CLR_START_len + CLR_CLR_len + CLR_END_len + CLR_CLEAR_len;
221 out = realloc(out, out_len*sizeof(char));
222 if (!out)
223 exit(realloc_error());
224 strncpy(&out[out_pos], &buf[buf_pos], so);
225 out_pos += so;
226 buf_pos += so;
227 strncpy(&out[out_pos], CLR_START, CLR_START_len);
228 out_pos += CLR_START_len;
229 strncpy(&out[out_pos], CLR_CLR, CLR_CLR_len);
230 out_pos += CLR_CLR_len;
231 strncpy(&out[out_pos], CLR_END, CLR_END_len);
232 out_pos += CLR_END_len;
233 strncpy(&out[out_pos], &buf[buf_pos], match_len);
234 out_pos += match_len;
235 strncpy(&out[out_pos], CLR_CLEAR, CLR_CLEAR_len);
236 out_pos += CLR_CLEAR_len;
238 buf_pos += match_len;
239 }
240 else
241 {
242 strncpy(&out[out_pos], &buf[buf_pos], out_len - out_pos);
243 out[out_len - 1] = '\0';
244 }
245 }
247 fputs(out, stdout);
248 }
250 return EXIT_OK;
251 }