match.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. * Wildcard pattern matching
  12. */
  13. #include "portab.h"
  14. static char UNUSED id[] = "$Id: match.c,v 1.5 2006/10/06 21:23:47 fw Exp $";
  15. #include "imp.h"
  16. #include <assert.h>
  17. #include <string.h>
  18. #include "exp.h"
  19. #include "match.h"
  20. #include "defines.h"
  21. #include "tool.h"
  22. /*
  23. * Die Pattern-Matching-Funkionen [Matche(), Matche_After_Star()] basieren
  24. * auf Versionen von J. Kercheval. Die Version 1.1 wurde am 12.03.1991 als
  25. * "public domain" freigegeben:
  26. * <http://www.snippets.org/snippets/portable/MATCH+C.php3>
  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. GLOBAL bool
  37. Match( const char *Pattern, const char *String )
  38. {
  39. /* Pattern mit String vergleichen */
  40. if( Matche( Pattern, String ) == MATCH_VALID ) return true;
  41. else return false;
  42. } /* Match */
  43. GLOBAL bool
  44. MatchCaseInsensitive(const char *pattern, const char *searchme)
  45. {
  46. char haystack[COMMAND_LEN];
  47. strlcpy(haystack, searchme, sizeof(haystack));
  48. return Match(pattern, ngt_LowerStr(haystack));
  49. } /* MatchCaseInsensitive */
  50. static int
  51. Matche( const char *p, const char *t )
  52. {
  53. register char range_start, range_end;
  54. bool invert;
  55. bool member_match;
  56. bool loop;
  57. for( ; *p; p++, t++ )
  58. {
  59. /* if this is the end of the text then this is the end of the match */
  60. if( ! *t )
  61. {
  62. return ( *p == '*' && *++p == '\0' ) ? MATCH_VALID : MATCH_ABORT;
  63. }
  64. /* determine and react to pattern type */
  65. switch( *p )
  66. {
  67. case '?': /* single any character match */
  68. break;
  69. case '*': /* multiple any character match */
  70. return Matche_After_Star( p, t );
  71. case '[': /* [..] construct, single member/exclusion character match */
  72. /* move to beginning of range */
  73. p++;
  74. /* check if this is a member match or exclusion match */
  75. invert = false;
  76. if( *p == '!' || *p == '^' )
  77. {
  78. invert = true;
  79. p++;
  80. }
  81. /* if closing bracket here or at range start then we have a malformed pattern */
  82. if ( *p == ']' ) return MATCH_PATTERN;
  83. member_match = false;
  84. loop = true;
  85. while( loop )
  86. {
  87. /* if end of construct then loop is done */
  88. if( *p == ']' )
  89. {
  90. loop = false;
  91. continue;
  92. }
  93. /* matching a '!', '^', '-', '\' or a ']' */
  94. if( *p == '\\' ) range_start = range_end = *++p;
  95. else range_start = range_end = *p;
  96. /* if end of pattern then bad pattern (Missing ']') */
  97. if( ! *p ) return MATCH_PATTERN;
  98. /* check for range bar */
  99. if( *++p == '-' )
  100. {
  101. /* get the range end */
  102. range_end = *++p;
  103. /* if end of pattern or construct then bad pattern */
  104. if( range_end == '\0' || range_end == ']' ) return MATCH_PATTERN;
  105. /* special character range end */
  106. if( range_end == '\\' )
  107. {
  108. range_end = *++p;
  109. /* if end of text then we have a bad pattern */
  110. if ( ! range_end ) return MATCH_PATTERN;
  111. }
  112. /* move just beyond this range */
  113. p++;
  114. }
  115. /* if the text character is in range then match found. make sure the range
  116. * letters have the proper relationship to one another before comparison */
  117. if( range_start < range_end )
  118. {
  119. if( *t >= range_start && *t <= range_end )
  120. {
  121. member_match = true;
  122. loop = false;
  123. }
  124. }
  125. else
  126. {
  127. if( *t >= range_end && *t <= range_start )
  128. {
  129. member_match = true;
  130. loop = false;
  131. }
  132. }
  133. }
  134. /* if there was a match in an exclusion set then no match */
  135. /* if there was no match in a member set then no match */
  136. if(( invert && member_match ) || ! ( invert || member_match )) return MATCH_RANGE;
  137. /* if this is not an exclusion then skip the rest of the [...]
  138. * construct that already matched. */
  139. if( member_match )
  140. {
  141. while( *p != ']' )
  142. {
  143. /* bad pattern (Missing ']') */
  144. if( ! *p ) return MATCH_PATTERN;
  145. /* skip exact match */
  146. if( *p == '\\' )
  147. {
  148. p++;
  149. /* if end of text then we have a bad pattern */
  150. if( ! *p ) return MATCH_PATTERN;
  151. }
  152. /* move to next pattern char */
  153. p++;
  154. }
  155. }
  156. break;
  157. case '\\': /* next character is quoted and must match exactly */
  158. /* move pattern pointer to quoted char and fall through */
  159. p++;
  160. /* if end of text then we have a bad pattern */
  161. if( ! *p ) return MATCH_PATTERN;
  162. /* must match this character exactly */
  163. default:
  164. if( *p != *t ) return MATCH_LITERAL;
  165. }
  166. }
  167. /* if end of text not reached then the pattern fails */
  168. if( *t ) return MATCH_END;
  169. else return MATCH_VALID;
  170. } /* Matche */
  171. static int
  172. Matche_After_Star( const char *p, const char *t )
  173. {
  174. register int nextp, match = 0;
  175. /* pass over existing ? and * in pattern */
  176. while( *p == '?' || *p == '*' )
  177. {
  178. /* take one char for each ? and + */
  179. if (*p == '?')
  180. {
  181. /* if end of text then no match */
  182. if( ! *t++ ) return MATCH_ABORT;
  183. }
  184. /* move to next char in pattern */
  185. p++;
  186. }
  187. /* if end of pattern we have matched regardless of text left */
  188. if( ! *p ) return MATCH_VALID;
  189. /* get the next character to match which must be a literal or '[' */
  190. nextp = *p;
  191. if( nextp == '\\' )
  192. {
  193. nextp = p[1];
  194. /* if end of text then we have a bad pattern */
  195. if( ! nextp ) return MATCH_PATTERN;
  196. }
  197. /* Continue until we run out of text or definite result seen */
  198. do
  199. {
  200. /* a precondition for matching is that the next character
  201. * in the pattern match the next character in the text or that
  202. * the next pattern char is the beginning of a range. Increment
  203. * text pointer as we go here */
  204. if( nextp == *t || nextp == '[' ) match = Matche( p, t );
  205. /* if the end of text is reached then no match */
  206. if( ! *t++ ) match = MATCH_ABORT;
  207. } while( match != MATCH_VALID && match != MATCH_ABORT && match != MATCH_PATTERN );
  208. /* return result */
  209. return match;
  210. } /* Matche_After_Star */
  211. /* -eof- */