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.
31 #define DOTS "........................................................" \
32 "......................."
34 #define NPRINT(mc, fmt, args...) fprintf(mc->stdout, fmt , ## args)
35 #define EPRINT(mc, fmt, args...) fprintf(mc->stderr, fmt , ## args)
40 * top-level console commands
44 static void get_string_lengthes(mrp_console_t *mc,
45 size_t *nmaxp, size_t *smaxp, size_t *tmaxp)
47 mrp_console_group_t *grp;
48 mrp_console_cmd_t *cmd;
49 mrp_list_hook_t *p, *n;
51 size_t nlen, slen, tmax, nmax, smax;
53 tmax = nmax = smax = 0;
55 mrp_list_foreach(&mc->ctx->cmd_groups, p, n) {
56 grp = mrp_list_entry(p, typeof(*grp), hook);
58 for (i = 0, cmd = grp->commands; i < grp->ncommand; i++, cmd++) {
59 nlen = strlen(cmd->name);
60 slen = strlen(cmd->summary);
61 nmax = MRP_MAX(nmax, nlen);
62 smax = MRP_MAX(smax, slen);
63 tmax = MRP_MAX(tmax, nlen + slen);
67 mrp_list_foreach(&core_groups, p, n) {
68 grp = mrp_list_entry(p, typeof(*grp), hook);
70 for (i = 0, cmd = grp->commands; i < grp->ncommand; i++, cmd++) {
71 nlen = strlen(cmd->name);
72 slen = strlen(cmd->summary);
73 nmax = MRP_MAX(nmax, nlen);
74 smax = MRP_MAX(smax, slen);
75 tmax = MRP_MAX(tmax, nlen + slen);
85 static void help_overview(mrp_console_t *mc)
87 mrp_console_group_t *grp;
88 mrp_console_cmd_t *cmd;
89 mrp_list_hook_t *p, *n;
91 size_t tmax, nmax, smax;
93 get_string_lengthes(mc, &nmax, &smax, &tmax);
95 if (4 + 2 + 2 + tmax < 79) {
101 NPRINT(mc, "The following commands are available:\n\n");
103 mrp_list_foreach(&mc->ctx->cmd_groups, p, n) {
104 grp = mrp_list_entry(p, typeof(*grp), hook);
107 NPRINT(mc, " commands in group '%s':\n", grp->name);
109 NPRINT(mc, " general commands:\n");
111 for (i = 0, cmd = grp->commands; i < grp->ncommand; i++, cmd++) {
112 NPRINT(mc, " %s %n", cmd->name, &l);
113 NPRINT(mc, "%*.*s %s\n", dend - l, dend - l, DOTS, cmd->summary);
119 mrp_list_foreach(&core_groups, p, n) {
120 grp = mrp_list_entry(p, typeof(*grp), hook);
123 NPRINT(mc, " commands in group '%s':\n", grp->name);
125 NPRINT(mc, " general commands:\n");
127 for (i = 0, cmd = grp->commands; i < grp->ncommand; i++, cmd++) {
128 NPRINT(mc, " %s %n", cmd->name, &l);
129 NPRINT(mc, "%*.*s %s\n", dend - l, dend - l, DOTS, cmd->summary);
137 static void help_group(mrp_console_t *mc, const char *name)
139 mrp_console_group_t *grp;
140 mrp_console_cmd_t *cmd;
141 mrp_list_hook_t *p, *n;
145 grp = find_group(mc->ctx, name);
148 if (grp->descr != NULL)
149 NPRINT(mc, "%s\n", grp->descr);
151 NPRINT(mc, "The following commands are available:\n");
152 for (i = 0, cmd = grp->commands; i < grp->ncommand; i++, cmd++) {
153 NPRINT(mc, "- %s (syntax: %s%s%s)\n\n", cmd->name,
154 grp->name ? grp->name : "", grp->name ? " " : "",
156 NPRINT(mc, "%s\n", cmd->description);
160 EPRINT(mc, "Command group '%s' does not exist.\n", name);
161 EPRINT(mc, "The existing groups are: ");
163 mrp_list_foreach(&mc->ctx->cmd_groups, p, n) {
164 grp = mrp_list_entry(p, typeof(*grp), hook);
166 EPRINT(mc, "%s'%s'", t, grp->name);
177 #define HELP_SYNTAX "help [group|command]"
178 #define HELP_SUMMARY "print help on a command group or a command"
179 #define HELP_DESCRIPTION \
180 "Give general help or help on a specific command group or a\n" \
183 static void cmd_help(mrp_console_t *mc, void *user_data, int argc, char **argv)
185 console_t *c = (console_t *)mc;
196 help_group(mc, argv[2]);
200 fprintf(mc->stdout, "Help for command '%s/%s'.\n", argv[2], argv[3]);
206 fprintf(mc->stderr, "help: invalid arguments (%d).\n", argc);
208 cmd_help(mc, user_data, 2, ha);
213 #define EXIT_SYNTAX "exit"
214 #define EXIT_SUMMARY "exit from a command group or the console"
215 #define EXIT_DESCRIPTION \
216 "Exit current console mode, or close the console.\n"
219 static void cmd_exit(mrp_console_t *mc, void *user_data, int argc, char **argv)
221 console_t *c = (console_t *)mc;
226 if (c->grp != NULL) {
234 fprintf(mc->stdout, "Bye.\n");
235 mrp_destroy_console(mc);
240 if (!strcmp(argv[2], "console"))
242 /* intentional fall-through */
247 fprintf(mc->stderr, "exit: invalid arguments\n");
248 cmd_help(mc, user_data, 2, ha);
253 MRP_CONSOLE_GROUP(builtin_cmd_group, "", NULL, NULL, {
254 MRP_TOKENIZED_CMD("help", cmd_help, FALSE,
255 HELP_SYNTAX, HELP_SUMMARY, HELP_DESCRIPTION),
256 MRP_TOKENIZED_CMD("exit", cmd_exit, FALSE,
257 EXIT_SYNTAX, EXIT_SUMMARY, EXIT_DESCRIPTION),