ex_getln: Refactor script_get()

1. Use `char *` for strings.
2. Add `const` qualifiers.
3. Add attributes and documentation.
4. Handle skipping *inside*.
5. Handle non-heredoc argument also inside: deferring this to the caller is
   pointless because all callers need the same thing. Though new ex_lua caller
   may live without allocations in this case, allocating nevertheless produces
   cleaner code.
6. Note that all callers call script_get with `eap` and `eap->arg`. Thus second
   argument is useless in practice: it is one and the same always and can be
   reached through the first argument.
This commit is contained in:
ZyX
2017-01-29 03:36:47 +03:00
parent 3531d8c8ea
commit 3d48c35d6b
4 changed files with 58 additions and 38 deletions

View File

@@ -5343,47 +5343,61 @@ static int ex_window(void)
return cmdwin_result;
}
/*
* Used for commands that either take a simple command string argument, or:
* cmd << endmarker
* {script}
* endmarker
* Returns a pointer to allocated memory with {script} or NULL.
*/
char_u *script_get(exarg_T *eap, char_u *cmd)
/// Get script string
///
/// Used for commands which accept either `:command script` or
///
/// :command << endmarker
/// script
/// endmarker
///
/// @param eap Command being run.
/// @param[out] lenp Location where length of resulting string is saved. Will
/// be set to zero when skipping.
///
/// @return [allocated] NULL or script. Does not show any error messages.
/// NULL is returned when skipping and on error.
char *script_get(exarg_T *const eap, size_t *const lenp)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_MALLOC
{
char_u *theline;
char *end_pattern = NULL;
char dot[] = ".";
garray_T ga;
const char *const cmd = (const char *)eap->arg;
if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
return NULL;
if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL) {
*lenp = STRLEN(eap->arg);
return xmemdupz(eap->arg, *lenp);
}
ga_init(&ga, 1, 0x400);
garray_T ga = { .ga_data = NULL, .ga_len = 0 };
if (!eap->skip) {
ga_init(&ga, 1, 0x400);
}
if (cmd[2] != NUL)
end_pattern = (char *)skipwhite(cmd + 2);
else
end_pattern = dot;
for (;; ) {
theline = eap->getline(
const char *const end_pattern = (
cmd[2] != NUL
? (const char *)skipwhite((const char_u *)cmd + 2)
: ".");
for (;;) {
char *const theline = (char *)eap->getline(
eap->cstack->cs_looplevel > 0 ? -1 :
NUL, eap->cookie, 0);
if (theline == NULL || STRCMP(end_pattern, theline) == 0) {
if (theline == NULL || strcmp(end_pattern, theline) == 0) {
xfree(theline);
break;
}
ga_concat(&ga, theline);
ga_append(&ga, '\n');
if (!eap->skip) {
ga_concat(&ga, (const char_u *)theline);
ga_append(&ga, '\n');
}
xfree(theline);
}
ga_append(&ga, NUL);
*lenp = (size_t)ga.ga_len; // Set length without trailing NUL.
if (!eap->skip) {
ga_append(&ga, NUL);
}
return (char_u *)ga.ga_data;
return (char *)ga.ga_data;
}
/// Iterate over history items