Main Page | Alphabetical List | Data Structures | 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 <pthread.h>
00017 #include <string.h>
00018 #include <sys/time.h>
00019 #include <signal.h>
00020 #include <errno.h>
00021 #include <unistd.h>
00022 #include <asterisk/term.h>
00023 #include <asterisk/options.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, "vt", 2))
00044       vt100compat = 1;
00045    if (vt100compat) {
00046       /* Make commands show up in nice colors */
00047       snprintf(prepdata, sizeof(prepdata), "%c[%d;%d;%dm", ESC, ATTR_BRIGHT, COLOR_BROWN, COLOR_BLACK + 10);
00048       snprintf(enddata, sizeof(enddata), "%c[%d;%d;%dm", ESC, ATTR_RESET, COLOR_WHITE, COLOR_BLACK + 10);
00049       snprintf(quitdata, sizeof(quitdata), "%c[0m", ESC);
00050    }
00051    return 0;
00052 }
00053 
00054 char *term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout)
00055 {
00056    int attr=0;
00057    char tmp[40];
00058    if (!vt100compat) {
00059       strncpy(outbuf, inbuf, maxout -1);
00060       return outbuf;
00061    }
00062    if (!fgcolor && !bgcolor) {
00063       strncpy(outbuf, inbuf, maxout - 1);
00064       return outbuf;
00065    }
00066    if ((fgcolor & 128) && (bgcolor & 128)) {
00067       /* Can't both be highlighted */
00068       strncpy(outbuf, inbuf, maxout - 1);
00069       return outbuf;
00070    }
00071    if (!bgcolor)
00072       bgcolor = COLOR_BLACK;
00073 
00074    if (bgcolor) {
00075       bgcolor &= ~128;
00076       bgcolor += 10;
00077    }
00078    if (fgcolor & 128) {
00079       attr = ATTR_BRIGHT;
00080       fgcolor &= ~128;
00081    }
00082    if (fgcolor && bgcolor) {
00083       snprintf(tmp, sizeof(tmp), "%d;%d", fgcolor, bgcolor);
00084    } else if (bgcolor) {
00085       snprintf(tmp, sizeof(tmp), "%d", bgcolor);
00086    } else if (fgcolor) {
00087       snprintf(tmp, sizeof(tmp), "%d", fgcolor);
00088    }
00089    if (attr) {
00090       snprintf(outbuf, maxout, "%c[%d;%sm%s%c[0;%d;%dm", ESC, attr, tmp, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10);
00091    } else {
00092       snprintf(outbuf, maxout, "%c[%sm%s%c[0;%d;%dm", ESC, tmp, inbuf, ESC, COLOR_WHITE, COLOR_BLACK + 10);
00093    }
00094    return outbuf;
00095 }
00096 
00097 char *term_prompt(char *outbuf, const char *inbuf, int maxout)
00098 {
00099    if (!vt100compat) {
00100       strncpy(outbuf, inbuf, maxout -1);
00101       return outbuf;
00102    }
00103    snprintf(outbuf, maxout, "%c[%d;%d;%dm%c%c[%d;%d;%dm%s",
00104       ESC, ATTR_BRIGHT, COLOR_BLUE, COLOR_BLACK + 10,
00105       inbuf[0],
00106       ESC, 0, COLOR_WHITE, COLOR_BLACK + 10,
00107       inbuf + 1);
00108    return outbuf;
00109 }
00110 
00111 char *term_prep(void)
00112 {
00113    return prepdata;
00114 }
00115 
00116 char *term_end(void)
00117 {
00118    return enddata;
00119 }
00120 
00121 char *term_quit(void)
00122 {
00123    return quitdata;
00124 }

Generated on Fri Oct 31 07:05:08 2003 for Asterisk by doxygen 1.3.4