xref: /reactos/base/applications/network/net/main.c (revision c3483339)
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"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 PrintNetMessage(
150     DWORD dwMessage)
151 {
152     PWSTR pBuffer;
153 
154     FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
155                    FORMAT_MESSAGE_FROM_HMODULE |
156                    FORMAT_MESSAGE_IGNORE_INSERTS,
157                    GetModuleHandleW(NULL),
158                    dwMessage,
159                    LANG_USER_DEFAULT,
160                    (LPWSTR)&pBuffer,
161                    0,
162                    NULL);
163     if (pBuffer)
164     {
165         ConPrintf(StdOut, L"%s\n", pBuffer);
166         LocalFree(pBuffer);
167         pBuffer = NULL;
168     }
169 }
170 
171 
172 VOID
173 ReadFromConsole(
174     LPWSTR lpInput,
175     DWORD dwLength,
176     BOOL bEcho)
177 {
178     DWORD dwOldMode;
179     DWORD dwRead = 0;
180     HANDLE hFile;
181     LPWSTR p;
182     PCHAR pBuf;
183 
184     pBuf = HeapAlloc(GetProcessHeap(), 0, dwLength - 1);
185     ZeroMemory(lpInput, dwLength * sizeof(WCHAR));
186     hFile = GetStdHandle(STD_INPUT_HANDLE);
187     GetConsoleMode(hFile, &dwOldMode);
188 
189     SetConsoleMode(hFile, ENABLE_LINE_INPUT | (bEcho ? ENABLE_ECHO_INPUT : 0));
190 
191     ReadFile(hFile, (PVOID)pBuf, dwLength - 1, &dwRead, NULL);
192 
193     MultiByteToWideChar(CP_OEMCP, 0, pBuf, dwRead, lpInput, dwLength - 1);
194     HeapFree(GetProcessHeap(), 0, pBuf);
195 
196     for (p = lpInput; *p; p++)
197     {
198         if (*p == L'\x0d')
199         {
200             *p = L'\0';
201             break;
202         }
203     }
204 
205     SetConsoleMode(hFile, dwOldMode);
206 }
207 
208 
209 int wmain(int argc, WCHAR **argv)
210 {
211     WCHAR szDllBuffer[MAX_PATH];
212     PCOMMAND cmdptr;
213     int nResult = 0;
214     BOOL bRun = FALSE;
215 
216     /* Initialize the Console Standard Streams */
217     ConInitStdStreams();
218 
219     /* Load netmsg.dll */
220     GetSystemDirectoryW(szDllBuffer, ARRAYSIZE(szDllBuffer));
221     wcscat(szDllBuffer, L"\\netmsg.dll");
222 
223     hModuleNetMsg = LoadLibrary(szDllBuffer);
224     if (hModuleNetMsg == NULL)
225     {
226         ConPrintf(StdErr, L"Failed to load netmsg.dll\n");
227         return 1;
228     }
229 
230     if (argc < 2)
231     {
232         nResult = 1;
233         goto done;
234     }
235 
236     /* Scan the command table */
237     for (cmdptr = cmds; cmdptr->name; cmdptr++)
238     {
239         if (_wcsicmp(argv[1], cmdptr->name) == 0)
240         {
241             nResult = cmdptr->func(argc, argv);
242             bRun = TRUE;
243             break;
244         }
245     }
246 
247 done:
248     if (bRun == FALSE)
249         PrintNetMessage(MSG_NET_SYNTAX);
250 
251     if (hModuleNetMsg != NULL)
252         FreeLibrary(hModuleNetMsg);
253 
254     return nResult;
255 }
256 
257 INT unimplemented(INT argc, WCHAR **argv)
258 {
259     ConPuts(StdOut, L"This command is not implemented yet\n");
260     return 1;
261 }
262