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: 269347 $")
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 *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 (!strncmp(term, "Eterm", 5)) {
00148
00149 vt100compat = 1;
00150 } else if (!strcmp(term, "vt100")) {
00151 vt100compat = 1;
00152 } else if (!strncmp(term, "crt", 3)) {
00153
00154 vt100compat = 1;
00155 }
00156 }
00157
00158 end:
00159 if (vt100compat) {
00160
00161 if (ast_opt_light_background) {
00162 snprintf(prepdata, sizeof(prepdata), "%c[%dm", ESC, COLOR_BROWN);
00163 snprintf(enddata, sizeof(enddata), "%c[%dm", ESC, COLOR_BLACK);
00164 snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
00165 } else if (ast_opt_force_black_background) {
00166 snprintf(prepdata, sizeof(prepdata), "%c[%d;%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN, COLOR_BLACK + 10);
00167 snprintf(enddata, sizeof(enddata), "%c[%d;%d;%dm", ESC, ATTR_RESET, COLOR_WHITE, COLOR_BLACK + 10);
00168 snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
00169 } else {
00170 snprintf(prepdata, sizeof(prepdata), "%c[%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN);
00171 snprintf(enddata, sizeof(enddata), "%c[%d;%dm", ESC, ATTR_RESET, COLOR_WHITE);
00172 snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
00173 }
00174 }
00175 return 0;
00176 }
00177
00178 char *term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout)
00179 {
00180 int attr = 0;
00181
00182 if (!vt100compat) {
00183 ast_copy_string(outbuf, inbuf, maxout);
00184 return outbuf;
00185 }
00186 if (!fgcolor) {
00187 ast_copy_string(outbuf, inbuf, maxout);
00188 return outbuf;
00189 }
00190
00191 if (fgcolor & 128) {
00192 attr = ast_opt_light_background ? 0 : ATTR_BRIGHT;
00193 fgcolor &= ~128;
00194 }
00195
00196 if (bgcolor) {
00197 bgcolor &= ~128;
00198 }
00199
00200 if (ast_opt_light_background) {
00201 fgcolor = opposite(fgcolor);
00202 }
00203
00204 if (ast_opt_force_black_background) {
00205 snprintf(outbuf, maxout, "%c[%d;%d;%dm%s%c[%d;%dm", ESC, attr, fgcolor, bgcolor + 10, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10);
00206 } else {
00207 snprintf(outbuf, maxout, "%c[%d;%dm%s%c[0m", ESC, attr, fgcolor, inbuf, ESC);
00208 }
00209 return outbuf;
00210 }
00211
00212 static void check_fgcolor(int *fgcolor, int *attr)
00213 {
00214 if (*fgcolor & 128) {
00215 *attr = ast_opt_light_background ? 0 : ATTR_BRIGHT;
00216 *fgcolor &= ~128;
00217 }
00218
00219 if (ast_opt_light_background) {
00220 *fgcolor = opposite(*fgcolor);
00221 }
00222 }
00223
00224 static void check_bgcolor(int *bgcolor)
00225 {
00226 if (*bgcolor) {
00227 *bgcolor &= ~128;
00228 }
00229 }
00230
00231 static int check_colors_allowed(int fgcolor)
00232 {
00233 return (!vt100compat || !fgcolor) ? 0 : 1;
00234 }
00235
00236 int ast_term_color_code(struct ast_str **str, int fgcolor, int bgcolor)
00237 {
00238 int attr = 0;
00239
00240 if (!check_colors_allowed(fgcolor)) {
00241 return -1;
00242 }
00243
00244 check_fgcolor(&fgcolor, &attr);
00245 check_bgcolor(&bgcolor);
00246
00247 if (ast_opt_force_black_background) {
00248 ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10);
00249 } else if (bgcolor) {
00250 ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10);
00251 } else {
00252 ast_str_append(str, 0, "%c[%d;%dm", ESC, attr, fgcolor);
00253 }
00254
00255 return 0;
00256 }
00257
00258 char *term_color_code(char *outbuf, int fgcolor, int bgcolor, int maxout)
00259 {
00260 int attr = 0;
00261
00262 if (!check_colors_allowed(fgcolor)) {
00263 *outbuf = '\0';
00264 return outbuf;
00265 }
00266
00267 check_fgcolor(&fgcolor, &attr);
00268 check_bgcolor(&bgcolor);
00269
00270 if (ast_opt_force_black_background) {
00271 snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10);
00272 } else if (bgcolor) {
00273 snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10);
00274 } else {
00275 snprintf(outbuf, maxout, "%c[%d;%dm", ESC, attr, fgcolor);
00276 }
00277
00278 return outbuf;
00279 }
00280
00281 char *term_strip(char *outbuf, char *inbuf, int maxout)
00282 {
00283 char *outbuf_ptr = outbuf, *inbuf_ptr = inbuf;
00284
00285 while (outbuf_ptr < outbuf + maxout) {
00286 switch (*inbuf_ptr) {
00287 case ESC:
00288 while (*inbuf_ptr && (*inbuf_ptr != 'm'))
00289 inbuf_ptr++;
00290 break;
00291 default:
00292 *outbuf_ptr = *inbuf_ptr;
00293 outbuf_ptr++;
00294 }
00295 if (! *inbuf_ptr)
00296 break;
00297 inbuf_ptr++;
00298 }
00299 return outbuf;
00300 }
00301
00302 char *term_prompt(char *outbuf, const char *inbuf, int maxout)
00303 {
00304 if (!vt100compat) {
00305 ast_copy_string(outbuf, inbuf, maxout);
00306 return outbuf;
00307 }
00308 if (ast_opt_force_black_background) {
00309 snprintf(outbuf, maxout, "%c[%d;%dm%c%c[%d;%dm%s",
00310 ESC, COLOR_BLUE, COLOR_BLACK + 10,
00311 inbuf[0],
00312 ESC, COLOR_WHITE, COLOR_BLACK + 10,
00313 inbuf + 1);
00314 } else if (ast_opt_light_background) {
00315 snprintf(outbuf, maxout, "%c[%d;0m%c%c[%d;0m%s",
00316 ESC, COLOR_BLUE,
00317 inbuf[0],
00318 ESC, COLOR_BLACK,
00319 inbuf + 1);
00320 } else {
00321 snprintf(outbuf, maxout, "%c[%d;%d;0m%c%c[%d;%d;0m%s",
00322 ESC, ATTR_BRIGHT, COLOR_BLUE,
00323 inbuf[0],
00324 ESC, 0, COLOR_WHITE,
00325 inbuf + 1);
00326 }
00327 return outbuf;
00328 }
00329
00330
00331 void term_filter_escapes(char *line)
00332 {
00333 int i;
00334 int len = strlen(line);
00335
00336 for (i = 0; i < len; i++) {
00337 if (line[i] != ESC)
00338 continue;
00339 if ((i < (len - 2)) &&
00340 (line[i + 1] == 0x5B)) {
00341 switch (line[i + 2]) {
00342 case 0x30:
00343 case 0x31:
00344 case 0x33:
00345 continue;
00346 }
00347 }
00348
00349 line[i] = ' ';
00350 }
00351 }
00352
00353 char *term_prep(void)
00354 {
00355 return prepdata;
00356 }
00357
00358 char *term_end(void)
00359 {
00360 return enddata;
00361 }
00362
00363 char *term_quit(void)
00364 {
00365 return quitdata;
00366 }