From 2a2655f8e0dc040d62c3c6d19bc298fc0ebb3f64 Mon Sep 17 00:00:00 2001 From: patacongo Date: Wed, 1 Feb 2012 19:47:12 +0000 Subject: [PATCH] Use realine instead of fgets in several other places git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@4357 7fd9a85b-ad96-42d3-883c-3090e2eb8679 --- apps/examples/README.txt | 37 +++++++++++++++++++++++++--- apps/examples/ftpc/ftpc_main.c | 37 +++++++++++++++++++++++++--- apps/examples/usbterm/usbterm_main.c | 29 ++++++++++++++++++++-- apps/nshlib/README.txt | 8 ++++++ apps/nshlib/nsh_serial.c | 2 +- 5 files changed, 103 insertions(+), 10 deletions(-) diff --git a/apps/examples/README.txt b/apps/examples/README.txt index b14d19b84a..a8af257d74 100644 --- a/apps/examples/README.txt +++ b/apps/examples/README.txt @@ -244,9 +244,20 @@ examples/ftpc where xx.xx.xx.xx is the IP address of the FTP server and pp is an optional port number. - NOTE: The ftpc task uses the system console for input/output. It will - not work from NSH over a telnet NSH connection (Well, it will work you - just won't be able to access the command line). + NOTE: By default, FTPC uses readline to get data from stdin. So your + appconfig file must have the following build path: + + CONFIGURED_APPS += system/readline + + NOTE: If you use the ftpc task over a telnet NSH connection, then you + should set the following configuration item: + + CONFIG_EXAMPLES_FTPC_FGETS=y + + By default, the FTPC client will use readline() to get characters from + the console. Readline includes and command-line editor and echos + characters received in stdin back through stdout. Neither of these + behaviors are desire-able if Telnet is used. You may also want to define the following in your configuration file. Otherwise, you will have not feeback about what is going on: @@ -393,6 +404,11 @@ examples/nsh CONFIGURED_APPS += nshlib + NOTE: If the NSH serial console is used, then following is also + required to build the readline() library: + + CONFIGURED_APPS += system/readline + And if networking is included: CONFIGURED_APPS += uiplib @@ -1257,6 +1273,21 @@ examples/usbterm CONFIG_EXAMPLES_USBTERM_TRACEINTERRUPTS Show interrupt-related events. + NOTE: By default, USBterm uses readline to get data from stdin. So your + appconfig file must have the following build path: + + CONFIGURED_APPS += system/readline + + NOTE: If you use the USBterm task over a telnet NSH connection, then you + should set the following configuration item: + + CONFIG_EXAMPLES_USBTERM_FGETS=y + + By default, the USBterm client will use readline() to get characters from + the console. Readline includes and command-line editor and echos + characters received in stdin back through stdout. Neither of these + behaviors are desire-able if Telnet is used. + Error results are always shown in the trace output Other relevant configuration options: CONFIG_CDCACM selected by the diff --git a/apps/examples/ftpc/ftpc_main.c b/apps/examples/ftpc/ftpc_main.c index 7a1cb50f7d..9093b0c10d 100644 --- a/apps/examples/ftpc/ftpc_main.c +++ b/apps/examples/ftpc/ftpc_main.c @@ -1,8 +1,8 @@ /**************************************************************************** * examples/ftpc/ftpc_main.c * - * Copyright (C) 2011 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt + * Copyright (C) 2011-2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -41,10 +41,13 @@ #include #include #include +#include #include #include +#include + #include "ftpc.h" /**************************************************************************** @@ -357,6 +360,9 @@ int ftpc_main(int argc, char **argv, char **envp) struct ftpc_connect_s connect = {{0}, 0}; SESSION handle; FAR char *ptr; +#ifndef CONFIG_EXAMPLES_FTPC_FGETS + int ret; +#endif if (argc != 2) { @@ -413,9 +419,32 @@ int ftpc_main(int argc, char **argv, char **envp) /* Get the next line of input */ - if (fgets(g_line, CONFIG_FTPC_LINELEN, stdin)) +#ifdef CONFIG_EXAMPLES_FTPC_FGETS + /* fgets returns NULL on end-of-file or any I/O error */ + + if (fgets(g_line, CONFIG_FTPC_LINELEN, stdin) == NULL) { - /* Parse process the command */ + printf("ERROR: fgets failed: %d\n", errno); + return 1; + } +#else + 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. + */ + + if (ret <= 0) + { + printf("ERROR: readline failed: %d\n", ret); + return 1; + } +#endif + else + { + /* Parse and process the command */ (void)ftpc_parse(handle, g_line); FFLUSH(); diff --git a/apps/examples/usbterm/usbterm_main.c b/apps/examples/usbterm/usbterm_main.c index d51101dd7c..066c6f5482 100644 --- a/apps/examples/usbterm/usbterm_main.c +++ b/apps/examples/usbterm/usbterm_main.c @@ -50,6 +50,8 @@ #include #include +#include + #include #include @@ -297,9 +299,32 @@ int MAIN_NAME(int argc, char *argv[]) fputs("usbterm> ", stdout); fflush(stdout); - /* Get the next line of input from stdin */ + /* Get the next line of input */ - if (fgets(g_usbterm.outbuffer, CONFIG_EXAMPLES_USBTERM_BUFLEN, stdin)) +#ifdef CONFIG_EXAMPLES_USBTERM_FGETS + /* fgets returns NULL on end-of-file or any I/O error */ + + if (fgets(g_usbterm.outbuffer, CONFIG_EXAMPLES_USBTERM_BUFLEN, stdin) == NULL) + { + printf("ERROR: fgets failed: %d\n", errno); + return 1; + } +#else + 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. + */ + + if (ret <= 0) + { + printf("ERROR: readline failed: %d\n", ret); + return 1; + } +#endif + else { /* Send the line of input via USB */ diff --git a/apps/nshlib/README.txt b/apps/nshlib/README.txt index fad1ac1044..fbdf9178a1 100644 --- a/apps/nshlib/README.txt +++ b/apps/nshlib/README.txt @@ -1047,3 +1047,11 @@ Common Problems Make sure that the polled console is disabled in the OS configuration file, .config. That file should have CONFIG_DEV_LOWCONSOLE=n for NSH over serial. + + Problem: + The function 'readline' is undefined. + Usual Cause: + The following is missing from your appconfig file: + + CONFIGURED_APPS += system/readline + diff --git a/apps/nshlib/nsh_serial.c b/apps/nshlib/nsh_serial.c index 72004dd7f9..57cb1b1c17 100644 --- a/apps/nshlib/nsh_serial.c +++ b/apps/nshlib/nsh_serial.c @@ -525,7 +525,7 @@ int nsh_consolemain(int argc, char *argv[]) else { - fprintf(pstate->ss_outstream, g_fmtcmdfailed, "readline", NSH_ERRNO); + fprintf(pstate->ss_outstream, g_fmtcmdfailed, "readline", NSH_ERRNO_OF(-ret)); return 1; } }