2
0

termiosproc.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 real_tcsendbreak(fd,duration: cint): cint; cdecl; external name 'tcsendbreak';
  66. Function TCSendBreak(fd,duration:cint):cint;{$ifdef VER2_0}inline;{$endif}
  67. begin
  68. TCSendBreak:=real_tcsendbreak(fd,duration);
  69. end;
  70. function real_tcsetpgrp(fd: cint; pgrp: pid_t): cint; cdecl; external name 'tcsetpgrp';
  71. Function TCSetPGrp(fd,id:cint):cint;{$ifdef VER2_0}inline;{$endif}
  72. begin
  73. TCSetPGrp:=real_tcsetpgrp(fd,id);;
  74. end;
  75. Function TCGetPGrp(fd:cint;var id:cint):cint;{$ifdef VER2_0}inline;{$endif}
  76. var
  77. pid: pid_t;
  78. begin
  79. if isatty(fd)=0 then
  80. exit(-1);
  81. TCGetPGrp:=fpIOCtl(fd,TIOCGPGRP,@pid);
  82. if TCGetPGrp>=0 then
  83. id:=pid;
  84. end;
  85. function real_tcdrain(fd: cint): cint; cdecl; external name 'tcdrain';
  86. Function TCDrain(fd:cint):cint;{$ifdef VER2_0}inline;{$endif}
  87. begin
  88. TCDrain:=fpIOCtl(fd,TIOCDRAIN,nil); {Should set timeout to 1 first?}
  89. end;
  90. function real_tcflow(fd,act:cint): cint; cdecl; external name 'tcflow';
  91. Function TCFlow(fd,act:cint):cint; {$ifdef VER2_0}inline;{$endif}
  92. begin
  93. TCFlow:=real_tcflow(fd,act);
  94. end;
  95. function real_tcflush(fd,qsel: cint): cint; cdecl; external name 'tcflush';
  96. Function TCFlush(fd,qsel:cint):cint; {$ifdef VER2_0}inline;{$endif}
  97. begin
  98. TCFlush:=real_tcflush(fd,qsel);
  99. end;
  100. Function IsATTY (Handle:cint):cint;
  101. {
  102. Check if the filehandle described by 'handle' is a TTY (Terminal)
  103. }
  104. var
  105. t : Termios;
  106. begin
  107. IsAtty:=ord(TCGetAttr(Handle,t) <> -1);
  108. end;
  109. Function IsATTY(var f: text):cint; {$ifdef VER2_0}inline;{$endif}
  110. {
  111. Idem as previous, only now for text variables.
  112. }
  113. begin
  114. IsATTY:=IsaTTY(textrec(f).handle);
  115. end;