summaryrefslogtreecommitdiff
path: root/libc/termios/termios.c
blob: a617e86f4d9c8071f3908173d8786628f840b42c (plain)
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
209
210
211
212
213
214
215
216
217
218
219
220
/* Copyright (C) 1996 Robert de Bath <robert@mayday.compulink.co.uk> This
 * file is part of the Linux-8086 C library and is distributed under the
 * GNU Library General Public License.
 */

/* Note: This is based loosely on the Glib termios routines. */

#ifndef __MSDOS__

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

#ifdef L_isatty
isatty(fd)
int fd;
{
   struct termios term;
   int rv, err = errno;
   rv= (ioctl(fd, TCGETS, &term)==0);
   if( rv==0 && errno == ENOSYS )
      rv = (fd<3);
   errno = err;
   return rv;
}
#endif 

#ifdef L_tcgetattr
int
tcgetattr(fd, term)
int   fd;
struct termios *term;
{
   return ioctl(fd, TCGETS, term);
}
#endif

#ifdef L_tcsetattr
int
tcsetattr(fildes, optional_actions, termios_p)
int fildes;
int optional_actions;
struct termios *termios_p;
{
   switch (optional_actions)
   {
   case TCSANOW:
      return ioctl(fildes, TCSETS, termios_p);
   case TCSADRAIN:
      return ioctl(fildes, TCSETSW, termios_p);
   case TCSAFLUSH:
      return ioctl(fildes, TCSETSF, termios_p);
   default:
      errno = EINVAL;
      return -1;
   }
}
#endif

#ifdef L_tcdrain
/* Wait for pending output to be written on FD.  */
int
tcdrain(fd)
int   fd;
{
   /* With an argument of 1, TCSBRK just waits for output to drain.  */
   return ioctl(fd, TCSBRK, 1);
}
#endif

#ifdef L_tcflow
int 
tcflow(fd, action)
int fd;
int action;
{
   return ioctl(fd, TCXONC, action);
}
#endif

#ifdef L_tcflush
/* Flush pending data on FD.  */
int
tcflush(fd, queue_selector)
int   fd;
int   queue_selector;
{
   return ioctl(fd, TCFLSH, queue_selector);
}
#endif

#ifdef L_tcsendbreak
/* Send zero bits on FD.  */
int
tcsendbreak(fd, duration)
int   fd;
int   duration;
{
   /*
    * The break lasts 0.25 to 0.5 seconds if DURATION is zero, and an
    * implementation-defined period if DURATION is nonzero. We define a
    * positive DURATION to be number of milliseconds to break.
    */
   if (duration <= 0)
      return ioctl(fd, TCSBRK, 0);

   /*
    * ioctl can't send a break of any other duration for us. This could be
    * changed to use trickery (e.g. lower speed and send a '\0') to send
    * the break, but for now just return an error.
    */
   errno = EINVAL;
   return -1;
}
#endif

#ifdef L_tcsetpgrp
/* Set the foreground process group ID of FD set PGRP_ID.  */
int
tcsetpgrp(fd, pgrp_id)
int   fd;
pid_t pgrp_id;
{
   return ioctl(fd, TIOCSPGRP, &pgrp_id);
}
#endif

#ifdef L_tcgetpgrp
/* Return the foreground process group ID of FD.  */
pid_t
tcgetpgrp(fd)
int   fd;
{
   int   pgrp;
   if (ioctl(fd, TIOCGPGRP, &pgrp) < 0)
      return (pid_t) - 1;
   return (pid_t) pgrp;
}
#endif

#ifdef L_cfgetospeed
speed_t cfgetospeed(tp)
struct termios *tp;
{
    return (tp->c_cflag & CBAUD);
}
#endif

#ifdef L_cfgetispeed
speed_t cfgetispeed(tp)
struct termios *tp;
{
    return (tp->c_cflag & CBAUD);
}
#endif

#ifdef L_cfsetospeed
int cfsetospeed(tp, speed)
struct termios *tp; speed_t speed;
{
#ifdef CBAUDEX
    if ((speed & ~CBAUD) || 
	((speed & CBAUDEX) && (speed < B57600 || speed > B115200)))
	return 0;
#else
    if (speed & ~CBAUD)
	return 0;
#endif
    tp->c_cflag &= ~CBAUD;
    tp->c_cflag |= speed;

    return 0;
}
#endif

#ifdef L_cfsetispeed
int cfsetispeed(tp, speed)
struct termios *tp; speed_t speed;
{
    return cfsetospeed(tp, speed);
}
#endif
 
/* From linux libc-4.6.27 again */
#ifdef L_cfmakeraw
/* Copyright (C) 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.*/

void
cfmakeraw(t)
struct termios *t;
{
/* I changed it to the current form according to the suggestions 
 * from Bruce Evans. Thanks Bruce. Please report the problems to
 * H.J. Lu (hlu@eecs.wsu.edu).
 */

/*
 * I took out the bits commented out by #if 1...#else    - RHP
 */

    /*  VMIN = 0 means non-blocking for Linux */
    t->c_cc[VMIN] = 1; t->c_cc[VTIME] = 1;
    /* clear some bits with &= ~(bits), set others with |= */
    t->c_cflag &= ~(CSIZE|PARENB|CSTOPB);
    t->c_cflag |=  (CS8|HUPCL|CREAD);
    t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|INPCK|ISTRIP);
    t->c_iflag &= ~(INLCR|IGNCR|ICRNL|IXON|IXOFF);
    t->c_iflag |=  (BRKINT|IGNPAR);
    t->c_oflag &= ~(OPOST|OLCUC|OCRNL|ONOCR|ONLRET|OFILL|OFDEL);
    t->c_oflag &= ~(NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY);
    t->c_oflag |=  (ONLCR|NL0|CR0|TAB3|BS0|VT0|FF0);
    t->c_lflag &= ~(ISIG|ICANON|IEXTEN|ECHO|ECHOE|ECHOK|ECHONL);
    t->c_lflag &= ~(NOFLSH|XCASE);
    t->c_lflag &= ~(ECHOPRT|ECHOCTL|ECHOKE);
}
#endif

#endif