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$")
36 #include <sys/types.h>
41 #include "asterisk/term.h"
42 #include "asterisk/options.h"
43 #include "asterisk/lock.h"
44 #include "asterisk/utils.h"
46 static int vt100compat
;
48 static char prepdata
[80] = "";
49 static char enddata
[80] = "";
50 static char quitdata
[80] = "";
52 static const char *termpath
[] = {
53 "/usr/share/terminfo",
54 "/usr/local/share/misc/terminfo",
59 /* Ripped off from Ross Ridge, but it's public domain code (libmytinfo) */
60 static short convshort(char *s
)
64 a
= (int) s
[0] & 0377;
65 b
= (int) s
[1] & 0377;
67 if (a
== 0377 && b
== 0377)
69 if (a
== 0376 && b
== 0377)
75 int ast_term_init(void)
77 char *term
= getenv("TERM");
78 char termfile
[256] = "";
79 char buffer
[512] = "";
80 int termfd
= -1, parseokay
= 0, i
;
84 if (!ast_opt_console
|| ast_opt_no_color
|| !ast_opt_no_fork
)
88 if (termpath
[i
] == NULL
) {
91 snprintf(termfile
, sizeof(termfile
), "%s/%c/%s", termpath
[i
], *term
, term
);
92 termfd
= open(termfile
, O_RDONLY
);
98 int actsize
= read(termfd
, buffer
, sizeof(buffer
) - 1);
99 short sz_names
= convshort(buffer
+ 2);
100 short sz_bools
= convshort(buffer
+ 4);
101 short n_nums
= convshort(buffer
+ 6);
103 /* if ((sz_names + sz_bools) & 1)
106 if (sz_names
+ sz_bools
+ n_nums
< actsize
) {
107 /* Offset 13 is defined in /usr/include/term.h, though we do not
108 * include it here, as it conflicts with include/asterisk/term.h */
109 short max_colors
= convshort(buffer
+ 12 + sz_names
+ sz_bools
+ 13 * 2);
110 if (max_colors
> 0) {
119 /* These comparisons should not be substrings nor case-insensitive, as
120 * terminal types are very particular about how they treat suffixes and
121 * capitalization. For example, terminal type 'linux-m' does NOT
122 * support color, while 'linux' does. Not even all vt100* terminals
123 * support color, either (e.g. 'vt100+fnkeys'). */
124 if (!strcmp(term
, "linux")) {
126 } else if (!strcmp(term
, "xterm")) {
128 } else if (!strcmp(term
, "xterm-color")) {
130 } else if (!strncmp(term
, "Eterm", 5)) {
131 /* Both entries which start with Eterm support color */
133 } else if (!strcmp(term
, "vt100")) {
135 } else if (!strncmp(term
, "crt", 3)) {
136 /* Both crt terminals support color */
142 /* Make commands show up in nice colors */
143 snprintf(prepdata
, sizeof(prepdata
), "%c[%d;%d;%dm", ESC
, ATTR_BRIGHT
, COLOR_BROWN
, COLOR_BLACK
+ 10);
144 snprintf(enddata
, sizeof(enddata
), "%c[%d;%d;%dm", ESC
, ATTR_RESET
, COLOR_WHITE
, COLOR_BLACK
+ 10);
145 snprintf(quitdata
, sizeof(quitdata
), "%c[0m", ESC
);
150 char *term_color(char *outbuf
, const char *inbuf
, int fgcolor
, int bgcolor
, int maxout
)
155 ast_copy_string(outbuf
, inbuf
, maxout
);
158 if (!fgcolor
&& !bgcolor
) {
159 ast_copy_string(outbuf
, inbuf
, maxout
);
162 if ((fgcolor
& 128) && (bgcolor
& 128)) {
163 /* Can't both be highlighted */
164 ast_copy_string(outbuf
, inbuf
, maxout
);
168 bgcolor
= COLOR_BLACK
;
178 if (fgcolor
&& bgcolor
) {
179 snprintf(tmp
, sizeof(tmp
), "%d;%d", fgcolor
, bgcolor
);
180 } else if (bgcolor
) {
181 snprintf(tmp
, sizeof(tmp
), "%d", bgcolor
);
182 } else if (fgcolor
) {
183 snprintf(tmp
, sizeof(tmp
), "%d", fgcolor
);
186 snprintf(outbuf
, maxout
, "%c[%d;%sm%s%c[0;%d;%dm", ESC
, attr
, tmp
, inbuf
, ESC
, COLOR_WHITE
, COLOR_BLACK
+ 10);
188 snprintf(outbuf
, maxout
, "%c[%sm%s%c[0;%d;%dm", ESC
, tmp
, inbuf
, ESC
, COLOR_WHITE
, COLOR_BLACK
+ 10);
193 char *term_color_code(char *outbuf
, int fgcolor
, int bgcolor
, int maxout
)
197 if ((!vt100compat
) || (!fgcolor
&& !bgcolor
)) {
201 if ((fgcolor
& 128) && (bgcolor
& 128)) {
202 /* Can't both be highlighted */
207 bgcolor
= COLOR_BLACK
;
217 if (fgcolor
&& bgcolor
) {
218 snprintf(tmp
, sizeof(tmp
), "%d;%d", fgcolor
, bgcolor
);
219 } else if (bgcolor
) {
220 snprintf(tmp
, sizeof(tmp
), "%d", bgcolor
);
221 } else if (fgcolor
) {
222 snprintf(tmp
, sizeof(tmp
), "%d", fgcolor
);
225 snprintf(outbuf
, maxout
, "%c[%d;%sm", ESC
, attr
, tmp
);
227 snprintf(outbuf
, maxout
, "%c[%sm", ESC
, tmp
);
232 char *term_strip(char *outbuf
, char *inbuf
, int maxout
)
234 char *outbuf_ptr
= outbuf
, *inbuf_ptr
= inbuf
;
236 while (outbuf_ptr
< outbuf
+ maxout
) {
237 switch (*inbuf_ptr
) {
239 while (*inbuf_ptr
&& (*inbuf_ptr
!= 'm'))
243 *outbuf_ptr
= *inbuf_ptr
;
253 char *term_prompt(char *outbuf
, const char *inbuf
, int maxout
)
256 ast_copy_string(outbuf
, inbuf
, maxout
);
259 snprintf(outbuf
, maxout
, "%c[%d;%d;%dm%c%c[%d;%d;%dm%s",
260 ESC
, ATTR_BRIGHT
, COLOR_BLUE
, COLOR_BLACK
+ 10,
262 ESC
, 0, COLOR_WHITE
, COLOR_BLACK
+ 10,
267 /* filter escape sequences */
268 void term_filter_escapes(char *line
)
271 int len
= strlen(line
);
273 for (i
= 0; i
< len
; i
++) {
276 if ((i
< (len
- 2)) &&
277 (line
[i
+ 1] == 0x5B)) {
278 switch (line
[i
+ 2]) {
285 /* replace ESC with a space */
290 char *term_prep(void)
300 char *term_quit(void)