The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
base.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: 7358bf696313a5cbe3514f88a9ebf13fc5c66cd8 $
19 *
20 * @file unlang/base.c
21 * @brief Base, utility functions for the unlang library.
22 *
23 * @copyright 2019 The FreeRADIUS server project
24 */
25RCSID("$Id: 7358bf696313a5cbe3514f88a9ebf13fc5c66cd8 $")
26
27#include "unlang_priv.h"
28
29/** Different operations the interpreter can execute
30 */
32
33/** Return whether a section has unlang data associated with it
34 *
35 * @param[in] cs to check.
36 * @return
37 * - true if it has data.
38 * - false if it doesn't have data.
39 */
41{
42 return (cf_data_find(cs, unlang_group_t, NULL) != NULL);
43}
44
46
47/** Register an operation with the interpreter
48 *
49 * The main purpose of this registration API is to avoid intermixing the xlat,
50 * condition, map APIs with the interpreter, i.e. the callbacks needed for that
51 * functionality can be in their own source files, and we don't need to include
52 * supporting types and function declarations in the interpreter.
53 *
54 * @param[in] op #unlang_op_t to register.
55 */
57{
58 fr_assert(op->type < UNLANG_TYPE_MAX); /* Unlang max isn't a valid type */
60
61 memcpy(&unlang_ops[op->type], op, sizeof(unlang_ops[op->type]));
62
63 /*
64 * Some instruction types are internal, and are not real keywords.
65 */
66 if ((op->flag & UNLANG_OP_FLAG_INTERNAL) != 0) return;
67
69}
70
71static TALLOC_CTX *unlang_ctx = NULL;
72
73static uint32_t op_hash(void const *data)
74{
75 unlang_op_t const *a = data;
76
77 return fr_hash_string(a->name);
78}
79
80static int8_t op_cmp(void const *one, void const *two)
81{
82 unlang_op_t const *a = one;
83 unlang_op_t const *b = two;
84
85 return CMP(strcmp(a->name, b->name), 0);
86}
87
88static int _unlang_global_free(UNUSED void *uctx)
89{
90 TALLOC_FREE(unlang_ctx);
91 unlang_op_table = NULL;
92
93 return 0;
94}
95
96static int _unlang_global_init(UNUSED void *uctx)
97{
98 unlang_ctx = talloc_init("unlang");
99 if (!unlang_ctx) return -1;
100
102 if (!unlang_op_table) goto fail;
103
104 /*
105 * Explicitly initialise the xlat tree, and perform dictionary lookups.
106 */
107 if (xlat_global_init() < 0) {
108 fail:
109 TALLOC_FREE(unlang_ctx);
110
111 memset(unlang_ops, 0, sizeof(unlang_ops));
112 return -1;
113 }
114
116
117 /*
118 * Operations which can fail, and which require cleanup.
119 */
120 if (unlang_subrequest_op_init() < 0) goto fail;
121
122 /*
123 * Register operations for the default keywords. The
124 * operations listed below cannot fail, and do not
125 * require cleanup.
126 */
149
150 return 0;
151}
152
154{
155 int ret;
156 fr_atexit_global_once_ret(&ret, _unlang_global_init, _unlang_global_free, NULL);
157 return ret;
158}
#define RCSID(id)
Definition build.h:485
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
Definition build.h:112
#define UNUSED
Definition build.h:317
void unlang_call_init(void)
Definition call.c:324
void unlang_caller_init(void)
Definition caller.c:142
void unlang_catch_init(void)
Definition catch.c:196
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:101
#define cf_data_find(_cf, _type, _name)
Definition cf_util.h:244
void unlang_compile_init(TALLOC_CTX *ctx)
Definition compile.c:2327
void unlang_condition_init(void)
Definition condition.c:266
#define MEM(x)
Definition debug.h:36
void unlang_detach_init(void)
Initialise subrequest ops.
Definition detach.c:82
void unlang_finally_init(void)
Definition finally.c:186
void unlang_foreach_init(void)
Definition foreach.c:839
void unlang_function_init(void)
Definition function.c:580
void unlang_group_init(void)
Definition group.c:67
bool fr_hash_table_insert(fr_hash_table_t *ht, void const *data)
Insert data into a hash table.
Definition hash.c:468
uint32_t fr_hash_string(char const *p)
Definition hash.c:865
#define fr_hash_table_alloc(_ctx, _hash_node, _cmp_node, _free_node)
Definition hash.h:58
int unlang_interpret_init_global(TALLOC_CTX *ctx)
Definition interpret.c:2047
static int _unlang_global_free(UNUSED void *uctx)
Definition base.c:88
static int8_t op_cmp(void const *one, void const *two)
Definition base.c:80
unlang_op_t unlang_ops[UNLANG_TYPE_MAX]
Different operations the interpreter can execute.
Definition base.c:31
bool unlang_section(CONF_SECTION *cs)
Return whether a section has unlang data associated with it.
Definition base.c:40
static TALLOC_CTX * unlang_ctx
Definition base.c:71
int unlang_global_init(void)
Definition base.c:153
static int _unlang_global_init(UNUSED void *uctx)
Definition base.c:96
fr_hash_table_t * unlang_op_table
Definition base.c:45
static uint32_t op_hash(void const *data)
Definition base.c:73
void unlang_register(unlang_op_t *op)
Register an operation with the interpreter.
Definition base.c:56
void unlang_map_init(void)
Definition map.c:1057
void unlang_limit_init(void)
Definition limit.c:228
void unlang_load_balance_init(void)
unsigned int uint32_t
void unlang_parallel_init(void)
Definition parallel.c:435
#define fr_assert(_expr)
Definition rad_assert.h:38
void unlang_return_init(void)
Definition return.c:68
void unlang_module_init(void)
Definition module.c:996
int unlang_subrequest_op_init(void)
Initialise subrequest ops.
Definition subrequest.c:814
void unlang_switch_init(void)
Definition switch.c:520
void unlang_timeout_init(void)
Definition timeout.c:370
void unlang_tmpl_init(void)
Definition tmpl.c:345
void unlang_transaction_init(void)
void unlang_try_init(void)
Definition try.c:84
void unlang_edit_init(void)
Definition edit.c:1761
int xlat_global_init(void)
@ UNLANG_TYPE_MAX
Definition unlang_priv.h:82
unlang_type_t type
enum value for the keyword
char const * name
Name of the keyword.
@ UNLANG_OP_FLAG_INTERNAL
it's not a real keyword
unlang_op_flag_t flag
Interpreter flags for this operation.
Generic representation of a grouping.
An unlang operation.
static fr_slen_t data
Definition value.h:1288