The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Data Structures | Macros | Typedefs | Functions | Variables
command.c File Reference

Internal commands for the server. More...

#include <freeradius-devel/server/command.h>
#include <freeradius-devel/server/log.h>
#include <freeradius-devel/util/debug.h>
#include <freeradius-devel/util/misc.h>
+ Include dependency graph for command.c:

Go to the source code of this file.

Data Structures

struct  fr_cmd_argv_s
 
struct  fr_cmd_s
 
struct  fr_cmd_stack_t
 A stack for walking commands. More...
 

Macros

#define FR_TYPE_ALTERNATE   FR_TYPE_VSA
 
#define FR_TYPE_ALTERNATE_CHOICE   FR_TYPE_GROUP
 
#define FR_TYPE_FIXED   FR_TYPE_TIME_DELTA
 
#define FR_TYPE_OPTIONAL   FR_TYPE_STRUCT
 
#define FR_TYPE_VARARGS   FR_TYPE_TLV
 
#define MATCHED_NAME   ((!*p || isspace((uint8_t) *p)) && !*q)
 
#define MATCHED_START   ((text + start) >= word) && ((text + start) <= p)
 
#define SKIP_NAME(name)
 
#define TOO_FAR   (*p && (*q > *p))
 

Typedefs

typedef struct fr_cmd_argv_s fr_cmd_argv_t
 

Functions

static int expand_all (fr_cmd_t *cmd, fr_cmd_info_t *info, fr_cmd_argv_t *argv, int count, int max_expansions, char const **expansions)
 
static int expand_syntax (fr_cmd_t *cmd, fr_cmd_info_t *info, fr_cmd_argv_t *argv, char const *text, int start, char const **word_p, int count, int max_expansions, char const **expansions)
 
int fr_command_add (TALLOC_CTX *talloc_ctx, fr_cmd_t **head, char const *name, void *ctx, fr_cmd_table_t const *table)
 Add one command to the global command tree. More...
 
int fr_command_add_multi (TALLOC_CTX *talloc_ctx, fr_cmd_t **head, char const *name, void *ctx, fr_cmd_table_t const *table)
 Add multiple commands to the global command tree. More...
 
static int fr_command_add_syntax (TALLOC_CTX *ctx, char *syntax, fr_cmd_argv_t **head, bool allow_varargs)
 
static fr_cmd_tfr_command_alloc (TALLOC_CTX *ctx, fr_cmd_t **head, char const *name)
 Allocate an fr_cmd_t structure. More...
 
int fr_command_clear (int new_argc, fr_cmd_info_t *info)
 Clear out any value boxes etc. More...
 
int fr_command_complete (fr_cmd_t *head, char const *text, int start, int max_expansions, char const **expansions)
 Do readline-style command completions. More...
 
void fr_command_debug (FILE *fp, fr_cmd_t *head)
 
static void fr_command_debug_internal (FILE *fp, fr_cmd_t *head, int depth)
 
static void fr_command_debug_node (FILE *fp, fr_cmd_t *cmd, int depth)
 
static fr_cmd_tfr_command_find (fr_cmd_t **head, char const *name, fr_cmd_t ***insert)
 Find a command. More...
 
char const * fr_command_help (fr_cmd_t *head, int argc, char *argv[])
 Get help text for a particular command. More...
 
void fr_command_info_init (TALLOC_CTX *ctx, fr_cmd_info_t *info)
 Initialize an fr_cmd_info_t structure. More...
 
void fr_command_list (FILE *fp, int max_depth, fr_cmd_t *head, int options)
 
static void fr_command_list_internal (FILE *fp, fr_cmd_t *head, int depth, int max_depth, char const **argv, int options)
 
static void fr_command_list_node (FILE *fp, fr_cmd_t *cmd, int depth, char const **argv, int options)
 
int fr_command_print_help (FILE *fp, fr_cmd_t *head, char const *text)
 Do readline-style help completions. More...
 
static int fr_command_register (UNUSED TALLOC_CTX *talloc_ctx, UNUSED char const *name, UNUSED void *ctx, UNUSED fr_cmd_table_t *table)
 
int fr_command_run (FILE *fp, FILE *fp_err, fr_cmd_info_t *info, bool read_only)
 Run a particular command. More...
 
static int fr_command_run_partial (FILE *fp, FILE *fp_err, fr_cmd_info_t *info, bool read_only, int offset, fr_cmd_t *head)
 
