Mon Sep 20 2010 00:23:54

Asterisk developer's documentation


term.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Terminal Routines 
00022  *
00023  * \author Mark Spencer <markster@digium.com> 
00024  */
00025 
00026 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 153365 $")
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       /* 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 }
00066 
00067 /* Ripped off from Ross Ridge, but it's public domain code (libmytinfo) */
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 (!term)
00091       return 0;
00092    if (!ast_opt_console || ast_opt_no_color || !ast_opt_no_fork)
00093       return 0;
00094 
00095    for (i=0 ;; i++) {
00096       if (termpath[i] == NULL) {
00097          break;
00098       }
00099       snprintf(termfile, sizeof(termfile), "%s/%c/%s", termpath[i], *term, term);
00100       termfd = open(termfile, O_RDONLY);
00101       if (termfd > -1) {
00102          break;
00103       }
00104    }
00105    if (termfd > -1) {
00106       int actsize = read(termfd, buffer, sizeof(buffer) - 1);
00107       short sz_names = convshort(buffer + 2);
00108       short sz_bools = convshort(buffer + 4);
00109       short n_nums   = convshort(buffer + 6);
00110 
00111       /* if ((sz_names + sz_bools) & 1)
00112          sz_bools++; */
00113 
00114       if (sz_names + sz_bools + n_nums < actsize) {
00115          /* Offset 13 is defined in /usr/include/term.h, though we do not
00116           * include it here, as it conflicts with include/asterisk/term.h */
00117          short max_colors = convshort(buffer + 12 + sz_names + sz_bools + 13 * 2);
00118          if (max_colors > 0) {
00119             vt100compat = 1;
00120          }
00121          parseokay = 1;
00122       }
00123       close(termfd);
00124    }
00125 
00126    if (!parseokay) {
00127       /* These comparisons should not be substrings nor case-insensitive, as
00128        * terminal types are very particular about how they treat suffixes and
00129        * capitalization.  For example, terminal type 'linux-m' does NOT
00130        * support color, while 'linux' does.  Not even all vt100* terminals
00131        * support color, either (e.g. 'vt100+fnkeys'). */
00132       if (!strcmp(term, "linux")) {
00133          vt100compat = 1;
00134       } else if (!strcmp(term, "xterm")) {
00135          vt100compat = 1;
00136       } else if (!strcmp(term, "xterm-color")) {
00137          vt100compat = 1;
00138       } else if (!strncmp(term, "Eterm", 5)) {
00139          /* Both entries which start with Eterm support color */
00140          vt100compat = 1;
00141       } else if (!strcmp(term, "vt100")) {
00142          vt100compat = 1;
00143       } else if (!strncmp(term, "crt", 3)) {
00144          /* Both crt terminals support color */
00145          vt100compat = 1;
00146       }
00147    }
00148 
00149    if (vt100compat) {
00150       /* Make commands show up in nice colors */
00151       if (ast_opt_light_background) {
00152          snprintf(prepdata, sizeof(prepdata), "%c[%dm", ESC, COLOR_BROWN);
00153          snprintf(enddata, sizeof(enddata), "%c[%dm", ESC, COLOR_BLACK);
00154          snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
00155       } else if (ast_opt_force_black_background) {
00156          snprintf(prepdata, sizeof(prepdata), "%c[%d;%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN, COLOR_BLACK + 10);
00157          snprintf(enddata, sizeof(enddata), "%c[%d;%d;%dm", ESC, ATTR_RESET, COLOR_WHITE, COLOR_BLACK + 10);
00158          snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
00159       } else {
00160          snprintf(prepdata, sizeof(prepdata), "%c[%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN);
00161          snprintf(enddata, sizeof(enddata), "%c[%d;%dm", ESC, ATTR_RESET, COLOR_WHITE);
00162          snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
00163       }
00164    }
00165    return 0;
00166 }
00167 
00168 char *term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout)
00169 {
00170    int attr = 0;
00171 
00172    if (!vt100compat) {
00173       ast_copy_string(outbuf, inbuf, maxout);
00174       return outbuf;
00175    }
00176    if (!fgcolor) {
00177       ast_copy_string(outbuf, inbuf, maxout);
00178       return outbuf;
00179    }
00180 
00181    if (fgcolor & 128) {
00182       attr = ast_opt_light_background ? 0 : ATTR_BRIGHT;
00183       fgcolor &= ~128;
00184    }
00185 
00186    if (bgcolor) {
00187       bgcolor &= ~128;
00188    }
00189 
00190    if (ast_opt_light_background) {
00191       fgcolor = opposite(fgcolor);
00192    }
00193 
00194    if (ast_opt_force_black_background) {
00195       snprintf(outbuf, maxout, "%c[%d;%d;%dm%s%c[%d;%dm", ESC, attr, fgcolor, bgcolor + 10, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10);
00196    } else {
00197       snprintf(outbuf, maxout, "%c[%d;%dm%s%c[0m", ESC, attr, fgcolor, inbuf, ESC);
00198    }
00199    return outbuf;
00200 }
00201 
00202 static void check_fgcolor(int *fgcolor, int *attr)
00203 {
00204    if (*fgcolor & 128) {
00205       *attr = ast_opt_light_background ? 0 : ATTR_BRIGHT;
00206       *fgcolor &= ~128;
00207    }
00208    
00209    if (ast_opt_light_background) {
00210       *fgcolor = opposite(*fgcolor);
00211    }
00212 }
00213 
00214 static void check_bgcolor(int *bgcolor)
00215 {
00216    if (*bgcolor) {
00217       *bgcolor &= ~128;
00218    }
00219 }
00220 
00221 static int check_colors_allowed(int fgcolor)
00222 {
00223    return (!vt100compat || !fgcolor) ? 0 : 1;
00224 }
00225 
00226 int ast_term_color_code(struct ast_str **str, int fgcolor, int bgcolor)
00227 {
00228    int attr = 0;
00229 
00230    if (!check_colors_allowed(fgcolor)) {
00231       return -1;
00232    }
00233 
00234    check_fgcolor(&fgcolor, &attr);
00235    check_bgcolor(&bgcolor);
00236    
00237    if (ast_opt_force_black_background) {
00238       ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10);
00239    } else if (bgcolor) {
00240       ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10);
00241    } else {
00242       ast_str_append(str, 0, "%c[%d;%dm", ESC, attr, fgcolor);
00243    }
00244 
00245    return 0;
00246 }
00247 
00248 char *term_color_code(char *outbuf, int fgcolor, int bgcolor, int maxout)
00249 {
00250    int attr = 0;
00251 
00252    if (!check_colors_allowed(fgcolor)) {
00253       *outbuf = '\0';
00254       return outbuf;
00255    }
00256 
00257    check_fgcolor(&fgcolor, &attr);
00258    check_bgcolor(&bgcolor);
00259 
00260    if (ast_opt_force_black_background) {
00261       snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10);
00262    } else if (bgcolor) {
00263       snprintf(outbuf, maxout, "%c[%d;%d;%dm", ESC, attr, fgcolor, bgcolor + 10);
00264    } else {
00265       snprintf(outbuf, maxout, "%c[%d;%dm", ESC, attr, fgcolor);
00266    }
00267 
00268    return outbuf;
00269 }
00270 
00271 char *term_strip(char *outbuf, char *inbuf, int maxout)
00272 {
00273    char *outbuf_ptr = outbuf, *inbuf_ptr = inbuf;
00274 
00275    while (outbuf_ptr < outbuf + maxout) {
00276       switch (*inbuf_ptr) {
00277          case ESC:
00278             while (*inbuf_ptr && (*inbuf_ptr != 'm'))
00279                inbuf_ptr++;
00280             break;
00281          default:
00282             *outbuf_ptr = *inbuf_ptr;
00283             outbuf_ptr++;
00284       }
00285       if (! *inbuf_ptr)
00286          break;
00287       inbuf_ptr++;
00288    }
00289    return outbuf;
00290 }
00291 
00292 char *term_prompt(char *outbuf, const char *inbuf, int maxout)
00293 {
00294    if (!vt100compat) {
00295       ast_copy_string(outbuf, inbuf, maxout);
00296       return outbuf;
00297    }
00298    if (ast_opt_force_black_background) {
00299       snprintf(outbuf, maxout, "%c[%d;%dm%c%c[%d;%dm%s",
00300          ESC, COLOR_BLUE, COLOR_BLACK + 10,
00301          inbuf[0],
00302          ESC, COLOR_WHITE, COLOR_BLACK + 10,
00303          inbuf + 1);
00304    } else if (ast_opt_light_background) {
00305       snprintf(outbuf, maxout, "%c[%d;0m%c%c[%d;0m%s",
00306          ESC, COLOR_BLUE,
00307          inbuf[0],
00308          ESC, COLOR_BLACK,
00309          inbuf + 1);
00310    } else {
00311       snprintf(outbuf, maxout, "%c[%d;%d;0m%c%c[%d;%d;0m%s",
00312          ESC, ATTR_BRIGHT, COLOR_BLUE,
00313          inbuf[0],
00314          ESC, 0, COLOR_WHITE,
00315          inbuf + 1);
00316    }
00317    return outbuf;
00318 }
00319 
00320 /* filter escape sequences */
00321 void term_filter_escapes(char *line)
00322 {
00323    int i;
00324    int len = strlen(line);
00325 
00326    for (i = 0; i < len; i++) {
00327       if (line[i] != ESC)
00328          continue;
00329       if ((i < (len - 2)) &&
00330           (line[i + 1] == 0x5B)) {
00331          switch (line[i + 2]) {
00332          case 0x30:
00333          case 0x31:
00334          case 0x33:
00335             continue;
00336          }
00337       }
00338       /* replace ESC with a space */
00339       line[i] = ' ';
00340    }
00341 }
00342 
00343 char *term_prep(void)
00344 {
00345    return prepdata;
00346 }
00347 
00348 char *term_end(void)
00349 {
00350    return enddata;
00351 }
00352 
00353 char *term_quit(void)
00354 {
00355    return quitdata;
00356 }