ReactOS 0.4.16-dev-1946-g52006dd
interpreter.c File Reference
#include "precomp.h"
#include <debug.h>
Include dependency graph for interpreter.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Typedefs

typedef enum _INTERPRETER_STATE INTERPRETER_STATE
 

Enumerations

enum  _INTERPRETER_STATE {
  STATE_ANALYZE , STATE_COMMAND , STATE_GROUP , STATE_CONTEXT ,
  STATE_PARENT_CONTEXT , STATE_DONE
}
 

Functions

static PCOMMAND_ENTRY GetGroupCommand (PWSTR pszCommand, PCOMMAND_GROUP pGroup)
 
static PCOMMAND_ENTRY GetContextCommand (PWSTR pszCommand, PCONTEXT_ENTRY pContext)
 
static PCOMMAND_GROUP GetContextGroup (PWSTR pszGroup, PCONTEXT_ENTRY pContext)
 
static PCONTEXT_ENTRY GetContextSubContext (PWSTR pszContext, PCONTEXT_ENTRY pContext)
 
static DWORD InterpretCommand (_In_ LPWSTR *argv, _In_ DWORD dwArgCount, _Inout_ PBOOL bDone)
 
DWORD InterpretLine (_In_ LPWSTR pszInputLine)
 
VOID PrintPrompt (_In_ PCONTEXT_ENTRY pContext)
 
VOID InterpretInteractive (VOID)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 12 of file interpreter.c.

Typedef Documentation

◆ INTERPRETER_STATE

Enumeration Type Documentation

◆ _INTERPRETER_STATE

Enumerator
STATE_ANALYZE 
STATE_COMMAND 
STATE_GROUP 
STATE_CONTEXT 
STATE_PARENT_CONTEXT 
STATE_DONE 

Definition at line 15 of file interpreter.c.

