1 /* *********************************************************************
2 * Broadcom Common Firmware Environment (CFE)
4 * UI Command Dispatch File: ui_cmddisp.c
6 * This module contains routines to maintain the command table,
7 * parse and execute commands
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 ********************************************************************* */
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"
59 #include "cfe_device.h"
60 #include "cfe_console.h"
61 #include "cfe_error.h"
64 #include "ui_command.h"
66 /* *********************************************************************
68 ********************************************************************* */
71 /* *********************************************************************
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
83 * errcode - code to translate
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.
104 * errcode - CFE error code
105 * tmplt,... - printf parameters
109 ********************************************************************* */
111 int ui_showerror(int errcode
,char *tmplt
,...)
115 va_start(marker
,tmplt
);
116 xvprintf(tmplt
,marker
);
118 xprintf(": %s\n",ui_errstring(errcode
));
124 /* *********************************************************************
125 * ui_do_one_command(head)
127 * Process one CLI command from a command queue
130 * head - queue of commands
133 * return value from CFE UI function
134 ********************************************************************* */
137 static int ui_do_one_command(queue_t
*head
)
142 res
= cmd_lookup(head
, &cmd
);
146 res
= cmd_sw_validate(&cmd
,cmd
.switches
);
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
]));
162 /* *********************************************************************
165 * Restart the command interpreter -- can be called if you get
166 * deep inside a UI routine and want to bail out.
169 * arg - value to return from ui_do_one_command
173 ********************************************************************* */
175 void ui_restart(int arg
)
177 if (arg
== 0) arg
= -1;
179 lib_longjmp(ui_jmpbuf
,arg
);
182 /* *********************************************************************
185 * Initialize the command dispatcher.
192 ********************************************************************* */
194 int ui_init_cmddisp(void)
201 /* *********************************************************************
204 * Display the usage for a command, given a command line
208 * cmd - command line structure (typically passed in from
209 * the command interpreter)
212 * CFE_ERR_INV_COMMAND
213 ********************************************************************* */
215 int ui_showusage(ui_cmdline_t
*cmd
)
219 return CFE_ERR_INV_COMMAND
;
223 /* *********************************************************************
224 * ui_docommands_internal(head)
226 * Process (possibly multiple) commands from a list of tokens
232 * exit status of first command that failed, or null
233 ********************************************************************* */
234 static int ui_docommands_internal(queue_t
*head
)
238 int status
= CMD_ERR_BLANK
;
244 * Find all the individual commands
247 while ((cmd
= cmd_readcommand(head
))) {
250 return CMD_ERR_BLANK
;
253 q_enqueue(&cmdqueue
,(queue_t
*) cmd
);
260 while ((cmd
= (ui_command_t
*) q_deqnext(&(cmdqueue
)))) {
261 status
= ui_do_one_command(&(cmd
->head
));
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
));
295 /* *********************************************************************
298 * Do one or more commands. This is the main UI function.
301 * str - command string
304 * return value of failing command, or 0 if all commands
306 ********************************************************************* */
308 int ui_docommands(char *str
)
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
);