diff --git a/src/term.c b/src/term.c index 6b184a2..ac7b19a 100644 --- a/src/term.c +++ b/src/term.c @@ -1,23 +1,36 @@ #include -#include #include +#include #include +#include "_ct_shared.h" #include "term.h" -int getterm(struct ct_term *const term) { - if ((term->fd = open("/dev/tty", O_RDONLY)) == -1) - return 1; - if (tcgetattr(term->fd, &term->termios0) == -1) - return 1; - term->termios = term->termios0; - return 0; +int stashterm(const struct ct_term **const term) { + struct ct_term *t = (struct ct_term *)malloc(sizeof(struct ct_term)); + + 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; + } + t->termios = t->termios0; + + *term = t; + return OK; + +fail: + free(t); + return ERR; } -void endterm(struct ct_term *const term) { +void resetterm(struct ct_term *const term) { if (term->fd != -1) { close(term->fd); - /* WARNING: should tcsetattr instead be called on term->fd before closing? */ + /* WARNING: should tcsetattr instead be called on term->fd before closing? + */ tcsetattr(STDIN_FILENO, TCSANOW, &term->termios0); } free(term); @@ -26,9 +39,9 @@ void endterm(struct ct_term *const term) { int termsize(struct ct_term *const term) { struct winsize argp; if (ioctl(term->fd, TIOCGWINSZ, &argp) == -1) - return 1; + return ERR; term->rows = argp.ws_row; term->cols = argp.ws_col; - return 0; + return OK; } diff --git a/src/term.h b/src/term.h index ba6c417..43a3a05 100644 --- a/src/term.h +++ b/src/term.h @@ -12,8 +12,8 @@ struct ct_term { struct termios termios; }; -int getterm(struct ct_term *const term); -void endterm(struct ct_term *const term); +int stashterm(const struct ct_term **const term); +void resetterm(struct ct_term *const term); int termsize(struct ct_term *const term); #endif /* _CURSETREE_TERM_H */