comparison rcg.c @ 12:9ec52ba4b994

rcg.c: fix bugs with background highlight option
author paulo@thepaulopc
date Sat, 06 Nov 2010 00:21:23 -0700
parents 4ef47c3bdce6
children 2fb87616ab65
comparison
equal deleted inserted replaced
9:69cd23b6ce0e 10:acb4b8c29fe4
1 #include <stdio.h> 1 #include <stdio.h>
2 #include <stdlib.h> 2 #include <stdlib.h>
3 #include <regex.h> 3 #include <regex.h>
4 4
5 #define __USE_GNU
6 #include <string.h> 5 #include <string.h>
7 6
8 #include <unistd.h> 7 #include <unistd.h>
9 8
10 #define _GNU_SOURCE 9 #define _GNU_SOURCE
15 const char *_CLR_START = "\x1B["; 14 const char *_CLR_START = "\x1B[";
16 const char *_CLR_BOLD = "1;"; 15 const char *_CLR_BOLD = "1;";
17 char *CLR_START; 16 char *CLR_START;
18 const char *CLR_END = "m"; 17 const char *CLR_END = "m";
19 const char *CLR_CLEAR = "\x1B[0m"; 18 const char *CLR_CLEAR = "\x1B[0m";
20
21 char *CLR_CLR;
22
23 const char *CLR_FG_RED = "31";
24 19
25 typedef struct _colors { 20 typedef struct _colors {
26 char *name; 21 char *name;
27 char *fg; 22 char *fg;
28 char *bg; 23 char *bg;
63 return NULL; 58 return NULL;
64 } 59 }
65 60
66 int colorLine = 0; 61 int colorLine = 0;
67 int embolden = 0; 62 int embolden = 0;
68 char *g_fg = "red"; 63 char *g_fg = "";
69 char *g_bg = ""; 64 char *g_bg = "";
70 65
71 typedef enum _exit_code { 66 typedef enum _exit_code {
72 EXIT_OK, 67 EXIT_OK,
73 EXIT_REALLOC_ERROR, 68 EXIT_REALLOC_ERROR,
148 strcat(CLR_START, _CLR_BOLD); 143 strcat(CLR_START, _CLR_BOLD);
149 } 144 }
150 else 145 else
151 CLR_START = strdup(_CLR_START); 146 CLR_START = strdup(_CLR_START);
152 147
153 CLR_CLR = getFGColor(g_fg); 148 if (strlen(g_fg) == 0 && strlen(g_bg) == 0)
154 if (!CLR_CLR) 149 g_fg = "red";
150 else if (strlen(g_fg) == 0 && strlen(g_bg) > 0)
151 g_fg = "default";
152
153 char *fgcolor = getFGColor(g_fg);
154 if (!fgcolor)
155 exit(args_error()); 155 exit(args_error());
156
157 char *clr = strdup(fgcolor);
156 if (strlen(g_bg) > 0) 158 if (strlen(g_bg) > 0)
157 { 159 {
158 char *fgcolor = getFGColor(g_fg);
159 char *bgcolor = getBGColor(g_bg); 160 char *bgcolor = getBGColor(g_bg);
160 if (bgcolor) 161 if (bgcolor)
161 { 162 {
162 size_t l = strlen(fgcolor) + 1 + strlen(bgcolor); 163 size_t l = strlen(clr) + 1 + strlen(bgcolor);
163 CLR_CLR = calloc((l + 1), sizeof(char)); 164 clr = realloc(clr, (l + 1)*sizeof(char));
164 if (!CLR_CLR) 165 if (!clr)
165 exit(realloc_error()); 166 exit(realloc_error());
166 strcpy(CLR_CLR, fgcolor); 167 strcat(clr, ";");
167 strcat(CLR_CLR, ";"); 168 strcat(clr, bgcolor);
168 strcat(CLR_CLR, bgcolor);
169 } 169 }
170 else 170 else
171 exit(args_error()); 171 exit(args_error());
172 } 172 }
173 173
174 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); 175 size_t clr_len = strlen(clr);
176 size_t CLR_END_len = strlen(CLR_END); 176 size_t CLR_END_len = strlen(CLR_END);
177 size_t CLR_CLEAR_len = strlen(CLR_CLEAR); 177 size_t CLR_CLEAR_len = strlen(CLR_CLEAR);
178 178
179 char *out = NULL; 179 char *out = NULL;
180 180
201 exit(realloc_error()); 201 exit(realloc_error());
202 202
203 if (colorLine) 203 if (colorLine)
204 { 204 {
205 so = 0; 205 so = 0;
206 eo = out_len - 1; 206 eo = out_len - 2;
207 } 207 }
208 else 208 else
209 { 209 {
210 so = rem[0].rm_so; 210 so = rem[0].rm_so;
211 eo = rem[0].rm_eo; 211 eo = rem[0].rm_eo;
215 215
216 //fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA 216 //fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA
217 217
218 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)
219 { 219 {
220 out_len += CLR_START_len + CLR_CLR_len + CLR_END_len + CLR_CLEAR_len; 220 out_len += CLR_START_len + clr_len + CLR_END_len + CLR_CLEAR_len;
221 out = realloc(out, out_len*sizeof(char)); 221 out = realloc(out, out_len*sizeof(char));
222 if (!out) 222 if (!out)
223 exit(realloc_error()); 223 exit(realloc_error());
224 strncpy(&out[out_pos], &buf[buf_pos], so); 224 strncpy(&out[out_pos], &buf[buf_pos], so);
225 out_pos += so; 225 out_pos += so;
226 buf_pos += so; 226 buf_pos += so;
227 strncpy(&out[out_pos], CLR_START, CLR_START_len); 227 strncpy(&out[out_pos], CLR_START, CLR_START_len);
228 out_pos += CLR_START_len; 228 out_pos += CLR_START_len;
229 strncpy(&out[out_pos], CLR_CLR, CLR_CLR_len); 229 strncpy(&out[out_pos], clr, clr_len);
230 out_pos += CLR_CLR_len; 230 out_pos += clr_len;
231 strncpy(&out[out_pos], CLR_END, CLR_END_len); 231 strncpy(&out[out_pos], CLR_END, CLR_END_len);
232 out_pos += CLR_END_len; 232 out_pos += CLR_END_len;
233 strncpy(&out[out_pos], &buf[buf_pos], match_len); 233 strncpy(&out[out_pos], &buf[buf_pos], match_len);
234 out_pos += match_len; 234 out_pos += match_len;
235 strncpy(&out[out_pos], CLR_CLEAR, CLR_CLEAR_len); 235 strncpy(&out[out_pos], CLR_CLEAR, CLR_CLEAR_len);