File: logformat.l

package info (click to toggle)
eclipse-titan 7.2.0-1.1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 103,144 kB
  • sloc: cpp: 264,784; ansic: 33,124; yacc: 23,073; makefile: 14,730; lex: 9,190; java: 4,849; perl: 3,783; sh: 2,298; xml: 1,378; javascript: 85; awk: 48; php: 32; python: 13
file content (555 lines) | stat: -rw-r--r-- 13,907 bytes parent folder | download
1
2
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
228
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
293
294
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
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
507
508
509
510
511
512
513
514
515
516
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
553
554
555
/******************************************************************************
 * Copyright (c) 2000-2020 Ericsson Telecom AB
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
 *
 * Contributors:
 *   Balasko, Jeno
 *   Beres, Szabolcs
 *   Lovassy, Arpad
 *   Szabados, Kristof
 *   Szabo, Bence Janos
 *   Szabo, Janos Zoltan – initial implementation
 *
 ******************************************************************************/
%option noyywrap
%option nounput
%{

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#include "../common/memory.h"
#include "../common/version_internal.h"

#ifdef LICENSE
#include "../common/license.h"
#endif

static const char *program_name = NULL;
static size_t indent_depth = 4;
static FILE *output_file = NULL;
static int separate_files = 0;
static int format_tab_newline = 1;
static size_t replaced_tab_newline = 0;

static size_t indent_level = 0;
static enum { OPEN_BRACE, CLOSE_BRACE, COMMA, OTHER, OTHER_WS }
  last_token = OTHER;

static char *line_buf = NULL;
static size_t buf_size = 0, buf_len = 0;
#define MIN_BUFSIZE 1024

static void init_line_buf(void)
{
  line_buf = (char *)Malloc(MIN_BUFSIZE);
  buf_size = MIN_BUFSIZE;
  buf_len = 0;
}

static void enlarge_line_buf(size_t size_incr)
{
  size_t new_buf_size = buf_size;
  while (buf_len + size_incr > new_buf_size) new_buf_size *= 2;
  if (new_buf_size > buf_size) {
    line_buf = (char *)Realloc(line_buf, new_buf_size);
    buf_size = new_buf_size;
  }
}

static void free_line_buf(void)
{
  Free(line_buf);
  line_buf = NULL;
  buf_size = 0;
  buf_len = 0;
}

static void append_str(size_t chunk_size, const char *chunk_ptr)
{
  enlarge_line_buf(chunk_size);
  memcpy(line_buf + buf_len, chunk_ptr, chunk_size);
  buf_len += chunk_size;
}

static void append_char(char c)
{
  if (buf_size <= buf_len) {
    buf_size *= 2;
    line_buf = (char *)Realloc(line_buf, buf_size);
  }
  line_buf[buf_len++] = c;
}

static void append_indentation(void)
{
  if (indent_depth > 0) {
    append_char('\n');
    if (indent_level > 0) {
      size_t nof_spaces = indent_level * indent_depth;
      enlarge_line_buf(nof_spaces);
      memset(line_buf + buf_len, ' ', nof_spaces);
      buf_len += nof_spaces;
    }
  } else {
    /* no indentation is made */
    append_char(' ');
  }
}

static void indent_before_other_token(void)
{
  switch (last_token) {
  case OPEN_BRACE:
  case COMMA:
    /* start a new line after an opening brace or comma */
    append_indentation();
    break;
  case CLOSE_BRACE:
  case OTHER_WS:
    /* add a single space as separator between the previous token or block */
    append_char(' ');
  default:
    break;
  }
  last_token = OTHER;
}

static void write_failure(void)
{
  fprintf(stderr, "%s: writing to output file failed: %s\n",
    program_name, strerror(errno));
  exit(EXIT_FAILURE);
}

//Solaris does not have strndup
char * my_strndup(const char *string, const size_t len){
  char* dup = (char*)malloc(len + 1);
  strncpy(dup, string, len);
  dup[len] = '\0';
  return dup;
}

char *
str_replace ( const char *string, const size_t len, const char *substr, const char *replacement ){
  char *tok = NULL;
  char *newstr = NULL;
  char *oldstr = NULL;
  char *head = NULL;
  size_t length_diff = strlen(substr) - strlen(replacement);
 
  /* if either substr or replacement is NULL, duplicate string a let caller handle it */
  if ( substr == NULL || replacement == NULL ) return my_strndup (string, len);
  newstr = my_strndup (string, len);
  head = newstr;
  while ( (tok = strstr ( head, substr ))){
    oldstr = newstr;
    newstr = malloc ( strlen ( oldstr ) - strlen ( substr ) + strlen ( replacement ) + 1 );
    /*failed to alloc mem, free old string and return NULL */
    if ( newstr == NULL ){
      free (oldstr);
      fprintf(stderr, "Failed to allocate memory.\n");
      exit(EXIT_FAILURE);
    }
    //We have to count how many characters we replaced
    replaced_tab_newline += length_diff;
    memcpy ( newstr, oldstr, tok - oldstr );
    memcpy ( newstr + (tok - oldstr), replacement, strlen ( replacement ) );
    memcpy ( newstr + (tok - oldstr) + strlen( replacement ), tok + strlen ( substr ), strlen ( oldstr ) - strlen ( substr ) - ( tok - oldstr ) );
    memset ( newstr + strlen ( oldstr ) - strlen ( substr ) + strlen ( replacement ) , 0, 1 );
    /* move back head right after the last replacement */
    head = newstr + (tok - oldstr) + strlen( replacement );
    free (oldstr);
  }
  return newstr;
}


static void write_line_buf(void)
{
  if (buf_len > 0) {
    if(format_tab_newline){
      char * temp = str_replace(line_buf, buf_len, "\\n", "\n");
      temp = str_replace(temp, buf_len-replaced_tab_newline, "\\t", "\t");
      strcpy(line_buf, temp);
      free(temp);
    }
    if (fwrite(line_buf, buf_len-replaced_tab_newline, 1, yyout) != 1) write_failure();
    /* append a newline character if it is missing from the end
     * (e.g. because of EOF) */
    if (line_buf[buf_len - replaced_tab_newline - 1] != '\n'){
      if (putc('\n', yyout) == EOF) write_failure();
    }
    replaced_tab_newline = 0;
  }
  if (buf_size > MIN_BUFSIZE && buf_size > 2 * buf_len) {
    /* reset the buffer size if a shorter one is enough */
    Free(line_buf);
    line_buf = (char *)Malloc(MIN_BUFSIZE);
    buf_size = MIN_BUFSIZE;
  }
  buf_len = 0;
  indent_level = 0;
  last_token = OTHER;
}

static FILE *open_file(const char *path, const char *mode)
{
  FILE *fp = fopen(path, mode);
  if (fp == NULL) {
    fprintf(stderr, "%s: cannot open file %s for %s: %s\n",
      program_name, path, mode[0] == 'r' ? "reading" : "writing",
      strerror(errno));
    exit(EXIT_FAILURE);
  }
  return fp;
}

static int in_testcase = 0;
static size_t n_testcases = 0;
static char **testcases = NULL;

static void begin_testcase(const char *testcase_name)
{
  if (separate_files) {
    size_t i;
    if (in_testcase) fclose(yyout);
    else in_testcase = 1;
    for (i = 0; i < n_testcases; i++) {
      if (!strcmp(testcase_name, testcases[i])) {
	yyout = open_file(testcase_name, "a");
	return;
      }
    }
    n_testcases++;
    testcases = (char **)Realloc(testcases, (n_testcases + 1) *
	sizeof(*testcases));
    testcases[n_testcases - 1] = mcopystr(testcase_name);
    yyout = open_file(testcase_name, "w");
  }
}

static void free_testcases(void)
{
  size_t i;
  for (i = 0; i < n_testcases; i++) Free(testcases[i]);
  Free(testcases);
  n_testcases = 0;
  testcases = NULL;
}

static void end_testcase(void)
{
  if (in_testcase) {
    fclose(yyout);
    yyout = output_file;
    in_testcase = 0;
  }
}

%}

