Skip to content

Commit 4207992

Browse files
committed
[NETSH] Implement the -r (remote) option and set machine command
Also: - Start all usage texts with a newline. - Add some error messages. - Fix a bug in the command interpreter.
1 parent 4ad647f commit 4207992

File tree

6 files changed

+111
-34
lines changed

6 files changed

+111
-34
lines changed

base/applications/network/netsh/context.c

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ PCONTEXT_ENTRY pCurrentContext = NULL;
2929
PCONTEXT_STACK_ENTRY pContextStackHead = NULL;
3030
PCONTEXT_STACK_ENTRY pContextStackTail = NULL;
3131

32+
PWSTR pszMachine = NULL;
33+
3234
static BOOL bOnline = TRUE;
3335

3436
/* FUNCTIONS ******************************************************************/
@@ -707,6 +709,43 @@ PushdCommand(
707709
}
708710

709711

712+
DWORD
713+
WINAPI
714+
SetMachineCommand(
715+
_In_ LPCWSTR pwszMachine,
716+
_In_ LPWSTR *argv,
717+
_In_ DWORD dwCurrentIndex,
718+
_In_ DWORD dwArgCount,
719+
_In_ DWORD dwFlags,
720+
_In_ LPCVOID pvData,
721+
_Out_ BOOL *pbDone)
722+
{
723+
DWORD dwError = ERROR_SUCCESS;
724+
725+
DPRINT("SetMachineCommand(pwszMachine %S dwCurrentIndex %lu dwArgCount %lu)\n",
726+
pwszMachine, dwCurrentIndex, dwArgCount);
727+
728+
if ((dwArgCount - dwCurrentIndex) > 1)
729+
return ERROR_SHOW_USAGE;
730+
731+
if (pszMachine != NULL)
732+
{
733+
HeapFree(GetProcessHeap(), 0, pszMachine);
734+
pszMachine = NULL;
735+
}
736+
737+
if ((dwArgCount - dwCurrentIndex) == 1)
738+
{
739+
pszMachine = HeapAlloc(GetProcessHeap(), 0, (sizeof(argv[dwCurrentIndex]) + 1) * sizeof(WCHAR));
740+
if (pszMachine == NULL)
741+
return ERROR_NOT_ENOUGH_MEMORY;
742+
wcscpy(pszMachine, argv[dwCurrentIndex]);
743+
}
744+
745+
return dwError;
746+
}
747+
748+
710749
DWORD
711750
WINAPI
712751
SetModeCommand(
@@ -720,9 +759,10 @@ SetModeCommand(
720759
{
721760
DWORD dwError = ERROR_SUCCESS;
722761

723-
DPRINT("SetModeCommand()\n");
762+
DPRINT("SetModeCommand(pwszMachine %S dwCurrentIndex %lu dwArgCount %lu)\n",
763+
pwszMachine, dwCurrentIndex, dwArgCount);
724764

725-
if (dwArgCount != 3)
765+
if ((dwArgCount - dwCurrentIndex) != 1)
726766
return ERROR_SHOW_USAGE;
727767

728768
if (!_wcsicmp(argv[dwCurrentIndex], L"offline"))
@@ -771,7 +811,7 @@ CreateRootContext(VOID)
771811
if (pRootContext == NULL)
772812
return FALSE;
773813

774-
pRootContext->hModule = GetModuleHandle(NULL);
814+
pRootContext->hModule = hModule;
775815

776816
AddContextCommand(pRootContext, L"..", UpCommand, IDS_HLP_UP, IDS_HLP_UP_EX, 0);
777817
AddContextCommand(pRootContext, L"?", NULL, IDS_HLP_HELP, IDS_HLP_HELP_EX, 0);
@@ -803,7 +843,8 @@ CreateRootContext(VOID)
803843
pGroup = AddCommandGroup(pRootContext, L"set", IDS_HLP_GROUP_SET, 0);
804844
if (pGroup)
805845
{
806-
AddGroupCommand(pGroup, L"mode", SetModeCommand, IDS_HLP_SET_MODE, IDS_HLP_SET_MODE_EX, 0);
846+
AddGroupCommand(pGroup, L"machine", SetMachineCommand, IDS_HLP_SET_MACHINE, IDS_HLP_SET_MACHINE_EX, 0);
847+
AddGroupCommand(pGroup, L"mode", SetModeCommand, IDS_HLP_SET_MODE, IDS_HLP_SET_MODE_EX, 0);
807848
}
808849

809850
pGroup = AddCommandGroup(pRootContext, L"show", IDS_HLP_GROUP_SHOW, 0);

base/applications/network/netsh/interpreter.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ InterpretCommand(
205205
if (pCommand->pfnCmdHandler != NULL)
206206
{
207207
dwArgIndex++;
208-
dwError = pCommand->pfnCmdHandler(NULL, argv, dwArgIndex, dwArgCount, 0, NULL, bDone);
208+
dwError = pCommand->pfnCmdHandler(pszMachine, argv, dwArgIndex, dwArgCount, 0, NULL, bDone);
209209
if (dwError != ERROR_SUCCESS)
210210
{
211211
if (dwError == ERROR_SHOW_USAGE)
@@ -317,7 +317,8 @@ InterpretCommand(
317317
}
318318
}
319319

320-
return ERROR_CMD_NOT_FOUND;
320+
/* Done */
321+
return ERROR_SUCCESS;
321322
}
322323

323324

@@ -365,7 +366,7 @@ InterpretLine(
365366

366367
VOID
367368
PrintPrompt(
368-
PCONTEXT_ENTRY pContext)
369+
_In_ PCONTEXT_ENTRY pContext)
369370
{
370371
if (pContext != pRootContext)
371372
{
@@ -394,6 +395,8 @@ InterpretInteractive(VOID)
394395
memset(args_vector, 0, sizeof(args_vector));
395396

396397
/* Shown just before the input where the user places commands */
398+
if (pszMachine)
399+
ConPrintf(StdOut, L"[%s] ", pszMachine);
397400
PrintPrompt(pCurrentContext);
398401
ConPuts(StdOut, L">");
399402

@@ -423,7 +426,9 @@ InterpretInteractive(VOID)
423426
dwError = InterpretCommand(args_vector, dwArgCount, &bDone);
424427
if (dwError == ERROR_CMD_NOT_FOUND)
425428
{
426-
ConResPrintf(StdErr, IDS_INVALID_COMMAND, input_line);
429+
PWSTR pszCommandString = MergeStrings(args_vector, dwArgCount);
430+
ConResPrintf(StdErr, IDS_INVALID_COMMAND, pszCommandString);
431+
HeapFree(GetProcessHeap(), 0, pszCommandString);
427432
}
428433

429434
if (bDone)

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,47 +26,50 @@ 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 "Usage: %1!s!\n\n Goes up one context level.\n"
29+
IDS_HLP_UP_EX "\nUsage: %1!s!\n\n Goes up one context level.\n"
3030
IDS_HLP_EXIT "Exits the program.\n"
31-
IDS_HLP_EXIT_EX "Usage: %1!s!\n\n Exits the program.\n"
31+
IDS_HLP_EXIT_EX "\nUsage: %1!s!\n\n Exits the program.\n"
3232
IDS_HLP_HELP "Displays a list of commands.\n"
33-
IDS_HLP_HELP_EX "Usage: %1!s!\n\n Displays a list of commands.\n"
33+
IDS_HLP_HELP_EX "\nUsage: %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 "Usage: %1!s!\n\n Changes to the context on the stack.\n"
35+
IDS_HLP_POPD_EX "\nUsage: %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 "Usage: %1!s!\n\n Stores the current context on the stack.\n"
37+
IDS_HLP_PUSHD_EX "\nUsage: %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 "Usage: %1!s! <script file name>\n\n Loads the script file and runs it.\n"
39+
IDS_HLP_EXEC_EX "\nUsage: %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 "Usage: %1!s!\n\n Creates a script that contains the current configuration. If saved to a\n\
41+
IDS_HLP_DUMP_EX "\nUsage: %1!s!\n\n Creates a script that contains the current configuration. If saved to a\n\
4242
file, the script can be used to altered configuration settings.\n"
4343
IDS_HLP_OFFLINE "Sets the current mode to offline.\n"
44-
IDS_HLP_OFFLINE_EX "Usage: %1!s!\n\n Sets the current mode to offline. Changes made while in offline mode\n\
44+
IDS_HLP_OFFLINE_EX "\nUsage: %1!s!\n\n Sets the current mode to offline. Changes made while in offline mode\n\
4545
are saved and made in the running configuration when the mode is\n switched back to online.\n"
4646
IDS_HLP_ONLINE "Sets the current mode to online.\n"
47-
IDS_HLP_ONLINE_EX "Usage: %1!s!\n\n Sets the current mode to online. Changes made while in online mode are\n\
47+
IDS_HLP_ONLINE_EX "\nUsage: %1!s!\n\n Sets the current mode to online. Changes made while in online mode are\n\
4848
immediately reflected in the running configuration. If changes are\n made while in offline mode, they are reflected in the running\n\
4949
configuration when the mode is switched back to online.\n"
5050
IDS_HLP_ABORT "Discards changes made while in offline mode.\n"
51-
IDS_HLP_ABORT_EX "Usage: %1!s!\n\n Discards changes made while in the offline mode. This does not affect\n\
51+
IDS_HLP_ABORT_EX "\nUsage: %1!s!\n\n Discards changes made while in the offline mode. This does not affect\n\
5252
changes made while in online mode.\n"
5353
IDS_HLP_COMMIT "Commits changes made while in offline mode.\n"
54-
IDS_HLP_COMMIT_EX "Usage: %1!s!\n\n Commits changes to the running configuration that were made while in\n\
54+
IDS_HLP_COMMIT_EX "\nUsage: %1!s!\n\n Commits changes to the running configuration that were made while in\n\
5555
offline mode. No action is taken for this command while in online\n mode.\n"
5656

5757
IDS_HLP_ADD_HELPER "Installs a helper DLL.\n"
58-
IDS_HLP_ADD_HELPER_EX "Usage: %1!s! <dll file name>\n\n Installs the specified helper DLL in netsh.\n"
58+
IDS_HLP_ADD_HELPER_EX "\nUsage: %1!s! <dll file name>\n\n Installs the specified helper DLL in netsh.\n"
5959

6060
IDS_HLP_DEL_HELPER "Removes a helper DLL.\n"
61-
IDS_HLP_DEL_HELPER_EX "Usage: %1!s! <dll file name>\n\n Removes the specified helper DLL from netsh.\n"
61+
IDS_HLP_DEL_HELPER_EX "\nUsage: %1!s! <dll file name>\n\n Removes the specified helper DLL from netsh.\n"
6262

63+
IDS_HLP_SET_MACHINE "Sets the current machine on which to operate.\n"
64+
IDS_HLP_SET_MACHINE_EX "\nUsage: %1!s! [[name=]<string>]\n\n Sets the current machine on which to operate. If a machine name\n\
65+
is not specified, the local machine is used.\n"
6366
IDS_HLP_SET_MODE "Sets the current mode to online or offline.\n"
64-
IDS_HLP_SET_MODE_EX "Usage: %1!s! [mode=]{online|offline}\n\n Sets the current mode to online or offline.\n"
67+
IDS_HLP_SET_MODE_EX "\nUsage: %1!s! [mode=]{online|offline}\n\n Sets the current mode to online or offline.\n"
6568

6669
IDS_HLP_SHOW_HELPER "Lists all the top-level helpers.\n"
67-
IDS_HLP_SHOW_HELPER_EX "Usage: %1!s!\n\n Lists all the top-level helpers.\n"
70+
IDS_HLP_SHOW_HELPER_EX "\nUsage: %1!s!\n\n Lists all the top-level helpers.\n"
6871
IDS_HLP_SHOW_MODE "Shows the current mode.\n"
69-
IDS_HLP_SHOW_MODE_EX "Usage: %1!s!\n\n Shows the current mode (offline or online).\n"
72+
IDS_HLP_SHOW_MODE_EX "\nUsage: %1!s!\n\n Shows the current mode (offline or online).\n"
7073

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

base/applications/network/netsh/netsh.c

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

15+
/* GLOBALS ********************************************************************/
16+
17+
HMODULE hModule = NULL;
18+
1519
/* FUNCTIONS ******************************************************************/
1620

1721
DWORD
@@ -95,6 +99,8 @@ wmain(
9599

96100
DPRINT("wmain(%S)\n", GetCommandLineW());
97101

102+
hModule = GetModuleHandle(NULL);
103+
98104
/* Initialize the Console Standard Streams */
99105
ConInitStdStreams();
100106

@@ -171,8 +177,15 @@ wmain(
171177
if ((index + 1) < argc)
172178
{
173179
index++;
174-
ConPuts(StdOut, L"\nThe -r option is not implemented yet\n");
175-
// remote = argv[index];
180+
pszMachine = HeapAlloc(GetProcessHeap(), 0, (wcslen(argv[index]) + 1) * sizeof(WCHAR));
181+
if (pszMachine == NULL)
182+
{
183+
dwError = ERROR_NOT_ENOUGH_MEMORY;
184+
PrintError(hModule, dwError);
185+
goto done;
186+
}
187+
188+
wcscpy(pszMachine, argv[index]);
176189
}
177190
else
178191
{
@@ -192,8 +205,14 @@ wmain(
192205
else if (pszCommand == NULL)
193206
{
194207
pszCommand = MergeStrings((LPWSTR*)&argv[index], argc - index);
195-
if (pszCommand)
196-
break;
208+
if (pszCommand == NULL)
209+
{
210+
dwError = ERROR_NOT_ENOUGH_MEMORY;
211+
PrintError(hModule, dwError);
212+
goto done;
213+
}
214+
215+
break;
197216
}
198217
}
199218
}
@@ -222,8 +241,12 @@ wmain(
222241

223242
done:
224243
/* FIXME: Cleanup code goes here */
244+
if (pszMachine != NULL)
245+
HeapFree(GetProcessHeap(), 0, pszMachine);
246+
225247
if (pszCommand != NULL)
226248
HeapFree(GetProcessHeap(), 0, pszCommand);
249+
227250
CleanupContext();
228251
UnloadHelpers();
229252

base/applications/network/netsh/precomp.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ extern PCONTEXT_ENTRY pCurrentContext;
132132

133133
extern PHELPER_ENTRY pHelperListHead;
134134

135+
extern HMODULE hModule;
136+
extern PWSTR pszMachine;
137+
135138
/* PROTOTYPES *****************************************************************/
136139

137140
/* context.c */

base/applications/network/netsh/resource.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@
4848
#define IDS_HLP_ADD_HELPER_EX 341
4949
#define IDS_HLP_DEL_HELPER 342
5050
#define IDS_HLP_DEL_HELPER_EX 343
51-
#define IDS_HLP_SET_MODE 344
52-
#define IDS_HLP_SET_MODE_EX 345
53-
#define IDS_HLP_SHOW_HELPER 346
54-
#define IDS_HLP_SHOW_HELPER_EX 347
55-
#define IDS_HLP_SHOW_MODE 348
56-
#define IDS_HLP_SHOW_MODE_EX 349
51+
#define IDS_HLP_SET_MACHINE 344
52+
#define IDS_HLP_SET_MACHINE_EX 345
53+
#define IDS_HLP_SET_MODE 346
54+
#define IDS_HLP_SET_MODE_EX 347
55+
#define IDS_HLP_SHOW_HELPER 348
56+
#define IDS_HLP_SHOW_HELPER_EX 349
57+
#define IDS_HLP_SHOW_MODE 350
58+
#define IDS_HLP_SHOW_MODE_EX 351
5759

5860
#define IDS_HLP_GROUP_ADD 360
5961
#define IDS_HLP_GROUP_DELETE 361

0 commit comments

Comments
 (0)