From 93b012884de4ca5e1f2550f767d0b8680b9c0e9f Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Sat, 4 Nov 2006 16:26:20 +0100 Subject: [PATCH] drop the old string API fully. Signed-off-by: Pierre Habouzit --- buffy.c | 8 ++--- compose.c | 8 ++--- crypt-gpgme.c | 2 +- headers.c | 2 +- imap/imap.c | 4 +-- init.c | 2 +- lib-lib/buffer.c | 2 +- lib-lib/file.c | 1 - lib-lib/str.c | 47 ++++++++++++++++++++++++++ lib-lib/str.h | 27 +++++++++++++-- lib/Makefile.am | 6 ++-- lib/str.c | 87 ------------------------------------------------ lib/str.h | 25 -------------- muttlib.c | 4 +-- nntp/newsrc.c | 2 +- nntp/nntp.c | 6 ++-- parse.c | 10 +++--- pattern.c | 2 +- pgpkey.c | 2 +- pgppubring.c | 2 +- pop/pop_lib.c | 2 +- rfc1524.c | 2 +- sendlib.c | 6 ++-- sidebar.c | 4 +-- smime.c | 2 +- 25 files changed, 110 insertions(+), 155 deletions(-) delete mode 100644 lib/str.c delete mode 100644 lib/str.h diff --git a/buffy.c b/buffy.c index ff316cc..f2d4887 100644 --- a/buffy.c +++ b/buffy.c @@ -180,7 +180,7 @@ int buffy_lookup (const char* path) { if (list_empty(Incoming) || !path || !*path) return (-1); for (i = 0; i < Incoming->length; i++) { - if (str_eq (((BUFFY*) Incoming->data[i])->path, path) ) + if (!m_strcmp(((BUFFY*)Incoming->data[i])->path, path) ) return (i); } return (-1); @@ -200,7 +200,7 @@ int buffy_parse_mailboxes (BUFFER * path, BUFFER * s, unsigned long data, mutt_extract_token (path, s, 0); m_strcpy(buf, sizeof(buf), path->data); - if (data == M_UNMAILBOXES && str_eq (buf, "*")) { + if (data == M_UNMAILBOXES && !strcmp(buf, "*")) { list_del (&Incoming, (list_del_t*) buffy_free); return 0; } @@ -334,7 +334,7 @@ int buffy_check (int force) * before polling */ if (!Context || !Context->path || (local ? (sb.st_dev != contex_sb.st_dev || sb.st_ino != contex_sb.st_ino) : - !str_eq (tmp->path, Context->path))) { + !m_strcmp(tmp->path, Context->path))) { switch (tmp->magic) { case M_MBOX: case M_MMDF: @@ -591,7 +591,7 @@ void buffy_next (char *s, size_t slen) i = 1 + buffy_lookup (s); for (l=0; l < Incoming->length; l++) { c = (l+i) % Incoming->length; - if ((!Context || !Context->path || !str_eq (((BUFFY*) Incoming->data[c])->path, Context->path)) && + if ((!Context || !Context->path || m_strcmp(((BUFFY*) Incoming->data[c])->path, Context->path)) && ((BUFFY*) Incoming->data[c])->new > 0) break; } diff --git a/compose.c b/compose.c index 508c151..09acea2 100644 --- a/compose.c +++ b/compose.c @@ -623,8 +623,8 @@ int mutt_compose_menu (HEADER * msg, /* structure for new message */ if (mutt_get_field ("Newsgroups: ", buf, sizeof (buf), 0) == 0 && buf[0]) { p_delete(&msg->env->newsgroups); - str_skip_trailws (buf); - msg->env->newsgroups = m_strdup(vskipspaces(buf)); + m_strrtrim(buf); + msg->env->newsgroups = m_strdup(skipspaces(buf)); move (HDR_TO, HDR_XOFFSET); clrtoeol (); if (msg->env->newsgroups) @@ -641,8 +641,8 @@ int mutt_compose_menu (HEADER * msg, /* structure for new message */ if (mutt_get_field ("Followup-To: ", buf, sizeof (buf), 0) == 0 && buf[0]) { p_delete(&msg->env->followup_to); - str_skip_trailws (buf); - msg->env->followup_to = m_strdup(vskipspaces(buf)); + m_strrtrim(buf); + msg->env->followup_to = m_strdup(skipspaces(buf)); move (HDR_CC, HDR_XOFFSET); clrtoeol (); if (msg->env->followup_to) diff --git a/crypt-gpgme.c b/crypt-gpgme.c index 4255156..a3dedae 100644 --- a/crypt-gpgme.c +++ b/crypt-gpgme.c @@ -3576,7 +3576,7 @@ static crypt_key_t *crypt_getkeybystr (char *p, short abilities, || (option (OPTPGPLONGIDS) && !m_strncasecmp(p, "0x", 2) && !m_strcasecmp(p + 2, crypt_keyid (k) + 8)) - || str_isstr (k->uid, p)) { + || m_stristr(k->uid, p)) { crypt_key_t *tmp; debug_print (5, ("match.\n")); diff --git a/headers.c b/headers.c index a3476a8..a9be3cd 100644 --- a/headers.c +++ b/headers.c @@ -144,7 +144,7 @@ void mutt_edit_headers (const char *editor, p = vskipspaces(cur->data + 7); if (*p) { if ((q = strpbrk (p, " \t"))) { - str_substrcpy(path, p, q, sizeof(path)); + m_strncpy(path, sizeof(path), p, q - p); q = vskipspaces(q); } else diff --git a/imap/imap.c b/imap/imap.c index 4b004f1..57648c6 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -904,7 +904,7 @@ int imap_sync_message (IMAP_DATA *idata, HEADER *hdr, BUFFER *cmd, if (mutt_bit_isset (idata->rights, ACL_WRITE)) imap_add_keywords (flags, hdr, idata->flags, sizeof (flags)); - str_skip_trailws (flags); + m_strrtrim(flags); /* UW-IMAP is OK with null flags, Cyrus isn't. The only solution is to * explicitly revoke all system flags (if we have permission) */ @@ -915,7 +915,7 @@ int imap_sync_message (IMAP_DATA *idata, HEADER *hdr, BUFFER *cmd, imap_set_flag (idata, ACL_WRITE, 1, "\\Answered ", flags, sizeof (flags)); imap_set_flag (idata, ACL_DELETE, 1, "\\Deleted ", flags, sizeof (flags)); - str_skip_trailws (flags); + m_strrtrim(flags); mutt_buffer_addstr (cmd, " -FLAGS.SILENT ("); } else diff --git a/init.c b/init.c index 624ef3b..fdaf0c9 100644 --- a/init.c +++ b/init.c @@ -2617,7 +2617,7 @@ void mutt_init (int skip_sys_rc, LIST * commands) uname (&utsname); /* some systems report the FQDN instead of just the hostname */ if ((p = strchr (utsname.nodename, '.'))) { - Hostname = str_substrdup (utsname.nodename, p); + Hostname = p_dupstr(utsname.nodename, p - utsname.nodename); p++; m_strcpy(buffer, sizeof(buffer), p); /* save the domain for below */ } diff --git a/lib-lib/buffer.c b/lib-lib/buffer.c index 6b46380..47e58ba 100644 --- a/lib-lib/buffer.c +++ b/lib-lib/buffer.c @@ -259,7 +259,7 @@ int mutt_extract_token(BUFFER *dest, BUFFER *tok, int flags) if (*tok->dptr == '{') { tok->dptr++; if ((pc = strchr (tok->dptr, '}'))) { - var = str_substrdup (tok->dptr, pc); + var = p_dupstr(tok->dptr, pc - tok->dptr); tok->dptr = pc + 1; } } else { diff --git a/lib-lib/file.c b/lib-lib/file.c index 6442667..36d34a3 100644 --- a/lib-lib/file.c +++ b/lib-lib/file.c @@ -39,7 +39,6 @@ #include "file.h" #include "../lib/debug.h" -#include "../lib/str.h" #ifndef O_NOFOLLOW # define O_NOFOLLOW 0 diff --git a/lib-lib/str.c b/lib-lib/str.c index 3b9b5b5..a5333e4 100644 --- a/lib-lib/str.c +++ b/lib-lib/str.c @@ -94,3 +94,50 @@ ssize_t m_strncpy(char *dst, ssize_t n, const char *src, ssize_t l) return len; } + +char *m_strrtrim(char *s) +{ + if (s) { + char *p = s + m_strlen(s); + + while (p > s && ISSPACE(p[-1])) { + *--p = '\0'; + } + return p; + } + + return NULL; +} + +const char *m_stristrn(const char *haystack, const char *needle, ssize_t nlen) +{ + int nc; + + if (!nlen) + return haystack; + + nc = tolower(*needle); + for (;;) { + int c = tolower(*haystack); + + if (c != nc) { + if (c == '\0') + return NULL; + } else { + ssize_t i; + + /* compare the rest of needle */ + for (i = 1;; i++) { + if (i == nlen) + return haystack; + if (c == '\0') + return NULL; + c = tolower(haystack[i]); + if (c != tolower(needle[i])) + break; + } + } + + haystack++; + } +} diff --git a/lib-lib/str.h b/lib-lib/str.h index 55a89b9..bb2d822 100644 --- a/lib-lib/str.h +++ b/lib-lib/str.h @@ -23,8 +23,6 @@ #include #include -#include "../lib/str.h" - #include "mem.h" #define HUGE_STRING 5120 @@ -43,7 +41,7 @@ extern char const __m_b36chars_lower[36]; extern char const __m_b36chars_upper[36]; /****************************************************************************/ -/* char related */ +/* conversions */ /****************************************************************************/ static inline int hexval(int c) { @@ -54,6 +52,11 @@ static inline int base64val(int c) { return (c & ~127) ? -1 : __m_b64digits[c]; } +static inline void m_strtolower(char *p) { + for (; *p; p++) + *p = tolower((unsigned char)*p); +} + /****************************************************************************/ /* length related */ /****************************************************************************/ @@ -100,6 +103,10 @@ static inline char *m_strdup(const char *s) { return len ? p_dup(s, len + 1) : NULL; } +static inline char *m_substrdup(const char *s, const char *end) { + return p_dupstr(s, end ? end - s : m_strlen(s)); +} + static inline char *m_strreplace(char **p, const char *s) { p_delete(p); return (*p = m_strdup(s)); @@ -132,4 +139,18 @@ static inline char *vskipspaces(const char *s) { return (char *)skipspaces(s); } +char *m_strrtrim(char *s); + +/****************************************************************************/ +/* search */ +/****************************************************************************/ + +const char * +m_stristrn(const char *haystack, const char *needle, ssize_t nlen); + +static inline const char * +m_stristr(const char *haystack, const char *needle) { + return m_stristrn(haystack, needle, m_strlen(needle)); +} + #endif /* MUTT_LIB_LIB_STR_H */ diff --git a/lib/Makefile.am b/lib/Makefile.am index 24b3676..775d21e 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -5,9 +5,9 @@ AR=@AR@ AUTOMAKE_OPTIONS = foreign noinst_LIBRARIES = libsane.a -noinst_HEADERS = str.h list.h rx.h debug.h +noinst_HEADERS = list.h rx.h debug.h -libsane_a_SOURCES = str.c list.c rx.h debug.h \ - str.h list.h rx.c debug.c +libsane_a_SOURCES = list.c rx.h debug.h \ + list.h rx.c debug.c -include ../cflags.mk diff --git a/lib/str.c b/lib/str.c deleted file mode 100644 index 3d3f10e..0000000 --- a/lib/str.c +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright notice from original mutt: - * Copyright (C) 1996-2000 Michael R. Elkins - * Copyright (C) 1999-2000 Thomas Roessler - * - * This file is part of mutt-ng, see http://www.muttng.org/. - * It's licensed under the GNU General Public License, - * please see the file GPL in the top level source directory. - */ - -#include -#include -#include - -#include -#include - -#include "str.h" - -/* convert all characters in the string to lowercase */ -char *str_tolower (char *s) -{ - char *p = s; - - while (*p) { - *p = tolower ((unsigned char) *p); - p++; - } - - return (s); -} - -/* NULL-pointer aware string comparison functions */ - -char *str_substrcpy (char *dest, const char *beg, const char *end, - size_t destlen) -{ - size_t len; - - len = end - beg; - if (len > destlen - 1) - len = destlen - 1; - memcpy (dest, beg, len); - dest[len] = 0; - return dest; -} - -char *str_substrdup(const char *begin, const char *end) -{ - return p_dupstr(begin, (end ? end - begin : strlen(begin))); -} - -const char *str_isstr (const char *haystack, const char *needle) -{ - const char *p, *q; - - if (!haystack) - return NULL; - if (!needle) - return (haystack); - - while (*(p = haystack)) { - for (q = needle; - *p && *q && - tolower ((unsigned char) *p) == tolower ((unsigned char) *q); - p++, q++); - if (!*q) - return (haystack); - haystack++; - } - return NULL; -} - -int str_eq (const char* s1, const char* s2) { - int l = m_strlen(s1); - - if (l != m_strlen(s2)) - return (0); - return (m_strncmp(s1, s2, l) == 0); -} - -void str_skip_trailws (char *s) { - char *p; - - for (p = s + m_strlen(s) - 1; p >= s && ISSPACE (*p); p--) - *p = 0; -} diff --git a/lib/str.h b/lib/str.h deleted file mode 100644 index b5bf82f..0000000 --- a/lib/str.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright notice from original mutt: - * Copyright (C) 1996-2000 Michael R. Elkins - * Copyright (C) 1999-2000 Thomas Roessler - * - * This file is part of mutt-ng, see http://www.muttng.org/. - * It's licensed under the GNU General Public License, - * please see the file GPL in the top level source directory. - */ -#ifndef _LIB_STR_H -#define _LIB_STR_H - -#include - -/* - * tools - */ -char *str_tolower (char*); -char *str_substrcpy (char*, const char*, const char*, size_t); -char *str_substrdup (const char*, const char*); -int str_eq (const char*, const char*); -const char *str_isstr (const char*, const char*); -void str_skip_trailws (char*); - -#endif /* !_LIB_STR_H */ diff --git a/muttlib.c b/muttlib.c index 65705ca..73b2e5e 100644 --- a/muttlib.c +++ b/muttlib.c @@ -889,7 +889,7 @@ void mutt_save_path (char *d, size_t dsize, address_t * a) if ((p = strpbrk (d, "%@"))) *p = 0; } - str_tolower (d); + m_strtolower(d); } else *d = 0; @@ -1070,7 +1070,7 @@ void mutt_FormatString (char *dest, /* output buffer */ data, flags); if (lower) - str_tolower (buf); + m_strtolower(buf); if (nodots) { char *p = buf; diff --git a/nntp/newsrc.c b/nntp/newsrc.c index 94929bd..fe774ad 100644 --- a/nntp/newsrc.c +++ b/nntp/newsrc.c @@ -265,7 +265,7 @@ const char *nntp_format_str (char *dest, size_t destlen, char op, switch (op) { case 's': m_strcpy(fn, sizeof (fn), NewsServer); - str_tolower (fn); + m_strtolower(fn); snprintf (tmp, sizeof (tmp), "%%%ss", fmt); snprintf (dest, destlen, tmp, fn); break; diff --git a/nntp/nntp.c b/nntp/nntp.c index 2559221..34d7746 100644 --- a/nntp/nntp.c +++ b/nntp/nntp.c @@ -159,9 +159,9 @@ static int nntp_connect_and_auth (NNTP_SERVER * serv) else if (!m_strncmp("201", buf, 3)) mutt_message (_("Connected to %s. Posting NOT ok."), conn->account.host); else { - mutt_socket_close (conn); - str_skip_trailws (buf); - mutt_error ("%s", buf); + mutt_socket_close(conn); + m_strrtrim(buf); + mutt_error("%s", buf); sleep (2); return -1; } diff --git a/parse.c b/parse.c index eae5511..9c1195d 100644 --- a/parse.c +++ b/parse.c @@ -1034,8 +1034,8 @@ int mutt_parse_rfc822_line (ENVELOPE * e, HEADER * hdr, char *line, char *p, #ifdef USE_NNTP else if (!m_strcasecmp(line + 1, "ollowup-to")) { if (!e->followup_to) { - str_skip_trailws (p); - e->followup_to = m_strdup(vskipspaces(p)); + m_strrtrim(p); + e->followup_to = m_strdup(skipspaces(p)); } matched = 1; } @@ -1078,7 +1078,7 @@ int mutt_parse_rfc822_line (ENVELOPE * e, HEADER * hdr, char *line, char *p, /* Take the first mailto URL */ if (url_check_scheme (beg) == U_MAILTO) { p_delete(&e->list_post); - e->list_post = str_substrdup (beg, end); + e->list_post = p_dupstr(beg, end - beg); break; } } @@ -1117,8 +1117,8 @@ int mutt_parse_rfc822_line (ENVELOPE * e, HEADER * hdr, char *line, char *p, case 'n': if (!m_strcasecmp(line + 1, "ewsgroups")) { p_delete(&e->newsgroups); - str_skip_trailws (p); - e->newsgroups = m_strdup(vskipspaces(p)); + m_strrtrim(p); + e->newsgroups = m_strdup(skipspaces(p)); matched = 1; } break; diff --git a/pattern.c b/pattern.c index 66d8290..d388924 100644 --- a/pattern.c +++ b/pattern.c @@ -816,7 +816,7 @@ pattern_t *mutt_pattern_comp ( /* const */ char *s, int flags, BUFFER * err) return NULL; } /* compile the sub-expression */ - buf = str_substrdup (ps.dptr + 1, p); + buf = p_dupstr(ps.dptr + 1, p - ps.dptr - 1); if ((tmp = mutt_pattern_comp (buf, flags, err)) == NULL) { p_delete(&buf); mutt_pattern_free (&curlist); diff --git a/pgpkey.c b/pgpkey.c index e289083..ecd789e 100644 --- a/pgpkey.c +++ b/pgpkey.c @@ -939,7 +939,7 @@ pgp_key_t pgp_getkeybystr (char *p, short abilities, pgp_ring_t keyring) && !m_strcasecmp(p + 2, pgp_keyid (k))) || (option (OPTPGPLONGIDS) && !m_strncasecmp(p, "0x", 2) && !m_strcasecmp(p + 2, k->keyid + 8)) - || str_isstr (a->addr, p)) { + || m_stristr(a->addr, p)) { debug_print (5, ("match.\n")); match = 1; break; diff --git a/pgppubring.c b/pgppubring.c index da1e4cd..bb27da1 100644 --- a/pgppubring.c +++ b/pgppubring.c @@ -706,7 +706,7 @@ static int pgpring_string_matches_hint (const char *s, const char *hints[], return 1; for (i = 0; i < nhints; i++) { - if (str_isstr (s, hints[i]) != NULL) + if (m_stristr(s, hints[i]) != NULL) return 1; } diff --git a/pop/pop_lib.c b/pop/pop_lib.c index e34165b..fae3c9b 100644 --- a/pop/pop_lib.c +++ b/pop/pop_lib.c @@ -74,7 +74,7 @@ void pop_error (POP_DATA * pop_data, char *msg) } m_strcpy(t, sizeof(pop_data->err_msg) - strlen(pop_data->err_msg), c); - str_skip_trailws (pop_data->err_msg); + m_strrtrim(pop_data->err_msg); } /* Parse CAPA output */ diff --git a/rfc1524.c b/rfc1524.c index 3cea0a8..9b745b4 100644 --- a/rfc1524.c +++ b/rfc1524.c @@ -132,7 +132,7 @@ static char *get_field (char *s) break; } } - str_skip_trailws (s); + m_strrtrim(s); return ch; } diff --git a/sendlib.c b/sendlib.c index 1762394..761054b 100644 --- a/sendlib.c +++ b/sendlib.c @@ -744,7 +744,7 @@ static size_t convert_file_from_to (FILE * file, for (c = tocodes, i = 0; c; c = c1 ? c1 + 1 : 0, i++) { if ((c1 = strchr (c, ':')) == c) continue; - tcode[i] = str_substrdup (c, c1); + tcode[i] = m_substrdup(c, c1); } ret = (size_t) (-1); @@ -753,7 +753,7 @@ static size_t convert_file_from_to (FILE * file, for (c = fromcodes; c; c = c1 ? c1 + 1 : 0) { if ((c1 = strchr (c, ':')) == c) continue; - fcode = str_substrdup (c, c1); + fcode = m_substrdup(c, c1); ret = convert_file_to (file, fcode, ncodes, (const char **) tcode, &cn, info); @@ -937,7 +937,7 @@ int mutt_lookup_mime_type (BODY * att, const char *path) for (q = p; *q && !ISSPACE (*q); q++); - str_substrcpy (subtype, p, q, sizeof (subtype)); + m_strncpy(subtype, sizeof(subtype), p, q - p); if ((type = mutt_check_mime_type (ct)) == TYPEOTHER) m_strcpy(xtype, sizeof(xtype), ct); diff --git a/sidebar.c b/sidebar.c index ab95a22..5ce1922 100644 --- a/sidebar.c +++ b/sidebar.c @@ -102,7 +102,7 @@ static const char* sidebar_number_format (char* dest, size_t destlen, char op, char tmp[SHORT_STRING]; BUFFY* b = (BUFFY*) Incoming->data[data]; int opt = flags & M_FORMAT_OPTIONAL; - int c = Context && str_eq (Context->path, b->path); + int c = Context && !m_strcmp(Context->path, b->path); switch (op) { /* deleted */ @@ -199,7 +199,7 @@ int make_sidebar_entry (char* box, int idx, size_t len) SidebarWidth = COLS; if (option (OPTSIDEBARNEWMAILONLY) && box && Context && Context->path && - !str_eq (Context->path, box) && ((BUFFY*) Incoming->data[idx])->new == 0) + m_strcmp(Context->path, box) && ((BUFFY*) Incoming->data[idx])->new == 0) /* if $sidebar_newmail_only is set, don't display the * box only if it's not the currently opened * (i.e. always display the currently opened) */ diff --git a/smime.c b/smime.c index 3db7185..804ba95 100644 --- a/smime.c +++ b/smime.c @@ -414,7 +414,7 @@ char *smime_ask_for_key (char *prompt, char *mailbox, short public) continue; /* Check if query matches this certificate */ - if (!str_isstr (fields[0], qry) && !str_isstr (fields[2], qry)) + if (!m_stristr(fields[0], qry) && !m_stristr(fields[2], qry)) continue; Table[cur].hash = hash; -- 2.20.1