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", cmdShare}, 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 PrintMessageStringV( 83 DWORD dwMessage, 84 ...) 85 { 86 PWSTR pBuffer; 87 va_list args = NULL; 88 89 va_start(args, dwMessage); 90 91 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE, 92 hModuleNetMsg, 93 dwMessage, 94 LANG_USER_DEFAULT, 95 (LPWSTR)&pBuffer, 96 0, 97 &args); 98 va_end(args); 99 100 if (pBuffer) 101 { 102 ConPuts(StdOut, pBuffer); 103 LocalFree(pBuffer); 104 pBuffer = NULL; 105 } 106 } 107 108 VOID 109 PrintMessageString( 110 DWORD dwMessage) 111 { 112 PWSTR pBuffer; 113 114 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE | 115 FORMAT_MESSAGE_IGNORE_INSERTS, 116 hModuleNetMsg, 117 dwMessage, 118 LANG_USER_DEFAULT, 119 (LPWSTR)&pBuffer, 120 0, 121 NULL); 122 if (pBuffer) 123 { 124 ConPuts(StdOut, pBuffer); 125 LocalFree(pBuffer); 126 pBuffer = NULL; 127 } 128 } 129 130 131 VOID 132 PrintPaddedMessageString( 133 DWORD dwMessage, 134 INT nPaddedLength) 135 { 136 PWSTR pBuffer; 137 DWORD dwLength; 138 139 dwLength = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE | 140 FORMAT_MESSAGE_IGNORE_INSERTS, 141 hModuleNetMsg, 142 dwMessage, 143 LANG_USER_DEFAULT, 144 (LPWSTR)&pBuffer, 145 0, 146 NULL); 147 if (pBuffer) 148 { 149 ConPuts(StdOut, pBuffer); 150 LocalFree(pBuffer); 151 pBuffer = NULL; 152 } 153 154 if (dwLength < (DWORD)nPaddedLength) 155 PrintPadding(L' ', (DWORD)nPaddedLength - dwLength); 156 } 157 158 159 VOID 160 PrintErrorMessage( 161 DWORD dwError) 162 { 163 WCHAR szErrorBuffer[16]; 164 PWSTR pBuffer; 165 PWSTR pErrorInserts[2] = {NULL, NULL}; 166 167 if (dwError >= MIN_LANMAN_MESSAGE_ID && dwError <= MAX_LANMAN_MESSAGE_ID) 168 { 169 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE | 170 FORMAT_MESSAGE_IGNORE_INSERTS, 171 hModuleNetMsg, 172 dwError, 173 LANG_USER_DEFAULT, 174 (LPWSTR)&pBuffer, 175 0, 176 NULL); 177 if (pBuffer) 178 { 179 ConPrintf(StdErr, L"%s\n", pBuffer); 180 LocalFree(pBuffer); 181 pBuffer = NULL; 182 } 183 } 184 else 185 { 186 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | 187 FORMAT_MESSAGE_IGNORE_INSERTS, 188 NULL, 189 dwError, 190 LANG_USER_DEFAULT, 191 (LPWSTR)&pBuffer, 192 0, 193 NULL); 194 if (pBuffer) 195 { 196 ConPrintf(StdErr, L"%s\n", pBuffer); 197 LocalFree(pBuffer); 198 pBuffer = NULL; 199 } 200 } 201 202 if (dwError != ERROR_SUCCESS) 203 { 204 /* Format insert for the 3514 message */ 205 swprintf(szErrorBuffer, L"%lu", dwError); 206 pErrorInserts[0] = szErrorBuffer; 207 208 /* Format and print the 3514 message */ 209 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE | 210 FORMAT_MESSAGE_ARGUMENT_ARRAY, 211 hModuleNetMsg, 212 3514, 213 LANG_USER_DEFAULT, 214 (LPWSTR)&pBuffer, 215 0, 216 (va_list *)pErrorInserts); 217 if (pBuffer) 218 { 219 ConPrintf(StdErr, L"%s\n", pBuffer); 220 LocalFree(pBuffer); 221 pBuffer = NULL; 222 } 223 } 224 } 225 226 227 VOID 228 PrintNetMessage( 229 DWORD dwMessage) 230 { 231 PWSTR pBuffer; 232 233 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | 234 FORMAT_MESSAGE_FROM_HMODULE | 235 FORMAT_MESSAGE_IGNORE_INSERTS, 236 GetModuleHandleW(NULL), 237 dwMessage, 238 LANG_USER_DEFAULT, 239 (LPWSTR)&pBuffer, 240 0, 241 NULL); 242 if (pBuffer) 243 { 244 ConPuts(StdOut, pBuffer); 245 ConPuts(StdOut, L"\n"); 246 LocalFree(pBuffer); 247 pBuffer = NULL; 248 } 249 } 250 251 252 VOID 253 ReadFromConsole( 254 LPWSTR lpInput, 255 DWORD dwLength, 256 BOOL bEcho) 257 { 258 DWORD dwOldMode; 259 DWORD dwRead = 0; 260 HANDLE hFile; 261 LPWSTR p; 262 PCHAR pBuf; 263 264 pBuf = HeapAlloc(GetProcessHeap(), 0, dwLength - 1); 265 ZeroMemory(lpInput, dwLength * sizeof(WCHAR)); 266 hFile = GetStdHandle(STD_INPUT_HANDLE); 267 GetConsoleMode(hFile, &dwOldMode); 268 269 SetConsoleMode(hFile, ENABLE_LINE_INPUT | (bEcho ? ENABLE_ECHO_INPUT : 0)); 270 271 ReadFile(hFile, (PVOID)pBuf, dwLength - 1, &dwRead, NULL); 272 273 MultiByteToWideChar(CP_OEMCP, 0, pBuf, dwRead, lpInput, dwLength - 1); 274 HeapFree(GetProcessHeap(), 0, pBuf); 275 276 for (p = lpInput; *p; p++) 277 { 278 if (*p == L'\x0d') 279 { 280 *p = L'\0'; 281 break; 282 } 283 } 284 285 SetConsoleMode(hFile, dwOldMode); 286 } 287 288 289 int wmain(int argc, WCHAR **argv) 290 { 291 WCHAR szDllBuffer[MAX_PATH]; 292 PCOMMAND cmdptr; 293 int nResult = 0; 294 BOOL bRun = FALSE; 295 296 /* Initialize the Console Standard Streams */ 297 ConInitStdStreams(); 298 299 /* Load netmsg.dll */ 300 GetSystemDirectoryW(szDllBuffer, ARRAYSIZE(szDllBuffer)); 301 wcscat(szDllBuffer, L"\\netmsg.dll"); 302 303 hModuleNetMsg = LoadLibrary(szDllBuffer); 304 if (hModuleNetMsg == NULL) 305 { 306 ConPrintf(StdErr, L"Failed to load netmsg.dll\n"); 307 return 1; 308 } 309 310 if (argc < 2) 311 { 312 nResult = 1; 313 goto done; 314 } 315 316 /* Scan the command table */ 317 for (cmdptr = cmds; cmdptr->name; cmdptr++) 318 { 319 if (_wcsicmp(argv[1], cmdptr->name) == 0) 320 { 321 nResult = cmdptr->func(argc, argv); 322 bRun = TRUE; 323 break; 324 } 325 } 326 327 done: 328 if (bRun == FALSE) 329 { 330 PrintMessageString(4381); 331 ConPuts(StdOut, L"\n"); 332 PrintNetMessage(MSG_NET_SYNTAX); 333 } 334 335 if (hModuleNetMsg != NULL) 336 FreeLibrary(hModuleNetMsg); 337 338 return nResult; 339 } 340 341 INT unimplemented(INT argc, WCHAR **argv) 342 { 343 ConPuts(StdOut, L"This command is not implemented yet\n"); 344 return 1; 345 } 346