home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / general / termios.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  5KB  |  216 lines

  1. /*-
  2.  * Copyright (c) 1989 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)termios.c    5.9 (Berkeley) 5/20/91";
  36. #endif /* LIBC_SCCS and not lint */
  37.  
  38. #define _KERNEL
  39. #include "ixemul.h"
  40. #include "kprintf.h"
  41.  
  42. #include <sys/types.h>
  43. #include <sys/errno.h>
  44. #undef _KERNEL
  45. #include <sys/ioctl.h>
  46. #include <sys/tty.h>
  47. #define _KERNEL    /* XXX - FREAD and FWRITE was ifdef'd _KERNEL*/
  48. #include <sys/fcntl.h>
  49. #undef _KERNEL
  50. #include <termios.h>
  51. #include <stdio.h>
  52. #include <unistd.h>
  53.  
  54. int tcgetattr(int fd, struct termios *t)
  55. {
  56.     return(ioctl(fd, TIOCGETA, t));
  57. }
  58.  
  59. int tcsetattr(int fd, int opt, const struct termios *t)
  60. {
  61.     struct termios localterm;
  62.  
  63.     if (opt & TCSASOFT) {
  64.         localterm = *t;
  65.         localterm.c_cflag |= CIGNORE;
  66.         t = &localterm;
  67.         opt &= ~TCSASOFT;
  68.     }
  69.     if (opt == TCSANOW)
  70.         return (ioctl(fd, TIOCSETA, t));
  71.     else if (opt == TCSADRAIN)
  72.         return (ioctl(fd, TIOCSETAW, t));
  73.     return (ioctl(fd, TIOCSETAF, t));
  74. }
  75.  
  76. int tcsetpgrp(int fd, pid_t pgrp)
  77. {
  78.     int s;
  79.  
  80.     s = pgrp;
  81.     return(ioctl(fd, TIOCSPGRP, &s));
  82. }
  83.  
  84. pid_t tcgetpgrp(int fd)
  85. {
  86.     int s;
  87.  
  88.     if (ioctl(fd, TIOCGPGRP, &s) < 0)
  89.         return((pid_t)-1);
  90.  
  91.     return((pid_t)s);
  92. }
  93.  
  94. speed_t cfgetospeed(const struct termios *t)
  95. {
  96.     return(t->c_ospeed);
  97. }
  98.  
  99. speed_t cfgetispeed(const struct termios *t)
  100. {
  101.     return(t->c_ispeed);
  102. }
  103.  
  104. int cfsetospeed(struct termios *t, speed_t speed)
  105. {
  106.     t->c_ospeed = speed;
  107.  
  108.     return (0);
  109. }
  110.  
  111. int cfsetispeed(struct termios *t, speed_t speed)
  112. {
  113.     t->c_ispeed = speed;
  114.  
  115.     return (0);
  116. }
  117.  
  118. int cfsetspeed(struct termios *t, speed_t speed)
  119. {
  120.     t->c_ispeed = t->c_ospeed = speed;
  121.     return 0;
  122. }
  123.  
  124. /*
  125.  * Make a pre-existing termios structure into "raw" mode:
  126.  * character-at-a-time mode with no characters interpreted,
  127.  * 8-bit data path.
  128.  */
  129. void cfmakeraw(struct termios *t)
  130. {
  131.     t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
  132.     t->c_oflag &= ~OPOST;
  133.     t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  134.     t->c_cflag &= ~(CSIZE|PARENB);
  135.     t->c_cflag |= CS8;
  136.     /* set MIN/TIME */
  137. }
  138.  
  139. int tcsendbreak(int fd, int len)
  140. {
  141.     struct timeval sleepytime;
  142.  
  143.     sleepytime.tv_sec = 0;
  144.     sleepytime.tv_usec = 400000;
  145.     if (ioctl(fd, TIOCSBRK, 0) == -1)
  146.         return (-1);
  147.     select(0, 0, 0, 0, &sleepytime);
  148.     if (ioctl(fd, TIOCCBRK, 0) == -1)
  149.         return (-1);
  150.  
  151.     return (0);
  152. }
  153.  
  154. int tcdrain(int fd)
  155. {
  156.     if (ioctl(fd, TIOCDRAIN, 0) == -1)
  157.         return (-1);
  158.  
  159.     return (0);
  160. }
  161.  
  162. int tcflush(int fd, int which)
  163. {
  164.     int com;
  165.  
  166.     switch (which) {
  167.     case TCIFLUSH:
  168.         com = FREAD;
  169.         break;
  170.     case TCOFLUSH:
  171.         com = FWRITE;
  172.         break;
  173.     case TCIOFLUSH:
  174.         com = FREAD | FWRITE;
  175.         break;
  176.     default:
  177.         errno = EINVAL;
  178.         KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  179.         return (-1);
  180.     }
  181.     if (ioctl(fd, TIOCFLUSH, &com) == -1)
  182.         return (-1);
  183.  
  184.     return (0);
  185. }
  186.  
  187. int tcflow(int fd, int action)
  188. {
  189.     switch (action) {
  190.     case TCOOFF:
  191.         return (ioctl(fd, TIOCSTOP, 0));
  192.         break;
  193.     case TCOON:
  194.         return (ioctl(fd, TIOCSTART, 0));
  195.         break;
  196.     case TCIOFF:
  197.     case TCION: {        /* these posix functions are STUPID */
  198.         struct termios term;
  199.         unsigned char c;
  200.  
  201.         if (tcgetattr(fd, &term) == -1)
  202.             return (-1);
  203.         c = term.c_cc[action == TCIOFF ? VSTOP : VSTART];
  204.         if (c != _POSIX_VDISABLE && write(fd, &c, 1) == -1)
  205.             return (-1);
  206.         break;
  207.     }
  208.     default:
  209.         errno = EINVAL;
  210.         KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  211.         return (-1);
  212.     }
  213.  
  214.     return (0);
  215. }
  216.