From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH 1/7] console: return characters written from console_putc
Date: Mon, 13 Apr 2026 12:09:36 +0200 [thread overview]
Message-ID: <20260413101118.1462119-1-a.fatoum@barebox.org> (raw)
console_putc() prints two characters instead of one when it does
LF -> CRLF conversion.
As console_putc already has to do the CRLF check, have it return the
number of printed characters as well to simplify its users.
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
common/console.c | 17 +++++++----------
common/console_common.c | 2 +-
common/console_simple.c | 13 +++++++------
include/console.h | 4 ++--
pbl/console.c | 8 ++++++--
5 files changed, 23 insertions(+), 21 deletions(-)
diff --git a/common/console.c b/common/console.c
index 62ca57e43b05..30a72b2bb6d2 100644
--- a/common/console.c
+++ b/common/console.c
@@ -576,10 +576,11 @@ int tstc(void)
}
EXPORT_SYMBOL(tstc);
-void console_putc(unsigned int ch, char c)
+int console_putc(unsigned int ch, char c)
{
struct console_device *cdev;
int init = initialized;
+ bool crlf = c == '\n';
switch (init) {
case CONSOLE_UNINITIALIZED:
@@ -587,20 +588,20 @@ void console_putc(unsigned int ch, char c)
fallthrough;
case CONSOLE_INITIALIZED_BUFFER:
kfifo_putc(console_output_fifo, c);
- if (c == '\n')
+ if (crlf)
putc_ll('\r');
putc_ll(c);
- return;
+ return 1 + crlf;
case CONSOLE_INIT_FULL:
for_each_console(cdev) {
if (cdev->f_active & ch) {
- if (c == '\n')
+ if (crlf)
cdev->putc(cdev, '\r');
cdev->putc(cdev, c);
}
}
- return;
+ return 1 + crlf;
default:
/* If we have problems inititalizing our data
* get them early
@@ -626,11 +627,7 @@ int console_puts(unsigned int ch, const char *str)
}
while (*s) {
- if (*s == '\n')
- n++;
-
- console_putc(ch, *s);
- n++;
+ n += console_putc(con, *s);
s++;
}
return n;
diff --git a/common/console_common.c b/common/console_common.c
index 20c93de68cdc..e3b49200c538 100644
--- a/common/console_common.c
+++ b/common/console_common.c
@@ -423,7 +423,7 @@ int dputc(int fd, char c)
if (fd == 1)
putchar(c);
else if (fd == 2)
- console_putc(CONSOLE_STDERR, c);
+ return console_putc(CONSOLE_STDERR, c);
else
return write(fd, &c, 1);
diff --git a/common/console_simple.c b/common/console_simple.c
index 25fe6217448c..dfc180e70f98 100644
--- a/common/console_simple.c
+++ b/common/console_simple.c
@@ -17,28 +17,29 @@ int console_puts(unsigned int ch, const char *str)
int i = 0;
while (*s) {
- console_putc(ch, *s);
+ i += console_putc(ch, *s);
s++;
- i++;
}
return i;
}
EXPORT_SYMBOL(console_puts);
-void console_putc(unsigned int ch, char c)
+int console_putc(unsigned int ch, char c)
{
+ bool crlf = c == '\n';
if (!console) {
- if (c == '\n')
+ if (crlf)
putc_ll('\r');
putc_ll(c);
- return;
+ return 1 + crlf;
}
- if (c == '\n')
+ if (crlf)
console->putc(console, '\r');
console->putc(console, c);
+ return 1 + crlf;
}
EXPORT_SYMBOL(console_putc);
diff --git a/include/console.h b/include/console.h
index cd00e86a805c..353a5088a547 100644
--- a/include/console.h
+++ b/include/console.h
@@ -229,7 +229,7 @@ int arch_ctrlc(void);
#ifndef CONFIG_CONSOLE_NONE
/* stdout */
-void console_putc(unsigned int ch, const char c);
+int console_putc(unsigned int ch, const char c);
int console_puts(unsigned int ch, const char *s);
void console_putbin(unsigned int ch, const u8 *str, size_t len);
void console_flush(void);
@@ -241,7 +241,7 @@ void console_ctrlc_allow(void);
void console_ctrlc_forbid(void);
#else
static inline int console_puts(unsigned int ch, const char *str) { return 0; }
-static inline void console_putc(unsigned int ch, char c) {}
+static inline int console_putc(unsigned int ch, char c) { return 0;}
static inline void console_flush(void) {}
/* test if ctrl-c was pressed */
diff --git a/pbl/console.c b/pbl/console.c
index f2e7b3a20e9c..1274552b2088 100644
--- a/pbl/console.c
+++ b/pbl/console.c
@@ -32,12 +32,14 @@ static void __putc(void *ctx, int c)
putc(ctx, c);
}
-void console_putc(unsigned int ch, char c)
+int console_putc(unsigned int ch, char c)
{
if (putc_offset)
__putc(putc_ctx, c);
else
putc_ll(c);
+
+ return 1;
}
int console_puts(unsigned int ch, const char *str)
@@ -45,8 +47,10 @@ int console_puts(unsigned int ch, const char *str)
int n = 0;
while (*str) {
- if (*str == '\n')
+ if (*str == '\n') {
console_putc(ch, '\r');
+ n++;
+ }
console_putc(ch, *str);
str++;
--
2.47.3
next reply other threads:[~2026-04-13 10:11 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-13 10:09 Ahmad Fatoum [this message]
2026-04-13 10:09 ` [PATCH 2/7] stddef: implement scoped_var for use in iterators Ahmad Fatoum
2026-04-13 10:09 ` [PATCH 3/7] console: have for_each_console declare the iterator internally Ahmad Fatoum
2026-04-13 10:09 ` [PATCH 4/7] console: make console_puts and friends accept a console_device Ahmad Fatoum
2026-04-13 10:09 ` [PATCH 5/7] console: implement console_putc in terms of console_putbin Ahmad Fatoum
2026-04-13 10:09 ` [PATCH 6/7] console: implement console_printf Ahmad Fatoum
2026-04-13 10:09 ` [PATCH 7/7] commands: dmesg: give log_print a console_device parameter Ahmad Fatoum
2026-04-13 12:28 ` [PATCH 1/7] console: return characters written from console_putc Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260413101118.1462119-1-a.fatoum@barebox.org \
--to=a.fatoum@barebox.org \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox