Mercurial > hg > index.fcgi > rcg > rcg-1
view rcg.c @ 4:99770a6b967d
rcg.c: use getopt.h and read in regex from arguments; use enum for exit codes
author | paulo@twcdns.fastsearch.net |
---|---|
date | Mon, 05 Apr 2010 23:45:38 -0700 |
parents | fe4b316b4386 |
children | 26e28ec5fe87 |
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 = "-CLR_START-";
16 const char *CLR_END = "-CLR_END-";
18 typedef enum _exit_code {
19 EXIT_OK,
20 EXIT_REALLOC_ERROR,
21 EXIT_ARGS_ERROR,
22 } Exit_code;
24 int re_error(int errcode, const regex_t *re)
25 {
26 char *err_string = calloc(BUFSIZE, sizeof(char));
27 regerror(errcode, re, err_string, BUFSIZE*sizeof(char));
28 fprintf(stderr, "%s \n", err_string);
29 free(err_string);
30 return errcode;
31 }
33 Exit_code realloc_error()
34 {
35 fprintf(stderr, "realloc() failure \n");
36 return EXIT_REALLOC_ERROR;
37 }
39 Exit_code args_error()
40 {
41 fprintf(stderr, "Invalid or nonexistent argument. \n");
42 return EXIT_ARGS_ERROR;
43 }
45 // returns regular expression argument
46 char *parseArgs(int argc, char **argv)
47 {
48 static struct option long_options[] =
49 {
50 { 0, 0, 0, 0 },
51 };
53 int c;
54 int l;
55 while ((c = getopt_long(argc, argv, "", long_options, &l)) >= 0)
56 {
57 }
59 if (optind >= argc)
60 exit(args_error());
62 return argv[optind];
63 }
65 int main(int argc, char **argv)
66 {
67 char *re_expression = parseArgs(argc, argv);
69 fprintf(stderr, "re_expression = %s \n", re_expression); //d/ 20100405 PBA
71 char *buf = calloc(BUFSIZE, sizeof(char));
73 int re_err;
74 regex_t *re = calloc(1, sizeof(regex_t));
75 re_err = regcomp(re, re_expression, REG_EXTENDED);
76 if (re_err != 0)
77 exit(re_error(re_err, re));
79 regmatch_t *rem = calloc(1, sizeof(regmatch_t));
81 size_t CLR_START_len = strlen(CLR_START);
82 size_t CLR_END_len = strlen(CLR_END);
84 while (fgets(buf, BUFSIZE, stdin))
85 {
86 int so = -1;
87 int eo = -1;
89 size_t out_len = strlen(buf);
90 char *out = calloc(out_len, sizeof(char));
91 unsigned int out_pos = 0;
92 unsigned int buf_pos = 0;
94 for (re_err = 0; re_err != REG_NOMATCH;)
95 {
96 re_err = regexec(re, &buf[buf_pos], 1, rem, 0);
98 //fprintf(stderr, "%s", &buf[buf_pos]); //d// 20100405 PBA
100 if (out_len >= (BUFSIZE - buf_pos))
101 out_len = (BUFSIZE - buf_pos) - 1;
103 so = rem[0].rm_so;
104 eo = rem[0].rm_eo;
106 //fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA
108 if (re_err != REG_NOMATCH && so >= 0 && eo >= 0)
109 {
110 int match_len = eo - so;
111 out_len += CLR_START_len + CLR_END_len;
112 out = (char *)realloc(out, out_len*sizeof(char));
113 if (!out)
114 exit(realloc_error());
115 strncpy(&out[out_pos], &buf[buf_pos], so);
116 out_pos += so;
117 buf_pos += so;
118 strncpy(&out[out_pos], CLR_START, CLR_START_len);
119 out_pos += CLR_START_len;
120 strncpy(&out[out_pos], &buf[buf_pos], match_len);
121 out_pos += match_len;
122 strncpy(&out[out_pos], CLR_END, CLR_END_len);
123 out_pos += CLR_END_len;
125 buf_pos += match_len;
126 }
127 else
128 strncpy(&out[out_pos], &buf[buf_pos], out_len - out_pos);
129 }
131 fputs(out, stdout);
132 free(out);
133 }
135 return EXIT_OK;
136 }