summaryrefslogtreecommitdiffstats
path: root/plugins/epan/mate/mate_grammar.lemon
blob: fef0ac27e7a7f32b3fe02d42d5006453cf1808a6 (plain)
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
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
%include {

/* mate_grammar.lemon
 * MATE's configuration language grammar
 *
 * Copyright 2005, Luis E. Garcia Ontanon <luis@ontanon.org>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

/*
 * XXX - there's a Lemon bug where this grammar produces a parser that
 * fails assertions; to work around it, we disable assert() failures.
 */
#ifndef NDEBUG
#define NDEBUG
#endif
#include "config.h"

#include <errno.h>

#include "mate.h"
#include "mate_grammar.h"
#include <wsutil/file_util.h>
#include <wsutil/str_util.h>

#define DUMMY void*

typedef struct _extraction {
	char* as;
	header_field_info* hfi;
	struct _extraction* next;
	struct _extraction* last;
} extraction_t;

typedef struct _pdu_criteria_t {
	AVPL* criterium_avpl;
	avpl_match_mode criterium_match_mode;
	accept_mode_t criterium_accept_mode;
} pdu_criteria_t;

typedef struct _gop_options {
	gop_tree_mode_t pdu_tree_mode;
	bool drop_unassigned;
	bool show_times;
	double expiration;
	double idle_timeout;
	double lifetime;
	AVPL* start;
	AVPL* stop;
	AVPL* extras;
} gop_options_t;

typedef struct _gog_statements {
	double expiration;
	gop_tree_mode_t gop_tree_mode;
	GPtrArray* transform_list;
	AVPL* extras;
	LoAL* current_gogkeys;
} gog_statement_t;

typedef struct _transf_match_t {
	avpl_match_mode match_mode;
	AVPL* avpl;
} transf_match_t;

typedef struct _transf_action_t {
	avpl_replace_mode replace_mode;
	AVPL* avpl;
} transf_action_t;

static void configuration_error(mate_config* mc, const char* fmt, ...) {
	static char error_buffer[256];
	const char* incl;
	int i;
	mate_config_frame* current_frame;
	va_list list;

	va_start( list, fmt );
	vsnprintf(error_buffer,sizeof(error_buffer),fmt,list);
	va_end( list );

	i = (int) mc->config_stack->len;

	while (i--) {

		if (i>0) {
			incl = "\n   included from: ";
		} else {
			incl = " ";
		}

		current_frame = (mate_config_frame *)g_ptr_array_index(mc->config_stack,(unsigned)i);

		g_string_append_printf(mc->config_error,"%s%s at line %u",incl, current_frame->filename, current_frame->linenum);
	}

	g_string_append_printf(mc->config_error,": %s\n",error_buffer);

	THROW(MateConfigError);

}

static AVPL_Transf* new_transform_elem(AVPL* match, AVPL* replace, avpl_match_mode match_mode, avpl_replace_mode replace_mode) {
	 AVPL_Transf* t = (AVPL_Transf *)g_malloc(sizeof(AVPL_Transf));

	 t->name = NULL;
	 t->match = match;
	 t->replace = replace;
	 t->match_mode = match_mode;
	 t->replace_mode = replace_mode;

	 t->map = NULL;
	 t->next = NULL;

	 return t;
}

static char* recolonize(mate_config* mc, char* s) {
	GString* str = g_string_new("");
	char** vec;
	char* r;
	unsigned i,v;
	char c;

	vec = g_strsplit(s,":",0);

	for (i = 0; vec[i]; i++) {
		ascii_strdown_inplace(vec[i]);

		v = 0;
		switch ( strlen(vec[i]) ) {
		 case 2:
			c = vec[i][1];
			vec[i][1] = vec[i][0];
			vec[i][0] = c;
			if (vec[i][0] >= '0' && vec[i][0] <= '9') {
				v += (vec[i][1] - '0' )*16;
			} else {
				v += (vec[i][1] - 'a' + 10)*16;
			}
		/* FALL THROUGH */
		 case 1:
			if (vec[i][0] >= '0' && vec[i][0] <= '9') {
				v += (vec[i][0] - '0' );
			} else {
				v += (vec[i][0] - 'a' + 10);
			}
		 case 0:
			break;
		 default:
			configuration_error(mc,"bad token %s",s);
		}

		g_string_append_printf(str,":%.2X",v);
	}

	g_strfreev(vec);

	g_string_erase(str,0,1);

	r = str->str;

	g_string_free(str,FALSE);

	return r;
}

DIAG_OFF_LEMON()
} /* end of %include */

