getch2: Handle setupterm errors

setupterm abort()s if it can't initialize the terminal and the last
parameter is NULL; handle setupterm errors and retry with "ansi" if
the TERM env var was unset.
This commit is contained in:
Diogo Franco (Kovensky) 2013-07-26 11:29:34 -03:00
parent ca039d42bb
commit 3928b39988
1 changed files with 18 additions and 1 deletions

View File

@ -237,7 +237,24 @@ int load_termcap(char *termtype){
#ifdef HAVE_TERMINFO
use_env(TRUE);
setupterm(termtype, 1, NULL);
int ret;
if (setupterm(termtype, 1, &ret) != OK) {
/* try again, with with "ansi" terminal if it was unset before */
if (!termtype)
termtype = getenv("TERM");
if (!termtype || *termtype == '\0')
termtype = "ansi";
if (setupterm(termtype, 1, &ret) != OK) {
if (ret < 0) {
printf("Could not access the 'terminfo' data base.\n");
return 0;
} else {
printf("Couldn't use terminal `%s' for input.\n", termtype);
return 0;
}
}
}
#else
static char term_buffer[2048];
if (!termtype) termtype = getenv("TERM");