ReactOS 0.4.16-dev-1946-g52006dd
help.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS NetSh
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Network Shell builtin help command and support functions
5 * COPYRIGHT: Copyright 2023 Eric Kohl <eric.kohl@reactos.org>
6 */
7
8/* INCLUDES *******************************************************************/
9
10#include "precomp.h"
11
12#define NDEBUG
13#include <debug.h>
14
15#define HUGE_HELP_BUFFER_SIZE 2048
16#define SMALL_HELP_BUFFER_SIZE 160
17#define TINY_HELP_BUFFER_SIZE 80
18
19typedef enum
20{
25
26typedef struct
27{
31 union
32 {
36 } Pointer;
38
39
40/* FUNCTIONS ******************************************************************/
41
42static
43VOID
45 _In_ PCONTEXT_ENTRY pContext,
46 _Inout_ LPWSTR pszBuffer,
47 _In_ DWORD cchLength)
48{
49 if (pContext != pRootContext)
50 {
51 GetContextFullName(pContext->pParentContext, pszBuffer, cchLength);
52 wcscat(pszBuffer, L" ");
53 wcscat(pszBuffer, pContext->pszContextName);
54 }
55 else
56 {
57 wcscpy(pszBuffer, pContext->pszContextName);
58 }
59}
60
61
62static
63VOID
65 _In_ PCONTEXT_ENTRY pContext)
66{
68
69 if (pContext == pCurrentContext)
70 {
72 }
73 else
74 {
75 GetContextFullName(pContext, szBuffer, SMALL_HELP_BUFFER_SIZE);
77 }
78}
79
80
81static
82VOID
84 _In_ PCONTEXT_ENTRY pContext,
85 _In_ PCOMMAND_GROUP pGroup)
86{
87 PCOMMAND_ENTRY pCommand;
90
91 pCommand = pGroup->pCommandListHead;
92 while (pCommand != NULL)
93 {
94 swprintf(szBuffer1, L"%s %s", pGroup->pwszCmdGroupToken, pCommand->pwszCmdToken);
95 LoadStringW(pContext->hModule, pCommand->dwShortCmdHelpToken, szBuffer2, SMALL_HELP_BUFFER_SIZE);
96
97 ConPrintf(StdOut, L"%-15s - %s", szBuffer1, szBuffer2);
98 pCommand = pCommand->pNext;
99 }
100}
101
102
103static
104int
106 _In_ const void *p1,
107 _In_ const void *p2)
108{
109 return _wcsicmp(((PHELP_ENTRY)p1)->pszCommand, ((PHELP_ENTRY)p2)->pszCommand);
110}
111
112
113static
114VOID
116 _In_ PCONTEXT_ENTRY pContext)
117{
118 PCOMMAND_ENTRY pCommand;
119 PCOMMAND_GROUP pGroup;
120 PCONTEXT_ENTRY pSubContext;
121 PHELP_ENTRY pHelpArray = NULL;
122 DWORD dwCount = 0, dwIndex;
124
125 DPRINT("PrintContext()\n");
126
127 if (pContext != pRootContext)
128 PrintContext(pContext->pParentContext);
129
131
132 /* Count short commands */
133 pCommand = pContext->pCommandListHead;
134 while (pCommand != NULL)
135 {
136 dwCount++;
137 pCommand = pCommand->pNext;
138 }
139
140 /* Count short groups */
141 pGroup = pContext->pGroupListHead;
142 while (pGroup != NULL)
143 {
144 dwCount++;
145 pGroup = pGroup->pNext;
146 }
147
148 /* Count short subcontexts */
149 pSubContext = pContext->pSubContextHead;
150 while (pSubContext != NULL)
151 {
152 dwCount++;
153 pSubContext = pSubContext->pNext;
154 }
155
156 pHelpArray = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCount * sizeof(HELP_ENTRY));
157 if (pHelpArray == NULL)
158 return;
159
160 dwIndex = 0;
161
162 /* Add short commands */
163 pCommand = pContext->pCommandListHead;
164 while (pCommand != NULL)
165 {
166 pHelpArray[dwIndex].Type = Command;
167 pHelpArray[dwIndex].pszCommand = pCommand->pwszCmdToken;
168 pHelpArray[dwIndex].dwHelpId = pCommand->dwShortCmdHelpToken;
169// pHelpArray[dwIndex].Pointer.pCommand = pCommand;
170 dwIndex++;
171 pCommand = pCommand->pNext;
172 }
173
174 /* Add short groups */
175 pGroup = pContext->pGroupListHead;
176 while (pGroup != NULL)
177 {
178 pHelpArray[dwIndex].Type = Group;
179 pHelpArray[dwIndex].pszCommand = pGroup->pwszCmdGroupToken;
180 pHelpArray[dwIndex].dwHelpId = pGroup->dwShortCmdHelpToken;
181// pHelpArray[dwIndex].Pointer.pGroup = pGroup;
182 dwIndex++;
183 pGroup = pGroup->pNext;
184 }
185
186 /* Count short subcontexts */
187 pSubContext = pContext->pSubContextHead;
188 while (pSubContext != NULL)
189 {
190 pHelpArray[dwIndex].Type = SubContext;
191 pHelpArray[dwIndex].pszCommand = pSubContext->pszContextName;
192 pHelpArray[dwIndex].Pointer.pSubContext = pSubContext;
193 dwIndex++;
194 pSubContext = pSubContext->pNext;
195 }
196
197 qsort(pHelpArray, dwCount, sizeof(HELP_ENTRY), HelpCompare);
198
199 for (dwIndex = 0; dwIndex < dwCount; dwIndex++)
200 {
201 switch (pHelpArray[dwIndex].Type)
202 {
203 case Command:
204 case Group:
205 if (LoadStringW(pContext->hModule, pHelpArray[dwIndex].dwHelpId, szBuffer, SMALL_HELP_BUFFER_SIZE) == 0)
206 szBuffer[0] = UNICODE_NULL;
207 ConPrintf(StdOut, L"%-15s - %s", pHelpArray[dwIndex].pszCommand, szBuffer);
208 break;
209
210 case SubContext:
211 GetContextFullName(pHelpArray[dwIndex].Pointer.pSubContext, szBuffer, SMALL_HELP_BUFFER_SIZE);
212 ConPrintf(StdOut, L"%-15s - Changes to the \"%s\" context.\n", pHelpArray[dwIndex].pszCommand, szBuffer);
213 break;
214 }
215 }
216
217 if (pHelpArray)
218 HeapFree(GetProcessHeap(), 0, pHelpArray);
219}
220
221
222static
223int
225 _In_ const void *p1,
226 _In_ const void *p2)
227{
228 return _wcsicmp((*((PCONTEXT_ENTRY*)p1))->pszContextName, (*((PCONTEXT_ENTRY*)p2))->pszContextName);
229}
230
231
232static
233VOID
235 _In_ PCONTEXT_ENTRY pContext)
236{
237 PCONTEXT_ENTRY pSubContext, *pSubContextArray = NULL;
238 DWORD dwCount, dwIndex;
239
240 if (pContext->pSubContextHead == NULL)
241 return;
242
243 dwCount = 0;
244 pSubContext = pContext->pSubContextHead;
245 while (pSubContext != NULL)
246 {
247 dwCount++;
248 pSubContext = pSubContext->pNext;
249 }
250
251 pSubContextArray = HeapAlloc(GetProcessHeap(), 0, dwCount * sizeof(PCONTEXT_ENTRY));
252 if (pSubContextArray == NULL)
253 return;
254
255 dwIndex = 0;
256 pSubContext = pContext->pSubContextHead;
257 while (pSubContext != NULL)
258 {
259 pSubContextArray[dwIndex] = pSubContext;
260 dwIndex++;
261 pSubContext = pSubContext->pNext;
262 }
263
264 qsort(pSubContextArray, dwCount, sizeof(PCONTEXT_ENTRY), SubContextCompare);
265
267 for (dwIndex = 0; dwIndex < dwCount; dwIndex++)
268 {
269 ConPrintf(StdOut, L" %s", pSubContextArray[dwIndex]->pszContextName);
270 }
271 ConPuts(StdOut, L"\n");
272
273 HeapFree(GetProcessHeap(), 0, pSubContextArray);
274}
275
276
277VOID
279 _In_ PCONTEXT_ENTRY pContext,
280 _In_ PCOMMAND_GROUP pGroup,
281 _In_ PCOMMAND_ENTRY pCommand)
282{
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
313 pszInBuffer,
314 0,
315 0,
316 pszOutBuffer,
318 (va_list *)&Args);
319
320 ConPuts(StdOut, pszOutBuffer);
321 ConPuts(StdOut, L"\n");
322
323done:
324 if (pszCommandBuffer)
325 HeapFree(GetProcessHeap(), 0, pszCommandBuffer);
326
327 if (pszOutBuffer)
328 HeapFree(GetProcessHeap(), 0, pszOutBuffer);
329
330 if (pszInBuffer)
331 HeapFree(GetProcessHeap(), 0, pszInBuffer);
332}
333
334
335VOID
337 _In_ PCONTEXT_ENTRY pContext,
338 _In_ LPWSTR pszGroupName,
339 _In_ BOOL bRecurse)
340{
341 PCOMMAND_GROUP pGroup;
342
343 if (bRecurse)
344 {
345 if (pContext != pRootContext)
346 PrintGroupHelp(pContext->pParentContext, pszGroupName, bRecurse);
347 }
348
349 pGroup = pContext->pGroupListHead;
350 while (pGroup != NULL)
351 {
352 if (_wcsicmp(pszGroupName, pGroup->pwszCmdGroupToken) == 0)
353 {
355 PrintShortGroupCommands(pContext, pGroup);
356 }
357 pGroup = pGroup->pNext;
358 }
359}
360
361
362VOID
364 _In_ PCONTEXT_ENTRY pContext)
365{
367 PrintContext(pContext);
368 PrintSubcontexts(pContext);
370}
Type
Definition: Type.h:7
char ** Args
Definition: acdebug.h:353
char * va_list
Definition: acmsvcex.h:78
void ConPuts(FILE *fp, LPCWSTR psz)
Definition: fc.c:16
void ConPrintf(FILE *fp, LPCWSTR psz,...)
Definition: fc.c:20
#define StdOut
Definition: fc.c:14
void ConResPrintf(FILE *fp, UINT nID,...)
Definition: fc.c:33
PCONTEXT_ENTRY pRootContext
Definition: context.c:26
PCONTEXT_ENTRY pCurrentContext
Definition: context.c:27
static VOID PrintShortGroupCommands(_In_ PCONTEXT_ENTRY pContext, _In_ PCOMMAND_GROUP pGroup)
Definition: help.c:83
#define HUGE_HELP_BUFFER_SIZE
Definition: help.c:15
VOID PrintCommandHelp(_In_ PCONTEXT_ENTRY pContext, _In_ PCOMMAND_GROUP pGroup, _In_ PCOMMAND_ENTRY pCommand)
Definition: help.c:278
static int HelpCompare(_In_ const void *p1, _In_ const void *p2)
Definition: help.c:105
static VOID PrintSubcontexts(_In_ PCONTEXT_ENTRY pContext)
Definition: help.c:234
static VOID PrintCurrentContextHeader(_In_ PCONTEXT_ENTRY pContext)
Definition: help.c:64
struct HELP_ENTRY * PHELP_ENTRY
enum HELP_TYPE * PHELP_TYPE
static VOID GetContextFullName(_In_ PCONTEXT_ENTRY pContext, _Inout_ LPWSTR pszBuffer, _In_ DWORD cchLength)
Definition: help.c:44
static int SubContextCompare(_In_ const void *p1, _In_ const void *p2)
Definition: help.c:224
#define SMALL_HELP_BUFFER_SIZE
Definition: help.c:16
HELP_TYPE
Definition: help.c:20
@ Group
Definition: help.c:22
@ SubContext
Definition: help.c:23
@ Command
Definition: help.c:21
static VOID PrintContext(_In_ PCONTEXT_ENTRY pContext)
Definition: help.c:115
#define TINY_HELP_BUFFER_SIZE
Definition: help.c:17
VOID PrintGroupHelp(_In_ PCONTEXT_ENTRY pContext, _In_ LPWSTR pszGroupName, _In_ BOOL bRecurse)
Definition: help.c:336
VOID PrintContextHelp(_In_ PCONTEXT_ENTRY pContext)
Definition: help.c:363
#define IDS_CONTEXT_COMMANDS
Definition: resource.h:16
#define IDS_HELP_FOOTER
Definition: resource.h:20
#define IDS_THIS_COMMANDS
Definition: resource.h:15
#define IDS_HELP_HEADER
Definition: resource.h:19
#define IDS_SUBCONTEXT_HEADER
Definition: resource.h:21
wcscat
wcscpy
void __cdecl qsort(_Inout_updates_bytes_(_NumOfElements *_SizeOfElements) void *_Base, _In_ size_t _NumOfElements, _In_ size_t _SizeOfElements, _In_ int(__cdecl *_PtFuncCompare)(const void *, const void *))
#define NULL
Definition: types.h:112
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
DWORD WINAPI FormatMessageW(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, LPWSTR lpBuffer, DWORD nSize, __ms_va_list *args)
Definition: format_msg.c:583
INT WINAPI DECLSPEC_HOTPATCH LoadStringW(HINSTANCE instance, UINT resource_id, LPWSTR buffer, INT buflen)
Definition: string.c:1220
#define swprintf
Definition: precomp.h:40
#define L(x)
Definition: resources.c:13
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
#define _Inout_
Definition: no_sal2.h:162
#define _In_
Definition: no_sal2.h:158
#define UNICODE_NULL
_Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
#define DPRINT
Definition: sndvol32.h:73
Definition: help.c:27
HELP_TYPE Type
Definition: help.c:28
PWSTR pszCommand
Definition: help.c:29
DWORD dwHelpId
Definition: help.c:30
union HELP_ENTRY::@18 Pointer
PCONTEXT_ENTRY pSubContext
Definition: help.c:35
PCOMMAND_ENTRY pCommand
Definition: help.c:33
PCOMMAND_GROUP pGroup
Definition: help.c:34
Definition: precomp.h:80
PWSTR pwszCmdToken
Definition: precomp.h:84
DWORD dwShortCmdHelpToken
Definition: precomp.h:86
struct _COMMAND_ENTRY * pNext
Definition: precomp.h:82
struct _COMMAND_GROUP * pNext
Definition: precomp.h:94
PWSTR pwszCmdGroupToken
Definition: precomp.h:96
DWORD dwShortCmdHelpToken
Definition: precomp.h:97
Definition: precomp.h:105
PWSTR pszContextName
Definition: precomp.h:112
struct _CONTEXT_ENTRY * pSubContextHead
Definition: precomp.h:126
struct _CONTEXT_ENTRY * pNext
Definition: precomp.h:107
#define DWORD_PTR
Definition: treelist.c:76
uint16_t * PWSTR
Definition: typedefs.h:56
uint32_t DWORD_PTR
Definition: typedefs.h:65
#define FORMAT_MESSAGE_FROM_STRING
Definition: winbase.h:398
#define FORMAT_MESSAGE_ARGUMENT_ARRAY
Definition: winbase.h:401
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184