src/cline.c

changeset 44
9574a181ec26
parent 36
a7ff583e153f
child 48
0d2c13c24fd0
equal deleted inserted replaced
43:104e75d18ede 44:9574a181ec26
31 31
32 #include "cline.h" 32 #include "cline.h"
33 #include "scanner.h" 33 #include "scanner.h"
34 #include "settings.h" 34 #include "settings.h"
35 #include "arguments.h" 35 #include "arguments.h"
36 #include "stream.h"
37 #include "regex_parser.h" 36 #include "regex_parser.h"
38 37
39 void printHelpText() { 38 void printHelpText() {
40 printf( 39 printf(
41 "\nUsage:" 40 "\nUsage:"
217 add_string(directories, argv[t]); 216 add_string(directories, argv[t]);
218 } 217 }
219 } 218 }
220 } 219 }
221 220
222 /* Configure output */
223 if (!settings->verbose) {
224 close_stdout();
225 }
226
227 /* Find tokens */ 221 /* Find tokens */
228 parseCSL(includeSuffix, settings->includeSuffixes); 222 parseCSL(includeSuffix, settings->includeSuffixes);
229 parseCSL(excludeSuffix, settings->excludeSuffixes); 223 parseCSL(excludeSuffix, settings->excludeSuffixes);
230 224
231 /* Scan directories */ 225 /* Scan directories */
232 if (regex_compile_all(settings->regex)) { 226 if (regex_compile_all(settings->regex)) {
233 int lines = 0; 227 /* Don't waste memory when only the total sum is needed */
228 string_list_t *output = settings->verbose ? new_string_list_t() : NULL;
229 char *outbuf;
230
231 int lineSum = 0, lines;
234 if (directories->count == 0) { 232 if (directories->count == 0) {
235 add_string(directories, "."); 233 add_string(directories, ".");
236 } 234 }
237 for (int t = 0 ; t < directories->count ; t++) { 235 for (int t = 0 ; t < directories->count ; t++) {
238 if (t > 0) { 236 lines = scanDirectory((scanner_t){directories->items[t], 0}, settings,
239 for (int u = 0 ; u < 79 ; u++) { 237 output);
240 printf("-"); 238 lineSum += lines;
241 } 239 if (directories->count > 1 ) {
242 printf("\n"); 240 outbuf = (char*) malloc(81);
243 } 241 memset(outbuf, '-', 79);
244 lines += scanDirectory((scanner_t){directories->items[t], 0}, settings); 242 outbuf[79] = '\n';
243 outbuf[80] = 0;
244 add_string(output, outbuf);
245 outbuf = (char*) malloc(81);
246 snprintf(outbuf, 81, "%-63s%10d lines\n", directories->items[t], lines);
247 add_string(output, outbuf);
248 outbuf = (char*) malloc(81);
249 memset(outbuf, '-', 79);
250 outbuf[79] = '\n';
251 outbuf[80] = 0;
252 add_string(output, outbuf);
253 }
245 } 254 }
246 destroy_string_list_t(directories); 255 destroy_string_list_t(directories);
247 256
248 /* Print double line and line count */ 257 /* Print result */
249 for (int t = 0 ; t < 79 ; t++) { 258 if (settings->verbose) {
250 printf("="); 259 for (int i = 0 ; i < output->count ; i++) {
251 } 260 printf("%s", output->items[i]);
252 printf("\n%73d lines\n", lines); 261 free(output->items[i]);
253 262 }
254 if (settings->confusing_lnlen && settings->regex->pattern_list->count > 0) { 263
255 printf("\nSome files contain too long lines.\n" 264 for (int t = 0 ; t < 79 ; t++) {
256 "The regex parser currently supports a maximum line length of %d." 265 printf("=");
257 "\nThe result might be wrong.\n", REGEX_MAX_LINELENGTH); 266 }
258 } 267 printf("\n%73d lines\n", lineSum);
259 268
260 if (!settings->verbose) { 269 if (settings->confusing_lnlen &&
261 reopen_stdout(); 270 settings->regex->pattern_list->count > 0) {
262 printf("%d", lines); 271
263 } 272 printf("\nSome files contain too long lines.\n"
273 "The regex parser currently supports a maximum line length of %d."
274 "\nThe result might be wrong.\n", REGEX_MAX_LINELENGTH);
275 }
276 } else {
277 printf("%d", lineSum);
278 }
279 destroy_string_list_t(output);
264 destroy_settings_t(settings); 280 destroy_settings_t(settings);
265 } 281 }
266 282
267 fflush(stdout); 283 fflush(stdout);
268 fflush(stderr); 284 fflush(stderr);

mercurial