2
0

fpterm.inputoutputconnection.unixpty.pas 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. { This file is part of fpterm - a terminal emulator, written in Free Pascal
  2. This unit implements an Unix-like pseudoterminal as a connection for the
  3. terminal.
  4. Copyright (C) 2024 Nikolay Nikolov <[email protected]>
  5. This library is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU Library General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or (at your
  8. option) any later version with the following modification:
  9. As a special exception, the copyright holders of this library give you
  10. permission to link this library with independent modules to produce an
  11. executable, regardless of the license terms of these independent modules,and
  12. to copy and distribute the resulting executable under terms of your choice,
  13. provided that you also meet, for each linked independent module, the terms
  14. and conditions of the license of that module. An independent module is a
  15. module which is not derived from or based on this library. If you modify
  16. this library, you may extend this exception to your version of the library,
  17. but you are not obligated to do so. If you do not wish to do so, delete this
  18. exception statement from your version.
  19. This program is distributed in the hope that it will be useful, but WITHOUT
  20. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  21. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  22. for more details.
  23. You should have received a copy of the GNU Library General Public License
  24. along with this library; if not, write to the Free Software Foundation,
  25. Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.
  26. }
  27. unit FpTerm.InputOutputConnection.UnixPty;
  28. {$mode objfpc}{$H+}
  29. interface
  30. uses
  31. FpTerm.InputOutputConnection, FpTerm.Pseudoterminal.Unix;
  32. type
  33. { TTerminalInputOutputConnection_UnixPty }
  34. TTerminalInputOutputConnection_UnixPty = class(TInterfacedObject, ITerminalInputOutputConnection)
  35. private
  36. FUnixPseudoTerminal: TUnixPseudoTerminal;
  37. protected
  38. function IsDataAvailable: Boolean;
  39. function IsClosed: Boolean;
  40. public
  41. constructor Create(const progname: ansistring; argv, envp: PPansiChar; Width, Height: Integer);
  42. destructor Destroy; override;
  43. function Read(var Buffer; Bytes: SizeUInt): SizeInt;
  44. procedure Write(const Buffer; Bytes: SizeUInt);
  45. procedure Resize(NewWidth, NewHeight: Integer);
  46. end;
  47. implementation
  48. uses
  49. {$IFDEF FPC_DOTTEDUNITS}
  50. System.SysUtils, UnixApi.Base;
  51. {$ELSE FPC_DOTTEDUNITS}
  52. SysUtils, BaseUnix;
  53. {$ENDIF FPC_DOTTEDUNITS}
  54. { TTerminalInputOutputConnection_UnixPty }
  55. function TTerminalInputOutputConnection_UnixPty.IsDataAvailable: Boolean;
  56. var
  57. pfd: tpollfd;
  58. begin
  59. pfd.fd := FUnixPseudoTerminal.FdMaster;
  60. pfd.events := POLLIN;
  61. Result := FpPoll(@pfd, 1, 0) > 0;
  62. end;
  63. function TTerminalInputOutputConnection_UnixPty.IsClosed: Boolean;
  64. var
  65. wstatus: cInt;
  66. wpid: TPid;
  67. begin
  68. Result := False;
  69. wpid := FpWaitPid(FUnixPseudoTerminal.ChildPid, wstatus, WNOHANG);
  70. if wpid = FUnixPseudoTerminal.ChildPid then
  71. begin
  72. if wifexited(wstatus) then
  73. Result := True;
  74. end;
  75. end;
  76. constructor TTerminalInputOutputConnection_UnixPty.Create(
  77. const progname: ansistring; argv, envp: PPAnsiChar; Width, Height: Integer);
  78. begin
  79. FUnixPseudoTerminal := TUnixPseudoTerminal.Create(progname, argv, envp, Width, Height);
  80. end;
  81. destructor TTerminalInputOutputConnection_UnixPty.Destroy;
  82. begin
  83. FreeAndNil(FUnixPseudoTerminal);
  84. inherited Destroy;
  85. end;
  86. function TTerminalInputOutputConnection_UnixPty.Read(var Buffer; Bytes: SizeUInt): SizeInt;
  87. begin
  88. Result := FpRead(FUnixPseudoTerminal.FdMaster, Buffer, Bytes);
  89. end;
  90. procedure TTerminalInputOutputConnection_UnixPty.Write(const Buffer; Bytes: SizeUInt);
  91. var
  92. W: TsSize;
  93. J: LongInt;
  94. begin
  95. if Bytes <= 0 then
  96. exit;
  97. repeat
  98. W := FpWrite(FUnixPseudoTerminal.FdMaster, Buffer, Bytes);
  99. J := FpGetErrno;
  100. until (W <> -1) or ((J <> ESysEINTR) and (J <> ESysEAgain));
  101. end;
  102. procedure TTerminalInputOutputConnection_UnixPty.Resize(NewWidth, NewHeight: Integer);
  103. begin
  104. FUnixPseudoTerminal.Resize(NewWidth, NewHeight);
  105. end;
  106. end.