[KERNEL32][CONSRV]
[reactos.git] / 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 #if 0
20 /* Get the size needed to copy a string to a capture buffer, including alignment */
21 static ULONG
22 IntStringSize(LPCVOID String,
23 BOOL Unicode)
24 {
25 ULONG Size = (Unicode ? wcslen(String) : strlen(String)) * sizeof(WCHAR);
26 return (Size + 3) & ~3;
27 }
28
29
30 /* Copy a string to a capture buffer */
31 static VOID
32 IntCaptureMessageString(PCSR_CAPTURE_BUFFER CaptureBuffer,
33 LPCVOID String,
34 BOOL Unicode,
35 PUNICODE_STRING RequestString)
36 {
37 ULONG Size;
38 if (Unicode)
39 {
40 Size = wcslen(String) * sizeof(WCHAR);
41 CsrCaptureMessageBuffer(CaptureBuffer, (PVOID)String, Size, (PVOID *)&RequestString->Buffer);
42 }
43 else
44 {
45 Size = strlen(String);
46 CsrAllocateMessagePointer(CaptureBuffer, Size * sizeof(WCHAR), (PVOID *)&RequestString->Buffer);
47 Size = MultiByteToWideChar(CP_ACP, 0, String, Size, RequestString->Buffer, Size * sizeof(WCHAR))
48 * sizeof(WCHAR);
49 }
50 RequestString->Length = RequestString->MaximumLength = (USHORT)Size;
51 }
52 #endif
53
54 static VOID
55 IntExpungeConsoleCommandHistory(LPCVOID lpExeName, BOOLEAN bUnicode)
56 {
57 CONSOLE_API_MESSAGE ApiMessage;
58 PCONSOLE_EXPUNGECOMMANDHISTORY ExpungeCommandHistoryRequest = &ApiMessage.Data.ExpungeCommandHistoryRequest;
59 PCSR_CAPTURE_BUFFER CaptureBuffer;
60
61 USHORT NumChars = (USHORT)(lpExeName ? (bUnicode ? wcslen(lpExeName) : strlen(lpExeName)) : 0);
62
63 if (lpExeName == NULL || NumChars == 0)
64 {
65 SetLastError(ERROR_INVALID_PARAMETER);
66 return;
67 }
68
69 ExpungeCommandHistoryRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
70 ExpungeCommandHistoryRequest->ExeLength = NumChars * (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
71 ExpungeCommandHistoryRequest->Unicode =
72 ExpungeCommandHistoryRequest->Unicode2 = bUnicode;
73
74 // CaptureBuffer = CsrAllocateCaptureBuffer(1, IntStringSize(lpExeName, bUnicode));
75 CaptureBuffer = CsrAllocateCaptureBuffer(1, ExpungeCommandHistoryRequest->ExeLength);
76 if (!CaptureBuffer)
77 {
78 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
79 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
80 return;
81 }
82
83 // IntCaptureMessageString(CaptureBuffer, lpExeName, bUnicode,
84 // &ExpungeCommandHistoryRequest->ExeName);
85 CsrCaptureMessageBuffer(CaptureBuffer,
86 (PVOID)lpExeName,
87 ExpungeCommandHistoryRequest->ExeLength,
88 (PVOID)&ExpungeCommandHistoryRequest->ExeName);
89
90 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
91 CaptureBuffer,
92 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepExpungeCommandHistory),
93 sizeof(*ExpungeCommandHistoryRequest));
94
95 CsrFreeCaptureBuffer(CaptureBuffer);
96
97 if (!NT_SUCCESS(ApiMessage.Status))
98 BaseSetLastNTError(ApiMessage.Status);
99 }
100
101
102 static DWORD
103 IntGetConsoleCommandHistory(LPVOID lpHistory, DWORD cbHistory, LPCVOID lpExeName, BOOLEAN bUnicode)
104 {
105 CONSOLE_API_MESSAGE ApiMessage;
106 PCONSOLE_GETCOMMANDHISTORY GetCommandHistoryRequest = &ApiMessage.Data.GetCommandHistoryRequest;
107 PCSR_CAPTURE_BUFFER CaptureBuffer;
108
109 USHORT NumChars = (USHORT)(lpExeName ? (bUnicode ? wcslen(lpExeName) : strlen(lpExeName)) : 0);
110
111 if (lpExeName == NULL || NumChars == 0)
112 {
113 SetLastError(ERROR_INVALID_PARAMETER);
114 return 0;
115 }
116
117 GetCommandHistoryRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
118 GetCommandHistoryRequest->HistoryLength = cbHistory;
119 GetCommandHistoryRequest->ExeLength = NumChars * (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
120 GetCommandHistoryRequest->Unicode =
121 GetCommandHistoryRequest->Unicode2 = bUnicode;
122
123 // CaptureBuffer = CsrAllocateCaptureBuffer(2, IntStringSize(lpExeName, bUnicode) +
124 // HistoryLength);
125 CaptureBuffer = CsrAllocateCaptureBuffer(2, GetCommandHistoryRequest->ExeLength +
126 GetCommandHistoryRequest->HistoryLength);
127 if (!CaptureBuffer)
128 {
129 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
130 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
131 return 0;
132 }
133
134 // IntCaptureMessageString(CaptureBuffer, lpExeName, bUnicode,
135 // &GetCommandHistoryRequest->ExeName);
136 CsrCaptureMessageBuffer(CaptureBuffer,
137 (PVOID)lpExeName,
138 GetCommandHistoryRequest->ExeLength,
139 (PVOID)&GetCommandHistoryRequest->ExeName);
140
141 // CsrAllocateMessagePointer(CaptureBuffer, HistoryLength,
142 CsrAllocateMessagePointer(CaptureBuffer, GetCommandHistoryRequest->HistoryLength,
143 (PVOID*)&GetCommandHistoryRequest->History);
144
145 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
146 CaptureBuffer,
147 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetCommandHistory),
148 sizeof(*GetCommandHistoryRequest));
149 if (!NT_SUCCESS(ApiMessage.Status))
150 {
151 CsrFreeCaptureBuffer(CaptureBuffer);
152 BaseSetLastNTError(ApiMessage.Status);
153 return 0;
154 }
155
156 RtlCopyMemory(lpHistory,
157 GetCommandHistoryRequest->History,
158 GetCommandHistoryRequest->HistoryLength);
159
160 CsrFreeCaptureBuffer(CaptureBuffer);
161
162 return GetCommandHistoryRequest->HistoryLength;
163 }
164
165
166 static DWORD
167 IntGetConsoleCommandHistoryLength(LPCVOID lpExeName, BOOL bUnicode)
168 {
169 CONSOLE_API_MESSAGE ApiMessage;
170 PCONSOLE_GETCOMMANDHISTORYLENGTH GetCommandHistoryLengthRequest = &ApiMessage.Data.GetCommandHistoryLengthRequest;
171 PCSR_CAPTURE_BUFFER CaptureBuffer;
172
173 USHORT NumChars = (USHORT)(lpExeName ? (bUnicode ? wcslen(lpExeName) : strlen(lpExeName)) : 0);
174
175 if (lpExeName == NULL || NumChars == 0)
176 {
177 SetLastError(ERROR_INVALID_PARAMETER);
178 return 0;
179 }
180
181 GetCommandHistoryLengthRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
182 GetCommandHistoryLengthRequest->ExeLength = NumChars * (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
183 GetCommandHistoryLengthRequest->Unicode =
184 GetCommandHistoryLengthRequest->Unicode2 = bUnicode;
185
186 // CaptureBuffer = CsrAllocateCaptureBuffer(1, IntStringSize(lpExeName, bUnicode));
187 CaptureBuffer = CsrAllocateCaptureBuffer(1, GetCommandHistoryLengthRequest->ExeLength);
188 if (!CaptureBuffer)
189 {
190 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
191 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
192 return 0;
193 }
194
195 // IntCaptureMessageString(CaptureBuffer, lpExeName, bUnicode,
196 // &GetCommandHistoryLengthRequest->ExeName);
197 CsrCaptureMessageBuffer(CaptureBuffer,
198 (PVOID)lpExeName,
199 GetCommandHistoryLengthRequest->ExeLength,
200 (PVOID)&GetCommandHistoryLengthRequest->ExeName);
201
202 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
203 CaptureBuffer,
204 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetCommandHistoryLength),
205 sizeof(*GetCommandHistoryLengthRequest));
206
207 CsrFreeCaptureBuffer(CaptureBuffer);
208
209 if (!NT_SUCCESS(ApiMessage.Status))
210 {
211 BaseSetLastNTError(ApiMessage.Status);
212 return 0;
213 }
214
215 return GetCommandHistoryLengthRequest->HistoryLength;
216 }
217
218
219 static BOOL
220 IntSetConsoleNumberOfCommands(DWORD dwNumCommands,
221 LPCVOID lpExeName,
222 BOOLEAN bUnicode)
223 {
224 CONSOLE_API_MESSAGE ApiMessage;
225 PCONSOLE_SETHISTORYNUMBERCOMMANDS SetHistoryNumberCommandsRequest = &ApiMessage.Data.SetHistoryNumberCommandsRequest;
226 PCSR_CAPTURE_BUFFER CaptureBuffer;
227
228 USHORT NumChars = (USHORT)(lpExeName ? (bUnicode ? wcslen(lpExeName) : strlen(lpExeName)) : 0);
229
230 if (lpExeName == NULL || NumChars == 0)
231 {
232 SetLastError(ERROR_INVALID_PARAMETER);
233 return FALSE;
234 }
235
236 SetHistoryNumberCommandsRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
237 SetHistoryNumberCommandsRequest->NumCommands = dwNumCommands;
238 SetHistoryNumberCommandsRequest->ExeLength = NumChars * (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
239 SetHistoryNumberCommandsRequest->Unicode =
240 SetHistoryNumberCommandsRequest->Unicode2 = bUnicode;
241
242 // CaptureBuffer = CsrAllocateCaptureBuffer(1, IntStringSize(lpExeName, bUnicode));
243 CaptureBuffer = CsrAllocateCaptureBuffer(1, SetHistoryNumberCommandsRequest->ExeLength);
244 if (!CaptureBuffer)
245 {
246 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
247 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
248 return FALSE;
249 }
250
251 // IntCaptureMessageString(CaptureBuffer, lpExeName, bUnicode,
252 // &SetHistoryNumberCommandsRequest->ExeName);
253 CsrCaptureMessageBuffer(CaptureBuffer,
254 (PVOID)lpExeName,
255 SetHistoryNumberCommandsRequest->ExeLength,
256 (PVOID)&SetHistoryNumberCommandsRequest->ExeName);
257
258 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
259 CaptureBuffer,
260 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepSetNumberOfCommands),
261 sizeof(*SetHistoryNumberCommandsRequest));
262
263 CsrFreeCaptureBuffer(CaptureBuffer);
264
265 if (!NT_SUCCESS(ApiMessage.Status))
266 {
267 BaseSetLastNTError(ApiMessage.Status);
268 return FALSE;
269 }
270
271 return TRUE;
272 }
273
274
275 /* FUNCTIONS ******************************************************************/
276
277 /*
278 * @implemented (Undocumented)
279 */
280 VOID
281 WINAPI
282 ExpungeConsoleCommandHistoryW(LPCWSTR lpExeName)
283 {
284 IntExpungeConsoleCommandHistory(lpExeName, TRUE);
285 }
286
287
288 /*
289 * @implemented (Undocumented)
290 */
291 VOID
292 WINAPI
293 ExpungeConsoleCommandHistoryA(LPCSTR lpExeName)
294 {
295 IntExpungeConsoleCommandHistory(lpExeName, FALSE);
296 }
297
298
299 /*
300 * @implemented (Undocumented)
301 */
302 DWORD
303 WINAPI
304 GetConsoleCommandHistoryW(LPWSTR lpHistory,
305 DWORD cbHistory,
306 LPCWSTR lpExeName)
307 {
308 return IntGetConsoleCommandHistory(lpHistory, cbHistory, lpExeName, TRUE);
309 }
310
311
312 /*
313 * @implemented (Undocumented)
314 */
315 DWORD
316 WINAPI
317 GetConsoleCommandHistoryA(LPSTR lpHistory,
318 DWORD cbHistory,
319 LPCSTR lpExeName)
320 {
321 return IntGetConsoleCommandHistory(lpHistory, cbHistory, lpExeName, FALSE);
322 }
323
324
325 /*
326 * @implemented (Undocumented)
327 */
328 DWORD
329 WINAPI
330 GetConsoleCommandHistoryLengthW(LPCWSTR lpExeName)
331 {
332 return IntGetConsoleCommandHistoryLength(lpExeName, TRUE);
333 }
334
335
336 /*
337 * @implemented (Undocumented)
338 */
339 DWORD
340 WINAPI
341 GetConsoleCommandHistoryLengthA(LPCSTR lpExeName)
342 {
343 return IntGetConsoleCommandHistoryLength(lpExeName, FALSE);
344 }
345
346
347 /*
348 * @implemented (Undocumented)
349 */
350 BOOL
351 WINAPI
352 SetConsoleNumberOfCommandsW(DWORD dwNumCommands,
353 LPCWSTR lpExeName)
354 {
355 return IntSetConsoleNumberOfCommands(dwNumCommands, lpExeName, TRUE);
356 }
357
358
359 /*
360 * @implemented (Undocumented)
361 */
362 BOOL
363 WINAPI
364 SetConsoleNumberOfCommandsA(DWORD dwNumCommands,
365 LPCSTR lpExeName)
366 {
367 return IntSetConsoleNumberOfCommands(dwNumCommands, lpExeName, FALSE);
368 }
369
370
371 /*
372 * @implemented
373 */
374 BOOL
375 WINAPI
376 SetConsoleCommandHistoryMode(IN DWORD dwMode)
377 {
378 CONSOLE_API_MESSAGE ApiMessage;
379 PCONSOLE_SETHISTORYMODE SetHistoryModeRequest = &ApiMessage.Data.SetHistoryModeRequest;
380
381 SetHistoryModeRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
382 SetHistoryModeRequest->Mode = dwMode;
383
384 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
385 NULL,
386 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepSetCommandHistoryMode),
387 sizeof(*SetHistoryModeRequest));
388 if (!NT_SUCCESS(ApiMessage.Status))
389 {
390 BaseSetLastNTError(ApiMessage.Status);
391 return FALSE;
392 }
393
394 return TRUE;
395 }
396
397 /* EOF */