From 744778be30f2f78bea4af5523f00da98480cda4d Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 7 Apr 2019 09:31:36 +1000 Subject: [PATCH] Add a -to-file switch to the echo command As discussed in #2836 --- doc/pages/commands.asciidoc | 4 ++++ src/commands.cc | 7 ++++++- src/file.cc | 9 +++++++++ src/file.hh | 1 + test/compose/echo-to-file/cmd | 1 + test/compose/echo-to-file/in | 1 + test/compose/echo-to-file/out | 1 + 7 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 test/compose/echo-to-file/cmd create mode 100644 test/compose/echo-to-file/in create mode 100644 test/compose/echo-to-file/out diff --git a/doc/pages/commands.asciidoc b/doc/pages/commands.asciidoc index 8a215e3d..62b80818 100644 --- a/doc/pages/commands.asciidoc +++ b/doc/pages/commands.asciidoc @@ -206,6 +206,10 @@ of the file onto the filesystem *-debug*::: print the given text to the *\*debug** buffer + *-to-file* ::: + write the given text to the given file on the host + filesystem. + *set-face* :: *alias* face + define a face in *scope* diff --git a/src/commands.cc b/src/commands.cc index 4176f673..f1e2b71a 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -1200,6 +1200,7 @@ const CommandDesc echo_cmd = { "echo ...: display given parameters in the status line", ParameterDesc{ { { "markup", { false, "parse markup" } }, + { "to-file", { true, "echo contents to given filename" } }, { "debug", { false, "write to debug buffer instead of status line" } } }, ParameterDesc::Flags::SwitchesOnlyAtStart }, @@ -1208,7 +1209,11 @@ const CommandDesc echo_cmd = { CommandCompleter{}, [](const ParametersParser& parser, Context& context, const ShellContext&) { - String message = fix_atom_text(join(parser, ' ', false)); + String message = join(parser, ' ', false); + if (auto filename = parser.get_switch("to-file")) + return write_to_file(*filename, message); + + message = fix_atom_text(message); if (parser.get_switch("debug")) write_to_debug_buffer(message); else if (parser.get_switch("markup")) diff --git a/src/file.cc b/src/file.cc index 7b49595e..52a0f288 100644 --- a/src/file.cc +++ b/src/file.cc @@ -257,6 +257,15 @@ void write(int fd, StringView data) } } +void write_to_file(StringView filename, StringView data) +{ + const int fd = open(filename.zstr(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + if (fd == -1) + throw file_access_error(filename, strerror(errno)); + auto close_fd = on_scope_end([fd]{ close(fd); }); + write(fd, data); +} + struct BufferedWriter { BufferedWriter(int fd) : fd{fd} {} diff --git a/src/file.hh b/src/file.hh index 6871a1a3..5264616b 100644 --- a/src/file.hh +++ b/src/file.hh @@ -39,6 +39,7 @@ bool fd_writable(int fd); String read_fd(int fd, bool text = false); String read_file(StringView filename, bool text = false); void write(int fd, StringView data); +void write_to_file(StringView filename, StringView data); struct MappedFile { diff --git a/test/compose/echo-to-file/cmd b/test/compose/echo-to-file/cmd new file mode 100644 index 00000000..f5e4924b --- /dev/null +++ b/test/compose/echo-to-file/cmd @@ -0,0 +1 @@ +:echo -to-file data %{foo bar}!cat data diff --git a/test/compose/echo-to-file/in b/test/compose/echo-to-file/in new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/test/compose/echo-to-file/in @@ -0,0 +1 @@ + diff --git a/test/compose/echo-to-file/out b/test/compose/echo-to-file/out new file mode 100644 index 00000000..d675fa44 --- /dev/null +++ b/test/compose/echo-to-file/out @@ -0,0 +1 @@ +foo bar