annotate 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
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
paulo@4 8 #include <unistd.h>
paulo@4 9
paulo@4 10 #define _GNU_SOURCE
paulo@4 11 #include <getopt.h>
paulo@4 12
pang@1 13 const size_t BUFSIZE = 5000;
pang@1 14
pang@1 15 const char *CLR_START = "-CLR_START-";
pang@1 16 const char *CLR_END = "-CLR_END-";
pang@1 17
paulo@4 18 typedef enum _exit_code {
paulo@4 19 EXIT_OK,
paulo@4 20 EXIT_REALLOC_ERROR,
paulo@4 21 EXIT_ARGS_ERROR,
paulo@4 22 } Exit_code;
paulo@4 23
paulo@3 24 int re_error(int errcode, const regex_t *re)
paulo@3 25 {
paulo@3 26 char *err_string = calloc(BUFSIZE, sizeof(char));
paulo@3 27 regerror(errcode, re, err_string, BUFSIZE*sizeof(char));
paulo@3 28 fprintf(stderr, "%s \n", err_string);
paulo@3 29 free(err_string);
paulo@3 30 return errcode;
paulo@3 31 }
paulo@3 32
paulo@4 33 Exit_code realloc_error()
paulo@3 34 {
paulo@3 35 fprintf(stderr, "realloc() failure \n");
paulo@4 36 return EXIT_REALLOC_ERROR;
paulo@4 37 }
paulo@4 38
paulo@4 39 Exit_code args_error()
paulo@4 40 {
paulo@4 41 fprintf(stderr, "Invalid or nonexistent argument. \n");
paulo@4 42 return EXIT_ARGS_ERROR;
paulo@4 43 }
paulo@4 44
paulo@4 45 // returns regular expression argument
paulo@4 46 char *parseArgs(int argc, char **argv)
paulo@4 47 {
paulo@4 48 static struct option long_options[] =
paulo@4 49 {
paulo@4 50 { 0, 0, 0, 0 },
paulo@4 51 };
paulo@4 52
paulo@4 53 int c;
paulo@4 54 int l;
paulo@4 55 while ((c = getopt_long(argc, argv, "", long_options, &l)) >= 0)
paulo@4 56 {
paulo@4 57 }
paulo@4 58
paulo@4 59 if (optind >= argc)
paulo@4 60 exit(args_error());
paulo@4 61
paulo@4 62 return argv[optind];
paulo@3 63 }
paulo@3 64
pang@1 65 int main(int argc, char **argv)
pang@1 66 {
paulo@4 67 char *re_expression = parseArgs(argc, argv);
paulo@4 68
paulo@4 69 fprintf(stderr, "re_expression = %s \n", re_expression); //d/ 20100405 PBA
paulo@4 70
pang@1 71 char *buf = calloc(BUFSIZE, sizeof(char));
pang@1 72
paulo@3 73 int re_err;
pang@1 74 regex_t *re = calloc(1, sizeof(regex_t));
paulo@4 75 re_err = regcomp(re, re_expression, REG_EXTENDED);
paulo@3 76 if (re_err != 0)
paulo@3 77 exit(re_error(re_err, re));
pang@1 78
pang@2 79 regmatch_t *rem = calloc(1, sizeof(regmatch_t));
pang@1 80
pang@1 81 size_t CLR_START_len = strlen(CLR_START);
pang@1 82 size_t CLR_END_len = strlen(CLR_END);
pang@1 83
pang@1 84 while (fgets(buf, BUFSIZE, stdin))
pang@1 85 {
pang@2 86 int so = -1;
pang@2 87 int eo = -1;
pang@1 88
paulo@3 89 size_t out_len = strlen(buf);
paulo@3 90 char *out = calloc(out_len, sizeof(char));
paulo@3 91 unsigned int out_pos = 0;
paulo@3 92 unsigned int buf_pos = 0;
paulo@3 93
paulo@4 94 for (re_err = 0; re_err != REG_NOMATCH;)
pang@2 95 {
paulo@3 96 re_err = regexec(re, &buf[buf_pos], 1, rem, 0);
pang@1 97
paulo@3 98 //fprintf(stderr, "%s", &buf[buf_pos]); //d// 20100405 PBA
paulo@3 99
pang@2 100 if (out_len >= (BUFSIZE - buf_pos))
pang@2 101 out_len = (BUFSIZE - buf_pos) - 1;
pang@1 102
pang@2 103 so = rem[0].rm_so;
pang@2 104 eo = rem[0].rm_eo;
pang@1 105
paulo@3 106 //fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA
pang@1 107
paulo@3 108 if (re_err != REG_NOMATCH && so >= 0 && eo >= 0)
pang@1 109 {
pang@1 110 int match_len = eo - so;
pang@1 111 out_len += CLR_START_len + CLR_END_len;
pang@1 112 out = (char *)realloc(out, out_len*sizeof(char));
paulo@3 113 if (!out)
paulo@3 114 exit(realloc_error());
paulo@3 115 strncpy(&out[out_pos], &buf[buf_pos], so);
paulo@3 116 out_pos += so;
paulo@3 117 buf_pos += so;
pang@1 118 strncpy(&out[out_pos], CLR_START, CLR_START_len);
pang@1 119 out_pos += CLR_START_len;
pang@2 120 strncpy(&out[out_pos], &buf[buf_pos], match_len);
pang@1 121 out_pos += match_len;
pang@1 122 strncpy(&out[out_pos], CLR_END, CLR_END_len);
pang@1 123 out_pos += CLR_END_len;
pang@2 124
pang@2 125 buf_pos += match_len;
pang@1 126 }
pang@2 127 else
paulo@3 128 strncpy(&out[out_pos], &buf[buf_pos], out_len - out_pos);
pang@1 129 }
pang@1 130
pang@1 131 fputs(out, stdout);
paulo@3 132 free(out);
pang@1 133 }
pang@1 134
paulo@4 135 return EXIT_OK;
pang@1 136 }