view 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
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <regex.h>
5 #define __USE_GNU
6 #include <string.h>
8 #include <unistd.h>
10 #define _GNU_SOURCE
11 #include <getopt.h>
13 const size_t BUFSIZE = 5000;
15 const char *CLR_START = "\x1B[31";
16 const char *CLR_END = "m";
17 const char *CLR_CLEAR = "\x1B[0m";
19 int colorLine = 0;
21 typedef enum _exit_code {
22 EXIT_OK,
23 EXIT_REALLOC_ERROR,
24 EXIT_ARGS_ERROR,
25 } Exit_code;
27 int re_error(int errcode, const regex_t *re)
28 {
29 char *err_string = calloc(BUFSIZE, sizeof(char));
30 regerror(errcode, re, err_string, BUFSIZE*sizeof(char));
31 fprintf(stderr, "%s \n", err_string);
32 free(err_string);
33 return errcode;
34 }
36 Exit_code realloc_error()
37 {
38 fprintf(stderr, "realloc() failure \n");
39 return EXIT_REALLOC_ERROR;
40 }
42 Exit_code args_error()
43 {
44 fprintf(stderr, "Invalid or nonexistent argument. \n");
45 return EXIT_ARGS_ERROR;
46 }
48 // returns regular expression argument
49 char *parseArgs(int argc, char **argv)
50 {
51 static struct option long_options[] =
52 {
53 { "line", 0, 0, 'l' },
54 { 0, 0, 0, 0 },
55 };
57 int c;
58 int l;
59 while ((c = getopt_long(argc, argv, "l", long_options, &l)) >= 0)
60 {
61 if (c == 'l')
62 colorLine = 1;
63 }
65 if (optind >= argc)
66 exit(args_error());
68 return argv[optind];
69 }
71 int main(int argc, char **argv)
72 {
73 char *re_expression = parseArgs(argc, argv);
75 //fprintf(stderr, "re_expression = %s \n", re_expression); //d/ 20100405 PBA
77 char *buf = calloc(BUFSIZE, sizeof(char));
79 int re_err;
80 regex_t *re = calloc(1, sizeof(regex_t));
81 re_err = regcomp(re, re_expression, REG_EXTENDED);
82 if (re_err != 0)
83 exit(re_error(re_err, re));
85 regmatch_t *rem = calloc(1, sizeof(regmatch_t));
87 size_t CLR_START_len = strlen(CLR_START);
88 size_t CLR_END_len = strlen(CLR_END);
89 size_t CLR_CLEAR_len = strlen(CLR_CLEAR);
91 char *out = NULL;
93 while (fgets(buf, BUFSIZE, stdin))
94 {
95 int so = -1;
96 int eo = -1;
98 size_t out_len = strlen(buf) + 1;
99 unsigned int out_pos = 0;
100 unsigned int buf_pos = 0;
102 for (re_err = 0; re_err != REG_NOMATCH;)
103 {
104 re_err = regexec(re, &buf[buf_pos], 1, rem, 0);
106 //fprintf(stderr, "%s", &buf[buf_pos]); //d// 20100405 PBA
108 if (out_len >= (BUFSIZE - buf_pos))
109 out_len = (BUFSIZE - buf_pos) - 1;
111 out = (char *)realloc(out, out_len*sizeof(char));
112 if (!out)
113 exit(realloc_error());
115 if (colorLine)
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 }
126 int match_len = eo - so;
128 //fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA
130 if (re_err != REG_NOMATCH && so >= 0 && eo >= 0 && match_len > 0)
131 {
132 out_len += CLR_START_len + CLR_END_len + CLR_CLEAR_len;
133 out = (char *)realloc(out, out_len*sizeof(char));
134 if (!out)
135 exit(realloc_error());
136 strncpy(&out[out_pos], &buf[buf_pos], so);
137 out_pos += so;
138 buf_pos += so;
139 strncpy(&out[out_pos], CLR_START, CLR_START_len);
140 out_pos += CLR_START_len;
141 strncpy(&out[out_pos], CLR_END, CLR_END_len);
142 out_pos += CLR_END_len;
143 strncpy(&out[out_pos], &buf[buf_pos], match_len);
144 out_pos += match_len;
145 strncpy(&out[out_pos], CLR_CLEAR, CLR_CLEAR_len);
146 out_pos += CLR_CLEAR_len;
148 buf_pos += match_len;
149 }
150 else
151 {
152 strncpy(&out[out_pos], &buf[buf_pos], out_len - out_pos);
153 out[out_len - 1] = '\0';
154 }
155 }
157 fputs(out, stdout);
158 }
160 return EXIT_OK;
161 }