%code {
DIAG_ON_LEMON()
}

%name MateParser

%token_prefix TOKEN_

%token_type { char* }
%token_destructor {
	(void) mc; /* Mark unused, similar to Q_UNUSED */
	g_free($$);
}

%extra_argument { mate_config* mc }

%syntax_error {
	if(yyminor == NULL) {
		configuration_error(mc,"Syntax error detected before end of file.");
	} else {
		configuration_error(mc,"Syntax error at or before \"%s\"",yyminor);
	}
}

%parse_failure {
	configuration_error(mc,"Parse error");
}

%type   transform_decl  { AVPL_Transf* }
%type   transform_body { AVPL_Transf* }
%type   transform_statements { AVPL_Transf* }
%type   transform_statement { AVPL_Transf* }
%type   transform_match { transf_match_t* }
%type   transform_action { transf_action_t* }
%type   match_mode { avpl_match_mode }
%type   action_mode { avpl_replace_mode }

%type gop_name { char* }
%type time_value { double }
%type pdu_name { char* }
%type gop_tree_mode { gop_tree_mode_t }
%type true_false { bool }

%type criteria_statement { pdu_criteria_t* }
%type accept_mode { accept_mode_t }
%type pdu_drop_unassigned_statement { bool }
%type discard_pdu_data_statement { bool }
%type last_extracted_statement { bool }

%type extraction_statement {extraction_t*}
%type extraction_statements {extraction_t*}

%type gop_options { gop_options_t* }

%type gop_start_statement { AVPL* }
%type gop_stop_statement { AVPL* }
%type extra_statement { AVPL* }
%type gop_drop_unassigned_statement { bool }
%type show_goptree_statement { gop_tree_mode_t }
%type show_times_statement { bool }
%type gop_expiration_statement { double }
%type idle_timeout_statement { double }
%type lifetime_statement { double }

%type gog_statements { gog_statement_t* }
%type gog_expiration_statement { double }
%type gog_goptree_statement { gop_tree_mode_t }
%type gog_key_statements { LoAL* }
%type gog_key_statement { AVPL* }
%type transform_list_statement { GPtrArray* }
%type transform { AVPL_Transf* }
%type gop_tree_type { gop_tree_mode_t }

%type payload_statement { GPtrArray* }
%type proto_stack { GPtrArray*  }
%type field { header_field_info* }
%type transform_list { GPtrArray* }
%type avpl { AVPL* }
%type avps { AVPL* }
%type avp { AVP* }
%type value { char* }
%type avp_oneoff { char* }


mate_config ::= decls.

decls ::= decls decl.
decls ::= .

decl ::= pdu_decl.
decl ::= gop_decl.
decl ::= gog_decl.
decl ::= transform_decl.
decl ::= defaults_decl.
decl ::= debug_decl.
decl ::= DONE_KW SEMICOLON.

/************* DEBUG
*/

debug_decl ::= DEBUG_KW OPEN_BRACE dbgfile_default dbglevel_default pdu_dbglevel_default gop_dbglevel_default gog_dbglevel_default CLOSE_BRACE SEMICOLON.

dbgfile_default ::= FILENAME_KW QUOTED(Filename) SEMICOLON. { mc->dbg_facility = ws_fopen(Filename,"w"); if (mc->dbg_facility == NULL) report_open_failure(Filename,errno,true); }
dbgfile_default ::= FILENAME_KW NAME(Filename) SEMICOLON. { mc->dbg_facility = ws_fopen(Filename,"w"); if (mc->dbg_facility == NULL) report_open_failure(Filename,errno,true);  }
dbgfile_default ::= .

dbglevel_default ::= LEVEL_KW INTEGER(LevelString) SEMICOLON. { mc->dbg_lvl = (int) strtol(LevelString,NULL,10); }
dbglevel_default ::= .

