4707c148cd6b1b5bb941e3c92782f0849845d02a
[reactos.git] / base / applications / network / net / main.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS net command
4 * FILE: base/applications/network/net/main.c
5 * PURPOSE:
6 *
7 * PROGRAMMERS: Magnus Olsen (greatlord@reactos.org)
8 */
9
10 #include "net.h"
11
12 #define MAX_BUFFER_SIZE 4096
13
14 typedef struct _COMMAND
15 {
16 WCHAR *name;
17 INT (*func)(INT, WCHAR**);
18 // VOID (*help)(INT, WCHAR**);
19 } COMMAND, *PCOMMAND;
20
21 COMMAND cmds[] =
22 {
23 {L"accounts", cmdAccounts},
24 {L"computer", cmdComputer},
25 {L"config", cmdConfig},
26 {L"continue", cmdContinue},
27 {L"file", unimplemented},
28 {L"group", cmdGroup},
29 {L"help", cmdHelp},
30 {L"helpmsg", cmdHelpMsg},
31 {L"localgroup", cmdLocalGroup},
32 {L"name", unimplemented},
33 {L"pause", cmdPause},
34 {L"print", unimplemented},
35 {L"send", unimplemented},
36 {L"session", unimplemented},
37 {L"share", unimplemented},
38 {L"start", cmdStart},
39 {L"statistics", cmdStatistics},
40 {L"stop", cmdStop},
41 {L"syntax", cmdSyntax},
42 {L"time", unimplemented},
43 {L"use", cmdUse},
44 {L"user", cmdUser},
45 {L"view", unimplemented},
46 {NULL, NULL}
47 };
48
49 HMODULE hModuleNetMsg = NULL;
50
51
52 VOID
53 PrintPaddedResourceString(
54 UINT uID,
55 INT nPaddedLength)
56 {
57 INT nLength;
58
59 nLength = ConResPuts(StdOut, uID);
60 if (nLength < nPaddedLength)
61 PrintPadding(L' ', nPaddedLength - nLength);
62 }
63
64
65 VOID
66 PrintPadding(
67 WCHAR chr,
68 INT nPaddedLength)
69 {
70 INT i;
71 WCHAR szMsgBuffer[MAX_BUFFER_SIZE];
72
73 for (i = 0; i < nPaddedLength; i++)
74 szMsgBuffer[i] = chr;
75 szMsgBuffer[nPaddedLength] = UNICODE_NULL;
76
77 ConPuts(StdOut, szMsgBuffer);
78 }
79
80
81 VOID
82 PrintErrorMessage(
83 DWORD dwError)
84 {
85 WCHAR szErrorBuffer[16];
86 PWSTR pBuffer;
87 PWSTR pErrorInserts[2] = {NULL, NULL};
88
89 if (dwError >= MIN_LANMAN_MESSAGE_ID && dwError <= MAX_LANMAN_MESSAGE_ID)
90 {
91 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE |
92 FORMAT_MESSAGE_IGNORE_INSERTS,
93 hModuleNetMsg,
94 dwError,
95 LANG_USER_DEFAULT,
96 (LPWSTR)&pBuffer,
97 0,
98 NULL);
99 if (pBuffer)
100 {
101 ConPrintf(StdErr, L"%s\n", pBuffer);
102 LocalFree(pBuffer);
103 pBuffer = NULL;
104 }
105 }
106 else
107 {
108 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
109 FORMAT_MESSAGE_IGNORE_INSERTS,
110 NULL,
111 dwError,
112 LANG_USER_DEFAULT,
113 (LPWSTR)&pBuffer,
114 0,
115 NULL);
116 if (pBuffer)
117 {
118 ConPrintf(StdErr, L"%s\n", pBuffer);
119 LocalFree(pBuffer);
120 pBuffer = NULL;
121 }
122 }
123
124 if (dwError != ERROR_SUCCESS)
125 {
126 /* Format insert for the 3514 message */
127 swprintf(szErrorBuffer, L"%lu", dwError);
128 pErrorInserts[0] = szErrorBuffer;
129
130 /* Format and print the 3514 message */
131 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE |
132 FORMAT_MESSAGE_ARGUMENT_ARRAY,
133 hModuleNetMsg,
134 3514,
135 LANG_USER_DEFAULT,
136 (LPWSTR)&pBuffer,
137 0,
138 (va_list *)pErrorInserts);
139 if (pBuffer)
140 {
141 ConPrintf(StdErr, L"%s\n", pBuffer);
142 LocalFree(pBuffer);
143 pBuffer = NULL;
144 }
145 }
146 }
147
148
149 VOID
150 PrintNetMessage(
151 DWORD dwMessage)
152 {
153 PWSTR pBuffer;
154
155 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
156 FORMAT_MESSAGE_FROM_HMODULE |
157 FORMAT_MESSAGE_IGNORE_INSERTS,
158 GetModuleHandleW(NULL),
159 dwMessage,
160 LANG_USER_DEFAULT,
161 (LPWSTR)&pBuffer,
162 0,
163 NULL);
164 if (pBuffer)
165 {
166 ConPuts(StdOut, pBuffer);
167 ConPuts(StdOut, L"\n");
168 LocalFree(pBuffer);
169 pBuffer = NULL;
170 }
171 }
172
173
174 VOID
175 ReadFromConsole(
176 LPWSTR lpInput,
177 DWORD dwLength,
178 BOOL bEcho)
179 {
180 DWORD dwOldMode;
181 DWORD dwRead = 0;
182 HANDLE hFile;
183 LPWSTR p;
184 PCHAR pBuf;
185
186 pBuf = HeapAlloc(GetProcessHeap(), 0, dwLength - 1);
187 ZeroMemory(lpInput, dwLength * sizeof(WCHAR));
188 hFile = GetStdHandle(STD_INPUT_HANDLE);
189 GetConsoleMode(hFile, &dwOldMode);
190
191 SetConsoleMode(hFile, ENABLE_LINE_INPUT | (bEcho ? ENABLE_ECHO_INPUT : 0));
192
193 ReadFile(hFile, (PVOID)pBuf, dwLength - 1, &dwRead, NULL);
194
195 MultiByteToWideChar(CP_OEMCP, 0, pBuf, dwRead, lpInput, dwLength - 1);
196 HeapFree(GetProcessHeap(), 0, pBuf);
197
198 for (p = lpInput; *p; p++)
199 {
200 if (*p == L'\x0d')
201 {
202 *p = L'\0';
203 break;
204 }
205 }
206
207 SetConsoleMode(hFile, dwOldMode);
208 }
209
210
211 int wmain(int argc, WCHAR **argv)
212 {
213 WCHAR szDllBuffer[MAX_PATH];
214 PCOMMAND cmdptr;
215 int nResult = 0;
216 BOOL bRun = FALSE;
217
218 /* Initialize the Console Standard Streams */
219 ConInitStdStreams();
220
221 /* Load netmsg.dll */
222 GetSystemDirectoryW(szDllBuffer, ARRAYSIZE(szDllBuffer));
223 wcscat(szDllBuffer, L"\\netmsg.dll");
224
225 hModuleNetMsg = LoadLibrary(szDllBuffer);
226 if (hModuleNetMsg == NULL)
227 {
228 ConPrintf(StdErr, L"Failed to load netmsg.dll\n");
229 return 1;
230 }
231
232 if (argc < 2)
233 {
234 nResult = 1;
235 goto done;
236 }
237
238 /* Scan the command table */
239 for (cmdptr = cmds; cmdptr->name; cmdptr++)
240 {
241 if (_wcsicmp(argv[1], cmdptr->name) == 0)
242 {
243 nResult = cmdptr->func(argc, argv);
244 bRun = TRUE;
245 break;
246 }
247 }
248
249 done:
250 if (bRun == FALSE)
251 {
252 ConResPuts(StdOut, IDS_GENERIC_SYNTAX);
253 PrintNetMessage(MSG_NET_SYNTAX);
254 }
255
256 if (hModuleNetMsg != NULL)
257 FreeLibrary(hModuleNetMsg);
258
259 return nResult;
260 }
261
262 INT unimplemented(INT argc, WCHAR **argv)
263 {
264 ConPuts(StdOut, L"This command is not implemented yet\n");
265 return 1;
266 }