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