Mercurial > hg > index.fcgi > rcg > rcg-1
changeset 22:b18d74b37e9e
add runtime bufsize parameter; fix compile-time warnings about discarding qualifiers
author | paulo |
---|---|
date | Wed, 30 Jul 2014 01:13:50 -0700 |
parents | ebfc52615768 |
children | ce077dec4459 |
files | rcg.c |
diffstat | 1 files changed, 33 insertions(+), 20 deletions(-) [+] |
line diff
1.1 --- a/rcg.c Tue Jul 29 23:42:57 2014 -0700 1.2 +++ b/rcg.c Wed Jul 30 01:13:50 2014 -0700 1.3 @@ -26,7 +26,9 @@ 1.4 #define _GNU_SOURCE 1.5 #include <getopt.h> 1.6 1.7 -const size_t BUFSIZE = 5000; 1.8 +size_t bufsize; 1.9 +const size_t BUFSIZE_MIN = 1024; 1.10 +const size_t ERRBUFSIZE = 512; 1.11 1.12 const char *_CLR_START = "\x1B["; 1.13 const char *_CLR_BOLD = "1;"; 1.14 @@ -56,7 +58,7 @@ 1.15 char *getFGColor(char *cname) 1.16 { 1.17 Colors *c; 1.18 - for (c = COLORS; c->name; c++) 1.19 + for (c = (Colors *)COLORS; c->name; c++) 1.20 { 1.21 if (strncmp(cname, c->name, strlen(c->name)) == 0) 1.22 return c->fg; 1.23 @@ -67,7 +69,7 @@ 1.24 char *getBGColor(char *cname) 1.25 { 1.26 Colors *c; 1.27 - for (c = COLORS; c->name; c++) 1.28 + for (c = (Colors *)COLORS; c->name; c++) 1.29 { 1.30 if (strncmp(cname, c->name, strlen(c->name)) == 0) 1.31 return c->bg; 1.32 @@ -88,8 +90,8 @@ 1.33 1.34 int re_error(int errcode, const regex_t *re) 1.35 { 1.36 - char *err_string = calloc(BUFSIZE, sizeof(char)); 1.37 - regerror(errcode, re, err_string, BUFSIZE*sizeof(char)); 1.38 + char *err_string = calloc(ERRBUFSIZE, sizeof(char)); 1.39 + regerror(errcode, re, err_string, ERRBUFSIZE*sizeof(char)); 1.40 fprintf(stderr, "%s \n", err_string); 1.41 free(err_string); 1.42 return errcode; 1.43 @@ -115,10 +117,11 @@ 1.44 Usage: rcg [options] <PATTERN> \n\ 1.45 \n\ 1.46 Options: \n\ 1.47 - -l, --line Highlight whole line \n\ 1.48 - -B, --bold Bold \n\ 1.49 - -b <color> Background color \n\ 1.50 - -f <color> Foreground color \n\ 1.51 + -z, --bufsize <size> Buffer size \n\ 1.52 + -l, --line Highlight whole line \n\ 1.53 + -B, --bold Bold \n\ 1.54 + -b <color> Background color \n\ 1.55 + -f <color> Foreground color \n\ 1.56 \n\ 1.57 <color> can be one of the following: \n\ 1.58 "; 1.59 @@ -126,30 +129,38 @@ 1.60 fprintf(stdout, usage); 1.61 1.62 Colors *c; 1.63 - for (c = COLORS; c->name; c++) 1.64 + for (c = (Colors *)COLORS; c->name; c++) 1.65 fprintf(stdout, " %s \n", c->name); 1.66 1.67 return EXIT_ARGS_ERROR; 1.68 } 1.69 1.70 +Exit_code bufsize_args_error() 1.71 +{ 1.72 + fprintf(stdout, "bufsize has to be a minimum of %d \n", (int)BUFSIZE_MIN); 1.73 + return EXIT_ARGS_ERROR; 1.74 +} 1.75 + 1.76 // returns regular expression argument 1.77 char *parseArgs(int argc, char **argv) 1.78 { 1.79 static struct option long_options[] = 1.80 { 1.81 - { "line", 0, 0, 'l' }, 1.82 - { "bold", 0, 0, 'B' }, 1.83 - { 0, 0, 0, 0 }, 1.84 + { "line", 0, NULL, 'l' }, 1.85 + { "bold", 0, NULL, 'B' }, 1.86 + { "bufsize", required_argument, NULL, 'z' }, 1.87 + { NULL, 0, NULL, 0 }, 1.88 }; 1.89 1.90 int c; 1.91 - int l; 1.92 - while ((c = getopt_long(argc, argv, "lBf:b:", long_options, &l)) >= 0) 1.93 + while ((c = getopt_long(argc, argv, "lBz:f:b:", long_options, NULL)) >= 0) 1.94 { 1.95 if (c == 'l') 1.96 colorLine = 1; 1.97 else if (c == 'B') 1.98 embolden = 1; 1.99 + else if (c == 'z') 1.100 + bufsize = (size_t)atoi(optarg); 1.101 else if (c == 'f') 1.102 g_fg = strdup(optarg); 1.103 else if (c == 'b') 1.104 @@ -159,16 +170,18 @@ 1.105 if (optind >= argc) 1.106 exit(args_error()); 1.107 1.108 + if (bufsize < BUFSIZE_MIN) 1.109 + exit(bufsize_args_error()); 1.110 + 1.111 return argv[optind]; 1.112 } 1.113 1.114 int main(int argc, char **argv) 1.115 { 1.116 + bufsize = BUFSIZE_MIN; 1.117 + 1.118 char *re_expression = parseArgs(argc, argv); 1.119 - 1.120 - //fprintf(stderr, "re_expression = %s \n", re_expression); //d/ 20100405 PBA 1.121 - 1.122 - char *buf = calloc(BUFSIZE, sizeof(char)); 1.123 + char *buf = calloc(bufsize, sizeof(char)); 1.124 1.125 int re_err; 1.126 regex_t *re = calloc(1, sizeof(regex_t)); 1.127 @@ -213,7 +226,7 @@ 1.128 1.129 char *out = NULL; 1.130 1.131 - while (fgets(buf, BUFSIZE, stdin)) 1.132 + while (fgets(buf, bufsize, stdin)) 1.133 { 1.134 int so = -1; 1.135 int eo = -1;