diff options
author | citats <citats@f38db490-d61c-443f-a65b-d21fe96a405b> | 2004-03-26 04:59:41 +0000 |
---|---|---|
committer | citats <citats@f38db490-d61c-443f-a65b-d21fe96a405b> | 2004-03-26 04:59:41 +0000 |
commit | e61a2437e93467635a2b53e449be071009a02971 (patch) | |
tree | daf2610904c6847e3f26374a22386a61fb3dded6 /asterisk.c | |
parent | da7bdb6f4e38a8b0e3247938d0921abda552adf6 (diff) |
Allow CLI Prompt to be customizable via ASTERISK_PROMPT environment
variable (bug #1270)
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@2565 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'asterisk.c')
-rwxr-xr-x | asterisk.c | 93 |
1 files changed, 90 insertions, 3 deletions
diff --git a/asterisk.c b/asterisk.c index 0ec97bb54..2954db0da 100755 --- a/asterisk.c +++ b/asterisk.c @@ -879,9 +879,96 @@ static int ast_el_read_char(EditLine *el, char *cp) static char *cli_prompt(EditLine *el) { - static char prompt[80]; - - if (remotehostname) + static char prompt[200]; + char *pfmt; + + if ((pfmt = getenv("ASTERISK_PROMPT"))) { + char *t = pfmt, *p = prompt; + memset(prompt, 0, sizeof(prompt)); + while (*t != '\0' && *p < sizeof(prompt)) { + if (*t == '%') { + t++; + switch (*t) { + char hostname[256]; + struct timeval tv; + struct tm tm; + FILE *LOADAVG; + case 'd': /* date */ + memset(&tm, 0, sizeof(struct tm)); + gettimeofday(&tv, NULL); + if (localtime_r(&(tv.tv_sec), &tm)) { + strftime(p, sizeof(prompt) - strlen(prompt), "%Y-%m-%d", &tm); + } + break; + case 'h': /* hostname */ + if (!gethostname(hostname, sizeof(hostname) - 1)) { + strncat(p, hostname, sizeof(prompt) - strlen(prompt)); + } else { + strncat(p, "localhost", sizeof(prompt) - strlen(prompt)); + } + break; +#ifdef linux + case 'l': /* load avg */ + t++; + if ((LOADAVG = fopen("/proc/loadavg", "r"))) { + float avg1, avg2, avg3; + int actproc, totproc, npid, which; + fscanf(LOADAVG, "%f %f %f %d/%d %d", + &avg1, &avg2, &avg3, &actproc, &totproc, &npid); + if (sscanf(t, "%d", &which) == 1) { + switch (which) { + case 1: + snprintf(p, sizeof(prompt) - strlen(prompt), "%.2f", avg1); + break; + case 2: + snprintf(p, sizeof(prompt) - strlen(prompt), "%.2f", avg2); + break; + case 3: + snprintf(p, sizeof(prompt) - strlen(prompt), "%.2f", avg3); + break; + case 4: + snprintf(p, sizeof(prompt) - strlen(prompt), "%d/%d", actproc, totproc); + break; + case 5: + snprintf(p, sizeof(prompt) - strlen(prompt), "%d", npid); + break; + } + } + } + break; +#endif + case 't': /* time */ + memset(&tm, 0, sizeof(struct tm)); + gettimeofday(&tv, NULL); + if (localtime_r(&(tv.tv_sec), &tm)) { + strftime(p, sizeof(prompt) - strlen(prompt), "%H:%M:%S", &tm); + } + break; + case '#': /* process console or remote? */ + if (! option_remote) { + strncat(p, "#", sizeof(prompt) - strlen(prompt)); + } else { + strncat(p, ">", sizeof(prompt) - strlen(prompt)); + } + break; + case '%': /* literal % */ + strncat(p, "%", sizeof(prompt) - strlen(prompt)); + break; + case '\0': /* % is last character - prevent bug */ + t--; + break; + } + while (*p != '\0') { + p++; + } + t++; + } else { + *p = *t; + p++; + t++; + } + } + } else if (remotehostname) snprintf(prompt, sizeof(prompt), ASTERISK_PROMPT2, remotehostname); else snprintf(prompt, sizeof(prompt), ASTERISK_PROMPT); |