summary refs log tree commit diff
path: root/sysdeps/unix/sysv/tcsetattr.c
blob: 30e1b0b48dc10f7651600bf557a576a88ddc06ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* Copyright (C) 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.

The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.

The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB.  If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA.  */

#include <ansidecl.h>
#include <errno.h>
#include <stddef.h>
#include <termios.h>
#include <sys/ioctl.h>

#include <sysv_termio.h>


CONST speed_t __unix_speeds[] =
  {
    0,
    50,
    75,
    110,
    134,
    150,
    200,
    300,
    600,
    1200,
    1800,
    2400,
    4800,
    9600,
    19200,
    38400,
  };


/* Set the state of FD to *TERMIOS_P.  */
int
DEFUN(tcsetattr, (fd, optional_actions, termios_p),
      int fd AND int optional_actions AND CONST struct termios *termios_p)
{
  struct __sysv_termio buf;
  int ioctl_function;
  size_t i;

  if (termios_p == NULL)
    {
      errno = EINVAL;
      return -1;
    }
  switch (optional_actions)
    {
    case TCSANOW:
      ioctl_function = _TCSETA;
      break;
    case TCSADRAIN:
      ioctl_function = _TCSETAW;
      break;
    case TCSAFLUSH:
      ioctl_function = _TCSETAF;
      break;
    default:
      errno = EINVAL;
      return -1;
    }

  if (termios_p->__ispeed != termios_p->__ospeed)
    {
      errno = EINVAL;
      return -1;
    }
  buf.c_cflag = -1;
  for (i = 0; i <= sizeof (__unix_speeds) / sizeof (__unix_speeds[0]); ++i)
    {
      if (__unix_speeds[i] == termios_p->__ispeed)
	buf.c_cflag = i;
    }
  if (buf.c_cflag == -1)
    {
      errno = EINVAL;
      return -1;
    }

  buf.c_iflag = 0;
  if (termios_p->c_iflag & IGNBRK)
    buf.c_iflag |= _SYSV_IGNBRK;
  if (termios_p->c_iflag & BRKINT)
    buf.c_iflag |= _SYSV_BRKINT;
  if (termios_p->c_iflag & IGNPAR)
    buf.c_iflag |= _SYSV_IGNPAR;
  if (termios_p->c_iflag & PARMRK)
    buf.c_iflag |= _SYSV_PARMRK;
  if (termios_p->c_iflag & INPCK)
    buf.c_iflag |= _SYSV_INPCK;
  if (termios_p->c_iflag & ISTRIP)
    buf.c_iflag |= _SYSV_ISTRIP;
  if (termios_p->c_iflag & INLCR)
    buf.c_iflag |= _SYSV_INLCR;
  if (termios_p->c_iflag & IGNCR)
    buf.c_iflag |= _SYSV_IGNCR;
  if (termios_p->c_iflag & ICRNL)
    buf.c_iflag |= _SYSV_ICRNL;
  if (termios_p->c_iflag & IXON)
    buf.c_iflag |= _SYSV_IXON;
  if (termios_p->c_iflag & IXOFF)
    buf.c_iflag |= _SYSV_IXOFF;
  if (termios_p->c_iflag & IXANY)
    buf.c_iflag |= _SYSV_IXANY;
  if (termios_p->c_iflag & IMAXBEL)
    buf.c_iflag |= _SYSV_IMAXBEL;

  buf.c_oflag = 0;
  if (termios_p->c_oflag & OPOST)
    buf.c_oflag = _SYSV_OPOST;
  if (termios_p->c_oflag & ONLCR)
    buf.c_oflag = _SYSV_ONLCR;

  /* So far, buf.c_cflag contains the speed in CBAUD.  */
  if (termios_p->c_cflag & CSTOPB)
    buf.c_cflag |= _SYSV_CSTOPB;
  if (termios_p->c_cflag & CREAD)
    buf.c_cflag |= _SYSV_CREAD;
  if (termios_p->c_cflag & PARENB)
    buf.c_cflag |= _SYSV_PARENB;
  if (termios_p->c_cflag & PARODD)
    buf.c_cflag |= _SYSV_PARODD;
  if (termios_p->c_cflag & HUPCL)
    buf.c_cflag |= _SYSV_HUPCL;
  if (termios_p->c_cflag & CLOCAL)
    buf.c_cflag |= _SYSV_CLOCAL;
  switch (termios_p->c_cflag & CSIZE)
    {
    case CS5:
      buf.c_cflag |= _SYSV_CS5;
      break;
    case CS6:
      buf.c_cflag |= _SYSV_CS6;
      break;
    case CS7:
      buf.c_cflag |= _SYSV_CS7;
      break;
    case CS8:
      buf.c_cflag |= _SYSV_CS8;
      break;
    }

  buf.c_lflag = 0;
  if (termios_p->c_lflag & ISIG)
    buf.c_lflag |= _SYSV_ISIG;
  if (termios_p->c_lflag & ICANON)
    buf.c_lflag |= _SYSV_ICANON;
  if (termios_p->c_lflag & ECHO)
    buf.c_lflag |= _SYSV_ECHO;
  if (termios_p->c_lflag & ECHOE)
    buf.c_lflag |= _SYSV_ECHOE;
  if (termios_p->c_lflag & ECHOK)
    buf.c_lflag |= _SYSV_ECHOK;
  if (termios_p->c_lflag & ECHONL)
    buf.c_lflag |= _SYSV_ECHONL;
  if (termios_p->c_lflag & NOFLSH)
    buf.c_lflag |= _SYSV_NOFLSH;
  if (termios_p->c_lflag & TOSTOP)
    buf.c_lflag |= _SYSV_TOSTOP;
  if (termios_p->c_lflag & ECHOCTL)
    buf.c_lflag |= _SYSV_ECHOCTL;
  if (termios_p->c_lflag & ECHOPRT)
    buf.c_lflag |= _SYSV_ECHOPRT;
  if (termios_p->c_lflag & ECHOKE)
    buf.c_lflag |= _SYSV_ECHOKE;
  if (termios_p->c_lflag & FLUSHO)
    buf.c_lflag |= _SYSV_FLUSHO;
  if (termios_p->c_lflag & PENDIN)
    buf.c_lflag |= _SYSV_PENDIN;
  if (termios_p->c_lflag & IEXTEN)
    buf.c_lflag |= _SYSV_IEXTEN;

  buf.c_cc[_SYSV_VINTR] = termios_p->c_cc[VINTR];
  buf.c_cc[_SYSV_VQUIT] = termios_p->c_cc[VQUIT];
  buf.c_cc[_SYSV_VERASE] = termios_p->c_cc[VERASE];
  buf.c_cc[_SYSV_VKILL] = termios_p->c_cc[VKILL];
  if (buf.c_lflag & _SYSV_ICANON)
    {
      buf.c_cc[_SYSV_VEOF] = termios_p->c_cc[VEOF];
      buf.c_cc[_SYSV_VEOL] = termios_p->c_cc[VEOL];
    }
  else
    {
      buf.c_cc[_SYSV_VMIN] = termios_p->c_cc[VMIN];
      buf.c_cc[_SYSV_VTIME] = termios_p->c_cc[VTIME];
    }
  buf.c_cc[_SYSV_VEOL2] = termios_p->c_cc[VEOL2];

  if (__ioctl (fd, ioctl_function, &buf) < 0)
    return -1;
  return 0;
}