[NET] Fix some 'net help' and 'net user' issues.
[reactos.git] / base / applications / network / net / main.c
index 2061465..773c25f 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS net command
- * FILE:
+ * FILE:            base/applications/network/net/main.c
  * PURPOSE:
  *
  * PROGRAMMERS:     Magnus Olsen (greatlord@reactos.org)
@@ -21,11 +21,11 @@ typedef struct _COMMAND
 COMMAND cmds[] =
 {
     {L"accounts",   cmdAccounts},
-    {L"computer",   unimplemented},
-    {L"config",     unimplemented},
+    {L"computer",   cmdComputer},
+    {L"config",     cmdConfig},
     {L"continue",   cmdContinue},
     {L"file",       unimplemented},
-    {L"group",      unimplemented},
+    {L"group",      cmdGroup},
     {L"help",       cmdHelp},
     {L"helpmsg",    cmdHelpMsg},
     {L"localgroup", cmdLocalGroup},
@@ -36,104 +36,135 @@ COMMAND cmds[] =
     {L"session",    unimplemented},
     {L"share",      unimplemented},
     {L"start",      cmdStart},
-    {L"statistics", unimplemented},
+    {L"statistics", cmdStatistics},
     {L"stop",       cmdStop},
     {L"time",       unimplemented},
-    {L"use",        unimplemented},
+    {L"use",        cmdUse},
     {L"user",       cmdUser},
     {L"view",       unimplemented},
     {NULL,          NULL}
 };
 
+HMODULE hModuleNetMsg = NULL;
+
 
 VOID
-PrintResourceString(
-    INT resID,
-    ...)
+PrintPaddedResourceString(
+    UINT uID,
+    INT nPaddedLength)
 {
-    WCHAR szMsgBuffer[MAX_BUFFER_SIZE];
-    WCHAR szOutBuffer[MAX_BUFFER_SIZE];
-    va_list arg_ptr;
-
-    va_start(arg_ptr, resID);
-    LoadStringW(GetModuleHandle(NULL), resID, szMsgBuffer, MAX_BUFFER_SIZE);
-    _vsnwprintf(szOutBuffer, MAX_BUFFER_SIZE, szMsgBuffer, arg_ptr);
-    va_end(arg_ptr);
+    INT nLength;
 
-    WriteToConsole(szOutBuffer);
+    nLength = ConResPuts(StdOut, uID);
+    if (nLength < nPaddedLength)
+        PrintPadding(L' ', nPaddedLength - nLength);
 }
 
 
 VOID
-PrintPaddedResourceString(
-    INT resID,
+PrintPadding(
+    WCHAR chr,
     INT nPaddedLength)
 {
+    INT i;
     WCHAR szMsgBuffer[MAX_BUFFER_SIZE];
-    INT nLength, i;
 
-    nLength = LoadStringW(GetModuleHandle(NULL), resID, szMsgBuffer, MAX_BUFFER_SIZE);
-    if (nLength < nPaddedLength)
-    {
-        for (i = nLength; i < nPaddedLength; i++)
-            szMsgBuffer[i] = L' ';
-        szMsgBuffer[nPaddedLength] = UNICODE_NULL;
-    }
+    for (i = 0; i < nPaddedLength; i++)
+         szMsgBuffer[i] = chr;
+    szMsgBuffer[nPaddedLength] = UNICODE_NULL;
 
-    WriteToConsole(szMsgBuffer);
+    ConPuts(StdOut, szMsgBuffer);
 }
 
 
 VOID
-PrintToConsole(
-    LPWSTR lpFormat,
-    ...)
+PrintErrorMessage(
+    DWORD dwError)
 {
-    WCHAR szBuffer[MAX_BUFFER_SIZE];
-    va_list arg_ptr;
+    WCHAR szErrorBuffer[16];
+    PWSTR pBuffer;
+    PWSTR pErrorInserts[2] = {NULL, NULL};
 
-    va_start(arg_ptr, lpFormat);
-    _vsnwprintf(szBuffer, MAX_BUFFER_SIZE, lpFormat, arg_ptr);
-    va_end(arg_ptr);
+    if (dwError >= MIN_LANMAN_MESSAGE_ID && dwError <= MAX_LANMAN_MESSAGE_ID)
+    {
+        FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE |
+                       FORMAT_MESSAGE_IGNORE_INSERTS,
+                       hModuleNetMsg,
+                       dwError,
+                       LANG_USER_DEFAULT,
+                       (LPWSTR)&pBuffer,
+                       0,
+                       NULL);
+        if (pBuffer)
+        {
+            ConPrintf(StdErr, L"%s\n", pBuffer);
+            LocalFree(pBuffer);
+            pBuffer = NULL;
+        }
+    }
+    else
+    {
+        FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
+                       FORMAT_MESSAGE_IGNORE_INSERTS,
+                       NULL,
+                       dwError,
+                       LANG_USER_DEFAULT,
+                       (LPWSTR)&pBuffer,
+                       0,
+                       NULL);
+        if (pBuffer)
+        {
+            ConPrintf(StdErr, L"%s\n", pBuffer);
+            LocalFree(pBuffer);
+            pBuffer = NULL;
+        }
+    }
 
