termiosproc.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. {******************************************************************************
  2. IOCtl and Termios calls
  3. ******************************************************************************}
  4. Function TCGetAttr(fd:cint;var tios:TermIOS):cint;
  5. begin
  6. {$ifndef BSD}
  7. TCGetAttr:=fpIOCtl(fd,TCGETS,@tios);
  8. {$else}
  9. TCGETAttr:=fpIoCtl(Fd,TIOCGETA,@tios);
  10. {$endif}
  11. end;
  12. Function TCSetAttr(fd:cint;OptAct:cint;const tios:TermIOS):cint;
  13. var
  14. nr:cint;
  15. begin
  16. {$ifndef BSD}
  17. case OptAct of
  18. TCSANOW : nr:=TCSETS;
  19. TCSADRAIN : nr:=TCSETSW;
  20. TCSAFLUSH : nr:=TCSETSF;
  21. {$else}
  22. case OptAct of
  23. TCSANOW : nr:=TIOCSETA;
  24. TCSADRAIN : nr:=TIOCSETAW;
  25. TCSAFLUSH : nr:=TIOCSETAF;
  26. {$endif}
  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);
  37. begin
  38. {$ifndef BSD}
  39. tios.c_cflag:=(tios.c_cflag and (not CBAUD)) or speed;
  40. {$else}
  41. tios.c_ispeed:=speed; {Probably the Bxxxx speed constants}
  42. {$endif}
  43. end;
  44. Procedure CFSetOSpeed(var tios:TermIOS;speed:Cardinal);
  45. begin
  46. {$ifndef BSD}
  47. CFSetISpeed(tios,speed);
  48. {$else}
  49. tios.c_ospeed:=speed;
  50. {$endif}
  51. end;
  52. Procedure CFMakeRaw(var tios:TermIOS);
  53. begin
  54. {$ifndef BSD}
  55. with tios do
  56. begin
  57. c_iflag:=c_iflag and (not (IGNBRK or BRKINT or PARMRK or ISTRIP or
  58. INLCR or IGNCR or ICRNL or IXON));
  59. c_oflag:=c_oflag and (not OPOST);
  60. c_lflag:=c_lflag and (not (ECHO or ECHONL or ICANON or ISIG or IEXTEN));
  61. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or CS8;
  62. end;
  63. {$else}
  64. with tios do
  65. begin
  66. c_iflag:=c_iflag and (not (IMAXBEL or IXOFF or INPCK or BRKINT or
  67. PARMRK or ISTRIP or INLCR or IGNCR or ICRNL or IXON or
  68. IGNPAR));
  69. c_iflag:=c_iflag OR IGNBRK;
  70. c_oflag:=c_oflag and (not OPOST);
  71. c_lflag:=c_lflag and (not (ECHO or ECHOE or ECHOK or ECHONL or ICANON or
  72. ISIG or IEXTEN or NOFLSH or TOSTOP or PENDIN));
  73. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or (CS8 OR cread);
  74. c_cc[VMIN]:=1;
  75. c_cc[VTIME]:=0;
  76. end;
  77. {$endif}
  78. end;
  79. Function TCSendBreak(fd,duration:cint):cint;
  80. begin
  81. {$ifndef BSD}
  82. TCSendBreak:=fpIOCtl(fd,TCSBRK,pointer(duration));
  83. {$else}
  84. TCSendBreak:=fpIOCtl(fd,TIOCSBRK,0);
  85. {$endif}
  86. end;
  87. Function TCSetPGrp(fd,id:cint):cint;
  88. begin
  89. TCSetPGrp:=fpIOCtl(fd,TIOCSPGRP,pointer(id));
  90. end;
  91. Function TCGetPGrp(fd:cint;var id:cint):cint;
  92. begin
  93. TCGetPGrp:=fpIOCtl(fd,TIOCGPGRP,@id);
  94. end;
  95. Function TCDrain(fd:cint):cint;
  96. begin
  97. {$ifndef BSD}
  98. TCDrain:=fpIOCtl(fd,TCSBRK,pointer(1));
  99. {$else}
  100. TCDrain:=fpIOCtl(fd,TIOCDRAIN,0); {Should set timeout to 1 first?}
  101. {$endif}
  102. end;
  103. Function TCFlow(fd,act:cint):cint;
  104. begin
  105. {$ifndef BSD}
  106. TCFlow:=fpIOCtl(fd,TCXONC,pointer(act));
  107. {$else}
  108. case act OF
  109. TCOOFF : TCFlow:=fpIoctl(fd,TIOCSTOP,0);
  110. TCOOn : TCFlow:=fpIOctl(Fd,TIOCStart,0);
  111. TCIOFF : {N/I}
  112. end;
  113. {$endif}
  114. end;
  115. Function TCFlush(fd,qsel:cint):cint;
  116. begin
  117. {$ifndef BSD}
  118. TCFlush:=fpIOCtl(fd,TCFLSH,pointer(qsel));
  119. {$else}
  120. TCFlush:=fpIOCtl(fd,TIOCFLUSH,pointer(qsel));
  121. {$endif}
  122. end;
  123. Function IsATTY (Handle:cint):cint;
  124. {
  125. Check if the filehandle described by 'handle' is a TTY (Terminal)
  126. }
  127. var
  128. t : Termios;
  129. begin
  130. IsAtty:=TCGetAttr(Handle,t);
  131. end;
  132. Function IsATTY(var f: text):cint;
  133. {
  134. Idem as previous, only now for text variables.
  135. }
  136. begin
  137. IsATTY:=IsaTTY(textrec(f).handle);
  138. end;