2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, 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>
28 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
30 #include "asterisk/_private.h"
36 #include "asterisk/term.h"
37 #include "asterisk/lock.h"
38 #include "asterisk/utils.h"
40 static int vt100compat
;
42 static char prepdata
[80] = "";
43 static char enddata
[80] = "";
44 static char quitdata
[80] = "";
46 static const char *termpath
[] = {
47 "/usr/share/terminfo",
48 "/usr/local/share/misc/terminfo",
53 /* Ripped off from Ross Ridge, but it's public domain code (libmytinfo) */
54 static short convshort(char *s
)
58 a
= (int) s
[0] & 0377;
59 b
= (int) s
[1] & 0377;
61 if (a
== 0377 && b
== 0377)
63 if (a
== 0376 && b
== 0377)
69 int ast_term_init(void)
71 char *term
= getenv("TERM");
72 char termfile
[256] = "";
73 char buffer
[512] = "";
74 int termfd
= -1, parseokay
= 0, i
;
78 if (!ast_opt_console
|| ast_opt_no_color
|| !ast_opt_no_fork
)
82 if (termpath
[i
] == NULL
) {
85 snprintf(termfile
, sizeof(termfile
), "%s/%c/%s", termpath
[i
], *term
, term
);
86 termfd
= open(termfile
, O_RDONLY
);
92 int actsize
= read(termfd
, buffer
, sizeof(buffer
) - 1);
93 short sz_names
= convshort(buffer
+ 2);
94 short sz_bools
= convshort(buffer
+ 4);
95 short n_nums
= convshort(buffer
+ 6);
97 /* if ((sz_names + sz_bools) & 1)
100 if (sz_names
+ sz_bools
+ n_nums
< actsize
) {
101 /* Offset 13 is defined in /usr/include/term.h, though we do not
102 * include it here, as it conflicts with include/asterisk/term.h */
103 short max_colors
= convshort(buffer
+ 12 + sz_names
+ sz_bools
+ 13 * 2);
104 if (max_colors
> 0) {
113 /* These comparisons should not be substrings nor case-insensitive, as
114 * terminal types are very particular about how they treat suffixes and
115 * capitalization. For example, terminal type 'linux-m' does NOT
116 * support color, while 'linux' does. Not even all vt100* terminals
117 * support color, either (e.g. 'vt100+fnkeys'). */
118 if (!strcmp(term
, "linux")) {
120 } else if (!strcmp(term
, "xterm")) {
122 } else if (!strcmp(term
, "xterm-color")) {
124 } else if (!strncmp(term
, "Eterm", 5)) {
125 /* Both entries which start with Eterm support color */
127 } else if (!strcmp(term
, "vt100")) {
129 } else if (!strncmp(term
, "crt", 3)) {
130 /* Both crt terminals support color */
136 /* Make commands show up in nice colors */
137 snprintf(prepdata
, sizeof(prepdata
), "%c[%d;%d;%dm", ESC
, ATTR_BRIGHT
, COLOR_BROWN
, COLOR_BLACK
+ 10);
138 snprintf(enddata
, sizeof(enddata
), "%c[%d;%d;%dm", ESC
, ATTR_RESET
, COLOR_WHITE
, COLOR_BLACK
+ 10);
139 snprintf(quitdata
, sizeof(quitdata
), "%c[0m", ESC
);
144 char *term_color(char *outbuf
, const char *inbuf
, int fgcolor
, int bgcolor
, int maxout
)
149 ast_copy_string(outbuf
, inbuf
, maxout
);
152 if (!fgcolor
&& !bgcolor
) {
153 ast_copy_string(outbuf
, inbuf
, maxout
);
156 if ((fgcolor
& 128) && (bgcolor
& 128)) {
157 /* Can't both be highlighted */
158 ast_copy_string(outbuf
, inbuf
, maxout
);
162 bgcolor
= COLOR_BLACK
;
172 if (fgcolor
&& bgcolor
) {
173 snprintf(tmp
, sizeof(tmp
), "%d;%d", fgcolor
, bgcolor
);
174 } else if (bgcolor
) {
175 snprintf(tmp
, sizeof(tmp
), "%d", bgcolor
);
176 } else if (fgcolor
) {
177 snprintf(tmp
, sizeof(tmp
), "%d", fgcolor
);
180 snprintf(outbuf
, maxout
, "%c[%d;%sm%s%c[0;%d;%dm", ESC
, attr
, tmp
, inbuf
, ESC
, COLOR_WHITE
, COLOR_BLACK
+ 10);
182 snprintf(outbuf
, maxout
, "%c[%sm%s%c[0;%d;%dm", ESC
, tmp
, inbuf
, ESC
, COLOR_WHITE
, COLOR_BLACK
+ 10);
187 char *term_color_code(char *outbuf
, int fgcolor
, int bgcolor
, int maxout
)
191 if ((!vt100compat
) || (!fgcolor
&& !bgcolor
)) {
195 if ((fgcolor
& 128) && (bgcolor
& 128)) {
196 /* Can't both be highlighted */
201 bgcolor
= COLOR_BLACK
;
211 if (fgcolor
&& bgcolor
) {
212 snprintf(tmp
, sizeof(tmp
), "%d;%d", fgcolor
, bgcolor
);
213 } else if (bgcolor
) {
214 snprintf(tmp
, sizeof(tmp
), "%d", bgcolor
);
215 } else if (fgcolor
) {
216 snprintf(tmp
, sizeof(tmp
), "%d", fgcolor
);
219 snprintf(outbuf
, maxout
, "%c[%d;%sm", ESC
, attr
, tmp
);
221 snprintf(outbuf
, maxout
, "%c[%sm", ESC
, tmp
);
226 char *term_strip(char *outbuf
, char *inbuf
, int maxout
)
228 char *outbuf_ptr
= outbuf
, *inbuf_ptr
= inbuf
;
230 while (outbuf_ptr
< outbuf
+ maxout
) {
231 switch (*inbuf_ptr
) {
233 while (*inbuf_ptr
&& (*inbuf_ptr
!= 'm'))
237 *outbuf_ptr
= *inbuf_ptr
;
247 char *term_prompt(char *outbuf
, const char *inbuf
, int maxout
)
250 ast_copy_string(outbuf
, inbuf
, maxout
);
253 snprintf(outbuf
, maxout
, "%c[%d;%d;%dm%c%c[%d;%d;%dm%s",
254 ESC
, ATTR_BRIGHT
, COLOR_BLUE
, COLOR_BLACK
+ 10,
256 ESC
, 0, COLOR_WHITE
, COLOR_BLACK
+ 10,
261 /* filter escape sequences */
262 void term_filter_escapes(char *line
)
265 int len
= strlen(line
);
267 for (i
= 0; i
< len
; i
++) {
270 if ((i
< (len
- 2)) &&
271 (line
[i
+ 1] == 0x5B)) {
272 switch (line
[i
+ 2]) {
279 /* replace ESC with a space */
284 char *term_prep(void)
294 char *term_quit(void)