Prevent fish from crashing if non-essential terminfo strings not found

On systems where the terminfo for TERM does not contain a string for
attributes such as enter_underline_mode, etc. fish was crashing with a
fatal error message and a note to email the developers.

These are non-essential text attribute changes and should not trigger
such a failure.
This commit is contained in:
Mahmoud Al-Qudsi 2018-06-17 20:59:34 -05:00
parent a6d64c1086
commit 8f166cbb2a
3 changed files with 22 additions and 21 deletions

View File

@ -193,36 +193,36 @@ int builtin_set_color(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
output_set_writer(set_color_builtin_outputter); output_set_writer(set_color_builtin_outputter);
if (bold && enter_bold_mode) { if (bold && enter_bold_mode) {
writembs(tparm(enter_bold_mode)); writembs_nofail(tparm(enter_bold_mode));
} }
if (underline && enter_underline_mode) { if (underline && enter_underline_mode) {
writembs(enter_underline_mode); writembs_nofail(enter_underline_mode);
} }
if (italics && enter_italics_mode) { if (italics && enter_italics_mode) {
writembs(enter_italics_mode); writembs_nofail(enter_italics_mode);
} }
if (dim && enter_dim_mode) { if (dim && enter_dim_mode) {
writembs(enter_dim_mode); writembs_nofail(enter_dim_mode);
} }
if (reverse && enter_reverse_mode) { if (reverse && enter_reverse_mode) {
writembs(enter_reverse_mode); writembs_nofail(enter_reverse_mode);
} else if (reverse && enter_standout_mode) { } else if (reverse && enter_standout_mode) {
writembs(enter_standout_mode); writembs_nofail(enter_standout_mode);
} }
if (bgcolor != NULL && bg.is_normal()) { if (bgcolor != NULL && bg.is_normal()) {
write_color(rgb_color_t::black(), false /* not is_fg */); write_color(rgb_color_t::black(), false /* not is_fg */);
writembs(tparm(exit_attribute_mode)); writembs_nofail(tparm(exit_attribute_mode));
} }
if (!fg.is_none()) { if (!fg.is_none()) {
if (fg.is_normal() || fg.is_reset()) { if (fg.is_normal() || fg.is_reset()) {
write_color(rgb_color_t::black(), true /* is_fg */); write_color(rgb_color_t::black(), true /* is_fg */);
writembs(tparm(exit_attribute_mode)); writembs_nofail(tparm(exit_attribute_mode));
} else { } else {
if (!write_color(fg, true /* is_fg */)) { if (!write_color(fg, true /* is_fg */)) {
// We need to do *something* or the lack of any output messes up // We need to do *something* or the lack of any output messes up

View File

@ -274,7 +274,7 @@ void set_color(rgb_color_t c, rgb_color_t c2) {
if (bg_set && !last_bg_set) { if (bg_set && !last_bg_set) {
// Background color changed and is set, so we enter bold mode to make reading easier. // Background color changed and is set, so we enter bold mode to make reading easier.
// This means bold mode is _always_ on when the background color is set. // This means bold mode is _always_ on when the background color is set.
writembs(enter_bold_mode); writembs_nofail(enter_bold_mode);
} }
if (!bg_set && last_bg_set) { if (!bg_set && last_bg_set) {
// Background color changed and is no longer set, so we exit bold mode. // Background color changed and is no longer set, so we exit bold mode.
@ -332,41 +332,41 @@ void set_color(rgb_color_t c, rgb_color_t c2) {
// Lastly, we set bold, underline, italics, dim, and reverse modes correctly. // Lastly, we set bold, underline, italics, dim, and reverse modes correctly.
if (is_bold && !was_bold && enter_bold_mode && strlen(enter_bold_mode) > 0 && !bg_set) { if (is_bold && !was_bold && enter_bold_mode && strlen(enter_bold_mode) > 0 && !bg_set) {
writembs(tparm(enter_bold_mode)); writembs_nofail(tparm(enter_bold_mode));
was_bold = is_bold; was_bold = is_bold;
} }
if (was_underline && !is_underline) { if (was_underline && !is_underline) {
writembs(exit_underline_mode); writembs_nofail(exit_underline_mode);
} }
if (!was_underline && is_underline) { if (!was_underline && is_underline) {
writembs(enter_underline_mode); writembs_nofail(enter_underline_mode);
} }
was_underline = is_underline; was_underline = is_underline;
if (was_italics && !is_italics && enter_italics_mode && strlen(enter_italics_mode) > 0) { if (was_italics && !is_italics && enter_italics_mode && strlen(enter_italics_mode) > 0) {
writembs(exit_italics_mode); writembs_nofail(exit_italics_mode);
was_italics = is_italics; was_italics = is_italics;
} }
if (!was_italics && is_italics && enter_italics_mode && strlen(enter_italics_mode) > 0) { if (!was_italics && is_italics && enter_italics_mode && strlen(enter_italics_mode) > 0) {
writembs(enter_italics_mode); writembs_nofail(enter_italics_mode);
was_italics = is_italics; was_italics = is_italics;
} }
if (is_dim && !was_dim && enter_dim_mode && strlen(enter_dim_mode) > 0) { if (is_dim && !was_dim && enter_dim_mode && strlen(enter_dim_mode) > 0) {
writembs(enter_dim_mode); writembs_nofail(enter_dim_mode);
was_dim = is_dim; was_dim = is_dim;
} }
if (is_reverse && !was_reverse) { if (is_reverse && !was_reverse) {
// Some terms do not have a reverse mode set, so standout mode is a fallback. // Some terms do not have a reverse mode set, so standout mode is a fallback.
if (enter_reverse_mode && strlen(enter_reverse_mode) > 0) { if (enter_reverse_mode && strlen(enter_reverse_mode) > 0) {
writembs(enter_reverse_mode); writembs_nofail(enter_reverse_mode);
was_reverse = is_reverse; was_reverse = is_reverse;
} else if (enter_standout_mode && strlen(enter_standout_mode) > 0) { } else if (enter_standout_mode && strlen(enter_standout_mode) > 0) {
writembs(enter_standout_mode); writembs_nofail(enter_standout_mode);
was_reverse = is_reverse; was_reverse = is_reverse;
} }
} }
@ -550,10 +550,10 @@ rgb_color_t parse_color(const env_var_t &var, bool is_background) {
} }
/// Write specified multibyte string. /// Write specified multibyte string.
void writembs_check(char *mbs, const char *mbs_name, const char *file, long line) { void writembs_check(char *mbs, const char *mbs_name, bool critical, const char *file, long line) {
if (mbs != NULL) { if (mbs != NULL) {
tputs(mbs, 1, &writeb); tputs(mbs, 1, &writeb);
} else { } else if (critical) {
auto term = env_get(L"TERM"); auto term = env_get(L"TERM");
const wchar_t *fmt = const wchar_t *fmt =
_(L"Tried to use terminfo string %s on line %ld of %s, which is " _(L"Tried to use terminfo string %s on line %ld of %s, which is "

View File

@ -30,8 +30,9 @@ enum {
void set_color(rgb_color_t c, rgb_color_t c2); void set_color(rgb_color_t c, rgb_color_t c2);
void writembs_check(char *mbs, const char *mbs_name, const char *file, long line); void writembs_check(char *mbs, const char *mbs_name, bool critical, const char *file, long line);
#define writembs(mbs) writembs_check((mbs), #mbs, __FILE__, __LINE__) #define writembs(mbs) writembs_check((mbs), #mbs, true, __FILE__, __LINE__)
#define writembs_nofail(mbs) writembs_check((mbs), #mbs, false, __FILE__, __LINE__)
int writech(wint_t ch); int writech(wint_t ch);