diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc index e2803fc027a4..d13722b7efaa 100644 --- a/src/libmain/shared.cc +++ b/src/libmain/shared.cc @@ -365,6 +365,7 @@ int handleExceptions(const std::string & programName, std::function fun) return 0; } + RunPager::RunPager() { if (!isatty(STDOUT_FILENO)) return; diff --git a/src/libmain/stack.cc b/src/libmain/stack.cc index 6edfb831b667..773aa950d7c4 100644 --- a/src/libmain/stack.cc +++ b/src/libmain/stack.cc @@ -10,6 +10,7 @@ namespace nix { + #ifndef __WIN32 static void sigsegvHandler(int signo, siginfo_t * info, void * ctx) { diff --git a/src/libutil/file-descriptor.hh b/src/libutil/file-descriptor.hh index a0c0d2004881..f4d443aa62f8 100644 --- a/src/libutil/file-descriptor.hh +++ b/src/libutil/file-descriptor.hh @@ -5,8 +5,8 @@ #include "error.hh" #ifdef _WIN32 -# include -# include +# define WIN32_LEAN_AND_MEAN +# include #endif namespace nix { @@ -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. */ diff --git a/src/libutil/logging.cc b/src/libutil/logging.cc index 5d2e85d7a2fe..64fc6e633822 100644 --- a/src/libutil/logging.cc +++ b/src/libutil/logging.cc @@ -11,12 +11,6 @@ #include #include -#ifdef _WIN32 -# define WIN32_LEAN_AND_MEAN -# include -# include -#endif - namespace nix { LoggerSettings loggerSettings; @@ -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"); } diff --git a/src/nix/cat.cc b/src/nix/cat.cc index f084104d1fa1..ee904b0c5ef9 100644 --- a/src/nix/cat.cc +++ b/src/nix/cat.cc @@ -3,12 +3,6 @@ #include "nar-accessor.hh" #include "progress-bar.hh" -#ifdef _WIN32 -# define WIN32_LEAN_AND_MEAN -# include -# include -#endif - using namespace nix; struct MixCat : virtual Args @@ -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))); } }; diff --git a/src/nix/dump-path.cc b/src/nix/dump-path.cc index fb7eeec769fc..953d77d3194a 100644 --- a/src/nix/dump-path.cc +++ b/src/nix/dump-path.cc @@ -2,12 +2,6 @@ #include "store-api.hh" #include "archive.hh" -#ifdef _WIN32 -# define WIN32_LEAN_AND_MEAN -# include -# include -#endif - using namespace nix; struct CmdDumpPath : StorePathCommand @@ -26,14 +20,7 @@ struct CmdDumpPath : StorePathCommand void run(ref 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(); } @@ -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(); } diff --git a/src/nix/eval.cc b/src/nix/eval.cc index a469d20f35e3..682f07353d23 100644 --- a/src/nix/eval.cc +++ b/src/nix/eval.cc @@ -9,12 +9,6 @@ #include -#ifdef _WIN32 -# define WIN32_LEAN_AND_MEAN -# include -# include -#endif - using namespace nix; struct CmdEval : MixJSON, InstallableValueCommand, MixReadOnlyOption @@ -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) { diff --git a/src/nix/log.cc b/src/nix/log.cc index 156004b9f07b..7f590c708f6f 100644 --- a/src/nix/log.cc +++ b/src/nix/log.cc @@ -5,12 +5,6 @@ #include "log-store.hh" #include "progress-bar.hh" -#ifdef _WIN32 -# define WIN32_LEAN_AND_MEAN -# include -# include -#endif - using namespace nix; struct CmdLog : InstallableCommand @@ -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; } diff --git a/src/nix/sigs.cc b/src/nix/sigs.cc index 192e797d68f3..1e277cbbee76 100644 --- a/src/nix/sigs.cc +++ b/src/nix/sigs.cc @@ -7,12 +7,6 @@ #include -#ifdef _WIN32 -# define WIN32_LEAN_AND_MEAN -# include -# include -#endif - using namespace nix; struct CmdCopySigs : StorePathsCommand @@ -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()); } }; @@ -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()); } };