1 | /* |
---|
2 | o---------------------------------------------------------------------o |
---|
3 | | |
---|
4 | | Numdiff |
---|
5 | | |
---|
6 | | Copyright (c) 2012+ laurent.deniau@cern.ch |
---|
7 | | Gnu General Public License |
---|
8 | | |
---|
9 | o---------------------------------------------------------------------o |
---|
10 | |
---|
11 | Purpose: |
---|
12 | manage arguments and options |
---|
13 | display the help |
---|
14 | run unit tests (immediate) |
---|
15 | |
---|
16 | o---------------------------------------------------------------------o |
---|
17 | */ |
---|
18 | |
---|
19 | #include <stdlib.h> |
---|
20 | #include <string.h> |
---|
21 | |
---|
22 | #include "args.h" |
---|
23 | #include "utest.h" |
---|
24 | #include "error.h" |
---|
25 | #include "ndiff.h" |
---|
26 | #include "context.h" |
---|
27 | |
---|
28 | #ifndef PUNCTCHRS |
---|
29 | #define PUNCTCHRS "._$" |
---|
30 | #endif |
---|
31 | |
---|
32 | #ifndef SERIEFMT |
---|
33 | #define SERIEFMT "%d" |
---|
34 | #endif |
---|
35 | |
---|
36 | #ifndef MAXKEEP |
---|
37 | #define MAXKEEP 25 |
---|
38 | #endif |
---|
39 | |
---|
40 | #ifndef OUTFILEEXT |
---|
41 | #define OUTFILEEXT ".out" |
---|
42 | #endif |
---|
43 | |
---|
44 | #ifndef REFFILEEXT |
---|
45 | #define REFFILEEXT ".ref" |
---|
46 | #endif |
---|
47 | |
---|
48 | #ifndef CFGFILEEXT |
---|
49 | #define CFGFILEEXT ".cfg" |
---|
50 | #endif |
---|
51 | |
---|
52 | struct option option = { |
---|
53 | // names and series numbering |
---|
54 | .fmt = SERIEFMT, |
---|
55 | |
---|
56 | // punctuation part of identifiers |
---|
57 | .chr = PUNCTCHRS, |
---|
58 | |
---|
59 | // number of diff displayed |
---|
60 | .keep = MAXKEEP, |
---|
61 | |
---|
62 | // file extensions |
---|
63 | .out_e = OUTFILEEXT, .ref_e = REFFILEEXT, .cfg_e = CFGFILEEXT |
---|
64 | }; |
---|
65 | |
---|
66 | static void |
---|
67 | run_utest(void) |
---|
68 | { |
---|
69 | struct utest *ut = utest_alloc(0); |
---|
70 | |
---|
71 | inform("Running unit tests (incomplete)"); |
---|
72 | |
---|
73 | // list of unit tests: TODO: more utests |
---|
74 | context_utest(ut); |
---|
75 | ndiff_utest(ut); |
---|
76 | |
---|
77 | // stat |
---|
78 | utest_stat(ut); |
---|
79 | utest_free(ut); |
---|
80 | } |
---|
81 | |
---|
82 | void |
---|
83 | invalid_option(const char *str) |
---|
84 | { |
---|
85 | warning("invalid program options or arguments '%s'", str); |
---|
86 | usage(); |
---|
87 | } |
---|
88 | |
---|
89 | void |
---|
90 | invalid_file(const char *str) |
---|
91 | { |
---|
92 | warning("invalid filename argument '%s'", str); |
---|
93 | usage(); |
---|
94 | } |
---|
95 | |
---|
96 | void |
---|
97 | usage(void) |
---|
98 | { |
---|
99 | logmsg_config.level = inform_level; |
---|
100 | |
---|
101 | inform("usage:"); |
---|
102 | inform("\tnumdiff [options] lhs_file rhs_file [cfg_file]"); |
---|
103 | inform("\tnumdiff [options] -list file1 file2 ..."); |
---|
104 | inform("options:"); |
---|
105 | inform("\t-a -accum file accumulate tests information in file"); |
---|
106 | inform("\t-b -blank ignore blank spaces (space and tabs)"); |
---|
107 | inform("\t-c -check enable check mode"); |
---|
108 | inform("\t-d -debug enable debug mode (include check mode)"); |
---|
109 | inform("\t-e -largerr allow abs and rel error specifier >= 1.0"); |
---|
110 | inform("\t-f -format fmt specify the (printf) format fmt for indexes, default is \"%s\"", option.fmt); |
---|
111 | inform("\t-g -cfgext ext specify the config file extension, default is \"%s\"", option.cfg_e); |
---|
112 | inform("\t-h -help display this help"); |
---|
113 | inform("\t-k -keep num specify the number of diffs to display per file, default is %d", option.keep); |
---|
114 | inform("\t-l -list enable list mode (list of filenames)"); |
---|
115 | inform("\t-n -serie enable series mode (indexed filenames)"); |
---|
116 | inform("\t-o -outext ext specify the output file extension, default is \"%s\"", option.out_e); |
---|
117 | inform("\t-p -punct chrs punctuation characters part of identifiers, default is \"%s\"", option.chr); |
---|
118 | inform("\t-q -quiet enable quiet mode (no output if no diff)"); |
---|
119 | inform("\t-r -refext ext specify the reference file extension, default is \"%s\"", option.ref_e); |
---|
120 | inform("\t-s -suite name set testsuite name for output message (title)"); |
---|
121 | inform("\t-t -test name set test name for output message (item)"); |
---|
122 | inform("\t-u -utest run the test suite (still incomplete)"); |
---|
123 | |
---|
124 | exit(EXIT_FAILURE); |
---|
125 | } |
---|
126 | |
---|
127 | void |
---|
128 | parse_args(int argc, const char *argv[]) |
---|
129 | { |
---|
130 | logmsg_config.level = inform_level; |
---|
131 | |
---|
132 | // parse command line arguments |
---|
133 | for (option.argi = 1; option.argi < argc; option.argi++) { |
---|
134 | |
---|
135 | // display help [immediate] |
---|
136 | if (!strcmp(argv[option.argi], "-h") || !strcmp(argv[option.argi], "-help")) { |
---|
137 | usage(); |
---|
138 | continue; |
---|
139 | } |
---|
140 | |
---|
141 | // run utests [immediate] |
---|
142 | if (!strcmp(argv[option.argi], "-u") || !strcmp(argv[option.argi], "-utest")) { |
---|
143 | run_utest(); |
---|
144 | option.utest += 1; |
---|
145 | continue; |
---|
146 | } |
---|
147 | |
---|
148 | // set trace mode [setup] |
---|
149 | if (!strcmp(argv[option.argi], "-trace")) { |
---|
150 | logmsg_config.level = trace_level; |
---|
151 | logmsg_config.locate = 1; |
---|
152 | debug("trace mode on"); |
---|
153 | option.debug = 1; |
---|
154 | continue; |
---|
155 | } |
---|
156 | |
---|
157 | // set debug mode [setup] |
---|
158 | if (!strcmp(argv[option.argi], "-d") || !strcmp(argv[option.argi], "-debug")) { |
---|
159 | logmsg_config.level = debug_level; |
---|
160 | logmsg_config.locate = 1; |
---|
161 | debug("debug mode on"); |
---|
162 | option.debug = 1; |
---|
163 | option.check = 1; |
---|
164 | continue; |
---|
165 | } |
---|
166 | |
---|
167 | // set check mode [setup] |
---|
168 | if (!strcmp(argv[option.argi], "-c") || !strcmp(argv[option.argi], "-check")) { |
---|
169 | debug("check mode on"); |
---|
170 | option.check = 1; |
---|
171 | continue; |
---|
172 | } |
---|
173 | |
---|
174 | // set info mode [setup] |
---|
175 | if (!strcmp(argv[option.argi], "-i") || !strcmp(argv[option.argi], "-info")) { |
---|
176 | debug("info mode on"); |
---|
177 | logmsg_config.level = inform_level; |
---|
178 | logmsg_config.locate = 0; |
---|
179 | continue; |
---|
180 | } |
---|
181 | |
---|
182 | // set blank mode [setup] |
---|
183 | if (!strcmp(argv[option.argi], "-b") || !strcmp(argv[option.argi], "-blank")) { |
---|
184 | debug("blank spaces ignored"); |
---|
185 | option.blank = 1; |
---|
186 | continue; |
---|
187 | } |
---|
188 | |
---|
189 | // allow large errors [setup] |
---|
190 | if (!strcmp(argv[option.argi], "-e") || !strcmp(argv[option.argi], "-largerr")) { |
---|
191 | debug("large errors allowed"); |
---|
192 | option.largerr = 1; |
---|
193 | continue; |
---|
194 | } |
---|
195 | |
---|
196 | // set quiet mode [setup] |
---|
197 | if (!strcmp(argv[option.argi], "-q") || !strcmp(argv[option.argi], "-quiet")) { |
---|
198 | debug("quiet mode on"); |
---|
199 | logmsg_config.level = warning_level; |
---|
200 | logmsg_config.locate = 0; |
---|
201 | continue; |
---|
202 | } |
---|
203 | |
---|
204 | // set serie mode [setup] |
---|
205 | if (!strcmp(argv[option.argi], "-n") || !strcmp(argv[option.argi], "-serie")) { |
---|
206 | debug("serie mode on"); |
---|
207 | option.serie = 1; |
---|
208 | continue; |
---|
209 | } |
---|
210 | |
---|
211 | // set list mode [setup] |
---|
212 | if (!strcmp(argv[option.argi], "-l") || !strcmp(argv[option.argi], "-list")) { |
---|
213 | debug("list mode on"); |
---|
214 | option.list = 1; |
---|
215 | continue; |
---|
216 | } |
---|
217 | |
---|
218 | // set suite name [setup] |
---|
219 | if (!strcmp(argv[option.argi], "-s") || !strcmp(argv[option.argi], "-suite")) { |
---|
220 | option.suite = argv[++option.argi]; |
---|
221 | debug("suite name set to '%s'", option.suite); |
---|
222 | fprintf(stdout, " [ %s ]\n", option.suite); |
---|
223 | continue; |
---|
224 | } |
---|
225 | |
---|
226 | // set test name [setup] |
---|
227 | if (!strcmp(argv[option.argi], "-t") || !strcmp(argv[option.argi], "-test")) { |
---|
228 | option.test = argv[++option.argi]; |
---|
229 | debug("test name set to '%s'", option.test); |
---|
230 | continue; |
---|
231 | } |
---|
232 | |
---|
233 | // set keep number [setup] |
---|
234 | if (!strcmp(argv[option.argi], "-k") || !strcmp(argv[option.argi], "-keep")) { |
---|
235 | option.keep = strtoul(argv[++option.argi],0,0); |
---|
236 | debug("keep set to %d", option.keep); |
---|
237 | continue; |
---|
238 | } |
---|
239 | |
---|
240 | // set serie format [setup] |
---|
241 | if (!strcmp(argv[option.argi], "-f") || !strcmp(argv[option.argi], "-format")) { |
---|
242 | if (option.serie) { |
---|
243 | option.fmt = argv[++option.argi]; |
---|
244 | debug("format set to '%s'", option.fmt); |
---|
245 | } else { |
---|
246 | option.argi++; |
---|
247 | inform("serie mode is off, format ignored"); |
---|
248 | } |
---|
249 | continue; |
---|
250 | } |
---|
251 | |
---|
252 | // set punctuation characters [setup] |
---|
253 | if (!strcmp(argv[option.argi], "-p") || !strcmp(argv[option.argi], "-punct")) { |
---|
254 | option.chr = argv[++option.argi]; |
---|
255 | debug("punctuation characters set to '%s'", option.chr); |
---|
256 | continue; |
---|
257 | } |
---|
258 | |
---|
259 | // set output extension [setup] |
---|
260 | if (!strcmp(argv[option.argi], "-o") || !strcmp(argv[option.argi], "-outext")) { |
---|
261 | option.out_e = argv[++option.argi]; |
---|
262 | debug("output extension set to '%s'", option.out_e); |
---|
263 | continue; |
---|
264 | } |
---|
265 | |
---|
266 | // set reference extension [setup] |
---|
267 | if (!strcmp(argv[option.argi], "-r") || !strcmp(argv[option.argi], "-refext")) { |
---|
268 | option.ref_e = argv[++option.argi]; |
---|
269 | debug("reference extension set to '%s'", option.ref_e); |
---|
270 | continue; |
---|
271 | } |
---|
272 | |
---|
273 | // set config extension [setup] |
---|
274 | if (!strcmp(argv[option.argi], "-g") || !strcmp(argv[option.argi], "-cfgext")) { |
---|
275 | option.cfg_e = argv[++option.argi]; |
---|
276 | debug("config extension set to '%s'", option.cfg_e); |
---|
277 | continue; |
---|
278 | } |
---|
279 | |
---|
280 | // set accumulation filename [setup] |
---|
281 | if (!strcmp(argv[option.argi], "-a") || !strcmp(argv[option.argi], "-accum")) { |
---|
282 | option.acc = argv[++option.argi]; |
---|
283 | debug("accumulation filename set to '%s'", option.acc); |
---|
284 | continue; |
---|
285 | } |
---|
286 | |
---|
287 | // unknown option |
---|
288 | if (argv[option.argi][0] == '-') |
---|
289 | invalid_option(argv[option.argi]); |
---|
290 | |
---|
291 | break; |
---|
292 | } |
---|
293 | } |
---|
294 | |
---|