line sum per directory now displayed + directories without matching files are no longer displayed when -m is used + new buffering strategy replaces stream hack when -V is used

Wed, 22 May 2013 13:00:36 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 22 May 2013 13:00:36 +0200
changeset 44
9574a181ec26
parent 43
104e75d18ede
child 45
6114a2c28d28

line sum per directory now displayed + directories without matching files are no longer displayed when -m is used + new buffering strategy replaces stream hack when -V is used

Makefile file | annotate | diff | comparison | revisions
src/cline.c file | annotate | diff | comparison | revisions
src/cline.h file | annotate | diff | comparison | revisions
src/scanner.c file | annotate | diff | comparison | revisions
src/scanner.h file | annotate | diff | comparison | revisions
src/stream.c file | annotate | diff | comparison | revisions
src/stream.h file | annotate | diff | comparison | revisions
src/string_list.c file | annotate | diff | comparison | revisions
     1.1 --- a/Makefile	Wed May 22 10:57:17 2013 +0200
     1.2 +++ b/Makefile	Wed May 22 13:00:36 2013 +0200
     1.3 @@ -37,11 +37,10 @@
     1.4  CONF = gcc
     1.5  #endif
     1.6  
     1.7 -VERSION_PREFIX=1.0.
     1.8  SRCDIR=src
     1.9  BUILDDIR=build
    1.10  PREFIX=/usr
    1.11 -OBJ = arguments.o bfile_heuristics.o cline.o regex_parser.o scanner.o settings.o stream.o string_list.o suffix_fnc.o
    1.12 +OBJ = arguments.o bfile_heuristics.o cline.o regex_parser.o scanner.o settings.o string_list.o suffix_fnc.o
    1.13  BIN = $(BUILDDIR)/cline
    1.14  
    1.15  include $(CONF).mk
     2.1 --- a/src/cline.c	Wed May 22 10:57:17 2013 +0200
     2.2 +++ b/src/cline.c	Wed May 22 13:00:36 2013 +0200
     2.3 @@ -33,7 +33,6 @@
     2.4  #include "scanner.h"
     2.5  #include "settings.h"
     2.6  #include "arguments.h"
     2.7 -#include "stream.h"
     2.8  #include "regex_parser.h"
     2.9  
    2.10  void printHelpText() {
    2.11 @@ -219,48 +218,65 @@
    2.12      }
    2.13    }
    2.14  
    2.15 -  /* Configure output */
    2.16 -  if (!settings->verbose) {
    2.17 -    close_stdout();
    2.18 -  }
    2.19 -
    2.20    /* Find tokens */
    2.21    parseCSL(includeSuffix, settings->includeSuffixes);
    2.22    parseCSL(excludeSuffix, settings->excludeSuffixes);
    2.23  
    2.24    /* Scan directories */
    2.25    if (regex_compile_all(settings->regex)) {
    2.26 -    int lines = 0;
    2.27 +    /* Don't waste memory when only the total sum is needed */
    2.28 +    string_list_t *output = settings->verbose ? new_string_list_t() : NULL;
    2.29 +    char *outbuf;
    2.30 +    
    2.31 +    int lineSum = 0, lines;
    2.32      if (directories->count == 0) {
    2.33          add_string(directories, ".");
    2.34      }
    2.35      for (int t = 0 ; t < directories->count ; t++) {
    2.36 -      if (t > 0) {
    2.37 -          for (int u = 0 ; u < 79 ; u++) {
    2.38 -              printf("-");
    2.39 -          }
    2.40 -          printf("\n");
    2.41 +      lines = scanDirectory((scanner_t){directories->items[t], 0}, settings,
    2.42 +          output);
    2.43 +      lineSum += lines;
    2.44 +      if (directories->count > 1 ) {
    2.45 +        outbuf = (char*) malloc(81);
    2.46 +        memset(outbuf, '-', 79);
    2.47 +        outbuf[79] = '\n';
    2.48 +        outbuf[80] = 0;
    2.49 +        add_string(output, outbuf);
    2.50 +        outbuf = (char*) malloc(81);
    2.51 +        snprintf(outbuf, 81, "%-63s%10d lines\n", directories->items[t], lines);
    2.52 +        add_string(output, outbuf);
    2.53 +        outbuf = (char*) malloc(81);
    2.54 +        memset(outbuf, '-', 79);
    2.55 +        outbuf[79] = '\n';
    2.56 +        outbuf[80] = 0;
    2.57 +        add_string(output, outbuf);
    2.58        }
    2.59 -      lines += scanDirectory((scanner_t){directories->items[t], 0}, settings);
    2.60      }
    2.61      destroy_string_list_t(directories);
    2.62  
    2.63 -    /* Print double line and line count */
    2.64 -    for (int t = 0 ; t < 79 ; t++) {
    2.65 -      printf("=");
    2.66 +    /* Print result */
    2.67 +    if (settings->verbose) {
    2.68 +      for (int i = 0 ; i < output->count ; i++) {
    2.69 +        printf("%s", output->items[i]);
    2.70 +        free(output->items[i]);
    2.71 +      }
    2.72 +      
    2.73 +      for (int t = 0 ; t < 79 ; t++) {
    2.74 +        printf("=");
    2.75 +      }
    2.76 +      printf("\n%73d lines\n", lineSum);
    2.77 +
    2.78 +      if (settings->confusing_lnlen &&
    2.79 +          settings->regex->pattern_list->count > 0) {
    2.80 +
    2.81 +        printf("\nSome files contain too long lines.\n"
    2.82 +          "The regex parser currently supports a maximum line length of %d."
    2.83 +          "\nThe result might be wrong.\n", REGEX_MAX_LINELENGTH);
    2.84 +      }
    2.85 +    } else {
    2.86 +      printf("%d", lineSum);
    2.87      }
    2.88 -    printf("\n%73d lines\n", lines);
    2.89 -
    2.90 -    if (settings->confusing_lnlen && settings->regex->pattern_list->count > 0) {
    2.91 -      printf("\nSome files contain too long lines.\n"
    2.92 -        "The regex parser currently supports a maximum line length of %d."
    2.93 -        "\nThe result might be wrong.\n", REGEX_MAX_LINELENGTH);
    2.94 -    }
    2.95 -
    2.96 -    if (!settings->verbose) {
    2.97 -      reopen_stdout();
    2.98 -      printf("%d", lines);
    2.99 -    }
   2.100 +    destroy_string_list_t(output);
   2.101      destroy_settings_t(settings);
   2.102    }
   2.103  
     3.1 --- a/src/cline.h	Wed May 22 10:57:17 2013 +0200
     3.2 +++ b/src/cline.h	Wed May 22 13:00:36 2013 +0200
     3.3 @@ -32,7 +32,7 @@
     3.4  #ifndef CLINE_H_
     3.5  #define CLINE_H_
     3.6  
     3.7 -const char* VERSION="1.0.37";
     3.8 +const char* VERSION="1.0.44";
     3.9  
    3.10  #include "stdinc.h"
    3.11  #include "settings.h"
     4.1 --- a/src/scanner.c	Wed May 22 10:57:17 2013 +0200
     4.2 +++ b/src/scanner.c	Wed May 22 13:00:36 2013 +0200
     4.3 @@ -117,11 +117,13 @@
     4.4    return list;
     4.5  }
     4.6  
     4.7 -int scanDirectory(scanner_t scanner, settings_t* settings) {
     4.8 +int scanDirectory(scanner_t scanner, settings_t* settings,
     4.9 +    string_list_t* output) {
    4.10  
    4.11    int lines, a;
    4.12    int lineSum = 0;
    4.13    bool bfile;
    4.14 +  char *outbuf;
    4.15  
    4.16    filelist_t *filelist = buildFileList(scanner, settings, NULL);
    4.17  
    4.18 @@ -129,11 +131,28 @@
    4.19  
    4.20      /* Scan subdirectories */
    4.21      if (!S_ISREG(filelist->st_mode)) {
    4.22 -      printf("%*s\n", filelist->displayname_len+scanner.spaces,
    4.23 +      if (settings->recursive && S_ISDIR(filelist->st_mode)) {
    4.24 +        string_list_t *recoutput = new_string_list_t();
    4.25 +        lines = scanDirectory(
    4.26 +            (scanner_t) {filelist->filename, scanner.spaces+1},
    4.27 +            settings, recoutput);
    4.28 +        lineSum += lines;
    4.29 +        if (!settings->matchesOnly || recoutput->count > 0) {
    4.30 +          outbuf = (char*) malloc(81);
    4.31 +          snprintf(outbuf, 81, "%*s/%*s%13d lines\n",
    4.32 +              filelist->displayname_len+scanner.spaces, filelist->displayname,
    4.33 +              60-filelist->displayname_len-scanner.spaces-1, "", lines);
    4.34 +          add_string(output, outbuf);
    4.35 +          for (int i = 0 ; i < recoutput->count ; i++) {
    4.36 +            add_string(output, recoutput->items[i]);
    4.37 +          }
    4.38 +        }
    4.39 +        destroy_string_list_t(recoutput);
    4.40 +      } else {
    4.41 +        outbuf = (char*) malloc(81);
    4.42 +        snprintf(outbuf, 81, "%*s\n", filelist->displayname_len+scanner.spaces,
    4.43            filelist->displayname);
    4.44 -      if (settings->recursive && S_ISDIR(filelist->st_mode)) {
    4.45 -        lineSum += scanDirectory(
    4.46 -            (scanner_t) {filelist->filename, scanner.spaces+1}, settings);
    4.47 +        add_string(output, outbuf);
    4.48        }
    4.49      } else {
    4.50        if ((settings->includeSuffixes->count == 0
    4.51 @@ -149,8 +168,10 @@
    4.52  
    4.53          FILE *file = fopen(filelist->filename, "r");
    4.54          if (file == NULL) {
    4.55 -          printf("%*s", filelist->displayname_len+scanner.spaces,
    4.56 +          outbuf = (char*) malloc(81);
    4.57 +          snprintf(outbuf, 81, "%*s", filelist->displayname_len+scanner.spaces,
    4.58                filelist->displayname);
    4.59 +          add_string(output, outbuf);
    4.60            perror("  File acces failed");
    4.61          } else {
    4.62            do {
    4.63 @@ -184,23 +205,30 @@
    4.64            /* Print and sum line count */
    4.65            if (bfile) {
    4.66              if (!settings->matchesOnly) {
    4.67 -              printf("%*s%*s%19s\n", filelist->displayname_len+scanner.spaces,
    4.68 +              outbuf = (char*) malloc(81);
    4.69 +              snprintf(outbuf, 81,
    4.70 +                  "%*s%*s%19s\n", filelist->displayname_len+scanner.spaces,
    4.71                    filelist->displayname,
    4.72                    60-filelist->displayname_len-scanner.spaces, "", "binary");
    4.73 +              add_string(output, outbuf);
    4.74              }
    4.75            } else {
    4.76              lineSum += lines;
    4.77 -            printf("%*s%*s%13d lines\n",
    4.78 +            outbuf = (char*) malloc(81);
    4.79 +            snprintf(outbuf, 81, "%*s%*s%13d lines\n",
    4.80                  filelist->displayname_len+scanner.spaces, filelist->displayname,
    4.81                  60-filelist->displayname_len-scanner.spaces, "", lines);
    4.82 +            add_string(output, outbuf);
    4.83            }
    4.84          }
    4.85        } else {
    4.86          if (!settings->matchesOnly) {
    4.87            /* Print hint */
    4.88 -          printf("%*s%*s%19s\n",
    4.89 +          outbuf = (char*) malloc(81);
    4.90 +          snprintf(outbuf, 81, "%*s%*s%19s\n",
    4.91                filelist->displayname_len+scanner.spaces, filelist->displayname,
    4.92                60-filelist->displayname_len-scanner.spaces, "", "no match");
    4.93 +          add_string(output, outbuf);
    4.94          }
    4.95        }
    4.96      }
     5.1 --- a/src/scanner.h	Wed May 22 10:57:17 2013 +0200
     5.2 +++ b/src/scanner.h	Wed May 22 13:00:36 2013 +0200
     5.3 @@ -34,6 +34,7 @@
     5.4  
     5.5  #include "stdinc.h"
     5.6  #include "settings.h"
     5.7 +#include "string_list.h"
     5.8  
     5.9  typedef struct {
    5.10    char *dir;
    5.11 @@ -44,7 +45,8 @@
    5.12  extern "C" {
    5.13  #endif
    5.14  
    5.15 -int scanDirectory(scanner_t scanner, settings_t* settings);
    5.16 +int scanDirectory(scanner_t scanner, settings_t* settings,
    5.17 +        string_list_t* output);
    5.18  
    5.19  #ifdef _cplusplus
    5.20  }
     6.1 --- a/src/stream.c	Wed May 22 10:57:17 2013 +0200
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,49 +0,0 @@
     6.4 -/*
     6.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 
     6.6 - * Copyright 2013 Mike Becker. All rights reserved.
     6.7 - * 
     6.8 - * Redistribution and use in source and binary forms, with or without
     6.9 - * modification, are permitted provided that the following conditions are met:
    6.10 - * 
    6.11 - * 1. Redistributions of source code must retain the above copyright
    6.12 - * notice, this list of conditions and the following disclaimer.
    6.13 - * 
    6.14 - * 2. Redistributions in binary form must reproduce the above copyright
    6.15 - * notice, this list of conditions and the following disclaimer in the
    6.16 - * documentation and/or other materials provided with the distribution.
    6.17 - * 
    6.18 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    6.19 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    6.20 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    6.21 - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
    6.22 - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    6.23 - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    6.24 - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    6.25 - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    6.26 - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    6.27 - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
    6.28 - *
    6.29 - * stream.c
    6.30 - *
    6.31 - *  Created on: 20.09.2011
    6.32 - *      Author: Mike
    6.33 - */
    6.34 -
    6.35 -#include "stream.h"
    6.36 -
    6.37 -void close_stdout() {
    6.38 -#ifdef _WIN32
    6.39 -  _STREAM_STDOUT = dup(STDOUT_FILENO);
    6.40 -#endif
    6.41 -  stdout = freopen("/dev/null", "w", stdout);
    6.42 -}
    6.43 -
    6.44 -void reopen_stdout() {
    6.45 -#ifdef _WIN32
    6.46 -  close(STDOUT_FILENO);
    6.47 -  fdopen(dup(_STREAM_STDOUT), "wa");
    6.48 -  close(_STREAM_STDOUT);
    6.49 -#else
    6.50 -  stdout = freopen("/dev/stdout", "w", stdout);
    6.51 -#endif
    6.52 -}
     7.1 --- a/src/stream.h	Wed May 22 10:57:17 2013 +0200
     7.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.3 @@ -1,52 +0,0 @@
     7.4 -/*
     7.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 
     7.6 - * Copyright 2013 Mike Becker. All rights reserved.
     7.7 - * 
     7.8 - * Redistribution and use in source and binary forms, with or without
     7.9 - * modification, are permitted provided that the following conditions are met:
    7.10 - * 
    7.11 - * 1. Redistributions of source code must retain the above copyright
    7.12 - * notice, this list of conditions and the following disclaimer.
    7.13 - * 
    7.14 - * 2. Redistributions in binary form must reproduce the above copyright
    7.15 - * notice, this list of conditions and the following disclaimer in the
    7.16 - * documentation and/or other materials provided with the distribution.
    7.17 - * 
    7.18 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    7.19 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    7.20 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    7.21 - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
    7.22 - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    7.23 - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    7.24 - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    7.25 - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    7.26 - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    7.27 - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
    7.28 - *
    7.29 - * stream.h
    7.30 - *
    7.31 - *  Created on: 20.09.2011
    7.32 - *      Author: Mike
    7.33 - */
    7.34 -
    7.35 -#ifndef STREAM_H_
    7.36 -#define STREAM_H_
    7.37 -
    7.38 -#include "stdinc.h"
    7.39 -
    7.40 -#ifdef _WIN32
    7.41 -int _STREAM_STDOUT;
    7.42 -#endif
    7.43 -
    7.44 -#ifdef _cplusplus
    7.45 -extern "C" {
    7.46 -#endif
    7.47 -
    7.48 -void close_stdout();
    7.49 -void reopen_stdout();
    7.50 -
    7.51 -#ifdef _cplusplus
    7.52 -extern "C" }
    7.53 -#endif
    7.54 -
    7.55 -#endif /* STREAM_H_ */
     8.1 --- a/src/string_list.c	Wed May 22 10:57:17 2013 +0200
     8.2 +++ b/src/string_list.c	Wed May 22 13:00:36 2013 +0200
     8.3 @@ -40,19 +40,28 @@
     8.4  }
     8.5  
     8.6  void destroy_string_list_t(string_list_t* list) {
     8.7 -  if (list->items != NULL) {
     8.8 -    free(list->items);
     8.9 -  }
    8.10 -  free(list);
    8.11 -}
    8.12 -
    8.13 -void add_string(string_list_t* list, char* item) {
    8.14 -  char** reallocated_list =
    8.15 -    realloc(list->items, sizeof(char*) * (list->count + 1));
    8.16 -  if (reallocated_list != NULL) {
    8.17 -    list->items = reallocated_list;
    8.18 -    list->items[list->count] = item;
    8.19 -    list->count++;
    8.20 +  if (list) {
    8.21 +    if (list->items) {
    8.22 +      free(list->items);
    8.23 +    }
    8.24 +    free(list);
    8.25    }
    8.26  }
    8.27  
    8.28 +/* Adds an item to the list, if a NULL-list is specified, the item will
    8.29 + * be freed. This way a NULL-list can be used as garbage bin.
    8.30 + */
    8.31 +void add_string(string_list_t* list, char* item) {
    8.32 +  if (list) {
    8.33 +    char** reallocated_list =
    8.34 +      realloc(list->items, sizeof(char*) * (list->count + 1));
    8.35 +    if (reallocated_list != NULL) {
    8.36 +      list->items = reallocated_list;
    8.37 +      list->items[list->count] = item;
    8.38 +      list->count++;
    8.39 +    }
    8.40 +  } else {
    8.41 +    free(item);
    8.42 +  }
    8.43 +}
    8.44 +

mercurial