pdu_dbglevel_default ::= PDU_KW LEVEL_KW INTEGER(LevelString) SEMICOLON. { mc->dbg_pdu_lvl = (int) strtol(LevelString,NULL,10); }
pdu_dbglevel_default ::= .

gop_dbglevel_default ::= GOP_KW LEVEL_KW INTEGER(LevelString) SEMICOLON. { mc->dbg_gop_lvl = (int) strtol(LevelString,NULL,10); }
gop_dbglevel_default ::= .

gog_dbglevel_default ::= GOG_KW LEVEL_KW INTEGER(LevelString) SEMICOLON. { mc->dbg_gog_lvl = (int) strtol(LevelString,NULL,10); }
gog_dbglevel_default ::= .


/************* DEFAULTS
*/

defaults_decl ::= DEFAULT_KW OPEN_BRACE pdu_defaults gop_defaults gog_defaults CLOSE_BRACE SEMICOLON.

pdu_defaults ::= PDU_KW OPEN_BRACE pdu_last_extracted_default pdu_drop_unassigned_default pdu_discard_default CLOSE_BRACE SEMICOLON.
pdu_defaults ::= .

pdu_last_extracted_default ::= LAST_EXTRACTED_KW true_false(Flag) SEMICOLON. { mc->defaults.pdu.last_extracted = Flag; }
pdu_last_extracted_default ::= .

pdu_drop_unassigned_default ::= DROP_UNASSIGNED_KW true_false(Flag) SEMICOLON. { mc->defaults.pdu.drop_unassigned = Flag; }
pdu_drop_unassigned_default ::= .

pdu_discard_default ::= DISCARD_PDU_DATA_KW true_false(Flag) SEMICOLON. { mc->defaults.pdu.discard = Flag; }
pdu_discard_default ::= .

gop_defaults ::= GOP_KW OPEN_BRACE gop_expiration_default gop_idle_timeout_default gop_lifetime_default gop_drop_unassigned_default gop_tree_mode_default gop_show_times_default  CLOSE_BRACE SEMICOLON.
gop_defaults ::= .

gop_expiration_default ::= EXPIRATION_KW time_value(B) SEMICOLON. { mc->defaults.gop.expiration = B; }
gop_expiration_default ::= .

gop_idle_timeout_default ::= IDLE_TIMEOUT_KW time_value(B) SEMICOLON. { mc->defaults.gop.idle_timeout = B; }
gop_idle_timeout_default ::= .

gop_lifetime_default ::= LIFETIME_KW time_value(B) SEMICOLON. { mc->defaults.gop.lifetime = B; }
gop_lifetime_default ::= .

gop_drop_unassigned_default ::= DROP_UNASSIGNED_KW true_false(B) SEMICOLON. { mc->defaults.gop.drop_unassigned = B; }
gop_drop_unassigned_default ::= .

gop_tree_mode_default ::= SHOW_TREE_KW gop_tree_mode(B) SEMICOLON. { mc->defaults.gop.pdu_tree_mode = (gop_pdu_tree_t)B; }
gop_tree_mode_default ::= .

gop_show_times_default ::= SHOW_TIMES_KW true_false(B) SEMICOLON. { mc->defaults.gop.show_times = B; }
gop_show_times_default ::= .

gog_defaults ::= GOG_KW OPEN_BRACE gog_expiration_default gop_tree_mode_default gog_goptree_default gog_show_times_default CLOSE_BRACE SEMICOLON.
gog_defaults ::= .

gog_expiration_default ::= EXPIRATION_KW time_value(B) SEMICOLON. { mc->defaults.gop.expiration = B; }
gog_expiration_default ::= .

gog_goptree_default ::= GOP_TREE_KW gop_tree_type(B) SEMICOLON. { mc->defaults.gog.gop_tree_mode = B; }
gog_goptree_default ::= .

gog_show_times_default ::= SHOW_TIMES_KW true_false(B) SEMICOLON. { mc->defaults.gog.show_times = B; }
gog_show_times_default ::= .


/******************************************* TRANSFORM
*/

