main.cc 33.1 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
///////////////////////////////////////////////////////////////////////////////
Elemer Lelik's avatar
Elemer Lelik committed
2
// Copyright (c) 2000-2015 Ericsson Telecom AB
Elemer Lelik's avatar
Elemer Lelik committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
///////////////////////////////////////////////////////////////////////////////
/* Main program for the merged compiler */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <vector>
#include <sstream>
#if defined SOLARIS || defined SOLARIS8
# include <sys/utsname.h>
#endif

#ifdef USAGE_STATS
#include "../common/usage_stats.hh"
#include <signal.h>
#endif

#include "../common/dbgnew.hh"
#include "../common/path.h"
#include "../common/version_internal.h"
#include "../common/userinfo.h"
#include "datatypes.h"
#include "main.hh"

#include "asn1/asn1_preparser.h"
#include "asn1/asn1.hh"
#include "ttcn3/ttcn3_preparser.h"
#include "ttcn3/compiler.h"

#include "AST.hh"
#include "asn1/AST_asn1.hh"
#include "ttcn3/AST_ttcn3.hh"

#include "CodeGenHelper.hh"
#include "Stopwatch.hh"

#include "ttcn3/Ttcn2Json.hh"

Elemer Lelik's avatar
Elemer Lelik committed
48
49
#include "ttcn3/profiler.h"

Elemer Lelik's avatar
Elemer Lelik committed
50
51
52
53
54
55
56
57
#ifdef LICENSE
#include "../common/license.h"
#endif

using namespace Common;

const char *output_dir = NULL;
const char *tcov_file_name = NULL;
Elemer Lelik's avatar
Elemer Lelik committed
58
const char *profiler_file_name = NULL;
Elemer Lelik's avatar
Elemer Lelik committed
59
60
61
62
63
64
65
66
67
68
69
70
71
tcov_file_list *tcov_files = NULL;
expstring_t effective_module_lines = NULL;
expstring_t effective_module_functions = NULL;

size_t nof_top_level_pdus = 0;
const char **top_level_pdu = NULL;

boolean generate_skeleton = FALSE, force_overwrite = FALSE,
  include_line_info = FALSE, include_location_info = FALSE,
  duplicate_underscores = FALSE, parse_only = FALSE,
  semantic_check_only = FALSE, output_only_linenum = FALSE,
  default_as_optional = FALSE, enable_set_bound_out_param = FALSE,
  use_runtime_2 = FALSE, gcc_compat = FALSE, asn1_xer = FALSE,
Elemer Lelik's avatar
Elemer Lelik committed
72
73
  check_subtype = TRUE, suppress_context = FALSE, display_up_to_date = FALSE,
  implicit_json_encoding = FALSE, json_refs_for_all_types = TRUE,
Elemer Lelik's avatar
Elemer Lelik committed
74
75
  force_gen_seof = FALSE, omit_in_value_list = FALSE,
  warnings_for_bad_variants = FALSE;
Elemer Lelik's avatar
Elemer Lelik committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227

// Default code splitting mode is set to 'no splitting'.
CodeGenHelper::split_type code_splitting_mode = CodeGenHelper::SPLIT_NONE;

#if defined SOLARIS || defined SOLARIS8
/** Automatic detection of Solaris version based on uname() system call.
 * Distinguishing is needed because some socket functions use socklen_t
 * (which is an alias for unsigned int) as length arguments on Solaris 8.
 * On Solaris 2.6 the argument type is simply int and no socklen_t or other
 * alias exists.
 * Note: It was discovered later that Solaris 7 (which is used rarely within
 * Ericsson) already uses socklen_t thus the SOLARIS8 platform identifier is a
 * bit misleading. */
static const char *get_platform_string(void)
{
    struct utsname name;
    int major, minor;
    if (uname(&name) < 0) {
        WARNING("System call uname() failed: %s", strerror(errno));
        errno = 0;
        return "SOLARIS";
    }
    if (sscanf(name.release, "%d.%d", &major, &minor) == 2 && major == 5) {
        if (minor <= 6) return "SOLARIS";
        else return "SOLARIS8";
    } else {
        ERROR("Invalid OS release: %s", name.release);
        return "SOLARIS";
    }
}
#elif defined LINUX
#define get_platform_string() "LINUX"
#elif defined FREEBSD
#define get_platform_string() "FREEBSD"
#elif defined WIN32
#define get_platform_string() "WIN32"
#elif defined INTERIX
#define get_platform_string() "INTERIX"
#else
#error Platform was not set.
#endif


const char *expected_platform = get_platform_string();

/// "The" AST.
Modules *modules = NULL;

// Features can be disabled in the license or by commandline switches
static bool raw_disabled = false, ber_disabled = false, per_disabled = false,
  text_disabled = false, xer_disabled = false, json_disabled = false;
static bool attribute_validation_disabled = FALSE;
#ifdef LICENSE
static bool has_raw_feature = false, has_ber_feature = false,
  has_per_feature = false, has_text_feature = false, has_xer_feature = false;
#endif

boolean enable_raw()
{
  if (raw_disabled) return FALSE;
#ifdef LICENSE
  if (!has_raw_feature) {
    WARNING("The license key does not allow the generation of "
            "RAW encoder/decoder functions.");
    raw_disabled = true;
    return FALSE;
  }
#endif
  return TRUE;
}

