cmake -E cat: Read from stdin when no other args are given

This commit is contained in:
Martin Duffy
2026-02-19 16:33:06 -05:00
parent bcaa0f9b29
commit 50535776c5
3 changed files with 22 additions and 8 deletions
+4
View File
@@ -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 <dir> <cmd> [<arg>...]
+11 -5
View File
@@ -203,7 +203,7 @@ bool cmTarFilesFrom(std::string const& file, std::vector<std::string>& files)
return true;
}
void cmCatFile(std::string const& fileToAppend)
void cmCatFile(cm::optional<std::string> 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<std::string> 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<std::string> 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;
+7 -3
View File
@@ -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"
)