Clean up after error properly.

This commit is contained in:
Nicholas Marriott 2007-12-06 11:11:15 +00:00
parent 0f2666ac77
commit bea5d6d1ab

47
tty.c
View File

@ -1,4 +1,4 @@
/* $Id: tty.c,v 1.10 2007-12-06 11:05:04 nicm Exp $ */ /* $Id: tty.c,v 1.11 2007-12-06 11:11:15 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -59,6 +59,7 @@ tty_open(struct tty *tty, char **cause)
return (-1); return (-1);
} }
tty->termp = NULL;
if (tty->term == NULL) if (tty->term == NULL)
tty->term = xstrdup("unknown"); tty->term = xstrdup("unknown");
if (setupterm(tty->term, tty->fd, &error) != OK) { if (setupterm(tty->term, tty->fd, &error) != OK) {
@ -76,71 +77,71 @@ tty_open(struct tty *tty, char **cause)
xasprintf(cause, "unknown error"); xasprintf(cause, "unknown error");
break; break;
} }
return (-1); goto error;
} }
tty->termp = cur_term; tty->termp = cur_term;
/* Check for required capabilities. */ /* Check for required capabilities. */
if (clear_screen == NULL) { if (clear_screen == NULL) {
xasprintf(cause, "clear_screen missing"); xasprintf(cause, "clear_screen missing");
return (-1); goto error;
} }
if (cursor_down == NULL) { if (cursor_down == NULL) {
xasprintf(cause, "cursor_down missing"); xasprintf(cause, "cursor_down missing");
return (-1); goto error;
} }
if (carriage_return == NULL) { if (carriage_return == NULL) {
xasprintf(cause, "carriage_return missing"); xasprintf(cause, "carriage_return missing");
return (-1); goto error;
} }
if (cursor_left == NULL) { if (cursor_left == NULL) {
xasprintf(cause, "cursor_left missing"); xasprintf(cause, "cursor_left missing");
return (-1); goto error;
} }
if (parm_up_cursor == NULL && cursor_up == NULL) { if (parm_up_cursor == NULL && cursor_up == NULL) {
xasprintf(cause, "parm_up_cursor missing"); xasprintf(cause, "parm_up_cursor missing");
return (-1); goto error;
} }
if (parm_down_cursor == NULL && cursor_down == NULL) { if (parm_down_cursor == NULL && cursor_down == NULL) {
xasprintf(cause, "parm_down_cursor missing"); xasprintf(cause, "parm_down_cursor missing");
return (-1); goto error;
} }
if (parm_right_cursor == NULL && cursor_right == NULL) { if (parm_right_cursor == NULL && cursor_right == NULL) {
xasprintf(cause, "parm_right_cursor missing"); xasprintf(cause, "parm_right_cursor missing");
return (-1); goto error;
} }
if (parm_left_cursor == NULL && cursor_left == NULL) { if (parm_left_cursor == NULL && cursor_left == NULL) {
xasprintf(cause, "parm_left_cursor missing"); xasprintf(cause, "parm_left_cursor missing");
return (-1); goto error;
} }
if (cursor_address == NULL) { if (cursor_address == NULL) {
xasprintf(cause, "cursor_address missing"); xasprintf(cause, "cursor_address missing");
return (-1); goto error;
} }
if (parm_insert_line == NULL && insert_line == NULL) { if (parm_insert_line == NULL && insert_line == NULL) {
xasprintf(cause, "parm_insert_line missing"); xasprintf(cause, "parm_insert_line missing");
return (-1); goto error;
} }
if (parm_delete_line == NULL && delete_line == NULL) { if (parm_delete_line == NULL && delete_line == NULL) {
xasprintf(cause, "parm_delete_line missing"); xasprintf(cause, "parm_delete_line missing");
return (-1); goto error;
} }
if (parm_ich == NULL && insert_character == NULL && if (parm_ich == NULL && insert_character == NULL &&
(enter_insert_mode == NULL || exit_insert_mode == NULL)) { (enter_insert_mode == NULL || exit_insert_mode == NULL)) {
xasprintf(cause, "parm_ich missing"); xasprintf(cause, "parm_ich missing");
return (-1); goto error;
} }
if (parm_dch == NULL && delete_character == NULL) { if (parm_dch == NULL && delete_character == NULL) {
xasprintf(cause, "parm_dch missing"); xasprintf(cause, "parm_dch missing");
return (-1); goto error;
} }
if (scroll_reverse == NULL) { if (scroll_reverse == NULL) {
xasprintf(cause, "scroll_reverse missing"); xasprintf(cause, "scroll_reverse missing");
return (-1); goto error;
} }
if (change_scroll_region == NULL) { if (change_scroll_region == NULL) {
xasprintf(cause, "change_scroll_region missing"); xasprintf(cause, "change_scroll_region missing");
return (-1); goto error;
} }
tty->in = buffer_create(BUFSIZ); tty->in = buffer_create(BUFSIZ);
@ -179,6 +180,15 @@ tty_open(struct tty *tty, char **cause)
tty_puts(tty, clear_screen); tty_puts(tty, clear_screen);
return (0); return (0);
error:
close(tty->fd);
tty->fd = -1;
if (tty->termp != NULL)
del_curterm(tty->termp);
return (-1);
} }
void void
@ -203,7 +213,8 @@ tty_close(struct tty *tty)
if (exit_attribute_mode != NULL) if (exit_attribute_mode != NULL)
tty_raw(tty, exit_attribute_mode); tty_raw(tty, exit_attribute_mode);
del_curterm(tty->termp); if (tty->termp != NULL)
del_curterm(tty->termp);
tty_keys_free(tty); tty_keys_free(tty);
close(tty->fd); close(tty->fd);