Mercurial > hg > index.fcgi > rcg > rcg-1
comparison 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 |
comparison
equal
deleted
inserted
replaced
7:cc57cc6c1e45 | 8:55ec37f190c7 |
---|---|
10 #define _GNU_SOURCE | 10 #define _GNU_SOURCE |
11 #include <getopt.h> | 11 #include <getopt.h> |
12 | 12 |
13 const size_t BUFSIZE = 5000; | 13 const size_t BUFSIZE = 5000; |
14 | 14 |
15 const char *CLR_START = "\x1B[31"; | 15 const char *_CLR_START = "\x1B["; |
16 const char *_CLR_BOLD = "1;"; | |
17 char *CLR_START; | |
16 const char *CLR_END = "m"; | 18 const char *CLR_END = "m"; |
17 const char *CLR_CLEAR = "\x1B[0m"; | 19 const char *CLR_CLEAR = "\x1B[0m"; |
18 | 20 |
21 char *CLR_CLR; | |
22 | |
23 const char *CLR_FG_RED = "31"; | |
24 | |
25 typedef struct _colors { | |
26 char *name; | |
27 char *fg; | |
28 char *bg; | |
29 } Colors; | |
30 | |
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 }; | |
43 | |
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 } | |
54 | |
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 } | |
65 | |
19 int colorLine = 0; | 66 int colorLine = 0; |
67 int embolden = 0; | |
68 char *g_fg = "red"; | |
69 char *g_bg = ""; | |
20 | 70 |
21 typedef enum _exit_code { | 71 typedef enum _exit_code { |
22 EXIT_OK, | 72 EXIT_OK, |
23 EXIT_REALLOC_ERROR, | 73 EXIT_REALLOC_ERROR, |
24 EXIT_ARGS_ERROR, | 74 EXIT_ARGS_ERROR, |
49 char *parseArgs(int argc, char **argv) | 99 char *parseArgs(int argc, char **argv) |
50 { | 100 { |
51 static struct option long_options[] = | 101 static struct option long_options[] = |
52 { | 102 { |
53 { "line", 0, 0, 'l' }, | 103 { "line", 0, 0, 'l' }, |
104 { "bold", 0, 0, 'B' }, | |
54 { 0, 0, 0, 0 }, | 105 { 0, 0, 0, 0 }, |
55 }; | 106 }; |
56 | 107 |
57 int c; | 108 int c; |
58 int l; | 109 int l; |
59 while ((c = getopt_long(argc, argv, "l", long_options, &l)) >= 0) | 110 while ((c = getopt_long(argc, argv, "lBf:b:", long_options, &l)) >= 0) |
60 { | 111 { |
61 if (c == 'l') | 112 if (c == 'l') |
62 colorLine = 1; | 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); | |
63 } | 120 } |
64 | 121 |
65 if (optind >= argc) | 122 if (optind >= argc) |
66 exit(args_error()); | 123 exit(args_error()); |
67 | 124 |
82 if (re_err != 0) | 139 if (re_err != 0) |
83 exit(re_error(re_err, re)); | 140 exit(re_error(re_err, re)); |
84 | 141 |
85 regmatch_t *rem = calloc(1, sizeof(regmatch_t)); | 142 regmatch_t *rem = calloc(1, sizeof(regmatch_t)); |
86 | 143 |
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); | |
152 | |
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 } | |
173 | |
87 size_t CLR_START_len = strlen(CLR_START); | 174 size_t CLR_START_len = strlen(CLR_START); |
175 size_t CLR_CLR_len = strlen(CLR_CLR); | |
88 size_t CLR_END_len = strlen(CLR_END); | 176 size_t CLR_END_len = strlen(CLR_END); |
89 size_t CLR_CLEAR_len = strlen(CLR_CLEAR); | 177 size_t CLR_CLEAR_len = strlen(CLR_CLEAR); |
90 | 178 |
91 char *out = NULL; | 179 char *out = NULL; |
92 | 180 |
106 //fprintf(stderr, "%s", &buf[buf_pos]); //d// 20100405 PBA | 194 //fprintf(stderr, "%s", &buf[buf_pos]); //d// 20100405 PBA |
107 | 195 |
108 if (out_len >= (BUFSIZE - buf_pos)) | 196 if (out_len >= (BUFSIZE - buf_pos)) |
109 out_len = (BUFSIZE - buf_pos) - 1; | 197 out_len = (BUFSIZE - buf_pos) - 1; |
110 | 198 |
111 out = (char *)realloc(out, out_len*sizeof(char)); | 199 out = realloc(out, out_len*sizeof(char)); |
112 if (!out) | 200 if (!out) |
113 exit(realloc_error()); | 201 exit(realloc_error()); |
114 | 202 |
115 if (colorLine) | 203 if (colorLine) |
116 { | 204 { |
127 | 215 |
128 //fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA | 216 //fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA |
129 | 217 |
130 if (re_err != REG_NOMATCH && so >= 0 && eo >= 0 && match_len > 0) | 218 if (re_err != REG_NOMATCH && so >= 0 && eo >= 0 && match_len > 0) |
131 { | 219 { |
132 out_len += CLR_START_len + CLR_END_len + CLR_CLEAR_len; | 220 out_len += CLR_START_len + CLR_CLR_len + CLR_END_len + CLR_CLEAR_len; |
133 out = (char *)realloc(out, out_len*sizeof(char)); | 221 out = realloc(out, out_len*sizeof(char)); |
134 if (!out) | 222 if (!out) |
135 exit(realloc_error()); | 223 exit(realloc_error()); |
136 strncpy(&out[out_pos], &buf[buf_pos], so); | 224 strncpy(&out[out_pos], &buf[buf_pos], so); |
137 out_pos += so; | 225 out_pos += so; |
138 buf_pos += so; | 226 buf_pos += so; |
139 strncpy(&out[out_pos], CLR_START, CLR_START_len); | 227 strncpy(&out[out_pos], CLR_START, CLR_START_len); |
140 out_pos += 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; | |
141 strncpy(&out[out_pos], CLR_END, CLR_END_len); | 231 strncpy(&out[out_pos], CLR_END, CLR_END_len); |
142 out_pos += CLR_END_len; | 232 out_pos += CLR_END_len; |
143 strncpy(&out[out_pos], &buf[buf_pos], match_len); | 233 strncpy(&out[out_pos], &buf[buf_pos], match_len); |
144 out_pos += match_len; | 234 out_pos += match_len; |
145 strncpy(&out[out_pos], CLR_CLEAR, CLR_CLEAR_len); | 235 strncpy(&out[out_pos], CLR_CLEAR, CLR_CLEAR_len); |