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