# HG changeset patch # User paulo@hit-nxdomain.opendns.com # Date 1273219338 25200 # Node ID a367f80b847b73817f3bf19240e188cd5a83ad84 # Parent 5ab8d6c1a37c677423a3d3ae1086155cc2309b3d add color and bold options diff -r 5ab8d6c1a37c -r a367f80b847b rcg.c --- a/rcg.c Thu May 06 23:21:30 2010 -0700 +++ b/rcg.c Fri May 07 01:02:18 2010 -0700 @@ -12,11 +12,61 @@ const size_t BUFSIZE = 5000; -const char *CLR_START = "\x1B[31"; +const char *_CLR_START = "\x1B["; +const char *_CLR_BOLD = "1;"; +char *CLR_START; const char *CLR_END = "m"; const char *CLR_CLEAR = "\x1B[0m"; +char *CLR_CLR; + +const char *CLR_FG_RED = "31"; + +typedef struct _colors { + char *name; + char *fg; + char *bg; +} Colors; + +const Colors COLORS[] = { + { "black", "30", "40" }, + { "red", "31", "41" }, + { "green", "32", "42" }, + { "brown", "33", "43" }, + { "blue", "34", "44" }, + { "magenta", "35", "45" }, + { "cyan", "36", "46" }, + { "white", "37", "47" }, + { "default", "39", "49" }, + { NULL }, +}; + +char *getFGColor(char *cname) +{ + Colors *c; + for (c = COLORS; c->name; c++) + { + if (strncmp(cname, c->name, strlen(c->name)) == 0) + return c->fg; + } + return NULL; +} + +char *getBGColor(char *cname) +{ + Colors *c; + for (c = COLORS; c->name; c++) + { + if (strncmp(cname, c->name, strlen(c->name)) == 0) + return c->bg; + } + return NULL; +} + int colorLine = 0; +int embolden = 0; +char *g_fg = "red"; +char *g_bg = ""; typedef enum _exit_code { EXIT_OK, @@ -51,15 +101,22 @@ static struct option long_options[] = { { "line", 0, 0, 'l' }, + { "bold", 0, 0, 'B' }, { 0, 0, 0, 0 }, }; int c; int l; - while ((c = getopt_long(argc, argv, "l", long_options, &l)) >= 0) + while ((c = getopt_long(argc, argv, "lBf:b:", long_options, &l)) >= 0) { if (c == 'l') colorLine = 1; + else if (c == 'B') + embolden = 1; + else if (c == 'f') + g_fg = strdup(optarg); + else if (c == 'b') + g_bg = strdup(optarg); } if (optind >= argc) @@ -84,7 +141,38 @@ regmatch_t *rem = calloc(1, sizeof(regmatch_t)); + if (embolden) + { + CLR_START = calloc(strlen(_CLR_START) + strlen(_CLR_BOLD) + 1, sizeof(char)); + strcpy(CLR_START, _CLR_START); + strcat(CLR_START, _CLR_BOLD); + } + else + CLR_START = strdup(_CLR_START); + + CLR_CLR = getFGColor(g_fg); + if (!CLR_CLR) + exit(args_error()); + if (strlen(g_bg) > 0) + { + char *fgcolor = getFGColor(g_fg); + char *bgcolor = getBGColor(g_bg); + if (bgcolor) + { + size_t l = strlen(fgcolor) + 1 + strlen(bgcolor); + CLR_CLR = calloc((l + 1), sizeof(char)); + if (!CLR_CLR) + exit(realloc_error()); + strcpy(CLR_CLR, fgcolor); + strcat(CLR_CLR, ";"); + strcat(CLR_CLR, bgcolor); + } + else + exit(args_error()); + } + size_t CLR_START_len = strlen(CLR_START); + size_t CLR_CLR_len = strlen(CLR_CLR); size_t CLR_END_len = strlen(CLR_END); size_t CLR_CLEAR_len = strlen(CLR_CLEAR); @@ -108,7 +196,7 @@ if (out_len >= (BUFSIZE - buf_pos)) out_len = (BUFSIZE - buf_pos) - 1; - out = (char *)realloc(out, out_len*sizeof(char)); + out = realloc(out, out_len*sizeof(char)); if (!out) exit(realloc_error()); @@ -129,8 +217,8 @@ if (re_err != REG_NOMATCH && so >= 0 && eo >= 0 && match_len > 0) { - out_len += CLR_START_len + CLR_END_len + CLR_CLEAR_len; - out = (char *)realloc(out, out_len*sizeof(char)); + out_len += CLR_START_len + CLR_CLR_len + CLR_END_len + CLR_CLEAR_len; + out = realloc(out, out_len*sizeof(char)); if (!out) exit(realloc_error()); strncpy(&out[out_pos], &buf[buf_pos], so); @@ -138,6 +226,8 @@ buf_pos += so; strncpy(&out[out_pos], CLR_START, CLR_START_len); out_pos += CLR_START_len; + strncpy(&out[out_pos], CLR_CLR, CLR_CLR_len); + out_pos += CLR_CLR_len; strncpy(&out[out_pos], CLR_END, CLR_END_len); out_pos += CLR_END_len; strncpy(&out[out_pos], &buf[buf_pos], match_len);