The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Macros | Functions | Variables
xlat_tokenize.c File Reference

String expansion ("translation"). More...

#include "lib/util/event.h"
#include "lib/util/value.h"
#include <freeradius-devel/util/debug.h>
#include <freeradius-devel/util/sbuff.h>
#include <freeradius-devel/server/regex.h>
#include <freeradius-devel/unlang/xlat_priv.h>
+ Include dependency graph for xlat_tokenize.c:

Go to the source code of this file.

Macros

#define INFO_INDENT(_fmt, ...)   INFO("%*s"_fmt, depth * 2, " ", ## __VA_ARGS__)
 
#define XLAT_DEBUG(...)
 
#define XLAT_HEXDUMP(...)
 

Functions

static void _xlat_debug_head (xlat_exp_head_t const *head, int depth)
 
static void _xlat_debug_node (xlat_exp_t const *node, int depth)
 
void xlat_debug (xlat_exp_t const *node)
 
void xlat_debug_head (xlat_exp_head_t const *head)
 
int xlat_from_tmpl_attr (TALLOC_CTX *ctx, xlat_exp_head_t **out, tmpl_t **vpt_p)
 Convert attr tmpl to an xlat for &attr[*]. More...
 
bool xlat_is_literal (xlat_exp_head_t const *head)
 Check to see if the expansion consists entirely of value-box elements. More...
 
bool xlat_needs_resolving (xlat_exp_head_t const *head)
 Check to see if the expansion needs resolving. More...
 
ssize_t xlat_print (fr_sbuff_t *out, xlat_exp_head_t const *head, fr_sbuff_escape_rules_t const *e_rules)
 Reconstitute an xlat expression from its constituent nodes. More...
 
ssize_t xlat_print_node (fr_sbuff_t *out, xlat_exp_head_t const *head, xlat_exp_t const *node, fr_sbuff_escape_rules_t const *e_rules, char c)
 
int xlat_resolve (xlat_exp_head_t *head, xlat_res_rules_t const *xr_rules)
 Walk over an xlat tree recursively, resolving any unresolved functions or references. More...
 
static int xlat_resolve_virtual_attribute (xlat_exp_t *node, tmpl_t *vpt)
 
bool xlat_to_string (TALLOC_CTX *ctx, char **str, xlat_exp_head_t **head)
 Convert an xlat node to an unescaped literal string and free the original node. More...
 
tmpl_txlat_to_tmpl_attr (TALLOC_CTX *ctx, xlat_exp_head_t *head)
 Try to convert an xlat to a tmpl for efficiency. More...
 
fr_slen_t xlat_tokenize (TALLOC_CTX *ctx, xlat_exp_head_t **out, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules, fr_value_box_safe_for_t literals_safe_for)
 Tokenize an xlat expansion. More...
 
fr_slen_t xlat_tokenize_argv (TALLOC_CTX *ctx, xlat_exp_head_t **out, fr_sbuff_t *in, xlat_t const *xlat, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules, bool comma, bool allow_attr)
 Tokenize an xlat expansion into a series of XLAT_TYPE_CHILD arguments. More...
 
static int xlat_tokenize_attribute (xlat_exp_head_t *head, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules, tmpl_attr_prefix_t attr_prefix)
 Parse an attribute ref or a virtual attribute. More...
 
int xlat_tokenize_expansion (xlat_exp_head_t *head, fr_sbuff_t *in, tmpl_rules_t const *t_rules)
 
static int xlat_tokenize_function_args (xlat_exp_head_t *head, fr_sbuff_t *in, tmpl_rules_t const *t_rules)
 Parse an xlat function and its child argument. More...
 
static int xlat_tokenize_input (xlat_exp_head_t *head, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules, fr_value_box_safe_for_t safe_for)
 Parse an xlat string i.e. More...
 
static int xlat_validate_function_arg (xlat_arg_parser_t const *arg_p, xlat_exp_t *arg)
 
int xlat_validate_function_args (xlat_exp_t *node)
 
int xlat_validate_function_mono (xlat_exp_t *node)
 

Variables

static bool const tmpl_attr_allowed_chars [UINT8_MAX+1]
 
static fr_sbuff_escape_rules_t const xlat_escape
 These rules apply to literal values and function arguments inside of an expansion. More...
 
bool const xlat_func_chars [UINT8_MAX+1]
 
static fr_sbuff_parse_rules_t const xlat_function_arg_rules
 Parse rules for literal values inside of an expansion. More...
 
static fr_table_num_sorted_t const xlat_quote_table []
 
