aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/options/posix/generic/termios-stubs.cpp
blob: 631456aa53bd467da636cf1d9d7996f374a6d226 (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
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif

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

#include <bits/ensure.h>
#include <mlibc/posix-sysdeps.hpp>

speed_t cfgetispeed(const struct termios *tios) {
	return tios->c_cflag & CBAUD;
}

speed_t cfgetospeed(const struct termios *tios) {
	return tios->c_cflag & CBAUD;
}

int cfsetispeed(struct termios *termios, speed_t speed) {
	return speed ? cfsetospeed(termios, speed) : 0;
}

int cfsetospeed(struct termios *termios, speed_t speed) {
	if(speed & ~CBAUD) {
		errno = EINVAL;
		return -1;
	}

	termios->c_cflag &= ~CBAUD;
	termios->c_cflag |= speed;

	return 0;
}

void cfmakeraw(struct termios *t) {
	t->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
	t->c_oflag &= ~OPOST;
	t->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
	t->c_cflag &= ~(CSIZE | PARENB);
	t->c_cflag |= CS8;
	t->c_cc[VMIN] = 1;
	t->c_cc[VTIME] = 0;
}

int tcdrain(int fd) {
	MLIBC_CHECK_OR_ENOSYS(mlibc::sys_tcdrain, -1);
	if(int e = mlibc::sys_tcdrain(fd); e) {
		errno = e;
		return -1;
	}
	return 0;
}

int tcflow(int fd, int action) {
	MLIBC_CHECK_OR_ENOSYS(mlibc::sys_tcflow, -1);
	if(int e = mlibc::sys_tcflow(fd, action); e) {
		errno = e;
		return -1;
	}
	return 0;
}

int tcflush(int fd, int queue_selector) {
	MLIBC_CHECK_OR_ENOSYS(mlibc::sys_tcflush, -1);
	if(int e = mlibc::sys_tcflush(fd, queue_selector); e) {
		errno = e;
		return -1;
	}
	return 0;
}

int tcgetattr(int fd, struct termios *attr) {
	MLIBC_CHECK_OR_ENOSYS(mlibc::sys_tcgetattr, -1);
	if(int e = mlibc::sys_tcgetattr(fd, attr); e) {
		errno = e;
		return -1;
	}
	return 0;
}

pid_t tcgetsid(int fd) {
	int sid;
	if(ioctl(fd, TIOCGSID, &sid) < 0) {
		return -1;
	}
	return sid;
}

int tcsendbreak(int, int) {
	__ensure(!"Not implemented");
	__builtin_unreachable();
}

int tcsetattr(int fd, int opts, const struct termios *attr) {
	MLIBC_CHECK_OR_ENOSYS(mlibc::sys_tcsetattr, -1);
	if(int e = mlibc::sys_tcsetattr(fd, opts, attr); e) {
		errno = e;
		return -1;
	}
	return 0;
}