Use LTTngUSTLogger logger plugin in logtest regression test
[deliverable/titan.core.git] / xsdconvert / converter.cc
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 ******************************************************************************/
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;
35 bool g_flag_used = true;
36 bool h_flag_used = false;
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();
48 static void printErrorStatistics(const unsigned int errors, const unsigned int warnings);
49 static bool generatePredefinedModules();
50 static char **readModulesFromFile(const char *from_file, int *last_module);
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) {
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
71 while ((c = getopt(argc, argv, "cdef:ghpqstvwxz")) != -1) {
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;
89 case 'h':
90 h_flag_used = true;
91 break;
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();
104 #ifdef LICENSE
105 print_license_info();
106 #endif
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;
124 }
125 }
126
127 int first_module = f_flag_used ? 0 : optind;
128 int last_module = f_flag_used ? 0 : argc;
129 char **module_names = f_flag_used ? NULL : argv;
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 }
139 }
140
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 }
146
147 #ifdef LICENSE
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 }
158 }
159 #endif
160
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 }
167 }
168
169 if (checkSyntax(q_flag_used, first_module, last_module, module_names) == EXIT_FAILURE) {
170 throw 1;
171 }
172
173 if (validate(first_module, last_module, module_names) == EXIT_FAILURE) {
174 throw 1;
175 }
176
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 }
185
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 }
199
200 if (XMLParser::getNumWarnings() > 0 ||
201 TTCN3ModuleInventory::getNumErrors() > 0 ||
202 TTCN3ModuleInventory::getNumWarnings() > 0) {
203 printErrorStatistics(TTCN3ModuleInventory::getNumErrors(),
204 XMLParser::getNumWarnings() + TTCN3ModuleInventory::getNumWarnings());
205 }
206
207 return EXIT_SUCCESS;
208 }
209
210 static void printProductinfo() {
211 fputs("XSD to TTCN-3 Converter for the TTCN-3 Test Executor, version "
212 PRODUCT_NUMBER "\n", stderr);
213 }
214
215 static void printUsage(const char * argv0) {
216 fprintf(stderr, "\n"
217 "usage: %s [-ceghpstVwx] [-f file] schema.xsd ...\n"
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"
224 " -g: generate TTCN-3 code disallowing element substitution\n"
225 " -h: generate TTCN-3 code allowing type substitution\n"
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
237 static void printVersion() {
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
244 static void printErrorStatistics(const unsigned int errors, const unsigned int warnings) {
245 if (errors == 0) {
246 if (warnings == 0) {
247 fprintf(stderr,
248 "Notify: No errors or warnings were detected.\n");
249 } else {
250 fprintf(stderr,
251 "Notify: No errors and %u warning%s were detected.\n",
252 warnings,
253 warnings > 1 ? "s" : "");
254 }
255 } else {
256 if (warnings == 0) {
257 fprintf(stderr,
258 "Notify: %u error%s and no warnings were detected.\n",
259 errors,
260 errors > 1 ? "s" : "");
261 } else {
262 fprintf(stderr,
263 "Notify: %u error%s and %u warning%s were detected.\n",
264 errors,
265 errors > 1 ? "s" : "",
266 warnings,
267 warnings > 1 ? "s" : "");
268 }
269 }
270 }
271
272 static bool checkFailure() {
273 if (TTCN3ModuleInventory::getNumErrors() > 0) {
274 printErrorStatistics(TTCN3ModuleInventory::getNumErrors(),
275 XMLParser::getNumWarnings() + TTCN3ModuleInventory::getNumWarnings());
276 return true;
277 } else {
278 return false;
279 }
280 }
281
282 static bool generatePredefinedModules() {
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) {
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 }
293 generate_TTCN3_header(fileUsefulTtcn3Types, "UsefulTtcn3Types", false);
294 fprintf(fileUsefulTtcn3Types, "%s", moduleUsefulTtcn3Types);
295 if (!q_flag_used) {
296 fprintf(stderr, "Notify: File \'UsefulTtcn3Types.ttcn\' was generated.\n");
297 }
298 fclose(fileUsefulTtcn3Types);
299 //}
300
301 //XSD.ttcn changed
302 //if (stat("XSD.ttcn", &stFileInfo) != 0) {
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 }
309 generate_TTCN3_header(fileXsd, "XSD", false);
310 fprintf(fileXsd, "%s", moduleXSD);
311 if (!q_flag_used) {
312 fprintf(stderr, "Notify: File \'XSD.ttcn\' was generated.\n");
313 }
314 fclose(fileXsd);
315 //}
316 return true;
317 }
318
319 static char **readModulesFromFile(const char *from_file, int *last_module) {
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
327 char *buf = (char *) Malloc(buf_len);
328 buf[buf_len - 1] = 0;
329 size_t bytes_read = fread(buf, 1, input_bytes, input);
330 fclose(input);
331 if ((size_t) input_bytes != bytes_read) {
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;
342 ret_val = (char **) Realloc(ret_val, sizeof (char *) * ++(*last_module));
343 ret_val[*last_module - 1] = mcopystr(name);
344 name = strtok(NULL, delim);
345 }
346 Free(buf);
347 return ret_val;
348 }
349
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) {
353 fprintf(stderr, "Notify: Checking documents...\n");
354 }
355 for (int i = first_module; i < last_module; ++i) {
356 if (!not_verbose) {
357 fprintf(stderr, "Notify: Parsing XML schema document `%s'...\n",
358 module_names[i]);
359 }
360 XMLParser syntaxchecker(module_names[i]);
361 syntaxchecker.checkSyntax();
362 }
363 if (XMLParser::getNumErrors() > 0) {
364 printErrorStatistics(XMLParser::getNumErrors(),
365 XMLParser::getNumWarnings());
366 return EXIT_FAILURE;
367 }
368 return EXIT_SUCCESS;
369 }
370
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) {
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(),
384 XMLParser::getNumWarnings());
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
398 if (d_flag_used > 0) {
399 modules.dump();
400 }
401
402 if (checkFailure()) {
403 return EXIT_FAILURE;
404 }
405
406 if (!quiet) {
407 fprintf(stderr, "Notify: Generating TTCN-3 modules...\n");
408 }
409
410 modules.moduleGeneration();
411
412 if (checkFailure()) {
413 return EXIT_FAILURE;
414 }
415
416 if (!need_predefined) {
417 if (!generatePredefinedModules()) {
418 return EXIT_FAILURE;
419 }
420 }
421 return EXIT_SUCCESS;
422 }
423
424 static int validate(const int first_module, const int last_module,
425 const char * const * const module_names) {
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(),
432 XMLParser::getNumWarnings());
433 return EXIT_FAILURE;
434 }
435 return EXIT_SUCCESS;
436 }
437
438 reffer::reffer(const char*) {
439 }
This page took 0.041192 seconds and 5 git commands to generate.