int fr_command_str_to_argv (fr_cmd_t *head, fr_cmd_info_t *info, char const *text)
 Split a string in-place, updating argv[]. More...
 
bool fr_command_strncmp (const char *word, const char *name)
 
int fr_command_tab_expand (TALLOC_CTX *ctx, fr_cmd_t *head, fr_cmd_info_t *info, int max_expansions, char const **expansions)
 Get the commands && help at a particular level. More...
 
static int fr_command_tab_expand_argv (TALLOC_CTX *ctx, fr_cmd_t *cmd, fr_cmd_info_t *info, char const *name, fr_cmd_argv_t *argv, int max_expansions, char const **expansions)
 
static int fr_command_tab_expand_partial (fr_cmd_t *head, char const *partial, int max_expansions, char const **expansions)
 
static int fr_command_tab_expand_syntax (TALLOC_CTX *ctx, fr_cmd_t *cmd, int syntax_offset, fr_cmd_info_t *info, int max_expansions, char const **expansions)
 
static bool fr_command_valid_name (char const *name)
 
static bool fr_command_valid_syntax (fr_cmd_argv_t *argv)
 
static int fr_command_verify_argv (fr_cmd_info_t *info, int start, int verify, int argc, fr_cmd_argv_t **argv_p, bool optional)
 
int fr_command_walk (fr_cmd_t *head, void **walk_ctx, void *ctx, fr_cmd_walk_t callback)
 Walk over a command hierarchy. More...
 
static void print_help (FILE *fp, fr_cmd_t *cmd)
 
static char const * skip_word (char const *text)
 
static int split (char **input, char **output, bool syntax_string)
 
static int split_alternation (char **input, char **output)
 
static int syntax_str_to_argv (int start_argc, fr_cmd_argv_t *start, fr_cmd_info_t *info, char const **text, bool *runnable)
 Check the syntax of a command, starting at *text More...
 

Variables

fr_command_register_hook_t fr_command_register_hook = fr_command_register
 
static const char * tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
 

Detailed Description

Internal commands for the server.

Id
98890f987989f0a9ed7705ab1055515f56f5a968

Definition in file command.c.


Data Structure Documentation

◆ fr_cmd_argv_s

struct fr_cmd_argv_s

Definition at line 45 of file command.c.

+ Collaboration diagram for fr_cmd_argv_s:
Data Fields
fr_cmd_argv_t * child
char const * name
fr_cmd_argv_t * next
fr_type_t type

◆ fr_cmd_s

struct fr_cmd_s

Definition at line 52 of file command.c.

+ Collaboration diagram for fr_cmd_s:
Data Fields
bool added_name was this name added?
struct fr_cmd_s * child if there are subcommands
void * ctx
fr_cmd_func_t func
char const * help
Todo:
  • long / short help
bool intermediate intermediate commands can't have callbacks
bool live is this entry live?
char const * name
struct fr_cmd_s * next
bool read_only
char const * syntax only for terminal nodes
fr_cmd_argv_t * syntax_argv arguments and types
fr_cmd_tab_t tab_expand

◆ fr_cmd_stack_t

struct fr_cmd_stack_t

A stack for walking commands.

Definition at line 999 of file command.c.

+ Collaboration diagram for fr_cmd_stack_t:
Data Fields
int depth
fr_cmd_t * entry[CMD_MAX_ARGV]
char const ** parents

Macro Definition Documentation

◆ FR_TYPE_ALTERNATE

#define FR_TYPE_ALTERNATE   FR_TYPE_VSA

Definition at line 88 of file command.c.

◆ FR_TYPE_ALTERNATE_CHOICE

#define FR_TYPE_ALTERNATE_CHOICE   FR_TYPE_GROUP

Definition at line 89 of file command.c.

◆ FR_TYPE_FIXED

#define FR_TYPE_FIXED   FR_TYPE_TIME_DELTA

Definition at line 84 of file command.c.

◆ FR_TYPE_OPTIONAL

#define FR_TYPE_OPTIONAL   FR_TYPE_STRUCT

Definition at line 87 of file command.c.

◆ FR_TYPE_VARARGS

#define FR_TYPE_VARARGS   FR_TYPE_TLV

Definition at line 86 of file command.c.

◆ MATCHED_NAME

