From ab7284679428197cc5d0e891db6f2ee1d00a6c6d Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Thu, 2 Nov 2023 10:55:30 -0400 Subject: cmake -E cat: Add ability to print standard input --- Help/manual/cmake.1.rst | 4 ++++ Help/release/dev/cmake-E-cat-stdin.rst | 5 +++++ Source/cmcmd.cxx | 18 ++++++++++++++---- Tests/RunCMake/CommandLine/E_cat-stdin-stdout.txt | 1 + Tests/RunCMake/CommandLine/E_cat-stdin.cmake | 10 ++++++++++ Tests/RunCMake/CommandLine/RunCMakeTest.cmake | 2 ++ 6 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 Help/release/dev/cmake-E-cat-stdin.rst create mode 100644 Tests/RunCMake/CommandLine/E_cat-stdin-stdout.txt create mode 100644 Tests/RunCMake/CommandLine/E_cat-stdin.cmake diff --git a/Help/manual/cmake.1.rst b/Help/manual/cmake.1.rst index 5223acb..15ae6ea 100644 --- a/Help/manual/cmake.1.rst +++ b/Help/manual/cmake.1.rst @@ -891,6 +891,10 @@ Available commands are: ``-`` will result in an error. Use ``--`` to indicate the end of options, in case a file starts with ``-``. + .. versionadded:: 3.29 + + ``cat`` can now print the standard input by passing the ``-`` argument. + .. program:: cmake-E .. option:: chdir [...] diff --git a/Help/release/dev/cmake-E-cat-stdin.rst b/Help/release/dev/cmake-E-cat-stdin.rst new file mode 100644 index 0000000..43a8aed --- /dev/null +++ b/Help/release/dev/cmake-E-cat-stdin.rst @@ -0,0 +1,5 @@ +cmake-E-cat-stdin +----------------- + +* :manual:`cmake(1)` :option:`-E cat ` can now print the standard + input by passing the ``-`` argument. diff --git a/Source/cmcmd.cxx b/Source/cmcmd.cxx index 43a945f..93b0086 100644 --- a/Source/cmcmd.cxx +++ b/Source/cmcmd.cxx @@ -203,11 +203,16 @@ bool cmTarFilesFrom(std::string const& file, std::vector& files) void cmCatFile(const std::string& fileToAppend) { #ifdef _WIN32 + _setmode(fileno(stdin), _O_BINARY); _setmode(fileno(stdout), _O_BINARY); #endif - cmsys::ifstream source(fileToAppend.c_str(), - (std::ios::binary | std::ios::in)); - std::cout << source.rdbuf(); + std::streambuf* buf = std::cin.rdbuf(); + cmsys::ifstream source; + if (fileToAppend != "-") { + source.open(fileToAppend.c_str(), (std::ios::binary | std::ios::in)); + buf = source.rdbuf(); + } + std::cout << buf; } bool cmRemoveDirectory(const std::string& dir, bool recursive = true) @@ -1147,7 +1152,12 @@ int cmcmd::ExecuteCMakeCommand(std::vector const& args, int return_value = 0; bool doing_options = true; for (auto const& arg : cmMakeRange(args).advance(2)) { - if (doing_options && cmHasLiteralPrefix(arg, "-")) { + if (arg == "-") { + doing_options = false; + // Destroy console buffers to drop cout/cerr encoding transform. + consoleBuf.reset(); + cmCatFile(arg); + } else if (doing_options && cmHasLiteralPrefix(arg, "-")) { if (arg == "--") { doing_options = false; } else { diff --git a/Tests/RunCMake/CommandLine/E_cat-stdin-stdout.txt b/Tests/RunCMake/CommandLine/E_cat-stdin-stdout.txt new file mode 100644 index 0000000..8210d59 --- /dev/null +++ b/Tests/RunCMake/CommandLine/E_cat-stdin-stdout.txt @@ -0,0 +1 @@ +^Hello world$ diff --git a/Tests/RunCMake/CommandLine/E_cat-stdin.cmake b/Tests/RunCMake/CommandLine/E_cat-stdin.cmake new file mode 100644 index 0000000..e83e619 --- /dev/null +++ b/Tests/RunCMake/CommandLine/E_cat-stdin.cmake @@ -0,0 +1,10 @@ +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/ell.txt" "ell") +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/rld.txt" "rld") +execute_process( + COMMAND ${CMAKE_COMMAND} -E echo_append "H" + COMMAND ${CMAKE_COMMAND} -E cat - + ) +execute_process( + COMMAND ${CMAKE_COMMAND} -E echo_append "o wo" + COMMAND ${CMAKE_COMMAND} -E cat "${CMAKE_CURRENT_BINARY_DIR}/ell.txt" - "${CMAKE_CURRENT_BINARY_DIR}/rld.txt" + ) diff --git a/Tests/RunCMake/CommandLine/RunCMakeTest.cmake b/Tests/RunCMake/CommandLine/RunCMakeTest.cmake index 52be1bb..6d4e0e2 100644 --- a/Tests/RunCMake/CommandLine/RunCMakeTest.cmake +++ b/Tests/RunCMake/CommandLine/RunCMakeTest.cmake @@ -782,6 +782,8 @@ run_cmake_command(E_cat-without-double-dash ${CMAKE_COMMAND} -E cat "-file-start unset(RunCMake_TEST_COMMAND_WORKING_DIRECTORY) unset(out) +run_cmake_command(E_cat-stdin ${CMAKE_COMMAND} -P ${RunCMake_SOURCE_DIR}/E_cat-stdin.cmake) + # Unset environment variables that are used for testing cmake -E unset(ENV{TEST_ENV}) unset(ENV{TEST_ENV_EXPECTED}) -- cgit v0.12