WHITESPACE [ \t]
NEWLINE \r|\n|\r\n

NUMBER 0|([1-9][0-9]*)

IDENTIFIER [a-zA-Z][a-zA-Z0-9_]*

VERDICT none|pass|inconc|fail|error

YEAR [0-9]{4}
MONTH Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec
DATE [0-2][0-9]|3[01]
HOUR [01][0-9]|2[0-3]
MIN [0-5][0-9]
SEC [0-5][0-9]
MICROSEC [0-9]{6}

TIMESTAMP1 {NUMBER}\.{MICROSEC}
TIMESTAMP2 {HOUR}\:{MIN}\:{SEC}\.{MICROSEC}
TIMESTAMP3 {YEAR}\/{MONTH}\/{DATE}" "{TIMESTAMP2}

TIMESTAMP {TIMESTAMP1}|{TIMESTAMP2}|{TIMESTAMP3}

%x SC_cstring SC_binstring SC_quadruple

%%
  /* start parsing of a new file from the initial state */
  BEGIN(INITIAL);

<*>^{TIMESTAMP} {
  /* recognize the start of a new entry in all states in order to recover from
   * unterminated strings */
  write_line_buf();
  append_str(yyleng, yytext);
  BEGIN(INITIAL);
}

"Test case "{IDENTIFIER}" started."{NEWLINE} {
  size_t i;
  /* cut the newline character(s) from the end */
  while (yytext[yyleng - 1] == '\r' || yytext[yyleng - 1] == '\n') yyleng--;
  append_str(yyleng, yytext);
  /* append a UNIX-style newline character */
  append_char('\n');
  /* find the end of testcase identifier */
  for (i = 10; yytext[i] != ' '; i++);
  yytext[i] = '\0';
  begin_testcase(yytext + 10);
  write_line_buf();
}

