#include "asterisk.h"
#include "asterisk/_private.h"
#include <sys/time.h>
#include <signal.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "asterisk/term.h"
#include "asterisk/lock.h"
#include "asterisk/utils.h"
Go to the source code of this file.
Functions | |
int | ast_term_color_code (struct ast_str **str, int fgcolor, int bgcolor) |
Append a color sequence to an ast_str. | |
int | ast_term_init (void) |
static void | check_bgcolor (int *bgcolor) |
static int | check_colors_allowed (int fgcolor) |
static void | check_fgcolor (int *fgcolor, int *attr) |
static short | convshort (char *s) |
static int | opposite (int color) |
char * | term_color (char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout) |
char * | term_color_code (char *outbuf, int fgcolor, int bgcolor, int maxout) |
Write a color sequence to a string. | |
char * | term_end (void) |
void | term_filter_escapes (char *line) |
char * | term_prep (void) |
char * | term_prompt (char *outbuf, const char *inbuf, int maxout) |
char * | term_quit (void) |
char * | term_strip (char *outbuf, const char *inbuf, int maxout) |
Variables | |
static char | enddata [80] = "" |
static char | prepdata [80] = "" |
static char | quitdata [80] = "" |
static const char *const | termpath [] |
static int | vt100compat |
Definition in file term.c.
int ast_term_color_code | ( | struct ast_str ** | str, | |
int | fgcolor, | |||
int | bgcolor | |||
) |
Append a color sequence to an ast_str.
str | The string to append to | |
fgcolor | foreground color | |
bgcolor | background color |
0 | success | |
-1 | failure |
Definition at line 238 of file term.c.
References ast_opt_force_black_background, ast_str_append(), check_bgcolor(), check_colors_allowed(), check_fgcolor(), COLOR_BLACK, ESC, and str.
Referenced by ast_xmldoc_printable(), data_result_print_cli(), and data_result_print_cli_node().
00239 { 00240 int attr = 0; 00241 00242 if (!check_colors_allowed(fgcolor)) { 00243 return -1; 00244 } 00245 00246 check_fgcolor(&fgcolor, &attr); 00247 check_bgcolor(&bgcolor); 00248 00249 if (ast_opt_force_black_background) { 00250 ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10); 00251 } else if (bgcolor) { 00252 ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10); 00253 } else { 00254 ast_str_append(str, 0, "%c[%d;%dm", ESC, attr, fgcolor); 00255 } 00256 00257 return 0; 00258 }
int ast_term_init | ( | void | ) |
Provided by term.c
Definition at line 83 of file term.c.
References ast_opt_console, ast_opt_force_black_background, ast_opt_light_background, ast_opt_no_color, ATTR_BRIGHT, ATTR_RESET, COLOR_BLACK, COLOR_BROWN, COLOR_WHITE, convshort(), and ESC.
Referenced by main().
00084 { 00085 char *term = getenv("TERM"); 00086 char termfile[256] = ""; 00087 char buffer[512] = ""; 00088 int termfd = -1, parseokay = 0, i; 00089 00090 if (ast_opt_no_color) { 00091 return 0; 00092 } 00093 00094 if (!ast_opt_console) { 00095 /* If any remote console is not compatible, we'll strip the color codes at that point */ 00096 vt100compat = 1; 00097 goto end; 00098 } 00099 00100 if (!term) { 00101 return 0; 00102 } 00103 00104 for (i = 0;; i++) { 00105 if (termpath[i] == NULL) { 00106 break; 00107 } 00108 snprintf(termfile, sizeof(termfile), "%s/%c/%s", termpath[i], *term, term); 00109 termfd = open(termfile, O_RDONLY); 00110 if (termfd > -1) { 00111 break; 00112 } 00113 } 00114 if (termfd > -1) { 00115 int actsize = read(termfd, buffer, sizeof(buffer) - 1); 00116 short sz_names = convshort(buffer + 2); 00117 short sz_bools = convshort(buffer + 4); 00118 short n_nums = convshort(buffer + 6); 00119 00120 /* if ((sz_names + sz_bools) & 1) 00121 sz_bools++; */ 00122 00123 if (sz_names + sz_bools + n_nums < actsize) { 00124 /* Offset 13 is defined in /usr/include/term.h, though we do not 00125 * include it here, as it conflicts with include/asterisk/term.h */ 00126 short max_colors = convshort(buffer + 12 + sz_names + sz_bools + 13 * 2); 00127 if (max_colors > 0) { 00128 vt100compat = 1; 00129 } 00130 parseokay = 1; 00131 } 00132 close(termfd); 00133 } 00134 00135 if (!parseokay) { 00136 /* These comparisons should not be substrings nor case-insensitive, as 00137 * terminal types are very particular about how they treat suffixes and 00138 * capitalization. For example, terminal type 'linux-m' does NOT 00139 * support color, while 'linux' does. Not even all vt100* terminals 00140 * support color, either (e.g. 'vt100+fnkeys'). */ 00141 if (!strcmp(term, "linux")) { 00142 vt100compat = 1; 00143 } else if (!strcmp(term, "xterm")) { 00144 vt100compat = 1; 00145 } else if (!strcmp(term, "xterm-color")) { 00146 vt100compat = 1; 00147 } else if (!strcmp(term, "xterm-256color")) { 00148 vt100compat = 1; 00149 } else if (!strncmp(term, "Eterm", 5)) { 00150 /* Both entries which start with Eterm support color */ 00151 vt100compat = 1; 00152 } else if (!strcmp(term, "vt100")) { 00153 vt100compat = 1; 00154 } else if (!strncmp(term, "crt", 3)) { 00155 /* Both crt terminals support color */ 00156 vt100compat = 1; 00157 } 00158 } 00159 00160 end: 00161 if (vt100compat) { 00162 /* Make commands show up in nice colors */ 00163 if (ast_opt_light_background) { 00164 snprintf(prepdata, sizeof(prepdata), "%c[%dm", ESC, COLOR_BROWN); 00165 snprintf(enddata, sizeof(enddata), "%c[%dm", ESC, COLOR_BLACK); 00166 snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC); 00167 } else if (ast_opt_force_black_background) { 00168 snprintf(prepdata, sizeof(prepdata), "%c[%d;%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN, COLOR_BLACK + 10); 00169 snprintf(enddata, sizeof(enddata), "%c[%d;%d;%dm", ESC, ATTR_RESET, COLOR_WHITE, COLOR_BLACK + 10); 00170 snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC); 00171 } else { 00172 snprintf(prepdata, sizeof(prepdata), "%c[%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN); 00173 snprintf(enddata, sizeof(enddata), "%c[%d;%dm", ESC, ATTR_RESET, COLOR_WHITE); 00174 snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC); 00175 } 00176 } 00177 return 0; 00178 }
static void check_bgcolor | ( | int * | bgcolor | ) | [static] |
static int check_colors_allowed | ( | int | fgcolor | ) | [static] |
Definition at line 233 of file term.c.
Referenced by ast_term_color_code(), and term_color_code().
00234 { 00235 return (!vt100compat || !fgcolor) ? 0 : 1; 00236 }
static void check_fgcolor | ( | int * | fgcolor, | |
int * | attr | |||
) | [static] |
Definition at line 214 of file term.c.
References ast_opt_light_background, ATTR_BRIGHT, and opposite().
Referenced by ast_term_color_code(), and term_color_code().
00215 { 00216 if (*fgcolor & 128) { 00217 *attr = ast_opt_light_background ? 0 : ATTR_BRIGHT; 00218 *fgcolor &= ~128; 00219 } 00220 00221 if (ast_opt_light_background) { 00222 *fgcolor = opposite(*fgcolor); 00223 } 00224 }
static short convshort | ( | char * | s | ) | [static] |
Definition at line 68 of file term.c.
Referenced by ast_term_init().
00069 { 00070 register int a, b; 00071 00072 a = (int) s[0] & 0377; 00073 b = (int) s[1] & 0377; 00074 00075 if (a == 0377 && b == 0377) 00076 return -1; 00077 if (a == 0376 && b == 0377) 00078 return -2; 00079 00080 return a + b * 256; 00081 }
static int opposite | ( | int | color | ) | [static] |
Definition at line 53 of file term.c.
References COLOR_BLACK, COLOR_BLUE, COLOR_BROWN, COLOR_CYAN, COLOR_GREEN, COLOR_MAGENTA, and COLOR_RED.
Referenced by check_fgcolor(), and term_color().
00054 { 00055 int lookup[] = { 00056 /* BLACK */ COLOR_BLACK, 00057 /* RED */ COLOR_MAGENTA, 00058 /* GREEN */ COLOR_GREEN, 00059 /* BROWN */ COLOR_BROWN, 00060 /* BLUE */ COLOR_CYAN, 00061 /* MAGENTA */ COLOR_RED, 00062 /* CYAN */ COLOR_BLUE, 00063 /* WHITE */ COLOR_BLACK }; 00064 return color ? lookup[color - 30] : 0; 00065 }
char* term_color | ( | char * | outbuf, | |
const char * | inbuf, | |||
int | fgcolor, | |||
int | bgcolor, | |||
int | maxout | |||
) |
Definition at line 180 of file term.c.
References ast_copy_string(), ast_opt_force_black_background, ast_opt_light_background, ATTR_BRIGHT, COLOR_BLACK, COLOR_WHITE, ESC, and opposite().
Referenced by __ast_custom_function_register(), __ast_register_translator(), ast_frame_dump(), ast_register_application2(), ast_unregister_translator(), fix_header(), handle_cli_agi_show(), handle_dahdi_show_cadences(), handle_show_function(), handle_showmancmd(), logger_print_normal(), lua_pbx_exec(), main(), pbx_extension_helper(), print_app_docs(), show_config_description(), and start_resource().
00181 { 00182 int attr = 0; 00183 00184 if (!vt100compat) { 00185 ast_copy_string(outbuf, inbuf, maxout); 00186 return outbuf; 00187 } 00188 if (!fgcolor) { 00189 ast_copy_string(outbuf, inbuf, maxout); 00190 return outbuf; 00191 } 00192 00193 if (fgcolor & 128) { 00194 attr = ast_opt_light_background ? 0 : ATTR_BRIGHT; 00195 fgcolor &= ~128; 00196 } 00197 00198 if (bgcolor) { 00199 bgcolor &= ~128; 00200 } 00201 00202 if (ast_opt_light_background) { 00203 fgcolor = opposite(fgcolor); 00204 } 00205 00206 if (ast_opt_force_black_background) { 00207 snprintf(outbuf, maxout, "%c[%d;%d;%dm%s%c[%d;%dm", ESC, attr, fgcolor, bgcolor + 10, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10); 00208 } else { 00209 snprintf(outbuf, maxout, "%c[%d;%dm%s%c[0m", ESC, attr, fgcolor, inbuf, ESC); 00210 } 00211 return outbuf; 00212 }
char* term_color_code | ( | char * | outbuf, | |
int | fgcolor, | |||
int | bgcolor, | |||
int | maxout | |||
) |
Write a color sequence to a string.
outbuf | the location to write to | |
fgcolor | foreground color | |
bgcolor | background color | |
maxout | maximum number of characters to write |
Definition at line 260 of file term.c.
References ast_opt_force_black_background, check_bgcolor(), check_colors_allowed(), check_fgcolor(), COLOR_BLACK, and ESC.
Referenced by cli_prompt().
00261 { 00262 int attr = 0; 00263 00264 if (!check_colors_allowed(fgcolor)) { 00265 *outbuf = '\0'; 00266 return outbuf; 00267 } 00268 00269 check_fgcolor(&fgcolor, &attr); 00270 check_bgcolor(&bgcolor); 00271 00272 if (ast_opt_force_black_background) { 00273 snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10); 00274 } else if (bgcolor) { 00275 snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10); 00276 } else { 00277 snprintf(outbuf, maxout, "%c[%d;%dm", ESC, attr, fgcolor); 00278 } 00279 00280 return outbuf; 00281 }
char* term_end | ( | void | ) |
Definition at line 361 of file term.c.
Referenced by ast_xmldoc_printable(), consolehandler(), and main().
00362 { 00363 return enddata; 00364 }
void term_filter_escapes | ( | char * | line | ) |
Definition at line 334 of file term.c.
Referenced by ast_log().
00335 { 00336 int i; 00337 int len = strlen(line); 00338 00339 for (i = 0; i < len; i++) { 00340 if (line[i] != ESC) 00341 continue; 00342 if ((i < (len - 2)) && 00343 (line[i + 1] == 0x5B)) { 00344 switch (line[i + 2]) { 00345 case 0x30: 00346 case 0x31: 00347 case 0x33: 00348 continue; 00349 } 00350 } 00351 /* replace ESC with a space */ 00352 line[i] = ' '; 00353 } 00354 }
char* term_prep | ( | void | ) |
char* term_prompt | ( | char * | outbuf, | |
const char * | inbuf, | |||
int | maxout | |||
) |
Definition at line 305 of file term.c.
References ast_copy_string(), ast_opt_force_black_background, ast_opt_light_background, ATTR_BRIGHT, COLOR_BLACK, COLOR_BLUE, COLOR_WHITE, and ESC.
00306 { 00307 if (!vt100compat) { 00308 ast_copy_string(outbuf, inbuf, maxout); 00309 return outbuf; 00310 } 00311 if (ast_opt_force_black_background) { 00312 snprintf(outbuf, maxout, "%c[%d;%dm%c%c[%d;%dm%s", 00313 ESC, COLOR_BLUE, COLOR_BLACK + 10, 00314 inbuf[0], 00315 ESC, COLOR_WHITE, COLOR_BLACK + 10, 00316 inbuf + 1); 00317 } else if (ast_opt_light_background) { 00318 snprintf(outbuf, maxout, "%c[%d;0m%c%c[%d;0m%s", 00319 ESC, COLOR_BLUE, 00320 inbuf[0], 00321 ESC, COLOR_BLACK, 00322 inbuf + 1); 00323 } else { 00324 snprintf(outbuf, maxout, "%c[%d;%d;0m%c%c[%d;%d;0m%s", 00325 ESC, ATTR_BRIGHT, COLOR_BLUE, 00326 inbuf[0], 00327 ESC, 0, COLOR_WHITE, 00328 inbuf + 1); 00329 } 00330 return outbuf; 00331 }
char* term_quit | ( | void | ) |
Definition at line 366 of file term.c.
Referenced by ast_el_read_char(), main(), and really_quit().
00367 { 00368 return quitdata; 00369 }
char* term_strip | ( | char * | outbuf, | |
const char * | inbuf, | |||
int | maxout | |||
) |
Definition at line 283 of file term.c.
References ESC.
Referenced by action_command(), ast_log_vsyslog(), and logger_print_normal().
00284 { 00285 char *outbuf_ptr = outbuf; 00286 const char *inbuf_ptr = inbuf; 00287 00288 while (outbuf_ptr < outbuf + maxout) { 00289 switch (*inbuf_ptr) { 00290 case ESC: 00291 while (*inbuf_ptr && (*inbuf_ptr != 'm')) 00292 inbuf_ptr++; 00293 break; 00294 default: 00295 *outbuf_ptr = *inbuf_ptr; 00296 outbuf_ptr++; 00297 } 00298 if (! *inbuf_ptr) 00299 break; 00300 inbuf_ptr++; 00301 } 00302 return outbuf; 00303 }
int vt100compat [static] |