version 1.9, 2009/08/23 16:45:00 |
version 1.10, 2009/08/25 12:18:51 |
|
|
|
|
int cmd_command_prompt_callback(void *, const char *); |
int cmd_command_prompt_callback(void *, const char *); |
void cmd_command_prompt_cfree(void *); |
void cmd_command_prompt_cfree(void *); |
char *cmd_command_prompt_replace(char *, const char *, int); |
|
|
|
const struct cmd_entry cmd_command_prompt_entry = { |
const struct cmd_entry cmd_command_prompt_entry = { |
"command-prompt", NULL, |
"command-prompt", NULL, |
|
|
if (s == NULL) |
if (s == NULL) |
return (0); |
return (0); |
|
|
newtempl = cmd_command_prompt_replace(cdata->template, s, cdata->idx); |
newtempl = cmd_template_replace(cdata->template, s, cdata->idx); |
xfree(cdata->template); |
xfree(cdata->template); |
cdata->template = newtempl; |
cdata->template = newtempl; |
|
|
|
|
if (cdata->template != NULL) |
if (cdata->template != NULL) |
xfree(cdata->template); |
xfree(cdata->template); |
xfree(cdata); |
xfree(cdata); |
} |
|
|
|
char * |
|
cmd_command_prompt_replace(char *template, const char *s, int idx) |
|
{ |
|
char ch; |
|
char *buf, *ptr; |
|
int replaced; |
|
size_t len; |
|
|
|
if (strstr(template, "%") == NULL) |
|
return (xstrdup(template)); |
|
|
|
buf = xmalloc(1); |
|
*buf = '\0'; |
|
len = 0; |
|
replaced = 0; |
|
|
|
ptr = template; |
|
while (*ptr != '\0') { |
|
switch (ch = *ptr++) { |
|
case '%': |
|
if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) { |
|
if (*ptr != '%' || replaced) |
|
break; |
|
replaced = 1; |
|
} |
|
ptr++; |
|
|
|
len += strlen(s); |
|
buf = xrealloc(buf, 1, len + 1); |
|
strlcat(buf, s, len + 1); |
|
continue; |
|
} |
|
buf = xrealloc(buf, 1, len + 2); |
|
buf[len++] = ch; |
|
buf[len] = '\0'; |
|
} |
|
|
|
return (buf); |
|
} |
} |