lua-regex.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /******************************************************************************
  2. * Copyright (C) 1994-2008 Lua.org, PUC-Rio. All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. ******************************************************************************/
  23. /*
  24. Adapted by Domingo Alvarez Duarte 2012
  25. http://code.google.com/p/lua-regex-standalone/
  26. */
  27. #include <assert.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <limits.h>
  32. #include <ctype.h>
  33. #include <inttypes.h>
  34. #include "lua-regex.h"
  35. /* macro to `unsign' a character */
  36. #define uchar(c) ((unsigned char)(c))
  37. #define L_ESC '%'
  38. #define SPECIALS "^$*+?.([%-"
  39. #define LUA_QL(x) "'" x "'"
  40. #define LUA_QS LUA_QL("%s")
  41. static ptrdiff_t posrelat (ptrdiff_t pos, size_t len) {
  42. /* relative string position: negative means back from end */
  43. if (pos < 0) pos += (ptrdiff_t)len;
  44. return (pos >= 0) ? pos : 0;
  45. }
  46. static int check_capture_all_closed (LuaMatchState *ms) {
  47. int i;
  48. for(i=0; i<ms->level; ++i){
  49. if(ms->capture[i].len == CAP_UNFINISHED){
  50. ms->error = "unfinished capture";
  51. return 0;
  52. }
  53. }
  54. return 1;
  55. }
  56. static int check_capture_is_closed (LuaMatchState *ms, int l) {
  57. if (l < 0 || l >= ms->level){
  58. ms->error = "invalid capture index";
  59. return 0;
  60. }
  61. if (ms->capture[l].len == CAP_UNFINISHED){
  62. ms->error = "unfinished capture";
  63. return 0;
  64. }
  65. return 1;
  66. }
  67. static int check_capture (LuaMatchState *ms, int *l_out) {
  68. int l;
  69. *l_out -= '1';
  70. l = *l_out;
  71. return check_capture_is_closed(ms, l);
  72. }
  73. static int capture_to_close (LuaMatchState *ms, int *level_out) {
  74. int level = ms->level;
  75. for (level--; level>=0; level--)
  76. if (ms->capture[level].len == CAP_UNFINISHED) {
  77. *level_out = level;
  78. return 1;
  79. }
  80. ms->error = "invalid pattern capture";
  81. return 0;
  82. }
  83. static int classend (LuaMatchState *ms, const char *p, const char **result) {
  84. switch (*p++) {
  85. case L_ESC: {
  86. if (p == ms->p_end){
  87. ms->error = "malformed pattern (ends with " LUA_QL("%%") ")";
  88. return 0;
  89. }
  90. *result = p+1;
  91. return 1;
  92. }
  93. case '[': {
  94. if (*p == '^') p++;
  95. do { /* look for a `]' */
  96. if (p == ms->p_end){
  97. ms->error = "malformed pattern (missing " LUA_QL("]") ")";
  98. return 0;
  99. }
  100. if (*(p++) == L_ESC && p < ms->p_end)
  101. p++; /* skip escapes (e.g. `%]') */
  102. } while (*p != ']');
  103. *result = p+1;
  104. return 1;
  105. }
  106. default: {
  107. *result = p;
  108. return 1;
  109. }
  110. }
  111. }
  112. static int match_class (int c, int cl) {
  113. int res;
  114. switch (tolower(cl)) {
  115. case 'a' : res = isalpha(c); break;
  116. case 'c' : res = iscntrl(c); break;
  117. case 'd' : res = isdigit(c); break;
  118. case 'g' : res = isgraph(c); break;
  119. case 'l' : res = islower(c); break;
  120. case 'p' : res = ispunct(c); break;
  121. case 's' : res = isspace(c); break;
  122. case 'u' : res = isupper(c); break;
  123. case 'w' : res = isalnum(c); break;
  124. case 'x' : res = isxdigit(c); break;
  125. case 'z' : res = (c == 0); break; /* deprecated option */
  126. default: return (cl == c);
  127. }
  128. return (islower(cl) ? res : !res);
  129. }
  130. static int matchbracketclass (int c, const char *p, const char *ec) {
  131. int sig = 1;
  132. if (*(p+1) == '^') {
  133. sig = 0;
  134. p++; /* skip the `^' */
  135. }
  136. while (++p < ec) {
  137. if (*p == L_ESC) {
  138. p++;
  139. if (match_class(c, uchar(*p)))
  140. return sig;
  141. }
  142. else if ((*(p+1) == '-') && (p+2 < ec)) {
  143. p+=2;
  144. if (uchar(*(p-2)) <= c && c <= uchar(*p))
  145. return sig;
  146. }
  147. else if (uchar(*p) == c) return sig;
  148. }
  149. return !sig;
  150. }
  151. static int singlematch (int c, const char *p, const char *ep) {
  152. switch (*p) {
  153. case '.': return 1; /* matches any char */
  154. case L_ESC: return match_class(c, uchar(*(p+1)));
  155. case '[': return matchbracketclass(c, p, ep-1);
  156. default: return (uchar(*p) == c);
  157. }
  158. }
  159. static const char *match (LuaMatchState *ms, const char *s, const char *p);
  160. //add escape char extension from https://github.com/jcgoble3/lua-matchext
  161. static const char *matchbalance (LuaMatchState *ms, const char *s,
  162. const char *p) {
  163. int escaped = (*(p-1) == 'B'); /* EXT */
  164. if (p >= ms->p_end - 1 - escaped){
  165. ms->error = "malformed pattern "
  166. "(missing arguments to " LUA_QL("%%b") ")";
  167. return NULL;
  168. }
  169. if (*s != *p) return NULL;
  170. else {
  171. int b = *p;
  172. int e = *(p + (escaped ? 2 : 1)); /* EXT */
  173. int esc = escaped ? *(p + 1) : INT_MAX; /* EXT */
  174. int cont = 1;
  175. while (++s < ms->src_end) {
  176. if (*s == esc) s++; /* EXT */
  177. else if (*s == e) {
  178. if (--cont == 0) return s+1;
  179. }
  180. else if (*s == b) cont++;
  181. }
  182. }
  183. return NULL; /* string ends out of balance */
  184. }
  185. static const char *max_expand (LuaMatchState *ms, const char *s,
  186. const char *p, const char *ep) {
  187. ptrdiff_t i = 0; /* counts maximum expand for item */
  188. while ((s+i)<ms->src_end && singlematch(uchar(*(s+i)), p, ep))
  189. i++;
  190. /* keeps trying to match with the maximum repetitions */
  191. while (i>=0) {
  192. const char *res = match(ms, (s+i), ep+1);
  193. if (res) return res;
  194. i--; /* else didn't match; reduce 1 repetition to try again */
  195. }
  196. return NULL;
  197. }
  198. static const char *min_expand (LuaMatchState *ms, const char *s,
  199. const char *p, const char *ep) {
  200. for (;;) {
  201. const char *res = match(ms, s, ep+1);
  202. if (res != NULL)
  203. return res;
  204. else if (s<ms->src_end && singlematch(uchar(*s), p, ep))
  205. s++; /* try with one more repetition */
  206. else return NULL;
  207. }
  208. }
  209. static const char *start_capture (LuaMatchState *ms, const char *s,
  210. const char *p, int what) {
  211. const char *res;
  212. int level = ms->level;
  213. if (level >= LUA_REGEX_MAXCAPTURES) {
  214. ms->error = "too many captures";
  215. return NULL;
  216. }
  217. ms->capture[level].init = s;
  218. ms->capture[level].len = what;
  219. ms->level = level+1;
  220. if ((res=match(ms, s, p)) == NULL) /* match failed? */
  221. ms->level--; /* undo capture */
  222. return res;
  223. }
  224. static const char *end_capture (LuaMatchState *ms, const char *s,
  225. const char *p) {
  226. int l;
  227. const char *res;
  228. if(!capture_to_close(ms, &l)) return NULL;
  229. ms->capture[l].len = s - ms->capture[l].init; /* close capture */
  230. if ((res = match(ms, s, p)) == NULL) /* match failed? */
  231. ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
  232. return res;
  233. }
  234. static const char *match_capture (LuaMatchState *ms, const char *s, int l) {
  235. size_t len;
  236. if(check_capture(ms, &l)){
  237. len = ms->capture[l].len;
  238. if ((size_t)(ms->src_end-s) >= len &&
  239. memcmp(ms->capture[l].init, s, len) == 0)
  240. return s+len;
  241. }
  242. return NULL;
  243. }
  244. static const char *match (LuaMatchState *ms, const char *s, const char *p) {
  245. init: /* using goto's to optimize tail recursion */
  246. if (p == ms->p_end) /* end of pattern? */
  247. return s; /* match succeeded */
  248. switch (*p) {
  249. case '(': { /* start capture */
  250. if (*(p+1) == ')') /* position capture? */
  251. return start_capture(ms, s, p+2, CAP_POSITION);
  252. else
  253. return start_capture(ms, s, p+1, CAP_UNFINISHED);
  254. }
  255. case ')': { /* end capture */
  256. return end_capture(ms, s, p+1);
  257. }
  258. case '$': {
  259. if ((p+1) == ms->p_end) /* is the `$' the last char in pattern? */
  260. return (s == ms->src_end) ? s : NULL; /* check end of string */
  261. else goto dflt;
  262. }
  263. case L_ESC: { /* escaped sequences not in the format class[*+?-]? */
  264. switch (*(p+1)) {
  265. case 'b': case 'B': { /* balanced string? */ /* EXT */
  266. s = matchbalance(ms, s, p+2);
  267. if (s == NULL) return NULL;
  268. p += (*(p + 1) == 'b') ? 4 : 5; /* EXT */ goto init; /* else return match(ms, s, p+4); */
  269. }
  270. case 'f': { /* frontier? */
  271. const char *ep; char previous;
  272. p += 2;
  273. if (*p != '['){
  274. ms->error = "missing " LUA_QL("[") " after "
  275. LUA_QL("%%f") " in pattern";
  276. return NULL;
  277. }
  278. if(!classend(ms, p, &ep)) return NULL; /* points to what is next */
  279. previous = (s == ms->src_init) ? '\0' : *(s-1);
  280. if (matchbracketclass(uchar(previous), p, ep-1) ||
  281. !matchbracketclass(uchar(*s), p, ep-1)) return NULL;
  282. p=ep; goto init; /* else return match(ms, s, ep); */
  283. }
  284. case '0': case '1': case '2': case '3':
  285. case '4': case '5': case '6': case '7':
  286. case '8': case '9': { /* capture results (%0-%9)? */
  287. s = match_capture(ms, s, uchar(*(p+1)));
  288. if (s == NULL) return NULL;
  289. p+=2; goto init; /* else return match(ms, s, p+2) */
  290. }
  291. default: goto dflt;
  292. }
  293. }
  294. default: dflt: { /* pattern class plus optional suffix */
  295. const char *ep;
  296. int m;
  297. if(!classend(ms, p, &ep)) return NULL; /* points to what is next */
  298. m = s < ms->src_end && singlematch(uchar(*s), p, ep);
  299. switch (*ep) {
  300. case '?': { /* optional */
  301. const char *res;
  302. if (m && ((res=match(ms, s+1, ep+1)) != NULL))
  303. return res;
  304. p=ep+1; goto init; /* else return match(ms, s, ep+1); */
  305. }
  306. case '*': { /* 0 or more repetitions */
  307. return max_expand(ms, s, p, ep);
  308. }
  309. case '+': { /* 1 or more repetitions */
  310. return (m ? max_expand(ms, s+1, p, ep) : NULL);
  311. }
  312. case '-': { /* 0 or more repetitions (minimum) */
  313. return min_expand(ms, s, p, ep);
  314. }
  315. default: {
  316. if (!m) return NULL;
  317. s++; p=ep; goto init; /* else return match(ms, s+1, ep); */
  318. }
  319. }
  320. }
  321. }
  322. }
  323. static const char *lmemfind (const char *s1, size_t l1,
  324. const char *s2, size_t l2) {
  325. if (l2 == 0) return s1; /* empty strings are everywhere */
  326. else if (l2 > l1) return NULL; /* avoids a negative `l1' */
  327. else {
  328. const char *init; /* to search for a `*s2' inside `s1' */
  329. l2--; /* 1st char will be checked by `memchr' */
  330. l1 = l1-l2; /* `s2' cannot be found after that */
  331. while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
  332. init++; /* 1st char is already checked */
  333. if (memcmp(init, s2+1, l2) == 0)
  334. return init-1;
  335. else { /* correct `l1' and `s1' to try again */
  336. l1 -= init-s1;
  337. s1 = init;
  338. }
  339. }
  340. return NULL; /* not found */
  341. }
  342. }
  343. /* check whether pattern has no special characters */
  344. static int nospecials (const char *p, size_t l) {
  345. size_t upto = 0;
  346. do {
  347. if (strpbrk(p + upto, SPECIALS))
  348. return 0; /* pattern has a special character */
  349. upto += strlen(p + upto) + 1; /* may have more after \0 */
  350. } while (upto <= l);
  351. return 1; /* no special chars found */
  352. }
  353. static ptrdiff_t str_find_aux (LuaMatchState *ms, int find, const char *s, ptrdiff_t ls,
  354. const char *p, ptrdiff_t lp, ptrdiff_t init, int raw_find,
  355. luaregex_func_param fp, void *udata) {
  356. ptrdiff_t result;
  357. ms->error = NULL;
  358. if(ls < 0) ls = strlen(s);
  359. assert(ls >= 0);
  360. if(lp < 0) lp = strlen(p);
  361. assert(lp >= 0);
  362. init = posrelat(init, ls);
  363. if (init < 0) init = 0;
  364. else if (init > ls + 1) { /* start after string's end? */
  365. return 0; /* cannot find anything */
  366. }
  367. ms->src_init = s;
  368. ms->src_end = s + ls;
  369. do_again:
  370. result = -1; /* not found */
  371. /* explicit request or no special characters? */
  372. if (find && (raw_find || nospecials(p, lp))) {
  373. /* do a plain search */
  374. const char *s2 = lmemfind(s + init, ls - init, p, lp);
  375. if (s2) {
  376. ms->start_pos = ((int)(s2 - s));
  377. result = ms->end_pos = ms->start_pos+lp;
  378. ms->level = 0;
  379. }
  380. }
  381. else {
  382. const char *s1 = s + init;
  383. int anchor = (*p == '^');
  384. if (anchor) {
  385. p++; lp--; /* skip anchor character */
  386. }
  387. ms->p_end = p + lp;
  388. do {
  389. const char *res;
  390. ms->level = 0;
  391. if ((res=match(ms, s1, p)) != NULL) {
  392. ms->start_pos = s1-s;
  393. result = ms->end_pos = res-s;
  394. goto eofunc;
  395. }
  396. } while (s1++ < ms->src_end && !anchor);
  397. }
  398. eofunc:
  399. if(result >= 0){
  400. if(!check_capture_all_closed(ms)) return 0;
  401. if(fp && (*fp)(ms, udata, 0)) {
  402. init = result;
  403. if (init == ms->start_pos) ++init; /* empty match? go at least one position */
  404. if (init < ls) goto do_again;
  405. }
  406. }
  407. return result > 0 ? ms->start_pos : result; //returning the start position
  408. }
  409. ptrdiff_t lua_str_find (LuaMatchState *ms, const char *s, ptrdiff_t ls,
  410. const char *p, ptrdiff_t lp, ptrdiff_t init, int raw_find,
  411. luaregex_func_param fp, void *udata) {
  412. return str_find_aux(ms, 1, s, ls, p, lp, init, raw_find, fp, udata);
  413. }
  414. ptrdiff_t lua_str_match (LuaMatchState *ms, const char *s, ptrdiff_t ls,
  415. const char *p, ptrdiff_t lp, ptrdiff_t init, int raw_find,
  416. luaregex_func_param fp, void *udata) {
  417. return str_find_aux(ms, 0, s, ls, p, lp, init, raw_find, fp, udata);
  418. }
  419. #define MIN_ALLOC_SIZE 2048
  420. #define NEW_SIZE(sz) (((sz/MIN_ALLOC_SIZE)+1)*MIN_ALLOC_SIZE)
  421. int char_buffer_add_char(LuaMatchState *ms, lua_char_buffer_st **b, char c){
  422. lua_char_buffer_st *tmp = *b;
  423. if(tmp->used+1 >= tmp->size){
  424. int new_size = tmp->size+MIN_ALLOC_SIZE;
  425. tmp = (lua_char_buffer_st*)realloc(tmp, sizeof(lua_char_buffer_st) + new_size);
  426. if(!tmp){
  427. ms->error = "not enough memory when reallocating";
  428. return 0;
  429. }
  430. *b = tmp;
  431. tmp->size = new_size;
  432. }
  433. tmp->buf[tmp->used++] = c;
  434. return 1;
  435. }
  436. int char_buffer_add_str(LuaMatchState *ms, lua_char_buffer_st **b, const char *str, ptrdiff_t len){
  437. lua_char_buffer_st *tmp = *b;
  438. if(len < 0) len = strlen(str);
  439. assert(len >= 0);
  440. if(tmp->used+len >= tmp->size){
  441. size_t new_size = tmp->size + NEW_SIZE(len);
  442. tmp = (lua_char_buffer_st*)realloc(tmp, sizeof(lua_char_buffer_st) + new_size);
  443. if(!tmp){
  444. ms->error = "not enough memory when reallocating";
  445. return 0;
  446. }
  447. *b = tmp;
  448. tmp->size = new_size;
  449. }
  450. memcpy(&tmp->buf[tmp->used], str, len);
  451. tmp->used += len;
  452. return 1;
  453. }
  454. static int add_value (LuaMatchState *ms, lua_char_buffer_st **b, const char *s,
  455. const char *e, const char *news, size_t lnews) {
  456. size_t i;
  457. for (i = 0; i < lnews; i++) {
  458. if (news[i] != L_ESC){
  459. if(!char_buffer_add_char(ms, b, news[i])) return 0;
  460. }
  461. else {
  462. i++; /* skip ESC */
  463. if (!isdigit(uchar(news[i]))) {
  464. if (news[i] != L_ESC){
  465. ms->error = "invalid use of replacement string";
  466. return 0;
  467. }
  468. if(!char_buffer_add_char(ms, b, news[i])) return 0;
  469. }
  470. else if (news[i] == '0'){
  471. if(!char_buffer_add_str(ms, b, s, e - s)) return 0;
  472. }
  473. else {
  474. int il = news[i] - '1';
  475. if (il >= ms->level) {
  476. if (il == 0){ /* ms->level == 0, too */
  477. if(!char_buffer_add_str(ms, b, s, e - s)) return 0; /* add whole match */
  478. }
  479. else{
  480. ms->error = "invalid capture index";
  481. return 0;
  482. }
  483. }
  484. else {
  485. ptrdiff_t cl = ms->capture[il].len;
  486. if (cl == CAP_UNFINISHED) {
  487. ms->error = "unfinished capture";
  488. return 0;
  489. }
  490. if (cl == CAP_POSITION){
  491. char buf[32];
  492. snprintf(buf, sizeof(buf), "%" PRIdPTR, ms->capture[il].init - ms->src_init + 1);
  493. if(!char_buffer_add_str(ms, b, buf, strlen(buf))) return 0;
  494. }
  495. else
  496. if(!char_buffer_add_str(ms, b, ms->capture[il].init, cl)) return 0;
  497. }
  498. }
  499. }
  500. }
  501. return 1;
  502. }
  503. lua_char_buffer_st *lua_str_gsub (const char *src, ptrdiff_t srcl, const char *p, ptrdiff_t lp,
  504. const char *tr, ptrdiff_t ltr, size_t max_s, const char **error_ptr,
  505. luaregex_func_param fp, void *udata) {
  506. int anchor;
  507. size_t n;
  508. LuaMatchState ms;
  509. lua_char_buffer_st *b;
  510. if(srcl < 0) srcl = strlen(src);
  511. assert(srcl >= 0);
  512. if(lp < 0) lp = strlen(p);
  513. assert(lp >= 0);
  514. if(ltr < 0) ltr = strlen(tr);
  515. assert(ltr >= 0);
  516. if(max_s == 0) max_s = srcl+1;
  517. anchor = (*p == '^');
  518. n = NEW_SIZE(srcl);
  519. b = (lua_char_buffer_st*)malloc(sizeof(lua_char_buffer_st) + n);
  520. if(!b) return NULL;
  521. b->size = n;
  522. b->used = 0;
  523. n = 0;
  524. if (anchor) {
  525. p++; lp--; /* skip anchor character */
  526. }
  527. ms.error = 0;
  528. ms.start_pos = ms.end_pos = 0;
  529. ms.src_init = src;
  530. ms.src_end = src+srcl;
  531. ms.p_end = p + lp;
  532. while (n < max_s) {
  533. const char *e;
  534. ms.level = 0;
  535. e = match(&ms, src, p);
  536. if(ms.error || !check_capture_all_closed(&ms)) goto free_and_null;
  537. if (e) {
  538. n++;
  539. if(fp){
  540. ms.end_pos = e-ms.src_init;
  541. if(!(*fp)(&ms, udata, &b)) goto free_and_null;
  542. }
  543. else if(!add_value(&ms, &b, src, e, tr, ltr)) goto free_and_null;
  544. }
  545. if (e && e>src){ /* non empty match? */
  546. ms.start_pos = e-ms.src_init;
  547. src = e; /* skip it */
  548. }
  549. else if (src < ms.src_end){
  550. if(!char_buffer_add_char(&ms, &b, *src++)) goto free_and_null;
  551. ++ms.start_pos;
  552. }
  553. else break;
  554. if (anchor) break;
  555. }
  556. if(!char_buffer_add_str(&ms, &b, src, ms.src_end-src)) goto free_and_null;
  557. b->buf[b->used] = '\0';
  558. return b;
  559. free_and_null:
  560. if(b) free(b);
  561. if(error_ptr) *error_ptr = ms.error;
  562. return NULL;
  563. }