regcomp.c 107 KB

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