| 
									
										
										
										
											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
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Copyright (C) 1999 - 2005, 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
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  |  * \brief Terminal Routines  | 
					
						
							| 
									
										
										
										
											2005-12-30 21:18:06 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * \author Mark Spencer <markster@digium.com>  | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											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 prepdata[80] = ""; | 
					
						
							|  |  |  | static char enddata[80] = ""; | 
					
						
							|  |  |  | static char quitdata[80] = ""; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-01 03:11:52 +00:00
										 |  |  | static const char *termpath[] = { | 
					
						
							|  |  |  | 	"/usr/share/terminfo", | 
					
						
							|  |  |  | 	"/usr/local/share/misc/terminfo", | 
					
						
							|  |  |  | 	"/usr/lib/terminfo", | 
					
						
							|  |  |  | 	NULL | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Ripped off from Ross Ridge, but it's public domain code (libmytinfo) */ | 
					
						
							|  |  |  | static short convshort(char *s) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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] = ""; | 
					
						
							|  |  |  | 	char buffer[512] = ""; | 
					
						
							|  |  |  | 	int termfd = -1, parseokay = 0, i; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  | 	if (!term) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2007-11-03 14:19:47 +00:00
										 |  |  | 	if (!ast_opt_console || ast_opt_no_color || !ast_opt_no_fork) | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2004-10-01 03:11:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (i=0 ;; i++) { | 
					
						
							|  |  |  | 		if (termpath[i] == NULL) { | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		snprintf(termfile, sizeof(termfile), "%s/%c/%s", termpath[i], *term, term); | 
					
						
							|  |  |  | 		termfd = open(termfile, O_RDONLY); | 
					
						
							|  |  |  | 		if (termfd > -1) { | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (termfd > -1) { | 
					
						
							|  |  |  | 		int actsize = read(termfd, buffer, sizeof(buffer) - 1); | 
					
						
							|  |  |  | 		short sz_names = convshort(buffer + 2); | 
					
						
							|  |  |  | 		short sz_bools = convshort(buffer + 4); | 
					
						
							|  |  |  | 		short n_nums   = convshort(buffer + 6); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* if ((sz_names + sz_bools) & 1)
 | 
					
						
							|  |  |  | 			sz_bools++; */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (sz_names + sz_bools + n_nums < actsize) { | 
					
						
							|  |  |  | 			/* Offset 13 is defined in /usr/include/term.h, though we do not
 | 
					
						
							|  |  |  | 			 * include it here, as it conflicts with include/asterisk/term.h */ | 
					
						
							|  |  |  | 			short max_colors = convshort(buffer + 12 + sz_names + sz_bools + 13 * 2); | 
					
						
							|  |  |  | 			if (max_colors > 0) { | 
					
						
							|  |  |  | 				vt100compat = 1; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			parseokay = 1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		close(termfd); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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; | 
					
						
							|  |  |  | 		} 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; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  | 	if (vt100compat) { | 
					
						
							|  |  |  | 		/* Make commands show up in nice colors */ | 
					
						
							|  |  |  | 		snprintf(prepdata, sizeof(prepdata), "%c[%d;%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN, COLOR_BLACK + 10); | 
					
						
							|  |  |  | 		snprintf(enddata, sizeof(enddata), "%c[%d;%d;%dm", ESC, ATTR_RESET, COLOR_WHITE, COLOR_BLACK + 10); | 
					
						
							|  |  |  | 		snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	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; | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  | 	char tmp[40]; | 
					
						
							|  |  |  | 	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; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (!fgcolor && !bgcolor) { | 
					
						
							| 
									
										
										
										
											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) && (bgcolor & 128)) { | 
					
						
							|  |  |  | 		/* Can't both be highlighted */ | 
					
						
							| 
									
										
										
										
											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 (!bgcolor) | 
					
						
							|  |  |  | 		bgcolor = COLOR_BLACK; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (bgcolor) { | 
					
						
							|  |  |  | 		bgcolor &= ~128; | 
					
						
							|  |  |  | 		bgcolor += 10; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (fgcolor & 128) { | 
					
						
							|  |  |  | 		attr = ATTR_BRIGHT; | 
					
						
							|  |  |  | 		fgcolor &= ~128; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (fgcolor && bgcolor) { | 
					
						
							|  |  |  | 		snprintf(tmp, sizeof(tmp), "%d;%d", fgcolor, bgcolor); | 
					
						
							|  |  |  | 	} else if (bgcolor) { | 
					
						
							|  |  |  | 		snprintf(tmp, sizeof(tmp), "%d", bgcolor); | 
					
						
							|  |  |  | 	} else if (fgcolor) { | 
					
						
							|  |  |  | 		snprintf(tmp, sizeof(tmp), "%d", fgcolor); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (attr) { | 
					
						
							|  |  |  | 		snprintf(outbuf, maxout, "%c[%d;%sm%s%c[0;%d;%dm", ESC, attr, tmp, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		snprintf(outbuf, maxout, "%c[%sm%s%c[0;%d;%dm", ESC, tmp, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return outbuf; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-02 19:13:16 +00:00
										 |  |  | char *term_color_code(char *outbuf, int fgcolor, int bgcolor, int maxout) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int attr=0; | 
					
						
							|  |  |  | 	char tmp[40]; | 
					
						
							|  |  |  | 	if ((!vt100compat) || (!fgcolor && !bgcolor)) { | 
					
						
							|  |  |  | 		*outbuf = '\0'; | 
					
						
							|  |  |  | 		return outbuf; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if ((fgcolor & 128) && (bgcolor & 128)) { | 
					
						
							|  |  |  | 		/* Can't both be highlighted */ | 
					
						
							|  |  |  | 		*outbuf = '\0'; | 
					
						
							|  |  |  | 		return outbuf; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (!bgcolor) | 
					
						
							|  |  |  | 		bgcolor = COLOR_BLACK; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (bgcolor) { | 
					
						
							|  |  |  | 		bgcolor &= ~128; | 
					
						
							|  |  |  | 		bgcolor += 10; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (fgcolor & 128) { | 
					
						
							|  |  |  | 		attr = ATTR_BRIGHT; | 
					
						
							|  |  |  | 		fgcolor &= ~128; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (fgcolor && bgcolor) { | 
					
						
							|  |  |  | 		snprintf(tmp, sizeof(tmp), "%d;%d", fgcolor, bgcolor); | 
					
						
							|  |  |  | 	} else if (bgcolor) { | 
					
						
							|  |  |  | 		snprintf(tmp, sizeof(tmp), "%d", bgcolor); | 
					
						
							|  |  |  | 	} else if (fgcolor) { | 
					
						
							|  |  |  | 		snprintf(tmp, sizeof(tmp), "%d", fgcolor); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (attr) { | 
					
						
							|  |  |  | 		snprintf(outbuf, maxout, "%c[%d;%sm", ESC, attr, tmp); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		snprintf(outbuf, maxout, "%c[%sm", ESC, tmp); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return outbuf; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | char *term_strip(char *outbuf, char *inbuf, int maxout) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char *outbuf_ptr = outbuf, *inbuf_ptr = inbuf; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-03-16 22:37:31 +00:00
										 |  |  | char *term_prompt(char *outbuf, const char *inbuf, int maxout) | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	snprintf(outbuf, maxout, "%c[%d;%d;%dm%c%c[%d;%d;%dm%s", | 
					
						
							|  |  |  | 		ESC, ATTR_BRIGHT, COLOR_BLUE, COLOR_BLACK + 10, | 
					
						
							|  |  |  | 		inbuf[0], | 
					
						
							|  |  |  | 		ESC, 0, COLOR_WHITE, COLOR_BLACK + 10, | 
					
						
							|  |  |  | 		inbuf + 1); | 
					
						
							|  |  |  | 	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]) { | 
					
						
							| 
									
										
										
										
											2006-11-15 00:19:35 +00:00
										 |  |  | 		 	case 0x30: | 
					
						
							|  |  |  | 			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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-05-14 03:17:45 +00:00
										 |  |  | char *term_prep(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return prepdata; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | char *term_end(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return enddata; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | char *term_quit(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return quitdata; | 
					
						
							|  |  |  | } |