Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

term.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- A telephony toolkit for Linux.
00003  *
00004  * Channel Management
00005  * 
00006  * Copyright (C) 1999, Mark Spencer
00007  *
00008  * Mark Spencer <markster@linux-support.net>
00009  *
00010  * This program is free software, distributed under the terms of
00011  * the GNU General Public License
00012  */
00013 
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include <string.h>
00017 #include <sys/time.h>
00018 #include <signal.h>
00019 #include <errno.h>
00020 #include <unistd.h>
00021 #include <asterisk/term.h>
00022 #include <asterisk/options.h>
00023 #include <asterisk/lock.h>
00024 #include "asterisk.h"
00025 
00026 static int vt100compat = 0;
00027 
00028 static char prepdata[80] = "";
00029 static char enddata[80] = "";
00030 static char quitdata[80] = "";
00031 
00032 int term_init(void)
00033 {
00034    char *term = getenv("TERM");
00035    if (!term)
00036       return 0;
00037    if (!option_console || option_nocolor || !option_nofork)
00038       return 0;
00039    if (!strncasecmp(term, "linux", 5)) 
00040       vt100compat = 1; else
00041    if (!strncasecmp(term, "xterm", 5))
00042       vt100compat = 1; else
00043    if (!strncasecmp(term, "Eterm", 5))
00044       vt100compat = 1; else
00045    if (!strncasecmp(term, "crt", 3))
00046       vt100compat = 1; else
00047    if (!strncasecmp(term, "vt", 2))
00048       vt100compat = 1;
00049    if (vt100compat) {
00050       /* Make commands show up in nice colors */
00051       snprintf(prepdata, sizeof(prepdata), "%c[%d;%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN, COLOR_BLACK + 10);
00052       snprintf(enddata, sizeof(enddata), "%c[%d;%d;%dm", ESC, ATTR_RESET, COLOR_WHITE, COLOR_BLACK + 10);
00053       snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
00054    }
00055    return 0;
00056 }
00057 
00058 char *term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout)
00059 {
00060    int attr=0;
00061    char tmp[40];
00062    if (!vt100compat) {
00063       strncpy(outbuf, inbuf, maxout -1);
00064       return outbuf;
00065    }
00066    if (!fgcolor && !bgcolor) {
00067       strncpy(outbuf, inbuf, maxout - 1);
00068       return outbuf;
00069    }
00070    if ((fgcolor & 128) && (bgcolor & 128)) {
00071       /* Can't both be highlighted */
00072       strncpy(outbuf, inbuf, maxout - 1);
00073       return outbuf;
00074    }
00075    if (!bgcolor)
00076       bgcolor = COLOR_BLACK;
00077 
00078    if (bgcolor) {
00079       bgcolor &= ~128;
00080       bgcolor += 10;
00081    }
00082    if (fgcolor & 128) {
00083       attr = ATTR_BRIGHT;
00084       fgcolor &= ~128;
00085    }
00086    if (fgcolor && bgcolor) {
00087       snprintf(tmp, sizeof(tmp), "%d;%d", fgcolor, bgcolor);
00088    } else if (bgcolor) {
00089       snprintf(tmp, sizeof(tmp), "%d", bgcolor);
00090    } else if (fgcolor) {
00091       snprintf(tmp, sizeof(tmp), "%d", fgcolor);
00092    }
00093    if (attr) {
00094       snprintf(outbuf, maxout, "%c[%d;%sm%s%c[0;%d;%dm", ESC, attr, tmp, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10);
00095    } else {
00096       snprintf(outbuf, maxout, "%c[%sm%s%c[0;%d;%dm", ESC, tmp, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10);
00097    }
00098    return outbuf;
00099 }
00100 
00101 char *term_color_code(char *outbuf, int fgcolor, int bgcolor, int maxout)
00102 {
00103    int attr=0;
00104    char tmp[40];
00105    if ((!vt100compat) || (!fgcolor && !bgcolor)) {
00106       *outbuf = '\0';
00107       return outbuf;
00108    }
00109    if ((fgcolor & 128) && (bgcolor & 128)) {
00110       /* Can't both be highlighted */
00111       *outbuf = '\0';
00112       return outbuf;
00113    }
00114    if (!bgcolor)
00115       bgcolor = COLOR_BLACK;
00116 
00117    if (bgcolor) {
00118       bgcolor &= ~128;
00119       bgcolor += 10;
00120    }
00121    if (fgcolor & 128) {
00122       attr = ATTR_BRIGHT;
00123       fgcolor &= ~128;
00124    }
00125    if (fgcolor && bgcolor) {
00126       snprintf(tmp, sizeof(tmp), "%d;%d", fgcolor, bgcolor);
00127    } else if (bgcolor) {
00128       snprintf(tmp, sizeof(tmp), "%d", bgcolor);
00129    } else if (fgcolor) {
00130       snprintf(tmp, sizeof(tmp), "%d", fgcolor);
00131    }
00132    if (attr) {
00133       snprintf(outbuf, maxout, "%c[%d;%sm", ESC, attr, tmp);
00134    } else {
00135       snprintf(outbuf, maxout, "%c[%sm", ESC, tmp);
00136    }
00137    return outbuf;
00138 }
00139 
00140 char *term_strip(char *outbuf, char *inbuf, int maxout)
00141 {
00142    char *outbuf_ptr = outbuf, *inbuf_ptr = inbuf;
00143 
00144    while (outbuf_ptr < outbuf + maxout) {
00145       switch (*inbuf_ptr) {
00146          case ESC:
00147             while (*inbuf_ptr && (*inbuf_ptr != 'm'))
00148                inbuf_ptr++;
00149             break;
00150          default:
00151             *outbuf_ptr = *inbuf_ptr;
00152             outbuf_ptr++;
00153       }
00154       if (! *inbuf_ptr)
00155          break;
00156       inbuf_ptr++;
00157    }
00158    return outbuf;
00159 }
00160 
00161 char *term_prompt(char *outbuf, const char *inbuf, int maxout)
00162 {
00163    if (!vt100compat) {
00164       strncpy(outbuf, inbuf, maxout -1);
00165       return outbuf;
00166    }
00167    snprintf(outbuf, maxout, "%c[%d;%d;%dm%c%c[%d;%d;%dm%s",
00168       ESC, ATTR_BRIGHT, COLOR_BLUE, COLOR_BLACK + 10,
00169       inbuf[0],
00170       ESC, 0, COLOR_WHITE, COLOR_BLACK + 10,
00171       inbuf + 1);
00172    return outbuf;
00173 }
00174 
00175 char *term_prep(void)
00176 {
00177    return prepdata;
00178 }
00179 
00180 char *term_end(void)
00181 {
00182    return enddata;
00183 }
00184 
00185 char *term_quit(void)
00186 {
00187    return quitdata;
00188 }

Generated on Thu Nov 29 22:50:24 2007 for Asterisk by  doxygen 1.4.2