/*
 * Copyright (c) 2002-2016 Balabit
 * Copyright (c) 2016 Viktor Juhasz <viktor.juhasz@balabit.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; 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.
 *
 */

%type <fnum> float_between_0_and_1

%code top {
#pragma GCC diagnostic ignored "-Wswitch-default"

#include "diskq-parser.h"

}


%code {

#include "cfg-parser.h"
#include "syslog-names.h"
#include "messages.h"
#include "diskq.h"
#include "diskq-options.h"
#include "diskq-config.h"

#include <string.h>

DiskQDestPlugin *last_plugin;
DiskQueueOptions *last_options;

}

%define api.prefix {diskq_}

/* this parameter is needed in order to instruct bison to use a complete
 * argument list for yylex/yyerror */

%lex-param {CfgLexer *lexer}
%parse-param {CfgLexer *lexer}
%parse-param {LogDriverPlugin **instance}
%parse-param {gpointer arg}

/* INCLUDE_DECLS */

%token KW_DISK_BUFFER
%token KW_MEM_BUF_LENGTH
%token KW_DISK_BUF_SIZE
%token KW_RELIABLE
%token KW_COMPACTION
%token KW_MEM_BUF_SIZE
%token KW_QOUT_SIZE
%token KW_DIR
%token KW_TRUNCATE_SIZE_RATIO


%%

start
	: LL_CONTEXT_INNER_DEST dest_diskq { YYACCEPT; }
	| LL_CONTEXT_OPTIONS KW_DISK_BUFFER '(' diskq_global_options ')' { YYACCEPT; }
	;

dest_diskq
        : KW_DISK_BUFFER
          {
            last_plugin = diskq_dest_plugin_new();
            *instance = (LogDriverPlugin*)last_plugin;
            last_options = diskq_get_options(last_plugin);
          }
          '(' dest_diskq_options ')' { disk_queue_options_check_plugin_settings(last_options); }
        ;

dest_diskq_options
        : dest_diskq_option dest_diskq_options
        |
        ;

dest_diskq_option
        : KW_RELIABLE '(' yesno ')'                      { disk_queue_options_reliable_set(last_options, $3); }
        | KW_COMPACTION '(' yesno ')'                    { disk_queue_options_compaction_set(last_options, $3); }
        | KW_MEM_BUF_SIZE '(' nonnegative_integer ')'    { disk_queue_options_mem_buf_size_set(last_options, $3); }
        | KW_MEM_BUF_LENGTH '(' nonnegative_integer ')'  { disk_queue_options_mem_buf_length_set(last_options, $3); }
        | KW_DISK_BUF_SIZE '(' nonnegative_integer64 ')' { disk_queue_options_disk_buf_size_set(last_options, $3); }
        | KW_QOUT_SIZE '(' nonnegative_integer ')'       { disk_queue_options_qout_size_set(last_options, $3); }
        | KW_DIR '(' string ')'                          { disk_queue_options_set_dir(last_options, $3); free($3); }
        | KW_TRUNCATE_SIZE_RATIO '(' float_between_0_and_1 ')' { disk_queue_options_set_truncate_size_ratio(last_options, $3); }
        ;

diskq_global_options
        : diskq_global_option diskq_global_options
        |
        ;

diskq_global_option
        : KW_TRUNCATE_SIZE_RATIO '(' float_between_0_and_1 ')' { disk_queue_config_set_truncate_size_ratio(configuration, $3); }
        ;

float_between_0_and_1
        : nonnegative_float { CHECK_ERROR(($1 <= 1), @1, "It cannot be bigger, than 1"); }
        ;

/* INCLUDE_RULES */

%%
