VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/include/NoCrtOutput.h@ 98103

Last change on this file since 98103 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.2 KB
Line 
1/* $Id: NoCrtOutput.h 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * NoCrtOutput - ErrorMsgXxx and PrintXxx functions for small EXEs.
4 */
5
6/*
7 * Copyright (C) 2022-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#ifndef GA_INCLUDED_WINNT_NoCrtOutput_h
29#define GA_INCLUDED_WINNT_NoCrtOutput_h
30#ifndef RT_WITHOUT_PRAGMA_ONCE
31# pragma once
32#endif
33
34#include <iprt/win/windows.h>
35#include <iprt/string.h>
36#include <iprt/utf16.h>
37
38
39/** @name Output helpers
40 *
41 * The general ASSUMPTION here is that all strings are restricted to 7-bit
42 * ASCII, with the exception of wchar_t ones.
43 *
44 * @note We don't use printf, RTPrintf or similar not for masochistic reasons
45 * but to keep the binary small and make it easier to switch between CRT
46 * and IPRT w/ no-CRT.
47 *
48 * @{
49 */
50
51DECLINLINE(void) OutputWStr(HANDLE hDst, const wchar_t *pwszStr)
52{
53 DWORD cbIgn;
54 if (GetConsoleMode(hDst, &cbIgn))
55 WriteConsoleW(hDst, pwszStr, (DWORD)RTUtf16Len(pwszStr), &cbIgn, NULL);
56 else
57 {
58 char *pszTmp;
59 int rc = RTUtf16ToUtf8(pwszStr, &pszTmp);
60 if (RT_SUCCESS(rc))
61 {
62 char *pszInCodepage;
63 rc = RTStrUtf8ToCurrentCP(&pszInCodepage, pszTmp);
64 if (RT_SUCCESS(rc))
65 {
66 WriteFile(hDst, pszInCodepage, (DWORD)strlen(pszInCodepage), &cbIgn, NULL);
67 RTStrFree(pszInCodepage);
68 }
69 else
70 WriteFile(hDst, RT_STR_TUPLE("<RTStrUtf8ToCurrentCP error>"), &cbIgn, NULL);
71 RTStrFree(pszTmp);
72 }
73 else
74 WriteFile(hDst, RT_STR_TUPLE("<RTUtf16ToUtf8 error>"), &cbIgn, NULL);
75 }
76}
77
78
79DECLINLINE(void) ErrorMsgBegin(const char *pszMsg)
80{
81 HANDLE const hStdErr = GetStdHandle(STD_ERROR_HANDLE);
82 DWORD cbIgn;
83 WriteFile(hStdErr, RT_STR_TUPLE("error: "), &cbIgn, NULL);
84 WriteFile(hStdErr, pszMsg, (DWORD)strlen(pszMsg), &cbIgn, NULL);
85}
86
87
88DECLINLINE(void) ErrorMsgStr(const char *pszMsg)
89{
90 HANDLE const hStdErr = GetStdHandle(STD_ERROR_HANDLE);
91 DWORD cbIgn;
92 WriteFile(hStdErr, pszMsg, (DWORD)strlen(pszMsg), &cbIgn, NULL);
93}
94
95
96DECLINLINE(void) ErrorMsgWStr(const wchar_t *pwszStr)
97{
98 OutputWStr(GetStdHandle(STD_ERROR_HANDLE), pwszStr);
99}
100
101
102DECLINLINE(int) ErrorMsgEnd(const char *pszMsg)
103{
104 HANDLE const hStdErr = GetStdHandle(STD_ERROR_HANDLE);
105 DWORD cbIgn;
106 if (pszMsg)
107 WriteFile(hStdErr, pszMsg, (DWORD)strlen(pszMsg), &cbIgn, NULL);
108 WriteFile(hStdErr, RT_STR_TUPLE("\r\n"), &cbIgn, NULL);
109#ifdef EXIT_FAIL /* VBoxDrvInst.cpp speciality */
110 return EXIT_FAIL;
111#else
112 return RTEXITCODE_FAILURE;
113#endif
114}
115
116
117DECLINLINE(void) ErrorMsgU64(uint64_t uValue, bool fSigned = false)
118{
119 char szVal[128];
120 RTStrFormatU64(szVal, sizeof(szVal), uValue, 10, 0, 0, fSigned ? RTSTR_F_VALSIGNED : 0);
121 ErrorMsgStr(szVal);
122}
123
124
125DECLINLINE(int) ErrorMsg(const char *pszMsg)
126{
127 ErrorMsgBegin(pszMsg);
128 return ErrorMsgEnd(NULL);
129}
130
131
132DECLINLINE(int) ErrorMsgSU(const char *pszMsg1, uint64_t uValue1)
133{
134 ErrorMsgBegin(pszMsg1);
135 ErrorMsgU64(uValue1);
136 return ErrorMsgEnd(NULL);
137}
138
139
140DECLINLINE(int) ErrorMsgSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3)
141{
142 ErrorMsgBegin(pszMsg1);
143 ErrorMsgWStr(pwszMsg2);
144 return ErrorMsgEnd(pszMsg3);
145}
146
147
148DECLINLINE(int) ErrorMsgSWSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3,
149 const wchar_t *pwszMsg4, const char *pszMsg5)
150{
151 ErrorMsgBegin(pszMsg1);
152 ErrorMsgWStr(pwszMsg2);
153 ErrorMsgStr(pszMsg3);
154 ErrorMsgWStr(pwszMsg4);
155 return ErrorMsgEnd(pszMsg5);
156}
157
158
159DECLINLINE(int) ErrorMsgSUSUS(const char *pszMsg1, uint64_t uValue1, const char *pszMsg2, uint64_t uValue2, const char *pszMsg3)
160{
161 ErrorMsgBegin(pszMsg1);
162 ErrorMsgU64(uValue1);
163 ErrorMsgStr(pszMsg2);
164 ErrorMsgU64(uValue2);
165 return ErrorMsgEnd(pszMsg3);
166}
167
168
169DECLINLINE(void) ErrorMsgErrVal(uint32_t uErrVal, bool fSigned)
170{
171 char szVal[128];
172 ssize_t cchVal = RTStrFormatU32(szVal, sizeof(szVal) - 1, uErrVal, 10, 0, 0, fSigned ? RTSTR_F_VALSIGNED : 0);
173 szVal[cchVal++] = '/';
174 szVal[cchVal] = '\0';
175 ErrorMsgStr(szVal);
176
177 RTStrFormatU32(szVal, sizeof(szVal) - 1, uErrVal, 16, 0, 0, RTSTR_F_SPECIAL);
178 ErrorMsgStr(szVal);
179}
180
181
182DECLINLINE(int) ErrorMsgErr(const char *pszMsg, uint32_t uErrVal, const char *pszErrIntro, bool fSigned)
183{
184 ErrorMsgBegin(pszMsg);
185 ErrorMsgStr(pszErrIntro);
186 ErrorMsgErrVal(uErrVal, fSigned);
187 return ErrorMsgEnd(")");
188}
189
190
191DECLINLINE(int) ErrorMsgRc(int rcExit, const char *pszMsg)
192{
193 ErrorMsgBegin(pszMsg);
194 ErrorMsgEnd(NULL);
195 return rcExit;
196}
197
198
199DECLINLINE(int) ErrorMsgRcSUS(int rcExit, const char *pszMsg1, uint64_t uValue, const char *pszMsg2)
200{
201 ErrorMsgBegin(pszMsg1);
202 ErrorMsgU64(uValue);
203 ErrorMsgEnd(pszMsg2);
204 return rcExit;
205}
206
207
208DECLINLINE(int) ErrorMsgVBoxErr(const char *pszMsg, int rc)
209{
210 return ErrorMsgErr(pszMsg, rc, " (", true);
211}
212
213
214DECLINLINE(int) ErrorMsgLastErr(const char *pszMsg)
215{
216 return ErrorMsgErr(pszMsg, GetLastError(), " (last error ", false);
217}
218
219
220DECLINLINE(int) ErrorMsgLastErrSUR(const char *pszMsg1, uint64_t uValue)
221{
222 DWORD dwErr = GetLastError();
223 ErrorMsgBegin(pszMsg1);
224 ErrorMsgU64(uValue);
225 ErrorMsgStr(" (last error ");
226 ErrorMsgErrVal(dwErr, false);
227 return ErrorMsgEnd(")");
228}
229
230
231DECLINLINE(int) ErrorMsgLastErrSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3)
232{
233 DWORD dwErr = GetLastError();
234 ErrorMsgBegin(pszMsg1);
235 ErrorMsgWStr(pwszMsg2);
236 ErrorMsgStr(pszMsg3);
237 ErrorMsgStr(" (last error ");
238 ErrorMsgErrVal(dwErr, false);
239 return ErrorMsgEnd(")");
240}
241
242
243DECLINLINE(int) ErrorMsgLastErrSWSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3,
244 const wchar_t *pwszMsg4, const char *pszMsg5)
245{
246 DWORD dwErr = GetLastError();
247 ErrorMsgBegin(pszMsg1);
248 ErrorMsgWStr(pwszMsg2);
249 ErrorMsgStr(pszMsg3);
250 ErrorMsgWStr(pwszMsg4);
251 ErrorMsgStr(pszMsg5);
252 ErrorMsgStr(" (last error ");
253 ErrorMsgErrVal(dwErr, false);
254 return ErrorMsgEnd(")");
255}
256
257
258DECLINLINE(int) ErrorMsgLastErrSWSRSUS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3, const char *pszMsg4,
259 uint64_t uValue, const char *pszMsg5)
260{
261 DWORD dwErr = GetLastError();
262 ErrorMsgBegin(pszMsg1);
263 ErrorMsgWStr(pwszMsg2);
264 ErrorMsgStr(pszMsg3);
265 ErrorMsgStr(" (last error ");
266 ErrorMsgErrVal(dwErr, false);
267 ErrorMsgStr(")");
268 ErrorMsgStr(pszMsg4);
269 ErrorMsgU64(uValue);
270 return ErrorMsgEnd(pszMsg5);
271}
272
273
274DECLINLINE(int) ErrorMsgLastErrSSS(const char *pszMsg1, const char *pszMsg2, const char *pszMsg3)
275{
276 DWORD dwErr = GetLastError();
277 ErrorMsgBegin(pszMsg1);
278 ErrorMsgStr(pszMsg2);
279 ErrorMsgStr(pszMsg3);
280 ErrorMsgStr(" (last error ");
281 ErrorMsgErrVal(dwErr, false);
282 return ErrorMsgEnd(")");
283}
284
285
286DECLINLINE(int) ErrorMsgRcLastErr(int rcExit, const char *pszMsg)
287{
288 ErrorMsgErr(pszMsg, GetLastError(), " (last error ", false);
289 return rcExit;
290}
291
292
293DECLINLINE(int) ErrorMsgRcLastErrSUR(int rcExit, const char *pszMsg1, uint64_t uValue)
294{
295 ErrorMsgLastErrSUR(pszMsg1, uValue);
296 return rcExit;
297}
298
299
300static int ErrorMsgRcLastErrSWSR(int rcExit, const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3)
301{
302 DWORD dwErr = GetLastError();
303 ErrorMsgBegin(pszMsg1);
304 ErrorMsgWStr(pwszMsg2);
305 ErrorMsgStr(pszMsg3);
306 ErrorMsgStr(" (last error ");
307 ErrorMsgErrVal(dwErr, false);
308 ErrorMsgEnd(")");
309 return rcExit;
310}
311
312
313
314DECLINLINE(int) ErrorMsgLStatus(const char *pszMsg, LSTATUS lrc)
315{
316 return ErrorMsgErr(pszMsg, (DWORD)lrc, " (", true);
317}
318
319
320DECLINLINE(int) ErrorMsgLStatusSWSRS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3,
321 LSTATUS lrc, const char *pszMsg4)
322{
323 ErrorMsgBegin(pszMsg1);
324 ErrorMsgWStr(pwszMsg2);
325 ErrorMsgStr(pszMsg3);
326 ErrorMsgErrVal((DWORD)lrc, true);
327 return ErrorMsgEnd(pszMsg4);
328}
329
330
331DECLINLINE(int) ErrorMsgLStatusSWSWSRS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3, const wchar_t *pwszMsg4,
332 const char *pszMsg5, LSTATUS lrc, const char *pszMsg6)
333{
334 ErrorMsgBegin(pszMsg1);
335 ErrorMsgWStr(pwszMsg2);
336 ErrorMsgStr(pszMsg3);
337 ErrorMsgWStr(pwszMsg4);
338 ErrorMsgStr(pszMsg5);
339 ErrorMsgErrVal((DWORD)lrc, true);
340 return ErrorMsgEnd(pszMsg6);
341}
342
343
344DECLINLINE(int) ErrorMsgLStatusSWSWSWSRS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3,
345 const wchar_t *pwszMsg4, const char *pszMsg5, const wchar_t *pwszMsg6,
346 const char *pszMsg7, LSTATUS lrc, const char *pszMsg8)
347{
348 ErrorMsgBegin(pszMsg1);
349 ErrorMsgWStr(pwszMsg2);
350 ErrorMsgStr(pszMsg3);
351 ErrorMsgWStr(pwszMsg4);
352 ErrorMsgStr(pszMsg5);
353 ErrorMsgWStr(pwszMsg6);
354 ErrorMsgStr(pszMsg7);
355 ErrorMsgErrVal((DWORD)lrc, true);
356 return ErrorMsgEnd(pszMsg8);
357}
358
359
360DECLINLINE(int) ErrorMsgLStatusSWSWSWSWSRS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3,
361 const wchar_t *pwszMsg4, const char *pszMsg5, const wchar_t *pwszMsg6,
362 const char *pszMsg7, const wchar_t *pwszMsg8, const char *pszMsg9, LSTATUS lrc,
363 const char *pszMsg10)
364{
365 ErrorMsgBegin(pszMsg1);
366 ErrorMsgWStr(pwszMsg2);
367 ErrorMsgStr(pszMsg3);
368 ErrorMsgWStr(pwszMsg4);
369 ErrorMsgStr(pszMsg5);
370 ErrorMsgWStr(pwszMsg6);
371 ErrorMsgStr(pszMsg7);
372 ErrorMsgWStr(pwszMsg8);
373 ErrorMsgStr(pszMsg9);
374 ErrorMsgErrVal((DWORD)lrc, true);
375 return ErrorMsgEnd(pszMsg10);
376}
377
378
379DECLINLINE(int) ErrorBadArg(const char *pszName, wchar_t const *pwszArg, const char *pszValues = NULL)
380{
381 ErrorMsgBegin("Bad argument '");
382 ErrorMsgStr(pszName);
383 ErrorMsgStr("': ");
384 ErrorMsgWStr(pwszArg);
385 if (pszValues)
386 ErrorMsgStr(", expected: ");
387 return ErrorMsgEnd(pszValues);
388}
389
390
391/** Simple fputs(stdout) replacement. */
392DECLINLINE(void) PrintStr(const char *pszMsg)
393{
394 HANDLE const hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
395 DWORD cbIgn;
396 WriteFile(hStdOut, pszMsg, (DWORD)strlen(pszMsg), &cbIgn, NULL);
397}
398
399
400/** Simple fputs(stdout) replacement. */
401DECLINLINE(void) PrintWStr(const wchar_t *pwszStr)
402{
403 OutputWStr(GetStdHandle(STD_OUTPUT_HANDLE), pwszStr);
404}
405
406
407DECLINLINE(void) PrintX64(uint64_t uValue)
408{
409 char szVal[128];
410 RTStrFormatU64(szVal, sizeof(szVal), uValue, 16, 0, 0, RTSTR_F_64BIT | RTSTR_F_SPECIAL);
411 PrintStr(szVal);
412}
413
414
415DECLINLINE(void) PrintSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3)
416{
417 PrintStr(pszMsg1);
418 PrintWStr(pwszMsg2);
419 PrintStr(pszMsg3);
420}
421
422
423DECLINLINE(void) PrintSWSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3,
424 const wchar_t *pwszMsg4, const char *pszMsg5)
425{
426 PrintStr(pszMsg1);
427 PrintWStr(pwszMsg2);
428 PrintStr(pszMsg3);
429 PrintWStr(pwszMsg4);
430 PrintStr(pszMsg5);
431}
432
433
434DECLINLINE(void) PrintSWSWSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3, const wchar_t *pwszMsg4,
435 const char *pszMsg5, const wchar_t *pwszMsg6, const char *pszMsg7)
436{
437 PrintStr(pszMsg1);
438 PrintWStr(pwszMsg2);
439 PrintStr(pszMsg3);
440 PrintWStr(pwszMsg4);
441 PrintStr(pszMsg5);
442 PrintWStr(pwszMsg6);
443 PrintStr(pszMsg7);
444}
445
446
447DECLINLINE(void) PrintSWSWSWSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3, const wchar_t *pwszMsg4,
448 const char *pszMsg5, const wchar_t *pwszMsg6, const char *pszMsg7, const wchar_t *pwszMsg8,
449 const char *pszMsg9)
450{
451 PrintStr(pszMsg1);
452 PrintWStr(pwszMsg2);
453 PrintStr(pszMsg3);
454 PrintWStr(pwszMsg4);
455 PrintStr(pszMsg5);
456 PrintWStr(pwszMsg6);
457 PrintStr(pszMsg7);
458 PrintWStr(pwszMsg8);
459 PrintStr(pszMsg9);
460}
461
462
463DECLINLINE(void) PrintSXS(const char *pszMsg1, uint64_t uValue, const char *pszMsg2)
464{
465 PrintStr(pszMsg1);
466 PrintX64(uValue);
467 PrintStr(pszMsg2);
468}
469
470
471DECLINLINE(void) PrintSWSWSWSXS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3, const wchar_t *pwszMsg4,
472 const char *pszMsg5, const wchar_t *pwszMsg6, const char *pszMsg7, uint64_t uValue,
473 const char *pszMsg8)
474{
475 PrintStr(pszMsg1);
476 PrintWStr(pwszMsg2);
477 PrintStr(pszMsg3);
478 PrintWStr(pwszMsg4);
479 PrintStr(pszMsg5);
480 PrintWStr(pwszMsg6);
481 PrintStr(pszMsg7);
482 PrintX64(uValue);
483 PrintStr(pszMsg8);
484}
485
486/** @} */
487
488#endif /* !GA_INCLUDED_WINNT_NoCrtOutput_h */
489
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette