Complete batch 20261016-C-embedded-2: 45 verified GLM/Pi samples, independent regrades and archives; preserve difficulty misses and Salty blockage.
153 lines
4.2 KiB
C
153 lines
4.2 KiB
C
#include "tty.h"
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <string.h>
|
|
|
|
static int read_attributes(const tty_ops *ops, int fd, struct termios *attributes)
|
|
{
|
|
int result;
|
|
do {
|
|
result = ops->getattr_call(fd, attributes);
|
|
} while (result < 0 && errno == EINTR);
|
|
return result < 0 ? -errno : 0;
|
|
}
|
|
|
|
static int write_attributes(const tty_ops *ops, int fd, const struct termios *attributes)
|
|
{
|
|
int result;
|
|
do {
|
|
result = ops->setattr_call(fd, TCSANOW, attributes);
|
|
} while (result < 0 && errno == EINTR);
|
|
return result < 0 ? -errno : 0;
|
|
}
|
|
|
|
static int read_flags(const tty_ops *ops, int fd, int *flags)
|
|
{
|
|
int result;
|
|
do {
|
|
result = ops->getflags_call(fd);
|
|
} while (result < 0 && errno == EINTR);
|
|
if (result < 0)
|
|
return -errno;
|
|
*flags = result;
|
|
return 0;
|
|
}
|
|
|
|
static int write_flags(const tty_ops *ops, int fd, int flags)
|
|
{
|
|
int result;
|
|
do {
|
|
result = ops->setflags_call(fd, flags);
|
|
} while (result < 0 && errno == EINTR);
|
|
return result < 0 ? -errno : 0;
|
|
}
|
|
|
|
static int operations_valid(const tty_ops *ops)
|
|
{
|
|
return ops != NULL && ops->getattr_call != NULL && ops->setattr_call != NULL &&
|
|
ops->getflags_call != NULL && ops->setflags_call != NULL;
|
|
}
|
|
|
|
void tty_init(tty_session *session)
|
|
{
|
|
memset(session, 0, sizeof *session);
|
|
session->fd = -1;
|
|
}
|
|
|
|
static int desired_flags(int previous, const tty_config *config)
|
|
{
|
|
if (config->nonblocking)
|
|
return previous | O_NONBLOCK;
|
|
return previous & ~O_NONBLOCK;
|
|
}
|
|
|
|
int tty_begin(tty_session *session, int fd, const tty_config *config,
|
|
const tty_ops *ops)
|
|
{
|
|
struct termios previous;
|
|
struct termios configured;
|
|
int previous_flags;
|
|
int result;
|
|
if (session->active)
|
|
return -EALREADY;
|
|
if (!operations_valid(ops))
|
|
return -EINVAL;
|
|
memset(&previous, 0, sizeof previous);
|
|
result = tty_prepare(&previous, config, &configured);
|
|
if (result != 0)
|
|
return result;
|
|
result = read_attributes(ops, fd, &previous);
|
|
if (result != 0)
|
|
return result;
|
|
result = read_flags(ops, fd, &previous_flags);
|
|
if (result != 0)
|
|
return result;
|
|
result = tty_prepare(&previous, config, &configured);
|
|
if (result != 0)
|
|
return result;
|
|
result = write_attributes(ops, fd, &configured);
|
|
if (result != 0)
|
|
return result;
|
|
result = write_flags(ops, fd, desired_flags(previous_flags, config));
|
|
if (result != 0) {
|
|
int original_error = result;
|
|
(void)write_attributes(ops, fd, &previous);
|
|
return original_error;
|
|
}
|
|
session->saved_attributes = previous;
|
|
session->saved_flags = previous_flags;
|
|
session->ops = ops;
|
|
session->fd = fd;
|
|
session->active = 1;
|
|
return 0;
|
|
}
|
|
|
|
int tty_update(tty_session *session, const tty_config *config)
|
|
{
|
|
struct termios previous;
|
|
struct termios configured;
|
|
int previous_flags;
|
|
int result;
|
|
if (!session->active)
|
|
return -ENODEV;
|
|
memset(&previous, 0, sizeof previous);
|
|
result = tty_prepare(&previous, config, &configured);
|
|
if (result != 0)
|
|
return result;
|
|
result = read_attributes(session->ops, session->fd, &previous);
|
|
if (result != 0)
|
|
return result;
|
|
result = read_flags(session->ops, session->fd, &previous_flags);
|
|
if (result != 0)
|
|
return result;
|
|
result = tty_prepare(&previous, config, &configured);
|
|
if (result != 0)
|
|
return result;
|
|
result = write_attributes(session->ops, session->fd, &configured);
|
|
if (result != 0)
|
|
return result;
|
|
result = write_flags(session->ops, session->fd, desired_flags(previous_flags, config));
|
|
if (result != 0) {
|
|
int original_error = result;
|
|
(void)write_attributes(session->ops, session->fd, &previous);
|
|
return original_error;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int tty_end(tty_session *session)
|
|
{
|
|
int result;
|
|
if (!session->active)
|
|
return 0;
|
|
result = write_attributes(session->ops, session->fd, &session->saved_attributes);
|
|
if (result != 0)
|
|
return result;
|
|
result = write_flags(session->ops, session->fd, session->saved_flags);
|
|
if (result != 0)
|
|
return result;
|
|
session->active = 0;
|
|
session->fd = -1;
|
|
return 0;
|
|
}
|