[KERNEL32][CONSRV]
[reactos.git] / reactos / dll / win32 / kernel32 / client / console / history.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: dll/win32/kernel32/client/console/history.c
5 * PURPOSE: Win32 Console Client history functions
6 * PROGRAMMERS: Jeffrey Morlan
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #include <k32.h>
12
13 #define NDEBUG
14 #include <debug.h>
15
16
17 /* PRIVATE FUNCTIONS **********************************************************/
18
19 /* Get the size needed to copy a string to a capture buffer, including alignment */
20 static ULONG
21 IntStringSize(LPCVOID String,
22 BOOL Unicode)
23 {
24 ULONG Size = (Unicode ? wcslen(String) : strlen(String)) * sizeof(WCHAR);
25 return (Size + 3) & -4;
26 }
27
28
29 /* Copy a string to a capture buffer */
30 static VOID
31 IntCaptureMessageString(PCSR_CAPTURE_BUFFER CaptureBuffer,
32 LPCVOID String,
33 BOOL Unicode,
34 PUNICODE_STRING RequestString)
35 {
36 ULONG Size;
37 if (Unicode)
38 {
39 Size = wcslen(String) * sizeof(WCHAR);
40 CsrCaptureMessageBuffer(CaptureBuffer, (PVOID)String, Size, (PVOID *)&RequestString->Buffer);
41 }
42 else
43 {
44 Size = strlen(String);
45 CsrAllocateMessagePointer(CaptureBuffer, Size * sizeof(WCHAR), (PVOID *)&RequestString->Buffer);
46 Size = MultiByteToWideChar(CP_ACP, 0, String, Size, RequestString->Buffer, Size * sizeof(WCHAR))
47 * sizeof(WCHAR);
48 }
49 RequestString->Length = RequestString->MaximumLength = (USHORT)Size;
50 }
51
52
53 static BOOL
54 IntExpungeConsoleCommandHistory(LPCVOID lpExeName, BOOL bUnicode)
55 {
56 NTSTATUS Status;
57 CONSOLE_API_MESSAGE ApiMessage;
58 PCONSOLE_EXPUNGECOMMANDHISTORY ExpungeCommandHistoryRequest = &ApiMessage.Data.ExpungeCommandHistoryRequest;
59 PCSR_CAPTURE_BUFFER CaptureBuffer;
60
61 if (lpExeName == NULL || !(bUnicode ? *(PWCHAR)lpExeName : *(PCHAR)lpExeName))
62 {
63 SetLastError(ERROR_INVALID_PARAMETER);
64 return FALSE;
65 }
66
67 CaptureBuffer = CsrAllocateCaptureBuffer(1, IntStringSize(lpExeName, bUnicode));
68 if (!CaptureBuffer)
69 {
70 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
71 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
72 return FALSE;
73 }
74
75 IntCaptureMessageString(CaptureBuffer, lpExeName, bUnicode,
76 &ExpungeCommandHistoryRequest->ExeName);
77
78 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
79 CaptureBuffer,
80 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepExpungeCommandHistory),
81 sizeof(CONSOLE_EXPUNGECOMMANDHISTORY));
82
83 CsrFreeCaptureBuffer(CaptureBuffer);
84
85 if (!NT_SUCCESS(Status))
86 {
87 BaseSetLastNTError(Status);
88 return FALSE;
89 }
90
91 return TRUE;
92 }
93
94
95 static DWORD
96 IntGetConsoleCommandHistory(LPVOID lpHistory, DWORD cbHistory, LPCVOID lpExeName, BOOL bUnicode)
97 {
98 NTSTATUS Status;
99 CONSOLE_API_MESSAGE ApiMessage;
100 PCONSOLE_GETCOMMANDHISTORY GetCommandHistoryRequest = &ApiMessage.Data.GetCommandHistoryRequest;
101 PCSR_CAPTURE_BUFFER CaptureBuffer;
102 DWORD HistoryLength = cbHistory * (bUnicode ? 1 : sizeof(WCHAR));
103
104 if (lpExeName == NULL || !(bUnicode ? *(PWCHAR)lpExeName : *(PCHAR)lpExeName))
105 {
106 SetLastError(ERROR_INVALID_PARAMETER);
107 return 0;
108 }
109
110 CaptureBuffer = CsrAllocateCaptureBuffer(2, IntStringSize(lpExeName, bUnicode) +
111 HistoryLength);
112 if (!CaptureBuffer)
113 {
114 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
115 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
116 return 0;
117 }
118
119 IntCaptureMessageString(CaptureBuffer, lpExeName, bUnicode,
120 &GetCommandHistoryRequest->ExeName);
121 GetCommandHistoryRequest->Length = HistoryLength;
122 CsrAllocateMessagePointer(CaptureBuffer, HistoryLength,
123 (PVOID*)&GetCommandHistoryRequest->History);
124
125 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
126 CaptureBuffer,
127 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetCommandHistory),
128 sizeof(CONSOLE_GETCOMMANDHISTORY));
129 if (!NT_SUCCESS(Status))
130 {
131 CsrFreeCaptureBuffer(CaptureBuffer);
132 BaseSetLastNTError(Status);
133 return 0;
134 }
135
136 if (bUnicode)
137 {
138 memcpy(lpHistory,
139 GetCommandHistoryRequest->History,
140 GetCommandHistoryRequest->Length);
141 }
142 else
143 {
144 WideCharToMultiByte(CP_ACP, 0,
145 GetCommandHistoryRequest->History,
146 GetCommandHistoryRequest->Length / sizeof(WCHAR),
147 lpHistory,
148 cbHistory,
149 NULL, NULL);
150 }
151
152 CsrFreeCaptureBuffer(CaptureBuffer);
153
154 return GetCommandHistoryRequest->Length;
155 }
156
157
158 static DWORD
159 IntGetConsoleCommandHistoryLength(LPCVOID lpExeName, BOOL bUnicode)
160 {
161 NTSTATUS Status;
162 CONSOLE_API_MESSAGE ApiMessage;
163 PCONSOLE_GETCOMMANDHISTORYLENGTH GetCommandHistoryLengthRequest = &ApiMessage.Data.GetCommandHistoryLengthRequest;
164 PCSR_CAPTURE_BUFFER CaptureBuffer;
165
166 if (lpExeName == NULL || !(bUnicode ? *(PWCHAR)lpExeName : *(PCHAR)lpExeName))
167 {
168 SetLastError(ERROR_INVALID_PARAMETER);
169 return 0;
170 }
171
172 CaptureBuffer = CsrAllocateCaptureBuffer(1, IntStringSize(lpExeName, bUnicode));
173 if (!CaptureBuffer)
174 {
175 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
176 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
177 return 0;
178 }
179
180 IntCaptureMessageString(CaptureBuffer, lpExeName, bUnicode,
181 &GetCommandHistoryLengthRequest->ExeName);
182
183 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
184 CaptureBuffer,
185 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetCommandHistoryLength),
186 sizeof(CONSOLE_GETCOMMANDHISTORYLENGTH));
187
188 CsrFreeCaptureBuffer(CaptureBuffer);
189
190 if (!NT_SUCCESS(Status))
191 {
192 BaseSetLastNTError(Status);
193 return 0;
194 }
195
196 return GetCommandHistoryLengthRequest->Length;
197 }
198
199
200 static BOOL
201 IntSetConsoleNumberOfCommands(DWORD dwNumCommands,
202 LPCVOID lpExeName,
203 BOOL bUnicode)
204 {
205 NTSTATUS Status;
206 CONSOLE_API_MESSAGE ApiMessage;
207 PCONSOLE_SETHISTORYNUMBERCOMMANDS SetHistoryNumberCommandsRequest = &ApiMessage.Data.SetHistoryNumberCommandsRequest;
208 PCSR_CAPTURE_BUFFER CaptureBuffer;
209
210 if (lpExeName == NULL || !(bUnicode ? *(PWCHAR)lpExeName : *(PCHAR)lpExeName))
211 {
212 SetLastError(ERROR_INVALID_PARAMETER);
213 return FALSE;
214 }
215
216 CaptureBuffer = CsrAllocateCaptureBuffer(1, IntStringSize(lpExeName, bUnicode));
217 if (!CaptureBuffer)
218 {
219 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
220 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
221 return FALSE;
222 }
223
224 IntCaptureMessageString(CaptureBuffer, lpExeName, bUnicode,
225 &SetHistoryNumberCommandsRequest->ExeName);
226 SetHistoryNumberCommandsRequest->NumCommands = dwNumCommands;
227
228 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
229 CaptureBuffer,
230 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepSetNumberOfCommands),
231 sizeof(CONSOLE_SETHISTORYNUMBERCOMMANDS));
232
233 CsrFreeCaptureBuffer(CaptureBuffer);
234
235 if (!NT_SUCCESS(Status))
236 {
237 BaseSetLastNTError(Status);
238 return FALSE;
239 }
240
241 return TRUE;
242 }
243
244
245 /* FUNCTIONS ******************************************************************/
246
247 /*
248 * @implemented (Undocumented)
249 */
250 BOOL
251 WINAPI
252 ExpungeConsoleCommandHistoryW(LPCWSTR lpExeName)
253 {
254 return IntExpungeConsoleCommandHistory(lpExeName, TRUE);
255 }
256
257
258 /*
259 * @implemented (Undocumented)
260 */
261 BOOL
262 WINAPI
263 ExpungeConsoleCommandHistoryA(LPCSTR lpExeName)
264 {
265 return IntExpungeConsoleCommandHistory(lpExeName, FALSE);
266 }
267
268
269 /*
270 * @implemented (Undocumented)
271 */
272 DWORD
273 WINAPI
274 GetConsoleCommandHistoryW(LPWSTR lpHistory,
275 DWORD cbHistory,
276 LPCWSTR lpExeName)
277 {
278 return IntGetConsoleCommandHistory(lpHistory, cbHistory, lpExeName, TRUE);
279 }
280
281
282 /*
283 * @implemented (Undocumented)
284 */
285 DWORD
286 WINAPI
287 GetConsoleCommandHistoryA(LPSTR lpHistory,
288 DWORD cbHistory,
289 LPCSTR lpExeName)
290 {
291 return IntGetConsoleCommandHistory(lpHistory, cbHistory, lpExeName, FALSE);
292 }
293
294
295 /*
296 * @implemented (Undocumented)
297 */
298 DWORD
299 WINAPI
300 GetConsoleCommandHistoryLengthW(LPCWSTR lpExeName)
301 {
302 return IntGetConsoleCommandHistoryLength(lpExeName, TRUE);
303 }
304
305
306 /*
307 * @implemented (Undocumented)
308 */
309 DWORD
310 WINAPI
311 GetConsoleCommandHistoryLengthA(LPCSTR lpExeName)
312 {
313 return IntGetConsoleCommandHistoryLength(lpExeName, FALSE) / sizeof(WCHAR);
314 }
315
316
317 /*
318 * @implemented (Undocumented)
319 */
320 BOOL
321 WINAPI
322 SetConsoleNumberOfCommandsW(DWORD dwNumCommands,
323 LPCSTR lpExeName)
324 {
325 return IntSetConsoleNumberOfCommands(dwNumCommands, lpExeName, TRUE);
326 }
327
328
329 /*
330 * @implemented (Undocumented)
331 */
332 BOOL
333 WINAPI
334 SetConsoleNumberOfCommandsA(DWORD dwNumCommands,
335 LPCWSTR lpExeName)
336 {
337 return IntSetConsoleNumberOfCommands(dwNumCommands, lpExeName, FALSE);
338 }
339
340
341 /*
342 * @unimplemented
343 */
344 BOOL
345 WINAPI
346 SetConsoleCommandHistoryMode(IN DWORD dwMode)
347 {
348 STUB;
349 return FALSE;
350 }
351
352 /* EOF */