Wed, 31 May 2017 11:40:07 +0200
replaces Makefiles with autoconf files
universe@10 | 1 | /* |
universe@34 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@36 | 3 | * Copyright 2013 Mike Becker. All rights reserved. |
universe@34 | 4 | * |
universe@34 | 5 | * Redistribution and use in source and binary forms, with or without |
universe@34 | 6 | * modification, are permitted provided that the following conditions are met: |
universe@34 | 7 | * |
universe@34 | 8 | * 1. Redistributions of source code must retain the above copyright |
universe@34 | 9 | * notice, this list of conditions and the following disclaimer. |
universe@34 | 10 | * |
universe@34 | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@34 | 12 | * notice, this list of conditions and the following disclaimer in the |
universe@34 | 13 | * documentation and/or other materials provided with the distribution. |
universe@34 | 14 | * |
universe@34 | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@34 | 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@34 | 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
universe@34 | 18 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
universe@34 | 19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
universe@34 | 20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
universe@34 | 21 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
universe@34 | 22 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
universe@34 | 23 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
universe@34 | 24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
universe@34 | 25 | * |
universe@10 | 26 | * cline.c |
universe@10 | 27 | * |
universe@10 | 28 | * Created on: 23.05.2011 |
universe@20 | 29 | * Author: Mike |
universe@10 | 30 | */ |
universe@10 | 31 | |
universe@3 | 32 | #include "cline.h" |
universe@10 | 33 | #include "scanner.h" |
universe@10 | 34 | #include "settings.h" |
universe@12 | 35 | #include "arguments.h" |
universe@27 | 36 | #include "regex_parser.h" |
universe@0 | 37 | |
universe@12 | 38 | void printHelpText() { |
universe@34 | 39 | printf( |
universe@1 | 40 | "\nUsage:" |
universe@33 | 41 | "\n cline [Options] [Directories...]" |
universe@33 | 42 | "\n cline [Options] [Directories...]" |
universe@0 | 43 | "\n\nCounts the line terminator characters (\\n) within all" |
universe@33 | 44 | " files in the specified\ndirectories." |
universe@0 | 45 | "\n\nOptions:" |
universe@21 | 46 | "\n -b <level> - binary file heuristics level (default medium)" |
universe@21 | 47 | "\n One of: ignore low medium high" |
universe@28 | 48 | "\n -E <pattern> - Excludes any line matching the <pattern>" |
universe@27 | 49 | "\n -e <start> <end> - Excludes lines between <start> and <end>" |
universe@28 | 50 | "\n You may use these options multiple times" |
universe@0 | 51 | "\n -h, --help - this help text" |
universe@1 | 52 | "\n -m - print information about matching files only" |
universe@1 | 53 | "\n -s <suffixes> - only count files with these suffixes (separated" |
universe@0 | 54 | "\n by commas)" |
universe@1 | 55 | "\n -S <suffixes> - count any file except those with these suffixes" |
universe@0 | 56 | "\n (separated by commas)" |
universe@1 | 57 | "\n -r, -R - includes subdirectories" |
universe@14 | 58 | "\n -v, --version - print out version information" |
universe@16 | 59 | "\n -V - turn verbose output off, print the result only" |
universe@31 | 60 | "\n\nShortcuts:" |
universe@31 | 61 | "\n --exclude-cstyle-comments" |
universe@31 | 62 | "\n = -E \"\\s*//\" -e \"\\s*/\\*\" \"\\*/\\s*\"" |
universe@0 | 63 | "\n\n" |
universe@1 | 64 | "The default call without any options is:" |
universe@28 | 65 | "\n cline ./\n\n" |
universe@7 | 66 | "So each file in the working directory is counted. If you want to count C" |
universe@7 | 67 | "\nsource code in your working directory and its subdirectories, type:" |
universe@27 | 68 | "\n cline -rs .c\n" |
universe@28 | 69 | "\nIf you want to exclude comment lines, you may use the -e/-E option." |
universe@27 | 70 | "\nAfter a line matches the regex pattern <start> any following line is" |
universe@28 | 71 | "\nnot counted unless a line matches the <end> pattern. A line is still " |
universe@28 | 72 | "\ncounted when it does not start or end with the respective patterns." |
universe@28 | 73 | "\nPlease note, that cline does not remove whitespace characters as this" |
universe@28 | 74 | "\nmight not be reasonable in some cases." |
universe@31 | 75 | "\n\nExample (C without comments):" |
universe@36 | 76 | "\n cline -s .c,.h --exclude-cstyle-comments" |
universe@36 | 77 | "\n"); |
universe@1 | 78 | } |
universe@1 | 79 | |
universe@14 | 80 | int exit_with_version(settings_t* settings) { |
universe@35 | 81 | printf("cline - Version: %s\n", VERSION); |
universe@14 | 82 | destroy_settings_t(settings); |
universe@14 | 83 | return 0; |
universe@14 | 84 | } |
universe@14 | 85 | |
universe@12 | 86 | int exit_with_help(settings_t* settings, int code) { |
universe@12 | 87 | printHelpText(); |
universe@8 | 88 | destroy_settings_t(settings); |
universe@8 | 89 | return code; |
universe@8 | 90 | } |
universe@8 | 91 | |
universe@1 | 92 | int main(int argc, char** argv) { |
universe@0 | 93 | |
universe@22 | 94 | /* Settings */ |
universe@3 | 95 | settings_t *settings = new_settings_t(); |
universe@5 | 96 | if (settings == NULL) { |
universe@5 | 97 | fprintf(stderr, "Memory allocation failed.\n"); |
universe@5 | 98 | return 1; |
universe@5 | 99 | } |
universe@3 | 100 | |
universe@22 | 101 | /* Get arguments */ |
universe@33 | 102 | string_list_t *directories = new_string_list_t(); |
universe@33 | 103 | if (directories == NULL) { |
universe@33 | 104 | fprintf(stderr, "Memory allocation failed.\n"); |
universe@33 | 105 | return 1; |
universe@33 | 106 | } |
universe@30 | 107 | char* includeSuffix = NULL; |
universe@30 | 108 | char* excludeSuffix = NULL; |
universe@8 | 109 | int checked = 0; |
universe@0 | 110 | |
universe@1 | 111 | for (int t = 1 ; t < argc ; t++) { |
universe@1 | 112 | |
universe@28 | 113 | int argflags = checkArgument(argv[t], "hsSrRmvVbeE"); |
universe@30 | 114 | int paropt = 0; |
universe@1 | 115 | |
universe@30 | 116 | /* s */ |
universe@30 | 117 | if ((argflags & 2) > 0) { |
universe@30 | 118 | if (!checkParamOpt(&paropt) || registerArgument(&checked, 2)) { |
universe@12 | 119 | return exit_with_help(settings, 1); |
universe@0 | 120 | } |
universe@1 | 121 | t++; |
universe@1 | 122 | if (t >= argc) { |
universe@12 | 123 | return exit_with_help(settings, 1); |
universe@1 | 124 | } |
universe@30 | 125 | includeSuffix = argv[t]; |
universe@30 | 126 | } |
universe@30 | 127 | /* S */ |
universe@30 | 128 | if ((argflags & 4) > 0) { |
universe@30 | 129 | if (!checkParamOpt(&paropt) || registerArgument(&checked, 4)) { |
universe@30 | 130 | return exit_with_help(settings, 1); |
universe@30 | 131 | } |
universe@30 | 132 | t++; |
universe@30 | 133 | if (t >= argc) { |
universe@30 | 134 | return exit_with_help(settings, 1); |
universe@30 | 135 | } |
universe@30 | 136 | excludeSuffix = argv[t]; |
universe@0 | 137 | } |
universe@22 | 138 | /* h */ |
universe@1 | 139 | if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) { |
universe@14 | 140 | return exit_with_help(settings, 0); |
universe@0 | 141 | } |
universe@22 | 142 | /* r, R */ |
universe@1 | 143 | if ((argflags & 24) > 0) { |
universe@8 | 144 | if (registerArgument(&checked, 24)) { |
universe@12 | 145 | return exit_with_help(settings, 1); |
universe@0 | 146 | } |
universe@3 | 147 | settings->recursive = true; |
universe@0 | 148 | } |
universe@22 | 149 | /* m */ |
universe@1 | 150 | if ((argflags & 32) > 0) { |
universe@8 | 151 | if (registerArgument(&checked, 32)) { |
universe@12 | 152 | return exit_with_help(settings, 1); |
universe@0 | 153 | } |
universe@3 | 154 | settings->matchesOnly = true; |
universe@0 | 155 | } |
universe@22 | 156 | /* v */ |
universe@14 | 157 | if ((argflags & 64) > 0 || strcmp(argv[t], "--version") == 0) { |
universe@14 | 158 | return exit_with_version(settings); |
universe@14 | 159 | } |
universe@22 | 160 | /* V */ |
universe@16 | 161 | if ((argflags & 128) > 0) { |
universe@16 | 162 | if (registerArgument(&checked, 128)) { |
universe@16 | 163 | return exit_with_help(settings, 1); |
universe@16 | 164 | } |
universe@16 | 165 | settings->verbose = false; |
universe@16 | 166 | } |
universe@22 | 167 | /* b */ |
universe@21 | 168 | if ((argflags & 256) > 0) { |
universe@30 | 169 | if (!checkParamOpt(&paropt) || registerArgument(&checked, 256)) { |
universe@21 | 170 | return exit_with_help(settings, 1); |
universe@21 | 171 | } |
universe@21 | 172 | t++; |
universe@21 | 173 | if (t >= argc) { |
universe@21 | 174 | return exit_with_help(settings, 1); |
universe@21 | 175 | } |
universe@24 | 176 | if (strcasecmp(argv[t], "ignore") == 0) { |
universe@21 | 177 | settings->bfileHeuristics->level = BFILE_IGNORE; |
universe@24 | 178 | } else if (strcasecmp(argv[t], "low") == 0) { |
universe@21 | 179 | settings->bfileHeuristics->level = BFILE_LOW_ACCURACY; |
universe@24 | 180 | } else if (strcasecmp(argv[t], "medium") == 0) { |
universe@21 | 181 | settings->bfileHeuristics->level = BFILE_MEDIUM_ACCURACY; |
universe@24 | 182 | } else if (strcasecmp(argv[t], "high") == 0) { |
universe@21 | 183 | settings->bfileHeuristics->level = BFILE_HIGH_ACCURACY; |
universe@21 | 184 | } else { |
universe@21 | 185 | return exit_with_help(settings, 1); |
universe@21 | 186 | } |
universe@21 | 187 | } |
universe@28 | 188 | /* e */ |
universe@27 | 189 | if ((argflags & 512) > 0) { |
universe@30 | 190 | if (!checkParamOpt(&paropt) || t + 2 >= argc) { |
universe@27 | 191 | return exit_with_help(settings, 1); |
universe@27 | 192 | } |
universe@27 | 193 | t++; add_string(settings->regex->pattern_list, argv[t]); |
universe@27 | 194 | t++; add_string(settings->regex->pattern_list, argv[t]); |
universe@27 | 195 | } |
universe@28 | 196 | /* E */ |
universe@28 | 197 | if ((argflags & 1024) > 0) { |
universe@28 | 198 | t++; |
universe@30 | 199 | if (!checkParamOpt(&paropt) || t >= argc) { |
universe@28 | 200 | return exit_with_help(settings, 1); |
universe@28 | 201 | } |
universe@28 | 202 | add_string(settings->regex->pattern_list, argv[t]); |
universe@28 | 203 | add_string(settings->regex->pattern_list, "$"); |
universe@28 | 204 | } |
universe@1 | 205 | if (argflags == 0) { |
universe@31 | 206 | /* SHORTCUTS */ |
universe@31 | 207 | /* exclude-cstyle-comments */ |
universe@31 | 208 | if (strcmp(argv[t], "--exclude-cstyle-comments") == 0) { |
universe@31 | 209 | add_string(settings->regex->pattern_list, "\\s*//"); |
universe@31 | 210 | add_string(settings->regex->pattern_list, "$"); |
universe@31 | 211 | add_string(settings->regex->pattern_list, "\\s*/\\*"); |
universe@31 | 212 | add_string(settings->regex->pattern_list, "\\*/\\s*"); |
universe@31 | 213 | } |
universe@31 | 214 | /* Path */ |
universe@33 | 215 | else { |
universe@33 | 216 | add_string(directories, argv[t]); |
universe@0 | 217 | } |
universe@0 | 218 | } |
universe@0 | 219 | } |
universe@0 | 220 | |
universe@22 | 221 | /* Find tokens */ |
universe@30 | 222 | parseCSL(includeSuffix, settings->includeSuffixes); |
universe@30 | 223 | parseCSL(excludeSuffix, settings->excludeSuffixes); |
universe@0 | 224 | |
universe@33 | 225 | /* Scan directories */ |
universe@28 | 226 | if (regex_compile_all(settings->regex)) { |
universe@44 | 227 | /* Don't waste memory when only the total sum is needed */ |
universe@44 | 228 | string_list_t *output = settings->verbose ? new_string_list_t() : NULL; |
universe@44 | 229 | char *outbuf; |
universe@44 | 230 | |
universe@44 | 231 | int lineSum = 0, lines; |
universe@33 | 232 | if (directories->count == 0) { |
universe@33 | 233 | add_string(directories, "."); |
universe@33 | 234 | } |
universe@33 | 235 | for (int t = 0 ; t < directories->count ; t++) { |
universe@44 | 236 | lines = scanDirectory((scanner_t){directories->items[t], 0}, settings, |
universe@44 | 237 | output); |
universe@44 | 238 | lineSum += lines; |
universe@44 | 239 | if (directories->count > 1 ) { |
universe@44 | 240 | outbuf = (char*) malloc(81); |
universe@44 | 241 | memset(outbuf, '-', 79); |
universe@44 | 242 | outbuf[79] = '\n'; |
universe@44 | 243 | outbuf[80] = 0; |
universe@44 | 244 | add_string(output, outbuf); |
universe@44 | 245 | outbuf = (char*) malloc(81); |
universe@44 | 246 | snprintf(outbuf, 81, "%-63s%10d lines\n", directories->items[t], lines); |
universe@44 | 247 | add_string(output, outbuf); |
universe@44 | 248 | outbuf = (char*) malloc(81); |
universe@44 | 249 | memset(outbuf, '-', 79); |
universe@44 | 250 | outbuf[79] = '\n'; |
universe@44 | 251 | outbuf[80] = 0; |
universe@44 | 252 | add_string(output, outbuf); |
universe@33 | 253 | } |
universe@33 | 254 | } |
universe@33 | 255 | destroy_string_list_t(directories); |
universe@0 | 256 | |
universe@44 | 257 | /* Print result */ |
universe@44 | 258 | if (settings->verbose) { |
universe@44 | 259 | for (int i = 0 ; i < output->count ; i++) { |
universe@44 | 260 | printf("%s", output->items[i]); |
universe@44 | 261 | free(output->items[i]); |
universe@44 | 262 | } |
universe@44 | 263 | |
universe@44 | 264 | for (int t = 0 ; t < 79 ; t++) { |
universe@44 | 265 | printf("="); |
universe@44 | 266 | } |
universe@44 | 267 | printf("\n%73d lines\n", lineSum); |
universe@44 | 268 | |
universe@44 | 269 | if (settings->confusing_lnlen && |
universe@44 | 270 | settings->regex->pattern_list->count > 0) { |
universe@44 | 271 | |
universe@44 | 272 | printf("\nSome files contain too long lines.\n" |
universe@44 | 273 | "The regex parser currently supports a maximum line length of %d." |
universe@44 | 274 | "\nThe result might be wrong.\n", REGEX_MAX_LINELENGTH); |
universe@44 | 275 | } |
universe@44 | 276 | } else { |
universe@44 | 277 | printf("%d", lineSum); |
universe@28 | 278 | } |
universe@44 | 279 | destroy_string_list_t(output); |
universe@33 | 280 | destroy_settings_t(settings); |
universe@16 | 281 | } |
universe@16 | 282 | |
universe@16 | 283 | fflush(stdout); |
universe@28 | 284 | fflush(stderr); |
universe@0 | 285 | return 0; |
universe@0 | 286 | } |