# HG changeset patch # User paulo # Date 1406708030 25200 # Node ID b18d74b37e9e53c52f1d82facfcd68b533821d95 # Parent ebfc5261576813183d10b79476cd4edbcef0cb19 add runtime bufsize parameter; fix compile-time warnings about discarding qualifiers diff -r ebfc52615768 -r b18d74b37e9e rcg.c --- a/rcg.c Tue Jul 29 23:42:57 2014 -0700 +++ b/rcg.c Wed Jul 30 01:13:50 2014 -0700 @@ -26,7 +26,9 @@ #define _GNU_SOURCE #include -const size_t BUFSIZE = 5000; +size_t bufsize; +const size_t BUFSIZE_MIN = 1024; +const size_t ERRBUFSIZE = 512; const char *_CLR_START = "\x1B["; const char *_CLR_BOLD = "1;"; @@ -56,7 +58,7 @@ char *getFGColor(char *cname) { Colors *c; - for (c = COLORS; c->name; c++) + for (c = (Colors *)COLORS; c->name; c++) { if (strncmp(cname, c->name, strlen(c->name)) == 0) return c->fg; @@ -67,7 +69,7 @@ char *getBGColor(char *cname) { Colors *c; - for (c = COLORS; c->name; c++) + for (c = (Colors *)COLORS; c->name; c++) { if (strncmp(cname, c->name, strlen(c->name)) == 0) return c->bg; @@ -88,8 +90,8 @@ int re_error(int errcode, const regex_t *re) { - char *err_string = calloc(BUFSIZE, sizeof(char)); - regerror(errcode, re, err_string, BUFSIZE*sizeof(char)); + char *err_string = calloc(ERRBUFSIZE, sizeof(char)); + regerror(errcode, re, err_string, ERRBUFSIZE*sizeof(char)); fprintf(stderr, "%s \n", err_string); free(err_string); return errcode; @@ -115,10 +117,11 @@ Usage: rcg [options] \n\ \n\ Options: \n\ - -l, --line Highlight whole line \n\ - -B, --bold Bold \n\ - -b Background color \n\ - -f Foreground color \n\ + -z, --bufsize Buffer size \n\ + -l, --line Highlight whole line \n\ + -B, --bold Bold \n\ + -b Background color \n\ + -f Foreground color \n\ \n\ can be one of the following: \n\ "; @@ -126,30 +129,38 @@ fprintf(stdout, usage); Colors *c; - for (c = COLORS; c->name; c++) + for (c = (Colors *)COLORS; c->name; c++) fprintf(stdout, " %s \n", c->name); return EXIT_ARGS_ERROR; } +Exit_code bufsize_args_error() +{ + fprintf(stdout, "bufsize has to be a minimum of %d \n", (int)BUFSIZE_MIN); + return EXIT_ARGS_ERROR; +} + // returns regular expression argument char *parseArgs(int argc, char **argv) { static struct option long_options[] = { - { "line", 0, 0, 'l' }, - { "bold", 0, 0, 'B' }, - { 0, 0, 0, 0 }, + { "line", 0, NULL, 'l' }, + { "bold", 0, NULL, 'B' }, + { "bufsize", required_argument, NULL, 'z' }, + { NULL, 0, NULL, 0 }, }; int c; - int l; - while ((c = getopt_long(argc, argv, "lBf:b:", long_options, &l)) >= 0) + while ((c = getopt_long(argc, argv, "lBz:f:b:", long_options, NULL)) >= 0) { if (c == 'l') colorLine = 1; else if (c == 'B') embolden = 1; + else if (c == 'z') + bufsize = (size_t)atoi(optarg); else if (c == 'f') g_fg = strdup(optarg); else if (c == 'b') @@ -159,16 +170,18 @@ if (optind >= argc) exit(args_error()); + if (bufsize < BUFSIZE_MIN) + exit(bufsize_args_error()); + return argv[optind]; } int main(int argc, char **argv) { + bufsize = BUFSIZE_MIN; + char *re_expression = parseArgs(argc, argv); - - //fprintf(stderr, "re_expression = %s \n", re_expression); //d/ 20100405 PBA - - char *buf = calloc(BUFSIZE, sizeof(char)); + char *buf = calloc(bufsize, sizeof(char)); int re_err; regex_t *re = calloc(1, sizeof(regex_t)); @@ -213,7 +226,7 @@ char *out = NULL; - while (fgets(buf, BUFSIZE, stdin)) + while (fgets(buf, bufsize, stdin)) { int so = -1; int eo = -1;