termiosproc.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. {
  2. This file is part of the Free Pascal run time library.
  3. (c) 2000-2003 by Marco van de Voort
  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. Termios implementation for FreeBSD
  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: TIoCtlRequest;
  22. begin
  23. case OptAct of
  24. {the three constants TIOCSETA, TIOCSETAW and TIOCSETAF are
  25. unsigned values above $80000000, so that they give range check errors
  26. on 32-bit systems
  27. Solved by using TIoCtlRequest type for FpIOCtl second parameter }
  28. TCSANOW : nr:=TIOCSETA;
  29. TCSADRAIN : nr:=TIOCSETAW;
  30. TCSAFLUSH : nr:=TIOCSETAF;
  31. else
  32. begin
  33. fpsetErrNo(ESysEINVAL);
  34. TCSetAttr:=-1;
  35. exit;
  36. end;
  37. end;
  38. TCSetAttr:=fpIOCtl(fd,nr,@Tios);
  39. end;
  40. Procedure CFSetISpeed(var tios:TermIOS;speed:Cardinal); {$ifdef VER2_0}inline;{$endif}
  41. begin
  42. tios.c_ispeed:=speed; {Probably the Bxxxx speed constants}
  43. end;
  44. Procedure CFSetOSpeed(var tios:TermIOS;speed:Cardinal); {$ifdef VER2_0}inline;{$endif}
  45. begin
  46. tios.c_ospeed:=speed;
  47. end;
  48. Procedure CFMakeRaw(var tios:TermIOS);
  49. begin
  50. with tios do
  51. begin
  52. c_iflag:=c_iflag and (not (IMAXBEL or IXOFF or INPCK or BRKINT or
  53. PARMRK or ISTRIP or INLCR or IGNCR or ICRNL or IXON or
  54. IGNPAR));
  55. c_iflag:=c_iflag OR IGNBRK;
  56. c_oflag:=c_oflag and (not OPOST);
  57. c_lflag:=c_lflag and (not (ECHO or ECHOE or ECHOK or ECHONL or ICANON or
  58. ISIG or IEXTEN or NOFLSH or TOSTOP or PENDIN));
  59. c_cflag:=(c_cflag and (not (CSIZE or PARENB))) or (CS8 OR cread);
  60. c_cc[VMIN]:=1;
  61. c_cc[VTIME]:=0;
  62. end;
  63. end;
  64. Function TCSendBreak(fd,duration:cint):cint; {$ifdef VER2_0}inline;{$endif}
  65. var
  66. sleepytime : ttimeval;
  67. begin
  68. sleepytime.tv_sec := 0;
  69. sleepytime.tv_usec := 400000;
  70. if fpioctl(fd, TIOCSBRK, nil) = -1 then
  71. exit(-1);
  72. fpselect(0, nil, nil, nil, @sleepytime);
  73. if fpioctl(fd, TIOCCBRK, nil) = -1 then
  74. exit(-1);
  75. TCSendBreak:=0;
  76. end;
  77. Function TCSetPGrp(fd,id:cint):cint; {$ifdef VER2_0}inline;{$endif}
  78. begin
  79. TCSetPGrp:=fpIOCtl(fd,TIOCSPGRP,pointer(id));
  80. end;
  81. Function TCGetPGrp(fd:cint;var id:cint):cint; {$ifdef VER2_0}inline;{$endif}
  82. begin
  83. TCGetPGrp:=fpIOCtl(fd,TIOCGPGRP,@id);
  84. end;
  85. Function TCDrain(fd:cint):cint; {$ifdef VER2_0}inline;{$endif}
  86. begin
  87. TCDrain:=fpIOCtl(fd,TIOCDRAIN,nil); {Should set timeout to 1 first?}
  88. end;
  89. const
  90. _POSIX_VDISABLE = $ff;
  91. Function TCFlow(fd,act:cint):cint; {$ifdef VER2_0}inline;{$endif}
  92. var
  93. term:Termios;
  94. c : cuchar;
  95. tmp : cint;
  96. begin
  97. case act OF
  98. TCOOFF : TCFlow:=fpIoctl(fd,TIOCSTOP,nil);
  99. TCOOn : TCFlow:=fpIOctl(Fd,TIOCStart,nil);
  100. TCION,
  101. TCIOFF : begin
  102. if tcgetattr(fd, term) = -1 then
  103. exit(-1);
  104. if act=TCIOFF then
  105. tmp:=VSTOP
  106. else
  107. tmp:=VSTART;
  108. c:=term.c_cc[tmp];
  109. if (c <> _POSIX_VDISABLE) and
  110. (fpwrite(fd, c, sizeof(c)) = -1) then
  111. exit (-1);
  112. TCFlow:=0;
  113. end;
  114. else
  115. begin
  116. errno := esysEINVAL;
  117. exit(-1);
  118. end;
  119. end;
  120. end;
  121. const FREAD = 1; // marked "BSD visible"
  122. FWRITE = 2;
  123. Function TCFlush(fd,qsel:cint):cint; {$ifdef VER2_0}inline;{$endif}
  124. var comval : cint;
  125. begin
  126. case qsel of
  127. TCIFlush : comval:=FREAD;
  128. TCOFlush : comval:=FWRITE;
  129. TCIOFlush : comval:=FREAD or FWRITE;
  130. else
  131. begin
  132. errno:=ESysEINVAL;
  133. exit(-1);
  134. end;
  135. end;
  136. TCFlush:=fpIOCtl(fd,TIOCFLUSH,pointer(@comval));
  137. end;
  138. Function IsATTY (Handle:cint):cint;
  139. {
  140. Check if the filehandle described by 'handle' is a TTY (Terminal)
  141. }
  142. var
  143. t : Termios;
  144. begin
  145. IsAtty:=ord(TCGetAttr(Handle,t) <> -1);
  146. end;
  147. Function IsATTY(var f: text):cint; {$ifdef VER2_0}inline;{$endif}
  148. {
  149. Idem as previous, only now for text variables.
  150. }
  151. begin
  152. IsATTY:=IsaTTY(textrec(f).handle);
  153. end;