Thu, 09 Feb 2012 15:56:18 +0100
allow parallel use of -s and -S
universe@10 | 1 | /* |
universe@10 | 2 | * cline.c |
universe@10 | 3 | * |
universe@10 | 4 | * Created on: 23.05.2011 |
universe@20 | 5 | * Author: Mike |
universe@10 | 6 | */ |
universe@10 | 7 | |
universe@3 | 8 | #include "cline.h" |
universe@10 | 9 | #include "scanner.h" |
universe@10 | 10 | #include "settings.h" |
universe@12 | 11 | #include "arguments.h" |
universe@16 | 12 | #include "stream.h" |
universe@27 | 13 | #include "regex_parser.h" |
universe@0 | 14 | |
universe@12 | 15 | void printHelpText() { |
universe@0 | 16 | const char* helpText = |
universe@1 | 17 | "\nUsage:" |
universe@27 | 18 | "\n cline [Options] [Directory]" |
universe@27 | 19 | "\n cline [Options] [Directory]" |
universe@0 | 20 | "\n\nCounts the line terminator characters (\\n) within all" |
universe@0 | 21 | " files in the specified\ndirectory." |
universe@0 | 22 | "\n\nOptions:" |
universe@21 | 23 | "\n -b <level> - binary file heuristics level (default medium)" |
universe@21 | 24 | "\n One of: ignore low medium high" |
universe@28 | 25 | "\n -E <pattern> - Excludes any line matching the <pattern>" |
universe@27 | 26 | "\n -e <start> <end> - Excludes lines between <start> and <end>" |
universe@28 | 27 | "\n You may use these options multiple times" |
universe@0 | 28 | "\n -h, --help - this help text" |
universe@1 | 29 | "\n -m - print information about matching files only" |
universe@1 | 30 | "\n -s <suffixes> - only count files with these suffixes (separated" |
universe@0 | 31 | "\n by commas)" |
universe@1 | 32 | "\n -S <suffixes> - count any file except those with these suffixes" |
universe@0 | 33 | "\n (separated by commas)" |
universe@1 | 34 | "\n -r, -R - includes subdirectories" |
universe@14 | 35 | "\n -v, --version - print out version information" |
universe@16 | 36 | "\n -V - turn verbose output off, print the result only" |
universe@0 | 37 | "\n\n" |
universe@1 | 38 | "The default call without any options is:" |
universe@28 | 39 | "\n cline ./\n\n" |
universe@7 | 40 | "So each file in the working directory is counted. If you want to count C" |
universe@7 | 41 | "\nsource code in your working directory and its subdirectories, type:" |
universe@27 | 42 | "\n cline -rs .c\n" |
universe@28 | 43 | "\nIf you want to exclude comment lines, you may use the -e/-E option." |
universe@27 | 44 | "\nAfter a line matches the regex pattern <start> any following line is" |
universe@28 | 45 | "\nnot counted unless a line matches the <end> pattern. A line is still " |
universe@28 | 46 | "\ncounted when it does not start or end with the respective patterns." |
universe@28 | 47 | "\nPlease note, that cline does not remove whitespace characters as this" |
universe@28 | 48 | "\nmight not be reasonable in some cases." |
universe@28 | 49 | "\n\nExample (C comments):" |
universe@28 | 50 | "\n cline -s .c,.h -E \"\\s*//\" -e \"\\s*/\\*\" \"\\*/\\s*\""; |
universe@1 | 51 | |
universe@12 | 52 | printf(helpText); |
universe@1 | 53 | } |
universe@1 | 54 | |
universe@14 | 55 | int exit_with_version(settings_t* settings) { |
universe@16 | 56 | printf("cline - Revision: %s", VERSION); |
universe@14 | 57 | destroy_settings_t(settings); |
universe@14 | 58 | return 0; |
universe@14 | 59 | } |
universe@14 | 60 | |
universe@12 | 61 | int exit_with_help(settings_t* settings, int code) { |
universe@12 | 62 | printHelpText(); |
universe@8 | 63 | destroy_settings_t(settings); |
universe@8 | 64 | return code; |
universe@8 | 65 | } |
universe@8 | 66 | |
universe@1 | 67 | int main(int argc, char** argv) { |
universe@0 | 68 | |
universe@22 | 69 | /* Settings */ |
universe@3 | 70 | settings_t *settings = new_settings_t(); |
universe@5 | 71 | if (settings == NULL) { |
universe@5 | 72 | fprintf(stderr, "Memory allocation failed.\n"); |
universe@5 | 73 | return 1; |
universe@5 | 74 | } |
universe@3 | 75 | |
universe@22 | 76 | /* Get arguments */ |
universe@7 | 77 | char* directory = "./"; |
universe@30 | 78 | char* includeSuffix = NULL; |
universe@30 | 79 | char* excludeSuffix = NULL; |
universe@8 | 80 | int checked = 0; |
universe@0 | 81 | |
universe@1 | 82 | for (int t = 1 ; t < argc ; t++) { |
universe@1 | 83 | |
universe@28 | 84 | int argflags = checkArgument(argv[t], "hsSrRmvVbeE"); |
universe@30 | 85 | int paropt = 0; |
universe@1 | 86 | |
universe@30 | 87 | /* s */ |
universe@30 | 88 | if ((argflags & 2) > 0) { |
universe@30 | 89 | if (!checkParamOpt(&paropt) || registerArgument(&checked, 2)) { |
universe@12 | 90 | return exit_with_help(settings, 1); |
universe@0 | 91 | } |
universe@1 | 92 | t++; |
universe@1 | 93 | if (t >= argc) { |
universe@12 | 94 | return exit_with_help(settings, 1); |
universe@1 | 95 | } |
universe@30 | 96 | includeSuffix = argv[t]; |
universe@30 | 97 | } |
universe@30 | 98 | /* S */ |
universe@30 | 99 | if ((argflags & 4) > 0) { |
universe@30 | 100 | if (!checkParamOpt(&paropt) || registerArgument(&checked, 4)) { |
universe@30 | 101 | return exit_with_help(settings, 1); |
universe@30 | 102 | } |
universe@30 | 103 | t++; |
universe@30 | 104 | if (t >= argc) { |
universe@30 | 105 | return exit_with_help(settings, 1); |
universe@30 | 106 | } |
universe@30 | 107 | excludeSuffix = argv[t]; |
universe@0 | 108 | } |
universe@22 | 109 | /* h */ |
universe@1 | 110 | if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) { |
universe@14 | 111 | return exit_with_help(settings, 0); |
universe@0 | 112 | } |
universe@22 | 113 | /* r, R */ |
universe@1 | 114 | if ((argflags & 24) > 0) { |
universe@8 | 115 | if (registerArgument(&checked, 24)) { |
universe@12 | 116 | return exit_with_help(settings, 1); |
universe@0 | 117 | } |
universe@3 | 118 | settings->recursive = true; |
universe@0 | 119 | } |
universe@22 | 120 | /* m */ |
universe@1 | 121 | if ((argflags & 32) > 0) { |
universe@8 | 122 | if (registerArgument(&checked, 32)) { |
universe@12 | 123 | return exit_with_help(settings, 1); |
universe@0 | 124 | } |
universe@3 | 125 | settings->matchesOnly = true; |
universe@0 | 126 | } |
universe@22 | 127 | /* v */ |
universe@14 | 128 | if ((argflags & 64) > 0 || strcmp(argv[t], "--version") == 0) { |
universe@14 | 129 | return exit_with_version(settings); |
universe@14 | 130 | } |
universe@22 | 131 | /* V */ |
universe@16 | 132 | if ((argflags & 128) > 0) { |
universe@16 | 133 | if (registerArgument(&checked, 128)) { |
universe@16 | 134 | return exit_with_help(settings, 1); |
universe@16 | 135 | } |
universe@16 | 136 | settings->verbose = false; |
universe@16 | 137 | } |
universe@22 | 138 | /* b */ |
universe@21 | 139 | if ((argflags & 256) > 0) { |
universe@30 | 140 | if (!checkParamOpt(&paropt) || registerArgument(&checked, 256)) { |
universe@21 | 141 | return exit_with_help(settings, 1); |
universe@21 | 142 | } |
universe@21 | 143 | t++; |
universe@21 | 144 | if (t >= argc) { |
universe@21 | 145 | return exit_with_help(settings, 1); |
universe@21 | 146 | } |
universe@24 | 147 | if (strcasecmp(argv[t], "ignore") == 0) { |
universe@21 | 148 | settings->bfileHeuristics->level = BFILE_IGNORE; |
universe@24 | 149 | } else if (strcasecmp(argv[t], "low") == 0) { |
universe@21 | 150 | settings->bfileHeuristics->level = BFILE_LOW_ACCURACY; |
universe@24 | 151 | } else if (strcasecmp(argv[t], "medium") == 0) { |
universe@21 | 152 | settings->bfileHeuristics->level = BFILE_MEDIUM_ACCURACY; |
universe@24 | 153 | } else if (strcasecmp(argv[t], "high") == 0) { |
universe@21 | 154 | settings->bfileHeuristics->level = BFILE_HIGH_ACCURACY; |
universe@21 | 155 | } else { |
universe@21 | 156 | return exit_with_help(settings, 1); |
universe@21 | 157 | } |
universe@21 | 158 | } |
universe@28 | 159 | /* e */ |
universe@27 | 160 | if ((argflags & 512) > 0) { |
universe@30 | 161 | if (!checkParamOpt(&paropt) || t + 2 >= argc) { |
universe@27 | 162 | return exit_with_help(settings, 1); |
universe@27 | 163 | } |
universe@27 | 164 | t++; add_string(settings->regex->pattern_list, argv[t]); |
universe@27 | 165 | t++; add_string(settings->regex->pattern_list, argv[t]); |
universe@27 | 166 | } |
universe@28 | 167 | /* E */ |
universe@28 | 168 | if ((argflags & 1024) > 0) { |
universe@28 | 169 | t++; |
universe@30 | 170 | if (!checkParamOpt(&paropt) || t >= argc) { |
universe@28 | 171 | return exit_with_help(settings, 1); |
universe@28 | 172 | } |
universe@28 | 173 | add_string(settings->regex->pattern_list, argv[t]); |
universe@28 | 174 | add_string(settings->regex->pattern_list, "$"); |
universe@28 | 175 | } |
universe@22 | 176 | /* Path */ |
universe@1 | 177 | if (argflags == 0) { |
universe@8 | 178 | if (registerArgument(&checked, 1024)) { |
universe@12 | 179 | return exit_with_help(settings, 1); |
universe@0 | 180 | } |
universe@0 | 181 | directory = argv[t]; |
universe@0 | 182 | } |
universe@0 | 183 | } |
universe@0 | 184 | |
universe@22 | 185 | /* Configure output */ |
universe@16 | 186 | if (!settings->verbose) { |
universe@16 | 187 | close_stdout(); |
universe@16 | 188 | } |
universe@16 | 189 | |
universe@22 | 190 | /* Find tokens */ |
universe@30 | 191 | parseCSL(includeSuffix, settings->includeSuffixes); |
universe@30 | 192 | parseCSL(excludeSuffix, settings->excludeSuffixes); |
universe@0 | 193 | |
universe@22 | 194 | /* Scan directory */ |
universe@28 | 195 | if (regex_compile_all(settings->regex)) { |
universe@28 | 196 | int lines = scanDirectory((scanner_t){directory, 0}, settings); |
universe@28 | 197 | destroy_settings_t(settings); |
universe@0 | 198 | |
universe@28 | 199 | /* Print double line and line count */ |
universe@28 | 200 | for (int t = 0 ; t < 79 ; t++) { |
universe@28 | 201 | printf("="); |
universe@28 | 202 | } |
universe@28 | 203 | printf("\n%73d lines\n", lines); |
universe@0 | 204 | |
universe@28 | 205 | if (settings->confusing_lnlen && settings->regex->pattern_list->count > 0) { |
universe@28 | 206 | printf("\nSome files contain too long lines.\n" |
universe@28 | 207 | "The regex parser currently supports a maximum line length of %d." |
universe@28 | 208 | "\nThe result might be wrong.\n", REGEX_MAX_LINELENGTH); |
universe@28 | 209 | } |
universe@25 | 210 | |
universe@28 | 211 | if (!settings->verbose) { |
universe@28 | 212 | reopen_stdout(); |
universe@28 | 213 | printf("%d", lines); |
universe@28 | 214 | } |
universe@16 | 215 | } |
universe@16 | 216 | |
universe@16 | 217 | fflush(stdout); |
universe@28 | 218 | fflush(stderr); |
universe@0 | 219 | return 0; |
universe@0 | 220 | } |