2 * Copyright 2019 Erich E. Hoover
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(chcp
);
27 static void output_writeconsole(const WCHAR
*str
, DWORD wlen
)
31 if (!WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), str
, wlen
, &count
, NULL
))
36 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
37 * back to WriteFile(), assuming the console encoding is still the right
40 len
= WideCharToMultiByte(GetOEMCP(), 0, str
, wlen
, NULL
, 0, NULL
, NULL
);
44 WideCharToMultiByte(GetOEMCP(), 0, str
, wlen
, msgA
, len
, NULL
, NULL
);
45 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), msgA
, len
, &count
, FALSE
);
50 static void output_formatstring(const WCHAR
*fmt
, va_list va_args
)
55 len
= FormatMessageW(FORMAT_MESSAGE_FROM_STRING
|FORMAT_MESSAGE_ALLOCATE_BUFFER
,
56 fmt
, 0, 0, (WCHAR
*)&str
, 0, &va_args
);
57 if (!len
&& GetLastError() != ERROR_NO_WORK_DONE
)
59 WINE_FIXME("Could not format string: le=%lu, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt
));
62 output_writeconsole(str
, len
);
66 static void WINAPIV
output_message(unsigned int id
, ...)
72 if (!(len
= LoadStringW(GetModuleHandleW(NULL
), id
, (WCHAR
*)&fmt
, 0)))
74 WINE_FIXME("LoadString failed with %ld\n", GetLastError());
79 fmt
= malloc(len
* sizeof(WCHAR
));
82 LoadStringW(GetModuleHandleW(NULL
), id
, fmt
, len
);
84 va_start(va_args
, id
);
85 output_formatstring(fmt
, va_args
);
91 int __cdecl
wmain(int argc
, WCHAR
*argv
[])
97 output_message(STRING_ACTIVE_CODE_PAGE
, GetConsoleCP());
102 int codepage
, success
;
104 if (!lstrcmpW(argv
[1], L
"/?"))
106 output_message(STRING_USAGE
);
110 codepage
= _wtoi(argv
[1]);
111 success
= SetConsoleCP(codepage
) && SetConsoleOutputCP(codepage
);
114 output_message(STRING_ACTIVE_CODE_PAGE
, codepage
);
116 output_message(STRING_INVALID_CODE_PAGE
);
121 WINE_FIXME("unexpected arguments:");
122 for (i
= 0; i
< argc
; i
++)
123 WINE_FIXME(" %s", wine_dbgstr_w(argv
[i
]));