summaryrefslogtreecommitdiff
path: root/main/term.c
diff options
context:
space:
mode:
authorKinsey Moore <kmoore@digium.com>2013-02-14 18:47:56 +0000
committerKinsey Moore <kmoore@digium.com>2013-02-14 18:47:56 +0000
commit2e1e0735fe84ddb5536687f2bf5948ff94640423 (patch)
treecbfaeb33a9451bb9af5721f7e098786cc7f0cc17 /main/term.c
parent86a537c2712f99842d1076dc093ce654a76ed55c (diff)
Revamp of terminal color codes
The core module related to coloring terminal output was old and needed some love. The main thing here was an attempt to get rid of the obscene number of stack-local buffers that were allocated for no other reason than to colorize some output. Instead, this uses a simple trick to allocate several buffers within threadlocal storage, then automatically rotates between them, so that you can make multiple calls to the colorization routine within one function and not need to allocate multiple buffers. Review: https://reviewboard.asterisk.org/r/2241/ Patches: bug.patch uploaded by Tilghman Lesher git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@381448 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'main/term.c')
-rw-r--r--main/term.c58
1 files changed, 46 insertions, 12 deletions
diff --git a/main/term.c b/main/term.c
index 132b7fa0c..2e28032a7 100644
--- a/main/term.c
+++ b/main/term.c
@@ -40,6 +40,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/term.h"
#include "asterisk/lock.h"
#include "asterisk/utils.h"
+#include "asterisk/threadstorage.h"
static int vt100compat;
@@ -54,6 +55,13 @@ static const char * const termpath[] = {
NULL
};
+AST_THREADSTORAGE(commonbuf);
+
+struct commonbuf {
+ short which;
+ char buffer[AST_TERM_MAX_ROTATING_BUFFERS][AST_TERM_MAX_ESCAPE_CHARS];
+};
+
static int opposite(int color)
{
int lookup[] = {
@@ -217,11 +225,11 @@ char *term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int
static void check_fgcolor(int *fgcolor, int *attr)
{
+ *attr = ast_opt_light_background ? 0 : ATTR_BRIGHT;
if (*fgcolor & 128) {
- *attr = ast_opt_light_background ? 0 : ATTR_BRIGHT;
*fgcolor &= ~128;
}
-
+
if (ast_opt_light_background) {
*fgcolor = opposite(*fgcolor);
}
@@ -249,7 +257,7 @@ int ast_term_color_code(struct ast_str **str, int fgcolor, int bgcolor)
check_fgcolor(&fgcolor, &attr);
check_bgcolor(&bgcolor);
-
+
if (ast_opt_force_black_background) {
ast_str_append(str, 0, "%c[%d;%d;%dm", ESC, attr, fgcolor, COLOR_BLACK + 10);
} else if (bgcolor) {
@@ -284,6 +292,32 @@ char *term_color_code(char *outbuf, int fgcolor, int bgcolor, int maxout)
return outbuf;
}
+const char *ast_term_color(int fgcolor, int bgcolor)
+{
+ struct commonbuf *cb = ast_threadstorage_get(&commonbuf, sizeof(*cb));
+ char *buf;
+
+ if (!cb) {
+ return "";
+ }
+ buf = cb->buffer[cb->which++];
+ if (cb->which == AST_TERM_MAX_ROTATING_BUFFERS) {
+ cb->which = 0;
+ }
+
+ return term_color_code(buf, fgcolor, bgcolor, AST_TERM_MAX_ESCAPE_CHARS);
+}
+
+const char *ast_term_reset(void)
+{
+ if (ast_opt_force_black_background) {
+ static const char reset[] = { ESC, '[', COLOR_BLACK + 10, 'm', 0 };
+ return reset;
+ } else {
+ return quitdata;
+ }
+}
+
char *term_strip(char *outbuf, const char *inbuf, int maxout)
{
char *outbuf_ptr = outbuf;
@@ -313,22 +347,22 @@ char *term_prompt(char *outbuf, const char *inbuf, int maxout)
return outbuf;
}
if (ast_opt_force_black_background) {
- snprintf(outbuf, maxout, "%c[%d;%dm%c%c[%d;%dm%s",
- ESC, COLOR_BLUE, COLOR_BLACK + 10,
+ snprintf(outbuf, maxout, "%c[%d;%d;%dm%c%c[%d;%dm%s",
+ ESC, ATTR_BRIGHT, COLOR_BLUE, COLOR_BLACK + 10,
inbuf[0],
ESC, COLOR_WHITE, COLOR_BLACK + 10,
inbuf + 1);
} else if (ast_opt_light_background) {
- snprintf(outbuf, maxout, "%c[%d;0m%c%c[%d;0m%s",
+ snprintf(outbuf, maxout, "%c[%d;0m%c%c[0m%s",
ESC, COLOR_BLUE,
inbuf[0],
- ESC, COLOR_BLACK,
+ ESC,
inbuf + 1);
} else {
- snprintf(outbuf, maxout, "%c[%d;%d;0m%c%c[%d;%d;0m%s",
+ snprintf(outbuf, maxout, "%c[%d;%d;0m%c%c[0m%s",
ESC, ATTR_BRIGHT, COLOR_BLUE,
inbuf[0],
- ESC, 0, COLOR_WHITE,
+ ESC,
inbuf + 1);
}
return outbuf;
@@ -357,17 +391,17 @@ void term_filter_escapes(char *line)
}
}
-char *term_prep(void)
+const char *term_prep(void)
{
return prepdata;
}
-char *term_end(void)
+const char *term_end(void)
{
return enddata;
}
-char *term_quit(void)
+const char *term_quit(void)
{
return quitdata;
}