Mercurial > hg > index.fcgi > rcg > rcg-1
view rcg.c @ 7:0b9cf47655fe
bugfixes
author | paulo@hit-nxdomain.opendns.com |
---|---|
date | Thu, 06 May 2010 23:15:40 -0700 |
parents | 910e059abd6d |
children | 5ab8d6c1a37c |
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 typedef enum _exit_code {
20 EXIT_OK,
21 EXIT_REALLOC_ERROR,
22 EXIT_ARGS_ERROR,
23 } Exit_code;
25 int re_error(int errcode, const regex_t *re)
26 {
27 char *err_string = calloc(BUFSIZE, sizeof(char));
28 regerror(errcode, re, err_string, BUFSIZE*sizeof(char));
29 fprintf(stderr, "%s \n", err_string);
30 free(err_string);
31 return errcode;
32 }
34 Exit_code realloc_error()
35 {
36 fprintf(stderr, "realloc() failure \n");
37 return EXIT_REALLOC_ERROR;
38 }
40 Exit_code args_error()
41 {
42 fprintf(stderr, "Invalid or nonexistent argument. \n");
43 return EXIT_ARGS_ERROR;
44 }
46 // returns regular expression argument
47 char *parseArgs(int argc, char **argv)
48 {
49 static struct option long_options[] =
50 {
51 { 0, 0, 0, 0 },
52 };
54 int c;
55 int l;
56 while ((c = getopt_long(argc, argv, "", long_options, &l)) >= 0)
57 {
58 }
60 if (optind >= argc)
61 exit(args_error());
63 return argv[optind];
64 }
66 int main(int argc, char **argv)
67 {
68 char *re_expression = parseArgs(argc, argv);
70 //fprintf(stderr, "re_expression = %s \n", re_expression); //d/ 20100405 PBA
72 char *buf = calloc(BUFSIZE, sizeof(char));
74 int re_err;
75 regex_t *re = calloc(1, sizeof(regex_t));
76 re_err = regcomp(re, re_expression, REG_EXTENDED);
77 if (re_err != 0)
78 exit(re_error(re_err, re));
80 regmatch_t *rem = calloc(1, sizeof(regmatch_t));
82 size_t CLR_START_len = strlen(CLR_START);
83 size_t CLR_END_len = strlen(CLR_END);
84 size_t CLR_CLEAR_len = strlen(CLR_CLEAR);
86 char *out = NULL;
88 while (fgets(buf, BUFSIZE, stdin))
89 {
90 int so = -1;
91 int eo = -1;
93 size_t out_len = strlen(buf) + 1;
94 unsigned int out_pos = 0;
95 unsigned int buf_pos = 0;
97 for (re_err = 0; re_err != REG_NOMATCH;)
98 {
99 re_err = regexec(re, &buf[buf_pos], 1, rem, 0);
101 //fprintf(stderr, "%s", &buf[buf_pos]); //d// 20100405 PBA
103 if (out_len >= (BUFSIZE - buf_pos))
104 out_len = (BUFSIZE - buf_pos) - 1;
106 out = (char *)realloc(out, out_len*sizeof(char));
107 if (!out)
108 exit(realloc_error());
110 so = rem[0].rm_so;
111 eo = rem[0].rm_eo;
112 int match_len = eo - so;
114 //fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA
116 if (re_err != REG_NOMATCH && so >= 0 && eo >= 0 && match_len > 0)
117 {
118 out_len += CLR_START_len + CLR_END_len + CLR_CLEAR_len;
119 out = (char *)realloc(out, out_len*sizeof(char));
120 if (!out)
121 exit(realloc_error());
122 strncpy(&out[out_pos], &buf[buf_pos], so);
123 out_pos += so;
124 buf_pos += so;
125 strncpy(&out[out_pos], CLR_START, CLR_START_len);
126 out_pos += CLR_START_len;
127 strncpy(&out[out_pos], CLR_END, CLR_END_len);
128 out_pos += CLR_END_len;
129 strncpy(&out[out_pos], &buf[buf_pos], match_len);
130 out_pos += match_len;
131 strncpy(&out[out_pos], CLR_CLEAR, CLR_CLEAR_len);
132 out_pos += CLR_CLEAR_len;
134 buf_pos += match_len;
135 }
136 else
137 {
138 strncpy(&out[out_pos], &buf[buf_pos], out_len - out_pos);
139 out[out_len - 1] = '\0';
140 }
141 }
143 fputs(out, stdout);
144 }
146 return EXIT_OK;
147 }