Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:56 2009

## Asterisk developer's documentation

- Main Page
- Related Pages
- Modules
- Data Structures
- Files
- Directories

# 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  */
    00024
    00025 #include <stdio.h>
    00026 #include <stdlib.h>
    00027 #include <string.h>
    00028 #include <sys/time.h>
    00029 #include <signal.h>
    00030 #include <errno.h>
    00031 #include <sys/types.h>
    00032 #include <sys/stat.h>
    00033 #include <fcntl.h>
    00034 #include <unistd.h>
    00035
    00036 #include "asterisk.h"
    00037
    00038 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7221 $")
    00039
    00040 #include "asterisk/term.h"
    00041 #include "asterisk/options.h"
    00042 #include "asterisk/lock.h"
    00043 #include "asterisk/utils.h"
    00044
    00045 static int vt100compat = 0;
    00046
    00047 static char prepdata[80] = "";
    00048 static char enddata[80] = "";
    00049 static char quitdata[80] = "";
    00050
    00051 static const char *termpath[] = {
    00052    "/usr/share/terminfo",
    00053    "/usr/local/share/misc/terminfo",
    00054    "/usr/lib/terminfo",
    00055    NULL
    00056    };
    00057
    00058 /* Ripped off from Ross Ridge, but it's public domain code (libmytinfo) */
    00059 static short convshort(char *s)
    00060 {
    00061    register int a,b;
    00062
    00063    a = (int) s[0] & 0377;
    00064    b = (int) s[1] & 0377;
    00065
    00066    if (a == 0377 && b == 0377)
    00067       return -1;
    00068    if (a == 0376 && b == 0377)
    00069       return -2;
    00070
    00071    return a + b * 256;
    00072 }
    00073
    00074 int term_init(void)
    00075 {
    00076    char *term = getenv("TERM");
    00077    char termfile[256] = "";
    00078    char buffer[512] = "";
    00079    int termfd = -1, parseokay = 0, i;
    00080
    00081    if (!term)
    00082       return 0;
    00083    if (!option_console || option_nocolor || !option_nofork)
    00084       return 0;
    00085
    00086    for (i=0 ;; i++) {
    00087       if (termpath[i] == NULL) {
    00088          break;
    00089       }
    00090       snprintf(termfile, sizeof(termfile), "%s/%c/%s", termpath[i], *term, term);
    00091       termfd = open(termfile, O_RDONLY);
    00092       if (termfd > -1) {
    00093          break;
    00094       }
    00095    }
    00096    if (termfd > -1) {
    00097       int actsize = read(termfd, buffer, sizeof(buffer) - 1);
    00098       short sz_names = convshort(buffer + 2);
    00099       short sz_bools = convshort(buffer + 4);
    00100       short n_nums   = convshort(buffer + 6);
    00101
    00102       /* if ((sz_names + sz_bools) & 1)
    00103          sz_bools++; */
    00104
    00105       if (sz_names + sz_bools + n_nums < actsize) {
    00106          /* Offset 13 is defined in /usr/include/term.h, though we do not
    00107           * include it here, as it conflicts with include/asterisk/term.h */
    00108          short max_colors = convshort(buffer + 12 + sz_names + sz_bools + 13 * 2);
    00109          if (max_colors > 0) {
    00110             vt100compat = 1;
    00111          }
    00112          parseokay = 1;
    00113       }
    00114       close(termfd);
    00115    }
    00116
    00117    if (!parseokay) {
    00118       /* These comparisons should not be substrings nor case-insensitive, as
    00119        * terminal types are very particular about how they treat suffixes and
    00120        * capitalization.  For example, terminal type 'linux-m' does NOT
    00121        * support color, while 'linux' does.  Not even all vt100* terminals
    00122        * support color, either (e.g. 'vt100+fnkeys'). */
    00123       if (!strcmp(term, "linux")) {
    00124          vt100compat = 1;
    00125       } else if (!strcmp(term, "xterm")) {
    00126          vt100compat = 1;
    00127       } else if (!strcmp(term, "xterm-color")) {
    00128          vt100compat = 1;
    00129       } else if (!strncmp(term, "Eterm", 5)) {
    00130          /* Both entries which start with Eterm support color */
    00131          vt100compat = 1;
    00132       } else if (!strcmp(term, "vt100")) {
    00133          vt100compat = 1;
    00134       } else if (!strncmp(term, "crt", 3)) {
    00135          /* Both crt terminals support color */
    00136          vt100compat = 1;
    00137       }
    00138    }
    00139
    00140    if (vt100compat) {
    00141       /* Make commands show up in nice colors */
    00142       snprintf(prepdata, sizeof(prepdata), "%c[%d;%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN, COLOR_BLACK + 10);
    00143       snprintf(enddata, sizeof(enddata), "%c[%d;%d;%dm", ESC, ATTR_RESET, COLOR_WHITE, COLOR_BLACK + 10);
    00144       snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
    00145    }
    00146    return 0;
    00147 }
    00148
    00149 char *term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout)
    00150 {
    00151    int attr=0;
    00152    char tmp[40];
    00153    if (!vt100compat) {
    00154       ast_copy_string(outbuf, inbuf, maxout);
    00155       return outbuf;
    00156    }
    00157    if (!fgcolor && !bgcolor) {
    00158       ast_copy_string(outbuf, inbuf, maxout);
    00159       return outbuf;
    00160    }
    00161    if ((fgcolor & 128) && (bgcolor & 128)) {
    00162       /* Can't both be highlighted */
    00163       ast_copy_string(outbuf, inbuf, maxout);
    00164       return outbuf;
    00165    }
    00166    if (!bgcolor)
    00167       bgcolor = COLOR_BLACK;
    00168
    00169    if (bgcolor) {
    00170       bgcolor &= ~128;
    00171       bgcolor += 10;
    00172    }
    00173    if (fgcolor & 128) {
    00174       attr = ATTR_BRIGHT;
    00175       fgcolor &= ~128;
    00176    }
    00177    if (fgcolor && bgcolor) {
    00178       snprintf(tmp, sizeof(tmp), "%d;%d", fgcolor, bgcolor);
    00179    } else if (bgcolor) {
    00180       snprintf(tmp, sizeof(tmp), "%d", bgcolor);
    00181    } else if (fgcolor) {
    00182       snprintf(tmp, sizeof(tmp), "%d", fgcolor);
    00183    }
    00184    if (attr) {
    00185       snprintf(outbuf, maxout, "%c[%d;%sm%s%c[0;%d;%dm", ESC, attr, tmp, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10);
    00186    } else {
    00187       snprintf(outbuf, maxout, "%c[%sm%s%c[0;%d;%dm", ESC, tmp, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10);
    00188    }
    00189    return outbuf;
    00190 }
    00191
    00192 char *term_color_code(char *outbuf, int fgcolor, int bgcolor, int maxout)
    00193 {
    00194    int attr=0;
    00195    char tmp[40];
    00196    if ((!vt100compat) || (!fgcolor && !bgcolor)) {
    00197       *outbuf = '\0';
    00198       return outbuf;
    00199    }
    00200    if ((fgcolor & 128) && (bgcolor & 128)) {
    00201       /* Can't both be highlighted */
    00202       *outbuf = '\0';
    00203       return outbuf;
    00204    }
    00205    if (!bgcolor)
    00206       bgcolor = COLOR_BLACK;
    00207
    00208    if (bgcolor) {
    00209       bgcolor &= ~128;
    00210       bgcolor += 10;
    00211    }
    00212    if (fgcolor & 128) {
    00213       attr = ATTR_BRIGHT;
    00214       fgcolor &= ~128;
    00215    }
    00216    if (fgcolor && bgcolor) {
    00217       snprintf(tmp, sizeof(tmp), "%d;%d", fgcolor, bgcolor);
    00218    } else if (bgcolor) {
    00219       snprintf(tmp, sizeof(tmp), "%d", bgcolor);
    00220    } else if (fgcolor) {
    00221       snprintf(tmp, sizeof(tmp), "%d", fgcolor);
    00222    }
    00223    if (attr) {
    00224       snprintf(outbuf, maxout, "%c[%d;%sm", ESC, attr, tmp);
    00225    } else {
    00226       snprintf(outbuf, maxout, "%c[%sm", ESC, tmp);
    00227    }
    00228    return outbuf;
    00229 }
    00230
    00231 char *term_strip(char *outbuf, char *inbuf, int maxout)
    00232 {
    00233    char *outbuf_ptr = outbuf, *inbuf_ptr = inbuf;
    00234
    00235    while (outbuf_ptr < outbuf + maxout) {
    00236       switch (*inbuf_ptr) {
    00237          case ESC:
    00238             while (*inbuf_ptr && (*inbuf_ptr != 'm'))
    00239                inbuf_ptr++;
    00240             break;
    00241          default:
    00242             *outbuf_ptr = *inbuf_ptr;
    00243             outbuf_ptr++;
    00244       }
    00245       if (! *inbuf_ptr)
    00246          break;
    00247       inbuf_ptr++;
    00248    }
    00249    return outbuf;
    00250 }
    00251
    00252 char *term_prompt(char *outbuf, const char *inbuf, int maxout)
    00253 {
    00254    if (!vt100compat) {
    00255       ast_copy_string(outbuf, inbuf, maxout);
    00256       return outbuf;
    00257    }
    00258    snprintf(outbuf, maxout, "%c[%d;%d;%dm%c%c[%d;%d;%dm%s",
    00259       ESC, ATTR_BRIGHT, COLOR_BLUE, COLOR_BLACK + 10,
    00260       inbuf[0],
    00261       ESC, 0, COLOR_WHITE, COLOR_BLACK + 10,
    00262       inbuf + 1);
    00263    return outbuf;
    00264 }
    00265
    00266 char *term_prep(void)
    00267 {
    00268    return prepdata;
    00269 }
    00270
    00271 char *term_end(void)
    00272 {
    00273    return enddata;
    00274 }
    00275
    00276 char *term_quit(void)
    00277 {
    00278    return quitdata;
    00279 }