#define MATCHED_NAME   ((!*p || isspace((uint8_t) *p)) && !*q)

Definition at line 1364 of file command.c.

◆ MATCHED_START

#define MATCHED_START   ((text + start) >= word) && ((text + start) <= p)

Definition at line 1366 of file command.c.

◆ SKIP_NAME

#define SKIP_NAME (   name)
Value:
do { p = word; q = name; while (*p && *q && (*p == *q)) { \
p++; \
q++; \
} } while (0)
static char const * name

Definition at line 1360 of file command.c.

◆ TOO_FAR

#define TOO_FAR   (*p && (*q > *p))

Definition at line 1365 of file command.c.

Typedef Documentation

◆ fr_cmd_argv_t

typedef struct fr_cmd_argv_s fr_cmd_argv_t

Definition at line 42 of file command.c.

Function Documentation

◆ expand_all()

static int expand_all ( fr_cmd_t cmd,
fr_cmd_info_t info,
fr_cmd_argv_t argv,
int  count,
int  max_expansions,
char const **  expansions 
)
static

Definition at line 2409 of file command.c.

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

◆ expand_syntax()

static int expand_syntax ( fr_cmd_t cmd,
fr_cmd_info_t info,
fr_cmd_argv_t argv,
char const *  text,
int  start,
char const **  word_p,
int  count,
int  max_expansions,
char const **  expansions 
)
static

Definition at line 2470 of file command.c.

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

◆ fr_command_add()

int fr_command_add ( TALLOC_CTX *  talloc_ctx,
fr_cmd_t **  head,
char const *  name,
void *  ctx,
fr_cmd_table_t const *  table 
)

Add one command to the global command tree.

We do not do any sanity checks on "name". If it has spaces in it, or "special" characters, that's up to you. We assume that other things in the server will sanity check them.

Parameters
talloc_ctxthe talloc context
headpointer to the head of the command table.
nameof the command to allocate. Can be NULL for "top level" commands
ctxfor any callback function
tableof information about the current command
Returns
  • <0 on error
  • 0 on success

Definition at line 725 of file command.c.

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

◆ fr_command_add_multi()

int fr_command_add_multi ( TALLOC_CTX *  talloc_ctx,
fr_cmd_t **  head,
char const *  name,
void *  ctx,
fr_cmd_table_t const *  table 
)

Add multiple commands to the global command tree.

e.g. for module "foo", add "show module foo", "set module foo", etc.

Parameters
talloc_ctxthe talloc context
headpointer to the head of the command table.
nameof the command to allocate
ctxfor any callback function
tablearray of tables, terminated by "help == NULL"
Returns
  • <0 on error
  • 0 on success

Definition at line 985 of file command.c.

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

◆ fr_command_add_syntax()

static int fr_command_add_syntax ( TALLOC_CTX *  ctx,
char *  syntax,
fr_cmd_argv_t **  head,
bool  allow_varargs 
)
static

Definition at line 553 of file command.c.

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

◆ fr_command_alloc()

static fr_cmd_t* fr_command_alloc ( TALLOC_CTX *  ctx,
fr_cmd_t **  head,
char const *  name 
)
static

Allocate an fr_cmd_t structure.

We presume that this allocation is done after a call to fr_command_find()

Parameters
ctxtalloc ctx for the allocation
headof the command list (singly linked)
nameof the command to allocate
Returns
  • fr_cmd_t structure which has been allocated. Only "name" is filled in.

Definition at line 150 of file command.c.

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

◆ fr_command_clear()

int fr_command_clear ( int  new_argc,
fr_cmd_info_t info 
)

Clear out any value boxes etc.

Parameters
new_argcthe argc to set inside of info
infothe information with the current argc

Definition at line 2369 of file command.c.

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

◆ fr_command_complete()

int fr_command_complete ( fr_cmd_t head,
char const *  text,
int  start,
int  max_expansions,
char const **  expansions 
)

Do readline-style command completions.

Most useful as part of readline tab expansions. The expansions are strdup() strings, and MUST be free'd by the caller.

Parameters
headof the command tree
textthe text to check
startoffset in the text where the completions should start
max_expansionshow many entries in the "expansions" array.
[in,out]expansionswhere the expansions are stored.
Returns
  • <0 on error
  • >= 0 number of expansions in the array

Definition at line 2659 of file command.c.

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

◆ fr_command_debug()

