cline.c

changeset 34
fa9bda32de17
parent 33
1a2d7298bc82
child 35
35120de6ee53
     1.1 --- a/cline.c	Tue Oct 02 10:49:25 2012 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,247 +0,0 @@
     1.4 -/*
     1.5 - * cline.c
     1.6 - *
     1.7 - *  Created on: 23.05.2011
     1.8 - *      Author: Mike
     1.9 - */
    1.10 -
    1.11 -#include "cline.h"
    1.12 -#include "scanner.h"
    1.13 -#include "settings.h"
    1.14 -#include "arguments.h"
    1.15 -#include "stream.h"
    1.16 -#include "regex_parser.h"
    1.17 -
    1.18 -void printHelpText() {
    1.19 -  const char* helpText = 
    1.20 -    "\nUsage:"
    1.21 -    "\n      cline [Options] [Directories...]"
    1.22 -    "\n      cline [Options] [Directories...]"
    1.23 -    "\n\nCounts the line terminator characters (\\n) within all"
    1.24 -    " files in the specified\ndirectories."
    1.25 -    "\n\nOptions:"
    1.26 -    "\n  -b <level>          - binary file heuristics level (default medium)"
    1.27 -    "\n                        One of: ignore low medium high"
    1.28 -    "\n  -E <pattern>        - Excludes any line matching the <pattern>"
    1.29 -    "\n  -e <start> <end>    - Excludes lines between <start> and <end>"
    1.30 -    "\n                        You may use these options multiple times"
    1.31 -    "\n  -h, --help          - this help text"
    1.32 -    "\n  -m                  - print information about matching files only"
    1.33 -    "\n  -s <suffixes>       - only count files with these suffixes (separated"
    1.34 -    "\n                        by commas)"
    1.35 -    "\n  -S <suffixes>       - count any file except those with these suffixes"
    1.36 -    "\n                        (separated by commas)"
    1.37 -    "\n  -r, -R              - includes subdirectories"
    1.38 -    "\n  -v, --version       - print out version information"
    1.39 -    "\n  -V                  - turn verbose output off, print the result only"
    1.40 -    "\n\nShortcuts:"
    1.41 -    "\n  --exclude-cstyle-comments"
    1.42 -    "\n = -E \"\\s*//\" -e \"\\s*/\\*\" \"\\*/\\s*\""
    1.43 -    "\n\n"
    1.44 -    "The default call without any options is:"    
    1.45 -    "\n  cline ./\n\n"
    1.46 -    "So each file in the working directory is counted. If you want to count C"
    1.47 -    "\nsource code in your working directory and its subdirectories, type:"
    1.48 -    "\n  cline -rs .c\n"
    1.49 -    "\nIf you want to exclude comment lines, you may use the -e/-E option."
    1.50 -    "\nAfter a line matches the regex pattern <start> any following line is"
    1.51 -    "\nnot counted unless a line matches the <end> pattern. A line is still "
    1.52 -    "\ncounted when it does not start or end with the respective patterns."
    1.53 -    "\nPlease note, that cline does not remove whitespace characters as this"
    1.54 -    "\nmight not be reasonable in some cases."
    1.55 -    "\n\nExample (C without comments):"
    1.56 -    "\n  cline -s .c,.h --exclude-cstyle-comments";
    1.57 -    
    1.58 -  printf(helpText);
    1.59 -}
    1.60 -
    1.61 -int exit_with_version(settings_t* settings) {
    1.62 -  printf("cline - Revision: %s\n", VERSION);
    1.63 -  destroy_settings_t(settings);
    1.64 -  return 0;
    1.65 -}
    1.66 -
    1.67 -int exit_with_help(settings_t* settings, int code) {
    1.68 -  printHelpText();
    1.69 -  destroy_settings_t(settings);
    1.70 -  return code;
    1.71 -}
    1.72 -
    1.73 -int main(int argc, char** argv) {
    1.74 -
    1.75 -  /* Settings */
    1.76 -  settings_t *settings = new_settings_t();
    1.77 -  if (settings == NULL) {
    1.78 -    fprintf(stderr, "Memory allocation failed.\n");
    1.79 -    return 1;
    1.80 -  }
    1.81 -
    1.82 -  /* Get arguments */
    1.83 -  string_list_t *directories = new_string_list_t();
    1.84 -  if (directories == NULL) {
    1.85 -    fprintf(stderr, "Memory allocation failed.\n");
    1.86 -    return 1;
    1.87 -  }
    1.88 -  char* includeSuffix = NULL;
    1.89 -  char* excludeSuffix = NULL;
    1.90 -  int checked = 0;
    1.91 -
    1.92 -  for (int t = 1 ; t < argc ; t++) {
    1.93 -
    1.94 -    int argflags = checkArgument(argv[t], "hsSrRmvVbeE");
    1.95 -    int paropt = 0;
    1.96 -
    1.97 -    /* s */
    1.98 -    if ((argflags & 2) > 0) {
    1.99 -      if (!checkParamOpt(&paropt) || registerArgument(&checked, 2)) {
   1.100 -        return exit_with_help(settings, 1);
   1.101 -      }
   1.102 -      t++;
   1.103 -      if (t >= argc) {
   1.104 -        return exit_with_help(settings, 1);
   1.105 -      }
   1.106 -      includeSuffix = argv[t];
   1.107 -    }
   1.108 -    /* S */
   1.109 -    if ((argflags & 4) > 0) {
   1.110 -      if (!checkParamOpt(&paropt) || registerArgument(&checked, 4)) {
   1.111 -        return exit_with_help(settings, 1);
   1.112 -      }
   1.113 -      t++;
   1.114 -      if (t >= argc) {
   1.115 -        return exit_with_help(settings, 1);
   1.116 -      }
   1.117 -      excludeSuffix = argv[t];
   1.118 -    }
   1.119 -    /* h */
   1.120 -    if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) {
   1.121 -      return exit_with_help(settings, 0);
   1.122 -    }
   1.123 -    /* r, R */
   1.124 -    if ((argflags & 24) > 0) {
   1.125 -      if (registerArgument(&checked, 24)) {
   1.126 -        return exit_with_help(settings, 1);
   1.127 -      }
   1.128 -      settings->recursive = true;
   1.129 -    }
   1.130 -    /* m */
   1.131 -    if ((argflags & 32) > 0) {
   1.132 -      if (registerArgument(&checked, 32)) {
   1.133 -        return exit_with_help(settings, 1);
   1.134 -      }
   1.135 -      settings->matchesOnly = true;
   1.136 -    }
   1.137 -    /* v */
   1.138 -    if ((argflags & 64) > 0 || strcmp(argv[t], "--version") == 0) {
   1.139 -      return exit_with_version(settings);
   1.140 -    }
   1.141 -    /* V */
   1.142 -    if ((argflags & 128) > 0) {
   1.143 -      if (registerArgument(&checked, 128)) {
   1.144 -        return exit_with_help(settings, 1);
   1.145 -      }
   1.146 -      settings->verbose = false;
   1.147 -    }
   1.148 -    /* b */
   1.149 -    if ((argflags & 256) > 0) {
   1.150 -      if (!checkParamOpt(&paropt) || registerArgument(&checked, 256)) {
   1.151 -        return exit_with_help(settings, 1);
   1.152 -      }
   1.153 -      t++;
   1.154 -      if (t >= argc) {
   1.155 -        return exit_with_help(settings, 1);
   1.156 -      }
   1.157 -      if (strcasecmp(argv[t], "ignore") == 0) {
   1.158 -        settings->bfileHeuristics->level = BFILE_IGNORE;
   1.159 -      } else if (strcasecmp(argv[t], "low") == 0) {
   1.160 -        settings->bfileHeuristics->level = BFILE_LOW_ACCURACY;
   1.161 -      } else if (strcasecmp(argv[t], "medium") == 0) {
   1.162 -        settings->bfileHeuristics->level = BFILE_MEDIUM_ACCURACY;
   1.163 -      } else if (strcasecmp(argv[t], "high") == 0) {
   1.164 -        settings->bfileHeuristics->level = BFILE_HIGH_ACCURACY;
   1.165 -      } else {
   1.166 -        return exit_with_help(settings, 1);
   1.167 -      }
   1.168 -    }
   1.169 -    /* e */
   1.170 -    if ((argflags & 512) > 0) {
   1.171 -      if (!checkParamOpt(&paropt) || t + 2 >= argc) {
   1.172 -        return exit_with_help(settings, 1);
   1.173 -      }
   1.174 -      t++; add_string(settings->regex->pattern_list, argv[t]);
   1.175 -      t++; add_string(settings->regex->pattern_list, argv[t]);
   1.176 -    }
   1.177 -    /* E */
   1.178 -    if ((argflags & 1024) > 0) {
   1.179 -      t++;
   1.180 -      if (!checkParamOpt(&paropt) || t >= argc) {
   1.181 -        return exit_with_help(settings, 1);
   1.182 -      }
   1.183 -      add_string(settings->regex->pattern_list, argv[t]);
   1.184 -      add_string(settings->regex->pattern_list, "$");
   1.185 -    }
   1.186 -    if (argflags == 0) {
   1.187 -      /* SHORTCUTS */
   1.188 -      /* exclude-cstyle-comments */
   1.189 -      if (strcmp(argv[t], "--exclude-cstyle-comments") == 0) {
   1.190 -        add_string(settings->regex->pattern_list, "\\s*//");
   1.191 -        add_string(settings->regex->pattern_list, "$");
   1.192 -        add_string(settings->regex->pattern_list, "\\s*/\\*");
   1.193 -        add_string(settings->regex->pattern_list, "\\*/\\s*");
   1.194 -      }
   1.195 -      /* Path */
   1.196 -      else {
   1.197 -        add_string(directories, argv[t]);
   1.198 -      }
   1.199 -    }
   1.200 -  }
   1.201 -
   1.202 -  /* Configure output */
   1.203 -  if (!settings->verbose) {
   1.204 -    close_stdout();
   1.205 -  }
   1.206 -
   1.207 -  /* Find tokens */
   1.208 -  parseCSL(includeSuffix, settings->includeSuffixes);
   1.209 -  parseCSL(excludeSuffix, settings->excludeSuffixes);
   1.210 -
   1.211 -  /* Scan directories */
   1.212 -  if (regex_compile_all(settings->regex)) {
   1.213 -    int lines = 0;
   1.214 -    if (directories->count == 0) {
   1.215 -        add_string(directories, ".");
   1.216 -    }
   1.217 -    for (int t = 0 ; t < directories->count ; t++) {
   1.218 -      if (t > 0) {
   1.219 -          for (int u = 0 ; u < 79 ; u++) {
   1.220 -              printf("-");
   1.221 -          }
   1.222 -          printf("\n");
   1.223 -      }
   1.224 -      lines += scanDirectory((scanner_t){directories->items[t], 0}, settings);
   1.225 -    }
   1.226 -    destroy_string_list_t(directories);
   1.227 -
   1.228 -    /* Print double line and line count */
   1.229 -    for (int t = 0 ; t < 79 ; t++) {
   1.230 -      printf("=");
   1.231 -    }
   1.232 -    printf("\n%73d lines\n", lines);
   1.233 -
   1.234 -    if (settings->confusing_lnlen && settings->regex->pattern_list->count > 0) {
   1.235 -      printf("\nSome files contain too long lines.\n"
   1.236 -        "The regex parser currently supports a maximum line length of %d."
   1.237 -        "\nThe result might be wrong.\n", REGEX_MAX_LINELENGTH);
   1.238 -    }
   1.239 -
   1.240 -    if (!settings->verbose) {
   1.241 -      reopen_stdout();
   1.242 -      printf("%d", lines);
   1.243 -    }
   1.244 -    destroy_settings_t(settings);
   1.245 -  }
   1.246 -
   1.247 -  fflush(stdout);
   1.248 -  fflush(stderr);
   1.249 -  return 0;
   1.250 -}

mercurial