refactor(usercmd.c): reduce scope of locals

This commit is contained in:
Lewis Russell 2023-09-11 10:32:01 +01:00 committed by Lewis Russell
parent 1d259724a9
commit 676e1d4900

View File

@ -130,23 +130,21 @@ static struct {
char *find_ucmd(exarg_T *eap, char *p, int *full, expand_T *xp, int *complp) char *find_ucmd(exarg_T *eap, char *p, int *full, expand_T *xp, int *complp)
{ {
int len = (int)(p - eap->cmd); int len = (int)(p - eap->cmd);
int j, k, matchlen = 0; int matchlen = 0;
ucmd_T *uc;
bool found = false; bool found = false;
bool possible = false; bool possible = false;
char *cp, *np; // Point into typed cmd and test name
garray_T *gap;
bool amb_local = false; // Found ambiguous buffer-local command, bool amb_local = false; // Found ambiguous buffer-local command,
// only full match global is accepted. // only full match global is accepted.
// Look for buffer-local user commands first, then global ones. // Look for buffer-local user commands first, then global ones.
gap = &prevwin_curwin()->w_buffer->b_ucmds; garray_T *gap = &prevwin_curwin()->w_buffer->b_ucmds;
while (true) { while (true) {
int j;
for (j = 0; j < gap->ga_len; j++) { for (j = 0; j < gap->ga_len; j++) {
uc = USER_CMD_GA(gap, j); ucmd_T *uc = USER_CMD_GA(gap, j);
cp = eap->cmd; char *cp = eap->cmd;
np = uc->uc_name; char *np = uc->uc_name;
k = 0; int k = 0;
while (k < len && *np != NUL && *cp++ == *np++) { while (k < len && *np != NUL && *cp++ == *np++) {
k++; k++;
} }
@ -447,17 +445,15 @@ int cmdcomplete_str_to_type(const char *complete_str)
static void uc_list(char *name, size_t name_len) static void uc_list(char *name, size_t name_len)
{ {
int i, j;
bool found = false; bool found = false;
ucmd_T *cmd;
uint32_t a;
// In cmdwin, the alternative buffer should be used. // In cmdwin, the alternative buffer should be used.
const garray_T *gap = &prevwin_curwin()->w_buffer->b_ucmds; const garray_T *gap = &prevwin_curwin()->w_buffer->b_ucmds;
while (true) { while (true) {
int i;
for (i = 0; i < gap->ga_len; i++) { for (i = 0; i < gap->ga_len; i++) {
cmd = USER_CMD_GA(gap, i); ucmd_T *cmd = USER_CMD_GA(gap, i);
a = cmd->uc_argt; uint32_t a = cmd->uc_argt;
// Skip commands which don't match the requested prefix and // Skip commands which don't match the requested prefix and
// commands filtered out. // commands filtered out.
@ -559,7 +555,7 @@ static void uc_list(char *name, size_t name_len)
} while ((int64_t)len < 8 - over); } while ((int64_t)len < 8 - over);
// Address Type // Address Type
for (j = 0; addr_type_complete[j].expand != ADDR_NONE; j++) { for (int j = 0; addr_type_complete[j].expand != ADDR_NONE; j++) {
if (addr_type_complete[j].expand != ADDR_LINES if (addr_type_complete[j].expand != ADDR_LINES
&& addr_type_complete[j].expand == cmd->uc_addr_type) { && addr_type_complete[j].expand == cmd->uc_addr_type) {
int rc = snprintf(IObuff + len, IOSIZE - len, "%s", addr_type_complete[j].shortname); int rc = snprintf(IObuff + len, IOSIZE - len, "%s", addr_type_complete[j].shortname);
@ -623,11 +619,11 @@ static void uc_list(char *name, size_t name_len)
int parse_addr_type_arg(char *value, int vallen, cmd_addr_T *addr_type_arg) int parse_addr_type_arg(char *value, int vallen, cmd_addr_T *addr_type_arg)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_ALL
{ {
int i, a, b; int i;
for (i = 0; addr_type_complete[i].expand != ADDR_NONE; i++) { for (i = 0; addr_type_complete[i].expand != ADDR_NONE; i++) {
a = (int)strlen(addr_type_complete[i].name) == vallen; int a = (int)strlen(addr_type_complete[i].name) == vallen;
b = strncmp(value, addr_type_complete[i].name, (size_t)vallen) == 0; int b = strncmp(value, addr_type_complete[i].name, (size_t)vallen) == 0;
if (a && b) { if (a && b) {
*addr_type_arg = addr_type_complete[i].expand; *addr_type_arg = addr_type_complete[i].expand;
break; break;
@ -657,11 +653,10 @@ int parse_compl_arg(const char *value, int vallen, int *complp, uint32_t *argt,
{ {
const char *arg = NULL; const char *arg = NULL;
size_t arglen = 0; size_t arglen = 0;
int i;
int valend = vallen; int valend = vallen;
// Look for any argument part - which is the part after any ',' // Look for any argument part - which is the part after any ','
for (i = 0; i < vallen; i++) { for (int i = 0; i < vallen; i++) {
if (value[i] == ',') { if (value[i] == ',') {
arg = (char *)&value[i + 1]; arg = (char *)&value[i + 1];
arglen = (size_t)(vallen - i - 1); arglen = (size_t)(vallen - i - 1);
@ -670,6 +665,7 @@ int parse_compl_arg(const char *value, int vallen, int *complp, uint32_t *argt,
} }
} }
int i;
for (i = 0; i < (int)ARRAY_SIZE(command_complete); i++) { for (i = 0; i < (int)ARRAY_SIZE(command_complete); i++) {
if (get_command_complete(i) == NULL) { if (get_command_complete(i) == NULL) {
continue; continue;
@ -713,8 +709,6 @@ static int uc_scan_attr(char *attr, size_t len, uint32_t *argt, long *def, int *
char **compl_arg, cmd_addr_T *addr_type_arg) char **compl_arg, cmd_addr_T *addr_type_arg)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_ALL
{ {
char *p;
if (len == 0) { if (len == 0) {
emsg(_("E175: No attribute specified")); emsg(_("E175: No attribute specified"));
return FAIL; return FAIL;
@ -732,13 +726,12 @@ static int uc_scan_attr(char *attr, size_t len, uint32_t *argt, long *def, int *
} else if (STRNICMP(attr, "bar", len) == 0) { } else if (STRNICMP(attr, "bar", len) == 0) {
*argt |= EX_TRLBAR; *argt |= EX_TRLBAR;
} else { } else {
int i;
char *val = NULL; char *val = NULL;
size_t vallen = 0; size_t vallen = 0;
size_t attrlen = len; size_t attrlen = len;
// Look for the attribute name - which is the part before any '=' // Look for the attribute name - which is the part before any '='
for (i = 0; i < (int)len; i++) { for (int i = 0; i < (int)len; i++) {
if (attr[i] == '=') { if (attr[i] == '=') {
val = &attr[i + 1]; val = &attr[i + 1];
vallen = len - (size_t)i - 1; vallen = len - (size_t)i - 1;
@ -772,7 +765,7 @@ wrong_nargs:
if (vallen == 1 && *val == '%') { if (vallen == 1 && *val == '%') {
*argt |= EX_DFLALL; *argt |= EX_DFLALL;
} else if (val != NULL) { } else if (val != NULL) {
p = val; char *p = val;
if (*def >= 0) { if (*def >= 0) {
two_count: two_count:
emsg(_("E177: Count cannot be specified twice")); emsg(_("E177: Count cannot be specified twice"));
@ -800,7 +793,7 @@ invalid_count:
} }
if (val != NULL) { if (val != NULL) {
p = val; char *p = val;
if (*def >= 0) { if (*def >= 0) {
goto two_count; goto two_count;
} }
@ -878,7 +871,6 @@ int uc_add_command(char *name, size_t name_len, const char *rep, uint32_t argt,
FUNC_ATTR_NONNULL_ARG(1, 3) FUNC_ATTR_NONNULL_ARG(1, 3)
{ {
ucmd_T *cmd = NULL; ucmd_T *cmd = NULL;
int i;
int cmp = 1; int cmp = 1;
char *rep_buf = NULL; char *rep_buf = NULL;
garray_T *gap; garray_T *gap;
@ -899,12 +891,12 @@ int uc_add_command(char *name, size_t name_len, const char *rep, uint32_t argt,
gap = &ucmds; gap = &ucmds;
} }
int i;
// Search for the command in the already defined commands. // Search for the command in the already defined commands.
for (i = 0; i < gap->ga_len; i++) { for (i = 0; i < gap->ga_len; i++) {
size_t len;
cmd = USER_CMD_GA(gap, i); cmd = USER_CMD_GA(gap, i);
len = strlen(cmd->uc_name); size_t len = strlen(cmd->uc_name);
cmp = strncmp(name, cmd->uc_name, name_len); cmp = strncmp(name, cmd->uc_name, name_len);
if (cmp == 0) { if (cmp == 0) {
if (name_len < len) { if (name_len < len) {
@ -980,9 +972,7 @@ fail:
/// ":command ..." /// ":command ..."
void ex_command(exarg_T *eap) void ex_command(exarg_T *eap)
{ {
char *name;
char *end; char *end;
char *p;
uint32_t argt = 0; uint32_t argt = 0;
long def = -1; long def = -1;
int flags = 0; int flags = 0;
@ -990,9 +980,8 @@ void ex_command(exarg_T *eap)
char *compl_arg = NULL; char *compl_arg = NULL;
cmd_addr_T addr_type_arg = ADDR_NONE; cmd_addr_T addr_type_arg = ADDR_NONE;
int has_attr = (eap->arg[0] == '-'); int has_attr = (eap->arg[0] == '-');
size_t name_len;
p = eap->arg; char *p = eap->arg;
// Check for attributes // Check for attributes
while (*p == '-') { while (*p == '-') {
@ -1006,13 +995,13 @@ void ex_command(exarg_T *eap)
} }
// Get the name (if any) and skip to the following argument. // Get the name (if any) and skip to the following argument.
name = p; char *name = p;
end = uc_validate_name(name); end = uc_validate_name(name);
if (!end) { if (!end) {
emsg(_("E182: Invalid command name")); emsg(_("E182: Invalid command name"));
goto theend; goto theend;
} }
name_len = (size_t)(end - name); size_t name_len = (size_t)(end - name);
// If there is nothing after the name, and no attributes were specified, // If there is nothing after the name, and no attributes were specified,
// we are listing commands // we are listing commands
@ -1065,7 +1054,6 @@ void ex_delcommand(exarg_T *eap)
int i = 0; int i = 0;
ucmd_T *cmd = NULL; ucmd_T *cmd = NULL;
int res = -1; int res = -1;
garray_T *gap;
const char *arg = eap->arg; const char *arg = eap->arg;
bool buffer_only = false; bool buffer_only = false;
@ -1074,7 +1062,7 @@ void ex_delcommand(exarg_T *eap)
arg = skipwhite(arg + 7); arg = skipwhite(arg + 7);
} }
gap = &curbuf->b_ucmds; garray_T *gap = &curbuf->b_ucmds;
while (true) { while (true) {
for (i = 0; i < gap->ga_len; i++) { for (i = 0; i < gap->ga_len; i++) {
cmd = USER_CMD_GA(gap, i); cmd = USER_CMD_GA(gap, i);
@ -1153,15 +1141,10 @@ bool uc_split_args_iter(const char *arg, size_t arglen, size_t *end, char *buf,
static char *uc_split_args(const char *arg, char **args, const size_t *arglens, size_t argc, static char *uc_split_args(const char *arg, char **args, const size_t *arglens, size_t argc,
size_t *lenp) size_t *lenp)
{ {
char *buf;
const char *p;
char *q;
int len;
// Precalculate length // Precalculate length
len = 2; // Initial and final quotes int len = 2; // Initial and final quotes
if (args == NULL) { if (args == NULL) {
p = arg; const char *p = arg;
while (*p) { while (*p) {
if (p[0] == '\\' && p[1] == '\\') { if (p[0] == '\\' && p[1] == '\\') {
@ -1188,7 +1171,7 @@ static char *uc_split_args(const char *arg, char **args, const size_t *arglens,
} }
} else { } else {
for (size_t i = 0; i < argc; i++) { for (size_t i = 0; i < argc; i++) {
p = args[i]; const char *p = args[i];
const char *arg_end = args[i] + arglens[i]; const char *arg_end = args[i] + arglens[i];
while (p < arg_end) { while (p < arg_end) {
@ -1209,13 +1192,13 @@ static char *uc_split_args(const char *arg, char **args, const size_t *arglens,
} }
} }
buf = xmalloc((size_t)len + 1); char *buf = xmalloc((size_t)len + 1);
q = buf; char *q = buf;
*q++ = '"'; *q++ = '"';
if (args == NULL) { if (args == NULL) {
p = arg; const char *p = arg;
while (*p) { while (*p) {
if (p[0] == '\\' && p[1] == '\\') { if (p[0] == '\\' && p[1] == '\\') {
*q++ = '\\'; *q++ = '\\';
@ -1242,7 +1225,7 @@ static char *uc_split_args(const char *arg, char **args, const size_t *arglens,
} }
} else { } else {
for (size_t i = 0; i < argc; i++) { for (size_t i = 0; i < argc; i++) {
p = args[i]; const char *p = args[i];
const char *arg_end = args[i] + arglens[i]; const char *arg_end = args[i] + arglens[i];
while (p < arg_end) { while (p < arg_end) {
@ -1622,14 +1605,7 @@ static size_t uc_check_code(char *code, size_t len, char *buf, ucmd_T *cmd, exar
int do_ucmd(exarg_T *eap, bool preview) int do_ucmd(exarg_T *eap, bool preview)
{ {
char *buf;
char *p;
char *q;
char *start;
char *end = NULL; char *end = NULL;
char *ksp;
size_t len, totlen;
size_t split_len = 0; size_t split_len = 0;
char *split_buf = NULL; char *split_buf = NULL;
@ -1654,18 +1630,19 @@ int do_ucmd(exarg_T *eap, bool preview)
// Replace <> in the command by the arguments. // Replace <> in the command by the arguments.
// First round: "buf" is NULL, compute length, allocate "buf". // First round: "buf" is NULL, compute length, allocate "buf".
// Second round: copy result into "buf". // Second round: copy result into "buf".
buf = NULL; char *buf = NULL;
while (true) { while (true) {
p = cmd->uc_rep; // source char *p = cmd->uc_rep; // source
q = buf; // destination char *q = buf; // destination
totlen = 0; size_t totlen = 0;
while (true) { while (true) {
start = vim_strchr(p, '<'); char *start = vim_strchr(p, '<');
if (start != NULL) { if (start != NULL) {
end = vim_strchr(start + 1, '>'); end = vim_strchr(start + 1, '>');
} }
if (buf != NULL) { if (buf != NULL) {
char *ksp;
for (ksp = p; *ksp != NUL && (uint8_t)(*ksp) != K_SPECIAL; ksp++) {} for (ksp = p; *ksp != NUL && (uint8_t)(*ksp) != K_SPECIAL; ksp++) {}
if ((uint8_t)(*ksp) == K_SPECIAL if ((uint8_t)(*ksp) == K_SPECIAL
&& (start == NULL || ksp < start || end == NULL) && (start == NULL || ksp < start || end == NULL)
@ -1673,7 +1650,7 @@ int do_ucmd(exarg_T *eap, bool preview)
// K_SPECIAL has been put in the buffer as K_SPECIAL // K_SPECIAL has been put in the buffer as K_SPECIAL
// KS_SPECIAL KE_FILLER, like for mappings, but // KS_SPECIAL KE_FILLER, like for mappings, but
// do_cmdline() doesn't handle that, so convert it back. // do_cmdline() doesn't handle that, so convert it back.
len = (size_t)(ksp - p); size_t len = (size_t)(ksp - p);
if (len > 0) { if (len > 0) {
memmove(q, p, len); memmove(q, p, len);
q += len; q += len;
@ -1693,7 +1670,7 @@ int do_ucmd(exarg_T *eap, bool preview)
end++; end++;
// Take everything up to the '<' // Take everything up to the '<'
len = (size_t)(start - p); size_t len = (size_t)(start - p);
if (buf == NULL) { if (buf == NULL) {
totlen += len; totlen += len;
} else { } else {