static size_t xlat_quote_table_len = NUM_ELEMENTS(xlat_quote_table)
 
static fr_sbuff_unescape_rules_t const xlat_unescape
 These rules apply to literal values and function arguments inside of an expansion. More...
 

Detailed Description

String expansion ("translation").

Id
f35029b4b1fcd60ee5db6eda0178bba8c68bf3a7

Tokenizes xlat expansion strings.

Definition in file xlat_tokenize.c.

Macro Definition Documentation

◆ INFO_INDENT

#define INFO_INDENT (   _fmt,
  ... 
)    INFO("%*s"_fmt, depth * 2, " ", ## __VA_ARGS__)

Definition at line 912 of file xlat_tokenize.c.

◆ XLAT_DEBUG

#define XLAT_DEBUG (   ...)

Definition at line 43 of file xlat_tokenize.c.

◆ XLAT_HEXDUMP

#define XLAT_HEXDUMP (   ...)

Definition at line 44 of file xlat_tokenize.c.

Function Documentation

◆ _xlat_debug_head()

static void _xlat_debug_head ( xlat_exp_head_t const *  head,
int  depth 
)
static

Definition at line 1025 of file xlat_tokenize.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _xlat_debug_node()

static void _xlat_debug_node ( xlat_exp_t const *  node,
int  depth 
)
static

Definition at line 915 of file xlat_tokenize.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ xlat_debug()

void xlat_debug ( xlat_exp_t const *  node)

Definition at line 1020 of file xlat_tokenize.c.

+ Here is the call graph for this function:

◆ xlat_debug_head()

void xlat_debug_head ( xlat_exp_head_t const *  head)

Definition at line 1048 of file xlat_tokenize.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ xlat_from_tmpl_attr()

int xlat_from_tmpl_attr ( TALLOC_CTX *  ctx,
xlat_exp_head_t **  out,
tmpl_t **  vpt_p 
)

Convert attr tmpl to an xlat for &attr[*].

Parameters
[in]ctxto allocate new expansion in.
[out]outWhere to write new xlat node.
[in,out]vpt_pto convert to xlat expansion. Will be set to NULL on completion
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 1826 of file xlat_tokenize.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ xlat_is_literal()

bool xlat_is_literal ( xlat_exp_head_t const *  head)

Check to see if the expansion consists entirely of value-box elements.

Parameters
[in]headto check.
Returns
  • true if expansion contains only literal elements.
  • false if expansion contains expandable elements.

Definition at line 1536 of file xlat_tokenize.c.

+ Here is the caller graph for this function:

◆ xlat_needs_resolving()

bool xlat_needs_resolving ( xlat_exp_head_t const *  head)

Check to see if the expansion needs resolving.

Parameters
[in]headto check.
Returns
  • true if expansion needs resolving
  • false otherwise

Definition at line 1552 of file xlat_tokenize.c.

+ Here is the caller graph for this function:

◆ xlat_print()

ssize_t xlat_print ( fr_sbuff_t out,
xlat_exp_head_t const *  head,
fr_sbuff_escape_rules_t const *  e_rules 
)

Reconstitute an xlat expression from its constituent nodes.

Parameters
[in]outWhere to write the output string.
[in]headFirst node to print.
[in]e_rulesSpecifying how to escape literal values.

Definition at line 1231 of file xlat_tokenize.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ xlat_print_node()

ssize_t xlat_print_node ( fr_sbuff_t out,
xlat_exp_head_t const *  head,
xlat_exp_t const *  node,
fr_sbuff_escape_rules_t const *  e_rules,
char  c 
)

Definition at line 1053 of file xlat_tokenize.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ xlat_resolve()

int xlat_resolve ( xlat_exp_head_t head,
xlat_res_rules_t const *  xr_rules 
)

Walk over an xlat tree recursively, resolving any unresolved functions or references.

Parameters
[in,out]headof xlat tree to resolve.
[in]xr_rulesSpecifies rules to use for resolution passes after initial tokenization.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 1607 of file xlat_tokenize.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ xlat_resolve_virtual_attribute()

static int xlat_resolve_virtual_attribute ( xlat_exp_t node,
tmpl_t vpt 
)
static

Definition at line 436 of file xlat_tokenize.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ xlat_to_string()

bool xlat_to_string ( TALLOC_CTX *  ctx,
char **  str,
xlat_exp_head_t **  head 
)

Convert an xlat node to an unescaped literal string and free the original node.

This is really "unparse the xlat nodes, and convert back to their original string".

Parameters
[in]ctxto allocate the new string in.
[out]stra duplicate of the node's fmt string.
[in,out]headto convert.
Returns
  • true the tree consists of a single value node which was converted.
  • false the tree was more complex than a single literal, op was a noop.

Definition at line 1568 of file xlat_tokenize.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ xlat_to_tmpl_attr()

tmpl_t* xlat_to_tmpl_attr ( TALLOC_CTX *  ctx,
xlat_exp_head_t head 
)

Try to convert an xlat to a tmpl for efficiency.

Parameters
ctxto allocate new tmpl_t in.
headto convert.
Returns
  • NULL if unable to convert (not necessarily error).
  • A new tmpl_t.

Definition at line 1793 of file xlat_tokenize.c.

+ Here is the call graph for this function:

◆ xlat_tokenize()

fr_slen_t xlat_tokenize ( TALLOC_CTX *  ctx,
xlat_exp_head_t **  out,
fr_sbuff_t in,
fr_sbuff_parse_rules_t const *  p_rules,
tmpl_rules_t const *  t_rules,
fr_value_box_safe_for_t  literals_safe_for 
)

Tokenize an xlat expansion.

Parameters
[in]ctxto allocate dynamic buffers in.
[out]outthe head of the xlat list / tree structure.
[in]inthe format string to expand.
[in]p_rulescontrolling how the string containing the xlat expansions should be parsed.
[in]t_rulescontrolling how attribute references are parsed. Do NOT alter this function to take tmpl_rules_t as this provides another value for literals_safe_for and this gets very confusing.
[in]literals_safe_forthe safe_for value to assign to any literals occurring at the top level of the expansion.
Returns
  • >0 on success.
  • 0 and *head == NULL - Parse failure on first char.
  • 0 and *head != NULL - Zero length expansion
  • < 0 the negative offset of the parse failure.

Definition at line 1498 of file xlat_tokenize.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ xlat_tokenize_argv()

fr_slen_t xlat_tokenize_argv ( TALLOC_CTX *  ctx,
xlat_exp_head_t **  out,
fr_sbuff_t in,
xlat_t const *  xlat,
fr_sbuff_parse_rules_t const *  p_rules,
tmpl_rules_t const *  t_rules,
bool  comma,
bool  allow_attr 
)

Tokenize an xlat expansion into a series of XLAT_TYPE_CHILD arguments.

Parameters
[in]ctxto allocate nodes in. Note: All nodes will be allocated in the same ctx. This is to allow manipulation by xlat instantiation functions later.
[out]outthe head of the xlat list / tree structure.
[in]inthe format string to expand.
[in]xlatwe're tokenizing arguments for.
[in]p_rulescontrolling how to parse the string outside of any expansions.
[in]t_rulescontrolling how attribute references are parsed.
[in]commawhether the arguments are delimited by commas
[in]allow_attrallow attribute references as arguments
Returns
  • < 0 on error.
  • >0 on success which is the number of characters parsed.

Definition at line 1263 of file xlat_tokenize.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ xlat_tokenize_attribute()

static int xlat_tokenize_attribute ( xlat_exp_head_t head,
fr_sbuff_t in,
fr_sbuff_parse_rules_t const *  p_rules,
tmpl_rules_t const *  t_rules,
tmpl_attr_prefix_t  attr_prefix 
)
static

Parse an attribute ref or a virtual attribute.

Definition at line 461 of file xlat_tokenize.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ xlat_tokenize_expansion()

int xlat_tokenize_expansion ( xlat_exp_head_t head,
fr_sbuff_t in,
tmpl_rules_t const *  t_rules 
)

Definition at line 564 of file xlat_tokenize.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ xlat_tokenize_function_args()

static int xlat_tokenize_function_args ( xlat_exp_head_t head,
fr_sbuff_t in,
tmpl_rules_t const *  t_rules 
)
static

Parse an xlat function and its child argument.

Parses a function call string in the format

%<func>(<argument>) 
Returns
  • 0 if the string was parsed into a function.
  • <0 on parse error.

Definition at line 285 of file xlat_tokenize.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ xlat_tokenize_input()

static int xlat_tokenize_input ( xlat_exp_head_t head,
fr_sbuff_t in,
fr_sbuff_parse_rules_t const *  p_rules,
tmpl_rules_t const *  t_rules,
fr_value_box_safe_for_t  safe_for 
)
static

Parse an xlat string i.e.

a non-expansion or non-function

When this function is being used outside of an xlat expansion, i.e. on a string which contains one or more xlat expansions, it uses the terminal grammar and escaping rules of that string type.

Which this function is being used inside of an xlat expansion, it uses xlat specific terminal grammar and escaping rules.

This allows us to be smart about processing quotes within the expansions themselves.

Parameters
[out]headto allocate nodes in, and where to write the first child, and where the flags are stored.
[in]insbuff to parse.
[in]p_rulesthat control parsing.
[in]t_rulesthat control attribute reference and xlat function parsing.
[in]safe_formark up literal values as being pre-escaped. May be merged with t_rules in future.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 774 of file xlat_tokenize.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ xlat_validate_function_arg()

static int xlat_validate_function_arg ( xlat_arg_parser_t const *  arg_p,
xlat_exp_t arg 
)
static

Definition at line 185 of file xlat_tokenize.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ xlat_validate_function_args()

int xlat_validate_function_args ( xlat_exp_t node)

Definition at line 241 of file xlat_tokenize.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ xlat_validate_function_mono()

int xlat_validate_function_mono ( xlat_exp_t node)

Definition at line 167 of file xlat_tokenize.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ tmpl_attr_allowed_chars

bool const tmpl_attr_allowed_chars[UINT8_MAX+1]
static
Initial value:
= {
['-'] = true, ['/'] = true, ['_'] = true,
['.'] = true, ['*'] = true, ['#'] = true,
['['] = true, [']'] = true,
}
#define SBUFF_CHAR_CLASS_ALPHA_NUM

Definition at line 557 of file xlat_tokenize.c.

◆ xlat_escape

fr_sbuff_escape_rules_t const xlat_escape
static
Initial value:
= {
.name = "xlat",
.chr = '\\',
.subs = {
['\a'] = 'a',
['\b'] = 'b',
['\n'] = 'n',
['\r'] = 'r',
['\t'] = 't',
['\v'] = 'v',
['\\'] = '\\',
['%'] = '%',
['}'] = '}'
},
.esc = {
},
.do_utf8 = true,
.do_oct = true
}
#define SBUFF_CHAR_UNPRINTABLES_EXTENDED
#define SBUFF_CHAR_UNPRINTABLES_LOW

These rules apply to literal values and function arguments inside of an expansion.

Definition at line 72 of file xlat_tokenize.c.

◆ xlat_func_chars

bool const xlat_func_chars[UINT8_MAX+1]
Initial value:
= {
['.'] = true, ['-'] = true, ['_'] = true,
}

Definition at line 179 of file xlat_tokenize.c.

◆ xlat_function_arg_rules

fr_sbuff_parse_rules_t const xlat_function_arg_rules
static
Initial value:
= {
.escapes = &xlat_unescape,
.terminals = &FR_SBUFF_TERMS(
L(")"),
L(","),
),
}
#define L(_str)
Helper for initialising arrays of string literals.
Definition: build.h:207
#define FR_SBUFF_TERMS(...)
Initialise a terminal structure with a list of sorted strings.
Definition: sbuff.h:167
static fr_sbuff_unescape_rules_t const xlat_unescape
These rules apply to literal values and function arguments inside of an expansion.
Definition: xlat_tokenize.c:50

Parse rules for literal values inside of an expansion.

These rules are used to parse literals as arguments to functions and on the RHS of alternations.

The caller sets the literal parse rules for outside of expansions when they call xlat_tokenize.

Definition at line 102 of file xlat_tokenize.c.

◆ xlat_quote_table

fr_table_num_sorted_t const xlat_quote_table[]
static
Initial value:
= {
}
@ T_SINGLE_QUOTED_STRING
Definition: token.h:122
@ T_BACK_QUOTED_STRING
Definition: token.h:123
@ T_DOUBLE_QUOTED_STRING
Definition: token.h:121

Definition at line 905 of file xlat_tokenize.c.

◆ xlat_quote_table_len

size_t xlat_quote_table_len = NUM_ELEMENTS(xlat_quote_table)
static

Definition at line 910 of file xlat_tokenize.c.

◆ xlat_unescape

fr_sbuff_unescape_rules_t const xlat_unescape
static
Initial value:
= {
.name = "xlat",
.chr = '\\',
.subs = {
['a'] = '\a',
['b'] = '\b',
['e'] = '\\',
['n'] = '\n',
['r'] = '\r',
['t'] = '\t',
['v'] = '\v',
['\\'] = '\\',
['%'] = '%',
['}'] = '}'
},
.do_hex = true,
.do_oct = true
}

These rules apply to literal values and function arguments inside of an expansion.

Definition at line 50 of file xlat_tokenize.c.