comparison rcg.c @ 8:5ab8d6c1a37c

add line coloring option
author paulo@hit-nxdomain.opendns.com
date Thu, 06 May 2010 23:21:30 -0700
parents 0b9cf47655fe
children a367f80b847b
comparison
equal deleted inserted replaced
6:2bc53e45316e 7:cc57cc6c1e45
13 const size_t BUFSIZE = 5000; 13 const size_t BUFSIZE = 5000;
14 14
15 const char *CLR_START = "\x1B[31"; 15 const char *CLR_START = "\x1B[31";
16 const char *CLR_END = "m"; 16 const char *CLR_END = "m";
17 const char *CLR_CLEAR = "\x1B[0m"; 17 const char *CLR_CLEAR = "\x1B[0m";
18
19 int colorLine = 0;
18 20
19 typedef enum _exit_code { 21 typedef enum _exit_code {
20 EXIT_OK, 22 EXIT_OK,
21 EXIT_REALLOC_ERROR, 23 EXIT_REALLOC_ERROR,
22 EXIT_ARGS_ERROR, 24 EXIT_ARGS_ERROR,
46 // returns regular expression argument 48 // returns regular expression argument
47 char *parseArgs(int argc, char **argv) 49 char *parseArgs(int argc, char **argv)
48 { 50 {
49 static struct option long_options[] = 51 static struct option long_options[] =
50 { 52 {
53 { "line", 0, 0, 'l' },
51 { 0, 0, 0, 0 }, 54 { 0, 0, 0, 0 },
52 }; 55 };
53 56
54 int c; 57 int c;
55 int l; 58 int l;
56 while ((c = getopt_long(argc, argv, "", long_options, &l)) >= 0) 59 while ((c = getopt_long(argc, argv, "l", long_options, &l)) >= 0)
57 { 60 {
61 if (c == 'l')
62 colorLine = 1;
58 } 63 }
59 64
60 if (optind >= argc) 65 if (optind >= argc)
61 exit(args_error()); 66 exit(args_error());
62 67
105 110
106 out = (char *)realloc(out, out_len*sizeof(char)); 111 out = (char *)realloc(out, out_len*sizeof(char));
107 if (!out) 112 if (!out)
108 exit(realloc_error()); 113 exit(realloc_error());
109 114
110 so = rem[0].rm_so; 115 if (colorLine)
111 eo = rem[0].rm_eo; 116 {
117 so = 0;
118 eo = out_len - 1;
119 }
120 else
121 {
122 so = rem[0].rm_so;
123 eo = rem[0].rm_eo;
124 }
125
112 int match_len = eo - so; 126 int match_len = eo - so;
113 127
114 //fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA 128 //fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA
115 129
116 if (re_err != REG_NOMATCH && so >= 0 && eo >= 0 && match_len > 0) 130 if (re_err != REG_NOMATCH && so >= 0 && eo >= 0 && match_len > 0)