Mercurial > hg > index.fcgi > rcg > rcg-1
comparison rcg.c @ 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 |
comparison
equal
deleted
inserted
replaced
16:e8e64275d424 | 17:f74c645e65a1 |
---|---|
24 #include <unistd.h> | 24 #include <unistd.h> |
25 | 25 |
26 #define _GNU_SOURCE | 26 #define _GNU_SOURCE |
27 #include <getopt.h> | 27 #include <getopt.h> |
28 | 28 |
29 const size_t BUFSIZE = 5000; | 29 size_t bufsize; |
30 const size_t BUFSIZE_MIN = 1024; | |
31 const size_t ERRBUFSIZE = 512; | |
30 | 32 |
31 const char *_CLR_START = "\x1B["; | 33 const char *_CLR_START = "\x1B["; |
32 const char *_CLR_BOLD = "1;"; | 34 const char *_CLR_BOLD = "1;"; |
33 char *CLR_START; | 35 char *CLR_START; |
34 const char *CLR_END = "m"; | 36 const char *CLR_END = "m"; |
54 }; | 56 }; |
55 | 57 |
56 char *getFGColor(char *cname) | 58 char *getFGColor(char *cname) |
57 { | 59 { |
58 Colors *c; | 60 Colors *c; |
59 for (c = COLORS; c->name; c++) | 61 for (c = (Colors *)COLORS; c->name; c++) |
60 { | 62 { |
61 if (strncmp(cname, c->name, strlen(c->name)) == 0) | 63 if (strncmp(cname, c->name, strlen(c->name)) == 0) |
62 return c->fg; | 64 return c->fg; |
63 } | 65 } |
64 return NULL; | 66 return NULL; |
65 } | 67 } |
66 | 68 |
67 char *getBGColor(char *cname) | 69 char *getBGColor(char *cname) |
68 { | 70 { |
69 Colors *c; | 71 Colors *c; |
70 for (c = COLORS; c->name; c++) | 72 for (c = (Colors *)COLORS; c->name; c++) |
71 { | 73 { |
72 if (strncmp(cname, c->name, strlen(c->name)) == 0) | 74 if (strncmp(cname, c->name, strlen(c->name)) == 0) |
73 return c->bg; | 75 return c->bg; |
74 } | 76 } |
75 return NULL; | 77 return NULL; |
86 EXIT_ARGS_ERROR, | 88 EXIT_ARGS_ERROR, |
87 } Exit_code; | 89 } Exit_code; |
88 | 90 |
89 int re_error(int errcode, const regex_t *re) | 91 int re_error(int errcode, const regex_t *re) |
90 { | 92 { |
91 char *err_string = calloc(BUFSIZE, sizeof(char)); | 93 char *err_string = calloc(ERRBUFSIZE, sizeof(char)); |
92 regerror(errcode, re, err_string, BUFSIZE*sizeof(char)); | 94 regerror(errcode, re, err_string, ERRBUFSIZE*sizeof(char)); |
93 fprintf(stderr, "%s \n", err_string); | 95 fprintf(stderr, "%s \n", err_string); |
94 free(err_string); | 96 free(err_string); |
95 return errcode; | 97 return errcode; |
96 } | 98 } |
97 | 99 |
113 { | 115 { |
114 char *usage = "\ | 116 char *usage = "\ |
115 Usage: rcg [options] <PATTERN> \n\ | 117 Usage: rcg [options] <PATTERN> \n\ |
116 \n\ | 118 \n\ |
117 Options: \n\ | 119 Options: \n\ |
118 -l, --line Highlight whole line \n\ | 120 -z, --bufsize <size> Buffer size \n\ |
119 -B, --bold Bold \n\ | 121 -l, --line Highlight whole line \n\ |
120 -b <color> Background color \n\ | 122 -B, --bold Bold \n\ |
121 -f <color> Foreground color \n\ | 123 -b <color> Background color \n\ |
124 -f <color> Foreground color \n\ | |
122 \n\ | 125 \n\ |
123 <color> can be one of the following: \n\ | 126 <color> can be one of the following: \n\ |
124 "; | 127 "; |
125 | 128 |
126 fprintf(stdout, usage); | 129 fprintf(stdout, usage); |
127 | 130 |
128 Colors *c; | 131 Colors *c; |
129 for (c = COLORS; c->name; c++) | 132 for (c = (Colors *)COLORS; c->name; c++) |
130 fprintf(stdout, " %s \n", c->name); | 133 fprintf(stdout, " %s \n", c->name); |
131 | 134 |
132 return EXIT_ARGS_ERROR; | 135 return EXIT_ARGS_ERROR; |
133 } | 136 } |
134 | 137 |
138 Exit_code bufsize_args_error() | |
139 { | |
140 fprintf(stdout, "bufsize has to be a minimum of %d \n", (int)BUFSIZE_MIN); | |
141 return EXIT_ARGS_ERROR; | |
142 } | |
143 | |
135 // returns regular expression argument | 144 // returns regular expression argument |
136 char *parseArgs(int argc, char **argv) | 145 char *parseArgs(int argc, char **argv) |
137 { | 146 { |
138 static struct option long_options[] = | 147 static struct option long_options[] = |
139 { | 148 { |
140 { "line", 0, 0, 'l' }, | 149 { "line", 0, NULL, 'l' }, |
141 { "bold", 0, 0, 'B' }, | 150 { "bold", 0, NULL, 'B' }, |
142 { 0, 0, 0, 0 }, | 151 { "bufsize", required_argument, NULL, 'z' }, |
152 { NULL, 0, NULL, 0 }, | |
143 }; | 153 }; |
144 | 154 |
145 int c; | 155 int c; |
146 int l; | 156 while ((c = getopt_long(argc, argv, "lBz:f:b:", long_options, NULL)) >= 0) |
147 while ((c = getopt_long(argc, argv, "lBf:b:", long_options, &l)) >= 0) | |
148 { | 157 { |
149 if (c == 'l') | 158 if (c == 'l') |
150 colorLine = 1; | 159 colorLine = 1; |
151 else if (c == 'B') | 160 else if (c == 'B') |
152 embolden = 1; | 161 embolden = 1; |
162 else if (c == 'z') | |
163 bufsize = (size_t)atoi(optarg); | |
153 else if (c == 'f') | 164 else if (c == 'f') |
154 g_fg = strdup(optarg); | 165 g_fg = strdup(optarg); |
155 else if (c == 'b') | 166 else if (c == 'b') |
156 g_bg = strdup(optarg); | 167 g_bg = strdup(optarg); |
157 } | 168 } |
158 | 169 |
159 if (optind >= argc) | 170 if (optind >= argc) |
160 exit(args_error()); | 171 exit(args_error()); |
161 | 172 |
173 if (bufsize < BUFSIZE_MIN) | |
174 exit(bufsize_args_error()); | |
175 | |
162 return argv[optind]; | 176 return argv[optind]; |
163 } | 177 } |
164 | 178 |
165 int main(int argc, char **argv) | 179 int main(int argc, char **argv) |
166 { | 180 { |
181 bufsize = BUFSIZE_MIN; | |
182 | |
167 char *re_expression = parseArgs(argc, argv); | 183 char *re_expression = parseArgs(argc, argv); |
168 | 184 char *buf = calloc(bufsize, sizeof(char)); |
169 //fprintf(stderr, "re_expression = %s \n", re_expression); //d/ 20100405 PBA | |
170 | |
171 char *buf = calloc(BUFSIZE, sizeof(char)); | |
172 | 185 |
173 int re_err; | 186 int re_err; |
174 regex_t *re = calloc(1, sizeof(regex_t)); | 187 regex_t *re = calloc(1, sizeof(regex_t)); |
175 re_err = regcomp(re, re_expression, REG_EXTENDED | REG_NEWLINE); | 188 re_err = regcomp(re, re_expression, REG_EXTENDED | REG_NEWLINE); |
176 if (re_err != 0) | 189 if (re_err != 0) |
211 exit(args_error()); | 224 exit(args_error()); |
212 } | 225 } |
213 | 226 |
214 char *out = NULL; | 227 char *out = NULL; |
215 | 228 |
216 while (fgets(buf, BUFSIZE, stdin)) | 229 while (fgets(buf, bufsize, stdin)) |
217 { | 230 { |
218 int so = -1; | 231 int so = -1; |
219 int eo = -1; | 232 int eo = -1; |
220 | 233 |
221 size_t out_len = strlen(buf) + 1; | 234 size_t out_len = strlen(buf) + 1; |