[KERNEL32] Update some console API prototypes.
[reactos.git] / dll / win32 / kernel32 / client / console / history.c
index cedd78c..b4513ea 100644 (file)
 
 /* PRIVATE FUNCTIONS **********************************************************/
 
-static BOOL
-IntExpungeConsoleCommandHistory(LPCVOID lpExeName, BOOL bUnicode)
+#if 0
+/* Get the size needed to copy a string to a capture buffer, including alignment */
+static ULONG
+IntStringSize(LPCVOID String,
+              BOOL Unicode)
 {
-    CSR_API_MESSAGE Request;
+    ULONG Size = (Unicode ? wcslen(String) : strlen(String)) * sizeof(WCHAR);
+    return (Size + 3) & ~3;
+}
+#endif
+
+static VOID
+IntExpungeConsoleCommandHistory(LPCVOID lpExeName, BOOLEAN bUnicode)
+{
+    CONSOLE_API_MESSAGE ApiMessage;
+    PCONSOLE_EXPUNGECOMMANDHISTORY ExpungeCommandHistoryRequest = &ApiMessage.Data.ExpungeCommandHistoryRequest;
     PCSR_CAPTURE_BUFFER CaptureBuffer;
-    NTSTATUS Status;
 
-    if (lpExeName == NULL || !(bUnicode ? *(PWCHAR)lpExeName : *(PCHAR)lpExeName))
+    USHORT NumChars = (USHORT)(lpExeName ? (bUnicode ? wcslen(lpExeName) : strlen(lpExeName)) : 0);
+
+    if (lpExeName == NULL || NumChars == 0)
     {
         SetLastError(ERROR_INVALID_PARAMETER);
-        return FALSE;
+        return;
     }
 
-    CaptureBuffer = CsrAllocateCaptureBuffer(1, IntStringSize(lpExeName, bUnicode));
+    ExpungeCommandHistoryRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
+    ExpungeCommandHistoryRequest->ExeLength     = NumChars * (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
+    ExpungeCommandHistoryRequest->Unicode  =
+    ExpungeCommandHistoryRequest->Unicode2 = bUnicode;
+
+    // CaptureBuffer = CsrAllocateCaptureBuffer(1, IntStringSize(lpExeName, bUnicode));
+    CaptureBuffer = CsrAllocateCaptureBuffer(1, ExpungeCommandHistoryRequest->ExeLength);
     if (!CaptureBuffer)
     {
         DPRINT1("CsrAllocateCaptureBuffer failed!\n");
         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        return FALSE;
+        return;
     }
 
-    IntCaptureMessageString(CaptureBuffer, lpExeName, bUnicode,
-                            &Request.Data.ExpungeCommandHistory.ExeName);
+    CsrCaptureMessageBuffer(CaptureBuffer,
+                            (PVOID)lpExeName,
+                            ExpungeCommandHistoryRequest->ExeLength,
+                            (PVOID)&ExpungeCommandHistoryRequest->ExeName);
 
-    Status = CsrClientCallServer(&Request,
-                                 CaptureBuffer,
-                                 CSR_CREATE_API_NUMBER(CSR_CONSOLE, EXPUNGE_COMMAND_HISTORY),
-                                 sizeof(CSR_API_MESSAGE));
+    CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
+                        CaptureBuffer,
+                        CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepExpungeCommandHistory),
+                        sizeof(*ExpungeCommandHistoryRequest));
 
     CsrFreeCaptureBuffer(CaptureBuffer);
 
-    if (!NT_SUCCESS(Status) || !NT_SUCCESS(Status = Request.Status))
-    {
-        BaseSetLastNTError(Status);
-        return FALSE;
-    }
-
-    return TRUE;
+    if (!NT_SUCCESS(ApiMessage.Status))
+        BaseSetLastNTError(ApiMessage.Status);
 }
 
 
 static DWORD
