Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
2.8 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. conrqust.c
  5. Abstract:
  6. This module contains the handler for console requests.
  7. Author:
  8. Avi Nathan (avin) 17-Jul-1991
  9. Environment:
  10. User Mode Only
  11. Revision History:
  12. Ellen Aycock-Wright (ellena) 15-Sept-1991 Modified for POSIX
  13. --*/
  14. #define WIN32_ONLY
  15. #include "psxses.h"
  16. #include <stdio.h>
  17. #include <io.h>
  18. #include <fcntl.h>
  19. #include <errno.h>
  20. //
  21. // ServeConRequest -- perform a console request
  22. //
  23. // pReq - the request message
  24. // pStatus - returned errno, 0 for success
  25. //
  26. // returns: TRUE (reply to the caller)
  27. //
  28. BOOL
  29. ServeConRequest(
  30. IN PSCCONREQUEST pReq,
  31. OUT PDWORD pStatus
  32. )
  33. {
  34. DWORD IoLengthDone;
  35. DWORD Rc;
  36. HANDLE h;
  37. int fd;
  38. int error;
  39. *pStatus = 0;
  40. //
  41. // If the KbdThread has not been started, we do IO in the
  42. // conventional way. Since starting the thread depends on the
  43. // user setting an environment variable, this is a backward-
  44. // compatibility mode.
  45. //
  46. switch (pReq->Request) {
  47. case ScReadFile:
  48. fd = HandleToLong(pReq->d.IoBuf.Handle);
  49. if (DoTrickyIO && _isatty(fd)) {
  50. IoLengthDone = TermInput(hConsoleInput,
  51. PsxSessionDataBase,
  52. (unsigned int)pReq->d.IoBuf.Len,
  53. pReq->d.IoBuf.Flags,
  54. &error
  55. );
  56. } else {
  57. _setmode(fd, _O_BINARY);
  58. IoLengthDone = _read(fd,
  59. PsxSessionDataBase,
  60. (unsigned int)pReq->d.IoBuf.Len);
  61. if (-1 == IoLengthDone) {
  62. error = EBADF;
  63. }
  64. }
  65. pReq->d.IoBuf.Len = IoLengthDone;
  66. if (IoLengthDone == -1) {
  67. *pStatus = error;
  68. }
  69. break;
  70. case ScWriteFile:
  71. fd = HandleToLong(pReq->d.IoBuf.Handle);
  72. if (fd > 2) {
  73. fd++;
  74. }
  75. if (DoTrickyIO && _isatty(fd)) {
  76. IoLengthDone = TermOutput(hConsoleOutput,
  77. (LPSTR)PsxSessionDataBase,
  78. (DWORD)pReq->d.IoBuf.Len);
  79. } else {
  80. // not a tty.
  81. _setmode(fd, _O_BINARY);
  82. IoLengthDone = _write(fd, PsxSessionDataBase,
  83. (unsigned int)pReq->d.IoBuf.Len);
  84. }
  85. pReq->d.IoBuf.Len = IoLengthDone;
  86. if (-1 == IoLengthDone) {
  87. *pStatus = EBADF;
  88. }
  89. break;
  90. case ScKbdCharIn:
  91. Rc = GetPsxChar(&pReq->d.AsciiChar);
  92. break;
  93. case ScIsatty:
  94. if (!DoTrickyIO) {
  95. // then it's never a tty.
  96. pReq->d.IoBuf.Len = 0;
  97. break;
  98. }
  99. pReq->d.IoBuf.Len = (_isatty(HandleToLong(pReq->d.IoBuf.Handle)) != 0);
  100. break;
  101. case ScIsatty2:
  102. pReq->d.IoBuf.Len = (_isatty(HandleToLong(pReq->d.IoBuf.Handle)) != 0);
  103. break;
  104. case ScOpenFile:
  105. fd = _open("CONIN$", _O_RDWR);
  106. _open("CONOUT$", _O_RDWR);
  107. pReq->d.IoBuf.Handle = (HANDLE)fd;
  108. break;
  109. case ScCloseFile:
  110. #if 0
  111. //
  112. // This code causes the keybd thread to fail ReadConsole() with
  113. // STATUS_INVALID_HANDLE.
  114. //
  115. _close((int)pReq->d.IoBuf.Handle);
  116. #endif
  117. break;
  118. default:
  119. *pStatus = EINVAL;
  120. }
  121. return TRUE;
  122. }