16{
enum _INTERPRETER_STATE INTERPRETER_STATE
@ STATE_ANALYZE
Definition: interpreter.c:17
@ STATE_CONTEXT
Definition: interpreter.c:20
@ STATE_PARENT_CONTEXT
Definition: interpreter.c:21
@ STATE_DONE
Definition: interpreter.c:22
@ STATE_GROUP
Definition: interpreter.c:19
@ STATE_COMMAND
Definition: interpreter.c:18

Function Documentation

◆ GetContextCommand()

static PCOMMAND_ENTRY GetContextCommand ( PWSTR  pszCommand,
PCONTEXT_ENTRY  pContext 
)
static

Definition at line 56 of file interpreter.c.

59{
60 PCOMMAND_ENTRY pCommand;
61
62 DPRINT("GetContextCommand(%S %p)\n", pszCommand, pContext);
63
64 if (pszCommand == NULL)
65 return NULL;
66
67 pCommand = pContext->pCommandListHead;
68 while (pCommand)
69 {
70 if (MatchToken(pszCommand, pCommand->pwszCmdToken))
71 return pCommand;
72
73 pCommand = pCommand->pNext;
74 }
75
76 return NULL;
77}
#define NULL
Definition: types.h:112
BOOL WINAPI MatchToken(_In_ LPCWSTR pwszUserToken, _In_ LPCWSTR pwszCmdToken)
Definition: netsh.c:371
#define DPRINT
Definition: sndvol32.h:73
Definition: precomp.h:80
PWSTR pwszCmdToken
Definition: precomp.h:84
struct _COMMAND_ENTRY * pNext
Definition: precomp.h:82
PCOMMAND_ENTRY pCommandListHead
Definition: precomp.h:120

Referenced by InterpretCommand().

◆ GetContextGroup()

static PCOMMAND_GROUP GetContextGroup ( PWSTR  pszGroup,
PCONTEXT_ENTRY  pContext 
)
static

Definition at line 82 of file interpreter.c.

85{
86 PCOMMAND_GROUP pGroup;
87
88 DPRINT("GetContextGroup(%S %p)\n", pszGroup, pContext);
89
90 if (pszGroup == NULL)
91 return NULL;
92
93 pGroup = pContext->pGroupListHead;
94 while (pGroup)
95 {
96 if (MatchToken(pszGroup, pGroup->pwszCmdGroupToken))
97 return pGroup;
98
99 pGroup = pGroup->pNext;
100 }
101
102 return NULL;
103}
struct _COMMAND_GROUP * pNext
Definition: precomp.h:94
PWSTR pwszCmdGroupToken
Definition: precomp.h:96
PCOMMAND_GROUP pGroupListHead
Definition: precomp.h:123

Referenced by InterpretCommand().

◆ GetContextSubContext()

static PCONTEXT_ENTRY GetContextSubContext ( PWSTR  pszContext,
PCONTEXT_ENTRY  pContext 
)
static

Definition at line 108 of file interpreter.c.

111{
112 PCONTEXT_ENTRY pSubContext;
113
114 DPRINT("GetContextSubContext(%S %p)\n", pszContext, pContext);
115
116 if (pszContext == NULL)
117 return NULL;
118
119 pSubContext = pContext->pSubContextHead;
120 while (pSubContext)
121 {
122 if (MatchToken(pszContext, pSubContext->pszContextName))
123 return pSubContext;
124
125 pSubContext = pSubContext->pNext;
126 }
127
128 return NULL;
129}
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

Referenced by InterpretCommand().

◆ GetGroupCommand()

static PCOMMAND_ENTRY GetGroupCommand ( PWSTR  pszCommand,
PCOMMAND_GROUP  pGroup 
)
static

Definition at line 30 of file interpreter.c.

33{
34 PCOMMAND_ENTRY pCommand;
35
36 DPRINT("GetGroupCommand(%S %p)\n", pszCommand, pGroup);
37
38 if (pszCommand == NULL)
39 return NULL;
40
41 pCommand = pGroup->pCommandListHead;
42 while (pCommand)
43 {
44 if (MatchToken(pszCommand, pCommand->pwszCmdToken))
45 return pCommand;
46
47 pCommand = pCommand->pNext;
48 }
49
50 return NULL;
51}
PCOMMAND_ENTRY pCommandListHead
Definition: precomp.h:100

Referenced by InterpretCommand().

◆ InterpretCommand()

static DWORD InterpretCommand ( _In_ LPWSTR argv,
_In_ DWORD  dwArgCount,
_Inout_ PBOOL  bDone 
)
static

Definition at line 134 of file interpreter.c.

138{
139 PCONTEXT_ENTRY pTempContext, pTempSubContext = NULL;
140 PCOMMAND_GROUP pGroup = NULL;
141 PCOMMAND_ENTRY pCommand = NULL;
143 DWORD dwArgIndex = 0;
144 DWORD dwError = ERROR_SUCCESS;
145
146 /* If no args provided */
147 if (dwArgCount == 0)
148 return ERROR_SUCCESS;
149
150 if (pCurrentContext == NULL)
152
153 /* Check for help keywords */
154 if ((dwArgCount == 1) &&
155 ((_wcsicmp(argv[0], L"?") == 0) || (_wcsicmp(argv[0], L"help") == 0)))
156 {
158 return ERROR_SUCCESS;
159 }
160
161 pTempContext = pCurrentContext;
162 while (dwArgIndex < dwArgCount)
163 {
164 switch (State)
165 {
166 case STATE_ANALYZE:
167 DPRINT("STATE_ANALYZE\n");
168
169 pCommand = GetContextCommand(argv[dwArgIndex], pTempContext);
170 if (pCommand != NULL)
171 {
173 break;
174 }
175
176 pGroup = GetContextGroup(argv[dwArgIndex], pTempContext);
177 if (pGroup != NULL)
178 {
180 break;
181 }
182
183 pTempSubContext = GetContextSubContext(argv[dwArgIndex], pTempContext);
184 if (pTempSubContext != NULL)
185 {
187 break;
188 }
189
191 break;
192
193 case STATE_COMMAND:
194 DPRINT("STATE_COMMAND\n");
195
196 /* Check for help keywords */
197 if (((dwArgIndex + 1) == (dwArgCount - 1)) &&
198 ((_wcsicmp(argv[dwArgIndex + 1], L"?") == 0) || (_wcsicmp(argv[dwArgIndex + 1], L"help") == 0)))
199 {
200 PrintCommandHelp(pTempContext, pGroup, pCommand);
202 break;
203 }
204
205 if (pCommand->pfnCmdHandler != NULL)
206 {
207 dwArgIndex++;
208 dwError = pCommand->pfnCmdHandler(pszMachine, argv, dwArgIndex, dwArgCount, 0, NULL, bDone);
209 if (dwError != ERROR_SUCCESS)
210 {
211 if (dwError == ERROR_SHOW_USAGE)
212 {
213 PrintCommandHelp(pTempContext, pGroup, pCommand);
214 dwError = ERROR_SUPPRESS_OUTPUT;
215 }
216 }
217 else
218 {
219 /* Execute the commands following a pushd command */
220 if ((_wcsicmp(pCommand->pwszCmdToken, L"pushd") == 0) &&
221 (dwArgIndex < dwArgCount))
222 {
224 break;
225 }
226 }
227 }
228
230 break;
231
232 case STATE_GROUP:
233 DPRINT("STATE_GROUP\n");
234
235 /* Check for group without command */
236 if (dwArgIndex == (dwArgCount - 1))
237 {
238 PrintGroupHelp(pTempContext, pGroup->pwszCmdGroupToken, TRUE);
240 break;
241 }
242
243 /* Check for help keywords */
244 if (((dwArgIndex + 1) <= (dwArgCount - 1)) &&
245 ((_wcsicmp(argv[dwArgIndex + 1], L"?") == 0) || (_wcsicmp(argv[dwArgIndex + 1], L"help") == 0)))
246 {
247 PrintGroupHelp(pTempContext, pGroup->pwszCmdGroupToken, TRUE);
249 break;
250 }
251
252 dwArgIndex++;
253 pCommand = GetGroupCommand(argv[dwArgIndex], pGroup);
254 if (pCommand != NULL)
255 {
257 break;
258 }
259
260 dwError = ERROR_CMD_NOT_FOUND;
262 break;
263
264 case STATE_CONTEXT:
265 DPRINT("STATE_CONTEXT\n");
266
267 if (pTempSubContext == pCurrentContext)
268 {
269 if (dwArgIndex != (dwArgCount - 1))
270 dwError = ERROR_CMD_NOT_FOUND;
271
273 break;
274 }
275
276 if (dwArgIndex == (dwArgCount - 1))
277 {
278 DPRINT("Set current context\n");
279 pCurrentContext = pTempSubContext;
283 break;
284 }
285
286 /* Check for help keywords */
287 if (((dwArgIndex + 1) <= (dwArgCount - 1)) &&
288 ((_wcsicmp(argv[dwArgIndex + 1], L"?") == 0) || (_wcsicmp(argv[dwArgIndex + 1], L"help") == 0)))
289 {
290 PrintContextHelp(pTempSubContext);
292 break;
293 }
294
295 DPRINT("Change temorary context\n");
296 pTempContext = pTempSubContext;
298 dwArgIndex++;
299 break;
300
302 DPRINT("STATE_PARENT_CONTEXT\n");
303
304 if (pTempContext->pParentContext == NULL)
305 {
306 dwError = ERROR_CMD_NOT_FOUND;
308 break;
309 }
310
311 DPRINT("Change temorary context\n");
312 pTempContext = pTempContext->pParentContext;
314 dwArgIndex = 0;
315 break;
316
317 case STATE_DONE:
318 DPRINT("STATE_DONE dwError %ld\n", dwError);
319 return dwError;
320 }
321 }
322
323 /* Done */
324 return ERROR_SUCCESS;
325}
PCONTEXT_ENTRY pRootContext
Definition: context.c:26
PCONTEXT_ENTRY pCurrentContext
Definition: context.c:27
PWSTR pszMachine
Definition: context.c:32
VOID PrintCommandHelp(_In_ PCONTEXT_ENTRY pContext, _In_ PCOMMAND_GROUP pGroup, _In_ PCOMMAND_ENTRY pCommand)
Definition: help.c:278
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
static PCOMMAND_GROUP GetContextGroup(PWSTR pszGroup, PCONTEXT_ENTRY pContext)
Definition: interpreter.c:82
static PCOMMAND_ENTRY GetContextCommand(PWSTR pszCommand, PCONTEXT_ENTRY pContext)
Definition: interpreter.c:56
static PCOMMAND_ENTRY GetGroupCommand(PWSTR pszCommand, PCOMMAND_GROUP pGroup)
Definition: interpreter.c:30
static PCONTEXT_ENTRY GetContextSubContext(PWSTR pszContext, PCONTEXT_ENTRY pContext)
Definition: interpreter.c:108
#define ERROR_SUCCESS
Definition: deptool.c:10
#define TRUE
Definition: types.h:120
#define L(x)
Definition: resources.c:13
unsigned long DWORD
Definition: ntddk_ex.h:95
#define argv
Definition: mplay32.c:18
#define ERROR_SUPPRESS_OUTPUT
Definition: netsh.h:26
#define ERROR_CMD_NOT_FOUND
Definition: netsh.h:13
_In_ LPWSTR _In_ DWORD dwArgCount
Definition: netsh.h:115
#define ERROR_SHOW_USAGE
Definition: netsh.h:22
_Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
PFN_HANDLE_CMD pfnCmdHandler
Definition: precomp.h:85
PNS_CONTEXT_CONNECT_FN pfnConnectFn
Definition: precomp.h:118
struct _CONTEXT_ENTRY * pParentContext
Definition: precomp.h:109

Referenced by InterpretInteractive(), and InterpretLine().

◆ InterpretInteractive()

VOID InterpretInteractive ( VOID  )

Definition at line 385 of file interpreter.c.

386{
387 WCHAR input_line[MAX_STRING_SIZE];
388 LPWSTR args_vector[MAX_ARGS_COUNT];
389 DWORD dwArgCount = 0;
390 BOOL bWhiteSpace = TRUE;
391 BOOL bDone = FALSE;
392 LPWSTR ptr;
393 DWORD dwError = ERROR_SUCCESS;
394
395 for (;;)
396 {
397 dwArgCount = 0;
398 memset(args_vector, 0, sizeof(args_vector));
399
400 /* Shown just before the input where the user places commands */
401 if (pszMachine)
402 ConPrintf(StdOut, L"[%s] ", pszMachine);
404 ConPuts(StdOut, L">");
405
406 /* Get input from the user. */
407 fgetws(input_line, MAX_STRING_SIZE, stdin);
408
409 ptr = input_line;
410 while (*ptr != 0)
411 {
412 if (iswspace(*ptr) || *ptr == L'\n')
413 {
414 *ptr = 0;
415 bWhiteSpace = TRUE;
416 }
417 else
418 {
419 if ((bWhiteSpace != FALSE) && (dwArgCount < MAX_ARGS_COUNT))
420 {
421 args_vector[dwArgCount] = ptr;
422 dwArgCount++;
423 }
424 bWhiteSpace = FALSE;
425 }
426 ptr++;
427 }
428
429 dwError = InterpretCommand(args_vector, dwArgCount, &bDone);
430 if ((dwError != ERROR_SUCCESS) && (dwError != ERROR_SUPPRESS_OUTPUT))
431 {
432 PWSTR pszCommandString = MergeStrings(args_vector, dwArgCount);
433 PrintError(NULL, dwError, pszCommandString);
434 HeapFree(GetProcessHeap(), 0, pszCommandString);
435 }
436 if (dwError != ERROR_SUPPRESS_OUTPUT)
437 ConPuts(StdOut, L"\n");
438
439 if (bDone)
440 break;
441 }
442}
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
static DWORD InterpretCommand(_In_ LPWSTR *argv, _In_ DWORD dwArgCount, _Inout_ PBOOL bDone)
Definition: interpreter.c:134
VOID PrintPrompt(_In_ PCONTEXT_ENTRY pContext)
Definition: interpreter.c:371
#define MAX_STRING_SIZE
Definition: precomp.h:40
#define MAX_ARGS_COUNT
Definition: precomp.h:41
static VOID PrintError(DWORD dwError)
Definition: cacls.c:37
#define FALSE
Definition: types.h:117
#define GetProcessHeap()
Definition: compat.h:736
#define HeapFree(x, y, z)
Definition: compat.h:735
unsigned int BOOL
Definition: ntddk_ex.h:94
#define iswspace(_c)
Definition: ctype.h:669
#define stdin
Definition: stdio.h:98
static PVOID ptr
Definition: dispmode.c:27
LPWSTR MergeStrings(_In_ LPWSTR pszStringArray[], _In_ INT nCount)
Definition: netsh.c:53
#define memset(x, y, z)
Definition: compat.h:39
uint16_t * PWSTR
Definition: typedefs.h:56
wchar_t * fgetws(wchar_t *buf, int bufsize, FILE *file)
Definition: wmain.c:22
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184

Referenced by wmain().

◆ InterpretLine()

DWORD InterpretLine ( _In_ LPWSTR  pszInputLine)

Definition at line 333 of file interpreter.c.

335{
336 LPWSTR args_vector[MAX_ARGS_COUNT];
337 DWORD dwArgCount = 0;
338 BOOL bWhiteSpace = TRUE;
339 BOOL bDone = FALSE;
340 LPWSTR ptr;
341
342 memset(args_vector, 0, sizeof(args_vector));
343
344 ptr = pszInputLine;
345 while (*ptr != 0)
346 {
347 if (iswspace(*ptr) || *ptr == L'\n')
348 {
349 *ptr = 0;
350 bWhiteSpace = TRUE;
351 }
352 else
353 {
354 if ((bWhiteSpace != FALSE) && (dwArgCount < MAX_ARGS_COUNT))
355 {
356 args_vector[dwArgCount] = ptr;
357 dwArgCount++;
358 }
359
360 bWhiteSpace = FALSE;
361 }
362
363 ptr++;
364 }
365
366 return InterpretCommand(args_vector, dwArgCount, &bDone);
367}

Referenced by RunScript(), and wmain().

◆ PrintPrompt()

VOID PrintPrompt ( _In_ PCONTEXT_ENTRY  pContext)

Definition at line 371 of file interpreter.c.

373{
374 if (pContext != pRootContext)
375 {
376 PrintPrompt(pContext->pParentContext);
377 ConPuts(StdOut, L" ");
378 }
379
380 ConPuts(StdOut, pContext->pszContextName);
381}

Referenced by Batch(), ExecuteCommandWithEcho(), InterpretInteractive(), PrintPrompt(), ReadCommand(), and ReadLine().