void fr_command_debug ( FILE *  fp,
fr_cmd_t head 
)

Definition at line 1602 of file command.c.

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

◆ fr_command_debug_internal()

static void fr_command_debug_internal ( FILE *  fp,
fr_cmd_t head,
int  depth 
)
static

Definition at line 1590 of file command.c.

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

◆ fr_command_debug_node()

static void fr_command_debug_node ( FILE *  fp,
fr_cmd_t cmd,
int  depth 
)
static

Definition at line 1583 of file command.c.

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

◆ fr_command_find()

static fr_cmd_t* fr_command_find ( fr_cmd_t **  head,
char const *  name,
fr_cmd_t ***  insert 
)
static

Find a command.

Parameters
headthe head of the list
nameof the command to find
insertwhere the new command should be inserted
Returns
  • NULL for "not found". In which case "head" is the insertion point for the new command
  • !NULL for the command which was found.

Definition at line 100 of file command.c.

+ Here is the caller graph for this function:

◆ fr_command_help()

char const* fr_command_help ( fr_cmd_t head,
int  argc,
char *  argv[] 
)

Get help text for a particular command.

Parameters
headthe head of the hierarchy.
argcthe number of arguments in argv
argvthe arguments
Returns
  • NULL on "no help text"
  • !NULL is the help text. Do not free or access it.

Definition at line 1553 of file command.c.

+ Here is the call graph for this function:

◆ fr_command_info_init()

void fr_command_info_init ( TALLOC_CTX *  ctx,
fr_cmd_info_t info 
)

Initialize an fr_cmd_info_t structure.

Definition at line 2397 of file command.c.

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

◆ fr_command_list()

void fr_command_list ( FILE *  fp,
int  max_depth,
fr_cmd_t head,
int  options 
)

Definition at line 1649 of file command.c.

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

◆ fr_command_list_internal()

static void fr_command_list_internal ( FILE *  fp,
fr_cmd_t head,
int  depth,
int  max_depth,
char const **  argv,
int  options 
)
static

Definition at line 1631 of file command.c.

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

◆ fr_command_list_node()

static void fr_command_list_node ( FILE *  fp,
fr_cmd_t cmd,
int  depth,
char const **  argv,
int  options 
)
static

Definition at line 1608 of file command.c.

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

◆ fr_command_print_help()

int fr_command_print_help ( FILE *  fp,
fr_cmd_t head,
char const *  text 
)

Do readline-style help completions.

Most useful as part of readline.

Parameters
fpwhere the help is printed
headof the command tree
textthe text to check

Definition at line 2802 of file command.c.

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

◆ fr_command_register()

static int fr_command_register ( UNUSED TALLOC_CTX *  talloc_ctx,
UNUSED char const *  name,
UNUSED void *  ctx,
UNUSED fr_cmd_table_t table 
)
static

Definition at line 37 of file command.c.

◆ fr_command_run()

int fr_command_run ( FILE *  fp,
FILE *  fp_err,
fr_cmd_info_t info,
bool  read_only 
)

Run a particular command.

info->argc is left alone, as are all other fields. If you want to run multiple commands, call fr_command_clear(0, info) to zero out the relevant information.

Parameters
fpwhere the output is sent
fp_errwhere the error output is sent
infothe structure describing the command to expand
read_onlywhether or not this command should be run in read-only mode.
Returns
  • <0 on error
  • 0 the command was run successfully

Definition at line 1473 of file command.c.

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

◆ fr_command_run_partial()

static int fr_command_run_partial ( FILE *  fp,
FILE *  fp_err,
fr_cmd_info_t info,
bool  read_only,
int  offset,
fr_cmd_t head 
)
static

Definition at line 1368 of file command.c.

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

◆ fr_command_str_to_argv()

int fr_command_str_to_argv ( fr_cmd_t head,
fr_cmd_info_t info,
char const *  text 
)

Split a string in-place, updating argv[].

This function also respects the various data types (mostly). Strings can have quotes. Nothing else can have quotes. Non-string data types are skipped and only parsed to data types by fr_command_run().

Parameters
headthe head of the hierarchy.
infothe structure describing the command to expand
textthe string to split
Returns
  • <0 on error.
  • total number of arguments in the argv[] array. Always >= argc.

Definition at line 2141 of file command.c.

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

◆ fr_command_strncmp()

