| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2010-06-09 17:32:52 +00:00
										 |  |  |  * Copyright (C) 1999 - 2010, Digium, Inc. | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-07-10 22:56:21 +00:00
										 |  |  |  * Mark Spencer <markster@digium.com> | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * See http://www.asterisk.org for more information about
 | 
					
						
							|  |  |  |  * the Asterisk project. Please do not directly contact | 
					
						
							|  |  |  |  * any of the maintainers of this project for assistance; | 
					
						
							|  |  |  |  * the project provides a web site, mailing lists and IRC | 
					
						
							|  |  |  |  * channels for your use. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  |  * This program is free software, distributed under the terms of | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  | /*! \file
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2010-06-09 17:32:52 +00:00
										 |  |  |  * \brief Terminal Routines | 
					
						
							| 
									
										
										
										
											2005-12-30 21:18:06 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2010-06-09 17:32:52 +00:00
										 |  |  |  * \author Mark Spencer <markster@digium.com> | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-15 16:20:16 +00:00
										 |  |  | /*** MODULEINFO
 | 
					
						
							|  |  |  | 	<support_level>core</support_level> | 
					
						
							|  |  |  |  ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-20 22:18:21 +00:00
										 |  |  | #include "asterisk/_private.h"
 | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  | #include <sys/time.h>
 | 
					
						
							|  |  |  | #include <signal.h>
 | 
					
						
							| 
									
										
										
										
											2004-10-01 03:11:52 +00:00
										 |  |  | #include <sys/stat.h>
 | 
					
						
							|  |  |  | #include <fcntl.h>
 | 
					
						
							| 
									
										
										
										
											2005-04-22 13:11:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-21 06:02:45 +00:00
										 |  |  | #include "asterisk/term.h"
 | 
					
						
							|  |  |  | #include "asterisk/lock.h"
 | 
					
						
							| 
									
										
										
										
											2005-07-11 00:00:11 +00:00
										 |  |  | #include "asterisk/utils.h"
 | 
					
						
							| 
									
										
										
										
											2013-02-14 18:47:56 +00:00
										 |  |  | #include "asterisk/threadstorage.h"
 | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-12-27 22:14:33 +00:00
										 |  |  | static int vt100compat; | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static char enddata[80] = ""; | 
					
						
							|  |  |  | static char quitdata[80] = ""; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-12 13:59:35 +00:00
										 |  |  | static const char * const termpath[] = { | 
					
						
							| 
									
										
										
										
											2004-10-01 03:11:52 +00:00
										 |  |  | 	"/usr/share/terminfo", | 
					
						
							|  |  |  | 	"/usr/local/share/misc/terminfo", | 
					
						
							|  |  |  | 	"/usr/lib/terminfo", | 
					
						
							|  |  |  | 	NULL | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-14 18:47:56 +00:00
										 |  |  | AST_THREADSTORAGE(commonbuf); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct commonbuf { | 
					
						
							|  |  |  | 	short which; | 
					
						
							|  |  |  | 	char buffer[AST_TERM_MAX_ROTATING_BUFFERS][AST_TERM_MAX_ESCAPE_CHARS]; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-25 23:13:32 +00:00
										 |  |  | static int opposite(int color) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int lookup[] = { | 
					
						
							|  |  |  | 		/* BLACK */ COLOR_BLACK, | 
					
						
							|  |  |  | 		/* RED */ COLOR_MAGENTA, | 
					
						
							|  |  |  | 		/* GREEN */ COLOR_GREEN, | 
					
						
							|  |  |  | 		/* BROWN */ COLOR_BROWN, | 
					
						
							|  |  |  | 		/* BLUE */ COLOR_CYAN, | 
					
						
							|  |  |  | 		/* MAGENTA */ COLOR_RED, | 
					
						
							|  |  |  | 		/* CYAN */ COLOR_BLUE, | 
					
						
							|  |  |  | 		/* WHITE */ COLOR_BLACK }; | 
					
						
							|  |  |  | 	return color ? lookup[color - 30] : 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-01 03:11:52 +00:00
										 |  |  | /* Ripped off from Ross Ridge, but it's public domain code (libmytinfo) */ | 
					
						
							| 
									
										
										
										
											2021-09-04 13:07:14 -04:00
										 |  |  | static short convshort(unsigned char *s) | 
					
						
							| 
									
										
										
										
											2004-10-01 03:11:52 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | 	register int a, b; | 
					
						
							| 
									
										
										
										
											2004-10-01 03:11:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	a = (int) s[0] & 0377; | 
					
						
							|  |  |  | 	b = (int) s[1] & 0377; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (a == 0377 && b == 0377) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	if (a == 0376 && b == 0377) | 
					
						
							|  |  |  | 		return -2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return a + b * 256; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-04 13:07:14 -04:00
										 |  |  | static inline int convint(unsigned char *s) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return s[0] | 
					
						
							|  |  |  | 		| s[1] << 8 | 
					
						
							|  |  |  | 		| s[2] << 16 | 
					
						
							|  |  |  | 		| s[3] << 24; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define MAGIC_LEGACY (00432)
 | 
					
						
							|  |  |  | #define MAGIC_EXTNUM (01036)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define HEADER_LEN (12)
 | 
					
						
							|  |  |  | #define MAX_COLORS_INDEX (13)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int parse_terminfo_file(int fd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int bytes_read, bytes_needed, num_size; | 
					
						
							|  |  |  | 	short magic, sz_names, sz_bools; | 
					
						
							|  |  |  | 	unsigned char buffer[1024]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bytes_read = read(fd, buffer, sizeof(buffer)); | 
					
						
							|  |  |  | 	if (bytes_read < HEADER_LEN) { | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	magic = convshort(buffer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (magic == MAGIC_LEGACY) { | 
					
						
							|  |  |  | 		num_size = 2; | 
					
						
							|  |  |  | 	} else if (magic == MAGIC_EXTNUM) { | 
					
						
							|  |  |  | 		/* Extended number format (ncurses 6.1) */ | 
					
						
							|  |  |  | 		num_size = 4; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		/* We don't know how to parse this file */ | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sz_names = convshort(buffer + 2); | 
					
						
							|  |  |  | 	sz_bools = convshort(buffer + 4); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* From term(5):
 | 
					
						
							|  |  |  | 	 * Between the boolean section and the number section, a null byte will be | 
					
						
							|  |  |  | 	 * inserted, if necessary, to ensure that the number section begins on an | 
					
						
							|  |  |  | 	 * even byte. */ | 
					
						
							|  |  |  | 	if ((sz_names + sz_bools) & 1) { | 
					
						
							|  |  |  | 		sz_bools++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bytes_needed = HEADER_LEN + sz_names + sz_bools + ((MAX_COLORS_INDEX + 1) * num_size); | 
					
						
							|  |  |  | 	if (bytes_needed <= bytes_read) { | 
					
						
							|  |  |  | 		/* Offset 13 is defined in /usr/include/term.h, though we do not
 | 
					
						
							|  |  |  | 		 * include it here, as it conflicts with include/asterisk/term.h */ | 
					
						
							|  |  |  | 		int max_colors; | 
					
						
							|  |  |  | 		int offset = HEADER_LEN + sz_names + sz_bools + MAX_COLORS_INDEX * num_size; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (num_size == 2) { | 
					
						
							|  |  |  | 			/* In the legacy terminfo format, numbers are signed shorts */ | 
					
						
							|  |  |  | 			max_colors = convshort(buffer + offset); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			/* Extended number format makes them signed ints */ | 
					
						
							|  |  |  | 			max_colors = convint(buffer + offset); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (max_colors > 0) { | 
					
						
							|  |  |  | 			vt100compat = 1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-10-04 00:26:21 +00:00
										 |  |  | int ast_term_init(void) | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	char *term = getenv("TERM"); | 
					
						
							| 
									
										
										
										
											2004-10-01 03:11:52 +00:00
										 |  |  | 	char termfile[256] = ""; | 
					
						
							|  |  |  | 	int termfd = -1, parseokay = 0, i; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-09 17:32:52 +00:00
										 |  |  | 	if (ast_opt_no_color) { | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2010-06-09 17:32:52 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!ast_opt_console) { | 
					
						
							|  |  |  | 		/* If any remote console is not compatible, we'll strip the color codes at that point */ | 
					
						
							|  |  |  | 		vt100compat = 1; | 
					
						
							|  |  |  | 		goto end; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!term) { | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2010-06-09 17:32:52 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-10-01 03:11:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-04 13:07:14 -04:00
										 |  |  | 	for (i = 0; !parseokay && termpath[i]; i++) { | 
					
						
							| 
									
										
										
										
											2004-10-01 03:11:52 +00:00
										 |  |  | 		snprintf(termfile, sizeof(termfile), "%s/%c/%s", termpath[i], *term, term); | 
					
						
							| 
									
										
										
										
											2021-09-04 13:07:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-01 03:11:52 +00:00
										 |  |  | 		termfd = open(termfile, O_RDONLY); | 
					
						
							|  |  |  | 		if (termfd > -1) { | 
					
						
							| 
									
										
										
										
											2021-09-04 13:07:14 -04:00
										 |  |  | 			parseokay = parse_terminfo_file(termfd); | 
					
						
							|  |  |  | 			close(termfd); | 
					
						
							| 
									
										
										
										
											2004-10-01 03:11:52 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!parseokay) { | 
					
						
							|  |  |  | 		/* These comparisons should not be substrings nor case-insensitive, as
 | 
					
						
							|  |  |  | 		 * terminal types are very particular about how they treat suffixes and | 
					
						
							|  |  |  | 		 * capitalization.  For example, terminal type 'linux-m' does NOT | 
					
						
							|  |  |  | 		 * support color, while 'linux' does.  Not even all vt100* terminals | 
					
						
							|  |  |  | 		 * support color, either (e.g. 'vt100+fnkeys'). */ | 
					
						
							|  |  |  | 		if (!strcmp(term, "linux")) { | 
					
						
							|  |  |  | 			vt100compat = 1; | 
					
						
							|  |  |  | 		} else if (!strcmp(term, "xterm")) { | 
					
						
							|  |  |  | 			vt100compat = 1; | 
					
						
							|  |  |  | 		} else if (!strcmp(term, "xterm-color")) { | 
					
						
							|  |  |  | 			vt100compat = 1; | 
					
						
							| 
									
										
										
										
											2011-07-28 21:46:27 +00:00
										 |  |  | 		} else if (!strcmp(term, "xterm-256color")) { | 
					
						
							|  |  |  | 			vt100compat = 1; | 
					
						
							| 
									
										
										
										
											2004-10-01 03:11:52 +00:00
										 |  |  | 		} else if (!strncmp(term, "Eterm", 5)) { | 
					
						
							|  |  |  | 			/* Both entries which start with Eterm support color */ | 
					
						
							|  |  |  | 			vt100compat = 1; | 
					
						
							|  |  |  | 		} else if (!strcmp(term, "vt100")) { | 
					
						
							|  |  |  | 			vt100compat = 1; | 
					
						
							|  |  |  | 		} else if (!strncmp(term, "crt", 3)) { | 
					
						
							|  |  |  | 			/* Both crt terminals support color */ | 
					
						
							|  |  |  | 			vt100compat = 1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-09 17:32:52 +00:00
										 |  |  | end: | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  | 	if (vt100compat) { | 
					
						
							|  |  |  | 		/* Make commands show up in nice colors */ | 
					
						
							| 
									
										
										
										
											2008-08-25 23:13:32 +00:00
										 |  |  | 		if (ast_opt_light_background) { | 
					
						
							|  |  |  | 			snprintf(enddata, sizeof(enddata), "%c[%dm", ESC, COLOR_BLACK); | 
					
						
							| 
									
										
										
										
											2008-10-07 17:44:32 +00:00
										 |  |  | 		} else if (ast_opt_force_black_background) { | 
					
						
							|  |  |  | 			snprintf(enddata, sizeof(enddata), "%c[%d;%d;%dm", ESC, ATTR_RESET, COLOR_WHITE, COLOR_BLACK + 10); | 
					
						
							| 
									
										
										
										
											2008-08-25 23:13:32 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2015-11-25 20:29:55 +01:00
										 |  |  | 			snprintf(enddata, sizeof(enddata), "%c[%dm", ESC, ATTR_RESET); | 
					
						
							| 
									
										
										
										
											2008-08-25 23:13:32 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-11-25 20:29:55 +01:00
										 |  |  | 		snprintf(quitdata, sizeof(quitdata), "%c[%dm", ESC, ATTR_RESET); | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-03-16 22:37:31 +00:00
										 |  |  | char *term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout) | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | 	int attr = 0; | 
					
						
							| 
									
										
										
										
											2008-08-25 23:13:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  | 	if (!vt100compat) { | 
					
						
							| 
									
										
										
										
											2005-07-10 22:56:21 +00:00
										 |  |  | 		ast_copy_string(outbuf, inbuf, maxout); | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  | 		return outbuf; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-08-25 23:13:32 +00:00
										 |  |  | 	if (!fgcolor) { | 
					
						
							| 
									
										
										
										
											2005-07-10 22:56:21 +00:00
										 |  |  | 		ast_copy_string(outbuf, inbuf, maxout); | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  | 		return outbuf; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (fgcolor & 128) { | 
					
						
							| 
									
										
										
										
											2008-08-25 23:13:32 +00:00
										 |  |  | 		attr = ast_opt_light_background ? 0 : ATTR_BRIGHT; | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  | 		fgcolor &= ~128; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-08-25 23:13:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-07 17:44:32 +00:00
										 |  |  | 	if (bgcolor) { | 
					
						
							|  |  |  | 		bgcolor &= ~128; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-25 23:13:32 +00:00
										 |  |  | 	if (ast_opt_light_background) { | 
					
						
							|  |  |  | 		fgcolor = opposite(fgcolor); | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-08-25 23:13:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-07 17:44:32 +00:00
										 |  |  | 	if (ast_opt_force_black_background) { | 
					
						
							| 
									
										
										
										
											2015-11-25 20:29:55 +01:00
										 |  |  | 		if (!bgcolor) { | 
					
						
							|  |  |  | 			bgcolor = COLOR_BLACK; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		snprintf(outbuf, maxout, "%c[%d;%d;%dm%s%s", ESC, attr, fgcolor, bgcolor + 10, inbuf, term_end()); | 
					
						
							| 
									
										
										
										
											2008-10-07 17:44:32 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2015-11-25 20:29:55 +01:00
										 |  |  | 		snprintf(outbuf, maxout, "%c[%d;%dm%s%s", ESC, attr, fgcolor, inbuf, term_end()); | 
					
						
							| 
									
										
										
										
											2008-10-07 17:44:32 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  | 	return outbuf; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | static void check_fgcolor(int *fgcolor, int *attr) | 
					
						
							| 
									
										
										
										
											2004-05-02 19:13:16 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-02-14 18:47:56 +00:00
										 |  |  | 	*attr = ast_opt_light_background ? 0 : ATTR_BRIGHT; | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | 	if (*fgcolor & 128) { | 
					
						
							|  |  |  | 		*fgcolor &= ~128; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-02-14 18:47:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | 	if (ast_opt_light_background) { | 
					
						
							|  |  |  | 		*fgcolor = opposite(*fgcolor); | 
					
						
							| 
									
										
										
										
											2004-05-02 19:13:16 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2004-05-02 19:13:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | static void check_bgcolor(int *bgcolor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (*bgcolor) { | 
					
						
							|  |  |  | 		*bgcolor &= ~128; | 
					
						
							| 
									
										
										
										
											2004-05-02 19:13:16 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-08-25 23:13:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-25 20:29:55 +01:00
										 |  |  | static int check_colors_allowed(void) | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-11-25 20:29:55 +01:00
										 |  |  | 	return vt100compat; | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int ast_term_color_code(struct ast_str **str, int fgcolor, int bgcolor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int attr = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-25 20:29:55 +01:00
										 |  |  | 	if (!check_colors_allowed()) { | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2004-05-02 19:13:16 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-08-25 23:13:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | 	check_fgcolor(&fgcolor, &attr); | 
					
						
							|  |  |  | 	check_bgcolor(&bgcolor); | 
					
						
							| 
									
										
										
										
											2013-02-14 18:47:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | 	if (ast_opt_force_black_background) { | 
					
						
							|  |  |  | 		ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10); | 
					
						
							|  |  |  | 	} else if (bgcolor) { | 
					
						
							|  |  |  | 		ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		ast_str_append(str, 0, "%c[%d;%dm", ESC, attr, fgcolor); | 
					
						
							| 
									
										
										
										
											2008-10-07 17:44:32 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | char *term_color_code(char *outbuf, int fgcolor, int bgcolor, int maxout) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int attr = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-25 20:29:55 +01:00
										 |  |  | 	if (!check_colors_allowed()) { | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | 		*outbuf = '\0'; | 
					
						
							|  |  |  | 		return outbuf; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	check_fgcolor(&fgcolor, &attr); | 
					
						
							|  |  |  | 	check_bgcolor(&bgcolor); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-07 17:44:32 +00:00
										 |  |  | 	if (ast_opt_force_black_background) { | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | 		snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10); | 
					
						
							|  |  |  | 	} else if (bgcolor) { | 
					
						
							| 
									
										
										
										
											2008-10-07 17:44:32 +00:00
										 |  |  | 		snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		snprintf(outbuf, maxout, "%c[%d;%dm", ESC, attr, fgcolor); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-02 19:13:16 +00:00
										 |  |  | 	return outbuf; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-14 18:47:56 +00:00
										 |  |  | const char *ast_term_color(int fgcolor, int bgcolor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct commonbuf *cb = ast_threadstorage_get(&commonbuf, sizeof(*cb)); | 
					
						
							|  |  |  | 	char *buf; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!cb) { | 
					
						
							|  |  |  | 		return ""; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	buf = cb->buffer[cb->which++]; | 
					
						
							|  |  |  | 	if (cb->which == AST_TERM_MAX_ROTATING_BUFFERS) { | 
					
						
							|  |  |  | 		cb->which = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return term_color_code(buf, fgcolor, bgcolor, AST_TERM_MAX_ESCAPE_CHARS); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const char *ast_term_reset(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-11-25 20:29:55 +01:00
										 |  |  | 	return term_end(); | 
					
						
							| 
									
										
										
										
											2013-02-14 18:47:56 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-24 21:17:26 +00:00
										 |  |  | char *term_strip(char *outbuf, const char *inbuf, int maxout) | 
					
						
							| 
									
										
										
										
											2004-05-02 19:13:16 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-02-24 21:17:26 +00:00
										 |  |  | 	char *outbuf_ptr = outbuf; | 
					
						
							|  |  |  | 	const char *inbuf_ptr = inbuf; | 
					
						
							| 
									
										
										
										
											2004-05-02 19:13:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	while (outbuf_ptr < outbuf + maxout) { | 
					
						
							|  |  |  | 		switch (*inbuf_ptr) { | 
					
						
							|  |  |  | 			case ESC: | 
					
						
							|  |  |  | 				while (*inbuf_ptr && (*inbuf_ptr != 'm')) | 
					
						
							|  |  |  | 					inbuf_ptr++; | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			default: | 
					
						
							|  |  |  | 				*outbuf_ptr = *inbuf_ptr; | 
					
						
							|  |  |  | 				outbuf_ptr++; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (! *inbuf_ptr) | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		inbuf_ptr++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return outbuf; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-11-11 02:12:27 +00:00
										 |  |  | /* filter escape sequences */ | 
					
						
							|  |  |  | void term_filter_escapes(char *line) | 
					
						
							| 
									
										
										
										
											2006-11-15 00:19:35 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 	int len = strlen(line); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < len; i++) { | 
					
						
							| 
									
										
										
										
											2006-11-11 15:22:08 +00:00
										 |  |  | 		if (line[i] != ESC) | 
					
						
							|  |  |  | 			continue; | 
					
						
							| 
									
										
										
										
											2006-11-15 00:19:35 +00:00
										 |  |  | 		if ((i < (len - 2)) && | 
					
						
							|  |  |  | 		    (line[i + 1] == 0x5B)) { | 
					
						
							| 
									
										
										
										
											2006-11-11 15:22:08 +00:00
										 |  |  | 			switch (line[i + 2]) { | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 			case 0x30: | 
					
						
							| 
									
										
										
										
											2006-11-15 00:19:35 +00:00
										 |  |  | 			case 0x31: | 
					
						
							|  |  |  | 			case 0x33: | 
					
						
							|  |  |  | 				continue; | 
					
						
							| 
									
										
										
										
											2006-11-11 15:22:08 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2006-11-15 00:19:35 +00:00
										 |  |  | 		/* replace ESC with a space */ | 
					
						
							|  |  |  | 		line[i] = ' '; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2006-11-11 02:12:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-14 18:47:56 +00:00
										 |  |  | const char *term_end(void) | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	return enddata; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-14 18:47:56 +00:00
										 |  |  | const char *term_quit(void) | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	return quitdata; | 
					
						
							|  |  |  | } |