summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpatacongo <patacongo@7fd9a85b-ad96-42d3-883c-3090e2eb8679>2011-05-14 17:37:47 +0000
committerpatacongo <patacongo@7fd9a85b-ad96-42d3-883c-3090e2eb8679>2011-05-14 17:37:47 +0000
commit543ced8c3ecda4eb5db68315494308558ef15e23 (patch)
treefdd388cc6fa54c5a3cc7702b5128b7ca3cf5325a
parent4abb53793da1caffa2aa82c7243f2858f513d74b (diff)
Flush buffer after newline in putc, fputc, and puts (but not fputs)
git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@3607 7fd9a85b-ad96-42d3-883c-3090e2eb8679
-rw-r--r--nuttx/TODO20
-rw-r--r--nuttx/lib/stdio/lib_fputc.c12
-rw-r--r--nuttx/lib/stdio/lib_fputs.c25
-rw-r--r--nuttx/lib/stdio/lib_libvsprintf.c2
-rw-r--r--nuttx/lib/stdio/lib_puts.c25
5 files changed, 65 insertions, 19 deletions
diff --git a/nuttx/TODO b/nuttx/TODO
index 0022dd72b0..7b05b97aae 100644
--- a/nuttx/TODO
+++ b/nuttx/TODO
@@ -1,4 +1,4 @@
-NuttX TODO List (Last updated May 6, 2011)
+NuttX TODO List (Last updated May 14, 2011)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
nuttx/
@@ -12,7 +12,7 @@ nuttx/
(5) Binary loaders (binfmt/)
(15) Network (net/, drivers/net)
(2) USB (drivers/usbdev, drivers/usbhost)
- (5) Libraries (lib/)
+ (6) Libraries (lib/)
(13) File system/Generic drivers (fs/, drivers/)
(1) Graphics subystem (graphics/)
(1) Pascal add-on (pcode/)
@@ -371,6 +371,22 @@ o Libraries (lib/)
Priority: Low (unless you are using mixed C-buffered I/O with fgets and
fgetc, for example).
+ Description: if CONFIG_STDIO_LINEBUFFER is defined, then fputs() should flush
+ the buffer on each newline encountered in the input stream. At
+ present, it does not flush at all! This is because fputs() is
+ based on fwrite() which handles binary data.
+
+ I suppose one could easily check if the last character is '\n'
+ and then flush in fputs() for that case. But that is imperfect
+ logic. It would work for the most frequent cases like puts("abcdef\n")
+ but not in all cases. For example, puts("abc\ndef") should flush
+ "abc\n" to output but keep "def" buffered. I can't get that behavior
+ using lib_fwrite() to implement fputs() (unless lib_fwrite were
+ extended to handle binary or text data with newlines).
+ Status: Open
+ Priority: Low (unless you doing lots of puts or fputs output and the
+ current buffer handling does not meet your needs).
+
Description: Need some minimal termios support... at a minimum, enough to
switch between raw and "normal" modes to support behavior like
that needed for readline().
diff --git a/nuttx/lib/stdio/lib_fputc.c b/nuttx/lib/stdio/lib_fputc.c
index f5eeecea77..34ef7aa25c 100644
--- a/nuttx/lib/stdio/lib_fputc.c
+++ b/nuttx/lib/stdio/lib_fputc.c
@@ -89,6 +89,18 @@ int fputc(int c, FAR FILE *stream)
unsigned char buf = (unsigned char)c;
if (lib_fwrite(&buf, 1, stream) > 0)
{
+ /* Flush the buffer if a newline is output */
+
+#ifdef CONFIG_STDIO_LINEBUFFER
+ if (c == '\n')
+ {
+ int ret = lib_fflush(stream, true);
+ if (ret < 0)
+ {
+ return EOF;
+ }
+ }
+#endif
return c;
}
else
diff --git a/nuttx/lib/stdio/lib_fputs.c b/nuttx/lib/stdio/lib_fputs.c
index 54801ca50d..a956c83b23 100644
--- a/nuttx/lib/stdio/lib_fputs.c
+++ b/nuttx/lib/stdio/lib_fputs.c
@@ -108,19 +108,20 @@ int fputs(FAR const char *s, FAR FILE *stream)
ntowrite = strlen(s);
if (ntowrite == 0)
- {
- nput = 0;
- }
+ {
+ nput = 0;
+ }
else
- {
- /* Write the string */
-
- nwritten = lib_fwrite(s, ntowrite, stream);
- if (nwritten > 0)
- {
- nput = nwritten;
- }
- }
+ {
+ /* Write the string */
+
+ nwritten = lib_fwrite(s, ntowrite, stream);
+ if (nwritten > 0)
+ {
+ nput = nwritten;
+ }
+ }
}
+
return nput;
}
diff --git a/nuttx/lib/stdio/lib_libvsprintf.c b/nuttx/lib/stdio/lib_libvsprintf.c
index 8e0709ec90..34a27ea4e3 100644
--- a/nuttx/lib/stdio/lib_libvsprintf.c
+++ b/nuttx/lib/stdio/lib_libvsprintf.c
@@ -1136,6 +1136,8 @@ int lib_vsprintf(FAR struct lib_outstream_s *obj, const char *src, va_list ap)
#ifdef CONFIG_STDIO_LINEBUFFER
if (*src == '\n')
{
+ /* Should return an error on a failure to flush */
+
(void)obj->flush(obj);
}
#endif
diff --git a/nuttx/lib/stdio/lib_puts.c b/nuttx/lib/stdio/lib_puts.c
index 5e651c4dea..e8cb186f6f 100644
--- a/nuttx/lib/stdio/lib_puts.c
+++ b/nuttx/lib/stdio/lib_puts.c
@@ -84,31 +84,46 @@
* Name: puts
*
* Description:
- * puts() writes the string s and a trailing newline to
- * stdout.
+ * puts() writes the string s and a trailing newline to stdout.
+ *
****************************************************************************/
int puts(FAR const char *s)
{
+ FILE *stream = stdout;
int nwritten;
int nput = EOF;
/* Write the string (the next two steps must be atomic) */
- lib_take_semaphore(stdout);
+ lib_take_semaphore(stream);
/* Write the string without its trailing '\0' */
- nwritten = fputs(s, stdout);
+ nwritten = fputs(s, stream);
if (nwritten > 0)
{
/* Followed by a newline */
+
char newline = '\n';
- if (lib_fwrite(&newline, 1, stdout) > 0)
+ if (lib_fwrite(&newline, 1, stream) > 0)
{
nput = nwritten + 1;
+
+ /* Flush the buffer after the newline is output */
+
+#ifdef CONFIG_STDIO_LINEBUFFER
+ {
+ int ret = lib_fflush(stream, true);
+ if (ret < 0)
+ {
+ nput = EOF;
+ }
+ }
+#endif
}
}
+
lib_give_semaphore(stdout);
return nput;
}