diff options
author | Jérémie Courrèges-Anglas <jca@wxcvbn.org> | 2013-04-10 14:42:18 +0200 |
---|---|---|
committer | Jérémie Courrèges-Anglas <jca@wxcvbn.org> | 2013-04-10 14:42:18 +0200 |
commit | fcad25a12f5dd358d3d33e928029d3359553855e (patch) | |
tree | 718567ac5c2d9a0c5a84a8b4826d8fd6dd8fa1b2 | |
parent | db8be33ae5bc8645342c62cca0a481bc0078d846 (diff) | |
download | ratpoison-fcad25a12f5dd358d3d33e928029d3359553855e.zip |
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
-rw-r--r-- | src/actions.c | 25 |
1 files changed, 16 insertions, 9 deletions
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); } } |