rregexec.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "regexp.h"
  4. #include "regcomp.h"
  5. static Resublist sempty; /* empty set of matches */
  6. /*
  7. * return 0 if no match
  8. * >0 if a match
  9. * <0 if we ran out of _relist space
  10. */
  11. static int
  12. rregexec1(Reprog *progp, /* program to run */
  13. wchar_t *bol, /* string to run machine on */
  14. Resub *mp, /* subexpression elements */
  15. int ms, /* number of elements at mp */
  16. wchar_t *starts,
  17. wchar_t *eol,
  18. wchar_t startchar)
  19. {
  20. int flag=0;
  21. Reinst *inst;
  22. Relist *tlp;
  23. wchar_t *s;
  24. int i, checkstart;
  25. wchar_t r, *rp, *ep;
  26. int n;
  27. Relist* tl; /* This list, next list */
  28. Relist* nl;
  29. Relist* tle; /* ends of this and next list */
  30. Relist* nle;
  31. int match;
  32. match = 0;
  33. checkstart = startchar;
  34. sempty.m[0].s.rsp = 0;
  35. if(mp!=0)
  36. for(i=0; i<ms; i++)
  37. mp[i].s.rsp = mp[i].e.rep = 0;
  38. _relist[0][0].inst = _relist[1][0].inst = 0;
  39. /* Execute machine once for each character, including terminal NUL */
  40. s = starts;
  41. do{
  42. r = *s;
  43. /* fast check for first char */
  44. if(checkstart && r!=startchar){
  45. s++;
  46. continue;
  47. }
  48. /* switch run lists */
  49. tl = _relist[flag];
  50. tle = _reliste[flag];
  51. nl = _relist[flag^=1];
  52. nle = _reliste[flag];
  53. nl->inst = 0;
  54. /* Add first instruction to current list */
  55. sempty.m[0].s.rsp = s;
  56. _renewthread(tl, progp->startinst, &sempty);
  57. /* Execute machine until current list is empty */
  58. for(tlp=tl; tlp->inst; tlp++){ /* assignment = */
  59. if(s == eol)
  60. break;
  61. for(inst=tlp->inst; ; inst = inst->l.next){
  62. switch(inst->type){
  63. case RUNE: /* regular character */
  64. if(inst->r.r == r)
  65. if(_renewthread(nl, inst->l.next, &tlp->se)==nle)
  66. return -1;
  67. break;
  68. case LBRA:
  69. tlp->se.m[inst->r.subid].s.rsp = s;
  70. continue;
  71. case RBRA:
  72. tlp->se.m[inst->r.subid].e.rep = s;
  73. continue;
  74. case ANY:
  75. if(r != '\n')
  76. if(_renewthread(nl, inst->l.next, &tlp->se)==nle)
  77. return -1;
  78. break;
  79. case ANYNL:
  80. if(_renewthread(nl, inst->l.next, &tlp->se)==nle)
  81. return -1;
  82. break;
  83. case BOL:
  84. if(s == bol || *(s-1) == '\n')
  85. continue;
  86. break;
  87. case EOL:
  88. if(r == 0 || r == '\n')
  89. continue;
  90. break;
  91. case CCLASS:
  92. ep = inst->r.cp->end;
  93. for(rp = inst->r.cp->spans; rp < ep; rp += 2)
  94. if(r >= rp[0] && r <= rp[1]){
  95. if(_renewthread(nl, inst->l.next, &tlp->se)==nle)
  96. return -1;
  97. break;
  98. }
  99. break;
  100. case NCCLASS:
  101. ep = inst->r.cp->end;
  102. for(rp = inst->r.cp->spans; rp < ep; rp += 2)
  103. if(r >= rp[0] && r <= rp[1])
  104. break;
  105. if(rp == ep)
  106. if(_renewthread(nl, inst->l.next, &tlp->se)==nle)
  107. return -1;
  108. break;
  109. case OR:
  110. /* evaluate right choice later */
  111. if(_renewthread(tlp, inst->r.right, &tlp->se) == tle)
  112. return -1;
  113. /* efficiency: advance and re-evaluate */
  114. continue;
  115. case END: /* Match! */
  116. match = 1;
  117. tlp->se.m[0].e.rep = s;
  118. if(mp != 0)
  119. _renewmatch(mp, ms, &tlp->se);
  120. break;
  121. }
  122. break;
  123. }
  124. }
  125. checkstart = startchar && nl->inst==0;
  126. s++;
  127. }while(r);
  128. return match;
  129. }
  130. extern int
  131. rregexec(Reprog *progp, /* program to run */
  132. wchar_t *bol, /* string to run machine on */
  133. Resub *mp, /* subexpression elements */
  134. int ms) /* number of elements at mp */
  135. {
  136. wchar_t *starts; /* where to start match */
  137. wchar_t *eol; /* where to end match */
  138. wchar_t startchar;
  139. int rv;
  140. /*
  141. * use user-specified starting/ending location if specified
  142. */
  143. starts = bol;
  144. eol = 0;
  145. if(mp && ms>0){
  146. if(mp->s.rsp)
  147. starts = mp->s.rsp;
  148. if(mp->e.rep)
  149. eol = mp->e.rep;
  150. }
  151. startchar = progp->startinst->type == RUNE ? progp->startinst->r.r : 0;
  152. /* keep trying till we have enough list space to terminate */
  153. for(;;){
  154. if(_relist[0] == 0){
  155. _relist[0] = malloc(2*_relistsize*sizeof(Relist));
  156. _relist[1] = _relist[0] + _relistsize;
  157. _reliste[0] = _relist[0] + _relistsize - 1;
  158. _reliste[1] = _relist[1] + _relistsize - 1;
  159. if(_relist[0] == 0)
  160. regerror("_relist overflow");
  161. }
  162. rv = rregexec1(progp, bol, mp, ms, starts, eol, startchar);
  163. if(rv >= 0)
  164. return rv;
  165. free(_relist[0]);
  166. _relist[0] = 0;
  167. _relistsize += LISTINCREMENT;
  168. }
  169. }