2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2010, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Terminal Routines
23 * \author Mark Spencer <markster@digium.com>
27 <support_level>core</support_level>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/_private.h"
40 #include "asterisk/term.h"
41 #include "asterisk/lock.h"
42 #include "asterisk/utils.h"
43 #include "asterisk/threadstorage.h"
45 static int vt100compat;
47 static char prepdata[80] = "";
48 static char enddata[80] = "";
49 static char quitdata[80] = "";
51 static const char * const termpath[] = {
52 "/usr/share/terminfo",
53 "/usr/local/share/misc/terminfo",
58 AST_THREADSTORAGE(commonbuf);
62 char buffer[AST_TERM_MAX_ROTATING_BUFFERS][AST_TERM_MAX_ESCAPE_CHARS];
65 static int opposite(int color)
68 /* BLACK */ COLOR_BLACK,
69 /* RED */ COLOR_MAGENTA,
70 /* GREEN */ COLOR_GREEN,
71 /* BROWN */ COLOR_BROWN,
72 /* BLUE */ COLOR_CYAN,
73 /* MAGENTA */ COLOR_RED,
74 /* CYAN */ COLOR_BLUE,
75 /* WHITE */ COLOR_BLACK };
76 return color ? lookup[color - 30] : 0;
79 /* Ripped off from Ross Ridge, but it's public domain code (libmytinfo) */
80 static short convshort(char *s)
84 a = (int) s[0] & 0377;
85 b = (int) s[1] & 0377;
87 if (a == 0377 && b == 0377)
89 if (a == 0376 && b == 0377)
95 int ast_term_init(void)
97 char *term = getenv("TERM");
98 char termfile[256] = "";
99 char buffer[512] = "";
100 int termfd = -1, parseokay = 0, i;
102 if (ast_opt_no_color) {
106 if (!ast_opt_console) {
107 /* If any remote console is not compatible, we'll strip the color codes at that point */
117 if (termpath[i] == NULL) {
120 snprintf(termfile, sizeof(termfile), "%s/%c/%s", termpath[i], *term, term);
121 termfd = open(termfile, O_RDONLY);
127 int actsize = read(termfd, buffer, sizeof(buffer) - 1);
128 short sz_names = convshort(buffer + 2);
129 short sz_bools = convshort(buffer + 4);
130 short n_nums = convshort(buffer + 6);
132 /* if ((sz_names + sz_bools) & 1)
135 if (sz_names + sz_bools + n_nums < actsize) {
136 /* Offset 13 is defined in /usr/include/term.h, though we do not
137 * include it here, as it conflicts with include/asterisk/term.h */
138 short max_colors = convshort(buffer + 12 + sz_names + sz_bools + 13 * 2);
139 if (max_colors > 0) {
148 /* These comparisons should not be substrings nor case-insensitive, as
149 * terminal types are very particular about how they treat suffixes and
150 * capitalization. For example, terminal type 'linux-m' does NOT
151 * support color, while 'linux' does. Not even all vt100* terminals
152 * support color, either (e.g. 'vt100+fnkeys'). */
153 if (!strcmp(term, "linux")) {
155 } else if (!strcmp(term, "xterm")) {
157 } else if (!strcmp(term, "xterm-color")) {
159 } else if (!strcmp(term, "xterm-256color")) {
161 } else if (!strncmp(term, "Eterm", 5)) {
162 /* Both entries which start with Eterm support color */
164 } else if (!strcmp(term, "vt100")) {
166 } else if (!strncmp(term, "crt", 3)) {
167 /* Both crt terminals support color */
174 /* Make commands show up in nice colors */
175 if (ast_opt_light_background) {
176 snprintf(prepdata, sizeof(prepdata), "%c[%dm", ESC, COLOR_BROWN);
177 snprintf(enddata, sizeof(enddata), "%c[%dm", ESC, COLOR_BLACK);
178 snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
179 } else if (ast_opt_force_black_background) {
180 snprintf(prepdata, sizeof(prepdata), "%c[%d;%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN, COLOR_BLACK + 10);
181 snprintf(enddata, sizeof(enddata), "%c[%d;%d;%dm", ESC, ATTR_RESET, COLOR_WHITE, COLOR_BLACK + 10);
182 snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
184 snprintf(prepdata, sizeof(prepdata), "%c[%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN);
185 snprintf(enddata, sizeof(enddata), "%c[%d;%dm", ESC, ATTR_RESET, COLOR_WHITE);
186 snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
192 char *term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout)
197 ast_copy_string(outbuf, inbuf, maxout);
201 ast_copy_string(outbuf, inbuf, maxout);
206 attr = ast_opt_light_background ? 0 : ATTR_BRIGHT;
214 if (ast_opt_light_background) {
215 fgcolor = opposite(fgcolor);
218 if (ast_opt_force_black_background) {
219 snprintf(outbuf, maxout, "%c[%d;%d;%dm%s%c[%d;%dm", ESC, attr, fgcolor, bgcolor + 10, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10);
221 snprintf(outbuf, maxout, "%c[%d;%dm%s%c[0m", ESC, attr, fgcolor, inbuf, ESC);
226 static void check_fgcolor(int *fgcolor, int *attr)
228 *attr = ast_opt_light_background ? 0 : ATTR_BRIGHT;
229 if (*fgcolor & 128) {
233 if (ast_opt_light_background) {
234 *fgcolor = opposite(*fgcolor);
238 static void check_bgcolor(int *bgcolor)
245 static int check_colors_allowed(int fgcolor)
247 return (!vt100compat || !fgcolor) ? 0 : 1;
250 int ast_term_color_code(struct ast_str **str, int fgcolor, int bgcolor)
254 if (!check_colors_allowed(fgcolor)) {
258 check_fgcolor(&fgcolor, &attr);
259 check_bgcolor(&bgcolor);
261 if (ast_opt_force_black_background) {
262 ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10);
263 } else if (bgcolor) {
264 ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10);
266 ast_str_append(str, 0, "%c[%d;%dm", ESC, attr, fgcolor);
272 char *term_color_code(char *outbuf, int fgcolor, int bgcolor, int maxout)
276 if (!check_colors_allowed(fgcolor)) {
281 check_fgcolor(&fgcolor, &attr);
282 check_bgcolor(&bgcolor);
284 if (ast_opt_force_black_background) {
285 snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10);
286 } else if (bgcolor) {
287 snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10);
289 snprintf(outbuf, maxout, "%c[%d;%dm", ESC, attr, fgcolor);
295 const char *ast_term_color(int fgcolor, int bgcolor)
297 struct commonbuf *cb = ast_threadstorage_get(&commonbuf, sizeof(*cb));
303 buf = cb->buffer[cb->which++];
304 if (cb->which == AST_TERM_MAX_ROTATING_BUFFERS) {
308 return term_color_code(buf, fgcolor, bgcolor, AST_TERM_MAX_ESCAPE_CHARS);
311 const char *ast_term_reset(void)
313 if (ast_opt_force_black_background) {
314 static const char reset[] = { ESC, '[', COLOR_BLACK + 10, 'm', 0 };
321 char *term_strip(char *outbuf, const char *inbuf, int maxout)
323 char *outbuf_ptr = outbuf;
324 const char *inbuf_ptr = inbuf;
326 while (outbuf_ptr < outbuf + maxout) {
327 switch (*inbuf_ptr) {
329 while (*inbuf_ptr && (*inbuf_ptr != 'm'))
333 *outbuf_ptr = *inbuf_ptr;
343 char *term_prompt(char *outbuf, const char *inbuf, int maxout)
346 ast_copy_string(outbuf, inbuf, maxout);
349 if (ast_opt_force_black_background) {
350 snprintf(outbuf, maxout, "%c[%d;%d;%dm%c%c[%d;%dm%s",
351 ESC, ATTR_BRIGHT, COLOR_BLUE, COLOR_BLACK + 10,
353 ESC, COLOR_WHITE, COLOR_BLACK + 10,
355 } else if (ast_opt_light_background) {
356 snprintf(outbuf, maxout, "%c[%d;0m%c%c[0m%s",
362 snprintf(outbuf, maxout, "%c[%d;%d;0m%c%c[0m%s",
363 ESC, ATTR_BRIGHT, COLOR_BLUE,
371 /* filter escape sequences */
372 void term_filter_escapes(char *line)
375 int len = strlen(line);
377 for (i = 0; i < len; i++) {
380 if ((i < (len - 2)) &&
381 (line[i + 1] == 0x5B)) {
382 switch (line[i + 2]) {
389 /* replace ESC with a space */
394 const char *term_prep(void)
399 const char *term_end(void)
404 const char *term_quit(void)