match.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
  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 <assert.h>
  17. #include <string.h>
  18. #include "defines.h"
  19. #include "tool.h"
  20. #include "match.h"
  21. /*
  22. * The pattern matching functions [Matche(), Matche_After_Star()] are based
  23. * on code of J. Kercheval. Version 1.1 has been released on 1991-03-12 as
  24. * "public domain": <http://c.snippets.org/snip_lister.php?fname=match.c>
  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. /**
  35. * Match string with pattern.
  36. *
  37. * @param Pattern Pattern to match with
  38. * @param String Input string
  39. * @return true if pattern matches
  40. */
  41. GLOBAL bool
  42. Match( const char *Pattern, const char *String )
  43. {
  44. if( Matche( Pattern, String ) == MATCH_VALID ) return true;
  45. else return false;
  46. } /* Match */
  47. /**
  48. * Match string with pattern case-insensitive.
  49. *
  50. * @param Pattern Pattern to match with
  51. * @param String Input string, at most COMMAND_LEN-1 characters long
  52. * @return true if pattern matches
  53. */
  54. GLOBAL bool
  55. MatchCaseInsensitive(const char *Pattern, const char *String)
  56. {
  57. char haystack[COMMAND_LEN];
  58. strlcpy(haystack, String, sizeof(haystack));
  59. return Match(Pattern, ngt_LowerStr(haystack));
  60. } /* MatchCaseInsensitive */
  61. /**
  62. * Match string with pattern case-insensitive.
  63. *
  64. * @param pattern Pattern to match with
  65. * @param String Input string, at most COMMAND_LEN-1 characters long
  66. * @param Separator Character separating the individual patterns in the list
  67. * @return true if pattern matches
  68. */
  69. GLOBAL bool
  70. MatchCaseInsensitiveList(const char *Pattern, const char *String,
  71. const char *Separator)
  72. {
  73. char tmp_pattern[COMMAND_LEN], haystack[COMMAND_LEN], *ptr;
  74. strlcpy(tmp_pattern, Pattern, sizeof(tmp_pattern));
  75. strlcpy(haystack, String, sizeof(haystack));
  76. ngt_LowerStr(haystack);
  77. ptr = strtok(tmp_pattern, Separator);
  78. while (ptr) {
  79. ngt_TrimStr(ptr);
  80. if (Match(ptr, haystack))
  81. return true;
  82. ptr = strtok(NULL, Separator);
  83. }
  84. return false;
  85. } /* MatchCaseInsensitive */
  86. static int
  87. Matche( const char *p, const char *t )
  88. {
  89. for( ; *p; p++, t++ )
  90. {
  91. /* if this is the end of the text then this is the end of the match */
  92. if( ! *t )
  93. {
  94. return ( *p == '*' && *++p == '\0' ) ? MATCH_VALID : MATCH_ABORT;
  95. }
  96. /* determine and react to pattern type */
  97. switch( *p )
  98. {
  99. case '?': /* single any character match */
  100. break;
  101. case '*': /* multiple any character match */
  102. return Matche_After_Star( p, t );
  103. default: /* must match this character exactly */
  104. if( *p != *t ) return MATCH_LITERAL;
  105. }
  106. }
  107. /* if end of text not reached then the pattern fails */
  108. if( *t ) return MATCH_END;
  109. else return MATCH_VALID;
  110. } /* Matche */
  111. static int
  112. Matche_After_Star( const char *p, const char *t )
  113. {
  114. register int nextp, match = 0;
  115. /* pass over existing ? and * in pattern */
  116. while( *p == '?' || *p == '*' )
  117. {
  118. /* take one char for each ? and + */
  119. if (*p == '?')
  120. {
  121. /* if end of text then no match */
  122. if( ! *t++ ) return MATCH_ABORT;
  123. }
  124. /* move to next char in pattern */
  125. p++;
  126. }
  127. /* if end of pattern we have matched regardless of text left */
  128. if( ! *p ) return MATCH_VALID;
  129. /* get the next character to match which must be a literal or '[' */
  130. nextp = *p;
  131. if( nextp == '\\' )
  132. {
  133. nextp = p[1];
  134. /* if end of text then we have a bad pattern */
  135. if( ! nextp ) return MATCH_PATTERN;
  136. }
  137. /* Continue until we run out of text or definite result seen */
  138. do
  139. {
  140. /* a precondition for matching is that the next character
  141. * in the pattern match the next character in the text or that
  142. * the next pattern char is the beginning of a range. Increment
  143. * text pointer as we go here */
  144. if( nextp == *t || nextp == '[' ) match = Matche( p, t );
  145. /* if the end of text is reached then no match */
  146. if( ! *t++ ) match = MATCH_ABORT;
  147. } while( match != MATCH_VALID && match != MATCH_ABORT && match != MATCH_PATTERN );
  148. /* return result */
  149. return match;
  150. } /* Matche_After_Star */
  151. /* -eof- */