home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnuc / gen_library / rcs / termios.c,v < prev    next >
Encoding:
Text File  |  1992-07-04  |  5.1 KB  |  273 lines

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