cmd.h
Go to the documentation of this file.
1 /* vim: set ts=2 expandtab: */
2 /**
3  * @file cmd.h
4  * @brief a mechanism for registering and controlling display of commands
5  *
6  * @author Josh King (jheretic), jking@chambana.net
7  *
8  * @internal
9  * Created 03/07/2013
10  * Revision $Id: doxygen.commotion.templates,v 0.1 2013/01/01 09:00:00 jheretic Exp $
11  * Compiler gcc/g++
12  * Company The Open Technology Institute
13  * Copyright Copyright (c) 2013, Josh King
14  *
15  * This file is part of Commotion, Copyright (c) 2013, Josh King
16  *
17  * Commotion is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU Affero General Public License as
19  * published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * Commotion is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Affero General Public License for more details.
26  *
27  * You should have received a copy of the GNU Affero General Public License
28  * along with Commotion. If not, see <http://www.gnu.org/licenses/>.
29  *
30  * =====================================================================================
31  */
32 
33 #ifndef _CMD_H
34 #define _CMD_H
35 
36 #include <stdlib.h>
37 #include <stddef.h>
38 #include <stdint.h>
39 #include "obj.h"
40 
41 #define CMD(N) static int cmd_##N(co_obj_t *self, co_obj_t **output, co_obj_t *params)
42 
43 #define CMD_REGISTER(N, U, D) co_cmd_register(#N, sizeof(#N), U, sizeof(U), D, sizeof(D), cmd_##N )
44 
45 #define CMD_OUTPUT(K, V) if(*output == NULL) *output = co_tree16_create(); co_tree_insert(*output, K, sizeof(K), V)
46 
47 #define HOOK(N) static int hook_##N##(co_obj_t *self, co_obj_t **output, co_obj_t *params)
48 
49 #define HOOK_REGISTER(N, C) co_cmd_hook_str(N, sizeof(N), hook_##N)
50 
51 typedef struct co_cmd_t co_cmd_t;
52 
53 void co_cmds_shutdown(void);
54 
55 int co_cmds_init(const size_t index_size);
56 
57 /**
58  * @struct co_cmd_t
59  * @brief a struct containing all the relevant information for a specific command
60  */
61 struct co_cmd_t {
62  co_obj_t _header;
63  uint8_t _exttype;
64  uint8_t _len;
65  co_cb_t exec; /**< pointer to the command's function */
66  co_obj_t *name; /**< command name */
67  co_obj_t *usage; /**< usage syntax */
68  co_obj_t *desc; /**< description */
69  co_obj_t *hooks;
70 } __attribute__((packed));
71 
72 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);
73 
74 /**
75  * @brief executes a command by running the function linked to in the command struct
76  * @param key the name of the command
77  * @param output return object of command
78  */
79 int co_cmd_exec(co_obj_t *key, co_obj_t **output, co_obj_t *param);
80 
81 /**
82  * @brief returns command usage format
83  * @param key command name
84  */
86 
87 /**
88  * @brief returns command description (what the command does)
89  * @param key the name of the command
90  */
92 
93 /**
94  * @brief hooks callback function into a command
95  * @param key the name of the command
96  * @param cb reference to callback function
97  */
98 int co_cmd_hook(const co_obj_t *key, co_obj_t *cb);
99 
100 /**
101  * @brief hooks callback function into a command
102  * @param key the name of the command
103  * @param klen length of key string
104  * @param cb reference to callback function
105  */
106 int co_cmd_hook_str(const char *key, const size_t klen, co_obj_t *cb);
107 
108 /**
109  * @brief process all registered commands with given iterator
110  * @param iter iterator function reference
111  * @param context other parameters passed to iterator
112  */
113 int co_cmd_process(co_iter_t iter, void *context);
114 #endif
co_obj_t * co_cmd_usage(co_obj_t *key)
returns command usage format
Definition: cmd.c:122
co_obj_t * name
Definition: cmd.h:66
co_obj_t * co_cmd_desc(co_obj_t *key)
returns command description (what the command does)
Definition: cmd.c:135
Commotion object model.
int co_cmd_process(co_iter_t iter, void *context)
process all registered commands with given iterator
Definition: cmd.c:182
co_obj_t * desc
Definition: cmd.h:68
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
Definition: cmd.c:109
int co_cmd_hook(const co_obj_t *key, co_obj_t *cb)
hooks callback function into a command
Definition: cmd.c:164
int co_cmd_hook_str(const char *key, const size_t klen, co_obj_t *cb)
hooks callback function into a command
Definition: cmd.c:148
Definition: obj.h:131
co_cb_t exec
Definition: cmd.h:65
co_obj_t * usage
Definition: cmd.h:61
co_obj_t * desc
Definition: cmd.h:62
co_obj_t * name
Definition: cmd.h:60
co_obj_t * usage
Definition: cmd.h:67
a struct containing all the relevant information for a specific command
Definition: cmd.h:61