ReactOS  0.4.15-dev-4853-g3a72a52
main.c
Go to the documentation of this file.
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 {
17  INT (*func)(INT, WCHAR**);
18 // VOID (*help)(INT, WCHAR**);
19 } COMMAND, *PCOMMAND;
20 
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"pause", cmdPause},
33  {L"session", unimplemented},
34  {L"share", cmdShare},
35  {L"start", cmdStart},
36  {L"statistics", cmdStatistics},
37  {L"stop", cmdStop},
38  {L"syntax", cmdSyntax},
39  {L"time", unimplemented},
40  {L"use", cmdUse},
41  {L"user", cmdUser},
42  {L"view", unimplemented},
43  {NULL, NULL}
44 };
45 
47 
48 
49 VOID
51  UINT uID,
52  INT nPaddedLength)
53 {
54  INT nLength;
55 
56  nLength = ConResPuts(StdOut, uID);
57  if (nLength < nPaddedLength)
58  PrintPadding(L' ', nPaddedLength - nLength);
59 }
60 
61 
62 VOID
64  WCHAR chr,
65  INT nPaddedLength)
66 {
67  INT i;
68  WCHAR szMsgBuffer[MAX_BUFFER_SIZE];
69 
70  for (i = 0; i < nPaddedLength; i++)
71  szMsgBuffer[i] = chr;
72  szMsgBuffer[nPaddedLength] = UNICODE_NULL;
73 
74  ConPuts(StdOut, szMsgBuffer);
75 }
76 
77 
78 DWORD
80  DWORD dwMessage)
81 {
82  switch (dwMessage)
83  {
84  case NERR_Success:
85  return 3500; // APPERR_3500
86  case ERROR_MORE_DATA:
87  return 3513; // APPERR_3513
88  }
89  return dwMessage;
90 }
91 
92 VOID
94  DWORD dwMessage,
95  ...)
96 {
97  PWSTR pBuffer;
98  va_list args = NULL;
99 
100  va_start(args, dwMessage);
101 
104  dwMessage,
106  (LPWSTR)&pBuffer,
107  0,
108  &args);
109  va_end(args);
110 
111  if (pBuffer)
112  {
115  pBuffer = NULL;
116  }
117 }
118 
119 VOID
121  DWORD dwMessage)
122 {
123  PWSTR pBuffer;
124 
128  dwMessage,
130  (LPWSTR)&pBuffer,
131  0,
132  NULL);
133  if (pBuffer)
134  {
137  pBuffer = NULL;
138  }
139 }
140 
141 
142 VOID
144  DWORD dwMessage,
145  INT nPaddedLength)
146 {
147  PWSTR pBuffer;
148  DWORD dwLength;
149 
153  dwMessage,
155  (LPWSTR)&pBuffer,
156  0,
157  NULL);
158  if (pBuffer)
159  {
162  pBuffer = NULL;
163  }
164 
165  if (dwLength < (DWORD)nPaddedLength)
166  PrintPadding(L' ', (DWORD)nPaddedLength - dwLength);
167 }
168 
169 
170 VOID
172  DWORD dwError)
173 {
174  WCHAR szErrorBuffer[16];
175  PWSTR pBuffer;
176  PWSTR pErrorInserts[2] = {NULL, NULL};
177 
178  if (dwError >= MIN_LANMAN_MESSAGE_ID && dwError <= MAX_LANMAN_MESSAGE_ID)
179  {
183  dwError,
185  (LPWSTR)&pBuffer,
186  0,
187  NULL);
188  if (pBuffer)
189  {
190  ConPrintf(StdErr, L"%s\n", pBuffer);
192  pBuffer = NULL;
193  }
194  }
195  else
196  {
199  NULL,
200  dwError,
202  (LPWSTR)&pBuffer,
203  0,
204  NULL);
205  if (pBuffer)
206  {
207  ConPrintf(StdErr, L"%s\n", pBuffer);
209  pBuffer = NULL;
210  }
211  }
212 
213  if (dwError != ERROR_SUCCESS)
214  {
215  /* Format insert for the 3514 message */
216  swprintf(szErrorBuffer, L"%lu", dwError);
217  pErrorInserts[0] = szErrorBuffer;
218 
219  /* Format and print the 3514 message */
223  3514,
225  (LPWSTR)&pBuffer,
226  0,
227  (va_list *)pErrorInserts);
228  if (pBuffer)
229  {
230  ConPrintf(StdErr, L"%s\n", pBuffer);
232  pBuffer = NULL;
233  }
234  }
235 }
236 
237 
238 VOID
240  DWORD dwMessage)
241 {
242  PWSTR pBuffer;
243 
248  dwMessage,
250  (LPWSTR)&pBuffer,
251  0,
252  NULL);
253  if (pBuffer)
254  {
256  ConPuts(StdOut, L"\n");
258  pBuffer = NULL;
259  }
260 }
261 
262 
263 VOID
265  LPWSTR lpInput,
266  DWORD dwLength,
267  BOOL bEcho)
268 {
269  DWORD dwOldMode;
270  DWORD dwRead = 0;
271  HANDLE hFile;
272  LPWSTR p;
273  PCHAR pBuf;
274 
275  pBuf = HeapAlloc(GetProcessHeap(), 0, dwLength - 1);
276  ZeroMemory(lpInput, dwLength * sizeof(WCHAR));
278  GetConsoleMode(hFile, &dwOldMode);
279 
281 
282  ReadFile(hFile, (PVOID)pBuf, dwLength - 1, &dwRead, NULL);
283 
284  MultiByteToWideChar(CP_OEMCP, 0, pBuf, dwRead, lpInput, dwLength - 1);
285  HeapFree(GetProcessHeap(), 0, pBuf);
286 
287  for (p = lpInput; *p; p++)
288  {
289  if (*p == L'\x0d')
290  {
291  *p = L'\0';
292  break;
293  }
294  }
295 
296  SetConsoleMode(hFile, dwOldMode);
297 }
298 
299 
300 int wmain(int argc, WCHAR **argv)
301 {
302  WCHAR szDllBuffer[MAX_PATH];
303  PCOMMAND cmdptr;
304  int nResult = 0;
305  BOOL bRun = FALSE;
306 
307  /* Initialize the Console Standard Streams */
309 
310  /* Load netmsg.dll */
311  GetSystemDirectoryW(szDllBuffer, ARRAYSIZE(szDllBuffer));
312  wcscat(szDllBuffer, L"\\netmsg.dll");
313 
314  hModuleNetMsg = LoadLibrary(szDllBuffer);
315  if (hModuleNetMsg == NULL)
316  {
317  ConPrintf(StdErr, L"Failed to load netmsg.dll\n");
318  return 1;
319  }
320 
321  if (argc < 2)
322  {
323  nResult = 1;
324  goto done;
325  }
326 
327  /* Scan the command table */
328  for (cmdptr = cmds; cmdptr->name; cmdptr++)
329  {
330  if (_wcsicmp(argv[1], cmdptr->name) == 0)
331  {
332  nResult = cmdptr->func(argc, argv);
333  bRun = TRUE;
334  break;
335  }
336  }
337 
338 done:
339  if (bRun == FALSE)
340  {
341  PrintMessageString(4381);
342  ConPuts(StdOut, L"\n");
343  PrintNetMessage(MSG_NET_SYNTAX);
344  }
345 
346  if (hModuleNetMsg != NULL)
348 
349  return nResult;
350 }
351 
353 {
354  ConPuts(StdOut, L"This command is not implemented yet\n");
355  return 1;
356 }
signed char * PCHAR
Definition: retypes.h:7
struct _COMMAND COMMAND
GLenum func
Definition: glext.h:6028
static int argc
Definition: ServiceArgs.c:12
VOID PrintMessageStringV(DWORD dwMessage,...)
Definition: main.c:93
#define ERROR_SUCCESS
Definition: deptool.c:10
#define LoadLibrary
Definition: winbase.h:3723
INT cmdSyntax(INT argc, WCHAR **argv)
Definition: cmdHelp.c:180
#define TRUE
Definition: types.h:120
uint16_t * PWSTR
Definition: typedefs.h:56
#define FORMAT_MESSAGE_ARGUMENT_ARRAY
Definition: winbase.h:421
BOOL WINAPI GetConsoleMode(HANDLE hConsoleHandle, LPDWORD lpMode)
Definition: console.c:1569
#define INT
Definition: polytest.cpp:20
_In_ DWORD nLength
Definition: wincon.h:473
COMMAND cmds[]
Definition: main.c:21
INT unimplemented(INT argc, WCHAR **argv)
Definition: main.c:352
BOOL bEcho
Definition: batch.c:73
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
#define ZeroMemory
Definition: winbase.h:1667
INT cmdStart(INT argc, WCHAR **argv)
Definition: cmdStart.c:172
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:203
INT(* func)(INT, WCHAR **)
Definition: main.c:17
VOID PrintNetMessage(DWORD dwMessage)
Definition: main.c:239
#define argv
Definition: mplay32.c:18
DWORD WINAPI FormatMessageW(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, LPWSTR lpBuffer, DWORD nSize, __ms_va_list *args)
Definition: format_msg.c:583
int wmain(int argc, WCHAR *argv[])
Definition: main.c:269
Definition: match.c:390
int32_t INT
Definition: typedefs.h:58
void ConPuts(FILE *fp, LPCWSTR psz)
Definition: fc.c:16
VOID ReadFromConsole(LPWSTR lpInput, DWORD dwLength, BOOL bEcho)
Definition: main.c:264
#define NERR_Success
Definition: lmerr.h:5
#define ENABLE_ECHO_INPUT
Definition: wincon.h:80
INT cmdShare(INT argc, WCHAR **argv)
Definition: cmdShare.c:94
#define L(x)
Definition: ntvdm.h:50
#define va_end(ap)
Definition: acmsvcex.h:90
#define FALSE
Definition: types.h:117
#define UNICODE_NULL
unsigned int BOOL
Definition: ntddk_ex.h:94
struct _COMMAND * PCOMMAND
#define MIN_LANMAN_MESSAGE_ID
Definition: lmcons.h:10
BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleMode(HANDLE hConsoleHandle, DWORD dwMode)
Definition: console.c:1606
VOID PrintErrorMessage(DWORD dwError)
Definition: main.c:171
INT cmdComputer(INT argc, WCHAR **argv)
Definition: cmdComputer.c:11
#define FORMAT_MESSAGE_ALLOCATE_BUFFER
Definition: winbase.h:416
INT cmdConfig(INT argc, WCHAR **argv)
Definition: cmdConfig.c:165
PVOID pBuffer
INT cmdHelp(INT argc, WCHAR **argv)
Definition: cmdHelp.c:12
char * va_list
Definition: acmsvcex.h:78
void ConResPuts(FILE *fp, UINT nID)
Definition: fc.c:27
INT cmdHelpMsg(INT argc, WCHAR **argv)
Definition: cmdHelpMsg.c:14
#define STD_INPUT_HANDLE
Definition: winbase.h:264
#define FORMAT_MESSAGE_FROM_SYSTEM
Definition: winbase.h:420
#define FORMAT_MESSAGE_FROM_HMODULE
Definition: winbase.h:419
#define FreeLibrary(x)
Definition: compat.h:607
#define GetProcessHeap()
Definition: compat.h:595
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
#define MAX_LANMAN_MESSAGE_ID
Definition: lmcons.h:11
#define MAX_BUFFER_SIZE
Definition: main.c:12
__wchar_t WCHAR
Definition: xmlstorage.h:180
static DWORD DWORD * dwLength
Definition: fusion.c:85
#define MAX_PATH
Definition: compat.h:34
INT cmdAccounts(INT argc, WCHAR **argv)
Definition: cmdAccounts.c:13
unsigned long DWORD
Definition: ntddk_ex.h:95
#define ENABLE_LINE_INPUT
Definition: wincon.h:79
DWORD TranslateAppMessage(DWORD dwMessage)
Definition: main.c:79
va_start(ap, x)
INT cmdContinue(INT argc, WCHAR **argv)
Definition: cmdContinue.c:12
INT cmdUse(INT argc, WCHAR **argv)
Definition: cmdUse.c:104
INT cmdUser(INT argc, WCHAR **argv)
Definition: cmdUser.c:1148
INT cmdStop(INT argc, WCHAR **argv)
Definition: cmdStop.c:12
int _cdecl swprintf(const WCHAR *,...)
_In_ HANDLE hFile
Definition: mswsock.h:90
#define ERROR_MORE_DATA
Definition: dderror.h:13
void ConPrintf(FILE *fp, LPCWSTR psz,...)
Definition: fc.c:20
#define FORMAT_MESSAGE_IGNORE_INSERTS
Definition: winbase.h:417
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
VOID PrintPaddedMessageString(DWORD dwMessage, INT nPaddedLength)
Definition: main.c:143
VOID PrintPaddedResourceString(UINT uID, INT nPaddedLength)
Definition: main.c:50
VOID PrintMessageString(DWORD dwMessage)
Definition: main.c:120
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
INT cmdStatistics(INT argc, WCHAR **argv)
UINT WINAPI GetSystemDirectoryW(OUT LPWSTR lpBuffer, IN UINT uSize)
Definition: path.c:2313
#define ReadFile(a, b, c, d, e)
Definition: compat.h:601
_CRTIMP wchar_t *__cdecl wcscat(_Inout_updates_z_(_String_length_(_Dest)+_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
unsigned int UINT
Definition: ndis.h:50
INT cmdLocalGroup(INT argc, WCHAR **argv)
#define NULL
Definition: types.h:112
VOID PrintPadding(WCHAR chr, INT nPaddedLength)
Definition: main.c:63
#define MultiByteToWideChar
Definition: compat.h:110
#define StdOut
Definition: fc.c:14
#define ConInitStdStreams()
Definition: fc.c:13
HMODULE WINAPI GetModuleHandleW(LPCWSTR lpModuleName)
Definition: loader.c:838
INT cmdPause(INT argc, WCHAR **argv)
Definition: cmdPause.c:12
HMODULE hModuleNetMsg
Definition: main.c:46
GLfloat GLfloat p
Definition: glext.h:8902
WCHAR * LPWSTR
Definition: xmlstorage.h:184
INT cmdGroup(INT argc, WCHAR **argv)
Definition: cmdGroup.c:154
Definition: main.c:14
#define CP_OEMCP
Definition: winnls.h:231
#define HeapFree(x, y, z)
Definition: compat.h:594
_Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
#define LANG_USER_DEFAULT
Definition: tnerror.cpp:50
WCHAR * name
Definition: main.c:16
#define StdErr
Definition: fc.c:15