Mercurial > hg > index.fcgi > rcg > rcg-1
view rcg.c @ 16:74cf5cfa3ec1
rcg.c: add usage help statement
author | paulo@thepaulopc |
---|---|
date | Thu, 18 Nov 2010 00:31:44 -0800 |
parents | acf12a2e872d |
children | e618a3ff2027 |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <regex.h>
5 #include <string.h>
7 #include <unistd.h>
9 #define _GNU_SOURCE
10 #include <getopt.h>
12 const size_t BUFSIZE = 5000;
14 const char *_CLR_START = "\x1B[";
15 const char *_CLR_BOLD = "1;";
16 char *CLR_START;
17 const char *CLR_END = "m";
18 const char *CLR_CLEAR = "\x1B[0m";
20 typedef struct _colors {
21 char *name;
22 char *fg;
23 char *bg;
24 } Colors;
26 const Colors COLORS[] = {
27 { "black", "30", "40" },
28 { "red", "31", "41" },
29 { "green", "32", "42" },
30 { "brown", "33", "43" },
31 { "blue", "34", "44" },
32 { "magenta", "35", "45" },
33 { "cyan", "36", "46" },
34 { "white", "37", "47" },
35 { "default", "39", "49" },
36 { NULL },
37 };
39 char *getFGColor(char *cname)
40 {
41 Colors *c;
42 for (c = COLORS; c->name; c++)
43 {
44 if (strncmp(cname, c->name, strlen(c->name)) == 0)
45 return c->fg;
46 }
47 return NULL;
48 }
50 char *getBGColor(char *cname)
51 {
52 Colors *c;
53 for (c = COLORS; c->name; c++)
54 {
55 if (strncmp(cname, c->name, strlen(c->name)) == 0)
56 return c->bg;
57 }
58 return NULL;
59 }
61 int colorLine = 0;
62 int embolden = 0;
63 char *g_fg = "";
64 char *g_bg = "";
66 typedef enum _exit_code {
67 EXIT_OK,
68 EXIT_REALLOC_ERROR,
69 EXIT_ARGS_ERROR,
70 } Exit_code;
72 int re_error(int errcode, const regex_t *re)
73 {
74 char *err_string = calloc(BUFSIZE, sizeof(char));
75 regerror(errcode, re, err_string, BUFSIZE*sizeof(char));
76 fprintf(stderr, "%s \n", err_string);
77 free(err_string);
78 return errcode;
79 }
81 Exit_code realloc_error()
82 {
83 fprintf(stderr, "realloc() failure \n");
84 return EXIT_REALLOC_ERROR;
85 }
87 Exit_code args_error()
88 {
89 char *usage = "\
90 Usage: rcg [options] <PATTERN> \n\
91 \n\
92 Options: \n\
93 -l, --line Highlight whole line \n\
94 -B, --bold Bold \n\
95 -b <color> Background color \n\
96 -f <color> Foreground color \n\
97 \n\
98 <color> can be one of the following: \n\
99 ";
101 fprintf(stdout, usage);
103 Colors *c;
104 for (c = COLORS; c->name; c++)
105 fprintf(stdout, " %s \n", c->name);
107 return EXIT_ARGS_ERROR;
108 }
110 // returns regular expression argument
111 char *parseArgs(int argc, char **argv)
112 {
113 static struct option long_options[] =
114 {
115 { "line", 0, 0, 'l' },
116 { "bold", 0, 0, 'B' },
117 { 0, 0, 0, 0 },
118 };
120 int c;
121 int l;
122 while ((c = getopt_long(argc, argv, "lBf:b:", long_options, &l)) >= 0)
123 {
124 if (c == 'l')
125 colorLine = 1;
126 else if (c == 'B')
127 embolden = 1;
128 else if (c == 'f')
129 g_fg = strdup(optarg);
130 else if (c == 'b')
131 g_bg = strdup(optarg);
132 }
134 if (optind >= argc)
135 exit(args_error());
137 return argv[optind];
138 }
140 int main(int argc, char **argv)
141 {
142 char *re_expression = parseArgs(argc, argv);
144 //fprintf(stderr, "re_expression = %s \n", re_expression); //d/ 20100405 PBA
146 char *buf = calloc(BUFSIZE, sizeof(char));
148 int re_err;
149 regex_t *re = calloc(1, sizeof(regex_t));
150 re_err = regcomp(re, re_expression, REG_EXTENDED | REG_NEWLINE);
151 if (re_err != 0)
152 exit(re_error(re_err, re));
154 regmatch_t *rem = calloc(1, sizeof(regmatch_t));
156 if (embolden)
157 {
158 CLR_START = calloc(strlen(_CLR_START) + strlen(_CLR_BOLD) + 1, sizeof(char));
159 strcpy(CLR_START, _CLR_START);
160 strcat(CLR_START, _CLR_BOLD);
161 }
162 else
163 CLR_START = strdup(_CLR_START);
165 if (strlen(g_fg) == 0 && strlen(g_bg) == 0)
166 g_fg = "red";
167 else if (strlen(g_fg) == 0 && strlen(g_bg) > 0)
168 g_fg = "default";
170 char *fgcolor = getFGColor(g_fg);
171 if (!fgcolor)
172 exit(args_error());
174 char *clr = strdup(fgcolor);
175 if (strlen(g_bg) > 0)
176 {
177 char *bgcolor = getBGColor(g_bg);
178 if (bgcolor)
179 {
180 size_t l = strlen(clr) + 1 + strlen(bgcolor);
181 clr = realloc(clr, (l + 1)*sizeof(char));
182 if (!clr)
183 exit(realloc_error());
184 strcat(clr, ";");
185 strcat(clr, bgcolor);
186 }
187 else
188 exit(args_error());
189 }
191 char *out = NULL;
193 while (fgets(buf, BUFSIZE, stdin))
194 {
195 int so = -1;
196 int eo = -1;
198 size_t out_len = strlen(buf) + 1;
199 unsigned int out_pos = 0;
200 unsigned int buf_pos = 0;
202 for (re_err = 0; re_err != REG_NOMATCH;)
203 {
204 re_err = regexec(re, &buf[buf_pos], 1, rem, 0);
206 //fprintf(stderr, "%s", &buf[buf_pos]); //d// 20100405 PBA
208 if (out_len >= (BUFSIZE - buf_pos))
209 out_len = (BUFSIZE - buf_pos) - 1;
211 out = realloc(out, out_len*sizeof(char));
212 if (!out)
213 exit(realloc_error());
215 so = rem[0].rm_so;
216 eo = rem[0].rm_eo;
218 int match_len = eo - so;
220 if (colorLine && match_len > 0)
221 {
222 so = 0;
223 eo = out_len - 2;
224 match_len = eo - so;
225 }
227 //fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA
229 if (re_err != REG_NOMATCH && so >= 0 && eo >= 0 && match_len > 0)
230 {
231 size_t CLR_STRING_len = strlen(CLR_START) + strlen(clr) + strlen(CLR_END) + strlen(CLR_CLEAR);
233 out_len += CLR_STRING_len;
234 out = realloc(out, out_len*sizeof(char));
235 if (!out)
236 exit(realloc_error());
238 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);
240 buf_pos += match_len + so;
241 out_pos += CLR_STRING_len + match_len + so;
242 }
243 else
244 {
245 strncpy(&out[out_pos], &buf[buf_pos], out_len - out_pos);
246 out[out_len - 1] = '\0';
247 re_err = REG_NOMATCH;
248 }
249 }
251 fputs(out, stdout);
252 fflush(stdout);
253 }
255 return EXIT_OK;
256 }