* Sync up to trunk head (r65394).
[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 PrintToConsole(
89 LPWSTR lpFormat,
90 ...)
91 {
92 WCHAR szBuffer[MAX_BUFFER_SIZE];
93 va_list arg_ptr;
94
95 va_start(arg_ptr, lpFormat);
96 _vsnwprintf(szBuffer, MAX_BUFFER_SIZE, lpFormat, arg_ptr);
97 va_end(arg_ptr);
98
99 WriteToConsole(szBuffer);
100 }
101
102
103 VOID
104 WriteToConsole(
105 LPWSTR lpString)
106 {
107 CHAR szOemBuffer[MAX_BUFFER_SIZE * 2];
108 HANDLE hOutput;
109 DWORD dwLength;
110
111 dwLength = wcslen(lpString);
112
113 hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
114 if ((GetFileType(hOutput) & ~FILE_TYPE_REMOTE) == FILE_TYPE_CHAR)
115 {
116 WriteConsoleW(hOutput,
117 lpString,
118 dwLength,
119 &dwLength,
120 NULL);
121 }
122 else
123 {
124 dwLength = WideCharToMultiByte(CP_OEMCP,
125 0,
126 lpString,
127 dwLength,
128 szOemBuffer,
129 MAX_BUFFER_SIZE * 2,
130 NULL,
131 NULL);
132 WriteFile(hOutput,
133 szOemBuffer,
134 dwLength,
135 &dwLength,
136 NULL);
137 }
138 }
139
140
141 VOID
142 ReadFromConsole(
143 LPWSTR lpInput,
144 DWORD dwLength,
145 BOOL bEcho)
146 {
147 DWORD dwOldMode;
148 DWORD dwRead = 0;
149 HANDLE hFile;
150 LPWSTR p;
151 PCHAR pBuf;
152
153 pBuf = HeapAlloc(GetProcessHeap(), 0, dwLength - 1);
154 ZeroMemory(lpInput, dwLength * sizeof(WCHAR));
155 hFile = GetStdHandle(STD_INPUT_HANDLE);
156 GetConsoleMode(hFile, &dwOldMode);
157
158 SetConsoleMode(hFile, ENABLE_LINE_INPUT | (bEcho ? ENABLE_ECHO_INPUT : 0));
159
160 ReadFile(hFile, (PVOID)pBuf, dwLength - 1, &dwRead, NULL);
161
162 MultiByteToWideChar(CP_OEMCP, 0, pBuf, dwRead, lpInput, dwLength - 1);
163 HeapFree(GetProcessHeap(), 0, pBuf);
164
165 for (p = lpInput; *p; p++)
166 {
167 if (*p == L'\x0d')
168 {
169 *p = L'\0';
170 break;
171 }
172 }
173
174 SetConsoleMode(hFile, dwOldMode);
175 }
176
177
178 int wmain(int argc, WCHAR **argv)
179 {
180 PCOMMAND cmdptr;
181
182 if (argc < 2)
183 {
184 PrintResourceString(IDS_NET_SYNTAX);
185 return 1;
186 }
187
188 /* Scan the command table */
189 for (cmdptr = cmds; cmdptr->name; cmdptr++)
190 {
191 if (_wcsicmp(argv[1], cmdptr->name) == 0)
192 {
193 return cmdptr->func(argc, argv);
194 }
195 }
196
197 PrintResourceString(IDS_NET_SYNTAX);
198
199 return 1;
200 }
201
202 INT unimplemented(INT argc, WCHAR **argv)
203 {
204 puts("This command is not implemented yet");
205 return 1;
206 }