comparison 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
comparison
equal deleted inserted replaced
2:a8e756ccd89d 3:226c3c00d52e
3 #include <regex.h> 3 #include <regex.h>
4 4
5 #define __USE_GNU 5 #define __USE_GNU
6 #include <string.h> 6 #include <string.h>
7 7
8 #include <unistd.h>
9
10 #define _GNU_SOURCE
11 #include <getopt.h>
12
8 const size_t BUFSIZE = 5000; 13 const size_t BUFSIZE = 5000;
9 14
10 const char *CLR_START = "-CLR_START-"; 15 const char *CLR_START = "-CLR_START-";
11 const char *CLR_END = "-CLR_END-"; 16 const char *CLR_END = "-CLR_END-";
17
18 typedef enum _exit_code {
19 EXIT_OK,
20 EXIT_REALLOC_ERROR,
21 EXIT_ARGS_ERROR,
22 } Exit_code;
12 23
13 int re_error(int errcode, const regex_t *re) 24 int re_error(int errcode, const regex_t *re)
14 { 25 {
15 char *err_string = calloc(BUFSIZE, sizeof(char)); 26 char *err_string = calloc(BUFSIZE, sizeof(char));
16 regerror(errcode, re, err_string, BUFSIZE*sizeof(char)); 27 regerror(errcode, re, err_string, BUFSIZE*sizeof(char));
17 fprintf(stderr, "%s \n", err_string); 28 fprintf(stderr, "%s \n", err_string);
18 free(err_string); 29 free(err_string);
19 return errcode; 30 return errcode;
20 } 31 }
21 32
22 int realloc_error() 33 Exit_code realloc_error()
23 { 34 {
24 fprintf(stderr, "realloc() failure \n"); 35 fprintf(stderr, "realloc() failure \n");
25 return 1; 36 return EXIT_REALLOC_ERROR;
37 }
38
39 Exit_code args_error()
40 {
41 fprintf(stderr, "Invalid or nonexistent argument. \n");
42 return EXIT_ARGS_ERROR;
43 }
44
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 };
52
53 int c;
54 int l;
55 while ((c = getopt_long(argc, argv, "", long_options, &l)) >= 0)
56 {
57 }
58
59 if (optind >= argc)
60 exit(args_error());
61
62 return argv[optind];
26 } 63 }
27 64
28 int main(int argc, char **argv) 65 int main(int argc, char **argv)
29 { 66 {
67 char *re_expression = parseArgs(argc, argv);
68
69 fprintf(stderr, "re_expression = %s \n", re_expression); //d/ 20100405 PBA
70
30 char *buf = calloc(BUFSIZE, sizeof(char)); 71 char *buf = calloc(BUFSIZE, sizeof(char));
31 72
32 int re_err; 73 int re_err;
33 regex_t *re = calloc(1, sizeof(regex_t)); 74 regex_t *re = calloc(1, sizeof(regex_t));
34 re_err = regcomp(re, "Hello", REG_EXTENDED); 75 re_err = regcomp(re, re_expression, REG_EXTENDED);
35 if (re_err != 0) 76 if (re_err != 0)
36 exit(re_error(re_err, re)); 77 exit(re_error(re_err, re));
37 78
38 regmatch_t *rem = calloc(1, sizeof(regmatch_t)); 79 regmatch_t *rem = calloc(1, sizeof(regmatch_t));
39 80
40 size_t CLR_START_len = strlen(CLR_START); 81 size_t CLR_START_len = strlen(CLR_START);
41 size_t CLR_END_len = strlen(CLR_END); 82 size_t CLR_END_len = strlen(CLR_END);
42
43 83
44 while (fgets(buf, BUFSIZE, stdin)) 84 while (fgets(buf, BUFSIZE, stdin))
45 { 85 {
46 int so = -1; 86 int so = -1;
47 int eo = -1; 87 int eo = -1;
49 size_t out_len = strlen(buf); 89 size_t out_len = strlen(buf);
50 char *out = calloc(out_len, sizeof(char)); 90 char *out = calloc(out_len, sizeof(char));
51 unsigned int out_pos = 0; 91 unsigned int out_pos = 0;
52 unsigned int buf_pos = 0; 92 unsigned int buf_pos = 0;
53 93
54 while (re_err != REG_NOMATCH) 94 for (re_err = 0; re_err != REG_NOMATCH;)
55 { 95 {
56 re_err = regexec(re, &buf[buf_pos], 1, rem, 0); 96 re_err = regexec(re, &buf[buf_pos], 1, rem, 0);
57 97
58 //fprintf(stderr, "%s", &buf[buf_pos]); //d// 20100405 PBA 98 //fprintf(stderr, "%s", &buf[buf_pos]); //d// 20100405 PBA
59 99
90 130
91 fputs(out, stdout); 131 fputs(out, stdout);
92 free(out); 132 free(out);
93 } 133 }
94 134
95 return 0; 135 return EXIT_OK;
96 } 136 }