cmd.h File Reference

a mechanism for registering and controlling display of commands More...

#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include "obj.h"

Go to the source code of this file.

Classes

struct  co_cmd_t
 a struct containing all the relevant information for a specific command More...
 

Macros

#define CMD(N)   static int cmd_##N(co_obj_t *self, co_obj_t **output, co_obj_t *params)
 
#define CMD_REGISTER(N, U, D)   co_cmd_register(#N, sizeof(#N), U, sizeof(U), D, sizeof(D), cmd_##N )
 
#define CMD_OUTPUT(K, V)   if(*output == NULL) *output = co_tree16_create(); co_tree_insert(*output, K, sizeof(K), V)
 
#define HOOK(N)   static int hook_##N##(co_obj_t *self, co_obj_t **output, co_obj_t *params)
 
#define HOOK_REGISTER(N, C)   co_cmd_hook_str(N, sizeof(N), hook_##N)
 

Typedefs

typedef struct co_cmd_t co_cmd_t
 

Functions

void co_cmds_shutdown (void)
 
int co_cmds_init (const size_t index_size)
 
struct co_cmd_t __attribute__ ((packed))
 
int co_cmd_register (const char *name, const size_t nlen, const char *usage, const size_t ulen, const char *desc, const size_t dlen, co_cb_t handler)
 
int co_cmd_exec (co_obj_t *key, co_obj_t **output, co_obj_t *param)
 executes a command by running the function linked to in the command struct More...
 
co_obj_tco_cmd_usage (co_obj_t *key)
 returns command usage format More...
 
co_obj_tco_cmd_desc (co_obj_t *key)
 returns command description (what the command does) More...
 
int co_cmd_hook (const co_obj_t *key, co_obj_t *cb)
 hooks callback function into a command More...
 
int co_cmd_hook_str (const char *key, const size_t klen, co_obj_t *cb)
 hooks callback function into a command More...
 
int co_cmd_process (co_iter_t iter, void *context)
 process all registered commands with given iterator More...
 

Variables

co_obj_t _header
 
uint8_t _exttype
 
uint8_t _len
 
co_cb_t exec
 
co_obj_tname
 
co_obj_tusage
 
co_obj_tdesc
 
co_obj_thooks
 

Detailed Description

a mechanism for registering and controlling display of commands

Author
Josh King (jheretic), jking.nosp@m.@cha.nosp@m.mbana.nosp@m..net

Function Documentation

co_obj_t* co_cmd_desc ( co_obj_t key)

returns command description (what the command does)

Parameters
keythe name of the command

References co_tree_find(), and co_cmd_t::desc.

136 {
137  char *kstr = NULL;
138  size_t klen = co_obj_data(&kstr, key);
139  co_cmd_t *cmd = (co_cmd_t *)co_tree_find(_cmds, kstr, klen - 1);
140 
141  CHECK((cmd != NULL), "No such command!");
142  return cmd->desc;
143 error:
144  return NULL;
145 }
co_obj_t * desc
Definition: cmd.h:68
co_obj_t * co_tree_find(const co_obj_t *root, const char *key, const size_t klen)
return value from given tree that corresponds to key
Definition: tree.c:173
a struct containing all the relevant information for a specific command
Definition: cmd.h:61
int co_cmd_exec ( co_obj_t key,
co_obj_t **  output,
co_obj_t param 
)

executes a command by running the function linked to in the command struct

Parameters
keythe name of the command
outputreturn object of command

References co_tree_find(), and co_cmd_t::exec.

Referenced by dispatcher_cb().

110 {
111  char *kstr = NULL;
112  size_t klen = co_obj_data(&kstr, key);
113  co_cmd_t *cmd = (co_cmd_t *)co_tree_find(_cmds, kstr, klen - 1);
114 
115  CHECK((cmd != NULL), "No such command!");
116  return cmd->exec((co_obj_t *)cmd, output, param);
117 error:
118  return 0;
119 }
Definition: obj.h:131
co_cb_t exec
Definition: cmd.h:65
co_obj_t * co_tree_find(const co_obj_t *root, const char *key, const size_t klen)
return value from given tree that corresponds to key
Definition: tree.c:173
a struct containing all the relevant information for a specific command
Definition: cmd.h:61
int co_cmd_hook ( const co_obj_t key,
co_obj_t cb 
)

