cba52ad0ba42bee6d827d648c0554a5486c5ed9a
1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2015 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 #include "XMLParser.hh"
9 #include "TTCN3Module.hh"
10 #include "TTCN3ModuleInventory.hh"
11 #include "SimpleType.hh"
12 #include "ComplexType.hh"
14 #include "../common/version_internal.h"
15 #include "../common/license.h"
17 #include <unistd.h> // for using "getopt" function
20 bool c_flag_used
= false;
22 bool e_flag_used
= false;
23 bool f_flag_used
= false;
24 bool g_flag_used
= true;
25 bool h_flag_used
= false;
26 bool p_flag_used
= false;
27 bool s_flag_used
= false;
28 bool t_flag_used
= false;
29 bool q_flag_used
= false;
30 bool w_flag_used
= false;
31 bool x_flag_used
= false;
32 bool z_flag_used
= false;
34 static void printProductinfo();
35 static void printUsage(const char * argv0
);
36 static void printVersion();
37 static void printErrorStatistics(const unsigned int errors
, const unsigned int warnings
);
38 static bool generatePredefinedModules();
39 static char **readModulesFromFile(const char *from_file
, int *last_module
);
40 static int checkSyntax(const bool not_verbose
, const int first_module
, const int last_module
,
41 const char * const * const module_names
);
42 static int validate(int const first_module
, int const last_module
,
43 const char * const * const module_names
);
44 static int generateCode(const bool quiet
, const bool need_predefined
,
45 const int first_module
, const int last_module
,
46 const char * const * const module_names
);
48 int main(int argc
, char **argv
) {
55 // The file holding a list of the XSD files.
56 const char *from_file
= NULL
;
60 while ((c
= getopt(argc
, argv
, "cdef:ghpqstvwxz")) != -1) {
110 fprintf(stderr
, "ERROR:\nInvalid option: -%c!\n", char(optopt
));
116 int first_module
= f_flag_used
? 0 : optind
;
117 int last_module
= f_flag_used
? 0 : argc
;
118 char **module_names
= f_flag_used
? NULL
: argv
;
121 // Idea from CR_TR00015706.
122 module_names
= readModulesFromFile(from_file
, &last_module
);
124 fprintf(stderr
, "ERROR: The file `%s' holding the XSD files cannot be "
125 "processed!\n", from_file
);
130 if (last_module
- first_module
<= 0) {
131 fprintf(stderr
, "ERROR:\nNo module name was specified!\n");
141 int license_valid
= verify_license(&lstr
);
144 if (!license_valid
) {
150 for (int i
= first_module
; i
< last_module
; ++i
) {
151 if (!fopen(module_names
[i
], "r")) {
152 fprintf(stderr
, "ERROR:\nInput file `%s' does not exist.\n",
158 if (checkSyntax(q_flag_used
, first_module
, last_module
, module_names
) == EXIT_FAILURE
) {
162 if (validate(first_module
, last_module
, module_names
) == EXIT_FAILURE
) {
167 printErrorStatistics(XMLParser::getNumErrors(),
168 XMLParser::getNumWarnings());
169 if (XMLParser::getNumErrors() > 0) {
175 if (generateCode(q_flag_used
, p_flag_used
, first_module
, last_module
,
176 module_names
) == EXIT_FAILURE
) {
181 for (int i
= 0; i
< last_module
; ++i
) {
182 Free(module_names
[i
]);
189 if (XMLParser::getNumWarnings() > 0 ||
190 TTCN3ModuleInventory::getNumErrors() > 0 ||
191 TTCN3ModuleInventory::getNumWarnings() > 0) {
192 printErrorStatistics(TTCN3ModuleInventory::getNumErrors(),
193 XMLParser::getNumWarnings() + TTCN3ModuleInventory::getNumWarnings());
199 static void printProductinfo() {
200 fputs("XSD to TTCN-3 Converter for the TTCN-3 Test Executor, version "
201 PRODUCT_NUMBER
"\n", stderr
);
204 static void printUsage(const char * argv0
) {
206 "usage: %s [-cepstVwx] [-f file] schema.xsd ...\n"
210 " -c: disable the generation of comments in TTCN-3 modules\n"
211 " -e: disable the generation of encoding instructions in TTCN-3 modules\n"
212 " -f file: the names of XSD files are taken from file instead of the command line\n"
213 " -g: generate TTCN-3 code disallowing element substitution\n"
214 " -h: generate TTCN-3 code allowing type substitution\n"
215 " -p: do not generate the UsefulTtcn3Types and XSD predefined modules\n"
216 " -q: quiet mode - disable the issue of status messages\n"
217 " -s: parse and validate only - no TTCN-3 module generation\n"
218 " -t: disable the generation of timing information in TTCN-3 modules\n"
219 " -v: show version information\n"
220 " -w: suppress warnings\n"
221 " -x: disable schema validation but generate TTCN-3 modules\n"
222 " -z: zap URI scheme from module name\n"
226 static void printVersion() {
227 fputs("Product number: " PRODUCT_NUMBER
"\n"
228 "Build date: " __DATE__
" " __TIME__
"\n"
229 "Compiled with: " C_COMPILER_VERSION
"\n\n"
230 COPYRIGHT_STRING
"\n\n", stderr
);
233 static void printErrorStatistics(const unsigned int errors
, const unsigned int warnings
) {
237 "Notify: No errors or warnings were detected.\n");
240 "Notify: No errors and %u warning%s were detected.\n",
242 warnings
> 1 ? "s" : "");
247 "Notify: %u error%s and no warnings were detected.\n",
249 errors
> 1 ? "s" : "");
252 "Notify: %u error%s and %u warning%s were detected.\n",
254 errors
> 1 ? "s" : "",
256 warnings
> 1 ? "s" : "");
261 static bool checkFailure() {
262 if (TTCN3ModuleInventory::getNumErrors() > 0) {
263 printErrorStatistics(TTCN3ModuleInventory::getNumErrors(),
264 XMLParser::getNumWarnings() + TTCN3ModuleInventory::getNumWarnings());
271 static bool generatePredefinedModules() {
272 struct stat stFileInfo
;
273 // Only generate the missing predefined modules.
274 if (stat("UsefulTtcn3Types.ttcn", &stFileInfo
) != 0) {
275 extern const char *moduleUsefulTtcn3Types
;
276 FILE *fileUsefulTtcn3Types
= fopen("UsefulTtcn3Types.ttcn", "w");
277 if (fileUsefulTtcn3Types
== NULL
) {
278 fprintf(stderr
, "ERROR:\nCannot create file UsefulTtcn3Types.ttcn!\n");
281 fprintf(fileUsefulTtcn3Types
, "%s", moduleUsefulTtcn3Types
);
283 fprintf(stderr
, "Notify: File \'UsefulTtcn3Types.ttcn\' was generated.\n");
285 fclose(fileUsefulTtcn3Types
);
288 if (stat("XSD.ttcn", &stFileInfo
) != 0) {
289 extern const char *moduleXSD
;
290 FILE *fileXsd
= fopen("XSD.ttcn", "w");
291 if (fileXsd
== NULL
) {
292 fprintf(stderr
, "ERROR:\nCannot create file XSD.ttcn!\n");
295 fprintf(fileXsd
, "%s", moduleXSD
);
297 fprintf(stderr
, "Notify: File \'XSD.ttcn\' was generated.\n");
304 static char **readModulesFromFile(const char *from_file
, int *last_module
) {
305 FILE *input
= fopen(from_file
, "r");
306 if (!input
) return NULL
;
307 // It should be a relatively small file.
308 fseek(input
, 0, SEEK_END
);
309 size_t input_bytes
= ftell(input
);
311 size_t buf_len
= input_bytes
+ 1; // sizeof(char)==1 by definition
312 char *buf
= (char *) Malloc(buf_len
);
313 buf
[buf_len
- 1] = 0;
314 size_t bytes_read
= fread(buf
, 1, input_bytes
, input
);
316 if ((size_t) input_bytes
!= bytes_read
) {
320 char **ret_val
= NULL
;
322 const char *delim
= " \f\n\r\t\v";
323 char *name
= strtok(buf
, delim
);
327 ret_val
= (char **) Realloc(ret_val
, sizeof (char *) * ++(*last_module
));
328 ret_val
[*last_module
- 1] = mcopystr(name
);
329 name
= strtok(NULL
, delim
);
335 static int checkSyntax(const bool not_verbose
, const int first_module
, const int last_module
,
336 const char * const * const module_names
) {
338 fprintf(stderr
, "Notify: Checking documents...\n");
340 for (int i
= first_module
; i
< last_module
; ++i
) {
342 fprintf(stderr
, "Notify: Parsing XML schema document `%s'...\n",
345 XMLParser
syntaxchecker(module_names
[i
]);
346 syntaxchecker
.checkSyntax();
348 if (XMLParser::getNumErrors() > 0) {
349 printErrorStatistics(XMLParser::getNumErrors(),
350 XMLParser::getNumWarnings());
356 static int generateCode(const bool quiet
, const bool need_predefined
,
357 const int first_module
, const int last_module
,
358 const char * const * const module_names
) {
359 TTCN3ModuleInventory
& modules
= TTCN3ModuleInventory::getInstance();
360 for (int i
= first_module
; i
< last_module
; ++i
) {
361 XMLParser
parser(module_names
[i
]);
362 TTCN3Module
*module
= modules
.addModule(module_names
[i
], &parser
);
363 parser
.startConversion(module
);
364 module
->goodbyeParser(); // the parser is going away, don't use it
367 if (XMLParser::getNumErrors() > 0) {
368 printErrorStatistics(XMLParser::getNumErrors(),
369 XMLParser::getNumWarnings());
373 if (d_flag_used
> 1) {
375 fputs("+++++++++++++++++++++++++++++\n", stderr
);
378 modules
.modulenameConversion();
379 modules
.referenceResolving();
380 modules
.nameConversion();
381 modules
.finalModification();
383 if (d_flag_used
> 0) {
387 if (checkFailure()) {
392 fprintf(stderr
, "Notify: Generating TTCN-3 modules...\n");
395 modules
.moduleGeneration();
397 if (checkFailure()) {
401 if (!need_predefined
) {
402 if (!generatePredefinedModules()) {
409 static int validate(const int first_module
, const int last_module
,
410 const char * const * const module_names
) {
411 for (int i
= first_module
; i
< last_module
; ++i
) {
412 XMLParser
validator(module_names
[i
]);
413 validator
.validate();
415 if (XMLParser::getNumErrors() > 0) {
416 printErrorStatistics(XMLParser::getNumErrors(),
417 XMLParser::getNumWarnings());
423 reffer::reffer(const char*) {
This page took 0.039426 seconds and 5 git commands to generate.