regcomp.c 108 KB

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