Teach set_color to call setupterm so it doesn't crash when run non-interactively

This commit is contained in:
ridiculousfish 2013-02-15 14:00:25 -08:00
parent bc7ad955a3
commit a355cf24f1
2 changed files with 26 additions and 3 deletions

View file

@ -9,6 +9,12 @@ Functions used for implementing the set_color builtin.
#include "color.h"
#include "output.h"
#if HAVE_NCURSES_H
#include <ncurses.h>
#else
#include <curses.h>
#endif
#if HAVE_TERM_H
#include <term.h>
#elif HAVE_NCURSES_TERM_H
@ -158,6 +164,23 @@ static int builtin_set_color(parser_t &parser, wchar_t **argv)
return STATUS_BUILTIN_ERROR;
}
/* Make sure that the term exists */
if (cur_term == NULL && setupterm(0, STDOUT_FILENO, 0) == ERR)
{
append_format(stderr_buffer, _("%s: Could not set up terminal\n"), argv[0]);
return STATUS_BUILTIN_ERROR;
}
/*
Test if we have at least basic support for setting fonts, colors
and related bits - otherwise just give up...
*/
if (! exit_attribute_mode)
{
return STATUS_BUILTIN_ERROR;
}
/* Save old output function so we can restore it */
int (* const saved_writer_func)(char) = output_get_writer();

View file

@ -238,7 +238,7 @@ static std::vector<terminfo_mapping_t> mappings;
/**
Set to one when the input subsytem has been initialized.
*/
static int is_init = 0;
static bool is_init = false;
/**
Initialize terminfo.
@ -344,7 +344,7 @@ int input_init()
if (is_init)
return 1;
is_init = 1;
is_init = true;
input_common_init(&interrupt_handler);
@ -381,7 +381,7 @@ void input_destroy()
return;
is_init=0;
is_init = false;
input_common_destroy();