From fcad25a12f5dd358d3d33e928029d3359553855e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Courr=C3=A8ges-Anglas?= Date: Wed, 10 Apr 2013 14:42:18 +0200 Subject: Refactor cmd_prompt * use a local variable instead of using ARG_STRING(0) ten times * minimize scope of local variables * use a struct sbuf instead of playing with xmalloc and strncpy * correct indentation --- src/actions.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/actions.c b/src/actions.c index 27a13f3..c6cb373 100644 --- a/src/actions.c +++ b/src/actions.c @@ -5804,25 +5804,32 @@ cmdret * cmd_prompt (int interactive UNUSED, struct cmdarg **args) { cmdret *ret; - char *query, *output, *prefix; + char *output; if (args[0] == NULL) - output = get_input(MESSAGE_PROMPT_COMMAND, hist_PROMPT, trivial_completions); + output = get_input (MESSAGE_PROMPT_COMMAND, hist_PROMPT, + trivial_completions); else { - prefix = strchr (ARG_STRING(0), ':'); + char *arg_str, *prefix; + + arg_str = ARG_STRING(0); + prefix = strchr (arg_str, ':'); + if (prefix) { + struct sbuf *query; + prefix++; /* Don't return the colon. */ - query = xmalloc (prefix - ARG_STRING(0) + 1); - strncpy (query, ARG_STRING(0), prefix - ARG_STRING(0)); - query[prefix - ARG_STRING(0)] = 0; /* null terminate */ - output = get_more_input (query, prefix, hist_PROMPT, trivial_completions); - free (query); + query = sbuf_new (prefix - arg_str); + sbuf_nconcat (query, arg_str, prefix - arg_str); + output = get_more_input (sbuf_get (query), prefix, hist_PROMPT, + trivial_completions); + sbuf_free (query); } else { - output = get_input (ARG_STRING(0), hist_PROMPT, trivial_completions); + output = get_input (arg_str, hist_PROMPT, trivial_completions); } } -- cgit v1.2.3