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
9 /* INCLUDES *******************************************************************/
17 /* PRIVATE FUNCTIONS **********************************************************/
20 /* Get the size needed to copy a string to a capture buffer, including alignment */
22 IntStringSize(LPCVOID String
,
25 ULONG Size
= (Unicode
? wcslen(String
) : strlen(String
)) * sizeof(WCHAR
);
26 return (Size
+ 3) & ~3;
30 /* Copy a string to a capture buffer */
32 IntCaptureMessageString(PCSR_CAPTURE_BUFFER CaptureBuffer
,
35 PUNICODE_STRING RequestString
)
40 Size
= wcslen(String
) * sizeof(WCHAR
);
41 CsrCaptureMessageBuffer(CaptureBuffer
, (PVOID
)String
, Size
, (PVOID
*)&RequestString
->Buffer
);
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
))
50 RequestString
->Length
= RequestString
->MaximumLength
= (USHORT
)Size
;
55 IntExpungeConsoleCommandHistory(LPCVOID lpExeName
, BOOLEAN bUnicode
)
57 CONSOLE_API_MESSAGE ApiMessage
;
58 PCONSOLE_EXPUNGECOMMANDHISTORY ExpungeCommandHistoryRequest
= &ApiMessage
.Data
.ExpungeCommandHistoryRequest
;
59 PCSR_CAPTURE_BUFFER CaptureBuffer
;
61 USHORT NumChars
= (USHORT
)(lpExeName
? (bUnicode
? wcslen(lpExeName
) : strlen(lpExeName
)) : 0);
63 if (lpExeName
== NULL
|| NumChars
== 0)
65 SetLastError(ERROR_INVALID_PARAMETER
);
69 ExpungeCommandHistoryRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
70 ExpungeCommandHistoryRequest
->ExeLength
= NumChars
* (bUnicode
? sizeof(WCHAR
) : sizeof(CHAR
));
71 ExpungeCommandHistoryRequest
->Unicode
=
72 ExpungeCommandHistoryRequest
->Unicode2
= bUnicode
;
74 // CaptureBuffer = CsrAllocateCaptureBuffer(1, IntStringSize(lpExeName, bUnicode));
75 CaptureBuffer
= CsrAllocateCaptureBuffer(1, ExpungeCommandHistoryRequest
->ExeLength
);
78 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
79 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
83 // IntCaptureMessageString(CaptureBuffer, lpExeName, bUnicode,
84 // &ExpungeCommandHistoryRequest->ExeName);
85 CsrCaptureMessageBuffer(CaptureBuffer
,
87 ExpungeCommandHistoryRequest
->ExeLength
,
88 (PVOID
)&ExpungeCommandHistoryRequest
->ExeName
);
90 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
92 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepExpungeCommandHistory
),
93 sizeof(*ExpungeCommandHistoryRequest
));
95 CsrFreeCaptureBuffer(CaptureBuffer
);
97 if (!NT_SUCCESS(ApiMessage
.Status
))
98 BaseSetLastNTError(ApiMessage
.Status
);
103 IntGetConsoleCommandHistory(LPVOID lpHistory
, DWORD cbHistory
, LPCVOID lpExeName
, BOOLEAN bUnicode
)
105 CONSOLE_API_MESSAGE ApiMessage
;
106 PCONSOLE_GETCOMMANDHISTORY GetCommandHistoryRequest
= &ApiMessage
.Data
.GetCommandHistoryRequest
;
107 PCSR_CAPTURE_BUFFER CaptureBuffer
;
109 USHORT NumChars
= (USHORT
)(lpExeName
? (bUnicode
? wcslen(lpExeName
) : strlen(lpExeName
)) : 0);
111 if (lpExeName
== NULL
|| NumChars
== 0)
113 SetLastError(ERROR_INVALID_PARAMETER
);
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
;
123 // CaptureBuffer = CsrAllocateCaptureBuffer(2, IntStringSize(lpExeName, bUnicode) +
125 CaptureBuffer
= CsrAllocateCaptureBuffer(2, GetCommandHistoryRequest
->ExeLength
+
126 GetCommandHistoryRequest
->HistoryLength
);
129 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
130 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
134 // IntCaptureMessageString(CaptureBuffer, lpExeName, bUnicode,
135 // &GetCommandHistoryRequest->ExeName);
136 CsrCaptureMessageBuffer(CaptureBuffer
,
138 GetCommandHistoryRequest
->ExeLength
,
139 (PVOID
)&GetCommandHistoryRequest
->ExeName
);
141 // CsrAllocateMessagePointer(CaptureBuffer, HistoryLength,
142 CsrAllocateMessagePointer(CaptureBuffer
, GetCommandHistoryRequest
->HistoryLength
,
143 (PVOID
*)&GetCommandHistoryRequest
->History
);
145 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
147 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetCommandHistory
),
148 sizeof(*GetCommandHistoryRequest
));
149 if (!NT_SUCCESS(ApiMessage
.Status
))
151 CsrFreeCaptureBuffer(CaptureBuffer
);
152 BaseSetLastNTError(ApiMessage
.Status
);
156 RtlCopyMemory(lpHistory
,
157 GetCommandHistoryRequest
->History
,
158 GetCommandHistoryRequest
->HistoryLength
);
160 CsrFreeCaptureBuffer(CaptureBuffer
);
162 return GetCommandHistoryRequest
->HistoryLength
;
167 IntGetConsoleCommandHistoryLength(LPCVOID lpExeName
, BOOL bUnicode
)
169 CONSOLE_API_MESSAGE ApiMessage
;
170 PCONSOLE_GETCOMMANDHISTORYLENGTH GetCommandHistoryLengthRequest
= &ApiMessage
.Data
.GetCommandHistoryLengthRequest
;
171 PCSR_CAPTURE_BUFFER CaptureBuffer
;
173 USHORT NumChars
= (USHORT
)(lpExeName
? (bUnicode
? wcslen(lpExeName
) : strlen(lpExeName
)) : 0);
175 if (lpExeName
== NULL
|| NumChars
== 0)
177 SetLastError(ERROR_INVALID_PARAMETER
);
181 GetCommandHistoryLengthRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
182 GetCommandHistoryLengthRequest
->ExeLength
= NumChars
* (bUnicode
? sizeof(WCHAR
) : sizeof(CHAR
));
183 GetCommandHistoryLengthRequest
->Unicode
=
184 GetCommandHistoryLengthRequest
->Unicode2
= bUnicode
;
186 // CaptureBuffer = CsrAllocateCaptureBuffer(1, IntStringSize(lpExeName, bUnicode));
187 CaptureBuffer
= CsrAllocateCaptureBuffer(1, GetCommandHistoryLengthRequest
->ExeLength
);
190 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
191 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
195 // IntCaptureMessageString(CaptureBuffer, lpExeName, bUnicode,
196 // &GetCommandHistoryLengthRequest->ExeName);
197 CsrCaptureMessageBuffer(CaptureBuffer
,
199 GetCommandHistoryLengthRequest
->ExeLength
,
200 (PVOID
)&GetCommandHistoryLengthRequest
->ExeName
);
202 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
204 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepGetCommandHistoryLength
),
205 sizeof(*GetCommandHistoryLengthRequest
));
207 CsrFreeCaptureBuffer(CaptureBuffer
);
209 if (!NT_SUCCESS(ApiMessage
.Status
))
211 BaseSetLastNTError(ApiMessage
.Status
);
215 return GetCommandHistoryLengthRequest
->HistoryLength
;
220 IntSetConsoleNumberOfCommands(DWORD dwNumCommands
,
224 CONSOLE_API_MESSAGE ApiMessage
;
225 PCONSOLE_SETHISTORYNUMBERCOMMANDS SetHistoryNumberCommandsRequest
= &ApiMessage
.Data
.SetHistoryNumberCommandsRequest
;
226 PCSR_CAPTURE_BUFFER CaptureBuffer
;
228 USHORT NumChars
= (USHORT
)(lpExeName
? (bUnicode
? wcslen(lpExeName
) : strlen(lpExeName
)) : 0);
230 if (lpExeName
== NULL
|| NumChars
== 0)
232 SetLastError(ERROR_INVALID_PARAMETER
);
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
;
242 // CaptureBuffer = CsrAllocateCaptureBuffer(1, IntStringSize(lpExeName, bUnicode));
243 CaptureBuffer
= CsrAllocateCaptureBuffer(1, SetHistoryNumberCommandsRequest
->ExeLength
);
246 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
247 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
251 // IntCaptureMessageString(CaptureBuffer, lpExeName, bUnicode,
252 // &SetHistoryNumberCommandsRequest->ExeName);
253 CsrCaptureMessageBuffer(CaptureBuffer
,
255 SetHistoryNumberCommandsRequest
->ExeLength
,
256 (PVOID
)&SetHistoryNumberCommandsRequest
->ExeName
);
258 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
260 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetNumberOfCommands
),
261 sizeof(*SetHistoryNumberCommandsRequest
));
263 CsrFreeCaptureBuffer(CaptureBuffer
);
265 if (!NT_SUCCESS(ApiMessage
.Status
))
267 BaseSetLastNTError(ApiMessage
.Status
);
275 /* FUNCTIONS ******************************************************************/
278 * @implemented (Undocumented)
282 ExpungeConsoleCommandHistoryW(LPCWSTR lpExeName
)
284 IntExpungeConsoleCommandHistory(lpExeName
, TRUE
);
289 * @implemented (Undocumented)
293 ExpungeConsoleCommandHistoryA(LPCSTR lpExeName
)
295 IntExpungeConsoleCommandHistory(lpExeName
, FALSE
);
300 * @implemented (Undocumented)
304 GetConsoleCommandHistoryW(LPWSTR lpHistory
,
308 return IntGetConsoleCommandHistory(lpHistory
, cbHistory
, lpExeName
, TRUE
);
313 * @implemented (Undocumented)
317 GetConsoleCommandHistoryA(LPSTR lpHistory
,
321 return IntGetConsoleCommandHistory(lpHistory
, cbHistory
, lpExeName
, FALSE
);
326 * @implemented (Undocumented)
330 GetConsoleCommandHistoryLengthW(LPCWSTR lpExeName
)
332 return IntGetConsoleCommandHistoryLength(lpExeName
, TRUE
);
337 * @implemented (Undocumented)
341 GetConsoleCommandHistoryLengthA(LPCSTR lpExeName
)
343 return IntGetConsoleCommandHistoryLength(lpExeName
, FALSE
);
348 * @implemented (Undocumented)
352 SetConsoleNumberOfCommandsW(DWORD dwNumCommands
,
355 return IntSetConsoleNumberOfCommands(dwNumCommands
, lpExeName
, TRUE
);
360 * @implemented (Undocumented)
364 SetConsoleNumberOfCommandsA(DWORD dwNumCommands
,
367 return IntSetConsoleNumberOfCommands(dwNumCommands
, lpExeName
, FALSE
);
376 SetConsoleCommandHistoryMode(IN DWORD dwMode
)
378 CONSOLE_API_MESSAGE ApiMessage
;
379 PCONSOLE_SETHISTORYMODE SetHistoryModeRequest
= &ApiMessage
.Data
.SetHistoryModeRequest
;
381 SetHistoryModeRequest
->ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
382 SetHistoryModeRequest
->Mode
= dwMode
;
384 CsrClientCallServer((PCSR_API_MESSAGE
)&ApiMessage
,
386 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX
, ConsolepSetCommandHistoryMode
),
387 sizeof(*SetHistoryModeRequest
));
388 if (!NT_SUCCESS(ApiMessage
.Status
))
390 BaseSetLastNTError(ApiMessage
.Status
);