transform_decl(A) ::= TRANSFORM_KW NAME(B) transform_body(C) SEMICOLON. {
	AVPL_Transf* c;

	if ( g_hash_table_lookup(mc->transfs,B) ) {
		configuration_error(mc,"A transformation called '%s' exists already",B);
	}

	for ( c = C; c; c = c->next )
		c->name = g_strdup(B);

	g_hash_table_insert(mc->transfs,C->name,C);

	A = NULL;
}

transform_body(A) ::= OPEN_BRACE transform_statements(B) CLOSE_BRACE. { A = B; }

transform_statements(A) ::= transform_statements(C) transform_statement(B). {
	AVPL_Transf* c;

	for ( c = C; c->next; c = c->next ) ;
	c->next = B;
	A = C;
}

transform_statements(A) ::= transform_statement(B). { A = B; }

transform_statement(A) ::= transform_match(Match) transform_action(Action) SEMICOLON. {
	A = new_transform_elem(Match->avpl,Action->avpl,Match->match_mode,Action->replace_mode);
}

transform_match(A) ::= MATCH_KW  match_mode(Mode) avpl(Avpl). {
	A = (transf_match_t *)g_malloc(sizeof(transf_match_t));
	A->match_mode = Mode;
	A->avpl = Avpl;
}

transform_match(A) ::= . {
	A = (transf_match_t *)g_malloc(sizeof(transf_match_t));
	A->match_mode = AVPL_STRICT;
	A->avpl = new_avpl("");

}

transform_action(A) ::= . {
	A = (transf_action_t *)g_malloc(sizeof(transf_action_t));
	A->replace_mode = AVPL_INSERT;
	A->avpl = new_avpl("");
}
transform_action(A) ::= action_mode(Mode) avpl(Avpl). {
	A = (transf_action_t *)g_malloc(sizeof(transf_action_t));
	A->replace_mode = Mode;
	A->avpl = Avpl;
}

match_mode(A) ::=  . { A = AVPL_STRICT; }
match_mode(A) ::=  STRICT_KW. { A = AVPL_STRICT; }
match_mode(A) ::=  EVERY_KW. { A = AVPL_EVERY; }
match_mode(A) ::=  LOOSE_KW. { A = AVPL_LOOSE; }

action_mode(A) ::= REPLACE_KW. { A = AVPL_REPLACE; }
action_mode(A) ::= INSERT_KW. { A = AVPL_INSERT; }
action_mode(A) ::= . { A = AVPL_INSERT; }

/******************************************* PDU
*/

pdu_decl ::=
	PDU_KW NAME(Name) PROTO_KW field(Field) TRANSPORT_KW proto_stack(Stack)
		OPEN_BRACE
			payload_statement(Payload)
			extraction_statements(Extraction)
			transform_list_statement(Transform)
			criteria_statement(Criteria)
			pdu_drop_unassigned_statement(DropUnassigned)
			discard_pdu_data_statement(DistcardPduData)
			last_extracted_statement(LastExtracted)
		CLOSE_BRACE SEMICOLON.
{

	mate_cfg_pdu* cfg  = new_pducfg(mc, Name);
	extraction_t *extraction, *next_extraction;
	GPtrArray* transport_stack = g_ptr_array_new();
	int i;

	if (! cfg ) configuration_error(mc,"could not create Pdu %s.",Name);

	cfg->hfid_proto = Field->id;

	cfg->last_extracted = LastExtracted;
	cfg->discard = DistcardPduData;
	cfg->drop_unassigned = DropUnassigned;

	/*
	 * Add this protocol to our table of wanted hfids.
	 */
	mc->wanted_hfids = g_array_append_val(mc->wanted_hfids, Field->id);

	/* flip the transport_stack */
	for (i = Stack->len - 1; Stack->len; i--) {
		g_ptr_array_add(transport_stack,g_ptr_array_remove_index(Stack,i));
	}

	g_ptr_array_free(Stack, true);

	cfg->transport_ranges = transport_stack;
	cfg->payload_ranges = Payload;

	if (Criteria) {
		cfg->criterium = Criteria->criterium_avpl;
		cfg->criterium_match_mode = Criteria->criterium_match_mode;
		cfg->criterium_accept_mode = Criteria->criterium_accept_mode;
	}

	cfg->transforms = Transform;

	for (extraction = Extraction; extraction; extraction = next_extraction) {
		next_extraction = extraction->next;

		if ( ! add_hfid(mc, extraction->hfi, extraction->as, cfg->hfids_attr) ) {
			configuration_error(mc,"MATE: failed to create extraction rule '%s'",extraction->as);
		}

		g_free(extraction);
	}
}

