From e05b6242a5efafa660338e4cb1ddef35d5d1590f Mon Sep 17 00:00:00 2001 From: Mark Spencer <markster@digium.com> Date: Sun, 2 May 2004 19:13:16 +0000 Subject: [PATCH] Merge Tilghman's color patches for the asterisk prompt (bug #1535) git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@2862 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- asterisk.c | 53 +++++++++++++++++++++++++++++++++--- doc/README.cliprompt | 5 +++- include/asterisk/term.h | 4 +++ term.c | 60 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 5 deletions(-) diff --git a/asterisk.c b/asterisk.c index e21f149dc5..f7abea0700 100755 --- a/asterisk.c +++ b/asterisk.c @@ -884,18 +884,41 @@ static char *cli_prompt(EditLine *el) { static char prompt[200]; char *pfmt; + int color_used=0; + char term_code[20]; if ((pfmt = getenv("ASTERISK_PROMPT"))) { char *t = pfmt, *p = prompt; memset(prompt, 0, sizeof(prompt)); while (*t != '\0' && *p < sizeof(prompt)) { if (*t == '%') { + char hostname[256]; + int i; + struct timeval tv; + struct tm tm; + time_t curtime; + FILE *LOADAVG; + int fgcolor = COLOR_WHITE, bgcolor = COLOR_BLACK; + t++; switch (*t) { - char hostname[256]; - struct timeval tv; - struct tm tm; - FILE *LOADAVG; + case 'C': /* color */ + t++; + if (sscanf(t, "%d;%d%n", &fgcolor, &bgcolor, &i) == 2) { + strncat(p, term_color_code(term_code, fgcolor, bgcolor, sizeof(term_code)),sizeof(prompt) - strlen(prompt)); + t += i - 1; + } else if (sscanf(t, "%d%n", &fgcolor, &i) == 1) { + strncat(p, term_color_code(term_code, fgcolor, 0, sizeof(term_code)),sizeof(prompt) - strlen(prompt)); + t += i - 1; + } + + /* If the color has been reset correctly, then there's no need to reset it later */ + if ((fgcolor == COLOR_WHITE) && (bgcolor == COLOR_BLACK)) { + color_used = 0; + } else { + color_used = 1; + } + break; case 'd': /* date */ memset(&tm, 0, sizeof(struct tm)); gettimeofday(&tv, NULL); @@ -910,6 +933,19 @@ static char *cli_prompt(EditLine *el) strncat(p, "localhost", sizeof(prompt) - strlen(prompt)); } break; + case 'H': /* short hostname */ + if (!gethostname(hostname, sizeof(hostname) - 1)) { + for (i=0;i<sizeof(hostname);i++) { + if (hostname[i] == '.') { + hostname[i] = '\0'; + break; + } + } + strncat(p, hostname, sizeof(prompt) - strlen(prompt)); + } else { + strncat(p, "localhost", sizeof(prompt) - strlen(prompt)); + } + break; #ifdef linux case 'l': /* load avg */ t++; @@ -971,6 +1007,15 @@ static char *cli_prompt(EditLine *el) t++; } } + if (color_used) { + /* Force colors back to normal at end */ + term_color_code(term_code, COLOR_WHITE, COLOR_BLACK, sizeof(term_code)); + if (strlen(term_code) > sizeof(prompt) - strlen(prompt)) { + strncat(prompt + sizeof(prompt) - strlen(term_code) - 1, term_code, strlen(term_code)); + } else { + strncat(p, term_code, sizeof(term_code)); + } + } } else if (remotehostname) snprintf(prompt, sizeof(prompt), ASTERISK_PROMPT2, remotehostname); else diff --git a/doc/README.cliprompt b/doc/README.cliprompt index e66553e9c4..281d3df5f7 100755 --- a/doc/README.cliprompt +++ b/doc/README.cliprompt @@ -8,10 +8,13 @@ You may include the following variables, that will be replaced by the current value by Asterisk: %d Date (year-month-date) -%h Hostname +%h Full hostname +%H Short hostname %t Time %% Percent sign %# '#' if Asterisk is run in console mode, '>' if running as remote console +%Cn[;n] Change terminal foreground (and optional background) color to specified + A full list of colors may be found in include/asterisk/term.h On Linux systems, you may also use %l1 Load average over past minute diff --git a/include/asterisk/term.h b/include/asterisk/term.h index 070547ea82..f6069db65d 100755 --- a/include/asterisk/term.h +++ b/include/asterisk/term.h @@ -46,6 +46,10 @@ extern "C" { extern char *term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int maxout); +extern char *term_color_code(char *outbuf, int fgcolor, int bgcolor, int maxout); + +extern char *term_strip(char *outbuf, char *inbuf, int maxout); + extern char *term_prompt(char *outbuf, const char *inbuf, int maxout); extern char *term_prep(void); diff --git a/term.c b/term.c index 8a4701d216..577d85adfa 100755 --- a/term.c +++ b/term.c @@ -96,6 +96,66 @@ char *term_color(char *outbuf, const char *inbuf, int fgcolor, int bgcolor, int return outbuf; } +char *term_color_code(char *outbuf, int fgcolor, int bgcolor, int maxout) +{ + int attr=0; + char tmp[40]; + if ((!vt100compat) || (!fgcolor && !bgcolor)) { + *outbuf = '\0'; + return outbuf; + } + if ((fgcolor & 128) && (bgcolor & 128)) { + /* Can't both be highlighted */ + *outbuf = '\0'; + return outbuf; + } + if (!bgcolor) + bgcolor = COLOR_BLACK; + + if (bgcolor) { + bgcolor &= ~128; + bgcolor += 10; + } + if (fgcolor & 128) { + attr = ATTR_BRIGHT; + fgcolor &= ~128; + } + if (fgcolor && bgcolor) { + snprintf(tmp, sizeof(tmp), "%d;%d", fgcolor, bgcolor); + } else if (bgcolor) { + snprintf(tmp, sizeof(tmp), "%d", bgcolor); + } else if (fgcolor) { + snprintf(tmp, sizeof(tmp), "%d", fgcolor); + } + if (attr) { + snprintf(outbuf, maxout, "%c[%d;%sm", ESC, attr, tmp); + } else { + snprintf(outbuf, maxout, "%c[%sm", ESC, tmp); + } + return outbuf; +} + +char *term_strip(char *outbuf, char *inbuf, int maxout) +{ + char *outbuf_ptr = outbuf, *inbuf_ptr = inbuf; + + while (outbuf_ptr < outbuf + maxout) { + switch (*inbuf_ptr) { + case ESC: + while (*inbuf_ptr && (*inbuf_ptr != 'm')) + inbuf_ptr++; + break; + default: + *outbuf_ptr = *inbuf_ptr; + outbuf_ptr++; + } + if (! *inbuf_ptr) + break; + inbuf_ptr++; + } + return outbuf; +} + char *term_prompt(char *outbuf, const char *inbuf, int maxout) { if (!vt100compat) { -- GitLab