Skip to content

Commit

Permalink
Use a function to get stdout descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
puffnfresh authored and Ericson2314 committed Mar 26, 2024
1 parent c0b3efa commit e820ba8
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 101 deletions.
1 change: 1 addition & 0 deletions src/libmain/shared.cc
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ int handleExceptions(const std::string & programName, std::function<void()> fun)
return 0;
}


RunPager::RunPager()
{
if (!isatty(STDOUT_FILENO)) return;
Expand Down
1 change: 1 addition & 0 deletions src/libmain/stack.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace nix {


#ifndef __WIN32
static void sigsegvHandler(int signo, siginfo_t * info, void * ctx)
{
Expand Down
13 changes: 11 additions & 2 deletions src/libutil/file-descriptor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include "error.hh"

#ifdef _WIN32
# include <handleapi.h>
# include <io.h>
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif

namespace nix {
Expand Down Expand Up @@ -97,6 +97,15 @@ void drainFD(
#endif
);

[[gnu::always_inline]]
inline Descriptor getStandardOut() {
#ifndef __WIN32
return STDOUT_FILENO;
#else
return GetStdHandle(STD_OUTPUT_HANDLE);
#endif
}

/**
* Automatic cleanup of resources.
*/
Expand Down
14 changes: 1 addition & 13 deletions src/libutil/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@
#include <nlohmann/json.hpp>
#include <iostream>

#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# include <processthreadsapi.h>
#endif

namespace nix {

LoggerSettings loggerSettings;
Expand All @@ -43,13 +37,7 @@ void Logger::warn(const std::string & msg)

void Logger::writeToStdout(std::string_view s)
{
Descriptor standard_out =
#ifdef _WIN32
GetStdHandle(STD_OUTPUT_HANDLE)
#else
STDOUT_FILENO
#endif
;
Descriptor standard_out = getStandardOut();
writeFull(standard_out, s);
writeFull(standard_out, "\n");
}
Expand Down
15 changes: 1 addition & 14 deletions src/nix/cat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
#include "nar-accessor.hh"
#include "progress-bar.hh"

#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# include <processthreadsapi.h>
#endif

using namespace nix;

struct MixCat : virtual Args
Expand All @@ -22,14 +16,7 @@ struct MixCat : virtual Args
throw Error("path '%1%' is not a regular file", path);
stopProgressBar();

Descriptor standard_out =
#ifdef _WIN32
GetStdHandle(STD_OUTPUT_HANDLE)
#else
STDOUT_FILENO
#endif
;
writeFull(standard_out, accessor->readFile(CanonPath(path)));
writeFull(getStandardOut(), accessor->readFile(CanonPath(path)));
}
};

Expand Down
24 changes: 2 additions & 22 deletions src/nix/dump-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
#include "store-api.hh"
#include "archive.hh"

#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# include <processthreadsapi.h>
#endif

using namespace nix;

struct CmdDumpPath : StorePathCommand
Expand All @@ -26,14 +20,7 @@ struct CmdDumpPath : StorePathCommand

void run(ref<Store> store, const StorePath & storePath) override
{
Descriptor standard_out =
#ifdef _WIN32
GetStdHandle(STD_OUTPUT_HANDLE)
#else
STDOUT_FILENO
#endif
;
FdSink sink(standard_out);
FdSink sink(getStandardOut());
store->narFromPath(storePath, sink);
sink.flush();
}
Expand Down Expand Up @@ -68,14 +55,7 @@ struct CmdDumpPath2 : Command

void run() override
{
Descriptor standard_out =
#ifdef _WIN32
GetStdHandle(STD_OUTPUT_HANDLE)
#else
STDOUT_FILENO
#endif
;
FdSink sink(standard_out);
FdSink sink(getStandardOut());
dumpPath(path, sink);
sink.flush();
}
Expand Down
15 changes: 1 addition & 14 deletions src/nix/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@

#include <nlohmann/json.hpp>

#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# include <processthreadsapi.h>
#endif

using namespace nix;

struct CmdEval : MixJSON, InstallableValueCommand, MixReadOnlyOption
Expand Down Expand Up @@ -122,14 +116,7 @@ struct CmdEval : MixJSON, InstallableValueCommand, MixReadOnlyOption

else if (raw) {
stopProgressBar();
Descriptor standard_out =
#ifdef _WIN32
GetStdHandle(STD_OUTPUT_HANDLE)
#else
STDOUT_FILENO
#endif
;
writeFull(standard_out, *state->coerceToString(noPos, *v, context, "while generating the eval command output"));
writeFull(getStandardOut(), *state->coerceToString(noPos, *v, context, "while generating the eval command output"));
}

else if (json) {
Expand Down
15 changes: 1 addition & 14 deletions src/nix/log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
#include "log-store.hh"
#include "progress-bar.hh"

#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# include <processthreadsapi.h>
#endif

using namespace nix;

struct CmdLog : InstallableCommand
Expand Down Expand Up @@ -63,14 +57,7 @@ struct CmdLog : InstallableCommand
if (!log) continue;
stopProgressBar();
printInfo("got build log for '%s' from '%s'", installable->what(), logSub.getUri());
Descriptor standard_out =
#ifdef _WIN32
GetStdHandle(STD_OUTPUT_HANDLE)
#else
STDOUT_FILENO
#endif
;
writeFull(standard_out, *log);
writeFull(getStandardOut(), *log);
return;
}

Expand Down
24 changes: 2 additions & 22 deletions src/nix/sigs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@

#include <atomic>

#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# include <processthreadsapi.h>
#endif

using namespace nix;

struct CmdCopySigs : StorePathsCommand
Expand Down Expand Up @@ -183,14 +177,7 @@ struct CmdKeyGenerateSecret : Command
throw UsageError("required argument '--key-name' is missing");

stopProgressBar();
Descriptor standard_out =
#ifdef _WIN32
GetStdHandle(STD_OUTPUT_HANDLE)
#else
STDOUT_FILENO
#endif
;
writeFull(standard_out, SecretKey::generate(*keyName).to_string());
writeFull(getStandardOut(), SecretKey::generate(*keyName).to_string());
}
};

Expand All @@ -212,14 +199,7 @@ struct CmdKeyConvertSecretToPublic : Command
{
SecretKey secretKey(drainFD(STDIN_FILENO));
stopProgressBar();
Descriptor standard_out =
#ifdef _WIN32
GetStdHandle(STD_OUTPUT_HANDLE)
#else
STDOUT_FILENO
#endif
;
writeFull(standard_out, secretKey.toPublicKey().to_string());
writeFull(getStandardOut(), secretKey.toPublicKey().to_string());
}
};

Expand Down

0 comments on commit e820ba8

Please sign in to comment.