home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / lora299s.zip / STDIO.CPP < prev    next >
C/C++ Source or Header  |  1998-05-12  |  4KB  |  225 lines

  1.  
  2. // LoraBBS Version 2.99 Free Edition
  3. // Copyright (C) 1987-98 Marco Maccaferri
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. #include "_ldefs.h"
  20. #include "combase.h"
  21.  
  22. TStdio::TStdio (void)
  23. {
  24.    RxBytes = 0;
  25.    RxPosition = 0;
  26. #if defined(__LINUX__)
  27.    tty_fd = -1;
  28. #endif
  29. }
  30.  
  31. TStdio::~TStdio (void)
  32. {
  33. #if defined(__LINUX__)
  34.    struct vt_mode vtm;
  35.  
  36.    if (tty_fd != -1) {
  37.       ioctl (tty_fd, VT_GETMODE, &vtm);
  38.       vtm.mode = VT_AUTO;
  39.       ioctl (tty_fd, VT_SETMODE, &vtm);
  40.       ioctl (tty_fd, TCSETSW, &old_termio);
  41.       ioctl (tty_fd, KDSKBMODE, K_XLATE);
  42.       close (tty_fd);
  43.       tty_fd = -1;
  44.    }
  45. #endif
  46. }
  47.  
  48. USHORT TStdio::BytesReady (VOID)
  49. {
  50. #if defined(__LINUX__)
  51.    int i;
  52. #endif
  53.    USHORT RetVal = TRUE;
  54.  
  55. #if defined(__LINUX__)
  56.    if (RxBytes == 0) {
  57.       RetVal = FALSE;
  58.       if ((i = read (tty_fd, RxBuffer, sizeof (RxBuffer))) > 0) {
  59.          RxBytes = (USHORT)i;
  60.          RxPosition = 0;
  61.          RetVal = TRUE;
  62.       }
  63.    }
  64. #else
  65.    if (!kbhit ())
  66.       RetVal = FALSE;
  67. #endif
  68.  
  69. #if defined(__OS2__)
  70.    if (RetVal == FALSE)
  71.       DosSleep (1L);
  72. #elif defined(__NT__)
  73.    if (RetVal == FALSE)
  74.       Sleep (1L);
  75. #endif
  76.  
  77.    return (RetVal);
  78. }
  79.  
  80. VOID TStdio::BufferByte (UCHAR byte)
  81. {
  82. #if defined(__LINUX__)
  83.    write (tty_fd, &byte, 1);
  84. #else
  85.    fwrite (&byte, 1, 1, stdout);
  86. #endif
  87. }
  88.  
  89. VOID TStdio::BufferBytes (UCHAR *bytes, USHORT len)
  90. {
  91. #if defined(__LINUX__)
  92.    write (tty_fd, bytes, len);
  93. #else
  94.    fwrite (bytes, len, 1, stdout);
  95. #endif
  96. }
  97.  
  98. USHORT TStdio::Carrier (VOID)
  99. {
  100.    return (TRUE);
  101. }
  102.  
  103. VOID TStdio::ClearOutbound (VOID)
  104. {
  105. }
  106.  
  107. VOID TStdio::ClearInbound (VOID)
  108. {
  109. #if defined(__LINUX__)
  110.    RxBytes = 0;
  111. #endif
  112. }
  113.  
  114. USHORT TStdio::Initialize (VOID)
  115. {
  116.    USHORT RetVal = TRUE;
  117. #if defined(__LINUX__)
  118.    struct vt_mode vtm;
  119. #endif
  120.  
  121. #if defined(__LINUX__)
  122.    tty_fd = fileno (stdin);
  123. //   tty_fd = open ("/dev/ttyS1", O_RDWR);
  124.    fcntl (tty_fd, F_SETFL, O_NONBLOCK);
  125.  
  126.    ioctl (tty_fd, TCGETS, &old_termio);
  127.    new_termio = old_termio;
  128.    new_termio.c_iflag &= ~(ICRNL);
  129.    new_termio.c_lflag &= ~(ISIG|ICANON|ECHO);
  130.    ioctl (tty_fd, TCSETSW, &new_termio);
  131.  
  132. //   ioctl (tty_fd, KDSKBMODE, K_RAW);
  133. //   ioctl (tty_fd, VT_GETMODE, &vtm);
  134. //   vtm.mode = VT_PROCESS;
  135. //   vtm.relsig = SIGUSR1;
  136. //   vtm.acqsig = SIGUSR2;
  137. //   ioctl (tty_fd, VT_SETMODE, &vtm);
  138. #endif
  139.  
  140.    return (RetVal);
  141. }
  142.  
  143. UCHAR TStdio::ReadByte (VOID)
  144. {
  145.    UCHAR c;
  146.  
  147. #if defined(__LINUX__)
  148.    if (RxBytes > 0) {
  149.       c = RxBuffer[RxPosition++];
  150.       RxBytes--;
  151.    }
  152. #else
  153.    c = (UCHAR)getch ();
  154. #endif
  155.  
  156.    return (c);
  157. }
  158.  
  159. USHORT TStdio::ReadBytes (UCHAR *bytes, USHORT len)
  160. {
  161.    USHORT Max = 0;
  162.  
  163.    while (len > 0 && BytesReady () == TRUE) {
  164.       *bytes++ = ReadByte ();
  165.       len--;
  166.       Max++;
  167.    }
  168.  
  169.    return (Max);
  170. }
  171.  
  172. VOID TStdio::SendByte (UCHAR byte)
  173. {
  174. #if defined(__LINUX__)
  175.    write (tty_fd, &byte, 1);
  176. #else
  177.    fwrite (&byte, 1, 1, stdout);
  178.    fflush (stdout);
  179. #endif
  180. }
  181.  
  182. VOID TStdio::SendBytes (UCHAR *bytes, USHORT len)
  183. {
  184. #if defined(__LINUX__)
  185.    write (tty_fd, bytes, len);
  186. #else
  187.    fwrite (bytes, len, 1, stdout);
  188.    fflush (stdout);
  189. #endif
  190. }
  191.  
  192. VOID TStdio::UnbufferBytes (VOID)
  193. {
  194. #if !defined(__LINUX__)
  195.    fflush (stdout);
  196. #endif
  197. }
  198.  
  199. VOID TStdio::SetName (PSZ name)
  200. {
  201.    name = name;
  202. }
  203.  
  204. VOID TStdio::SetCity (PSZ name)
  205. {
  206.    name = name;
  207. }
  208.  
  209. VOID TStdio::SetLevel (PSZ level)
  210. {
  211.    level = level;
  212. }
  213.  
  214. VOID TStdio::SetTimeLeft (ULONG seconds)
  215. {
  216.    seconds = seconds;
  217. }
  218.  
  219. VOID TStdio::SetTime (ULONG seconds)
  220. {
  221.    seconds = seconds;
  222. }
  223.  
  224.  
  225.