[PATCH 3/7] console: have for_each_console declare the iterator internally

Ahmad Fatoum
2026-04-13T10:11:56Z

Simplify users of the for_each_console macro, by declaring the iterator
variable inside.

No functional change.

Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
commands/bfetch.c | 1 -
commands/edit.c | 2 --
common/bootm.c | 1 -
common/console.c | 11 -----------
common/console_common.c | 5 -----
include/console.h | 7 +++++--
lib/term.c | 1 -
7 files changed, 5 insertions(+), 23 deletions(-)

diff --git a/commands/bfetch.c b/commands/bfetch.c
index d9c615f65aca..705edb08461a 100644
--- a/commands/bfetch.c
+++ b/commands/bfetch.c
@@ -290,7 +290,6 @@ static void print_shell_console(unsigned *line)
const char *shell;
struct command *cmd;
struct string_list sl;
- struct console_device *console;

if (IS_ENABLED(CONFIG_SHELL_HUSH))
shell = "Hush";
diff --git a/commands/edit.c b/commands/edit.c
index c7262711d01f..4442543b9acb 100644
--- a/commands/edit.c
+++ b/commands/edit.c
@@ -464,8 +464,6 @@ static int read_modal_key(bool is_modal)

static bool is_efi_console_active(void)
{
- struct console_device *cdev;
-
if (!IS_ENABLED(CONFIG_DRIVER_SERIAL_EFI_STDIO))
return false;

diff --git a/common/bootm.c b/common/bootm.c
index d4a3a232743f..d84e7647b389 100644
--- a/common/bootm.c
+++ b/common/bootm.c
@@ -653,7 +653,6 @@ struct image_data *bootm_boot_prep(const struct bootm_data *bootm_data)
}

if (bootm_earlycon) {
- struct console_device *console;
const char *earlycon = NULL;

for_each_console(console) {
diff --git a/common/console.c b/common/console.c
index 30a72b2bb6d2..d7013e72e358 100644
--- a/common/console.c
+++ b/common/console.c
@@ -257,7 +257,6 @@ void console_set_stdoutpath(struct console_device *cdev, unsigned baudrate)

struct console_device *of_console_by_stdout_path(void)
{
- struct console_device *console;
struct device_node *stdout_np;

stdout_np = of_get_stdoutpath(NULL);
@@ -459,7 +458,6 @@ EXPORT_SYMBOL(console_unregister);
static __maybe_unused int console_activate_all_fallback(void)
{
int activate = CONSOLE_STDIOE;
- struct console_device *cdev;

for_each_console(cdev) {
if (cdev->f_active & (CONSOLE_STDOUT | CONSOLE_STDERR))
@@ -487,7 +485,6 @@ postenvironment_initcall(console_activate_all_fallback);

static int getc_raw(void)
{
- struct console_device *cdev;
int active = 0;

while (1) {
@@ -515,8 +512,6 @@ static int getc_raw(void)

static int tstc_raw(void)
{
- struct console_device *cdev;
-
if (!IS_ALLOWED(SCONFIG_CONSOLE_INPUT))
return 0;

@@ -578,7 +573,6 @@ EXPORT_SYMBOL(tstc);

int console_putc(unsigned int ch, char c)
{
- struct console_device *cdev;
int init = initialized;
bool crlf = c == '\n';

@@ -613,7 +607,6 @@ EXPORT_SYMBOL(console_putc);

int console_puts(unsigned int ch, const char *str)
{
- struct console_device *cdev;
const char *s = str;
int n = 0;

@@ -636,8 +629,6 @@ EXPORT_SYMBOL(console_puts);

void console_putbin(unsigned int ch, const u8 *str, size_t len)
{
- struct console_device *cdev;
-
switch (initialized) {
case CONSOLE_UNINITIALIZED:
console_init_early();
@@ -667,8 +658,6 @@ EXPORT_SYMBOL(console_putbin);

void console_flush(void)
{
- struct console_device *cdev;
-
for_each_console(cdev) {
if (cdev->flush)
cdev->flush(cdev);
diff --git a/common/console_common.c b/common/console_common.c
index e3b49200c538..1d98e4ec1bcc 100644
--- a/common/console_common.c
+++ b/common/console_common.c
@@ -308,8 +308,6 @@ EXPORT_SYMBOL(vprintf);

struct console_device *console_get_by_dev(struct device *dev)
{
- struct console_device *cdev;
-
for_each_console(cdev) {
if (cdev->dev == dev)
return cdev;
@@ -321,8 +319,6 @@ EXPORT_SYMBOL(console_get_by_dev);

struct console_device *console_get_by_name(const char *name)
{
- struct console_device *cdev;
-
for_each_console(cdev) {
if (cdev->devname && !strcmp(cdev->devname, name))
return cdev;
@@ -340,7 +336,6 @@ EXPORT_SYMBOL(console_get_by_name);
*/
struct console_device *console_get_first_interactive(void)
{
- struct console_device *cdev;
const unsigned char active = CONSOLE_STDIN | CONSOLE_STDOUT;

/* if no console input is allows, then we can't have STDIN on any. */
diff --git a/include/console.h b/include/console.h
index 353a5088a547..082d1ac8a924 100644
--- a/include/console.h
+++ b/include/console.h
@@ -198,7 +198,9 @@ bool console_allow_color(void);

#ifndef CONFIG_CONSOLE_NONE
extern struct list_head console_list;
-#define for_each_console(console) list_for_each_entry(console, &console_list, list)
+#define for_each_console(console) \
+ scoped_var(struct console_device *console) \
+ list_for_each_entry(console, &console_list, list)

struct console_device *console_get_first_interactive(void);

@@ -210,7 +212,8 @@ static inline int barebox_set_loglevel(int loglevel)
return old_loglevel;
}
#else
-#define for_each_console(console) while (((void)console, 0))
+#define for_each_console(console) \
+ scoped_var(struct console_device *console) while (((void)console, 0))

static inline struct console_device *console_get_first_interactive(void)
{
diff --git a/lib/term.c b/lib/term.c
index 8b4de96c63a4..669d48fd4d8c 100644
--- a/lib/term.c
+++ b/lib/term.c
@@ -109,7 +109,6 @@ int term_getsize(int *screenwidth, int *screenheight)
{
int width = INT_MAX, height = INT_MAX;
bool found = false;
- struct console_device *cdev;

for_each_console(cdev) {
int w, h;
--
2.47.3