Sat, 15 Oct 2011 14:52:12 +0200
Increased warning level, fixed resulting warnings, changed std form c99 to gnu99
10 | 1 | /* |
2 | * cline.c | |
3 | * | |
4 | * Created on: 23.05.2011 | |
5 | * Author: beckermi | |
6 | */ | |
6
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
7 | |
10 | 8 | #include "cline.h" |
9 | #include "scanner.h" | |
10 | #include "settings.h" | |
12
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
11 | #include "arguments.h" |
16
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
12 | #include "stream.h" |
0 | 13 | |
12
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
14 | void printHelpText() { |
0 | 15 | const char* helpText = |
1 | 16 | "\nUsage:" |
12
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
17 | "\n cline [-hrm][-s suffix][<directory>]" |
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
18 | "\n cline [-hrm][-S suffix][<directory>]" |
0 | 19 | "\n\nCounts the line terminator characters (\\n) within all" |
20 | " files in the specified\ndirectory." | |
21 | "\n\nOptions:" | |
22 | "\n -h, --help - this help text" | |
1 | 23 | "\n -m - print information about matching files only" |
24 | "\n -s <suffixes> - only count files with these suffixes (separated" | |
0 | 25 | "\n by commas)" |
1 | 26 | "\n -S <suffixes> - count any file except those with these suffixes" |
0 | 27 | "\n (separated by commas)" |
1 | 28 | "\n -r, -R - includes subdirectories" |
14
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
29 | "\n -v, --version - print out version information" |
16
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
30 | "\n -V - turn verbose output off, print the result only" |
0 | 31 | "\n\n" |
1 | 32 | "The default call without any options is:" |
12
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
33 | "\n cline ./\n" |
7
1b55f3fa52c9
Fixed helpText and removed extra variables for default values
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
34 | "So each file in the working directory is counted. If you want to count C" |
1b55f3fa52c9
Fixed helpText and removed extra variables for default values
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
35 | "\nsource code in your working directory and its subdirectories, type:" |
12
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
36 | "\n cline -rs .c\n"; |
1 | 37 | |
12
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
38 | printf(helpText); |
1 | 39 | } |
40 | ||
14
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
41 | int exit_with_version(settings_t* settings) { |
16
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
42 | printf("cline - Revision: %s", VERSION); |
14
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
43 | destroy_settings_t(settings); |
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
44 | return 0; |
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
45 | } |
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
46 | |
12
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
47 | int exit_with_help(settings_t* settings, int code) { |
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
48 | printHelpText(); |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
49 | destroy_settings_t(settings); |
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
50 | return code; |
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
51 | } |
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
52 | |
1 | 53 | int main(int argc, char** argv) { |
0 | 54 | |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
55 | // Settings |
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
56 | settings_t *settings = new_settings_t(); |
5
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
57 | if (settings == NULL) { |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
58 | fprintf(stderr, "Memory allocation failed.\n"); |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
59 | return 1; |
9393eff3d2f9
Fixed memory leak when exiting the programm ahead of time
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
60 | } |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
61 | |
0 | 62 | // Get arguments |
7
1b55f3fa52c9
Fixed helpText and removed extra variables for default values
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
63 | char* directory = "./"; |
1b55f3fa52c9
Fixed helpText and removed extra variables for default values
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
64 | char* suffix = " "; |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
65 | int checked = 0; |
0 | 66 | |
1 | 67 | for (int t = 1 ; t < argc ; t++) { |
68 | ||
16
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
69 | int argflags = checkArgument(argv[t], "hsSrRmvV"); |
1 | 70 | |
14
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
71 | // s, S |
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
72 | if ((argflags & 6) > 0) { |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
73 | if (registerArgument(&checked, 6)) { |
12
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
74 | return exit_with_help(settings, 1); |
0 | 75 | } |
14
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
76 | settings->includeSuffixes = (argflags & 2) > 0; |
1 | 77 | t++; |
78 | if (t >= argc) { | |
12
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
79 | return exit_with_help(settings, 1); |
1 | 80 | } |
81 | suffix = argv[t]; | |
0 | 82 | } |
1 | 83 | // h |
84 | if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) { | |
14
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
85 | return exit_with_help(settings, 0); |
0 | 86 | } |
1 | 87 | // r, R |
88 | if ((argflags & 24) > 0) { | |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
89 | if (registerArgument(&checked, 24)) { |
12
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
90 | return exit_with_help(settings, 1); |
0 | 91 | } |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
92 | settings->recursive = true; |
0 | 93 | } |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
94 | // m |
1 | 95 | if ((argflags & 32) > 0) { |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
96 | if (registerArgument(&checked, 32)) { |
12
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
97 | return exit_with_help(settings, 1); |
0 | 98 | } |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
99 | settings->matchesOnly = true; |
0 | 100 | } |
14
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
101 | // v |
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
102 | if ((argflags & 64) > 0 || strcmp(argv[t], "--version") == 0) { |
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
103 | return exit_with_version(settings); |
ee9333c91dda
some minor fixes + makefile now injects revisionnumber into cline.h
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
104 | } |
16
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
105 | // V |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
106 | if ((argflags & 128) > 0) { |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
107 | if (registerArgument(&checked, 128)) { |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
108 | return exit_with_help(settings, 1); |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
109 | } |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
110 | settings->verbose = false; |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
111 | } |
6
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
112 | // Path |
1 | 113 | if (argflags == 0) { |
8
28319b20968c
encapsulated common operations
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
114 | if (registerArgument(&checked, 1024)) { |
12
902cb8d2053c
removed dynamic programm name
Mike Becker <universe@uap-core.de>
parents:
11
diff
changeset
|
115 | return exit_with_help(settings, 1); |
0 | 116 | } |
117 | directory = argv[t]; | |
118 | } | |
119 | } | |
120 | ||
16
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
121 | // Configure output |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
122 | if (!settings->verbose) { |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
123 | close_stdout(); |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
124 | } |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
125 | |
0 | 126 | // Find tokens |
6
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
127 | char* finder = strtok(suffix, ","); |
1 | 128 | while (finder != NULL) { |
6
be923400164c
encapsulated suffix list in type suffix_list_t
Mike Becker <universe@uap-core.de>
parents:
5
diff
changeset
|
129 | add_suffix(settings->suffixList, finder); |
0 | 130 | finder = strtok(NULL, ","); |
131 | } | |
132 | ||
133 | // Open directory | |
134 | DIR *dir = opendir(directory); | |
1 | 135 | if (dir == NULL) { |
0 | 136 | perror("Operation failed"); |
4
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
137 | destroy_settings_t(settings); |
0 | 138 | return 1; |
139 | } | |
140 | ||
141 | // Scan directory | |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
142 | int lines = scanDirectory(dir, 0, directory, settings); |
4
c3acfb3b4957
Fixed missing return statement in new_settings_t.
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
143 | closedir(dir); |
7
1b55f3fa52c9
Fixed helpText and removed extra variables for default values
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
144 | destroy_settings_t(settings); |
0 | 145 | |
146 | // Print double line and line count | |
16
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
147 | #ifdef _WIN32 |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
148 | const int columns = 79; |
16
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
149 | #else |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
150 | const int columns = 80; |
16
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
151 | #endif /* _WIN32 */ |
1 | 152 | |
153 | for (int t = 0 ; t < columns ; t++) { | |
0 | 154 | printf("="); |
155 | } | |
16
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
156 | #ifdef _WIN32 |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
157 | printf("\n%73d lines\n", lines); |
16
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
158 | #else |
3
510d6b198dde
Moved some functions to functions.c
Mike Becker <universe@uap-core.de>
parents:
1
diff
changeset
|
159 | printf("\n%74d lines\n", lines); |
16
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
160 | #endif /* _WIN32 */ |
0 | 161 | |
16
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
162 | if (!settings->verbose) { |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
163 | reopen_stdout(); |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
164 | printf("%d", lines); |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
165 | } |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
166 | |
bc9a0fefd892
fixed makefile to run safely on compile errors + added -V option to cline
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
167 | fflush(stdout); |
0 | 168 | return 0; |
169 | } |