diff --git a/ipmitool/src/Makefile.am b/ipmitool/src/Makefile.am index 263bc68..69c4c26 100644 --- a/ipmitool/src/Makefile.am +++ b/ipmitool/src/Makefile.am @@ -37,7 +37,7 @@ SUBDIRS = plugins MAINTAINERCLEANFILES = Makefile.in -ipmitool_SOURCES = ipmitool.c +ipmitool_SOURCES = ipmitool.c ipmishell.c ipmitool_LDADD = $(top_builddir)/lib/libipmitool.la plugins/libintf.la if IPMIEVD diff --git a/ipmitool/src/ipmishell.c b/ipmitool/src/ipmishell.c new file mode 100644 index 0000000..3950980 --- /dev/null +++ b/ipmitool/src/ipmishell.c @@ -0,0 +1,310 @@ +/* + * Copyright (c) 2003, 2004 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. + * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. + * SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE + * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING + * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL + * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, + * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR + * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF + * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, + * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + * You acknowledge that this software is not designed or intended for use + * in the design, construction, operation or maintenance of any nuclear + * facility. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#ifdef HAVE_CONFIG_H +# include +#endif + +#define EXEC_BUF_SIZE 1024 +#define EXEC_ARG_SIZE 32 + +extern void ipmi_cmd_print(void); +extern int ipmi_cmd_run(struct ipmi_intf * intf, char * name, int argc, char ** argv); + +extern const struct valstr ipmi_privlvl_vals[]; +extern const struct valstr ipmi_authtype_session_vals[]; + +#ifdef HAVE_READLINE + +#include +#include +#define RL_PROMPT "ipmitool> " +#define RL_TIMEOUT 30 + +static struct ipmi_intf * shell_intf; + +/* This function attempts to keep lan sessions active + * so they do not time out waiting for user input. The + * readline timeout is set to 1 second but lan session + * timeout is ~60 seconds. + */ +static int rl_event_keepalive(void) +{ + static int internal_timer = 0; + + if (!shell_intf) + return -1; + if (!shell_intf->keepalive) + return 0; + if (internal_timer++ < RL_TIMEOUT) + return 0; + + internal_timer = 0; + shell_intf->keepalive(shell_intf); + + return 0; +} + +int ipmi_shell_main(struct ipmi_intf * intf, int argc, char ** argv) +{ + char *pbuf, **ap, *__argv[20]; + int __argc, rc=0; + + rl_readline_name = "ipmitool"; + + /* this essentially disables command completion + * until its implemented right, otherwise we get + * the current directory contents... */ + rl_bind_key('\t', rl_insert); + + if (intf->keepalive) { + /* hook to keep lan sessions active */ + shell_intf = intf; + rl_event_hook = rl_event_keepalive; + /* set to 1 second */ + rl_set_keyboard_input_timeout(1000*1000); + } + + while ((pbuf = (char *)readline(RL_PROMPT)) != NULL) { + if (strlen(pbuf) == 0) { + free(pbuf); + continue; + } + if (!strncmp(pbuf, "quit", 4) || !strncmp(pbuf, "exit", 4)) { + free(pbuf); + return 0; + } + if (!strncmp(pbuf, "help", 4) || !strncmp(pbuf, "?", 1)) { + ipmi_cmd_print(); + free(pbuf); + continue; + } + + /* for the all-important up arrow :) */ + add_history(pbuf); + + __argc = 0; + for (ap = __argv; (*ap = strsep(&pbuf, " \t")) != NULL;) { + __argc++; + if (**ap != '\0') { + if (++ap >= &__argv[20]) + break; + } + } + + if (__argc && __argv[0]) + rc = ipmi_cmd_run(intf, __argv[0], __argc-1, &(__argv[1])); + + free(pbuf); + } + return rc; +} + +#else /* HAVE_READLINE */ + +int ipmi_shell_main(struct ipmi_intf * intf, int argc, char ** argv) +{ + printf("Compiled without readline support, shell is disabled.\n"); + return -1; +} + +#endif /* HAVE_READLINE */ + +static void ipmi_set_usage(void) +{ + printf("Usage: set