regx.c 16 KB

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