From 0a2abb3f3dc5de3044e721bfe0993a0b1fd4f56f Mon Sep 17 00:00:00 2001 From: Juan RP Date: Sat, 9 Jul 2011 10:50:44 +0200 Subject: [PATCH] xbps-repo(8): 'search' target is now case insensitive. Along with this change, compat code has been added (from NetBSD) for systems that don't have it. The compat code has been reorganized to be in a common place and its prototypes in compat.h. The configure scripts checks if strcasestr() is available, and uses compat code if not found. This fixes issue #2 on github.com/vanilla/xbps. --- NEWS | 12 +++++++- bin/xbps-bin/install.c | 2 +- bin/xbps-bin/main.c | 2 +- bin/xbps-bin/util.c | 13 +++++--- bin/xbps-repo/xbps-repo.8 | 5 +-- configure | 38 +++++++++++++++++++++-- include/compat.h | 18 +++++++++++ include/strlcat.h | 10 ------ include/strlcpy.h | 10 ------ include/xbps_api_impl.h | 3 +- lib/compat/strcasestr.c | 64 +++++++++++++++++++++++++++++++++++++++ lib/compat/strlcat.c | 2 +- lib/compat/strlcpy.c | 2 +- lib/compat/vasprintf.c | 50 ++++++++++++++++++++++++++++++ lib/util.c | 37 ++-------------------- 15 files changed, 198 insertions(+), 70 deletions(-) create mode 100644 include/compat.h delete mode 100644 include/strlcat.h delete mode 100644 include/strlcpy.h create mode 100644 lib/compat/strcasestr.c create mode 100644 lib/compat/vasprintf.c diff --git a/NEWS b/NEWS index 205da302..19b8a966 100644 --- a/NEWS +++ b/NEWS @@ -1,4 +1,14 @@ -xbps-0.9.0 (???): +xbps-0.9.1 (???): + + * xbps-repo(8): the 'search' target now matches patterns in + case insensitive mode. + + * A bug has been fixed while updating a package and another package + providing a virtual package with a greater version was available + in repositories. Only accept this if the virtual package is explicitly + enabled in the "virtual-packages" section in the configuration file. + +xbps-0.9.0 (2011-07-08): * configure doesn't require a zlib package with a pkg-config file, to workaround some distribution that don't provide it (hi Ubuntu!). Rather diff --git a/bin/xbps-bin/install.c b/bin/xbps-bin/install.c index 6e8892ac..762f9fa8 100644 --- a/bin/xbps-bin/install.c +++ b/bin/xbps-bin/install.c @@ -34,7 +34,7 @@ #include #include -#include "strlcpy.h" +#include "compat.h" #include "defs.h" #include "../xbps-repo/defs.h" diff --git a/bin/xbps-bin/main.c b/bin/xbps-bin/main.c index 9cce6c7e..a902c149 100644 --- a/bin/xbps-bin/main.c +++ b/bin/xbps-bin/main.c @@ -34,7 +34,7 @@ #include #include -#include "strlcpy.h" +#include "compat.h" #include "defs.h" #include "../xbps-repo/defs.h" diff --git a/bin/xbps-bin/util.c b/bin/xbps-bin/util.c index 67916500..b95159ec 100644 --- a/bin/xbps-bin/util.c +++ b/bin/xbps-bin/util.c @@ -23,16 +23,20 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#ifdef HAVE_STRCASESTR +# define _GNU_SOURCE /* for strcasestr(3) */ +#endif #include #include #include -#include #include #include #include +#include +#include #include -#include "strlcpy.h" +#include "compat.h" #include "defs.h" #include "../xbps-repo/defs.h" @@ -190,8 +194,9 @@ show_pkg_namedesc(prop_object_t obj, void *arg, bool *loop_done) if ((xbps_pkgpattern_match(pkgver, rsd->pattern) == 1) || (xbps_pkgpattern_match(desc, rsd->pattern) == 1) || - (strcmp(pkgname, rsd->pattern) == 0) || - (strstr(pkgver, rsd->pattern)) || (strstr(desc, rsd->pattern))) { + (strcasecmp(pkgname, rsd->pattern) == 0) || + (strcasestr(pkgver, rsd->pattern)) || + (strcasestr(desc, rsd->pattern))) { tmp = calloc(1, rsd->pkgver_len + 1); if (tmp == NULL) return errno; diff --git a/bin/xbps-repo/xbps-repo.8 b/bin/xbps-repo/xbps-repo.8 index 71015634..002cc2f8 100644 --- a/bin/xbps-repo/xbps-repo.8 +++ b/bin/xbps-repo/xbps-repo.8 @@ -1,4 +1,4 @@ -.TH "XBPS\-REPO" "8" "06/20/2011" "\ \&" "\ \&" +.TH "XBPS\-REPO" "8" "07/09/2011" "\ \&" "\ \&" .\" ----------------------------------------------------------------- .\" * set default formatting .\" ----------------------------------------------------------------- @@ -93,7 +93,8 @@ Search for packages containing the shell \fBpkgname\fR or \fBdescription\fR -values in repository pool\&. +values in repository pool\&. Please note that patterns are matched in case +insensitive mode. .RE .PP \fBshow \fR\fB\fIpkgname\fR\fR diff --git a/configure b/configure index da27ceaa..1b854010 100755 --- a/configure +++ b/configure @@ -315,10 +315,42 @@ else fi echo "$VASPRINTF." rm -f _$func.c _$func -if [ "$VASPRINTF" = "yes" ]; then +if [ "$VASPRINTF" = "no" ]; then + echo "COMPAT_SRCS+= compat/vasprintf.o" >>$CONFIG_MK + echo "#include \"compat.h\"" >>$CONFIG_H +else echo "CPPFLAGS += -DHAVE_VASPRINTF" >> $CONFIG_MK fi +# +# Check for strcasestr(). +# +func=strcasestr +printf "Checking for $func() ..." +cat <_$func.c +#define _GNU_SOURCE +#include +int main(void) { + const char *h = "NEEDCOFEE"; + const char *n = "IneedCoffee"; + strcasestr(n, h); + return 0; +} +EOF +if $XCC _$func.c -o _$func 2>/dev/null; then + STRCASESTR=yes +else + STRCASESTR=no +fi +echo "$STRCASESTR." +rm -f _$func _$func.c +if [ "$STRCASESTR" = no ]; then + echo "COMPAT_SRCS += compat/strcasestr.o" >>$CONFIG_MK + echo "#include \"compat.h\"" >>$CONFIG_H +else + echo "CPPFLAGS += -DHAVE_STRCASESTR" >>$CONFIG_MK +fi + # # Check for strlcpy(). # @@ -342,7 +374,7 @@ echo "$STRLCPY." rm -f _$func.c _$func if [ "$STRLCPY" = no ]; then echo "COMPAT_SRCS += compat/strlcpy.o" >>$CONFIG_MK - echo "#include \"strlcpy.h\"" >>$CONFIG_H + echo "#include \"compat.h\"" >>$CONFIG_H else echo "CPPFLAGS += -DHAVE_STRLCPY" >> $CONFIG_MK fi @@ -369,7 +401,7 @@ echo "$STRLCAT." rm -f _$func.c _$func if [ "$STRLCAT" = no ]; then echo "COMPAT_SRCS += compat/strlcat.o" >>$CONFIG_MK - echo "#include \"strlcat.h\"" >>$CONFIG_H + echo "#include \"compat.h\"" >>$CONFIG_H else echo "CPPFLAGS += -DHAVE_STRLCAT" >>$CONFIG_MK fi diff --git a/include/compat.h b/include/compat.h new file mode 100644 index 00000000..7d5f4f09 --- /dev/null +++ b/include/compat.h @@ -0,0 +1,18 @@ +#ifndef COMPAT_H +#define COMPAT_H + +#include + +#ifndef HAVE_STRLCAT +size_t strlcat(char *, const char *, size_t); +#endif + +#ifndef HAVE_STRLCPY +size_t strlcpy(char *, const char *, size_t); +#endif + +#ifndef HAVE_STRCASESTR +char *strcasestr(const char *, const char *); +#endif + +#endif /* COMPAT_H */ diff --git a/include/strlcat.h b/include/strlcat.h deleted file mode 100644 index d64462aa..00000000 --- a/include/strlcat.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef STRLCAT_H -#define STRLCAT_H - -#include - -#ifndef HAVE_STRLCAT -size_t strlcat(char *, const char *, size_t); -#endif - -#endif diff --git a/include/strlcpy.h b/include/strlcpy.h deleted file mode 100644 index e0066b42..00000000 --- a/include/strlcpy.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef STRLCPY_H -#define STRLCPY_H - -#include - -#ifndef HAVE_STRLCPY -size_t strlcpy(char *, const char *, size_t); -#endif - -#endif diff --git a/include/xbps_api_impl.h b/include/xbps_api_impl.h index 811e5686..6a7afc04 100644 --- a/include/xbps_api_impl.h +++ b/include/xbps_api_impl.h @@ -35,9 +35,8 @@ #include #include +#include "compat.h" #include "queue.h" -#include "strlcpy.h" -#include "strlcat.h" #include "fetch.h" #define ARCHIVE_READ_BLOCKSIZE 10240 diff --git a/lib/compat/strcasestr.c b/lib/compat/strcasestr.c new file mode 100644 index 00000000..6a662cc8 --- /dev/null +++ b/lib/compat/strcasestr.c @@ -0,0 +1,64 @@ +/* $NetBSD: strcasestr.c,v 1.3 2005/11/29 03:12:00 christos Exp $ */ + +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Chris Torek. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "xbps_api_impl.h" +#include +#include +#include + +/* + * Find the first occurrence of find in s, ignore case. + */ +char * +strcasestr(const char *s, const char *find) +{ + char c, sc; + size_t len; + + assert(s != NULL); + assert(find != NULL); + + if ((c = *find++) != 0) { + c = tolower((unsigned char)c); + len = strlen(find); + do { + do { + if ((sc = *s++) == 0) + return (NULL); + } while ((char)tolower((unsigned char)sc) != c); + } while (strncasecmp(s, find, len) != 0); + s--; + } + return __UNCONST(s); +} diff --git a/lib/compat/strlcat.c b/lib/compat/strlcat.c index 48334aff..836e3929 100644 --- a/lib/compat/strlcat.c +++ b/lib/compat/strlcat.c @@ -19,7 +19,7 @@ #include #include -#include "strlcat.h" +#include "compat.h" /* * Appends src to string dst of size siz (unlike strncat, siz is the diff --git a/lib/compat/strlcpy.c b/lib/compat/strlcpy.c index 2ee0f2b6..b87eb8d4 100644 --- a/lib/compat/strlcpy.c +++ b/lib/compat/strlcpy.c @@ -19,7 +19,7 @@ #include #include -#include "xbps_api_impl.h" +#include "compat.h" /* * Copy src to string dst of size siz. At most siz-1 characters diff --git a/lib/compat/vasprintf.c b/lib/compat/vasprintf.c new file mode 100644 index 00000000..1bac36db --- /dev/null +++ b/lib/compat/vasprintf.c @@ -0,0 +1,50 @@ +/*- + * Copyright (c) 2008-2011 Juan Romero Pardines. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +char * +xbps_xasprintf(const char *fmt, ...) +{ + va_list ap, aq; + size_t len; + int res; + char *buf; + + va_start(aq, fmt); + va_copy(ap, aq); + len = vsnprintf(NULL, 0, fmt, aq) + 1; + if ((buf = malloc(len)) == NULL) { + va_end(ap); + return NULL; + } + + va_start(ap, fmt); + res = vsnprintf(buf, len, fmt, ap); + if (res < 0 || res >= (int)len) { + free(buf); + return NULL; + } + + return buf; +} diff --git a/lib/util.c b/lib/util.c index 5d8ae686..8e71a927 100644 --- a/lib/util.c +++ b/lib/util.c @@ -25,14 +25,9 @@ #ifdef HAVE_VASPRINTF # define _GNU_SOURCE /* for vasprintf(3) */ -# ifdef _XOPEN_SOURCE -# undef _XOPEN_SOURCE -# endif -# include -# undef _GNU_SOURCE -#else -# include #endif + +#include #include #include #include @@ -440,30 +435,4 @@ xbps_xasprintf(const char *fmt, ...) return buf; } -#else -char * -xbps_xasprintf(const char *fmt, ...) -{ - va_list ap, aq; - size_t len; - int res; - char *buf; - - va_start(aq, fmt); - va_copy(ap, aq); - len = vsnprintf(NULL, 0, fmt, aq) + 1; - if ((buf = malloc(len)) == NULL) { - va_end(ap); - return NULL; - } - - va_start(ap, fmt); - res = vsnprintf(buf, len, fmt, ap); - if (res < 0 || res >= (int)len) { - free(buf); - return NULL; - } - - return buf; -} -#endif /* !HAVE_VASPRINTF */ +#endif /* HAVE_VASPRINTF */