Mercurial > hg > index.fcgi > rcg > rcg-1
comparison rcg.c @ 5:26e28ec5fe87
rcg.c: add color string; has bug with realloc()
author | paulo@twcdns.fastsearch.net |
---|---|
date | Tue, 06 Apr 2010 01:25:30 -0700 |
parents | 99770a6b967d |
children | 910e059abd6d |
comparison
equal
deleted
inserted
replaced
3:226c3c00d52e | 4:d486aba4efcf |
---|---|
10 #define _GNU_SOURCE | 10 #define _GNU_SOURCE |
11 #include <getopt.h> | 11 #include <getopt.h> |
12 | 12 |
13 const size_t BUFSIZE = 5000; | 13 const size_t BUFSIZE = 5000; |
14 | 14 |
15 const char *CLR_START = "-CLR_START-"; | 15 const char *CLR_START = "\x1B[31"; |
16 const char *CLR_END = "-CLR_END-"; | 16 const char *CLR_END = "m"; |
17 const char *CLR_CLEAR = "\x1B[0m"; | |
17 | 18 |
18 typedef enum _exit_code { | 19 typedef enum _exit_code { |
19 EXIT_OK, | 20 EXIT_OK, |
20 EXIT_REALLOC_ERROR, | 21 EXIT_REALLOC_ERROR, |
21 EXIT_ARGS_ERROR, | 22 EXIT_ARGS_ERROR, |
64 | 65 |
65 int main(int argc, char **argv) | 66 int main(int argc, char **argv) |
66 { | 67 { |
67 char *re_expression = parseArgs(argc, argv); | 68 char *re_expression = parseArgs(argc, argv); |
68 | 69 |
69 fprintf(stderr, "re_expression = %s \n", re_expression); //d/ 20100405 PBA | 70 //fprintf(stderr, "re_expression = %s \n", re_expression); //d/ 20100405 PBA |
70 | 71 |
71 char *buf = calloc(BUFSIZE, sizeof(char)); | 72 char *buf = calloc(BUFSIZE, sizeof(char)); |
72 | 73 |
73 int re_err; | 74 int re_err; |
74 regex_t *re = calloc(1, sizeof(regex_t)); | 75 regex_t *re = calloc(1, sizeof(regex_t)); |
78 | 79 |
79 regmatch_t *rem = calloc(1, sizeof(regmatch_t)); | 80 regmatch_t *rem = calloc(1, sizeof(regmatch_t)); |
80 | 81 |
81 size_t CLR_START_len = strlen(CLR_START); | 82 size_t CLR_START_len = strlen(CLR_START); |
82 size_t CLR_END_len = strlen(CLR_END); | 83 size_t CLR_END_len = strlen(CLR_END); |
84 size_t CLR_CLEAR_len = strlen(CLR_CLEAR); | |
83 | 85 |
84 while (fgets(buf, BUFSIZE, stdin)) | 86 while (fgets(buf, BUFSIZE, stdin)) |
85 { | 87 { |
86 int so = -1; | 88 int so = -1; |
87 int eo = -1; | 89 int eo = -1; |
106 //fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA | 108 //fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA |
107 | 109 |
108 if (re_err != REG_NOMATCH && so >= 0 && eo >= 0) | 110 if (re_err != REG_NOMATCH && so >= 0 && eo >= 0) |
109 { | 111 { |
110 int match_len = eo - so; | 112 int match_len = eo - so; |
111 out_len += CLR_START_len + CLR_END_len; | 113 out_len += CLR_START_len + CLR_END_len + CLR_CLEAR_len; |
112 out = (char *)realloc(out, out_len*sizeof(char)); | 114 out = (char *)realloc(out, out_len*sizeof(char)); |
113 if (!out) | 115 if (!out) |
114 exit(realloc_error()); | 116 exit(realloc_error()); |
115 strncpy(&out[out_pos], &buf[buf_pos], so); | 117 strncpy(&out[out_pos], &buf[buf_pos], so); |
116 out_pos += so; | 118 out_pos += so; |
117 buf_pos += so; | 119 buf_pos += so; |
118 strncpy(&out[out_pos], CLR_START, CLR_START_len); | 120 strncpy(&out[out_pos], CLR_START, CLR_START_len); |
119 out_pos += 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; | |
120 strncpy(&out[out_pos], &buf[buf_pos], match_len); | 124 strncpy(&out[out_pos], &buf[buf_pos], match_len); |
121 out_pos += match_len; | 125 out_pos += match_len; |
122 strncpy(&out[out_pos], CLR_END, CLR_END_len); | 126 strncpy(&out[out_pos], CLR_CLEAR, CLR_CLEAR_len); |
123 out_pos += CLR_END_len; | 127 out_pos += CLR_CLEAR_len; |
124 | 128 |
125 buf_pos += match_len; | 129 buf_pos += match_len; |
126 } | 130 } |
127 else | 131 else |
132 { | |
128 strncpy(&out[out_pos], &buf[buf_pos], out_len - out_pos); | 133 strncpy(&out[out_pos], &buf[buf_pos], out_len - out_pos); |
134 out[out_len] = '\0'; | |
135 } | |
129 } | 136 } |
130 | 137 |
131 fputs(out, stdout); | 138 fputs(out, stdout); |
132 free(out); | 139 free(out); |
133 } | 140 } |