[NET] Load netmsg.dll right from the start and print some messages using netmsg.dll...
[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", unimplemented},
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"time", unimplemented},
42 {L"use", cmdUse},
43 {L"user", cmdUser},
44 {L"view", unimplemented},
45 {NULL, NULL}
46 };
47
48 HMODULE hModuleNetMsg = NULL;
49
50
51 VOID
52 PrintPaddedResourceString(
53 UINT uID,
54 INT nPaddedLength)
55 {
56 INT nLength;
57
58 nLength = ConResPuts(StdOut, uID);
59 if (nLength < nPaddedLength)
60 PrintPadding(L' ', nPaddedLength - nLength);
61 }
62
63
64 VOID
65 PrintPadding(
66 WCHAR chr,
67 INT nPaddedLength)
68 {
69 INT i;
70 WCHAR szMsgBuffer[MAX_BUFFER_SIZE];
71
72 for (i = 0; i < nPaddedLength; i++)
73 szMsgBuffer[i] = chr;
74 szMsgBuffer[nPaddedLength] = UNICODE_NULL;
75
76 ConPuts(StdOut, szMsgBuffer);
77 }
78
79
80 VOID
81 PrintErrorMessage(
82 DWORD dwError)
83 {
84 WCHAR szErrorBuffer[16];
85 PWSTR pBuffer;
86 PWSTR pErrorInserts[2] = {NULL, NULL};
87
88 if (dwError >= MIN_LANMAN_MESSAGE_ID && dwError <= MAX_LANMAN_MESSAGE_ID)
89 {
90 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE |
91 FORMAT_MESSAGE_IGNORE_INSERTS,
92 hModuleNetMsg,
93 dwError,
94 LANG_USER_DEFAULT,
95 (LPWSTR)&pBuffer,
96 0,
97 NULL);
98 if (pBuffer)
99 {
100 ConPrintf(StdErr, L"%s\n", pBuffer);
101 LocalFree(pBuffer);
102 pBuffer = NULL;
103 }
104 }
105 else
106 {
107 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
108 FORMAT_MESSAGE_IGNORE_INSERTS,
109 NULL,
110 dwError,
111 LANG_USER_DEFAULT,
112 (LPWSTR)&pBuffer,
113 0,
114 NULL);
115 if (pBuffer)
116 {
117 ConPrintf(StdErr, L"%s\n", pBuffer);
118 LocalFree(pBuffer);
119 pBuffer = NULL;
120 }
121 }
122
123 if (dwError != ERROR_SUCCESS)
124 {
125 /* Format insert for the 3514 message */
126 swprintf(szErrorBuffer, L"%lu", dwError);
127 pErrorInserts[0] = szErrorBuffer;
128
129 /* Format and print the 3514 message */
130 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE |
131 FORMAT_MESSAGE_ARGUMENT_ARRAY,
132 hModuleNetMsg,
133 3514,
134 LANG_USER_DEFAULT,
135 (LPWSTR)&pBuffer,
136 0,
137 (va_list *)pErrorInserts);
138 if (pBuffer)
139 {
140 ConPrintf(StdErr, L"%s\n", pBuffer);
141 LocalFree(pBuffer);
142 pBuffer = NULL;
143 }
144 }
145 }
146
147
148 VOID
149 ReadFromConsole(
150 LPWSTR lpInput,
151 DWORD dwLength,
152 BOOL bEcho)
153 {
154 DWORD dwOldMode;
155 DWORD dwRead = 0;
156 HANDLE hFile;
157 LPWSTR p;
158 PCHAR pBuf;
159
160 pBuf = HeapAlloc(GetProcessHeap(), 0, dwLength - 1);
161 ZeroMemory(lpInput, dwLength * sizeof(WCHAR));
162 hFile = GetStdHandle(STD_INPUT_HANDLE);
163 GetConsoleMode(hFile, &dwOldMode);
164
165 SetConsoleMode(hFile, ENABLE_LINE_INPUT | (bEcho ? ENABLE_ECHO_INPUT : 0));
166
167 ReadFile(hFile, (PVOID)pBuf, dwLength - 1, &dwRead, NULL);
168
169 MultiByteToWideChar(CP_OEMCP, 0, pBuf, dwRead, lpInput, dwLength - 1);
170 HeapFree(GetProcessHeap(), 0, pBuf);
171
172 for (p = lpInput; *p; p++)
173 {
174 if (*p == L'\x0d')
175 {
176 *p = L'\0';
177 break;
178 }
179 }
180
181 SetConsoleMode(hFile, dwOldMode);
182 }
183
184
185 int wmain(int argc, WCHAR **argv)
186 {
187 WCHAR szDllBuffer[MAX_PATH];
188 PCOMMAND cmdptr;
189 int nResult = 0;
190 BOOL bRun = FALSE;
191
192 /* Initialize the Console Standard Streams */
193 ConInitStdStreams();
194
195 /* Load netmsg.dll */
196 GetSystemDirectoryW(szDllBuffer, ARRAYSIZE(szDllBuffer));
197 wcscat(szDllBuffer, L"\\netmsg.dll");
198
199 hModuleNetMsg = LoadLibrary(szDllBuffer);
200 if (hModuleNetMsg == NULL)
201 {
202 ConPrintf(StdErr, L"Failed to load netmsg.dll\n");
203 return 1;
204 }
205
206 if (argc < 2)
207 {
208 nResult = 1;
209 goto done;
210 }
211
212 /* Scan the command table */
213 for (cmdptr = cmds; cmdptr->name; cmdptr++)
214 {
215 if (_wcsicmp(argv[1], cmdptr->name) == 0)
216 {
217 nResult = cmdptr->func(argc, argv);
218 bRun = TRUE;
219 break;
220 }
221 }
222
223 done:
224 if (bRun == FALSE)
225 ConResPuts(StdOut, IDS_NET_SYNTAX);
226
227 if (hModuleNetMsg != NULL)
228 FreeLibrary(hModuleNetMsg);
229
230 return nResult;
231 }
232
233 INT unimplemented(INT argc, WCHAR **argv)
234 {
235 ConPuts(StdOut, L"This command is not implemented yet\n");
236 return 1;
237 }