view rcg.c @ 6:910e059abd6d

rcg.c: fix bug with realloc()
author paulo@twcdns.fastsearch.net
date Tue, 06 Apr 2010 22:51:22 -0700
parents 26e28ec5fe87
children 0b9cf47655fe
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 while (fgets(buf, BUFSIZE, stdin))
87 {
88 int so = -1;
89 int eo = -1;
91 size_t out_len = strlen(buf) + 1;
92 char *out = calloc(out_len, sizeof(char));
93 unsigned int out_pos = 0;
94 unsigned int buf_pos = 0;
96 for (re_err = 0; re_err != REG_NOMATCH;)
97 {
98 re_err = regexec(re, &buf[buf_pos], 1, rem, 0);
100 //fprintf(stderr, "%s", &buf[buf_pos]); //d// 20100405 PBA
102 if (out_len >= (BUFSIZE - buf_pos))
103 out_len = (BUFSIZE - buf_pos) - 1;
105 so = rem[0].rm_so;
106 eo = rem[0].rm_eo;
108 //fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA
110 if (re_err != REG_NOMATCH && so >= 0 && eo >= 0)
111 {
112 int match_len = eo - so;
113 out_len += CLR_START_len + CLR_END_len + CLR_CLEAR_len;
114 out = (char *)realloc(out, out_len*sizeof(char));
115 if (!out)
116 exit(realloc_error());
117 strncpy(&out[out_pos], &buf[buf_pos], so);
118 out_pos += so;
119 buf_pos += so;
120 strncpy(&out[out_pos], CLR_START, CLR_START_len);
121 out_pos += CLR_START_len;
122 strncpy(&out[out_pos], CLR_END, CLR_END_len);
123 out_pos += CLR_END_len;
124 strncpy(&out[out_pos], &buf[buf_pos], match_len);
125 out_pos += match_len;
126 strncpy(&out[out_pos], CLR_CLEAR, CLR_CLEAR_len);
127 out_pos += CLR_CLEAR_len;
129 buf_pos += match_len;
130 }
131 else
132 {
133 strncpy(&out[out_pos], &buf[buf_pos], out_len - out_pos);
134 out[out_len] = '\0';
135 }
136 }
138 fputs(out, stdout);
139 free(out);
140 }
142 return EXIT_OK;
143 }