From 052738b8f422060a09a7f5eaadc6b1c19fbcc61d Mon Sep 17 00:00:00 2001 From: Paul LeoNerd Evans Date: Fri, 20 Jan 2012 17:00:09 +0000 Subject: Allow {start,stop}_driver to fail, abort constructor, have driver-ti try to write the entire start/stop string or error out on -1 --- driver-ti.c | 43 +++++++++++++++++++++++++++++++++++-------- termkey-internal.h | 4 ++-- termkey.c | 3 ++- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/driver-ti.c b/driver-ti.c index cbf4a13..af46769 100644 --- a/driver-ti.c +++ b/driver-ti.c @@ -289,27 +289,54 @@ abort_free_ti: return NULL; } -static void start_driver(TermKey *tk, void *info) +static int start_driver(TermKey *tk, void *info) { TermKeyTI *ti = info; + char *start_string = ti->start_string; + size_t len; + + if(tk->fd == -1 || !start_string) + return 1; /* The terminfo database will contain keys in application cursor key mode. * We may need to enable that mode */ - if(tk->fd != -1 && ti->start_string) { - // Can't call putp or tputs because they suck and don't give us fd control - (void)write(tk->fd, ti->start_string, strlen(ti->start_string)); + + // Can't call putp or tputs because they suck and don't give us fd control + len = strlen(start_string); + while(len) { + size_t written = write(tk->fd, start_string, len); + if(written == -1) + return 0; + start_string += written; + len -= written; } + return 1; } -static void stop_driver(TermKey *tk, void *info) +static int stop_driver(TermKey *tk, void *info) { TermKeyTI *ti = info; + char *stop_string = ti->stop_string; + size_t len; + + if(tk->fd == -1 || !stop_string) + return 1; - if(tk->fd != -1 && ti->stop_string) { - // Can't call putp or tputs because they suck and don't give us fd control - (void)write(tk->fd, ti->stop_string, strlen(ti->stop_string)); + /* The terminfo database will contain keys in application cursor key mode. + * We may need to enable that mode + */ + + // Can't call putp or tputs because they suck and don't give us fd control + len = strlen(stop_string); + while(len) { + size_t written = write(tk->fd, stop_string, len); + if(written == -1) + return 0; + stop_string += written; + len -= written; } + return 1; } static void free_driver(void *info) diff --git a/termkey-internal.h b/termkey-internal.h index c1f4cd6..e277939 100644 --- a/termkey-internal.h +++ b/termkey-internal.h @@ -11,8 +11,8 @@ struct TermKeyDriver const char *name; void *(*new_driver)(TermKey *tk, const char *term); void (*free_driver)(void *info); - void (*start_driver)(TermKey *tk, void *info); - void (*stop_driver)(TermKey *tk, void *info); + int (*start_driver)(TermKey *tk, void *info); + int (*stop_driver)(TermKey *tk, void *info); TermKeyResult (*peekkey)(TermKey *tk, void *info, TermKeyKey *key, int force, size_t *nbytes); }; diff --git a/termkey.c b/termkey.c index a66d951..d322a93 100644 --- a/termkey.c +++ b/termkey.c @@ -318,7 +318,8 @@ static TermKey *termkey_new_full(int fd, int flags, size_t buffsize, int waittim struct TermKeyDriverNode *p; for(p = tk->drivers; p; p = p->next) if(p->driver->start_driver) - (*p->driver->start_driver)(tk, p->info); + if(!(*p->driver->start_driver)(tk, p->info)) + goto abort_free_drivers; #ifdef DEBUG fprintf(stderr, "Drivers started; termkey instance %p is ready\n", tk); -- cgit v1.2.3-70-g09d2