regcomp.c 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851
  1. /* Extended regular expression matching and search library.
  2. Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009
  3. Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License along
  15. with this program; if not, write to the Free Software Foundation,
  16. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  17. static reg_errcode_t re_compile_internal (regex_t *preg, const char * pattern,
  18. size_t length, reg_syntax_t syntax);
  19. static void re_compile_fastmap_iter (regex_t *bufp,
  20. const re_dfastate_t *init_state,
  21. char *fastmap);
  22. static reg_errcode_t init_dfa (re_dfa_t *dfa, size_t pat_len);
  23. #ifdef RE_ENABLE_I18N
  24. static void free_charset (re_charset_t *cset);
  25. #endif /* RE_ENABLE_I18N */
  26. static void free_workarea_compile (regex_t *preg);
  27. static reg_errcode_t create_initial_state (re_dfa_t *dfa);
  28. #ifdef RE_ENABLE_I18N
  29. static void optimize_utf8 (re_dfa_t *dfa);
  30. #endif
  31. static reg_errcode_t analyze (regex_t *preg);
  32. static reg_errcode_t preorder (bin_tree_t *root,
  33. reg_errcode_t (fn (void *, bin_tree_t *)),
  34. void *extra);
  35. static reg_errcode_t postorder (bin_tree_t *root,
  36. reg_errcode_t (fn (void *, bin_tree_t *)),
  37. void *extra);
  38. static reg_errcode_t optimize_subexps (void *extra, bin_tree_t *node);
  39. static reg_errcode_t lower_subexps (void *extra, bin_tree_t *node);
  40. static bin_tree_t *lower_subexp (reg_errcode_t *err, regex_t *preg,
  41. bin_tree_t *node);
  42. static reg_errcode_t calc_first (void *extra, bin_tree_t *node);
  43. static reg_errcode_t calc_next (void *extra, bin_tree_t *node);
  44. static reg_errcode_t link_nfa_nodes (void *extra, bin_tree_t *node);
  45. static Idx duplicate_node (re_dfa_t *dfa, Idx org_idx, unsigned int constraint);
  46. static Idx search_duplicated_node (const re_dfa_t *dfa, Idx org_node,
  47. unsigned int constraint);
  48. static reg_errcode_t calc_eclosure (re_dfa_t *dfa);
  49. static reg_errcode_t calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa,
  50. Idx node, bool root);
  51. static reg_errcode_t calc_inveclosure (re_dfa_t *dfa);
  52. static Idx fetch_number (re_string_t *input, re_token_t *token,
  53. reg_syntax_t syntax);
  54. static int peek_token (re_token_t *token, re_string_t *input,
  55. reg_syntax_t syntax) internal_function;
  56. static bin_tree_t *parse (re_string_t *regexp, regex_t *preg,
  57. reg_syntax_t syntax, reg_errcode_t *err);
  58. static bin_tree_t *parse_reg_exp (re_string_t *regexp, regex_t *preg,
  59. re_token_t *token, reg_syntax_t syntax,
  60. Idx nest, reg_errcode_t *err);
  61. static bin_tree_t *parse_branch (re_string_t *regexp, regex_t *preg,
  62. re_token_t *token, reg_syntax_t syntax,
  63. Idx nest, reg_errcode_t *err);
  64. static bin_tree_t *parse_expression (re_string_t *regexp, regex_t *preg,
  65. re_token_t *token, reg_syntax_t syntax,
  66. Idx nest, reg_errcode_t *err);
  67. static bin_tree_t *parse_sub_exp (re_string_t *regexp, regex_t *preg,
  68. re_token_t *token, reg_syntax_t syntax,
  69. Idx nest, reg_errcode_t *err);
  70. static bin_tree_t *parse_dup_op (bin_tree_t *dup_elem, re_string_t *regexp,
  71. re_dfa_t *dfa, re_token_t *token,
  72. reg_syntax_t syntax, reg_errcode_t *err);
  73. static bin_tree_t *parse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa,
  74. re_token_t *token, reg_syntax_t syntax,
  75. reg_errcode_t *err);
  76. static reg_errcode_t parse_bracket_element (bracket_elem_t *elem,
  77. re_string_t *regexp,
  78. re_token_t *token, int token_len,
  79. re_dfa_t *dfa,
  80. reg_syntax_t syntax,
  81. bool accept_hyphen);
  82. static reg_errcode_t parse_bracket_symbol (bracket_elem_t *elem,
  83. re_string_t *regexp,
  84. re_token_t *token);
  85. #ifdef RE_ENABLE_I18N
  86. static reg_errcode_t build_equiv_class (bitset_t sbcset,
  87. re_charset_t *mbcset,
  88. Idx *equiv_class_alloc,
  89. const unsigned char *name);
  90. static reg_errcode_t build_charclass (RE_TRANSLATE_TYPE trans,
  91. bitset_t sbcset,
  92. re_charset_t *mbcset,
  93. Idx *char_class_alloc,
  94. const unsigned char *class_name,
  95. reg_syntax_t syntax);
  96. #else /* not RE_ENABLE_I18N */
  97. static reg_errcode_t build_equiv_class (bitset_t sbcset,
  98. const unsigned char *name);
  99. static reg_errcode_t build_charclass (RE_TRANSLATE_TYPE trans,
  100. bitset_t sbcset,
  101. const unsigned char *class_name,
  102. reg_syntax_t syntax);
  103. #endif /* not RE_ENABLE_I18N */
  104. static bin_tree_t *build_charclass_op (re_dfa_t *dfa,
  105. RE_TRANSLATE_TYPE trans,
  106. const unsigned char *class_name,
  107. const unsigned char *extra,
  108. bool non_match, reg_errcode_t *err);
  109. static bin_tree_t *create_tree (re_dfa_t *dfa,
  110. bin_tree_t *left, bin_tree_t *right,
  111. re_token_type_t type);
  112. static bin_tree_t *create_token_tree (re_dfa_t *dfa,
  113. bin_tree_t *left, bin_tree_t *right,
  114. const re_token_t *token);
  115. static bin_tree_t *duplicate_tree (const bin_tree_t *src, re_dfa_t *dfa);
  116. static void free_token (re_token_t *node);
  117. static reg_errcode_t free_tree (void *extra, bin_tree_t *node);
  118. static reg_errcode_t mark_opt_subexp (void *extra, bin_tree_t *node);
  119. /* This table gives an error message for each of the error codes listed
  120. in regex.h. Obviously the order here has to be same as there.
  121. POSIX doesn't require that we do anything for REG_NOERROR,
  122. but why not be nice? */
  123. static const char __re_error_msgid[] =
  124. {
  125. #define REG_NOERROR_IDX 0
  126. gettext_noop ("Success") /* REG_NOERROR */
  127. "\0"
  128. #define REG_NOMATCH_IDX (REG_NOERROR_IDX + sizeof "Success")
  129. gettext_noop ("No match") /* REG_NOMATCH */
  130. "\0"
  131. #define REG_BADPAT_IDX (REG_NOMATCH_IDX + sizeof "No match")
  132. gettext_noop ("Invalid regular expression") /* REG_BADPAT */
  133. "\0"
  134. #define REG_ECOLLATE_IDX (REG_BADPAT_IDX + sizeof "Invalid regular expression")
  135. gettext_noop ("Invalid collation character") /* REG_ECOLLATE */
  136. "\0"
  137. #define REG_ECTYPE_IDX (REG_ECOLLATE_IDX + sizeof "Invalid collation character")
  138. gettext_noop ("Invalid character class name") /* REG_ECTYPE */
  139. "\0"
  140. #define REG_EESCAPE_IDX (REG_ECTYPE_IDX + sizeof "Invalid character class name")
  141. gettext_noop ("Trailing backslash") /* REG_EESCAPE */
  142. "\0"
  143. #define REG_ESUBREG_IDX (REG_EESCAPE_IDX + sizeof "Trailing backslash")
  144. gettext_noop ("Invalid back reference") /* REG_ESUBREG */
  145. "\0"
  146. #define REG_EBRACK_IDX (REG_ESUBREG_IDX + sizeof "Invalid back reference")
  147. gettext_noop ("Unmatched [ or [^") /* REG_EBRACK */
  148. "\0"
  149. #define REG_EPAREN_IDX (REG_EBRACK_IDX + sizeof "Unmatched [ or [^")
  150. gettext_noop ("Unmatched ( or \\(") /* REG_EPAREN */
  151. "\0"
  152. #define REG_EBRACE_IDX (REG_EPAREN_IDX + sizeof "Unmatched ( or \\(")
  153. gettext_noop ("Unmatched \\{") /* REG_EBRACE */
  154. "\0"
  155. #define REG_BADBR_IDX (REG_EBRACE_IDX + sizeof "Unmatched \\{")
  156. gettext_noop ("Invalid content of \\{\\}") /* REG_BADBR */
  157. "\0"
  158. #define REG_ERANGE_IDX (REG_BADBR_IDX + sizeof "Invalid content of \\{\\}")
  159. gettext_noop ("Invalid range end") /* REG_ERANGE */
  160. "\0"
  161. #define REG_ESPACE_IDX (REG_ERANGE_IDX + sizeof "Invalid range end")
  162. gettext_noop ("Memory exhausted") /* REG_ESPACE */
  163. "\0"
  164. #define REG_BADRPT_IDX (REG_ESPACE_IDX + sizeof "Memory exhausted")
  165. gettext_noop ("Invalid preceding regular expression") /* REG_BADRPT */
  166. "\0"
  167. #define REG_EEND_IDX (REG_BADRPT_IDX + sizeof "Invalid preceding regular expression")
  168. gettext_noop ("Premature end of regular expression") /* REG_EEND */
  169. "\0"
  170. #define REG_ESIZE_IDX (REG_EEND_IDX + sizeof "Premature end of regular expression")
  171. gettext_noop ("Regular expression too big") /* REG_ESIZE */
  172. "\0"
  173. #define REG_ERPAREN_IDX (REG_ESIZE_IDX + sizeof "Regular expression too big")
  174. gettext_noop ("Unmatched ) or \\)") /* REG_ERPAREN */
  175. };
  176. static const size_t __re_error_msgid_idx[] =
  177. {
  178. REG_NOERROR_IDX,
  179. REG_NOMATCH_IDX,
  180. REG_BADPAT_IDX,
  181. REG_ECOLLATE_IDX,
  182. REG_ECTYPE_IDX,
  183. REG_EESCAPE_IDX,
  184. REG_ESUBREG_IDX,
  185. REG_EBRACK_IDX,
  186. REG_EPAREN_IDX,
  187. REG_EBRACE_IDX,
  188. REG_BADBR_IDX,
  189. REG_ERANGE_IDX,
  190. REG_ESPACE_IDX,
  191. REG_BADRPT_IDX,
  192. REG_EEND_IDX,
  193. REG_ESIZE_IDX,
  194. REG_ERPAREN_IDX
  195. };
  196. /* Entry points for GNU code. */
  197. /* re_compile_pattern is the GNU regular expression compiler: it
  198. compiles PATTERN (of length LENGTH) and puts the result in BUFP.
  199. Returns 0 if the pattern was valid, otherwise an error string.
  200. Assumes the `allocated' (and perhaps `buffer') and `translate' fields
  201. are set in BUFP on entry. */
  202. #ifdef _LIBC
  203. const char *
  204. re_compile_pattern (pattern, length, bufp)
  205. const char *pattern;
  206. size_t length;
  207. struct re_pattern_buffer *bufp;
  208. #else /* size_t might promote */
  209. const char *
  210. re_compile_pattern (const char *pattern, size_t length,
  211. struct re_pattern_buffer *bufp)
  212. #endif
  213. {
  214. reg_errcode_t ret;
  215. /* And GNU code determines whether or not to get register information
  216. by passing null for the REGS argument to re_match, etc., not by
  217. setting no_sub, unless RE_NO_SUB is set. */
  218. bufp->no_sub = !!(re_syntax_options & RE_NO_SUB);
  219. /* Match anchors at newline. */
  220. bufp->newline_anchor = 1;
  221. ret = re_compile_internal (bufp, pattern, length, re_syntax_options);
  222. if (!ret)
  223. return NULL;
  224. return gettext (__re_error_msgid + __re_error_msgid_idx[(int) ret]);
  225. }
  226. #ifdef _LIBC
  227. weak_alias (__re_compile_pattern, re_compile_pattern)
  228. #endif
  229. /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
  230. also be assigned to arbitrarily: each pattern buffer stores its own
  231. syntax, so it can be changed between regex compilations. */
  232. /* This has no initializer because initialized variables in Emacs
  233. become read-only after dumping. */
  234. reg_syntax_t re_syntax_options;
  235. /* Specify the precise syntax of regexps for compilation. This provides
  236. for compatibility for various utilities which historically have
  237. different, incompatible syntaxes.
  238. The argument SYNTAX is a bit mask comprised of the various bits
  239. defined in regex.h. We return the old syntax. */
  240. reg_syntax_t
  241. re_set_syntax (syntax)
  242. reg_syntax_t syntax;
  243. {
  244. reg_syntax_t ret = re_syntax_options;
  245. re_syntax_options = syntax;
  246. return ret;
  247. }
  248. #ifdef _LIBC
  249. weak_alias (__re_set_syntax, re_set_syntax)
  250. #endif
  251. int
  252. re_compile_fastmap (bufp)
  253. struct re_pattern_buffer *bufp;
  254. {
  255. re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
  256. char *fastmap = bufp->fastmap;
  257. memset (fastmap, '\0', sizeof (char) * SBC_MAX);
  258. re_compile_fastmap_iter (bufp, dfa->init_state, fastmap);
  259. if (dfa->init_state != dfa->init_state_word)
  260. re_compile_fastmap_iter (bufp, dfa->init_state_word, fastmap);
  261. if (dfa->init_state != dfa->init_state_nl)
  262. re_compile_fastmap_iter (bufp, dfa->init_state_nl, fastmap);
  263. if (dfa->init_state != dfa->init_state_begbuf)
  264. re_compile_fastmap_iter (bufp, dfa->init_state_begbuf, fastmap);
  265. bufp->fastmap_accurate = 1;
  266. return 0;
  267. }
  268. #ifdef _LIBC
  269. weak_alias (__re_compile_fastmap, re_compile_fastmap)
  270. #endif
  271. static inline void
  272. __attribute ((always_inline))
  273. re_set_fastmap (char *fastmap, bool icase, int ch)
  274. {
  275. fastmap[ch] = 1;
  276. if (icase)
  277. fastmap[tolower (ch)] = 1;
  278. }
  279. /* Helper function for re_compile_fastmap.
  280. Compile fastmap for the initial_state INIT_STATE. */
  281. static void
  282. re_compile_fastmap_iter (regex_t *bufp, const re_dfastate_t *init_state,
  283. char *fastmap)
  284. {
  285. re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
  286. Idx node_cnt;
  287. bool icase = (dfa->mb_cur_max == 1 && (bufp->syntax & RE_ICASE));
  288. for (node_cnt = 0; node_cnt < init_state->nodes.nelem; ++node_cnt)
  289. {
  290. Idx node = init_state->nodes.elems[node_cnt];
  291. re_token_type_t type = dfa->nodes[node].type;
  292. if (type == CHARACTER)
  293. {
  294. re_set_fastmap (fastmap, icase, dfa->nodes[node].opr.c);
  295. #ifdef RE_ENABLE_I18N
  296. if ((bufp->syntax & RE_ICASE) && dfa->mb_cur_max > 1)
  297. {
  298. unsigned char buf[MB_LEN_MAX];
  299. unsigned char *p;
  300. wchar_t wc;
  301. mbstate_t state;
  302. p = buf;
  303. *p++ = dfa->nodes[node].opr.c;
  304. while (++node < dfa->nodes_len
  305. && dfa->nodes[node].type == CHARACTER
  306. && dfa->nodes[node].mb_partial)
  307. *p++ = dfa->nodes[node].opr.c;
  308. memset (&state, '\0', sizeof (state));
  309. if (__mbrtowc (&wc, (const char *) buf, p - buf,
  310. &state) == p - buf
  311. && (__wcrtomb ((char *) buf, towlower (wc), &state)
  312. != (size_t) -1))
  313. re_set_fastmap (fastmap, false, buf[0]);
  314. }
  315. #endif
  316. }
  317. else if (type == SIMPLE_BRACKET)
  318. {
  319. int i, ch;
  320. for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
  321. {
  322. int j;
  323. bitset_word_t w = dfa->nodes[node].opr.sbcset[i];
  324. for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
  325. if (w & ((bitset_word_t) 1 << j))
  326. re_set_fastmap (fastmap, icase, ch);
  327. }
  328. }
  329. #ifdef RE_ENABLE_I18N
  330. else if (type == COMPLEX_BRACKET)
  331. {
  332. re_charset_t *cset = dfa->nodes[node].opr.mbcset;
  333. Idx i;
  334. # ifdef _LIBC
  335. /* See if we have to try all bytes which start multiple collation
  336. elements.
  337. e.g. In da_DK, we want to catch 'a' since "aa" is a valid
  338. collation element, and don't catch 'b' since 'b' is
  339. the only collation element which starts from 'b' (and
  340. it is caught by SIMPLE_BRACKET). */
  341. if (_NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES) != 0
  342. && (cset->ncoll_syms || cset->nranges))
  343. {
  344. const int32_t *table = (const int32_t *)
  345. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
  346. for (i = 0; i < SBC_MAX; ++i)
  347. if (table[i] < 0)
  348. re_set_fastmap (fastmap, icase, i);
  349. }
  350. # endif /* _LIBC */
  351. /* See if we have to start the match at all multibyte characters,
  352. i.e. where we would not find an invalid sequence. This only
  353. applies to multibyte character sets; for single byte character
  354. sets, the SIMPLE_BRACKET again suffices. */
  355. if (dfa->mb_cur_max > 1
  356. && (cset->nchar_classes || cset->non_match
  357. # ifdef _LIBC
  358. || cset->nequiv_classes
  359. # endif /* _LIBC */
  360. ))
  361. {
  362. unsigned char c = 0;
  363. do
  364. {
  365. mbstate_t mbs;
  366. memset (&mbs, 0, sizeof (mbs));
  367. if (__mbrtowc (NULL, (char *) &c, 1, &mbs) == (size_t) -2)
  368. re_set_fastmap (fastmap, false, (int) c);
  369. }
  370. while (++c != 0);
  371. }
  372. else
  373. {
  374. /* ... Else catch all bytes which can start the mbchars. */
  375. for (i = 0; i < cset->nmbchars; ++i)
  376. {
  377. char buf[256];
  378. mbstate_t state;
  379. memset (&state, '\0', sizeof (state));
  380. if (__wcrtomb (buf, cset->mbchars[i], &state) != (size_t) -1)
  381. re_set_fastmap (fastmap, icase, *(unsigned char *) buf);
  382. if ((bufp->syntax & RE_ICASE) && dfa->mb_cur_max > 1)
  383. {
  384. if (__wcrtomb (buf, towlower (cset->mbchars[i]), &state)
  385. != (size_t) -1)
  386. re_set_fastmap (fastmap, false, *(unsigned char *) buf);
  387. }
  388. }
  389. }
  390. }
  391. #endif /* RE_ENABLE_I18N */
  392. else if (type == OP_PERIOD
  393. #ifdef RE_ENABLE_I18N
  394. || type == OP_UTF8_PERIOD
  395. #endif /* RE_ENABLE_I18N */
  396. || type == END_OF_RE)
  397. {
  398. memset (fastmap, '\1', sizeof (char) * SBC_MAX);
  399. if (type == END_OF_RE)
  400. bufp->can_be_null = 1;
  401. return;
  402. }
  403. }
  404. }
  405. /* Entry point for POSIX code. */
  406. /* regcomp takes a regular expression as a string and compiles it.
  407. PREG is a regex_t *. We do not expect any fields to be initialized,
  408. since POSIX says we shouldn't. Thus, we set
  409. `buffer' to the compiled pattern;
  410. `used' to the length of the compiled pattern;
  411. `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
  412. REG_EXTENDED bit in CFLAGS is set; otherwise, to
  413. RE_SYNTAX_POSIX_BASIC;
  414. `newline_anchor' to REG_NEWLINE being set in CFLAGS;
  415. `fastmap' to an allocated space for the fastmap;
  416. `fastmap_accurate' to zero;
  417. `re_nsub' to the number of subexpressions in PATTERN.
  418. PATTERN is the address of the pattern string.
  419. CFLAGS is a series of bits which affect compilation.
  420. If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
  421. use POSIX basic syntax.
  422. If REG_NEWLINE is set, then . and [^...] don't match newline.
  423. Also, regexec will try a match beginning after every newline.
  424. If REG_ICASE is set, then we considers upper- and lowercase
  425. versions of letters to be equivalent when matching.
  426. If REG_NOSUB is set, then when PREG is passed to regexec, that
  427. routine will report only success or failure, and nothing about the
  428. registers.
  429. It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
  430. the return codes and their meanings.) */
  431. int
  432. regcomp (preg, pattern, cflags)
  433. regex_t *_Restrict_ preg;
  434. const char *_Restrict_ pattern;
  435. int cflags;
  436. {
  437. reg_errcode_t ret;
  438. reg_syntax_t syntax = ((cflags & REG_EXTENDED) ? RE_SYNTAX_POSIX_EXTENDED
  439. : RE_SYNTAX_POSIX_BASIC);
  440. preg->buffer = NULL;
  441. preg->allocated = 0;
  442. preg->used = 0;
  443. /* Try to allocate space for the fastmap. */
  444. preg->fastmap = re_malloc (char, SBC_MAX);
  445. if (BE (preg->fastmap == NULL, 0))
  446. return REG_ESPACE;
  447. syntax |= (cflags & REG_ICASE) ? RE_ICASE : 0;
  448. /* If REG_NEWLINE is set, newlines are treated differently. */
  449. if (cflags & REG_NEWLINE)
  450. { /* REG_NEWLINE implies neither . nor [^...] match newline. */
  451. syntax &= ~RE_DOT_NEWLINE;
  452. syntax |= RE_HAT_LISTS_NOT_NEWLINE;
  453. /* It also changes the matching behavior. */
  454. preg->newline_anchor = 1;
  455. }
  456. else
  457. preg->newline_anchor = 0;
  458. preg->no_sub = !!(cflags & REG_NOSUB);
  459. preg->translate = NULL;
  460. ret = re_compile_internal (preg, pattern, strlen (pattern), syntax);
  461. /* POSIX doesn't distinguish between an unmatched open-group and an
  462. unmatched close-group: both are REG_EPAREN. */
  463. if (ret == REG_ERPAREN)
  464. ret = REG_EPAREN;
  465. /* We have already checked preg->fastmap != NULL. */
  466. if (BE (ret == REG_NOERROR, 1))
  467. /* Compute the fastmap now, since regexec cannot modify the pattern
  468. buffer. This function never fails in this implementation. */
  469. (void) re_compile_fastmap (preg);
  470. else
  471. {
  472. /* Some error occurred while compiling the expression. */
  473. re_free (preg->fastmap);
  474. preg->fastmap = NULL;
  475. }
  476. return (int) ret;
  477. }
  478. #ifdef _LIBC
  479. weak_alias (__regcomp, regcomp)
  480. #endif
  481. /* Returns a message corresponding to an error code, ERRCODE, returned
  482. from either regcomp or regexec. We don't use PREG here. */
  483. #ifdef _LIBC
  484. size_t
  485. regerror (errcode, preg, errbuf, errbuf_size)
  486. int errcode;
  487. const regex_t *_Restrict_ preg;
  488. char *_Restrict_ errbuf;
  489. size_t errbuf_size;
  490. #else /* size_t might promote */
  491. size_t
  492. regerror (int errcode, const regex_t *_Restrict_ preg,
  493. char *_Restrict_ errbuf, size_t errbuf_size)
  494. #endif
  495. {
  496. const char *msg;
  497. size_t msg_size;
  498. if (BE (errcode < 0
  499. || errcode >= (int) (sizeof (__re_error_msgid_idx)
  500. / sizeof (__re_error_msgid_idx[0])), 0))
  501. /* Only error codes returned by the rest of the code should be passed
  502. to this routine. If we are given anything else, or if other regex
  503. code generates an invalid error code, then the program has a bug.
  504. Dump core so we can fix it. */
  505. abort ();
  506. msg = gettext (__re_error_msgid + __re_error_msgid_idx[errcode]);
  507. msg_size = strlen (msg) + 1; /* Includes the null. */
  508. if (BE (errbuf_size != 0, 1))
  509. {
  510. size_t cpy_size = msg_size;
  511. if (BE (msg_size > errbuf_size, 0))
  512. {
  513. cpy_size = errbuf_size - 1;
  514. errbuf[cpy_size] = '\0';
  515. }
  516. memcpy (errbuf, msg, cpy_size);
  517. }
  518. return msg_size;
  519. }
  520. #ifdef _LIBC
  521. weak_alias (__regerror, regerror)
  522. #endif
  523. #ifdef RE_ENABLE_I18N
  524. /* This static array is used for the map to single-byte characters when
  525. UTF-8 is used. Otherwise we would allocate memory just to initialize
  526. it the same all the time. UTF-8 is the preferred encoding so this is
  527. a worthwhile optimization. */
  528. static const bitset_t utf8_sb_map =
  529. {
  530. /* Set the first 128 bits. */
  531. # if 4 * BITSET_WORD_BITS < ASCII_CHARS
  532. # error "bitset_word_t is narrower than 32 bits"
  533. # elif 3 * BITSET_WORD_BITS < ASCII_CHARS
  534. BITSET_WORD_MAX, BITSET_WORD_MAX, BITSET_WORD_MAX,
  535. # elif 2 * BITSET_WORD_BITS < ASCII_CHARS
  536. BITSET_WORD_MAX, BITSET_WORD_MAX,
  537. # elif 1 * BITSET_WORD_BITS < ASCII_CHARS
  538. BITSET_WORD_MAX,
  539. # endif
  540. (BITSET_WORD_MAX
  541. >> (SBC_MAX % BITSET_WORD_BITS == 0
  542. ? 0
  543. : BITSET_WORD_BITS - SBC_MAX % BITSET_WORD_BITS))
  544. };
  545. #endif
  546. static void
  547. free_dfa_content (re_dfa_t *dfa)
  548. {
  549. Idx i, j;
  550. if (dfa->nodes)
  551. for (i = 0; i < dfa->nodes_len; ++i)
  552. free_token (dfa->nodes + i);
  553. re_free (dfa->nexts);
  554. for (i = 0; i < dfa->nodes_len; ++i)
  555. {
  556. if (dfa->eclosures != NULL)
  557. re_node_set_free (dfa->eclosures + i);
  558. if (dfa->inveclosures != NULL)
  559. re_node_set_free (dfa->inveclosures + i);
  560. if (dfa->edests != NULL)
  561. re_node_set_free (dfa->edests + i);
  562. }
  563. re_free (dfa->edests);
  564. re_free (dfa->eclosures);
  565. re_free (dfa->inveclosures);
  566. re_free (dfa->nodes);
  567. if (dfa->state_table)
  568. for (i = 0; i <= dfa->state_hash_mask; ++i)
  569. {
  570. struct re_state_table_entry *entry = dfa->state_table + i;
  571. for (j = 0; j < entry->num; ++j)
  572. {
  573. re_dfastate_t *state = entry->array[j];
  574. free_state (state);
  575. }
  576. re_free (entry->array);
  577. }
  578. re_free (dfa->state_table);
  579. #ifdef RE_ENABLE_I18N
  580. if (dfa->sb_char != utf8_sb_map)
  581. re_free (dfa->sb_char);
  582. #endif
  583. re_free (dfa->subexp_map);
  584. #ifdef DEBUG
  585. re_free (dfa->re_str);
  586. #endif
  587. re_free (dfa);
  588. }
  589. /* Free dynamically allocated space used by PREG. */
  590. void
  591. regfree (preg)
  592. regex_t *preg;
  593. {
  594. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  595. if (BE (dfa != NULL, 1))
  596. free_dfa_content (dfa);
  597. preg->buffer = NULL;
  598. preg->allocated = 0;
  599. re_free (preg->fastmap);
  600. preg->fastmap = NULL;
  601. re_free (preg->translate);
  602. preg->translate = NULL;
  603. }
  604. #ifdef _LIBC
  605. weak_alias (__regfree, regfree)
  606. #endif
  607. /* Entry points compatible with 4.2 BSD regex library. We don't define
  608. them unless specifically requested. */
  609. #if defined _REGEX_RE_COMP || defined _LIBC
  610. /* BSD has one and only one pattern buffer. */
  611. static struct re_pattern_buffer re_comp_buf;
  612. char *
  613. # ifdef _LIBC
  614. /* Make these definitions weak in libc, so POSIX programs can redefine
  615. these names if they don't use our functions, and still use
  616. regcomp/regexec above without link errors. */
  617. weak_function
  618. # endif
  619. re_comp (s)
  620. const char *s;
  621. {
  622. reg_errcode_t ret;
  623. char *fastmap;
  624. if (!s)
  625. {
  626. if (!re_comp_buf.buffer)
  627. return gettext ("No previous regular expression");
  628. return 0;
  629. }
  630. if (re_comp_buf.buffer)
  631. {
  632. fastmap = re_comp_buf.fastmap;
  633. re_comp_buf.fastmap = NULL;
  634. __regfree (&re_comp_buf);
  635. memset (&re_comp_buf, '\0', sizeof (re_comp_buf));
  636. re_comp_buf.fastmap = fastmap;
  637. }
  638. if (re_comp_buf.fastmap == NULL)
  639. {
  640. re_comp_buf.fastmap = (char *) malloc (SBC_MAX);
  641. if (re_comp_buf.fastmap == NULL)
  642. return (char *) gettext (__re_error_msgid
  643. + __re_error_msgid_idx[(int) REG_ESPACE]);
  644. }
  645. /* Since `re_exec' always passes NULL for the `regs' argument, we
  646. don't need to initialize the pattern buffer fields which affect it. */
  647. /* Match anchors at newlines. */
  648. re_comp_buf.newline_anchor = 1;
  649. ret = re_compile_internal (&re_comp_buf, s, strlen (s), re_syntax_options);
  650. if (!ret)
  651. return NULL;
  652. /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
  653. return (char *) gettext (__re_error_msgid + __re_error_msgid_idx[(int) ret]);
  654. }
  655. #ifdef _LIBC
  656. libc_freeres_fn (free_mem)
  657. {
  658. __regfree (&re_comp_buf);
  659. }
  660. #endif
  661. #endif /* _REGEX_RE_COMP */
  662. /* Internal entry point.
  663. Compile the regular expression PATTERN, whose length is LENGTH.
  664. SYNTAX indicate regular expression's syntax. */
  665. static reg_errcode_t
  666. re_compile_internal (regex_t *preg, const char * pattern, size_t length,
  667. reg_syntax_t syntax)
  668. {
  669. reg_errcode_t err = REG_NOERROR;
  670. re_dfa_t *dfa;
  671. re_string_t regexp;
  672. /* Initialize the pattern buffer. */
  673. preg->fastmap_accurate = 0;
  674. preg->syntax = syntax;
  675. preg->not_bol = preg->not_eol = 0;
  676. preg->used = 0;
  677. preg->re_nsub = 0;
  678. preg->can_be_null = 0;
  679. preg->regs_allocated = REGS_UNALLOCATED;
  680. /* Initialize the dfa. */
  681. dfa = (re_dfa_t *) preg->buffer;
  682. if (BE (preg->allocated < sizeof (re_dfa_t), 0))
  683. {
  684. /* If zero allocated, but buffer is non-null, try to realloc
  685. enough space. This loses if buffer's address is bogus, but
  686. that is the user's responsibility. If ->buffer is NULL this
  687. is a simple allocation. */
  688. dfa = re_realloc (preg->buffer, re_dfa_t, 1);
  689. if (dfa == NULL)
  690. return REG_ESPACE;
  691. preg->allocated = sizeof (re_dfa_t);
  692. preg->buffer = (unsigned char *) dfa;
  693. }
  694. preg->used = sizeof (re_dfa_t);
  695. err = init_dfa (dfa, length);
  696. if (BE (err != REG_NOERROR, 0))
  697. {
  698. free_dfa_content (dfa);
  699. preg->buffer = NULL;
  700. preg->allocated = 0;
  701. return err;
  702. }
  703. #ifdef DEBUG
  704. /* Note: length+1 will not overflow since it is checked in init_dfa. */
  705. dfa->re_str = re_malloc (char, length + 1);
  706. strncpy (dfa->re_str, pattern, length + 1);
  707. #endif
  708. __libc_lock_init (dfa->lock);
  709. err = re_string_construct (&regexp, pattern, length, preg->translate,
  710. (syntax & RE_ICASE) != 0, dfa);
  711. if (BE (err != REG_NOERROR, 0))
  712. {
  713. re_compile_internal_free_return:
  714. free_workarea_compile (preg);
  715. re_string_destruct (&regexp);
  716. free_dfa_content (dfa);
  717. preg->buffer = NULL;
  718. preg->allocated = 0;
  719. return err;
  720. }
  721. /* Parse the regular expression, and build a structure tree. */
  722. preg->re_nsub = 0;
  723. dfa->str_tree = parse (&regexp, preg, syntax, &err);
  724. if (BE (dfa->str_tree == NULL, 0))
  725. goto re_compile_internal_free_return;
  726. /* Analyze the tree and create the nfa. */
  727. err = analyze (preg);
  728. if (BE (err != REG_NOERROR, 0))
  729. goto re_compile_internal_free_return;
  730. #ifdef RE_ENABLE_I18N
  731. /* If possible, do searching in single byte encoding to speed things up. */
  732. if (dfa->is_utf8 && !(syntax & RE_ICASE) && preg->translate == NULL)
  733. optimize_utf8 (dfa);
  734. #endif
  735. /* Then create the initial state of the dfa. */
  736. err = create_initial_state (dfa);
  737. /* Release work areas. */
  738. free_workarea_compile (preg);
  739. re_string_destruct (&regexp);
  740. if (BE (err != REG_NOERROR, 0))
  741. {
  742. free_dfa_content (dfa);
  743. preg->buffer = NULL;
  744. preg->allocated = 0;
  745. }
  746. return err;
  747. }
  748. /* Initialize DFA. We use the length of the regular expression PAT_LEN
  749. as the initial length of some arrays. */
  750. static reg_errcode_t
  751. init_dfa (re_dfa_t *dfa, size_t pat_len)
  752. {
  753. __re_size_t table_size;
  754. #ifdef RE_ENABLE_I18N
  755. size_t max_i18n_object_size = MAX (sizeof (wchar_t), sizeof (wctype_t));
  756. #else
  757. size_t max_i18n_object_size = 0;
  758. #endif
  759. size_t max_object_size =
  760. MAX (sizeof (struct re_state_table_entry),
  761. MAX (sizeof (re_token_t),
  762. MAX (sizeof (re_node_set),
  763. MAX (sizeof (regmatch_t),
  764. max_i18n_object_size))));
  765. memset (dfa, '\0', sizeof (re_dfa_t));
  766. /* Force allocation of str_tree_storage the first time. */
  767. dfa->str_tree_storage_idx = BIN_TREE_STORAGE_SIZE;
  768. /* Avoid overflows. The extra "/ 2" is for the table_size doubling
  769. calculation below, and for similar doubling calculations
  770. elsewhere. And it's <= rather than <, because some of the
  771. doubling calculations add 1 afterwards. */
  772. if (BE (SIZE_MAX / max_object_size / 2 <= pat_len, 0))
  773. return REG_ESPACE;
  774. dfa->nodes_alloc = pat_len + 1;
  775. dfa->nodes = re_malloc (re_token_t, dfa->nodes_alloc);
  776. /* table_size = 2 ^ ceil(log pat_len) */
  777. for (table_size = 1; ; table_size <<= 1)
  778. if (table_size > pat_len)
  779. break;
  780. dfa->state_table = calloc (sizeof (struct re_state_table_entry), table_size);
  781. dfa->state_hash_mask = table_size - 1;
  782. dfa->mb_cur_max = MB_CUR_MAX;
  783. #ifdef _LIBC
  784. if (dfa->mb_cur_max == 6
  785. && strcmp (_NL_CURRENT (LC_CTYPE, _NL_CTYPE_CODESET_NAME), "UTF-8") == 0)
  786. dfa->is_utf8 = 1;
  787. dfa->map_notascii = (_NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MAP_TO_NONASCII)
  788. != 0);
  789. #else
  790. if (strcmp (locale_charset (), "UTF-8") == 0)
  791. dfa->is_utf8 = 1;
  792. /* We check exhaustively in the loop below if this charset is a
  793. superset of ASCII. */
  794. dfa->map_notascii = 0;
  795. #endif
  796. #ifdef RE_ENABLE_I18N
  797. if (dfa->mb_cur_max > 1)
  798. {
  799. if (dfa->is_utf8)
  800. dfa->sb_char = (re_bitset_ptr_t) utf8_sb_map;
  801. else
  802. {
  803. int i, j, ch;
  804. dfa->sb_char = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
  805. if (BE (dfa->sb_char == NULL, 0))
  806. return REG_ESPACE;
  807. /* Set the bits corresponding to single byte chars. */
  808. for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
  809. for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
  810. {
  811. wint_t wch = __btowc (ch);
  812. if (wch != WEOF)
  813. dfa->sb_char[i] |= (bitset_word_t) 1 << j;
  814. # ifndef _LIBC
  815. if (isascii (ch) && wch != ch)
  816. dfa->map_notascii = 1;
  817. # endif
  818. }
  819. }
  820. }
  821. #endif
  822. if (BE (dfa->nodes == NULL || dfa->state_table == NULL, 0))
  823. return REG_ESPACE;
  824. return REG_NOERROR;
  825. }
  826. /* Initialize WORD_CHAR table, which indicate which character is
  827. "word". In this case "word" means that it is the word construction
  828. character used by some operators like "\<", "\>", etc. */
  829. static void
  830. internal_function
  831. init_word_char (re_dfa_t *dfa)
  832. {
  833. int i, j, ch;
  834. dfa->word_ops_used = 1;
  835. for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
  836. for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
  837. if (isalnum (ch) || ch == '_')
  838. dfa->word_char[i] |= (bitset_word_t) 1 << j;
  839. }
  840. /* Free the work area which are only used while compiling. */
  841. static void
  842. free_workarea_compile (regex_t *preg)
  843. {
  844. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  845. bin_tree_storage_t *storage, *next;
  846. for (storage = dfa->str_tree_storage; storage; storage = next)
  847. {
  848. next = storage->next;
  849. re_free (storage);
  850. }
  851. dfa->str_tree_storage = NULL;
  852. dfa->str_tree_storage_idx = BIN_TREE_STORAGE_SIZE;
  853. dfa->str_tree = NULL;
  854. re_free (dfa->org_indices);
  855. dfa->org_indices = NULL;
  856. }
  857. /* Create initial states for all contexts. */
  858. static reg_errcode_t
  859. create_initial_state (re_dfa_t *dfa)
  860. {
  861. Idx first, i;
  862. reg_errcode_t err;
  863. re_node_set init_nodes;
  864. /* Initial states have the epsilon closure of the node which is
  865. the first node of the regular expression. */
  866. first = dfa->str_tree->first->node_idx;
  867. dfa->init_node = first;
  868. err = re_node_set_init_copy (&init_nodes, dfa->eclosures + first);
  869. if (BE (err != REG_NOERROR, 0))
  870. return err;
  871. /* The back-references which are in initial states can epsilon transit,
  872. since in this case all of the subexpressions can be null.
  873. Then we add epsilon closures of the nodes which are the next nodes of
  874. the back-references. */
  875. if (dfa->nbackref > 0)
  876. for (i = 0; i < init_nodes.nelem; ++i)
  877. {
  878. Idx node_idx = init_nodes.elems[i];
  879. re_token_type_t type = dfa->nodes[node_idx].type;
  880. Idx clexp_idx;
  881. if (type != OP_BACK_REF)
  882. continue;
  883. for (clexp_idx = 0; clexp_idx < init_nodes.nelem; ++clexp_idx)
  884. {
  885. re_token_t *clexp_node;
  886. clexp_node = dfa->nodes + init_nodes.elems[clexp_idx];
  887. if (clexp_node->type == OP_CLOSE_SUBEXP
  888. && clexp_node->opr.idx == dfa->nodes[node_idx].opr.idx)
  889. break;
  890. }
  891. if (clexp_idx == init_nodes.nelem)
  892. continue;
  893. if (type == OP_BACK_REF)
  894. {
  895. Idx dest_idx = dfa->edests[node_idx].elems[0];
  896. if (!re_node_set_contains (&init_nodes, dest_idx))
  897. {
  898. re_node_set_merge (&init_nodes, dfa->eclosures + dest_idx);
  899. i = 0;
  900. }
  901. }
  902. }
  903. /* It must be the first time to invoke acquire_state. */
  904. dfa->init_state = re_acquire_state_context (&err, dfa, &init_nodes, 0);
  905. /* We don't check ERR here, since the initial state must not be NULL. */
  906. if (BE (dfa->init_state == NULL, 0))
  907. return err;
  908. if (dfa->init_state->has_constraint)
  909. {
  910. dfa->init_state_word = re_acquire_state_context (&err, dfa, &init_nodes,
  911. CONTEXT_WORD);
  912. dfa->init_state_nl = re_acquire_state_context (&err, dfa, &init_nodes,
  913. CONTEXT_NEWLINE);
  914. dfa->init_state_begbuf = re_acquire_state_context (&err, dfa,
  915. &init_nodes,
  916. CONTEXT_NEWLINE
  917. | CONTEXT_BEGBUF);
  918. if (BE (dfa->init_state_word == NULL || dfa->init_state_nl == NULL
  919. || dfa->init_state_begbuf == NULL, 0))
  920. return err;
  921. }
  922. else
  923. dfa->init_state_word = dfa->init_state_nl
  924. = dfa->init_state_begbuf = dfa->init_state;
  925. re_node_set_free (&init_nodes);
  926. return REG_NOERROR;
  927. }
  928. #ifdef RE_ENABLE_I18N
  929. /* If it is possible to do searching in single byte encoding instead of UTF-8
  930. to speed things up, set dfa->mb_cur_max to 1, clear is_utf8 and change
  931. DFA nodes where needed. */
  932. static void
  933. optimize_utf8 (re_dfa_t *dfa)
  934. {
  935. Idx node;
  936. int i;
  937. bool mb_chars = false;
  938. bool has_period = false;
  939. for (node = 0; node < dfa->nodes_len; ++node)
  940. switch (dfa->nodes[node].type)
  941. {
  942. case CHARACTER:
  943. if (dfa->nodes[node].opr.c >= ASCII_CHARS)
  944. mb_chars = true;
  945. break;
  946. case ANCHOR:
  947. switch (dfa->nodes[node].opr.ctx_type)
  948. {
  949. case LINE_FIRST:
  950. case LINE_LAST:
  951. case BUF_FIRST:
  952. case BUF_LAST:
  953. break;
  954. default:
  955. /* Word anchors etc. cannot be handled. It's okay to test
  956. opr.ctx_type since constraints (for all DFA nodes) are
  957. created by ORing one or more opr.ctx_type values. */
  958. return;
  959. }
  960. break;
  961. case OP_PERIOD:
  962. has_period = true;
  963. break;
  964. case OP_BACK_REF:
  965. case OP_ALT:
  966. case END_OF_RE:
  967. case OP_DUP_ASTERISK:
  968. case OP_OPEN_SUBEXP:
  969. case OP_CLOSE_SUBEXP:
  970. break;
  971. case COMPLEX_BRACKET:
  972. return;
  973. case SIMPLE_BRACKET:
  974. /* Just double check. */
  975. {
  976. int rshift = (ASCII_CHARS % BITSET_WORD_BITS == 0
  977. ? 0
  978. : BITSET_WORD_BITS - ASCII_CHARS % BITSET_WORD_BITS);
  979. for (i = ASCII_CHARS / BITSET_WORD_BITS; i < BITSET_WORDS; ++i)
  980. {
  981. if (dfa->nodes[node].opr.sbcset[i] >> rshift != 0)
  982. return;
  983. rshift = 0;
  984. }
  985. }
  986. break;
  987. default:
  988. abort ();
  989. }
  990. if (mb_chars || has_period)
  991. for (node = 0; node < dfa->nodes_len; ++node)
  992. {
  993. if (dfa->nodes[node].type == CHARACTER
  994. && dfa->nodes[node].opr.c >= ASCII_CHARS)
  995. dfa->nodes[node].mb_partial = 0;
  996. else if (dfa->nodes[node].type == OP_PERIOD)
  997. dfa->nodes[node].type = OP_UTF8_PERIOD;
  998. }
  999. /* The search can be in single byte locale. */
  1000. dfa->mb_cur_max = 1;
  1001. dfa->is_utf8 = 0;
  1002. dfa->has_mb_node = dfa->nbackref > 0 || has_period;
  1003. }
  1004. #endif
  1005. /* Analyze the structure tree, and calculate "first", "next", "edest",
  1006. "eclosure", and "inveclosure". */
  1007. static reg_errcode_t
  1008. analyze (regex_t *preg)
  1009. {
  1010. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  1011. reg_errcode_t ret;
  1012. /* Allocate arrays. */
  1013. dfa->nexts = re_malloc (Idx, dfa->nodes_alloc);
  1014. dfa->org_indices = re_malloc (Idx, dfa->nodes_alloc);
  1015. dfa->edests = re_malloc (re_node_set, dfa->nodes_alloc);
  1016. dfa->eclosures = re_malloc (re_node_set, dfa->nodes_alloc);
  1017. if (BE (dfa->nexts == NULL || dfa->org_indices == NULL || dfa->edests == NULL
  1018. || dfa->eclosures == NULL, 0))
  1019. return REG_ESPACE;
  1020. dfa->subexp_map = re_malloc (Idx, preg->re_nsub);
  1021. if (dfa->subexp_map != NULL)
  1022. {
  1023. Idx i;
  1024. for (i = 0; i < preg->re_nsub; i++)
  1025. dfa->subexp_map[i] = i;
  1026. preorder (dfa->str_tree, optimize_subexps, dfa);
  1027. for (i = 0; i < preg->re_nsub; i++)
  1028. if (dfa->subexp_map[i] != i)
  1029. break;
  1030. if (i == preg->re_nsub)
  1031. {
  1032. free (dfa->subexp_map);
  1033. dfa->subexp_map = NULL;
  1034. }
  1035. }
  1036. ret = postorder (dfa->str_tree, lower_subexps, preg);
  1037. if (BE (ret != REG_NOERROR, 0))
  1038. return ret;
  1039. ret = postorder (dfa->str_tree, calc_first, dfa);
  1040. if (BE (ret != REG_NOERROR, 0))
  1041. return ret;
  1042. preorder (dfa->str_tree, calc_next, dfa);
  1043. ret = preorder (dfa->str_tree, link_nfa_nodes, dfa);
  1044. if (BE (ret != REG_NOERROR, 0))
  1045. return ret;
  1046. ret = calc_eclosure (dfa);
  1047. if (BE (ret != REG_NOERROR, 0))
  1048. return ret;
  1049. /* We only need this during the prune_impossible_nodes pass in regexec.c;
  1050. skip it if p_i_n will not run, as calc_inveclosure can be quadratic. */
  1051. if ((!preg->no_sub && preg->re_nsub > 0 && dfa->has_plural_match)
  1052. || dfa->nbackref)
  1053. {
  1054. dfa->inveclosures = re_malloc (re_node_set, dfa->nodes_len);
  1055. if (BE (dfa->inveclosures == NULL, 0))
  1056. return REG_ESPACE;
  1057. ret = calc_inveclosure (dfa);
  1058. }
  1059. return ret;
  1060. }
  1061. /* Our parse trees are very unbalanced, so we cannot use a stack to
  1062. implement parse tree visits. Instead, we use parent pointers and
  1063. some hairy code in these two functions. */
  1064. static reg_errcode_t
  1065. postorder (bin_tree_t *root, reg_errcode_t (fn (void *, bin_tree_t *)),
  1066. void *extra)
  1067. {
  1068. bin_tree_t *node, *prev;
  1069. for (node = root; ; )
  1070. {
  1071. /* Descend down the tree, preferably to the left (or to the right
  1072. if that's the only child). */
  1073. while (node->left || node->right)
  1074. if (node->left)
  1075. node = node->left;
  1076. else
  1077. node = node->right;
  1078. do
  1079. {
  1080. reg_errcode_t err = fn (extra, node);
  1081. if (BE (err != REG_NOERROR, 0))
  1082. return err;
  1083. if (node->parent == NULL)
  1084. return REG_NOERROR;
  1085. prev = node;
  1086. node = node->parent;
  1087. }
  1088. /* Go up while we have a node that is reached from the right. */
  1089. while (node->right == prev || node->right == NULL);
  1090. node = node->right;
  1091. }
  1092. }
  1093. static reg_errcode_t
  1094. preorder (bin_tree_t *root, reg_errcode_t (fn (void *, bin_tree_t *)),
  1095. void *extra)
  1096. {
  1097. bin_tree_t *node;
  1098. for (node = root; ; )
  1099. {
  1100. reg_errcode_t err = fn (extra, node);
  1101. if (BE (err != REG_NOERROR, 0))
  1102. return err;
  1103. /* Go to the left node, or up and to the right. */
  1104. if (node->left)
  1105. node = node->left;
  1106. else
  1107. {
  1108. bin_tree_t *prev = NULL;
  1109. while (node->right == prev || node->right == NULL)
  1110. {
  1111. prev = node;
  1112. node = node->parent;
  1113. if (!node)
  1114. return REG_NOERROR;
  1115. }
  1116. node = node->right;
  1117. }
  1118. }
  1119. }
  1120. /* Optimization pass: if a SUBEXP is entirely contained, strip it and tell
  1121. re_search_internal to map the inner one's opr.idx to this one's. Adjust
  1122. backreferences as well. Requires a preorder visit. */
  1123. static reg_errcode_t
  1124. optimize_subexps (void *extra, bin_tree_t *node)
  1125. {
  1126. re_dfa_t *dfa = (re_dfa_t *) extra;
  1127. if (node->token.type == OP_BACK_REF && dfa->subexp_map)
  1128. {
  1129. int idx = node->token.opr.idx;
  1130. node->token.opr.idx = dfa->subexp_map[idx];
  1131. dfa->used_bkref_map |= 1 << node->token.opr.idx;
  1132. }
  1133. else if (node->token.type == SUBEXP
  1134. && node->left && node->left->token.type == SUBEXP)
  1135. {
  1136. Idx other_idx = node->left->token.opr.idx;
  1137. node->left = node->left->left;
  1138. if (node->left)
  1139. node->left->parent = node;
  1140. dfa->subexp_map[other_idx] = dfa->subexp_map[node->token.opr.idx];
  1141. if (other_idx < BITSET_WORD_BITS)
  1142. dfa->used_bkref_map &= ~((bitset_word_t) 1 << other_idx);
  1143. }
  1144. return REG_NOERROR;
  1145. }
  1146. /* Lowering pass: Turn each SUBEXP node into the appropriate concatenation
  1147. of OP_OPEN_SUBEXP, the body of the SUBEXP (if any) and OP_CLOSE_SUBEXP. */
  1148. static reg_errcode_t
  1149. lower_subexps (void *extra, bin_tree_t *node)
  1150. {
  1151. regex_t *preg = (regex_t *) extra;
  1152. reg_errcode_t err = REG_NOERROR;
  1153. if (node->left && node->left->token.type == SUBEXP)
  1154. {
  1155. node->left = lower_subexp (&err, preg, node->left);
  1156. if (node->left)
  1157. node->left->parent = node;
  1158. }
  1159. if (node->right && node->right->token.type == SUBEXP)
  1160. {
  1161. node->right = lower_subexp (&err, preg, node->right);
  1162. if (node->right)
  1163. node->right->parent = node;
  1164. }
  1165. return err;
  1166. }
  1167. static bin_tree_t *
  1168. lower_subexp (reg_errcode_t *err, regex_t *preg, bin_tree_t *node)
  1169. {
  1170. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  1171. bin_tree_t *body = node->left;
  1172. bin_tree_t *op, *cls, *tree1, *tree;
  1173. if (preg->no_sub
  1174. /* We do not optimize empty subexpressions, because otherwise we may
  1175. have bad CONCAT nodes with NULL children. This is obviously not
  1176. very common, so we do not lose much. An example that triggers
  1177. this case is the sed "script" /\(\)/x. */
  1178. && node->left != NULL
  1179. && (node->token.opr.idx >= BITSET_WORD_BITS
  1180. || !(dfa->used_bkref_map
  1181. & ((bitset_word_t) 1 << node->token.opr.idx))))
  1182. return node->left;
  1183. /* Convert the SUBEXP node to the concatenation of an
  1184. OP_OPEN_SUBEXP, the contents, and an OP_CLOSE_SUBEXP. */
  1185. op = create_tree (dfa, NULL, NULL, OP_OPEN_SUBEXP);
  1186. cls = create_tree (dfa, NULL, NULL, OP_CLOSE_SUBEXP);
  1187. tree1 = body ? create_tree (dfa, body, cls, CONCAT) : cls;
  1188. tree = create_tree (dfa, op, tree1, CONCAT);
  1189. if (BE (tree == NULL || tree1 == NULL || op == NULL || cls == NULL, 0))
  1190. {
  1191. *err = REG_ESPACE;
  1192. return NULL;
  1193. }
  1194. op->token.opr.idx = cls->token.opr.idx = node->token.opr.idx;
  1195. op->token.opt_subexp = cls->token.opt_subexp = node->token.opt_subexp;
  1196. return tree;
  1197. }
  1198. /* Pass 1 in building the NFA: compute FIRST and create unlinked automaton
  1199. nodes. Requires a postorder visit. */
  1200. static reg_errcode_t
  1201. calc_first (void *extra, bin_tree_t *node)
  1202. {
  1203. re_dfa_t *dfa = (re_dfa_t *) extra;
  1204. if (node->token.type == CONCAT)
  1205. {
  1206. node->first = node->left->first;
  1207. node->node_idx = node->left->node_idx;
  1208. }
  1209. else
  1210. {
  1211. node->first = node;
  1212. node->node_idx = re_dfa_add_node (dfa, node->token);
  1213. if (BE (node->node_idx == REG_MISSING, 0))
  1214. return REG_ESPACE;
  1215. if (node->token.type == ANCHOR)
  1216. dfa->nodes[node->node_idx].constraint = node->token.opr.ctx_type;
  1217. }
  1218. return REG_NOERROR;
  1219. }
  1220. /* Pass 2: compute NEXT on the tree. Preorder visit. */
  1221. static reg_errcode_t
  1222. calc_next (void *extra, bin_tree_t *node)
  1223. {
  1224. switch (node->token.type)
  1225. {
  1226. case OP_DUP_ASTERISK:
  1227. node->left->next = node;
  1228. break;
  1229. case CONCAT:
  1230. node->left->next = node->right->first;
  1231. node->right->next = node->next;
  1232. break;
  1233. default:
  1234. if (node->left)
  1235. node->left->next = node->next;
  1236. if (node->right)
  1237. node->right->next = node->next;
  1238. break;
  1239. }
  1240. return REG_NOERROR;
  1241. }
  1242. /* Pass 3: link all DFA nodes to their NEXT node (any order will do). */
  1243. static reg_errcode_t
  1244. link_nfa_nodes (void *extra, bin_tree_t *node)
  1245. {
  1246. re_dfa_t *dfa = (re_dfa_t *) extra;
  1247. Idx idx = node->node_idx;
  1248. reg_errcode_t err = REG_NOERROR;
  1249. switch (node->token.type)
  1250. {
  1251. case CONCAT:
  1252. break;
  1253. case END_OF_RE:
  1254. assert (node->next == NULL);
  1255. break;
  1256. case OP_DUP_ASTERISK:
  1257. case OP_ALT:
  1258. {
  1259. Idx left, right;
  1260. dfa->has_plural_match = 1;
  1261. if (node->left != NULL)
  1262. left = node->left->first->node_idx;
  1263. else
  1264. left = node->next->node_idx;
  1265. if (node->right != NULL)
  1266. right = node->right->first->node_idx;
  1267. else
  1268. right = node->next->node_idx;
  1269. assert (REG_VALID_INDEX (left));
  1270. assert (REG_VALID_INDEX (right));
  1271. err = re_node_set_init_2 (dfa->edests + idx, left, right);
  1272. }
  1273. break;
  1274. case ANCHOR:
  1275. case OP_OPEN_SUBEXP:
  1276. case OP_CLOSE_SUBEXP:
  1277. err = re_node_set_init_1 (dfa->edests + idx, node->next->node_idx);
  1278. break;
  1279. case OP_BACK_REF:
  1280. dfa->nexts[idx] = node->next->node_idx;
  1281. if (node->token.type == OP_BACK_REF)
  1282. re_node_set_init_1 (dfa->edests + idx, dfa->nexts[idx]);
  1283. break;
  1284. default:
  1285. assert (!IS_EPSILON_NODE (node->token.type));
  1286. dfa->nexts[idx] = node->next->node_idx;
  1287. break;
  1288. }
  1289. return err;
  1290. }
  1291. /* Duplicate the epsilon closure of the node ROOT_NODE.
  1292. Note that duplicated nodes have constraint INIT_CONSTRAINT in addition
  1293. to their own constraint. */
  1294. static reg_errcode_t
  1295. internal_function
  1296. duplicate_node_closure (re_dfa_t *dfa, Idx top_org_node, Idx top_clone_node,
  1297. Idx root_node, unsigned int init_constraint)
  1298. {
  1299. Idx org_node, clone_node;
  1300. bool ok;
  1301. unsigned int constraint = init_constraint;
  1302. for (org_node = top_org_node, clone_node = top_clone_node;;)
  1303. {
  1304. Idx org_dest, clone_dest;
  1305. if (dfa->nodes[org_node].type == OP_BACK_REF)
  1306. {
  1307. /* If the back reference epsilon-transit, its destination must
  1308. also have the constraint. Then duplicate the epsilon closure
  1309. of the destination of the back reference, and store it in
  1310. edests of the back reference. */
  1311. org_dest = dfa->nexts[org_node];
  1312. re_node_set_empty (dfa->edests + clone_node);
  1313. clone_dest = duplicate_node (dfa, org_dest, constraint);
  1314. if (BE (clone_dest == REG_MISSING, 0))
  1315. return REG_ESPACE;
  1316. dfa->nexts[clone_node] = dfa->nexts[org_node];
  1317. ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
  1318. if (BE (! ok, 0))
  1319. return REG_ESPACE;
  1320. }
  1321. else if (dfa->edests[org_node].nelem == 0)
  1322. {
  1323. /* In case of the node can't epsilon-transit, don't duplicate the
  1324. destination and store the original destination as the
  1325. destination of the node. */
  1326. dfa->nexts[clone_node] = dfa->nexts[org_node];
  1327. break;
  1328. }
  1329. else if (dfa->edests[org_node].nelem == 1)
  1330. {
  1331. /* In case of the node can epsilon-transit, and it has only one
  1332. destination. */
  1333. org_dest = dfa->edests[org_node].elems[0];
  1334. re_node_set_empty (dfa->edests + clone_node);
  1335. clone_dest = search_duplicated_node (dfa, org_dest, constraint);
  1336. /* If the node is root_node itself, it means the epsilon closure
  1337. has a loop. Then tie it to the destination of the root_node. */
  1338. if (org_node == root_node && clone_node != org_node)
  1339. {
  1340. ok = re_node_set_insert (dfa->edests + clone_node, org_dest);
  1341. if (BE (! ok, 0))
  1342. return REG_ESPACE;
  1343. break;
  1344. }
  1345. /* In case the node has another constraint, append it. */
  1346. constraint |= dfa->nodes[org_node].constraint;
  1347. clone_dest = duplicate_node (dfa, org_dest, constraint);
  1348. if (BE (clone_dest == REG_MISSING, 0))
  1349. return REG_ESPACE;
  1350. ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
  1351. if (BE (! ok, 0))
  1352. return REG_ESPACE;
  1353. }
  1354. else /* dfa->edests[org_node].nelem == 2 */
  1355. {
  1356. /* In case of the node can epsilon-transit, and it has two
  1357. destinations. In the bin_tree_t and DFA, that's '|' and '*'. */
  1358. org_dest = dfa->edests[org_node].elems[0];
  1359. re_node_set_empty (dfa->edests + clone_node);
  1360. /* Search for a duplicated node which satisfies the constraint. */
  1361. clone_dest = search_duplicated_node (dfa, org_dest, constraint);
  1362. if (clone_dest == REG_MISSING)
  1363. {
  1364. /* There is no such duplicated node, create a new one. */
  1365. reg_errcode_t err;
  1366. clone_dest = duplicate_node (dfa, org_dest, constraint);
  1367. if (BE (clone_dest == REG_MISSING, 0))
  1368. return REG_ESPACE;
  1369. ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
  1370. if (BE (! ok, 0))
  1371. return REG_ESPACE;
  1372. err = duplicate_node_closure (dfa, org_dest, clone_dest,
  1373. root_node, constraint);
  1374. if (BE (err != REG_NOERROR, 0))
  1375. return err;
  1376. }
  1377. else
  1378. {
  1379. /* There is a duplicated node which satisfy the constraint,
  1380. use it to avoid infinite loop. */
  1381. ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
  1382. if (BE (! ok, 0))
  1383. return REG_ESPACE;
  1384. }
  1385. org_dest = dfa->edests[org_node].elems[1];
  1386. clone_dest = duplicate_node (dfa, org_dest, constraint);
  1387. if (BE (clone_dest == REG_MISSING, 0))
  1388. return REG_ESPACE;
  1389. ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
  1390. if (BE (! ok, 0))
  1391. return REG_ESPACE;
  1392. }
  1393. org_node = org_dest;
  1394. clone_node = clone_dest;
  1395. }
  1396. return REG_NOERROR;
  1397. }
  1398. /* Search for a node which is duplicated from the node ORG_NODE, and
  1399. satisfies the constraint CONSTRAINT. */
  1400. static Idx
  1401. search_duplicated_node (const re_dfa_t *dfa, Idx org_node,
  1402. unsigned int constraint)
  1403. {
  1404. Idx idx;
  1405. for (idx = dfa->nodes_len - 1; dfa->nodes[idx].duplicated && idx > 0; --idx)
  1406. {
  1407. if (org_node == dfa->org_indices[idx]
  1408. && constraint == dfa->nodes[idx].constraint)
  1409. return idx; /* Found. */
  1410. }
  1411. return REG_MISSING; /* Not found. */
  1412. }
  1413. /* Duplicate the node whose index is ORG_IDX and set the constraint CONSTRAINT.
  1414. Return the index of the new node, or REG_MISSING if insufficient storage is
  1415. available. */
  1416. static Idx
  1417. duplicate_node (re_dfa_t *dfa, Idx org_idx, unsigned int constraint)
  1418. {
  1419. Idx dup_idx = re_dfa_add_node (dfa, dfa->nodes[org_idx]);
  1420. if (BE (dup_idx != REG_MISSING, 1))
  1421. {
  1422. dfa->nodes[dup_idx].constraint = constraint;
  1423. dfa->nodes[dup_idx].constraint |= dfa->nodes[org_idx].constraint;
  1424. dfa->nodes[dup_idx].duplicated = 1;
  1425. /* Store the index of the original node. */
  1426. dfa->org_indices[dup_idx] = org_idx;
  1427. }
  1428. return dup_idx;
  1429. }
  1430. static reg_errcode_t
  1431. calc_inveclosure (re_dfa_t *dfa)
  1432. {
  1433. Idx src, idx;
  1434. bool ok;
  1435. for (idx = 0; idx < dfa->nodes_len; ++idx)
  1436. re_node_set_init_empty (dfa->inveclosures + idx);
  1437. for (src = 0; src < dfa->nodes_len; ++src)
  1438. {
  1439. Idx *elems = dfa->eclosures[src].elems;
  1440. for (idx = 0; idx < dfa->eclosures[src].nelem; ++idx)
  1441. {
  1442. ok = re_node_set_insert_last (dfa->inveclosures + elems[idx], src);
  1443. if (BE (! ok, 0))
  1444. return REG_ESPACE;
  1445. }
  1446. }
  1447. return REG_NOERROR;
  1448. }
  1449. /* Calculate "eclosure" for all the node in DFA. */
  1450. static reg_errcode_t
  1451. calc_eclosure (re_dfa_t *dfa)
  1452. {
  1453. Idx node_idx;
  1454. bool incomplete;
  1455. #ifdef DEBUG
  1456. assert (dfa->nodes_len > 0);
  1457. #endif
  1458. incomplete = false;
  1459. /* For each nodes, calculate epsilon closure. */
  1460. for (node_idx = 0; ; ++node_idx)
  1461. {
  1462. reg_errcode_t err;
  1463. re_node_set eclosure_elem;
  1464. if (node_idx == dfa->nodes_len)
  1465. {
  1466. if (!incomplete)
  1467. break;
  1468. incomplete = false;
  1469. node_idx = 0;
  1470. }
  1471. #ifdef DEBUG
  1472. assert (dfa->eclosures[node_idx].nelem != REG_MISSING);
  1473. #endif
  1474. /* If we have already calculated, skip it. */
  1475. if (dfa->eclosures[node_idx].nelem != 0)
  1476. continue;
  1477. /* Calculate epsilon closure of `node_idx'. */
  1478. err = calc_eclosure_iter (&eclosure_elem, dfa, node_idx, true);
  1479. if (BE (err != REG_NOERROR, 0))
  1480. return err;
  1481. if (dfa->eclosures[node_idx].nelem == 0)
  1482. {
  1483. incomplete = true;
  1484. re_node_set_free (&eclosure_elem);
  1485. }
  1486. }
  1487. return REG_NOERROR;
  1488. }
  1489. /* Calculate epsilon closure of NODE. */
  1490. static reg_errcode_t
  1491. calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa, Idx node, bool root)
  1492. {
  1493. reg_errcode_t err;
  1494. Idx i;
  1495. bool incomplete;
  1496. bool ok;
  1497. re_node_set eclosure;
  1498. incomplete = false;
  1499. err = re_node_set_alloc (&eclosure, dfa->edests[node].nelem + 1);
  1500. if (BE (err != REG_NOERROR, 0))
  1501. return err;
  1502. /* This indicates that we are calculating this node now.
  1503. We reference this value to avoid infinite loop. */
  1504. dfa->eclosures[node].nelem = REG_MISSING;
  1505. /* If the current node has constraints, duplicate all nodes
  1506. since they must inherit the constraints. */
  1507. if (dfa->nodes[node].constraint
  1508. && dfa->edests[node].nelem
  1509. && !dfa->nodes[dfa->edests[node].elems[0]].duplicated)
  1510. {
  1511. err = duplicate_node_closure (dfa, node, node, node,
  1512. dfa->nodes[node].constraint);
  1513. if (BE (err != REG_NOERROR, 0))
  1514. return err;
  1515. }
  1516. /* Expand each epsilon destination nodes. */
  1517. if (IS_EPSILON_NODE(dfa->nodes[node].type))
  1518. for (i = 0; i < dfa->edests[node].nelem; ++i)
  1519. {
  1520. re_node_set eclosure_elem;
  1521. Idx edest = dfa->edests[node].elems[i];
  1522. /* If calculating the epsilon closure of `edest' is in progress,
  1523. return intermediate result. */
  1524. if (dfa->eclosures[edest].nelem == REG_MISSING)
  1525. {
  1526. incomplete = true;
  1527. continue;
  1528. }
  1529. /* If we haven't calculated the epsilon closure of `edest' yet,
  1530. calculate now. Otherwise use calculated epsilon closure. */
  1531. if (dfa->eclosures[edest].nelem == 0)
  1532. {
  1533. err = calc_eclosure_iter (&eclosure_elem, dfa, edest, false);
  1534. if (BE (err != REG_NOERROR, 0))
  1535. return err;
  1536. }
  1537. else
  1538. eclosure_elem = dfa->eclosures[edest];
  1539. /* Merge the epsilon closure of `edest'. */
  1540. re_node_set_merge (&eclosure, &eclosure_elem);
  1541. /* If the epsilon closure of `edest' is incomplete,
  1542. the epsilon closure of this node is also incomplete. */
  1543. if (dfa->eclosures[edest].nelem == 0)
  1544. {
  1545. incomplete = true;
  1546. re_node_set_free (&eclosure_elem);
  1547. }
  1548. }
  1549. /* Epsilon closures include itself. */
  1550. ok = re_node_set_insert (&eclosure, node);
  1551. if (BE (! ok, 0))
  1552. return REG_ESPACE;
  1553. if (incomplete && !root)
  1554. dfa->eclosures[node].nelem = 0;
  1555. else
  1556. dfa->eclosures[node] = eclosure;
  1557. *new_set = eclosure;
  1558. return REG_NOERROR;
  1559. }
  1560. /* Functions for token which are used in the parser. */
  1561. /* Fetch a token from INPUT.
  1562. We must not use this function inside bracket expressions. */
  1563. static void
  1564. internal_function
  1565. fetch_token (re_token_t *result, re_string_t *input, reg_syntax_t syntax)
  1566. {
  1567. re_string_skip_bytes (input, peek_token (result, input, syntax));
  1568. }
  1569. /* Peek a token from INPUT, and return the length of the token.
  1570. We must not use this function inside bracket expressions. */
  1571. static int
  1572. internal_function
  1573. peek_token (re_token_t *token, re_string_t *input, reg_syntax_t syntax)
  1574. {
  1575. unsigned char c;
  1576. if (re_string_eoi (input))
  1577. {
  1578. token->type = END_OF_RE;
  1579. return 0;
  1580. }
  1581. c = re_string_peek_byte (input, 0);
  1582. token->opr.c = c;
  1583. token->word_char = 0;
  1584. #ifdef RE_ENABLE_I18N
  1585. token->mb_partial = 0;
  1586. if (input->mb_cur_max > 1 &&
  1587. !re_string_first_byte (input, re_string_cur_idx (input)))
  1588. {
  1589. token->type = CHARACTER;
  1590. token->mb_partial = 1;
  1591. return 1;
  1592. }
  1593. #endif
  1594. if (c == '\\')
  1595. {
  1596. unsigned char c2;
  1597. if (re_string_cur_idx (input) + 1 >= re_string_length (input))
  1598. {
  1599. token->type = BACK_SLASH;
  1600. return 1;
  1601. }
  1602. c2 = re_string_peek_byte_case (input, 1);
  1603. token->opr.c = c2;
  1604. token->type = CHARACTER;
  1605. #ifdef RE_ENABLE_I18N
  1606. if (input->mb_cur_max > 1)
  1607. {
  1608. wint_t wc = re_string_wchar_at (input,
  1609. re_string_cur_idx (input) + 1);
  1610. token->word_char = IS_WIDE_WORD_CHAR (wc) != 0;
  1611. }
  1612. else
  1613. #endif
  1614. token->word_char = IS_WORD_CHAR (c2) != 0;
  1615. switch (c2)
  1616. {
  1617. case '|':
  1618. if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_NO_BK_VBAR))
  1619. token->type = OP_ALT;
  1620. break;
  1621. case '1': case '2': case '3': case '4': case '5':
  1622. case '6': case '7': case '8': case '9':
  1623. if (!(syntax & RE_NO_BK_REFS))
  1624. {
  1625. token->type = OP_BACK_REF;
  1626. token->opr.idx = c2 - '1';
  1627. }
  1628. break;
  1629. case '<':
  1630. if (!(syntax & RE_NO_GNU_OPS))
  1631. {
  1632. token->type = ANCHOR;
  1633. token->opr.ctx_type = WORD_FIRST;
  1634. }
  1635. break;
  1636. case '>':
  1637. if (!(syntax & RE_NO_GNU_OPS))
  1638. {
  1639. token->type = ANCHOR;
  1640. token->opr.ctx_type = WORD_LAST;
  1641. }
  1642. break;
  1643. case 'b':
  1644. if (!(syntax & RE_NO_GNU_OPS))
  1645. {
  1646. token->type = ANCHOR;
  1647. token->opr.ctx_type = WORD_DELIM;
  1648. }
  1649. break;
  1650. case 'B':
  1651. if (!(syntax & RE_NO_GNU_OPS))
  1652. {
  1653. token->type = ANCHOR;
  1654. token->opr.ctx_type = NOT_WORD_DELIM;
  1655. }
  1656. break;
  1657. case 'w':
  1658. if (!(syntax & RE_NO_GNU_OPS))
  1659. token->type = OP_WORD;
  1660. break;
  1661. case 'W':
  1662. if (!(syntax & RE_NO_GNU_OPS))
  1663. token->type = OP_NOTWORD;
  1664. break;
  1665. case 's':
  1666. if (!(syntax & RE_NO_GNU_OPS))
  1667. token->type = OP_SPACE;
  1668. break;
  1669. case 'S':
  1670. if (!(syntax & RE_NO_GNU_OPS))
  1671. token->type = OP_NOTSPACE;
  1672. break;
  1673. case '`':
  1674. if (!(syntax & RE_NO_GNU_OPS))
  1675. {
  1676. token->type = ANCHOR;
  1677. token->opr.ctx_type = BUF_FIRST;
  1678. }
  1679. break;
  1680. case '\'':
  1681. if (!(syntax & RE_NO_GNU_OPS))
  1682. {
  1683. token->type = ANCHOR;
  1684. token->opr.ctx_type = BUF_LAST;
  1685. }
  1686. break;
  1687. case '(':
  1688. if (!(syntax & RE_NO_BK_PARENS))
  1689. token->type = OP_OPEN_SUBEXP;
  1690. break;
  1691. case ')':
  1692. if (!(syntax & RE_NO_BK_PARENS))
  1693. token->type = OP_CLOSE_SUBEXP;
  1694. break;
  1695. case '+':
  1696. if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM))
  1697. token->type = OP_DUP_PLUS;
  1698. break;
  1699. case '?':
  1700. if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM))
  1701. token->type = OP_DUP_QUESTION;
  1702. break;
  1703. case '{':
  1704. if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES)))
  1705. token->type = OP_OPEN_DUP_NUM;
  1706. break;
  1707. case '}':
  1708. if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES)))
  1709. token->type = OP_CLOSE_DUP_NUM;
  1710. break;
  1711. default:
  1712. break;
  1713. }
  1714. return 2;
  1715. }
  1716. token->type = CHARACTER;
  1717. #ifdef RE_ENABLE_I18N
  1718. if (input->mb_cur_max > 1)
  1719. {
  1720. wint_t wc = re_string_wchar_at (input, re_string_cur_idx (input));
  1721. token->word_char = IS_WIDE_WORD_CHAR (wc) != 0;
  1722. }
  1723. else
  1724. #endif
  1725. token->word_char = IS_WORD_CHAR (token->opr.c);
  1726. switch (c)
  1727. {
  1728. case '\n':
  1729. if (syntax & RE_NEWLINE_ALT)
  1730. token->type = OP_ALT;
  1731. break;
  1732. case '|':
  1733. if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_NO_BK_VBAR))
  1734. token->type = OP_ALT;
  1735. break;
  1736. case '*':
  1737. token->type = OP_DUP_ASTERISK;
  1738. break;
  1739. case '+':
  1740. if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM))
  1741. token->type = OP_DUP_PLUS;
  1742. break;
  1743. case '?':
  1744. if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM))
  1745. token->type = OP_DUP_QUESTION;
  1746. break;
  1747. case '{':
  1748. if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
  1749. token->type = OP_OPEN_DUP_NUM;
  1750. break;
  1751. case '}':
  1752. if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
  1753. token->type = OP_CLOSE_DUP_NUM;
  1754. break;
  1755. case '(':
  1756. if (syntax & RE_NO_BK_PARENS)
  1757. token->type = OP_OPEN_SUBEXP;
  1758. break;
  1759. case ')':
  1760. if (syntax & RE_NO_BK_PARENS)
  1761. token->type = OP_CLOSE_SUBEXP;
  1762. break;
  1763. case '[':
  1764. token->type = OP_OPEN_BRACKET;
  1765. break;
  1766. case '.':
  1767. token->type = OP_PERIOD;
  1768. break;
  1769. case '^':
  1770. if (!(syntax & (RE_CONTEXT_INDEP_ANCHORS | RE_CARET_ANCHORS_HERE)) &&
  1771. re_string_cur_idx (input) != 0)
  1772. {
  1773. char prev = re_string_peek_byte (input, -1);
  1774. if (!(syntax & RE_NEWLINE_ALT) || prev != '\n')
  1775. break;
  1776. }
  1777. token->type = ANCHOR;
  1778. token->opr.ctx_type = LINE_FIRST;
  1779. break;
  1780. case '$':
  1781. if (!(syntax & RE_CONTEXT_INDEP_ANCHORS) &&
  1782. re_string_cur_idx (input) + 1 != re_string_length (input))
  1783. {
  1784. re_token_t next;
  1785. re_string_skip_bytes (input, 1);
  1786. peek_token (&next, input, syntax);
  1787. re_string_skip_bytes (input, -1);
  1788. if (next.type != OP_ALT && next.type != OP_CLOSE_SUBEXP)
  1789. break;
  1790. }
  1791. token->type = ANCHOR;
  1792. token->opr.ctx_type = LINE_LAST;
  1793. break;
  1794. default:
  1795. break;
  1796. }
  1797. return 1;
  1798. }
  1799. /* Peek a token from INPUT, and return the length of the token.
  1800. We must not use this function out of bracket expressions. */
  1801. static int
  1802. internal_function
  1803. peek_token_bracket (re_token_t *token, re_string_t *input, reg_syntax_t syntax)
  1804. {
  1805. unsigned char c;
  1806. if (re_string_eoi (input))
  1807. {
  1808. token->type = END_OF_RE;
  1809. return 0;
  1810. }
  1811. c = re_string_peek_byte (input, 0);
  1812. token->opr.c = c;
  1813. #ifdef RE_ENABLE_I18N
  1814. if (input->mb_cur_max > 1 &&
  1815. !re_string_first_byte (input, re_string_cur_idx (input)))
  1816. {
  1817. token->type = CHARACTER;
  1818. return 1;
  1819. }
  1820. #endif /* RE_ENABLE_I18N */
  1821. if (c == '\\' && (syntax & RE_BACKSLASH_ESCAPE_IN_LISTS)
  1822. && re_string_cur_idx (input) + 1 < re_string_length (input))
  1823. {
  1824. /* In this case, '\' escape a character. */
  1825. unsigned char c2;
  1826. re_string_skip_bytes (input, 1);
  1827. c2 = re_string_peek_byte (input, 0);
  1828. token->opr.c = c2;
  1829. token->type = CHARACTER;
  1830. return 1;
  1831. }
  1832. if (c == '[') /* '[' is a special char in a bracket exps. */
  1833. {
  1834. unsigned char c2;
  1835. int token_len;
  1836. if (re_string_cur_idx (input) + 1 < re_string_length (input))
  1837. c2 = re_string_peek_byte (input, 1);
  1838. else
  1839. c2 = 0;
  1840. token->opr.c = c2;
  1841. token_len = 2;
  1842. switch (c2)
  1843. {
  1844. case '.':
  1845. token->type = OP_OPEN_COLL_ELEM;
  1846. break;
  1847. case '=':
  1848. token->type = OP_OPEN_EQUIV_CLASS;
  1849. break;
  1850. case ':':
  1851. if (syntax & RE_CHAR_CLASSES)
  1852. {
  1853. token->type = OP_OPEN_CHAR_CLASS;
  1854. break;
  1855. }
  1856. /* else fall through. */
  1857. default:
  1858. token->type = CHARACTER;
  1859. token->opr.c = c;
  1860. token_len = 1;
  1861. break;
  1862. }
  1863. return token_len;
  1864. }
  1865. switch (c)
  1866. {
  1867. case '-':
  1868. token->type = OP_CHARSET_RANGE;
  1869. break;
  1870. case ']':
  1871. token->type = OP_CLOSE_BRACKET;
  1872. break;
  1873. case '^':
  1874. token->type = OP_NON_MATCH_LIST;
  1875. break;
  1876. default:
  1877. token->type = CHARACTER;
  1878. }
  1879. return 1;
  1880. }
  1881. /* Functions for parser. */
  1882. /* Entry point of the parser.
  1883. Parse the regular expression REGEXP and return the structure tree.
  1884. If an error is occured, ERR is set by error code, and return NULL.
  1885. This function build the following tree, from regular expression <reg_exp>:
  1886. CAT
  1887. / \
  1888. / \
  1889. <reg_exp> EOR
  1890. CAT means concatenation.
  1891. EOR means end of regular expression. */
  1892. static bin_tree_t *
  1893. parse (re_string_t *regexp, regex_t *preg, reg_syntax_t syntax,
  1894. reg_errcode_t *err)
  1895. {
  1896. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  1897. bin_tree_t *tree, *eor, *root;
  1898. re_token_t current_token;
  1899. dfa->syntax = syntax;
  1900. fetch_token (&current_token, regexp, syntax | RE_CARET_ANCHORS_HERE);
  1901. tree = parse_reg_exp (regexp, preg, &current_token, syntax, 0, err);
  1902. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  1903. return NULL;
  1904. eor = create_tree (dfa, NULL, NULL, END_OF_RE);
  1905. if (tree != NULL)
  1906. root = create_tree (dfa, tree, eor, CONCAT);
  1907. else
  1908. root = eor;
  1909. if (BE (eor == NULL || root == NULL, 0))
  1910. {
  1911. *err = REG_ESPACE;
  1912. return NULL;
  1913. }
  1914. return root;
  1915. }
  1916. /* This function build the following tree, from regular expression
  1917. <branch1>|<branch2>:
  1918. ALT
  1919. / \
  1920. / \
  1921. <branch1> <branch2>
  1922. ALT means alternative, which represents the operator `|'. */
  1923. static bin_tree_t *
  1924. parse_reg_exp (re_string_t *regexp, regex_t *preg, re_token_t *token,
  1925. reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
  1926. {
  1927. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  1928. bin_tree_t *tree, *branch = NULL;
  1929. tree = parse_branch (regexp, preg, token, syntax, nest, err);
  1930. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  1931. return NULL;
  1932. while (token->type == OP_ALT)
  1933. {
  1934. fetch_token (token, regexp, syntax | RE_CARET_ANCHORS_HERE);
  1935. if (token->type != OP_ALT && token->type != END_OF_RE
  1936. && (nest == 0 || token->type != OP_CLOSE_SUBEXP))
  1937. {
  1938. branch = parse_branch (regexp, preg, token, syntax, nest, err);
  1939. if (BE (*err != REG_NOERROR && branch == NULL, 0))
  1940. return NULL;
  1941. }
  1942. else
  1943. branch = NULL;
  1944. tree = create_tree (dfa, tree, branch, OP_ALT);
  1945. if (BE (tree == NULL, 0))
  1946. {
  1947. *err = REG_ESPACE;
  1948. return NULL;
  1949. }
  1950. }
  1951. return tree;
  1952. }
  1953. /* This function build the following tree, from regular expression
  1954. <exp1><exp2>:
  1955. CAT
  1956. / \
  1957. / \
  1958. <exp1> <exp2>
  1959. CAT means concatenation. */
  1960. static bin_tree_t *
  1961. parse_branch (re_string_t *regexp, regex_t *preg, re_token_t *token,
  1962. reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
  1963. {
  1964. bin_tree_t *tree, *expr;
  1965. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  1966. tree = parse_expression (regexp, preg, token, syntax, nest, err);
  1967. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  1968. return NULL;
  1969. while (token->type != OP_ALT && token->type != END_OF_RE
  1970. && (nest == 0 || token->type != OP_CLOSE_SUBEXP))
  1971. {
  1972. expr = parse_expression (regexp, preg, token, syntax, nest, err);
  1973. if (BE (*err != REG_NOERROR && expr == NULL, 0))
  1974. {
  1975. return NULL;
  1976. }
  1977. if (tree != NULL && expr != NULL)
  1978. {
  1979. tree = create_tree (dfa, tree, expr, CONCAT);
  1980. if (tree == NULL)
  1981. {
  1982. *err = REG_ESPACE;
  1983. return NULL;
  1984. }
  1985. }
  1986. else if (tree == NULL)
  1987. tree = expr;
  1988. /* Otherwise expr == NULL, we don't need to create new tree. */
  1989. }
  1990. return tree;
  1991. }
  1992. /* This function build the following tree, from regular expression a*:
  1993. *
  1994. |
  1995. a
  1996. */
  1997. static bin_tree_t *
  1998. parse_expression (re_string_t *regexp, regex_t *preg, re_token_t *token,
  1999. reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
  2000. {
  2001. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  2002. bin_tree_t *tree;
  2003. switch (token->type)
  2004. {
  2005. case CHARACTER:
  2006. tree = create_token_tree (dfa, NULL, NULL, token);
  2007. if (BE (tree == NULL, 0))
  2008. {
  2009. *err = REG_ESPACE;
  2010. return NULL;
  2011. }
  2012. #ifdef RE_ENABLE_I18N
  2013. if (dfa->mb_cur_max > 1)
  2014. {
  2015. while (!re_string_eoi (regexp)
  2016. && !re_string_first_byte (regexp, re_string_cur_idx (regexp)))
  2017. {
  2018. bin_tree_t *mbc_remain;
  2019. fetch_token (token, regexp, syntax);
  2020. mbc_remain = create_token_tree (dfa, NULL, NULL, token);
  2021. tree = create_tree (dfa, tree, mbc_remain, CONCAT);
  2022. if (BE (mbc_remain == NULL || tree == NULL, 0))
  2023. {
  2024. *err = REG_ESPACE;
  2025. return NULL;
  2026. }
  2027. }
  2028. }
  2029. #endif
  2030. break;
  2031. case OP_OPEN_SUBEXP:
  2032. tree = parse_sub_exp (regexp, preg, token, syntax, nest + 1, err);
  2033. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  2034. return NULL;
  2035. break;
  2036. case OP_OPEN_BRACKET:
  2037. tree = parse_bracket_exp (regexp, dfa, token, syntax, err);
  2038. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  2039. return NULL;
  2040. break;
  2041. case OP_BACK_REF:
  2042. if (!BE (dfa->completed_bkref_map & (1 << token->opr.idx), 1))
  2043. {
  2044. *err = REG_ESUBREG;
  2045. return NULL;
  2046. }
  2047. dfa->used_bkref_map |= 1 << token->opr.idx;
  2048. tree = create_token_tree (dfa, NULL, NULL, token);
  2049. if (BE (tree == NULL, 0))
  2050. {
  2051. *err = REG_ESPACE;
  2052. return NULL;
  2053. }
  2054. ++dfa->nbackref;
  2055. dfa->has_mb_node = 1;
  2056. break;
  2057. case OP_OPEN_DUP_NUM:
  2058. if (syntax & RE_CONTEXT_INVALID_DUP)
  2059. {
  2060. *err = REG_BADRPT;
  2061. return NULL;
  2062. }
  2063. /* FALLTHROUGH */
  2064. case OP_DUP_ASTERISK:
  2065. case OP_DUP_PLUS:
  2066. case OP_DUP_QUESTION:
  2067. if (syntax & RE_CONTEXT_INVALID_OPS)
  2068. {
  2069. *err = REG_BADRPT;
  2070. return NULL;
  2071. }
  2072. else if (syntax & RE_CONTEXT_INDEP_OPS)
  2073. {
  2074. fetch_token (token, regexp, syntax);
  2075. return parse_expression (regexp, preg, token, syntax, nest, err);
  2076. }
  2077. /* else fall through */
  2078. case OP_CLOSE_SUBEXP:
  2079. if ((token->type == OP_CLOSE_SUBEXP) &&
  2080. !(syntax & RE_UNMATCHED_RIGHT_PAREN_ORD))
  2081. {
  2082. *err = REG_ERPAREN;
  2083. return NULL;
  2084. }
  2085. /* else fall through */
  2086. case OP_CLOSE_DUP_NUM:
  2087. /* We treat it as a normal character. */
  2088. /* Then we can these characters as normal characters. */
  2089. token->type = CHARACTER;
  2090. /* mb_partial and word_char bits should be initialized already
  2091. by peek_token. */
  2092. tree = create_token_tree (dfa, NULL, NULL, token);
  2093. if (BE (tree == NULL, 0))
  2094. {
  2095. *err = REG_ESPACE;
  2096. return NULL;
  2097. }
  2098. break;
  2099. case ANCHOR:
  2100. if ((token->opr.ctx_type
  2101. & (WORD_DELIM | NOT_WORD_DELIM | WORD_FIRST | WORD_LAST))
  2102. && dfa->word_ops_used == 0)
  2103. init_word_char (dfa);
  2104. if (token->opr.ctx_type == WORD_DELIM
  2105. || token->opr.ctx_type == NOT_WORD_DELIM)
  2106. {
  2107. bin_tree_t *tree_first, *tree_last;
  2108. if (token->opr.ctx_type == WORD_DELIM)
  2109. {
  2110. token->opr.ctx_type = WORD_FIRST;
  2111. tree_first = create_token_tree (dfa, NULL, NULL, token);
  2112. token->opr.ctx_type = WORD_LAST;
  2113. }
  2114. else
  2115. {
  2116. token->opr.ctx_type = INSIDE_WORD;
  2117. tree_first = create_token_tree (dfa, NULL, NULL, token);
  2118. token->opr.ctx_type = INSIDE_NOTWORD;
  2119. }
  2120. tree_last = create_token_tree (dfa, NULL, NULL, token);
  2121. tree = create_tree (dfa, tree_first, tree_last, OP_ALT);
  2122. if (BE (tree_first == NULL || tree_last == NULL || tree == NULL, 0))
  2123. {
  2124. *err = REG_ESPACE;
  2125. return NULL;
  2126. }
  2127. }
  2128. else
  2129. {
  2130. tree = create_token_tree (dfa, NULL, NULL, token);
  2131. if (BE (tree == NULL, 0))
  2132. {
  2133. *err = REG_ESPACE;
  2134. return NULL;
  2135. }
  2136. }
  2137. /* We must return here, since ANCHORs can't be followed
  2138. by repetition operators.
  2139. eg. RE"^*" is invalid or "<ANCHOR(^)><CHAR(*)>",
  2140. it must not be "<ANCHOR(^)><REPEAT(*)>". */
  2141. fetch_token (token, regexp, syntax);
  2142. return tree;
  2143. case OP_PERIOD:
  2144. tree = create_token_tree (dfa, NULL, NULL, token);
  2145. if (BE (tree == NULL, 0))
  2146. {
  2147. *err = REG_ESPACE;
  2148. return NULL;
  2149. }
  2150. if (dfa->mb_cur_max > 1)
  2151. dfa->has_mb_node = 1;
  2152. break;
  2153. case OP_WORD:
  2154. case OP_NOTWORD:
  2155. tree = build_charclass_op (dfa, regexp->trans,
  2156. (const unsigned char *) "alnum",
  2157. (const unsigned char *) "_",
  2158. token->type == OP_NOTWORD, err);
  2159. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  2160. return NULL;
  2161. break;
  2162. case OP_SPACE:
  2163. case OP_NOTSPACE:
  2164. tree = build_charclass_op (dfa, regexp->trans,
  2165. (const unsigned char *) "space",
  2166. (const unsigned char *) "",
  2167. token->type == OP_NOTSPACE, err);
  2168. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  2169. return NULL;
  2170. break;
  2171. case OP_ALT:
  2172. case END_OF_RE:
  2173. return NULL;
  2174. case BACK_SLASH:
  2175. *err = REG_EESCAPE;
  2176. return NULL;
  2177. default:
  2178. /* Must not happen? */
  2179. #ifdef DEBUG
  2180. assert (0);
  2181. #endif
  2182. return NULL;
  2183. }
  2184. fetch_token (token, regexp, syntax);
  2185. while (token->type == OP_DUP_ASTERISK || token->type == OP_DUP_PLUS
  2186. || token->type == OP_DUP_QUESTION || token->type == OP_OPEN_DUP_NUM)
  2187. {
  2188. tree = parse_dup_op (tree, regexp, dfa, token, syntax, err);
  2189. if (BE (*err != REG_NOERROR && tree == NULL, 0))
  2190. return NULL;
  2191. /* In BRE consecutive duplications are not allowed. */
  2192. if ((syntax & RE_CONTEXT_INVALID_DUP)
  2193. && (token->type == OP_DUP_ASTERISK
  2194. || token->type == OP_OPEN_DUP_NUM))
  2195. {
  2196. *err = REG_BADRPT;
  2197. return NULL;
  2198. }
  2199. }
  2200. return tree;
  2201. }
  2202. /* This function build the following tree, from regular expression
  2203. (<reg_exp>):
  2204. SUBEXP
  2205. |
  2206. <reg_exp>
  2207. */
  2208. static bin_tree_t *
  2209. parse_sub_exp (re_string_t *regexp, regex_t *preg, re_token_t *token,
  2210. reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
  2211. {
  2212. re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
  2213. bin_tree_t *tree;
  2214. size_t cur_nsub;
  2215. cur_nsub = preg->re_nsub++;
  2216. fetch_token (token, regexp, syntax | RE_CARET_ANCHORS_HERE);
  2217. /* The subexpression may be a null string. */
  2218. if (token->type == OP_CLOSE_SUBEXP)
  2219. tree = NULL;
  2220. else
  2221. {
  2222. tree = parse_reg_exp (regexp, preg, token, syntax, nest, err);
  2223. if (BE (*err == REG_NOERROR && token->type != OP_CLOSE_SUBEXP, 0))
  2224. *err = REG_EPAREN;
  2225. if (BE (*err != REG_NOERROR, 0))
  2226. return NULL;
  2227. }
  2228. if (cur_nsub <= '9' - '1')
  2229. dfa->completed_bkref_map |= 1 << cur_nsub;
  2230. tree = create_tree (dfa, tree, NULL, SUBEXP);
  2231. if (BE (tree == NULL, 0))
  2232. {
  2233. *err = REG_ESPACE;
  2234. return NULL;
  2235. }
  2236. tree->token.opr.idx = cur_nsub;
  2237. return tree;
  2238. }
  2239. /* This function parse repetition operators like "*", "+", "{1,3}" etc. */
  2240. static bin_tree_t *
  2241. parse_dup_op (bin_tree_t *elem, re_string_t *regexp, re_dfa_t *dfa,
  2242. re_token_t *token, reg_syntax_t syntax, reg_errcode_t *err)
  2243. {
  2244. bin_tree_t *tree = NULL, *old_tree = NULL;
  2245. Idx i, start, end, start_idx = re_string_cur_idx (regexp);
  2246. re_token_t start_token = *token;
  2247. if (token->type == OP_OPEN_DUP_NUM)
  2248. {
  2249. end = 0;
  2250. start = fetch_number (regexp, token, syntax);
  2251. if (start == REG_MISSING)
  2252. {
  2253. if (token->type == CHARACTER && token->opr.c == ',')
  2254. start = 0; /* We treat "{,m}" as "{0,m}". */
  2255. else
  2256. {
  2257. *err = REG_BADBR; /* <re>{} is invalid. */
  2258. return NULL;
  2259. }
  2260. }
  2261. if (BE (start != REG_ERROR, 1))
  2262. {
  2263. /* We treat "{n}" as "{n,n}". */
  2264. end = ((token->type == OP_CLOSE_DUP_NUM) ? start
  2265. : ((token->type == CHARACTER && token->opr.c == ',')
  2266. ? fetch_number (regexp, token, syntax) : REG_ERROR));
  2267. }
  2268. if (BE (start == REG_ERROR || end == REG_ERROR, 0))
  2269. {
  2270. /* Invalid sequence. */
  2271. if (BE (!(syntax & RE_INVALID_INTERVAL_ORD), 0))
  2272. {
  2273. if (token->type == END_OF_RE)
  2274. *err = REG_EBRACE;
  2275. else
  2276. *err = REG_BADBR;
  2277. return NULL;
  2278. }
  2279. /* If the syntax bit is set, rollback. */
  2280. re_string_set_index (regexp, start_idx);
  2281. *token = start_token;
  2282. token->type = CHARACTER;
  2283. /* mb_partial and word_char bits should be already initialized by
  2284. peek_token. */
  2285. return elem;
  2286. }
  2287. if (BE (end != REG_MISSING && start > end, 0))
  2288. {
  2289. /* First number greater than second. */
  2290. *err = REG_BADBR;
  2291. return NULL;
  2292. }
  2293. }
  2294. else
  2295. {
  2296. start = (token->type == OP_DUP_PLUS) ? 1 : 0;
  2297. end = (token->type == OP_DUP_QUESTION) ? 1 : REG_MISSING;
  2298. }
  2299. fetch_token (token, regexp, syntax);
  2300. if (BE (elem == NULL, 0))
  2301. return NULL;
  2302. if (BE (start == 0 && end == 0, 0))
  2303. {
  2304. postorder (elem, free_tree, NULL);
  2305. return NULL;
  2306. }
  2307. /* Extract "<re>{n,m}" to "<re><re>...<re><re>{0,<m-n>}". */
  2308. if (BE (start > 0, 0))
  2309. {
  2310. tree = elem;
  2311. for (i = 2; i <= start; ++i)
  2312. {
  2313. elem = duplicate_tree (elem, dfa);
  2314. tree = create_tree (dfa, tree, elem, CONCAT);
  2315. if (BE (elem == NULL || tree == NULL, 0))
  2316. goto parse_dup_op_espace;
  2317. }
  2318. if (start == end)
  2319. return tree;
  2320. /* Duplicate ELEM before it is marked optional. */
  2321. elem = duplicate_tree (elem, dfa);
  2322. old_tree = tree;
  2323. }
  2324. else
  2325. old_tree = NULL;
  2326. if (elem->token.type == SUBEXP)
  2327. postorder (elem, mark_opt_subexp, (void *) (long) elem->token.opr.idx);
  2328. tree = create_tree (dfa, elem, NULL,
  2329. (end == REG_MISSING ? OP_DUP_ASTERISK : OP_ALT));
  2330. if (BE (tree == NULL, 0))
  2331. goto parse_dup_op_espace;
  2332. /* This loop is actually executed only when end != REG_MISSING,
  2333. to rewrite <re>{0,n} as (<re>(<re>...<re>?)?)?... We have
  2334. already created the start+1-th copy. */
  2335. if ((Idx) -1 < 0 || end != REG_MISSING)
  2336. for (i = start + 2; i <= end; ++i)
  2337. {
  2338. elem = duplicate_tree (elem, dfa);
  2339. tree = create_tree (dfa, tree, elem, CONCAT);
  2340. if (BE (elem == NULL || tree == NULL, 0))
  2341. goto parse_dup_op_espace;
  2342. tree = create_tree (dfa, tree, NULL, OP_ALT);
  2343. if (BE (tree == NULL, 0))
  2344. goto parse_dup_op_espace;
  2345. }
  2346. if (old_tree)
  2347. tree = create_tree (dfa, old_tree, tree, CONCAT);
  2348. return tree;
  2349. parse_dup_op_espace:
  2350. *err = REG_ESPACE;
  2351. return NULL;
  2352. }
  2353. /* Size of the names for collating symbol/equivalence_class/character_class.
  2354. I'm not sure, but maybe enough. */
  2355. #define BRACKET_NAME_BUF_SIZE 32
  2356. #ifndef _LIBC
  2357. /* Local function for parse_bracket_exp only used in case of NOT _LIBC.
  2358. Build the range expression which starts from START_ELEM, and ends
  2359. at END_ELEM. The result are written to MBCSET and SBCSET.
  2360. RANGE_ALLOC is the allocated size of mbcset->range_starts, and
  2361. mbcset->range_ends, is a pointer argument sinse we may
  2362. update it. */
  2363. static reg_errcode_t
  2364. internal_function
  2365. # ifdef RE_ENABLE_I18N
  2366. build_range_exp (bitset_t sbcset, re_charset_t *mbcset, Idx *range_alloc,
  2367. bracket_elem_t *start_elem, bracket_elem_t *end_elem)
  2368. # else /* not RE_ENABLE_I18N */
  2369. build_range_exp (bitset_t sbcset, bracket_elem_t *start_elem,
  2370. bracket_elem_t *end_elem)
  2371. # endif /* not RE_ENABLE_I18N */
  2372. {
  2373. unsigned int start_ch, end_ch;
  2374. /* Equivalence Classes and Character Classes can't be a range start/end. */
  2375. if (BE (start_elem->type == EQUIV_CLASS || start_elem->type == CHAR_CLASS
  2376. || end_elem->type == EQUIV_CLASS || end_elem->type == CHAR_CLASS,
  2377. 0))
  2378. return REG_ERANGE;
  2379. /* We can handle no multi character collating elements without libc
  2380. support. */
  2381. if (BE ((start_elem->type == COLL_SYM
  2382. && strlen ((char *) start_elem->opr.name) > 1)
  2383. || (end_elem->type == COLL_SYM
  2384. && strlen ((char *) end_elem->opr.name) > 1), 0))
  2385. return REG_ECOLLATE;
  2386. # ifdef RE_ENABLE_I18N
  2387. {
  2388. wchar_t wc;
  2389. wint_t start_wc;
  2390. wint_t end_wc;
  2391. wchar_t cmp_buf[6] = {L'\0', L'\0', L'\0', L'\0', L'\0', L'\0'};
  2392. start_ch = ((start_elem->type == SB_CHAR) ? start_elem->opr.ch
  2393. : ((start_elem->type == COLL_SYM) ? start_elem->opr.name[0]
  2394. : 0));
  2395. end_ch = ((end_elem->type == SB_CHAR) ? end_elem->opr.ch
  2396. : ((end_elem->type == COLL_SYM) ? end_elem->opr.name[0]
  2397. : 0));
  2398. start_wc = ((start_elem->type == SB_CHAR || start_elem->type == COLL_SYM)
  2399. ? __btowc (start_ch) : start_elem->opr.wch);
  2400. end_wc = ((end_elem->type == SB_CHAR || end_elem->type == COLL_SYM)
  2401. ? __btowc (end_ch) : end_elem->opr.wch);
  2402. if (start_wc == WEOF || end_wc == WEOF)
  2403. return REG_ECOLLATE;
  2404. cmp_buf[0] = start_wc;
  2405. cmp_buf[4] = end_wc;
  2406. if (wcscoll (cmp_buf, cmp_buf + 4) > 0)
  2407. return REG_ERANGE;
  2408. /* Got valid collation sequence values, add them as a new entry.
  2409. However, for !_LIBC we have no collation elements: if the
  2410. character set is single byte, the single byte character set
  2411. that we build below suffices. parse_bracket_exp passes
  2412. no MBCSET if dfa->mb_cur_max == 1. */
  2413. if (mbcset)
  2414. {
  2415. /* Check the space of the arrays. */
  2416. if (BE (*range_alloc == mbcset->nranges, 0))
  2417. {
  2418. /* There is not enough space, need realloc. */
  2419. wchar_t *new_array_start, *new_array_end;
  2420. Idx new_nranges;
  2421. /* +1 in case of mbcset->nranges is 0. */
  2422. new_nranges = 2 * mbcset->nranges + 1;
  2423. /* Use realloc since mbcset->range_starts and mbcset->range_ends
  2424. are NULL if *range_alloc == 0. */
  2425. new_array_start = re_realloc (mbcset->range_starts, wchar_t,
  2426. new_nranges);
  2427. new_array_end = re_realloc (mbcset->range_ends, wchar_t,
  2428. new_nranges);
  2429. if (BE (new_array_start == NULL || new_array_end == NULL, 0))
  2430. return REG_ESPACE;
  2431. mbcset->range_starts = new_array_start;
  2432. mbcset->range_ends = new_array_end;
  2433. *range_alloc = new_nranges;
  2434. }
  2435. mbcset->range_starts[mbcset->nranges] = start_wc;
  2436. mbcset->range_ends[mbcset->nranges++] = end_wc;
  2437. }
  2438. /* Build the table for single byte characters. */
  2439. for (wc = 0; wc < SBC_MAX; ++wc)
  2440. {
  2441. cmp_buf[2] = wc;
  2442. if (wcscoll (cmp_buf, cmp_buf + 2) <= 0
  2443. && wcscoll (cmp_buf + 2, cmp_buf + 4) <= 0)
  2444. bitset_set (sbcset, wc);
  2445. }
  2446. }
  2447. # else /* not RE_ENABLE_I18N */
  2448. {
  2449. unsigned int ch;
  2450. start_ch = ((start_elem->type == SB_CHAR ) ? start_elem->opr.ch
  2451. : ((start_elem->type == COLL_SYM) ? start_elem->opr.name[0]
  2452. : 0));
  2453. end_ch = ((end_elem->type == SB_CHAR ) ? end_elem->opr.ch
  2454. : ((end_elem->type == COLL_SYM) ? end_elem->opr.name[0]
  2455. : 0));
  2456. if (start_ch > end_ch)
  2457. return REG_ERANGE;
  2458. /* Build the table for single byte characters. */
  2459. for (ch = 0; ch < SBC_MAX; ++ch)
  2460. if (start_ch <= ch && ch <= end_ch)
  2461. bitset_set (sbcset, ch);
  2462. }
  2463. # endif /* not RE_ENABLE_I18N */
  2464. return REG_NOERROR;
  2465. }
  2466. #endif /* not _LIBC */
  2467. #ifndef _LIBC
  2468. /* Helper function for parse_bracket_exp only used in case of NOT _LIBC..
  2469. Build the collating element which is represented by NAME.
  2470. The result are written to MBCSET and SBCSET.
  2471. COLL_SYM_ALLOC is the allocated size of mbcset->coll_sym, is a
  2472. pointer argument since we may update it. */
  2473. static reg_errcode_t
  2474. internal_function
  2475. build_collating_symbol (bitset_t sbcset,
  2476. # ifdef RE_ENABLE_I18N
  2477. re_charset_t *mbcset, Idx *coll_sym_alloc,
  2478. # endif
  2479. const unsigned char *name)
  2480. {
  2481. size_t name_len = strlen ((const char *) name);
  2482. if (BE (name_len != 1, 0))
  2483. return REG_ECOLLATE;
  2484. else
  2485. {
  2486. bitset_set (sbcset, name[0]);
  2487. return REG_NOERROR;
  2488. }
  2489. }
  2490. #endif /* not _LIBC */
  2491. /* This function parse bracket expression like "[abc]", "[a-c]",
  2492. "[[.a-a.]]" etc. */
  2493. static bin_tree_t *
  2494. parse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa, re_token_t *token,
  2495. reg_syntax_t syntax, reg_errcode_t *err)
  2496. {
  2497. #ifdef _LIBC
  2498. const unsigned char *collseqmb;
  2499. const char *collseqwc;
  2500. uint32_t nrules;
  2501. int32_t table_size;
  2502. const int32_t *symb_table;
  2503. const unsigned char *extra;
  2504. /* Local function for parse_bracket_exp used in _LIBC environement.
  2505. Seek the collating symbol entry correspondings to NAME.
  2506. Return the index of the symbol in the SYMB_TABLE. */
  2507. auto inline int32_t
  2508. __attribute ((always_inline))
  2509. seek_collating_symbol_entry (name, name_len)
  2510. const unsigned char *name;
  2511. size_t name_len;
  2512. {
  2513. int32_t hash = elem_hash ((const char *) name, name_len);
  2514. int32_t elem = hash % table_size;
  2515. if (symb_table[2 * elem] != 0)
  2516. {
  2517. int32_t second = hash % (table_size - 2) + 1;
  2518. do
  2519. {
  2520. /* First compare the hashing value. */
  2521. if (symb_table[2 * elem] == hash
  2522. /* Compare the length of the name. */
  2523. && name_len == extra[symb_table[2 * elem + 1]]
  2524. /* Compare the name. */
  2525. && memcmp (name, &extra[symb_table[2 * elem + 1] + 1],
  2526. name_len) == 0)
  2527. {
  2528. /* Yep, this is the entry. */
  2529. break;
  2530. }
  2531. /* Next entry. */
  2532. elem += second;
  2533. }
  2534. while (symb_table[2 * elem] != 0);
  2535. }
  2536. return elem;
  2537. }
  2538. /* Local function for parse_bracket_exp used in _LIBC environement.
  2539. Look up the collation sequence value of BR_ELEM.
  2540. Return the value if succeeded, UINT_MAX otherwise. */
  2541. auto inline unsigned int
  2542. __attribute ((always_inline))
  2543. lookup_collation_sequence_value (br_elem)
  2544. bracket_elem_t *br_elem;
  2545. {
  2546. if (br_elem->type == SB_CHAR)
  2547. {
  2548. /*
  2549. if (MB_CUR_MAX == 1)
  2550. */
  2551. if (nrules == 0)
  2552. return collseqmb[br_elem->opr.ch];
  2553. else
  2554. {
  2555. wint_t wc = __btowc (br_elem->opr.ch);
  2556. return __collseq_table_lookup (collseqwc, wc);
  2557. }
  2558. }
  2559. else if (br_elem->type == MB_CHAR)
  2560. {
  2561. return __collseq_table_lookup (collseqwc, br_elem->opr.wch);
  2562. }
  2563. else if (br_elem->type == COLL_SYM)
  2564. {
  2565. size_t sym_name_len = strlen ((char *) br_elem->opr.name);
  2566. if (nrules != 0)
  2567. {
  2568. int32_t elem, idx;
  2569. elem = seek_collating_symbol_entry (br_elem->opr.name,
  2570. sym_name_len);
  2571. if (symb_table[2 * elem] != 0)
  2572. {
  2573. /* We found the entry. */
  2574. idx = symb_table[2 * elem + 1];
  2575. /* Skip the name of collating element name. */
  2576. idx += 1 + extra[idx];
  2577. /* Skip the byte sequence of the collating element. */
  2578. idx += 1 + extra[idx];
  2579. /* Adjust for the alignment. */
  2580. idx = (idx + 3) & ~3;
  2581. /* Skip the multibyte collation sequence value. */
  2582. idx += sizeof (unsigned int);
  2583. /* Skip the wide char sequence of the collating element. */
  2584. idx += sizeof (unsigned int) *
  2585. (1 + *(unsigned int *) (extra + idx));
  2586. /* Return the collation sequence value. */
  2587. return *(unsigned int *) (extra + idx);
  2588. }
  2589. else if (symb_table[2 * elem] == 0 && sym_name_len == 1)
  2590. {
  2591. /* No valid character. Match it as a single byte
  2592. character. */
  2593. return collseqmb[br_elem->opr.name[0]];
  2594. }
  2595. }
  2596. else if (sym_name_len == 1)
  2597. return collseqmb[br_elem->opr.name[0]];
  2598. }
  2599. return UINT_MAX;
  2600. }
  2601. /* Local function for parse_bracket_exp used in _LIBC environement.
  2602. Build the range expression which starts from START_ELEM, and ends
  2603. at END_ELEM. The result are written to MBCSET and SBCSET.
  2604. RANGE_ALLOC is the allocated size of mbcset->range_starts, and
  2605. mbcset->range_ends, is a pointer argument sinse we may
  2606. update it. */
  2607. auto inline reg_errcode_t
  2608. __attribute ((always_inline))
  2609. build_range_exp (sbcset, mbcset, range_alloc, start_elem, end_elem)
  2610. re_charset_t *mbcset;
  2611. Idx *range_alloc;
  2612. bitset_t sbcset;
  2613. bracket_elem_t *start_elem, *end_elem;
  2614. {
  2615. unsigned int ch;
  2616. uint32_t start_collseq;
  2617. uint32_t end_collseq;
  2618. /* Equivalence Classes and Character Classes can't be a range
  2619. start/end. */
  2620. if (BE (start_elem->type == EQUIV_CLASS || start_elem->type == CHAR_CLASS
  2621. || end_elem->type == EQUIV_CLASS || end_elem->type == CHAR_CLASS,
  2622. 0))
  2623. return REG_ERANGE;
  2624. start_collseq = lookup_collation_sequence_value (start_elem);
  2625. end_collseq = lookup_collation_sequence_value (end_elem);
  2626. /* Check start/end collation sequence values. */
  2627. if (BE (start_collseq == UINT_MAX || end_collseq == UINT_MAX, 0))
  2628. return REG_ECOLLATE;
  2629. if (BE ((syntax & RE_NO_EMPTY_RANGES) && start_collseq > end_collseq, 0))
  2630. return REG_ERANGE;
  2631. /* Got valid collation sequence values, add them as a new entry.
  2632. However, if we have no collation elements, and the character set
  2633. is single byte, the single byte character set that we
  2634. build below suffices. */
  2635. if (nrules > 0 || dfa->mb_cur_max > 1)
  2636. {
  2637. /* Check the space of the arrays. */
  2638. if (BE (*range_alloc == mbcset->nranges, 0))
  2639. {
  2640. /* There is not enough space, need realloc. */
  2641. uint32_t *new_array_start;
  2642. uint32_t *new_array_end;
  2643. Idx new_nranges;
  2644. /* +1 in case of mbcset->nranges is 0. */
  2645. new_nranges = 2 * mbcset->nranges + 1;
  2646. new_array_start = re_realloc (mbcset->range_starts, uint32_t,
  2647. new_nranges);
  2648. new_array_end = re_realloc (mbcset->range_ends, uint32_t,
  2649. new_nranges);
  2650. if (BE (new_array_start == NULL || new_array_end == NULL, 0))
  2651. return REG_ESPACE;
  2652. mbcset->range_starts = new_array_start;
  2653. mbcset->range_ends = new_array_end;
  2654. *range_alloc = new_nranges;
  2655. }
  2656. mbcset->range_starts[mbcset->nranges] = start_collseq;
  2657. mbcset->range_ends[mbcset->nranges++] = end_collseq;
  2658. }
  2659. /* Build the table for single byte characters. */
  2660. for (ch = 0; ch < SBC_MAX; ch++)
  2661. {
  2662. uint32_t ch_collseq;
  2663. /*
  2664. if (MB_CUR_MAX == 1)
  2665. */
  2666. if (nrules == 0)
  2667. ch_collseq = collseqmb[ch];
  2668. else
  2669. ch_collseq = __collseq_table_lookup (collseqwc, __btowc (ch));
  2670. if (start_collseq <= ch_collseq && ch_collseq <= end_collseq)
  2671. bitset_set (sbcset, ch);
  2672. }
  2673. return REG_NOERROR;
  2674. }
  2675. /* Local function for parse_bracket_exp used in _LIBC environement.
  2676. Build the collating element which is represented by NAME.
  2677. The result are written to MBCSET and SBCSET.
  2678. COLL_SYM_ALLOC is the allocated size of mbcset->coll_sym, is a
  2679. pointer argument sinse we may update it. */
  2680. auto inline reg_errcode_t
  2681. __attribute ((always_inline))
  2682. build_collating_symbol (sbcset, mbcset, coll_sym_alloc, name)
  2683. re_charset_t *mbcset;
  2684. Idx *coll_sym_alloc;
  2685. bitset_t sbcset;
  2686. const unsigned char *name;
  2687. {
  2688. int32_t elem, idx;
  2689. size_t name_len = strlen ((const char *) name);
  2690. if (nrules != 0)
  2691. {
  2692. elem = seek_collating_symbol_entry (name, name_len);
  2693. if (symb_table[2 * elem] != 0)
  2694. {
  2695. /* We found the entry. */
  2696. idx = symb_table[2 * elem + 1];
  2697. /* Skip the name of collating element name. */
  2698. idx += 1 + extra[idx];
  2699. }
  2700. else if (symb_table[2 * elem] == 0 && name_len == 1)
  2701. {
  2702. /* No valid character, treat it as a normal
  2703. character. */
  2704. bitset_set (sbcset, name[0]);
  2705. return REG_NOERROR;
  2706. }
  2707. else
  2708. return REG_ECOLLATE;
  2709. /* Got valid collation sequence, add it as a new entry. */
  2710. /* Check the space of the arrays. */
  2711. if (BE (*coll_sym_alloc == mbcset->ncoll_syms, 0))
  2712. {
  2713. /* Not enough, realloc it. */
  2714. /* +1 in case of mbcset->ncoll_syms is 0. */
  2715. Idx new_coll_sym_alloc = 2 * mbcset->ncoll_syms + 1;
  2716. /* Use realloc since mbcset->coll_syms is NULL
  2717. if *alloc == 0. */
  2718. int32_t *new_coll_syms = re_realloc (mbcset->coll_syms, int32_t,
  2719. new_coll_sym_alloc);
  2720. if (BE (new_coll_syms == NULL, 0))
  2721. return REG_ESPACE;
  2722. mbcset->coll_syms = new_coll_syms;
  2723. *coll_sym_alloc = new_coll_sym_alloc;
  2724. }
  2725. mbcset->coll_syms[mbcset->ncoll_syms++] = idx;
  2726. return REG_NOERROR;
  2727. }
  2728. else
  2729. {
  2730. if (BE (name_len != 1, 0))
  2731. return REG_ECOLLATE;
  2732. else
  2733. {
  2734. bitset_set (sbcset, name[0]);
  2735. return REG_NOERROR;
  2736. }
  2737. }
  2738. }
  2739. #endif
  2740. re_token_t br_token;
  2741. re_bitset_ptr_t sbcset;
  2742. #ifdef RE_ENABLE_I18N
  2743. re_charset_t *mbcset;
  2744. Idx coll_sym_alloc = 0, range_alloc = 0, mbchar_alloc = 0;
  2745. Idx equiv_class_alloc = 0, char_class_alloc = 0;
  2746. #endif /* not RE_ENABLE_I18N */
  2747. bool non_match = false;
  2748. bin_tree_t *work_tree;
  2749. int token_len;
  2750. bool first_round = true;
  2751. #ifdef _LIBC
  2752. collseqmb = (const unsigned char *)
  2753. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQMB);
  2754. nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
  2755. if (nrules)
  2756. {
  2757. /*
  2758. if (MB_CUR_MAX > 1)
  2759. */
  2760. collseqwc = _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQWC);
  2761. table_size = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_SYMB_HASH_SIZEMB);
  2762. symb_table = (const int32_t *) _NL_CURRENT (LC_COLLATE,
  2763. _NL_COLLATE_SYMB_TABLEMB);
  2764. extra = (const unsigned char *) _NL_CURRENT (LC_COLLATE,
  2765. _NL_COLLATE_SYMB_EXTRAMB);
  2766. }
  2767. #endif
  2768. sbcset = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
  2769. #ifdef RE_ENABLE_I18N
  2770. mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
  2771. #endif /* RE_ENABLE_I18N */
  2772. #ifdef RE_ENABLE_I18N
  2773. if (BE (sbcset == NULL || mbcset == NULL, 0))
  2774. #else
  2775. if (BE (sbcset == NULL, 0))
  2776. #endif /* RE_ENABLE_I18N */
  2777. {
  2778. *err = REG_ESPACE;
  2779. return NULL;
  2780. }
  2781. token_len = peek_token_bracket (token, regexp, syntax);
  2782. if (BE (token->type == END_OF_RE, 0))
  2783. {
  2784. *err = REG_BADPAT;
  2785. goto parse_bracket_exp_free_return;
  2786. }
  2787. if (token->type == OP_NON_MATCH_LIST)
  2788. {
  2789. #ifdef RE_ENABLE_I18N
  2790. mbcset->non_match = 1;
  2791. #endif /* not RE_ENABLE_I18N */
  2792. non_match = true;
  2793. if (syntax & RE_HAT_LISTS_NOT_NEWLINE)
  2794. bitset_set (sbcset, '\n');
  2795. re_string_skip_bytes (regexp, token_len); /* Skip a token. */
  2796. token_len = peek_token_bracket (token, regexp, syntax);
  2797. if (BE (token->type == END_OF_RE, 0))
  2798. {
  2799. *err = REG_BADPAT;
  2800. goto parse_bracket_exp_free_return;
  2801. }
  2802. }
  2803. /* We treat the first ']' as a normal character. */
  2804. if (token->type == OP_CLOSE_BRACKET)
  2805. token->type = CHARACTER;
  2806. while (1)
  2807. {
  2808. bracket_elem_t start_elem, end_elem;
  2809. unsigned char start_name_buf[BRACKET_NAME_BUF_SIZE];
  2810. unsigned char end_name_buf[BRACKET_NAME_BUF_SIZE];
  2811. reg_errcode_t ret;
  2812. int token_len2 = 0;
  2813. bool is_range_exp = false;
  2814. re_token_t token2;
  2815. start_elem.opr.name = start_name_buf;
  2816. ret = parse_bracket_element (&start_elem, regexp, token, token_len, dfa,
  2817. syntax, first_round);
  2818. if (BE (ret != REG_NOERROR, 0))
  2819. {
  2820. *err = ret;
  2821. goto parse_bracket_exp_free_return;
  2822. }
  2823. first_round = false;
  2824. /* Get information about the next token. We need it in any case. */
  2825. token_len = peek_token_bracket (token, regexp, syntax);
  2826. /* Do not check for ranges if we know they are not allowed. */
  2827. if (start_elem.type != CHAR_CLASS && start_elem.type != EQUIV_CLASS)
  2828. {
  2829. if (BE (token->type == END_OF_RE, 0))
  2830. {
  2831. *err = REG_EBRACK;
  2832. goto parse_bracket_exp_free_return;
  2833. }
  2834. if (token->type == OP_CHARSET_RANGE)
  2835. {
  2836. re_string_skip_bytes (regexp, token_len); /* Skip '-'. */
  2837. token_len2 = peek_token_bracket (&token2, regexp, syntax);
  2838. if (BE (token2.type == END_OF_RE, 0))
  2839. {
  2840. *err = REG_EBRACK;
  2841. goto parse_bracket_exp_free_return;
  2842. }
  2843. if (token2.type == OP_CLOSE_BRACKET)
  2844. {
  2845. /* We treat the last '-' as a normal character. */
  2846. re_string_skip_bytes (regexp, -token_len);
  2847. token->type = CHARACTER;
  2848. }
  2849. else
  2850. is_range_exp = true;
  2851. }
  2852. }
  2853. if (is_range_exp == true)
  2854. {
  2855. end_elem.opr.name = end_name_buf;
  2856. ret = parse_bracket_element (&end_elem, regexp, &token2, token_len2,
  2857. dfa, syntax, true);
  2858. if (BE (ret != REG_NOERROR, 0))
  2859. {
  2860. *err = ret;
  2861. goto parse_bracket_exp_free_return;
  2862. }
  2863. token_len = peek_token_bracket (token, regexp, syntax);
  2864. #ifdef _LIBC
  2865. *err = build_range_exp (sbcset, mbcset, &range_alloc,
  2866. &start_elem, &end_elem);
  2867. #else
  2868. # ifdef RE_ENABLE_I18N
  2869. *err = build_range_exp (sbcset,
  2870. dfa->mb_cur_max > 1 ? mbcset : NULL,
  2871. &range_alloc, &start_elem, &end_elem);
  2872. # else
  2873. *err = build_range_exp (sbcset, &start_elem, &end_elem);
  2874. # endif
  2875. #endif /* RE_ENABLE_I18N */
  2876. if (BE (*err != REG_NOERROR, 0))
  2877. goto parse_bracket_exp_free_return;
  2878. }
  2879. else
  2880. {
  2881. switch (start_elem.type)
  2882. {
  2883. case SB_CHAR:
  2884. bitset_set (sbcset, start_elem.opr.ch);
  2885. break;
  2886. #ifdef RE_ENABLE_I18N
  2887. case MB_CHAR:
  2888. /* Check whether the array has enough space. */
  2889. if (BE (mbchar_alloc == mbcset->nmbchars, 0))
  2890. {
  2891. wchar_t *new_mbchars;
  2892. /* Not enough, realloc it. */
  2893. /* +1 in case of mbcset->nmbchars is 0. */
  2894. mbchar_alloc = 2 * mbcset->nmbchars + 1;
  2895. /* Use realloc since array is NULL if *alloc == 0. */
  2896. new_mbchars = re_realloc (mbcset->mbchars, wchar_t,
  2897. mbchar_alloc);
  2898. if (BE (new_mbchars == NULL, 0))
  2899. goto parse_bracket_exp_espace;
  2900. mbcset->mbchars = new_mbchars;
  2901. }
  2902. mbcset->mbchars[mbcset->nmbchars++] = start_elem.opr.wch;
  2903. break;
  2904. #endif /* RE_ENABLE_I18N */
  2905. case EQUIV_CLASS:
  2906. *err = build_equiv_class (sbcset,
  2907. #ifdef RE_ENABLE_I18N
  2908. mbcset, &equiv_class_alloc,
  2909. #endif /* RE_ENABLE_I18N */
  2910. start_elem.opr.name);
  2911. if (BE (*err != REG_NOERROR, 0))
  2912. goto parse_bracket_exp_free_return;
  2913. break;
  2914. case COLL_SYM:
  2915. *err = build_collating_symbol (sbcset,
  2916. #ifdef RE_ENABLE_I18N
  2917. mbcset, &coll_sym_alloc,
  2918. #endif /* RE_ENABLE_I18N */
  2919. start_elem.opr.name);
  2920. if (BE (*err != REG_NOERROR, 0))
  2921. goto parse_bracket_exp_free_return;
  2922. break;
  2923. case CHAR_CLASS:
  2924. *err = build_charclass (regexp->trans, sbcset,
  2925. #ifdef RE_ENABLE_I18N
  2926. mbcset, &char_class_alloc,
  2927. #endif /* RE_ENABLE_I18N */
  2928. start_elem.opr.name, syntax);
  2929. if (BE (*err != REG_NOERROR, 0))
  2930. goto parse_bracket_exp_free_return;
  2931. break;
  2932. default:
  2933. assert (0);
  2934. break;
  2935. }
  2936. }
  2937. if (BE (token->type == END_OF_RE, 0))
  2938. {
  2939. *err = REG_EBRACK;
  2940. goto parse_bracket_exp_free_return;
  2941. }
  2942. if (token->type == OP_CLOSE_BRACKET)
  2943. break;
  2944. }
  2945. re_string_skip_bytes (regexp, token_len); /* Skip a token. */
  2946. /* If it is non-matching list. */
  2947. if (non_match)
  2948. bitset_not (sbcset);
  2949. #ifdef RE_ENABLE_I18N
  2950. /* Ensure only single byte characters are set. */
  2951. if (dfa->mb_cur_max > 1)
  2952. bitset_mask (sbcset, dfa->sb_char);
  2953. if (mbcset->nmbchars || mbcset->ncoll_syms || mbcset->nequiv_classes
  2954. || mbcset->nranges || (dfa->mb_cur_max > 1 && (mbcset->nchar_classes
  2955. || mbcset->non_match)))
  2956. {
  2957. bin_tree_t *mbc_tree;
  2958. int sbc_idx;
  2959. /* Build a tree for complex bracket. */
  2960. dfa->has_mb_node = 1;
  2961. br_token.type = COMPLEX_BRACKET;
  2962. br_token.opr.mbcset = mbcset;
  2963. mbc_tree = create_token_tree (dfa, NULL, NULL, &br_token);
  2964. if (BE (mbc_tree == NULL, 0))
  2965. goto parse_bracket_exp_espace;
  2966. for (sbc_idx = 0; sbc_idx < BITSET_WORDS; ++sbc_idx)
  2967. if (sbcset[sbc_idx])
  2968. break;
  2969. /* If there are no bits set in sbcset, there is no point
  2970. of having both SIMPLE_BRACKET and COMPLEX_BRACKET. */
  2971. if (sbc_idx < BITSET_WORDS)
  2972. {
  2973. /* Build a tree for simple bracket. */
  2974. br_token.type = SIMPLE_BRACKET;
  2975. br_token.opr.sbcset = sbcset;
  2976. work_tree = create_token_tree (dfa, NULL, NULL, &br_token);
  2977. if (BE (work_tree == NULL, 0))
  2978. goto parse_bracket_exp_espace;
  2979. /* Then join them by ALT node. */
  2980. work_tree = create_tree (dfa, work_tree, mbc_tree, OP_ALT);
  2981. if (BE (work_tree == NULL, 0))
  2982. goto parse_bracket_exp_espace;
  2983. }
  2984. else
  2985. {
  2986. re_free (sbcset);
  2987. work_tree = mbc_tree;
  2988. }
  2989. }
  2990. else
  2991. #endif /* not RE_ENABLE_I18N */
  2992. {
  2993. #ifdef RE_ENABLE_I18N
  2994. free_charset (mbcset);
  2995. #endif
  2996. /* Build a tree for simple bracket. */
  2997. br_token.type = SIMPLE_BRACKET;
  2998. br_token.opr.sbcset = sbcset;
  2999. work_tree = create_token_tree (dfa, NULL, NULL, &br_token);
  3000. if (BE (work_tree == NULL, 0))
  3001. goto parse_bracket_exp_espace;
  3002. }
  3003. return work_tree;
  3004. parse_bracket_exp_espace:
  3005. *err = REG_ESPACE;
  3006. parse_bracket_exp_free_return:
  3007. re_free (sbcset);
  3008. #ifdef RE_ENABLE_I18N
  3009. free_charset (mbcset);
  3010. #endif /* RE_ENABLE_I18N */
  3011. return NULL;
  3012. }
  3013. /* Parse an element in the bracket expression. */
  3014. static reg_errcode_t
  3015. parse_bracket_element (bracket_elem_t *elem, re_string_t *regexp,
  3016. re_token_t *token, int token_len, re_dfa_t *dfa,
  3017. reg_syntax_t syntax, bool accept_hyphen)
  3018. {
  3019. #ifdef RE_ENABLE_I18N
  3020. int cur_char_size;
  3021. cur_char_size = re_string_char_size_at (regexp, re_string_cur_idx (regexp));
  3022. if (cur_char_size > 1)
  3023. {
  3024. elem->type = MB_CHAR;
  3025. elem->opr.wch = re_string_wchar_at (regexp, re_string_cur_idx (regexp));
  3026. re_string_skip_bytes (regexp, cur_char_size);
  3027. return REG_NOERROR;
  3028. }
  3029. #endif /* RE_ENABLE_I18N */
  3030. re_string_skip_bytes (regexp, token_len); /* Skip a token. */
  3031. if (token->type == OP_OPEN_COLL_ELEM || token->type == OP_OPEN_CHAR_CLASS
  3032. || token->type == OP_OPEN_EQUIV_CLASS)
  3033. return parse_bracket_symbol (elem, regexp, token);
  3034. if (BE (token->type == OP_CHARSET_RANGE, 0) && !accept_hyphen)
  3035. {
  3036. /* A '-' must only appear as anything but a range indicator before
  3037. the closing bracket. Everything else is an error. */
  3038. re_token_t token2;
  3039. (void) peek_token_bracket (&token2, regexp, syntax);
  3040. if (token2.type != OP_CLOSE_BRACKET)
  3041. /* The actual error value is not standardized since this whole
  3042. case is undefined. But ERANGE makes good sense. */
  3043. return REG_ERANGE;
  3044. }
  3045. elem->type = SB_CHAR;
  3046. elem->opr.ch = token->opr.c;
  3047. return REG_NOERROR;
  3048. }
  3049. /* Parse a bracket symbol in the bracket expression. Bracket symbols are
  3050. such as [:<character_class>:], [.<collating_element>.], and
  3051. [=<equivalent_class>=]. */
  3052. static reg_errcode_t
  3053. parse_bracket_symbol (bracket_elem_t *elem, re_string_t *regexp,
  3054. re_token_t *token)
  3055. {
  3056. unsigned char ch, delim = token->opr.c;
  3057. int i = 0;
  3058. if (re_string_eoi(regexp))
  3059. return REG_EBRACK;
  3060. for (;; ++i)
  3061. {
  3062. if (i >= BRACKET_NAME_BUF_SIZE)
  3063. return REG_EBRACK;
  3064. if (token->type == OP_OPEN_CHAR_CLASS)
  3065. ch = re_string_fetch_byte_case (regexp);
  3066. else
  3067. ch = re_string_fetch_byte (regexp);
  3068. if (re_string_eoi(regexp))
  3069. return REG_EBRACK;
  3070. if (ch == delim && re_string_peek_byte (regexp, 0) == ']')
  3071. break;
  3072. elem->opr.name[i] = ch;
  3073. }
  3074. re_string_skip_bytes (regexp, 1);
  3075. elem->opr.name[i] = '\0';
  3076. switch (token->type)
  3077. {
  3078. case OP_OPEN_COLL_ELEM:
  3079. elem->type = COLL_SYM;
  3080. break;
  3081. case OP_OPEN_EQUIV_CLASS:
  3082. elem->type = EQUIV_CLASS;
  3083. break;
  3084. case OP_OPEN_CHAR_CLASS:
  3085. elem->type = CHAR_CLASS;
  3086. break;
  3087. default:
  3088. break;
  3089. }
  3090. return REG_NOERROR;
  3091. }
  3092. /* Helper function for parse_bracket_exp.
  3093. Build the equivalence class which is represented by NAME.
  3094. The result are written to MBCSET and SBCSET.
  3095. EQUIV_CLASS_ALLOC is the allocated size of mbcset->equiv_classes,
  3096. is a pointer argument sinse we may update it. */
  3097. static reg_errcode_t
  3098. #ifdef RE_ENABLE_I18N
  3099. build_equiv_class (bitset_t sbcset, re_charset_t *mbcset,
  3100. Idx *equiv_class_alloc, const unsigned char *name)
  3101. #else /* not RE_ENABLE_I18N */
  3102. build_equiv_class (bitset_t sbcset, const unsigned char *name)
  3103. #endif /* not RE_ENABLE_I18N */
  3104. {
  3105. #ifdef _LIBC
  3106. uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
  3107. if (nrules != 0)
  3108. {
  3109. const int32_t *table, *indirect;
  3110. const unsigned char *weights, *extra, *cp;
  3111. unsigned char char_buf[2];
  3112. int32_t idx1, idx2;
  3113. unsigned int ch;
  3114. size_t len;
  3115. /* This #include defines a local function! */
  3116. # include <locale/weight.h>
  3117. /* Calculate the index for equivalence class. */
  3118. cp = name;
  3119. table = (const int32_t *) _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
  3120. weights = (const unsigned char *) _NL_CURRENT (LC_COLLATE,
  3121. _NL_COLLATE_WEIGHTMB);
  3122. extra = (const unsigned char *) _NL_CURRENT (LC_COLLATE,
  3123. _NL_COLLATE_EXTRAMB);
  3124. indirect = (const int32_t *) _NL_CURRENT (LC_COLLATE,
  3125. _NL_COLLATE_INDIRECTMB);
  3126. idx1 = findidx (&cp);
  3127. if (BE (idx1 == 0 || cp < name + strlen ((const char *) name), 0))
  3128. /* This isn't a valid character. */
  3129. return REG_ECOLLATE;
  3130. /* Build single byte matcing table for this equivalence class. */
  3131. char_buf[1] = (unsigned char) '\0';
  3132. len = weights[idx1];
  3133. for (ch = 0; ch < SBC_MAX; ++ch)
  3134. {
  3135. char_buf[0] = ch;
  3136. cp = char_buf;
  3137. idx2 = findidx (&cp);
  3138. /*
  3139. idx2 = table[ch];
  3140. */
  3141. if (idx2 == 0)
  3142. /* This isn't a valid character. */
  3143. continue;
  3144. if (len == weights[idx2])
  3145. {
  3146. int cnt = 0;
  3147. while (cnt <= len &&
  3148. weights[idx1 + 1 + cnt] == weights[idx2 + 1 + cnt])
  3149. ++cnt;
  3150. if (cnt > len)
  3151. bitset_set (sbcset, ch);
  3152. }
  3153. }
  3154. /* Check whether the array has enough space. */
  3155. if (BE (*equiv_class_alloc == mbcset->nequiv_classes, 0))
  3156. {
  3157. /* Not enough, realloc it. */
  3158. /* +1 in case of mbcset->nequiv_classes is 0. */
  3159. Idx new_equiv_class_alloc = 2 * mbcset->nequiv_classes + 1;
  3160. /* Use realloc since the array is NULL if *alloc == 0. */
  3161. int32_t *new_equiv_classes = re_realloc (mbcset->equiv_classes,
  3162. int32_t,
  3163. new_equiv_class_alloc);
  3164. if (BE (new_equiv_classes == NULL, 0))
  3165. return REG_ESPACE;
  3166. mbcset->equiv_classes = new_equiv_classes;
  3167. *equiv_class_alloc = new_equiv_class_alloc;
  3168. }
  3169. mbcset->equiv_classes[mbcset->nequiv_classes++] = idx1;
  3170. }
  3171. else
  3172. #endif /* _LIBC */
  3173. {
  3174. if (BE (strlen ((const char *) name) != 1, 0))
  3175. return REG_ECOLLATE;
  3176. bitset_set (sbcset, *name);
  3177. }
  3178. return REG_NOERROR;
  3179. }
  3180. /* Helper function for parse_bracket_exp.
  3181. Build the character class which is represented by NAME.
  3182. The result are written to MBCSET and SBCSET.
  3183. CHAR_CLASS_ALLOC is the allocated size of mbcset->char_classes,
  3184. is a pointer argument sinse we may update it. */
  3185. static reg_errcode_t
  3186. #ifdef RE_ENABLE_I18N
  3187. build_charclass (RE_TRANSLATE_TYPE trans, bitset_t sbcset,
  3188. re_charset_t *mbcset, Idx *char_class_alloc,
  3189. const unsigned char *class_name, reg_syntax_t syntax)
  3190. #else /* not RE_ENABLE_I18N */
  3191. build_charclass (RE_TRANSLATE_TYPE trans, bitset_t sbcset,
  3192. const unsigned char *class_name, reg_syntax_t syntax)
  3193. #endif /* not RE_ENABLE_I18N */
  3194. {
  3195. int i;
  3196. const char *name = (const char *) class_name;
  3197. /* In case of REG_ICASE "upper" and "lower" match the both of
  3198. upper and lower cases. */
  3199. if ((syntax & RE_ICASE)
  3200. && (strcmp (name, "upper") == 0 || strcmp (name, "lower") == 0))
  3201. name = "alpha";
  3202. #ifdef RE_ENABLE_I18N
  3203. /* Check the space of the arrays. */
  3204. if (BE (*char_class_alloc == mbcset->nchar_classes, 0))
  3205. {
  3206. /* Not enough, realloc it. */
  3207. /* +1 in case of mbcset->nchar_classes is 0. */
  3208. Idx new_char_class_alloc = 2 * mbcset->nchar_classes + 1;
  3209. /* Use realloc since array is NULL if *alloc == 0. */
  3210. wctype_t *new_char_classes = re_realloc (mbcset->char_classes, wctype_t,
  3211. new_char_class_alloc);
  3212. if (BE (new_char_classes == NULL, 0))
  3213. return REG_ESPACE;
  3214. mbcset->char_classes = new_char_classes;
  3215. *char_class_alloc = new_char_class_alloc;
  3216. }
  3217. mbcset->char_classes[mbcset->nchar_classes++] = __wctype (name);
  3218. #endif /* RE_ENABLE_I18N */
  3219. #define BUILD_CHARCLASS_LOOP(ctype_func) \
  3220. do { \
  3221. if (BE (trans != NULL, 0)) \
  3222. { \
  3223. for (i = 0; i < SBC_MAX; ++i) \
  3224. if (ctype_func (i)) \
  3225. bitset_set (sbcset, trans[i]); \
  3226. } \
  3227. else \
  3228. { \
  3229. for (i = 0; i < SBC_MAX; ++i) \
  3230. if (ctype_func (i)) \
  3231. bitset_set (sbcset, i); \
  3232. } \
  3233. } while (0)
  3234. if (strcmp (name, "alnum") == 0)
  3235. BUILD_CHARCLASS_LOOP (isalnum);
  3236. else if (strcmp (name, "cntrl") == 0)
  3237. BUILD_CHARCLASS_LOOP (iscntrl);
  3238. else if (strcmp (name, "lower") == 0)
  3239. BUILD_CHARCLASS_LOOP (islower);
  3240. else if (strcmp (name, "space") == 0)
  3241. BUILD_CHARCLASS_LOOP (isspace);
  3242. else if (strcmp (name, "alpha") == 0)
  3243. BUILD_CHARCLASS_LOOP (isalpha);
  3244. else if (strcmp (name, "digit") == 0)
  3245. BUILD_CHARCLASS_LOOP (isdigit);
  3246. else if (strcmp (name, "print") == 0)
  3247. BUILD_CHARCLASS_LOOP (isprint);
  3248. else if (strcmp (name, "upper") == 0)
  3249. BUILD_CHARCLASS_LOOP (isupper);
  3250. else if (strcmp (name, "blank") == 0)
  3251. BUILD_CHARCLASS_LOOP (isblank);
  3252. else if (strcmp (name, "graph") == 0)
  3253. BUILD_CHARCLASS_LOOP (isgraph);
  3254. else if (strcmp (name, "punct") == 0)
  3255. BUILD_CHARCLASS_LOOP (ispunct);
  3256. else if (strcmp (name, "xdigit") == 0)
  3257. BUILD_CHARCLASS_LOOP (isxdigit);
  3258. else
  3259. return REG_ECTYPE;
  3260. return REG_NOERROR;
  3261. }
  3262. static bin_tree_t *
  3263. build_charclass_op (re_dfa_t *dfa, RE_TRANSLATE_TYPE trans,
  3264. const unsigned char *class_name,
  3265. const unsigned char *extra, bool non_match,
  3266. reg_errcode_t *err)
  3267. {
  3268. re_bitset_ptr_t sbcset;
  3269. #ifdef RE_ENABLE_I18N
  3270. re_charset_t *mbcset;
  3271. Idx alloc = 0;
  3272. #endif /* not RE_ENABLE_I18N */
  3273. reg_errcode_t ret;
  3274. re_token_t br_token;
  3275. bin_tree_t *tree;
  3276. sbcset = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
  3277. #ifdef RE_ENABLE_I18N
  3278. mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
  3279. #endif /* RE_ENABLE_I18N */
  3280. #ifdef RE_ENABLE_I18N
  3281. if (BE (sbcset == NULL || mbcset == NULL, 0))
  3282. #else /* not RE_ENABLE_I18N */
  3283. if (BE (sbcset == NULL, 0))
  3284. #endif /* not RE_ENABLE_I18N */
  3285. {
  3286. *err = REG_ESPACE;
  3287. return NULL;
  3288. }
  3289. if (non_match)
  3290. {
  3291. #ifdef RE_ENABLE_I18N
  3292. mbcset->non_match = 1;
  3293. #endif /* not RE_ENABLE_I18N */
  3294. }
  3295. /* We don't care the syntax in this case. */
  3296. ret = build_charclass (trans, sbcset,
  3297. #ifdef RE_ENABLE_I18N
  3298. mbcset, &alloc,
  3299. #endif /* RE_ENABLE_I18N */
  3300. class_name, 0);
  3301. if (BE (ret != REG_NOERROR, 0))
  3302. {
  3303. re_free (sbcset);
  3304. #ifdef RE_ENABLE_I18N
  3305. free_charset (mbcset);
  3306. #endif /* RE_ENABLE_I18N */
  3307. *err = ret;
  3308. return NULL;
  3309. }
  3310. /* \w match '_' also. */
  3311. for (; *extra; extra++)
  3312. bitset_set (sbcset, *extra);
  3313. /* If it is non-matching list. */
  3314. if (non_match)
  3315. bitset_not (sbcset);
  3316. #ifdef RE_ENABLE_I18N
  3317. /* Ensure only single byte characters are set. */
  3318. if (dfa->mb_cur_max > 1)
  3319. bitset_mask (sbcset, dfa->sb_char);
  3320. #endif
  3321. /* Build a tree for simple bracket. */
  3322. br_token.type = SIMPLE_BRACKET;
  3323. br_token.opr.sbcset = sbcset;
  3324. tree = create_token_tree (dfa, NULL, NULL, &br_token);
  3325. if (BE (tree == NULL, 0))
  3326. goto build_word_op_espace;
  3327. #ifdef RE_ENABLE_I18N
  3328. if (dfa->mb_cur_max > 1)
  3329. {
  3330. bin_tree_t *mbc_tree;
  3331. /* Build a tree for complex bracket. */
  3332. br_token.type = COMPLEX_BRACKET;
  3333. br_token.opr.mbcset = mbcset;
  3334. dfa->has_mb_node = 1;
  3335. mbc_tree = create_token_tree (dfa, NULL, NULL, &br_token);
  3336. if (BE (mbc_tree == NULL, 0))
  3337. goto build_word_op_espace;
  3338. /* Then join them by ALT node. */
  3339. tree = create_tree (dfa, tree, mbc_tree, OP_ALT);
  3340. if (BE (mbc_tree != NULL, 1))
  3341. return tree;
  3342. }
  3343. else
  3344. {
  3345. free_charset (mbcset);
  3346. return tree;
  3347. }
  3348. #else /* not RE_ENABLE_I18N */
  3349. return tree;
  3350. #endif /* not RE_ENABLE_I18N */
  3351. build_word_op_espace:
  3352. re_free (sbcset);
  3353. #ifdef RE_ENABLE_I18N
  3354. free_charset (mbcset);
  3355. #endif /* RE_ENABLE_I18N */
  3356. *err = REG_ESPACE;
  3357. return NULL;
  3358. }
  3359. /* This is intended for the expressions like "a{1,3}".
  3360. Fetch a number from `input', and return the number.
  3361. Return REG_MISSING if the number field is empty like "{,1}".
  3362. Return REG_ERROR if an error occurred. */
  3363. static Idx
  3364. fetch_number (re_string_t *input, re_token_t *token, reg_syntax_t syntax)
  3365. {
  3366. Idx num = REG_MISSING;
  3367. unsigned char c;
  3368. while (1)
  3369. {
  3370. fetch_token (token, input, syntax);
  3371. c = token->opr.c;
  3372. if (BE (token->type == END_OF_RE, 0))
  3373. return REG_ERROR;
  3374. if (token->type == OP_CLOSE_DUP_NUM || c == ',')
  3375. break;
  3376. num = ((token->type != CHARACTER || c < '0' || '9' < c
  3377. || num == REG_ERROR)
  3378. ? REG_ERROR
  3379. : ((num == REG_MISSING) ? c - '0' : num * 10 + c - '0'));
  3380. num = (num > RE_DUP_MAX) ? REG_ERROR : num;
  3381. }
  3382. return num;
  3383. }
  3384. #ifdef RE_ENABLE_I18N
  3385. static void
  3386. free_charset (re_charset_t *cset)
  3387. {
  3388. re_free (cset->mbchars);
  3389. # ifdef _LIBC
  3390. re_free (cset->coll_syms);
  3391. re_free (cset->equiv_classes);
  3392. re_free (cset->range_starts);
  3393. re_free (cset->range_ends);
  3394. # endif
  3395. re_free (cset->char_classes);
  3396. re_free (cset);
  3397. }
  3398. #endif /* RE_ENABLE_I18N */
  3399. /* Functions for binary tree operation. */
  3400. /* Create a tree node. */
  3401. static bin_tree_t *
  3402. create_tree (re_dfa_t *dfa, bin_tree_t *left, bin_tree_t *right,
  3403. re_token_type_t type)
  3404. {
  3405. re_token_t t;
  3406. t.type = type;
  3407. return create_token_tree (dfa, left, right, &t);
  3408. }
  3409. static bin_tree_t *
  3410. create_token_tree (re_dfa_t *dfa, bin_tree_t *left, bin_tree_t *right,
  3411. const re_token_t *token)
  3412. {
  3413. bin_tree_t *tree;
  3414. if (BE (dfa->str_tree_storage_idx == BIN_TREE_STORAGE_SIZE, 0))
  3415. {
  3416. bin_tree_storage_t *storage = re_malloc (bin_tree_storage_t, 1);
  3417. if (storage == NULL)
  3418. return NULL;
  3419. storage->next = dfa->str_tree_storage;
  3420. dfa->str_tree_storage = storage;
  3421. dfa->str_tree_storage_idx = 0;
  3422. }
  3423. tree = &dfa->str_tree_storage->data[dfa->str_tree_storage_idx++];
  3424. tree->parent = NULL;
  3425. tree->left = left;
  3426. tree->right = right;
  3427. tree->token = *token;
  3428. tree->token.duplicated = 0;
  3429. tree->token.opt_subexp = 0;
  3430. tree->first = NULL;
  3431. tree->next = NULL;
  3432. tree->node_idx = REG_MISSING;
  3433. if (left != NULL)
  3434. left->parent = tree;
  3435. if (right != NULL)
  3436. right->parent = tree;
  3437. return tree;
  3438. }
  3439. /* Mark the tree SRC as an optional subexpression.
  3440. To be called from preorder or postorder. */
  3441. static reg_errcode_t
  3442. mark_opt_subexp (void *extra, bin_tree_t *node)
  3443. {
  3444. Idx idx = (Idx) (long) extra;
  3445. if (node->token.type == SUBEXP && node->token.opr.idx == idx)
  3446. node->token.opt_subexp = 1;
  3447. return REG_NOERROR;
  3448. }
  3449. /* Free the allocated memory inside NODE. */
  3450. static void
  3451. free_token (re_token_t *node)
  3452. {
  3453. #ifdef RE_ENABLE_I18N
  3454. if (node->type == COMPLEX_BRACKET && node->duplicated == 0)
  3455. free_charset (node->opr.mbcset);
  3456. else
  3457. #endif /* RE_ENABLE_I18N */
  3458. if (node->type == SIMPLE_BRACKET && node->duplicated == 0)
  3459. re_free (node->opr.sbcset);
  3460. }
  3461. /* Worker function for tree walking. Free the allocated memory inside NODE
  3462. and its children. */
  3463. static reg_errcode_t
  3464. free_tree (void *extra, bin_tree_t *node)
  3465. {
  3466. free_token (&node->token);
  3467. return REG_NOERROR;
  3468. }
  3469. /* Duplicate the node SRC, and return new node. This is a preorder
  3470. visit similar to the one implemented by the generic visitor, but
  3471. we need more infrastructure to maintain two parallel trees --- so,
  3472. it's easier to duplicate. */
  3473. static bin_tree_t *
  3474. duplicate_tree (const bin_tree_t *root, re_dfa_t *dfa)
  3475. {
  3476. const bin_tree_t *node;
  3477. bin_tree_t *dup_root;
  3478. bin_tree_t **p_new = &dup_root, *dup_node = root->parent;
  3479. for (node = root; ; )
  3480. {
  3481. /* Create a new tree and link it back to the current parent. */
  3482. *p_new = create_token_tree (dfa, NULL, NULL, &node->token);
  3483. if (*p_new == NULL)
  3484. return NULL;
  3485. (*p_new)->parent = dup_node;
  3486. (*p_new)->token.duplicated = 1;
  3487. dup_node = *p_new;
  3488. /* Go to the left node, or up and to the right. */
  3489. if (node->left)
  3490. {
  3491. node = node->left;
  3492. p_new = &dup_node->left;
  3493. }
  3494. else
  3495. {
  3496. const bin_tree_t *prev = NULL;
  3497. while (node->right == prev || node->right == NULL)
  3498. {
  3499. prev = node;
  3500. node = node->parent;
  3501. dup_node = dup_node->parent;
  3502. if (!node)
  3503. return dup_root;
  3504. }
  3505. node = node->right;
  3506. p_new = &dup_node->right;
  3507. }
  3508. }
  3509. }