X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=ngircd-alex.git;a=blobdiff_plain;f=src%2Fngircd%2Fmatch.c;h=75bf4358395d27fa21b8a9c213c307a82a2196d6;hp=ff5807723a83b24ca542e655184e6a5dc508c731;hb=32f63abb59b5c9f47b4d517e0bbf9cc73fd044dc;hpb=8adff5922376676c2eeb49de1cbab86cc345b887 diff --git a/src/ngircd/match.c b/src/ngircd/match.c index ff580772..75bf4358 100644 --- a/src/ngircd/match.c +++ b/src/ngircd/match.c @@ -1,20 +1,20 @@ /* * ngIRCd -- The Next Generation IRC Daemon - * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de) + * Copyright (c)2001-2012 Alexander Barton (alex@barton.de) and Contributors. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * Please read the file COPYING, README and AUTHORS for more information. - * - * Wildcard pattern matching */ - #include "portab.h" -static char UNUSED id[] = "$Id: match.c,v 1.3 2005/03/19 18:43:49 fw Exp $"; +/** + * @file + * Wildcard pattern matching + */ #include "imp.h" #include @@ -22,30 +22,38 @@ static char UNUSED id[] = "$Id: match.c,v 1.3 2005/03/19 18:43:49 fw Exp $"; #include "exp.h" #include "match.h" +#include "defines.h" +#include "tool.h" /* - * Die Pattern-Matching-Funkionen [Matche(), Matche_After_Star()] basieren - * auf Versionen von J. Kercheval. Die Version 1.1 wurde am 12.03.1991 als - * "public domain" freigegeben: - * + * The pattern matching functions [Matche(), Matche_After_Star()] are based + * on code of J. Kercheval. Version 1.1 has been released on 1991-03-12 as + * "public domain": */ -LOCAL int Matche PARAMS(( char *p, char *t )); -LOCAL int Matche_After_Star PARAMS(( char *p, char *t )); +static int Matche PARAMS(( const char *p, const char *t )); +static int Matche_After_Star PARAMS(( const char *p, const char *t )); -#define MATCH_PATTERN 6 /* bad pattern */ -#define MATCH_LITERAL 5 /* match failure on literal match */ -#define MATCH_RANGE 4 /* match failure on [..] construct */ -#define MATCH_ABORT 3 /* premature end of text string */ -#define MATCH_END 2 /* premature end of pattern string */ -#define MATCH_VALID 1 /* valid match */ +#define MATCH_PATTERN 6 /**< bad pattern */ +#define MATCH_LITERAL 5 /**< match failure on literal match */ +#define MATCH_RANGE 4 /**< match failure on [..] construct */ +#define MATCH_ABORT 3 /**< premature end of text string */ +#define MATCH_END 2 /**< premature end of pattern string */ +#define MATCH_VALID 1 /**< valid match */ +/** + * Match string with pattern. + * + * @param Pattern Pattern to match with + * @param String Input string + * @return true if pattern matches + */ GLOBAL bool -Match( char *Pattern, char *String ) +Match( const char *Pattern, const char *String ) { /* Pattern mit String vergleichen */ if( Matche( Pattern, String ) == MATCH_VALID ) return true; @@ -53,8 +61,54 @@ Match( char *Pattern, char *String ) } /* Match */ -LOCAL int -Matche( char *p, char *t ) +/** + * Match string with pattern case-insensitive. + * + * @param Pattern Pattern to match with + * @param String Input string, at most COMMAND_LEN-1 characters long + * @return true if pattern matches + */ +GLOBAL bool +MatchCaseInsensitive(const char *Pattern, const char *String) +{ + char haystack[COMMAND_LEN]; + + strlcpy(haystack, String, sizeof(haystack)); + return Match(Pattern, ngt_LowerStr(haystack)); +} /* MatchCaseInsensitive */ + + +/** + * Match string with pattern case-insensitive. + * + * @param pattern Pattern to match with + * @param String Input string, at most COMMAND_LEN-1 characters long + * @param Separator Character separating the individual patterns in the list + * @return true if pattern matches + */ +GLOBAL bool +MatchCaseInsensitiveList(const char *Pattern, const char *String, + const char *Separator) +{ + char tmp_pattern[COMMAND_LEN], haystack[COMMAND_LEN], *ptr; + + strlcpy(tmp_pattern, Pattern, sizeof(tmp_pattern)); + strlcpy(haystack, String, sizeof(haystack)); + ngt_LowerStr(haystack); + + ptr = strtok(tmp_pattern, Separator); + while (ptr) { + ngt_TrimStr(ptr); + if (Match(ptr, haystack)) + return true; + ptr = strtok(NULL, Separator); + } + return false; +} /* MatchCaseInsensitive */ + + +static int +Matche( const char *p, const char *t ) { register char range_start, range_end; bool invert; @@ -200,8 +254,8 @@ Matche( char *p, char *t ) } /* Matche */ -LOCAL int -Matche_After_Star( char *p, char *t ) +static int +Matche_After_Star( const char *p, const char *t ) { register int nextp, match = 0;