hooks callback function into a command

Parameters
keythe name of the command
cbreference to callback function

References co_list_append(), and co_tree_find().

165 {
166  char *kstr = NULL;
167  size_t klen = co_obj_data(&kstr, key);
168  co_cmd_t *cmd = (co_cmd_t *)co_tree_find(_cmds, kstr, klen);
169 
170  CHECK((cmd != NULL), "No such command!");
171  if(cmd->hooks == NULL)
172  {
173  cmd->hooks = co_tree16_create();
174  }
175  CHECK(co_list_append(cmd->hooks, cb), "Failed to add hook to command.");
176  return 1;
177 error:
178  return 0;
179 }
int co_list_append(co_obj_t *list, co_obj_t *new_obj)
insert new item at end of list
Definition: list.c:425
co_obj_t * co_tree_find(const co_obj_t *root, const char *key, const size_t klen)
return value from given tree that corresponds to key
Definition: tree.c:173
a struct containing all the relevant information for a specific command
Definition: cmd.h:61
int co_cmd_hook_str ( const char *  key,
const size_t  klen,
co_obj_t cb 
)

hooks callback function into a command

Parameters
keythe name of the command
klenlength of key string
cbreference to callback function

References co_list_append(), and co_tree_find().

149 {
150  co_cmd_t *cmd = (co_cmd_t *)co_tree_find(_cmds, key, klen);
151 
152  CHECK((cmd != NULL), "No such command!");
153  if(cmd->hooks == NULL)
154  {
155  cmd->hooks = co_tree16_create();
156  }
157  CHECK(co_list_append(cmd->hooks, cb), "Failed to add hook to command.");
158  return 1;
159 error:
160  return 0;
161 }
int co_list_append(co_obj_t *list, co_obj_t *new_obj)
insert new item at end of list
Definition: list.c:425
co_obj_t * co_tree_find(const co_obj_t *root, const char *key, const size_t klen)
return value from given tree that corresponds to key
Definition: tree.c:173
a struct containing all the relevant information for a specific command
Definition: cmd.h:61
int co_cmd_process ( co_iter_t  iter,
void *  context 
)

process all registered commands with given iterator

Parameters
iteriterator function reference
contextother parameters passed to iterator

References co_tree_process().

183 {
184  CHECK(co_tree_process(_cmds, iter, context), "Failed to process commands.");
185  return 1;
186 error:
187  return 0;
188 }
int co_tree_process(co_obj_t *tree, const co_iter_t iter, void *context)
process tree with given iterator function
Definition: tree.c:555
co_obj_t* co_cmd_usage ( co_obj_t key)

returns command usage format

Parameters
keycommand name

References co_tree_find(), and co_cmd_t::usage.

123 {
124  char *kstr = NULL;
125  size_t klen = co_obj_data(&kstr, key);
126  co_cmd_t *cmd = (co_cmd_t *)co_tree_find(_cmds, kstr, klen - 1);
127 
128  CHECK((cmd != NULL), "No such command!");
129  return cmd->usage;
130 error:
131  return NULL;
132 }
co_obj_t * co_tree_find(const co_obj_t *root, const char *key, const size_t klen)
return value from given tree that corresponds to key
Definition: tree.c:173
co_obj_t * usage
Definition: cmd.h:67
a struct containing all the relevant information for a specific command
Definition: cmd.h:61

Variable Documentation

co_obj_t* desc

description

Referenced by serval_crypto_register().

co_cb_t exec

pointer to the command's function

Referenced by co_process_start().

co_obj_t* usage

usage syntax

Referenced by serval_crypto_register().