mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 21/30] commands: echo: add wide file output via wecho alias
Date: Mon, 22 Nov 2021 09:47:23 +0100	[thread overview]
Message-ID: <20211122084732.2597109-22-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20211122084732.2597109-1-a.fatoum@pengutronix.de>

For interacting with EFI, it can be useful to write UCS-2 files from the
shell. Add an optional wecho alias to facilitate this.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 commands/echo.c | 55 +++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 51 insertions(+), 4 deletions(-)

diff --git a/commands/echo.c b/commands/echo.c
index 99575b4c0c35..572b852ea32a 100644
--- a/commands/echo.c
+++ b/commands/echo.c
@@ -10,6 +10,40 @@
 #include <errno.h>
 #include <libbb.h>
 
+static void echo_dputc(int fd, char c, bool wide)
+{
+	wchar_t wc;
+	int n;
+
+	if (!wide || fd == 1 || fd == 2) {
+		dputc(fd, c);
+		return;
+	}
+
+	n = mbtowc(&wc, &c, 1);
+	if (n < 0)
+		return;
+
+	write(fd, &wc, sizeof(wchar_t));
+}
+
+static void echo_dputs(int fd, const char *s, bool wide)
+{
+	wchar_t *ws;
+
+	if (!wide || fd == 1 || fd == 2) {
+		dputs(fd, s);
+		return;
+	}
+
+	ws = strdup_char_to_wchar(s);
+	if (!ws)
+		return;
+
+	write(fd, ws, wcslen(ws) * sizeof(wchar_t));
+	free(ws);
+}
+
 static int do_echo(int argc, char *argv[])
 {
 	int i, optind = 1;
@@ -18,6 +52,10 @@ static int do_echo(int argc, char *argv[])
 	int oflags = O_WRONLY | O_CREAT;
 	char str[CONFIG_CBSIZE];
 	int process_escape = 0;
+	bool wide = false;
+
+	if (IS_ENABLED(CONFIG_PRINTF_WCHAR) && *argv[0] == 'w')
+		wide = true;
 
 	/* We can't use getopt() here because we want to
 	 * echo all things we don't understand.
@@ -66,18 +104,22 @@ exit_parse:
 	}
 
 	for (i = optind; i < argc; i++) {
+		const char *out;
+
 		if (i > optind)
-			dputc(fd, ' ');
+			echo_dputc(fd, ' ', wide);
 		if (process_escape) {
 			process_escape_sequence(argv[i], str, CONFIG_CBSIZE);
-			dputs(fd, str);
+			out = str;
 		} else {
-			dputs(fd, argv[i]);
+			out = argv[i];
 		}
+
+		echo_dputs(fd, out, wide);
 	}
 
 	if (newline)
-		dputc(fd, '\n');
+		echo_dputc(fd, '\n', wide);
 
 	if (file)
 		close(fd);
@@ -99,8 +141,13 @@ BAREBOX_CMD_HELP_OPT ("-a FILE", "append to FILE instead of using stdout")
 BAREBOX_CMD_HELP_OPT ("-o FILE", "overwrite FILE instead of using stdout")
 BAREBOX_CMD_HELP_END
 
+static __maybe_unused const char * const echo_aliases[] = { "wecho", NULL};
+
 BAREBOX_CMD_START(echo)
 	.cmd		= do_echo,
+#ifdef CONFIG_PRINTF_WCHAR
+	.aliases	= echo_aliases,
+#endif
 	BAREBOX_CMD_DESC("echo args to console")
 	BAREBOX_CMD_OPTS("[-neao] STRING")
 	BAREBOX_CMD_GROUP(CMD_GRP_CONSOLE)
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


  parent reply	other threads:[~2021-11-22  8:54 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-22  8:47 [PATCH 00/30] efi: refactor for upcoming loader support Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 01/30] fs: remove useless AT_FDCWD references Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 02/30] fs: remove unused struct node_d in struct dir Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 03/30] block : efi: rename driver variable from efi_fs_driver to efi_bio_driver Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 04/30] include: <linux/types.h>: wrap in #ifndef __ASSEMBLY__ Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 05/30] hw_random: stm32: propagate error codes from rng read Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 06/30] efi: align LOAD_FILE_PROTOCOL_GUID's name with other PROTOCOL_GUIDs Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 07/30] asm-generic: move sync_caches_for_execution declaration to <asm/cache.h> Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 08/30] common: move EFI code into new efi/ top level directory Ahmad Fatoum
2021-11-23  8:55   ` Jules Maselbas
2021-11-22  8:47 ` [PATCH 09/30] serial: efi-stdio: move efi-stdio.h header to central location Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 10/30] efi: use SPDX-License-Identifier where appropriate Ahmad Fatoum
2021-11-23  8:52   ` Jules Maselbas
2021-11-22  8:47 ` [PATCH 11/30] drivers: efi: move Kconfig options to new menu Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 12/30] efi: factor out errno translation Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 13/30] efi: rename <efi/efi.h> to <efi/efi-payload.h> Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 14/30] efi: centralize efivarfs_parse_filename Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 15/30] kbuild: force 16-bit wchar_t treewide Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 16/30] include: <linux/nls.h>: remove duplicate wchar_t typedef Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 17/30] lib: wchar: add wctomb and mbtowc Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 18/30] lib: implement wcsnlen Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 19/30] vsprintf: add optional support for %ls format modifier Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 20/30] libfile: null-terminate read_file of wchar_t buffer Ahmad Fatoum
2021-11-22  8:47 ` Ahmad Fatoum [this message]
2021-11-22  8:47 ` [PATCH 22/30] efi: make efi_main __noreturn Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 23/30] efi: define and use new EFI_ERROR_MASK macro Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 24/30] common: move CONFIG_ELF into General Settings Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 25/30] efi: don't zero executable buffer before freeing Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 26/30] partitions: efi: move header to central location Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 27/30] efi: print early efi_main string on CONFIG_DEBUG_LL=y Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 28/30] ARM64: board-dt-2nd: remove no longer needed noinline function split Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 29/30] bus: acpi: register bus even if without ACPI EFI table Ahmad Fatoum
2021-11-22  8:47 ` [PATCH 30/30] efi: guid: fix typos Ahmad Fatoum
2021-11-25  7:44 ` [PATCH 00/30] efi: refactor for upcoming loader support 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=20211122084732.2597109-22-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --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