8 #include "cline.h" |
8 #include "cline.h" |
9 #include "scanner.h" |
9 #include "scanner.h" |
10 #include "settings.h" |
10 #include "settings.h" |
11 #include "arguments.h" |
11 #include "arguments.h" |
12 #include "stream.h" |
12 #include "stream.h" |
|
13 #include "regex_parser.h" |
13 |
14 |
14 void printHelpText() { |
15 void printHelpText() { |
15 const char* helpText = |
16 const char* helpText = |
16 "\nUsage:" |
17 "\nUsage:" |
17 "\n cline [-hrmvV][-s suffix][-b level][<directory>]" |
18 "\n cline [Options] [Directory]" |
18 "\n cline [-hrmvV][-S suffix][-b level][<directory>]" |
19 "\n cline [Options] [Directory]" |
19 "\n\nCounts the line terminator characters (\\n) within all" |
20 "\n\nCounts the line terminator characters (\\n) within all" |
20 " files in the specified\ndirectory." |
21 " files in the specified\ndirectory." |
21 "\n\nOptions:" |
22 "\n\nOptions:" |
22 "\n -b <level> - binary file heuristics level (default medium)" |
23 "\n -b <level> - binary file heuristics level (default medium)" |
23 "\n One of: ignore low medium high" |
24 "\n One of: ignore low medium high" |
|
25 "\n -e <start> <end> - Excludes lines between <start> and <end>" |
|
26 "\n You may use this option multiple times" |
24 "\n -h, --help - this help text" |
27 "\n -h, --help - this help text" |
25 "\n -m - print information about matching files only" |
28 "\n -m - print information about matching files only" |
26 "\n -s <suffixes> - only count files with these suffixes (separated" |
29 "\n -s <suffixes> - only count files with these suffixes (separated" |
27 "\n by commas)" |
30 "\n by commas)" |
28 "\n -S <suffixes> - count any file except those with these suffixes" |
31 "\n -S <suffixes> - count any file except those with these suffixes" |
33 "\n\n" |
36 "\n\n" |
34 "The default call without any options is:" |
37 "The default call without any options is:" |
35 "\n cline ./\n" |
38 "\n cline ./\n" |
36 "So each file in the working directory is counted. If you want to count C" |
39 "So each file in the working directory is counted. If you want to count C" |
37 "\nsource code in your working directory and its subdirectories, type:" |
40 "\nsource code in your working directory and its subdirectories, type:" |
38 "\n cline -rs .c\n"; |
41 "\n cline -rs .c\n" |
|
42 "\nIf you want to exclude comment lines, you may use the -e option." |
|
43 "\nAfter a line matches the regex pattern <start> any following line is" |
|
44 "\nnot counted unless a line matches the <end> pattern."; |
39 |
45 |
40 printf(helpText); |
46 printf(helpText); |
41 } |
47 } |
42 |
48 |
43 int exit_with_version(settings_t* settings) { |
49 int exit_with_version(settings_t* settings) { |
66 char* suffix = " "; |
72 char* suffix = " "; |
67 int checked = 0; |
73 int checked = 0; |
68 |
74 |
69 for (int t = 1 ; t < argc ; t++) { |
75 for (int t = 1 ; t < argc ; t++) { |
70 |
76 |
71 int argflags = checkArgument(argv[t], "hsSrRmvVb"); |
77 int argflags = checkArgument(argv[t], "hsSrRmvVbe"); |
72 |
78 |
73 /* s, S */ |
79 /* s, S */ |
74 if ((argflags & 6) > 0) { |
80 if ((argflags & 6) > 0) { |
75 if (registerArgument(&checked, 6)) { |
81 if (registerArgument(&checked, 6)) { |
76 return exit_with_help(settings, 1); |
82 return exit_with_help(settings, 1); |
130 settings->bfileHeuristics->level = BFILE_HIGH_ACCURACY; |
136 settings->bfileHeuristics->level = BFILE_HIGH_ACCURACY; |
131 } else { |
137 } else { |
132 return exit_with_help(settings, 1); |
138 return exit_with_help(settings, 1); |
133 } |
139 } |
134 } |
140 } |
|
141 if ((argflags & 512) > 0) { |
|
142 if (t + 2 >= argc) { |
|
143 return exit_with_help(settings, 1); |
|
144 } |
|
145 t++; add_string(settings->regex->pattern_list, argv[t]); |
|
146 t++; add_string(settings->regex->pattern_list, argv[t]); |
|
147 } |
135 /* Path */ |
148 /* Path */ |
136 if (argflags == 0) { |
149 if (argflags == 0) { |
137 if (registerArgument(&checked, 1024)) { |
150 if (registerArgument(&checked, 1024)) { |
138 return exit_with_help(settings, 1); |
151 return exit_with_help(settings, 1); |
139 } |
152 } |
152 add_string(settings->suffixList, finder); |
165 add_string(settings->suffixList, finder); |
153 finder = strtok(NULL, ","); |
166 finder = strtok(NULL, ","); |
154 } |
167 } |
155 |
168 |
156 /* Scan directory */ |
169 /* Scan directory */ |
|
170 regex_compile_all(settings->regex); |
157 int lines = scanDirectory((scanner_t){directory, 0}, settings); |
171 int lines = scanDirectory((scanner_t){directory, 0}, settings); |
158 destroy_settings_t(settings); |
172 destroy_settings_t(settings); |
159 |
173 |
160 /* Print double line and line count */ |
174 /* Print double line and line count */ |
161 for (int t = 0 ; t < 79 ; t++) { |
175 for (int t = 0 ; t < 79 ; t++) { |
162 printf("="); |
176 printf("="); |
163 } |
177 } |
164 printf("\n%73d lines\n", lines); |
178 printf("\n%73d lines\n", lines); |
165 |
179 |
166 if (settings->confusing_lnlen) { |
180 if (settings->confusing_lnlen && settings->regex->pattern_list->count > 0) { |
167 /* TODO: display this only when the regexp parser is used */ |
|
168 printf("\nSome files contain too long lines.\n" |
181 printf("\nSome files contain too long lines.\n" |
169 "The regexp parser currently supports a maximum line length of 2048." |
182 "The regex parser currently supports a maximum line length of %d." |
170 "\nThe result might be wrong.\n"); |
183 "\nThe result might be wrong.\n", REGEX_MAX_LINELENGTH); |
171 } |
184 } |
172 |
185 |
173 if (!settings->verbose) { |
186 if (!settings->verbose) { |
174 reopen_stdout(); |
187 reopen_stdout(); |
175 printf("%d", lines); |
188 printf("%d", lines); |