Skip to content

Commit

Permalink
Add str compat functions and use existing wrappers
Browse files Browse the repository at this point in the history
Provide strsep and strcasestr implementations and use the existing
wrappers instead of direct calls to these functions.

strsep function is wrapped as mutt_str_sep.
  • Loading branch information
pekdon authored and flatcap committed Feb 9, 2022
1 parent 5bf26d7 commit b62d08c
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 13 deletions.
1 change: 1 addition & 0 deletions auto.def
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ if {1} {
mkdtemp \
qsort_s \
strsep \
strcasestr \
tcgetwinsize \
utimesnsat \
vasprintf \
Expand Down
2 changes: 1 addition & 1 deletion keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1364,7 +1364,7 @@ static void *parse_menu(bool *menus, char *s, struct Buffer *err)
char *marker = menu_names_dup;
char *menu_name = NULL;

while ((menu_name = strsep(&marker, ",")))
while ((menu_name = mutt_str_sep(&marker, ",")))
{
int value = mutt_map_get_value(menu_name, MenuNames);
if (value == -1)
Expand Down
2 changes: 1 addition & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ static void log_translation(void)
{
const char *header = ""; // Do not merge these two lines
header = _(header); // otherwise the .po files will end up badly ordered
const char *lang = strcasestr(header, "Language:");
const char *lang = mutt_istr_find(header, "Language:");
int len = 64;
if (lang)
{
Expand Down
69 changes: 69 additions & 0 deletions mutt/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,62 @@
#include <sysexits.h>
#endif

#ifndef HAVE_STRCASESTR
/**
* strcasestr - Find the first occurrence of needle in haystack, ignoring case
* @param haystack String to search
* @param needle String to find
* @retval ptr Matched string, or NULL on failure
*/
static char *strcasestr(const char *haystack, const char *needle)
{
size_t haystackn = strlen(haystack);
size_t needlen = strlen(needle);

const char *p = haystack;
while (haystackn >= needlen)
{
if (strncasecmp(p, needle, needlen) == 0)
return (char *) p;
p++;
haystackn--;
}
return NULL;
}
#endif /* HAVE_STRCASESTR */

#ifndef HAVE_STRSEP
/**
* strsep - Extract a token from a string
* @param stringp String to be split up
* @param delim Characters to split stringp at
* @retval ptr Next token, or NULL if the no more tokens
*
* @note The pointer stringp will be moved and NULs inserted into it
*/
static char *strsep(char **stringp, const char *delim)
{
if (*stringp == NULL)
return NULL;

char *start = *stringp;
for (char *p = *stringp; *p != '\0'; p++)
{
for (const char *s = delim; *s != '\0'; s++)
{
if (*p == *s)
{
*p = '\0';
*stringp = p + 1;
return start;
}
}
}
*stringp = NULL;
return start;
}
#endif /* HAVE_STRSEP */

/**
* struct SysExits - Lookup table of error messages
*/
Expand Down Expand Up @@ -118,6 +174,19 @@ const char *mutt_str_sysexit(int err_num)
return NULL;
}

/**
* mutt_str_sep - Find first occurance of any of delim characters in *stringp
* @param stringp Pointer to string to search for delim, updated with position of after delim if found else NULL
* @param delim String with characters to search for in *stringp
* @retval ptr Input value of *stringp
*/
char *mutt_str_sep(char **stringp, const char *delim)
{
if (!stringp || !*stringp || !delim)
return NULL;
return strsep(stringp, delim);
}

/**
* startswith - Check whether a string starts with a prefix
* @param str String to check
Expand Down
1 change: 1 addition & 0 deletions mutt/string2.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ size_t mutt_str_lws_rlen(const char *s, size_t n);
const char *mutt_str_next_word(const char *s);
void mutt_str_remove_trailing_ws(char *s);
char * mutt_str_replace(char **p, const char *s);
char * mutt_str_sep(char **stringp, const char *delim);
char * mutt_str_skip_email_wsp(const char *s);
char * mutt_str_skip_whitespace(const char *p);
const char *mutt_str_sysexit(int e);
Expand Down
4 changes: 2 additions & 2 deletions muttlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1518,11 +1518,11 @@ int mutt_set_xdg_path(enum XdgType type, struct Buffer *buf)
{
const char *xdg_env = mutt_str_getenv(xdg_env_vars[type]);
char *xdg = xdg_env ? mutt_str_dup(xdg_env) : mutt_str_dup(xdg_defaults[type]);
char *x = xdg; /* strsep() changes xdg, so free x instead later */
char *x = xdg; /* mutt_str_sep() changes xdg, so free x instead later */
char *token = NULL;
int rc = 0;

while ((token = strsep(&xdg, ":")))
while ((token = mutt_str_sep(&xdg, ":")))
{
if (mutt_buffer_printf(buf, "%s/%s/neomuttrc", token, PACKAGE) < 0)
continue;
Expand Down
2 changes: 1 addition & 1 deletion ncrypt/crypt_gpgme.c
Original file line number Diff line number Diff line change
Expand Up @@ -4425,7 +4425,7 @@ static bool verify_sender(struct Email *e)
tmp_email += mailbox_length;
tmp_sender += mailbox_length;
domainname_match =
(strncasecmp(tmp_email, tmp_sender, domainname_length) == 0);
(mutt_istrn_cmp(tmp_email, tmp_sender, domainname_length) == 0);
if (mailbox_match && domainname_match)
rc = false;
}
Expand Down
8 changes: 3 additions & 5 deletions nntp/nntp.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,10 @@ static int nntp_capabilities(struct NntpAccountData *adata)
} while (!mutt_str_equal(".", buf));
*buf = '\0';
#ifdef USE_SASL
if (adata->authenticators && strcasestr(authinfo, " SASL "))
if (adata->authenticators && mutt_istr_find(authinfo, " SASL "))
mutt_str_copy(buf, adata->authenticators, sizeof(buf));
#endif
if (strcasestr(authinfo, " USER "))
if (mutt_istr_find(authinfo, " USER "))
{
if (*buf != '\0')
mutt_str_cat(buf, sizeof(buf), " ");
Expand Down Expand Up @@ -482,11 +482,9 @@ static int nntp_auth(struct NntpAccountData *adata)
/* check authenticator */
if (adata->hasCAPABILITIES)
{
char *m = NULL;

if (!adata->authenticators)
continue;
m = strcasestr(adata->authenticators, method);
const char *m = mutt_istr_find(adata->authenticators, method);
if (!m)
continue;
if ((m > adata->authenticators) && (*(m - 1) != ' '))
Expand Down
2 changes: 1 addition & 1 deletion pattern/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static bool patmatch(const struct Pattern *pat, const char *buf)
if (pat->is_multi)
return (mutt_list_find(&pat->p.multi_cases, buf) != NULL);
if (pat->string_match)
return pat->ign_case ? strcasestr(buf, pat->p.str) : strstr(buf, pat->p.str);
return pat->ign_case ? mutt_istr_find(buf, pat->p.str) : strstr(buf, pat->p.str);
if (pat->group_match)
return mutt_group_match(pat->p.group, buf);
return (regexec(pat->p.regex, buf, 0, NULL, 0) == 0);
Expand Down
2 changes: 1 addition & 1 deletion rfc3676.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ static void print_flowed_line(char *line, struct State *s, int ql,
mutt_debug(LL_DEBUG5, "f=f: line [%s], width = %ld, spaces = %lu\n", line,
(long) width, fst->spaces);

for (words = 0; (p = strsep(&line, " "));)
for (words = 0; (p = mutt_str_sep(&line, " "));)
{
mutt_debug(LL_DEBUG5, "f=f: word [%s], width: %lu, remaining = [%s]\n", p,
fst->width, line);
Expand Down
2 changes: 1 addition & 1 deletion test/parse/mutt_parse_mailto.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static void check_addrlist(struct AddressList *list, const char *const exp[], si
char *pp = parsed;
for (size_t i = 0; i < num; ++i)
{
char *tok = mutt_str_skip_whitespace(strsep(&pp, ","));
char *tok = mutt_str_skip_whitespace(mutt_str_sep(&pp, ","));
if (!TEST_CHECK(mutt_str_equal(tok, exp[i])))
{
TEST_MSG("Expected: %s", exp[i]);
Expand Down

0 comments on commit b62d08c

Please sign in to comment.