Index: term.c =================================================================== RCS file: /usr/cvsroot/asterisk/term.c,v retrieving revision 1.3 diff -u -r1.3 term.c --- term.c 22 Oct 2003 03:04:45 -0000 1.3 +++ term.c 2 May 2004 18:43:34 -0000 @@ -96,6 +96,66 @@ 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) { Index: asterisk.c =================================================================== RCS file: /usr/cvsroot/asterisk/asterisk.c,v retrieving revision 1.78 diff -u -r1.78 asterisk.c --- asterisk.c 1 May 2004 03:14:02 -0000 1.78 +++ asterisk.c 2 May 2004 18:43:35 -0000 @@ -884,18 +884,41 @@ { 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 @@ strncat(p, "localhost", sizeof(prompt) - strlen(prompt)); } break; + case 'H': /* short hostname */ + if (!gethostname(hostname, sizeof(hostname) - 1)) { + for (i=0;i 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 Index: include/asterisk/term.h =================================================================== RCS file: /usr/cvsroot/asterisk/include/asterisk/term.h,v retrieving revision 1.2 diff -u -r1.2 term.h --- include/asterisk/term.h 16 Mar 2003 22:37:31 -0000 1.2 +++ include/asterisk/term.h 2 May 2004 18:43:35 -0000 @@ -46,6 +46,10 @@ 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); Index: doc/README.cliprompt =================================================================== RCS file: /usr/cvsroot/asterisk/doc/README.cliprompt,v retrieving revision 1.2 diff -u -r1.2 README.cliprompt --- doc/README.cliprompt 1 May 2004 15:23:35 -0000 1.2 +++ doc/README.cliprompt 2 May 2004 18:43:35 -0000 @@ -8,10 +8,13 @@ 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