RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / cfe / cfe / ui / ui_cmddisp.c
blobacd53cad1ffed2dea303859337801c88bf5f618a
1 /* *********************************************************************
2 * Broadcom Common Firmware Environment (CFE)
3 *
4 * UI Command Dispatch File: ui_cmddisp.c
5 *
6 * This module contains routines to maintain the command table,
7 * parse and execute commands
8 *
9 * Author: Mitch Lichtenberg (mpl@broadcom.com)
11 *********************************************************************
13 * Copyright 2000,2001,2002,2003
14 * Broadcom Corporation. All rights reserved.
16 * This software is furnished under license and may be used and
17 * copied only in accordance with the following terms and
18 * conditions. Subject to these conditions, you may download,
19 * copy, install, use, modify and distribute modified or unmodified
20 * copies of this software in source and/or binary form. No title
21 * or ownership is transferred hereby.
23 * 1) Any source code used, modified or distributed must reproduce
24 * and retain this copyright notice and list of conditions
25 * as they appear in the source file.
27 * 2) No right is granted to use any trade name, trademark, or
28 * logo of Broadcom Corporation. The "Broadcom Corporation"
29 * name may not be used to endorse or promote products derived
30 * from this software without the prior written permission of
31 * Broadcom Corporation.
33 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
34 * IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
35 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
36 * PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
37 * SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
38 * PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
39 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
41 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
42 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
43 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
44 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
45 * THE POSSIBILITY OF SUCH DAMAGE.
46 ********************************************************************* */
49 #include <stdarg.h>
51 #include "lib_types.h"
52 #include "lib_string.h"
53 #include "lib_queue.h"
54 #include "lib_malloc.h"
55 #include "lib_printf.h"
56 #include "lib_setjmp.h"
58 #include "cfe_iocb.h"
59 #include "cfe_device.h"
60 #include "cfe_console.h"
61 #include "cfe_error.h"
62 #include "env_subr.h"
63 #include "cfe.h"
64 #include "ui_command.h"
66 /* *********************************************************************
67 * Types
68 ********************************************************************* */
71 /* *********************************************************************
72 * Globals
73 ********************************************************************* */
75 static jmp_buf ui_jmpbuf; /* for getting control in exceptions */
77 /* *********************************************************************
78 * ui_errstring(errcode)
80 * Return a string that corresponds to a CFE error code
82 * Input parameters:
83 * errcode - code to translate
85 * Return value:
86 * string describing error code
87 ********************************************************************* */
89 const char *ui_errstring(int errcode)
91 return cfe_errortext(errcode);
95 /* *********************************************************************
96 * ui_showerror(errcode,tmplt,...)
98 * printf-like function to display a CFE error code along with
99 * other information. Works sort of like 'perror'. You can
100 * return the value passed to this routine to a CFE UI handler
101 * since it returns its input.
103 * Input parameters:
104 * errcode - CFE error code
105 * tmplt,... - printf parameters
107 * Return value:
108 * errcode
109 ********************************************************************* */
111 int ui_showerror(int errcode,char *tmplt,...)
113 va_list marker;
115 va_start(marker,tmplt);
116 xvprintf(tmplt,marker);
117 va_end(marker);
118 xprintf(": %s\n",ui_errstring(errcode));
120 return errcode;
124 /* *********************************************************************
125 * ui_do_one_command(head)
127 * Process one CLI command from a command queue
129 * Input parameters:
130 * head - queue of commands
132 * Return value:
133 * return value from CFE UI function
134 ********************************************************************* */
137 static int ui_do_one_command(queue_t *head)
139 int res;
140 ui_cmdline_t cmd;
142 res = cmd_lookup(head, &cmd);
144 if (res == 0) {
146 res = cmd_sw_validate(&cmd,cmd.switches);
147 if (res != -1) {
148 xprintf("Invalid switch: %s\n",
149 cmd_sw_name(&cmd,res));
150 return CFE_ERR_INV_PARAM;
153 if (lib_setjmp(ui_jmpbuf) != 0) return -1;
154 res = (*cmd.func)(&cmd,cmd.argc-cmd.argidx,
155 &(cmd.argv[cmd.argidx]));
157 cmd_free(&cmd);
158 return res;
162 /* *********************************************************************
163 * ui_restart(arg)
165 * Restart the command interpreter -- can be called if you get
166 * deep inside a UI routine and want to bail out.
168 * Input parameters:
169 * arg - value to return from ui_do_one_command
171 * Return value:
172 * nothing
173 ********************************************************************* */
175 void ui_restart(int arg)
177 if (arg == 0) arg = -1;
179 lib_longjmp(ui_jmpbuf,arg);
182 /* *********************************************************************
183 * ui_init_cmddisp()
185 * Initialize the command dispatcher.
187 * Input parameters:
188 * nothing
190 * Return value:
192 ********************************************************************* */
194 int ui_init_cmddisp(void)
196 cmd_init();
198 return 0;
201 /* *********************************************************************
202 * ui_showusage(cmd)
204 * Display the usage for a command, given a command line
205 * structure
207 * Input parameters:
208 * cmd - command line structure (typically passed in from
209 * the command interpreter)
211 * Return value:
212 * CFE_ERR_INV_COMMAND
213 ********************************************************************* */
215 int ui_showusage(ui_cmdline_t *cmd)
217 cmd_showusage(cmd);
219 return CFE_ERR_INV_COMMAND;
223 /* *********************************************************************
224 * ui_docommands_internal(head)
226 * Process (possibly multiple) commands from a list of tokens
228 * Input parameters:
229 * buf - buffer
231 * Return value:
232 * exit status of first command that failed, or null
233 ********************************************************************* */
234 static int ui_docommands_internal(queue_t *head)
236 queue_t cmdqueue;
237 ui_command_t *cmd;
238 int status = CMD_ERR_BLANK;
239 int term;
241 q_init(&cmdqueue);
244 * Find all the individual commands
247 while ((cmd = cmd_readcommand(head))) {
249 if (cmd == NULL) {
250 return CMD_ERR_BLANK;
253 q_enqueue(&cmdqueue,(queue_t *) cmd);
257 * Do each command
260 while ((cmd = (ui_command_t *) q_deqnext(&(cmdqueue)))) {
261 status = ui_do_one_command(&(cmd->head));
262 term = cmd->term;
263 KFREE(cmd);
264 if (status == CMD_ERR_BLANK) continue;
267 * And causes us to stop at the first failure.
269 if ((term == CMD_TERM_AND) && (status != 0)) break;
272 * OR causes us to stop at the first success.
275 if ((term == CMD_TERM_OR) && (status == 0)) break;
278 * Neither AND nor OR causes us to keep chugging away.
283 * Free any remaining tokens and commands that we did not do
286 while ((cmd = (ui_command_t *) q_deqnext(&(cmdqueue)))) {
287 cmd_free_tokens(&(cmd->head));
288 KFREE(cmd);
291 return status;
295 /* *********************************************************************
296 * ui_docommands(str)
298 * Do one or more commands. This is the main UI function.
300 * Input parameters:
301 * str - command string
303 * Return value:
304 * return value of failing command, or 0 if all commands
305 * succeeded
306 ********************************************************************* */
308 int ui_docommands(char *str)
310 queue_t cmd_list;
311 int res;
313 /* Convert the command into a token list */
314 cmd_build_list(&cmd_list,str);
316 /* Walk the list and expand environment variables */
317 cmd_walk_and_expand(&cmd_list);
319 /* Process each command. This removes tokens from the list */
320 res = ui_docommands_internal(&cmd_list);
322 /* Free any leftover tokens. There should not be any. */
323 cmd_free_tokens(&cmd_list);
325 return res;