-IntGetConsoleCommandHistory(LPVOID lpHistory, DWORD cbHistory, LPCVOID lpExeName, BOOL bUnicode)
+IntGetConsoleCommandHistory(LPVOID lpHistory, DWORD cbHistory, LPCVOID lpExeName, BOOLEAN bUnicode)
 {
-    CSR_API_MESSAGE Request;
+    CONSOLE_API_MESSAGE ApiMessage;
+    PCONSOLE_GETCOMMANDHISTORY GetCommandHistoryRequest = &ApiMessage.Data.GetCommandHistoryRequest;
     PCSR_CAPTURE_BUFFER CaptureBuffer;
-    NTSTATUS Status;
-    DWORD HistoryLength = cbHistory * (bUnicode ? 1 : sizeof(WCHAR));
 
-    if (lpExeName == NULL || !(bUnicode ? *(PWCHAR)lpExeName : *(PCHAR)lpExeName))
+    USHORT NumChars = (USHORT)(lpExeName ? (bUnicode ? wcslen(lpExeName) : strlen(lpExeName)) : 0);
+
+    if (lpExeName == NULL || NumChars == 0)
     {
         SetLastError(ERROR_INVALID_PARAMETER);
         return 0;
     }
 
-    CaptureBuffer = CsrAllocateCaptureBuffer(2, IntStringSize(lpExeName, bUnicode) +
-                                                HistoryLength);
+    GetCommandHistoryRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
+    GetCommandHistoryRequest->HistoryLength = cbHistory;
+    GetCommandHistoryRequest->ExeLength     = NumChars * (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
+    GetCommandHistoryRequest->Unicode  =
+    GetCommandHistoryRequest->Unicode2 = bUnicode;
+
+    // CaptureBuffer = CsrAllocateCaptureBuffer(2, IntStringSize(lpExeName, bUnicode) +
+    //                                             HistoryLength);
+    CaptureBuffer = CsrAllocateCaptureBuffer(2, GetCommandHistoryRequest->ExeLength +
+                                                GetCommandHistoryRequest->HistoryLength);
     if (!CaptureBuffer)
     {
         DPRINT1("CsrAllocateCaptureBuffer failed!\n");
@@ -80,58 +105,57 @@ IntGetConsoleCommandHistory(LPVOID lpHistory, DWORD cbHistory, LPCVOID lpExeName
         return 0;
     }
 
-    IntCaptureMessageString(CaptureBuffer, lpExeName, bUnicode,
-                            &Request.Data.GetCommandHistory.ExeName);
-    Request.Data.GetCommandHistory.Length = HistoryLength;
-    CsrAllocateMessagePointer(CaptureBuffer, HistoryLength,
-                              (PVOID*)&Request.Data.GetCommandHistory.History);
-
-    Status = CsrClientCallServer(&Request,
-                                 CaptureBuffer,
-                                 CSR_CREATE_API_NUMBER(CSR_CONSOLE, GET_COMMAND_HISTORY),
-                                 sizeof(CSR_API_MESSAGE));
-    if (!NT_SUCCESS(Status) || !NT_SUCCESS(Status = Request.Status))
+    CsrCaptureMessageBuffer(CaptureBuffer,
+                            (PVOID)lpExeName,
+                            GetCommandHistoryRequest->ExeLength,
+                            (PVOID)&GetCommandHistoryRequest->ExeName);
+
+    CsrAllocateMessagePointer(CaptureBuffer, GetCommandHistoryRequest->HistoryLength,
+                              (PVOID*)&GetCommandHistoryRequest->History);
+
+    CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
+                        CaptureBuffer,
+                        CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetCommandHistory),
+                        sizeof(*GetCommandHistoryRequest));
+    if (!NT_SUCCESS(ApiMessage.Status))
     {
         CsrFreeCaptureBuffer(CaptureBuffer);
-        BaseSetLastNTError(Status);
+        BaseSetLastNTError(ApiMessage.Status);
         return 0;
     }
 
-    if (bUnicode)
-    {
-        memcpy(lpHistory,
-               Request.Data.GetCommandHistory.History,
-               Request.Data.GetCommandHistory.Length);
-    }
-    else
-    {
-        WideCharToMultiByte(CP_ACP, 0,
-                            Request.Data.GetCommandHistory.History,
-                            Request.Data.GetCommandHistory.Length / sizeof(WCHAR),
-                            lpHistory,
-                            cbHistory,
-                            NULL, NULL);
-    }
+    RtlCopyMemory(lpHistory,
+                  GetCommandHistoryRequest->History,
+                  GetCommandHistoryRequest->HistoryLength);
 
     CsrFreeCaptureBuffer(CaptureBuffer);
-    return Request.Data.GetCommandHistory.Length;
+
+    return GetCommandHistoryRequest->HistoryLength;
 }
 
 
 static DWORD
 IntGetConsoleCommandHistoryLength(LPCVOID lpExeName, BOOL bUnicode)
 {
-    CSR_API_MESSAGE Request;
+    CONSOLE_API_MESSAGE ApiMessage;
+    PCONSOLE_GETCOMMANDHISTORYLENGTH GetCommandHistoryLengthRequest = &ApiMessage.Data.GetCommandHistoryLengthRequest;
     PCSR_CAPTURE_BUFFER CaptureBuffer;
-    NTSTATUS Status;
 
-    if (lpExeName == NULL || !(bUnicode ? *(PWCHAR)lpExeName : *(PCHAR)lpExeName))
+    USHORT NumChars = (USHORT)(lpExeName ? (bUnicode ? wcslen(lpExeName) : strlen(lpExeName)) : 0);
+
+    if (lpExeName == NULL || NumChars == 0)
     {
         SetLastError(ERROR_INVALID_PARAMETER);
         return 0;
     }
 
-    CaptureBuffer = CsrAllocateCaptureBuffer(1, IntStringSize(lpExeName, bUnicode));
+    GetCommandHistoryLengthRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
+    GetCommandHistoryLengthRequest->ExeLength     = NumChars * (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
+    GetCommandHistoryLengthRequest->Unicode  =
+    GetCommandHistoryLengthRequest->Unicode2 = bUnicode;
+
+    // CaptureBuffer = CsrAllocateCaptureBuffer(1, IntStringSize(lpExeName, bUnicode));
+    CaptureBuffer = CsrAllocateCaptureBuffer(1, GetCommandHistoryLengthRequest->ExeLength);
     if (!CaptureBuffer)
     {
         DPRINT1("CsrAllocateCaptureBuffer failed!\n");
@@ -139,42 +163,53 @@ IntGetConsoleCommandHistoryLength(LPCVOID lpExeName, BOOL bUnicode)
         return 0;
     }
 
-    IntCaptureMessageString(CaptureBuffer, lpExeName, bUnicode,
-                            &Request.Data.GetCommandHistoryLength.ExeName);
+    CsrCaptureMessageBuffer(CaptureBuffer,
+                            (PVOID)lpExeName,
+                            GetCommandHistoryLengthRequest->ExeLength,
+                            (PVOID)&GetCommandHistoryLengthRequest->ExeName);
 
-    Status = CsrClientCallServer(&Request,
-                                 CaptureBuffer,
-                                 CSR_CREATE_API_NUMBER(CSR_CONSOLE, GET_COMMAND_HISTORY_LENGTH),
-                                 sizeof(CSR_API_MESSAGE));
+    CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
+                        CaptureBuffer,
+                        CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetCommandHistoryLength),
+                        sizeof(*GetCommandHistoryLengthRequest));
 
     CsrFreeCaptureBuffer(CaptureBuffer);
 
