diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2008-08-14 23:37:12 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2008-08-14 23:37:12 +0000 |
commit | 24738c1122691d74a8a3b7713c2878d5ed32486c (patch) | |
tree | 95474c328e4af9e7a046267ac03eaa3eeceb144f /nuttx/examples/nsh/nsh_serial.c | |
parent | 3169482b6c251e6c532b96c7af5ce2b181660706 (diff) | |
download | px4-nuttx-24738c1122691d74a8a3b7713c2878d5ed32486c.tar.gz px4-nuttx-24738c1122691d74a8a3b7713c2878d5ed32486c.tar.bz2 px4-nuttx-24738c1122691d74a8a3b7713c2878d5ed32486c.zip |
Fix reentrancy problems for serial
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@821 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/examples/nsh/nsh_serial.c')
-rw-r--r-- | nuttx/examples/nsh/nsh_serial.c | 108 |
1 files changed, 95 insertions, 13 deletions
diff --git a/nuttx/examples/nsh/nsh_serial.c b/nuttx/examples/nsh/nsh_serial.c index 5052109d2..fee653266 100644 --- a/nuttx/examples/nsh/nsh_serial.c +++ b/nuttx/examples/nsh/nsh_serial.c @@ -43,6 +43,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <stdarg.h> #include "nsh.h" @@ -54,13 +55,10 @@ * Private Types ****************************************************************************/ -struct cmdmap_s +struct serial_s { - const char *cmd; /* Name of the command */ - cmd_t handler; /* Function that handles the command */ - ubyte minargs; /* Minimum number of arguments (including command) */ - ubyte maxargs; /* Maximum number of arguments (including command) */ - const char *usage; /* Usage instructions for 'help' command */ + int ss_refs; /* Reference counts on the intance */ + char ss_line[CONFIG_EXAMPLES_NSH_LINELEN]; }; /**************************************************************************** @@ -71,8 +69,6 @@ struct cmdmap_s * Private Data ****************************************************************************/ -static char g_line[CONFIG_EXAMPLES_NSH_LINELEN]; - /**************************************************************************** * Public Data ****************************************************************************/ @@ -82,6 +78,20 @@ static char g_line[CONFIG_EXAMPLES_NSH_LINELEN]; ****************************************************************************/ /**************************************************************************** + * Name: nsh_allocstruct + ****************************************************************************/ + +static inline FAR struct serial_s *nsh_allocstruct(void) +{ + struct serial_s *pstate = (struct serial_s *)malloc(sizeof(struct serial_s)); + if (pstate) + { + pstate->ss_refs = 1; + } + return pstate; +} + +/**************************************************************************** * Public Functions ****************************************************************************/ @@ -89,8 +99,10 @@ static char g_line[CONFIG_EXAMPLES_NSH_LINELEN]; * Name: nsh_main ****************************************************************************/ -int nsh_serialmain(void) +int nsh_main(void) { + FAR struct serial_s *pstate = nsh_allocstruct(); + printf("NuttShell (NSH)\n"); fflush(stdout); @@ -103,17 +115,37 @@ int nsh_serialmain(void) /* Get the next line of input */ - if (fgets(g_line, CONFIG_EXAMPLES_NSH_LINELEN, stdin)) + if (fgets(pstate->ss_line, CONFIG_EXAMPLES_NSH_LINELEN, stdin)) { /* Parse process the command */ - (void)nsh_parse(NULL, g_line); + (void)nsh_parse(pstate, pstate->ss_line); fflush(stdout); } } } /**************************************************************************** + * Name: nsh_output + * + * Description: + * Print a string to stdout. + * + ****************************************************************************/ + +int nsh_output(FAR void *handle, const char *fmt, ...) +{ + va_list ap; + int ret; + + va_start(ap, fmt); + ret = vfprintf(stdout, fmt, ap); + va_end(ap); + + return ret; +} + +/**************************************************************************** * Name: nsh_linebuffer * * Description: @@ -121,9 +153,59 @@ int nsh_serialmain(void) * ****************************************************************************/ -extern char *nsh_linebuffer(FAR void *handle) +FAR char *nsh_linebuffer(FAR void *handle) +{ + FAR struct serial_s *pstate = (FAR struct serial_s *)handle; + return pstate->ss_line; +} + +/**************************************************************************** + * Name: nsh_clone + * + * Description: + * Make an independent copy of the handle + * + ****************************************************************************/ + +FAR void *nsh_clone(FAR void *handle) { - return g_line; + return nsh_allocstruct(); +} + +/**************************************************************************** + * Name: nsh_addref + * + * Description: + * Increment the reference count on the handle. + * + ****************************************************************************/ + +void nsh_addref(FAR void *handle) +{ + FAR struct serial_s *pstate = (FAR struct serial_s *)handle; + pstate->ss_refs++; +} + +/**************************************************************************** + * Name: nsh_release + * + * Description: + * Decrement the reference count on the handle, releasing it when the count + * decrements to zero. + * + ****************************************************************************/ + +void nsh_release(FAR void *handle) +{ + FAR struct serial_s *pstate = (FAR struct serial_s *)handle; + if (pstate->ss_refs > 1) + { + pstate->ss_refs--; + } + else + { + free(handle); + } } /**************************************************************************** |