Skip to content

Commit 4ad647f

Browse files
committed
[NETSH] Improvements to the command line evaluation and the command interpreter
- Interpreter functions return error code instead of boolean
1 parent de5f284 commit 4ad647f

File tree

3 files changed

+105
-70
lines changed

3 files changed

+105
-70
lines changed

base/applications/network/netsh/interpreter.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ GetContextSubContext(
129129
}
130130

131131

132-
BOOL
132+
static
133+
DWORD
133134
InterpretCommand(
134135
_In_ LPWSTR *argv,
135136
_In_ DWORD dwArgCount,
@@ -141,11 +142,10 @@ InterpretCommand(
141142
INTERPRETER_STATE State = STATE_ANALYZE;
142143
DWORD dwArgIndex = 0;
143144
DWORD dwError = ERROR_SUCCESS;
144-
BOOL bFound = TRUE;
145145

146146
/* If no args provided */
147147
if (dwArgCount == 0)
148-
return TRUE;
148+
return ERROR_SUCCESS;
149149

150150
if (pCurrentContext == NULL)
151151
pCurrentContext = pRootContext;
@@ -155,7 +155,7 @@ InterpretCommand(
155155
((_wcsicmp(argv[0], L"?") == 0) || (_wcsicmp(argv[0], L"help") == 0)))
156156
{
157157
PrintContextHelp(pCurrentContext);
158-
return TRUE;
158+
return ERROR_SUCCESS;
159159
}
160160

161161
pTempContext = pCurrentContext;
@@ -256,7 +256,7 @@ InterpretCommand(
256256
break;
257257
}
258258

259-
bFound = FALSE;
259+
dwError = ERROR_CMD_NOT_FOUND;
260260
State = STATE_DONE;
261261
break;
262262

@@ -266,7 +266,7 @@ InterpretCommand(
266266
if (pTempSubContext == pCurrentContext)
267267
{
268268
if (dwArgIndex != (dwArgCount - 1))
269-
bFound = FALSE;
269+
dwError = ERROR_CMD_NOT_FOUND;
270270

271271
State = STATE_DONE;
272272
break;
@@ -300,7 +300,7 @@ InterpretCommand(
300300

301301
if (pTempContext->pParentContext == NULL)
302302
{
303-
bFound = FALSE;
303+
dwError = ERROR_CMD_NOT_FOUND;
304304
State = STATE_DONE;
305305
break;
306306
}
@@ -313,19 +313,19 @@ InterpretCommand(
313313

314314
case STATE_DONE:
315315
DPRINT("STATE_DONE\n");
316-
return bFound;
316+
return dwError;
317317
}
318318
}
319319

320-
return TRUE;
320+
return ERROR_CMD_NOT_FOUND;
321321
}
322322

323323

324324
/*
325325
* InterpretScript(char *line):
326326
* The main function used for when reading commands from scripts.
327327
*/
328-
BOOL
328+
DWORD
329329
InterpretLine(
330330
_In_ LPWSTR pszInputLine)
331331
{
@@ -386,6 +386,7 @@ InterpretInteractive(VOID)
386386
BOOL bWhiteSpace = TRUE;
387387
BOOL bDone = FALSE;
388388
LPWSTR ptr;
389+
DWORD dwError = ERROR_SUCCESS;
389390

390391
for (;;)
391392
{
@@ -419,7 +420,8 @@ InterpretInteractive(VOID)
419420
ptr++;
420421
}
421422

422-
if (InterpretCommand(args_vector, dwArgCount, &bDone) == FALSE)
423+
dwError = InterpretCommand(args_vector, dwArgCount, &bDone);
424+
if (dwError == ERROR_CMD_NOT_FOUND)
423425
{
424426
ConResPrintf(StdErr, IDS_INVALID_COMMAND, input_line);
425427
}

base/applications/network/netsh/netsh.c

Lines changed: 85 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ DWORD
1818
RunScript(
1919
_In_ LPCWSTR filename)
2020
{
21-
FILE *script;
2221
WCHAR tmp_string[MAX_STRING_SIZE];
22+
FILE *script;
23+
DWORD dwError = ERROR_SUCCESS;
2324

2425
/* Open the file for processing */
2526
script = _wfopen(filename, L"r");
@@ -32,19 +33,51 @@ RunScript(
3233
/* Read and process the script */
3334
while (fgetws(tmp_string, MAX_STRING_SIZE, script) != NULL)
3435
{
35-
if (InterpretLine(tmp_string) == FALSE)
36-
{
37-
fclose(script);
38-
return ERROR_SUCCESS; /* FIXME */
39-
}
36+
dwError = InterpretLine(tmp_string);
37+
if (dwError != ERROR_SUCCESS)
38+
break;
4039
}
4140

4241
/* Close the file */
4342
fclose(script);
4443

45-
return ERROR_SUCCESS;
44+
return dwError;
4645
}
4746

47+
48+
LPWSTR
49+
MergeStrings(
50+
_In_ LPWSTR pszStringArray[],
51+
_In_ INT nCount)
52+
{
53+
LPWSTR pszOutString = NULL;
54+
INT i, nLength;
55+
56+
if ((pszStringArray == NULL) || (nCount == 0))
57+
return NULL;
58+
59+
nLength = 0;
60+
for (i = 0; i < nCount; i++)
61+
nLength += wcslen(pszStringArray[i]);
62+
63+
if (nLength > 0)
64+
nLength += nCount; /* Space characters and terminating zero */
65+
66+
pszOutString = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nLength * sizeof(WCHAR));
67+
if (pszOutString == NULL)
68+
return NULL;
69+
70+
for (i = 0; i < nCount; i++)
71+
{
72+
if (i != 0)
73+
wcscat(pszOutString, L" ");
74+
wcscat(pszOutString, pszStringArray[i]);
75+
}
76+
77+
return pszOutString;
78+
}
79+
80+
4881
/*
4982
* wmain():
5083
* Main entry point of the application.
@@ -54,12 +87,11 @@ wmain(
5487
_In_ int argc,
5588
_In_ const LPWSTR argv[])
5689
{
57-
LPCWSTR tmpBuffer = NULL;
5890
LPCWSTR pszFileName = NULL;
5991
LPCWSTR pszContext = NULL;
92+
LPWSTR pszCommand = NULL;
6093
int index;
61-
int result = EXIT_SUCCESS;
62-
BOOL bDone = FALSE;
94+
DWORD dwError = ERROR_SUCCESS;
6395

6496
DPRINT("wmain(%S)\n", GetCommandLineW());
6597

@@ -74,34 +106,17 @@ wmain(
74106
/* Process the command arguments */
75107
for (index = 1; index < argc; index++)
76108
{
77-
if ((argv[index][0] == '/')||
78-
(argv[index][0] == '-'))
79-
{
80-
tmpBuffer = argv[index] + 1;
81-
}
82-
else
83-
{
84-
if (pszFileName != NULL)
85-
{
86-
ConResPuts(StdOut, IDS_APP_USAGE);
87-
result = EXIT_FAILURE;
88-
goto done;
89-
}
90-
91-
/* Run a command from the command line */
92-
if (InterpretCommand((LPWSTR*)&argv[index], argc - index, &bDone) == FALSE)
93-
result = EXIT_FAILURE;
94-
goto done;
95-
}
96-
97-
if (_wcsicmp(tmpBuffer, L"?") == 0)
109+
if ((_wcsicmp(argv[index], L"-?") == 0) ||
110+
(_wcsicmp(argv[index], L"/?") == 0) ||
111+
(_wcsicmp(argv[index], L"?") == 0))
98112
{
99113
/* Help option */
100114
ConResPuts(StdOut, IDS_APP_USAGE);
101-
result = EXIT_SUCCESS;
115+
dwError = ERROR_SUCCESS;
102116
goto done;
103117
}
104-
else if (_wcsicmp(tmpBuffer, L"a") == 0)
118+
else if ((_wcsicmp(argv[index], L"-a") == 0) ||
119+
(_wcsicmp(argv[index], L"/a") == 0))
105120
{
106121
/* Aliasfile option */
107122
if ((index + 1) < argc)
@@ -113,10 +128,12 @@ wmain(
113128
else
114129
{
115130
ConResPuts(StdOut, IDS_APP_USAGE);
116-
result = EXIT_FAILURE;
131+
dwError = ERROR_INVALID_SYNTAX;
132+
goto done;
117133
}
118134
}
119-
else if (_wcsicmp(tmpBuffer, L"c") == 0)
135+
else if ((_wcsicmp(argv[index], L"-c") == 0) ||
136+
(_wcsicmp(argv[index], L"/c") == 0))
120137
{
121138
/* Context option */
122139
if ((index + 1) < argc)
@@ -127,10 +144,12 @@ wmain(
127144
else
128145
{
129146
ConResPuts(StdOut, IDS_APP_USAGE);
130-
result = EXIT_FAILURE;
147+
dwError = ERROR_INVALID_SYNTAX;
148+
goto done;
131149
}
132150
}
133-
else if (_wcsicmp(tmpBuffer, L"f") == 0)
151+
else if ((_wcsicmp(argv[index], L"-f") == 0) ||
152+
(_wcsicmp(argv[index], L"/f") == 0))
134153
{
135154
/* File option */
136155
if ((index + 1) < argc)
@@ -141,10 +160,12 @@ wmain(
141160
else
142161
{
143162
ConResPuts(StdOut, IDS_APP_USAGE);
144-
result = EXIT_FAILURE;
163+
dwError = ERROR_INVALID_SYNTAX;
164+
goto done;
145165
}
146166
}
147-
else if (_wcsicmp(tmpBuffer, L"r") == 0)
167+
else if ((_wcsicmp(argv[index], L"-r") == 0) ||
168+
(_wcsicmp(argv[index], L"/r") == 0))
148169
{
149170
/* Remote option */
150171
if ((index + 1) < argc)
@@ -156,33 +177,43 @@ wmain(
156177
else
157178
{
158179
ConResPuts(StdOut, IDS_APP_USAGE);
159-
result = EXIT_FAILURE;
180+
dwError = ERROR_INVALID_SYNTAX;
181+
goto done;
160182
}
161183
}
162184
else
163185
{
164-
/* Invalid command */
165-
ConResPrintf(StdOut, IDS_INVALID_COMMAND, argv[index]);
166-
result = EXIT_FAILURE;
167-
goto done;
186+
if (pszFileName != NULL)
187+
{
188+
ConResPuts(StdOut, IDS_APP_USAGE);
189+
dwError = ERROR_INVALID_SYNTAX;
190+
goto done;
191+
}
192+
else if (pszCommand == NULL)
193+
{
194+
pszCommand = MergeStrings((LPWSTR*)&argv[index], argc - index);
195+
if (pszCommand)
196+
break;
197+
}
168198
}
169199
}
170200

171201
/* Set a context */
172202
if (pszContext)
173203
{
174-
if (InterpretLine((LPWSTR)pszContext) == FALSE)
175-
{
176-
result = EXIT_FAILURE;
204+
dwError = InterpretLine((LPWSTR)pszContext);
205+
if (dwError != ERROR_SUCCESS)
177206
goto done;
178-
}
179207
}
180208

181-
/* Run a script or the interactive interpeter */
209+
/* Run a script, the command line instruction or the interactive interpeter */
182210
if (pszFileName != NULL)
183211
{
184-
if (RunScript(pszFileName) == FALSE)
185-
result = EXIT_FAILURE;
212+
dwError = RunScript(pszFileName);
213+
}
214+
else if (pszCommand != NULL)
215+
{
216+
dwError = InterpretLine(pszCommand);
186217
}
187218
else
188219
{
@@ -191,10 +222,12 @@ wmain(
191222

192223
done:
193224
/* FIXME: Cleanup code goes here */
225+
if (pszCommand != NULL)
226+
HeapFree(GetProcessHeap(), 0, pszCommand);
194227
CleanupContext();
195228
UnloadHelpers();
196229

197-
return result;
230+
return (dwError == ERROR_SUCCESS) ? EXIT_SUCCESS : EXIT_FAILURE;
198231
}
199232

200233
VOID

base/applications/network/netsh/precomp.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,16 +215,11 @@ ShowHelperCommand(
215215

216216

217217
/* interpreter.c */
218-
BOOL
218+
219+
DWORD
219220
InterpretLine(
220221
_In_ LPWSTR pszFileName);
221222

222-
BOOL
223-
InterpretCommand(
224-
_In_ LPWSTR *argv,
225-
_In_ DWORD dwArgCount,
226-
_Inout_ PBOOL bDone);
227-
228223
VOID
229224
InterpretInteractive(VOID);
230225

@@ -234,4 +229,9 @@ DWORD
234229
RunScript(
235230
_In_ LPCWSTR filename);
236231

232+
LPWSTR
233+
MergeStrings(
234+
_In_ LPWSTR pszStringArray[],
235+
_In_ INT nCount);
236+
237237
#endif /* PRECOMP_H */

0 commit comments

Comments
 (0)