bool fr_command_strncmp ( const char *  word,
const char *  name 
)

Definition at line 2906 of file command.c.

+ Here is the caller graph for this function:

◆ fr_command_tab_expand()

int fr_command_tab_expand ( TALLOC_CTX *  ctx,
fr_cmd_t head,
fr_cmd_info_t info,
int  max_expansions,
char const **  expansions 
)

Get the commands && help at a particular level.

Parameters
ctxtalloc context for dynamically allocated expansions. The caller should free it to free all expansions it created. Expansions added by this function are "const char *", and are managed by the command hierarchy.
headthe head of the hierarchy.
infothe structure describing the command to expand
max_expansionsthe maximum number of entries in the expansions array
expansionswhere the expansions will be stored.
Returns
  • <0 on error
  • number of entries in the expansions array

Definition at line 1290 of file command.c.

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

◆ fr_command_tab_expand_argv()

static int fr_command_tab_expand_argv ( TALLOC_CTX *  ctx,
fr_cmd_t cmd,
fr_cmd_info_t info,
char const *  name,
fr_cmd_argv_t argv,
int  max_expansions,
char const **  expansions 
)
static

Definition at line 1158 of file command.c.

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

◆ fr_command_tab_expand_partial()

static int fr_command_tab_expand_partial ( fr_cmd_t head,
char const *  partial,
int  max_expansions,
char const **  expansions 
)
static

Definition at line 1137 of file command.c.

+ Here is the caller graph for this function:

◆ fr_command_tab_expand_syntax()

static int fr_command_tab_expand_syntax ( TALLOC_CTX *  ctx,
fr_cmd_t cmd,
int  syntax_offset,
fr_cmd_info_t info,
int  max_expansions,
char const **  expansions 
)
static

Definition at line 1262 of file command.c.

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

◆ fr_command_valid_name()

static bool fr_command_valid_name ( char const *  name)
static

Definition at line 174 of file command.c.

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

◆ fr_command_valid_syntax()

static bool fr_command_valid_syntax ( fr_cmd_argv_t argv)
static

Definition at line 204 of file command.c.

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

◆ fr_command_verify_argv()

static int fr_command_verify_argv ( fr_cmd_info_t info,
int  start,
int  verify,
int  argc,
fr_cmd_argv_t **  argv_p,
bool  optional 
)
static

Definition at line 1672 of file command.c.

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

◆ fr_command_walk()

int fr_command_walk ( fr_cmd_t head,
void **  walk_ctx,
void *  ctx,
fr_cmd_walk_t  callback 
)

Walk over a command hierarchy.

Parameters
headthe head of the hierarchy. Call it with NULL to clean up walk_ctx
[in,out]walk_ctxto track across multiple function calls. MUST point to a void* when starting
ctxfor the callback
callbackto call with fr_walk_info_t about each command
Returns
  • <0 on error
  • 0 for nothing more to do.
  • 1 for "please call me again to get the next command". and walk_ctx now points to data allocated by, and managed by this function. It MUST be cleaned up via another call to this function.

Definition at line 1019 of file command.c.

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

◆ print_help()

static void print_help ( FILE *  fp,
fr_cmd_t cmd 
)
static

Definition at line 2785 of file command.c.

+ Here is the caller graph for this function:

◆ skip_word()

static char const* skip_word ( char const *  text)
static

Definition at line 1872 of file command.c.

+ Here is the caller graph for this function:

◆ split()

static int split ( char **  input,
char **  output,
bool  syntax_string 
)
static

Definition at line 385 of file command.c.

+ Here is the caller graph for this function:

◆ split_alternation()

static int split_alternation ( char **  input,
char **  output 
)
static

Definition at line 268 of file command.c.

+ Here is the caller graph for this function:

◆ syntax_str_to_argv()

static int syntax_str_to_argv ( int  start_argc,
fr_cmd_argv_t start,
fr_cmd_info_t info,
char const **  text,
bool runnable 
)
static

Check the syntax of a command, starting at *text

Note that we don't keep a stack of where we are for partial commands. So we MUST re-parse the ENTIRE input every time.

Definition at line 1904 of file command.c.

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

Variable Documentation

◆ fr_command_register_hook

fr_command_register_hook_t fr_command_register_hook = fr_command_register

Definition at line 42 of file command.c.

◆ tabs

const char* tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
static

Definition at line 1581 of file command.c.