gam-resource-manager: adjust to updated proxied call callback signature.
[profile/ivi/murphy.git] / src / core / console-builtin.c
1 /*
2  * Copyright (c) 2012, Intel Corporation
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
30
31 #define DOTS "........................................................" \
32              "......................."
33
34 #define NPRINT(mc, fmt, args...) fprintf(mc->stdout, fmt , ## args)
35 #define EPRINT(mc, fmt, args...) fprintf(mc->stderr, fmt , ## args)
36
37
38
39 /*
40  * top-level console commands
41  */
42
43
44 static void get_string_lengthes(mrp_console_t *mc,
45                                 size_t *nmaxp, size_t *smaxp, size_t *tmaxp)
46 {
47     mrp_console_group_t *grp;
48     mrp_console_cmd_t   *cmd;
49     mrp_list_hook_t     *p, *n;
50     int                  i;
51     size_t               nlen, slen, tmax, nmax, smax;
52
53     tmax = nmax = smax = 0;
54
55     mrp_list_foreach(&mc->ctx->cmd_groups, p, n) {
56         grp = mrp_list_entry(p, typeof(*grp), hook);
57
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);
64         }
65     }
66
67     mrp_list_foreach(&core_groups, p, n) {
68         grp = mrp_list_entry(p, typeof(*grp), hook);
69
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);
76         }
77     }
78
79     *nmaxp = nmax;
80     *smaxp = smax;
81     *tmaxp = tmax;
82 }
83
84
85 static void help_overview(mrp_console_t *mc)
86 {
87     mrp_console_group_t *grp;
88     mrp_console_cmd_t   *cmd;
89     mrp_list_hook_t     *p, *n;
90     int                  i, l, dend;
91     size_t               tmax, nmax, smax;
92
93     get_string_lengthes(mc, &nmax, &smax, &tmax);
94
95     if (4 + 2 + 2 + tmax < 79) {
96         dend = 79 - smax - 2;
97     }
98     else
99         dend = tmax + 20;
100
101     NPRINT(mc, "The following commands are available:\n\n");
102
103     mrp_list_foreach(&mc->ctx->cmd_groups, p, n) {
104         grp = mrp_list_entry(p, typeof(*grp), hook);
105
106         if (*grp->name)
107             NPRINT(mc, "  commands in group '%s':\n", grp->name);
108         else
109             NPRINT(mc, "  general commands:\n");
110
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);
114         }
115
116         NPRINT(mc, "\n");
117     }
118
119     mrp_list_foreach(&core_groups, p, n) {
120         grp = mrp_list_entry(p, typeof(*grp), hook);
121
122         if (*grp->name)
123             NPRINT(mc, "  commands in group '%s':\n", grp->name);
124         else
125             NPRINT(mc, "  general commands:\n");
126
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);
130         }
131
132         NPRINT(mc, "\n");
133     }
134 }
135
136
137 static void help_group(mrp_console_t *mc, const char *name)
138 {
139     mrp_console_group_t *grp;
140     mrp_console_cmd_t   *cmd;
141     mrp_list_hook_t     *p, *n;
142     const char          *t;
143     int                  i;
144
145     grp = find_group(mc->ctx, name);
146
147     if (grp != NULL) {
148         if (grp->descr != NULL)
149             NPRINT(mc, "%s\n", grp->descr);
150
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 ? " " : "",
155                    cmd->syntax);
156             NPRINT(mc, "%s\n", cmd->description);
157         }
158     }
159     else {
160         EPRINT(mc, "Command group '%s' does not exist.\n", name);
161         EPRINT(mc, "The existing groups are: ");
162         t = "";
163         mrp_list_foreach(&mc->ctx->cmd_groups, p, n) {
164             grp = mrp_list_entry(p, typeof(*grp), hook);
165             if (*grp->name) {
166                 EPRINT(mc, "%s'%s'", t, grp->name);
167                 t = ", ";
168             }
169         }
170         EPRINT(mc, ".\n");
171     }
172
173
174 }
175
176
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"        \
181     "single command.\n"
182
183 static void cmd_help(mrp_console_t *mc, void *user_data, int argc, char **argv)
184 {
185     console_t *c = (console_t *)mc;
186     char      *ha[2];
187
188     MRP_UNUSED(c);
189
190     switch (argc) {
191     case 2:
192         help_overview(mc);
193         break;
194
195     case 3:
196         help_group(mc, argv[2]);
197         break;
198
199     case 4:
200         fprintf(mc->stdout, "Help for command '%s/%s'.\n", argv[2], argv[3]);
201         break;
202
203     default:
204         ha[0] = "help";
205         ha[1] = "help";
206         fprintf(mc->stderr, "help: invalid arguments (%d).\n", argc);
207         fflush(mc->stderr);
208         cmd_help(mc, user_data, 2, ha);
209     }
210 }
211
212
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"
217
218
219 static void cmd_exit(mrp_console_t *mc, void *user_data, int argc, char **argv)
220 {
221     console_t *c = (console_t *)mc;
222     char      *ha[2];
223
224     switch (argc) {
225     case 2:
226         if (c->grp != NULL) {
227             if (c->cmd != NULL)
228                 c->cmd = NULL;
229             else
230                 c->grp = NULL;
231         }
232         else {
233         close_console:
234             fprintf(mc->stdout, "Bye.\n");
235             mrp_destroy_console(mc);
236         }
237         break;
238
239     case 3:
240         if (!strcmp(argv[2], "console"))
241             goto close_console;
242         /* intentional fall-through */
243
244     default:
245         ha[0] = "help";
246         ha[1] = "exit";
247         fprintf(mc->stderr, "exit: invalid arguments\n");
248         cmd_help(mc, user_data, 2, ha);
249     }
250 }
251
252
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),
258 });