From 05404983e2eedd4801abc7433de57db59a51ad85 Mon Sep 17 00:00:00 2001 From: Chris Johns Date: Wed, 18 Nov 2009 00:09:21 +0000 Subject: 2009-11-18 Chris Johns * libmisc/shell/main_getenv.c, libmisc/shell/main_setenv.c, libmisc/shell/main_unsetenv.c: New. The shell can now play with environment variables. * libmisc/Makefile.am, libmisc/shell/shellconfig.h: Add setenv, getenv, and unsetenv to the shell. --- cpukit/ChangeLog | 8 ++++ cpukit/libmisc/Makefile.am | 3 +- cpukit/libmisc/shell/main_getenv.c | 49 +++++++++++++++++++++++ cpukit/libmisc/shell/main_setenv.c | 75 ++++++++++++++++++++++++++++++++++++ cpukit/libmisc/shell/main_unsetenv.c | 43 +++++++++++++++++++++ cpukit/libmisc/shell/shellconfig.h | 18 +++++++++ 6 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 cpukit/libmisc/shell/main_getenv.c create mode 100644 cpukit/libmisc/shell/main_setenv.c create mode 100644 cpukit/libmisc/shell/main_unsetenv.c (limited to 'cpukit') diff --git a/cpukit/ChangeLog b/cpukit/ChangeLog index 4f0bbe9f35..a16d09ea8f 100644 --- a/cpukit/ChangeLog +++ b/cpukit/ChangeLog @@ -1,3 +1,11 @@ +2009-11-18 Chris Johns + + * libmisc/shell/main_getenv.c, libmisc/shell/main_setenv.c, + libmisc/shell/main_unsetenv.c: New. The shell can now play with + environment variables. + * libmisc/Makefile.am, libmisc/shell/shellconfig.h: Add setenv, + getenv, and unsetenv to the shell. + 2009-11-17 Ralf Corsépius * configure.ac: Add mghttpd/Makefile. diff --git a/cpukit/libmisc/Makefile.am b/cpukit/libmisc/Makefile.am index cd0988921c..8955978608 100644 --- a/cpukit/libmisc/Makefile.am +++ b/cpukit/libmisc/Makefile.am @@ -95,7 +95,8 @@ libshell_a_SOURCES = shell/cat_file.c shell/cmds.c shell/internal.h \ shell/dd-misc.c shell/dd-position.c \ shell/main_hexdump.c shell/hexdump-conv.c shell/hexdump-display.c \ shell/hexdump-odsyntax.c shell/hexdump-parse.c shell/hexsyntax.c \ - shell/main_time.c shell/main_mknod.c + shell/main_time.c shell/main_mknod.c \ + shell/main_setenv.c shell/main_getenv.c shell/main_unsetenv.c if LIBNETWORKING libshell_a_SOURCES += shell/main_mount_ftp.c shell/main_mount_tftp.c \ diff --git a/cpukit/libmisc/shell/main_getenv.c b/cpukit/libmisc/shell/main_getenv.c new file mode 100644 index 0000000000..d18c301ee3 --- /dev/null +++ b/cpukit/libmisc/shell/main_getenv.c @@ -0,0 +1,49 @@ +/* + * Get an environment vairable. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include +#include + +#include +#include +#include "internal.h" + +int rtems_shell_main_getenv(int argc, char *argv[]) +{ + char* string; + + if (argc != 2) + { + printf ("error: only argument is the variable name\n"); + return 1; + } + + string = getenv (argv[1]); + + if (!string) + { + printf ("error: %s not found\n", argv[1]); + return 1; + } + + printf ("%s\n", string); + + return 0; +} + +rtems_shell_cmd_t rtems_shell_GETENV_Command = { + "getenv", /* name */ + "getenv [var]", /* usage */ + "misc", /* topic */ + rtems_shell_main_getenv, /* command */ + NULL, /* alias */ + NULL /* next */ +}; diff --git a/cpukit/libmisc/shell/main_setenv.c b/cpukit/libmisc/shell/main_setenv.c new file mode 100644 index 0000000000..7164c27f9a --- /dev/null +++ b/cpukit/libmisc/shell/main_setenv.c @@ -0,0 +1,75 @@ +/* + * Set an environment vairable. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include +#include + +#include +#include +#include "internal.h" + +int rtems_shell_main_setenv(int argc, char *argv[]) +{ + char* env = NULL; + char* string = NULL; + int len = 0; + int arg; + char* p; + + if (argc <= 2) + { + printf ("error: no variable or string\n"); + return 1; + } + + env = argv[1]; + + for (arg = 2; arg < argc; arg++) + len += strlen (argv[arg]); + + len += argc - 2 - 1; + + string = malloc (len + 1); + + if (!string) + { + printf ("error: no memory\n"); + return 1; + } + + for (arg = 2, p = string; arg < argc; arg++) + { + strcpy (p, argv[arg]); + p += strlen (argv[arg]); + if (arg < (argc - 1)) + { + *p = ' '; + p++; + } + } + + if (setenv (env, string, 1) < 0) + { + printf ("error: %s\n", strerror (errno)); + return 1; + } + + return 0; +} + +rtems_shell_cmd_t rtems_shell_SETENV_Command = { + "setenv", /* name */ + "setenv [var] [string]", /* usage */ + "misc", /* topic */ + rtems_shell_main_setenv, /* command */ + NULL, /* alias */ + NULL /* next */ +}; diff --git a/cpukit/libmisc/shell/main_unsetenv.c b/cpukit/libmisc/shell/main_unsetenv.c new file mode 100644 index 0000000000..91d9dac7a4 --- /dev/null +++ b/cpukit/libmisc/shell/main_unsetenv.c @@ -0,0 +1,43 @@ +/* + * Unset an environment vairable. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include +#include + +#include +#include +#include "internal.h" + +int rtems_shell_main_unsetenv(int argc, char *argv[]) +{ + if (argc != 2) + { + printf ("error: only argument is the variable name\n"); + return 1; + } + + if (unsetenv (argv[1]) < 0) + { + printf ("error: %s\n", strerror (errno)); + return 1; + } + + return 0; +} + +rtems_shell_cmd_t rtems_shell_UNSETENV_Command = { + "unsetenv", /* name */ + "unsetenv [var]", /* usage */ + "misc", /* topic */ + rtems_shell_main_unsetenv, /* command */ + NULL, /* alias */ + NULL /* next */ +}; diff --git a/cpukit/libmisc/shell/shellconfig.h b/cpukit/libmisc/shell/shellconfig.h index 1410430440..3d731aa0ef 100644 --- a/cpukit/libmisc/shell/shellconfig.h +++ b/cpukit/libmisc/shell/shellconfig.h @@ -23,6 +23,9 @@ extern rtems_shell_cmd_t rtems_shell_HELP_Command; extern rtems_shell_cmd_t rtems_shell_ALIAS_Command; extern rtems_shell_cmd_t rtems_shell_TIME_Command; extern rtems_shell_cmd_t rtems_shell_LOGOFF_Command; +extern rtems_shell_cmd_t rtems_shell_SETENV_Command; +extern rtems_shell_cmd_t rtems_shell_GETENV_Command; +extern rtems_shell_cmd_t rtems_shell_UNSETENV_Command; extern rtems_shell_cmd_t rtems_shell_MDUMP_Command; extern rtems_shell_cmd_t rtems_shell_WDUMP_Command; @@ -195,6 +198,21 @@ extern rtems_shell_filesystems_t *rtems_shell_Mount_filesystems[]; defined(CONFIGURE_SHELL_COMMAND_LOGOFF) &rtems_shell_LOGOFF_Command, #endif + #if (defined(CONFIGURE_SHELL_COMMANDS_ALL) && \ + !defined(CONFIGURE_SHELL_NO_COMMAND_SETENV)) || \ + defined(CONFIGURE_SHELL_COMMAND_SETENV) + &rtems_shell_SETENV_Command, + #endif + #if (defined(CONFIGURE_SHELL_COMMANDS_ALL) && \ + !defined(CONFIGURE_SHELL_NO_COMMAND_GETENV)) || \ + defined(CONFIGURE_SHELL_COMMAND_GETENV) + &rtems_shell_GETENV_Command, + #endif + #if (defined(CONFIGURE_SHELL_COMMANDS_ALL) && \ + !defined(CONFIGURE_SHELL_NO_COMMAND_CRLENV)) || \ + defined(CONFIGURE_SHELL_COMMAND_UNSETENV) + &rtems_shell_UNSETENV_Command, + #endif /* * Memory printing/modification family commands -- cgit v1.2.3