boolean enable_ber()
{
  if (ber_disabled) return FALSE;
#ifdef LICENSE
  if (!has_ber_feature) {
    WARNING("The license key does not allow the generation of "
            "BER encoder/decoder functions.");
    ber_disabled = true;
    return FALSE;
  }
#endif
  return TRUE;
}

boolean enable_per()
{
  if (per_disabled) return FALSE;
#ifdef LICENSE
  if (!has_per_feature) {
    WARNING("The license key does not allow the generation of "
            "PER encoder/decoder functions.");
    per_disabled = true;
    return FALSE;
  }
#endif
  return TRUE;
}

boolean enable_text()
{
  if (text_disabled) return FALSE;
#ifdef LICENSE
  if (!has_text_feature) {
    WARNING("The license key does not allow the generation of "
            "TEXT encoder/decoder functions.");
    text_disabled = true;
    return FALSE;
  }
#endif
  return TRUE;
}

boolean enable_xer()
{
  if (xer_disabled) return FALSE;
#ifdef LICENSE
  if (!has_xer_feature) {
    WARNING("The license key does not allow the generation of "
            "XER encoder/decoder functions.");
    xer_disabled = true;
    return FALSE;
  }
#endif
  return TRUE;
}

boolean enable_json()
{
  return !json_disabled;
}

boolean disable_attribute_validation()
{
  if (attribute_validation_disabled) return TRUE;

  return FALSE;
}

char *canonize_input_file(const char *path_name)
{
  switch (get_path_status(path_name)) {
  case PS_NONEXISTENT:
    ERROR("Input file `%s' does not exist.", path_name);
    return NULL;
  case PS_DIRECTORY:
    ERROR("Argument `%s' is a directory.", path_name);
    return NULL;
  default:
    break;
  }
  char *dir_name = get_dir_from_path(path_name);
Elemer Lelik's avatar
Elemer Lelik committed
228
  char *abs_dir = get_absolute_dir(dir_name, NULL, true);
Elemer Lelik's avatar
Elemer Lelik committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
  Free(dir_name);
  char *file_name = get_file_from_path(path_name);
  char *ret_val = compose_path_name(abs_dir, file_name);
  Free(abs_dir);
  Free(file_name);
  return ret_val;
}

struct module_struct {
  const char *file_name;
  char *absolute_path;
  Module::moduletype_t module_type;
  bool need_codegen; /**< Code is generated for a module if
  - the module appears on the command line after the dash, or
  - there is no dash (code is generated for all modules) */
};

static void add_module(size_t& n_modules, module_struct*& module_list,
  const char *file_name, Module::moduletype_t module_type)
{
  char *absolute_path = canonize_input_file(file_name);
  if (absolute_path == NULL) return;
  for (size_t i = 0; i < n_modules; i++) {
    const module_struct *module = module_list + i;
    if (module->module_type == module_type &&
	!strcmp(module->absolute_path, absolute_path)) {
      ERROR("Input file `%s' was given more than once.", file_name);
      Free(absolute_path);
      return;
    }
  }
  module_list = (module_struct*)
    Realloc(module_list, (n_modules + 1) * sizeof(module_struct));
  module_struct *module = module_list + n_modules;
  module->file_name = file_name;
  module->absolute_path = absolute_path;
  module->module_type = module_type;
  module->need_codegen = false;
  n_modules++;
}

const char *get_tcov_file_name(const char *file_name)
{
  tcov_file_list *tcov_file = tcov_files;
  expstring_t file_name_pp = mputprintf(NULL, "%spp", file_name);
  while (tcov_file != NULL) {
	// This name can be a `.ttcnpp' too.
	const char *real_file_name = static_cast<const char *>(tcov_file->file_name);
	if (!strcmp(file_name, real_file_name) ||
		!strcmp(static_cast<const char *>(file_name_pp), real_file_name)) {
	  Free(file_name_pp);
	  return real_file_name;
	}
	tcov_file = tcov_file->next;
  }
  Free(file_name_pp);
  return NULL;
}

boolean in_tcov_files(const char *file_name)
{
  return get_tcov_file_name(file_name) ? TRUE : FALSE;
}

Elemer Lelik's avatar
Elemer Lelik committed
293
294
static bool check_file_list(const char *file_name, module_struct *module_list,
                            size_t n_modules, tcov_file_list *&file_list_head)
