src/cline.c

changeset 103
31fa205db85a
parent 102
665b60727a89
equal deleted inserted replaced
102:665b60727a89 103:31fa205db85a
32 #include "settings.h" 32 #include "settings.h"
33 #include "scanner.h" 33 #include "scanner.h"
34 #include "arguments.h" 34 #include "arguments.h"
35 #include "regex_parser.h" 35 #include "regex_parser.h"
36 36
37 static void printHelpText() { 37 static void print_help() {
38 printf( 38 printf(
39 "\nUsage:" 39 "\nUsage:"
40 "\n cline [Options] [Directories...]" 40 "\n cline [Options] [Directories...]"
41 "\n\nCounts the line terminator characters (\\n) within all" 41 "\n\nCounts the line terminator characters (\\n) within all"
42 " files in the specified\ndirectories." 42 " files in the specified\ndirectories."
87 return 0; 87 return 0;
88 } 88 }
89 89
90 static int exit_with_help(settings *settings, int code) { 90 static int exit_with_help(settings *settings, int code) {
91 printf("cline - Version: " VERSION "\n"); 91 printf("cline - Version: " VERSION "\n");
92 printHelpText(); 92 print_help();
93 destroy_settings(settings); 93 destroy_settings(settings);
94 return code; 94 return code;
95 } 95 }
96 96
97 static void normalize_excluded_dirs(settings *settings) { 97 static void normalize_excluded_dirs(settings *settings) {
98 /* normalize all paths */ 98 /* normalize all paths */
99 for (size_t i = 0 ; i < settings->excludeDirs->count ; i++) { 99 for (size_t i = 0 ; i < settings->exclude_dirs->count ; i++) {
100 char *arg = strdup(settings->excludeDirs->items[i]); 100 char *arg = strdup(settings->exclude_dirs->items[i]);
101 if (strpbrk(arg, "/\\") == NULL) { 101 if (strpbrk(arg, "/\\") == NULL) {
102 /* do not normalize names */ 102 /* do not normalize names */
103 settings->excludeDirs->items[i] = arg; 103 settings->exclude_dirs->items[i] = arg;
104 } else { 104 } else {
105 size_t arglen = strlen(arg); 105 size_t arglen = strlen(arg);
106 /* fix file separators */ 106 /* fix file separators */
107 char fs = settings->fileSeparator; 107 char fs = FILE_SEPARATOR;
108 char badfs = settings->fileSeparator == '/' ? '\\' : '/'; 108 char badfs = FILE_SEPARATOR == '/' ? '\\' : '/';
109 for (size_t j = 0 ; j < arglen ; j++) { 109 for (size_t j = 0 ; j < arglen ; j++) {
110 if (arg[j] == badfs) { 110 if (arg[j] == badfs) {
111 arg[j] = fs; 111 arg[j] = fs;
112 } 112 }
113 } 113 }
114 /* make path absolute */ 114 /* make path absolute */
115 settings->excludeDirs->items[i] = make_path_absolute(arg); 115 settings->exclude_dirs->items[i] = make_path_absolute(arg);
116 free(arg); 116 free(arg);
117 } 117 }
118 } 118 }
119 119
120 /* tell the string list to free the items */ 120 /* tell the string list to free the items */
121 settings->excludeDirs->free_item = free; 121 settings->exclude_dirs->free_item = free;
122 } 122 }
123 123
124 static const char *sepline_double = "===============================================================================\n"; 124 static const char *sepline_double = "===============================================================================\n";
125 static const char *sepline_single = "-------------------------------------------------------------------------------\n"; 125 static const char *sepline_single = "-------------------------------------------------------------------------------\n";
126 126
143 const char *excludeSuffix = NULL; 143 const char *excludeSuffix = NULL;
144 int checked = 0; 144 int checked = 0;
145 145
146 for (int t = 1 ; t < argc ; t++) { 146 for (int t = 1 ; t < argc ; t++) {
147 147
148 int argflags = checkArgument(argv[t], "hsSrRmvVbeEicdD"); 148 int argflags = argument_check(argv[t], "hsSrRmvVbeEicdD");
149 int paropt = 0; 149 int paropt = 0; /* checks if no more than one opt with arg is combined */
150 150
151 /* h */ 151 /* h */
152 if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) { 152 if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) {
153 return exit_with_help(settings, 0); 153 return exit_with_help(settings, 0);
154 } 154 }
155 /* s */ 155 /* s */
156 if ((argflags & 2) > 0) { 156 if ((argflags & 2) > 0) {
157 if (!checkParamOpt(&paropt) || registerArgument(&checked, 2)) { 157 if (paropt++ || argument_register(&checked, 2)) {
158 return exit_with_help(settings, 1); 158 return exit_with_help(settings, 1);
159 } 159 }
160 t++; 160 t++;
161 if (t >= argc) { 161 if (t >= argc) {
162 return exit_with_help(settings, 1); 162 return exit_with_help(settings, 1);
163 } 163 }
164 includeSuffix = argv[t]; 164 includeSuffix = argv[t];
165 } 165 }
166 /* S */ 166 /* S */
167 if ((argflags & 4) > 0) { 167 if ((argflags & 4) > 0) {
168 if (!checkParamOpt(&paropt) || registerArgument(&checked, 4)) { 168 if (paropt++ || argument_register(&checked, 4)) {
169 return exit_with_help(settings, 1); 169 return exit_with_help(settings, 1);
170 } 170 }
171 t++; 171 t++;
172 if (t >= argc) { 172 if (t >= argc) {
173 return exit_with_help(settings, 1); 173 return exit_with_help(settings, 1);
174 } 174 }
175 excludeSuffix = argv[t]; 175 excludeSuffix = argv[t];
176 } 176 }
177 /* r, R */ 177 /* r, R */
178 if ((argflags & 24) > 0) { 178 if ((argflags & 24) > 0) {
179 if (registerArgument(&checked, 24)) { 179 if (argument_register(&checked, 24)) {
180 return exit_with_help(settings, 1); 180 return exit_with_help(settings, 1);
181 } 181 }
182 settings->recursive = true; 182 settings->recursive = true;
183 } 183 }
184 /* m */ 184 /* m */
185 if ((argflags & 32) > 0) { 185 if ((argflags & 32) > 0) {
186 if (registerArgument(&checked, 32)) { 186 if (argument_register(&checked, 32)) {
187 return exit_with_help(settings, 1); 187 return exit_with_help(settings, 1);
188 } 188 }
189 settings->matchesOnly = true; 189 settings->matches_only = true;
190 } 190 }
191 /* v */ 191 /* v */
192 if ((argflags & 64) > 0 || strcmp(argv[t], "--version") == 0) { 192 if ((argflags & 64) > 0 || strcmp(argv[t], "--version") == 0) {
193 return exit_with_version(settings); 193 return exit_with_version(settings);
194 } 194 }
195 /* V */ 195 /* V */
196 if ((argflags & 128) > 0) { 196 if ((argflags & 128) > 0) {
197 if (registerArgument(&checked, 128)) { 197 if (argument_register(&checked, 128)) {
198 return exit_with_help(settings, 1); 198 return exit_with_help(settings, 1);
199 } 199 }
200 settings->verbose = false; 200 settings->verbose = false;
201 } 201 }
202 /* b */ 202 /* b */
203 if ((argflags & 256) > 0) { 203 if ((argflags & 256) > 0) {
204 if (!checkParamOpt(&paropt) || registerArgument(&checked, 256)) { 204 if (paropt++ || argument_register(&checked, 256)) {
205 return exit_with_help(settings, 1); 205 return exit_with_help(settings, 1);
206 } 206 }
207 t++; 207 t++;
208 if (t >= argc) { 208 if (t >= argc) {
209 return exit_with_help(settings, 1); 209 return exit_with_help(settings, 1);
210 } 210 }
211 if (strcasecmp(argv[t], "ignore") == 0) { 211 if (strcasecmp(argv[t], "ignore") == 0) {
212 settings->bfileHeuristics->level = BFILE_IGNORE; 212 settings->bfile->level = BFILE_IGNORE;
213 } else if (strcasecmp(argv[t], "low") == 0) { 213 } else if (strcasecmp(argv[t], "low") == 0) {
214 settings->bfileHeuristics->level = BFILE_LOW_ACCURACY; 214 settings->bfile->level = BFILE_LOW_ACCURACY;
215 } else if (strcasecmp(argv[t], "medium") == 0) { 215 } else if (strcasecmp(argv[t], "medium") == 0) {
216 settings->bfileHeuristics->level = BFILE_MEDIUM_ACCURACY; 216 settings->bfile->level = BFILE_MEDIUM_ACCURACY;
217 } else if (strcasecmp(argv[t], "high") == 0) { 217 } else if (strcasecmp(argv[t], "high") == 0) {
218 settings->bfileHeuristics->level = BFILE_HIGH_ACCURACY; 218 settings->bfile->level = BFILE_HIGH_ACCURACY;
219 } else { 219 } else {
220 return exit_with_help(settings, 1); 220 return exit_with_help(settings, 1);
221 } 221 }
222 } 222 }
223 /* e */ 223 /* e */
224 if ((argflags & 512) > 0) { 224 if ((argflags & 512) > 0) {
225 if (!checkParamOpt(&paropt) || t + 2 >= argc) { 225 if (paropt++ || t + 2 >= argc) {
226 return exit_with_help(settings, 1); 226 return exit_with_help(settings, 1);
227 } 227 }
228 t++; add_string(settings->regex->pattern_list, argv[t]); 228 t++; add_string(settings->regex->pattern_list, argv[t]);
229 t++; add_string(settings->regex->pattern_list, argv[t]); 229 t++; add_string(settings->regex->pattern_list, argv[t]);
230 } 230 }
231 /* E */ 231 /* E */
232 if ((argflags & 1024) > 0) { 232 if ((argflags & 1024) > 0) {
233 t++; 233 t++;
234 if (!checkParamOpt(&paropt) || t >= argc) { 234 if (paropt++ || t >= argc) {
235 return exit_with_help(settings, 1); 235 return exit_with_help(settings, 1);
236 } 236 }
237 add_string(settings->regex->pattern_list, argv[t]); 237 add_string(settings->regex->pattern_list, argv[t]);
238 add_string(settings->regex->pattern_list, "$"); 238 add_string(settings->regex->pattern_list, "$");
239 } 239 }
245 } 245 }
246 settings->individual_sums = true; 246 settings->individual_sums = true;
247 } 247 }
248 /* c */ 248 /* c */
249 if ((argflags & 4096) > 0) { 249 if ((argflags & 4096) > 0) {
250 if (registerArgument(&checked, 4096)) { 250 if (argument_register(&checked, 4096)) {
251 return exit_with_help(settings, 1); 251 return exit_with_help(settings, 1);
252 } 252 }
253 settings->count_chars = true; 253 settings->count_chars = true;
254 settings->regex->count_chars = true; 254 settings->regex->count_chars = true;
255 } 255 }
256 /* d */ 256 /* d */
257 if ((argflags & 8192) > 0) { 257 if ((argflags & 8192) > 0) {
258 if (registerArgument(&checked, 8192)) { 258 if (argument_register(&checked, 8192)) {
259 return exit_with_help(settings, 1); 259 return exit_with_help(settings, 1);
260 } 260 }
261 /* ignored together with -V */ 261 /* ignored together with -V */
262 if ((checked & 128) == 0) { 262 if ((checked & 128) == 0) {
263 settings->dirsOnly = true; 263 settings->dirs_only = true;
264 } 264 }
265 } 265 }
266 /* D */ 266 /* D */
267 if ((argflags & 16384) > 0) { 267 if ((argflags & 16384) > 0) {
268 t++; 268 t++;
269 if (!checkParamOpt(&paropt) || t >= argc) { 269 if (paropt++ || t >= argc) {
270 return exit_with_help(settings, 1); 270 return exit_with_help(settings, 1);
271 } 271 }
272 add_string(settings->excludeDirs, argv[t]); 272 add_string(settings->exclude_dirs, argv[t]);
273 } 273 }
274 if (argflags == 0) { 274 if (argflags == 0) {
275 /* SHORTCUTS */ 275 /* SHORTCUTS */
276 if (strcmp(argv[t], "--exclude-cstyle-comments") == 0) { 276 if (strcmp(argv[t], "--exclude-cstyle-comments") == 0) {
277 add_string(settings->regex->pattern_list, "\\s*//"); 277 add_string(settings->regex->pattern_list, "\\s*//");
293 } 293 }
294 } 294 }
295 } 295 }
296 296
297 /* Find tokens */ 297 /* Find tokens */
298 parseCSL(includeSuffix, settings->includeSuffixes); 298 parse_csl(includeSuffix, settings->include_suffixes);
299 parseCSL(excludeSuffix, settings->excludeSuffixes); 299 parse_csl(excludeSuffix, settings->exclude_suffixes);
300 300
301 /* Compiler regular expressions, if any specified */ 301 /* Compiler regular expressions, if any specified */
302 if (!regex_compile_all(settings->regex)) { 302 if (!regex_compile_all(settings->regex)) {
303 destroy_string_list(directories); 303 destroy_string_list(directories);
304 destroy_settings(settings); 304 destroy_settings(settings);
321 string_list *output = NULL; 321 string_list *output = NULL;
322 if (settings->verbose) { 322 if (settings->verbose) {
323 output = new_string_list(); 323 output = new_string_list();
324 output->free_item = free; 324 output->free_item = free;
325 } 325 }
326 scanDirectory((scanner){directories->items[t], 0}, 326 scan_dir((scanner){directories->items[t], 0},
327 settings, output, result); 327 settings, output, result);
328 total += result->result; 328 total += result->result;
329 if (settings->verbose) { 329 if (settings->verbose) {
330 has_output |= output->count > 0; 330 has_output |= output->count > 0;
331 for (size_t i = 0 ; i < output->count ; i++) { 331 for (size_t i = 0 ; i < output->count ; i++) {

mercurial