From 146624ed9b7e5cc7920ce7a7af4db99cc9bd3163 Mon Sep 17 00:00:00 2001 From: Eric Kohl Date: Tue, 7 Oct 2025 16:42:22 +0200 Subject: [PATCH] [NETSH] Implement the exec command and fix script execution --- base/applications/network/netsh/context.c | 25 +++++++++++++++++-- base/applications/network/netsh/interpreter.c | 5 ++-- base/applications/network/netsh/lang/en-US.rc | 4 ++- base/applications/network/netsh/netsh.c | 8 +++--- base/applications/network/netsh/precomp.h | 6 +++++ base/applications/network/netsh/resource.h | 20 ++++++++------- 6 files changed, 49 insertions(+), 19 deletions(-) diff --git a/base/applications/network/netsh/context.c b/base/applications/network/netsh/context.c index 55585fb6baa..05d70a3ae4c 100644 --- a/base/applications/network/netsh/context.c +++ b/base/applications/network/netsh/context.c @@ -325,6 +325,26 @@ UpCommand( } +DWORD +WINAPI +ExecCommand( + LPCWSTR pwszMachine, + LPWSTR *argv, + DWORD dwCurrentIndex, + DWORD dwArgCount, + DWORD dwFlags, + LPCVOID pvData, + BOOL *pbDone) +{ + DPRINT("ExecCommand()\n"); + + if (dwArgCount - dwCurrentIndex != 1) + return ERROR_INVALID_SYNTAX; + + return RunScript(argv[dwCurrentIndex]); +} + + DWORD WINAPI ExitCommand( @@ -372,7 +392,7 @@ PopdCommand( DPRINT("PopdCommand()\n"); if (pContextStackHead == NULL) - return 0; + return ERROR_SUCCESS; pEntry = pContextStackHead; @@ -412,7 +432,7 @@ PushdCommand( pEntry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CONTEXT_STACK_ENTRY)); if (pEntry == NULL) - return 1; + return ERROR_NOT_ENOUGH_MEMORY; pEntry->pContext = pCurrentContext; if (pContextStackHead == NULL) @@ -446,6 +466,7 @@ CreateRootContext(VOID) AddContextCommand(pRootContext, L"..", UpCommand, IDS_HLP_UP, IDS_HLP_UP_EX, 0); AddContextCommand(pRootContext, L"?", NULL, IDS_HLP_HELP, IDS_HLP_HELP_EX, 0); AddContextCommand(pRootContext, L"bye", ExitCommand, IDS_HLP_EXIT, IDS_HLP_EXIT_EX, 0); + AddContextCommand(pRootContext, L"exec", ExecCommand, IDS_HLP_EXEC, IDS_HLP_EXEC_EX, 0); AddContextCommand(pRootContext, L"exit", ExitCommand, IDS_HLP_EXIT, IDS_HLP_EXIT_EX, 0); AddContextCommand(pRootContext, L"help", NULL, IDS_HLP_HELP, IDS_HLP_HELP_EX, 0); AddContextCommand(pRootContext, L"popd", PopdCommand, IDS_HLP_POPD, IDS_HLP_POPD_EX, 0); diff --git a/base/applications/network/netsh/interpreter.c b/base/applications/network/netsh/interpreter.c index 2c82f3796e2..b6344af584d 100644 --- a/base/applications/network/netsh/interpreter.c +++ b/base/applications/network/netsh/interpreter.c @@ -208,8 +208,7 @@ InterpretCommand( dwError = pCommand->pfnCmdHandler(NULL, argv, dwArgIndex, dwArgCount, 0, NULL, bDone); if (dwError != ERROR_SUCCESS) { - ConPrintf(StdOut, L"Error: %lu\n\n", dwError); - ConResPrintf(StdOut, pCommand->dwCmdHlpToken); + ConPrintf(StdOut, L"Commnand: %S Error: %lu\n\n", pCommand->pwszCmdToken, dwError); } } @@ -347,7 +346,7 @@ InterpretScript( ptr++; } - return InterpretCommand(args_vector, dwArgCount, &bDone) == FALSE; + return InterpretCommand(args_vector, dwArgCount, &bDone); } diff --git a/base/applications/network/netsh/lang/en-US.rc b/base/applications/network/netsh/lang/en-US.rc index 2edcf4400e0..7abb2121707 100644 --- a/base/applications/network/netsh/lang/en-US.rc +++ b/base/applications/network/netsh/lang/en-US.rc @@ -22,7 +22,7 @@ END STRINGTABLE BEGIN IDS_HELP_HEADER "\nThe following commands are available:\n" - IDS_HELP_FOOTER "\nHelp Footer\n\n" + IDS_HELP_FOOTER "\nTo view help for a command, type the command, followed by a space, and then\n type ?.\n\n" IDS_SUBCONTEXT_HEADER "\nThe following sub-contexts are available:\n" IDS_HLP_UP "Goes up one context level.\n" @@ -35,6 +35,8 @@ BEGIN IDS_HLP_POPD_EX "Syntax: popd\n\n Changes to the context on the stack.\n\n" IDS_HLP_PUSHD "Stores the current context on the stack.\n" IDS_HLP_PUSHD_EX "Syntax: pushd\n\n Stores the current context on the stack.\n\n" + IDS_HLP_EXEC "Runs a script file.\n" + IDS_HLP_EXEC_EX "Syntax: exec \n\n Loads the script file and runs it.\n\n" IDS_HLP_ADD_HELPER "Installs a helper DLL.\n" IDS_HLP_ADD_HELPER_EX "Syntax: add helper \n\n Installs the specified helper DLL in netsh.\n\n" diff --git a/base/applications/network/netsh/netsh.c b/base/applications/network/netsh/netsh.c index 067fe0af390..b53b266bd4f 100644 --- a/base/applications/network/netsh/netsh.c +++ b/base/applications/network/netsh/netsh.c @@ -14,7 +14,7 @@ /* FUNCTIONS ******************************************************************/ -BOOL +DWORD RunScript( _In_ LPCWSTR filename) { @@ -26,7 +26,7 @@ RunScript( if (script == NULL) { ConResPrintf(StdErr, IDS_OPEN_FAILED, filename); - return FALSE; + return ERROR_FILE_NOT_FOUND; } /* Read and process the script */ @@ -35,14 +35,14 @@ RunScript( if (InterpretScript(tmp_string) == FALSE) { fclose(script); - return FALSE; + return ERROR_SUCCESS; /* FIXME */ } } /* Close the file */ fclose(script); - return TRUE; + return ERROR_SUCCESS; } /* diff --git a/base/applications/network/netsh/precomp.h b/base/applications/network/netsh/precomp.h index 453f2dad52b..03387c366d4 100644 --- a/base/applications/network/netsh/precomp.h +++ b/base/applications/network/netsh/precomp.h @@ -220,4 +220,10 @@ InterpretCommand( VOID InterpretInteractive(VOID); +/* netsh.c */ + +DWORD +RunScript( + _In_ LPCWSTR filename); + #endif /* PRECOMP_H */ diff --git a/base/applications/network/netsh/resource.h b/base/applications/network/netsh/resource.h index 02b7d493961..4fa7a0b1c45 100644 --- a/base/applications/network/netsh/resource.h +++ b/base/applications/network/netsh/resource.h @@ -31,14 +31,16 @@ #define IDS_HLP_POPD_EX 307 #define IDS_HLP_PUSHD 308 #define IDS_HLP_PUSHD_EX 309 +#define IDS_HLP_EXEC 310 +#define IDS_HLP_EXEC_EX 311 -#define IDS_HLP_ADD_HELPER 310 -#define IDS_HLP_ADD_HELPER_EX 311 -#define IDS_HLP_DEL_HELPER 312 -#define IDS_HLP_DEL_HELPER_EX 313 -#define IDS_HLP_SHOW_HELPER 314 -#define IDS_HLP_SHOW_HELPER_EX 315 +#define IDS_HLP_ADD_HELPER 320 +#define IDS_HLP_ADD_HELPER_EX 321 +#define IDS_HLP_DEL_HELPER 322 +#define IDS_HLP_DEL_HELPER_EX 323 +#define IDS_HLP_SHOW_HELPER 324 +#define IDS_HLP_SHOW_HELPER_EX 325 -#define IDS_HLP_GROUP_ADD 320 -#define IDS_HLP_GROUP_DELETE 321 -#define IDS_HLP_GROUP_SHOW 322 +#define IDS_HLP_GROUP_ADD 330 +#define IDS_HLP_GROUP_DELETE 331 +#define IDS_HLP_GROUP_SHOW 332