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

Generated on Fri Feb 27 12:19:44 2004 for Asterisk by doxygen 1.3.5