Skip to content

Commit b37aafe

Browse files
committed
[NETSH] Sort the help command list alphabetically
1 parent 2a0acdb commit b37aafe

File tree

1 file changed

+124
-59
lines changed
  • base/applications/network/netsh

1 file changed

+124
-59
lines changed

base/applications/network/netsh/help.c

Lines changed: 124 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,27 @@
1212
#define NDEBUG
1313
#include <debug.h>
1414

15+
typedef enum
16+
{
17+
Command,
18+
Group,
19+
SubContext
20+
} HELP_TYPE, *PHELP_TYPE;
21+
22+
typedef struct
23+
{
24+
HELP_TYPE Type;
25+
PWSTR pszCommand;
26+
DWORD dwHelpId;
27+
union
28+
{
29+
PCOMMAND_ENTRY pCommand;
30+
PCOMMAND_GROUP pGroup;
31+
PCONTEXT_ENTRY pSubContext;
32+
} Pointer;
33+
} HELP_ENTRY, *PHELP_ENTRY;
34+
35+
1536
/* FUNCTIONS ******************************************************************/
1637

1738
static
@@ -53,61 +74,6 @@ PrintCurrentContextHeader(
5374
}
5475

5576

56-
static
57-
VOID
58-
PrintShortCommands(
59-
_In_ PCONTEXT_ENTRY pContext)
60-
{
61-
PCOMMAND_ENTRY pCommand;
62-
WCHAR szBuffer[80];
63-
64-
pCommand = pContext->pCommandListHead;
65-
while (pCommand != NULL)
66-
{
67-
if (LoadStringW(pContext->hModule, pCommand->dwShortCmdHelpToken, szBuffer, 80) == 0)
68-
szBuffer[0] = UNICODE_NULL;
69-
ConPrintf(StdOut, L"%-15s - %s", pCommand->pwszCmdToken, szBuffer);
70-
pCommand = pCommand->pNext;
71-
}
72-
}
73-
74-
75-
static
76-
VOID
77-
PrintShortGroups(
78-
_In_ PCONTEXT_ENTRY pContext)
79-
{
80-
PCOMMAND_GROUP pGroup;
81-
WCHAR szBuffer[80];
82-
83-
pGroup = pContext->pGroupListHead;
84-
while (pGroup != NULL)
85-
{
86-
if (LoadStringW(pContext->hModule, pGroup->dwShortCmdHelpToken, szBuffer, 80) == 0)
87-
szBuffer[0] = UNICODE_NULL;
88-
ConPrintf(StdOut, L"%-15s - %s", pGroup->pwszCmdGroupToken, szBuffer);
89-
pGroup = pGroup->pNext;
90-
}
91-
}
92-
93-
94-
static
95-
VOID
96-
PrintShortSubContexts(
97-
_In_ PCONTEXT_ENTRY pContext)
98-
{
99-
PCONTEXT_ENTRY pSubContext;
100-
WCHAR szBuffer[80];
101-
102-
pSubContext = pContext->pSubContextHead;
103-
while (pSubContext != NULL)
104-
{
105-
GetContextFullName(pSubContext, szBuffer, 80);
106-
ConPrintf(StdOut, L"%-15s - Changes to the \"%s\" context.\n", pSubContext->pszContextName, szBuffer);
107-
pSubContext = pSubContext->pNext;
108-
}
109-
}
110-
11177
static
11278
VOID
11379
PrintShortGroupCommands(
@@ -143,23 +109,122 @@ PrintLongCommand(
143109
}
144110

145111

112+
static
113+
int
114+
HelpCompare(
115+
_In_ const void *p1,
116+
_In_ const void *p2)
117+
{
118+
return _wcsicmp(((PHELP_ENTRY)p1)->pszCommand, ((PHELP_ENTRY)p2)->pszCommand);
119+
}
120+
121+
146122
static
147123
VOID
148124
PrintContext(
149125
_In_ PCONTEXT_ENTRY pContext)
150126
{
151-
DPRINT1("PrintContext()\n");
127+
PCOMMAND_ENTRY pCommand;
128+
PCOMMAND_GROUP pGroup;
129+
PCONTEXT_ENTRY pSubContext;
130+
PHELP_ENTRY pHelpArray = NULL;
131+
DWORD dwCount = 0, dwIndex;
132+
WCHAR szBuffer[80];
133+
134+
DPRINT("PrintContext()\n");
152135

153136
if (pContext != pRootContext)
154137
PrintContext(pContext->pParentContext);
155138

156139
PrintCurrentContextHeader(pContext);
157140

158-
PrintShortCommands(pContext);
141+
/* Count short commands */
142+
pCommand = pContext->pCommandListHead;
143+
while (pCommand != NULL)
144+
{
145+
dwCount++;
146+
pCommand = pCommand->pNext;
147+
}
148+
149+
/* Count short groups */
150+
pGroup = pContext->pGroupListHead;
151+
while (pGroup != NULL)
152+
{
153+
dwCount++;
154+
pGroup = pGroup->pNext;
155+
}
156+
157+
/* Count short subcontexts */
158+
pSubContext = pContext->pSubContextHead;
159+
while (pSubContext != NULL)
160+
{
161+
dwCount++;
162+
pSubContext = pSubContext->pNext;
163+
}
159164

160-
PrintShortGroups(pContext);
165+
pHelpArray = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCount * sizeof(HELP_ENTRY));
166+
if (pHelpArray == NULL)
167+
return;
168+
169+
dwIndex = 0;
170+
171+
/* Add short commands */
172+
pCommand = pContext->pCommandListHead;
173+
while (pCommand != NULL)
174+
{
175+
pHelpArray[dwIndex].Type = Command;
176+
pHelpArray[dwIndex].pszCommand = pCommand->pwszCmdToken;
177+
pHelpArray[dwIndex].dwHelpId = pCommand->dwShortCmdHelpToken;
178+
// pHelpArray[dwIndex].Pointer.pCommand = pCommand;
179+
dwIndex++;
180+
pCommand = pCommand->pNext;
181+
}
182+
183+
/* Add short groups */
184+
pGroup = pContext->pGroupListHead;
185+
while (pGroup != NULL)
186+
{
187+
pHelpArray[dwIndex].Type = Group;
188+
pHelpArray[dwIndex].pszCommand = pGroup->pwszCmdGroupToken;
189+
pHelpArray[dwIndex].dwHelpId = pGroup->dwShortCmdHelpToken;
190+
// pHelpArray[dwIndex].Pointer.pGroup = pGroup;
191+
dwIndex++;
192+
pGroup = pGroup->pNext;
193+
}
194+
195+
/* Count short subcontexts */
196+
pSubContext = pContext->pSubContextHead;
197+
while (pSubContext != NULL)
198+
{
199+
pHelpArray[dwIndex].Type = SubContext;
200+
pHelpArray[dwIndex].pszCommand = pSubContext->pszContextName;
201+
pHelpArray[dwIndex].Pointer.pSubContext = pSubContext;
202+
dwIndex++;
203+
pSubContext = pSubContext->pNext;
204+
}
205+
206+
qsort(pHelpArray, dwCount, sizeof(HELP_ENTRY), HelpCompare);
207+
208+
for (dwIndex = 0; dwIndex < dwCount; dwIndex++)
209+
{
210+
switch (pHelpArray[dwIndex].Type)
211+
{
212+
case Command:
213+
case Group:
214+
if (LoadStringW(pContext->hModule, pHelpArray[dwIndex].dwHelpId, szBuffer, 80) == 0)
215+
szBuffer[0] = UNICODE_NULL;
216+
ConPrintf(StdOut, L"%-15s - %s", pHelpArray[dwIndex].pszCommand, szBuffer);
217+
break;
218+
219+
case SubContext:
220+
GetContextFullName(pHelpArray[dwIndex].Pointer.pSubContext, szBuffer, 80);
221+
ConPrintf(StdOut, L"%-15s - Changes to the \"%s\" context.\n", pHelpArray[dwIndex].pszCommand, szBuffer);
222+
break;
223+
}
224+
}
161225

162-
PrintShortSubContexts(pContext);
226+
if (pHelpArray)
227+
HeapFree(GetProcessHeap(), 0, pHelpArray);
163228
}
164229

165230

0 commit comments

Comments
 (0)