1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2014 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 p_flag_used
= false;
25 bool s_flag_used
= false;
26 bool t_flag_used
= false;
27 bool q_flag_used
= false;
28 bool w_flag_used
= false;
29 bool x_flag_used
= false;
30 bool z_flag_used
= false;
32 static void printProductinfo();
33 static void printUsage(const char * argv0
);
34 static void printVersion();
35 static void printErrorStatistics(unsigned int errors
, unsigned int warnings
);
36 static bool generatePredefinedModules();
37 static char **readModulesFromFile(const char *from_file
, int *last_module
);
38 static int checkSyntax(bool not_verbose
, int first_module
, int last_module
,
39 const char * const * const module_names
);
40 static int validate(int first_module
, int last_module
,
41 const char * const * const module_names
);
42 static int generateCode(bool quiet
, bool need_predefined
,
43 int first_module
, int last_module
,
44 const char * const * const module_names
);
46 int main(int argc
, char **argv
)
54 // The file holding a list of the XSD files.
55 const char *from_file
= NULL
;
59 while ((c
= getopt(argc
, argv
, "cdef:pqstvwxz")) != -1)
105 fprintf(stderr
, "ERROR:\nInvalid option: -%c!\n", char(optopt
));
111 int first_module
= f_flag_used
? 0 : optind
,
112 last_module
= f_flag_used
? 0 : argc
;
113 char **module_names
= f_flag_used
? NULL
: argv
;
116 // Idea from CR_TR00015706.
117 module_names
= readModulesFromFile(from_file
, &last_module
);
119 fprintf(stderr
, "ERROR: The file `%s' holding the XSD files cannot be "
120 "processed!\n", from_file
);
125 if (last_module
- first_module
<= 0) {
126 fprintf(stderr
, "ERROR:\nNo module name was specified!\n");
135 load_license (&lstr
);
136 int license_valid
= verify_license(&lstr
);
137 free_license (&lstr
);
139 if (!license_valid
) {
145 for (int i
= first_module
; i
< last_module
; ++i
) {
146 if (!fopen(module_names
[i
], "r")) {
147 fprintf(stderr
, "ERROR:\nInput file `%s' does not exist.\n",
153 if (checkSyntax(q_flag_used
, first_module
, last_module
, module_names
) == EXIT_FAILURE
)
156 if (validate(first_module
, last_module
, module_names
) == EXIT_FAILURE
)
160 printErrorStatistics(XMLParser::getNumErrors(),
161 XMLParser::getNumWarnings());
162 if (XMLParser::getNumErrors() > 0)
167 if (generateCode(q_flag_used
, p_flag_used
, first_module
, last_module
,
168 module_names
) == EXIT_FAILURE
)
171 if (XMLParser::getNumWarnings() > 0 ||
172 TTCN3ModuleInventory::getNumErrors() > 0 ||
173 TTCN3ModuleInventory::getNumWarnings() > 0) {
174 printErrorStatistics(TTCN3ModuleInventory::getNumErrors(),
175 XMLParser::getNumWarnings() + TTCN3ModuleInventory::getNumWarnings());
182 for (int i
= 0; i
< last_module
; ++i
)
183 Free(module_names
[i
]);
189 static void printProductinfo()
191 fputs("XSD to TTCN-3 Converter for the TTCN-3 Test Executor, version "
192 PRODUCT_NUMBER
"\n", stderr
);
195 static void printUsage(const char * argv0
)
198 "usage: %s [-cepstVwx] [-f file] schema.xsd ...\n"
202 " -c: disable the generation of comments in TTCN-3 modules\n"
203 " -e: disable the generation of encoding instructions in TTCN-3 modules\n"
204 " -f file: the names of XSD files are taken from file instead of the command line\n"
205 " -p: do not generate the UsefulTtcn3Types and XSD predefined modules\n"
206 " -q: quiet mode - disable the issue of status messages\n"
207 " -s: parse and validate only - no TTCN-3 module generation\n"
208 " -t: disable the generation of timing information in TTCN-3 modules\n"
209 " -v: show version information\n"
210 " -w: suppress warnings\n"
211 " -x: disable schema validation but generate TTCN-3 modules\n"
212 " -z: zap URI scheme from module name\n"
216 static void printVersion()
218 fputs("Product number: " PRODUCT_NUMBER
"\n"
219 "Build date: " __DATE__
" " __TIME__
"\n"
220 "Compiled with: " C_COMPILER_VERSION
"\n\n"
221 COPYRIGHT_STRING
"\n\n", stderr
);
224 static void printErrorStatistics(unsigned int errors
, unsigned int warnings
)
229 "Notify: No errors or warnings were detected.\n");
232 "Notify: No errors and %u warning%s were detected.\n",
234 warnings
> 1 ? "s" : "");
239 "Notify: %u error%s and no warnings were detected.\n",
241 errors
> 1 ? "s" : "");
244 "Notify: %u error%s and %u warning%s were detected.\n",
246 errors
> 1 ? "s" : "",
248 warnings
> 1 ? "s" : "");
252 static bool generatePredefinedModules()
254 struct stat stFileInfo
;
255 // FIXME: Regenerate only the missing file.
256 if (stat("UsefulTtcn3Types.ttcn", &stFileInfo
) == 0 &&
257 stat("XSD.ttcn", &stFileInfo
) == 0)
260 extern const char *moduleUsefulTtcn3Types
;
261 extern const char *moduleXSD
;
263 FILE *fileUsefulTtcn3Types
= fopen("UsefulTtcn3Types.ttcn", "w");
264 FILE *fileXsd
= fopen("XSD.ttcn", "w");
266 if (fileUsefulTtcn3Types
== NULL
) {
267 fprintf(stderr
, "ERROR:\nCannot create file UsefulTtcn3Types.ttcn!\n");
270 if (fileXsd
== NULL
) {
271 fprintf(stderr
, "ERROR:\nCannot create file XSD.ttcn!\n");
275 fprintf(fileUsefulTtcn3Types
, "%s", moduleUsefulTtcn3Types
);
276 fprintf(fileXsd
, "%s", moduleXSD
);
279 fprintf(stderr
, "Notify: File \'UsefulTtcn3Types.ttcn\' was generated.\n");
280 fprintf(stderr
, "Notify: File \'XSD.ttcn\' was generated.\n");
283 fclose(fileUsefulTtcn3Types
);
289 static char **readModulesFromFile(const char *from_file
, int *last_module
)
291 FILE *input
= fopen(from_file
, "r");
292 if (!input
) return NULL
;
293 // It should be a relatively small file.
294 fseek(input
, 0, SEEK_END
);
295 size_t input_bytes
= ftell(input
);
297 size_t buf_len
= input_bytes
+ 1; // sizeof(char)==1 by definition
298 char *buf
= (char *)Malloc(buf_len
);
299 buf
[buf_len
- 1] = 0;
300 size_t bytes_read
= fread(buf
, 1, input_bytes
, input
);
302 if ((size_t)input_bytes
!= bytes_read
) {
306 char **ret_val
= NULL
;
308 const char *delim
= " \f\n\r\t\v";
309 char *name
= strtok(buf
, delim
);
313 ret_val
= (char **)Realloc(ret_val
, sizeof(char *) * ++(*last_module
));
314 ret_val
[*last_module
- 1] = mcopystr(name
);
315 name
= strtok(NULL
, delim
);
321 static int checkSyntax(bool not_verbose
, int first_module
, int last_module
,
322 const char * const * const module_names
)
325 fprintf(stderr
, "Notify: Checking documents...\n");
326 for (int i
= first_module
; i
< last_module
; ++i
) {
328 fprintf(stderr
, "Notify: Parsing XML schema document `%s'...\n",
330 XMLParser
syntaxchecker(module_names
[i
]);
331 syntaxchecker
.checkSyntax();
333 if (XMLParser::getNumErrors() > 0) {
334 printErrorStatistics(XMLParser::getNumErrors(),
335 XMLParser::getNumWarnings());
341 typedef List
<QualifiedName
> QualifiedNames
;
343 static int generateCode(bool quiet
, bool need_predefined
,
344 int first_module
, int last_module
,
345 const char * const * const module_names
)
347 TTCN3ModuleInventory
& modules
= TTCN3ModuleInventory::getInstance();
348 for (int i
= first_module
; i
< last_module
; ++i
) {
349 XMLParser
parser(module_names
[i
]);
350 TTCN3Module
*module
= modules
.addModule(module_names
[i
], &parser
);
351 parser
.startConversion(module
);
352 module
->goodbyeParser(); // the parser is going away, don't use it
355 if (XMLParser::getNumErrors() > 0) {
356 printErrorStatistics(XMLParser::getNumErrors(),
357 XMLParser::getNumWarnings());
361 if (d_flag_used
> 1) {
363 fputs("+++++++++++++++++++++++++++++\n", stderr
);
366 modules
.modulenameConversion();
367 modules
.referenceResolving();
368 modules
.nameConversion();
369 modules
.finalModification();
371 if (d_flag_used
> 0) modules
.dump();
373 if (TTCN3ModuleInventory::getNumErrors() > 0) {
374 printErrorStatistics(TTCN3ModuleInventory::getNumErrors(),
375 XMLParser::getNumWarnings() + TTCN3ModuleInventory::getNumWarnings());
380 fprintf(stderr
, "Notify: Generating TTCN-3 modules...\n");
382 modules
.moduleGeneration();
384 if (TTCN3ModuleInventory::getNumErrors() > 0) {
385 printErrorStatistics(TTCN3ModuleInventory::getNumErrors(),
386 XMLParser::getNumWarnings() + TTCN3ModuleInventory::getNumWarnings());
390 if (!need_predefined
)
391 if (!generatePredefinedModules())
396 static int validate(int first_module
, int last_module
,
397 const char * const * const module_names
)
399 for (int i
= first_module
; i
< last_module
; ++i
) {
400 XMLParser
validator(module_names
[i
]);
401 validator
.validate();
403 if (XMLParser::getNumErrors() > 0) {
404 printErrorStatistics(XMLParser::getNumErrors(),
405 XMLParser::getNumWarnings());
411 reffer::reffer(const char*) {}
This page took 0.038923 seconds and 6 git commands to generate.