/*
 *
  ***** BEGIN LICENSE BLOCK *****
 
  Copyright (C) 2009-2019 Olof Hagsand
  Copyright (C) 2020-2022 Olof Hagsand and Rubicon Communications, LLC(Netgate)

  This file is part of CLIXON.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  Alternatively, the contents of this file may be used under the terms of
  the GNU General Public License Version 3 or later (the "GPL"),
  in which case the provisions of the GPL are applicable instead
  of those above. If you wish to allow use of your version of this file only
  under the terms of the GPL, and not to allow others to
  use your version of this file under the terms of Apache License version 2, 
  indicate your decision by deleting the provisions above and replace them with
  the  notice and other provisions required by the GPL. If you do not delete
  the provisions above, a recipient may use your version of this file under
  the terms of any one of the Apache License version 2 or the GPL.

  ***** END LICENSE BLOCK *****

 * Sub-parsers to upper-level YANG parser: everything that is "stringified"
  */

#ifdef HAVE_CONFIG_H
#include "clixon_config.h" /* generated by config & autoconf */
#endif

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <syslog.h>

/* cligen */
#include <cligen/cligen.h>

/* clicon */
#include "clixon_log.h"
#include "clixon_err.h"
#include "clixon_queue.h"
#include "clixon_hash.h"
#include "clixon_handle.h"
#include "clixon_yang.h"
#include "clixon_yang_sub_parse.h"
#include "clixon_yang_schemanode_parse.h"

/*! Invoke yang sub-parser on string
 *
 * @param[in]  str           yang string
 * @param[in]  ys            Yang statement
 * @param[in]  accept        Sub-parse rule to accept
 * @param[in]  mainfile      Name of main parse file
 * @param[in]  linenum       Line number context in mainfile (assume parse module of ys)
 * @param[out] enabled       0: Disabled, 1: Enabled (if present)
 * @retval     0             OK
 * @retval    -1             Error
 */
int
yang_subparse(char                      *str,
	      yang_stmt                 *ys,
	      enum yang_sub_parse_accept accept,
	      const char                *mainfile,
	      int                        linenum,
	      int                       *enabled)
{
    int                    retval = -1;
    clixon_yang_sub_parse_yacc ife = {0,};

    clicon_debug(2, "%s %s", __FUNCTION__, str);
    ife.if_parse_string = str;
    ife.if_linenum = linenum;
    if (enabled)
	ife.if_ys = ys; /* Used as trigger to check if enabled */
    ife.if_accept = accept;
    ife.if_mainfile = mainfile;
    if (clixon_yang_sub_parsel_init(&ife) < 0)
	goto done;
    if (clixon_yang_sub_parseparse(&ife) != 0) { /* yacc returns 1 on error */
	if (clicon_errno == 0)
	    clicon_err(OE_YANG, 0, "If-feature parser error with no error code (should not happen)");
	goto done;
    }
    if (enabled)
	*enabled = ife.if_enabled;
    retval = 0;
 done:
    clixon_yang_sub_parsel_exit(&ife);
    return retval;
}

/*! Invoke yang (absolute_/descendant-)schema-nodeid sub-parser on string
 *
 * Used for  refine-arg-str / usage-augment-arg-str in RFC 7950 Sec 14
 * @param[in]  str      (abs/desc)_schema_nodeid string
 * @param[in]  accept        Sub-parse rule to accept
 * @param[in]  mainfile Name of main parse file
 * @param[in]  linenum  Line number context in mainfile
 * @retval     0        OK
 * @retval    -1        Error
 */
int
yang_schema_nodeid_subparse(char                      *str,
			    enum yang_sub_parse_accept accept,
			    const char                *mainfile,
			    int                        linenum)
{
    int                         retval = -1;
    clixon_yang_schemanode_yacc ife = {0,};

    clicon_debug(2, "%s %s", __FUNCTION__, str);
    ife.if_parse_string = str;
    ife.if_linenum = linenum;
    ife.if_mainfile = mainfile;
    ife.if_accept = accept; /* accept absolute-schema-nodeid */
    if (clixon_yang_schemanode_parsel_init(&ife) < 0)
	goto done;
    if (clixon_yang_schemanode_parseparse(&ife) != 0) { /* yacc returns 1 on error */
	if (clicon_errno == 0)
	    clicon_err(OE_YANG, 0, "descendant-schema-nodeid parser error with no error code (should not happen)");
	goto done;
    }
    retval = 0;
 done:
    clixon_yang_schemanode_parsel_exit(&ife);
    return retval;
}
