Sync with trunk r65656.
[reactos.git] / base / applications / network / net / main.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS net command
4 * FILE:
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", unimplemented},
26 {L"continue", cmdContinue},
27 {L"file", unimplemented},
28 {L"group", unimplemented},
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", unimplemented},
40 {L"stop", cmdStop},
41 {L"time", unimplemented},
42 {L"use", unimplemented},
43 {L"user", cmdUser},
44 {L"view", unimplemented},
45 {NULL, NULL}
46 };
47
48
49 VOID
50 PrintResourceString(
51 INT resID,
52 ...)
53 {
54 WCHAR szMsgBuffer[MAX_BUFFER_SIZE];
55 WCHAR szOutBuffer[MAX_BUFFER_SIZE];
56 va_list arg_ptr;
57
58 va_start(arg_ptr, resID);
59 LoadStringW(GetModuleHandle(NULL), resID, szMsgBuffer, MAX_BUFFER_SIZE);
60 _vsnwprintf(szOutBuffer, MAX_BUFFER_SIZE, szMsgBuffer, arg_ptr);
61 va_end(arg_ptr);
62
63 WriteToConsole(szOutBuffer);
64 }
65
66
67 VOID
68 PrintPaddedResourceString(
69 INT resID,
70 INT nPaddedLength)
71 {
72 WCHAR szMsgBuffer[MAX_BUFFER_SIZE];
73 INT nLength, i;
74
75 nLength = LoadStringW(GetModuleHandle(NULL), resID, szMsgBuffer, MAX_BUFFER_SIZE);
76 if (nLength < nPaddedLength)
77 {
78 for (i = nLength; i < nPaddedLength; i++)
79 szMsgBuffer[i] = L' ';
80 szMsgBuffer[nPaddedLength] = UNICODE_NULL;
81 }
82
83 WriteToConsole(szMsgBuffer);
84 }
85
86
87 VOID
88 PrintPadding(
89 WCHAR chr,
90 INT nPaddedLength)
91 {
92 WCHAR szMsgBuffer[MAX_BUFFER_SIZE];
93 INT i;
94
95 for (i = 0; i < nPaddedLength; i++)
96 szMsgBuffer[i] = chr;
97 szMsgBuffer[nPaddedLength] = UNICODE_NULL;
98
99 WriteToConsole(szMsgBuffer);
100 }
101
102
103 VOID
104 PrintToConsole(
105 LPWSTR lpFormat,
106 ...)
107 {
108 WCHAR szBuffer[MAX_BUFFER_SIZE];
109 va_list arg_ptr;
110
111 va_start(arg_ptr, lpFormat);
112 _vsnwprintf(szBuffer, MAX_BUFFER_SIZE, lpFormat, arg_ptr);
113 va_end(arg_ptr);
114
115 WriteToConsole(szBuffer);
116 }
117
118
119 VOID
120 WriteToConsole(
121 LPWSTR lpString)
122 {
123 CHAR szOemBuffer[MAX_BUFFER_SIZE * 2];
124 HANDLE hOutput;
125 DWORD dwLength;
126
127 dwLength = wcslen(lpString);
128
129 hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
130 if ((GetFileType(hOutput) & ~FILE_TYPE_REMOTE) == FILE_TYPE_CHAR)
131 {
132 WriteConsoleW(hOutput,
133 lpString,
134 dwLength,
135 &dwLength,
136 NULL);
137 }
138 else
139 {
140 dwLength = WideCharToMultiByte(CP_OEMCP,
141 0,
142 lpString,
143 dwLength,
144 szOemBuffer,
145 MAX_BUFFER_SIZE * 2,
146 NULL,
147 NULL);
148 WriteFile(hOutput,
149 szOemBuffer,
150 dwLength,
151 &dwLength,
152 NULL);
153 }
154 }
155
156
157 VOID
158 ReadFromConsole(
159 LPWSTR lpInput,
160 DWORD dwLength,
161 BOOL bEcho)
162 {
163 DWORD dwOldMode;
164 DWORD dwRead = 0;
165 HANDLE hFile;
166 LPWSTR p;
167 PCHAR pBuf;
168
169 pBuf = HeapAlloc(GetProcessHeap(), 0, dwLength - 1);
170 ZeroMemory(lpInput, dwLength * sizeof(WCHAR));
171 hFile = GetStdHandle(STD_INPUT_HANDLE);
172 GetConsoleMode(hFile, &dwOldMode);
173
174 SetConsoleMode(hFile, ENABLE_LINE_INPUT | (bEcho ? ENABLE_ECHO_INPUT : 0));
175
176 ReadFile(hFile, (PVOID)pBuf, dwLength - 1, &dwRead, NULL);
177
178 MultiByteToWideChar(CP_OEMCP, 0, pBuf, dwRead, lpInput, dwLength - 1);
179 HeapFree(GetProcessHeap(), 0, pBuf);
180
181 for (p = lpInput; *p; p++)
182 {
183 if (*p == L'\x0d')
184 {
185 *p = L'\0';
186 break;
187 }
188 }
189
190 SetConsoleMode(hFile, dwOldMode);
191 }
192
193
194 int wmain(int argc, WCHAR **argv)
195 {
196 PCOMMAND cmdptr;
197
198 if (argc < 2)
199 {
200 PrintResourceString(IDS_NET_SYNTAX);
201 return 1;
202 }
203
204 /* Scan the command table */
205 for (cmdptr = cmds; cmdptr->name; cmdptr++)
206 {
207 if (_wcsicmp(argv[1], cmdptr->name) == 0)
208 {
209 return cmdptr->func(argc, argv);
210 }
211 }
212
213 PrintResourceString(IDS_NET_SYNTAX);
214
215 return 1;
216 }
217
218 INT unimplemented(INT argc, WCHAR **argv)
219 {
220 puts("This command is not implemented yet");
221 return 1;
222 }