src/cline.c

Fri, 03 Jun 2022 17:51:59 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 03 Jun 2022 17:51:59 +0200
changeset 64
21a2db0c9f63
parent 62
7f5f9f43d0c0
child 66
be2084398c37
permissions
-rw-r--r--

simplify .hgignore file

universe@10 1 /*
universe@34 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@57 3 * Copyright 2018 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@57 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
universe@10 25 */
universe@10 26
universe@3 27 #include "cline.h"
universe@10 28 #include "scanner.h"
universe@10 29 #include "settings.h"
universe@12 30 #include "arguments.h"
universe@27 31 #include "regex_parser.h"
universe@0 32
universe@12 33 void printHelpText() {
universe@34 34 printf(
universe@1 35 "\nUsage:"
universe@33 36 "\n cline [Options] [Directories...]"
universe@0 37 "\n\nCounts the line terminator characters (\\n) within all"
universe@33 38 " files in the specified\ndirectories."
universe@0 39 "\n\nOptions:"
universe@21 40 "\n -b <level> - binary file heuristics level (default medium)"
universe@21 41 "\n One of: ignore low medium high"
universe@28 42 "\n -E <pattern> - Excludes any line matching the <pattern>"
universe@27 43 "\n -e <start> <end> - Excludes lines between <start> and <end>"
universe@28 44 "\n You may use these options multiple times"
universe@0 45 "\n -h, --help - this help text"
universe@61 46 "\n -i - print out individual sums per file extension"
universe@61 47 "\n (cannot be used together with -V)"
universe@1 48 "\n -m - print information about matching files only"
universe@1 49 "\n -s <suffixes> - only count files with these suffixes (separated"
universe@0 50 "\n by commas)"
universe@1 51 "\n -S <suffixes> - count any file except those with these suffixes"
universe@0 52 "\n (separated by commas)"
universe@1 53 "\n -r, -R - includes subdirectories"
universe@14 54 "\n -v, --version - print out version information"
universe@16 55 "\n -V - turn verbose output off, print the result only"
universe@31 56 "\n\nShortcuts:"
universe@57 57 "\n --exclude-cstyle-comments : -E '\\s*//' -e '\\s*/\\*' '\\*/\\s*'"
universe@57 58 "\n --exclude-blank-lines : -E '^\\s*$'"
universe@0 59 "\n\n"
universe@1 60 "The default call without any options is:"
universe@28 61 "\n cline ./\n\n"
universe@7 62 "So each file in the working directory is counted. If you want to count C"
universe@7 63 "\nsource code in your working directory and its subdirectories, type:"
universe@27 64 "\n cline -rs .c\n"
universe@28 65 "\nIf you want to exclude comment lines, you may use the -e/-E option."
universe@27 66 "\nAfter a line matches the regex pattern <start> any following line is"
universe@28 67 "\nnot counted unless a line matches the <end> pattern. A line is still "
universe@28 68 "\ncounted when it does not start or end with the respective patterns."
universe@28 69 "\nPlease note, that cline does not remove whitespace characters as this"
universe@28 70 "\nmight not be reasonable in some cases."
universe@31 71 "\n\nExample (C without comments):"
universe@36 72 "\n cline -s .c,.h --exclude-cstyle-comments"
universe@36 73 "\n");
universe@1 74 }
universe@1 75
universe@14 76 int exit_with_version(settings_t* settings) {
universe@48 77 printf("cline - Version: " VERSION "\n");
universe@14 78 destroy_settings_t(settings);
universe@14 79 return 0;
universe@14 80 }
universe@14 81
universe@12 82 int exit_with_help(settings_t* settings, int code) {
universe@50 83 printf("cline - Version: " VERSION "\n");
universe@12 84 printHelpText();
universe@8 85 destroy_settings_t(settings);
universe@8 86 return code;
universe@8 87 }
universe@8 88
universe@1 89 int main(int argc, char** argv) {
universe@0 90
universe@22 91 /* Settings */
universe@3 92 settings_t *settings = new_settings_t();
universe@5 93 if (settings == NULL) {
universe@5 94 fprintf(stderr, "Memory allocation failed.\n");
universe@5 95 return 1;
universe@5 96 }
universe@3 97
universe@22 98 /* Get arguments */
universe@33 99 string_list_t *directories = new_string_list_t();
universe@33 100 if (directories == NULL) {
universe@33 101 fprintf(stderr, "Memory allocation failed.\n");
universe@33 102 return 1;
universe@33 103 }
universe@30 104 char* includeSuffix = NULL;
universe@30 105 char* excludeSuffix = NULL;
universe@8 106 int checked = 0;
universe@0 107
universe@1 108 for (int t = 1 ; t < argc ; t++) {
universe@1 109
universe@60 110 int argflags = checkArgument(argv[t], "hsSrRmvVbeEi");
universe@30 111 int paropt = 0;
universe@1 112
universe@59 113 /* h */
universe@59 114 if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) {
universe@59 115 return exit_with_help(settings, 0);
universe@59 116 }
universe@30 117 /* s */
universe@30 118 if ((argflags & 2) > 0) {
universe@30 119 if (!checkParamOpt(&paropt) || registerArgument(&checked, 2)) {
universe@12 120 return exit_with_help(settings, 1);
universe@0 121 }
universe@1 122 t++;
universe@1 123 if (t >= argc) {
universe@12 124 return exit_with_help(settings, 1);
universe@1 125 }
universe@30 126 includeSuffix = argv[t];
universe@30 127 }
universe@30 128 /* S */
universe@30 129 if ((argflags & 4) > 0) {
universe@30 130 if (!checkParamOpt(&paropt) || registerArgument(&checked, 4)) {
universe@30 131 return exit_with_help(settings, 1);
universe@30 132 }
universe@30 133 t++;
universe@30 134 if (t >= argc) {
universe@30 135 return exit_with_help(settings, 1);
universe@30 136 }
universe@30 137 excludeSuffix = argv[t];
universe@0 138 }
universe@22 139 /* r, R */
universe@1 140 if ((argflags & 24) > 0) {
universe@8 141 if (registerArgument(&checked, 24)) {
universe@12 142 return exit_with_help(settings, 1);
universe@0 143 }
universe@3 144 settings->recursive = true;
universe@0 145 }
universe@22 146 /* m */
universe@1 147 if ((argflags & 32) > 0) {
universe@8 148 if (registerArgument(&checked, 32)) {
universe@12 149 return exit_with_help(settings, 1);
universe@0 150 }
universe@3 151 settings->matchesOnly = true;
universe@0 152 }
universe@22 153 /* v */
universe@14 154 if ((argflags & 64) > 0 || strcmp(argv[t], "--version") == 0) {
universe@14 155 return exit_with_version(settings);
universe@14 156 }
universe@22 157 /* V */
universe@16 158 if ((argflags & 128) > 0) {
universe@16 159 if (registerArgument(&checked, 128)) {
universe@16 160 return exit_with_help(settings, 1);
universe@16 161 }
universe@16 162 settings->verbose = false;
universe@16 163 }
universe@22 164 /* b */
universe@21 165 if ((argflags & 256) > 0) {
universe@30 166 if (!checkParamOpt(&paropt) || registerArgument(&checked, 256)) {
universe@21 167 return exit_with_help(settings, 1);
universe@21 168 }
universe@21 169 t++;
universe@21 170 if (t >= argc) {
universe@21 171 return exit_with_help(settings, 1);
universe@21 172 }
universe@24 173 if (strcasecmp(argv[t], "ignore") == 0) {
universe@21 174 settings->bfileHeuristics->level = BFILE_IGNORE;
universe@24 175 } else if (strcasecmp(argv[t], "low") == 0) {
universe@21 176 settings->bfileHeuristics->level = BFILE_LOW_ACCURACY;
universe@24 177 } else if (strcasecmp(argv[t], "medium") == 0) {
universe@21 178 settings->bfileHeuristics->level = BFILE_MEDIUM_ACCURACY;
universe@24 179 } else if (strcasecmp(argv[t], "high") == 0) {
universe@21 180 settings->bfileHeuristics->level = BFILE_HIGH_ACCURACY;
universe@21 181 } else {
universe@21 182 return exit_with_help(settings, 1);
universe@21 183 }
universe@21 184 }
universe@28 185 /* e */
universe@27 186 if ((argflags & 512) > 0) {
universe@30 187 if (!checkParamOpt(&paropt) || t + 2 >= argc) {
universe@27 188 return exit_with_help(settings, 1);
universe@27 189 }
universe@27 190 t++; add_string(settings->regex->pattern_list, argv[t]);
universe@27 191 t++; add_string(settings->regex->pattern_list, argv[t]);
universe@27 192 }
universe@28 193 /* E */
universe@28 194 if ((argflags & 1024) > 0) {
universe@28 195 t++;
universe@30 196 if (!checkParamOpt(&paropt) || t >= argc) {
universe@28 197 return exit_with_help(settings, 1);
universe@28 198 }
universe@28 199 add_string(settings->regex->pattern_list, argv[t]);
universe@28 200 add_string(settings->regex->pattern_list, "$");
universe@28 201 }
universe@61 202 /* i */
universe@60 203 if ((argflags & 2048) > 0) {
universe@61 204 // cannot be used together with -V
universe@61 205 if (registerArgument(&checked, 128)) {
universe@61 206 return exit_with_help(settings, 1);
universe@61 207 }
universe@61 208 settings->individual_sums = true;
universe@60 209 }
universe@1 210 if (argflags == 0) {
universe@31 211 /* SHORTCUTS */
universe@31 212 if (strcmp(argv[t], "--exclude-cstyle-comments") == 0) {
universe@31 213 add_string(settings->regex->pattern_list, "\\s*//");
universe@31 214 add_string(settings->regex->pattern_list, "$");
universe@31 215 add_string(settings->regex->pattern_list, "\\s*/\\*");
universe@31 216 add_string(settings->regex->pattern_list, "\\*/\\s*");
universe@57 217 } else if (strcmp(argv[t], "--exclude-blank-lines") == 0) {
universe@57 218 add_string(settings->regex->pattern_list, "^\\s*$");
universe@57 219 add_string(settings->regex->pattern_list, "$");
universe@31 220 }
universe@31 221 /* Path */
universe@33 222 else {
universe@33 223 add_string(directories, argv[t]);
universe@0 224 }
universe@0 225 }
universe@0 226 }
universe@0 227
universe@22 228 /* Find tokens */
universe@30 229 parseCSL(includeSuffix, settings->includeSuffixes);
universe@30 230 parseCSL(excludeSuffix, settings->excludeSuffixes);
universe@0 231
universe@33 232 /* Scan directories */
universe@28 233 if (regex_compile_all(settings->regex)) {
universe@61 234 scanresult_t* result = new_scanresult_t(settings);
universe@44 235 /* Don't waste memory when only the total sum is needed */
universe@44 236 string_list_t *output = settings->verbose ? new_string_list_t() : NULL;
universe@44 237 char *outbuf;
universe@44 238
universe@60 239 int total = 0;
universe@33 240 if (directories->count == 0) {
universe@33 241 add_string(directories, ".");
universe@33 242 }
universe@33 243 for (int t = 0 ; t < directories->count ; t++) {
universe@60 244 scanDirectory((scanner_t){directories->items[t], 0}, settings,
universe@61 245 output, result);
universe@61 246 total += result->lines;
universe@44 247 if (directories->count > 1 ) {
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@44 253 outbuf = (char*) malloc(81);
universe@60 254 snprintf(outbuf, 81, "%-63s%10d lines\n", directories->items[t],
universe@61 255 result->lines);
universe@44 256 add_string(output, outbuf);
universe@44 257 outbuf = (char*) malloc(81);
universe@44 258 memset(outbuf, '-', 79);
universe@44 259 outbuf[79] = '\n';
universe@44 260 outbuf[80] = 0;
universe@44 261 add_string(output, outbuf);
universe@33 262 }
universe@33 263 }
universe@33 264 destroy_string_list_t(directories);
universe@0 265
universe@44 266 /* Print result */
universe@44 267 if (settings->verbose) {
universe@44 268 for (int i = 0 ; i < output->count ; i++) {
universe@44 269 printf("%s", output->items[i]);
universe@44 270 free(output->items[i]);
universe@44 271 }
universe@44 272
universe@61 273 if (result->ext) {
universe@61 274 if (result->ext->count > 0) {
universe@61 275 for (int t = 0 ; t < 79 ; t++) {
universe@61 276 printf("=");
universe@61 277 }
universe@61 278 printf("\nIndividual sums:\n");
universe@61 279 for (int t = 0 ; t < result->ext->count ; t++) {
universe@61 280 printf(" %-62s%10d lines\n",
universe@61 281 result->ext->extensions[t],
universe@61 282 result->ext->lines[t]);
universe@61 283 }
universe@61 284 }
universe@61 285 }
universe@61 286
universe@44 287 for (int t = 0 ; t < 79 ; t++) {
universe@44 288 printf("=");
universe@44 289 }
universe@60 290 printf("\n%73d lines\n", total);
universe@44 291
universe@44 292 if (settings->confusing_lnlen &&
universe@44 293 settings->regex->pattern_list->count > 0) {
universe@44 294
universe@44 295 printf("\nSome files contain too long lines.\n"
universe@44 296 "The regex parser currently supports a maximum line length of %d."
universe@44 297 "\nThe result might be wrong.\n", REGEX_MAX_LINELENGTH);
universe@44 298 }
universe@44 299 } else {
universe@60 300 printf("%d", total);
universe@28 301 }
universe@61 302 destroy_scanresult_t(result);
universe@44 303 destroy_string_list_t(output);
universe@33 304 destroy_settings_t(settings);
universe@16 305 }
universe@16 306
universe@16 307 fflush(stdout);
universe@28 308 fflush(stderr);
universe@0 309 return 0;
universe@0 310 }

mercurial