mirror of
https://github.com/asterisk/asterisk.git
synced 2025-10-13 00:04:53 +00:00
Add %u and %g to the ASTERISK_PROMPT settings, for username and group,
respectively. Also, take the opportunity to clean up the CLI prompt generation code. (closes issue #13175) Reported by: eliel Patches: cliprompt.patch uploaded by eliel (license 64) git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@134353 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
@@ -12,18 +12,18 @@ the current value by Asterisk:
|
|||||||
\item \%h - Full hostname
|
\item \%h - Full hostname
|
||||||
\item \%H - Short hostname
|
\item \%H - Short hostname
|
||||||
\item \%t - Time
|
\item \%t - Time
|
||||||
|
\item \%u - Username
|
||||||
|
\item \%g - Groupname
|
||||||
\item \%\% - Percent sign
|
\item \%\% - Percent sign
|
||||||
\item \%\# - '\#' if Asterisk is run in console mode, '$>$' if running as remote console
|
\item \%\# - '\#' if Asterisk is run in console mode, '$>$' if running as remote console
|
||||||
\item \%Cn[;n] - Change terminal foreground (and optional background) color to specified
|
\item \%Cn[;n] - Change terminal foreground (and optional background) color to specified
|
||||||
A full list of colors may be found in \path{include/asterisk/term.h}
|
A full list of colors may be found in \path{include/asterisk/term.h}
|
||||||
\end{itemize}
|
\end{itemize}
|
||||||
|
|
||||||
On Linux systems, you may also use:
|
On systems which implement getloadavg(3), you may also use:
|
||||||
|
|
||||||
\begin{itemize}
|
\begin{itemize}
|
||||||
\item \%l1 - Load average over past minute
|
\item \%l1 - Load average over past minute
|
||||||
\item \%l2 - Load average over past 5 minutes
|
\item \%l2 - Load average over past 5 minutes
|
||||||
\item \%l3 - Load average over past 15 minutes
|
\item \%l3 - Load average over past 15 minutes
|
||||||
\item \%l4 - Process fraction (processes running / total processes)
|
|
||||||
\item \%l5 - The most recently allocated pid
|
|
||||||
\end{itemize}
|
\end{itemize}
|
||||||
|
150
main/asterisk.c
150
main/asterisk.c
@@ -1991,25 +1991,34 @@ static int ast_el_read_char(EditLine *el, char *cp)
|
|||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static struct ast_str *prompt = NULL;
|
||||||
|
|
||||||
static char *cli_prompt(EditLine *el)
|
static char *cli_prompt(EditLine *el)
|
||||||
{
|
{
|
||||||
static char prompt[200];
|
char tmp[100];
|
||||||
char *pfmt;
|
char *pfmt;
|
||||||
int color_used = 0;
|
int color_used = 0;
|
||||||
|
static int cli_prompt_changes = 0;
|
||||||
char term_code[20];
|
char term_code[20];
|
||||||
|
struct passwd *pw;
|
||||||
|
struct group *gr;
|
||||||
|
|
||||||
|
if (prompt == NULL) {
|
||||||
|
prompt = ast_str_create(100);
|
||||||
|
} else if (!cli_prompt_changes) {
|
||||||
|
return prompt->str;
|
||||||
|
} else {
|
||||||
|
ast_str_reset(prompt);
|
||||||
|
}
|
||||||
|
|
||||||
if ((pfmt = getenv("ASTERISK_PROMPT"))) {
|
if ((pfmt = getenv("ASTERISK_PROMPT"))) {
|
||||||
char *t = pfmt, *p = prompt;
|
char *t = pfmt;
|
||||||
memset(prompt, 0, sizeof(prompt));
|
struct timeval ts = ast_tvnow();
|
||||||
while (*t != '\0' && *p < sizeof(prompt)) {
|
while (*t != '\0') {
|
||||||
if (*t == '%') {
|
if (*t == '%') {
|
||||||
char hostname[MAXHOSTNAMELEN]="";
|
char hostname[MAXHOSTNAMELEN] = "";
|
||||||
int i;
|
int i, which;
|
||||||
struct timeval ts = ast_tvnow();
|
|
||||||
struct ast_tm tm = { 0, };
|
struct ast_tm tm = { 0, };
|
||||||
#ifdef linux
|
|
||||||
FILE *LOADAVG;
|
|
||||||
#endif
|
|
||||||
int fgcolor = COLOR_WHITE, bgcolor = COLOR_BLACK;
|
int fgcolor = COLOR_WHITE, bgcolor = COLOR_BLACK;
|
||||||
|
|
||||||
t++;
|
t++;
|
||||||
@@ -2017,10 +2026,10 @@ static char *cli_prompt(EditLine *el)
|
|||||||
case 'C': /* color */
|
case 'C': /* color */
|
||||||
t++;
|
t++;
|
||||||
if (sscanf(t, "%d;%d%n", &fgcolor, &bgcolor, &i) == 2) {
|
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) - 1);
|
ast_str_append(&prompt, 0, "%s", term_color_code(term_code, fgcolor, bgcolor, sizeof(term_code)));
|
||||||
t += i - 1;
|
t += i - 1;
|
||||||
} else if (sscanf(t, "%d%n", &fgcolor, &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) - 1);
|
ast_str_append(&prompt, 0, "%s", term_color_code(term_code, fgcolor, 0, sizeof(term_code)));
|
||||||
t += i - 1;
|
t += i - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2028,102 +2037,91 @@ static char *cli_prompt(EditLine *el)
|
|||||||
color_used = ((fgcolor == COLOR_WHITE) && (bgcolor == COLOR_BLACK)) ? 0 : 1;
|
color_used = ((fgcolor == COLOR_WHITE) && (bgcolor == COLOR_BLACK)) ? 0 : 1;
|
||||||
break;
|
break;
|
||||||
case 'd': /* date */
|
case 'd': /* date */
|
||||||
if (ast_localtime(&ts, &tm, NULL))
|
if (ast_localtime(&ts, &tm, NULL)) {
|
||||||
ast_strftime(p, sizeof(prompt) - strlen(prompt), "%Y-%m-%d", &tm);
|
ast_strftime(tmp, sizeof(tmp), "%Y-%m-%d", &tm);
|
||||||
|
ast_str_append(&prompt, 0, "%s", tmp);
|
||||||
|
cli_prompt_changes++;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'g': /* group */
|
||||||
|
if ((gr = getgrgid(getgid()))) {
|
||||||
|
ast_str_append(&prompt, 0, "%s", gr->gr_name);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'h': /* hostname */
|
case 'h': /* hostname */
|
||||||
if (!gethostname(hostname, sizeof(hostname) - 1))
|
if (!gethostname(hostname, sizeof(hostname) - 1)) {
|
||||||
strncat(p, hostname, sizeof(prompt) - strlen(prompt) - 1);
|
ast_str_append(&prompt, 0, "%s", hostname);
|
||||||
else
|
} else {
|
||||||
strncat(p, "localhost", sizeof(prompt) - strlen(prompt) - 1);
|
ast_str_append(&prompt, 0, "%s", "localhost");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'H': /* short hostname */
|
case 'H': /* short hostname */
|
||||||
if (!gethostname(hostname, sizeof(hostname) - 1)) {
|
if (!gethostname(hostname, sizeof(hostname) - 1)) {
|
||||||
for (i = 0; i < sizeof(hostname); i++) {
|
char *dotptr;
|
||||||
if (hostname[i] == '.') {
|
if ((dotptr = strchr(hostname, '.'))) {
|
||||||
hostname[i] = '\0';
|
*dotptr = '\0';
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
strncat(p, hostname, sizeof(prompt) - strlen(prompt) - 1);
|
ast_str_append(&prompt, 0, "%s", hostname);
|
||||||
} else
|
} else {
|
||||||
strncat(p, "localhost", sizeof(prompt) - strlen(prompt) - 1);
|
ast_str_append(&prompt, 0, "%s", "localhost");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
#ifdef linux
|
#ifdef HAVE_GETLOADAVG
|
||||||
case 'l': /* load avg */
|
case 'l': /* load avg */
|
||||||
t++;
|
t++;
|
||||||
if ((LOADAVG = fopen("/proc/loadavg", "r"))) {
|
if (sscanf(t, "%d", &which) == 1 && which > 0 && which <= 3) {
|
||||||
float avg1, avg2, avg3;
|
double list[3];
|
||||||
int actproc, totproc, npid, which;
|
getloadavg(list, 3);
|
||||||
fscanf(LOADAVG, "%f %f %f %d/%d %d",
|
ast_str_append(&prompt, 0, "%.2f", list[which - 1]);
|
||||||
&avg1, &avg2, &avg3, &actproc, &totproc, &npid);
|
cli_prompt_changes++;
|
||||||
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;
|
break;
|
||||||
#endif
|
#endif
|
||||||
case 's': /* Asterisk system name (from asterisk.conf) */
|
case 's': /* Asterisk system name (from asterisk.conf) */
|
||||||
strncat(p, ast_config_AST_SYSTEM_NAME, sizeof(prompt) - strlen(prompt) - 1);
|
ast_str_append(&prompt, 0, "%s", ast_config_AST_SYSTEM_NAME);
|
||||||
break;
|
break;
|
||||||
case 't': /* time */
|
case 't': /* time */
|
||||||
if (ast_localtime(&ts, &tm, NULL))
|
if (ast_localtime(&ts, &tm, NULL)) {
|
||||||
ast_strftime(p, sizeof(prompt) - strlen(prompt), "%H:%M:%S", &tm);
|
ast_strftime(tmp, sizeof(tmp), "%H:%M:%S", &tm);
|
||||||
|
ast_str_append(&prompt, 0, "%s", tmp);
|
||||||
|
cli_prompt_changes++;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'u': /* username */
|
||||||
|
if ((pw = getpwuid(getuid()))) {
|
||||||
|
ast_str_append(&prompt, 0, "%s", pw->pw_name);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case '#': /* process console or remote? */
|
case '#': /* process console or remote? */
|
||||||
if (!ast_opt_remote)
|
ast_str_append(&prompt, 0, "%c", ast_opt_remote ? '>' : '#');
|
||||||
strncat(p, "#", sizeof(prompt) - strlen(prompt) - 1);
|
|
||||||
else
|
|
||||||
strncat(p, ">", sizeof(prompt) - strlen(prompt) - 1);
|
|
||||||
break;
|
break;
|
||||||
case '%': /* literal % */
|
case '%': /* literal % */
|
||||||
strncat(p, "%", sizeof(prompt) - strlen(prompt) - 1);
|
ast_str_append(&prompt, 0, "%c", '%');
|
||||||
break;
|
break;
|
||||||
case '\0': /* % is last character - prevent bug */
|
case '\0': /* % is last character - prevent bug */
|
||||||
t--;
|
t--;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
while (*p != '\0')
|
|
||||||
p++;
|
|
||||||
t++;
|
t++;
|
||||||
} else {
|
} else {
|
||||||
*p = *t;
|
if (prompt->used + 5 > prompt->len) {
|
||||||
p++;
|
ast_str_make_space(&prompt, prompt->len + 5);
|
||||||
t++;
|
}
|
||||||
|
prompt->str[prompt->used++] = *t++;
|
||||||
|
prompt->str[prompt->used] = '\0';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (color_used) {
|
if (color_used) {
|
||||||
/* Force colors back to normal at end */
|
/* Force colors back to normal at end */
|
||||||
term_color_code(term_code, COLOR_WHITE, COLOR_BLACK, sizeof(term_code));
|
ast_str_append(&prompt, 0, "%s", term_color_code(term_code, COLOR_WHITE, COLOR_BLACK, sizeof(term_code)));
|
||||||
if (strlen(term_code) > sizeof(prompt) - strlen(prompt) - 1) {
|
|
||||||
ast_copy_string(prompt + sizeof(prompt) - strlen(term_code) - 1, term_code, strlen(term_code) + 1);
|
|
||||||
} else {
|
|
||||||
/* This looks wrong, but we've already checked the length of term_code to ensure it's safe */
|
|
||||||
strncat(p, term_code, sizeof(term_code));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (remotehostname)
|
} else if (remotehostname) {
|
||||||
snprintf(prompt, sizeof(prompt), ASTERISK_PROMPT2, remotehostname);
|
ast_str_set(&prompt, 0, ASTERISK_PROMPT2, remotehostname);
|
||||||
else
|
} else {
|
||||||
ast_copy_string(prompt, ASTERISK_PROMPT, sizeof(prompt));
|
ast_str_set(&prompt, 0, "%s", ASTERISK_PROMPT);
|
||||||
|
}
|
||||||
|
|
||||||
return(prompt);
|
return(prompt->str);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char **ast_el_strtoarr(char *buf)
|
static char **ast_el_strtoarr(char *buf)
|
||||||
|
Reference in New Issue
Block a user