VirtualBox

source: vbox/trunk/src/VBox/Runtime/win/errmsgwin-sorter.cpp@ 85126

Last change on this file since 85126 was 85121, checked in by vboxsync, 4 years ago

iprt/cdefs.h: Refactored the typedef use of DECLCALLBACK as well as DECLCALLBACKMEMBER to wrap the whole expression, similar to the DECLR?CALLBACKMEMBER macros. This allows adding a throw() at the end when compiling with the VC++ compiler to indicate that the callbacks won't throw anything, so we can stop supressing the C5039 warning about passing functions that can potential throw C++ exceptions to extern C code that can't necessarily cope with such (unwind,++). Introduced a few _EX variations that allows specifying different/no calling convention too, as that's handy when dynamically resolving host APIs. Fixed numerous places missing DECLCALLBACK and such. Left two angry @todos regarding use of CreateThread. bugref:9794

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 10.0 KB
Line 
1/* $Id: errmsgwin-sorter.cpp 85121 2020-07-08 19:33:26Z vboxsync $ */
2/** @file
3 * IPRT - Status code messages, Windows, sorter build program.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/win/windows.h>
32
33#include <iprt/errcore.h>
34#include <iprt/asm.h>
35#include <iprt/string.h>
36
37#include <stdio.h>
38#include <stdlib.h>
39
40
41/*
42 * Include the string table code.
43 */
44#define BLDPROG_STRTAB_MAX_STRLEN 512
45#define BLDPROG_STRTAB_WITH_COMPRESSION
46#define BLDPROG_STRTAB_PURE_ASCII
47#define BLDPROG_STRTAB_WITH_CAMEL_WORDS
48#include <iprt/bldprog-strtab-template.cpp.h>
49
50
51/*********************************************************************************************************************************
52* Defined Constants And Macros *
53*********************************************************************************************************************************/
54/* This is define casts the result to DWORD, whereas HRESULT and RTWINERRMSG
55 are using long, causing newer compilers to complain. */
56#undef _NDIS_ERROR_TYPEDEF_
57#define _NDIS_ERROR_TYPEDEF_(lErr) (long)(lErr)
58
59
60/*********************************************************************************************************************************
61* Structures and Typedefs *
62*********************************************************************************************************************************/
63typedef long VBOXSTATUSTYPE; /* used by errmsgvboxcomdata.h */
64
65/** Used for raw-input and sorting. */
66typedef struct RTWINERRMSGINT1
67{
68 /** Pointer to the full message string. */
69 const char *pszMsgFull;
70 /** Pointer to the define string. */
71 const char *pszDefine;
72 /** Status code number. */
73 long iCode;
74 /** Set if duplicate. */
75 bool fDuplicate;
76} RTWINERRMSGINT1;
77typedef RTWINERRMSGINT1 *PRTWINERRMSGINT1;
78
79
80/** This is used when building the string table and printing it. */
81typedef struct RTWINERRMSGINT2
82{
83 /** The full message string. */
84 BLDPROGSTRING MsgFull;
85 /** The define string. */
86 BLDPROGSTRING Define;
87 /** Pointer to the define string. */
88 const char *pszDefine;
89 /** Status code number. */
90 long iCode;
91} RTWINERRMSGINT2;
92typedef RTWINERRMSGINT2 *PRTWINERRMSGINT2;
93
94
95/*********************************************************************************************************************************
96* Global Variables *
97*********************************************************************************************************************************/
98static const char *g_pszProgName = "errmsgwin-sorter";
99static RTWINERRMSGINT1 g_aStatusMsgs[] =
100{
101#if !defined(IPRT_NO_ERROR_DATA) && !defined(DOXYGEN_RUNNING)
102# include "errmsgwindata.h"
103# if defined(VBOX) && !defined(IN_GUEST)
104# include "errmsgvboxcomdata.h"
105# endif
106#endif
107 { "Success.", "ERROR_SUCCESS", 0, false },
108};
109
110
111static RTEXITCODE error(const char *pszFormat, ...)
112{
113 va_list va;
114 va_start(va, pszFormat);
115 fprintf(stderr, "%s: error: ", g_pszProgName);
116 vfprintf(stderr, pszFormat, va);
117 va_end(va);
118 return RTEXITCODE_FAILURE;
119}
120
121
122/** qsort callback. */
123static int CompareWinErrMsg(const void *pv1, const void *pv2) RT_NOTHROW_DEF
124{
125 PCRTWINERRMSG p1 = (PCRTWINERRMSG)pv1;
126 PCRTWINERRMSG p2 = (PCRTWINERRMSG)pv2;
127 int iDiff;
128 if (p1->iCode < p2->iCode)
129 iDiff = -1;
130 else if (p1->iCode > p2->iCode)
131 iDiff = 1;
132 else
133 iDiff = 0;
134 return iDiff;
135}
136
137
138int main(int argc, char **argv)
139{
140 /*
141 * Parse arguments.
142 */
143 enum { kMode_All, kMode_OnlyDefines } enmMode;
144 if (argc == 3 && strcmp(argv[1], "--all") == 0)
145 enmMode = kMode_All;
146 else if (argc == 3 && strcmp(argv[1], "--only-defines") == 0)
147 enmMode = kMode_OnlyDefines;
148 else
149 {
150 fprintf(stderr,
151 "syntax error!\n"
152 "Usage: %s <--all|--only-defines> <outfile>\n", argv[0]);
153 return RTEXITCODE_SYNTAX;
154 }
155 const char * const pszOutFile = argv[2];
156
157 /*
158 * Sort the table and mark duplicates.
159 */
160 qsort(g_aStatusMsgs, RT_ELEMENTS(g_aStatusMsgs), sizeof(g_aStatusMsgs[0]), CompareWinErrMsg);
161
162 int rcExit = RTEXITCODE_SUCCESS;
163 long iPrev = (long)0x80000000;
164 bool fHaveSuccess = false;
165 for (size_t i = 0; i < RT_ELEMENTS(g_aStatusMsgs); i++)
166 {
167 PRTWINERRMSGINT1 pMsg = &g_aStatusMsgs[i];
168 if (pMsg->iCode == iPrev && i != 0)
169 {
170 pMsg->fDuplicate = true;
171
172 if (iPrev == 0)
173 continue;
174
175 PRTWINERRMSGINT1 pPrev = &g_aStatusMsgs[i - 1];
176 if (strcmp(pMsg->pszDefine, pPrev->pszDefine) == 0)
177 continue;
178 rcExit = error("Duplicate value %#lx (%ld) - %s and %s\n",
179 (unsigned long)iPrev, iPrev, pMsg->pszDefine, pPrev->pszDefine);
180 }
181 else
182 {
183 pMsg->fDuplicate = false;
184 iPrev = pMsg->iCode;
185 if (iPrev == 0)
186 fHaveSuccess = true;
187 }
188 }
189 if (!fHaveSuccess)
190 rcExit = error("No zero / success value in the table!\n");
191
192 /*
193 * Create a string table for it all.
194 */
195 BLDPROGSTRTAB StrTab;
196 if (!BldProgStrTab_Init(&StrTab, RT_ELEMENTS(g_aStatusMsgs) * 3))
197 return error("Out of memory!\n");
198
199 static RTWINERRMSGINT2 s_aStatusMsgs2[RT_ELEMENTS(g_aStatusMsgs)];
200 size_t cStatusMsgs = 0;
201 for (size_t i = 0; i < RT_ELEMENTS(g_aStatusMsgs); i++)
202 {
203 if (!g_aStatusMsgs[i].fDuplicate)
204 {
205 s_aStatusMsgs2[cStatusMsgs].iCode = g_aStatusMsgs[i].iCode;
206 s_aStatusMsgs2[cStatusMsgs].pszDefine = g_aStatusMsgs[i].pszDefine;
207 BldProgStrTab_AddStringDup(&StrTab, &s_aStatusMsgs2[cStatusMsgs].Define, g_aStatusMsgs[i].pszDefine);
208 if (enmMode != kMode_OnlyDefines)
209 BldProgStrTab_AddStringDup(&StrTab, &s_aStatusMsgs2[cStatusMsgs].MsgFull, g_aStatusMsgs[i].pszMsgFull);
210 cStatusMsgs++;
211 }
212 }
213
214 if (!BldProgStrTab_CompileIt(&StrTab, true))
215 return error("BldProgStrTab_CompileIt failed!\n");
216
217 /*
218 * Prepare output file.
219 */
220 FILE *pOut = fopen(pszOutFile, "wt");
221 if (pOut)
222 {
223 /*
224 * Print the table.
225 */
226 fprintf(pOut,
227 "\n"
228 "typedef struct RTMSGWINENTRYINT\n"
229 "{\n"
230 " uint32_t offDefine : 20;\n"
231 " uint32_t cchDefine : 9;\n"
232 "%s"
233 " int32_t iCode;\n"
234 "} RTMSGWINENTRYINT;\n"
235 "typedef RTMSGWINENTRYINT *PCRTMSGWINENTRYINT;\n"
236 "\n"
237 "static const RTMSGWINENTRYINT g_aWinMsgs[ /*%lu*/ ] =\n"
238 "{\n"
239 ,
240 enmMode == kMode_All
241 ? " uint32_t offMsgFull : 23;\n"
242 " uint32_t cchMsgFull : 9;\n" : "",
243 (unsigned long)cStatusMsgs);
244
245 if (enmMode == kMode_All)
246 for (size_t i = 0; i < cStatusMsgs; i++)
247 fprintf(pOut, "/*%#010lx:*/ { %#08x, %3u, %#08x, %3u, %ld },\n",
248 s_aStatusMsgs2[i].iCode,
249 s_aStatusMsgs2[i].Define.offStrTab,
250 (unsigned)s_aStatusMsgs2[i].Define.cchString,
251 s_aStatusMsgs2[i].MsgFull.offStrTab,
252 (unsigned)s_aStatusMsgs2[i].MsgFull.cchString,
253 s_aStatusMsgs2[i].iCode);
254 else if (enmMode == kMode_OnlyDefines)
255 for (size_t i = 0; i < cStatusMsgs; i++)
256 fprintf(pOut, "/*%#010lx:*/ { %#08x, %3u, %ld },\n",
257 s_aStatusMsgs2[i].iCode,
258 s_aStatusMsgs2[i].Define.offStrTab,
259 (unsigned)s_aStatusMsgs2[i].Define.cchString,
260 s_aStatusMsgs2[i].iCode);
261 else
262 return error("Unsupported message selection (%d)!\n", enmMode);
263 fprintf(pOut,
264 "};\n"
265 "\n");
266
267 BldProgStrTab_WriteStringTable(&StrTab, pOut, "static ", "g_", "WinMsgStrTab");
268
269 /*
270 * Close the output file and we're done.
271 */
272 fflush(pOut);
273 if (ferror(pOut))
274 rcExit = error("Error writing '%s'!\n", pszOutFile);
275 if (fclose(pOut) != 0)
276 rcExit = error("Failed to close '%s' after writing it!\n", pszOutFile);
277 }
278 else
279 rcExit = error("Failed to open '%s' for writing!\n", pszOutFile);
280 return rcExit;
281}
282
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