rev |
line source |
pang@1
|
1 #include <stdio.h>
|
pang@1
|
2 #include <stdlib.h>
|
pang@1
|
3 #include <regex.h>
|
pang@1
|
4
|
pang@1
|
5 #define __USE_GNU
|
pang@1
|
6 #include <string.h>
|
pang@1
|
7
|
pang@1
|
8 const size_t BUFSIZE = 5000;
|
pang@1
|
9
|
pang@1
|
10 const char *CLR_START = "-CLR_START-";
|
pang@1
|
11 const char *CLR_END = "-CLR_END-";
|
pang@1
|
12
|
paulo@3
|
13 int re_error(int errcode, const regex_t *re)
|
paulo@3
|
14 {
|
paulo@3
|
15 char *err_string = calloc(BUFSIZE, sizeof(char));
|
paulo@3
|
16 regerror(errcode, re, err_string, BUFSIZE*sizeof(char));
|
paulo@3
|
17 fprintf(stderr, "%s \n", err_string);
|
paulo@3
|
18 free(err_string);
|
paulo@3
|
19 return errcode;
|
paulo@3
|
20 }
|
paulo@3
|
21
|
paulo@3
|
22 int realloc_error()
|
paulo@3
|
23 {
|
paulo@3
|
24 fprintf(stderr, "realloc() failure \n");
|
paulo@3
|
25 return 1;
|
paulo@3
|
26 }
|
paulo@3
|
27
|
pang@1
|
28 int main(int argc, char **argv)
|
pang@1
|
29 {
|
pang@1
|
30 char *buf = calloc(BUFSIZE, sizeof(char));
|
pang@1
|
31
|
paulo@3
|
32 int re_err;
|
pang@1
|
33 regex_t *re = calloc(1, sizeof(regex_t));
|
paulo@3
|
34 re_err = regcomp(re, "Hello", REG_EXTENDED);
|
paulo@3
|
35 if (re_err != 0)
|
paulo@3
|
36 exit(re_error(re_err, re));
|
pang@1
|
37
|
pang@2
|
38 regmatch_t *rem = calloc(1, sizeof(regmatch_t));
|
pang@1
|
39
|
pang@1
|
40 size_t CLR_START_len = strlen(CLR_START);
|
pang@1
|
41 size_t CLR_END_len = strlen(CLR_END);
|
pang@1
|
42
|
pang@1
|
43
|
pang@1
|
44 while (fgets(buf, BUFSIZE, stdin))
|
pang@1
|
45 {
|
pang@2
|
46 int so = -1;
|
pang@2
|
47 int eo = -1;
|
pang@1
|
48
|
paulo@3
|
49 size_t out_len = strlen(buf);
|
paulo@3
|
50 char *out = calloc(out_len, sizeof(char));
|
paulo@3
|
51 unsigned int out_pos = 0;
|
paulo@3
|
52 unsigned int buf_pos = 0;
|
paulo@3
|
53
|
paulo@3
|
54 while (re_err != REG_NOMATCH)
|
pang@2
|
55 {
|
paulo@3
|
56 re_err = regexec(re, &buf[buf_pos], 1, rem, 0);
|
pang@1
|
57
|
paulo@3
|
58 //fprintf(stderr, "%s", &buf[buf_pos]); //d// 20100405 PBA
|
paulo@3
|
59
|
pang@2
|
60 if (out_len >= (BUFSIZE - buf_pos))
|
pang@2
|
61 out_len = (BUFSIZE - buf_pos) - 1;
|
pang@1
|
62
|
pang@2
|
63 so = rem[0].rm_so;
|
pang@2
|
64 eo = rem[0].rm_eo;
|
pang@1
|
65
|
paulo@3
|
66 //fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA
|
pang@1
|
67
|
paulo@3
|
68 if (re_err != REG_NOMATCH && so >= 0 && eo >= 0)
|
pang@1
|
69 {
|
pang@1
|
70 int match_len = eo - so;
|
pang@1
|
71 out_len += CLR_START_len + CLR_END_len;
|
pang@1
|
72 out = (char *)realloc(out, out_len*sizeof(char));
|
paulo@3
|
73 if (!out)
|
paulo@3
|
74 exit(realloc_error());
|
paulo@3
|
75 strncpy(&out[out_pos], &buf[buf_pos], so);
|
paulo@3
|
76 out_pos += so;
|
paulo@3
|
77 buf_pos += so;
|
pang@1
|
78 strncpy(&out[out_pos], CLR_START, CLR_START_len);
|
pang@1
|
79 out_pos += CLR_START_len;
|
pang@2
|
80 strncpy(&out[out_pos], &buf[buf_pos], match_len);
|
pang@1
|
81 out_pos += match_len;
|
pang@1
|
82 strncpy(&out[out_pos], CLR_END, CLR_END_len);
|
pang@1
|
83 out_pos += CLR_END_len;
|
pang@2
|
84
|
pang@2
|
85 buf_pos += match_len;
|
pang@1
|
86 }
|
pang@2
|
87 else
|
paulo@3
|
88 strncpy(&out[out_pos], &buf[buf_pos], out_len - out_pos);
|
pang@1
|
89 }
|
pang@1
|
90
|
pang@1
|
91 fputs(out, stdout);
|
paulo@3
|
92 free(out);
|
pang@1
|
93 }
|
pang@1
|
94
|
pang@1
|
95 return 0;
|
pang@1
|
96 }
|