match.c 6.3 KB

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