43 static int vt100compat;
45 static char enddata[80] =
"";
46 static char quitdata[80] =
"";
48 static const char *
const termpath[] = {
49 "/usr/share/terminfo",
50 "/usr/local/share/misc/terminfo",
62 static int opposite(
int color)
73 return color ? lookup[color - 30] : 0;
77 static short convshort(
unsigned char *s)
81 a = (int) s[0] & 0377;
82 b = (int) s[1] & 0377;
84 if (a == 0377 && b == 0377)
86 if (a == 0376 && b == 0377)
92 static inline int convint(
unsigned char *s)
100 #define MAGIC_LEGACY (00432)
101 #define MAGIC_EXTNUM (01036)
103 #define HEADER_LEN (12)
104 #define MAX_COLORS_INDEX (13)
106 static int parse_terminfo_file(
int fd)
108 int bytes_read, bytes_needed, num_size;
109 short magic, sz_names, sz_bools;
110 unsigned char buffer[1024];
112 bytes_read = read(fd, buffer,
sizeof(buffer));
113 if (bytes_read < HEADER_LEN) {
117 magic = convshort(buffer);
119 if (magic == MAGIC_LEGACY) {
121 }
else if (magic == MAGIC_EXTNUM) {
129 sz_names = convshort(buffer + 2);
130 sz_bools = convshort(buffer + 4);
136 if ((sz_names + sz_bools) & 1) {
140 bytes_needed = HEADER_LEN + sz_names + sz_bools + ((MAX_COLORS_INDEX + 1) * num_size);
141 if (bytes_needed <= bytes_read) {
145 int offset = HEADER_LEN + sz_names + sz_bools + MAX_COLORS_INDEX * num_size;
149 max_colors = convshort(buffer + offset);
152 max_colors = convint(buffer + offset);
155 if (max_colors > 0) {
167 char *term = getenv(
"TERM");
168 char termfile[256] =
"";
169 int termfd = -1, parseokay = 0, i;
171 if (ast_opt_no_color) {
175 if (!ast_opt_console) {
185 for (i = 0; !parseokay && termpath[i]; i++) {
186 snprintf(termfile,
sizeof(termfile),
"%s/%c/%s", termpath[i], *term, term);
188 termfd = open(termfile, O_RDONLY);
190 parseokay = parse_terminfo_file(termfd);
201 if (!strcmp(term,
"linux")) {
203 }
else if (!strcmp(term,
"xterm")) {
205 }
else if (!strcmp(term,
"xterm-color")) {
207 }
else if (!strcmp(term,
"xterm-256color")) {
209 }
else if (!strncmp(term,
"Eterm", 5)) {
212 }
else if (!strcmp(term,
"vt100")) {
214 }
else if (!strncmp(term,
"crt", 3)) {
223 if (ast_opt_light_background) {
224 snprintf(enddata,
sizeof(enddata),
"%c[%dm", ESC, COLOR_BLACK);
225 }
else if (ast_opt_force_black_background) {
226 snprintf(enddata,
sizeof(enddata),
"%c[%d;%d;%dm", ESC, ATTR_RESET, COLOR_WHITE, COLOR_BLACK + 10);
228 snprintf(enddata,
sizeof(enddata),
"%c[%dm", ESC, ATTR_RESET);
230 snprintf(quitdata,
sizeof(quitdata),
"%c[%dm", ESC, ATTR_RESET);
235 char *
term_color(
char *outbuf,
const char *
inbuf,
int fgcolor,
int bgcolor,
int maxout)
249 attr = ast_opt_light_background ? 0 : ATTR_BRIGHT;
257 if (ast_opt_light_background) {
258 fgcolor = opposite(fgcolor);
261 if (ast_opt_force_black_background) {
263 bgcolor = COLOR_BLACK;
265 snprintf(outbuf, maxout,
"%c[%d;%d;%dm%s%s", ESC, attr, fgcolor, bgcolor + 10, inbuf, term_end());
267 snprintf(outbuf, maxout,
"%c[%d;%dm%s%s", ESC, attr, fgcolor, inbuf, term_end());
272 static void check_fgcolor(
int *fgcolor,
int *attr)
274 *attr = ast_opt_light_background ? 0 : ATTR_BRIGHT;
275 if (*fgcolor & 128) {
279 if (ast_opt_light_background) {
280 *fgcolor = opposite(*fgcolor);
284 static void check_bgcolor(
int *bgcolor)
291 static int check_colors_allowed(
void)
300 if (!check_colors_allowed()) {
304 check_fgcolor(&fgcolor, &attr);
305 check_bgcolor(&bgcolor);
307 if (ast_opt_force_black_background) {
308 ast_str_append(str, 0,
"%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10);
309 }
else if (bgcolor) {
310 ast_str_append(str, 0,
"%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10);
322 if (!check_colors_allowed()) {
327 check_fgcolor(&fgcolor, &attr);
328 check_bgcolor(&bgcolor);
330 if (ast_opt_force_black_background) {
331 snprintf(outbuf, maxout,
"%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10);
332 }
else if (bgcolor) {
333 snprintf(outbuf, maxout,
"%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10);
335 snprintf(outbuf, maxout,
"%c[%d;%dm", ESC, attr, fgcolor);
349 buf = cb->buffer[cb->which++];
350 if (cb->which == AST_TERM_MAX_ROTATING_BUFFERS) {
364 char *outbuf_ptr = outbuf;
365 const char *inbuf_ptr =
inbuf;
367 while (outbuf_ptr < outbuf + maxout) {
368 switch (*inbuf_ptr) {
370 while (*inbuf_ptr && (*inbuf_ptr !=
'm'))
374 *outbuf_ptr = *inbuf_ptr;
385 void term_filter_escapes(
char *line)
388 int len = strlen(line);
390 for (i = 0; i < len; i++) {
393 if ((i < (len - 2)) &&
394 (line[i + 1] == 0x5B)) {
395 switch (line[i + 2]) {
407 const char *term_end(
void)
412 const char *term_quit(
void)
#define AST_THREADSTORAGE(name)
Define a thread storage variable.
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definitions to aid in the use of thread local storage.
static int inbuf(struct baseio *bio, FILE *fi)
utility used by inchar(), for base_encode()
#define AST_TERM_MAX_ESCAPE_CHARS
Maximum number of characters needed for a color escape sequence, and another one for a trailing reset...
char * term_color_code(char *outbuf, int fgcolor, int bgcolor, int maxout)
Write a color sequence to a string.
const char * ast_term_color(int fgcolor, int bgcolor)
Return a color sequence string.
Support for dynamic strings.
char * term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout)
Colorize a specified string by adding terminal color codes.
const char * ast_term_reset(void)
Returns the terminal reset code.
Prototypes for public functions only of internal interest,.
int ast_term_color_code(struct ast_str **str, int fgcolor, int bgcolor)
Append a color sequence to an ast_str.
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Handy terminal functions for vt* terms.
char * term_strip(char *outbuf, const char *inbuf, int maxout)
Remove colorings from a specified string.