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: 269334 $")
00029
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <sys/time.h>
00034 #include <signal.h>
00035 #include <errno.h>
00036 #include <sys/types.h>
00037 #include <sys/stat.h>
00038 #include <fcntl.h>
00039 #include <unistd.h>
00040
00041 #include "asterisk/term.h"
00042 #include "asterisk/options.h"
00043 #include "asterisk/lock.h"
00044 #include "asterisk/utils.h"
00045
00046 static int vt100compat;
00047
00048 static char prepdata[80] = "";
00049 static char enddata[80] = "";
00050 static char quitdata[80] = "";
00051
00052 static const char *termpath[] = {
00053 "/usr/share/terminfo",
00054 "/usr/local/share/misc/terminfo",
00055 "/usr/lib/terminfo",
00056 NULL
00057 };
00058
00059
00060 static short convshort(char *s)
00061 {
00062 register int a,b;
00063
00064 a = (int) s[0] & 0377;
00065 b = (int) s[1] & 0377;
00066
00067 if (a == 0377 && b == 0377)
00068 return -1;
00069 if (a == 0376 && b == 0377)
00070 return -2;
00071
00072 return a + b * 256;
00073 }
00074
00075 int ast_term_init(void)
00076 {
00077 char *term = getenv("TERM");
00078 char termfile[256] = "";
00079 char buffer[512] = "";
00080 int termfd = -1, parseokay = 0, i;
00081
00082 if (ast_opt_no_color) {
00083 return 0;
00084 }
00085
00086 if (!ast_opt_console) {
00087
00088 vt100compat = 1;
00089 goto end;
00090 }
00091
00092 if (!term) {
00093 return 0;
00094 }
00095
00096 for (i = 0;; i++) {
00097 if (termpath[i] == NULL) {
00098 break;
00099 }
00100 snprintf(termfile, sizeof(termfile), "%s/%c/%s", termpath[i], *term, term);
00101 termfd = open(termfile, O_RDONLY);
00102 if (termfd > -1) {
00103 break;
00104 }
00105 }
00106 if (termfd > -1) {
00107 int actsize = read(termfd, buffer, sizeof(buffer) - 1);
00108 short sz_names = convshort(buffer + 2);
00109 short sz_bools = convshort(buffer + 4);
00110 short n_nums = convshort(buffer + 6);
00111
00112
00113
00114
00115 if (sz_names + sz_bools + n_nums < actsize) {
00116
00117
00118 short max_colors = convshort(buffer + 12 + sz_names + sz_bools + 13 * 2);
00119 if (max_colors > 0) {
00120 vt100compat = 1;
00121 }
00122 parseokay = 1;
00123 }
00124 close(termfd);
00125 }
00126
00127 if (!parseokay) {
00128
00129
00130
00131
00132
00133 if (!strcmp(term, "linux")) {
00134 vt100compat = 1;
00135 } else if (!strcmp(term, "xterm")) {
00136 vt100compat = 1;
00137 } else if (!strcmp(term, "xterm-color")) {
00138 vt100compat = 1;
00139 } else if (!strncmp(term, "Eterm", 5)) {
00140
00141 vt100compat = 1;
00142 } else if (!strcmp(term, "vt100")) {
00143 vt100compat = 1;
00144 } else if (!strncmp(term, "crt", 3)) {
00145
00146 vt100compat = 1;
00147 }
00148 }
00149
00150 end:
00151 if (vt100compat) {
00152
00153 snprintf(prepdata, sizeof(prepdata), "%c[%d;%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN, COLOR_BLACK + 10);
00154 snprintf(enddata, sizeof(enddata), "%c[%d;%d;%dm", ESC, ATTR_RESET, COLOR_WHITE, COLOR_BLACK + 10);
00155 snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
00156 }
00157 return 0;
00158 }
00159
00160 char *term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout)
00161 {
00162 int attr=0;
00163 char tmp[40];
00164 if (!vt100compat) {
00165 ast_copy_string(outbuf, inbuf, maxout);
00166 return outbuf;
00167 }
00168 if (!fgcolor && !bgcolor) {
00169 ast_copy_string(outbuf, inbuf, maxout);
00170 return outbuf;
00171 }
00172 if ((fgcolor & 128) && (bgcolor & 128)) {
00173
00174 ast_copy_string(outbuf, inbuf, maxout);
00175 return outbuf;
00176 }
00177 if (!bgcolor)
00178 bgcolor = COLOR_BLACK;
00179
00180 if (bgcolor) {
00181 bgcolor &= ~128;
00182 bgcolor += 10;
00183 }
00184 if (fgcolor & 128) {
00185 attr = ATTR_BRIGHT;
00186 fgcolor &= ~128;
00187 }
00188 if (fgcolor && bgcolor) {
00189 snprintf(tmp, sizeof(tmp), "%d;%d", fgcolor, bgcolor);
00190 } else if (bgcolor) {
00191 snprintf(tmp, sizeof(tmp), "%d", bgcolor);
00192 } else if (fgcolor) {
00193 snprintf(tmp, sizeof(tmp), "%d", fgcolor);
00194 }
00195 if (attr) {
00196 snprintf(outbuf, maxout, "%c[%d;%sm%s%c[0;%d;%dm", ESC, attr, tmp, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10);
00197 } else {
00198 snprintf(outbuf, maxout, "%c[%sm%s%c[0;%d;%dm", ESC, tmp, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10);
00199 }
00200 return outbuf;
00201 }
00202
00203 char *term_color_code(char *outbuf, int fgcolor, int bgcolor, int maxout)
00204 {
00205 int attr=0;
00206 char tmp[40];
00207 if ((!vt100compat) || (!fgcolor && !bgcolor)) {
00208 *outbuf = '\0';
00209 return outbuf;
00210 }
00211 if ((fgcolor & 128) && (bgcolor & 128)) {
00212
00213 *outbuf = '\0';
00214 return outbuf;
00215 }
00216 if (!bgcolor)
00217 bgcolor = COLOR_BLACK;
00218
00219 if (bgcolor) {
00220 bgcolor &= ~128;
00221 bgcolor += 10;
00222 }
00223 if (fgcolor & 128) {
00224 attr = ATTR_BRIGHT;
00225 fgcolor &= ~128;
00226 }
00227 if (fgcolor && bgcolor) {
00228 snprintf(tmp, sizeof(tmp), "%d;%d", fgcolor, bgcolor);
00229 } else if (bgcolor) {
00230 snprintf(tmp, sizeof(tmp), "%d", bgcolor);
00231 } else if (fgcolor) {
00232 snprintf(tmp, sizeof(tmp), "%d", fgcolor);
00233 }
00234 if (attr) {
00235 snprintf(outbuf, maxout, "%c[%d;%sm", ESC, attr, tmp);
00236 } else {
00237 snprintf(outbuf, maxout, "%c[%sm", ESC, tmp);
00238 }
00239 return outbuf;
00240 }
00241
00242 char *term_strip(char *outbuf, char *inbuf, int maxout)
00243 {
00244 char *outbuf_ptr = outbuf, *inbuf_ptr = inbuf;
00245
00246 while (outbuf_ptr < outbuf + maxout) {
00247 switch (*inbuf_ptr) {
00248 case ESC:
00249 while (*inbuf_ptr && (*inbuf_ptr != 'm'))
00250 inbuf_ptr++;
00251 break;
00252 default:
00253 *outbuf_ptr = *inbuf_ptr;
00254 outbuf_ptr++;
00255 }
00256 if (! *inbuf_ptr)
00257 break;
00258 inbuf_ptr++;
00259 }
00260 return outbuf;
00261 }
00262
00263 char *term_prompt(char *outbuf, const char *inbuf, int maxout)
00264 {
00265 if (!vt100compat) {
00266 ast_copy_string(outbuf, inbuf, maxout);
00267 return outbuf;
00268 }
00269 snprintf(outbuf, maxout, "%c[%d;%d;%dm%c%c[%d;%d;%dm%s",
00270 ESC, ATTR_BRIGHT, COLOR_BLUE, COLOR_BLACK + 10,
00271 inbuf[0],
00272 ESC, 0, COLOR_WHITE, COLOR_BLACK + 10,
00273 inbuf + 1);
00274 return outbuf;
00275 }
00276
00277
00278 void term_filter_escapes(char *line)
00279 {
00280 int i;
00281 int len = strlen(line);
00282
00283 for (i = 0; i < len; i++) {
00284 if (line[i] != ESC)
00285 continue;
00286 if ((i < (len - 2)) &&
00287 (line[i + 1] == 0x5B)) {
00288 switch (line[i + 2]) {
00289 case 0x30:
00290 case 0x31:
00291 case 0x33:
00292 continue;
00293 }
00294 }
00295
00296 line[i] = ' ';
00297 }
00298 }
00299
00300 char *term_prep(void)
00301 {
00302 return prepdata;
00303 }
00304
00305 char *term_end(void)
00306 {
00307 return enddata;
00308 }
00309
00310 char *term_quit(void)
00311 {
00312 return quitdata;
00313 }