From e62ecda06481671e9b95874ddd7daf81247e07cb Mon Sep 17 00:00:00 2001 From: patacongo Date: Tue, 12 Aug 2008 23:59:32 +0000 Subject: [PATCH] Add 'sh' to NSH git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@819 42af7a65-404d-4744-a932-0658087f49c3 --- ChangeLog | 4 ++++ Documentation/NuttX.html | 6 ++++- examples/README.txt | 6 ++--- examples/nsh/nsh.h | 10 ++++---- examples/nsh/nsh_fscmds.c | 47 +++++++++++++++++++++++++++++++++++++- examples/nsh/nsh_main.c | 3 +++ examples/nsh/nsh_serial.c | 26 ++++++++++++++++----- examples/nsh/nsh_telnetd.c | 18 +++++++++++++-- 8 files changed, 101 insertions(+), 19 deletions(-) diff --git a/ChangeLog b/ChangeLog index 93b6ce2979..98ecf7778c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -395,7 +395,11 @@ 0.3.13 2008-xx-xx Gregory Nutt * Added mkfatfs, mkfifo, sleep, usleep and nice commands to NSH + * Fixed problem with console input in Cygwin-based simulator; NSH now works + with simulator. * NSH will now execute commands in background * sched_get_priority_max/min returned error on SCHED_RR * Removed duplicate getenv() implementation in /lib + * Correct detection of End-of-File in fgets + * Implement sh and crude script handler in NSH diff --git a/Documentation/NuttX.html b/Documentation/NuttX.html index 93cf94966a..6356422c5c 100644 --- a/Documentation/NuttX.html +++ b/Documentation/NuttX.html @@ -8,7 +8,7 @@

NuttX RTOS

-

Last Updated: August 10, 2008

+

Last Updated: August 12, 2008

