/*
 * Copyright (c) 2012 Nagy, Attila <bra@fsn.hu>
 * Copyright (c) 2012-2014 Balabit
 * Copyright (c) 2012-2014 Gergely Nagy <algernon@balabit.hu>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */

%code requires {

#include "afamqp-parser.h"

}

%code {

#include "cfg-grammar-internal.h"

}

%define api.prefix {afamqp_}
%lex-param {CfgLexer *lexer}
%parse-param {CfgLexer *lexer}
%parse-param {LogDriver **instance}
%parse-param {gpointer arg}


/* INCLUDE_DECLS */

%token KW_AMQP
%token KW_EXCHANGE
%token KW_EXCHANGE_DECLARE
%token KW_EXCHANGE_TYPE
%token KW_PERSISTENT
%token KW_VHOST
%token KW_ROUTING_KEY
%token KW_BODY
%token KW_AUTH_METHOD
%token KW_PASSWORD
%token KW_USERNAME
%token KW_MAX_CHANNEL
%token KW_FRAME_SIZE
%token KW_HEARTBEAT
%token KW_CA_FILE
%token KW_KEY_FILE
%token KW_CERT_FILE
%token KW_PEER_VERIFY
%token KW_TLS

%%

start
        : LL_CONTEXT_DESTINATION KW_AMQP
          {
            last_driver = *instance = afamqp_dd_new(configuration);
          }
          '(' afamqp_options ')'		{ YYACCEPT; }
	;

afamqp_options
        : afamqp_option afamqp_options
	|
	;

afamqp_option
	: KW_HOST '(' string ')'                   { afamqp_dd_set_host(last_driver, $3); free($3); }
	| KW_PORT '(' positive_integer ')'         { afamqp_dd_set_port(last_driver, $3); }
	| KW_VHOST '(' string ')'                  { afamqp_dd_set_vhost(last_driver, $3); free($3); }
	| KW_EXCHANGE '(' string ')'               { afamqp_dd_set_exchange(last_driver, $3); free($3); }
	| KW_EXCHANGE_DECLARE '(' yesno ')'        { afamqp_dd_set_exchange_declare(last_driver, $3); }
	| KW_EXCHANGE_TYPE '(' string ')'          { afamqp_dd_set_exchange_type(last_driver, $3); free($3); }
	| KW_ROUTING_KEY '(' template_content ')'  { afamqp_dd_set_routing_key(last_driver, $3); }
	| KW_BODY '(' template_content ')'         { afamqp_dd_set_body(last_driver, $3); }
	| KW_PERSISTENT '(' yesno ')'              { afamqp_dd_set_persistent(last_driver, $3); }
	| KW_AUTH_METHOD '(' string ')'            { CHECK_ERROR(afamqp_dd_set_auth_method(last_driver, $3), @3, "unknown auth-method() argument"); free($3); }
	| KW_USERNAME '(' string ')'               { afamqp_dd_set_user(last_driver, $3); free($3); }
	| KW_PASSWORD '(' string ')'               { afamqp_dd_set_password(last_driver, $3); free($3); }
	| KW_MAX_CHANNEL '(' positive_integer ')'  { afamqp_dd_set_max_channel(last_driver, $3); }
	| KW_FRAME_SIZE '(' positive_integer ')'   { afamqp_dd_set_frame_size(last_driver, $3); }
	| KW_HEARTBEAT '(' nonnegative_integer ')' { afamqp_dd_set_offered_heartbeat(last_driver, $3); }
	| value_pair_option                        { afamqp_dd_set_value_pairs(last_driver, $1); }
	| threaded_dest_driver_general_option
	| afamqp_tls_option
	| KW_TLS '(' afamqp_tls_options ')'
	| { last_template_options = afamqp_dd_get_template_options(last_driver); } template_option
	;


afamqp_tls_options
    : afamqp_tls_option afamqp_tls_options
    |
    ;

afamqp_tls_option
    : KW_CA_FILE '(' path_check ')'        { afamqp_dd_set_ca_file(last_driver, $3); free($3); }
    | KW_KEY_FILE '(' path_secret ')'      { afamqp_dd_set_key_file(last_driver, $3); free($3); }
    | KW_CERT_FILE '(' path_check ')'      { afamqp_dd_set_cert_file(last_driver, $3); free($3); }
    | KW_PEER_VERIFY '(' yesno ')'         { afamqp_dd_set_peer_verify(last_driver, $3); }
    ;

/* INCLUDE_RULES */

%%
