diff --git a/Help/manual/cmake.1.rst b/Help/manual/cmake.1.rst index 5a2524bd27..f31ef43e8d 100644 --- a/Help/manual/cmake.1.rst +++ b/Help/manual/cmake.1.rst @@ -1192,6 +1192,10 @@ Available commands are: ``cat`` can now print the standard input by passing the ``-`` argument. + .. versionadded:: 4.4 + + ``cat`` will print the standard input when no arguments are passed. + .. program:: cmake-E .. option:: chdir [...] diff --git a/Source/cmcmd.cxx b/Source/cmcmd.cxx index 7d95c73fbb..4b08d00484 100644 --- a/Source/cmcmd.cxx +++ b/Source/cmcmd.cxx @@ -203,7 +203,7 @@ bool cmTarFilesFrom(std::string const& file, std::vector& files) return true; } -void cmCatFile(std::string const& fileToAppend) +void cmCatFile(cm::optional fileToAppend) { #ifdef _WIN32 _setmode(fileno(stdin), _O_BINARY); @@ -211,8 +211,8 @@ void cmCatFile(std::string const& fileToAppend) #endif std::streambuf* buf = std::cin.rdbuf(); cmsys::ifstream source; - if (fileToAppend != "-") { - source.open(fileToAppend.c_str(), (std::ios::binary | std::ios::in)); + if (fileToAppend.has_value()) { + source.open(fileToAppend->c_str(), (std::ios::binary | std::ios::in)); buf = source.rdbuf(); } std::cout << buf; @@ -1535,7 +1535,13 @@ int cmcmd::ExecuteCMakeCommand(std::vector const& args, } // Command to concat files into one - if (args[1] == "cat" && args.size() >= 3) { + if (args[1] == "cat") { + if (args.size() == 2) { + // Destroy console buffers to drop cout/cerr encoding transform. + console.reset(); + cmCatFile(cm::nullopt); + return 0; + } int return_value = 0; bool doing_options = true; for (auto const& arg : cmMakeRange(args).advance(2)) { @@ -1543,7 +1549,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector const& args, doing_options = false; // Destroy console buffers to drop cout/cerr encoding transform. console.reset(); - cmCatFile(arg); + cmCatFile(cm::nullopt); } else if (doing_options && cmHasPrefix(arg, '-')) { if (arg == "--") { doing_options = false; diff --git a/Tests/RunCMake/CommandLine/E_cat-stdin.cmake b/Tests/RunCMake/CommandLine/E_cat-stdin.cmake index e83e6194d3..e168bb994d 100644 --- a/Tests/RunCMake/CommandLine/E_cat-stdin.cmake +++ b/Tests/RunCMake/CommandLine/E_cat-stdin.cmake @@ -1,10 +1,14 @@ -file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/ell.txt" "ell") +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/ll.txt" "ll") file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/rld.txt" "rld") execute_process( COMMAND ${CMAKE_COMMAND} -E echo_append "H" - COMMAND ${CMAKE_COMMAND} -E cat - + COMMAND ${CMAKE_COMMAND} -E cat - # Read stdin from - arg + ) +execute_process( + COMMAND ${CMAKE_COMMAND} -E echo_append "e" + COMMAND ${CMAKE_COMMAND} -E cat # Read stdin when no args ) 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" + COMMAND ${CMAKE_COMMAND} -E cat "${CMAKE_CURRENT_BINARY_DIR}/ll.txt" - "${CMAKE_CURRENT_BINARY_DIR}/rld.txt" )