"Test case "{IDENTIFIER}" finished. Verdict: "{VERDICT}{NEWLINE} {
  /* cut the newline character(s) from the end */
  while (yytext[yyleng - 1] == '\r' || yytext[yyleng - 1] == '\n') yyleng--;
  append_str(yyleng, yytext);
  /* append a UNIX-style newline character */
  append_char('\n');
  write_line_buf();
  end_testcase();
}

{WHITESPACE}+ {
  if (indent_level > 0) {
    /* only record the presence of the whitespace in indented blocks */
    if (last_token == OTHER) last_token = OTHER_WS;
  } else {
    /* copy to output transparently */
    append_str(yyleng, yytext);
  }
}

{NEWLINE} {
  if (indent_level > 0) {
    /* only record the presence of the newline in indented blocks */
    if (last_token == OTHER) last_token = OTHER_WS;
  } else {
    /* canonize the newline character to UNIX style */
    append_char('\n');
  }
}

\{ {
  if (indent_level > 0) {
    switch (last_token) {
    case OPEN_BRACE:
    case COMMA:
      /* start a new line for the nested block */
      append_indentation();
      break;
    default:
      /* separate the brace from the previous token */
      append_char(' ');
    }
  }
  append_char('{');
  indent_level++;
  last_token = OPEN_BRACE;
}

\} {
  if (indent_level > 0) {
    indent_level--;
    if (last_token == OPEN_BRACE) {
      /* add a space to an empty block */
      append_char(' ');
    } else {
      /* start a newline for the closing brace */
      append_indentation();
    }
    last_token = CLOSE_BRACE;
  }
  append_char('}');
}

, {
  append_char(',');
  if (indent_level > 0) last_token = COMMA;
}

\" {
  if (indent_level > 0) indent_before_other_token();
  append_char('"');
  BEGIN(SC_cstring);
}

\' {
  if (indent_level > 0) indent_before_other_token();
  append_char('\'');
  BEGIN(SC_binstring);
}

"char"{WHITESPACE}*\( {
  if (indent_level > 0) indent_before_other_token();
  /* convert whitespaces to canonical form */
  append_str(5, "char(");
  BEGIN(SC_quadruple);
}

. {
  if (indent_level > 0) indent_before_other_token();
  append_char(yytext[0]);
}

<SC_cstring>
{
\"\" append_str(2, "\"\""); /* escaped quotation mark */
\" {
  /* end of the string */
  append_char('"');
  BEGIN(INITIAL);
}
}

<SC_binstring>\' {
  /* end of the string */
  append_char('\'');
  BEGIN(INITIAL);
}