payload_statement(A) ::= . { A = NULL; }
payload_statement(A) ::= PAYLOAD_KW proto_stack(B) SEMICOLON. { A = B; }

criteria_statement(A) ::= . { A = NULL; }
criteria_statement(A) ::= CRITERIA_KW accept_mode(B) match_mode(C) avpl(D) SEMICOLON. {
	A = g_new(pdu_criteria_t, 1);
	A->criterium_avpl = D;
	A->criterium_match_mode = C;
	A->criterium_accept_mode = B;
}

accept_mode(A) ::= . { A = ACCEPT_MODE; }
accept_mode(A) ::= ACCEPT_KW. { A = ACCEPT_MODE; }
accept_mode(A) ::= REJECT_KW. { A = REJECT_MODE; }

extraction_statements(A) ::= extraction_statements(B) extraction_statement(C). { A = B; A->last = A->last->next = C; }
extraction_statements(A) ::= extraction_statement(B). { A = B; A->last = A; }

extraction_statement(A) ::= EXTRACT_KW NAME(NAME) FROM_KW field(FIELD) SEMICOLON. {
	A = g_new(extraction_t, 1);
	A->as = NAME;
	A->hfi = FIELD;
	A->next = A->last = NULL;
}


pdu_drop_unassigned_statement(A) ::= DROP_UNASSIGNED_KW true_false(B) SEMICOLON. { A = B; }
pdu_drop_unassigned_statement(A) ::= . { A =  mc->defaults.pdu.drop_unassigned; }

discard_pdu_data_statement(A) ::=  DISCARD_PDU_DATA_KW true_false(B) SEMICOLON. { A = B; }
discard_pdu_data_statement(A) ::=  . { A =  mc->defaults.pdu.discard; }

last_extracted_statement(A) ::= LAST_PDU_KW true_false(B) SEMICOLON. { A = B; }
last_extracted_statement(A) ::= . { A = mc->defaults.pdu.last_extracted; }

proto_stack(A) ::= proto_stack(B) SLASH field(C). {
	int* hfidp = g_new(int, 1);

	*hfidp = C->id;
	g_ptr_array_add(B,hfidp);
	A = B;
}

proto_stack(A) ::= field(B). {
	int* hfidp = g_new(int, 1);
	*hfidp = B->id;

	A = g_ptr_array_new();
	g_ptr_array_add(A,hfidp);
}

field(A) ::= NAME(B). {
	A = proto_registrar_get_byname(B);
	if (A == NULL) {
		configuration_error(mc,"\nUnknown Proto/Transport/field: %s\n",B);
	};
}

/******************************************* GOP
*/

gop_decl(A) ::= GOP_KW NAME(Name) ON_KW pdu_name(PduName) MATCH_KW avpl(Key) OPEN_BRACE
		gop_start_statement(Start)
		gop_stop_statement(Stop)
		extra_statement(Extra)
		transform_list_statement(Transform)
		gop_expiration_statement(Expiration)
		idle_timeout_statement(IdleTimeout)
		lifetime_statement(Lifetime)
		gop_drop_unassigned_statement(DropUnassigned)
		show_goptree_statement(TreeMode)
		show_times_statement(ShowTimes)
	CLOSE_BRACE SEMICOLON. {
		mate_cfg_gop* cfg;

	if (g_hash_table_lookup(mc->gopcfgs,Name)) configuration_error(mc,"A Gop Named '%s' exists already.",Name);
	if (g_hash_table_lookup(mc->gops_by_pduname,PduName) ) configuration_error(mc,"Gop for Pdu '%s' exists already",PduName);

	cfg = new_gopcfg(mc, Name);
	g_hash_table_insert(mc->gops_by_pduname,PduName,cfg);
	g_hash_table_insert(mc->gopcfgs,cfg->name,cfg);

	cfg->on_pdu = PduName;
	cfg->key = Key;
	cfg->drop_unassigned = DropUnassigned;
	cfg->show_times = ShowTimes;
	cfg->pdu_tree_mode = (gop_pdu_tree_t)TreeMode;
	cfg->expiration = Expiration;
	cfg->idle_timeout = IdleTimeout;
	cfg->lifetime = Lifetime;
	cfg->start = Start;
	cfg->stop = Stop;
	cfg->transforms = Transform;

	merge_avpl(cfg->extra,Extra,true);
	delete_avpl(Extra,true);
}

