aboutsummaryrefslogtreecommitdiffstats
path: root/asterisk.c
diff options
context:
space:
mode:
authormarkster <markster@f38db490-d61c-443f-a65b-d21fe96a405b>2004-05-02 19:13:16 +0000
committermarkster <markster@f38db490-d61c-443f-a65b-d21fe96a405b>2004-05-02 19:13:16 +0000
commiteae2e282472b4c2d73e90d946ab858f1b7494b77 (patch)
tree6c482de8485265a99288ae876a69775a09c5f7f6 /asterisk.c
parent4696367480f82cbfce5503707e111a20538288cc (diff)
Merge Tilghman's color patches for the asterisk prompt (bug #1535)
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@2862 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'asterisk.c')
-rwxr-xr-xasterisk.c53
1 files changed, 49 insertions, 4 deletions
diff --git a/asterisk.c b/asterisk.c
index e21f149dc..f7abea070 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