@@ -1029,9 +1029,13 @@ buildroot-0.1.0 2007-03-09 <spudmonkey@racsa.co.cr> nuttx-0.3.13 2008-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr> * Added mkfatfs, mkfifo, sleep, usleep and nice commands to NSH + * Fixed problem with console input in Cygwin-based simulator; NSH now works + with simulator. * NSH will now execute commands in background * sched_get_priority_max/min returned error on SCHED_RR * Removed duplicate getenv() implementation in /lib + * Correct detection of End-of-File in fgets + * Implement sh and crude script handler in NSH pascal-0.1.3 2008-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr> diff --git a/examples/README.txt b/examples/README.txt index 07a3f9d543..3dd7f14947 100644 --- a/examples/README.txt +++ b/examples/README.txt @@ -66,7 +66,8 @@ examples/nsh very large and will not be used unless this setting is 'y' * CONFIG_EXAMPLES_NSH_LINELEN - The maximum length of one command line. Default: 80 + The maximum length of one command line and of one output line. + Default: 80 * CONFIG_EXAMPLES_NSH_TELNET By default, NSH is configured to use the serial console. @@ -82,9 +83,6 @@ examples/nsh Determines the size of the I/O buffer to use for sending/ receiving TELNET commands/reponses - * CONFIG_EXAMPLES_NSH_CMD_SIZE - The size of one parsed NSH command - * CONFIG_EXAMPLES_NSH_STACKSIZE The stack size to use when spawning new threads as new TELNET connections are established. diff --git a/examples/nsh/nsh.h b/examples/nsh/nsh.h index a4b9c41602..0cc47274ab 100644 --- a/examples/nsh/nsh.h +++ b/examples/nsh/nsh.h @@ -76,10 +76,6 @@ # define CONFIG_EXAMPLES_NSH_IOBUFFER_SIZE 512 #endif -#ifndef CONFIG_EXAMPLES_NSH_CMD_SIZE -# define CONFIG_EXAMPLES_NSH_CMD_SIZE 40 -#endif - /* As threads are created to handle each request, a stack must be allocated * for the thread. Use a default if the user provided no stacksize. */ @@ -119,7 +115,7 @@ extern const char g_fmtcmdoutofmemory[]; /* Message handler */ -extern int nsh_parse(FAR void *handle, char *cmdline); +extern int nsh_parse(FAR void *handle, char *cmdline); /* I/O interfaces */ @@ -139,6 +135,7 @@ extern int nsh_serialmain(void); # define nsh_output(handle, ...) printf(__VA_ARGS__) #endif +extern char *nsh_linebuffer(FAR void *handle); /* Shell command handlers */ @@ -151,6 +148,9 @@ extern void cmd_ps(FAR void *handle, int argc, char **argv); extern void cmd_cat(FAR void *handle, int argc, char **argv); extern void cmd_cp(FAR void *handle, int argc, char **argv); extern void cmd_ls(FAR void *handle, int argc, char **argv); +# if CONFIG_NFILE_STREAMS > 0 + extern void cmd_sh(FAR void *handle, int argc, char **argv); +# endif /* CONFIG_NFILE_STREAMS */ # ifndef CONFIG_DISABLE_MOUNTPOINT extern void cmd_mkdir(FAR void *handle, int argc, char **argv); extern void cmd_mkfifo(FAR void *handle, int argc, char **argv); diff --git a/examples/nsh/nsh_fscmds.c b/examples/nsh/nsh_fscmds.c index 2cfb240982..7650c81449 100644 --- a/examples/nsh/nsh_fscmds.c +++ b/examples/nsh/nsh_fscmds.c @@ -742,7 +742,7 @@ void cmd_rm(FAR void *handle, int argc, char **argv) #endif /**************************************************************************** - * Name: cmd_rm + * Name: cmd_rmdir ****************************************************************************/ #if !defined(CONFIG_DISABLE_MOUNTPOINT) && CONFIG_NFILE_DESCRIPTORS > 0 @@ -755,6 +755,51 @@ void cmd_rmdir(FAR void *handle, int argc, char **argv) } #endif +/**************************************************************************** + * Name: cmd_sh + ****************************************************************************/ + +#if CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_NFILE_STREAMS > 0 +void cmd_sh(FAR void *handle, int argc, char **argv) +{ + FILE *stream; + char *buffer; + char *pret; + + /* Get a reference to the common input buffer */ + + buffer = nsh_linebuffer(handle); + if (buffer) + { + stream = fopen(argv[1], "r"); + if (!stream) + { + nsh_output(handle, g_fmtcmdfailed, argv[0], "fopen", NSH_ERRNO); + return; + } + + do + { + /* Get the next line of input from the file*/ + + fflush(stdout); + pret = fgets(buffer, CONFIG_EXAMPLES_NSH_LINELEN, stream); + if (pret) + { + /* Parse process the command. NOTE: this is recursive... + * we got to cmd_sh via a call to nsh_parse. So some + * considerable amount of stack may be used. + */ + + (void)nsh_parse(handle, buffer); + } + } + while(pret); + fclose(stream); + } +} +#endif + /**************************************************************************** * Name: cmd_umount ****************************************************************************/ diff --git a/examples/nsh/nsh_main.c b/examples/nsh/nsh_main.c index 4f6b624542..785062bc56 100644 --- a/examples/nsh/nsh_main.c +++ b/examples/nsh/nsh_main.c @@ -116,6 +116,9 @@ static const struct cmdmap_s g_cmdmap[] = { "rm", cmd_rm, 2, 2, "" }, { "rmdir", cmd_rmdir, 2, 2, "" }, #endif +#if CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_NFILE_STREAMS > 0 + { "sh", cmd_sh, 2, 2, "" }, +# endif /* CONFIG_NFILE_STREAMS */ #ifndef CONFIG_DISABLE_SIGNALS { "sleep", cmd_sleep, 2, 2, "" }, #endif /* CONFIG_DISABLE_SIGNALS */ diff --git a/examples/nsh/nsh_serial.c b/examples/nsh/nsh_serial.c index ad8ccad178..5052109d2c 100644 --- a/examples/nsh/nsh_serial.c +++ b/examples/nsh/nsh_serial.c @@ -71,7 +71,7 @@ struct cmdmap_s * Private Data ****************************************************************************/ -static char line[CONFIG_EXAMPLES_NSH_LINELEN]; +static char g_line[CONFIG_EXAMPLES_NSH_LINELEN]; /**************************************************************************** * Public Data @@ -103,15 +103,29 @@ int nsh_serialmain(void) /* Get the next line of input */ - fgets(line, CONFIG_EXAMPLES_NSH_LINELEN, stdin); + if (fgets(g_line, CONFIG_EXAMPLES_NSH_LINELEN, stdin)) + { + /* Parse process the command */ - /* Parse process the command */ - - (void)nsh_parse(NULL, line); - fflush(stdout); + (void)nsh_parse(NULL, g_line); + fflush(stdout); + } } } +/**************************************************************************** + * Name: nsh_linebuffer + * + * Description: + * Return a reference to the current line buffer + * + ****************************************************************************/ + +extern char *nsh_linebuffer(FAR void *handle) +{ + return g_line; +} + /**************************************************************************** * Name: cmd_exit * diff --git a/examples/nsh/nsh_telnetd.c b/examples/nsh/nsh_telnetd.c index 872390d5f8..0dbf6b18bb 100644 --- a/examples/nsh/nsh_telnetd.c +++ b/examples/nsh/nsh_telnetd.c @@ -90,7 +90,7 @@ struct telnetd_s uint8 tn_bufndx; uint8 tn_state; char tn_iobuffer[CONFIG_EXAMPLES_NSH_IOBUFFER_SIZE]; - char tn_cmd[CONFIG_EXAMPLES_NSH_CMD_SIZE]; + char tn_cmd[CONFIG_EXAMPLES_NSH_LINELEN]; }; /**************************************************************************** @@ -170,7 +170,7 @@ static void nsh_putchar(struct telnetd_s *pstate, uint8 ch) /* If a newline was added or if the buffer is full, then process it now */ - if (ch == ISO_nl || pstate->tn_bufndx == (CONFIG_EXAMPLES_NSH_CMD_SIZE - 1)) + if (ch == ISO_nl || pstate->tn_bufndx == (CONFIG_EXAMPLES_NSH_LINELEN - 1)) { if (pstate->tn_bufndx > 0) { @@ -550,6 +550,20 @@ int nsh_telnetout(FAR void *handle, const char *fmt, ...) return len; } +/**************************************************************************** + * Name: nsh_linebuffer + * + * Description: + * Return a reference to the current line buffer + * + ****************************************************************************/ + +extern char *nsh_linebuffer(FAR void *handle) +{ + struct telnetd_s *pstate = (struct telnetd_s *)handle; + return pstate->tn_cmd; +} + /**************************************************************************** * Name: cmd_exit *