termiosproc.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Peter Vreman
  4. member of the Free Pascal development team.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This file contains the implementation of several termio(s) functions
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {******************************************************************************
  13. IOCtl and Termios calls
  14. ******************************************************************************}
  15. Function TCGetAttr(fd:cint;var tios:TermIOS):cint; {$ifdef VER2_0}inline;{$endif}
  16. begin
  17. TCGETAttr:=fpIoCtl(Fd,TIOCGETA,@tios);
  18. end;
  19. Function TCSetAttr(fd:cint;OptAct:cint;const tios:TermIOS):cint;
  20. var
  21. nr:culong;
  22. begin
  23. case OptAct of
  24. TCSANOW : nr:=TIOCSETA;
  25. TCSADRAIN : nr:=TIOCSETAW;
  26. TCSAFLUSH : nr:=TIOCSETAF;
  27. else
  28. begin
  29. fpsetErrNo(ESysEINVAL);
  30. TCSetAttr:=-1;
  31. exit;
  32. end;
  33. end;
  34. TCSetAttr:=fpIOCtl(fd,nr,@Tios);
  35. end;
  36. Procedure CFSetISpeed(var tios:TermIOS;speed:Cardinal); {$ifdef VER2_0}inline;{$endif}
  37. begin
  38. tios.c_ispeed:=speed; {Probably the Bxxxx speed constants}
  39. end;
  40. Procedure CFSetOSpeed(var tios:TermIOS;speed:Cardinal); {$ifdef VER2_0}inline;{$endif}
  41. begin
  42. tios.c_ospeed:=speed;
  43. end;
  44. Procedure CFMakeRaw(var tios:TermIOS);
  45. begin
  46. with tios do
  47. begin
  48. c_iflag:=c_iflag and (not (IMAXBEL or IXOFF or INPCK or BRKINT or
  49. PARMRK or ISTRIP or INLCR or IGNCR or ICRNL or IXON or
  50. IGNPAR));
  51. c_iflag:=c_iflag OR IGNBRK;
  52. c_oflag:=c_oflag and (not OPOST);
  53. c_lflag:=c_lflag and (not (ECHO or ECHOE or ECHOK or ECHONL or ICANON or
  54. ISIG or IEXTEN or NOFLSH or TOSTOP or PENDIN));
  55. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or (CS8 OR cread);
  56. c_cc[VMIN]:=1;
  57. c_cc[VTIME]:=0;
  58. end;
  59. end;
  60. //Function TCGetAttr(fd:cint;var tios:TermIOS):cint; cdecl; external 'c' name 'tcgetattr';
  61. //Function TCSetAttr(fd:cint;OptAct:cint;const tios:TermIOS):cint; cdecl; external 'c' name 'tcsetattr';
  62. //Procedure CFSetISpeed(var tios:TermIOS;speed:Cardinal); cdecl; external 'c' name 'cfsetispeed';
  63. //Procedure CFSetOSpeed(var tios:TermIOS;speed:Cardinal); cdecl; external 'c' name 'cfsetospeed';
  64. //Procedure CFMakeRaw(var tios:TermIOS); cdecl; external 'c' name 'cfmakeraw';
  65. Function TCSendBreak(fd,duration:cint):cint;{$ifdef VER2_0}inline;{$endif}
  66. begin
  67. TCSendBreak:=fpIOCtl(fd,TIOCSBRK,nil);
  68. end;
  69. Function TCSetPGrp(fd,id:cint):cint;{$ifdef VER2_0}inline;{$endif}
  70. begin
  71. TCSetPGrp:=fpIOCtl(fd,TIOCSPGRP,pointer(ptrint(id)));
  72. end;
  73. Function TCGetPGrp(fd:cint;var id:cint):cint;{$ifdef VER2_0}inline;{$endif}
  74. begin
  75. TCGetPGrp:=fpIOCtl(fd,TIOCGPGRP,@id);
  76. end;
  77. Function TCDrain(fd:cint):cint;{$ifdef VER2_0}inline;{$endif}
  78. begin
  79. TCDrain:=fpIOCtl(fd,TIOCDRAIN,nil); {Should set timeout to 1 first?}
  80. end;
  81. Function TCFlow(fd,act:cint):cint; {$ifdef VER2_0}inline;{$endif}
  82. begin
  83. case act OF
  84. TCOOFF : TCFlow:=fpIoctl(fd,TIOCSTOP,nil);
  85. TCOOn : TCFlow:=fpIOctl(Fd,TIOCStart,nil);
  86. TCIOFF : {N/I}
  87. end;
  88. end;
  89. Function TCFlush(fd,qsel:cint):cint; {$ifdef VER2_0}inline;{$endif}
  90. begin
  91. TCFlush:=fpIOCtl(fd,TIOCFLUSH,pointer(ptrint(qsel)));
  92. end;
  93. Function IsATTY (Handle:cint):cint;
  94. {
  95. Check if the filehandle described by 'handle' is a TTY (Terminal)
  96. }
  97. var
  98. t : Termios;
  99. begin
  100. IsAtty:=ord(TCGetAttr(Handle,t) <> -1);
  101. end;
  102. Function IsATTY(var f: text):cint; {$ifdef VER2_0}inline;{$endif}
  103. {
  104. Idem as previous, only now for text variables.
  105. }
  106. begin
  107. IsATTY:=IsaTTY(textrec(f).handle);
  108. end;