-    WriteToConsole(szBuffer);
+    if (dwError != ERROR_SUCCESS)
+    {
+        /* Format insert for the 3514 message */
+        swprintf(szErrorBuffer, L"%lu", dwError);
+        pErrorInserts[0] = szErrorBuffer;
+
+        /* Format and print the 3514 message */
+        FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE |
+                       FORMAT_MESSAGE_ARGUMENT_ARRAY,
+                       hModuleNetMsg,
+                       3514,
+                       LANG_USER_DEFAULT,
+                       (LPWSTR)&pBuffer,
+                       0,
+                       (va_list *)pErrorInserts);
+        if (pBuffer)
+        {
+            ConPrintf(StdErr, L"%s\n", pBuffer);
+            LocalFree(pBuffer);
+            pBuffer = NULL;
+        }
+    }
 }
 
 
 VOID
-WriteToConsole(
-    LPWSTR lpString)
+PrintNetMessage(
+    DWORD dwMessage)
 {
-    CHAR szOemBuffer[MAX_BUFFER_SIZE * 2];
-    HANDLE hOutput;
-    DWORD dwLength;
-
-    dwLength = wcslen(lpString);
-
-    hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
-    if ((GetFileType(hOutput) & ~FILE_TYPE_REMOTE) == FILE_TYPE_CHAR)
+    PWSTR pBuffer;
+
+    FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
+                   FORMAT_MESSAGE_FROM_HMODULE |
+                   FORMAT_MESSAGE_IGNORE_INSERTS,
+                   GetModuleHandleW(NULL),
+                   dwMessage,
+                   LANG_USER_DEFAULT,
+                   (LPWSTR)&pBuffer,
+                   0,
+                   NULL);
+    if (pBuffer)
     {
-        WriteConsoleW(hOutput,
-                      lpString,
-                      dwLength,
-                      &dwLength,
-                      NULL);
-    }
-    else
-    {
-        dwLength = WideCharToMultiByte(CP_OEMCP,
-                                       0,
-                                       lpString,
-                                       dwLength,
-                                       szOemBuffer,
-                                       MAX_BUFFER_SIZE * 2,
-                                       NULL,
-                                       NULL);
-        WriteFile(hOutput,
-                  szOemBuffer,
-                  dwLength,
-                  &dwLength,
-                  NULL);
+        ConPrintf(StdOut, L"%s\n", pBuffer);
+        LocalFree(pBuffer);
+        pBuffer = NULL;
     }
 }
 
@@ -177,30 +208,57 @@ ReadFromConsole(
 
 int wmain(int argc, WCHAR **argv)
 {
+    WCHAR szDllBuffer[MAX_PATH];
     PCOMMAND cmdptr;
+    int nResult = 0;
+    BOOL bRun = FALSE;
 
-    if (argc < 2)
+    /* Initialize the Console Standard Streams */
+    ConInitStdStreams();
+
+    /* Load netmsg.dll */
+    GetSystemDirectoryW(szDllBuffer, ARRAYSIZE(szDllBuffer));
+    wcscat(szDllBuffer, L"\\netmsg.dll");
+
+    hModuleNetMsg = LoadLibrary(szDllBuffer);
+    if (hModuleNetMsg == NULL)
     {
-        PrintResourceString(IDS_NET_SYNTAX);
+        ConPrintf(StdErr, L"Failed to load netmsg.dll\n");
         return 1;
     }
 
+    if (argc < 2)
+    {
+        nResult = 1;
+        goto done;
+    }
+
     /* Scan the command table */
     for (cmdptr = cmds; cmdptr->name; cmdptr++)
     {
         if (_wcsicmp(argv[1], cmdptr->name) == 0)
         {
-            return cmdptr->func(argc, argv);
+            nResult = cmdptr->func(argc, argv);
+            bRun = TRUE;
+            break;
         }
     }
 
-    PrintResourceString(IDS_NET_SYNTAX);
+done:
+    if (bRun == FALSE)
+    {
+        ConResPuts(StdOut, IDS_GENERIC_SYNTAX);
+        PrintNetMessage(MSG_NET_SYNTAX);
+    }
 
-    return 1;
+    if (hModuleNetMsg != NULL)
+        FreeLibrary(hModuleNetMsg);
+
+    return nResult;
 }
 
 INT unimplemented(INT argc, WCHAR **argv)
 {
-    puts("This command is not implemented yet");
+    ConPuts(StdOut, L"This command is not implemented yet\n");
     return 1;
 }