regex.c 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821
  1. #include <sys/types.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <limits.h>
  6. #include <ctype.h>
  7. #include <assert.h>
  8. #include "regex.h"
  9. /* utility definitions */
  10. #ifdef _POSIX2_RE_DUP_MAX
  11. #define DUPMAX _POSIX2_RE_DUP_MAX
  12. #else
  13. #define DUPMAX 255
  14. #endif
  15. #define INFINITY (DUPMAX + 1)
  16. #define NC (CHAR_MAX - CHAR_MIN + 1)
  17. typedef unsigned char uch;
  18. /* for old systems with bcopy() but no memmove() */
  19. #ifdef USEBCOPY
  20. #define memmove(d, s, c) bcopy(s, d, c)
  21. #endif
  22. #define MAGIC1 ((('r'^0200)<<8) | 'e')
  23. /*
  24. * The internal representation is a *strip*, a sequence of
  25. * operators ending with an endmarker. (Some terminology etc. is a
  26. * historical relic of earlier versions which used multiple strips.)
  27. * Certain oddities in the representation are there to permit running
  28. * the machinery backwards; in particular, any deviation from sequential
  29. * flow must be marked at both its source and its destination. Some
  30. * fine points:
  31. *
  32. * - OPLUS_ and O_PLUS are *inside* the loop they create.
  33. * - OQUEST_ and O_QUEST are *outside* the bypass they create.
  34. * - OCH_ and O_CH are *outside* the multi-way branch they create, while
  35. * OOR1 and OOR2 are respectively the end and the beginning of one of
  36. * the branches. Note that there is an implicit OOR2 following OCH_
  37. * and an implicit OOR1 preceding O_CH.
  38. *
  39. * In state representations, an operator's bit is on to signify a state
  40. * immediately *preceding* "execution" of that operator.
  41. */
  42. typedef long sop; /* strip operator */
  43. typedef long sopno;
  44. #define OPRMASK 0x7c000000
  45. #define OPDMASK 0x03ffffff
  46. #define OPSHIFT (26)
  47. #define OP(n) ((n)&OPRMASK)
  48. #define OPND(n) ((n)&OPDMASK)
  49. #define SOP(op, opnd) ((op)|(opnd))
  50. /* operators meaning operand */
  51. /* (back, fwd are offsets) */
  52. #define OEND (1<<OPSHIFT) /* endmarker - */
  53. #define OCHAR (2<<OPSHIFT) /* character unsigned char */
  54. #define OBOL (3<<OPSHIFT) /* left anchor - */
  55. #define OEOL (4<<OPSHIFT) /* right anchor - */
  56. #define OANY (5<<OPSHIFT) /* . - */
  57. #define OANYOF (6<<OPSHIFT) /* [...] set number */
  58. #define OBACK_ (7<<OPSHIFT) /* begin \d paren number */
  59. #define O_BACK (8<<OPSHIFT) /* end \d paren number */
  60. #define OPLUS_ (9<<OPSHIFT) /* + prefix fwd to suffix */
  61. #define O_PLUS (10<<OPSHIFT) /* + suffix back to prefix */
  62. #define OQUEST_ (11<<OPSHIFT) /* ? prefix fwd to suffix */
  63. #define O_QUEST (12<<OPSHIFT) /* ? suffix back to prefix */
  64. #define OLPAREN (13<<OPSHIFT) /* ( fwd to ) */
  65. #define ORPAREN (14<<OPSHIFT) /* ) back to ( */
  66. #define OCH_ (15<<OPSHIFT) /* begin choice fwd to OOR2 */
  67. #define OOR1 (16<<OPSHIFT) /* | pt. 1 back to OOR1 or OCH_ */
  68. #define OOR2 (17<<OPSHIFT) /* | pt. 2 fwd to OOR2 or O_CH */
  69. #define O_CH (18<<OPSHIFT) /* end choice back to OOR1 */
  70. #define OBOW (19<<OPSHIFT) /* begin word - */
  71. #define OEOW (20<<OPSHIFT) /* end word - */
  72. /*
  73. * Structure for [] character-set representation. Character sets are
  74. * done as bit vectors, grouped 8 to a byte vector for compactness.
  75. * The individual set therefore has both a pointer to the byte vector
  76. * and a mask to pick out the relevant bit of each byte. A hash code
  77. * simplifies testing whether two sets could be identical.
  78. *
  79. * This will get trickier for multicharacter collating elements. As
  80. * preliminary hooks for dealing with such things, we also carry along
  81. * a string of multi-character elements, and decide the size of the
  82. * vectors at run time.
  83. */
  84. typedef struct {
  85. uch *ptr; /* -> uch [csetsize] */
  86. uch mask; /* bit within array */
  87. uch hash; /* hash code */
  88. size_t smultis;
  89. char *multis; /* -> char[smulti] ab\0cd\0ef\0\0 */
  90. } cset;
  91. /* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */
  92. #define CHadd(cs, c) ((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (c))
  93. #define CHsub(cs, c) ((cs)->ptr[(uch)(c)] &= ~(cs)->mask, (cs)->hash -= (c))
  94. #define CHIN(cs, c) ((cs)->ptr[(uch)(c)] & (cs)->mask)
  95. #define MCadd(p, cs, cp) mcadd(p, cs, cp) /* regcomp() internal fns */
  96. /* stuff for character categories */
  97. typedef unsigned char cat_t;
  98. /*
  99. * main compiled-expression structure
  100. */
  101. struct re_guts {
  102. int magic;
  103. # define MAGIC2 ((('R'^0200)<<8)|'E')
  104. sop *strip; /* malloced area for strip */
  105. int csetsize; /* number of bits in a cset vector */
  106. int ncsets; /* number of csets in use */
  107. cset *sets; /* -> cset [ncsets] */
  108. uch *setbits; /* -> uch[csetsize][ncsets/CHAR_BIT] */
  109. int cflags; /* copy of regcomp() cflags argument */
  110. sopno nstates; /* = number of sops */
  111. sopno firststate; /* the initial OEND (normally 0) */
  112. sopno laststate; /* the final OEND */
  113. int iflags; /* internal flags */
  114. # define USEBOL 01 /* used ^ */
  115. # define USEEOL 02 /* used $ */
  116. # define BAD 04 /* something wrong */
  117. int nbol; /* number of ^ used */
  118. int neol; /* number of $ used */
  119. int ncategories; /* how many character categories */
  120. cat_t *categories; /* ->catspace[-CHAR_MIN] */
  121. char *must; /* match must contain this string */
  122. int mlen; /* length of must */
  123. size_t nsub; /* copy of re_nsub */
  124. int backrefs; /* does it use back references? */
  125. sopno nplus; /* how deep does it nest +s? */
  126. /* catspace must be last */
  127. cat_t catspace[1]; /* actually [NC] */
  128. };
  129. /* misc utilities */
  130. #define OUT (CHAR_MAX+1) /* a non-character value */
  131. #define ISWORD(c) (isalnum(c) || (c) == '_')
  132. /* character-class table */
  133. static struct cclass {
  134. char *name;
  135. char *chars;
  136. char *multis;
  137. } cclasses[] = {
  138. {"alnum","ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",""},
  139. {"alpha", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", ""},
  140. {"blank"," \t", ""},
  141. {"cntrl","\007\b\t\n\v\f\r\1\2\3\4\5\6\16\17\20\21\22\23\24\25\26\27\30\31\32\33\34\35\36\37\177",""},
  142. {"digit","0123456789",""},
  143. {"graph","ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",""},
  144. {"lower","abcdefghijklmnopqrstuvwxyz",""},
  145. {"print","ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ ",""},
  146. {"punct","!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",""},
  147. {"space","\t\n\v\f\r ", ""},
  148. {"upper","ABCDEFGHIJKLMNOPQRSTUVWXYZ",""},
  149. {"xdigit","0123456789ABCDEFabcdef",""},
  150. {NULL,NULL,""}
  151. };
  152. /* character-name table */
  153. static struct cname {
  154. char *name;
  155. char code;
  156. } cnames[] = {
  157. {"NUL", '\0'},
  158. {"SOH", '\001'},
  159. {"STX", '\002'},
  160. {"ETX", '\003'},
  161. {"EOT", '\004'},
  162. {"ENQ", '\005'},
  163. {"ACK", '\006'},
  164. {"BEL", '\007'},
  165. {"alert", '\007'},
  166. {"BS", '\010'},
  167. {"backspace", '\b'},
  168. {"HT", '\011'},
  169. {"tab", '\t'},
  170. {"LF", '\012'},
  171. {"newline", '\n'},
  172. {"VT", '\013'},
  173. {"vertical-tab", '\v'},
  174. {"FF", '\014'},
  175. {"form-feed", '\f'},
  176. {"CR", '\015'},
  177. {"carriage-return", '\r'},
  178. {"SO", '\016'},
  179. {"SI", '\017'},
  180. {"DLE", '\020'},
  181. {"DC1", '\021'},
  182. {"DC2", '\022'},
  183. {"DC3", '\023'},
  184. {"DC4", '\024'},
  185. {"NAK", '\025'},
  186. {"SYN", '\026'},
  187. {"ETB", '\027'},
  188. {"CAN", '\030'},
  189. {"EM", '\031'},
  190. {"SUB", '\032'},
  191. {"ESC", '\033'},
  192. {"IS4", '\034'},
  193. {"FS", '\034'},
  194. {"IS3", '\035'},
  195. {"GS", '\035'},
  196. {"IS2", '\036'},
  197. {"RS", '\036'},
  198. {"IS1", '\037'},
  199. {"US", '\037'},
  200. {"space", ' '},
  201. {"exclamation-mark", '!'},
  202. {"quotation-mark", '"'},
  203. {"number-sign", '#'},
  204. {"dollar-sign", '$'},
  205. {"percent-sign", '%'},
  206. {"ampersand", '&'},
  207. {"apostrophe", '\''},
  208. {"left-parenthesis", '('},
  209. {"right-parenthesis", ')'},
  210. {"asterisk", '*'},
  211. {"plus-sign", '+'},
  212. {"comma", ','},
  213. {"hyphen", '-'},
  214. {"hyphen-minus", '-'},
  215. {"period", '.'},
  216. {"full-stop", '.'},
  217. {"slash", '/'},
  218. {"solidus", '/'},
  219. {"zero", '0'},
  220. {"one", '1'},
  221. {"two", '2'},
  222. {"three", '3'},
  223. {"four", '4'},
  224. {"five", '5'},
  225. {"six", '6'},
  226. {"seven", '7'},
  227. {"eight", '8'},
  228. {"nine", '9'},
  229. {"colon", ':'},
  230. {"semicolon", ';'},
  231. {"less-than-sign", '<'},
  232. {"equals-sign", '='},
  233. {"greater-than-sign", '>'},
  234. {"question-mark", '?'},
  235. {"commercial-at", '@'},
  236. {"left-square-bracket", '['},
  237. {"backslash", '\\'},
  238. {"reverse-solidus", '\\'},
  239. {"right-square-bracket", ']'},
  240. {"circumflex", '^'},
  241. {"circumflex-accent", '^'},
  242. {"underscore", '_'},
  243. {"low-line", '_'},
  244. {"grave-accent", '`'},
  245. {"left-brace", '{'},
  246. {"left-curly-bracket", '{'},
  247. {"vertical-line", '|'},
  248. {"right-brace", '}'},
  249. {"right-curly-bracket", '}'},
  250. {"tilde", '~'},
  251. {"DEL", '\177'},
  252. {NULL, 0}
  253. };
  254. /*
  255. * parse structure, passed up and down to avoid global variables and
  256. * other clumsinesses
  257. */
  258. struct parse {
  259. char *next; /* next character in RE */
  260. char *end; /* end of string (-> NUL normally) */
  261. int error; /* has an error been seen? */
  262. sop *strip; /* malloced strip */
  263. sopno ssize; /* malloced strip size (allocated) */
  264. sopno slen; /* malloced strip length (used) */
  265. int ncsalloc; /* number of csets allocated */
  266. struct re_guts *g;
  267. # define NPAREN 10 /* we need to remember () 1-9 for back refs */
  268. sopno pbegin[NPAREN]; /* -> ( ([0] unused) */
  269. sopno pend[NPAREN]; /* -> ) ([0] unused) */
  270. };
  271. #ifdef __cplusplus
  272. extern "C" {
  273. #endif
  274. static void p_ere(register struct parse *p, int stop);
  275. static void p_ere_exp(register struct parse *p);
  276. static void p_str(register struct parse *p);
  277. static void p_bre(register struct parse *p, register int end1, register int end2);
  278. static int p_simp_re(register struct parse *p, int starordinary);
  279. static int p_count(register struct parse *p);
  280. static void p_bracket(register struct parse *p);
  281. static void p_b_term(register struct parse *p, register cset *cs);
  282. static void p_b_cclass(register struct parse *p, register cset *cs);
  283. static void p_b_eclass(register struct parse *p, register cset *cs);
  284. static char p_b_symbol(register struct parse *p);
  285. static char p_b_coll_elem(register struct parse *p, int endc);
  286. static char othercase(int ch);
  287. static void bothcases(register struct parse *p, int ch);
  288. static void ordinary(register struct parse *p, register int ch);
  289. static void nonnewline(register struct parse *p);
  290. static void repeat(register struct parse *p, sopno start, int from, int to);
  291. static int seterr(register struct parse *p, int e);
  292. static cset *allocset(register struct parse *p);
  293. static void freeset(register struct parse *p, register cset *cs);
  294. static int freezeset(register struct parse *p, register cset *cs);
  295. static int firstch(register struct parse *p, register cset *cs);
  296. static int nch(register struct parse *p, register cset *cs);
  297. static void mcadd(register struct parse *p, register cset *cs, register char *cp);
  298. static void mcinvert(register struct parse *p, register cset *cs);
  299. static void mccase(register struct parse *p, register cset *cs);
  300. static int isinsets(register struct re_guts *g, int c);
  301. static int samesets(register struct re_guts *g, int c1, int c2);
  302. static void categorize(struct parse *p, register struct re_guts *g);
  303. static sopno dupl(register struct parse *p, sopno start, sopno finish);
  304. static void doemit(register struct parse *p, sop op, size_t opnd);
  305. static void doinsert(register struct parse *p, sop op, size_t opnd, sopno pos);
  306. static void dofwd(register struct parse *p, sopno pos, sop value);
  307. static void enlarge(register struct parse *p, sopno size);
  308. static void stripsnug(register struct parse *p, register struct re_guts *g);
  309. static void findmust(register struct parse *p, register struct re_guts *g);
  310. static sopno pluscount(register struct parse *p, register struct re_guts *g);
  311. #ifdef __cplusplus
  312. }
  313. #endif
  314. static char nuls[10]; /* place to point scanner in event of error */
  315. /*
  316. * macros for use with parse structure
  317. * BEWARE: these know that the parse structure is named `p' !!!
  318. */
  319. #define PEEK() (*p->next)
  320. #define PEEK2() (*(p->next+1))
  321. #define MORE() (p->next < p->end)
  322. #define MORE2() (p->next+1 < p->end)
  323. #define SEE(c) (MORE() && PEEK() == (c))
  324. #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
  325. #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0)
  326. #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
  327. #define NEXT() (p->next++)
  328. #define NEXT2() (p->next += 2)
  329. #define NEXTn(n) (p->next += (n))
  330. #define GETNEXT() (*p->next++)
  331. #define SETERROR(e) seterr(p, (e))
  332. #define REQUIRE(co, e) ((co) || SETERROR(e))
  333. #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e))
  334. #define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e))
  335. #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e))
  336. #define EMIT(op, sopnd) doemit(p, (sop)(op), (size_t)(sopnd))
  337. #define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
  338. #define AHEAD(pos) dofwd(p, pos, HERE()-(pos))
  339. #define ASTERN(sop, pos) EMIT(sop, HERE()-pos)
  340. #define HERE() (p->slen)
  341. #define THERE() (p->slen - 1)
  342. #define THERETHERE() (p->slen - 2)
  343. #define DROP(n) (p->slen -= (n))
  344. #define never 0 /* some <assert.h>s have bugs too */
  345. int /* 0 success, otherwise REG_something */
  346. regcomp(preg, pattern, cflags)
  347. regex_t *preg;
  348. const char *pattern;
  349. int cflags;
  350. {
  351. struct parse pa;
  352. register struct re_guts *g;
  353. register struct parse *p = &pa;
  354. register int i;
  355. register size_t len;
  356. #define GOODFLAGS(f) ((f)&~REG_DUMP)
  357. cflags = GOODFLAGS(cflags);
  358. if ((cflags&REG_EXTENDED) && (cflags&REG_NOSPEC))
  359. return(REG_INVARG);
  360. if (cflags&REG_PEND) {
  361. if (preg->re_endp < pattern)
  362. return(REG_INVARG);
  363. len = preg->re_endp - pattern;
  364. } else
  365. len = strlen((char *)pattern);
  366. /* do the mallocs early so failure handling is easy */
  367. g = (struct re_guts *)malloc(sizeof(struct re_guts) +
  368. (NC-1)*sizeof(cat_t));
  369. if (g == NULL)
  370. return(REG_ESPACE);
  371. p->ssize = len/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
  372. p->strip = (sop *)malloc(p->ssize * sizeof(sop));
  373. p->slen = 0;
  374. if (p->strip == NULL) {
  375. free((char *)g);
  376. return(REG_ESPACE);
  377. }
  378. /* set things up */
  379. p->g = g;
  380. p->next = (char *)pattern; /* convenience; we do not modify it */
  381. p->end = p->next + len;
  382. p->error = 0;
  383. p->ncsalloc = 0;
  384. for (i = 0; i < NPAREN; i++) {
  385. p->pbegin[i] = 0;
  386. p->pend[i] = 0;
  387. }
  388. g->csetsize = NC;
  389. g->sets = NULL;
  390. g->setbits = NULL;
  391. g->ncsets = 0;
  392. g->cflags = cflags;
  393. g->iflags = 0;
  394. g->nbol = 0;
  395. g->neol = 0;
  396. g->must = NULL;
  397. g->mlen = 0;
  398. g->nsub = 0;
  399. g->ncategories = 1; /* category 0 is "everything else" */
  400. g->categories = &g->catspace[-(CHAR_MIN)];
  401. (void) memset((char *)g->catspace, 0, NC*sizeof(cat_t));
  402. g->backrefs = 0;
  403. /* do it */
  404. EMIT(OEND, 0);
  405. g->firststate = THERE();
  406. if (cflags&REG_EXTENDED)
  407. p_ere(p, OUT);
  408. else if (cflags&REG_NOSPEC)
  409. p_str(p);
  410. else
  411. p_bre(p, OUT, OUT);
  412. EMIT(OEND, 0);
  413. g->laststate = THERE();
  414. /* tidy up loose ends and fill things in */
  415. categorize(p, g);
  416. stripsnug(p, g);
  417. findmust(p, g);
  418. g->nplus = pluscount(p, g);
  419. g->magic = MAGIC2;
  420. preg->re_nsub = g->nsub;
  421. preg->re_g = g;
  422. preg->re_magic = MAGIC1;
  423. /* not debugging, so can't rely on the assert() in regexec() */
  424. if (g->iflags&BAD)
  425. SETERROR(REG_ASSERT);
  426. /* win or lose, we're done */
  427. if (p->error != 0) /* lose */
  428. regfree(preg);
  429. return(p->error);
  430. #undef GOODFLAGS
  431. }
  432. /*
  433. - p_ere - ERE parser top level, concatenation and alternation
  434. == static void p_ere(register struct parse *p, int stop);
  435. */
  436. static void
  437. p_ere(p, stop)
  438. register struct parse *p;
  439. int stop; /* character this ERE should end at */
  440. {
  441. register char c;
  442. register sopno prevback;
  443. register sopno prevfwd;
  444. register sopno conc;
  445. register int first = 1; /* is this the first alternative? */
  446. for (;;) {
  447. /* do a bunch of concatenated expressions */
  448. conc = HERE();
  449. while (MORE() && (c = PEEK()) != '|' && c != stop)
  450. p_ere_exp(p);
  451. REQUIRE(HERE() != conc, REG_EMPTY); /* require nonempty */
  452. if (!EAT('|'))
  453. break; /* NOTE BREAK OUT */
  454. if (first) {
  455. INSERT(OCH_, conc); /* offset is wrong */
  456. prevfwd = conc;
  457. prevback = conc;
  458. first = 0;
  459. }
  460. ASTERN(OOR1, prevback);
  461. prevback = THERE();
  462. AHEAD(prevfwd); /* fix previous offset */
  463. prevfwd = HERE();
  464. EMIT(OOR2, 0); /* offset is very wrong */
  465. }
  466. if (!first) { /* tail-end fixups */
  467. AHEAD(prevfwd);
  468. ASTERN(O_CH, prevback);
  469. }
  470. assert(!MORE() || SEE(stop));
  471. }
  472. /*
  473. - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
  474. == static void p_ere_exp(register struct parse *p);
  475. */
  476. static void
  477. p_ere_exp(p)
  478. register struct parse *p;
  479. {
  480. register char c;
  481. register sopno pos;
  482. register int count;
  483. register int count2;
  484. register sopno subno;
  485. int wascaret = 0;
  486. assert(MORE()); /* caller should have ensured this */
  487. c = GETNEXT();
  488. pos = HERE();
  489. switch (c) {
  490. case '(':
  491. REQUIRE(MORE(), REG_EPAREN);
  492. p->g->nsub++;
  493. subno = p->g->nsub;
  494. if (subno < NPAREN)
  495. p->pbegin[subno] = HERE();
  496. EMIT(OLPAREN, subno);
  497. if (!SEE(')'))
  498. p_ere(p, ')');
  499. if (subno < NPAREN) {
  500. p->pend[subno] = HERE();
  501. assert(p->pend[subno] != 0);
  502. }
  503. EMIT(ORPAREN, subno);
  504. MUSTEAT(')', REG_EPAREN);
  505. break;
  506. case '^':
  507. EMIT(OBOL, 0);
  508. p->g->iflags |= USEBOL;
  509. p->g->nbol++;
  510. wascaret = 1;
  511. break;
  512. case '$':
  513. EMIT(OEOL, 0);
  514. p->g->iflags |= USEEOL;
  515. p->g->neol++;
  516. break;
  517. case '|':
  518. SETERROR(REG_EMPTY);
  519. break;
  520. case '*':
  521. case '+':
  522. case '?':
  523. SETERROR(REG_BADRPT);
  524. break;
  525. case '.':
  526. if (p->g->cflags&REG_NEWLINE)
  527. nonnewline(p);
  528. else
  529. EMIT(OANY, 0);
  530. break;
  531. case '[':
  532. p_bracket(p);
  533. break;
  534. case '\\':
  535. REQUIRE(MORE(), REG_EESCAPE);
  536. c = GETNEXT();
  537. ordinary(p, c);
  538. break;
  539. case '{': /* okay as ordinary except if digit follows */
  540. REQUIRE(!MORE() || !isdigit(PEEK()), REG_BADRPT);
  541. /* FALLTHROUGH */
  542. default:
  543. ordinary(p, c);
  544. break;
  545. }
  546. if (!MORE())
  547. return;
  548. c = PEEK();
  549. /* we call { a repetition if followed by a digit */
  550. if (!( c == '*' || c == '+' || c == '?' ||
  551. (c == '{' && MORE2() && isdigit(PEEK2())) ))
  552. return; /* no repetition, we're done */
  553. NEXT();
  554. REQUIRE(!wascaret, REG_BADRPT);
  555. switch (c) {
  556. case '*': /* implemented as +? */
  557. /* this case does not require the (y|) trick, noKLUDGE */
  558. INSERT(OPLUS_, pos);
  559. ASTERN(O_PLUS, pos);
  560. INSERT(OQUEST_, pos);
  561. ASTERN(O_QUEST, pos);
  562. break;
  563. case '+':
  564. INSERT(OPLUS_, pos);
  565. ASTERN(O_PLUS, pos);
  566. break;
  567. case '?':
  568. /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
  569. INSERT(OCH_, pos); /* offset slightly wrong */
  570. ASTERN(OOR1, pos); /* this one's right */
  571. AHEAD(pos); /* fix the OCH_ */
  572. EMIT(OOR2, 0); /* offset very wrong... */
  573. AHEAD(THERE()); /* ...so fix it */
  574. ASTERN(O_CH, THERETHERE());
  575. break;
  576. case '{':
  577. count = p_count(p);
  578. if (EAT(',')) {
  579. if (isdigit(PEEK())) {
  580. count2 = p_count(p);
  581. REQUIRE(count <= count2, REG_BADBR);
  582. } else /* single number with comma */
  583. count2 = INFINITY;
  584. } else /* just a single number */
  585. count2 = count;
  586. repeat(p, pos, count, count2);
  587. if (!EAT('}')) { /* error heuristics */
  588. while (MORE() && PEEK() != '}')
  589. NEXT();
  590. REQUIRE(MORE(), REG_EBRACE);
  591. SETERROR(REG_BADBR);
  592. }
  593. break;
  594. }
  595. if (!MORE())
  596. return;
  597. c = PEEK();
  598. if (!( c == '*' || c == '+' || c == '?' ||
  599. (c == '{' && MORE2() && isdigit(PEEK2())) ) )
  600. return;
  601. SETERROR(REG_BADRPT);
  602. }
  603. /*
  604. - p_str - string (no metacharacters) "parser"
  605. == static void p_str(register struct parse *p);
  606. */
  607. static void
  608. p_str(p)
  609. register struct parse *p;
  610. {
  611. REQUIRE(MORE(), REG_EMPTY);
  612. while (MORE())
  613. ordinary(p, GETNEXT());
  614. }
  615. /*
  616. - p_bre - BRE parser top level, anchoring and concatenation
  617. == static void p_bre(register struct parse *p, register int end1, \
  618. == register int end2);
  619. * Giving end1 as OUT essentially eliminates the end1/end2 check.
  620. *
  621. * This implementation is a bit of a kludge, in that a trailing $ is first
  622. * taken as an ordinary character and then revised to be an anchor. The
  623. * only undesirable side effect is that '$' gets included as a character
  624. * category in such cases. This is fairly harmless; not worth fixing.
  625. * The amount of lookahead needed to avoid this kludge is excessive.
  626. */
  627. static void
  628. p_bre(p, end1, end2)
  629. register struct parse *p;
  630. register int end1; /* first terminating character */
  631. register int end2; /* second terminating character */
  632. {
  633. register sopno start = HERE();
  634. register int first = 1; /* first subexpression? */
  635. register int wasdollar = 0;
  636. if (EAT('^')) {
  637. EMIT(OBOL, 0);
  638. p->g->iflags |= USEBOL;
  639. p->g->nbol++;
  640. }
  641. while (MORE() && !SEETWO(end1, end2)) {
  642. wasdollar = p_simp_re(p, first);
  643. first = 0;
  644. }
  645. if (wasdollar) { /* oops, that was a trailing anchor */
  646. DROP(1);
  647. EMIT(OEOL, 0);
  648. p->g->iflags |= USEEOL;
  649. p->g->neol++;
  650. }
  651. REQUIRE(HERE() != start, REG_EMPTY); /* require nonempty */
  652. }
  653. /*
  654. - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
  655. == static int p_simp_re(register struct parse *p, int starordinary);
  656. */
  657. static int /* was the simple RE an unbackslashed $? */
  658. p_simp_re(p, starordinary)
  659. register struct parse *p;
  660. int starordinary; /* is a leading * an ordinary character? */
  661. {
  662. register int c;
  663. register int count;
  664. register int count2;
  665. register sopno pos;
  666. register int i;
  667. register sopno subno;
  668. # define BACKSL (1<<CHAR_BIT)
  669. pos = HERE(); /* repetion op, if any, covers from here */
  670. assert(MORE()); /* caller should have ensured this */
  671. c = GETNEXT();
  672. if (c == '\\') {
  673. REQUIRE(MORE(), REG_EESCAPE);
  674. c = BACKSL | (unsigned char)GETNEXT();
  675. }
  676. switch (c) {
  677. case '.':
  678. if (p->g->cflags&REG_NEWLINE)
  679. nonnewline(p);
  680. else
  681. EMIT(OANY, 0);
  682. break;
  683. case '[':
  684. p_bracket(p);
  685. break;
  686. case BACKSL|'{':
  687. SETERROR(REG_BADRPT);
  688. break;
  689. case BACKSL|'(':
  690. p->g->nsub++;
  691. subno = p->g->nsub;
  692. if (subno < NPAREN)
  693. p->pbegin[subno] = HERE();
  694. EMIT(OLPAREN, subno);
  695. /* the MORE here is an error heuristic */
  696. if (MORE() && !SEETWO('\\', ')'))
  697. p_bre(p, '\\', ')');
  698. if (subno < NPAREN) {
  699. p->pend[subno] = HERE();
  700. assert(p->pend[subno] != 0);
  701. }
  702. EMIT(ORPAREN, subno);
  703. REQUIRE(EATTWO('\\', ')'), REG_EPAREN);
  704. break;
  705. case BACKSL|')': /* should not get here -- must be user */
  706. case BACKSL|'}':
  707. SETERROR(REG_EPAREN);
  708. break;
  709. case BACKSL|'1':
  710. case BACKSL|'2':
  711. case BACKSL|'3':
  712. case BACKSL|'4':
  713. case BACKSL|'5':
  714. case BACKSL|'6':
  715. case BACKSL|'7':
  716. case BACKSL|'8':
  717. case BACKSL|'9':
  718. i = (c&~BACKSL) - '0';
  719. assert(i < NPAREN);
  720. if (p->pend[i] != 0) {
  721. assert(i <= p->g->nsub);
  722. EMIT(OBACK_, i);
  723. assert(p->pbegin[i] != 0);
  724. assert(OP(p->strip[p->pbegin[i]]) == OLPAREN);
  725. assert(OP(p->strip[p->pend[i]]) == ORPAREN);
  726. (void) dupl(p, p->pbegin[i]+1, p->pend[i]);
  727. EMIT(O_BACK, i);
  728. } else
  729. SETERROR(REG_ESUBREG);
  730. p->g->backrefs = 1;
  731. break;
  732. case '*':
  733. REQUIRE(starordinary, REG_BADRPT);
  734. /* FALLTHROUGH */
  735. default:
  736. ordinary(p, (char)c); /* takes off BACKSL, if any */
  737. break;
  738. }
  739. if (EAT('*')) { /* implemented as +? */
  740. /* this case does not require the (y|) trick, noKLUDGE */
  741. INSERT(OPLUS_, pos);
  742. ASTERN(O_PLUS, pos);
  743. INSERT(OQUEST_, pos);
  744. ASTERN(O_QUEST, pos);
  745. } else if (EATTWO('\\', '{')) {
  746. count = p_count(p);
  747. if (EAT(',')) {
  748. if (MORE() && isdigit(PEEK())) {
  749. count2 = p_count(p);
  750. REQUIRE(count <= count2, REG_BADBR);
  751. } else /* single number with comma */
  752. count2 = INFINITY;
  753. } else /* just a single number */
  754. count2 = count;
  755. repeat(p, pos, count, count2);
  756. if (!EATTWO('\\', '}')) { /* error heuristics */
  757. while (MORE() && !SEETWO('\\', '}'))
  758. NEXT();
  759. REQUIRE(MORE(), REG_EBRACE);
  760. SETERROR(REG_BADBR);
  761. }
  762. } else if (c == (unsigned char)'$') /* $ (but not \$) ends it */
  763. return(1);
  764. return(0);
  765. }
  766. /*
  767. - p_count - parse a repetition count
  768. == static int p_count(register struct parse *p);
  769. */
  770. static int /* the value */
  771. p_count(p)
  772. register struct parse *p;
  773. {
  774. register int count = 0;
  775. register int ndigits = 0;
  776. while (MORE() && isdigit(PEEK()) && count <= DUPMAX) {
  777. count = count*10 + (GETNEXT() - '0');
  778. ndigits++;
  779. }
  780. REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR);
  781. return(count);
  782. }
  783. /*
  784. - p_bracket - parse a bracketed character list
  785. == static void p_bracket(register struct parse *p);
  786. *
  787. * Note a significant property of this code: if the allocset() did SETERROR,
  788. * no set operations are done.
  789. */
  790. static void
  791. p_bracket(p)
  792. register struct parse *p;
  793. {
  794. register cset *cs = allocset(p);
  795. register int invert = 0;
  796. /* Dept of Truly Sickening Special-Case Kludges */
  797. if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]", 6) == 0) {
  798. EMIT(OBOW, 0);
  799. NEXTn(6);
  800. return;
  801. }
  802. if (p->next + 5 < p->end && strncmp(p->next, "[:>:]]", 6) == 0) {
  803. EMIT(OEOW, 0);
  804. NEXTn(6);
  805. return;
  806. }
  807. if (EAT('^'))
  808. invert++; /* make note to invert set at end */
  809. if (EAT(']'))
  810. CHadd(cs, ']');
  811. else if (EAT('-'))
  812. CHadd(cs, '-');
  813. while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
  814. p_b_term(p, cs);
  815. if (EAT('-'))
  816. CHadd(cs, '-');
  817. MUSTEAT(']', REG_EBRACK);
  818. if (p->error != 0) /* don't mess things up further */
  819. return;
  820. if (p->g->cflags&REG_ICASE) {
  821. register int i;
  822. register int ci;
  823. for (i = p->g->csetsize - 1; i >= 0; i--)
  824. if (CHIN(cs, i) && isalpha(i)) {
  825. ci = othercase(i);
  826. if (ci != i)
  827. CHadd(cs, ci);
  828. }
  829. if (cs->multis != NULL)
  830. mccase(p, cs);
  831. }
  832. if (invert) {
  833. register int i;
  834. for (i = p->g->csetsize - 1; i >= 0; i--)
  835. if (CHIN(cs, i))
  836. CHsub(cs, i);
  837. else
  838. CHadd(cs, i);
  839. if (p->g->cflags&REG_NEWLINE)
  840. CHsub(cs, '\n');
  841. if (cs->multis != NULL)
  842. mcinvert(p, cs);
  843. }
  844. assert(cs->multis == NULL); /* xxx */
  845. if (nch(p, cs) == 1) { /* optimize singleton sets */
  846. ordinary(p, firstch(p, cs));
  847. freeset(p, cs);
  848. } else
  849. EMIT(OANYOF, freezeset(p, cs));
  850. }
  851. /*
  852. - p_b_term - parse one term of a bracketed character list
  853. == static void p_b_term(register struct parse *p, register cset *cs);
  854. */
  855. static void
  856. p_b_term(p, cs)
  857. register struct parse *p;
  858. register cset *cs;
  859. {
  860. register char c;
  861. register char start, finish;
  862. register int i;
  863. /* classify what we've got */
  864. switch ((MORE()) ? PEEK() : '\0') {
  865. case '[':
  866. c = (MORE2()) ? PEEK2() : '\0';
  867. break;
  868. case '-':
  869. SETERROR(REG_ERANGE);
  870. return; /* NOTE RETURN */
  871. break;
  872. default:
  873. c = '\0';
  874. break;
  875. }
  876. switch (c) {
  877. case ':': /* character class */
  878. NEXT2();
  879. REQUIRE(MORE(), REG_EBRACK);
  880. c = PEEK();
  881. REQUIRE(c != '-' && c != ']', REG_ECTYPE);
  882. p_b_cclass(p, cs);
  883. REQUIRE(MORE(), REG_EBRACK);
  884. REQUIRE(EATTWO(':', ']'), REG_ECTYPE);
  885. break;
  886. case '=': /* equivalence class */
  887. NEXT2();
  888. REQUIRE(MORE(), REG_EBRACK);
  889. c = PEEK();
  890. REQUIRE(c != '-' && c != ']', REG_ECOLLATE);
  891. p_b_eclass(p, cs);
  892. REQUIRE(MORE(), REG_EBRACK);
  893. REQUIRE(EATTWO('=', ']'), REG_ECOLLATE);
  894. break;
  895. default: /* symbol, ordinary character, or range */
  896. /* xxx revision needed for multichar stuff */
  897. start = p_b_symbol(p);
  898. if (SEE('-') && MORE2() && PEEK2() != ']') {
  899. /* range */
  900. NEXT();
  901. if (EAT('-'))
  902. finish = '-';
  903. else
  904. finish = p_b_symbol(p);
  905. } else
  906. finish = start;
  907. /* xxx what about signed chars here... */
  908. REQUIRE(start <= finish, REG_ERANGE);
  909. for (i = start; i <= finish; i++)
  910. CHadd(cs, i);
  911. break;
  912. }
  913. }
  914. /*
  915. - p_b_cclass - parse a character-class name and deal with it
  916. == static void p_b_cclass(register struct parse *p, register cset *cs);
  917. */
  918. static void
  919. p_b_cclass(p, cs)
  920. register struct parse *p;
  921. register cset *cs;
  922. {
  923. register char *sp = p->next;
  924. register struct cclass *cp;
  925. register size_t len;
  926. register char *u;
  927. register char c;
  928. while (MORE() && isalpha(PEEK()))
  929. NEXT();
  930. len = p->next - sp;
  931. for (cp = cclasses; cp->name != NULL; cp++)
  932. if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
  933. break;
  934. if (cp->name == NULL) {
  935. /* oops, didn't find it */
  936. SETERROR(REG_ECTYPE);
  937. return;
  938. }
  939. u = cp->chars;
  940. while ((c = *u++) != '\0')
  941. CHadd(cs, c);
  942. for (u = cp->multis; *u != '\0'; u += strlen(u) + 1)
  943. MCadd(p, cs, u);
  944. }
  945. /*
  946. - p_b_eclass - parse an equivalence-class name and deal with it
  947. == static void p_b_eclass(register struct parse *p, register cset *cs);
  948. *
  949. * This implementation is incomplete. xxx
  950. */
  951. static void
  952. p_b_eclass(p, cs)
  953. register struct parse *p;
  954. register cset *cs;
  955. {
  956. register char c;
  957. c = p_b_coll_elem(p, '=');
  958. CHadd(cs, c);
  959. }
  960. /*
  961. - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
  962. == static char p_b_symbol(register struct parse *p);
  963. */
  964. static char /* value of symbol */
  965. p_b_symbol(p)
  966. register struct parse *p;
  967. {
  968. register char value;
  969. REQUIRE(MORE(), REG_EBRACK);
  970. if (!EATTWO('[', '.'))
  971. return(GETNEXT());
  972. /* collating symbol */
  973. value = p_b_coll_elem(p, '.');
  974. REQUIRE(EATTWO('.', ']'), REG_ECOLLATE);
  975. return(value);
  976. }
  977. /*
  978. - p_b_coll_elem - parse a collating-element name and look it up
  979. == static char p_b_coll_elem(register struct parse *p, int endc);
  980. */
  981. static char /* value of collating element */
  982. p_b_coll_elem(p, endc)
  983. register struct parse *p;
  984. int endc; /* name ended by endc,']' */
  985. {
  986. register char *sp = p->next;
  987. register struct cname *cp;
  988. register int len;
  989. while (MORE() && !SEETWO(endc, ']'))
  990. NEXT();
  991. if (!MORE()) {
  992. SETERROR(REG_EBRACK);
  993. return(0);
  994. }
  995. len = p->next - sp;
  996. for (cp = cnames; cp->name != NULL; cp++)
  997. if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
  998. return(cp->code); /* known name */
  999. if (len == 1)
  1000. return(*sp); /* single character */
  1001. SETERROR(REG_ECOLLATE); /* neither */
  1002. return(0);
  1003. }
  1004. /*
  1005. - othercase - return the case counterpart of an alphabetic
  1006. == static char othercase(int ch);
  1007. */
  1008. static char /* if no counterpart, return ch */
  1009. othercase(ch)
  1010. int ch;
  1011. {
  1012. assert(isalpha(ch));
  1013. if (isupper(ch))
  1014. return(tolower(ch));
  1015. else if (islower(ch))
  1016. return(toupper(ch));
  1017. else /* peculiar, but could happen */
  1018. return(ch);
  1019. }
  1020. /*
  1021. - bothcases - emit a dualcase version of a two-case character
  1022. == static void bothcases(register struct parse *p, int ch);
  1023. *
  1024. * Boy, is this implementation ever a kludge...
  1025. */
  1026. static void
  1027. bothcases(p, ch)
  1028. register struct parse *p;
  1029. int ch;
  1030. {
  1031. register char *oldnext = p->next;
  1032. register char *oldend = p->end;
  1033. char bracket[3];
  1034. assert(othercase(ch) != ch); /* p_bracket() would recurse */
  1035. p->next = bracket;
  1036. p->end = bracket+2;
  1037. bracket[0] = ch;
  1038. bracket[1] = ']';
  1039. bracket[2] = '\0';
  1040. p_bracket(p);
  1041. assert(p->next == bracket+2);
  1042. p->next = oldnext;
  1043. p->end = oldend;
  1044. }
  1045. /*
  1046. - ordinary - emit an ordinary character
  1047. == static void ordinary(register struct parse *p, register int ch);
  1048. */
  1049. static void
  1050. ordinary(p, ch)
  1051. register struct parse *p;
  1052. register int ch;
  1053. {
  1054. register cat_t *cap = p->g->categories;
  1055. if ((p->g->cflags&REG_ICASE) && isalpha(ch) && othercase(ch) != ch)
  1056. bothcases(p, ch);
  1057. else {
  1058. EMIT(OCHAR, (unsigned char)ch);
  1059. if (cap[ch] == 0)
  1060. cap[ch] = p->g->ncategories++;
  1061. }
  1062. }
  1063. /*
  1064. - nonnewline - emit REG_NEWLINE version of OANY
  1065. == static void nonnewline(register struct parse *p);
  1066. *
  1067. * Boy, is this implementation ever a kludge...
  1068. */
  1069. static void
  1070. nonnewline(p)
  1071. register struct parse *p;
  1072. {
  1073. register char *oldnext = p->next;
  1074. register char *oldend = p->end;
  1075. char bracket[4];
  1076. p->next = bracket;
  1077. p->end = bracket+3;
  1078. bracket[0] = '^';
  1079. bracket[1] = '\n';
  1080. bracket[2] = ']';
  1081. bracket[3] = '\0';
  1082. p_bracket(p);
  1083. assert(p->next == bracket+3);
  1084. p->next = oldnext;
  1085. p->end = oldend;
  1086. }
  1087. /*
  1088. - repeat - generate code for a bounded repetition, recursively if needed
  1089. == static void repeat(register struct parse *p, sopno start, int from, int to);
  1090. */
  1091. static void
  1092. repeat(p, start, from, to)
  1093. register struct parse *p;
  1094. sopno start; /* operand from here to end of strip */
  1095. int from; /* repeated from this number */
  1096. int to; /* to this number of times (maybe INFINITY) */
  1097. {
  1098. register sopno finish = HERE();
  1099. # define N 2
  1100. # define INF 3
  1101. # define REP(f, t) ((f)*8 + (t))
  1102. # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
  1103. register sopno copy;
  1104. if (p->error != 0) /* head off possible runaway recursion */
  1105. return;
  1106. assert(from <= to);
  1107. switch (REP(MAP(from), MAP(to))) {
  1108. case REP(0, 0): /* must be user doing this */
  1109. DROP(finish-start); /* drop the operand */
  1110. break;
  1111. case REP(0, 1): /* as x{1,1}? */
  1112. case REP(0, N): /* as x{1,n}? */
  1113. case REP(0, INF): /* as x{1,}? */
  1114. /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
  1115. INSERT(OCH_, start); /* offset is wrong... */
  1116. repeat(p, start+1, 1, to);
  1117. ASTERN(OOR1, start);
  1118. AHEAD(start); /* ... fix it */
  1119. EMIT(OOR2, 0);
  1120. AHEAD(THERE());
  1121. ASTERN(O_CH, THERETHERE());
  1122. break;
  1123. case REP(1, 1): /* trivial case */
  1124. /* done */
  1125. break;
  1126. case REP(1, N): /* as x?x{1,n-1} */
  1127. /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
  1128. INSERT(OCH_, start);
  1129. ASTERN(OOR1, start);
  1130. AHEAD(start);
  1131. EMIT(OOR2, 0); /* offset very wrong... */
  1132. AHEAD(THERE()); /* ...so fix it */
  1133. ASTERN(O_CH, THERETHERE());
  1134. copy = dupl(p, start+1, finish+1);
  1135. assert(copy == finish+4);
  1136. repeat(p, copy, 1, to-1);
  1137. break;
  1138. case REP(1, INF): /* as x+ */
  1139. INSERT(OPLUS_, start);
  1140. ASTERN(O_PLUS, start);
  1141. break;
  1142. case REP(N, N): /* as xx{m-1,n-1} */
  1143. copy = dupl(p, start, finish);
  1144. repeat(p, copy, from-1, to-1);
  1145. break;
  1146. case REP(N, INF): /* as xx{n-1,INF} */
  1147. copy = dupl(p, start, finish);
  1148. repeat(p, copy, from-1, to);
  1149. break;
  1150. default: /* "can't happen" */
  1151. SETERROR(REG_ASSERT); /* just in case */
  1152. break;
  1153. }
  1154. }
  1155. /*
  1156. - seterr - set an error condition
  1157. == static int seterr(register struct parse *p, int e);
  1158. */
  1159. static int /* useless but makes type checking happy */
  1160. seterr(p, e)
  1161. register struct parse *p;
  1162. int e;
  1163. {
  1164. if (p->error == 0) /* keep earliest error condition */
  1165. p->error = e;
  1166. p->next = nuls; /* try to bring things to a halt */
  1167. p->end = nuls;
  1168. return(0); /* make the return value well-defined */
  1169. }
  1170. /*
  1171. - allocset - allocate a set of characters for []
  1172. == static cset *allocset(register struct parse *p);
  1173. */
  1174. static cset *
  1175. allocset(p)
  1176. register struct parse *p;
  1177. {
  1178. register int no = p->g->ncsets++;
  1179. register size_t nc;
  1180. register size_t nbytes;
  1181. register cset *cs;
  1182. register size_t css = (size_t)p->g->csetsize;
  1183. register int i;
  1184. if (no >= p->ncsalloc) { /* need another column of space */
  1185. p->ncsalloc += CHAR_BIT;
  1186. nc = p->ncsalloc;
  1187. assert(nc % CHAR_BIT == 0);
  1188. nbytes = nc / CHAR_BIT * css;
  1189. if (p->g->sets == NULL)
  1190. p->g->sets = (cset *)malloc(nc * sizeof(cset));
  1191. else
  1192. p->g->sets = (cset *)realloc((char *)p->g->sets,
  1193. nc * sizeof(cset));
  1194. if (p->g->setbits == NULL)
  1195. p->g->setbits = (uch *)malloc(nbytes);
  1196. else {
  1197. p->g->setbits = (uch *)realloc((char *)p->g->setbits,
  1198. nbytes);
  1199. /* xxx this isn't right if setbits is now NULL */
  1200. for (i = 0; i < no; i++)
  1201. p->g->sets[i].ptr = p->g->setbits + css*(i/CHAR_BIT);
  1202. }
  1203. if (p->g->sets != NULL && p->g->setbits != NULL)
  1204. (void) memset((char *)p->g->setbits + (nbytes - css),
  1205. 0, css);
  1206. else {
  1207. no = 0;
  1208. SETERROR(REG_ESPACE);
  1209. /* caller's responsibility not to do set ops */
  1210. }
  1211. }
  1212. assert(p->g->sets != NULL); /* xxx */
  1213. cs = &p->g->sets[no];
  1214. cs->ptr = p->g->setbits + css*((no)/CHAR_BIT);
  1215. cs->mask = 1 << ((no) % CHAR_BIT);
  1216. cs->hash = 0;
  1217. cs->smultis = 0;
  1218. cs->multis = NULL;
  1219. return(cs);
  1220. }
  1221. /*
  1222. - freeset - free a now-unused set
  1223. == static void freeset(register struct parse *p, register cset *cs);
  1224. */
  1225. static void
  1226. freeset(p, cs)
  1227. register struct parse *p;
  1228. register cset *cs;
  1229. {
  1230. register int i;
  1231. register cset *top = &p->g->sets[p->g->ncsets];
  1232. register size_t css = (size_t)p->g->csetsize;
  1233. for (i = 0; i < css; i++)
  1234. CHsub(cs, i);
  1235. if (cs == top-1) /* recover only the easy case */
  1236. p->g->ncsets--;
  1237. }
  1238. /*
  1239. - freezeset - final processing on a set of characters
  1240. == static int freezeset(register struct parse *p, register cset *cs);
  1241. *
  1242. * The main task here is merging identical sets. This is usually a waste
  1243. * of time (although the hash code minimizes the overhead), but can win
  1244. * big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash
  1245. * is done using addition rather than xor -- all ASCII [aA] sets xor to
  1246. * the same value!
  1247. */
  1248. static int /* set number */
  1249. freezeset(p, cs)
  1250. register struct parse *p;
  1251. register cset *cs;
  1252. {
  1253. register uch h = cs->hash;
  1254. register int i;
  1255. register cset *top = &p->g->sets[p->g->ncsets];
  1256. register cset *cs2;
  1257. register size_t css = (size_t)p->g->csetsize;
  1258. /* look for an earlier one which is the same */
  1259. for (cs2 = &p->g->sets[0]; cs2 < top; cs2++)
  1260. if (cs2->hash == h && cs2 != cs) {
  1261. /* maybe */
  1262. for (i = 0; i < css; i++)
  1263. if (!!CHIN(cs2, i) != !!CHIN(cs, i))
  1264. break; /* no */
  1265. if (i == css)
  1266. break; /* yes */
  1267. }
  1268. if (cs2 < top) { /* found one */
  1269. freeset(p, cs);
  1270. cs = cs2;
  1271. }
  1272. return((int)(cs - p->g->sets));
  1273. }
  1274. /*
  1275. - firstch - return first character in a set (which must have at least one)
  1276. == static int firstch(register struct parse *p, register cset *cs);
  1277. */
  1278. static int /* character; there is no "none" value */
  1279. firstch(p, cs)
  1280. register struct parse *p;
  1281. register cset *cs;
  1282. {
  1283. register int i;
  1284. register size_t css = (size_t)p->g->csetsize;
  1285. for (i = 0; i < css; i++)
  1286. if (CHIN(cs, i))
  1287. return((char)i);
  1288. assert(never);
  1289. return(0); /* arbitrary */
  1290. }
  1291. /*
  1292. - nch - number of characters in a set
  1293. == static int nch(register struct parse *p, register cset *cs);
  1294. */
  1295. static int
  1296. nch(p, cs)
  1297. register struct parse *p;
  1298. register cset *cs;
  1299. {
  1300. register int i;
  1301. register size_t css = (size_t)p->g->csetsize;
  1302. register int n = 0;
  1303. for (i = 0; i < css; i++)
  1304. if (CHIN(cs, i))
  1305. n++;
  1306. return(n);
  1307. }
  1308. /*
  1309. - mcadd - add a collating element to a cset
  1310. == static void mcadd(register struct parse *p, register cset *cs, \
  1311. == register char *cp);
  1312. */
  1313. static void
  1314. mcadd(p, cs, cp)
  1315. register struct parse *p;
  1316. register cset *cs;
  1317. register char *cp;
  1318. {
  1319. register size_t oldend = cs->smultis;
  1320. cs->smultis += strlen(cp) + 1;
  1321. if (cs->multis == NULL)
  1322. cs->multis = malloc(cs->smultis);
  1323. else
  1324. cs->multis = realloc(cs->multis, cs->smultis);
  1325. if (cs->multis == NULL) {
  1326. SETERROR(REG_ESPACE);
  1327. return;
  1328. }
  1329. (void) strcpy(cs->multis + oldend - 1, cp);
  1330. cs->multis[cs->smultis - 1] = '\0';
  1331. }
  1332. /*
  1333. - mcinvert - invert the list of collating elements in a cset
  1334. == static void mcinvert(register struct parse *p, register cset *cs);
  1335. *
  1336. * This would have to know the set of possibilities. Implementation
  1337. * is deferred.
  1338. */
  1339. static void
  1340. mcinvert(p, cs)
  1341. register struct parse *p;
  1342. register cset *cs;
  1343. {
  1344. assert(cs->multis == NULL); /* xxx */
  1345. }
  1346. /*
  1347. - mccase - add case counterparts of the list of collating elements in a cset
  1348. == static void mccase(register struct parse *p, register cset *cs);
  1349. *
  1350. * This would have to know the set of possibilities. Implementation
  1351. * is deferred.
  1352. */
  1353. static void
  1354. mccase(p, cs)
  1355. register struct parse *p;
  1356. register cset *cs;
  1357. {
  1358. assert(cs->multis == NULL); /* xxx */
  1359. }
  1360. /*
  1361. - isinsets - is this character in any sets?
  1362. == static int isinsets(register struct re_guts *g, int c);
  1363. */
  1364. static int /* predicate */
  1365. isinsets(g, c)
  1366. register struct re_guts *g;
  1367. int c;
  1368. {
  1369. register uch *col;
  1370. register int i;
  1371. register int ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
  1372. register unsigned uc = (unsigned char)c;
  1373. for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
  1374. if (col[uc] != 0)
  1375. return(1);
  1376. return(0);
  1377. }
  1378. /*
  1379. - samesets - are these two characters in exactly the same sets?
  1380. == static int samesets(register struct re_guts *g, int c1, int c2);
  1381. */
  1382. static int /* predicate */
  1383. samesets(g, c1, c2)
  1384. register struct re_guts *g;
  1385. int c1;
  1386. int c2;
  1387. {
  1388. register uch *col;
  1389. register int i;
  1390. register int ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
  1391. register unsigned uc1 = (unsigned char)c1;
  1392. register unsigned uc2 = (unsigned char)c2;
  1393. for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
  1394. if (col[uc1] != col[uc2])
  1395. return(0);
  1396. return(1);
  1397. }
  1398. /*
  1399. - categorize - sort out character categories
  1400. == static void categorize(struct parse *p, register struct re_guts *g);
  1401. */
  1402. static void
  1403. categorize(p, g)
  1404. struct parse *p;
  1405. register struct re_guts *g;
  1406. {
  1407. register cat_t *cats = g->categories;
  1408. register int c;
  1409. register int c2;
  1410. register cat_t cat;
  1411. /* avoid making error situations worse */
  1412. if (p->error != 0)
  1413. return;
  1414. for (c = CHAR_MIN; c <= CHAR_MAX; c++)
  1415. if (cats[c] == 0 && isinsets(g, c)) {
  1416. cat = g->ncategories++;
  1417. cats[c] = cat;
  1418. for (c2 = c+1; c2 <= CHAR_MAX; c2++)
  1419. if (cats[c2] == 0 && samesets(g, c, c2))
  1420. cats[c2] = cat;
  1421. }
  1422. }
  1423. /*
  1424. - dupl - emit a duplicate of a bunch of sops
  1425. == static sopno dupl(register struct parse *p, sopno start, sopno finish);
  1426. */
  1427. static sopno /* start of duplicate */
  1428. dupl(p, start, finish)
  1429. register struct parse *p;
  1430. sopno start; /* from here */
  1431. sopno finish; /* to this less one */
  1432. {
  1433. register sopno ret = HERE();
  1434. register sopno len = finish - start;
  1435. assert(finish >= start);
  1436. if (len == 0)
  1437. return(ret);
  1438. enlarge(p, p->ssize + len); /* this many unexpected additions */
  1439. assert(p->ssize >= p->slen + len);
  1440. (void) memcpy((char *)(p->strip + p->slen),
  1441. (char *)(p->strip + start), (size_t)len*sizeof(sop));
  1442. p->slen += len;
  1443. return(ret);
  1444. }
  1445. /*
  1446. - doemit - emit a strip operator
  1447. == static void doemit(register struct parse *p, sop op, size_t opnd);
  1448. *
  1449. * It might seem better to implement this as a macro with a function as
  1450. * hard-case backup, but it's just too big and messy unless there are
  1451. * some changes to the data structures. Maybe later.
  1452. */
  1453. static void
  1454. doemit(p, op, opnd)
  1455. register struct parse *p;
  1456. sop op;
  1457. size_t opnd;
  1458. {
  1459. /* avoid making error situations worse */
  1460. if (p->error != 0)
  1461. return;
  1462. /* deal with oversize operands ("can't happen", more or less) */
  1463. assert(opnd < 1<<OPSHIFT);
  1464. /* deal with undersized strip */
  1465. if (p->slen >= p->ssize)
  1466. enlarge(p, (p->ssize+1) / 2 * 3); /* +50% */
  1467. assert(p->slen < p->ssize);
  1468. /* finally, it's all reduced to the easy case */
  1469. p->strip[p->slen++] = SOP(op, opnd);
  1470. }
  1471. /*
  1472. - doinsert - insert a sop into the strip
  1473. == static void doinsert(register struct parse *p, sop op, size_t opnd, sopno pos);
  1474. */
  1475. static void
  1476. doinsert(p, op, opnd, pos)
  1477. register struct parse *p;
  1478. sop op;
  1479. size_t opnd;
  1480. sopno pos;
  1481. {
  1482. register sopno sn;
  1483. register sop s;
  1484. register int i;
  1485. /* avoid making error situations worse */
  1486. if (p->error != 0)
  1487. return;
  1488. sn = HERE();
  1489. EMIT(op, opnd); /* do checks, ensure space */
  1490. assert(HERE() == sn+1);
  1491. s = p->strip[sn];
  1492. /* adjust paren pointers */
  1493. assert(pos > 0);
  1494. for (i = 1; i < NPAREN; i++) {
  1495. if (p->pbegin[i] >= pos) {
  1496. p->pbegin[i]++;
  1497. }
  1498. if (p->pend[i] >= pos) {
  1499. p->pend[i]++;
  1500. }
  1501. }
  1502. memmove((char *)&p->strip[pos+1], (char *)&p->strip[pos],
  1503. (HERE()-pos-1)*sizeof(sop));
  1504. p->strip[pos] = s;
  1505. }
  1506. /*
  1507. - dofwd - complete a forward reference
  1508. == static void dofwd(register struct parse *p, sopno pos, sop value);
  1509. */
  1510. static void
  1511. dofwd(p, pos, value)
  1512. register struct parse *p;
  1513. register sopno pos;
  1514. sop value;
  1515. {
  1516. /* avoid making error situations worse */
  1517. if (p->error != 0)
  1518. return;
  1519. assert(value < 1<<OPSHIFT);
  1520. p->strip[pos] = OP(p->strip[pos]) | value;
  1521. }
  1522. /*
  1523. - enlarge - enlarge the strip
  1524. == static void enlarge(register struct parse *p, sopno size);
  1525. */
  1526. static void
  1527. enlarge(p, size)
  1528. register struct parse *p;
  1529. register sopno size;
  1530. {
  1531. register sop *sp;
  1532. if (p->ssize >= size)
  1533. return;
  1534. sp = (sop *)realloc(p->strip, size*sizeof(sop));
  1535. if (sp == NULL) {
  1536. SETERROR(REG_ESPACE);
  1537. return;
  1538. }
  1539. p->strip = sp;
  1540. p->ssize = size;
  1541. }
  1542. /*
  1543. - stripsnug - compact the strip
  1544. == static void stripsnug(register struct parse *p, register struct re_guts *g);
  1545. */
  1546. static void
  1547. stripsnug(p, g)
  1548. register struct parse *p;
  1549. register struct re_guts *g;
  1550. {
  1551. g->nstates = p->slen;
  1552. g->strip = (sop *)realloc((char *)p->strip, p->slen * sizeof(sop));
  1553. if (g->strip == NULL) {
  1554. SETERROR(REG_ESPACE);
  1555. g->strip = p->strip;
  1556. }
  1557. }
  1558. /*
  1559. - findmust - fill in must and mlen with longest mandatory literal string
  1560. == static void findmust(register struct parse *p, register struct re_guts *g);
  1561. *
  1562. * This algorithm could do fancy things like analyzing the operands of |
  1563. * for common subsequences. Someday. This code is simple and finds most
  1564. * of the interesting cases.
  1565. *
  1566. * Note that must and mlen got initialized during setup.
  1567. */
  1568. static void
  1569. findmust(p, g)
  1570. struct parse *p;
  1571. register struct re_guts *g;
  1572. {
  1573. register sop *scan;
  1574. sop *start;
  1575. register sop *newstart;
  1576. register sopno newlen;
  1577. register sop s;
  1578. register char *cp;
  1579. register sopno i;
  1580. /* avoid making error situations worse */
  1581. if (p->error != 0)
  1582. return;
  1583. /* find the longest OCHAR sequence in strip */
  1584. newlen = 0;
  1585. scan = g->strip + 1;
  1586. do {
  1587. s = *scan++;
  1588. switch (OP(s)) {
  1589. case OCHAR: /* sequence member */
  1590. if (newlen == 0) /* new sequence */
  1591. newstart = scan - 1;
  1592. newlen++;
  1593. break;
  1594. case OPLUS_: /* things that don't break one */
  1595. case OLPAREN:
  1596. case ORPAREN:
  1597. break;
  1598. case OQUEST_: /* things that must be skipped */
  1599. case OCH_:
  1600. scan--;
  1601. do {
  1602. scan += OPND(s);
  1603. s = *scan;
  1604. /* assert() interferes w debug printouts */
  1605. if (OP(s) != O_QUEST && OP(s) != O_CH &&
  1606. OP(s) != OOR2) {
  1607. g->iflags |= BAD;
  1608. return;
  1609. }
  1610. } while (OP(s) != O_QUEST && OP(s) != O_CH);
  1611. /* fallthrough */
  1612. default: /* things that break a sequence */
  1613. if (newlen > g->mlen) { /* ends one */
  1614. start = newstart;
  1615. g->mlen = newlen;
  1616. }
  1617. newlen = 0;
  1618. break;
  1619. }
  1620. } while (OP(s) != OEND);
  1621. if (g->mlen == 0) /* there isn't one */
  1622. return;
  1623. /* turn it into a character string */
  1624. g->must = malloc((size_t)g->mlen + 1);
  1625. if (g->must == NULL) { /* argh; just forget it */
  1626. g->mlen = 0;
  1627. return;
  1628. }
  1629. cp = g->must;
  1630. scan = start;
  1631. for (i = g->mlen; i > 0; i--) {
  1632. while (OP(s = *scan++) != OCHAR)
  1633. continue;
  1634. assert(cp < g->must + g->mlen);
  1635. *cp++ = (char)OPND(s);
  1636. }
  1637. assert(cp == g->must + g->mlen);
  1638. *cp++ = '\0'; /* just on general principles */
  1639. }
  1640. /*
  1641. - pluscount - count + nesting
  1642. == static sopno pluscount(register struct parse *p, register struct re_guts *g);
  1643. */
  1644. static sopno /* nesting depth */
  1645. pluscount(p, g)
  1646. struct parse *p;
  1647. register struct re_guts *g;
  1648. {
  1649. register sop *scan;
  1650. register sop s;
  1651. register sopno plusnest = 0;
  1652. register sopno maxnest = 0;
  1653. if (p->error != 0)
  1654. return(0); /* there may not be an OEND */
  1655. scan = g->strip + 1;
  1656. do {
  1657. s = *scan++;
  1658. switch (OP(s)) {
  1659. case OPLUS_:
  1660. plusnest++;
  1661. break;
  1662. case O_PLUS:
  1663. if (plusnest > maxnest)
  1664. maxnest = plusnest;
  1665. plusnest--;
  1666. break;
  1667. }
  1668. } while (OP(s) != OEND);
  1669. if (plusnest != 0)
  1670. g->iflags |= BAD;
  1671. return(maxnest);
  1672. }
  1673. static int nope = 0; /* for use in asserts; shuts lint up */
  1674. /* macros for manipulating states, small version */
  1675. #define states unsigned
  1676. #define states1 unsigned /* for later use in regexec() decision */
  1677. #define CLEAR(v) ((v) = 0)
  1678. #define SET0(v, n) ((v) &= ~((unsigned)1 << (n)))
  1679. #define SET1(v, n) ((v) |= (unsigned)1 << (n))
  1680. #define ISSET(v, n) ((v) & ((unsigned)1 << (n)))
  1681. #define ASSIGN(d, s) ((d) = (s))
  1682. #define EQ(a, b) ((a) == (b))
  1683. #define STATEVARS int dummy /* dummy version */
  1684. #define STATESETUP(m, n) /* nothing */
  1685. #define STATETEARDOWN(m) /* nothing */
  1686. #define SETUP(v) ((v) = 0)
  1687. #define onestate unsigned
  1688. #define INIT(o, n) ((o) = (unsigned)1 << (n))
  1689. #define INC(o) ((o) <<= 1)
  1690. #define ISSTATEIN(v, o) ((v) & (o))
  1691. /* some abbreviations; note that some of these know variable names! */
  1692. /* do "if I'm here, I can also be there" etc without branches */
  1693. #define FWD(dst, src, n) ((dst) |= ((unsigned)(src)&(here)) << (n))
  1694. #define BACK(dst, src, n) ((dst) |= ((unsigned)(src)&(here)) >> (n))
  1695. #define ISSETBACK(v, n) ((v) & ((unsigned)here >> (n)))
  1696. /* function names */
  1697. #define SNAMES /* engine.c looks after details */
  1698. /*
  1699. * The matching engine and friends. This file is #included by regexec.c
  1700. * after suitable #defines of a variety of macros used herein, so that
  1701. * different state representations can be used without duplicating masses
  1702. * of code.
  1703. */
  1704. #ifdef SNAMES
  1705. #define matcher smatcher
  1706. #define fast sfast
  1707. #define slow sslow
  1708. #define dissect sdissect
  1709. #define backref sbackref
  1710. #define step sstep
  1711. #define print sprint
  1712. #define at sat
  1713. #define match smat
  1714. #endif
  1715. #ifdef LNAMES
  1716. #define matcher lmatcher
  1717. #define fast lfast
  1718. #define slow lslow
  1719. #define dissect ldissect
  1720. #define backref lbackref
  1721. #define step lstep
  1722. #define print lprint
  1723. #define at lat
  1724. #define match lmat
  1725. #endif
  1726. /* another structure passed up and down to avoid zillions of parameters */
  1727. struct match {
  1728. struct re_guts *g;
  1729. int eflags;
  1730. regmatch_t *pmatch; /* [nsub+1] (0 element unused) */
  1731. char *offp; /* offsets work from here */
  1732. char *beginp; /* start of string -- virtual NUL precedes */
  1733. char *endp; /* end of string -- virtual NUL here */
  1734. char *coldp; /* can be no match starting before here */
  1735. char **lastpos; /* [nplus+1] */
  1736. STATEVARS;
  1737. states st; /* current states */
  1738. states fresh; /* states for a fresh start */
  1739. states tmp; /* temporary */
  1740. states empty; /* empty set of states */
  1741. };
  1742. static int matcher(register struct re_guts *g, char *string, size_t nmatch, regmatch_t pmatch[], int eflags);
  1743. static char *dissect(register struct match *m, char *start, char *stop, sopno startst, sopno stopst);
  1744. static char *backref(register struct match *m, char *start, char *stop, sopno startst, sopno stopst, sopno lev);
  1745. static char *fast(register struct match *m, char *start, char *stop, sopno startst, sopno stopst);
  1746. static char *slow(register struct match *m, char *start, char *stop, sopno startst, sopno stopst);
  1747. static states step(register struct re_guts *g, sopno start, sopno stop, register states bef, int ch, register states aft);
  1748. #define BOL (OUT+1)
  1749. #define EOL (BOL+1)
  1750. #define BOLEOL (BOL+2)
  1751. #define NOTHING (BOL+3)
  1752. #define BOW (BOL+4)
  1753. #define EOW (BOL+5)
  1754. #define CODEMAX (BOL+5) /* highest code used */
  1755. #define NONCHAR(c) ((c) > CHAR_MAX)
  1756. #define NNONCHAR (CODEMAX-CHAR_MAX)
  1757. #define SP(t, s, c) /* nothing */
  1758. #define AT(t, p1, p2, s1, s2) /* nothing */
  1759. #define NOTE(s) /* nothing */
  1760. /*
  1761. - matcher - the actual matching engine
  1762. == static int matcher(register struct re_guts *g, char *string, \
  1763. == size_t nmatch, regmatch_t pmatch[], int eflags);
  1764. */
  1765. static int /* 0 success, REG_NOMATCH failure */
  1766. matcher(g, string, nmatch, pmatch, eflags)
  1767. register struct re_guts *g;
  1768. char *string;
  1769. size_t nmatch;
  1770. regmatch_t pmatch[];
  1771. int eflags;
  1772. {
  1773. register char *endp;
  1774. register int i;
  1775. struct match mv;
  1776. register struct match *m = &mv;
  1777. register char *dp;
  1778. const register sopno gf = g->firststate+1; /* +1 for OEND */
  1779. const register sopno gl = g->laststate;
  1780. char *start;
  1781. char *stop;
  1782. /* simplify the situation where possible */
  1783. if (g->cflags&REG_NOSUB)
  1784. nmatch = 0;
  1785. if (eflags&REG_STARTEND) {
  1786. start = string + pmatch[0].rm_so;
  1787. stop = string + pmatch[0].rm_eo;
  1788. } else {
  1789. start = string;
  1790. stop = start + strlen(start);
  1791. }
  1792. if (stop < start)
  1793. return(REG_INVARG);
  1794. /* prescreening; this does wonders for this rather slow code */
  1795. if (g->must != NULL) {
  1796. for (dp = start; dp < stop; dp++)
  1797. if (*dp == g->must[0] && stop - dp >= g->mlen &&
  1798. memcmp(dp, g->must, (size_t)g->mlen) == 0)
  1799. break;
  1800. if (dp == stop) /* we didn't find g->must */
  1801. return(REG_NOMATCH);
  1802. }
  1803. /* match struct setup */
  1804. m->g = g;
  1805. m->eflags = eflags;
  1806. m->pmatch = NULL;
  1807. m->lastpos = NULL;
  1808. m->offp = string;
  1809. m->beginp = start;
  1810. m->endp = stop;
  1811. STATESETUP(m, 4);
  1812. SETUP(m->st);
  1813. SETUP(m->fresh);
  1814. SETUP(m->tmp);
  1815. SETUP(m->empty);
  1816. CLEAR(m->empty);
  1817. /* this loop does only one repetition except for backrefs */
  1818. for (;;) {
  1819. endp = fast(m, start, stop, gf, gl);
  1820. if (endp == NULL) { /* a miss */
  1821. STATETEARDOWN(m);
  1822. return(REG_NOMATCH);
  1823. }
  1824. if (nmatch == 0 && !g->backrefs)
  1825. break; /* no further info needed */
  1826. /* where? */
  1827. assert(m->coldp != NULL);
  1828. for (;;) {
  1829. NOTE("finding start");
  1830. endp = slow(m, m->coldp, stop, gf, gl);
  1831. if (endp != NULL)
  1832. break;
  1833. assert(m->coldp < m->endp);
  1834. m->coldp++;
  1835. }
  1836. if (nmatch == 1 && !g->backrefs)
  1837. break; /* no further info needed */
  1838. /* oh my, he wants the subexpressions... */
  1839. if (m->pmatch == NULL)
  1840. m->pmatch = (regmatch_t *)malloc((m->g->nsub + 1) *
  1841. sizeof(regmatch_t));
  1842. if (m->pmatch == NULL) {
  1843. STATETEARDOWN(m);
  1844. return(REG_ESPACE);
  1845. }
  1846. for (i = 1; i <= m->g->nsub; i++)
  1847. m->pmatch[i].rm_so = m->pmatch[i].rm_eo = -1;
  1848. if (!g->backrefs && !(m->eflags&REG_BACKR)) {
  1849. NOTE("dissecting");
  1850. dp = dissect(m, m->coldp, endp, gf, gl);
  1851. } else {
  1852. if (g->nplus > 0 && m->lastpos == NULL)
  1853. m->lastpos = (char **)malloc((g->nplus+1) *
  1854. sizeof(char *));
  1855. if (g->nplus > 0 && m->lastpos == NULL) {
  1856. free(m->pmatch);
  1857. STATETEARDOWN(m);
  1858. return(REG_ESPACE);
  1859. }
  1860. NOTE("backref dissect");
  1861. dp = backref(m, m->coldp, endp, gf, gl, (sopno)0);
  1862. }
  1863. if (dp != NULL)
  1864. break;
  1865. /* uh-oh... we couldn't find a subexpression-level match */
  1866. assert(g->backrefs); /* must be back references doing it */
  1867. assert(g->nplus == 0 || m->lastpos != NULL);
  1868. for (;;) {
  1869. if (dp != NULL || endp <= m->coldp)
  1870. break; /* defeat */
  1871. NOTE("backoff");
  1872. endp = slow(m, m->coldp, endp-1, gf, gl);
  1873. if (endp == NULL)
  1874. break; /* defeat */
  1875. /* try it on a shorter possibility */
  1876. NOTE("backoff dissect");
  1877. dp = backref(m, m->coldp, endp, gf, gl, (sopno)0);
  1878. }
  1879. assert(dp == NULL || dp == endp);
  1880. if (dp != NULL) /* found a shorter one */
  1881. break;
  1882. /* despite initial appearances, there is no match here */
  1883. NOTE("false alarm");
  1884. start = m->coldp + 1; /* recycle starting later */
  1885. assert(start <= stop);
  1886. }
  1887. /* fill in the details if requested */
  1888. if (nmatch > 0) {
  1889. pmatch[0].rm_so = m->coldp - m->offp;
  1890. pmatch[0].rm_eo = endp - m->offp;
  1891. }
  1892. if (nmatch > 1) {
  1893. assert(m->pmatch != NULL);
  1894. for (i = 1; i < nmatch; i++)
  1895. if (i <= m->g->nsub)
  1896. pmatch[i] = m->pmatch[i];
  1897. else {
  1898. pmatch[i].rm_so = -1;
  1899. pmatch[i].rm_eo = -1;
  1900. }
  1901. }
  1902. if (m->pmatch != NULL)
  1903. free((char *)m->pmatch);
  1904. if (m->lastpos != NULL)
  1905. free((char *)m->lastpos);
  1906. STATETEARDOWN(m);
  1907. return(0);
  1908. }
  1909. /*
  1910. - dissect - figure out what matched what, no back references
  1911. == static char *dissect(register struct match *m, char *start, \
  1912. == char *stop, sopno startst, sopno stopst);
  1913. */
  1914. static char * /* == stop (success) always */
  1915. dissect(m, start, stop, startst, stopst)
  1916. register struct match *m;
  1917. char *start;
  1918. char *stop;
  1919. sopno startst;
  1920. sopno stopst;
  1921. {
  1922. register int i;
  1923. register sopno ss; /* start sop of current subRE */
  1924. register sopno es; /* end sop of current subRE */
  1925. register char *sp; /* start of string matched by it */
  1926. register char *stp; /* string matched by it cannot pass here */
  1927. register char *rest; /* start of rest of string */
  1928. register char *tail; /* string unmatched by rest of RE */
  1929. register sopno ssub; /* start sop of subsubRE */
  1930. register sopno esub; /* end sop of subsubRE */
  1931. register char *ssp; /* start of string matched by subsubRE */
  1932. register char *sep; /* end of string matched by subsubRE */
  1933. register char *oldssp; /* previous ssp */
  1934. register char *dp;
  1935. AT("diss", start, stop, startst, stopst);
  1936. sp = start;
  1937. for (ss = startst; ss < stopst; ss = es) {
  1938. /* identify end of subRE */
  1939. es = ss;
  1940. switch (OP(m->g->strip[es])) {
  1941. case OPLUS_:
  1942. case OQUEST_:
  1943. es += OPND(m->g->strip[es]);
  1944. break;
  1945. case OCH_:
  1946. while (OP(m->g->strip[es]) != O_CH)
  1947. es += OPND(m->g->strip[es]);
  1948. break;
  1949. }
  1950. es++;
  1951. /* figure out what it matched */
  1952. switch (OP(m->g->strip[ss])) {
  1953. case OEND:
  1954. assert(nope);
  1955. break;
  1956. case OCHAR:
  1957. sp++;
  1958. break;
  1959. case OBOL:
  1960. case OEOL:
  1961. case OBOW:
  1962. case OEOW:
  1963. break;
  1964. case OANY:
  1965. case OANYOF:
  1966. sp++;
  1967. break;
  1968. case OBACK_:
  1969. case O_BACK:
  1970. assert(nope);
  1971. break;
  1972. /* cases where length of match is hard to find */
  1973. case OQUEST_:
  1974. stp = stop;
  1975. for (;;) {
  1976. /* how long could this one be? */
  1977. rest = slow(m, sp, stp, ss, es);
  1978. assert(rest != NULL); /* it did match */
  1979. /* could the rest match the rest? */
  1980. tail = slow(m, rest, stop, es, stopst);
  1981. if (tail == stop)
  1982. break; /* yes! */
  1983. /* no -- try a shorter match for this one */
  1984. stp = rest - 1;
  1985. assert(stp >= sp); /* it did work */
  1986. }
  1987. ssub = ss + 1;
  1988. esub = es - 1;
  1989. /* did innards match? */
  1990. if (slow(m, sp, rest, ssub, esub) != NULL) {
  1991. dp = dissect(m, sp, rest, ssub, esub);
  1992. assert(dp == rest);
  1993. } else /* no */
  1994. assert(sp == rest);
  1995. sp = rest;
  1996. break;
  1997. case OPLUS_:
  1998. stp = stop;
  1999. for (;;) {
  2000. /* how long could this one be? */
  2001. rest = slow(m, sp, stp, ss, es);
  2002. assert(rest != NULL); /* it did match */
  2003. /* could the rest match the rest? */
  2004. tail = slow(m, rest, stop, es, stopst);
  2005. if (tail == stop)
  2006. break; /* yes! */
  2007. /* no -- try a shorter match for this one */
  2008. stp = rest - 1;
  2009. assert(stp >= sp); /* it did work */
  2010. }
  2011. ssub = ss + 1;
  2012. esub = es - 1;
  2013. ssp = sp;
  2014. oldssp = ssp;
  2015. for (;;) { /* find last match of innards */
  2016. sep = slow(m, ssp, rest, ssub, esub);
  2017. if (sep == NULL || sep == ssp)
  2018. break; /* failed or matched null */
  2019. oldssp = ssp; /* on to next try */
  2020. ssp = sep;
  2021. }
  2022. if (sep == NULL) {
  2023. /* last successful match */
  2024. sep = ssp;
  2025. ssp = oldssp;
  2026. }
  2027. assert(sep == rest); /* must exhaust substring */
  2028. assert(slow(m, ssp, sep, ssub, esub) == rest);
  2029. dp = dissect(m, ssp, sep, ssub, esub);
  2030. assert(dp == sep);
  2031. sp = rest;
  2032. break;
  2033. case OCH_:
  2034. stp = stop;
  2035. for (;;) {
  2036. /* how long could this one be? */
  2037. rest = slow(m, sp, stp, ss, es);
  2038. assert(rest != NULL); /* it did match */
  2039. /* could the rest match the rest? */
  2040. tail = slow(m, rest, stop, es, stopst);
  2041. if (tail == stop)
  2042. break; /* yes! */
  2043. /* no -- try a shorter match for this one */
  2044. stp = rest - 1;
  2045. assert(stp >= sp); /* it did work */
  2046. }
  2047. ssub = ss + 1;
  2048. esub = ss + OPND(m->g->strip[ss]) - 1;
  2049. assert(OP(m->g->strip[esub]) == OOR1);
  2050. for (;;) { /* find first matching branch */
  2051. if (slow(m, sp, rest, ssub, esub) == rest)
  2052. break; /* it matched all of it */
  2053. /* that one missed, try next one */
  2054. assert(OP(m->g->strip[esub]) == OOR1);
  2055. esub++;
  2056. assert(OP(m->g->strip[esub]) == OOR2);
  2057. ssub = esub + 1;
  2058. esub += OPND(m->g->strip[esub]);
  2059. if (OP(m->g->strip[esub]) == OOR2)
  2060. esub--;
  2061. else
  2062. assert(OP(m->g->strip[esub]) == O_CH);
  2063. }
  2064. dp = dissect(m, sp, rest, ssub, esub);
  2065. assert(dp == rest);
  2066. sp = rest;
  2067. break;
  2068. case O_PLUS:
  2069. case O_QUEST:
  2070. case OOR1:
  2071. case OOR2:
  2072. case O_CH:
  2073. assert(nope);
  2074. break;
  2075. case OLPAREN:
  2076. i = OPND(m->g->strip[ss]);
  2077. assert(0 < i && i <= m->g->nsub);
  2078. m->pmatch[i].rm_so = sp - m->offp;
  2079. break;
  2080. case ORPAREN:
  2081. i = OPND(m->g->strip[ss]);
  2082. assert(0 < i && i <= m->g->nsub);
  2083. m->pmatch[i].rm_eo = sp - m->offp;
  2084. break;
  2085. default: /* uh oh */
  2086. assert(nope);
  2087. break;
  2088. }
  2089. }
  2090. assert(sp == stop);
  2091. return(sp);
  2092. }
  2093. /*
  2094. - backref - figure out what matched what, figuring in back references
  2095. == static char *backref(register struct match *m, char *start, \
  2096. == char *stop, sopno startst, sopno stopst, sopno lev);
  2097. */
  2098. static char * /* == stop (success) or NULL (failure) */
  2099. backref(m, start, stop, startst, stopst, lev)
  2100. register struct match *m;
  2101. char *start;
  2102. char *stop;
  2103. sopno startst;
  2104. sopno stopst;
  2105. sopno lev; /* PLUS nesting level */
  2106. {
  2107. register int i;
  2108. register sopno ss; /* start sop of current subRE */
  2109. register char *sp; /* start of string matched by it */
  2110. register sopno ssub; /* start sop of subsubRE */
  2111. register sopno esub; /* end sop of subsubRE */
  2112. register char *ssp; /* start of string matched by subsubRE */
  2113. register char *dp;
  2114. register size_t len;
  2115. register int hard;
  2116. register sop s;
  2117. register regoff_t offsave;
  2118. register cset *cs;
  2119. AT("back", start, stop, startst, stopst);
  2120. sp = start;
  2121. /* get as far as we can with easy stuff */
  2122. hard = 0;
  2123. for (ss = startst; !hard && ss < stopst; ss++)
  2124. switch (OP(s = m->g->strip[ss])) {
  2125. case OCHAR:
  2126. if (sp == stop || *sp++ != (char)OPND(s))
  2127. return(NULL);
  2128. break;
  2129. case OANY:
  2130. if (sp == stop)
  2131. return(NULL);
  2132. sp++;
  2133. break;
  2134. case OANYOF:
  2135. cs = &m->g->sets[OPND(s)];
  2136. if (sp == stop || !CHIN(cs, *sp++))
  2137. return(NULL);
  2138. break;
  2139. case OBOL:
  2140. if ( (sp == m->beginp && !(m->eflags&REG_NOTBOL)) ||
  2141. (sp < m->endp && *(sp-1) == '\n' &&
  2142. (m->g->cflags&REG_NEWLINE)) )
  2143. { /* yes */ }
  2144. else
  2145. return(NULL);
  2146. break;
  2147. case OEOL:
  2148. if ( (sp == m->endp && !(m->eflags&REG_NOTEOL)) ||
  2149. (sp < m->endp && *sp == '\n' &&
  2150. (m->g->cflags&REG_NEWLINE)) )
  2151. { /* yes */ }
  2152. else
  2153. return(NULL);
  2154. break;
  2155. case OBOW:
  2156. if (( (sp == m->beginp && !(m->eflags&REG_NOTBOL)) ||
  2157. (sp < m->endp && *(sp-1) == '\n' &&
  2158. (m->g->cflags&REG_NEWLINE)) ||
  2159. (sp > m->beginp &&
  2160. !ISWORD(*(sp-1))) ) &&
  2161. (sp < m->endp && ISWORD(*sp)) )
  2162. { /* yes */ }
  2163. else
  2164. return(NULL);
  2165. break;
  2166. case OEOW:
  2167. if (( (sp == m->endp && !(m->eflags&REG_NOTEOL)) ||
  2168. (sp < m->endp && *sp == '\n' &&
  2169. (m->g->cflags&REG_NEWLINE)) ||
  2170. (sp < m->endp && !ISWORD(*sp)) ) &&
  2171. (sp > m->beginp && ISWORD(*(sp-1))) )
  2172. { /* yes */ }
  2173. else
  2174. return(NULL);
  2175. break;
  2176. case O_QUEST:
  2177. break;
  2178. case OOR1: /* matches null but needs to skip */
  2179. ss++;
  2180. s = m->g->strip[ss];
  2181. do {
  2182. assert(OP(s) == OOR2);
  2183. ss += OPND(s);
  2184. } while (OP(s = m->g->strip[ss]) != O_CH);
  2185. /* note that the ss++ gets us past the O_CH */
  2186. break;
  2187. default: /* have to make a choice */
  2188. hard = 1;
  2189. break;
  2190. }
  2191. if (!hard) { /* that was it! */
  2192. if (sp != stop)
  2193. return(NULL);
  2194. return(sp);
  2195. }
  2196. ss--; /* adjust for the for's final increment */
  2197. /* the hard stuff */
  2198. AT("hard", sp, stop, ss, stopst);
  2199. s = m->g->strip[ss];
  2200. switch (OP(s)) {
  2201. case OBACK_: /* the vilest depths */
  2202. i = OPND(s);
  2203. assert(0 < i && i <= m->g->nsub);
  2204. if (m->pmatch[i].rm_eo == -1)
  2205. return(NULL);
  2206. assert(m->pmatch[i].rm_so != -1);
  2207. len = m->pmatch[i].rm_eo - m->pmatch[i].rm_so;
  2208. assert(stop - m->beginp >= len);
  2209. if (sp > stop - len)
  2210. return(NULL); /* not enough left to match */
  2211. ssp = m->offp + m->pmatch[i].rm_so;
  2212. if (memcmp(sp, ssp, len) != 0)
  2213. return(NULL);
  2214. while (m->g->strip[ss] != SOP(O_BACK, i))
  2215. ss++;
  2216. return(backref(m, sp+len, stop, ss+1, stopst, lev));
  2217. break;
  2218. case OQUEST_: /* to null or not */
  2219. dp = backref(m, sp, stop, ss+1, stopst, lev);
  2220. if (dp != NULL)
  2221. return(dp); /* not */
  2222. return(backref(m, sp, stop, ss+OPND(s)+1, stopst, lev));
  2223. break;
  2224. case OPLUS_:
  2225. assert(m->lastpos != NULL);
  2226. assert(lev+1 <= m->g->nplus);
  2227. m->lastpos[lev+1] = sp;
  2228. return(backref(m, sp, stop, ss+1, stopst, lev+1));
  2229. break;
  2230. case O_PLUS:
  2231. if (sp == m->lastpos[lev]) /* last pass matched null */
  2232. return(backref(m, sp, stop, ss+1, stopst, lev-1));
  2233. /* try another pass */
  2234. m->lastpos[lev] = sp;
  2235. dp = backref(m, sp, stop, ss-OPND(s)+1, stopst, lev);
  2236. if (dp == NULL)
  2237. return(backref(m, sp, stop, ss+1, stopst, lev-1));
  2238. else
  2239. return(dp);
  2240. break;
  2241. case OCH_: /* find the right one, if any */
  2242. ssub = ss + 1;
  2243. esub = ss + OPND(s) - 1;
  2244. assert(OP(m->g->strip[esub]) == OOR1);
  2245. for (;;) { /* find first matching branch */
  2246. dp = backref(m, sp, stop, ssub, esub, lev);
  2247. if (dp != NULL)
  2248. return(dp);
  2249. /* that one missed, try next one */
  2250. if (OP(m->g->strip[esub]) == O_CH)
  2251. return(NULL); /* there is none */
  2252. esub++;
  2253. assert(OP(m->g->strip[esub]) == OOR2);
  2254. ssub = esub + 1;
  2255. esub += OPND(m->g->strip[esub]);
  2256. if (OP(m->g->strip[esub]) == OOR2)
  2257. esub--;
  2258. else
  2259. assert(OP(m->g->strip[esub]) == O_CH);
  2260. }
  2261. break;
  2262. case OLPAREN: /* must undo assignment if rest fails */
  2263. i = OPND(s);
  2264. assert(0 < i && i <= m->g->nsub);
  2265. offsave = m->pmatch[i].rm_so;
  2266. m->pmatch[i].rm_so = sp - m->offp;
  2267. dp = backref(m, sp, stop, ss+1, stopst, lev);
  2268. if (dp != NULL)
  2269. return(dp);
  2270. m->pmatch[i].rm_so = offsave;
  2271. return(NULL);
  2272. break;
  2273. case ORPAREN: /* must undo assignment if rest fails */
  2274. i = OPND(s);
  2275. assert(0 < i && i <= m->g->nsub);
  2276. offsave = m->pmatch[i].rm_eo;
  2277. m->pmatch[i].rm_eo = sp - m->offp;
  2278. dp = backref(m, sp, stop, ss+1, stopst, lev);
  2279. if (dp != NULL)
  2280. return(dp);
  2281. m->pmatch[i].rm_eo = offsave;
  2282. return(NULL);
  2283. break;
  2284. default: /* uh oh */
  2285. assert(nope);
  2286. break;
  2287. }
  2288. /* "can't happen" */
  2289. assert(nope);
  2290. /* NOTREACHED */
  2291. return((char *)NULL); /* dummy */
  2292. }
  2293. /*
  2294. - fast - step through the string at top speed
  2295. == static char *fast(register struct match *m, char *start, \
  2296. == char *stop, sopno startst, sopno stopst);
  2297. */
  2298. static char * /* where tentative match ended, or NULL */
  2299. fast(m, start, stop, startst, stopst)
  2300. register struct match *m;
  2301. char *start;
  2302. char *stop;
  2303. sopno startst;
  2304. sopno stopst;
  2305. {
  2306. register states st = m->st;
  2307. register states fresh = m->fresh;
  2308. register states tmp = m->tmp;
  2309. register char *p = start;
  2310. register int c = (start == m->beginp) ? OUT : *(start-1);
  2311. register int lastc; /* previous c */
  2312. register int flagch;
  2313. register int i;
  2314. register char *coldp; /* last p after which no match was underway */
  2315. CLEAR(st);
  2316. SET1(st, startst);
  2317. st = step(m->g, startst, stopst, st, NOTHING, st);
  2318. ASSIGN(fresh, st);
  2319. SP("start", st, *p);
  2320. coldp = NULL;
  2321. for (;;) {
  2322. /* next character */
  2323. lastc = c;
  2324. c = (p == m->endp) ? OUT : *p;
  2325. if (EQ(st, fresh))
  2326. coldp = p;
  2327. /* is there an EOL and/or BOL between lastc and c? */
  2328. flagch = '\0';
  2329. i = 0;
  2330. if ( (lastc == '\n' && m->g->cflags&REG_NEWLINE) ||
  2331. (lastc == OUT && !(m->eflags&REG_NOTBOL)) ) {
  2332. flagch = BOL;
  2333. i = m->g->nbol;
  2334. }
  2335. if ( (c == '\n' && m->g->cflags&REG_NEWLINE) ||
  2336. (c == OUT && !(m->eflags&REG_NOTEOL)) ) {
  2337. flagch = (flagch == BOL) ? BOLEOL : EOL;
  2338. i += m->g->neol;
  2339. }
  2340. if (i != 0) {
  2341. for (; i > 0; i--)
  2342. st = step(m->g, startst, stopst, st, flagch, st);
  2343. SP("boleol", st, c);
  2344. }
  2345. /* how about a word boundary? */
  2346. if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
  2347. (c != OUT && ISWORD(c)) ) {
  2348. flagch = BOW;
  2349. }
  2350. if ( (lastc != OUT && ISWORD(lastc)) &&
  2351. (flagch == EOL || (c != OUT && !ISWORD(c))) ) {
  2352. flagch = EOW;
  2353. }
  2354. if (flagch == BOW || flagch == EOW) {
  2355. st = step(m->g, startst, stopst, st, flagch, st);
  2356. SP("boweow", st, c);
  2357. }
  2358. /* are we done? */
  2359. if (ISSET(st, stopst) || p == stop)
  2360. break; /* NOTE BREAK OUT */
  2361. /* no, we must deal with this character */
  2362. ASSIGN(tmp, st);
  2363. ASSIGN(st, fresh);
  2364. assert(c != OUT);
  2365. st = step(m->g, startst, stopst, tmp, c, st);
  2366. SP("aft", st, c);
  2367. assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
  2368. p++;
  2369. }
  2370. assert(coldp != NULL);
  2371. m->coldp = coldp;
  2372. if (ISSET(st, stopst))
  2373. return(p+1);
  2374. else
  2375. return(NULL);
  2376. }
  2377. /*
  2378. - slow - step through the string more deliberately
  2379. == static char *slow(register struct match *m, char *start, \
  2380. == char *stop, sopno startst, sopno stopst);
  2381. */
  2382. static char * /* where it ended */
  2383. slow(m, start, stop, startst, stopst)
  2384. register struct match *m;
  2385. char *start;
  2386. char *stop;
  2387. sopno startst;
  2388. sopno stopst;
  2389. {
  2390. register states st = m->st;
  2391. register states empty = m->empty;
  2392. register states tmp = m->tmp;
  2393. register char *p = start;
  2394. register int c = (start == m->beginp) ? OUT : *(start-1);
  2395. register int lastc; /* previous c */
  2396. register int flagch;
  2397. register int i;
  2398. register char *matchp; /* last p at which a match ended */
  2399. AT("slow", start, stop, startst, stopst);
  2400. CLEAR(st);
  2401. SET1(st, startst);
  2402. SP("sstart", st, *p);
  2403. st = step(m->g, startst, stopst, st, NOTHING, st);
  2404. matchp = NULL;
  2405. for (;;) {
  2406. /* next character */
  2407. lastc = c;
  2408. c = (p == m->endp) ? OUT : *p;
  2409. /* is there an EOL and/or BOL between lastc and c? */
  2410. flagch = '\0';
  2411. i = 0;
  2412. if ( (lastc == '\n' && m->g->cflags&REG_NEWLINE) ||
  2413. (lastc == OUT && !(m->eflags&REG_NOTBOL)) ) {
  2414. flagch = BOL;
  2415. i = m->g->nbol;
  2416. }
  2417. if ( (c == '\n' && m->g->cflags&REG_NEWLINE) ||
  2418. (c == OUT && !(m->eflags&REG_NOTEOL)) ) {
  2419. flagch = (flagch == BOL) ? BOLEOL : EOL;
  2420. i += m->g->neol;
  2421. }
  2422. if (i != 0) {
  2423. for (; i > 0; i--)
  2424. st = step(m->g, startst, stopst, st, flagch, st);
  2425. SP("sboleol", st, c);
  2426. }
  2427. /* how about a word boundary? */
  2428. if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
  2429. (c != OUT && ISWORD(c)) ) {
  2430. flagch = BOW;
  2431. }
  2432. if ( (lastc != OUT && ISWORD(lastc)) &&
  2433. (flagch == EOL || (c != OUT && !ISWORD(c))) ) {
  2434. flagch = EOW;
  2435. }
  2436. if (flagch == BOW || flagch == EOW) {
  2437. st = step(m->g, startst, stopst, st, flagch, st);
  2438. SP("sboweow", st, c);
  2439. }
  2440. /* are we done? */
  2441. if (ISSET(st, stopst))
  2442. matchp = p;
  2443. if (EQ(st, empty) || p == stop)
  2444. break; /* NOTE BREAK OUT */
  2445. /* no, we must deal with this character */
  2446. ASSIGN(tmp, st);
  2447. ASSIGN(st, empty);
  2448. assert(c != OUT);
  2449. st = step(m->g, startst, stopst, tmp, c, st);
  2450. SP("saft", st, c);
  2451. assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
  2452. p++;
  2453. }
  2454. return(matchp);
  2455. }
  2456. /*
  2457. - step - map set of states reachable before char to set reachable after
  2458. == static states step(register struct re_guts *g, sopno start, sopno stop, \
  2459. == register states bef, int ch, register states aft);
  2460. == #define BOL (OUT+1)
  2461. == #define EOL (BOL+1)
  2462. == #define BOLEOL (BOL+2)
  2463. == #define NOTHING (BOL+3)
  2464. == #define BOW (BOL+4)
  2465. == #define EOW (BOL+5)
  2466. == #define CODEMAX (BOL+5) // highest code used
  2467. == #define NONCHAR(c) ((c) > CHAR_MAX)
  2468. == #define NNONCHAR (CODEMAX-CHAR_MAX)
  2469. */
  2470. static states
  2471. step(g, start, stop, bef, ch, aft)
  2472. register struct re_guts *g;
  2473. sopno start; /* start state within strip */
  2474. sopno stop; /* state after stop state within strip */
  2475. register states bef; /* states reachable before */
  2476. int ch; /* character or NONCHAR code */
  2477. register states aft; /* states already known reachable after */
  2478. {
  2479. register cset *cs;
  2480. register sop s;
  2481. register sopno pc;
  2482. register onestate here; /* note, macros know this name */
  2483. register sopno look;
  2484. register long i;
  2485. for (pc = start, INIT(here, pc); pc != stop; pc++, INC(here)) {
  2486. s = g->strip[pc];
  2487. switch (OP(s)) {
  2488. case OEND:
  2489. assert(pc == stop-1);
  2490. break;
  2491. case OCHAR:
  2492. /* only characters can match */
  2493. assert(!NONCHAR(ch) || ch != (char)OPND(s));
  2494. if (ch == (char)OPND(s))
  2495. FWD(aft, bef, 1);
  2496. break;
  2497. case OBOL:
  2498. if (ch == BOL || ch == BOLEOL)
  2499. FWD(aft, bef, 1);
  2500. break;
  2501. case OEOL:
  2502. if (ch == EOL || ch == BOLEOL)
  2503. FWD(aft, bef, 1);
  2504. break;
  2505. case OBOW:
  2506. if (ch == BOW)
  2507. FWD(aft, bef, 1);
  2508. break;
  2509. case OEOW:
  2510. if (ch == EOW)
  2511. FWD(aft, bef, 1);
  2512. break;
  2513. case OANY:
  2514. if (!NONCHAR(ch))
  2515. FWD(aft, bef, 1);
  2516. break;
  2517. case OANYOF:
  2518. cs = &g->sets[OPND(s)];
  2519. if (!NONCHAR(ch) && CHIN(cs, ch))
  2520. FWD(aft, bef, 1);
  2521. break;
  2522. case OBACK_: /* ignored here */
  2523. case O_BACK:
  2524. FWD(aft, aft, 1);
  2525. break;
  2526. case OPLUS_: /* forward, this is just an empty */
  2527. FWD(aft, aft, 1);
  2528. break;
  2529. case O_PLUS: /* both forward and back */
  2530. FWD(aft, aft, 1);
  2531. i = ISSETBACK(aft, OPND(s));
  2532. BACK(aft, aft, OPND(s));
  2533. if (!i && ISSETBACK(aft, OPND(s))) {
  2534. /* oho, must reconsider loop body */
  2535. pc -= OPND(s) + 1;
  2536. INIT(here, pc);
  2537. }
  2538. break;
  2539. case OQUEST_: /* two branches, both forward */
  2540. FWD(aft, aft, 1);
  2541. FWD(aft, aft, OPND(s));
  2542. break;
  2543. case O_QUEST: /* just an empty */
  2544. FWD(aft, aft, 1);
  2545. break;
  2546. case OLPAREN: /* not significant here */
  2547. case ORPAREN:
  2548. FWD(aft, aft, 1);
  2549. break;
  2550. case OCH_: /* mark the first two branches */
  2551. FWD(aft, aft, 1);
  2552. assert(OP(g->strip[pc+OPND(s)]) == OOR2);
  2553. FWD(aft, aft, OPND(s));
  2554. break;
  2555. case OOR1: /* done a branch, find the O_CH */
  2556. if (ISSTATEIN(aft, here)) {
  2557. for (look = 1;
  2558. OP(s = g->strip[pc+look]) != O_CH;
  2559. look += OPND(s))
  2560. assert(OP(s) == OOR2);
  2561. FWD(aft, aft, look);
  2562. }
  2563. break;
  2564. case OOR2: /* propagate OCH_'s marking */
  2565. FWD(aft, aft, 1);
  2566. if (OP(g->strip[pc+OPND(s)]) != O_CH) {
  2567. assert(OP(g->strip[pc+OPND(s)]) == OOR2);
  2568. FWD(aft, aft, OPND(s));
  2569. }
  2570. break;
  2571. case O_CH: /* just empty */
  2572. FWD(aft, aft, 1);
  2573. break;
  2574. default: /* ooooops... */
  2575. assert(nope);
  2576. break;
  2577. }
  2578. }
  2579. return(aft);
  2580. }
  2581. #undef matcher
  2582. #undef fast
  2583. #undef slow
  2584. #undef dissect
  2585. #undef backref
  2586. #undef step
  2587. #undef print
  2588. #undef at
  2589. #undef match
  2590. /* now undo things */
  2591. #undef states
  2592. #undef CLEAR
  2593. #undef SET0
  2594. #undef SET1
  2595. #undef ISSET
  2596. #undef ASSIGN
  2597. #undef EQ
  2598. #undef STATEVARS
  2599. #undef STATESETUP
  2600. #undef STATETEARDOWN
  2601. #undef SETUP
  2602. #undef onestate
  2603. #undef INIT
  2604. #undef INC
  2605. #undef ISSTATEIN
  2606. #undef FWD
  2607. #undef BACK
  2608. #undef ISSETBACK
  2609. #undef SNAMES
  2610. /* macros for manipulating states, large version */
  2611. #define states char *
  2612. #define CLEAR(v) memset(v, 0, m->g->nstates)
  2613. #define SET0(v, n) ((v)[n] = 0)
  2614. #define SET1(v, n) ((v)[n] = 1)
  2615. #define ISSET(v, n) ((v)[n])
  2616. #define ASSIGN(d, s) memcpy(d, s, m->g->nstates)
  2617. #define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0)
  2618. #define STATEVARS int vn; char *space
  2619. #define STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \
  2620. if ((m)->space == NULL) return(REG_ESPACE); \
  2621. (m)->vn = 0; }
  2622. #define STATETEARDOWN(m) { free((m)->space); }
  2623. #define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates])
  2624. #define onestate int
  2625. #define INIT(o, n) ((o) = (n))
  2626. #define INC(o) ((o)++)
  2627. #define ISSTATEIN(v, o) ((v)[o])
  2628. /* some abbreviations; note that some of these know variable names! */
  2629. /* do "if I'm here, I can also be there" etc without branches */
  2630. #define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here])
  2631. #define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here])
  2632. #define ISSETBACK(v, n) ((v)[here - (n)])
  2633. /* function names */
  2634. #define LNAMES /* flag */
  2635. /*
  2636. * The matching engine and friends. This file is #included by regexec.c
  2637. * after suitable #defines of a variety of macros used herein, so that
  2638. * different state representations can be used without duplicating masses
  2639. * of code.
  2640. */
  2641. #ifdef SNAMES
  2642. #define matcher smatcher
  2643. #define fast sfast
  2644. #define slow sslow
  2645. #define dissect sdissect
  2646. #define backref sbackref
  2647. #define step sstep
  2648. #define print sprint
  2649. #define at sat
  2650. #define match smat
  2651. #endif
  2652. #ifdef LNAMES
  2653. #define matcher lmatcher
  2654. #define fast lfast
  2655. #define slow lslow
  2656. #define dissect ldissect
  2657. #define backref lbackref
  2658. #define step lstep
  2659. #define print lprint
  2660. #define at lat
  2661. #define match lmat
  2662. #endif
  2663. /* another structure passed up and down to avoid zillions of parameters */
  2664. struct match {
  2665. struct re_guts *g;
  2666. int eflags;
  2667. regmatch_t *pmatch; /* [nsub+1] (0 element unused) */
  2668. char *offp; /* offsets work from here */
  2669. char *beginp; /* start of string -- virtual NUL precedes */
  2670. char *endp; /* end of string -- virtual NUL here */
  2671. char *coldp; /* can be no match starting before here */
  2672. char **lastpos; /* [nplus+1] */
  2673. STATEVARS;
  2674. states st; /* current states */
  2675. states fresh; /* states for a fresh start */
  2676. states tmp; /* temporary */
  2677. states empty; /* empty set of states */
  2678. };
  2679. static int matcher(register struct re_guts *g, char *string, size_t nmatch, regmatch_t pmatch[], int eflags);
  2680. static char *dissect(register struct match *m, char *start, char *stop, sopno startst, sopno stopst);
  2681. static char *backref(register struct match *m, char *start, char *stop, sopno startst, sopno stopst, sopno lev);
  2682. static char *fast(register struct match *m, char *start, char *stop, sopno startst, sopno stopst);
  2683. static char *slow(register struct match *m, char *start, char *stop, sopno startst, sopno stopst);
  2684. static states step(register struct re_guts *g, sopno start, sopno stop, register states bef, int ch, register states aft);
  2685. #define BOL (OUT+1)
  2686. #define EOL (BOL+1)
  2687. #define BOLEOL (BOL+2)
  2688. #define NOTHING (BOL+3)
  2689. #define BOW (BOL+4)
  2690. #define EOW (BOL+5)
  2691. #define CODEMAX (BOL+5) /* highest code used */
  2692. #define NONCHAR(c) ((c) > CHAR_MAX)
  2693. #define NNONCHAR (CODEMAX-CHAR_MAX)
  2694. #define SP(t, s, c) /* nothing */
  2695. #define AT(t, p1, p2, s1, s2) /* nothing */
  2696. #define NOTE(s) /* nothing */
  2697. /*
  2698. - matcher - the actual matching engine
  2699. == static int matcher(register struct re_guts *g, char *string, \
  2700. == size_t nmatch, regmatch_t pmatch[], int eflags);
  2701. */
  2702. static int /* 0 success, REG_NOMATCH failure */
  2703. matcher(g, string, nmatch, pmatch, eflags)
  2704. register struct re_guts *g;
  2705. char *string;
  2706. size_t nmatch;
  2707. regmatch_t pmatch[];
  2708. int eflags;
  2709. {
  2710. register char *endp;
  2711. register int i;
  2712. struct match mv;
  2713. register struct match *m = &mv;
  2714. register char *dp;
  2715. const register sopno gf = g->firststate+1; /* +1 for OEND */
  2716. const register sopno gl = g->laststate;
  2717. char *start;
  2718. char *stop;
  2719. /* simplify the situation where possible */
  2720. if (g->cflags&REG_NOSUB)
  2721. nmatch = 0;
  2722. if (eflags&REG_STARTEND) {
  2723. start = string + pmatch[0].rm_so;
  2724. stop = string + pmatch[0].rm_eo;
  2725. } else {
  2726. start = string;
  2727. stop = start + strlen(start);
  2728. }
  2729. if (stop < start)
  2730. return(REG_INVARG);
  2731. /* prescreening; this does wonders for this rather slow code */
  2732. if (g->must != NULL) {
  2733. for (dp = start; dp < stop; dp++)
  2734. if (*dp == g->must[0] && stop - dp >= g->mlen &&
  2735. memcmp(dp, g->must, (size_t)g->mlen) == 0)
  2736. break;
  2737. if (dp == stop) /* we didn't find g->must */
  2738. return(REG_NOMATCH);
  2739. }
  2740. /* match struct setup */
  2741. m->g = g;
  2742. m->eflags = eflags;
  2743. m->pmatch = NULL;
  2744. m->lastpos = NULL;
  2745. m->offp = string;
  2746. m->beginp = start;
  2747. m->endp = stop;
  2748. STATESETUP(m, 4);
  2749. SETUP(m->st);
  2750. SETUP(m->fresh);
  2751. SETUP(m->tmp);
  2752. SETUP(m->empty);
  2753. CLEAR(m->empty);
  2754. /* this loop does only one repetition except for backrefs */
  2755. for (;;) {
  2756. endp = fast(m, start, stop, gf, gl);
  2757. if (endp == NULL) { /* a miss */
  2758. STATETEARDOWN(m);
  2759. return(REG_NOMATCH);
  2760. }
  2761. if (nmatch == 0 && !g->backrefs)
  2762. break; /* no further info needed */
  2763. /* where? */
  2764. assert(m->coldp != NULL);
  2765. for (;;) {
  2766. NOTE("finding start");
  2767. endp = slow(m, m->coldp, stop, gf, gl);
  2768. if (endp != NULL)
  2769. break;
  2770. assert(m->coldp < m->endp);
  2771. m->coldp++;
  2772. }
  2773. if (nmatch == 1 && !g->backrefs)
  2774. break; /* no further info needed */
  2775. /* oh my, he wants the subexpressions... */
  2776. if (m->pmatch == NULL)
  2777. m->pmatch = (regmatch_t *)malloc((m->g->nsub + 1) *
  2778. sizeof(regmatch_t));
  2779. if (m->pmatch == NULL) {
  2780. STATETEARDOWN(m);
  2781. return(REG_ESPACE);
  2782. }
  2783. for (i = 1; i <= m->g->nsub; i++)
  2784. m->pmatch[i].rm_so = m->pmatch[i].rm_eo = -1;
  2785. if (!g->backrefs && !(m->eflags&REG_BACKR)) {
  2786. NOTE("dissecting");
  2787. dp = dissect(m, m->coldp, endp, gf, gl);
  2788. } else {
  2789. if (g->nplus > 0 && m->lastpos == NULL)
  2790. m->lastpos = (char **)malloc((g->nplus+1) *
  2791. sizeof(char *));
  2792. if (g->nplus > 0 && m->lastpos == NULL) {
  2793. free(m->pmatch);
  2794. STATETEARDOWN(m);
  2795. return(REG_ESPACE);
  2796. }
  2797. NOTE("backref dissect");
  2798. dp = backref(m, m->coldp, endp, gf, gl, (sopno)0);
  2799. }
  2800. if (dp != NULL)
  2801. break;
  2802. /* uh-oh... we couldn't find a subexpression-level match */
  2803. assert(g->backrefs); /* must be back references doing it */
  2804. assert(g->nplus == 0 || m->lastpos != NULL);
  2805. for (;;) {
  2806. if (dp != NULL || endp <= m->coldp)
  2807. break; /* defeat */
  2808. NOTE("backoff");
  2809. endp = slow(m, m->coldp, endp-1, gf, gl);
  2810. if (endp == NULL)
  2811. break; /* defeat */
  2812. /* try it on a shorter possibility */
  2813. NOTE("backoff dissect");
  2814. dp = backref(m, m->coldp, endp, gf, gl, (sopno)0);
  2815. }
  2816. assert(dp == NULL || dp == endp);
  2817. if (dp != NULL) /* found a shorter one */
  2818. break;
  2819. /* despite initial appearances, there is no match here */
  2820. NOTE("false alarm");
  2821. start = m->coldp + 1; /* recycle starting later */
  2822. assert(start <= stop);
  2823. }
  2824. /* fill in the details if requested */
  2825. if (nmatch > 0) {
  2826. pmatch[0].rm_so = m->coldp - m->offp;
  2827. pmatch[0].rm_eo = endp - m->offp;
  2828. }
  2829. if (nmatch > 1) {
  2830. assert(m->pmatch != NULL);
  2831. for (i = 1; i < nmatch; i++)
  2832. if (i <= m->g->nsub)
  2833. pmatch[i] = m->pmatch[i];
  2834. else {
  2835. pmatch[i].rm_so = -1;
  2836. pmatch[i].rm_eo = -1;
  2837. }
  2838. }
  2839. if (m->pmatch != NULL)
  2840. free((char *)m->pmatch);
  2841. if (m->lastpos != NULL)
  2842. free((char *)m->lastpos);
  2843. STATETEARDOWN(m);
  2844. return(0);
  2845. }
  2846. /*
  2847. - dissect - figure out what matched what, no back references
  2848. == static char *dissect(register struct match *m, char *start, \
  2849. == char *stop, sopno startst, sopno stopst);
  2850. */
  2851. static char * /* == stop (success) always */
  2852. dissect(m, start, stop, startst, stopst)
  2853. register struct match *m;
  2854. char *start;
  2855. char *stop;
  2856. sopno startst;
  2857. sopno stopst;
  2858. {
  2859. register int i;
  2860. register sopno ss; /* start sop of current subRE */
  2861. register sopno es; /* end sop of current subRE */
  2862. register char *sp; /* start of string matched by it */
  2863. register char *stp; /* string matched by it cannot pass here */
  2864. register char *rest; /* start of rest of string */
  2865. register char *tail; /* string unmatched by rest of RE */
  2866. register sopno ssub; /* start sop of subsubRE */
  2867. register sopno esub; /* end sop of subsubRE */
  2868. register char *ssp; /* start of string matched by subsubRE */
  2869. register char *sep; /* end of string matched by subsubRE */
  2870. register char *oldssp; /* previous ssp */
  2871. register char *dp;
  2872. AT("diss", start, stop, startst, stopst);
  2873. sp = start;
  2874. for (ss = startst; ss < stopst; ss = es) {
  2875. /* identify end of subRE */
  2876. es = ss;
  2877. switch (OP(m->g->strip[es])) {
  2878. case OPLUS_:
  2879. case OQUEST_:
  2880. es += OPND(m->g->strip[es]);
  2881. break;
  2882. case OCH_:
  2883. while (OP(m->g->strip[es]) != O_CH)
  2884. es += OPND(m->g->strip[es]);
  2885. break;
  2886. }
  2887. es++;
  2888. /* figure out what it matched */
  2889. switch (OP(m->g->strip[ss])) {
  2890. case OEND:
  2891. assert(nope);
  2892. break;
  2893. case OCHAR:
  2894. sp++;
  2895. break;
  2896. case OBOL:
  2897. case OEOL:
  2898. case OBOW:
  2899. case OEOW:
  2900. break;
  2901. case OANY:
  2902. case OANYOF:
  2903. sp++;
  2904. break;
  2905. case OBACK_:
  2906. case O_BACK:
  2907. assert(nope);
  2908. break;
  2909. /* cases where length of match is hard to find */
  2910. case OQUEST_:
  2911. stp = stop;
  2912. for (;;) {
  2913. /* how long could this one be? */
  2914. rest = slow(m, sp, stp, ss, es);
  2915. assert(rest != NULL); /* it did match */
  2916. /* could the rest match the rest? */
  2917. tail = slow(m, rest, stop, es, stopst);
  2918. if (tail == stop)
  2919. break; /* yes! */
  2920. /* no -- try a shorter match for this one */
  2921. stp = rest - 1;
  2922. assert(stp >= sp); /* it did work */
  2923. }
  2924. ssub = ss + 1;
  2925. esub = es - 1;
  2926. /* did innards match? */
  2927. if (slow(m, sp, rest, ssub, esub) != NULL) {
  2928. dp = dissect(m, sp, rest, ssub, esub);
  2929. assert(dp == rest);
  2930. } else /* no */
  2931. assert(sp == rest);
  2932. sp = rest;
  2933. break;
  2934. case OPLUS_:
  2935. stp = stop;
  2936. for (;;) {
  2937. /* how long could this one be? */
  2938. rest = slow(m, sp, stp, ss, es);
  2939. assert(rest != NULL); /* it did match */
  2940. /* could the rest match the rest? */
  2941. tail = slow(m, rest, stop, es, stopst);
  2942. if (tail == stop)
  2943. break; /* yes! */
  2944. /* no -- try a shorter match for this one */
  2945. stp = rest - 1;
  2946. assert(stp >= sp); /* it did work */
  2947. }
  2948. ssub = ss + 1;
  2949. esub = es - 1;
  2950. ssp = sp;
  2951. oldssp = ssp;
  2952. for (;;) { /* find last match of innards */
  2953. sep = slow(m, ssp, rest, ssub, esub);
  2954. if (sep == NULL || sep == ssp)
  2955. break; /* failed or matched null */
  2956. oldssp = ssp; /* on to next try */
  2957. ssp = sep;
  2958. }
  2959. if (sep == NULL) {
  2960. /* last successful match */
  2961. sep = ssp;
  2962. ssp = oldssp;
  2963. }
  2964. assert(sep == rest); /* must exhaust substring */
  2965. assert(slow(m, ssp, sep, ssub, esub) == rest);
  2966. dp = dissect(m, ssp, sep, ssub, esub);
  2967. assert(dp == sep);
  2968. sp = rest;
  2969. break;
  2970. case OCH_:
  2971. stp = stop;
  2972. for (;;) {
  2973. /* how long could this one be? */
  2974. rest = slow(m, sp, stp, ss, es);
  2975. assert(rest != NULL); /* it did match */
  2976. /* could the rest match the rest? */
  2977. tail = slow(m, rest, stop, es, stopst);
  2978. if (tail == stop)
  2979. break; /* yes! */
  2980. /* no -- try a shorter match for this one */
  2981. stp = rest - 1;
  2982. assert(stp >= sp); /* it did work */
  2983. }
  2984. ssub = ss + 1;
  2985. esub = ss + OPND(m->g->strip[ss]) - 1;
  2986. assert(OP(m->g->strip[esub]) == OOR1);
  2987. for (;;) { /* find first matching branch */
  2988. if (slow(m, sp, rest, ssub, esub) == rest)
  2989. break; /* it matched all of it */
  2990. /* that one missed, try next one */
  2991. assert(OP(m->g->strip[esub]) == OOR1);
  2992. esub++;
  2993. assert(OP(m->g->strip[esub]) == OOR2);
  2994. ssub = esub + 1;
  2995. esub += OPND(m->g->strip[esub]);
  2996. if (OP(m->g->strip[esub]) == OOR2)
  2997. esub--;
  2998. else
  2999. assert(OP(m->g->strip[esub]) == O_CH);
  3000. }
  3001. dp = dissect(m, sp, rest, ssub, esub);
  3002. assert(dp == rest);
  3003. sp = rest;
  3004. break;
  3005. case O_PLUS:
  3006. case O_QUEST:
  3007. case OOR1:
  3008. case OOR2:
  3009. case O_CH:
  3010. assert(nope);
  3011. break;
  3012. case OLPAREN:
  3013. i = OPND(m->g->strip[ss]);
  3014. assert(0 < i && i <= m->g->nsub);
  3015. m->pmatch[i].rm_so = sp - m->offp;
  3016. break;
  3017. case ORPAREN:
  3018. i = OPND(m->g->strip[ss]);
  3019. assert(0 < i && i <= m->g->nsub);
  3020. m->pmatch[i].rm_eo = sp - m->offp;
  3021. break;
  3022. default: /* uh oh */
  3023. assert(nope);
  3024. break;
  3025. }
  3026. }
  3027. assert(sp == stop);
  3028. return(sp);
  3029. }
  3030. /*
  3031. - backref - figure out what matched what, figuring in back references
  3032. == static char *backref(register struct match *m, char *start, \
  3033. == char *stop, sopno startst, sopno stopst, sopno lev);
  3034. */
  3035. static char * /* == stop (success) or NULL (failure) */
  3036. backref(m, start, stop, startst, stopst, lev)
  3037. register struct match *m;
  3038. char *start;
  3039. char *stop;
  3040. sopno startst;
  3041. sopno stopst;
  3042. sopno lev; /* PLUS nesting level */
  3043. {
  3044. register int i;
  3045. register sopno ss; /* start sop of current subRE */
  3046. register char *sp; /* start of string matched by it */
  3047. register sopno ssub; /* start sop of subsubRE */
  3048. register sopno esub; /* end sop of subsubRE */
  3049. register char *ssp; /* start of string matched by subsubRE */
  3050. register char *dp;
  3051. register size_t len;
  3052. register int hard;
  3053. register sop s;
  3054. register regoff_t offsave;
  3055. register cset *cs;
  3056. AT("back", start, stop, startst, stopst);
  3057. sp = start;
  3058. /* get as far as we can with easy stuff */
  3059. hard = 0;
  3060. for (ss = startst; !hard && ss < stopst; ss++)
  3061. switch (OP(s = m->g->strip[ss])) {
  3062. case OCHAR:
  3063. if (sp == stop || *sp++ != (char)OPND(s))
  3064. return(NULL);
  3065. break;
  3066. case OANY:
  3067. if (sp == stop)
  3068. return(NULL);
  3069. sp++;
  3070. break;
  3071. case OANYOF:
  3072. cs = &m->g->sets[OPND(s)];
  3073. if (sp == stop || !CHIN(cs, *sp++))
  3074. return(NULL);
  3075. break;
  3076. case OBOL:
  3077. if ( (sp == m->beginp && !(m->eflags&REG_NOTBOL)) ||
  3078. (sp < m->endp && *(sp-1) == '\n' &&
  3079. (m->g->cflags&REG_NEWLINE)) )
  3080. { /* yes */ }
  3081. else
  3082. return(NULL);
  3083. break;
  3084. case OEOL:
  3085. if ( (sp == m->endp && !(m->eflags&REG_NOTEOL)) ||
  3086. (sp < m->endp && *sp == '\n' &&
  3087. (m->g->cflags&REG_NEWLINE)) )
  3088. { /* yes */ }
  3089. else
  3090. return(NULL);
  3091. break;
  3092. case OBOW:
  3093. if (( (sp == m->beginp && !(m->eflags&REG_NOTBOL)) ||
  3094. (sp < m->endp && *(sp-1) == '\n' &&
  3095. (m->g->cflags&REG_NEWLINE)) ||
  3096. (sp > m->beginp &&
  3097. !ISWORD(*(sp-1))) ) &&
  3098. (sp < m->endp && ISWORD(*sp)) )
  3099. { /* yes */ }
  3100. else
  3101. return(NULL);
  3102. break;
  3103. case OEOW:
  3104. if (( (sp == m->endp && !(m->eflags&REG_NOTEOL)) ||
  3105. (sp < m->endp && *sp == '\n' &&
  3106. (m->g->cflags&REG_NEWLINE)) ||
  3107. (sp < m->endp && !ISWORD(*sp)) ) &&
  3108. (sp > m->beginp && ISWORD(*(sp-1))) )
  3109. { /* yes */ }
  3110. else
  3111. return(NULL);
  3112. break;
  3113. case O_QUEST:
  3114. break;
  3115. case OOR1: /* matches null but needs to skip */
  3116. ss++;
  3117. s = m->g->strip[ss];
  3118. do {
  3119. assert(OP(s) == OOR2);
  3120. ss += OPND(s);
  3121. } while (OP(s = m->g->strip[ss]) != O_CH);
  3122. /* note that the ss++ gets us past the O_CH */
  3123. break;
  3124. default: /* have to make a choice */
  3125. hard = 1;
  3126. break;
  3127. }
  3128. if (!hard) { /* that was it! */
  3129. if (sp != stop)
  3130. return(NULL);
  3131. return(sp);
  3132. }
  3133. ss--; /* adjust for the for's final increment */
  3134. /* the hard stuff */
  3135. AT("hard", sp, stop, ss, stopst);
  3136. s = m->g->strip[ss];
  3137. switch (OP(s)) {
  3138. case OBACK_: /* the vilest depths */
  3139. i = OPND(s);
  3140. assert(0 < i && i <= m->g->nsub);
  3141. if (m->pmatch[i].rm_eo == -1)
  3142. return(NULL);
  3143. assert(m->pmatch[i].rm_so != -1);
  3144. len = m->pmatch[i].rm_eo - m->pmatch[i].rm_so;
  3145. assert(stop - m->beginp >= len);
  3146. if (sp > stop - len)
  3147. return(NULL); /* not enough left to match */
  3148. ssp = m->offp + m->pmatch[i].rm_so;
  3149. if (memcmp(sp, ssp, len) != 0)
  3150. return(NULL);
  3151. while (m->g->strip[ss] != SOP(O_BACK, i))
  3152. ss++;
  3153. return(backref(m, sp+len, stop, ss+1, stopst, lev));
  3154. break;
  3155. case OQUEST_: /* to null or not */
  3156. dp = backref(m, sp, stop, ss+1, stopst, lev);
  3157. if (dp != NULL)
  3158. return(dp); /* not */
  3159. return(backref(m, sp, stop, ss+OPND(s)+1, stopst, lev));
  3160. break;
  3161. case OPLUS_:
  3162. assert(m->lastpos != NULL);
  3163. assert(lev+1 <= m->g->nplus);
  3164. m->lastpos[lev+1] = sp;
  3165. return(backref(m, sp, stop, ss+1, stopst, lev+1));
  3166. break;
  3167. case O_PLUS:
  3168. if (sp == m->lastpos[lev]) /* last pass matched null */
  3169. return(backref(m, sp, stop, ss+1, stopst, lev-1));
  3170. /* try another pass */
  3171. m->lastpos[lev] = sp;
  3172. dp = backref(m, sp, stop, ss-OPND(s)+1, stopst, lev);
  3173. if (dp == NULL)
  3174. return(backref(m, sp, stop, ss+1, stopst, lev-1));
  3175. else
  3176. return(dp);
  3177. break;
  3178. case OCH_: /* find the right one, if any */
  3179. ssub = ss + 1;
  3180. esub = ss + OPND(s) - 1;
  3181. assert(OP(m->g->strip[esub]) == OOR1);
  3182. for (;;) { /* find first matching branch */
  3183. dp = backref(m, sp, stop, ssub, esub, lev);
  3184. if (dp != NULL)
  3185. return(dp);
  3186. /* that one missed, try next one */
  3187. if (OP(m->g->strip[esub]) == O_CH)
  3188. return(NULL); /* there is none */
  3189. esub++;
  3190. assert(OP(m->g->strip[esub]) == OOR2);
  3191. ssub = esub + 1;
  3192. esub += OPND(m->g->strip[esub]);
  3193. if (OP(m->g->strip[esub]) == OOR2)
  3194. esub--;
  3195. else
  3196. assert(OP(m->g->strip[esub]) == O_CH);
  3197. }
  3198. break;
  3199. case OLPAREN: /* must undo assignment if rest fails */
  3200. i = OPND(s);
  3201. assert(0 < i && i <= m->g->nsub);
  3202. offsave = m->pmatch[i].rm_so;
  3203. m->pmatch[i].rm_so = sp - m->offp;
  3204. dp = backref(m, sp, stop, ss+1, stopst, lev);
  3205. if (dp != NULL)
  3206. return(dp);
  3207. m->pmatch[i].rm_so = offsave;
  3208. return(NULL);
  3209. break;
  3210. case ORPAREN: /* must undo assignment if rest fails */
  3211. i = OPND(s);
  3212. assert(0 < i && i <= m->g->nsub);
  3213. offsave = m->pmatch[i].rm_eo;
  3214. m->pmatch[i].rm_eo = sp - m->offp;
  3215. dp = backref(m, sp, stop, ss+1, stopst, lev);
  3216. if (dp != NULL)
  3217. return(dp);
  3218. m->pmatch[i].rm_eo = offsave;
  3219. return(NULL);
  3220. break;
  3221. default: /* uh oh */
  3222. assert(nope);
  3223. break;
  3224. }
  3225. /* "can't happen" */
  3226. assert(nope);
  3227. /* NOTREACHED */
  3228. return((char *)NULL); /* dummy */
  3229. }
  3230. /*
  3231. - fast - step through the string at top speed
  3232. == static char *fast(register struct match *m, char *start, \
  3233. == char *stop, sopno startst, sopno stopst);
  3234. */
  3235. static char * /* where tentative match ended, or NULL */
  3236. fast(m, start, stop, startst, stopst)
  3237. register struct match *m;
  3238. char *start;
  3239. char *stop;
  3240. sopno startst;
  3241. sopno stopst;
  3242. {
  3243. register states st = m->st;
  3244. register states fresh = m->fresh;
  3245. register states tmp = m->tmp;
  3246. register char *p = start;
  3247. register int c = (start == m->beginp) ? OUT : *(start-1);
  3248. register int lastc; /* previous c */
  3249. register int flagch;
  3250. register int i;
  3251. register char *coldp; /* last p after which no match was underway */
  3252. CLEAR(st);
  3253. SET1(st, startst);
  3254. st = step(m->g, startst, stopst, st, NOTHING, st);
  3255. ASSIGN(fresh, st);
  3256. SP("start", st, *p);
  3257. coldp = NULL;
  3258. for (;;) {
  3259. /* next character */
  3260. lastc = c;
  3261. c = (p == m->endp) ? OUT : *p;
  3262. if (EQ(st, fresh))
  3263. coldp = p;
  3264. /* is there an EOL and/or BOL between lastc and c? */
  3265. flagch = '\0';
  3266. i = 0;
  3267. if ( (lastc == '\n' && m->g->cflags&REG_NEWLINE) ||
  3268. (lastc == OUT && !(m->eflags&REG_NOTBOL)) ) {
  3269. flagch = BOL;
  3270. i = m->g->nbol;
  3271. }
  3272. if ( (c == '\n' && m->g->cflags&REG_NEWLINE) ||
  3273. (c == OUT && !(m->eflags&REG_NOTEOL)) ) {
  3274. flagch = (flagch == BOL) ? BOLEOL : EOL;
  3275. i += m->g->neol;
  3276. }
  3277. if (i != 0) {
  3278. for (; i > 0; i--)
  3279. st = step(m->g, startst, stopst, st, flagch, st);
  3280. SP("boleol", st, c);
  3281. }
  3282. /* how about a word boundary? */
  3283. if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
  3284. (c != OUT && ISWORD(c)) ) {
  3285. flagch = BOW;
  3286. }
  3287. if ( (lastc != OUT && ISWORD(lastc)) &&
  3288. (flagch == EOL || (c != OUT && !ISWORD(c))) ) {
  3289. flagch = EOW;
  3290. }
  3291. if (flagch == BOW || flagch == EOW) {
  3292. st = step(m->g, startst, stopst, st, flagch, st);
  3293. SP("boweow", st, c);
  3294. }
  3295. /* are we done? */
  3296. if (ISSET(st, stopst) || p == stop)
  3297. break; /* NOTE BREAK OUT */
  3298. /* no, we must deal with this character */
  3299. ASSIGN(tmp, st);
  3300. ASSIGN(st, fresh);
  3301. assert(c != OUT);
  3302. st = step(m->g, startst, stopst, tmp, c, st);
  3303. SP("aft", st, c);
  3304. assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
  3305. p++;
  3306. }
  3307. assert(coldp != NULL);
  3308. m->coldp = coldp;
  3309. if (ISSET(st, stopst))
  3310. return(p+1);
  3311. else
  3312. return(NULL);
  3313. }
  3314. /*
  3315. - slow - step through the string more deliberately
  3316. == static char *slow(register struct match *m, char *start, \
  3317. == char *stop, sopno startst, sopno stopst);
  3318. */
  3319. static char * /* where it ended */
  3320. slow(m, start, stop, startst, stopst)
  3321. register struct match *m;
  3322. char *start;
  3323. char *stop;
  3324. sopno startst;
  3325. sopno stopst;
  3326. {
  3327. register states st = m->st;
  3328. register states empty = m->empty;
  3329. register states tmp = m->tmp;
  3330. register char *p = start;
  3331. register int c = (start == m->beginp) ? OUT : *(start-1);
  3332. register int lastc; /* previous c */
  3333. register int flagch;
  3334. register int i;
  3335. register char *matchp; /* last p at which a match ended */
  3336. AT("slow", start, stop, startst, stopst);
  3337. CLEAR(st);
  3338. SET1(st, startst);
  3339. SP("sstart", st, *p);
  3340. st = step(m->g, startst, stopst, st, NOTHING, st);
  3341. matchp = NULL;
  3342. for (;;) {
  3343. /* next character */
  3344. lastc = c;
  3345. c = (p == m->endp) ? OUT : *p;
  3346. /* is there an EOL and/or BOL between lastc and c? */
  3347. flagch = '\0';
  3348. i = 0;
  3349. if ( (lastc == '\n' && m->g->cflags&REG_NEWLINE) ||
  3350. (lastc == OUT && !(m->eflags&REG_NOTBOL)) ) {
  3351. flagch = BOL;
  3352. i = m->g->nbol;
  3353. }
  3354. if ( (c == '\n' && m->g->cflags&REG_NEWLINE) ||
  3355. (c == OUT && !(m->eflags&REG_NOTEOL)) ) {
  3356. flagch = (flagch == BOL) ? BOLEOL : EOL;
  3357. i += m->g->neol;
  3358. }
  3359. if (i != 0) {
  3360. for (; i > 0; i--)
  3361. st = step(m->g, startst, stopst, st, flagch, st);
  3362. SP("sboleol", st, c);
  3363. }
  3364. /* how about a word boundary? */
  3365. if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
  3366. (c != OUT && ISWORD(c)) ) {
  3367. flagch = BOW;
  3368. }
  3369. if ( (lastc != OUT && ISWORD(lastc)) &&
  3370. (flagch == EOL || (c != OUT && !ISWORD(c))) ) {
  3371. flagch = EOW;
  3372. }
  3373. if (flagch == BOW || flagch == EOW) {
  3374. st = step(m->g, startst, stopst, st, flagch, st);
  3375. SP("sboweow", st, c);
  3376. }
  3377. /* are we done? */
  3378. if (ISSET(st, stopst))
  3379. matchp = p;
  3380. if (EQ(st, empty) || p == stop)
  3381. break; /* NOTE BREAK OUT */
  3382. /* no, we must deal with this character */
  3383. ASSIGN(tmp, st);
  3384. ASSIGN(st, empty);
  3385. assert(c != OUT);
  3386. st = step(m->g, startst, stopst, tmp, c, st);
  3387. SP("saft", st, c);
  3388. assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
  3389. p++;
  3390. }
  3391. return(matchp);
  3392. }
  3393. static states
  3394. step(g, start, stop, bef, ch, aft)
  3395. register struct re_guts *g;
  3396. sopno start; /* start state within strip */
  3397. sopno stop; /* state after stop state within strip */
  3398. register states bef; /* states reachable before */
  3399. int ch; /* character or NONCHAR code */
  3400. register states aft; /* states already known reachable after */
  3401. {
  3402. register cset *cs;
  3403. register sop s;
  3404. register sopno pc;
  3405. register onestate here; /* note, macros know this name */
  3406. register sopno look;
  3407. register long i;
  3408. for (pc = start, INIT(here, pc); pc != stop; pc++, INC(here)) {
  3409. s = g->strip[pc];
  3410. switch (OP(s)) {
  3411. case OEND:
  3412. assert(pc == stop-1);
  3413. break;
  3414. case OCHAR:
  3415. /* only characters can match */
  3416. assert(!NONCHAR(ch) || ch != (char)OPND(s));
  3417. if (ch == (char)OPND(s))
  3418. FWD(aft, bef, 1);
  3419. break;
  3420. case OBOL:
  3421. if (ch == BOL || ch == BOLEOL)
  3422. FWD(aft, bef, 1);
  3423. break;
  3424. case OEOL:
  3425. if (ch == EOL || ch == BOLEOL)
  3426. FWD(aft, bef, 1);
  3427. break;
  3428. case OBOW:
  3429. if (ch == BOW)
  3430. FWD(aft, bef, 1);
  3431. break;
  3432. case OEOW:
  3433. if (ch == EOW)
  3434. FWD(aft, bef, 1);
  3435. break;
  3436. case OANY:
  3437. if (!NONCHAR(ch))
  3438. FWD(aft, bef, 1);
  3439. break;
  3440. case OANYOF:
  3441. cs = &g->sets[OPND(s)];
  3442. if (!NONCHAR(ch) && CHIN(cs, ch))
  3443. FWD(aft, bef, 1);
  3444. break;
  3445. case OBACK_: /* ignored here */
  3446. case O_BACK:
  3447. FWD(aft, aft, 1);
  3448. break;
  3449. case OPLUS_: /* forward, this is just an empty */
  3450. FWD(aft, aft, 1);
  3451. break;
  3452. case O_PLUS: /* both forward and back */
  3453. FWD(aft, aft, 1);
  3454. i = ISSETBACK(aft, OPND(s));
  3455. BACK(aft, aft, OPND(s));
  3456. if (!i && ISSETBACK(aft, OPND(s))) {
  3457. /* oho, must reconsider loop body */
  3458. pc -= OPND(s) + 1;
  3459. INIT(here, pc);
  3460. }
  3461. break;
  3462. case OQUEST_: /* two branches, both forward */
  3463. FWD(aft, aft, 1);
  3464. FWD(aft, aft, OPND(s));
  3465. break;
  3466. case O_QUEST: /* just an empty */
  3467. FWD(aft, aft, 1);
  3468. break;
  3469. case OLPAREN: /* not significant here */
  3470. case ORPAREN:
  3471. FWD(aft, aft, 1);
  3472. break;
  3473. case OCH_: /* mark the first two branches */
  3474. FWD(aft, aft, 1);
  3475. assert(OP(g->strip[pc+OPND(s)]) == OOR2);
  3476. FWD(aft, aft, OPND(s));
  3477. break;
  3478. case OOR1: /* done a branch, find the O_CH */
  3479. if (ISSTATEIN(aft, here)) {
  3480. for (look = 1;
  3481. OP(s = g->strip[pc+look]) != O_CH;
  3482. look += OPND(s))
  3483. assert(OP(s) == OOR2);
  3484. FWD(aft, aft, look);
  3485. }
  3486. break;
  3487. case OOR2: /* propagate OCH_'s marking */
  3488. FWD(aft, aft, 1);
  3489. if (OP(g->strip[pc+OPND(s)]) != O_CH) {
  3490. assert(OP(g->strip[pc+OPND(s)]) == OOR2);
  3491. FWD(aft, aft, OPND(s));
  3492. }
  3493. break;
  3494. case O_CH: /* just empty */
  3495. FWD(aft, aft, 1);
  3496. break;
  3497. default: /* ooooops... */
  3498. assert(nope);
  3499. break;
  3500. }
  3501. }
  3502. return(aft);
  3503. }
  3504. #undef matcher
  3505. #undef fast
  3506. #undef slow
  3507. #undef dissect
  3508. #undef backref
  3509. #undef step
  3510. #undef print
  3511. #undef at
  3512. #undef match
  3513. int /* 0 success, REG_NOMATCH failure */
  3514. regexec(preg, string, nmatch, pmatch, eflags)
  3515. const regex_t *preg;
  3516. const char *string;
  3517. size_t nmatch;
  3518. regmatch_t pmatch[];
  3519. int eflags;
  3520. {
  3521. register struct re_guts *g = preg->re_g;
  3522. #define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
  3523. if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
  3524. return(REG_BADPAT);
  3525. assert(!(g->iflags&BAD));
  3526. if (g->iflags&BAD) /* backstop for no-debug case */
  3527. return(REG_BADPAT);
  3528. eflags = GOODFLAGS(eflags);
  3529. if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags&REG_LARGE))
  3530. return(smatcher(g, (char *)string, nmatch, pmatch, eflags));
  3531. else
  3532. return(lmatcher(g, (char *)string, nmatch, pmatch, eflags));
  3533. #undef GOODFLAGS
  3534. }
  3535. /*
  3536. - regfree - free everything
  3537. = extern void regfree(regex_t *);
  3538. */
  3539. void
  3540. regfree(preg)
  3541. regex_t *preg;
  3542. {
  3543. register struct re_guts *g;
  3544. if (preg->re_magic != MAGIC1) /* oops */
  3545. return; /* nice to complain, but hard */
  3546. g = preg->re_g;
  3547. if (g == NULL || g->magic != MAGIC2) /* oops again */
  3548. return;
  3549. preg->re_magic = 0; /* mark it invalid */
  3550. g->magic = 0; /* mark it invalid */
  3551. if (g->strip != NULL)
  3552. free((char *)g->strip);
  3553. if (g->sets != NULL)
  3554. free((char *)g->sets);
  3555. if (g->setbits != NULL)
  3556. free((char *)g->setbits);
  3557. if (g->must != NULL)
  3558. free(g->must);
  3559. free((char *)g);
  3560. }
  3561. #ifdef WITH_MAIN
  3562. int main(int argc, char* argv[]){
  3563. regex_t preg;
  3564. int i, s;
  3565. if(argc<2) return 1;
  3566. if (regcomp(&preg, argv[1], REG_NOSUB)) {
  3567. fprintf(stderr, "Failed to compile regex\n");
  3568. return 2;
  3569. }
  3570. for (i = 2; i<argc; i++){
  3571. s = regexec(&preg, argv[i], 0, NULL, 0);
  3572. fprintf(stdout, "String[%d]: ", i-1);
  3573. switch(s){
  3574. case 0:
  3575. printf("match\n");
  3576. break;
  3577. case REG_NOMATCH:
  3578. printf("not match\n");
  3579. break;
  3580. default:
  3581. printf("failed (%d)\n", s);
  3582. }
  3583. }
  3584. regfree(&preg);
  3585. return 0;
  3586. }
  3587. #endif