Synchronize with trunk r58528.
[reactos.git] / dll / win32 / kernel32 / client / console / readwrite.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: dll/win32/kernel32/client/console/readwrite.c
5 * PURPOSE: Win32 Console Client read-write functions
6 * PROGRAMMERS: Emanuele Aliberti
7 * Marty Dill
8 * Filip Navara (xnavara@volny.cz)
9 * Thomas Weidenmueller (w3seek@reactos.org)
10 * Jeffrey Morlan
11 */
12
13 /* INCLUDES *******************************************************************/
14
15 #include <k32.h>
16
17 #define NDEBUG
18 #include <debug.h>
19
20
21 /* PRIVATE FUNCTIONS **********************************************************/
22
23 /******************
24 * Read functions *
25 ******************/
26
27 static
28 BOOL
29 IntReadConsole(HANDLE hConsoleInput,
30 PVOID lpBuffer,
31 DWORD nNumberOfCharsToRead,
32 LPDWORD lpNumberOfCharsRead,
33 PCONSOLE_READCONSOLE_CONTROL pInputControl,
34 BOOL bUnicode)
35 {
36 NTSTATUS Status;
37 CONSOLE_API_MESSAGE ApiMessage;
38 PCONSOLE_READCONSOLE ReadConsoleRequest = &ApiMessage.Data.ReadConsoleRequest;
39 PCSR_CAPTURE_BUFFER CaptureBuffer;
40 ULONG CharSize;
41
42 /* Determine the needed size */
43 CharSize = (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
44 ReadConsoleRequest->BufferSize = nNumberOfCharsToRead * CharSize;
45
46 /* Allocate a Capture Buffer */
47 CaptureBuffer = CsrAllocateCaptureBuffer(1, ReadConsoleRequest->BufferSize);
48 if (CaptureBuffer == NULL)
49 {
50 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
51 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
52 return FALSE;
53 }
54
55 /* Allocate space in the Buffer */
56 CsrAllocateMessagePointer(CaptureBuffer,
57 ReadConsoleRequest->BufferSize,
58 (PVOID*)&ReadConsoleRequest->Buffer);
59
60 /* Set up the data to send to the Console Server */
61 ReadConsoleRequest->InputHandle = hConsoleInput;
62 ReadConsoleRequest->Unicode = bUnicode;
63 ReadConsoleRequest->NrCharactersToRead = (WORD)nNumberOfCharsToRead;
64 ReadConsoleRequest->NrCharactersRead = 0;
65 ReadConsoleRequest->CtrlWakeupMask = 0;
66 if (pInputControl && pInputControl->nLength == sizeof(CONSOLE_READCONSOLE_CONTROL))
67 {
68 ReadConsoleRequest->NrCharactersRead = pInputControl->nInitialChars;
69 memcpy(ReadConsoleRequest->Buffer,
70 lpBuffer,
71 pInputControl->nInitialChars * sizeof(WCHAR));
72 ReadConsoleRequest->CtrlWakeupMask = pInputControl->dwCtrlWakeupMask;
73 }
74
75 /* Call the server */
76 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
77 CaptureBuffer,
78 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepReadConsole),
79 sizeof(CONSOLE_READCONSOLE));
80
81 /* Check for success */
82 if (NT_SUCCESS(Status))
83 {
84 memcpy(lpBuffer,
85 ReadConsoleRequest->Buffer,
86 ReadConsoleRequest->NrCharactersRead * CharSize);
87
88 if (lpNumberOfCharsRead != NULL)
89 *lpNumberOfCharsRead = ReadConsoleRequest->NrCharactersRead;
90
91 if (pInputControl && pInputControl->nLength == sizeof(CONSOLE_READCONSOLE_CONTROL))
92 pInputControl->dwControlKeyState = ReadConsoleRequest->ControlKeyState;
93 }
94 else
95 {
96 DPRINT1("CSR returned error in ReadConsole\n");
97
98 if (lpNumberOfCharsRead != NULL)
99 *lpNumberOfCharsRead = 0;
100
101 /* Error out */
102 BaseSetLastNTError(Status);
103 }
104
105 CsrFreeCaptureBuffer(CaptureBuffer);
106
107 /* Return TRUE or FALSE */
108 // return TRUE;
109 return (ReadConsoleRequest->NrCharactersRead > 0);
110 // return NT_SUCCESS(Status);
111 }
112
113
114 static
115 BOOL
116 IntGetConsoleInput(HANDLE hConsoleInput,
117 BOOL bRead,
118 PINPUT_RECORD lpBuffer,
119 DWORD nLength,
120 LPDWORD lpNumberOfEventsRead,
121 BOOL bUnicode)
122 {
123 NTSTATUS Status;
124 CONSOLE_API_MESSAGE ApiMessage;
125 PCONSOLE_GETINPUT GetInputRequest = &ApiMessage.Data.GetInputRequest;
126 PCSR_CAPTURE_BUFFER CaptureBuffer;
127 ULONG Size;
128
129 if (lpBuffer == NULL)
130 {
131 SetLastError(ERROR_INVALID_PARAMETER);
132 return FALSE;
133 }
134
135 Size = nLength * sizeof(INPUT_RECORD);
136
137 DPRINT("IntGetConsoleInput: %lx %p\n", Size, lpNumberOfEventsRead);
138
139 /* Allocate a Capture Buffer */
140 CaptureBuffer = CsrAllocateCaptureBuffer(1, Size);
141 if (CaptureBuffer == NULL)
142 {
143 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
144 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
145 return FALSE;
146 }
147
148 /* Allocate space in the Buffer */
149 CsrAllocateMessagePointer(CaptureBuffer,
150 Size,
151 (PVOID*)&GetInputRequest->InputRecord);
152
153 /* Set up the data to send to the Console Server */
154 GetInputRequest->InputHandle = hConsoleInput;
155 GetInputRequest->Unicode = bUnicode;
156 GetInputRequest->bRead = bRead;
157 GetInputRequest->InputsRead = 0;
158 GetInputRequest->Length = nLength;
159
160 /* Call the server */
161 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
162 CaptureBuffer,
163 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetConsoleInput),
164 sizeof(CONSOLE_GETINPUT));
165 DPRINT("Server returned: %x\n", Status);
166
167 /* Check for success */
168 if (NT_SUCCESS(Status))
169 {
170 /* Return the number of events read */
171 DPRINT("Events read: %lx\n", GetInputRequest->InputsRead);
172
173 if (lpNumberOfEventsRead != NULL)
174 *lpNumberOfEventsRead = GetInputRequest->InputsRead;
175
176 /* Copy into the buffer */
177 DPRINT("Copying to buffer\n");
178 RtlCopyMemory(lpBuffer,
179 GetInputRequest->InputRecord,
180 sizeof(INPUT_RECORD) * GetInputRequest->InputsRead);
181 }
182 else
183 {
184 if (lpNumberOfEventsRead != NULL)
185 *lpNumberOfEventsRead = 0;
186
187 /* Error out */
188 BaseSetLastNTError(Status);
189 }
190
191 /* Release the capture buffer */
192 CsrFreeCaptureBuffer(CaptureBuffer);
193
194 /* Return TRUE or FALSE */
195 return (GetInputRequest->InputsRead > 0);
196 // return NT_SUCCESS(Status);
197 }
198
199
200 static
201 BOOL
202 IntReadConsoleOutput(HANDLE hConsoleOutput,
203 PCHAR_INFO lpBuffer,
204 COORD dwBufferSize,
205 COORD dwBufferCoord,
206 PSMALL_RECT lpReadRegion,
207 BOOL bUnicode)
208 {
209 CONSOLE_API_MESSAGE ApiMessage;
210 PCONSOLE_READOUTPUT ReadOutputRequest = &ApiMessage.Data.ReadOutputRequest;
211 PCSR_CAPTURE_BUFFER CaptureBuffer;
212 DWORD Size, SizeX, SizeY;
213
214 if (lpBuffer == NULL)
215 {
216 SetLastError(ERROR_INVALID_PARAMETER);
217 return FALSE;
218 }
219
220 Size = dwBufferSize.X * dwBufferSize.Y * sizeof(CHAR_INFO);
221
222 DPRINT("IntReadConsoleOutput: %lx %p\n", Size, lpReadRegion);
223
224 /* Allocate a Capture Buffer */
225 CaptureBuffer = CsrAllocateCaptureBuffer(1, Size);
226 if (CaptureBuffer == NULL)
227 {
228 DPRINT1("CsrAllocateCaptureBuffer failed with size 0x%x!\n", Size);
229 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
230 return FALSE;
231 }
232
233 /* Allocate space in the Buffer */
234 CsrAllocateMessagePointer(CaptureBuffer,
235 Size,
236 (PVOID*)&ReadOutputRequest->CharInfo);
237
238 /* Set up the data to send to the Console Server */
239 ReadOutputRequest->OutputHandle = hConsoleOutput;
240 ReadOutputRequest->Unicode = bUnicode;
241 ReadOutputRequest->BufferSize = dwBufferSize;
242 ReadOutputRequest->BufferCoord = dwBufferCoord;
243 ReadOutputRequest->ReadRegion = *lpReadRegion;
244
245 /* Call the server */
246 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
247 CaptureBuffer,
248 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepReadConsoleOutput),
249 sizeof(CONSOLE_READOUTPUT));
250 DPRINT("Server returned: %x\n", ApiMessage.Status);
251
252 /* Check for success */
253 if (NT_SUCCESS(ApiMessage.Status))
254 {
255 /* Copy into the buffer */
256 DPRINT("Copying to buffer\n");
257 SizeX = ReadOutputRequest->ReadRegion.Right -
258 ReadOutputRequest->ReadRegion.Left + 1;
259 SizeY = ReadOutputRequest->ReadRegion.Bottom -
260 ReadOutputRequest->ReadRegion.Top + 1;
261 RtlCopyMemory(lpBuffer,
262 ReadOutputRequest->CharInfo,
263 sizeof(CHAR_INFO) * SizeX * SizeY);
264 }
265 else
266 {
267 /* Error out */
268 BaseSetLastNTError(ApiMessage.Status);
269 }
270
271 /* Return the read region */
272 DPRINT("read region: %lx\n", ReadOutputRequest->ReadRegion);
273 *lpReadRegion = ReadOutputRequest->ReadRegion;
274
275 /* Release the capture buffer */
276 CsrFreeCaptureBuffer(CaptureBuffer);
277
278 /* Return TRUE or FALSE */
279 return NT_SUCCESS(ApiMessage.Status);
280 }
281
282
283 static
284 BOOL
285 IntReadConsoleOutputCode(HANDLE hConsoleOutput,
286 CODE_TYPE CodeType,
287 PVOID pCode,
288 DWORD nLength,
289 COORD dwReadCoord,
290 LPDWORD lpNumberOfCodesRead)
291 {
292 NTSTATUS Status;
293 BOOL bRet = TRUE;
294 CONSOLE_API_MESSAGE ApiMessage;
295 PCONSOLE_READOUTPUTCODE ReadOutputCodeRequest = &ApiMessage.Data.ReadOutputCodeRequest;
296 PCSR_CAPTURE_BUFFER CaptureBuffer;
297 ULONG SizeBytes, CodeSize;
298 DWORD CodesRead;
299
300 /* Determine the needed size */
301 switch (CodeType)
302 {
303 case CODE_ASCII:
304 CodeSize = sizeof(CHAR);
305 break;
306
307 case CODE_UNICODE:
308 CodeSize = sizeof(WCHAR);
309 break;
310
311 case CODE_ATTRIBUTE:
312 CodeSize = sizeof(WORD);
313 break;
314
315 default:
316 SetLastError(ERROR_INVALID_PARAMETER);
317 return FALSE;
318 }
319 SizeBytes = nLength * CodeSize;
320
321 /* Allocate a Capture Buffer */
322 CaptureBuffer = CsrAllocateCaptureBuffer(1, SizeBytes);
323 if (CaptureBuffer == NULL)
324 {
325 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
326 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
327 return FALSE;
328 }
329
330 /* Allocate space in the Buffer */
331 CsrAllocateMessagePointer(CaptureBuffer,
332 SizeBytes,
333 (PVOID*)&ReadOutputCodeRequest->pCode.pCode);
334
335 /* Start reading */
336 ReadOutputCodeRequest->OutputHandle = hConsoleOutput;
337 ReadOutputCodeRequest->CodeType = CodeType;
338 ReadOutputCodeRequest->ReadCoord = dwReadCoord;
339
340 ReadOutputCodeRequest->NumCodesToRead = nLength;
341
342 /* Call the server */
343 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
344 CaptureBuffer,
345 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepReadConsoleOutputString),
346 sizeof(CONSOLE_READOUTPUTCODE));
347
348 /* Check for success */
349 if (NT_SUCCESS(Status))
350 {
351 CodesRead = ReadOutputCodeRequest->CodesRead;
352 memcpy(pCode, ReadOutputCodeRequest->pCode.pCode, CodesRead * CodeSize);
353
354 // ReadOutputCodeRequest->ReadCoord = ReadOutputCodeRequest->EndCoord;
355
356 if (lpNumberOfCodesRead != NULL)
357 *lpNumberOfCodesRead = CodesRead;
358
359 bRet = TRUE;
360 }
361 else
362 {
363 if (lpNumberOfCodesRead != NULL)
364 *lpNumberOfCodesRead = 0;
365
366 /* Error out */
367 BaseSetLastNTError(Status);
368 bRet = FALSE;
369 }
370
371 CsrFreeCaptureBuffer(CaptureBuffer);
372
373 return bRet;
374 }
375
376
377 /*******************
378 * Write functions *
379 *******************/
380
381 static
382 BOOL
383 IntWriteConsole(HANDLE hConsoleOutput,
384 PVOID lpBuffer,
385 DWORD nNumberOfCharsToWrite,
386 LPDWORD lpNumberOfCharsWritten,
387 LPVOID lpReserved,
388 BOOL bUnicode)
389 {
390 NTSTATUS Status;
391 BOOL bRet = TRUE;
392 CONSOLE_API_MESSAGE ApiMessage;
393 PCONSOLE_WRITECONSOLE WriteConsoleRequest = &ApiMessage.Data.WriteConsoleRequest;
394 PCSR_CAPTURE_BUFFER CaptureBuffer;
395 ULONG CharSize;
396
397 /* Determine the needed size */
398 CharSize = (bUnicode ? sizeof(WCHAR) : sizeof(CHAR));
399 WriteConsoleRequest->BufferSize = nNumberOfCharsToWrite * CharSize;
400
401 /* Allocate a Capture Buffer */
402 CaptureBuffer = CsrAllocateCaptureBuffer(1, WriteConsoleRequest->BufferSize);
403 if (CaptureBuffer == NULL)
404 {
405 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
406 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
407 return FALSE;
408 }
409
410 /* Capture the buffer to write */
411 CsrCaptureMessageBuffer(CaptureBuffer,
412 (PVOID)lpBuffer,
413 WriteConsoleRequest->BufferSize,
414 (PVOID*)&WriteConsoleRequest->Buffer);
415
416 /* Start writing */
417 WriteConsoleRequest->NrCharactersToWrite = nNumberOfCharsToWrite;
418 WriteConsoleRequest->OutputHandle = hConsoleOutput;
419 WriteConsoleRequest->Unicode = bUnicode;
420
421 /* Call the server */
422 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
423 CaptureBuffer,
424 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepWriteConsole),
425 sizeof(CONSOLE_WRITECONSOLE));
426
427 /* Check for success */
428 if (NT_SUCCESS(Status))
429 {
430 if (lpNumberOfCharsWritten != NULL)
431 *lpNumberOfCharsWritten = WriteConsoleRequest->NrCharactersWritten;
432
433 bRet = TRUE;
434 }
435 else
436 {
437 if (lpNumberOfCharsWritten != NULL)
438 *lpNumberOfCharsWritten = 0;
439
440 /* Error out */
441 BaseSetLastNTError(Status);
442 bRet = FALSE;
443 }
444
445 CsrFreeCaptureBuffer(CaptureBuffer);
446
447 return bRet;
448 }
449
450
451 static
452 BOOL
453 IntWriteConsoleInput(HANDLE hConsoleInput,
454 PINPUT_RECORD lpBuffer,
455 DWORD nLength,
456 LPDWORD lpNumberOfEventsWritten,
457 BOOL bUnicode)
458 {
459 CONSOLE_API_MESSAGE ApiMessage;
460 PCONSOLE_WRITEINPUT WriteInputRequest = &ApiMessage.Data.WriteInputRequest;
461 PCSR_CAPTURE_BUFFER CaptureBuffer;
462 DWORD Size;
463
464 Size = nLength * sizeof(INPUT_RECORD);
465
466 DPRINT("IntWriteConsoleInput: %lx %p\n", Size, lpNumberOfEventsWritten);
467
468 /* Allocate a Capture Buffer */
469 CaptureBuffer = CsrAllocateCaptureBuffer(1, Size);
470 if (CaptureBuffer == NULL)
471 {
472 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
473 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
474 return FALSE;
475 }
476
477 /* Capture the user buffer */
478 CsrCaptureMessageBuffer(CaptureBuffer,
479 lpBuffer,
480 Size,
481 (PVOID*)&WriteInputRequest->InputRecord);
482
483 /* Set up the data to send to the Console Server */
484 WriteInputRequest->InputHandle = hConsoleInput;
485 WriteInputRequest->Unicode = bUnicode;
486 WriteInputRequest->Length = nLength;
487
488 /* Call the server */
489 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
490 CaptureBuffer,
491 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepWriteConsoleInput),
492 sizeof(CONSOLE_WRITEINPUT));
493 DPRINT("Server returned: %x\n", ApiMessage.Status);
494
495 /* Check for success */
496 if (NT_SUCCESS(ApiMessage.Status))
497 {
498 /* Return the number of events read */
499 DPRINT("Events read: %lx\n", WriteInputRequest->Length);
500
501 if (lpNumberOfEventsWritten != NULL)
502 *lpNumberOfEventsWritten = WriteInputRequest->Length;
503 }
504 else
505 {
506 if (lpNumberOfEventsWritten != NULL)
507 *lpNumberOfEventsWritten = 0;
508
509 /* Error out */
510 BaseSetLastNTError(ApiMessage.Status);
511 }
512
513 /* Release the capture buffer */
514 CsrFreeCaptureBuffer(CaptureBuffer);
515
516 /* Return TRUE or FALSE */
517 return NT_SUCCESS(ApiMessage.Status);
518 }
519
520
521 static
522 BOOL
523 IntWriteConsoleOutput(HANDLE hConsoleOutput,
524 CONST CHAR_INFO *lpBuffer,
525 COORD dwBufferSize,
526 COORD dwBufferCoord,
527 PSMALL_RECT lpWriteRegion,
528 BOOL bUnicode)
529 {
530 CONSOLE_API_MESSAGE ApiMessage;
531 PCONSOLE_WRITEOUTPUT WriteOutputRequest = &ApiMessage.Data.WriteOutputRequest;
532 PCSR_CAPTURE_BUFFER CaptureBuffer;
533 ULONG Size;
534
535 if ((lpBuffer == NULL) || (lpWriteRegion == NULL))
536 {
537 SetLastError(ERROR_INVALID_PARAMETER);
538 return FALSE;
539 }
540 /*
541 if (lpWriteRegion == NULL)
542 {
543 SetLastError(ERROR_INVALID_PARAMETER);
544 return FALSE;
545 }
546 */
547
548 Size = dwBufferSize.Y * dwBufferSize.X * sizeof(CHAR_INFO);
549
550 DPRINT("IntWriteConsoleOutput: %lx %p\n", Size, lpWriteRegion);
551
552 /* Allocate a Capture Buffer */
553 CaptureBuffer = CsrAllocateCaptureBuffer(1, Size);
554 if (CaptureBuffer == NULL)
555 {
556 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
557 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
558 return FALSE;
559 }
560
561 /* Capture the user buffer */
562 CsrCaptureMessageBuffer(CaptureBuffer,
563 (PVOID)lpBuffer,
564 Size,
565 (PVOID*)&WriteOutputRequest->CharInfo);
566
567 /* Set up the data to send to the Console Server */
568 WriteOutputRequest->OutputHandle = hConsoleOutput;
569 WriteOutputRequest->Unicode = bUnicode;
570 WriteOutputRequest->BufferSize = dwBufferSize;
571 WriteOutputRequest->BufferCoord = dwBufferCoord;
572 WriteOutputRequest->WriteRegion = *lpWriteRegion;
573
574 /* Call the server */
575 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
576 CaptureBuffer,
577 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepWriteConsoleOutput),
578 sizeof(CONSOLE_WRITEOUTPUT));
579 DPRINT("Server returned: %x\n", ApiMessage.Status);
580
581 /* Check for success */
582 if (!NT_SUCCESS(ApiMessage.Status))
583 {
584 /* Error out */
585 BaseSetLastNTError(ApiMessage.Status);
586 }
587
588 /* Return the read region */
589 DPRINT("read region: %lx\n", WriteOutputRequest->WriteRegion);
590 *lpWriteRegion = WriteOutputRequest->WriteRegion;
591
592 /* Release the capture buffer */
593 CsrFreeCaptureBuffer(CaptureBuffer);
594
595 /* Return TRUE or FALSE */
596 return NT_SUCCESS(ApiMessage.Status);
597 }
598
599
600 static
601 BOOL
602 IntWriteConsoleOutputCode(HANDLE hConsoleOutput,
603 CODE_TYPE CodeType,
604 CONST VOID *pCode,
605 DWORD nLength,
606 COORD dwWriteCoord,
607 LPDWORD lpNumberOfCodesWritten)
608 {
609 NTSTATUS Status;
610 BOOL bRet = TRUE;
611 CONSOLE_API_MESSAGE ApiMessage;
612 PCONSOLE_WRITEOUTPUTCODE WriteOutputCodeRequest = &ApiMessage.Data.WriteOutputCodeRequest;
613 PCSR_CAPTURE_BUFFER CaptureBuffer;
614 ULONG CodeSize;
615
616 /* Determine the needed size */
617 switch (CodeType)
618 {
619 case CODE_ASCII:
620 CodeSize = sizeof(CHAR);
621 break;
622
623 case CODE_UNICODE:
624 CodeSize = sizeof(WCHAR);
625 break;
626
627 case CODE_ATTRIBUTE:
628 CodeSize = sizeof(WORD);
629 break;
630
631 default:
632 SetLastError(ERROR_INVALID_PARAMETER);
633 return FALSE;
634 }
635 WriteOutputCodeRequest->BufferSize = nLength * CodeSize;
636
637 /* Allocate a Capture Buffer */
638 CaptureBuffer = CsrAllocateCaptureBuffer(1, WriteOutputCodeRequest->BufferSize);
639 if (CaptureBuffer == NULL)
640 {
641 DPRINT1("CsrAllocateCaptureBuffer failed!\n");
642 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
643 return FALSE;
644 }
645
646 /* Capture the buffer to write */
647 CsrCaptureMessageBuffer(CaptureBuffer,
648 (PVOID)pCode,
649 WriteOutputCodeRequest->BufferSize,
650 (PVOID*)&WriteOutputCodeRequest->pCode.pCode);
651
652 /* Start writing */
653 WriteOutputCodeRequest->OutputHandle = hConsoleOutput;
654 WriteOutputCodeRequest->CodeType = CodeType;
655 WriteOutputCodeRequest->Coord = dwWriteCoord;
656
657 WriteOutputCodeRequest->Length = nLength;
658
659 /* Call the server */
660 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
661 CaptureBuffer,
662 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepWriteConsoleOutputString),
663 sizeof(CONSOLE_WRITEOUTPUTCODE));
664
665 /* Check for success */
666 if (NT_SUCCESS(Status))
667 {
668 // WriteOutputCodeRequest->Coord = WriteOutputCodeRequest->EndCoord;
669
670 if (lpNumberOfCodesWritten != NULL)
671 // *lpNumberOfCodesWritten = WriteOutputCodeRequest->NrCharactersWritten;
672 *lpNumberOfCodesWritten = WriteOutputCodeRequest->Length;
673
674 bRet = TRUE;
675 }
676 else
677 {
678 if (lpNumberOfCodesWritten != NULL)
679 *lpNumberOfCodesWritten = 0;
680
681 /* Error out */
682 BaseSetLastNTError(Status);
683 bRet = FALSE;
684 }
685
686 CsrFreeCaptureBuffer(CaptureBuffer);
687
688 return bRet;
689 }
690
691
692 static
693 BOOL
694 IntFillConsoleOutputCode(HANDLE hConsoleOutput,
695 CODE_TYPE CodeType,
696 PVOID pCode,
697 DWORD nLength,
698 COORD dwWriteCoord,
699 LPDWORD lpNumberOfCodesWritten)
700 {
701 NTSTATUS Status;
702 CONSOLE_API_MESSAGE ApiMessage;
703 PCONSOLE_FILLOUTPUTCODE FillOutputRequest = &ApiMessage.Data.FillOutputRequest;
704
705 FillOutputRequest->OutputHandle = hConsoleOutput;
706 FillOutputRequest->CodeType = CodeType;
707
708 switch (CodeType)
709 {
710 case CODE_ASCII:
711 FillOutputRequest->Code.AsciiChar = *(PCHAR)pCode;
712 break;
713
714 case CODE_UNICODE:
715 FillOutputRequest->Code.UnicodeChar = *(PWCHAR)pCode;
716 break;
717
718 case CODE_ATTRIBUTE:
719 FillOutputRequest->Code.Attribute = *(PWORD)pCode;
720 break;
721
722 default:
723 SetLastError(ERROR_INVALID_PARAMETER);
724 return FALSE;
725 }
726
727 /* Set up the data to send to the Console Server */
728 FillOutputRequest->Coord = dwWriteCoord;
729 FillOutputRequest->Length = nLength;
730
731 /* Call the server */
732 Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
733 NULL,
734 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepFillConsoleOutput),
735 sizeof(CONSOLE_FILLOUTPUTCODE));
736
737 /* Check for success */
738 if (NT_SUCCESS(Status))
739 {
740 if (lpNumberOfCodesWritten != NULL)
741 *lpNumberOfCodesWritten = FillOutputRequest->Length;
742 // *lpNumberOfCodesWritten = Request.Data.FillOutputRequest.NrCharactersWritten;
743
744 return TRUE;
745 }
746 else
747 {
748 if (lpNumberOfCodesWritten != NULL)
749 *lpNumberOfCodesWritten = 0;
750
751 BaseSetLastNTError(Status);
752 return FALSE;
753 }
754 }
755
756
757 /* FUNCTIONS ******************************************************************/
758
759 /******************
760 * Read functions *
761 ******************/
762
763 /*--------------------------------------------------------------
764 * ReadConsoleW
765 *
766 * @implemented
767 */
768 BOOL
769 WINAPI
770 ReadConsoleW(HANDLE hConsoleInput,
771 LPVOID lpBuffer,
772 DWORD nNumberOfCharsToRead,
773 LPDWORD lpNumberOfCharsRead,
774 PCONSOLE_READCONSOLE_CONTROL pInputControl)
775 {
776 return IntReadConsole(hConsoleInput,
777 lpBuffer,
778 nNumberOfCharsToRead,
779 lpNumberOfCharsRead,
780 pInputControl,
781 TRUE);
782 }
783
784
785 /*--------------------------------------------------------------
786 * ReadConsoleA
787 *
788 * @implemented
789 */
790 BOOL
791 WINAPI
792 ReadConsoleA(HANDLE hConsoleInput,
793 LPVOID lpBuffer,
794 DWORD nNumberOfCharsToRead,
795 LPDWORD lpNumberOfCharsRead,
796 PCONSOLE_READCONSOLE_CONTROL pInputControl)
797 {
798 return IntReadConsole(hConsoleInput,
799 lpBuffer,
800 nNumberOfCharsToRead,
801 lpNumberOfCharsRead,
802 NULL,
803 FALSE);
804 }
805
806
807 /*--------------------------------------------------------------
808 * PeekConsoleInputW
809 *
810 * @implemented
811 */
812 BOOL
813 WINAPI
814 PeekConsoleInputW(HANDLE hConsoleInput,
815 PINPUT_RECORD lpBuffer,
816 DWORD nLength,
817 LPDWORD lpNumberOfEventsRead)
818 {
819 return IntGetConsoleInput(hConsoleInput,
820 FALSE,
821 lpBuffer,
822 nLength,
823 lpNumberOfEventsRead,
824 TRUE);
825 }
826
827
828 /*--------------------------------------------------------------
829 * PeekConsoleInputA
830 *
831 * @implemented
832 */
833 BOOL
834 WINAPI
835 PeekConsoleInputA(HANDLE hConsoleInput,
836 PINPUT_RECORD lpBuffer,
837 DWORD nLength,
838 LPDWORD lpNumberOfEventsRead)
839 {
840 return IntGetConsoleInput(hConsoleInput,
841 FALSE,
842 lpBuffer,
843 nLength,
844 lpNumberOfEventsRead,
845 FALSE);
846 }
847
848
849 /*--------------------------------------------------------------
850 * ReadConsoleInputW
851 *
852 * @implemented
853 */
854 BOOL
855 WINAPI
856 ReadConsoleInputW(HANDLE hConsoleInput,
857 PINPUT_RECORD lpBuffer,
858 DWORD nLength,
859 LPDWORD lpNumberOfEventsRead)
860 {
861 return IntGetConsoleInput(hConsoleInput,
862 TRUE,
863 lpBuffer,
864 nLength,
865 lpNumberOfEventsRead,
866 TRUE);
867 }
868
869
870 /*--------------------------------------------------------------
871 * ReadConsoleInputA
872 *
873 * @implemented
874 */
875 BOOL
876 WINAPI
877 ReadConsoleInputA(HANDLE hConsoleInput,
878 PINPUT_RECORD lpBuffer,
879 DWORD nLength,
880 LPDWORD lpNumberOfEventsRead)
881 {
882 return IntGetConsoleInput(hConsoleInput,
883 TRUE,
884 lpBuffer,
885 nLength,
886 lpNumberOfEventsRead,
887 FALSE);
888 }
889
890
891 BOOL
892 WINAPI
893 ReadConsoleInputExW(HANDLE hConsole, LPVOID lpBuffer, DWORD dwLen, LPDWORD Unknown1, DWORD Unknown2)
894 {
895 STUB;
896 return FALSE;
897 }
898
899
900 BOOL
901 WINAPI
902 ReadConsoleInputExA(HANDLE hConsole, LPVOID lpBuffer, DWORD dwLen, LPDWORD Unknown1, DWORD Unknown2)
903 {
904 STUB;
905 return FALSE;
906 }
907
908
909 /*--------------------------------------------------------------
910 * ReadConsoleOutputW
911 *
912 * @implemented
913 */
914 BOOL
915 WINAPI
916 ReadConsoleOutputW(HANDLE hConsoleOutput,
917 PCHAR_INFO lpBuffer,
918 COORD dwBufferSize,
919 COORD dwBufferCoord,
920 PSMALL_RECT lpReadRegion)
921 {
922 return IntReadConsoleOutput(hConsoleOutput,
923 lpBuffer,
924 dwBufferSize,
925 dwBufferCoord,
926 lpReadRegion,
927 TRUE);
928 }
929
930
931 /*--------------------------------------------------------------
932 * ReadConsoleOutputA
933 *
934 * @implemented
935 */
936 BOOL
937 WINAPI
938 ReadConsoleOutputA(HANDLE hConsoleOutput,
939 PCHAR_INFO lpBuffer,
940 COORD dwBufferSize,
941 COORD dwBufferCoord,
942 PSMALL_RECT lpReadRegion)
943 {
944 return IntReadConsoleOutput(hConsoleOutput,
945 lpBuffer,
946 dwBufferSize,
947 dwBufferCoord,
948 lpReadRegion,
949 FALSE);
950 }
951
952
953 /*--------------------------------------------------------------
954 * ReadConsoleOutputCharacterW
955 *
956 * @implemented
957 */
958 BOOL
959 WINAPI
960 ReadConsoleOutputCharacterW(HANDLE hConsoleOutput,
961 LPWSTR lpCharacter,
962 DWORD nLength,
963 COORD dwReadCoord,
964 LPDWORD lpNumberOfCharsRead)
965 {
966 return IntReadConsoleOutputCode(hConsoleOutput,
967 CODE_UNICODE,
968 lpCharacter,
969 nLength,
970 dwReadCoord,
971 lpNumberOfCharsRead);
972 }
973
974
975 /*--------------------------------------------------------------
976 * ReadConsoleOutputCharacterA
977 *
978 * @implemented
979 */
980 BOOL
981 WINAPI
982 ReadConsoleOutputCharacterA(HANDLE hConsoleOutput,
983 LPSTR lpCharacter,
984 DWORD nLength,
985 COORD dwReadCoord,
986 LPDWORD lpNumberOfCharsRead)
987 {
988 return IntReadConsoleOutputCode(hConsoleOutput,
989 CODE_ASCII,
990 lpCharacter,
991 nLength,
992 dwReadCoord,
993 lpNumberOfCharsRead);
994 }
995
996
997 /*--------------------------------------------------------------
998 * ReadConsoleOutputAttribute
999 *
1000 * @implemented
1001 */
1002 BOOL
1003 WINAPI
1004 ReadConsoleOutputAttribute(HANDLE hConsoleOutput,
1005 LPWORD lpAttribute,
1006 DWORD nLength,
1007 COORD dwReadCoord,
1008 LPDWORD lpNumberOfAttrsRead)
1009 {
1010 return IntReadConsoleOutputCode(hConsoleOutput,
1011 CODE_ATTRIBUTE,
1012 lpAttribute,
1013 nLength,
1014 dwReadCoord,
1015 lpNumberOfAttrsRead);
1016 }
1017
1018
1019 /*******************
1020 * Write functions *
1021 *******************/
1022
1023 /*--------------------------------------------------------------
1024 * WriteConsoleW
1025 *
1026 * @implemented
1027 */
1028 BOOL
1029 WINAPI
1030 WriteConsoleW(HANDLE hConsoleOutput,
1031 CONST VOID *lpBuffer,
1032 DWORD nNumberOfCharsToWrite,
1033 LPDWORD lpNumberOfCharsWritten,
1034 LPVOID lpReserved)
1035 {
1036 return IntWriteConsole(hConsoleOutput,
1037 (PVOID)lpBuffer,
1038 nNumberOfCharsToWrite,
1039 lpNumberOfCharsWritten,
1040 lpReserved,
1041 TRUE);
1042 }
1043
1044
1045 /*--------------------------------------------------------------
1046 * WriteConsoleA
1047 *
1048 * @implemented
1049 */
1050 BOOL
1051 WINAPI
1052 WriteConsoleA(HANDLE hConsoleOutput,
1053 CONST VOID *lpBuffer,
1054 DWORD nNumberOfCharsToWrite,
1055 LPDWORD lpNumberOfCharsWritten,
1056 LPVOID lpReserved)
1057 {
1058 return IntWriteConsole(hConsoleOutput,
1059 (PVOID)lpBuffer,
1060 nNumberOfCharsToWrite,
1061 lpNumberOfCharsWritten,
1062 lpReserved,
1063 FALSE);
1064 }
1065
1066
1067 /*--------------------------------------------------------------
1068 * WriteConsoleInputW
1069 *
1070 * @implemented
1071 */
1072 BOOL
1073 WINAPI
1074 WriteConsoleInputW(HANDLE hConsoleInput,
1075 CONST INPUT_RECORD *lpBuffer,
1076 DWORD nLength,
1077 LPDWORD lpNumberOfEventsWritten)
1078 {
1079 return IntWriteConsoleInput(hConsoleInput,
1080 (PINPUT_RECORD)lpBuffer,
1081 nLength,
1082 lpNumberOfEventsWritten,
1083 TRUE);
1084 }
1085
1086
1087 /*--------------------------------------------------------------
1088 * WriteConsoleInputA
1089 *
1090 * @implemented
1091 */
1092 BOOL
1093 WINAPI
1094 WriteConsoleInputA(HANDLE hConsoleInput,
1095 CONST INPUT_RECORD *lpBuffer,
1096 DWORD nLength,
1097 LPDWORD lpNumberOfEventsWritten)
1098 {
1099 return IntWriteConsoleInput(hConsoleInput,
1100 (PINPUT_RECORD)lpBuffer,
1101 nLength,
1102 lpNumberOfEventsWritten,
1103 FALSE);
1104 }
1105
1106
1107 /*--------------------------------------------------------------
1108 * WriteConsoleOutputW
1109 *
1110 * @implemented
1111 */
1112 BOOL
1113 WINAPI
1114 WriteConsoleOutputW(HANDLE hConsoleOutput,
1115 CONST CHAR_INFO *lpBuffer,
1116 COORD dwBufferSize,
1117 COORD dwBufferCoord,
1118 PSMALL_RECT lpWriteRegion)
1119 {
1120 return IntWriteConsoleOutput(hConsoleOutput,
1121 lpBuffer,
1122 dwBufferSize,
1123 dwBufferCoord,
1124 lpWriteRegion,
1125 TRUE);
1126 }
1127
1128
1129 /*--------------------------------------------------------------
1130 * WriteConsoleOutputA
1131 *
1132 * @implemented
1133 */
1134 BOOL
1135 WINAPI
1136 WriteConsoleOutputA(HANDLE hConsoleOutput,
1137 CONST CHAR_INFO *lpBuffer,
1138 COORD dwBufferSize,
1139 COORD dwBufferCoord,
1140 PSMALL_RECT lpWriteRegion)
1141 {
1142 return IntWriteConsoleOutput(hConsoleOutput,
1143 lpBuffer,
1144 dwBufferSize,
1145 dwBufferCoord,
1146 lpWriteRegion,
1147 FALSE);
1148 }
1149
1150
1151 /*--------------------------------------------------------------
1152 * WriteConsoleOutputCharacterW
1153 *
1154 * @implemented
1155 */
1156 BOOL
1157 WINAPI
1158 WriteConsoleOutputCharacterW(HANDLE hConsoleOutput,
1159 LPCWSTR lpCharacter,
1160 DWORD nLength,
1161 COORD dwWriteCoord,
1162 LPDWORD lpNumberOfCharsWritten)
1163 {
1164 return IntWriteConsoleOutputCode(hConsoleOutput,
1165 CODE_UNICODE,
1166 lpCharacter,
1167 nLength,
1168 dwWriteCoord,
1169 lpNumberOfCharsWritten);
1170 }
1171
1172
1173 /*--------------------------------------------------------------
1174 * WriteConsoleOutputCharacterA
1175 *
1176 * @implemented
1177 */
1178 BOOL
1179 WINAPI
1180 WriteConsoleOutputCharacterA(HANDLE hConsoleOutput,
1181 LPCSTR lpCharacter,
1182 DWORD nLength,
1183 COORD dwWriteCoord,
1184 LPDWORD lpNumberOfCharsWritten)
1185 {
1186 return IntWriteConsoleOutputCode(hConsoleOutput,
1187 CODE_ASCII,
1188 lpCharacter,
1189 nLength,
1190 dwWriteCoord,
1191 lpNumberOfCharsWritten);
1192 }
1193
1194
1195 /*--------------------------------------------------------------
1196 * WriteConsoleOutputAttribute
1197 *
1198 * @implemented
1199 */
1200 BOOL
1201 WINAPI
1202 WriteConsoleOutputAttribute(HANDLE hConsoleOutput,
1203 CONST WORD *lpAttribute,
1204 DWORD nLength,
1205 COORD dwWriteCoord,
1206 LPDWORD lpNumberOfAttrsWritten)
1207 {
1208 return IntWriteConsoleOutputCode(hConsoleOutput,
1209 CODE_ATTRIBUTE,
1210 lpAttribute,
1211 nLength,
1212 dwWriteCoord,
1213 lpNumberOfAttrsWritten);
1214 }
1215
1216
1217 /*--------------------------------------------------------------
1218 * FillConsoleOutputCharacterW
1219 *
1220 * @implemented
1221 */
1222 BOOL
1223 WINAPI
1224 FillConsoleOutputCharacterW(HANDLE hConsoleOutput,
1225 WCHAR cCharacter,
1226 DWORD nLength,
1227 COORD dwWriteCoord,
1228 LPDWORD lpNumberOfCharsWritten)
1229 {
1230 return IntFillConsoleOutputCode(hConsoleOutput,
1231 CODE_UNICODE,
1232 &cCharacter,
1233 nLength,
1234 dwWriteCoord,
1235 lpNumberOfCharsWritten);
1236 }
1237
1238
1239 /*--------------------------------------------------------------
1240 * FillConsoleOutputCharacterA
1241 *
1242 * @implemented
1243 */
1244 BOOL
1245 WINAPI
1246 FillConsoleOutputCharacterA(HANDLE hConsoleOutput,
1247 CHAR cCharacter,
1248 DWORD nLength,
1249 COORD dwWriteCoord,
1250 LPDWORD lpNumberOfCharsWritten)
1251 {
1252 return IntFillConsoleOutputCode(hConsoleOutput,
1253 CODE_ASCII,
1254 &cCharacter,
1255 nLength,
1256 dwWriteCoord,
1257 lpNumberOfCharsWritten);
1258 }
1259
1260
1261 /*--------------------------------------------------------------
1262 * FillConsoleOutputAttribute
1263 *
1264 * @implemented
1265 */
1266 BOOL
1267 WINAPI
1268 FillConsoleOutputAttribute(HANDLE hConsoleOutput,
1269 WORD wAttribute,
1270 DWORD nLength,
1271 COORD dwWriteCoord,
1272 LPDWORD lpNumberOfAttrsWritten)
1273 {
1274 return IntFillConsoleOutputCode(hConsoleOutput,
1275 CODE_ATTRIBUTE,
1276 &wAttribute,
1277 nLength,
1278 dwWriteCoord,
1279 lpNumberOfAttrsWritten);
1280 }
1281
1282 /* EOF */