The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
condition.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: e80621626ea115e377d64fe04d5e36021be4afbf $
19 *
20 * @file unlang/condition.c
21 * @brief Unlang "condition" keyword evaluation.
22 *
23 * @copyright 2006-2019 The FreeRADIUS server project
24 */
25RCSID("$Id: e80621626ea115e377d64fe04d5e36021be4afbf $")
26
27#include "condition_priv.h"
28#include "group_priv.h"
29
30typedef struct {
31 fr_value_box_list_t out; //!< Head of the result of a nested
32 ///< expansion.
33 unlang_result_t result; //!< Store the result of unlang expressions.
35
37{
38 unlang_frame_state_cond_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_cond_t);
39 fr_value_box_t *box = fr_value_box_list_head(&state->out);
40 bool value;
41
42 /*
43 * Something in the conditional evaluation failed.
44 */
45 if (state->result.rcode == RLM_MODULE_FAIL) {
48
49 RDEBUG2("... failed to evaluate condition ...");
50
51 if (!gext->has_else) RETURN_UNLANG_FAIL;
53 }
54
55 if (!box) {
56 value = false;
57
58 } else if (fr_value_box_list_next(&state->out, box) != NULL) {
59 value = true;
60
61 } else {
63 }
64
65 if (!value) {
66 RDEBUG2("...");
68 }
69
70 /*
71 * Tell the main interpreter to skip over the else /
72 * elsif blocks, as this "if" condition was taken.
73 */
74 while (frame->next &&
75 ((frame->next->type == UNLANG_TYPE_ELSE) ||
76 (frame->next->type == UNLANG_TYPE_ELSIF))) {
77 frame->next = frame->next->next;
78 }
79
80 /*
81 * We took the "if". Go recurse into its' children.
82 */
83 return unlang_group(p_result, request, frame);
84}
85
87{
90 unlang_frame_state_cond_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_cond_t);
91
92 fr_assert(gext->head != NULL);
93
94 /*
95 * If we always run this condition, then don't bother pushing anything onto the stack.
96 *
97 * We still run this condition, even for "false" values, due to things like
98 *
99 * if (0) { ... } elsif ....
100 */
101 if (gext->is_truthy) {
102 return unlang_group(p_result, request, frame);
103 }
104
106
107 fr_value_box_list_init(&state->out);
108
109 if (unlang_xlat_push(state, &state->result, &state->out,
110 request, gext->head, UNLANG_SUB_FRAME) < 0) return UNLANG_ACTION_FAIL;
111
113}
114
116{
118 &(unlang_op_t){
119 .name = "if",
120 .interpret = unlang_if,
122 .frame_state_size = sizeof(unlang_frame_state_cond_t),
123 .frame_state_type = "unlang_frame_state_cond_t",
124 });
125
127 &(unlang_op_t){
128 .name = "else",
130 .interpret = unlang_group
131 });
132
134 &(unlang_op_t){
135 .name = "elseif",
136 .interpret = unlang_if,
138 .frame_state_size = sizeof(unlang_frame_state_cond_t),
139 .frame_state_type = "unlang_frame_state_cond_t",
140 });
141}
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition action.h:35
@ UNLANG_ACTION_PUSHED_CHILD
unlang_t pushed a new child onto the stack, execute it instead of continuing.
Definition action.h:39
@ UNLANG_ACTION_EXECUTE_NEXT
Execute the next unlang_t.
Definition action.h:38
@ UNLANG_ACTION_FAIL
Encountered an unexpected error.
Definition action.h:36
#define RCSID(id)
Definition build.h:485
unlang_result_t result
Store the result of unlang expressions.
Definition condition.c:33
void unlang_condition_init(void)
Definition condition.c:115
static unlang_action_t unlang_if(unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition condition.c:86
fr_value_box_list_t out
Head of the result of a nested expansion.
Definition condition.c:31
static unlang_action_t unlang_if_resume(unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition condition.c:36
xlat_exp_head_t * head
static unlang_cond_t * unlang_group_to_cond(unlang_group_t *g)
Cast a group structure to the cond keyword extension.
Test enumeration values.
Definition dict_test.h:92
unlang_action_t unlang_group(UNUSED unlang_result_t *p_result, request_t *request, UNUSED unlang_stack_frame_t *frame)
Definition group.c:31
Declarations for the "group" keyword.
#define UNLANG_SUB_FRAME
Definition interpret.h:37
rlm_rcode_t rcode
The current rcode, from executing the instruction or merging the result from a frame.
Definition interpret.h:134
void unlang_register(int type, unlang_op_t *op)
Register an operation with the interpreter.
Definition base.c:63
#define fr_assert(_expr)
Definition rad_assert.h:38
#define RDEBUG2(fmt,...)
Definition radclient.h:54
#define RETURN_UNLANG_FAIL
Definition rcode.h:57
@ RLM_MODULE_FAIL
Module failed, don't reply.
Definition rcode.h:42
int unlang_xlat_push(TALLOC_CTX *ctx, unlang_result_t *p_result, fr_value_box_list_t *out, request_t *request, xlat_exp_head_t const *xlat, bool top_frame)
Push a pre-compiled xlat onto the stack for evaluation.
Definition xlat.c:282
unlang_t * next
Next node (executed on UNLANG_ACTION_EXECUTE_NEXT et al).
void * state
Stack frame specialisations.
static unlang_group_t * unlang_generic_to_group(unlang_t const *p)
@ UNLANG_TYPE_ELSIF
!Condition && Condition.
Definition unlang_priv.h:56
@ UNLANG_TYPE_ELSE
!Condition.
Definition unlang_priv.h:55
@ UNLANG_TYPE_IF
Condition.
Definition unlang_priv.h:54
static void frame_repeat(unlang_stack_frame_t *frame, unlang_process_t process)
Mark the current stack frame up for repeat, and set a new process function.
unlang_t const * instruction
The unlang node we're evaluating.
unlang_t const * next
The next unlang node we will evaluate.
@ UNLANG_OP_FLAG_DEBUG_BRACES
Print debug braces.
unlang_type_t type
The specialisation of this node.
Generic representation of a grouping.
An unlang operation.
Our interpreter stack, as distinct from the C stack.
bool fr_value_box_is_truthy(fr_value_box_t const *in)
Check truthiness of values.
Definition value.c:6587