Add %U and %H escapes and redo prompt rewriting. "%%" now gets collapsed
to "%" as was originally intended. This also gets rid of lastchar (does lookahead instead of lookback) which should simplify the logic slightly.
This commit is contained in:
80
check.c
80
check.c
@@ -176,49 +176,79 @@ expand_prompt(old_prompt, user, host)
|
|||||||
{
|
{
|
||||||
size_t len;
|
size_t len;
|
||||||
int subst;
|
int subst;
|
||||||
char *p, *np, *new_prompt, lastchar;
|
char *p, *np, *new_prompt;
|
||||||
|
|
||||||
/* How much space do we need to malloc for the prompt? */
|
/* How much space do we need to malloc for the prompt? */
|
||||||
subst = 0;
|
subst = 0;
|
||||||
for (p = old_prompt, len = strlen(old_prompt), lastchar = '\0'; *p; p++) {
|
for (p = old_prompt, len = strlen(old_prompt); *p; p++) {
|
||||||
if (lastchar == '%') {
|
if (p[0] =='%') {
|
||||||
if (*p == 'h') {
|
switch (p[1]) {
|
||||||
|
case 'h':
|
||||||
|
p++;
|
||||||
len += strlen(user_shost) - 2;
|
len += strlen(user_shost) - 2;
|
||||||
subst = 1;
|
subst = 1;
|
||||||
} else if (*p == 'u') {
|
break;
|
||||||
|
case 'H':
|
||||||
|
p++;
|
||||||
|
len += strlen(user_host) - 2;
|
||||||
|
subst = 1;
|
||||||
|
break;
|
||||||
|
case 'u':
|
||||||
|
p++;
|
||||||
len += strlen(user_name) - 2;
|
len += strlen(user_name) - 2;
|
||||||
subst = 1;
|
subst = 1;
|
||||||
}
|
break;
|
||||||
}
|
case 'U':
|
||||||
|
p++;
|
||||||
if (lastchar == '%' && *p == '%') {
|
len += strlen(*user_runas) - 2;
|
||||||
lastchar = '\0';
|
subst = 1;
|
||||||
|
break;
|
||||||
|
case '%':
|
||||||
|
p++;
|
||||||
len--;
|
len--;
|
||||||
} else
|
subst = 1;
|
||||||
lastchar = *p;
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (subst) {
|
if (subst) {
|
||||||
new_prompt = (char *) emalloc(len + 1);
|
new_prompt = (char *) emalloc(len + 1);
|
||||||
for (p = old_prompt, np = new_prompt, lastchar = '\0'; *p; p++) {
|
for (p = old_prompt, np = new_prompt; *p; p++) {
|
||||||
if (lastchar == '%' && (*p == 'h' || *p == 'u' || *p == '%')) {
|
if (p[0] =='%') {
|
||||||
/* substitute user/host name */
|
switch (p[1]) {
|
||||||
if (*p == 'h') {
|
case 'h':
|
||||||
np--;
|
p++;
|
||||||
strcpy(np, user_shost);
|
strcpy(np, user_shost);
|
||||||
np += strlen(user_shost);
|
np += strlen(user_shost);
|
||||||
} else if (*p == 'u') {
|
continue;
|
||||||
np--;
|
case 'H':
|
||||||
|
p++;
|
||||||
|
strcpy(np, user_host);
|
||||||
|
np += strlen(user_host);
|
||||||
|
continue;
|
||||||
|
case 'u':
|
||||||
|
p++;
|
||||||
strcpy(np, user_name);
|
strcpy(np, user_name);
|
||||||
np += strlen(user_name);
|
np += strlen(user_name);
|
||||||
|
continue;
|
||||||
|
case 'U':
|
||||||
|
p++;
|
||||||
|
strcpy(np, *user_runas);
|
||||||
|
np += strlen(*user_runas);
|
||||||
|
continue;
|
||||||
|
case '%':
|
||||||
|
/* convert %% -> % */
|
||||||
|
p++;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
/* no conversion */
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else
|
|
||||||
*np++ = *p;
|
*np++ = *p;
|
||||||
|
|
||||||
if (lastchar == '%' && *p == '%')
|
|
||||||
lastchar = '\0';
|
|
||||||
else
|
|
||||||
lastchar = *p;
|
|
||||||
}
|
}
|
||||||
*np = '\0';
|
*np = '\0';
|
||||||
} else
|
} else
|
||||||
|
Reference in New Issue
Block a user