[CONSRV]
[reactos.git] / win32ss / user / consrv / lineinput.c
index 2c89a07..536ce77 100644 (file)
 #define NDEBUG
 #include <debug.h>
 
-typedef struct tagHISTORY_BUFFER
+typedef struct _HISTORY_BUFFER
 {
     LIST_ENTRY ListEntry;
-    WORD Position;
-    WORD MaxEntries;
-    WORD NumEntries;
+    UINT Position;
+    UINT MaxEntries;
+    UINT NumEntries;
     PUNICODE_STRING Entries;
     UNICODE_STRING ExeName;
 } HISTORY_BUFFER, *PHISTORY_BUFFER;
@@ -27,7 +27,7 @@ typedef struct tagHISTORY_BUFFER
 /* FUNCTIONS *****************************************************************/
 
 static PHISTORY_BUFFER
-HistoryCurrentBuffer(PCSRSS_CONSOLE Console)
+HistoryCurrentBuffer(PCONSOLE Console)
 {
     /* TODO: use actual EXE name sent from process that called ReadConsole */
     UNICODE_STRING ExeName = { 14, 14, L"cmd.exe" };
@@ -61,7 +61,7 @@ HistoryCurrentBuffer(PCSRSS_CONSOLE Console)
 }
 
 static VOID
-HistoryAddEntry(PCSRSS_CONSOLE Console)
+HistoryAddEntry(PCONSOLE Console)
 {
     UNICODE_STRING NewEntry;
     PHISTORY_BUFFER Hist;
@@ -113,7 +113,7 @@ HistoryAddEntry(PCSRSS_CONSOLE Console)
 }
 
 static VOID
-HistoryGetCurrentEntry(PCSRSS_CONSOLE Console, PUNICODE_STRING Entry)
+HistoryGetCurrentEntry(PCONSOLE Console, PUNICODE_STRING Entry)
 {
     PHISTORY_BUFFER Hist;
     if (!(Hist = HistoryCurrentBuffer(Console)) || Hist->NumEntries == 0)
@@ -123,7 +123,7 @@ HistoryGetCurrentEntry(PCSRSS_CONSOLE Console, PUNICODE_STRING Entry)
 }
 
 static PHISTORY_BUFFER
