match.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * Please read the file COPYING, README and AUTHORS for more information.
  10. */
  11. #include "portab.h"
  12. /**
  13. * @file
  14. * Wildcard pattern matching
  15. */
  16. #include "imp.h"
  17. #include <assert.h>
  18. #include <string.h>
  19. #include "exp.h"
  20. #include "match.h"
  21. #include "defines.h"
  22. #include "tool.h"
  23. /*
  24. * The pattern matching functions [Matche(), Matche_After_Star()] are based
  25. * on code of J. Kercheval. Version 1.1 has been released on 1991-03-12 as
  26. * "public domain": <http://c.snippets.org/snip_lister.php?fname=match.c>
  27. */
  28. static int Matche PARAMS(( const char *p, const char *t ));
  29. static int Matche_After_Star PARAMS(( const char *p, const char *t ));
  30. #define MATCH_PATTERN 6 /**< bad pattern */
  31. #define MATCH_LITERAL 5 /**< match failure on literal match */
  32. #define MATCH_RANGE 4 /**< match failure on [..] construct */
  33. #define MATCH_ABORT 3 /**< premature end of text string */
  34. #define MATCH_END 2 /**< premature end of pattern string */
  35. #define MATCH_VALID 1 /**< valid match */
  36. /**
  37. * Match string with pattern.
  38. *
  39. * @param Pattern Pattern to match with
  40. * @param String Input string
  41. * @return true if pattern matches
  42. */
  43. GLOBAL bool
  44. Match( const char *Pattern, const char *String )
  45. {
  46. /* Pattern mit String vergleichen */
  47. if( Matche( Pattern, String ) == MATCH_VALID ) return true;
  48. else return false;
  49. } /* Match */
  50. /**
  51. * Match string with pattern case-insensitive.
  52. *
  53. * @param pattern Pattern to match with
  54. * @param searchme Input string, at most COMMAND_LEN-1 characters long
  55. * @return true if pattern matches
  56. */
  57. GLOBAL bool
  58. MatchCaseInsensitive(const char *pattern, const char *searchme)
  59. {
  60. char haystack[COMMAND_LEN];
  61. strlcpy(haystack, searchme, sizeof(haystack));
  62. return Match(pattern, ngt_LowerStr(haystack));
  63. } /* MatchCaseInsensitive */
  64. static int
  65. Matche( const char *p, const char *t )
  66. {
  67. register char range_start, range_end;
  68. bool invert;
  69. bool member_match;
  70. bool loop;
  71. for( ; *p; p++, t++ )
  72. {
  73. /* if this is the end of the text then this is the end of the match */
  74. if( ! *t )
  75. {
  76. return ( *p == '*' && *++p == '\0' ) ? MATCH_VALID : MATCH_ABORT;
  77. }
  78. /* determine and react to pattern type */
  79. switch( *p )
  80. {
  81. case '?': /* single any character match */
  82. break;
  83. case '*': /* multiple any character match */
  84. return Matche_After_Star( p, t );
  85. case '[': /* [..] construct, single member/exclusion character match */
  86. /* move to beginning of range */
  87. p++;
  88. /* check if this is a member match or exclusion match */
  89. invert = false;
  90. if( *p == '!' || *p == '^' )
  91. {
  92. invert = true;
  93. p++;
  94. }
  95. /* if closing bracket here or at range start then we have a malformed pattern */
  96. if ( *p == ']' ) return MATCH_PATTERN;
  97. member_match = false;
  98. loop = true;
  99. while( loop )
  100. {
  101. /* if end of construct then loop is done */
  102. if( *p == ']' )
  103. {
  104. loop = false;
  105. continue;
  106. }
  107. /* matching a '!', '^', '-', '\' or a ']' */
  108. if( *p == '\\' ) range_start = range_end = *++p;
  109. else range_start = range_end = *p;
  110. /* if end of pattern then bad pattern (Missing ']') */
  111. if( ! *p ) return MATCH_PATTERN;
  112. /* check for range bar */
  113. if( *++p == '-' )
  114. {
  115. /* get the range end */
  116. range_end = *++p;
  117. /* if end of pattern or construct then bad pattern */
  118. if( range_end == '\0' || range_end == ']' ) return MATCH_PATTERN;
  119. /* special character range end */
  120. if( range_end == '\\' )
  121. {
  122. range_end = *++p;
  123. /* if end of text then we have a bad pattern */
  124. if ( ! range_end ) return MATCH_PATTERN;
  125. }
  126. /* move just beyond this range */
  127. p++;
  128. }
  129. /* if the text character is in range then match found. make sure the range
  130. * letters have the proper relationship to one another before comparison */
  131. if( range_start < range_end )
  132. {
  133. if( *t >= range_start && *t <= range_end )
  134. {
  135. member_match = true;
  136. loop = false;
  137. }
  138. }
  139. else
  140. {
  141. if( *t >= range_end && *t <= range_start )
  142. {
  143. member_match = true;
  144. loop = false;
  145. }
  146. }
  147. }
  148. /* if there was a match in an exclusion set then no match */
  149. /* if there was no match in a member set then no match */
  150. if(( invert && member_match ) || ! ( invert || member_match )) return MATCH_RANGE;
  151. /* if this is not an exclusion then skip the rest of the [...]
  152. * construct that already matched. */
  153. if( member_match )
  154. {
  155. while( *p != ']' )
  156. {
  157. /* bad pattern (Missing ']') */
  158. if( ! *p ) return MATCH_PATTERN;
  159. /* skip exact match */
  160. if( *p == '\\' )
  161. {
  162. p++;
  163. /* if end of text then we have a bad pattern */
  164. if( ! *p ) return MATCH_PATTERN;
  165. }
  166. /* move to next pattern char */
  167. p++;
  168. }
  169. }
  170. break;
  171. case '\\': /* next character is quoted and must match exactly */
  172. /* move pattern pointer to quoted char and fall through */
  173. p++;
  174. /* if end of text then we have a bad pattern */
  175. if( ! *p ) return MATCH_PATTERN;
  176. /* must match this character exactly */
  177. default:
  178. if( *p != *t ) return MATCH_LITERAL;
  179. }
  180. }
  181. /* if end of text not reached then the pattern fails */
  182. if( *t ) return MATCH_END;
  183. else return MATCH_VALID;
  184. } /* Matche */
  185. static int
  186. Matche_After_Star( const char *p, const char *t )
  187. {
  188. register int nextp, match = 0;
  189. /* pass over existing ? and * in pattern */
  190. while( *p == '?' || *p == '*' )
  191. {
  192. /* take one char for each ? and + */
  193. if (*p == '?')
  194. {
  195. /* if end of text then no match */
  196. if( ! *t++ ) return MATCH_ABORT;
  197. }
  198. /* move to next char in pattern */
  199. p++;
  200. }
  201. /* if end of pattern we have matched regardless of text left */
  202. if( ! *p ) return MATCH_VALID;
  203. /* get the next character to match which must be a literal or '[' */
  204. nextp = *p;
  205. if( nextp == '\\' )
  206. {
  207. nextp = p[1];
  208. /* if end of text then we have a bad pattern */
  209. if( ! nextp ) return MATCH_PATTERN;
  210. }
  211. /* Continue until we run out of text or definite result seen */
  212. do
  213. {
  214. /* a precondition for matching is that the next character
  215. * in the pattern match the next character in the text or that
  216. * the next pattern char is the beginning of a range. Increment
  217. * text pointer as we go here */
  218. if( nextp == *t || nextp == '[' ) match = Matche( p, t );
  219. /* if the end of text is reached then no match */
  220. if( ! *t++ ) match = MATCH_ABORT;
  221. } while( match != MATCH_VALID && match != MATCH_ABORT && match != MATCH_PATTERN );
  222. /* return result */
  223. return match;
  224. } /* Matche_After_Star */
  225. /* -eof- */