Clean up standard stream logic

Now we have enough portability stuff
This commit is contained in:
John Ericson 2024-11-07 13:15:03 -05:00
parent 0ed5af164f
commit 372353722e
9 changed files with 44 additions and 18 deletions

View File

@ -543,7 +543,7 @@ public:
auto state(state_.lock());
if (!state->active) return {};
std::cerr << fmt("\r\e[K%s ", msg);
auto s = trim(readLine(STDIN_FILENO, true));
auto s = trim(readLine(getStandardInput(), true));
if (s.size() != 1) return {};
draw(*state);
return s[0];

View File

@ -106,8 +106,25 @@ void drainFD(
#endif
);
/**
* Get [Standard Input](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin))
*/
[[gnu::always_inline]]
inline Descriptor getStandardOut() {
inline Descriptor getStandardInput()
{
#ifndef _WIN32
return STDIN_FILENO;
#else
return GetStdHandle(STD_INPUT_HANDLE);
#endif
}
/**
* Get [Standard Output](https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout))
*/
[[gnu::always_inline]]
inline Descriptor getStandardOutput()
{
#ifndef _WIN32
return STDOUT_FILENO;
#else
@ -115,6 +132,19 @@ inline Descriptor getStandardOut() {
#endif
}
/**
* Get [Standard Error](https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr))
*/
[[gnu::always_inline]]
inline Descriptor getStandardError()
{
#ifndef _WIN32
return STDERR_FILENO;
#else
return GetStdHandle(STD_ERROR_HANDLE);
#endif
}
/**
* Automatic cleanup of resources.
*/

View File

@ -38,7 +38,7 @@ void Logger::warn(const std::string & msg)
void Logger::writeToStdout(std::string_view s)
{
Descriptor standard_out = getStandardOut();
Descriptor standard_out = getStandardOutput();
writeFull(standard_out, s);
writeFull(standard_out, "\n");
}
@ -118,11 +118,7 @@ void writeToStderr(std::string_view s)
{
try {
writeFull(
#ifdef _WIN32
GetStdHandle(STD_ERROR_HANDLE),
#else
STDERR_FILENO,
#endif
getStandardError(),
s, false);
} catch (SystemError & e) {
/* Ignore failing writes to stderr. We need to ignore write

View File

@ -694,7 +694,7 @@ static void opDump(Strings opFlags, Strings opArgs)
if (!opFlags.empty()) throw UsageError("unknown flag");
if (opArgs.size() != 1) throw UsageError("only one argument allowed");
FdSink sink(getStandardOut());
FdSink sink(getStandardOutput());
std::string path = *opArgs.begin();
dumpPath(path, sink);
sink.flush();
@ -722,7 +722,7 @@ static void opExport(Strings opFlags, Strings opArgs)
for (auto & i : opArgs)
paths.insert(store->followLinksToStorePath(i));
FdSink sink(getStandardOut());
FdSink sink(getStandardOutput());
store->exportPaths(paths, sink);
sink.flush();
}
@ -835,7 +835,7 @@ static void opServe(Strings opFlags, Strings opArgs)
if (!opArgs.empty()) throw UsageError("no arguments expected");
FdSource in(STDIN_FILENO);
FdSink out(getStandardOut());
FdSink out(getStandardOutput());
/* Exchange the greeting. */
ServeProto::Version clientVersion =

View File

@ -16,7 +16,7 @@ struct MixCat : virtual Args
throw Error("path '%1%' is not a regular file", path);
stopProgressBar();
writeFull(getStandardOut(), accessor->readFile(CanonPath(path)));
writeFull(getStandardOutput(), accessor->readFile(CanonPath(path)));
}
};

View File

@ -20,7 +20,7 @@ struct CmdDumpPath : StorePathCommand
void run(ref<Store> store, const StorePath & storePath) override
{
FdSink sink(getStandardOut());
FdSink sink(getStandardOutput());
store->narFromPath(storePath, sink);
sink.flush();
}
@ -55,7 +55,7 @@ struct CmdDumpPath2 : Command
void run() override
{
FdSink sink(getStandardOut());
FdSink sink(getStandardOutput());
dumpPath(path, sink);
sink.flush();
}

View File

@ -115,7 +115,7 @@ struct CmdEval : MixJSON, InstallableValueCommand, MixReadOnlyOption
else if (raw) {
stopProgressBar();
writeFull(getStandardOut(), *state->coerceToString(noPos, *v, context, "while generating the eval command output"));
writeFull(getStandardOutput(), *state->coerceToString(noPos, *v, context, "while generating the eval command output"));
}
else if (json) {

View File

@ -57,7 +57,7 @@ struct CmdLog : InstallableCommand
if (!log) continue;
stopProgressBar();
printInfo("got build log for '%s' from '%s'", installable->what(), logSub.getUri());
writeFull(getStandardOut(), *log);
writeFull(getStandardOutput(), *log);
return;
}

View File

@ -177,7 +177,7 @@ struct CmdKeyGenerateSecret : Command
throw UsageError("required argument '--key-name' is missing");
stopProgressBar();
writeFull(getStandardOut(), SecretKey::generate(*keyName).to_string());
writeFull(getStandardOutput(), SecretKey::generate(*keyName).to_string());
}
};
@ -199,7 +199,7 @@ struct CmdKeyConvertSecretToPublic : Command
{
SecretKey secretKey(drainFD(STDIN_FILENO));
stopProgressBar();
writeFull(getStandardOut(), secretKey.toPublicKey().to_string());
writeFull(getStandardOutput(), secretKey.toPublicKey().to_string());
}
};