shexp.cpp 9.3 KB

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