Something went wrong. Try again.
Reactos
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371/* * PROJECT: ReactOS NetSh * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later) * PURPOSE: Network Shell builtin help command and support functions * COPYRIGHT: Copyright 2023 Eric Kohl <eric.kohl@reactos.org> */
/* INCLUDES *******************************************************************/
#include "precomp.h"
#define NDEBUG#include <debug.h>
#define HUGE_HELP_BUFFER_SIZE 2048#define SMALL_HELP_BUFFER_SIZE 160#define TINY_HELP_BUFFER_SIZE 80
typedef enum{ Command, Group, SubContext} HELP_TYPE, *PHELP_TYPE;
typedef struct{ HELP_TYPE Type; PWSTR pszCommand; DWORD dwHelpId; union { PCOMMAND_ENTRY pCommand; PCOMMAND_GROUP pGroup; PCONTEXT_ENTRY pSubContext; } Pointer;} HELP_ENTRY, *PHELP_ENTRY;
/* FUNCTIONS ******************************************************************/
staticVOIDGetContextFullName( _In_ PCONTEXT_ENTRY pContext, _Inout_ LPWSTR pszBuffer, _In_ DWORD cchLength){ if (pContext != pRootContext) { GetContextFullName(pContext->pParentContext, pszBuffer, cchLength); wcscat(pszBuffer, L" "); wcscat(pszBuffer, pContext->pszContextName); } else { wcscpy(pszBuffer, pContext->pszContextName); }}
staticVOIDPrintCurrentContextHeader( _In_ PCONTEXT_ENTRY pContext){ WCHAR szBuffer[SMALL_HELP_BUFFER_SIZE];
if (pContext == pCurrentContext) { ConResPrintf(StdOut, IDS_THIS_COMMANDS); } else { GetContextFullName(pContext, szBuffer, SMALL_HELP_BUFFER_SIZE); ConResPrintf(StdOut, IDS_CONTEXT_COMMANDS, szBuffer); }}
staticVOIDPrintShortGroupCommands( _In_ PCONTEXT_ENTRY pContext, _In_ PCOMMAND_GROUP pGroup){ PCOMMAND_ENTRY pCommand; WCHAR szBuffer1[TINY_HELP_BUFFER_SIZE]; WCHAR szBuffer2[SMALL_HELP_BUFFER_SIZE];
pCommand = pGroup->pCommandListHead; while (pCommand != NULL) { swprintf(szBuffer1, L"%s %s", pGroup->pwszCmdGroupToken, pCommand->pwszCmdToken); LoadStringW(pContext->hModule, pCommand->dwShortCmdHelpToken, szBuffer2, SMALL_HELP_BUFFER_SIZE);
ConPrintf(StdOut, L"%-15s - %s", szBuffer1, szBuffer2); pCommand = pCommand->pNext; }}
staticintHelpCompare( _In_ const void *p1, _In_ const void *p2){ return _wcsicmp(((PHELP_ENTRY)p1)->pszCommand, ((PHELP_ENTRY)p2)->pszCommand);}
staticVOIDPrintContext( _In_ PCONTEXT_ENTRY pContext){ PCOMMAND_ENTRY pCommand; PCOMMAND_GROUP pGroup; PCONTEXT_ENTRY pSubContext; PHELP_ENTRY pHelpArray = NULL; DWORD dwCount = 0, dwIndex; WCHAR szBuffer[SMALL_HELP_BUFFER_SIZE];
DPRINT("PrintContext()\n");
if (pContext != pRootContext) PrintContext(pContext->pParentContext);
PrintCurrentContextHeader(pContext);
/* Count short commands */ pCommand = pContext->pCommandListHead; while (pCommand != NULL) { dwCount++; pCommand = pCommand->pNext; }
/* Count short groups */ pGroup = pContext->pGroupListHead; while (pGroup != NULL) { dwCount++; pGroup = pGroup->pNext; }
/* Count short subcontexts */ pSubContext = pContext->pSubContextHead; while (pSubContext != NULL) { dwCount++; pSubContext = pSubContext->pNext; }
pHelpArray = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCount * sizeof(HELP_ENTRY)); if (pHelpArray == NULL) return;
dwIndex = 0;
/* Add short commands */ pCommand = pContext->pCommandListHead; while (pCommand != NULL) { pHelpArray[dwIndex].Type = Command; pHelpArray[dwIndex].pszCommand = pCommand->pwszCmdToken; pHelpArray[dwIndex].dwHelpId = pCommand->dwShortCmdHelpToken;// pHelpArray[dwIndex].Pointer.pCommand = pCommand; dwIndex++; pCommand = pCommand->pNext; }
/* Add short groups */ pGroup = pContext->pGroupListHead; while (pGroup != NULL) { pHelpArray[dwIndex].Type = Group; pHelpArray[dwIndex].pszCommand = pGroup->pwszCmdGroupToken; pHelpArray[dwIndex].dwHelpId = pGroup->dwShortCmdHelpToken;// pHelpArray[dwIndex].Pointer.pGroup = pGroup; dwIndex++; pGroup = pGroup->pNext; }
/* Count short subcontexts */ pSubContext = pContext->pSubContextHead; while (pSubContext != NULL) { pHelpArray[dwIndex].Type = SubContext; pHelpArray[dwIndex].pszCommand = pSubContext->pszContextName; pHelpArray[dwIndex].Pointer.pSubContext = pSubContext; dwIndex++; pSubContext = pSubContext->pNext; }
qsort(pHelpArray, dwCount, sizeof(HELP_ENTRY), HelpCompare);
for (dwIndex = 0; dwIndex < dwCount; dwIndex++) { switch (pHelpArray[dwIndex].Type) { case Command: case Group: if (LoadStringW(pContext->hModule, pHelpArray[dwIndex].dwHelpId, szBuffer, SMALL_HELP_BUFFER_SIZE) == 0) szBuffer[0] = UNICODE_NULL; ConPrintf(StdOut, L"%-15s - %s", pHelpArray[dwIndex].pszCommand, szBuffer); break;
case SubContext: GetContextFullName(pHelpArray[dwIndex].Pointer.pSubContext, szBuffer, SMALL_HELP_BUFFER_SIZE); ConPrintf(StdOut, L"%-15s - Changes to the \"%s\" context.\n", pHelpArray[dwIndex].pszCommand, szBuffer); break; } }
if (pHelpArray) HeapFree(GetProcessHeap(), 0, pHelpArray);}
staticintSubContextCompare( _In_ const void *p1, _In_ const void *p2){ return _wcsicmp((*((PCONTEXT_ENTRY*)p1))->pszContextName, (*((PCONTEXT_ENTRY*)p2))->pszContextName);}
staticVOIDPrintSubcontexts( _In_ PCONTEXT_ENTRY pContext){ PCONTEXT_ENTRY pSubContext, *pSubContextArray = NULL; DWORD dwCount, dwIndex;
if (pContext->pSubContextHead == NULL) return;
dwCount = 0; pSubContext = pContext->pSubContextHead; while (pSubContext != NULL) { dwCount++; pSubContext = pSubContext->pNext; }
pSubContextArray = HeapAlloc(GetProcessHeap(), 0, dwCount * sizeof(PCONTEXT_ENTRY)); if (pSubContextArray == NULL) return;
dwIndex = 0; pSubContext = pContext->pSubContextHead; while (pSubContext != NULL) { pSubContextArray[dwIndex] = pSubContext; dwIndex++; pSubContext = pSubContext->pNext; } qsort(pSubContextArray, dwCount, sizeof(PCONTEXT_ENTRY), SubContextCompare);
ConResPrintf(StdOut, IDS_SUBCONTEXT_HEADER); for (dwIndex = 0; dwIndex < dwCount; dwIndex++) { ConPrintf(StdOut, L" %s", pSubContextArray[dwIndex]->pszContextName); } ConPuts(StdOut, L"\n");
HeapFree(GetProcessHeap(), 0, pSubContextArray);}
VOIDPrintCommandHelp( _In_ PCONTEXT_ENTRY pContext, _In_ PCOMMAND_GROUP pGroup, _In_ PCOMMAND_ENTRY pCommand){ LPWSTR pszInBuffer = NULL, pszOutBuffer = NULL, pszCommandBuffer = NULL; DWORD_PTR Args[2];
DPRINT("PrintCommandHelp(%p %p %p)\n", pContext, pGroup, pCommand);
pszInBuffer = HeapAlloc(GetProcessHeap(), 0, HUGE_HELP_BUFFER_SIZE * sizeof(WCHAR)); if (pszInBuffer == NULL) goto done;
pszOutBuffer = HeapAlloc(GetProcessHeap(), 0, HUGE_HELP_BUFFER_SIZE * sizeof(WCHAR)); if (pszOutBuffer == NULL) goto done;
pszCommandBuffer = HeapAlloc(GetProcessHeap(), 0, TINY_HELP_BUFFER_SIZE * sizeof(WCHAR)); if (pszCommandBuffer == NULL) goto done;
wcscpy(pszCommandBuffer, pCommand->pwszCmdToken); if (pGroup) { wcscat(pszCommandBuffer, L" "); wcscat(pszCommandBuffer, pGroup->pwszCmdGroupToken); }
LoadStringW(pContext->hModule, pCommand->dwCmdHlpToken, pszInBuffer, HUGE_HELP_BUFFER_SIZE);
Args[0] = (DWORD_PTR)pszCommandBuffer; Args[1] = (DWORD_PTR)NULL;
FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY, pszInBuffer, 0, 0, pszOutBuffer, HUGE_HELP_BUFFER_SIZE, (va_list *)&Args);
ConPuts(StdOut, pszOutBuffer); ConPuts(StdOut, L"\n");
done: if (pszCommandBuffer) HeapFree(GetProcessHeap(), 0, pszCommandBuffer);
if (pszOutBuffer) HeapFree(GetProcessHeap(), 0, pszOutBuffer);
if (pszInBuffer) HeapFree(GetProcessHeap(), 0, pszInBuffer);}
VOIDPrintGroupHelp( _In_ PCONTEXT_ENTRY pContext, _In_ LPWSTR pszGroupName, _In_ BOOL bRecurse){ PCOMMAND_GROUP pGroup;
if (bRecurse) { if (pContext != pRootContext) PrintGroupHelp(pContext->pParentContext, pszGroupName, bRecurse); }
pGroup = pContext->pGroupListHead; while (pGroup != NULL) { if (_wcsicmp(pszGroupName, pGroup->pwszCmdGroupToken) == 0) { PrintCurrentContextHeader(pContext); PrintShortGroupCommands(pContext, pGroup); } pGroup = pGroup->pNext; }}
VOIDPrintContextHelp( _In_ PCONTEXT_ENTRY pContext){ ConResPrintf(StdOut, IDS_HELP_HEADER); PrintContext(pContext); PrintSubcontexts(pContext); ConResPrintf(StdOut, IDS_HELP_FOOTER);}