# HG changeset patch # User paulo@twcdns.fastsearch.net # Date 1270542330 25200 # Node ID 26e28ec5fe8744c1d686dfe59107c574f9928955 # Parent 99770a6b967d9dde77c0cb70b8597444f4220c4d rcg.c: add color string; has bug with realloc() diff -r 99770a6b967d -r 26e28ec5fe87 rcg.c --- a/rcg.c Mon Apr 05 23:45:38 2010 -0700 +++ b/rcg.c Tue Apr 06 01:25:30 2010 -0700 @@ -12,8 +12,9 @@ const size_t BUFSIZE = 5000; -const char *CLR_START = "-CLR_START-"; -const char *CLR_END = "-CLR_END-"; +const char *CLR_START = "\x1B[31"; +const char *CLR_END = "m"; +const char *CLR_CLEAR = "\x1B[0m"; typedef enum _exit_code { EXIT_OK, @@ -66,7 +67,7 @@ { char *re_expression = parseArgs(argc, argv); - fprintf(stderr, "re_expression = %s \n", re_expression); //d/ 20100405 PBA + //fprintf(stderr, "re_expression = %s \n", re_expression); //d/ 20100405 PBA char *buf = calloc(BUFSIZE, sizeof(char)); @@ -80,6 +81,7 @@ size_t CLR_START_len = strlen(CLR_START); size_t CLR_END_len = strlen(CLR_END); + size_t CLR_CLEAR_len = strlen(CLR_CLEAR); while (fgets(buf, BUFSIZE, stdin)) { @@ -108,7 +110,7 @@ if (re_err != REG_NOMATCH && so >= 0 && eo >= 0) { int match_len = eo - so; - out_len += CLR_START_len + CLR_END_len; + out_len += CLR_START_len + CLR_END_len + CLR_CLEAR_len; out = (char *)realloc(out, out_len*sizeof(char)); if (!out) exit(realloc_error()); @@ -117,15 +119,20 @@ buf_pos += so; strncpy(&out[out_pos], CLR_START, CLR_START_len); out_pos += CLR_START_len; + strncpy(&out[out_pos], CLR_END, CLR_END_len); + out_pos += CLR_END_len; strncpy(&out[out_pos], &buf[buf_pos], match_len); out_pos += match_len; - strncpy(&out[out_pos], CLR_END, CLR_END_len); - out_pos += CLR_END_len; + strncpy(&out[out_pos], CLR_CLEAR, CLR_CLEAR_len); + out_pos += CLR_CLEAR_len; buf_pos += match_len; } else + { strncpy(&out[out_pos], &buf[buf_pos], out_len - out_pos); + out[out_len] = '\0'; + } } fputs(out, stdout);