[KERNEL32][CONSRV]
[reactos.git] / dll / win32 / kernel32 / client / console / history.c
index cedd78c..2fc5024 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)
+{
+    ULONG Size = (Unicode ? wcslen(String) : strlen(String)) * sizeof(WCHAR);
+    return (Size + 3) & ~3;
+}
+
+
+/* Copy a string to a capture buffer */
+static VOID
+IntCaptureMessageString(PCSR_CAPTURE_BUFFER CaptureBuffer,
+                        LPCVOID String,
+                        BOOL Unicode,
+                        PUNICODE_STRING RequestString)
+{
+    ULONG Size;
+    if (Unicode)
+    {
+        Size = wcslen(String) * sizeof(WCHAR);
+        CsrCaptureMessageBuffer(CaptureBuffer, (PVOID)String, Size, (PVOID *)&RequestString->Buffer);
+    }
+    else
+    {
+        Size = strlen(String);
+        CsrAllocateMessagePointer(CaptureBuffer, Size * sizeof(WCHAR), (PVOID *)&RequestString->Buffer);
+        Size = MultiByteToWideChar(CP_ACP, 0, String, Size, RequestString->Buffer, Size * sizeof(WCHAR))
+               * sizeof(WCHAR);
+    }
+    RequestString->Length = RequestString->MaximumLength = (USHORT)Size;
+}
+#endif
+
+static VOID
+IntExpungeConsoleCommandHistory(LPCVOID lpExeName, BOOLEAN bUnicode)
 {
-    CSR_API_MESSAGE Request;
+    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);
+    // IntCaptureMessageString(CaptureBuffer, lpExeName, bUnicode,
+    //                         &ExpungeCommandHistoryRequest->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 +131,60 @@ 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))
+    // IntCaptureMessageString(CaptureBuffer, lpExeName, bUnicode,
+    //                         &GetCommandHistoryRequest->ExeName);
+    CsrCaptureMessageBuffer(CaptureBuffer,
+                            (PVOID)lpExeName,
+                            GetCommandHistoryRequest->ExeLength,
+                            (PVOID)&GetCommandHistoryRequest->ExeName);
+
+    // CsrAllocateMessagePointer(CaptureBuffer, HistoryLength,
+    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 +192,55 @@ IntGetConsoleCommandHistoryLength(LPCVOID lpExeName, BOOL bUnicode)
         return 0;
     }
 
-    IntCaptureMessageString(CaptureBuffer, lpExeName, bUnicode,
-                            &Request.Data.GetCommandHistoryLength.ExeName);
+    // IntCaptureMessageString(CaptureBuffer, lpExeName, bUnicode,
+    //                         &GetCommandHistoryLengthRequest->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 +248,23 @@ IntSetConsoleNumberOfCommands(DWORD dwNumCommands,
         return FALSE;
     }
 
-    IntCaptureMessageString(CaptureBuffer, lpExeName, bUnicode,
-                            &Request.Data.SetHistoryNumberCommands.ExeName);
-    Request.Data.SetHistoryNumberCommands.NumCommands = dwNumCommands;
+    // IntCaptureMessageString(CaptureBuffer, lpExeName, bUnicode,
+    //                         &SetHistoryNumberCommandsRequest->ExeName);
+    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 +277,22 @@ IntSetConsoleNumberOfCommands(DWORD dwNumCommands,
 /*
  * @implemented (Undocumented)
  */
-BOOL
+VOID
 WINAPI
 ExpungeConsoleCommandHistoryW(LPCWSTR lpExeName)
 {
-    return IntExpungeConsoleCommandHistory(lpExeName, TRUE);
+    IntExpungeConsoleCommandHistory(lpExeName, TRUE);
 }
 
 
 /*
  * @implemented (Undocumented)
  */
-BOOL
+VOID
 WINAPI
 ExpungeConsoleCommandHistoryA(LPCSTR lpExeName)
 {
-    return IntExpungeConsoleCommandHistory(lpExeName, FALSE);
+    IntExpungeConsoleCommandHistory(lpExeName, FALSE);
 }
 
 
@@ -271,7 +340,7 @@ DWORD
 WINAPI
 GetConsoleCommandHistoryLengthA(LPCSTR lpExeName)
 {
-    return IntGetConsoleCommandHistoryLength(lpExeName, FALSE) / sizeof(WCHAR);
+    return IntGetConsoleCommandHistoryLength(lpExeName, FALSE);
 }
 
 
@@ -281,7 +350,7 @@ GetConsoleCommandHistoryLengthA(LPCSTR lpExeName)
 BOOL
 WINAPI
 SetConsoleNumberOfCommandsW(DWORD dwNumCommands,
-                            LPCSTR lpExeName)
+                            LPCWSTR lpExeName)
 {
     return IntSetConsoleNumberOfCommands(dwNumCommands, lpExeName, TRUE);
 }
@@ -293,21 +362,36 @@ SetConsoleNumberOfCommandsW(DWORD dwNumCommands,
 BOOL
 WINAPI
 SetConsoleNumberOfCommandsA(DWORD dwNumCommands,
-                            LPCWSTR lpExeName)
+                            LPCSTR lpExeName)
 {
     return IntSetConsoleNumberOfCommands(dwNumCommands, lpExeName, FALSE);
 }
 
 
 /*
- * @unimplemented
+ * @implemented
  */
 BOOL
 WINAPI
 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 */