aboutsummaryrefslogtreecommitdiff
path: root/stand/kboot/libkboot/termios.c
blob: cd25b252f735e4b1e699f6009ea1851b5b1cc04a (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
/*
 * Copyright (c) 2005-2020 Rich Felker, et al.
 *
 * SPDX-License-Identifier: MIT
 *
 * Note: From the musl project, stripped down and repackaged with HOST_/host_ prepended
 */

#include <sys/types.h>
#include "termios.h"
#include "host_syscall.h"

int
host_tcgetattr(int fd, struct host_termios *tio)
{
	if (host_ioctl(fd, HOST_TCGETS, (uintptr_t)tio))
		return -1;
	return 0;
}

int
host_tcsetattr(int fd, int act, const struct host_termios *tio)
{
	if (act < 0 || act > 2) {
		errno = EINVAL;	/* XXX ?? */
		return -1;
	}
	return host_ioctl(fd, HOST_TCSETS+act, (uintptr_t)tio);
}

void
host_cfmakeraw(struct host_termios *t)
{
	t->c_iflag &= ~(HOST_IGNBRK | HOST_BRKINT | HOST_PARMRK | HOST_ISTRIP |
	    HOST_INLCR | HOST_IGNCR | HOST_ICRNL | HOST_IXON);
	t->c_oflag &= ~HOST_OPOST;
	t->c_lflag &= ~(HOST_ECHO | HOST_ECHONL | HOST_ICANON | HOST_ISIG |
	    HOST_IEXTEN);
	t->c_cflag &= ~(HOST_CSIZE | HOST_PARENB);
	t->c_cflag |= HOST_CS8;
	t->c_cc[HOST_VMIN] = 1;
	t->c_cc[HOST_VTIME] = 0;
}

int host_cfsetospeed(struct host_termios *tio, host_speed_t speed)
{
	if (speed & ~HOST_CBAUD) {
//		errno = EINVAL; /* XXX ? */
		return -1;
	}
	tio->c_cflag &= ~HOST_CBAUD;
	tio->c_cflag |= speed;
	return 0;
}

int host_cfsetispeed(struct host_termios *tio, host_speed_t speed)
{
	return speed ? host_cfsetospeed(tio, speed) : 0;
}

int
host_cfsetspeed(struct host_termios *tio, host_speed_t speed)
{
	return host_cfsetospeed(tio, speed);	/* weak alias in musl */
}