/*-
* Copyright (c) 2011-2025 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Christos Zoulas.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* NPF variables are used to build the intermediate representation (IR)
* of the configuration grammar. They represent primitive types (strings,
* numbers, etc) as well as complex types (address and mask, table, etc).
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: npf_var.c,v 1.15 2025/06/01 00:54:36 joe Exp $");
/*
* Verify the parameters.
*/
if (vp == NULL) {
return NULL;
}
if (level >= var_num) {
yyerror("circular dependency for variable '%s'", vp->v_key);
return NULL;
}
if (vp->v_count <= idx) {
yyerror("variable '%s' has only %zu elements, requested %zu",
vp->v_key, vp->v_count, idx);
return NULL;
}
/*
* Get the element at the given index.
*/
el = vp->v_elements;
while (idx--) {
el = el->e_next;
}
/*
* Resolve if it is a reference to another variable.
*/
if (el->e_type == NPFVAR_VAR_ID) {
const npfvar_t *rvp = npfvar_lookup(el->e_data);
return npfvar_get_element(rvp, 0, level + 1);
}
return el;
}
int
npfvar_get_type(const npfvar_t *vp, size_t idx)
{
npf_element_t *el = npfvar_get_element(vp, idx, 0);
return el ? (int)el->e_type : -1;
}
if (el && NPFVAR_TYPE(el->e_type) != NPFVAR_TYPE(type)) {
yyerror("variable '%s' element %zu "
"is of type '%s' rather than '%s'", vp->v_key,
idx, npfvar_type(el->e_type), npfvar_type(type));
return NULL;
}
return el->e_data;
}