changeset 12:9ec52ba4b994

rcg.c: fix bugs with background highlight option
author paulo@thepaulopc
date Sat, 06 Nov 2010 00:21:23 -0700
parents 6627edb46f36
children 2fb87616ab65
files rcg.c
diffstat 1 files changed, 20 insertions(+), 20 deletions(-) [+]
line diff
     1.1 --- a/rcg.c	Sat Nov 06 00:17:34 2010 -0700
     1.2 +++ b/rcg.c	Sat Nov 06 00:21:23 2010 -0700
     1.3 @@ -2,7 +2,6 @@
     1.4  #include <stdlib.h>
     1.5  #include <regex.h>
     1.6  
     1.7 -#define __USE_GNU
     1.8  #include <string.h>
     1.9  
    1.10  #include <unistd.h>
    1.11 @@ -18,10 +17,6 @@
    1.12  const char *CLR_END = "m";
    1.13  const char *CLR_CLEAR = "\x1B[0m";
    1.14  
    1.15 -char *CLR_CLR;
    1.16 -
    1.17 -const char *CLR_FG_RED = "31";
    1.18 -
    1.19  typedef struct _colors {
    1.20  	char *name;
    1.21  	char *fg;
    1.22 @@ -65,7 +60,7 @@
    1.23  
    1.24  int colorLine = 0;
    1.25  int embolden = 0;
    1.26 -char *g_fg = "red";
    1.27 +char *g_fg = "";
    1.28  char *g_bg = "";
    1.29  
    1.30  typedef enum _exit_code {
    1.31 @@ -150,29 +145,34 @@
    1.32  	else
    1.33  		CLR_START = strdup(_CLR_START);
    1.34  
    1.35 -	CLR_CLR = getFGColor(g_fg);
    1.36 -	if (!CLR_CLR)
    1.37 +	if (strlen(g_fg) == 0 && strlen(g_bg) == 0)
    1.38 +		g_fg = "red";
    1.39 +	else if (strlen(g_fg) == 0 && strlen(g_bg) > 0)
    1.40 +		g_fg = "default";
    1.41 +
    1.42 +	char *fgcolor = getFGColor(g_fg);
    1.43 +	if (!fgcolor)
    1.44  		exit(args_error());
    1.45 +
    1.46 +	char *clr = strdup(fgcolor);
    1.47  	if (strlen(g_bg) > 0)
    1.48  	{
    1.49 -		char *fgcolor = getFGColor(g_fg);
    1.50  		char *bgcolor = getBGColor(g_bg);
    1.51  		if (bgcolor)
    1.52  		{
    1.53 -			size_t l = strlen(fgcolor) + 1 + strlen(bgcolor);
    1.54 -			CLR_CLR = calloc((l + 1), sizeof(char));
    1.55 -			if (!CLR_CLR)
    1.56 +			size_t l = strlen(clr) + 1 + strlen(bgcolor);
    1.57 +			clr = realloc(clr, (l + 1)*sizeof(char));
    1.58 +			if (!clr)
    1.59  				exit(realloc_error());
    1.60 -			strcpy(CLR_CLR, fgcolor);
    1.61 -			strcat(CLR_CLR, ";");
    1.62 -			strcat(CLR_CLR, bgcolor);
    1.63 +			strcat(clr, ";");
    1.64 +			strcat(clr, bgcolor);
    1.65  		}
    1.66  		else
    1.67  			exit(args_error());
    1.68  	}
    1.69  
    1.70  	size_t CLR_START_len = strlen(CLR_START);
    1.71 -	size_t CLR_CLR_len = strlen(CLR_CLR);
    1.72 +	size_t clr_len = strlen(clr);
    1.73  	size_t CLR_END_len = strlen(CLR_END);
    1.74  	size_t CLR_CLEAR_len = strlen(CLR_CLEAR);
    1.75  
    1.76 @@ -203,7 +203,7 @@
    1.77  			if (colorLine)
    1.78  			{
    1.79  				so = 0;
    1.80 -				eo = out_len - 1;
    1.81 +				eo = out_len - 2;
    1.82  			}
    1.83  			else
    1.84  			{
    1.85 @@ -217,7 +217,7 @@
    1.86  
    1.87  			if (re_err != REG_NOMATCH && so >= 0 && eo >= 0 && match_len > 0)
    1.88  			{
    1.89 -				out_len += CLR_START_len + CLR_CLR_len + CLR_END_len + CLR_CLEAR_len;
    1.90 +				out_len += CLR_START_len + clr_len + CLR_END_len + CLR_CLEAR_len;
    1.91  				out = realloc(out, out_len*sizeof(char));
    1.92  				if (!out)
    1.93  					exit(realloc_error());
    1.94 @@ -226,8 +226,8 @@
    1.95  				buf_pos += so;
    1.96  				strncpy(&out[out_pos], CLR_START, CLR_START_len);
    1.97  				out_pos += CLR_START_len;
    1.98 -				strncpy(&out[out_pos], CLR_CLR, CLR_CLR_len);
    1.99 -				out_pos += CLR_CLR_len;
   1.100 +				strncpy(&out[out_pos], clr, clr_len);
   1.101 +				out_pos += clr_len;
   1.102  				strncpy(&out[out_pos], CLR_END, CLR_END_len);
   1.103  				out_pos += CLR_END_len;
   1.104  				strncpy(&out[out_pos], &buf[buf_pos], match_len);