Skip to content

Commit

Permalink
merge: trivial refactoring
Browse files Browse the repository at this point in the history
 * boolify execute_command()
 * refactor (no)spam parsing
 * hcache: const serialize functions
 * use mutt_perror()
 * use mutt_mem_realloc()
 * buffer: tidy
 * test: improve md5 coverage
 * test: improve coverage of memory functions
  • Loading branch information
flatcap committed Jun 10, 2023
2 parents 10e847b + 4cb07a6 commit 0bfc700
Show file tree
Hide file tree
Showing 51 changed files with 229 additions and 417 deletions.
2 changes: 1 addition & 1 deletion color/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ void get_colorid_name(unsigned int cid, struct Buffer *buf)

name = mutt_map_get_name(cid, ColorFields);
if (name)
buf_printf(buf, "%s", name);
buf_addstr(buf, name);
else
buf_printf(buf, "UNKNOWN %d", cid);
}
Expand Down
118 changes: 59 additions & 59 deletions commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ int source_rc(const char *rcfile_path, struct Buffer *err)
line_rc = parse_rc_buffer(linebuf, token, err);
if (line_rc == MUTT_CMD_ERROR)
{
mutt_error(_("Error in %s, line %d: %s"), rcfile, lineno, err->data);
mutt_error(_("Error in %s, line %d: %s"), rcfile, lineno, buf_string(err));
if (--rc < -MAX_ERRS)
{
if (conv)
Expand All @@ -278,7 +278,7 @@ int source_rc(const char *rcfile_path, struct Buffer *err)
else if (line_rc == MUTT_CMD_WARNING)
{
/* Warning */
mutt_warning(_("Warning in %s, line %d: %s"), rcfile, lineno, err->data);
mutt_warning(_("Warning in %s, line %d: %s"), rcfile, lineno, buf_string(err));
warnings++;
}
else if (line_rc == MUTT_CMD_FINISH)
Expand Down Expand Up @@ -539,7 +539,7 @@ static enum CommandResult parse_ifdef(struct Buffer *buf, struct Buffer *s,
enum CommandResult rc = parse_rc_line(buf->data, err);
if (rc == MUTT_CMD_ERROR)
{
mutt_error(_("Error: %s"), err->data);
mutt_error(_("Error: %s"), buf_string(err));
return MUTT_CMD_ERROR;
}
return rc;
Expand Down Expand Up @@ -957,79 +957,79 @@ static enum CommandResult parse_source(struct Buffer *buf, struct Buffer *s,
}

/**
* parse_spam_list - Parse the 'spam' and 'nospam' commands - Implements Command::parse() - @ingroup command_parse
* parse_nospam - Parse the 'nospam' command - Implements Command::parse() - @ingroup command_parse
*/
static enum CommandResult parse_spam_list(struct Buffer *buf, struct Buffer *s,
intptr_t data, struct Buffer *err)
static enum CommandResult parse_nospam(struct Buffer *buf, struct Buffer *s,
intptr_t data, struct Buffer *err)
{
struct Buffer templ;

buf_init(&templ);

/* Insist on at least one parameter */
if (!MoreArgs(s))
{
if (data == MUTT_SPAM)
buf_strcpy(err, _("spam: no matching pattern"));
else
buf_strcpy(err, _("nospam: no matching pattern"));
buf_printf(err, _("%s: too few arguments"), "nospam");
return MUTT_CMD_ERROR;
}

/* Extract the first token, a regex */
// Extract the first token, a regex or "*"
parse_extract_token(buf, s, TOKEN_NO_FLAGS);

/* data should be either MUTT_SPAM or MUTT_NOSPAM. MUTT_SPAM is for spam commands. */
if (data == MUTT_SPAM)
if (MoreArgs(s))
{
/* If there's a second parameter, it's a template for the spam tag. */
if (MoreArgs(s))
{
parse_extract_token(&templ, s, TOKEN_NO_FLAGS);

/* Add to the spam list. */
if (mutt_replacelist_add(&SpamList, buf->data, templ.data, err) != 0)
{
FREE(&templ.data);
return MUTT_CMD_ERROR;
}
FREE(&templ.data);
}
else
{
/* If not, try to remove from the nospam list. */
mutt_regexlist_remove(&NoSpamList, buf->data);
}
buf_printf(err, _("%s: too many arguments"), "finish");
return MUTT_CMD_ERROR;
}

// "*" is special - clear both spam and nospam lists
if (mutt_str_equal(buf_string(buf), "*"))
{
mutt_replacelist_free(&SpamList);
mutt_regexlist_free(&NoSpamList);
return MUTT_CMD_SUCCESS;
}
else if (data == MUTT_NOSPAM)

// If it's on the spam list, just remove it
if (mutt_replacelist_remove(&SpamList, buf_string(buf)) != 0)
return MUTT_CMD_SUCCESS;

// Otherwise, add it to the nospam list
if (mutt_regexlist_add(&NoSpamList, buf_string(buf), REG_ICASE, err) != 0)
return MUTT_CMD_ERROR;

return MUTT_CMD_SUCCESS;
}

/**
* parse_spam - Parse the 'spam' command - Implements Command::parse() - @ingroup command_parse
*/
static enum CommandResult parse_spam(struct Buffer *buf, struct Buffer *s,
intptr_t data, struct Buffer *err)
{
if (!MoreArgs(s))
{
/* MUTT_NOSPAM is for nospam commands. */
/* nospam only ever has one parameter. */
buf_printf(err, _("%s: too few arguments"), "spam");
return MUTT_CMD_ERROR;
}

/* "*" is a special case. */
if (mutt_str_equal(buf->data, "*"))
{
mutt_replacelist_free(&SpamList);
mutt_regexlist_free(&NoSpamList);
return MUTT_CMD_SUCCESS;
}
// Extract the first token, a regex
parse_extract_token(buf, s, TOKEN_NO_FLAGS);

/* If it's on the spam list, just remove it. */
if (mutt_replacelist_remove(&SpamList, buf->data) != 0)
return MUTT_CMD_SUCCESS;
// If there's a second parameter, it's a template for the spam tag
if (MoreArgs(s))
{
struct Buffer *templ = buf_pool_get();
parse_extract_token(templ, s, TOKEN_NO_FLAGS);

/* Otherwise, add it to the nospam list. */
if (mutt_regexlist_add(&NoSpamList, buf->data, REG_ICASE, err) != 0)
// Add to the spam list
int rc = mutt_replacelist_add(&SpamList, buf_string(buf), buf_string(templ), err);
buf_pool_release(&templ);
if (rc != 0)
return MUTT_CMD_ERROR;

return MUTT_CMD_SUCCESS;
}
else
{
// If not, try to remove from the nospam list
mutt_regexlist_remove(&NoSpamList, buf_string(buf));
}

/* This should not happen. */
buf_strcpy(err, "This is no good at all.");
return MUTT_CMD_ERROR;
return MUTT_CMD_SUCCESS;
}

/**
Expand Down Expand Up @@ -1109,7 +1109,7 @@ enum CommandResult parse_subscribe_to(struct Buffer *buf, struct Buffer *s,
return MUTT_CMD_WARNING;
}

if (buf->data && (*buf->data != '\0'))
if (!buf_is_empty(buf))
{
/* Expand and subscribe */
if (imap_subscribe(mutt_expand_path(buf->data, buf->dsize), true) == 0)
Expand Down Expand Up @@ -1541,14 +1541,14 @@ static const struct Command MuttCommands[] = {
{ "mono", mutt_parse_mono, 0 },
{ "my_hdr", parse_my_hdr, 0 },
{ "named-mailboxes", parse_mailboxes, MUTT_NAMED },
{ "nospam", parse_spam_list, MUTT_NOSPAM },
{ "nospam", parse_nospam, 0 },
{ "push", mutt_parse_push, 0 },
{ "reset", parse_set, MUTT_SET_RESET },
{ "score", mutt_parse_score, 0 },
{ "set", parse_set, MUTT_SET_SET },
{ "setenv", parse_setenv, MUTT_SET_SET },
{ "source", parse_source, 0 },
{ "spam", parse_spam_list, MUTT_SPAM },
{ "spam", parse_spam, 0 },
{ "subjectrx", parse_subjectrx_list, 0 },
{ "subscribe", parse_subscribe, 0 },
{ "tag-formats", parse_tag_formats, 0 },
Expand Down
28 changes: 12 additions & 16 deletions compmbox/compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,21 +318,21 @@ static void expand_command_str(const struct Mailbox *m, const char *cmd, char *b
* @param m Mailbox to work with
* @param command Command string to execute
* @param progress Message to show the user
* @retval 1 Success
* @retval 0 Failure
* @retval true Success
* @retval false Failure
*
* Run the supplied command, taking care of all the NeoMutt requirements,
* such as locking files and blocking signals.
*/
static int execute_command(struct Mailbox *m, const char *command, const char *progress)
static bool execute_command(struct Mailbox *m, const char *command, const char *progress)
{
if (!m || !command || !progress)
return 0;
return false;

if (m->verbose)
mutt_message(progress, m->realpath);

int rc = 1;
bool rc = true;
char sys_cmd[STR_COMMAND] = { 0 };

mutt_sig_block();
Expand All @@ -343,7 +343,7 @@ static int execute_command(struct Mailbox *m, const char *command, const char *p

if (mutt_system(sys_cmd) != 0)
{
rc = 0;
rc = false;
mutt_any_key_to_continue(NULL);
mutt_error(_("Error running \"%s\""), sys_cmd);
}
Expand Down Expand Up @@ -465,8 +465,7 @@ static enum MxOpenReturns comp_mbox_open(struct Mailbox *m)
goto cmo_fail;
}

int rc = execute_command(m, ci->cmd_open, _("Decompressing %s"));
if (rc == 0)
if (!execute_command(m, ci->cmd_open, _("Decompressing %s")))
goto cmo_fail;

unlock_realpath(m);
Expand Down Expand Up @@ -532,8 +531,7 @@ static bool comp_mbox_open_append(struct Mailbox *m, OpenMailboxFlags flags)
/* Open the existing mailbox, unless we are appending */
if (!ci->cmd_append && (mutt_file_get_size(m->realpath) > 0))
{
int rc = execute_command(m, ci->cmd_open, _("Decompressing %s"));
if (rc == 0)
if (!execute_command(m, ci->cmd_open, _("Decompressing %s")))
{
mutt_error(_("Compress command failed: %s"), ci->cmd_open);
goto cmoa_fail2;
Expand Down Expand Up @@ -605,10 +603,10 @@ static enum MxStatus comp_mbox_check(struct Mailbox *m)
return MX_STATUS_ERROR;
}

int rc = execute_command(m, ci->cmd_open, _("Decompressing %s"));
bool rc = execute_command(m, ci->cmd_open, _("Decompressing %s"));
store_size(m);
unlock_realpath(m);
if (rc == 0)
if (!rc)
return MX_STATUS_ERROR;

return ops->mbox_check(m);
Expand Down Expand Up @@ -651,8 +649,7 @@ static enum MxStatus comp_mbox_sync(struct Mailbox *m)
if (check != MX_STATUS_OK)
goto sync_cleanup;

int rc = execute_command(m, ci->cmd_close, _("Compressing %s"));
if (rc == 0)
if (!execute_command(m, ci->cmd_close, _("Compressing %s")))
{
check = MX_STATUS_ERROR;
goto sync_cleanup;
Expand Down Expand Up @@ -706,8 +703,7 @@ static enum MxStatus comp_mbox_close(struct Mailbox *m)
msg = _("Compressing %s");
}

int rc = execute_command(m, append, msg);
if (rc == 0)
if (!execute_command(m, append, msg))
{
mutt_any_key_to_continue(NULL);
mutt_error(_("Error. Preserving temporary file: %s"), mailbox_path(m));
Expand Down
2 changes: 1 addition & 1 deletion email/url.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ int url_tobuffer(struct Url *url, struct Buffer *buf, uint8_t flags)
if (strchr(url->host, ':'))
buf_add_printf(buf, "[%s]", url->host);
else
buf_add_printf(buf, "%s", url->host);
buf_addstr(buf, url->host);

if (url->port)
buf_add_printf(buf, ":%hu/", url->port);
Expand Down
24 changes: 13 additions & 11 deletions hcache/serialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void lazy_realloc(void *ptr, size_t size)
* @param[in,out] off Offset into the blob
* @retval ptr End of the newly packed binary
*/
unsigned char *serial_dump_int(unsigned int i, unsigned char *d, int *off)
unsigned char *serial_dump_int(const unsigned int i, unsigned char *d, int *off)
{
lazy_realloc(&d, *off + sizeof(int));
memcpy(d + *off, &i, sizeof(int));
Expand All @@ -81,7 +81,7 @@ unsigned char *serial_dump_int(unsigned int i, unsigned char *d, int *off)
* @param[in,out] off Offset into the blob
* @retval ptr End of the newly packed binary
*/
unsigned char *serial_dump_uint32_t(uint32_t s, unsigned char *d, int *off)
unsigned char *serial_dump_uint32_t(const uint32_t s, unsigned char *d, int *off)
{
lazy_realloc(&d, *off + sizeof(uint32_t));
memcpy(d + *off, &s, sizeof(uint32_t));
Expand Down Expand Up @@ -212,8 +212,8 @@ void serial_restore_char(char **c, const unsigned char *d, int *off, bool conver
* @param[in] convert If true, the strings will be converted to utf-8
* @retval ptr End of the newly packed binary
*/
unsigned char *serial_dump_address(struct AddressList *al, unsigned char *d,
int *off, bool convert)
unsigned char *serial_dump_address(const struct AddressList *al,
unsigned char *d, int *off, bool convert)
{
unsigned int counter = 0;
unsigned int start_off = *off;
Expand Down Expand Up @@ -282,7 +282,8 @@ void serial_restore_address(struct AddressList *al, const unsigned char *d,
* @param[in] convert If true, the strings will be converted to utf-8
* @retval ptr End of the newly packed binary
*/
unsigned char *serial_dump_stailq(struct ListHead *l, unsigned char *d, int *off, bool convert)
unsigned char *serial_dump_stailq(const struct ListHead *l, unsigned char *d,
int *off, bool convert)
{
unsigned int counter = 0;
unsigned int start_off = *off;
Expand Down Expand Up @@ -331,7 +332,8 @@ void serial_restore_stailq(struct ListHead *l, const unsigned char *d, int *off,
* @param[in] convert If true, the strings will be converted to utf-8
* @retval ptr End of the newly packed binary
*/
unsigned char *serial_dump_buffer(struct Buffer *buf, unsigned char *d, int *off, bool convert)
unsigned char *serial_dump_buffer(const struct Buffer *buf, unsigned char *d,
int *off, bool convert)
{
if (!buf)
{
Expand Down Expand Up @@ -380,8 +382,8 @@ void serial_restore_buffer(struct Buffer *buf, const unsigned char *d, int *off,
* @param[in] convert If true, the strings will be converted to utf-8
* @retval ptr End of the newly packed binary
*/
unsigned char *serial_dump_parameter(struct ParameterList *pl, unsigned char *d,
int *off, bool convert)
unsigned char *serial_dump_parameter(const struct ParameterList *pl,
unsigned char *d, int *off, bool convert)
{
unsigned int counter = 0;
unsigned int start_off = *off;
Expand Down Expand Up @@ -434,7 +436,7 @@ void serial_restore_parameter(struct ParameterList *pl, const unsigned char *d,
* @param[in] convert If true, the strings will be converted to utf-8
* @retval ptr End of the newly packed binary
*/
unsigned char *serial_dump_body(struct Body *c, unsigned char *d, int *off, bool convert)
unsigned char *serial_dump_body(const struct Body *c, unsigned char *d, int *off, bool convert)
{
struct Body nb;

Expand Down Expand Up @@ -500,8 +502,8 @@ void serial_restore_body(struct Body *c, const unsigned char *d, int *off, bool
* @param[in] convert If true, the strings will be converted to utf-8
* @retval ptr End of the newly packed binary
*/
unsigned char *serial_dump_envelope(struct Envelope *env, unsigned char *d,
int *off, bool convert)
unsigned char *serial_dump_envelope(const struct Envelope *env,
unsigned char *d, int *off, bool convert)
{
d = serial_dump_address(&env->return_path, d, off, convert);
d = serial_dump_address(&env->from, d, off, convert);
Expand Down
Loading

0 comments on commit 0bfc700

Please sign in to comment.