Skip to content

Commit

Permalink
Extract some helper functions in engine/cmds.c
Browse files Browse the repository at this point in the history
Just to make things less repetitive and easier to read.
  • Loading branch information
xaizek committed Dec 30, 2023
1 parent 547c358 commit 93fe3be
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/engine/cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ static int delcommand_cmd(const cmd_info_t *cmd_info);
TSTATIC char ** dispatch_line(const char args[], int *count, char sep,
int regexp, int quotes, int noescaping, int comments, int *last_arg,
int (**positions)[2]);
static int is_builtin_like_cmd(const cmd_t *cmd);
static int is_custom_cmd(const cmd_t *cmd);
static int is_separator(char c, char sep);

void
Expand Down Expand Up @@ -1122,8 +1124,7 @@ vle_cmds_add_user(const char name[], const char body[], const char descr[],
cmd_t *cur = &inner->head;
while(cur->next != NULL && (cmp = strcmp(cur->next->name, name)) < 0)
{
int builtin_like = (cur->next->type == BUILTIN_CMD)
|| (cur->next->type == FOREIGN_CMD);
const int builtin_like = is_builtin_like_cmd(cur->next);
if(has_emark && builtin_like && cur->next->emark &&
strncmp(name, cur->next->name, len - 1) == 0)
{
Expand All @@ -1143,7 +1144,7 @@ vle_cmds_add_user(const char name[], const char body[], const char descr[],
if(cmp == 0)
{
cur = cur->next;
if(cur->type == BUILTIN_CMD || cur->type == FOREIGN_CMD)
if(is_builtin_like_cmd(cur))
return CMDS_ERR_NO_BUILTIN_REDEFINE;
if(!overwrite)
return CMDS_ERR_NEED_BANG;
Expand Down Expand Up @@ -1621,8 +1622,7 @@ vle_cmds_print_udcs(const char beginning[])
void *ptr;
size_t new_size;

if(strncmp(cur->name, beginning, len) != 0 ||
(cur->type != USER_CMD && cur->type != FOREIGN_CMD))
if(strncmp(cur->name, beginning, len) != 0 || !is_custom_cmd(cur))
{
cur = cur->next;
continue;
Expand Down Expand Up @@ -1668,6 +1668,22 @@ vle_cmds_print_udcs(const char beginning[])
return content;
}

/* Checks that a command has all the features of a builtin one. Returns
* non-zero if so. */
static int
is_builtin_like_cmd(const cmd_t *cmd)
{
return (cmd->type == BUILTIN_CMD || cmd->type == FOREIGN_CMD);
}

/* Checks that a command's definition is not part of the code base. Returns
* non-zero if so. */
static int
is_custom_cmd(const cmd_t *cmd)
{
return (cmd->type == USER_CMD || cmd->type == FOREIGN_CMD);
}

char *
vle_cmds_past_arg(const char args[])
{
Expand Down

0 comments on commit 93fe3be

Please sign in to comment.