termiosproc.inc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. {
  2. }
  3. {******************************************************************************
  4. IOCtl and Termios calls
  5. ******************************************************************************}
  6. {$ifndef FPC_USE_LIBC}
  7. Function TCGetAttr(fd:cint;var tios:TermIOS):cint; inline;
  8. begin
  9. TCGetAttr:=fpIOCtl(fd,TCGETS,@tios);
  10. end;
  11. Function TCSetAttr(fd:cint;OptAct:cint;const tios:TermIOS):cint;
  12. var
  13. nr:TIOCtlRequest;
  14. begin
  15. case OptAct of
  16. TCSANOW : nr:=TCSETS;
  17. TCSADRAIN : nr:=TCSETSW;
  18. TCSAFLUSH : nr:=TCSETSF;
  19. else
  20. begin
  21. fpsetErrNo(ESysEINVAL);
  22. TCSetAttr:=-1;
  23. exit;
  24. end;
  25. end;
  26. TCSetAttr:=fpIOCtl(fd,nr,@Tios);
  27. end;
  28. Procedure CFSetISpeed(var tios:TermIOS;speed:Cardinal); inline;
  29. begin
  30. tios.c_cflag:=(tios.c_cflag and (not CBAUD)) or speed;
  31. end;
  32. Procedure CFSetOSpeed(var tios:TermIOS;speed:Cardinal); inline;
  33. begin
  34. CFSetISpeed(tios,speed);
  35. end;
  36. { checked against glibc 2.3.3 (FK) }
  37. Procedure CFMakeRaw(var tios:TermIOS);
  38. begin
  39. with tios do
  40. begin
  41. c_iflag:=c_iflag and (not (IGNBRK or BRKINT or PARMRK or ISTRIP or
  42. INLCR or IGNCR or ICRNL or IXON));
  43. c_oflag:=c_oflag and (not OPOST);
  44. c_lflag:=c_lflag and (not (ECHO or ECHONL or ICANON or ISIG or IEXTEN));
  45. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or CS8;
  46. c_cc[VMIN]:=1;
  47. c_cc[VTIME]:=0;
  48. end;
  49. end;
  50. Function TCSendBreak(fd,duration:cint):cint; inline;
  51. begin
  52. TCSendBreak:=fpIOCtl(fd,TCSBRK,pointer(ptrint(duration)));
  53. end;
  54. Function TCSetPGrp(fd,id:cint):cint; inline;
  55. begin
  56. TCSetPGrp:=fpIOCtl(fd,TIOCtlRequest(TIOCSPGRP),pointer(ptrint(id)));
  57. end;
  58. Function TCGetPGrp(fd:cint;var id:cint):cint; inline;
  59. begin
  60. TCGetPGrp:=fpIOCtl(fd,TIOCGPGRP,@id);
  61. end;
  62. Function TCDrain(fd:cint):cint; inline;
  63. begin
  64. TCDrain:=fpIOCtl(fd,TCSBRK,pointer(1));
  65. end;
  66. Function TCFlow(fd,act:cint):cint; inline;
  67. begin
  68. TCFlow:=fpIOCtl(fd,TCXONC,pointer(ptrint(act)));
  69. end;
  70. Function TCFlush(fd,qsel:cint):cint; inline;
  71. begin
  72. TCFlush:=fpIOCtl(fd,TCFLSH,pointer(ptrint(qsel)));
  73. end;
  74. Function IsATTY (Handle:cint):cint;
  75. {
  76. Check if the filehandle described by 'handle' is a TTY (Terminal)
  77. }
  78. var
  79. t : Termios;
  80. begin
  81. if TCGetAttr(Handle,t)=0 then
  82. IsAtty:=1
  83. else
  84. IsAtty:=0;
  85. end;
  86. Function IsATTY(var f: text):cint; inline;
  87. {
  88. Idem as previous, only now for text variables.
  89. }
  90. begin
  91. IsATTY:=IsaTTY(textrec(f).handle);
  92. end;
  93. {$else}
  94. // We plan to use FPC_USE_LIBC for Debian/kFreeBSD. This means that we need
  95. // to avoid IOCTLs, since those go to the kernel and need to be FreeBSD specific.
  96. // -> reroute as much as possible to libc.
  97. function real_tcsendbreak(fd,duration: cint): cint; cdecl; external name 'tcsendbreak';
  98. function real_tcdrain(fd: cint): cint; cdecl; external name 'tcdrain';
  99. function real_tcflow(fd,act:cint): cint; cdecl; external name 'tcflow';
  100. function real_tcflush(fd,qsel: cint): cint; cdecl; external name 'tcflush';
  101. Function real_TCSetAttr(fd:cint;OptAct:cint;constref tios:TermIOS):cint; cdecl; external name 'tcsetattr';
  102. Function real_TCGetAttr(fd:cint;var tios:TermIOS):cint; cdecl; external name 'tcgetattr';
  103. function real_tcgetpgrp(fd:cint):pid_t; cdecl; external name 'tcgetpgrp';
  104. function real_tcsetpgrp(fd: cint; pgrp: pid_t): cint; cdecl; external name 'tcsetpgrp';
  105. Function TCGetAttr(fd:cint;var tios:TermIOS):cint; inline;
  106. begin
  107. TCGetAttr:=real_tcgetattr(fd,tios);
  108. end;
  109. Function TCSetAttr(fd:cint;OptAct:cint;const tios:TermIOS):cint;
  110. begin
  111. TCSetAttr:=Real_TCSetAttr(fd,OptAct,tios);
  112. end;
  113. Procedure CFSetISpeed(var tios:TermIOS;speed:Cardinal); inline;
  114. begin
  115. tios.c_cflag:=(tios.c_cflag and (not CBAUD)) or speed;
  116. end;
  117. Procedure CFSetOSpeed(var tios:TermIOS;speed:Cardinal); inline;
  118. begin
  119. CFSetISpeed(tios,speed);
  120. end;
  121. { checked against glibc 2.3.3 (FK) }
  122. Procedure CFMakeRaw(var tios:TermIOS);
  123. begin
  124. with tios do
  125. begin
  126. c_iflag:=c_iflag and (not (IGNBRK or BRKINT or PARMRK or ISTRIP or
  127. INLCR or IGNCR or ICRNL or IXON));
  128. c_oflag:=c_oflag and (not OPOST);
  129. c_lflag:=c_lflag and (not (ECHO or ECHONL or ICANON or ISIG or IEXTEN));
  130. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or CS8;
  131. c_cc[VMIN]:=1;
  132. c_cc[VTIME]:=0;
  133. end;
  134. end;
  135. Function TCSendBreak(fd,duration:cint):cint; inline;
  136. begin
  137. TCSendBreak:=real_tcsendbreak(fd,duration);
  138. end;
  139. Function TCSetPGrp(fd,id:cint):cint; inline;
  140. begin
  141. TCSetPGrp:=real_tcsetpgrp(fd,id);;
  142. end;
  143. Function TCGetPGrp(fd:cint;var id:cint):cint; inline;
  144. begin
  145. id:=real_tcgetpgrp(fd);
  146. tcgetpgrp:=id;
  147. end;
  148. Function TCDrain(fd:cint):cint; inline;
  149. begin
  150. TCDrain:=real_TCDrain(fd);
  151. end;
  152. Function TCFlow(fd,act:cint):cint; inline;
  153. begin
  154. TCFlow:=real_tcflow(fd,act);
  155. end;
  156. Function TCFlush(fd,qsel:cint):cint; inline;
  157. begin
  158. TCFlush:=real_tcflush(fd,qsel);
  159. end;
  160. Function IsATTY (Handle:cint):cint;
  161. {
  162. Check if the filehandle described by 'handle' is a TTY (Terminal)
  163. }
  164. var
  165. t : Termios;
  166. begin
  167. if TCGetAttr(Handle,t)=0 then
  168. IsAtty:=1
  169. else
  170. IsAtty:=0;
  171. end;
  172. Function IsATTY(var f: text):cint; inline;
  173. {
  174. Idem as previous, only now for text variables.
  175. }
  176. begin
  177. IsATTY:=IsaTTY(textrec(f).handle);
  178. end;
  179. {$endif}