gop_drop_unassigned_statement(A) ::= DROP_UNASSIGNED_KW true_false(B) SEMICOLON. { A = B; }
gop_drop_unassigned_statement(A) ::= . { A =  mc->defaults.gop.drop_unassigned; }

gop_start_statement(A) ::= START_KW avpl(B) SEMICOLON. { A = B; }
gop_start_statement(A) ::= . { A = NULL; }

gop_stop_statement(A) ::= STOP_KW avpl(B) SEMICOLON. { A = B; }
gop_stop_statement(A) ::= . { A = NULL; }

show_goptree_statement(A) ::= SHOW_TREE_KW gop_tree_mode(B) SEMICOLON. { A = B; }
show_goptree_statement(A) ::= . { A = (gop_tree_mode_t)mc->defaults.gop.pdu_tree_mode; }

show_times_statement(A) ::= SHOW_TIMES_KW true_false(B) SEMICOLON. { A = B; }
show_times_statement(A) ::= . { A = mc->defaults.gop.show_times; }

gop_expiration_statement(A) ::= EXPIRATION_KW time_value(B) SEMICOLON. { A = B; }
gop_expiration_statement(A) ::= . { A = mc->defaults.gop.lifetime; }

idle_timeout_statement(A) ::= IDLE_TIMEOUT_KW time_value(B) SEMICOLON. { A = B; }
idle_timeout_statement(A) ::= . { A = mc->defaults.gop.lifetime; }

lifetime_statement(A) ::= LIFETIME_KW time_value(B) SEMICOLON. { A = B; }
lifetime_statement(A) ::= . { A = mc->defaults.gop.lifetime; }

gop_tree_mode(A) ::= NO_TREE_KW.	{ A = (gop_tree_mode_t)GOP_NO_TREE; }
gop_tree_mode(A) ::= PDU_TREE_KW.	{ A = (gop_tree_mode_t)GOP_PDU_TREE; }
gop_tree_mode(A) ::= FRAME_TREE_KW.	{ A = (gop_tree_mode_t)GOP_FRAME_TREE; }
gop_tree_mode(A) ::= BASIC_TREE_KW.	{ A = (gop_tree_mode_t)GOP_BASIC_PDU_TREE; }

true_false(A) ::= TRUE_KW. { A = true; }
true_false(A) ::= FALSE_KW. { A = false; }

pdu_name(A) ::= NAME(B). {
	mate_cfg_pdu* c;
	if (( c =  (mate_cfg_pdu *)g_hash_table_lookup(mc->pducfgs,B) )) {
		A = c->name;
	} else {
		configuration_error(mc,"No such Pdu: '%s'",B);
	}
}


time_value(A) ::= FLOATING(B). {
	A = g_ascii_strtod(B,NULL);
}

time_value(A) ::= INTEGER(B). {
	A = g_ascii_strtod(B,NULL);
}

/************* GOG
*/

gog_decl ::= GOG_KW NAME(Name) OPEN_BRACE
	gog_key_statements(Keys)
	extra_statement(Extra)
	transform_list_statement(Transforms)
	gog_expiration_statement(Expiration)
	gog_goptree_statement(Tree)
	show_times_statement(ShowTimes)
	CLOSE_BRACE SEMICOLON. {
		mate_cfg_gog* cfg = NULL;

		if ( g_hash_table_lookup(mc->gogcfgs,Name) ) {
			configuration_error(mc,"Gog '%s' exists already ",Name);
		}

		cfg = new_gogcfg(mc, Name);

		cfg->expiration = Expiration;
		cfg->gop_tree_mode = Tree;
		cfg->transforms = Transforms;
		cfg->keys = Keys;
		cfg->show_times = ShowTimes;

		merge_avpl(cfg->extra,Extra,true);
		delete_avpl(Extra,true);
}

