ncdirect cbreak mode polish #958

* propagate return value of tcsetattr() from ncdirect_stop()
* restore terminal state on error paths in ncdirect_init()
This commit is contained in:
nick black 2020-08-26 13:57:37 -04:00 committed by Nick Black
parent 4b037bc4b8
commit 39d0989a5e

View File

@ -425,36 +425,39 @@ ncdirect* ncdirect_init(const char* termtype, FILE* outfp){
}
ret->ttyfp = outfp;
memset(&ret->palette, 0, sizeof(ret->palette));
// we don't need a controlling tty for everything we do; allow a failure here
if((ret->ctermfd = get_controlling_tty(ret->ttyfp)) >= 0){
if(cbreak_mode(ret->ctermfd, &ret->tpreserved)){
delete(ret);
return nullptr;
}
}
int termerr;
if(setupterm(termtype, ret->ctermfd, &termerr) != OK){
fprintf(stderr, "Terminfo error %d (see terminfo(3ncurses))\n", termerr);
delete(ret);
return nullptr;
}
if(ncvisual_init(ffmpeg_log_level(NCLOGLEVEL_SILENT))){
delete(ret);
return nullptr;
}
if(interrogate_terminfo(&ret->tcache)){
delete(ret);
return nullptr;
}
ret->fgdefault = ret->bgdefault = true;
ret->fgrgb = ret->bgrgb = 0;
ncdirect_styles_set(ret, 0);
init_lang(nullptr);
const char* encoding = nl_langinfo(CODESET);
if(encoding && strcmp(encoding, "UTF-8") == 0){
ret->utf8 = true;
}
// we don't need a controlling tty for everything we do; allow a failure here
if((ret->ctermfd = get_controlling_tty(ret->ttyfp)) >= 0){
if(cbreak_mode(ret->ctermfd, &ret->tpreserved)){
goto err;
}
}
int termerr;
if(setupterm(termtype, ret->ctermfd, &termerr) != OK){
fprintf(stderr, "Terminfo error %d (see terminfo(3ncurses))\n", termerr);
goto err;
}
if(ncvisual_init(ffmpeg_log_level(NCLOGLEVEL_SILENT))){
goto err;
}
if(interrogate_terminfo(&ret->tcache)){
goto err;
}
ret->fgdefault = ret->bgdefault = true;
ret->fgrgb = ret->bgrgb = 0;
ncdirect_styles_set(ret, 0);
return ret;
err:
if(ret->ctermfd >= 0){
tcsetattr(ret->ctermfd, TCSANOW, &ret->tpreserved);
}
delete(ret);
return nullptr;
}
int ncdirect_stop(ncdirect* nc){
@ -473,7 +476,7 @@ int ncdirect_stop(ncdirect* nc){
if(nc->tcache.cnorm && tty_emit("cnorm", nc->tcache.cnorm, nc->ctermfd)){
ret = -1;
}
tcsetattr(nc->ctermfd, TCSANOW, &nc->tpreserved);
ret |= tcsetattr(nc->ctermfd, TCSANOW, &nc->tpreserved);
ret |= close(nc->ctermfd);
}
delete(nc);