shexp.cpp 9.4 KB

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