2 * Copyright (c) 2012, Intel Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of Intel Corporation nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #ifndef __MURPHY_CONSOLE_COMMAND_H__
31 #define __MURPHY_CONSOLE_COMMAND_H__
34 /** Macro to declare an array of console commands. */
35 #define MRP_CONSOLE_COMMANDS(_var, ...) \
36 static mrp_console_cmd_t _var[] = __VA_ARGS__
38 /** Macro to declare a console command group. */
39 #define MRP_CONSOLE_GROUP(_var, _name, _descr, _data, ...) \
40 MRP_CONSOLE_COMMANDS(_var##_cmds, __VA_ARGS__); \
41 static mrp_console_group_t _var = { \
42 .name = (char *)_name, \
45 .commands = _var##_cmds, \
46 .ncommand = MRP_ARRAY_SIZE(_var##_cmds), \
47 .hook = MRP_LIST_INIT(_var.hook), \
50 /** Macro to declare a console command that wants tokenized input. */
51 #define MRP_TOKENIZED_CMD(_name, _cb, _flags, _syntax, _summ, _descr) { \
55 .description = _descr, \
56 .flags = ((_flags) == 0x1 ? MRP_CONSOLE_SELECTABLE : _flags), \
60 /** Macro to declare a console command that wants a raw input. */
61 #define MRP_RAWINPUT_CMD(_name, _cb, _flags, _syntax, _summ, _descr) { \
65 .description = _descr, \
66 .flags = MRP_CONSOLE_RAWINPUT | \
67 ((_flags) == 0x1 ? MRP_CONSOLE_SELECTABLE : _flags), \
71 typedef struct mrp_console_s mrp_console_t;
75 * console command flags
79 MRP_CONSOLE_TOKENIZE = 0x0, /* wants tokenized input */
80 MRP_CONSOLE_RAWINPUT = 0x2, /* wants raw input */
81 MRP_CONSOLE_SELECTABLE = 0x4, /* selectable as command mode */
82 MRP_CONSOLE_CATCHALL = 0x8, /* catch-all command handler */
91 const char *name; /* command name */
92 const char *syntax; /* command syntax */
93 const char *summary; /* short help */
94 const char *description; /* long command description */
95 mrp_console_flag_t flags; /* command flags */
96 union { /* tokenized or raw input cb */
97 void (*tok)(mrp_console_t *c, void *user_data, int argc, char **argv);
98 void (*raw)(mrp_console_t *c, void *user_data, const char *grp,
99 const char *cmd, char *args);
105 * a group of console commands
109 char *name; /* command group name/prefix */
110 char *descr; /* group description */
111 void *user_data; /* opaque callback data */
112 mrp_console_cmd_t *commands; /* commands in this group */
113 int ncommand; /* number of commands */
114 mrp_list_hook_t hook; /* to list of command groups */
115 } mrp_console_group_t;
118 /** Register a console command group. */
119 int mrp_console_add_group(mrp_context_t *ctx, mrp_console_group_t *group);
121 /** Unregister a console command group. */
122 int mrp_console_del_group(mrp_context_t *ctx, mrp_console_group_t *group);
124 /** Convenience macro to register a group of core commands. */
125 #define MRP_CORE_CONSOLE_GROUP(_var, _name, _descr, _data, ...) \
126 MRP_CONSOLE_GROUP(_var, _name, _descr, _data, __VA_ARGS__); \
128 static void _var##_register_core_group(void) \
129 __attribute__((constructor)); \
131 static void __attribute__((constructor)) \
132 _var##_register_core_group(void) { \
133 mrp_console_add_core_group(&_var); \
136 static void __attribute__((destructor)) \
137 _var##_unregister_core_group(void) { \
138 mrp_console_del_core_group(&_var); \
140 struct mrp_allow_trailing_semicolon
142 /** Pre-register a group of core commands to register later to any context. */
143 int mrp_console_add_core_group(mrp_console_group_t *group);
145 /** Unregister a pre-registered group of core commands. */
146 int mrp_console_del_core_group(mrp_console_group_t *group);
148 #endif /* __MURPHY_CONSOLE_COMMAND_H__ */