00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "asterisk.h"
00027
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 330107 $")
00029
00030 #include "asterisk/_private.h"
00031 #include <sys/time.h>
00032 #include <signal.h>
00033 #include <sys/stat.h>
00034 #include <fcntl.h>
00035
00036 #include "asterisk/term.h"
00037 #include "asterisk/lock.h"
00038 #include "asterisk/utils.h"
00039
00040 static int vt100compat;
00041
00042 static char prepdata[80] = "";
00043 static char enddata[80] = "";
00044 static char quitdata[80] = "";
00045
00046 static const char * const termpath[] = {
00047 "/usr/share/terminfo",
00048 "/usr/local/share/misc/terminfo",
00049 "/usr/lib/terminfo",
00050 NULL
00051 };
00052
00053 static int opposite(int color)
00054 {
00055 int lookup[] = {
00056 COLOR_BLACK,
00057 COLOR_MAGENTA,
00058 COLOR_GREEN,
00059 COLOR_BROWN,
00060 COLOR_CYAN,
00061 COLOR_RED,
00062 COLOR_BLUE,
00063 COLOR_BLACK };
00064 return color ? lookup[color - 30] : 0;
00065 }
00066
00067
00068 static short convshort(char *s)
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 }
00082
00083 int ast_term_init(void)
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
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
00121
00122
00123 if (sz_names + sz_bools + n_nums < actsize) {
00124
00125
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
00137
00138
00139
00140
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
00151 vt100compat = 1;
00152 } else if (!strcmp(term, "vt100")) {
00153 vt100compat = 1;
00154 } else if (!strncmp(term, "crt", 3)) {
00155
00156 vt100compat = 1;
00157 }
00158 }
00159
00160 end:
00161 if (vt100compat) {
00162
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 }
00179
00180 char *term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout)
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 }
00213
00214 static void check_fgcolor(int *fgcolor, int *attr)
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 }
00225
00226 static void check_bgcolor(int *bgcolor)
00227 {
00228 if (*bgcolor) {
00229 *bgcolor &= ~128;
00230 }
00231 }
00232
00233 static int check_colors_allowed(int fgcolor)
00234 {
00235 return (!vt100compat || !fgcolor) ? 0 : 1;
00236 }
00237
00238 int ast_term_color_code(struct ast_str **str, int fgcolor, int bgcolor)
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 }
00259
00260 char *term_color_code(char *outbuf, int fgcolor, int bgcolor, int maxout)
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 }
00282
00283 char *term_strip(char *outbuf, const char *inbuf, int maxout)
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 }
00304
00305 char *term_prompt(char *outbuf, const char *inbuf, int maxout)
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 }
00332
00333
00334 void term_filter_escapes(char *line)
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
00352 line[i] = ' ';
00353 }
00354 }
00355
00356 char *term_prep(void)
00357 {
00358 return prepdata;
00359 }
00360
00361 char *term_end(void)
00362 {
00363 return enddata;
00364 }
00365
00366 char *term_quit(void)
00367 {
00368 return quitdata;
00369 }