Elemer Lelik's avatar
Elemer Lelik committed
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
{
  FILE *fp = fopen(file_name, "r");
  if (fp == NULL) {
	ERROR("File `%s' does not exist.", file_name);
    return false;
  }
#ifndef PATH_MAX
#define PATH_MAX 1024
#endif
  char line[PATH_MAX];
  bool unlisted_files = false;
  while (fgets(line, sizeof(line), fp) != NULL) {
	// Remove trailing '\n'.
	size_t line_len = strlen(line) - 1;
	if (line[line_len] == '\n')
	  line[line_len] = 0;
	// Handle `.ttcnpp' files in input file.
	if (line_len > 1) {
	  char last = line[line_len - 1];
	  char before_last = line[line_len - 2];
      if (last == 'p' && before_last == 'p')
    	line_len -= 2;
	}
	if (line_len < 1)
      continue;
    size_t i = 0;
    for (; i < n_modules; ++i) {
      const module_struct *module = module_list + i;
      if (!strncmp(module->file_name, line, line_len)) {
Elemer Lelik's avatar
Elemer Lelik committed
324
325
        tcov_file_list *next_file = (tcov_file_list*)Malloc(sizeof(tcov_file_list));
        next_file->next = file_list_head;
Elemer Lelik's avatar
Elemer Lelik committed
326
327
        // We'll need the `.ttcnpp' file name.
        next_file->file_name = mcopystr(line);
Elemer Lelik's avatar
Elemer Lelik committed
328
        file_list_head = next_file;
Elemer Lelik's avatar
Elemer Lelik committed
329
330
331
332
333
334
335
336
337
338
339
        break;
      }
    }
    if (i == n_modules) {
      ERROR("File `%s' was listed in `%s', but not in the command line.",
    		line, file_name);
      unlisted_files = true;
    }
  }
  fclose(fp);
  if (unlisted_files) {
Elemer Lelik's avatar
Elemer Lelik committed
340
341
342
343
344
	while (file_list_head != NULL) {
	  tcov_file_list *next_file = file_list_head->next;
	  Free(file_list_head->file_name);
	  Free(file_list_head);
	  file_list_head = next_file;
Elemer Lelik's avatar
Elemer Lelik committed
345
	}
Elemer Lelik's avatar
Elemer Lelik committed
346
	file_list_head = NULL;
Elemer Lelik's avatar
Elemer Lelik committed
347
348
349
350
351
352
  }
  return !unlisted_files;
}

static boolean is_valid_asn1_filename(const char* file_name)
{
Elemer Lelik's avatar
Elemer Lelik committed
353
354
355
  // only check the actual file name, not the whole path
  const char* file_name_start = strrchr(file_name, '/');
  if (0 == strchr(file_name_start != NULL ? file_name_start : file_name, '-' )) {
Elemer Lelik's avatar
Elemer Lelik committed
356
357
358
359
360
361
362
363
    return TRUE;
  }
  return FALSE;
}

static void usage()
{
  fprintf(stderr, "\n"
Elemer Lelik's avatar
Elemer Lelik committed
364
    "usage: %s [-abcdEfgijlLOpqrRsStuwxXyY] [-K file] [-z file] [-V verb_level]\n"
Elemer Lelik's avatar
Elemer Lelik committed
365
    "	[-o dir] [-U none|type] [-P modulename.top_level_pdu_name] [-Q number] ...\n"
Elemer Lelik's avatar
Elemer Lelik committed
366
367
    "	[-T] module.ttcn [-A] module.asn ...\n"
    "	or  %s -v\n"
Elemer Lelik's avatar
Elemer Lelik committed
368
    "	or  %s --ttcn2json [-jf] ... [-T] module.ttcn [-A] module.asn ... [- schema.json]\n"
Elemer Lelik's avatar
Elemer Lelik committed
369
370
371
372
373
374
    "\n"
    "OPTIONS:\n"
    "	-a:		force XER in ASN.1 files\n"
    "	-b:		disable BER encoder/decoder functions\n"
    "	-c:		write out checksums in case of error\n"
    "	-d:		treat default fields as omit\n"
Elemer Lelik's avatar
Elemer Lelik committed
375
    "	-E:		display only warnings for unrecognized encoding variants\n"
Elemer Lelik's avatar
Elemer Lelik committed
376
377
378
    "	-f:		force overwriting of output files\n"
    "	-g:		emulate GCC error/warning message format\n"
    "	-i:		use only line numbers in error/warning messages\n"
Elemer Lelik's avatar
Elemer Lelik committed
379
    "	-j:		disable JSON encoder/decoder functions\n"
Elemer Lelik's avatar
Elemer Lelik committed
380
    "	-K file:	enable selective code coverage\n"
Elemer Lelik's avatar
Elemer Lelik committed
381
382
    "	-l:		include source line info in C++ code\n"
    "	-L:		add source line info for logging\n"
Elemer Lelik's avatar
Elemer Lelik committed
383
    "	-M:		allow 'omit' in template value lists (legacy behavior)\n"
Elemer Lelik's avatar
Elemer Lelik committed
384
    "	-o dir:		output files will be placed into dir\n"
Elemer Lelik's avatar
Elemer Lelik committed
385
    "	-p:		parse only (no semantic check or code generation)\n"
Elemer Lelik's avatar
Elemer Lelik committed
386
    "	-P pduname:	define top-level pdu\n"
Elemer Lelik's avatar
Elemer Lelik committed
387
388
389
390
391
392
393
394
395
    "	-q:		suppress all messages (quiet mode)\n"
    "	-Qn:		quit after n errors\n"
    "	-r:		disable RAW encoder/decoder functions\n"
    "	-R:		use function test runtime (TITAN_RUNTIME_2)\n"
    "	-s:		parse and semantic check only (no code generation)\n"
    "	-S:		suppress context information\n"
    "	-t:		generate Test Port skeleton\n"
    "	-u:		duplicate underscores in file names\n"
    "	-U none|type:	select code splitting mode for the generated C++ code\n"
Elemer Lelik's avatar
Elemer Lelik committed
396
    "	-V verb_level:	set verbosity level bitmask (decimal)\n"
Elemer Lelik's avatar
Elemer Lelik committed
397
398
399
400
    "	-w:		suppress warnings\n"
    "	-x:		disable TEXT encoder/decoder functions\n"
    "	-X:		disable XER encoder/decoder functions\n"
    "	-y:		disable subtype checking\n"
Elemer Lelik's avatar
Elemer Lelik committed
401
    "	-Y:		enforce legacy behaviour for \"out\" function parameters (see refguide)\n"
Elemer Lelik's avatar
Elemer Lelik committed
402
    "	-z file:	enable profiling and code coverage for the TTCN-3 files in the argument\n"
Elemer Lelik's avatar
Elemer Lelik committed
403
404
405
    "	-T file:	force interpretation of file as TTCN-3 module\n"
    "	-A file:	force interpretation of file as ASN.1 module\n"
    "	-v:		show version\n"
Elemer Lelik's avatar
Elemer Lelik committed
406
407
408
409
    "	--ttcn2json:	generate JSON schema from input modules\n"
    "JSON schema generator options:\n"
    "	-j:		only include types with JSON encoding\n"
    "	-f:		only generate references to types with JSON encoding/decoding functions\n", argv0, argv0, argv0);
Elemer Lelik's avatar
Elemer Lelik committed
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
}

