regcomp.c 107 KB

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