diff rcg.c @ 5:26e28ec5fe87

rcg.c: add color string; has bug with realloc()
author paulo@twcdns.fastsearch.net
date Tue, 06 Apr 2010 01:25:30 -0700
parents 99770a6b967d
children 910e059abd6d
line diff
     1.1 --- a/rcg.c	Mon Apr 05 23:45:38 2010 -0700
     1.2 +++ b/rcg.c	Tue Apr 06 01:25:30 2010 -0700
     1.3 @@ -12,8 +12,9 @@
     1.4  
     1.5  const size_t BUFSIZE = 5000;
     1.6  
     1.7 -const char *CLR_START = "-CLR_START-";
     1.8 -const char *CLR_END = "-CLR_END-";
     1.9 +const char *CLR_START = "\x1B[31";
    1.10 +const char *CLR_END = "m";
    1.11 +const char *CLR_CLEAR = "\x1B[0m";
    1.12  
    1.13  typedef enum _exit_code {
    1.14  	EXIT_OK,
    1.15 @@ -66,7 +67,7 @@
    1.16  {
    1.17  	char *re_expression = parseArgs(argc, argv);
    1.18  
    1.19 -	fprintf(stderr, "re_expression = %s \n", re_expression);		//d/ 20100405 PBA
    1.20 +	//fprintf(stderr, "re_expression = %s \n", re_expression);		//d/ 20100405 PBA
    1.21  
    1.22  	char *buf = calloc(BUFSIZE, sizeof(char));
    1.23  
    1.24 @@ -80,6 +81,7 @@
    1.25  
    1.26  	size_t CLR_START_len = strlen(CLR_START);
    1.27  	size_t CLR_END_len = strlen(CLR_END);
    1.28 +	size_t CLR_CLEAR_len = strlen(CLR_CLEAR);
    1.29  
    1.30  	while (fgets(buf, BUFSIZE, stdin))
    1.31  	{
    1.32 @@ -108,7 +110,7 @@
    1.33  			if (re_err != REG_NOMATCH && so >= 0 && eo >= 0)
    1.34  			{
    1.35  				int match_len = eo - so;
    1.36 -				out_len += CLR_START_len + CLR_END_len;
    1.37 +				out_len += CLR_START_len + CLR_END_len + CLR_CLEAR_len;
    1.38  				out = (char *)realloc(out, out_len*sizeof(char));
    1.39  				if (!out)
    1.40  					exit(realloc_error());
    1.41 @@ -117,15 +119,20 @@
    1.42  				buf_pos += so;
    1.43  				strncpy(&out[out_pos], CLR_START, CLR_START_len);
    1.44  				out_pos += CLR_START_len;
    1.45 +				strncpy(&out[out_pos], CLR_END, CLR_END_len);
    1.46 +				out_pos += CLR_END_len;
    1.47  				strncpy(&out[out_pos], &buf[buf_pos], match_len);
    1.48  				out_pos += match_len;
    1.49 -				strncpy(&out[out_pos], CLR_END, CLR_END_len);
    1.50 -				out_pos += CLR_END_len;
    1.51 +				strncpy(&out[out_pos], CLR_CLEAR, CLR_CLEAR_len);
    1.52 +				out_pos += CLR_CLEAR_len;
    1.53  
    1.54  				buf_pos += match_len;
    1.55  			}
    1.56  			else
    1.57 +			{
    1.58  				strncpy(&out[out_pos], &buf[buf_pos], out_len - out_pos);
    1.59 +				out[out_len] = '\0';
    1.60 +			}
    1.61  		}
    1.62  
    1.63  		fputs(out, stdout);