Skip to content

Commit 78a7372

Browse files
committed
[NETSH] Fix help output
- Increase the output buffer for context and group help texts. - Use a heap buffers to load and format the command help texts. - Pass the full command name to FormatMessage to make help output work with WinXP helpers. - Adjust the internal help texts accordingly.
1 parent d1cbb29 commit 78a7372

File tree

4 files changed

+78
-27
lines changed

4 files changed

+78
-27
lines changed

base/applications/network/netsh/help.c

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

15+
#define HUGE_HELP_BUFFER_SIZE 2048
16+
#define SMALL_HELP_BUFFER_SIZE 160
17+
#define TINY_HELP_BUFFER_SIZE 80
18+
1519
typedef enum
1620
{
1721
Command,
@@ -60,15 +64,15 @@ VOID
6064
PrintCurrentContextHeader(
6165
_In_ PCONTEXT_ENTRY pContext)
6266
{
63-
WCHAR szBuffer[80];
67+
WCHAR szBuffer[SMALL_HELP_BUFFER_SIZE];
6468

6569
if (pContext == pCurrentContext)
6670
{
6771
ConResPrintf(StdOut, IDS_THIS_COMMANDS);
6872
}
6973
else
7074
{
71-
GetContextFullName(pContext, szBuffer, 80);
75+
GetContextFullName(pContext, szBuffer, SMALL_HELP_BUFFER_SIZE);
7276
ConResPrintf(StdOut, IDS_CONTEXT_COMMANDS, szBuffer);
7377
}
7478
}
@@ -81,14 +85,14 @@ PrintShortGroupCommands(
8185
_In_ PCOMMAND_GROUP pGroup)
8286
{
8387
PCOMMAND_ENTRY pCommand;
84-
WCHAR szBuffer1[64];
85-
WCHAR szBuffer2[80];
88+
WCHAR szBuffer1[TINY_HELP_BUFFER_SIZE];
89+
WCHAR szBuffer2[SMALL_HELP_BUFFER_SIZE];
8690

8791
pCommand = pGroup->pCommandListHead;
8892
while (pCommand != NULL)
8993
{
9094
swprintf(szBuffer1, L"%s %s", pGroup->pwszCmdGroupToken, pCommand->pwszCmdToken);
91-
LoadStringW(pContext->hModule, pCommand->dwShortCmdHelpToken, szBuffer2, 80);
95+
LoadStringW(pContext->hModule, pCommand->dwShortCmdHelpToken, szBuffer2, SMALL_HELP_BUFFER_SIZE);
9296

9397
ConPrintf(StdOut, L"%-15s - %s", szBuffer1, szBuffer2);
9498
pCommand = pCommand->pNext;
@@ -116,7 +120,7 @@ PrintContext(
116120
PCONTEXT_ENTRY pSubContext;
117121
PHELP_ENTRY pHelpArray = NULL;
118122
DWORD dwCount = 0, dwIndex;
119-
WCHAR szBuffer[80];
123+
WCHAR szBuffer[SMALL_HELP_BUFFER_SIZE];
120124

121125
DPRINT("PrintContext()\n");
122126

@@ -198,13 +202,13 @@ PrintContext(
198202
{
199203
case Command:
200204
case Group:
201-
if (LoadStringW(pContext->hModule, pHelpArray[dwIndex].dwHelpId, szBuffer, 80) == 0)
205+
if (LoadStringW(pContext->hModule, pHelpArray[dwIndex].dwHelpId, szBuffer, SMALL_HELP_BUFFER_SIZE) == 0)
202206
szBuffer[0] = UNICODE_NULL;
203207
ConPrintf(StdOut, L"%-15s - %s", pHelpArray[dwIndex].pszCommand, szBuffer);
204208
break;
205209

206210
case SubContext:
207-
GetContextFullName(pHelpArray[dwIndex].Pointer.pSubContext, szBuffer, 80);
211+
GetContextFullName(pHelpArray[dwIndex].Pointer.pSubContext, szBuffer, SMALL_HELP_BUFFER_SIZE);
208212
ConPrintf(StdOut, L"%-15s - Changes to the \"%s\" context.\n", pHelpArray[dwIndex].pszCommand, szBuffer);
209213
break;
210214
}
@@ -273,12 +277,58 @@ PrintSubcontexts(
273277
VOID
274278
PrintCommandHelp(
275279
_In_ PCONTEXT_ENTRY pContext,
280+
_In_ PCOMMAND_GROUP pGroup,
276281
_In_ PCOMMAND_ENTRY pCommand)
277282
{
278-
WCHAR szBuffer[80];
283+
LPWSTR pszInBuffer = NULL, pszOutBuffer = NULL, pszCommandBuffer = NULL;
284+
DWORD_PTR Args[2];
285+
286+
DPRINT("PrintCommandHelp(%p %p %p)\n", pContext, pGroup, pCommand);
287+
288+
pszInBuffer = HeapAlloc(GetProcessHeap(), 0, HUGE_HELP_BUFFER_SIZE * sizeof(WCHAR));
289+
if (pszInBuffer == NULL)
290+
goto done;
291+
292+
pszOutBuffer = HeapAlloc(GetProcessHeap(), 0, HUGE_HELP_BUFFER_SIZE * sizeof(WCHAR));
293+
if (pszOutBuffer == NULL)
294+
goto done;
295+
296+
pszCommandBuffer = HeapAlloc(GetProcessHeap(), 0, TINY_HELP_BUFFER_SIZE * sizeof(WCHAR));
297+
if (pszCommandBuffer == NULL)
298+
goto done;
299+
300+
wcscpy(pszCommandBuffer, pCommand->pwszCmdToken);
301+
if (pGroup)
302+
{
303+
wcscat(pszCommandBuffer, L" ");
304+
wcscat(pszCommandBuffer, pGroup->pwszCmdGroupToken);
305+
}
306+
307+
LoadStringW(pContext->hModule, pCommand->dwCmdHlpToken, pszInBuffer, HUGE_HELP_BUFFER_SIZE);
308+
309+
Args[0] = (DWORD_PTR)pszCommandBuffer;
310+
Args[1] = (DWORD_PTR)NULL;
311+
312+
FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY,
313+
pszInBuffer,
314+
0,
315+
0,
316+
pszOutBuffer,
317+
HUGE_HELP_BUFFER_SIZE,
318+
(va_list *)&Args);
319+
320+
ConPuts(StdOut, pszOutBuffer);
321+
ConPuts(StdOut, L"\n");
322+
323+
done:
324+
if (pszCommandBuffer)
325+
HeapFree(GetProcessHeap(), 0, pszCommandBuffer);
326+
327+
if (pszOutBuffer)
328+
HeapFree(GetProcessHeap(), 0, pszOutBuffer);
279329

280-
LoadStringW(pContext->hModule, pCommand->dwCmdHlpToken, szBuffer, 80);
281-
ConPrintf(StdOut, szBuffer);
330+
if (pszInBuffer)
331+
HeapFree(GetProcessHeap(), 0, pszInBuffer);
282332
}
283333

284334

base/applications/network/netsh/interpreter.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ InterpretCommand(
136136
_Inout_ PBOOL bDone)
137137
{
138138
PCONTEXT_ENTRY pTempContext, pTempSubContext = NULL;
139-
PCOMMAND_GROUP pGroup;
140-
PCOMMAND_ENTRY pCommand;
139+
PCOMMAND_GROUP pGroup = NULL;
140+
PCOMMAND_ENTRY pCommand = NULL;
141141
INTERPRETER_STATE State = STATE_ANALYZE;
142142
DWORD dwArgIndex = 0;
143143
DWORD dwError = ERROR_SUCCESS;
@@ -197,7 +197,7 @@ InterpretCommand(
197197
if (((dwArgIndex + 1) == (dwArgCount - 1)) &&
198198
((_wcsicmp(argv[dwArgIndex + 1], L"?") == 0) || (_wcsicmp(argv[dwArgIndex + 1], L"help") == 0)))
199199
{
200-
PrintCommandHelp(pTempContext, pCommand);
200+
PrintCommandHelp(pTempContext, pGroup, pCommand);
201201
State = STATE_DONE;
202202
break;
203203
}
@@ -209,9 +209,9 @@ InterpretCommand(
209209
if (dwError != ERROR_SUCCESS)
210210
{
211211
if (dwError == ERROR_SHOW_USAGE)
212-
PrintCommandHelp(pTempContext, pCommand);
212+
PrintCommandHelp(pTempContext, pGroup, pCommand);
213213
else
214-
ConPrintf(StdOut, L"Commnand: %S Error: %lu\n\n", pCommand->pwszCmdToken, dwError);
214+
ConPrintf(StdOut, L"Command: %s Error: %lu\n\n", pCommand->pwszCmdToken, dwError);
215215
}
216216
}
217217

base/applications/network/netsh/lang/en-US.rc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,29 @@ BEGIN
2626
IDS_SUBCONTEXT_HEADER "\nThe following sub-contexts are available:\n"
2727

2828
IDS_HLP_UP "Goes up one context level.\n"
29-
IDS_HLP_UP_EX "Syntax: ..\n\n Goes up one context level.\n\n"
29+
IDS_HLP_UP_EX "Usage: %1!s!\n\n Goes up one context level.\n"
3030
IDS_HLP_EXIT "Exits the program.\n"
31-
IDS_HLP_EXIT_EX "Syntax: exit\n\n Exits the program.\n\n"
31+
IDS_HLP_EXIT_EX "Usage: %1!s!\n\n Exits the program.\n"
3232
IDS_HLP_HELP "Displays a list of commands.\n"
33-
IDS_HLP_HELP_EX "Syntax: help\n\n Displays a list of commands.\n\n"
33+
IDS_HLP_HELP_EX "Usage: %1!s!\n\n Displays a list of commands.\n"
3434
IDS_HLP_POPD "Changes to the context on the stack.\n"
35-
IDS_HLP_POPD_EX "Syntax: popd\n\n Changes to the context on the stack.\n\n"
35+
IDS_HLP_POPD_EX "Usage: %1!s!\n\n Changes to the context on the stack.\n"
3636
IDS_HLP_PUSHD "Stores the current context on the stack.\n"
37-
IDS_HLP_PUSHD_EX "Syntax: pushd\n\n Stores the current context on the stack.\n\n"
37+
IDS_HLP_PUSHD_EX "Usage: %1!s!\n\n Stores the current context on the stack.\n"
3838
IDS_HLP_EXEC "Runs a script file.\n"
39-
IDS_HLP_EXEC_EX "Syntax: exec <ScriptFile>\n\n Loads the script file and runs it.\n\n"
39+
IDS_HLP_EXEC_EX "Usage: %1!s! <script file name>\n\n Loads the script file and runs it.\n"
4040
IDS_HLP_DUMP "Displays a configuration script.\n"
41-
IDS_HLP_DUMP_EX "Syntax: dump\n\n Creates a script that contains the current configuration. If saved to a\n\
42-
file, the script can be used to altered configuration settings.\n\n"
41+
IDS_HLP_DUMP_EX "Syntax: %1!s!\n\n Creates a script that contains the current configuration. If saved to a\n\
42+
file, the script can be used to altered configuration settings.\n"
4343

4444
IDS_HLP_ADD_HELPER "Installs a helper DLL.\n"
45-
IDS_HLP_ADD_HELPER_EX "Syntax: add helper <dll file name>\n\n Installs the specified helper DLL in netsh.\n\n"
45+
IDS_HLP_ADD_HELPER_EX "Usage: %1!s! <dll file name>\n\n Installs the specified helper DLL in netsh.\n"
4646

4747
IDS_HLP_DEL_HELPER "Removes a helper DLL.\n"
48-
IDS_HLP_DEL_HELPER_EX "Syntax: delete helper <dll file name>\n\n Removes the specified helper DLL from netsh.\n\n"
48+
IDS_HLP_DEL_HELPER_EX "Usage: %1!s! <dll file name>\n\n Removes the specified helper DLL from netsh.\n"
4949

5050
IDS_HLP_SHOW_HELPER "Lists all the top-level helpers.\n"
51-
IDS_HLP_SHOW_HELPER_EX "Syntax: show helper\n\n Lists all the top-level helpers.\n\n"
51+
IDS_HLP_SHOW_HELPER_EX "Usage: %1!s!\n\n Lists all the top-level helpers.\n"
5252

5353
IDS_HLP_GROUP_ADD "Adds a configuration entry to a list of entries.\n"
5454
IDS_HLP_GROUP_DELETE "Deletes a configuration entry from a list of entries.\n"

base/applications/network/netsh/precomp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ FindContextByGuid(
150150
VOID
151151
PrintCommandHelp(
152152
_In_ PCONTEXT_ENTRY pContext,
153+
_In_ PCOMMAND_GROUP pGroup,
153154
_In_ PCOMMAND_ENTRY pCommand);
154155

155156
VOID

0 commit comments

Comments
 (0)