shexp.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /*
  39. * shexp.h: Defines and prototypes for shell exp. match routines
  40. *
  41. *
  42. * This routine will match a string with a shell expression. The expressions
  43. * accepted are based loosely on the expressions accepted by zsh.
  44. *
  45. * o * matches anything
  46. * o ? matches one character
  47. * o \ will escape a special character
  48. * o $ matches the end of the string
  49. * o [abc] matches one occurence of a, b, or c. The only character that needs
  50. * to be escaped in this is ], all others are not special.
  51. * o [a-z] matches any character between a and z
  52. * o [^az] matches any character except a or z
  53. * o ~ followed by another shell expression will remove any pattern
  54. * matching the shell expression from the match list
  55. * o (foo|bar) will match either the substring foo, or the substring bar.
  56. * These can be shell expressions as well.
  57. *
  58. * The public interface to these routines is documented below.
  59. *
  60. * Rob McCool
  61. *
  62. */
  63. #ifndef SHEXP_H
  64. #define SHEXP_H
  65. /*
  66. * Requires that the macro MALLOC be set to a "safe" malloc that will
  67. * exit if no memory is available. If not under MCC httpd, define MALLOC
  68. * to be the real malloc and play with fire, or make your own function.
  69. */
  70. #include "../netsite.h"
  71. #include <ctype.h> /* isalnum */
  72. #include <string.h> /* strlen */
  73. /*
  74. * Wrappers for shexp/regexp
  75. *
  76. * Portions of code that explicitly want to have either shexp's
  77. * or regexp's should call those functions directly.
  78. *
  79. * Common code bases for multiple products should use the following
  80. * macros instead to use either shell or regular expressions,
  81. * depending on the flavor chosen for a given server.
  82. *
  83. */
  84. #if defined(MCC_PROXY) && defined(USE_REGEX)
  85. #include "base/regexp.h"
  86. #define WILDPAT_VALID(exp) regexp_valid(exp)
  87. #define WILDPAT_MATCH(str, exp) regexp_match(str, exp)
  88. #define WILDPAT_CMP(str, exp) regexp_cmp(str, exp)
  89. #define WILDPAT_CASECMP(str, exp) regexp_casecmp(str, exp)
  90. #else /* HTTP servers */
  91. #define WILDPAT_VALID(exp) shexp_valid(exp)
  92. #define WILDPAT_MATCH(str, exp) shexp_match(str, exp)
  93. #define WILDPAT_CMP(str, exp) shexp_cmp(str, exp)
  94. #define WILDPAT_CASECMP(str, exp) shexp_casecmp(str, exp)
  95. #endif
  96. /* --------------------------- Public routines ---------------------------- */
  97. NSPR_BEGIN_EXTERN_C
  98. /*
  99. * shexp_valid takes a shell expression exp as input. It returns:
  100. *
  101. * NON_SXP if exp is a standard string
  102. * INVALID_SXP if exp is a shell expression, but invalid
  103. * VALID_SXP if exp is a valid shell expression
  104. */
  105. #define NON_SXP -1
  106. #define INVALID_SXP -2
  107. #define VALID_SXP 1
  108. /* and generic shexp/regexp versions */
  109. #define NON_WILDPAT NON_SXP
  110. #define INVALID_WILDPAT INVALID_SXP
  111. #define VALID_WILDPAT VALID_SXP
  112. /* and regexp versions */
  113. #define NON_REGEXP NON_SXP
  114. #define INVALID_REGEXP INVALID_SXP
  115. #define VALID_REGEXP VALID_SXP
  116. NSAPI_PUBLIC int shexp_valid(char *exp);
  117. /*
  118. * shexp_match
  119. *
  120. * Takes a prevalidated shell expression exp, and a string str.
  121. *
  122. * Returns 0 on match and 1 on non-match.
  123. */
  124. NSAPI_PUBLIC int shexp_match(char *str, char *exp);
  125. /*
  126. * shexp_cmp
  127. *
  128. * Same as above, but validates the exp first. 0 on match, 1 on non-match,
  129. * -1 on invalid exp. shexp_casecmp does the same thing but is case
  130. * insensitive.
  131. */
  132. NSAPI_PUBLIC int shexp_cmp(char *str, char *exp);
  133. NSAPI_PUBLIC int shexp_casecmp(char *str, char *exp);
  134. NSPR_END_EXTERN_C
  135. #endif