xref: /freebsd/contrib/llvm-project/libcxx/src/print.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
106c3fb27SDimitry Andric //===----------------------------------------------------------------------===//
206c3fb27SDimitry Andric //
306c3fb27SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406c3fb27SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
506c3fb27SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606c3fb27SDimitry Andric //
706c3fb27SDimitry Andric //===----------------------------------------------------------------------===//
806c3fb27SDimitry Andric 
906c3fb27SDimitry Andric #include <__config>
10*5f757f3fSDimitry Andric 
11*5f757f3fSDimitry Andric #if defined(_LIBCPP_WIN32API)
12*5f757f3fSDimitry Andric 
1306c3fb27SDimitry Andric #  include <cstdlib>
1406c3fb27SDimitry Andric #  include <print>
1506c3fb27SDimitry Andric 
1606c3fb27SDimitry Andric #  define WIN32_LEAN_AND_MEAN
1706c3fb27SDimitry Andric #  define NOMINMAX
1806c3fb27SDimitry Andric #  include <io.h>
1906c3fb27SDimitry Andric #  include <windows.h>
2006c3fb27SDimitry Andric 
2106c3fb27SDimitry Andric #  include <__system_error/system_error.h>
2206c3fb27SDimitry Andric 
2306c3fb27SDimitry Andric #  include "filesystem/error.h"
2406c3fb27SDimitry Andric 
2506c3fb27SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
2606c3fb27SDimitry Andric 
2706c3fb27SDimitry Andric _LIBCPP_EXPORTED_FROM_ABI bool __is_windows_terminal(FILE* __stream) {
2806c3fb27SDimitry Andric   // Note the Standard does this in one call, but it's unclear whether
2906c3fb27SDimitry Andric   // an invalid handle is allowed when calling GetConsoleMode.
3006c3fb27SDimitry Andric   //
3106c3fb27SDimitry Andric   // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=msvc-170
3206c3fb27SDimitry Andric   // https://learn.microsoft.com/en-us/windows/console/getconsolemode
3306c3fb27SDimitry Andric   intptr_t __handle = _get_osfhandle(fileno(__stream));
3406c3fb27SDimitry Andric   if (__handle == -1)
3506c3fb27SDimitry Andric     return false;
3606c3fb27SDimitry Andric 
3706c3fb27SDimitry Andric   unsigned long __mode;
3806c3fb27SDimitry Andric   return GetConsoleMode(reinterpret_cast<void*>(__handle), &__mode);
3906c3fb27SDimitry Andric }
4006c3fb27SDimitry Andric 
4106c3fb27SDimitry Andric #  ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4206c3fb27SDimitry Andric _LIBCPP_EXPORTED_FROM_ABI void
4306c3fb27SDimitry Andric __write_to_windows_console([[maybe_unused]] FILE* __stream, [[maybe_unused]] wstring_view __view) {
4406c3fb27SDimitry Andric   // https://learn.microsoft.com/en-us/windows/console/writeconsole
4506c3fb27SDimitry Andric   if (WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fileno(__stream))), // clang-format aid
4606c3fb27SDimitry Andric                     __view.data(),
4706c3fb27SDimitry Andric                     __view.size(),
4806c3fb27SDimitry Andric                     nullptr,
4906c3fb27SDimitry Andric                     nullptr) == 0) {
50*5f757f3fSDimitry Andric     __throw_system_error(filesystem::detail::make_windows_error(GetLastError()), "failed to write formatted output");
5106c3fb27SDimitry Andric   }
5206c3fb27SDimitry Andric }
5306c3fb27SDimitry Andric #  endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
5406c3fb27SDimitry Andric 
5506c3fb27SDimitry Andric _LIBCPP_END_NAMESPACE_STD
56*5f757f3fSDimitry Andric 
57*5f757f3fSDimitry Andric #endif // !_LIBCPP_WIN32API
58