xref: /freebsd/stand/kboot/libkboot/termios.c (revision 62a52c15422470f97fc7b311d89c83f910bcc1b1)
1 /*
2  * Copyright (c) 2005-2020 Rich Felker, et al.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Note: From the musl project, stripped down and repackaged with HOST_/host_ prepended
7  */
8 
9 #include <sys/types.h>
10 #include "termios.h"
11 #include "host_syscall.h"
12 
13 int
14 host_tcgetattr(int fd, struct host_termios *tio)
15 {
16 	if (host_ioctl(fd, HOST_TCGETS, (uintptr_t)tio))
17 		return -1;
18 	return 0;
19 }
20 
21 int
22 host_tcsetattr(int fd, int act, const struct host_termios *tio)
23 {
24 	if (act < 0 || act > 2) {
25 //		errno = EINVAL;	/* XXX ?? */
26 		return -1;
27 	}
28 	return host_ioctl(fd, HOST_TCSETS+act, (uintptr_t)tio);
29 }
30 
31 void
32 host_cfmakeraw(struct host_termios *t)
33 {
34 	t->c_iflag &= ~(HOST_IGNBRK | HOST_BRKINT | HOST_PARMRK | HOST_ISTRIP |
35 	    HOST_INLCR | HOST_IGNCR | HOST_ICRNL | HOST_IXON);
36 	t->c_oflag &= ~HOST_OPOST;
37 	t->c_lflag &= ~(HOST_ECHO | HOST_ECHONL | HOST_ICANON | HOST_ISIG |
38 	    HOST_IEXTEN);
39 	t->c_cflag &= ~(HOST_CSIZE | HOST_PARENB);
40 	t->c_cflag |= HOST_CS8;
41 	t->c_cc[HOST_VMIN] = 1;
42 	t->c_cc[HOST_VTIME] = 0;
43 }
44 
45 int host_cfsetospeed(struct host_termios *tio, host_speed_t speed)
46 {
47 	if (speed & ~HOST_CBAUD) {
48 //		errno = EINVAL; /* XXX ? */
49 		return -1;
50 	}
51 	tio->c_cflag &= ~HOST_CBAUD;
52 	tio->c_cflag |= speed;
53 	return 0;
54 }
55 
56 int host_cfsetispeed(struct host_termios *tio, host_speed_t speed)
57 {
58 	return speed ? host_cfsetospeed(tio, speed) : 0;
59 }
60 
61 int
62 host_cfsetspeed(struct host_termios *tio, host_speed_t speed)
63 {
64 	return host_cfsetospeed(tio, speed);	/* weak alias in musl */
65 }
66 
67