From 866fd14b51b1499834e626e79ccf78c6977a8b11 Mon Sep 17 00:00:00 2001 From: Emile Clark-Boman Date: Sun, 28 Sep 2025 00:06:40 +1000 Subject: [PATCH] change ct_term internal interface --- src/ct_alloc.h | 8 ++++++++ src/term.c | 54 ++++++++++++++++++++++++-------------------------- src/term.h | 22 ++++++++++++++------ 3 files changed, 50 insertions(+), 34 deletions(-) create mode 100644 src/ct_alloc.h diff --git a/src/ct_alloc.h b/src/ct_alloc.h new file mode 100644 index 0000000..ee9fc5e --- /dev/null +++ b/src/ct_alloc.h @@ -0,0 +1,8 @@ +#ifndef CURSETREE_ALLOC_H +#define CURSETREE_ALLOC_H + +#include + +#define CT_ALLOC(type, count) ((type *)malloc(sizeof(type) * count)) + +#endif /* CURSETREE_ALLOC_H */ diff --git a/src/term.c b/src/term.c index 844b4f7..c3d232b 100644 --- a/src/term.c +++ b/src/term.c @@ -1,32 +1,39 @@ #include #include -#include #include -#include "_ct_shared.h" +#include "ct_alloc.h" #include "term.h" -int stashterm(struct ct_term **const term) { - struct ct_term *t = (struct ct_term *)malloc(sizeof(struct ct_term)); +static inline int ct_tty(struct termios *restrict const termios) { + int fd = open("/dev/tty", O_RDONLY); - t->fd = open("/dev/tty", O_RDONLY); - if (t->fd == -1) - goto fail; - else if (tcgetattr(t->fd, &t->termios0) == -1) { - close(t->fd); - goto fail; + if (fd != -1 && tcgetattr(fd, termios) == -1) { + close(fd); + return -2; } - t->termios = t->termios0; - - *term = t; - return OK; - -fail: - free(t); - return ERR; +end: + return fd; } -void resetterm(struct ct_term *const term) { +int ct_stashterm(struct ct_term **restrict term) { + struct ct_term *t = (struct ct_term *)malloc(sizeof(struct ct_term)); + + // cast away the const to initialise these fields + *(int *)&(*term)->fd = ct_tty((struct termios *)&(*term)->termios0); + + if ((*term)->fd < 0) { + free(*term); + return NERR; + } + // duplicate as termios0 will now be readonly + (*term)->termios = (*term)->termios0; + ct_termsize(*term); + + return OK; +} + +void ct_resetterm(struct ct_term *const term) { if (term->fd != -1) { close(term->fd); /* WARNING: should tcsetattr instead be called on term->fd before closing? @@ -36,12 +43,3 @@ void resetterm(struct ct_term *const term) { free(term); } -int termsize(struct ct_term *const term) { - struct winsize argp; - if (ioctl(term->fd, TIOCGWINSZ, &argp) == -1) - return ERR; - - term->rows = argp.ws_row; - term->cols = argp.ws_col; - return OK; -} diff --git a/src/term.h b/src/term.h index 3ca3d7d..f66089d 100644 --- a/src/term.h +++ b/src/term.h @@ -2,20 +2,30 @@ #define _CURSETREE_TERM_H #include +#include -#include "_ct_shared.h" +#include "ct_shared.h" struct ct_term { - int fd; + const int fd; pos cols; pos rows; - struct termios termios0; struct termios termios; + const struct termios termios0; }; -int stashterm(struct ct_term **const term); -void resetterm(struct ct_term *const term); -int termsize(struct ct_term *const term); +static inline int ct_termsize(struct ct_term *const term) { + struct winsize argp; + if (ioctl(term->fd, TIOCGWINSZ, &argp) == -1) + return ERR; + + term->rows = argp.ws_row; + term->cols = argp.ws_col; + return OK; +} + +int ct_stashterm(struct ct_term **const term); +void ct_resetterm(struct ct_term *const term); #endif /* _CURSETREE_TERM_H */