shexp.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * shexp.h: Defines and prototypes for shell exp. match routines
  43. *
  44. *
  45. * This routine will match a string with a shell expression. The expressions
  46. * accepted are based loosely on the expressions accepted by zsh.
  47. *
  48. * o * matches anything
  49. * o ? matches one character
  50. * o \ will escape a special character
  51. * o $ matches the end of the string
  52. * o [abc] matches one occurence of a, b, or c. The only character that needs
  53. * to be escaped in this is ], all others are not special.
  54. * o [a-z] matches any character between a and z
  55. * o [^az] matches any character except a or z
  56. * o ~ followed by another shell expression will remove any pattern
  57. * matching the shell expression from the match list
  58. * o (foo|bar) will match either the substring foo, or the substring bar.
  59. * These can be shell expressions as well.
  60. *
  61. * The public interface to these routines is documented below.
  62. *
  63. * Rob McCool
  64. *
  65. */
  66. #ifndef SHEXP_H
  67. #define SHEXP_H
  68. /*
  69. * Requires that the macro MALLOC be set to a "safe" malloc that will
  70. * exit if no memory is available. If not under MCC httpd, define MALLOC
  71. * to be the real malloc and play with fire, or make your own function.
  72. */
  73. #include "../netsite.h"
  74. #include <ctype.h> /* isalnum */
  75. #include <string.h> /* strlen */
  76. /*
  77. * Wrappers for shexp/regexp
  78. *
  79. * Portions of code that explicitly want to have either shexp's
  80. * or regexp's should call those functions directly.
  81. *
  82. * Common code bases for multiple products should use the following
  83. * macros instead to use either shell or regular expressions,
  84. * depending on the flavor chosen for a given server.
  85. *
  86. */
  87. #if defined(MCC_PROXY) && defined(USE_REGEX)
  88. #include "base/regexp.h"
  89. #define WILDPAT_VALID(exp) regexp_valid(exp)
  90. #define WILDPAT_MATCH(str, exp) regexp_match(str, exp)
  91. #define WILDPAT_CMP(str, exp) regexp_cmp(str, exp)
  92. #define WILDPAT_CASECMP(str, exp) regexp_casecmp(str, exp)
  93. #else /* HTTP servers */
  94. #define WILDPAT_VALID(exp) shexp_valid(exp)
  95. #define WILDPAT_MATCH(str, exp) shexp_match(str, exp)
  96. #define WILDPAT_CMP(str, exp) shexp_cmp(str, exp)
  97. #define WILDPAT_CASECMP(str, exp) shexp_casecmp(str, exp)
  98. #endif
  99. /* --------------------------- Public routines ---------------------------- */
  100. NSPR_BEGIN_EXTERN_C
  101. /*
  102. * shexp_valid takes a shell expression exp as input. It returns:
  103. *
  104. * NON_SXP if exp is a standard string
  105. * INVALID_SXP if exp is a shell expression, but invalid
  106. * VALID_SXP if exp is a valid shell expression
  107. */
  108. #define NON_SXP -1
  109. #define INVALID_SXP -2
  110. #define VALID_SXP 1
  111. /* and generic shexp/regexp versions */
  112. #define NON_WILDPAT NON_SXP
  113. #define INVALID_WILDPAT INVALID_SXP
  114. #define VALID_WILDPAT VALID_SXP
  115. /* and regexp versions */
  116. #define NON_REGEXP NON_SXP
  117. #define INVALID_REGEXP INVALID_SXP
  118. #define VALID_REGEXP VALID_SXP
  119. NSAPI_PUBLIC int shexp_valid(char *exp);
  120. /*
  121. * shexp_match
  122. *
  123. * Takes a prevalidated shell expression exp, and a string str.
  124. *
  125. * Returns 0 on match and 1 on non-match.
  126. */
  127. NSAPI_PUBLIC int shexp_match(char *str, char *exp);
  128. /*
  129. * shexp_cmp
  130. *
  131. * Same as above, but validates the exp first. 0 on match, 1 on non-match,
  132. * -1 on invalid exp. shexp_casecmp does the same thing but is case
  133. * insensitive.
  134. */
  135. NSAPI_PUBLIC int shexp_cmp(char *str, char *exp);
  136. NSAPI_PUBLIC int shexp_casecmp(char *str, char *exp);
  137. NSPR_END_EXTERN_C
  138. #endif