#define SET_FLAG(x) if (x##flag) {\
    ERROR("Flag -" #x " was specified more than once.");\
    errflag = true;\
    } else x##flag = true


extern int ttcn3_debug;
extern int asn1_yydebug;
extern int pattern_yydebug;
extern int pattern_unidebug;
extern int rawAST_debug;
extern int coding_attrib_debug;

int main(int argc, char *argv[])
{
  argv0 = argv[0];
#ifndef NDEBUG
  asn1_yydebug    = !! getenv("DEBUG_ASN1");
  ttcn3_debug     = !! getenv("DEBUG_TTCN");
  pattern_unidebug= pattern_yydebug = !! getenv("DEBUG_PATTERN");
  rawAST_debug    = !! getenv("DEBUG_RAW");
  coding_attrib_debug = !!getenv("DEBUG_ATRIB") || getenv("DEBUG_ATTRIB");
#endif

#ifdef MEMORY_DEBUG
#if defined(__CYGWIN__) || defined(INTERIX)
  //nothing to do
#else
  debug_new_counter.set_program_name(argv0);
#endif
#endif

  if (argc == 1) {
    fputs("TTCN-3 and ASN.1 Compiler for the TTCN-3 Test Executor, version "
      PRODUCT_NUMBER "\n", stderr);
    usage();
    return EXIT_FAILURE;
  }
Elemer Lelik's avatar
Elemer Lelik committed
450
  
Elemer Lelik's avatar
Elemer Lelik committed
451
452
453
454
455
456
457
458
  bool
    Aflag = false,  Lflag = false, Yflag = false,
    Pflag = false, Tflag = false, Vflag = false, bflag = false,
    cflag = false, fflag = false, iflag = false, lflag = false,
    oflag = false, pflag = false, qflag = false, rflag = false, sflag = false,
    tflag = false, uflag = false, vflag = false, wflag = false, xflag = false,
    dflag = false, Xflag = false, Rflag = false, gflag = false, aflag = false,
    s0flag = false, Cflag = false, yflag = false, Uflag = false, Qflag = false,
Elemer Lelik's avatar
Elemer Lelik committed
459
    Sflag = false, Kflag = false, jflag = false, zflag = false, Fflag = false,
Elemer Lelik's avatar
Elemer Lelik committed
460
461
    Mflag = false, Eflag = false, errflag = false, print_usage = false,
    ttcn2json = false;
Elemer Lelik's avatar
Elemer Lelik committed
462
463
464
465
466
467
468
469
470
471
472
473
474
475

  CodeGenHelper cgh;

  bool asn1_modules_present = false;
#ifdef LICENSE
  bool ttcn3_modules_present = false;
#endif
  size_t n_modules = 0;
  module_struct *module_list = NULL;
  char* json_schema_name = NULL;
  
  if (0 == strcmp(argv[1], "--ttcn2json")) {
    ttcn2json = true;
    display_up_to_date = TRUE;
Elemer Lelik's avatar
Elemer Lelik committed
476
    implicit_json_encoding = TRUE;
Elemer Lelik's avatar
Elemer Lelik committed
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
    for (int i = 2; i < argc; ++i) {
      // A dash (-) is used to separate the schema file name from the input files
      if (0 == strcmp(argv[i], "-")) {
        if (i == argc - 2) {
          json_schema_name = mcopystr(argv[i + 1]);
        } else {
          ERROR("Expected JSON schema name (1 argument) after option `--ttcn2json' and `-'");
          errflag = true;
        }
        break;
      } 
      else if (0 == strcmp(argv[i], "-A")) {
        ++i;
        if (i == argc) {
          ERROR("Option `-A' must be followed by an ASN.1 file name");
          errflag = true;
          break;
        }
        add_module(n_modules, module_list, argv[i], Module::MOD_ASN);
        asn1_modules_present = true;
      }
      else if (0 == strcmp(argv[i], "-T")) {
        ++i;
        if (i == argc) {
          ERROR("Option `-T' must be followed by a TTCN-3 file name");
          errflag = true;
          break;
        }
        add_module(n_modules, module_list, argv[i], Module::MOD_TTCN);
      }
Elemer Lelik's avatar
Elemer Lelik committed
507
508
509
510
511
512
513
514
515
516
      else if (0 == strcmp(argv[i], "-j")) {
        implicit_json_encoding = FALSE;
      }
      else if (0 == strcmp(argv[i], "-f")) {
        json_refs_for_all_types = FALSE;
      }
      else if (0 == strcmp(argv[i], "-fj") || 0 == strcmp(argv[i], "-jf")) {
        implicit_json_encoding = FALSE;
        json_refs_for_all_types = FALSE;
      }
Elemer Lelik's avatar
Elemer Lelik committed
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
      else if (argv[i][0] == '-') {
        ERROR("Invalid option `%s' after option `--ttcn2json'", argv[i]);
        print_usage = true;
        errflag = true;
        break;
      }
      else {
        add_module(n_modules, module_list, argv[i], Module::MOD_UNKNOWN);
      }
    }
    
    if (!errflag && 0 == n_modules) {
      ERROR("No TTCN-3 or ASN.1 modules specified after option `--ttcn2json'");
      errflag = true;
      print_usage = true;
    }
    
    if (!errflag && NULL == json_schema_name) {
      // Create the schema name using the first TTCN-3 or ASN.1 file's name
      const module_struct& first = module_list[0];
      if (0 == strncmp(first.file_name + strlen(first.file_name) - 4, ".asn", 4)) {
        json_schema_name = mcopystrn(first.file_name, strlen(first.file_name) - 4);
        json_schema_name = mputstrn(json_schema_name, ".json", 5);
      }
      else if (0 == strncmp(first.file_name + strlen(first.file_name) - 5, ".ttcn", 5)) {
        json_schema_name = mcopystrn(first.file_name, strlen(first.file_name) - 5);
        json_schema_name = mputstrn(json_schema_name, ".json", 5);
      }
      else {
        json_schema_name = mprintf("%s.json", first.file_name);
      }
    }
  }

  if (!ttcn2json) {
    for ( ; ; ) {
Elemer Lelik's avatar
Elemer Lelik committed
553
      int c = getopt(argc, argv, "aA:bcC:dEfFgijK:lLMo:pP:qQ:rRsStT:uU:vV:wxXyYz:0-");
Elemer Lelik's avatar
Elemer Lelik committed
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
      if (c == -1) break;
      switch (c) {
      case 'a':
        SET_FLAG(a);
        asn1_xer = TRUE;
        break;
      case 'A':
        Aflag = true;
        add_module(n_modules, module_list, optarg, Module::MOD_ASN);
        asn1_modules_present = true;
        break;
      case 'C':
        SET_FLAG(C);
        expected_platform = optarg;
        break;
      case 'L':
        SET_FLAG(L);
        include_location_info = TRUE;
        break;
      case 'P':
        Pflag = true;
        nof_top_level_pdus++;
        top_level_pdu=(const char**)
          Realloc(top_level_pdu, nof_top_level_pdus*sizeof(*top_level_pdu));
        top_level_pdu[nof_top_level_pdus-1] = optarg;
        break;
      case 'T':
        Tflag = true;
        add_module(n_modules, module_list, optarg, Module::MOD_TTCN);
  #ifdef LICENSE
        ttcn3_modules_present = true;
  #endif
        break;
      case 'V':
        SET_FLAG(V);
        /* set verbosity level bitmask */
        if (isdigit(optarg[0])) {
    verb_level = atoi(optarg);
    // don't bother with overflow
    errno = 0;
        } else {
    ERROR("Option `-V' requires a decimal number as argument instead of "
      "`%s'.", optarg);
    errflag = true;
        }
        break;
      case 'b':
        SET_FLAG(b);
        ber_disabled = TRUE;
        break;
      case 'c':
        SET_FLAG(c);
        break;
      case 'd':
        SET_FLAG(d);
        default_as_optional = TRUE;
        break;
      case 'f':
        SET_FLAG(f);
        force_overwrite = TRUE;
        break;
      case 'g':
        SET_FLAG(g);
        gcc_compat = TRUE;
        break;
      case 'i':
        SET_FLAG(i);
        output_only_linenum = TRUE;
        break;
      case 'K':
        SET_FLAG(K);
        tcov_file_name = optarg;
        break;
      case 'l':
        SET_FLAG(l);
        include_line_info = TRUE;
        break;
      case 'o':
        SET_FLAG(o);
        output_dir = optarg;
        break;
      case 'Y':
        SET_FLAG(Y);
        enable_set_bound_out_param = TRUE;
        break;
      case 'p':
        SET_FLAG(p);
        parse_only = TRUE;
        break;
      case 'q':
        SET_FLAG(q);
        /* quiet; suppress all message */
        verb_level = 0;
        break;
      case 'r':
        SET_FLAG(r);
        raw_disabled = TRUE;
        break;
      case 'R':
        SET_FLAG(R);
        use_runtime_2 = TRUE;
        break;
      case 's':
        SET_FLAG(s);
        semantic_check_only = TRUE;
        break;
      case 'S':
        SET_FLAG(S);
        suppress_context = TRUE;
        break;
      case '0':
        SET_FLAG(s0);
        attribute_validation_disabled = TRUE;
        break;
      case 't':
        SET_FLAG(t);
        generate_skeleton = TRUE;
        break;
      case 'u':
        SET_FLAG(u);
        duplicate_underscores = TRUE;
        break;
      case 'U':
        SET_FLAG(U);
        if (!cgh.set_split_mode(optarg)) {
          ERROR("Wrong code splitting option: '%s'. Valid values are: 'none', "
            "'type'.", optarg);
          errflag = true;
        }
        break;
      case 'v':
        SET_FLAG(v);
        break;
      case 'w':
        SET_FLAG(w);
        /* suppress warnings and "not supported" messages */
        verb_level &= ~(1|2);
        break;
      case 'x':
        SET_FLAG(x);
        text_disabled = TRUE;
        break;
      case 'X':
        SET_FLAG(X);
        xer_disabled = TRUE;
        break;
      case 'j':
        SET_FLAG(j);
        json_disabled = TRUE;
        break;
      case 'y':
        SET_FLAG(y);
        check_subtype = FALSE;
        break;
Elemer Lelik's avatar
Elemer Lelik committed
708
709
      case 'z':
        SET_FLAG(z);
Elemer Lelik's avatar
Elemer Lelik committed
710
711
712
713
714
        profiler_file_name = optarg;
        break;
      case 'F':
        SET_FLAG(F);
        force_gen_seof = TRUE;
Elemer Lelik's avatar
Elemer Lelik committed
715
        break;
Elemer Lelik's avatar
Elemer Lelik committed
716
717
718
719
      case 'M':
        SET_FLAG(M);
        omit_in_value_list = TRUE;
        break;
Elemer Lelik's avatar
Elemer Lelik committed
720
721
722
723
      case 'E':
        SET_FLAG(E);
        warnings_for_bad_variants = TRUE;
        break;
Elemer Lelik's avatar
Elemer Lelik committed
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746

      case 'Q': {
        long max_errs;
        SET_FLAG(Q);
        printf("Q: %s\n", optarg);

        errno = 0;
        max_errs = strtol(optarg, (char**)NULL, 10);
        if (errno != 0
          || (long)(int)max_errs != max_errs) { // does not fit into int
          ERROR("Invalid value %s: %s", optarg, strerror(errno));
          errflag = true;
        }
        else if (max_errs < 0) {
          ERROR("Negative value %s not allowed", optarg);
          errflag = true;
        }
        else { // all good
          if (max_errs == 0) max_errs = 1;
        }

        Error_Context::set_max_errors(max_errs);
        break; }
Elemer Lelik's avatar
Elemer Lelik committed
747
748

      case '-':
Elemer Lelik's avatar
Elemer Lelik committed
749
        if (!strcmp(argv[optind], "--ttcn2json")) {
Elemer Lelik's avatar
Elemer Lelik committed
750
          ERROR("Option `--ttcn2json' is only allowed as the first option");
Elemer Lelik's avatar
Elemer Lelik committed
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
        } else {
          ERROR("Invalid option: `%s'", argv[optind]);
        }
        // no break

      default:
        errflag = true;
        print_usage = true;
        break;
      }
    }

    /* Checking incompatible options */
    if (vflag) {
      if (Aflag || Lflag || Pflag || Tflag || Vflag || Yflag ||
        bflag || fflag || iflag || lflag || oflag || pflag || qflag ||
        rflag || sflag || tflag || uflag || wflag || xflag || Xflag || Rflag ||
Elemer Lelik's avatar
Elemer Lelik committed
768
        Uflag || yflag || Kflag || jflag || zflag || Fflag || Mflag || Eflag) {
Elemer Lelik's avatar
Elemer Lelik committed
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
        errflag = true;
        print_usage = true;
      }
    } else {
      if (pflag) {
        if (sflag) {
    ERROR("Options `-p' and `-s' are incompatible with each other.");
    // totally confusing: exit immediately
    errflag = true;
        }
      }
      if (Kflag && !Lflag) {
        ERROR("Source line information `-L' is necessary for code coverage `-K'.");
        errflag = true;
      }
Elemer Lelik's avatar
Elemer Lelik committed
784
785
786
787
      if (zflag && !Lflag) {
        ERROR("Source line information `-L' is necessary for profiling `-z'.");
        errflag = true;
      }
Elemer Lelik's avatar
Elemer Lelik committed
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
      if (iflag && gflag) {
        WARNING("Option `-g' overrides `-i'.");
        iflag = false; // -g gives more information
      }
      if (oflag && get_path_status(output_dir) != PS_DIRECTORY) {
        ERROR("The argument of -o switch (`%s') must be a directory.",
      output_dir);
        errflag = true;
      }
      if (optind == argc && n_modules == 0) {
        ERROR("No input TTCN-3 or ASN.1 module was given.");
        errflag = true;
      }
    }
  } // if (!ttcn2json)
  
  if (errflag) {
    if (print_usage) usage();
    Free(json_schema_name);
    return EXIT_FAILURE;
  }

  if (vflag) {
    fputs("TTCN-3 and ASN.1 Compiler for the TTCN-3 Test Executor\n"
	  "Product number: " PRODUCT_NUMBER "\n"
	  "Build date: " __DATE__ " " __TIME__ "\n"
	  "Compiled with: " C_COMPILER_VERSION "\n\n"
	  COPYRIGHT_STRING "\n\n", stderr);
#ifdef LICENSE
    print_license_info();
    fputs("\nUsing ", stderr);
    fputs(openssl_version_str(), stderr);
    fputs("\n\n", stderr);
#endif
    return EXIT_SUCCESS;
  }

#ifdef LICENSE
  init_openssl();
  license_struct lstr;
  load_license(&lstr);
  int license_valid = verify_license(&lstr);
  free_openssl();
  if (!license_valid) {
    free_license(&lstr);
    exit(EXIT_FAILURE);
  }
#endif

  if (!ttcn2json) {
    /* the position of '-' switch in argv list */
    int dash_position = -1;

    /* Add the remaining files until switch '-' to the module_list */
    for(int i = optind; i < argc; i++) {
      if (strcmp(argv[i], "-"))
        add_module(n_modules, module_list, argv[i], Module::MOD_UNKNOWN);
      else {
        dash_position = i;
        break;
      }
    }

    if (dash_position == -1) {
      /** if '-' was not present in the command line code should be generated for
       * all modules */
      for (size_t i = 0; i < n_modules; i++) module_list[i].need_codegen = true;
    } else {
      for (int i = dash_position + 1; i < argc; i++) {
        char *absolute_path = canonize_input_file(argv[i]);
        if (absolute_path == NULL) continue;
        bool found = false;
        for (size_t j = 0; j < n_modules; j++) {
    module_struct *module = module_list + j;
    if (!strcmp(module->absolute_path, absolute_path)) {
      module->need_codegen = true;
      found = true;
      // do not stop: the same file may be present on the list twice
      // (as both ASN.1 and TTCN-3 module)
    }
        }
        Free(absolute_path);
        if (!found) {
          ERROR("File `%s' was not given before the `-' switch for selective "
                "code generation.", argv[i]);
          // go further (i.e. check all files after the `-')
        }
      }
    }
  } // if (!ttcn2json)

  {
  STOPWATCH("Determining module types");
  // check the readability of all files and
  // determine the type of unknown modules
  for (size_t i = 0; i < n_modules; i++) {
    module_struct *module = module_list + i;
    FILE *fp = fopen(module->file_name, "r");
    if (fp != NULL) {
      if (module->module_type == Module::MOD_UNKNOWN) {
        // try the ASN.1 and TTCN-3 preparsers
  boolean asn1_module = is_asn1_module(module->file_name, fp, NULL);
  boolean ttcn3_module = is_ttcn3_module(module->file_name, fp, NULL);
  if (asn1_module) {
    if (!is_valid_asn1_filename (module->file_name)) {
      ERROR("The file name (without suffix) shall be identical to the module name.\n"
            "If the name of the ASN.1 module contains a hyphen, the corresponding "
            "file name shall contain an underscore character instead.");
    }
    if (ttcn3_module) {
            ERROR("File `%s' looks so strange that it can contain both an "
        "ASN.1 and a TTCN-3 module. Use the command-line switch `-A' or "
        "`-T' to set its type.", module->file_name);
    } else {
      bool found = false;
      for (size_t j = 0; j < n_modules; j++) {
        module_struct *module2 = module_list + j;
        if (module2->module_type == Module::MOD_ASN &&
      !strcmp(module->absolute_path, module2->absolute_path)) {
     found = true;
     break;
        }
      }
      if (found) {
        ERROR("Input file `%s' was given more than once.",
    module->file_name);
      } else {
        module->module_type = Module::MOD_ASN;
        asn1_modules_present = true;
      }
    }
  } else if (ttcn3_module) {
    bool found = false;
    for (size_t j = 0; j < n_modules; j++) {
      module_struct *module2 = module_list + j;
      if (module2->module_type == Module::MOD_TTCN &&
    !strcmp(module->absolute_path, module2->absolute_path)) {
         found = true;
         break;
      }
    }
    if (found) {
      ERROR("Input file `%s' was given more than once.",
        module->file_name);
    } else {
            module->module_type = Module::MOD_TTCN;
#ifdef LICENSE
      ttcn3_modules_present = true;
#endif
    }
  } else {
    ERROR("Cannot recognize file `%s' as an ASN.1 or TTCN-3 module. "
      "Use the command-line switch `-A' or `-T' to set its type.",
      module->file_name);
  }
      }
      fclose(fp);
    } else {
      ERROR("Cannot open input file `%s' for reading: %s", module->file_name,
  strerror(errno));
      errno = 0;
      // do not invoke the real parsers on that file
      module->module_type = Module::MOD_UNKNOWN;
    }
  }
  }

#if defined(MINGW)
  if (!semantic_check_only) {
    NOTIFY("On native win32 builds code generation is disabled.");
    semantic_check_only = TRUE;
  }
#endif
#ifdef LICENSE
  /* Checking of required license features */
  if (asn1_modules_present && !check_feature(&lstr, FEATURE_ASN1)) {
    ERROR("The license key does not allow the parsing of "
          "ASN.1 modules.");
    return EXIT_FAILURE;
  }
  if (ttcn3_modules_present && !check_feature(&lstr, FEATURE_TTCN3)) {
    ERROR("The license key does not allow the parsing of "
          "TTCN-3 modules.");
    return EXIT_FAILURE;
  }
  if (!parse_only && !semantic_check_only &&
      !check_feature(&lstr, FEATURE_CODEGEN)) {
    WARNING("The license key does not allow the generation of "
              "C++ code.");
    semantic_check_only = TRUE;
  }
  if (generate_skeleton && !check_feature(&lstr, FEATURE_TPGEN)) {
    WARNING("The license key does not allow the generation of "
              "Test Port skeletons.");
    generate_skeleton = FALSE;
  }
  has_raw_feature = check_feature(&lstr, FEATURE_RAW);
  has_ber_feature = check_feature(&lstr, FEATURE_BER);
  has_per_feature = check_feature(&lstr, FEATURE_PER);
  has_text_feature = check_feature(&lstr, FEATURE_TEXT);
  has_xer_feature = check_feature(&lstr, FEATURE_XER);
  free_license(&lstr);
#endif
Elemer Lelik's avatar
Elemer Lelik committed
991
  if (Kflag && !check_file_list(tcov_file_name, module_list, n_modules, tcov_files)) {
Elemer Lelik's avatar
Elemer Lelik committed
992
993
994
995
	ERROR("Error while processing `%s' provided for code coverage data "
	      "generation.", tcov_file_name);
	return EXIT_FAILURE;
  }
Elemer Lelik's avatar
Elemer Lelik committed
996
997
998
999
1000
1001
1002
1003
1004
  if (zflag) {
    tcov_file_list *file_list_head = NULL;
    if(!check_file_list(profiler_file_name, module_list, n_modules, file_list_head)) {
      ERROR("Error while processing `%s' provided for profiling and code coverage.",
        profiler_file_name);
      return EXIT_FAILURE;
    }
    init_profiler_data(file_list_head);
  }
Elemer Lelik's avatar
Elemer Lelik committed
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
  {
    STOPWATCH("Parsing modules");

    // asn1_yydebug=1;
    if (asn1_modules_present) asn1_init();
    modules = new Common::Modules();

    for (size_t i = 0; i < n_modules; i++) {
      const module_struct *module = module_list + i;
      switch (module->module_type) {
      case Module::MOD_ASN:
        asn1_parse_file(module->file_name, module->need_codegen);
        break;
      case Module::MOD_TTCN:
        ttcn3_parse_file(module->file_name, module->need_codegen);
        break;
      default: // MOD_UNKNOWN ?
        break;
      }
    }

    for (size_t i = 0; i < n_modules; i++) Free(module_list[i].absolute_path);
    Free(module_list);
  }

  if (!parse_only && 0 == Error_Context::get_error_count()) {
    NOTIFY("Checking modules...");
    {
      STOPWATCH("Semantic check");
      modules->chk();
    }
  }

  if (verb_level > 7) modules->dump();

  int ret_val = EXIT_SUCCESS;
  unsigned int error_count = Error_Context::get_error_count();
  if (error_count > 0) ret_val = EXIT_FAILURE;

  if (parse_only || semantic_check_only) {
    // print detailed statistics
    Error_Context::print_error_statistics();
  } else {
    if (error_count == 0) {
#ifdef USAGE_STATS
      {
        // Ignore SIGPIPE signals
        struct sigaction sig_act;
        if (sigaction(SIGPIPE, NULL, &sig_act))
          ERROR("System call sigaction() failed when getting signal "
            "handling information for %s.", "SIGINT");
        sig_act.sa_handler = SIG_IGN;
        sig_act.sa_flags = 0;
        if (sigaction(SIGPIPE, &sig_act, NULL))
          ERROR("System call sigaction() failed when disabling signal "
            "%s.", "SIGINT");
      }

      std::stringstream stream;
      stream << "compiler";
      std::set<ModuleVersion> versions = modules->getVersionsWithProductNumber();
      if (!versions.empty()) {
        stream << "&products=";
        for (std::set<ModuleVersion>::iterator it = versions.begin(); it != versions.end(); ++it) {
          if (it != versions.begin()) {
            stream << ",";
          }
          stream << it->toString();
        }
      }

      HttpSender *sender = new HttpSender;
      UsageData::getInstance().sendDataThreaded(stream.str(), sender);
#endif
      if (ttcn2json) {
        NOTIFY("Generating JSON schema...");
        STOPWATCH("Generating JSON schema");
        // the Ttcn2Json constructor starts the process
        Ttcn::Ttcn2Json t2j(modules, json_schema_name);
      } else {
        NOTIFY("Generating code...");
        STOPWATCH("Generating code");
        modules->generate_code(cgh);
        report_nof_updated_files();
      }
    } else {
      NOTIFY("Error%s found in the input module%s. Code will not be generated.",
	error_count > 1 ? "s" : "", n_modules > 1 ? "s" : "");
      if (cflag) {
        modules->write_checksums();
      }
    }
  }

  if (Kflag) {
    while (tcov_files != NULL) {
	  tcov_file_list *next_file = tcov_files->next;
	  Free(tcov_files->file_name);
Elemer Lelik's avatar
Elemer Lelik committed
1103
	  Free(tcov_files);
Elemer Lelik's avatar
Elemer Lelik committed
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
	  tcov_files = next_file;
    }
    tcov_files = NULL;
  }
  delete modules;
  Free(top_level_pdu);
  if (asn1_modules_present) asn1_free();
  Type::free_pools();
  Common::Node::chk_counter();
  Location::delete_source_file_names();
  Free(json_schema_name);
Elemer Lelik's avatar
Elemer Lelik committed
1115
1116
1117
  if (zflag) {
    free_profiler_data();
  }
Elemer Lelik's avatar
Elemer Lelik committed
1118
1119
1120
1121
1122

  // dbgnew.hh already does it: check_mem_leak(argv[0]);

  return ret_val;
}