shexp.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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.c: shell-like wildcard match routines
  43. *
  44. *
  45. * See shexp.h for public documentation.
  46. *
  47. * Rob McCool
  48. *
  49. */
  50. #include "shexp.h"
  51. #include <ctype.h> /* isalpha, tolower */
  52. /* ----------------------------- shexp_valid ------------------------------ */
  53. int valid_subexp(char *exp, char stop)
  54. {
  55. register int x,y,t;
  56. int nsc,tld;
  57. x=0;nsc=0;tld=0;
  58. while(exp[x] && (exp[x] != stop)) {
  59. switch(exp[x]) {
  60. case '~':
  61. if(tld) return INVALID_SXP;
  62. else ++tld;
  63. case '*':
  64. case '?':
  65. case '^':
  66. case '$':
  67. ++nsc;
  68. break;
  69. case '[':
  70. ++nsc;
  71. if((!exp[++x]) || (exp[x] == ']'))
  72. return INVALID_SXP;
  73. for(++x;exp[x] && (exp[x] != ']');++x)
  74. if(exp[x] == '\\')
  75. if(!exp[++x])
  76. return INVALID_SXP;
  77. if(!exp[x])
  78. return INVALID_SXP;
  79. break;
  80. case '(':
  81. ++nsc;
  82. while(1) {
  83. if(exp[++x] == ')')
  84. return INVALID_SXP;
  85. for(y=x;(exp[y]) && (exp[y] != '|') && (exp[y] != ')');++y)
  86. if(exp[y] == '\\')
  87. if(!exp[++y])
  88. return INVALID_SXP;
  89. if(!exp[y])
  90. return INVALID_SXP;
  91. t = valid_subexp(&exp[x],exp[y]);
  92. if(t == INVALID_SXP)
  93. return INVALID_SXP;
  94. x+=t;
  95. if(exp[x] == ')') {
  96. break;
  97. }
  98. }
  99. break;
  100. case ')':
  101. case ']':
  102. return INVALID_SXP;
  103. case '\\':
  104. if(!exp[++x])
  105. return INVALID_SXP;
  106. default:
  107. break;
  108. }
  109. ++x;
  110. }
  111. if((!stop) && (!nsc))
  112. return NON_SXP;
  113. return ((exp[x] == stop) ? x : INVALID_SXP);
  114. }
  115. NSAPI_PUBLIC int shexp_valid(char *exp) {
  116. int x;
  117. x = valid_subexp(exp, '\0');
  118. return (x < 0 ? x : VALID_SXP);
  119. }
  120. /* ----------------------------- shexp_match ----------------------------- */
  121. #define MATCH 0
  122. #define NOMATCH 1
  123. #define ABORTED -1
  124. int _shexp_match(char *str, char *exp);
  125. int handle_union(char *str, char *exp)
  126. {
  127. char *e2 = (char *) MALLOC(sizeof(char)*strlen(exp));
  128. register int t,p2,p1 = 1;
  129. int cp;
  130. while(1) {
  131. for(cp=1;exp[cp] != ')';cp++)
  132. if(exp[cp] == '\\')
  133. ++cp;
  134. for(p2 = 0;(exp[p1] != '|') && (p1 != cp);p1++,p2++) {
  135. if(exp[p1] == '\\')
  136. e2[p2++] = exp[p1++];
  137. e2[p2] = exp[p1];
  138. }
  139. for(t=cp+1;(e2[p2] = exp[t]);++t,++p2);
  140. if(_shexp_match(str,e2) == MATCH) {
  141. FREE(e2);
  142. return MATCH;
  143. }
  144. if(p1 == cp) {
  145. FREE(e2);
  146. return NOMATCH;
  147. }
  148. else ++p1;
  149. }
  150. }
  151. int _shexp_match(char *str, char *exp)
  152. {
  153. register int x,y;
  154. int ret,neg;
  155. ret = 0;
  156. for(x=0,y=0;exp[y];++y,++x) {
  157. if((!str[x]) && (exp[y] != '(') && (exp[y] != '$') && (exp[y] != '*'))
  158. ret = ABORTED;
  159. else {
  160. switch(exp[y]) {
  161. case '$':
  162. if( (str[x]) )
  163. ret = NOMATCH;
  164. else
  165. --x; /* we don't want loop to increment x */
  166. break;
  167. case '*':
  168. while(exp[++y] == '*');
  169. if(!exp[y])
  170. return MATCH;
  171. while(str[x]) {
  172. switch(_shexp_match(&str[x++],&exp[y])) {
  173. case NOMATCH:
  174. continue;
  175. case ABORTED:
  176. ret = ABORTED;
  177. break;
  178. default:
  179. return MATCH;
  180. }
  181. break;
  182. }
  183. if((exp[y] == '$') && (exp[y+1] == '\0') && (!str[x]))
  184. return MATCH;
  185. else
  186. ret = ABORTED;
  187. break;
  188. case '[':
  189. if((neg = ((exp[++y] == '^') && (exp[y+1] != ']'))))
  190. ++y;
  191. if((isalnum(exp[y])) && (exp[y+1] == '-') &&
  192. (isalnum(exp[y+2])) && (exp[y+3] == ']'))
  193. {
  194. int start = exp[y], end = exp[y+2];
  195. /* Droolproofing for pinheads not included */
  196. if(neg ^ ((str[x] < start) || (str[x] > end))) {
  197. ret = NOMATCH;
  198. break;
  199. }
  200. y+=3;
  201. }
  202. else {
  203. int matched;
  204. for(matched=0;exp[y] != ']';y++)
  205. matched |= (str[x] == exp[y]);
  206. if(neg ^ (!matched))
  207. ret = NOMATCH;
  208. }
  209. break;
  210. case '(':
  211. return handle_union(&str[x],&exp[y]);
  212. break;
  213. case '?':
  214. break;
  215. case '\\':
  216. ++y;
  217. default:
  218. #ifdef XP_UNIX
  219. if(str[x] != exp[y])
  220. #else /* XP_WIN32 */
  221. if(strnicmp(str + x, exp + y, 1))
  222. #endif /* XP_WIN32 */
  223. ret = NOMATCH;
  224. break;
  225. }
  226. }
  227. if(ret)
  228. break;
  229. }
  230. return (ret ? ret : (str[x] ? NOMATCH : MATCH));
  231. }
  232. NSAPI_PUBLIC int shexp_match(char *str, char *xp) {
  233. register int x;
  234. char *exp = STRDUP(xp);
  235. for(x=strlen(exp)-1;x;--x) {
  236. if((exp[x] == '~') && (exp[x-1] != '\\')) {
  237. exp[x] = '\0';
  238. if(_shexp_match(str,&exp[++x]) == MATCH)
  239. goto punt;
  240. break;
  241. }
  242. }
  243. if(_shexp_match(str,exp) == MATCH) {
  244. FREE(exp);
  245. return 0;
  246. }
  247. punt:
  248. FREE(exp);
  249. return 1;
  250. }
  251. /* ------------------------------ shexp_cmp ------------------------------- */
  252. NSAPI_PUBLIC int shexp_cmp(char *str, char *exp)
  253. {
  254. switch(shexp_valid(exp)) {
  255. case INVALID_SXP:
  256. return -1;
  257. case NON_SXP:
  258. #ifdef XP_UNIX
  259. return (strcmp(exp,str) ? 1 : 0);
  260. #else /* XP_WIN32 */
  261. return (stricmp(exp,str) ? 1 : 0);
  262. #endif /* XP_WIN32 */
  263. default:
  264. return shexp_match(str, exp);
  265. }
  266. }
  267. /* ---------------------------- shexp_casecmp ----------------------------- */
  268. NSAPI_PUBLIC int shexp_casecmp(char *str, char *exp)
  269. {
  270. char *lstr = STRDUP(str), *lexp = STRDUP(exp), *t;
  271. int ret;
  272. for(t = lstr; *t; t++)
  273. if(isalpha(*t)) *t = tolower(*t);
  274. for(t = lexp; *t; t++)
  275. if(isalpha(*t)) *t = tolower(*t);
  276. switch(shexp_valid(lexp)) {
  277. case INVALID_SXP:
  278. ret = -1;
  279. break;
  280. case NON_SXP:
  281. ret = (strcmp(lexp, lstr) ? 1 : 0);
  282. break;
  283. default:
  284. ret = shexp_match(lstr, lexp);
  285. }
  286. FREE(lstr);
  287. FREE(lexp);
  288. return ret;
  289. }