gog_goptree_statement(A) ::= GOP_TREE_KW gop_tree_type(B) SEMICOLON. { A = B; }
gog_goptree_statement(A) ::= . { A = mc->defaults.gog.gop_tree_mode; }

gog_expiration_statement(A) ::= EXPIRATION_KW time_value(B) SEMICOLON. { A = B; }
gog_expiration_statement(A) ::= . { A = mc->defaults.gog.expiration; }

gop_tree_type(A) ::= NULL_TREE_KW. { A = GOP_NULL_TREE; }
gop_tree_type(A) ::= FULL_TREE_KW. { A = GOP_FULL_TREE; }
gop_tree_type(A) ::= BASIC_TREE_KW. { A = GOP_BASIC_TREE; }

gog_key_statements(A) ::= gog_key_statements(B) gog_key_statement(C). {
	loal_append(B,C);
	A = B;
}

gog_key_statements(A) ::= gog_key_statement(B). {
	A = new_loal("");
	loal_append(A,B);
}


gog_key_statement(A) ::= MEMBER_KW gop_name(B) avpl(C) SEMICOLON. {
	rename_avpl(C,B);
	A = C;
}

gop_name(A) ::= NAME(B). {
	mate_cfg_gop* c;
	if (( c = (mate_cfg_gop *)g_hash_table_lookup(mc->gopcfgs,B) )) {
		A = c->name;
	} else {
		configuration_error(mc,"No Gop called '%s' has been already declared",B);
	}
}
/******************************************** GENERAL
*/


extra_statement(A) ::= EXTRA_KW avpl(B) SEMICOLON. { A = B; }
extra_statement(A) ::= . { A = new_avpl(""); }

transform_list_statement(A) ::= TRANSFORM_KW transform_list(B) SEMICOLON. { A = B; }
transform_list_statement(A) ::= . { A = g_ptr_array_new(); }

transform_list(A) ::= transform_list(B) COMMA transform(C). {
	A = B;
	g_ptr_array_add(B,C);
}

transform_list(A) ::= transform(B). {
	A = g_ptr_array_new();
	g_ptr_array_add(A,B);
}

transform(A) ::= NAME(B). {
	AVPL_Transf* t;

	if (( t = (AVPL_Transf *)g_hash_table_lookup(mc->transfs,B) )) {
		A = t;
	} else {
		configuration_error(mc,"There's no such Transformation: %s",B);
	}
}

avpl(A) ::= OPEN_PARENS avps(B) CLOSE_PARENS. { A = B; }
avpl(A) ::= OPEN_PARENS CLOSE_PARENS. { A = new_avpl(""); }

avps(A) ::= avps(B) COMMA avp(C). { A = B; if ( ! insert_avp(B,C) ) delete_avp(C); }
avps(A) ::= avp(B). { A = new_avpl(""); if ( ! insert_avp(A,B) ) delete_avp(B); }

avp(A) ::= NAME(B) AVP_OPERATOR(C) value(D). { A = new_avp(B,D,*C); }
avp(A) ::= NAME(B). { A = new_avp(B,"",'?'); }
avp(A) ::= NAME(B) OPEN_BRACE avp_oneoff(C) CLOSE_BRACE. { A = new_avp(B,C,'|'); }

avp_oneoff(A) ::= avp_oneoff(B) PIPE value(C). { A = ws_strdup_printf("%s|%s",B,C); }
avp_oneoff(A) ::= value(B). { A = g_strdup(B); }

value(A) ::= QUOTED(B). { A = g_strdup(B); }
value(A) ::= NAME(B). { A = g_strdup(B); }
value(A) ::= FLOATING(B). { A = g_strdup(B); }
value(A) ::= INTEGER(B). { A = g_strdup(B); }
value(A) ::= DOTED_IP(B). { A = g_strdup(B); }
value(A) ::= COLONIZED(B). { A = recolonize(mc,B); }