<SC_cstring,SC_binstring>
{
\\{NEWLINE} append_str(2, "\\\n"); /* canonize escaped newline characters */
{NEWLINE} append_char('\n'); /* canonize simple newline characters */
\\. append_str(yyleng, yytext); /* copy escaped characters */
. append_char(yytext[0]); /* copy simple characters */
}

<SC_quadruple>
{
\) {
  append_char(')');
  BEGIN(INITIAL);
}
({WHITESPACE}|{NEWLINE})+ /* ignore all whitespaces and newlines */
, append_str(2, ", "); /* append a space after the comma */
. append_char(yytext[0]); /* copy simple characters */
}

%%

static void usage(void)
{
  fprintf(stderr,
    "usage: %s [-i n] [-o outfile] [-s] [-n] [file.log] ...\n"
    "   or  %s -v\n"
    "\n"
    "OPTIONS:\n"
    "        -i n:           set the depth of each indentation to n "
      "characters\n"
    "        -o outfile:     write the formatted log into file outfile\n"
    "        -s:             place the logs of each test case into separate "
      "files\n"
    "        -n              newline and tab control characters are not modified\n"
    "        -v:             print version\n",
    program_name, program_name);
}

int main(int argc, char *argv[])
{
    int c;
    int iflag = 0, oflag = 0, sflag = 0, vflag = 0, errflag = 0, nflag = 0;
#ifdef LICENSE
    license_struct lstr;
#endif
    program_name = argv[0];
    output_file = stdout;
    if(argc == 1){
       errflag = 1;
    }
    while ((c = getopt(argc, argv, "i:o:snv")) != -1 && !errflag) {
	switch (c) {
	case 'i': {
	    unsigned int int_arg;
	    if (iflag || vflag) errflag = 1;
	    if (sscanf(optarg, "%u", &int_arg) != 1) {
		fprintf(stderr, "%s: invalid indentation depth: %s\n",
		    program_name, optarg);
		return EXIT_FAILURE;
	    }
	    indent_depth = int_arg;
	    iflag = 1;
	    break; }
	case 'o':
	    if (oflag || vflag) errflag = 1;
	    else output_file = open_file(optarg, "w");
	    oflag = 1;
	    break;
	case 's':
	    if (sflag || vflag) errflag = 1;
	    sflag = 1;
	    break;
        case 'n':
            if(vflag) errflag = 1;
            format_tab_newline = 0;
            nflag = 1;
            break;
	case 'v':
	    if (iflag || oflag || sflag || nflag) errflag = 1;
	    vflag = 1;
	    break;
	default:
	    errflag = 1;
	}
    }
    if (errflag) {
        usage();
        return EXIT_FAILURE;
    } else if (vflag) {
        fputs("Log Formatter for the TTCN-3 Test Executor\n"
	    "Product number: " PRODUCT_NUMBER "\n"
	    "Version: " VERSION_STRING "\n"
	    "Build date: " __DATE__ " " __TIME__ "\n"
	    "Compiled with: " C_COMPILER_VERSION "\n\n"
	    COPYRIGHT_STRING "\n\n", stderr);
#ifdef LICENSE
	print_license_info();
#endif
	return EXIT_SUCCESS;
    }
#ifdef LICENSE
    init_openssl();
    load_license(&lstr);
    if (!verify_license(&lstr)) {
      free_license(&lstr);
      free_openssl();
      exit(EXIT_FAILURE);
    }
    if (!check_feature(&lstr, FEATURE_LOGFORMAT)) {
	fputs("The license key does not allow the formatting of log files.\n",
	    stderr);
	return EXIT_FAILURE;
    }
    free_license(&lstr);
    free_openssl();
#endif
    separate_files = sflag;
    yyout = output_file;
    init_line_buf();
    if (optind >= argc) {
	yyin = stdin;
	yylex();
	write_line_buf();
	end_testcase();
    } else {
	for ( ; optind < argc; optind++) {
	    yyin = open_file(argv[optind], "r");
	    yylex();
	    fclose(yyin);
	    write_line_buf();
	    end_testcase();
	}
    }
    free_line_buf();
    if (oflag) fclose(output_file);
    free_testcases();
    check_mem_leak(program_name);
    return EXIT_SUCCESS;
}