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