shexp.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /*
  13. * shexp.c: shell-like wildcard match routines
  14. *
  15. *
  16. * See shexp.h for public documentation.
  17. *
  18. * Rob McCool
  19. *
  20. */
  21. #include "shexp.h"
  22. #include <ctype.h> /* isalpha, tolower */
  23. /* ----------------------------- shexp_valid ------------------------------ */
  24. int valid_subexp(char *exp, char stop)
  25. {
  26. register int x,y,t;
  27. int nsc,tld;
  28. x=0;nsc=0;tld=0;
  29. while(exp[x] && (exp[x] != stop)) {
  30. switch(exp[x]) {
  31. case '~':
  32. if(tld) return INVALID_SXP;
  33. else ++tld;
  34. case '*':
  35. case '?':
  36. case '^':
  37. case '$':
  38. ++nsc;
  39. break;
  40. case '[':
  41. ++nsc;
  42. if((!exp[++x]) || (exp[x] == ']'))
  43. return INVALID_SXP;
  44. for(++x;exp[x] && (exp[x] != ']');++x)
  45. if(exp[x] == '\\')
  46. if(!exp[++x])
  47. return INVALID_SXP;
  48. if(!exp[x])
  49. return INVALID_SXP;
  50. break;
  51. case '(':
  52. ++nsc;
  53. while(1) {
  54. if(exp[++x] == ')')
  55. return INVALID_SXP;
  56. for(y=x;(exp[y]) && (exp[y] != '|') && (exp[y] != ')');++y)
  57. if(exp[y] == '\\')
  58. if(!exp[++y])
  59. return INVALID_SXP;
  60. if(!exp[y])
  61. return INVALID_SXP;
  62. t = valid_subexp(&exp[x],exp[y]);
  63. if(t == INVALID_SXP)
  64. return INVALID_SXP;
  65. x+=t;
  66. if(exp[x] == ')') {
  67. break;
  68. }
  69. }
  70. break;
  71. case ')':
  72. case ']':
  73. return INVALID_SXP;
  74. case '\\':
  75. if(!exp[++x])
  76. return INVALID_SXP;
  77. default:
  78. break;
  79. }
  80. ++x;
  81. }
  82. if((!stop) && (!nsc))
  83. return NON_SXP;
  84. return ((exp[x] == stop) ? x : INVALID_SXP);
  85. }
  86. NSAPI_PUBLIC int shexp_valid(char *exp) {
  87. int x;
  88. x = valid_subexp(exp, '\0');
  89. return (x < 0 ? x : VALID_SXP);
  90. }
  91. /* ----------------------------- shexp_match ----------------------------- */
  92. #define MATCH 0
  93. #define NOMATCH 1
  94. #define ABORTED -1
  95. int _shexp_match(char *str, char *exp);
  96. int handle_union(char *str, char *exp)
  97. {
  98. char *e2 = (char *) MALLOC(sizeof(char)*strlen(exp));
  99. register int t,p2,p1 = 1;
  100. int cp;
  101. while(1) {
  102. for(cp=1;exp[cp] != ')';cp++)
  103. if(exp[cp] == '\\')
  104. ++cp;
  105. for(p2 = 0;(exp[p1] != '|') && (p1 != cp);p1++,p2++) {
  106. if(exp[p1] == '\\')
  107. e2[p2++] = exp[p1++];
  108. e2[p2] = exp[p1];
  109. }
  110. for(t=cp+1;(e2[p2] = exp[t]);++t,++p2);
  111. if(_shexp_match(str,e2) == MATCH) {
  112. FREE(e2);
  113. return MATCH;
  114. }
  115. if(p1 == cp) {
  116. FREE(e2);
  117. return NOMATCH;
  118. }
  119. else ++p1;
  120. }
  121. }
  122. int _shexp_match(char *str, char *exp)
  123. {
  124. register int x,y;
  125. int ret,neg;
  126. ret = 0;
  127. for(x=0,y=0;exp[y];++y,++x) {
  128. if((!str[x]) && (exp[y] != '(') && (exp[y] != '$') && (exp[y] != '*'))
  129. ret = ABORTED;
  130. else {
  131. switch(exp[y]) {
  132. case '$':
  133. if( (str[x]) )
  134. ret = NOMATCH;
  135. else
  136. --x; /* we don't want loop to increment x */
  137. break;
  138. case '*':
  139. while(exp[++y] == '*');
  140. if(!exp[y])
  141. return MATCH;
  142. while(str[x]) {
  143. switch(_shexp_match(&str[x++],&exp[y])) {
  144. case NOMATCH:
  145. continue;
  146. case ABORTED:
  147. ret = ABORTED;
  148. break;
  149. default:
  150. return MATCH;
  151. }
  152. break;
  153. }
  154. if((exp[y] == '$') && (exp[y+1] == '\0') && (!str[x]))
  155. return MATCH;
  156. else
  157. ret = ABORTED;
  158. break;
  159. case '[':
  160. if((neg = ((exp[++y] == '^') && (exp[y+1] != ']'))))
  161. ++y;
  162. if((isalnum(exp[y])) && (exp[y+1] == '-') &&
  163. (isalnum(exp[y+2])) && (exp[y+3] == ']'))
  164. {
  165. int start = exp[y], end = exp[y+2];
  166. /* Droolproofing for pinheads not included */
  167. if(neg ^ ((str[x] < start) || (str[x] > end))) {
  168. ret = NOMATCH;
  169. break;
  170. }
  171. y+=3;
  172. }
  173. else {
  174. int matched;
  175. for(matched=0;exp[y] != ']';y++)
  176. matched |= (str[x] == exp[y]);
  177. if(neg ^ (!matched))
  178. ret = NOMATCH;
  179. }
  180. break;
  181. case '(':
  182. return handle_union(&str[x],&exp[y]);
  183. break;
  184. case '?':
  185. break;
  186. case '\\':
  187. ++y;
  188. default:
  189. if(str[x] != exp[y])
  190. ret = NOMATCH;
  191. break;
  192. }
  193. }
  194. if(ret)
  195. break;
  196. }
  197. return (ret ? ret : (str[x] ? NOMATCH : MATCH));
  198. }
  199. NSAPI_PUBLIC int shexp_match(char *str, char *xp) {
  200. register int x;
  201. char *exp = STRDUP(xp);
  202. for(x=strlen(exp)-1;x;--x) {
  203. if((exp[x] == '~') && (exp[x-1] != '\\')) {
  204. exp[x] = '\0';
  205. if(_shexp_match(str,&exp[++x]) == MATCH)
  206. goto punt;
  207. break;
  208. }
  209. }
  210. if(_shexp_match(str,exp) == MATCH) {
  211. FREE(exp);
  212. return 0;
  213. }
  214. punt:
  215. FREE(exp);
  216. return 1;
  217. }
  218. /* ------------------------------ shexp_cmp ------------------------------- */
  219. NSAPI_PUBLIC int shexp_cmp(char *str, char *exp)
  220. {
  221. switch(shexp_valid(exp)) {
  222. case INVALID_SXP:
  223. return -1;
  224. case NON_SXP:
  225. return (strcmp(exp,str) ? 1 : 0);
  226. default:
  227. return shexp_match(str, exp);
  228. }
  229. }
  230. /* ---------------------------- shexp_casecmp ----------------------------- */
  231. NSAPI_PUBLIC int shexp_casecmp(char *str, char *exp)
  232. {
  233. char *lstr = STRDUP(str), *lexp = STRDUP(exp), *t;
  234. int ret;
  235. for(t = lstr; *t; t++)
  236. if(isalpha(*t)) *t = tolower(*t);
  237. for(t = lexp; *t; t++)
  238. if(isalpha(*t)) *t = tolower(*t);
  239. switch(shexp_valid(lexp)) {
  240. case INVALID_SXP:
  241. ret = -1;
  242. break;
  243. case NON_SXP:
  244. ret = (strcmp(lexp, lstr) ? 1 : 0);
  245. break;
  246. default:
  247. ret = shexp_match(lstr, lexp);
  248. }
  249. FREE(lstr);
  250. FREE(lexp);
  251. return ret;
  252. }