shexp.cpp 9.2 KB

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