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:
Todd C. Miller
2002-12-14 19:15:30 +00:00
parent 4f9ed308f0
commit 0b0f8d6d32

96
check.c
View File

@@ -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]) {
len += strlen(user_shost) - 2; case 'h':
subst = 1; p++;
} else if (*p == 'u') { len += strlen(user_shost) - 2;
len += strlen(user_name) - 2; subst = 1;
subst = 1; break;
case 'H':
p++;
len += strlen(user_host) - 2;
subst = 1;
break;
case 'u':
p++;
len += strlen(user_name) - 2;
subst = 1;
break;
case 'U':
p++;
len += strlen(*user_runas) - 2;
subst = 1;
break;
case '%':
p++;
len--;
subst = 1;
break;
default:
break;
} }
} }
if (lastchar == '%' && *p == '%') {
lastchar = '\0';
len--;
} else
lastchar = *p;
} }
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':
strcpy(np, user_name); p++;
np += strlen(user_name); strcpy(np, user_host);
np += strlen(user_host);
continue;
case 'u':
p++;
strcpy(np, 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