-HistoryFindBuffer(PCSRSS_CONSOLE Console, PUNICODE_STRING ExeName)
+HistoryFindBuffer(PCONSOLE Console, PUNICODE_STRING ExeName)
 {
     PLIST_ENTRY Entry = Console->HistoryBuffers.Flink;
     while (Entry != &Console->HistoryBuffers)
@@ -151,31 +151,32 @@ HistoryDeleteBuffer(PHISTORY_BUFFER Hist)
 
 CSR_API(SrvGetConsoleCommandHistoryLength)
 {
-    PCSRSS_GET_COMMAND_HISTORY_LENGTH GetCommandHistoryLength = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.GetCommandHistoryLength;
-    PCSR_PROCESS ProcessData = CsrGetClientThread()->Process;
-    PCSRSS_CONSOLE Console;
+    PCONSOLE_GETCOMMANDHISTORYLENGTH GetCommandHistoryLengthRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.GetCommandHistoryLengthRequest;
+    PCONSOLE_PROCESS_DATA ProcessData = ConsoleGetPerProcessData(CsrGetClientThread()->Process);
+    PCONSOLE Console;
     NTSTATUS Status;
     PHISTORY_BUFFER Hist;
     ULONG Length = 0;
     INT i;
 
-    if (!Win32CsrValidateBuffer(ProcessData,
-                                GetCommandHistoryLength->ExeName.Buffer,
-                                GetCommandHistoryLength->ExeName.Length, 1))
+    if (!CsrValidateMessageBuffer(ApiMessage,
+                                  (PVOID*)&GetCommandHistoryLengthRequest->ExeName.Buffer,
+                                  GetCommandHistoryLengthRequest->ExeName.Length,
+                                  sizeof(BYTE)))
     {
-        return STATUS_ACCESS_VIOLATION;
+        return STATUS_INVALID_PARAMETER;
     }
 
     Status = ConioConsoleFromProcessData(ProcessData, &Console);
     if (NT_SUCCESS(Status))
     {
-        Hist = HistoryFindBuffer(Console, &GetCommandHistoryLength->ExeName);
+        Hist = HistoryFindBuffer(Console, &GetCommandHistoryLengthRequest->ExeName);
         if (Hist)
         {
             for (i = 0; i < Hist->NumEntries; i++)
                 Length += Hist->Entries[i].Length + sizeof(WCHAR);
         }
-        GetCommandHistoryLength->Length = Length;
+        GetCommandHistoryLengthRequest->Length = Length;
         ConioUnlockConsole(Console);
     }
     return Status;
@@ -183,27 +184,31 @@ CSR_API(SrvGetConsoleCommandHistoryLength)
 
 CSR_API(SrvGetConsoleCommandHistory)
 {
-    PCSRSS_GET_COMMAND_HISTORY GetCommandHistory = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.GetCommandHistory;
-    PCSR_PROCESS ProcessData = CsrGetClientThread()->Process;
-    PCSRSS_CONSOLE Console;
+    PCONSOLE_GETCOMMANDHISTORY GetCommandHistoryRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.GetCommandHistoryRequest;
+    PCONSOLE_PROCESS_DATA ProcessData = ConsoleGetPerProcessData(CsrGetClientThread()->Process);
+    PCONSOLE Console;
     NTSTATUS Status;
     PHISTORY_BUFFER Hist;
-    PBYTE Buffer = (PBYTE)GetCommandHistory->History;
-    ULONG BufferSize = GetCommandHistory->Length;
+    PBYTE Buffer = (PBYTE)GetCommandHistoryRequest->History;
+    ULONG BufferSize = GetCommandHistoryRequest->Length;
     INT i;
 
-    if (!Win32CsrValidateBuffer(ProcessData, Buffer, BufferSize, 1) ||
-        !Win32CsrValidateBuffer(ProcessData,
-                                GetCommandHistory->ExeName.Buffer,
-                                GetCommandHistory->ExeName.Length, 1))
+    if ( !CsrValidateMessageBuffer(ApiMessage,
+                                   (PVOID*)&GetCommandHistoryRequest->History,
+                                   GetCommandHistoryRequest->Length,
+                                   sizeof(BYTE))                    ||
+         !CsrValidateMessageBuffer(ApiMessage,
+                                   (PVOID*)&GetCommandHistoryRequest->ExeName.Buffer,
+                                   GetCommandHistoryRequest->ExeName.Length,
+                                   sizeof(BYTE)) )
     {
-        return STATUS_ACCESS_VIOLATION;
+        return STATUS_INVALID_PARAMETER;
     }
 
     Status = ConioConsoleFromProcessData(ProcessData, &Console);
     if (NT_SUCCESS(Status))
     {
-        Hist = HistoryFindBuffer(Console, &GetCommandHistory->ExeName);
+        Hist = HistoryFindBuffer(Console, &GetCommandHistoryRequest->ExeName);
         if (Hist)
         {
             for (i = 0; i < Hist->NumEntries; i++)
@@ -219,7 +224,7 @@ CSR_API(SrvGetConsoleCommandHistory)
                 Buffer += sizeof(WCHAR);
             }
         }
-        GetCommandHistory->Length = Buffer - (PBYTE)GetCommandHistory->History;
+        GetCommandHistoryRequest->Length = Buffer - (PBYTE)GetCommandHistoryRequest->History;
         ConioUnlockConsole(Console);
     }
     return Status;
@@ -227,23 +232,24 @@ CSR_API(SrvGetConsoleCommandHistory)
 
 CSR_API(SrvExpungeConsoleCommandHistory)
 {
-    PCSRSS_EXPUNGE_COMMAND_HISTORY ExpungeCommandHistory = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.ExpungeCommandHistory;
-    PCSR_PROCESS ProcessData = CsrGetClientThread()->Process;
-    PCSRSS_CONSOLE Console;
+    PCONSOLE_EXPUNGECOMMANDHISTORY ExpungeCommandHistoryRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.ExpungeCommandHistoryRequest;
+    PCONSOLE_PROCESS_DATA ProcessData = ConsoleGetPerProcessData(CsrGetClientThread()->Process);
+    PCONSOLE Console;
     PHISTORY_BUFFER Hist;
     NTSTATUS Status;
 
-    if (!Win32CsrValidateBuffer(ProcessData,
-                                ExpungeCommandHistory->ExeName.Buffer,
-                                ExpungeCommandHistory->ExeName.Length, 1))
+    if (!CsrValidateMessageBuffer(ApiMessage,
+                                  (PVOID*)&ExpungeCommandHistoryRequest->ExeName.Buffer,
+                                  ExpungeCommandHistoryRequest->ExeName.Length,
+                                  sizeof(BYTE)))
     {
-        return STATUS_ACCESS_VIOLATION;
+        return STATUS_INVALID_PARAMETER;
     }
 
     Status = ConioConsoleFromProcessData(ProcessData, &Console);
     if (NT_SUCCESS(Status))
     {
-        Hist = HistoryFindBuffer(Console, &ExpungeCommandHistory->ExeName);
+        Hist = HistoryFindBuffer(Console, &ExpungeCommandHistoryRequest->ExeName);
         HistoryDeleteBuffer(Hist);
         ConioUnlockConsole(Console);
     }
@@ -252,25 +258,26 @@ CSR_API(SrvExpungeConsoleCommandHistory)
 
 CSR_API(SrvSetConsoleNumberOfCommands)
 {
-    PCSRSS_SET_HISTORY_NUMBER_COMMANDS SetHistoryNumberCommands = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.SetHistoryNumberCommands;
-    PCSR_PROCESS ProcessData = CsrGetClientThread()->Process;
-    PCSRSS_CONSOLE Console;
+    PCONSOLE_SETHISTORYNUMBERCOMMANDS SetHistoryNumberCommandsRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.SetHistoryNumberCommandsRequest;
+    PCONSOLE_PROCESS_DATA ProcessData = ConsoleGetPerProcessData(CsrGetClientThread()->Process);
+    PCONSOLE Console;
     PHISTORY_BUFFER Hist;
     NTSTATUS Status;
-    WORD MaxEntries = SetHistoryNumberCommands->NumCommands;
+    UINT MaxEntries = SetHistoryNumberCommandsRequest->NumCommands;
     PUNICODE_STRING OldEntryList, NewEntryList;
 
-    if (!Win32CsrValidateBuffer(ProcessData,
-                                SetHistoryNumberCommands->ExeName.Buffer,
-                                SetHistoryNumberCommands->ExeName.Length, 1))
+    if (!CsrValidateMessageBuffer(ApiMessage,
+                                  (PVOID*)&SetHistoryNumberCommandsRequest->ExeName.Buffer,
+                                  SetHistoryNumberCommandsRequest->ExeName.Length,
+                                  sizeof(BYTE)))
     {
-        return STATUS_ACCESS_VIOLATION;
+        return STATUS_INVALID_PARAMETER;
     }
 
     Status = ConioConsoleFromProcessData(ProcessData, &Console);
     if (NT_SUCCESS(Status))
     {
-        Hist = HistoryFindBuffer(Console, &SetHistoryNumberCommands->ExeName);
+        Hist = HistoryFindBuffer(Console, &SetHistoryNumberCommandsRequest->ExeName);
         if (Hist)
         {
             OldEntryList = Hist->Entries;
@@ -302,14 +309,14 @@ CSR_API(SrvSetConsoleNumberOfCommands)
 
 CSR_API(SrvGetConsoleHistory)
 {
-    PCSRSS_GET_HISTORY_INFO GetHistoryInfo = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.GetHistoryInfo;
-    PCSRSS_CONSOLE Console;
-    NTSTATUS Status = ConioConsoleFromProcessData(CsrGetClientThread()->Process, &Console);
+    PCONSOLE_GETSETHISTORYINFO HistoryInfoRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.HistoryInfoRequest;
+    PCONSOLE Console;
+    NTSTATUS Status = ConioConsoleFromProcessData(ConsoleGetPerProcessData(CsrGetClientThread()->Process), &Console);
     if (NT_SUCCESS(Status))
     {
-        GetHistoryInfo->HistoryBufferSize      = Console->HistoryBufferSize;
-        GetHistoryInfo->NumberOfHistoryBuffers = Console->NumberOfHistoryBuffers;
-        GetHistoryInfo->dwFlags                = Console->HistoryNoDup;
+        HistoryInfoRequest->HistoryBufferSize      = Console->HistoryBufferSize;
+        HistoryInfoRequest->NumberOfHistoryBuffers = Console->NumberOfHistoryBuffers;
+        HistoryInfoRequest->dwFlags                = Console->HistoryNoDup;
         ConioUnlockConsole(Console);
     }
     return Status;
@@ -317,25 +324,25 @@ CSR_API(SrvGetConsoleHistory)
 
 CSR_API(SrvSetConsoleHistory)
 {
-    PCSRSS_SET_HISTORY_INFO SetHistoryInfo = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.SetHistoryInfo;
-    PCSRSS_CONSOLE Console;
-    NTSTATUS Status = ConioConsoleFromProcessData(CsrGetClientThread()->Process, &Console);
+    PCONSOLE_GETSETHISTORYINFO HistoryInfoRequest = &((PCONSOLE_API_MESSAGE)ApiMessage)->Data.HistoryInfoRequest;
+    PCONSOLE Console;
+    NTSTATUS Status = ConioConsoleFromProcessData(ConsoleGetPerProcessData(CsrGetClientThread()->Process), &Console);
     if (NT_SUCCESS(Status))
     {
-        Console->HistoryBufferSize      = (WORD)SetHistoryInfo->HistoryBufferSize;
-        Console->NumberOfHistoryBuffers = (WORD)SetHistoryInfo->NumberOfHistoryBuffers;
-        Console->HistoryNoDup           = SetHistoryInfo->dwFlags & HISTORY_NO_DUP_FLAG;
+        Console->HistoryBufferSize      = HistoryInfoRequest->HistoryBufferSize;
+        Console->NumberOfHistoryBuffers = HistoryInfoRequest->NumberOfHistoryBuffers;
+        Console->HistoryNoDup           = HistoryInfoRequest->dwFlags & HISTORY_NO_DUP_FLAG;
         ConioUnlockConsole(Console);
     }
     return Status;
 }
 
 static VOID
-LineInputSetPos(PCSRSS_CONSOLE Console, UINT Pos)
+LineInputSetPos(PCONSOLE Console, UINT Pos)
 {
     if (Pos != Console->LinePos && Console->Mode & ENABLE_ECHO_INPUT)
     {
-        PCSRSS_SCREEN_BUFFER Buffer = Console->ActiveBuffer;
+        PCONSOLE_SCREEN_BUFFER Buffer = Console->ActiveBuffer;
         UINT OldCursorX = Buffer->CurrentX;
         UINT OldCursorY = Buffer->CurrentY;
         INT XY = OldCursorY * Buffer->MaxX + OldCursorX;
@@ -355,7 +362,7 @@ LineInputSetPos(PCSRSS_CONSOLE Console, UINT Pos)
 }
 
 static VOID
-LineInputEdit(PCSRSS_CONSOLE Console, UINT NumToDelete, UINT NumToInsert, WCHAR *Insertion)
+LineInputEdit(PCONSOLE Console, UINT NumToDelete, UINT NumToInsert, WCHAR *Insertion)
 {
     UINT Pos = Console->LinePos;
     UINT NewSize = Console->LineSize - NumToDelete + NumToInsert;
@@ -392,7 +399,7 @@ LineInputEdit(PCSRSS_CONSOLE Console, UINT NumToDelete, UINT NumToInsert, WCHAR
 }
 
 static VOID
-LineInputRecallHistory(PCSRSS_CONSOLE Console, INT Offset)
+LineInputRecallHistory(PCONSOLE Console, INT Offset)
 {
     PHISTORY_BUFFER Hist;
 
@@ -411,7 +418,7 @@ LineInputRecallHistory(PCSRSS_CONSOLE Console, INT Offset)
 }
 
 VOID FASTCALL
-LineInputKeyDown(PCSRSS_CONSOLE Console, KEY_EVENT_RECORD *KeyEvent)
+LineInputKeyDown(PCONSOLE Console, KEY_EVENT_RECORD *KeyEvent)
 {
     UINT Pos = Console->LinePos;
     PHISTORY_BUFFER Hist;