-    if (!NT_SUCCESS(Status) || !NT_SUCCESS(Status = Request.Status))
+    if (!NT_SUCCESS(ApiMessage.Status))
     {
-        BaseSetLastNTError(Status);
+        BaseSetLastNTError(ApiMessage.Status);
         return 0;
     }
 
-    return Request.Data.GetCommandHistoryLength.Length;
+    return GetCommandHistoryLengthRequest->HistoryLength;
 }
 
 
 static BOOL
 IntSetConsoleNumberOfCommands(DWORD dwNumCommands,
                               LPCVOID lpExeName,
-                              BOOL bUnicode)
+                              BOOLEAN bUnicode)
 {
-    CSR_API_MESSAGE Request;
+    CONSOLE_API_MESSAGE ApiMessage;
+    PCONSOLE_SETHISTORYNUMBERCOMMANDS SetHistoryNumberCommandsRequest = &ApiMessage.Data.SetHistoryNumberCommandsRequest;
     PCSR_CAPTURE_BUFFER CaptureBuffer;
-    NTSTATUS Status;
 
-    if (lpExeName == NULL || !(bUnicode ? *(PWCHAR)lpExeName : *(PCHAR)lpExeName))
+    USHORT NumChars = (USHORT)(lpExeName ? (bUnicode ? wcslen(lpExeName) : strlen(lpExeName)) : 0);
+
+    if (lpExeName == NULL || NumChars == 0)
     {
         SetLastError(ERROR_INVALID_PARAMETER);
         return FALSE;
     }
 
-    CaptureBuffer = CsrAllocateCaptureBuffer(1, IntStringSize(lpExeName, bUnicode));
+    SetHistoryNumberCommandsRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
+    SetHistoryNumberCommandsRequest->NumCommands   = dwNumCommands;
+    SetHistoryNumberCommandsRequest->ExeLength     = NumChars * (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
+    SetHistoryNumberCommandsRequest->Unicode  =
+    SetHistoryNumberCommandsRequest->Unicode2 = bUnicode;
+
+    // CaptureBuffer = CsrAllocateCaptureBuffer(1, IntStringSize(lpExeName, bUnicode));
+    CaptureBuffer = CsrAllocateCaptureBuffer(1, SetHistoryNumberCommandsRequest->ExeLength);
     if (!CaptureBuffer)
     {
         DPRINT1("CsrAllocateCaptureBuffer failed!\n");
@@ -182,20 +217,21 @@ IntSetConsoleNumberOfCommands(DWORD dwNumCommands,
         return FALSE;
     }
 
-    IntCaptureMessageString(CaptureBuffer, lpExeName, bUnicode,
-                            &Request.Data.SetHistoryNumberCommands.ExeName);
-    Request.Data.SetHistoryNumberCommands.NumCommands = dwNumCommands;
+    CsrCaptureMessageBuffer(CaptureBuffer,
+                            (PVOID)lpExeName,
+                            SetHistoryNumberCommandsRequest->ExeLength,
+                            (PVOID)&SetHistoryNumberCommandsRequest->ExeName);
 
-    Status = CsrClientCallServer(&Request,
-                                 CaptureBuffer,
-                                 CSR_CREATE_API_NUMBER(CSR_CONSOLE, SET_HISTORY_NUMBER_COMMANDS),
-                                 sizeof(CSR_API_MESSAGE));
+    CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
+                        CaptureBuffer,
+                        CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepSetNumberOfCommands),
+                        sizeof(*SetHistoryNumberCommandsRequest));
 
     CsrFreeCaptureBuffer(CaptureBuffer);
 
-    if (!NT_SUCCESS(Status) || !NT_SUCCESS(Status = Request.Status))
+    if (!NT_SUCCESS(ApiMessage.Status))
     {
-        BaseSetLastNTError(Status);
+        BaseSetLastNTError(ApiMessage.Status);
         return FALSE;
     }
 
@@ -208,22 +244,24 @@ IntSetConsoleNumberOfCommands(DWORD dwNumCommands,
 /*
  * @implemented (Undocumented)
  */
-BOOL
+VOID
 WINAPI
-ExpungeConsoleCommandHistoryW(LPCWSTR lpExeName)
+DECLSPEC_HOTPATCH
+ExpungeConsoleCommandHistoryW(IN LPCWSTR lpExeName)
 {
-    return IntExpungeConsoleCommandHistory(lpExeName, TRUE);
+    IntExpungeConsoleCommandHistory(lpExeName, TRUE);
 }
 
 
 /*
  * @implemented (Undocumented)
  */
-BOOL
+VOID
 WINAPI
-ExpungeConsoleCommandHistoryA(LPCSTR lpExeName)
+DECLSPEC_HOTPATCH
+ExpungeConsoleCommandHistoryA(IN LPCSTR lpExeName)
 {
-    return IntExpungeConsoleCommandHistory(lpExeName, FALSE);
+    IntExpungeConsoleCommandHistory(lpExeName, FALSE);
 }
 
 
@@ -232,9 +270,10 @@ ExpungeConsoleCommandHistoryA(LPCSTR lpExeName)
  */
 DWORD
 WINAPI
-GetConsoleCommandHistoryW(LPWSTR lpHistory,
-                          DWORD cbHistory,
-                          LPCWSTR lpExeName)
+DECLSPEC_HOTPATCH
+GetConsoleCommandHistoryW(OUT LPWSTR lpHistory,
+                          IN DWORD cbHistory,
+                          IN LPCWSTR lpExeName)
 {
     return IntGetConsoleCommandHistory(lpHistory, cbHistory, lpExeName, TRUE);
 }
@@ -245,9 +284,10 @@ GetConsoleCommandHistoryW(LPWSTR lpHistory,
  */
 DWORD
 WINAPI
-GetConsoleCommandHistoryA(LPSTR lpHistory,
-                          DWORD cbHistory,
-                          LPCSTR lpExeName)
+DECLSPEC_HOTPATCH
+GetConsoleCommandHistoryA(OUT LPSTR lpHistory,
+                          IN DWORD cbHistory,
+                          IN LPCSTR lpExeName)
 {
     return IntGetConsoleCommandHistory(lpHistory, cbHistory, lpExeName, FALSE);
 }
@@ -258,7 +298,8 @@ GetConsoleCommandHistoryA(LPSTR lpHistory,
  */
 DWORD
 WINAPI
-GetConsoleCommandHistoryLengthW(LPCWSTR lpExeName)
+DECLSPEC_HOTPATCH
+GetConsoleCommandHistoryLengthW(IN LPCWSTR lpExeName)
 {
     return IntGetConsoleCommandHistoryLength(lpExeName, TRUE);
 }
@@ -269,9 +310,10 @@ GetConsoleCommandHistoryLengthW(LPCWSTR lpExeName)
  */
 DWORD
 WINAPI
-GetConsoleCommandHistoryLengthA(LPCSTR lpExeName)
+DECLSPEC_HOTPATCH
+GetConsoleCommandHistoryLengthA(IN LPCSTR lpExeName)
 {
-    return IntGetConsoleCommandHistoryLength(lpExeName, FALSE) / sizeof(WCHAR);
+    return IntGetConsoleCommandHistoryLength(lpExeName, FALSE);
 }
 
 
@@ -280,8 +322,9 @@ GetConsoleCommandHistoryLengthA(LPCSTR lpExeName)
  */
 BOOL
 WINAPI
-SetConsoleNumberOfCommandsW(DWORD dwNumCommands,
-                            LPCSTR lpExeName)
+DECLSPEC_HOTPATCH
+SetConsoleNumberOfCommandsW(IN DWORD dwNumCommands,
+                            IN LPCWSTR lpExeName)
 {
     return IntSetConsoleNumberOfCommands(dwNumCommands, lpExeName, TRUE);
 }
@@ -292,22 +335,39 @@ SetConsoleNumberOfCommandsW(DWORD dwNumCommands,
  */
 BOOL
 WINAPI
-SetConsoleNumberOfCommandsA(DWORD dwNumCommands,
-                            LPCWSTR lpExeName)
+DECLSPEC_HOTPATCH
+SetConsoleNumberOfCommandsA(IN DWORD dwNumCommands,
+                            IN LPCSTR lpExeName)
 {
     return IntSetConsoleNumberOfCommands(dwNumCommands, lpExeName, FALSE);
 }
 
 
 /*
- * @unimplemented
+ * @implemented
  */
 BOOL
 WINAPI
+DECLSPEC_HOTPATCH
 SetConsoleCommandHistoryMode(IN DWORD dwMode)
 {
-    STUB;
-    return FALSE;
+    CONSOLE_API_MESSAGE ApiMessage;
+    PCONSOLE_SETHISTORYMODE SetHistoryModeRequest = &ApiMessage.Data.SetHistoryModeRequest;
+
+    SetHistoryModeRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
+    SetHistoryModeRequest->Mode          = dwMode;
+
+    CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
+                        NULL,
+                        CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepSetCommandHistoryMode),
+                        sizeof(*SetHistoryModeRequest));
+    if (!NT_SUCCESS(ApiMessage.Status))
+    {
+        BaseSetLastNTError(ApiMessage.Status);
+        return FALSE;
+    }
+
+    return TRUE;
 }
 
 /* EOF */