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