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_color_code(char *outbuf, int fgcolor, int bgcolor, int maxout) 00100 { 00101 int attr=0; 00102 char tmp[40]; 00103 if ((!vt100compat) || (!fgcolor && !bgcolor)) { 00104 *outbuf = '\0'; 00105 return outbuf; 00106 } 00107 if ((fgcolor & 128) && (bgcolor & 128)) { 00108 /* Can't both be highlighted */ 00109 *outbuf = '\0'; 00110 return outbuf; 00111 } 00112 if (!bgcolor) 00113 bgcolor = COLOR_BLACK; 00114 00115 if (bgcolor) { 00116 bgcolor &= ~128; 00117 bgcolor += 10; 00118 } 00119 if (fgcolor & 128) { 00120 attr = ATTR_BRIGHT; 00121 fgcolor &= ~128; 00122 } 00123 if (fgcolor && bgcolor) { 00124 snprintf(tmp, sizeof(tmp), "%d;%d", fgcolor, bgcolor); 00125 } else if (bgcolor) { 00126 snprintf(tmp, sizeof(tmp), "%d", bgcolor); 00127 } else if (fgcolor) { 00128 snprintf(tmp, sizeof(tmp), "%d", fgcolor); 00129 } 00130 if (attr) { 00131 snprintf(outbuf, maxout, "%c[%d;%sm", ESC, attr, tmp); 00132 } else { 00133 snprintf(outbuf, maxout, "%c[%sm", ESC, tmp); 00134 } 00135 return outbuf; 00136 } 00137 00138 char *term_strip(char *outbuf, char *inbuf, int maxout) 00139 { 00140 char *outbuf_ptr = outbuf, *inbuf_ptr = inbuf; 00141 00142 while (outbuf_ptr < outbuf + maxout) { 00143 switch (*inbuf_ptr) { 00144 case ESC: 00145 while (*inbuf_ptr && (*inbuf_ptr != 'm')) 00146 inbuf_ptr++; 00147 break; 00148 default: 00149 *outbuf_ptr = *inbuf_ptr; 00150 outbuf_ptr++; 00151 } 00152 if (! *inbuf_ptr) 00153 break; 00154 inbuf_ptr++; 00155 } 00156 return outbuf; 00157 } 00158 00159 char *term_prompt(char *outbuf, const char *inbuf, int maxout) 00160 { 00161 if (!vt100compat) { 00162 strncpy(outbuf, inbuf, maxout -1); 00163 return outbuf; 00164 } 00165 snprintf(outbuf, maxout, "%c[%d;%d;%dm%c%c[%d;%d;%dm%s", 00166 ESC, ATTR_BRIGHT, COLOR_BLUE, COLOR_BLACK + 10, 00167 inbuf[0], 00168 ESC, 0, COLOR_WHITE, COLOR_BLACK + 10, 00169 inbuf + 1); 00170 return outbuf; 00171 } 00172 00173 char *term_prep(void) 00174 { 00175 return prepdata; 00176 } 00177 00178 char *term_end(void) 00179 { 00180 return enddata; 00181 } 00182 00183 char *term_quit(void) 00184 { 00185 return quitdata; 00186 }

Generated on Sat Jun 12 16:40:59 2004 for Asterisk by doxygen 1.3.7