mirror of
https://github.com/apache/nuttx.git
synced 2026-05-27 19:36:35 +08:00
Extend line buffering logic to puts, fputs, putc, fputc, and putchar()
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3608 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
@@ -12,7 +12,7 @@ nuttx/
|
|||||||
(5) Binary loaders (binfmt/)
|
(5) Binary loaders (binfmt/)
|
||||||
(15) Network (net/, drivers/net)
|
(15) Network (net/, drivers/net)
|
||||||
(2) USB (drivers/usbdev, drivers/usbhost)
|
(2) USB (drivers/usbdev, drivers/usbhost)
|
||||||
(6) Libraries (lib/)
|
(5) Libraries (lib/)
|
||||||
(13) File system/Generic drivers (fs/, drivers/)
|
(13) File system/Generic drivers (fs/, drivers/)
|
||||||
(1) Graphics subystem (graphics/)
|
(1) Graphics subystem (graphics/)
|
||||||
(1) Pascal add-on (pcode/)
|
(1) Pascal add-on (pcode/)
|
||||||
@@ -371,22 +371,6 @@ o Libraries (lib/)
|
|||||||
Priority: Low (unless you are using mixed C-buffered I/O with fgets and
|
Priority: Low (unless you are using mixed C-buffered I/O with fgets and
|
||||||
fgetc, for example).
|
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
|
Description: Need some minimal termios support... at a minimum, enough to
|
||||||
switch between raw and "normal" modes to support behavior like
|
switch between raw and "normal" modes to support behavior like
|
||||||
that needed for readline().
|
that needed for readline().
|
||||||
|
|||||||
@@ -87,14 +87,17 @@
|
|||||||
int fputc(int c, FAR FILE *stream)
|
int fputc(int c, FAR FILE *stream)
|
||||||
{
|
{
|
||||||
unsigned char buf = (unsigned char)c;
|
unsigned char buf = (unsigned char)c;
|
||||||
if (lib_fwrite(&buf, 1, stream) > 0)
|
int ret;
|
||||||
|
|
||||||
|
ret = lib_fwrite(&buf, 1, stream);
|
||||||
|
if (ret > 0)
|
||||||
{
|
{
|
||||||
/* Flush the buffer if a newline is output */
|
/* Flush the buffer if a newline is output */
|
||||||
|
|
||||||
#ifdef CONFIG_STDIO_LINEBUFFER
|
#ifdef CONFIG_STDIO_LINEBUFFER
|
||||||
if (c == '\n')
|
if (c == '\n')
|
||||||
{
|
{
|
||||||
int ret = lib_fflush(stream, true);
|
ret = lib_fflush(stream, true);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
return EOF;
|
return EOF;
|
||||||
|
|||||||
+40
-13
@@ -93,35 +93,62 @@
|
|||||||
int fputs(FAR const char *s, FAR FILE *stream)
|
int fputs(FAR const char *s, FAR FILE *stream)
|
||||||
{
|
{
|
||||||
int ntowrite;
|
int ntowrite;
|
||||||
int nwritten;
|
int nput;
|
||||||
int nput = EOF;
|
int ret;
|
||||||
|
|
||||||
/* Make sure that a string was provided. */
|
/* Make sure that a string was provided. */
|
||||||
|
|
||||||
|
#ifdef CONFIG_DEBUG /* Most parameter checking is disabled if DEBUG is off */
|
||||||
if (!s)
|
if (!s)
|
||||||
{
|
{
|
||||||
set_errno(EINVAL);
|
set_errno(EINVAL);
|
||||||
|
return EOF;
|
||||||
}
|
}
|
||||||
else
|
#endif
|
||||||
|
|
||||||
|
/* Get the length of the string. */
|
||||||
|
|
||||||
|
ntowrite = strlen(s);
|
||||||
|
if (ntowrite == 0)
|
||||||
{
|
{
|
||||||
/* Get the length of the string. */
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
ntowrite = strlen(s);
|
/* Write the string */
|
||||||
if (ntowrite == 0)
|
|
||||||
|
#ifdef CONFIG_STDIO_LINEBUFFER
|
||||||
|
nput = ntowrite;
|
||||||
|
while (ntowrite-- > 0)
|
||||||
|
{
|
||||||
|
ret = lib_fwrite(s, 1, stream);
|
||||||
|
if (ret <= 0)
|
||||||
{
|
{
|
||||||
nput = 0;
|
return EOF;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
/* Write the string */
|
|
||||||
|
|
||||||
nwritten = lib_fwrite(s, ntowrite, stream);
|
/* Flush the buffer if a newline was put to the buffer */
|
||||||
if (nwritten > 0)
|
|
||||||
|
if (*s == '\n')
|
||||||
|
{
|
||||||
|
ret = lib_fflush(stream, true);
|
||||||
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
nput = nwritten;
|
return EOF;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Set up for the next lib_fwrite() */
|
||||||
|
|
||||||
|
s++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return nput;
|
return nput;
|
||||||
|
#else
|
||||||
|
nput = lib_fwrite(s, ntowrite, stream);
|
||||||
|
if (nput < 0)
|
||||||
|
{
|
||||||
|
return EOF
|
||||||
|
}
|
||||||
|
return nput;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,6 +93,7 @@ int puts(FAR const char *s)
|
|||||||
FILE *stream = stdout;
|
FILE *stream = stdout;
|
||||||
int nwritten;
|
int nwritten;
|
||||||
int nput = EOF;
|
int nput = EOF;
|
||||||
|
int ret;
|
||||||
|
|
||||||
/* Write the string (the next two steps must be atomic) */
|
/* Write the string (the next two steps must be atomic) */
|
||||||
|
|
||||||
@@ -106,20 +107,19 @@ int puts(FAR const char *s)
|
|||||||
/* Followed by a newline */
|
/* Followed by a newline */
|
||||||
|
|
||||||
char newline = '\n';
|
char newline = '\n';
|
||||||
if (lib_fwrite(&newline, 1, stream) > 0)
|
ret = lib_fwrite(&newline, 1, stream);
|
||||||
|
if (ret > 0)
|
||||||
{
|
{
|
||||||
nput = nwritten + 1;
|
nput = nwritten + 1;
|
||||||
|
|
||||||
/* Flush the buffer after the newline is output */
|
/* Flush the buffer after the newline is output */
|
||||||
|
|
||||||
#ifdef CONFIG_STDIO_LINEBUFFER
|
#ifdef CONFIG_STDIO_LINEBUFFER
|
||||||
{
|
ret = lib_fflush(stream, true);
|
||||||
int ret = lib_fflush(stream, true);
|
if (ret < 0)
|
||||||
if (ret < 0)
|
{
|
||||||
{
|
nput = EOF;
|
||||||
nput = EOF;
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user