regx.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. #include <thread.h>
  13. #include <cursor.h>
  14. #include <mouse.h>
  15. #include <keyboard.h>
  16. #include <frame.h>
  17. #include <fcall.h>
  18. #include <plumb.h>
  19. #include "dat.h"
  20. #include "fns.h"
  21. Rangeset sel;
  22. Rune *lastregexp;
  23. /*
  24. * Machine Information
  25. */
  26. typedef struct Inst Inst;
  27. struct Inst
  28. {
  29. uint type; /* <= Runemax+1 ==> literal, otherwise action */
  30. union {
  31. int sid;
  32. int subid;
  33. int class;
  34. Inst *other;
  35. Inst *right;
  36. };
  37. union{
  38. Inst *left;
  39. Inst *next;
  40. };
  41. };
  42. #define NPROG 1024
  43. Inst program[NPROG];
  44. Inst *progp;
  45. Inst *startinst; /* First inst. of program; might not be program[0] */
  46. Inst *bstartinst; /* same for backwards machine */
  47. Channel *rechan; /* chan(Inst*) */
  48. typedef struct Ilist Ilist;
  49. struct Ilist
  50. {
  51. Inst *inst; /* Instruction of the thread */
  52. Rangeset se;
  53. uint startp; /* first char of match */
  54. };
  55. #define NLIST 127
  56. Ilist *tl, *nl; /* This list, next list */
  57. Ilist list[2][NLIST+1]; /* +1 for trailing null */
  58. static Rangeset sempty;
  59. /*
  60. * Actions and Tokens
  61. *
  62. * 0x100xx are operators, value == precedence
  63. * 0x200xx are tokens, i.e. operands for operators
  64. */
  65. enum {
  66. OPERATOR = Runemask+1, /* Bitmask of all operators */
  67. START = OPERATOR, /* Start, used for marker on stack */
  68. RBRA, /* Right bracket, ) */
  69. LBRA, /* Left bracket, ( */
  70. OR, /* Alternation, | */
  71. CAT, /* Concatentation, implicit operator */
  72. STAR, /* Closure, * */
  73. PLUS, /* a+ == aa* */
  74. QUEST, /* a? == a|nothing, i.e. 0 or 1 a's */
  75. ANY = OPERATOR<<1, /* Any character but newline, . */
  76. NOP, /* No operation, internal use only */
  77. BOL, /* Beginning of line, ^ */
  78. EOL, /* End of line, $ */
  79. CCLASS, /* Character class, [] */
  80. NCCLASS, /* Negated character class, [^] */
  81. END, /* Terminate: match found */
  82. ISATOR = OPERATOR,
  83. ISAND = OPERATOR<<1,
  84. };
  85. /*
  86. * Parser Information
  87. */
  88. typedef struct Node Node;
  89. struct Node
  90. {
  91. Inst *first;
  92. Inst *last;
  93. };
  94. #define NSTACK 20
  95. Node andstack[NSTACK];
  96. Node *andp;
  97. int atorstack[NSTACK];
  98. int *atorp;
  99. int lastwasand; /* Last token was operand */
  100. int cursubid;
  101. int subidstack[NSTACK];
  102. int *subidp;
  103. int backwards;
  104. int nbra;
  105. Rune *exprp; /* pointer to next character in source expression */
  106. #define DCLASS 10 /* allocation increment */
  107. int nclass; /* number active */
  108. int Nclass; /* high water mark */
  109. Rune **class;
  110. int negateclass;
  111. int addinst(Ilist *l, Inst *inst, Rangeset *sep);
  112. void newmatch(Rangeset*);
  113. void bnewmatch(Rangeset*);
  114. void pushand(Inst*, Inst*);
  115. void pushator(int);
  116. Node *popand(int);
  117. int popator(void);
  118. void startlex(Rune*);
  119. int lex(void);
  120. void operator(int);
  121. void operand(int);
  122. void evaluntil(int);
  123. void optimize(Inst*);
  124. void bldcclass(void);
  125. void
  126. rxinit(void)
  127. {
  128. rechan = chancreate(sizeof(Inst*), 0);
  129. lastregexp = runemalloc(1);
  130. }
  131. void
  132. regerror(char *e)
  133. {
  134. lastregexp[0] = 0;
  135. warning(nil, "regexp: %s\n", e);
  136. sendp(rechan, nil);
  137. threadexits(nil);
  138. }
  139. Inst *
  140. newinst(int t)
  141. {
  142. if(progp >= &program[NPROG])
  143. regerror("expression too long");
  144. progp->type = t;
  145. progp->left = nil;
  146. progp->right = nil;
  147. return progp++;
  148. }
  149. void
  150. realcompile(void *arg)
  151. {
  152. int token;
  153. Rune *s;
  154. threadsetname("regcomp");
  155. s = arg;
  156. startlex(s);
  157. atorp = atorstack;
  158. andp = andstack;
  159. subidp = subidstack;
  160. cursubid = 0;
  161. lastwasand = FALSE;
  162. /* Start with a low priority operator to prime parser */
  163. pushator(START-1);
  164. while((token=lex()) != END){
  165. if((token&ISATOR) == OPERATOR)
  166. operator(token);
  167. else
  168. operand(token);
  169. }
  170. /* Close with a low priority operator */
  171. evaluntil(START);
  172. /* Force END */
  173. operand(END);
  174. evaluntil(START);
  175. if(nbra)
  176. regerror("unmatched `('");
  177. --andp; /* points to first and only operand */
  178. sendp(rechan, andp->first);
  179. threadexits(nil);
  180. }
  181. /* r is null terminated */
  182. int
  183. rxcompile(Rune *r)
  184. {
  185. int i, nr;
  186. Inst *oprogp;
  187. nr = runestrlen(r)+1;
  188. if(runeeq(lastregexp, runestrlen(lastregexp)+1, r, nr)==TRUE)
  189. return TRUE;
  190. lastregexp[0] = 0;
  191. for(i=0; i<nclass; i++)
  192. free(class[i]);
  193. nclass = 0;
  194. progp = program;
  195. backwards = FALSE;
  196. bstartinst = nil;
  197. threadcreate(realcompile, r, STACK);
  198. startinst = recvp(rechan);
  199. if(startinst == nil)
  200. return FALSE;
  201. optimize(program);
  202. oprogp = progp;
  203. backwards = TRUE;
  204. threadcreate(realcompile, r, STACK);
  205. bstartinst = recvp(rechan);
  206. if(bstartinst == nil)
  207. return FALSE;
  208. optimize(oprogp);
  209. lastregexp = runerealloc(lastregexp, nr);
  210. runemove(lastregexp, r, nr);
  211. return TRUE;
  212. }
  213. void
  214. operand(int t)
  215. {
  216. Inst *i;
  217. if(lastwasand)
  218. operator(CAT); /* catenate is implicit */
  219. i = newinst(t);
  220. if(t == CCLASS){
  221. if(negateclass)
  222. i->type = NCCLASS; /* UGH */
  223. i->class = nclass-1; /* UGH */
  224. }
  225. pushand(i, i);
  226. lastwasand = TRUE;
  227. }
  228. void
  229. operator(int t)
  230. {
  231. if(t==RBRA && --nbra<0)
  232. regerror("unmatched `)'");
  233. if(t==LBRA){
  234. cursubid++; /* silently ignored */
  235. nbra++;
  236. if(lastwasand)
  237. operator(CAT);
  238. }else
  239. evaluntil(t);
  240. if(t!=RBRA)
  241. pushator(t);
  242. lastwasand = FALSE;
  243. if(t==STAR || t==QUEST || t==PLUS || t==RBRA)
  244. lastwasand = TRUE; /* these look like operands */
  245. }
  246. void
  247. pushand(Inst *f, Inst *l)
  248. {
  249. if(andp >= &andstack[NSTACK])
  250. error("operand stack overflow");
  251. andp->first = f;
  252. andp->last = l;
  253. andp++;
  254. }
  255. void
  256. pushator(int t)
  257. {
  258. if(atorp >= &atorstack[NSTACK])
  259. error("operator stack overflow");
  260. *atorp++=t;
  261. if(cursubid >= NRange)
  262. *subidp++= -1;
  263. else
  264. *subidp++=cursubid;
  265. }
  266. Node *
  267. popand(int op)
  268. {
  269. char buf[64];
  270. if(andp <= &andstack[0])
  271. if(op){
  272. sprint(buf, "missing operand for %c", op);
  273. regerror(buf);
  274. }else
  275. regerror("malformed regexp");
  276. return --andp;
  277. }
  278. int
  279. popator()
  280. {
  281. if(atorp <= &atorstack[0])
  282. error("operator stack underflow");
  283. --subidp;
  284. return *--atorp;
  285. }
  286. void
  287. evaluntil(int pri)
  288. {
  289. Node *op1, *op2, *t;
  290. Inst *inst1, *inst2;
  291. while(pri==RBRA || atorp[-1]>=pri){
  292. switch(popator()){
  293. case LBRA:
  294. op1 = popand('(');
  295. inst2 = newinst(RBRA);
  296. inst2->subid = *subidp;
  297. op1->last->next = inst2;
  298. inst1 = newinst(LBRA);
  299. inst1->subid = *subidp;
  300. inst1->next = op1->first;
  301. pushand(inst1, inst2);
  302. return; /* must have been RBRA */
  303. default:
  304. error("unknown regexp operator");
  305. break;
  306. case OR:
  307. op2 = popand('|');
  308. op1 = popand('|');
  309. inst2 = newinst(NOP);
  310. op2->last->next = inst2;
  311. op1->last->next = inst2;
  312. inst1 = newinst(OR);
  313. inst1->right = op1->first;
  314. inst1->left = op2->first;
  315. pushand(inst1, inst2);
  316. break;
  317. case CAT:
  318. op2 = popand(0);
  319. op1 = popand(0);
  320. if(backwards && op2->first->type!=END){
  321. t = op1;
  322. op1 = op2;
  323. op2 = t;
  324. }
  325. op1->last->next = op2->first;
  326. pushand(op1->first, op2->last);
  327. break;
  328. case STAR:
  329. op2 = popand('*');
  330. inst1 = newinst(OR);
  331. op2->last->next = inst1;
  332. inst1->right = op2->first;
  333. pushand(inst1, inst1);
  334. break;
  335. case PLUS:
  336. op2 = popand('+');
  337. inst1 = newinst(OR);
  338. op2->last->next = inst1;
  339. inst1->right = op2->first;
  340. pushand(op2->first, inst1);
  341. break;
  342. case QUEST:
  343. op2 = popand('?');
  344. inst1 = newinst(OR);
  345. inst2 = newinst(NOP);
  346. inst1->left = inst2;
  347. inst1->right = op2->first;
  348. op2->last->next = inst2;
  349. pushand(inst1, inst2);
  350. break;
  351. }
  352. }
  353. }
  354. void
  355. optimize(Inst *start)
  356. {
  357. Inst *inst, *target;
  358. for(inst=start; inst->type!=END; inst++){
  359. target = inst->next;
  360. while(target->type == NOP)
  361. target = target->next;
  362. inst->next = target;
  363. }
  364. }
  365. void
  366. startlex(Rune *s)
  367. {
  368. exprp = s;
  369. nbra = 0;
  370. }
  371. int
  372. lex(void){
  373. int c;
  374. c = *exprp++;
  375. switch(c){
  376. case '\\':
  377. if(*exprp)
  378. if((c= *exprp++)=='n')
  379. c='\n';
  380. break;
  381. case 0:
  382. c = END;
  383. --exprp; /* In case we come here again */
  384. break;
  385. case '*':
  386. c = STAR;
  387. break;
  388. case '?':
  389. c = QUEST;
  390. break;
  391. case '+':
  392. c = PLUS;
  393. break;
  394. case '|':
  395. c = OR;
  396. break;
  397. case '.':
  398. c = ANY;
  399. break;
  400. case '(':
  401. c = LBRA;
  402. break;
  403. case ')':
  404. c = RBRA;
  405. break;
  406. case '^':
  407. c = BOL;
  408. break;
  409. case '$':
  410. c = EOL;
  411. break;
  412. case '[':
  413. c = CCLASS;
  414. bldcclass();
  415. break;
  416. }
  417. return c;
  418. }
  419. int
  420. nextrec(void)
  421. {
  422. if(exprp[0]==0 || (exprp[0]=='\\' && exprp[1]==0))
  423. regerror("malformed `[]'");
  424. if(exprp[0] == '\\'){
  425. exprp++;
  426. if(*exprp=='n'){
  427. exprp++;
  428. return '\n';
  429. }
  430. return *exprp++|(Runemax+1);
  431. }
  432. return *exprp++;
  433. }
  434. void
  435. bldcclass(void)
  436. {
  437. int c1, c2, n, na;
  438. Rune *classp;
  439. classp = runemalloc(DCLASS);
  440. n = 0;
  441. na = DCLASS;
  442. /* we have already seen the '[' */
  443. if(*exprp == '^'){
  444. classp[n++] = '\n'; /* don't match newline in negate case */
  445. negateclass = TRUE;
  446. exprp++;
  447. }else
  448. negateclass = FALSE;
  449. while((c1 = nextrec()) != ']'){
  450. if(c1 == '-'){
  451. Error:
  452. free(classp);
  453. regerror("malformed `[]'");
  454. }
  455. if(n+4 >= na){ /* 3 runes plus NUL */
  456. na += DCLASS;
  457. classp = runerealloc(classp, na);
  458. }
  459. if(*exprp == '-'){
  460. exprp++; /* eat '-' */
  461. if((c2 = nextrec()) == ']')
  462. goto Error;
  463. classp[n+0] = Runemax;
  464. classp[n+1] = c1;
  465. classp[n+2] = c2;
  466. n += 3;
  467. }else
  468. classp[n++] = c1;
  469. }
  470. classp[n] = 0;
  471. if(nclass == Nclass){
  472. Nclass += DCLASS;
  473. class = realloc(class, Nclass*sizeof(Rune*));
  474. }
  475. class[nclass++] = classp;
  476. }
  477. int
  478. classmatch(int classno, int c, int negate)
  479. {
  480. Rune *p;
  481. p = class[classno];
  482. while(*p){
  483. if(*p == Runemax){
  484. if(p[1]<=c && c<=p[2])
  485. return !negate;
  486. p += 3;
  487. }else if(*p++ == c)
  488. return !negate;
  489. }
  490. return negate;
  491. }
  492. /*
  493. * Note optimization in addinst:
  494. * *l must be pending when addinst called; if *l has been looked
  495. * at already, the optimization is a bug.
  496. */
  497. int
  498. addinst(Ilist *l, Inst *inst, Rangeset *sep)
  499. {
  500. Ilist *p;
  501. for(p = l; p->inst; p++){
  502. if(p->inst==inst){
  503. if((sep)->r[0].q0 < p->se.r[0].q0)
  504. p->se= *sep; /* this would be bug */
  505. return 0; /* It's already there */
  506. }
  507. }
  508. p->inst = inst;
  509. p->se= *sep;
  510. (p+1)->inst = nil;
  511. return 1;
  512. }
  513. int
  514. rxnull(void)
  515. {
  516. return startinst==nil || bstartinst==nil;
  517. }
  518. /* either t!=nil or r!=nil, and we match the string in the appropriate place */
  519. int
  520. rxexecute(Text *t, Rune *r, uint startp, uint eof, Rangeset *rp)
  521. {
  522. int flag;
  523. Inst *inst;
  524. Ilist *tlp;
  525. uint p;
  526. int nnl, ntl;
  527. int nc, c;
  528. int wrapped;
  529. int startchar;
  530. flag = 0;
  531. p = startp;
  532. startchar = 0;
  533. wrapped = 0;
  534. nnl = 0;
  535. if(startinst->type<OPERATOR)
  536. startchar = startinst->type;
  537. list[0][0].inst = list[1][0].inst = nil;
  538. sel.r[0].q0 = -1;
  539. if(t != nil)
  540. nc = t->file->nc;
  541. else
  542. nc = runestrlen(r);
  543. /* Execute machine once for each character */
  544. for(;;p++){
  545. doloop:
  546. if(p>=eof || p>=nc){
  547. switch(wrapped++){
  548. case 0: /* let loop run one more click */
  549. case 2:
  550. break;
  551. case 1: /* expired; wrap to beginning */
  552. if(sel.r[0].q0>=0 || eof!=Infinity)
  553. goto Return;
  554. list[0][0].inst = list[1][0].inst = nil;
  555. p = 0;
  556. goto doloop;
  557. default:
  558. goto Return;
  559. }
  560. c = 0;
  561. }else{
  562. if(((wrapped && p>=startp) || sel.r[0].q0>0) && nnl==0)
  563. break;
  564. if(t != nil)
  565. c = textreadc(t, p);
  566. else
  567. c = r[p];
  568. }
  569. /* fast check for first char */
  570. if(startchar && nnl==0 && c!=startchar)
  571. continue;
  572. tl = list[flag];
  573. nl = list[flag^=1];
  574. nl->inst = nil;
  575. ntl = nnl;
  576. nnl = 0;
  577. if(sel.r[0].q0<0 && (!wrapped || p<startp || startp==eof)){
  578. /* Add first instruction to this list */
  579. sempty.r[0].q0 = p;
  580. if(addinst(tl, startinst, &sempty))
  581. if(++ntl >= NLIST){
  582. Overflow:
  583. warning(nil, "regexp list overflow\n");
  584. sel.r[0].q0 = -1;
  585. goto Return;
  586. }
  587. }
  588. /* Execute machine until this list is empty */
  589. for(tlp = tl; inst = tlp->inst; tlp++){ /* assignment = */
  590. Switchstmt:
  591. switch(inst->type){
  592. default: /* regular character */
  593. if(inst->type==c){
  594. Addinst:
  595. if(addinst(nl, inst->next, &tlp->se))
  596. if(++nnl >= NLIST)
  597. goto Overflow;
  598. }
  599. break;
  600. case LBRA:
  601. if(inst->subid>=0)
  602. tlp->se.r[inst->subid].q0 = p;
  603. inst = inst->next;
  604. goto Switchstmt;
  605. case RBRA:
  606. if(inst->subid>=0)
  607. tlp->se.r[inst->subid].q1 = p;
  608. inst = inst->next;
  609. goto Switchstmt;
  610. case ANY:
  611. if(c!='\n')
  612. goto Addinst;
  613. break;
  614. case BOL:
  615. if(p==0 || (t!=nil && textreadc(t, p-1)=='\n') || (r!=nil && r[p-1]=='\n')){
  616. Step:
  617. inst = inst->next;
  618. goto Switchstmt;
  619. }
  620. break;
  621. case EOL:
  622. if(c == '\n')
  623. goto Step;
  624. break;
  625. case CCLASS:
  626. if(c>=0 && classmatch(inst->class, c, 0))
  627. goto Addinst;
  628. break;
  629. case NCCLASS:
  630. if(c>=0 && classmatch(inst->class, c, 1))
  631. goto Addinst;
  632. break;
  633. case OR:
  634. /* evaluate right choice later */
  635. if(addinst(tlp, inst->right, &tlp->se))
  636. if(++ntl >= NLIST)
  637. goto Overflow;
  638. /* efficiency: advance and re-evaluate */
  639. inst = inst->left;
  640. goto Switchstmt;
  641. case END: /* Match! */
  642. tlp->se.r[0].q1 = p;
  643. newmatch(&tlp->se);
  644. break;
  645. }
  646. }
  647. }
  648. Return:
  649. *rp = sel;
  650. return sel.r[0].q0 >= 0;
  651. }
  652. void
  653. newmatch(Rangeset *sp)
  654. {
  655. if(sel.r[0].q0<0 || sp->r[0].q0<sel.r[0].q0 ||
  656. (sp->r[0].q0==sel.r[0].q0 && sp->r[0].q1>sel.r[0].q1))
  657. sel = *sp;
  658. }
  659. int
  660. rxbexecute(Text *t, uint startp, Rangeset *rp)
  661. {
  662. int flag;
  663. Inst *inst;
  664. Ilist *tlp;
  665. int p;
  666. int nnl, ntl;
  667. int c;
  668. int wrapped;
  669. int startchar;
  670. flag = 0;
  671. nnl = 0;
  672. wrapped = 0;
  673. p = startp;
  674. startchar = 0;
  675. if(bstartinst->type<OPERATOR)
  676. startchar = bstartinst->type;
  677. list[0][0].inst = list[1][0].inst = nil;
  678. sel.r[0].q0= -1;
  679. /* Execute machine once for each character, including terminal NUL */
  680. for(;;--p){
  681. doloop:
  682. if(p <= 0){
  683. switch(wrapped++){
  684. case 0: /* let loop run one more click */
  685. case 2:
  686. break;
  687. case 1: /* expired; wrap to end */
  688. if(sel.r[0].q0>=0)
  689. goto Return;
  690. list[0][0].inst = list[1][0].inst = nil;
  691. p = t->file->nc;
  692. goto doloop;
  693. case 3:
  694. default:
  695. goto Return;
  696. }
  697. c = 0;
  698. }else{
  699. if(((wrapped && p<=startp) || sel.r[0].q0>0) && nnl==0)
  700. break;
  701. c = textreadc(t, p-1);
  702. }
  703. /* fast check for first char */
  704. if(startchar && nnl==0 && c!=startchar)
  705. continue;
  706. tl = list[flag];
  707. nl = list[flag^=1];
  708. nl->inst = nil;
  709. ntl = nnl;
  710. nnl = 0;
  711. if(sel.r[0].q0<0 && (!wrapped || p>startp)){
  712. /* Add first instruction to this list */
  713. /* the minus is so the optimizations in addinst work */
  714. sempty.r[0].q0 = -p;
  715. if(addinst(tl, bstartinst, &sempty))
  716. if(++ntl >= NLIST){
  717. Overflow:
  718. warning(nil, "regexp list overflow\n");
  719. sel.r[0].q0 = -1;
  720. goto Return;
  721. }
  722. }
  723. /* Execute machine until this list is empty */
  724. for(tlp = tl; inst = tlp->inst; tlp++){ /* assignment = */
  725. Switchstmt:
  726. switch(inst->type){
  727. default: /* regular character */
  728. if(inst->type == c){
  729. Addinst:
  730. if(addinst(nl, inst->next, &tlp->se))
  731. if(++nnl >= NLIST)
  732. goto Overflow;
  733. }
  734. break;
  735. case LBRA:
  736. if(inst->subid>=0)
  737. tlp->se.r[inst->subid].q0 = p;
  738. inst = inst->next;
  739. goto Switchstmt;
  740. case RBRA:
  741. if(inst->subid >= 0)
  742. tlp->se.r[inst->subid].q1 = p;
  743. inst = inst->next;
  744. goto Switchstmt;
  745. case ANY:
  746. if(c != '\n')
  747. goto Addinst;
  748. break;
  749. case BOL:
  750. if(c=='\n' || p==0){
  751. Step:
  752. inst = inst->next;
  753. goto Switchstmt;
  754. }
  755. break;
  756. case EOL:
  757. if(p<t->file->nc && textreadc(t, p)=='\n')
  758. goto Step;
  759. break;
  760. case CCLASS:
  761. if(c>0 && classmatch(inst->class, c, 0))
  762. goto Addinst;
  763. break;
  764. case NCCLASS:
  765. if(c>0 && classmatch(inst->class, c, 1))
  766. goto Addinst;
  767. break;
  768. case OR:
  769. /* evaluate right choice later */
  770. if(addinst(tl, inst->right, &tlp->se))
  771. if(++ntl >= NLIST)
  772. goto Overflow;
  773. /* efficiency: advance and re-evaluate */
  774. inst = inst->left;
  775. goto Switchstmt;
  776. case END: /* Match! */
  777. tlp->se.r[0].q0 = -tlp->se.r[0].q0; /* minus sign */
  778. tlp->se.r[0].q1 = p;
  779. bnewmatch(&tlp->se);
  780. break;
  781. }
  782. }
  783. }
  784. Return:
  785. *rp = sel;
  786. return sel.r[0].q0 >= 0;
  787. }
  788. void
  789. bnewmatch(Rangeset *sp)
  790. {
  791. int i;
  792. if(sel.r[0].q0<0 || sp->r[0].q0>sel.r[0].q1 || (sp->r[0].q0==sel.r[0].q1 && sp->r[0].q1<sel.r[0].q0))
  793. for(i = 0; i<NRange; i++){ /* note the reversal; q0<=q1 */
  794. sel.r[i].q0 = sp->r[i].q1;
  795. sel.r[i].q1 = sp->r[i].q0;
  796. }
  797. }