Commit | Line | Data |
---|---|---|
d44e3c4f | 1 | /****************************************************************************** |
2 | * Copyright (c) 2000-2016 Ericsson Telecom AB | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * Balasko, Jeno | |
10 | * Beres, Szabolcs | |
11 | * Godar, Marton | |
12 | * Kovacs, Ferenc | |
13 | * Lovassy, Arpad | |
14 | * Raduly, Csaba | |
15 | * Szabados, Kristof | |
16 | * Szabo, Bence Janos | |
17 | * | |
18 | ******************************************************************************/ | |
970ed795 EL |
19 | #include "XMLParser.hh" |
20 | #include "TTCN3Module.hh" | |
21 | #include "TTCN3ModuleInventory.hh" | |
22 | #include "SimpleType.hh" | |
23 | #include "ComplexType.hh" | |
24 | ||
25 | #include "../common/version_internal.h" | |
26 | #include "../common/license.h" | |
27 | ||
28 | #include <unistd.h> // for using "getopt" function | |
29 | #include <sys/stat.h> | |
30 | ||
31 | bool c_flag_used = false; | |
32 | int d_flag_used = 0; | |
33 | bool e_flag_used = false; | |
34 | bool f_flag_used = false; | |
3abe9331 | 35 | bool g_flag_used = true; |
51fa56b9 | 36 | bool h_flag_used = false; |
970ed795 EL |
37 | bool p_flag_used = false; |
38 | bool s_flag_used = false; | |
39 | bool t_flag_used = false; | |
40 | bool q_flag_used = false; | |
41 | bool w_flag_used = false; | |
42 | bool x_flag_used = false; | |
43 | bool z_flag_used = false; | |
44 | ||
45 | static void printProductinfo(); | |
46 | static void printUsage(const char * argv0); | |
47 | static void printVersion(); | |
3abe9331 | 48 | static void printErrorStatistics(const unsigned int errors, const unsigned int warnings); |
970ed795 EL |
49 | static bool generatePredefinedModules(); |
50 | static char **readModulesFromFile(const char *from_file, int *last_module); | |
3abe9331 | 51 | static int checkSyntax(const bool not_verbose, const int first_module, const int last_module, |
52 | const char * const * const module_names); | |
53 | static int validate(int const first_module, int const last_module, | |
54 | const char * const * const module_names); | |
55 | static int generateCode(const bool quiet, const bool need_predefined, | |
56 | const int first_module, const int last_module, | |
57 | const char * const * const module_names); | |
58 | ||
59 | int main(int argc, char **argv) { | |
970ed795 EL |
60 | if (argc == 1) { |
61 | printProductinfo(); | |
62 | printUsage(argv[0]); | |
63 | return EXIT_SUCCESS; | |
64 | } | |
65 | ||
66 | // The file holding a list of the XSD files. | |
67 | const char *from_file = NULL; | |
68 | char c; | |
69 | opterr = 0; | |
70 | ||
51fa56b9 | 71 | while ((c = getopt(argc, argv, "cdef:ghpqstvwxz")) != -1) { |
3abe9331 | 72 | switch (c) { |
73 | case 'c': | |
74 | c_flag_used = true; | |
75 | break; | |
76 | case 'd': | |
77 | ++d_flag_used; | |
78 | break; | |
79 | case 'e': | |
80 | e_flag_used = true; | |
81 | break; | |
82 | case 'f': | |
83 | f_flag_used = true; | |
84 | from_file = optarg; | |
85 | break; | |
86 | case 'g': | |
87 | g_flag_used = false; | |
88 | break; | |
51fa56b9 | 89 | case 'h': |
90 | h_flag_used = true; | |
91 | break; | |
3abe9331 | 92 | case 'p': |
93 | p_flag_used = true; | |
94 | break; | |
95 | case 's': | |
96 | s_flag_used = true; | |
97 | break; | |
98 | case 't': | |
99 | t_flag_used = true; | |
100 | break; | |
101 | case 'v': | |
102 | printProductinfo(); | |
103 | printVersion(); | |
970ed795 | 104 | #ifdef LICENSE |
3abe9331 | 105 | print_license_info(); |
970ed795 | 106 | #endif |
3abe9331 | 107 | return EXIT_SUCCESS; |
108 | case 'q': | |
109 | q_flag_used = true; | |
110 | break; | |
111 | case 'w': | |
112 | w_flag_used = true; | |
113 | break; | |
114 | case 'x': | |
115 | x_flag_used = true; | |
116 | break; | |
117 | case 'z': | |
118 | z_flag_used = true; | |
119 | break; | |
120 | default: | |
121 | fprintf(stderr, "ERROR:\nInvalid option: -%c!\n", char(optopt)); | |
122 | printUsage(argv[0]); | |
123 | return EXIT_FAILURE; | |
970ed795 EL |
124 | } |
125 | } | |
126 | ||
3abe9331 | 127 | int first_module = f_flag_used ? 0 : optind; |
128 | int last_module = f_flag_used ? 0 : argc; | |
970ed795 | 129 | char **module_names = f_flag_used ? NULL : argv; |
3abe9331 | 130 | try { |
131 | if (f_flag_used) { | |
132 | // Idea from CR_TR00015706. | |
133 | module_names = readModulesFromFile(from_file, &last_module); | |
134 | if (!module_names) { | |
135 | fprintf(stderr, "ERROR: The file `%s' holding the XSD files cannot be " | |
136 | "processed!\n", from_file); | |
137 | throw 1; | |
138 | } | |
970ed795 | 139 | } |
970ed795 | 140 | |
3abe9331 | 141 | if (last_module - first_module <= 0) { |
142 | fprintf(stderr, "ERROR:\nNo module name was specified!\n"); | |
143 | printUsage(argv[0]); | |
144 | throw 1; | |
145 | } | |
970ed795 EL |
146 | |
147 | #ifdef LICENSE | |
3abe9331 | 148 | { |
149 | init_openssl(); | |
150 | license_struct lstr; | |
151 | load_license(&lstr); | |
152 | int license_valid = verify_license(&lstr); | |
153 | free_license(&lstr); | |
154 | free_openssl(); | |
155 | if (!license_valid) { | |
156 | exit(EXIT_FAILURE); | |
157 | } | |
970ed795 | 158 | } |
970ed795 EL |
159 | #endif |
160 | ||
3abe9331 | 161 | for (int i = first_module; i < last_module; ++i) { |
162 | if (!fopen(module_names[i], "r")) { | |
163 | fprintf(stderr, "ERROR:\nInput file `%s' does not exist.\n", | |
164 | module_names[i]); | |
165 | throw 1; | |
166 | } | |
970ed795 | 167 | } |
970ed795 | 168 | |
3abe9331 | 169 | if (checkSyntax(q_flag_used, first_module, last_module, module_names) == EXIT_FAILURE) { |
170 | throw 1; | |
171 | } | |
970ed795 | 172 | |
3abe9331 | 173 | if (validate(first_module, last_module, module_names) == EXIT_FAILURE) { |
174 | throw 1; | |
175 | } | |
970ed795 | 176 | |
3abe9331 | 177 | if (s_flag_used) { |
178 | printErrorStatistics(XMLParser::getNumErrors(), | |
179 | XMLParser::getNumWarnings()); | |
180 | if (XMLParser::getNumErrors() > 0) { | |
181 | throw 1; | |
182 | } | |
183 | return EXIT_SUCCESS; | |
184 | } | |
970ed795 | 185 | |
3abe9331 | 186 | if (generateCode(q_flag_used, p_flag_used, first_module, last_module, |
187 | module_names) == EXIT_FAILURE) { | |
188 | throw 1; | |
189 | } | |
190 | } catch (int) { | |
191 | if (f_flag_used) { | |
192 | for (int i = 0; i < last_module; ++i) { | |
193 | Free(module_names[i]); | |
194 | } | |
195 | Free(module_names); | |
196 | } | |
197 | return EXIT_FAILURE; | |
198 | } | |
970ed795 EL |
199 | |
200 | if (XMLParser::getNumWarnings() > 0 || | |
3abe9331 | 201 | TTCN3ModuleInventory::getNumErrors() > 0 || |
202 | TTCN3ModuleInventory::getNumWarnings() > 0) { | |
970ed795 EL |
203 | printErrorStatistics(TTCN3ModuleInventory::getNumErrors(), |
204 | XMLParser::getNumWarnings() + TTCN3ModuleInventory::getNumWarnings()); | |
205 | } | |
206 | ||
207 | return EXIT_SUCCESS; | |
970ed795 EL |
208 | } |
209 | ||
3abe9331 | 210 | static void printProductinfo() { |
970ed795 EL |
211 | fputs("XSD to TTCN-3 Converter for the TTCN-3 Test Executor, version " |
212 | PRODUCT_NUMBER "\n", stderr); | |
213 | } | |
214 | ||
3abe9331 | 215 | static void printUsage(const char * argv0) { |
970ed795 | 216 | fprintf(stderr, "\n" |
3f84031e | 217 | "usage: %s [-ceghpstVwx] [-f file] schema.xsd ...\n" |
970ed795 EL |
218 | " or %s -v\n" |
219 | "\n" | |
220 | "OPTIONS:\n" | |
221 | " -c: disable the generation of comments in TTCN-3 modules\n" | |
222 | " -e: disable the generation of encoding instructions in TTCN-3 modules\n" | |
223 | " -f file: the names of XSD files are taken from file instead of the command line\n" | |
3abe9331 | 224 | " -g: generate TTCN-3 code disallowing element substitution\n" |
51fa56b9 | 225 | " -h: generate TTCN-3 code allowing type substitution\n" |
970ed795 EL |
226 | " -p: do not generate the UsefulTtcn3Types and XSD predefined modules\n" |
227 | " -q: quiet mode - disable the issue of status messages\n" | |
228 | " -s: parse and validate only - no TTCN-3 module generation\n" | |
229 | " -t: disable the generation of timing information in TTCN-3 modules\n" | |
230 | " -v: show version information\n" | |
231 | " -w: suppress warnings\n" | |
232 | " -x: disable schema validation but generate TTCN-3 modules\n" | |
233 | " -z: zap URI scheme from module name\n" | |
234 | , argv0, argv0); | |
235 | } | |
236 | ||
3abe9331 | 237 | static void printVersion() { |
970ed795 EL |
238 | fputs("Product number: " PRODUCT_NUMBER "\n" |
239 | "Build date: " __DATE__ " " __TIME__ "\n" | |
240 | "Compiled with: " C_COMPILER_VERSION "\n\n" | |
241 | COPYRIGHT_STRING "\n\n", stderr); | |
242 | } | |
243 | ||
3abe9331 | 244 | static void printErrorStatistics(const unsigned int errors, const unsigned int warnings) { |
970ed795 | 245 | if (errors == 0) { |
3abe9331 | 246 | if (warnings == 0) { |
970ed795 EL |
247 | fprintf(stderr, |
248 | "Notify: No errors or warnings were detected.\n"); | |
3abe9331 | 249 | } else { |
970ed795 EL |
250 | fprintf(stderr, |
251 | "Notify: No errors and %u warning%s were detected.\n", | |
252 | warnings, | |
253 | warnings > 1 ? "s" : ""); | |
3abe9331 | 254 | } |
255 | } else { | |
256 | if (warnings == 0) { | |
970ed795 EL |
257 | fprintf(stderr, |
258 | "Notify: %u error%s and no warnings were detected.\n", | |
259 | errors, | |
260 | errors > 1 ? "s" : ""); | |
3abe9331 | 261 | } else { |
970ed795 EL |
262 | fprintf(stderr, |
263 | "Notify: %u error%s and %u warning%s were detected.\n", | |
264 | errors, | |
265 | errors > 1 ? "s" : "", | |
3abe9331 | 266 | warnings, |
267 | warnings > 1 ? "s" : ""); | |
268 | } | |
970ed795 EL |
269 | } |
270 | } | |
271 | ||
3abe9331 | 272 | static bool checkFailure() { |
273 | if (TTCN3ModuleInventory::getNumErrors() > 0) { | |
274 | printErrorStatistics(TTCN3ModuleInventory::getNumErrors(), | |
275 | XMLParser::getNumWarnings() + TTCN3ModuleInventory::getNumWarnings()); | |
970ed795 | 276 | return true; |
3abe9331 | 277 | } else { |
970ed795 EL |
278 | return false; |
279 | } | |
3abe9331 | 280 | } |
970ed795 | 281 | |
3abe9331 | 282 | static bool generatePredefinedModules() { |
feade998 | 283 | //struct stat stFileInfo; |
284 | // Only generate the missing predefined modules. | |
285 | // Generate, because the copyright is now generated into the modules. | |
286 | //if (stat("UsefulTtcn3Types.ttcn", &stFileInfo) != 0) { | |
3abe9331 | 287 | extern const char *moduleUsefulTtcn3Types; |
288 | FILE *fileUsefulTtcn3Types = fopen("UsefulTtcn3Types.ttcn", "w"); | |
289 | if (fileUsefulTtcn3Types == NULL) { | |
290 | fprintf(stderr, "ERROR:\nCannot create file UsefulTtcn3Types.ttcn!\n"); | |
291 | return false; | |
292 | } | |
feade998 | 293 | generate_TTCN3_header(fileUsefulTtcn3Types, "UsefulTtcn3Types", false); |
3abe9331 | 294 | fprintf(fileUsefulTtcn3Types, "%s", moduleUsefulTtcn3Types); |
295 | if (!q_flag_used) { | |
296 | fprintf(stderr, "Notify: File \'UsefulTtcn3Types.ttcn\' was generated.\n"); | |
297 | } | |
298 | fclose(fileUsefulTtcn3Types); | |
feade998 | 299 | //} |
970ed795 | 300 | |
3f84031e | 301 | //XSD.ttcn changed |
302 | //if (stat("XSD.ttcn", &stFileInfo) != 0) { | |
3abe9331 | 303 | extern const char *moduleXSD; |
304 | FILE *fileXsd = fopen("XSD.ttcn", "w"); | |
305 | if (fileXsd == NULL) { | |
306 | fprintf(stderr, "ERROR:\nCannot create file XSD.ttcn!\n"); | |
307 | return false; | |
308 | } | |
feade998 | 309 | generate_TTCN3_header(fileXsd, "XSD", false); |
3abe9331 | 310 | fprintf(fileXsd, "%s", moduleXSD); |
311 | if (!q_flag_used) { | |
312 | fprintf(stderr, "Notify: File \'XSD.ttcn\' was generated.\n"); | |
313 | } | |
314 | fclose(fileXsd); | |
3f84031e | 315 | //} |
970ed795 EL |
316 | return true; |
317 | } | |
318 | ||
3abe9331 | 319 | static char **readModulesFromFile(const char *from_file, int *last_module) { |
970ed795 EL |
320 | FILE *input = fopen(from_file, "r"); |
321 | if (!input) return NULL; | |
322 | // It should be a relatively small file. | |
323 | fseek(input, 0, SEEK_END); | |
324 | size_t input_bytes = ftell(input); | |
325 | rewind(input); | |
326 | size_t buf_len = input_bytes + 1; // sizeof(char)==1 by definition | |
3abe9331 | 327 | char *buf = (char *) Malloc(buf_len); |
970ed795 EL |
328 | buf[buf_len - 1] = 0; |
329 | size_t bytes_read = fread(buf, 1, input_bytes, input); | |
330 | fclose(input); | |
3abe9331 | 331 | if ((size_t) input_bytes != bytes_read) { |
970ed795 EL |
332 | Free(buf); |
333 | return NULL; | |
334 | } | |
335 | char **ret_val = NULL; | |
336 | *last_module = 0; | |
337 | const char *delim = " \f\n\r\t\v"; | |
338 | char *name = strtok(buf, delim); | |
339 | while (name) { | |
340 | if (!strlen(name)) | |
341 | continue; | |
3abe9331 | 342 | ret_val = (char **) Realloc(ret_val, sizeof (char *) * ++(*last_module)); |
970ed795 EL |
343 | ret_val[*last_module - 1] = mcopystr(name); |
344 | name = strtok(NULL, delim); | |
345 | } | |
346 | Free(buf); | |
347 | return ret_val; | |
348 | } | |
349 | ||
3abe9331 | 350 | static int checkSyntax(const bool not_verbose, const int first_module, const int last_module, |
351 | const char * const * const module_names) { | |
352 | if (!not_verbose) { | |
970ed795 | 353 | fprintf(stderr, "Notify: Checking documents...\n"); |
3abe9331 | 354 | } |
970ed795 | 355 | for (int i = first_module; i < last_module; ++i) { |
3abe9331 | 356 | if (!not_verbose) { |
970ed795 | 357 | fprintf(stderr, "Notify: Parsing XML schema document `%s'...\n", |
3abe9331 | 358 | module_names[i]); |
359 | } | |
970ed795 EL |
360 | XMLParser syntaxchecker(module_names[i]); |
361 | syntaxchecker.checkSyntax(); | |
362 | } | |
363 | if (XMLParser::getNumErrors() > 0) { | |
364 | printErrorStatistics(XMLParser::getNumErrors(), | |
3abe9331 | 365 | XMLParser::getNumWarnings()); |
970ed795 EL |
366 | return EXIT_FAILURE; |
367 | } | |
368 | return EXIT_SUCCESS; | |
369 | } | |
370 | ||
3abe9331 | 371 | static int generateCode(const bool quiet, const bool need_predefined, |
372 | const int first_module, const int last_module, | |
373 | const char * const * const module_names) { | |
970ed795 EL |
374 | TTCN3ModuleInventory& modules = TTCN3ModuleInventory::getInstance(); |
375 | for (int i = first_module; i < last_module; ++i) { | |
376 | XMLParser parser(module_names[i]); | |
377 | TTCN3Module *module = modules.addModule(module_names[i], &parser); | |
378 | parser.startConversion(module); | |
379 | module->goodbyeParser(); // the parser is going away, don't use it | |
380 | } | |
381 | ||
382 | if (XMLParser::getNumErrors() > 0) { | |
383 | printErrorStatistics(XMLParser::getNumErrors(), | |
3abe9331 | 384 | XMLParser::getNumWarnings()); |
970ed795 EL |
385 | return EXIT_FAILURE; |
386 | } | |
387 | ||
388 | if (d_flag_used > 1) { | |
389 | modules.dump(); | |
390 | fputs("+++++++++++++++++++++++++++++\n", stderr); | |
391 | } | |
392 | ||
393 | modules.modulenameConversion(); | |
394 | modules.referenceResolving(); | |
395 | modules.nameConversion(); | |
396 | modules.finalModification(); | |
397 | ||
3abe9331 | 398 | if (d_flag_used > 0) { |
399 | modules.dump(); | |
400 | } | |
970ed795 | 401 | |
3abe9331 | 402 | if (checkFailure()) { |
970ed795 EL |
403 | return EXIT_FAILURE; |
404 | } | |
405 | ||
3abe9331 | 406 | if (!quiet) { |
970ed795 | 407 | fprintf(stderr, "Notify: Generating TTCN-3 modules...\n"); |
3abe9331 | 408 | } |
970ed795 EL |
409 | |
410 | modules.moduleGeneration(); | |
411 | ||
3abe9331 | 412 | if (checkFailure()) { |
970ed795 EL |
413 | return EXIT_FAILURE; |
414 | } | |
415 | ||
3abe9331 | 416 | if (!need_predefined) { |
417 | if (!generatePredefinedModules()) { | |
970ed795 | 418 | return EXIT_FAILURE; |
3abe9331 | 419 | } |
420 | } | |
970ed795 EL |
421 | return EXIT_SUCCESS; |
422 | } | |
423 | ||
3abe9331 | 424 | static int validate(const int first_module, const int last_module, |
425 | const char * const * const module_names) { | |
970ed795 EL |
426 | for (int i = first_module; i < last_module; ++i) { |
427 | XMLParser validator(module_names[i]); | |
428 | validator.validate(); | |
429 | } | |
430 | if (XMLParser::getNumErrors() > 0) { | |
431 | printErrorStatistics(XMLParser::getNumErrors(), | |
3abe9331 | 432 | XMLParser::getNumWarnings()); |
970ed795 EL |
433 | return EXIT_FAILURE; |
434 | } | |
435 | return EXIT_SUCCESS; | |
436 | } | |
437 | ||
3abe9331 | 438 | reffer::reffer(const char*) { |
439 | } |