[NETSH] Improvements to the command line evaluation and the command interpreter

- Interpreter functions return error code instead of boolean
This commit is contained in:
Eric Kohl
2025-11-02 13:50:31 +01:00
parent de5f284ae3
commit 4ad647fb5c
3 changed files with 105 additions and 70 deletions

View File

@@ -129,7 +129,8 @@ GetContextSubContext(
}
BOOL
static
DWORD
InterpretCommand(
_In_ LPWSTR *argv,
_In_ DWORD dwArgCount,
@@ -141,11 +142,10 @@ InterpretCommand(
INTERPRETER_STATE State = STATE_ANALYZE;
DWORD dwArgIndex = 0;
DWORD dwError = ERROR_SUCCESS;
BOOL bFound = TRUE;
/* If no args provided */
if (dwArgCount == 0)
return TRUE;
return ERROR_SUCCESS;
if (pCurrentContext == NULL)
pCurrentContext = pRootContext;
@@ -155,7 +155,7 @@ InterpretCommand(
((_wcsicmp(argv[0], L"?") == 0) || (_wcsicmp(argv[0], L"help") == 0)))
{
PrintContextHelp(pCurrentContext);
return TRUE;
return ERROR_SUCCESS;
}
pTempContext = pCurrentContext;
@@ -256,7 +256,7 @@ InterpretCommand(
break;
}
bFound = FALSE;
dwError = ERROR_CMD_NOT_FOUND;
State = STATE_DONE;
break;
@@ -266,7 +266,7 @@ InterpretCommand(
if (pTempSubContext == pCurrentContext)
{
if (dwArgIndex != (dwArgCount - 1))
bFound = FALSE;
dwError = ERROR_CMD_NOT_FOUND;
State = STATE_DONE;
break;
@@ -300,7 +300,7 @@ InterpretCommand(
if (pTempContext->pParentContext == NULL)
{
bFound = FALSE;
dwError = ERROR_CMD_NOT_FOUND;
State = STATE_DONE;
break;
}
@@ -313,11 +313,11 @@ InterpretCommand(
case STATE_DONE:
DPRINT("STATE_DONE\n");
return bFound;
return dwError;
}
}
return TRUE;
return ERROR_CMD_NOT_FOUND;
}
@@ -325,7 +325,7 @@ InterpretCommand(
* InterpretScript(char *line):
* The main function used for when reading commands from scripts.
*/
BOOL
DWORD
InterpretLine(
_In_ LPWSTR pszInputLine)
{
@@ -386,6 +386,7 @@ InterpretInteractive(VOID)
BOOL bWhiteSpace = TRUE;
BOOL bDone = FALSE;
LPWSTR ptr;
DWORD dwError = ERROR_SUCCESS;
for (;;)
{
@@ -419,7 +420,8 @@ InterpretInteractive(VOID)
ptr++;
}
if (InterpretCommand(args_vector, dwArgCount, &bDone) == FALSE)
dwError = InterpretCommand(args_vector, dwArgCount, &bDone);
if (dwError == ERROR_CMD_NOT_FOUND)
{
ConResPrintf(StdErr, IDS_INVALID_COMMAND, input_line);
}

View File

@@ -18,8 +18,9 @@ DWORD
RunScript(
_In_ LPCWSTR filename)
{
FILE *script;
WCHAR tmp_string[MAX_STRING_SIZE];
FILE *script;
DWORD dwError = ERROR_SUCCESS;
/* Open the file for processing */
script = _wfopen(filename, L"r");
@@ -32,19 +33,51 @@ RunScript(
/* Read and process the script */
while (fgetws(tmp_string, MAX_STRING_SIZE, script) != NULL)
{
if (InterpretLine(tmp_string) == FALSE)
{
fclose(script);
return ERROR_SUCCESS; /* FIXME */
}
dwError = InterpretLine(tmp_string);
if (dwError != ERROR_SUCCESS)
break;
}
/* Close the file */
fclose(script);
return ERROR_SUCCESS;
return dwError;
}
LPWSTR
MergeStrings(
_In_ LPWSTR pszStringArray[],
_In_ INT nCount)
{
LPWSTR pszOutString = NULL;
INT i, nLength;
if ((pszStringArray == NULL) || (nCount == 0))
return NULL;
nLength = 0;
for (i = 0; i < nCount; i++)
nLength += wcslen(pszStringArray[i]);
if (nLength > 0)
nLength += nCount; /* Space characters and terminating zero */
pszOutString = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nLength * sizeof(WCHAR));
if (pszOutString == NULL)
return NULL;
for (i = 0; i < nCount; i++)
{
if (i != 0)
wcscat(pszOutString, L" ");
wcscat(pszOutString, pszStringArray[i]);
}
return pszOutString;
}
/*
* wmain():
* Main entry point of the application.
@@ -54,12 +87,11 @@ wmain(
_In_ int argc,
_In_ const LPWSTR argv[])
{
LPCWSTR tmpBuffer = NULL;
LPCWSTR pszFileName = NULL;
LPCWSTR pszContext = NULL;
LPWSTR pszCommand = NULL;
int index;
int result = EXIT_SUCCESS;
BOOL bDone = FALSE;
DWORD dwError = ERROR_SUCCESS;
DPRINT("wmain(%S)\n", GetCommandLineW());
@@ -74,34 +106,17 @@ wmain(
/* Process the command arguments */
for (index = 1; index < argc; index++)
{
if ((argv[index][0] == '/')||
(argv[index][0] == '-'))
{
tmpBuffer = argv[index] + 1;
}
else
{
if (pszFileName != NULL)
{
ConResPuts(StdOut, IDS_APP_USAGE);
result = EXIT_FAILURE;
goto done;
}
/* Run a command from the command line */
if (InterpretCommand((LPWSTR*)&argv[index], argc - index, &bDone) == FALSE)
result = EXIT_FAILURE;
goto done;
}
if (_wcsicmp(tmpBuffer, L"?") == 0)
if ((_wcsicmp(argv[index], L"-?") == 0) ||
(_wcsicmp(argv[index], L"/?") == 0) ||
(_wcsicmp(argv[index], L"?") == 0))
{
/* Help option */
ConResPuts(StdOut, IDS_APP_USAGE);
result = EXIT_SUCCESS;
dwError = ERROR_SUCCESS;
goto done;
}
else if (_wcsicmp(tmpBuffer, L"a") == 0)
else if ((_wcsicmp(argv[index], L"-a") == 0) ||
(_wcsicmp(argv[index], L"/a") == 0))
{
/* Aliasfile option */
if ((index + 1) < argc)
@@ -113,10 +128,12 @@ wmain(
else
{
ConResPuts(StdOut, IDS_APP_USAGE);
result = EXIT_FAILURE;
dwError = ERROR_INVALID_SYNTAX;
goto done;
}
}
else if (_wcsicmp(tmpBuffer, L"c") == 0)
else if ((_wcsicmp(argv[index], L"-c") == 0) ||
(_wcsicmp(argv[index], L"/c") == 0))
{
/* Context option */
if ((index + 1) < argc)
@@ -127,10 +144,12 @@ wmain(
else
{
ConResPuts(StdOut, IDS_APP_USAGE);
result = EXIT_FAILURE;
dwError = ERROR_INVALID_SYNTAX;
goto done;
}
}
else if (_wcsicmp(tmpBuffer, L"f") == 0)
else if ((_wcsicmp(argv[index], L"-f") == 0) ||
(_wcsicmp(argv[index], L"/f") == 0))
{
/* File option */
if ((index + 1) < argc)
@@ -141,10 +160,12 @@ wmain(
else
{
ConResPuts(StdOut, IDS_APP_USAGE);
result = EXIT_FAILURE;
dwError = ERROR_INVALID_SYNTAX;
goto done;
}
}
else if (_wcsicmp(tmpBuffer, L"r") == 0)
else if ((_wcsicmp(argv[index], L"-r") == 0) ||
(_wcsicmp(argv[index], L"/r") == 0))
{
/* Remote option */
if ((index + 1) < argc)
@@ -156,33 +177,43 @@ wmain(
else
{
ConResPuts(StdOut, IDS_APP_USAGE);
result = EXIT_FAILURE;
dwError = ERROR_INVALID_SYNTAX;
goto done;
}
}
else
{
/* Invalid command */
ConResPrintf(StdOut, IDS_INVALID_COMMAND, argv[index]);
result = EXIT_FAILURE;
goto done;
if (pszFileName != NULL)
{
ConResPuts(StdOut, IDS_APP_USAGE);
dwError = ERROR_INVALID_SYNTAX;
goto done;
}
else if (pszCommand == NULL)
{
pszCommand = MergeStrings((LPWSTR*)&argv[index], argc - index);
if (pszCommand)
break;
}
}
}
/* Set a context */
if (pszContext)
{
if (InterpretLine((LPWSTR)pszContext) == FALSE)
{
result = EXIT_FAILURE;
dwError = InterpretLine((LPWSTR)pszContext);
if (dwError != ERROR_SUCCESS)
goto done;
}
}
/* Run a script or the interactive interpeter */
/* Run a script, the command line instruction or the interactive interpeter */
if (pszFileName != NULL)
{
if (RunScript(pszFileName) == FALSE)
result = EXIT_FAILURE;
dwError = RunScript(pszFileName);
}
else if (pszCommand != NULL)
{
dwError = InterpretLine(pszCommand);
}
else
{
@@ -191,10 +222,12 @@ wmain(
done:
/* FIXME: Cleanup code goes here */
if (pszCommand != NULL)
HeapFree(GetProcessHeap(), 0, pszCommand);
CleanupContext();
UnloadHelpers();
return result;
return (dwError == ERROR_SUCCESS) ? EXIT_SUCCESS : EXIT_FAILURE;
}
VOID

View File

@@ -215,16 +215,11 @@ ShowHelperCommand(
/* interpreter.c */
BOOL
DWORD
InterpretLine(
_In_ LPWSTR pszFileName);
BOOL
InterpretCommand(
_In_ LPWSTR *argv,
_In_ DWORD dwArgCount,
_Inout_ PBOOL bDone);
VOID
InterpretInteractive(VOID);
@@ -234,4 +229,9 @@ DWORD
RunScript(
_In_ LPCWSTR filename);
LPWSTR
MergeStrings(
_In_ LPWSTR pszStringArray[],
_In_ INT nCount);
#endif /* PRECOMP_H */