diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2013-02-10 00:46:27 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2013-02-10 00:46:27 +0000 |
commit | f0fef8e21fd0459b75b3e5faf318634e5befbdaf (patch) | |
tree | d9451e3904a1b2e84bce6b183ccaca719b78ff51 | |
parent | 9768bf44e8f274eecc92f4eefbf0496f32649196 (diff) | |
download | px4-nuttx-f0fef8e21fd0459b75b3e5faf318634e5befbdaf.tar.gz px4-nuttx-f0fef8e21fd0459b75b3e5faf318634e5befbdaf.tar.bz2 px4-nuttx-f0fef8e21fd0459b75b3e5faf318634e5befbdaf.zip |
Fix a readline bug. If a NUL is received, it would return end-of-file
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@5633 42af7a65-404d-4744-a932-0658087f49c3
-rw-r--r-- | apps/ChangeLog.txt | 5 | ||||
-rw-r--r-- | apps/examples/cdcacm/Kconfig | 1 | ||||
-rw-r--r-- | apps/examples/ftpc/ftpc_main.c | 11 | ||||
-rw-r--r-- | apps/examples/usbterm/usbterm_main.c | 9 | ||||
-rw-r--r-- | apps/include/readline.h | 6 | ||||
-rw-r--r-- | apps/nshlib/nsh_consolemain.c | 2 | ||||
-rw-r--r-- | apps/nshlib/nsh_session.c | 11 | ||||
-rw-r--r-- | apps/system/readline/readline.c | 22 | ||||
-rw-r--r-- | nuttx/ChangeLog | 2 | ||||
-rw-r--r-- | nuttx/arch/arm/src/stm32/stm32_serial.c | 2 | ||||
-rw-r--r-- | nuttx/configs/stm32f3discovery/nsh/defconfig | 15 | ||||
-rw-r--r-- | nuttx/drivers/serial/serial.c | 8 |
12 files changed, 51 insertions, 43 deletions
diff --git a/apps/ChangeLog.txt b/apps/ChangeLog.txt index 5b61a1277..a22d2184a 100644 --- a/apps/ChangeLog.txt +++ b/apps/ChangeLog.txt @@ -517,3 +517,8 @@ match NuttX name change. * apps/examples/ostest/restart.c: Add a test case to verify task_restart(). + * apps/system/readline.c: readline() now returns EOF on any failure + (instead of a negated errno value). This is because the underlying + read is based on logic similar to getc. The value zero was being + confused with a NUL. So if a NUL was received, the NSH session + would terminate because it thought it was the end of file. diff --git a/apps/examples/cdcacm/Kconfig b/apps/examples/cdcacm/Kconfig index 1e0b3f3ed..78fea4d0d 100644 --- a/apps/examples/cdcacm/Kconfig +++ b/apps/examples/cdcacm/Kconfig @@ -6,6 +6,7 @@ config EXAMPLES_CDCACM bool "CDC/ACM example" default n + depends on CDCACM ---help--- Enable the USB CDC/ACM class driver example diff --git a/apps/examples/ftpc/ftpc_main.c b/apps/examples/ftpc/ftpc_main.c index 866a69cdb..deba62c1c 100644 --- a/apps/examples/ftpc/ftpc_main.c +++ b/apps/examples/ftpc/ftpc_main.c @@ -1,7 +1,7 @@ /**************************************************************************** * examples/ftpc/ftpc_main.c * - * Copyright (C) 2011-2012 Gregory Nutt. All rights reserved. + * Copyright (C) 2011-2013 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <gnutt@nuttx.org> * * Redistribution and use in source and binary forms, with or without @@ -431,14 +431,13 @@ int ftpc_main(int argc, char **argv, char **envp) ret = readline(g_line, CONFIG_FTPC_LINELEN, stdin, stdout); /* Readline normally returns the number of characters read, - * but will return 0 on end of file or a negative value - * if an error occurs. Either will cause the session to - * terminate. + * but will return EOF on end of file or if an error occurs. + * Either will cause the session to terminate. */ - if (ret <= 0) + if (ret == EOF) { - printf("ERROR: readline failed: %d\n", ret); + printf("ERROR: readline failed: %d\n", errno); return 1; } #endif diff --git a/apps/examples/usbterm/usbterm_main.c b/apps/examples/usbterm/usbterm_main.c index 69301dfcd..747a52978 100644 --- a/apps/examples/usbterm/usbterm_main.c +++ b/apps/examples/usbterm/usbterm_main.c @@ -1,7 +1,7 @@ /**************************************************************************** * examples/usbterm/usbterm_main.c * - * Copyright (C) 2011-2012 Gregory Nutt. All rights reserved. + * Copyright (C) 2011-2013 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <gnutt@nuttx.org> * * Redistribution and use in source and binary forms, with or without @@ -325,12 +325,11 @@ int usbterm_main(int argc, char *argv[]) ret = readline(g_usbterm.outbuffer, CONFIG_EXAMPLES_USBTERM_BUFLEN, stdin, stdout); /* Readline normally returns the number of characters read, - * but will return 0 on end of file or a negative value - * if an error occurs. Either will cause the session to - * terminate. + * but will return EOF on end of file or if an error occurs. Either + * will cause the session to terminate. */ - if (ret <= 0) + if (ret == EOF) { printf("ERROR: readline failed: %d\n", ret); return 1; diff --git a/apps/include/readline.h b/apps/include/readline.h index 647778210..6da6f3fd7 100644 --- a/apps/include/readline.h +++ b/apps/include/readline.h @@ -1,7 +1,7 @@ /**************************************************************************** * apps/include/readline.h * - * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Copyright (C) 2011, 2013 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <gnutt@nuttx.org> * * Redistribution and use in source and binary forms, with or without @@ -85,8 +85,8 @@ extern "C" { * * Returned values: * On success, the (positive) number of bytes transferred is returned. - * A length of zero would indicated an end of file condition. An failure, - * a negated errno value is returned. + * EOF is returned to indicate either an end of file condition or a + * failure. * **************************************************************************/ diff --git a/apps/nshlib/nsh_consolemain.c b/apps/nshlib/nsh_consolemain.c index 8be44f7aa..27e9d6db9 100644 --- a/apps/nshlib/nsh_consolemain.c +++ b/apps/nshlib/nsh_consolemain.c @@ -42,8 +42,6 @@ #include <stdio.h> #include <assert.h> -#include <apps/readline.h> - #include "nsh.h" #include "nsh_console.h" diff --git a/apps/nshlib/nsh_session.c b/apps/nshlib/nsh_session.c index 8079b2de5..0c5249672 100644 --- a/apps/nshlib/nsh_session.c +++ b/apps/nshlib/nsh_session.c @@ -129,11 +129,13 @@ int nsh_session(FAR struct console_stdio_s *pstate) fputs(g_nshprompt, pstate->cn_outstream); fflush(pstate->cn_outstream); - /* Get the next line of input */ + /* Get the next line of input. readline() returns EOF on end-of-file + * or any read failure. + */ ret = readline(pstate->cn_line, CONFIG_NSH_LINELEN, INSTREAM(pstate), OUTSTREAM(pstate)); - if (ret > 0) + if (ret != EOF) { /* Parse process the command */ @@ -142,9 +144,8 @@ int nsh_session(FAR struct console_stdio_s *pstate) } /* Readline normally returns the number of characters read, - * but will return 0 on end of file or a negative value - * if an error occurs. Either will cause the session to - * terminate. + * but will return EOF on end of file or if an error occurs. + * EOF will cause the session to terminate. */ else diff --git a/apps/system/readline/readline.c b/apps/system/readline/readline.c index bac7eee8c..1fb30e840 100644 --- a/apps/system/readline/readline.c +++ b/apps/system/readline/readline.c @@ -1,7 +1,7 @@ /**************************************************************************** * apps/system/readline/readline.c * - * Copyright (C) 2007-2008, 2011-2012 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2008, 2011-2013 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <gnutt@nuttx.org> * * Redistribution and use in source and binary forms, with or without @@ -136,9 +136,9 @@ static inline int readline_rawgetc(int infd) if (nread == 0) { - /* Return zero on end-of-file */ + /* Return EOF on end-of-file */ - return 0; + return EOF; } /* Check if an error occurred */ @@ -152,7 +152,9 @@ static inline int readline_rawgetc(int infd) int errcode = errno; if (errcode != EINTR) { - return -errcode; + /* Return EOF on any errors that we cannot handle */ + + return EOF; } } } @@ -233,8 +235,8 @@ static inline void readline_consolewrite(int outfd, FAR const char *buffer, size * * Returned values: * On success, the (positive) number of bytes transferred is returned. - * A length of zero would indicate an end of file condition. On failure, - * a negated errno value is returned. + * EOF is returned to indicate either an end of file condition or a + * failure. * **************************************************************************/ @@ -281,13 +283,15 @@ ssize_t readline(FAR char *buf, int buflen, FILE *instream, FILE *outstream) for(;;) { - /* Get the next character */ + /* Get the next character. readline_rawgetc() returns EOF on any + * errors or at the end of file. + */ int ch = readline_rawgetc(infd); /* Check for end-of-file or read error */ - if (ch <= 0) + if (ch == EOF) { /* Did we already received some data? */ @@ -302,7 +306,7 @@ ssize_t readline(FAR char *buf, int buflen, FILE *instream, FILE *outstream) return nch; } - return ch; + return EOF; } /* Are we processing a VT100 escape sequence */ diff --git a/nuttx/ChangeLog b/nuttx/ChangeLog index c1766a609..79e2723ce 100644 --- a/nuttx/ChangeLog +++ b/nuttx/ChangeLog @@ -4175,3 +4175,5 @@ example "+-" would look weird. From Petteri Aimonen. * mm/mm_mallinfo.c: Take MM semaphore in mm_mallinfo. From Petteri Aimonen. + * configs/stm32f3discovery/nsh/defconfig: Disable SPI. It is nto + used. diff --git a/nuttx/arch/arm/src/stm32/stm32_serial.c b/nuttx/arch/arm/src/stm32/stm32_serial.c index 18e65f1d9..327e515b5 100644 --- a/nuttx/arch/arm/src/stm32/stm32_serial.c +++ b/nuttx/arch/arm/src/stm32/stm32_serial.c @@ -981,6 +981,8 @@ static void up_setspeed(struct uart_dev_s *dev) } else { + DEBUGASSERT(usartdiv8 >= 8); + /* Perform mysterious operations on bits 0-3 */ brr = ((usartdiv8 & 0xfff0) | ((usartdiv8 & 0x000f) >> 1)); diff --git a/nuttx/configs/stm32f3discovery/nsh/defconfig b/nuttx/configs/stm32f3discovery/nsh/defconfig index 4b81a70ad..1913ab95c 100644 --- a/nuttx/configs/stm32f3discovery/nsh/defconfig +++ b/nuttx/configs/stm32f3discovery/nsh/defconfig @@ -166,7 +166,7 @@ CONFIG_STM32_STM32F30XX=y # CONFIG_STM32_IWDG is not set CONFIG_STM32_PWR=y # CONFIG_STM32_SDIO is not set -CONFIG_STM32_SPI1=y +# CONFIG_STM32_SPI1 is not set # CONFIG_STM32_SPI2 is not set CONFIG_STM32_SYSCFG=y # CONFIG_STM32_TIM1 is not set @@ -184,7 +184,6 @@ CONFIG_STM32_SYSCFG=y CONFIG_STM32_USART2=y CONFIG_STM32_USB=y # CONFIG_STM32_WWDG is not set -CONFIG_STM32_SPI=y # # Alternate Pin Mapping @@ -204,14 +203,13 @@ CONFIG_STM32_USART=y # CONFIG_STM32_USART_SINGLEWIRE is not set # -# SPI Configuration +# USB Host Configuration # -# CONFIG_STM32_SPI_INTERRUPTS is not set -# CONFIG_STM32_SPI_DMA is not set # -# USB Host Configuration +# USB Device Configuration # +# CONFIG_STM32_USB_ITRMP is not set # # External Memory Configuration @@ -345,10 +343,7 @@ CONFIG_DEV_NULL=y # CONFIG_PWM is not set # CONFIG_I2C is not set CONFIG_ARCH_HAVE_I2CRESET=y -CONFIG_SPI=y -# CONFIG_SPI_OWNBUS is not set -CONFIG_SPI_EXCHANGE=y -# CONFIG_SPI_CMDDATA is not set +# CONFIG_SPI is not set # CONFIG_RTC is not set # CONFIG_WATCHDOG is not set # CONFIG_ANALOG is not set diff --git a/nuttx/drivers/serial/serial.c b/nuttx/drivers/serial/serial.c index aaec71202..e723fb336 100644 --- a/nuttx/drivers/serial/serial.c +++ b/nuttx/drivers/serial/serial.c @@ -528,7 +528,7 @@ static ssize_t uart_read(FAR struct file *filep, FAR char *buffer, size_t buflen * return what we have. */ - else if (filep->f_oflags & O_NONBLOCK) + else if ((filep->f_oflags & O_NONBLOCK) != 0) { /* If nothing was transferred, then return the -EAGAIN * error (not zero which means end of file). @@ -560,7 +560,7 @@ static ssize_t uart_read(FAR struct file *filep, FAR char *buffer, size_t buflen * wait. */ - else if (filep->f_oflags & O_NONBLOCK) + else if ((filep->f_oflags & O_NONBLOCK) != 0) { /* Break out of the loop returning -EAGAIN */ @@ -1109,7 +1109,9 @@ void uart_connected(FAR uart_dev_t *dev, bool connected) { irqstate_t flags; - /* Is the device disconnected? */ + /* Is the device disconnected? Interrupts are disabled because this + * function may be called from interrupt handling logic. + */ flags = irqsave(); dev->disconnected = !connected; |