Skip to content

Commit 20eee6d

Browse files
committed
[NETSH] Support the -c (context) command line option
1 parent 2e7a176 commit 20eee6d

File tree

3 files changed

+94
-91
lines changed

3 files changed

+94
-91
lines changed

base/applications/network/netsh/interpreter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ InterpretCommand(
326326
* The main function used for when reading commands from scripts.
327327
*/
328328
BOOL
329-
InterpretScript(
329+
InterpretLine(
330330
_In_ LPWSTR pszInputLine)
331331
{
332332
LPWSTR args_vector[MAX_ARGS_COUNT];

base/applications/network/netsh/netsh.c

Lines changed: 92 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ RunScript(
3232
/* Read and process the script */
3333
while (fgetws(tmp_string, MAX_STRING_SIZE, script) != NULL)
3434
{
35-
if (InterpretScript(tmp_string) == FALSE)
35+
if (InterpretLine(tmp_string) == FALSE)
3636
{
3737
fclose(script);
3838
return ERROR_SUCCESS; /* FIXME */
@@ -56,6 +56,7 @@ wmain(
5656
{
5757
LPCWSTR tmpBuffer = NULL;
5858
LPCWSTR pszFileName = NULL;
59+
LPCWSTR pszContext = NULL;
5960
int index;
6061
int result = EXIT_SUCCESS;
6162
BOOL bDone = FALSE;
@@ -70,120 +71,122 @@ wmain(
7071
CreateRootContext();
7172
LoadHelpers();
7273

73-
if (argc < 2)
74+
/* Process the command arguments */
75+
for (index = 1; index < argc; index++)
7476
{
75-
/* If there are no command arguments, then go straight to the interpreter */
76-
InterpretInteractive();
77-
}
78-
else
79-
{
80-
/* If there are command arguments, then process them */
81-
for (index = 1; index < argc; index++)
77+
if ((argv[index][0] == '/')||
78+
(argv[index][0] == '-'))
8279
{
83-
if ((argv[index][0] == '/')||
84-
(argv[index][0] == '-'))
85-
{
86-
tmpBuffer = argv[index] + 1;
87-
}
88-
else
80+
tmpBuffer = argv[index] + 1;
81+
}
82+
else
83+
{
84+
if (pszFileName != NULL)
8985
{
90-
if (pszFileName != NULL)
91-
{
92-
ConResPuts(StdOut, IDS_APP_USAGE);
93-
result = EXIT_FAILURE;
94-
goto done;
95-
}
96-
97-
/* Run a command from the command line */
98-
if (InterpretCommand((LPWSTR*)&argv[index], argc - index, &bDone) != ERROR_SUCCESS)
99-
result = EXIT_FAILURE;
86+
ConResPuts(StdOut, IDS_APP_USAGE);
87+
result = EXIT_FAILURE;
10088
goto done;
10189
}
10290

103-
if (_wcsicmp(tmpBuffer, L"?") == 0)
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)
98+
{
99+
/* Help option */
100+
ConResPuts(StdOut, IDS_APP_USAGE);
101+
result = EXIT_SUCCESS;
102+
goto done;
103+
}
104+
else if (_wcsicmp(tmpBuffer, L"a") == 0)
105+
{
106+
/* Aliasfile option */
107+
if ((index + 1) < argc)
104108
{
105-
/* Help option */
106-
ConResPuts(StdOut, IDS_APP_USAGE);
107-
result = EXIT_SUCCESS;
108-
goto done;
109+
index++;
110+
ConPuts(StdOut, L"\nThe -a option is not implemented yet\n");
111+
// aliasfile = argv[index];
109112
}
110-
else if (_wcsicmp(tmpBuffer, L"a") == 0)
113+
else
111114
{
112-
/* Aliasfile option */
113-
if ((index + 1) < argc)
114-
{
115-
index++;
116-
ConPuts(StdOut, L"\nThe -a option is not implemented yet\n");
117-
// aliasfile = argv[index];
118-
}
119-
else
120-
{
121-
ConResPuts(StdOut, IDS_APP_USAGE);
122-
result = EXIT_FAILURE;
123-
}
115+
ConResPuts(StdOut, IDS_APP_USAGE);
116+
result = EXIT_FAILURE;
124117
}
125-
else if (_wcsicmp(tmpBuffer, L"c") == 0)
118+
}
119+
else if (_wcsicmp(tmpBuffer, L"c") == 0)
120+
{
121+
/* Context option */
122+
if ((index + 1) < argc)
126123
{
127-
/* Context option */
128-
if ((index + 1) < argc)
129-
{
130-
index++;
131-
ConPuts(StdOut, L"\nThe -c option is not implemented yet\n");
132-
// context = argv[index];
133-
}
134-
else
135-
{
136-
ConResPuts(StdOut, IDS_APP_USAGE);
137-
result = EXIT_FAILURE;
138-
}
124+
index++;
125+
pszContext = argv[index];
139126
}
140-
else if (_wcsicmp(tmpBuffer, L"f") == 0)
127+
else
141128
{
142-
/* File option */
143-
if ((index + 1) < argc)
144-
{
145-
index++;
146-
pszFileName = argv[index];
147-
}
148-
else
149-
{
150-
ConResPuts(StdOut, IDS_APP_USAGE);
151-
result = EXIT_FAILURE;
152-
}
129+
ConResPuts(StdOut, IDS_APP_USAGE);
130+
result = EXIT_FAILURE;
153131
}
154-
else if (_wcsicmp(tmpBuffer, L"r") == 0)
132+
}
133+
else if (_wcsicmp(tmpBuffer, L"f") == 0)
134+
{
135+
/* File option */
136+
if ((index + 1) < argc)
155137
{
156-
/* Remote option */
157-
if ((index + 1) < argc)
158-
{
159-
index++;
160-
ConPuts(StdOut, L"\nThe -r option is not implemented yet\n");
161-
// remote = argv[index];
162-
}
163-
else
164-
{
165-
ConResPuts(StdOut, IDS_APP_USAGE);
166-
result = EXIT_FAILURE;
167-
}
138+
index++;
139+
pszFileName = argv[index];
168140
}
169141
else
170142
{
171-
/* Invalid command */
172-
ConResPrintf(StdOut, IDS_INVALID_COMMAND, argv[index]);
143+
ConResPuts(StdOut, IDS_APP_USAGE);
173144
result = EXIT_FAILURE;
174-
goto done;
175145
}
176146
}
177-
178-
/* Now we process the filename if it exists */
179-
if (pszFileName != NULL)
147+
else if (_wcsicmp(tmpBuffer, L"r") == 0)
180148
{
181-
if (RunScript(pszFileName) == FALSE)
149+
/* Remote option */
150+
if ((index + 1) < argc)
182151
{
152+
index++;
153+
ConPuts(StdOut, L"\nThe -r option is not implemented yet\n");
154+
// remote = argv[index];
155+
}
156+
else
157+
{
158+
ConResPuts(StdOut, IDS_APP_USAGE);
183159
result = EXIT_FAILURE;
184-
goto done;
185160
}
186161
}
162+
else
163+
{
164+
/* Invalid command */
165+
ConResPrintf(StdOut, IDS_INVALID_COMMAND, argv[index]);
166+
result = EXIT_FAILURE;
167+
goto done;
168+
}
169+
}
170+
171+
/* Set a context */
172+
if (pszContext)
173+
{
174+
if (InterpretLine((LPWSTR)pszContext) == FALSE)
175+
{
176+
result = EXIT_FAILURE;
177+
goto done;
178+
}
179+
}
180+
181+
/* Run a script or the interactive interpeter */
182+
if (pszFileName != NULL)
183+
{
184+
if (RunScript(pszFileName) == FALSE)
185+
result = EXIT_FAILURE;
186+
}
187+
else
188+
{
189+
InterpretInteractive();
187190
}
188191

189192
done:

base/applications/network/netsh/precomp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ ShowHelperCommand(
216216

217217
/* interpreter.c */
218218
BOOL
219-
InterpretScript(
219+
InterpretLine(
220220
_In_ LPWSTR pszFileName);
221221

222222
BOOL

0 commit comments

Comments
 (0)