regcomp.c 106 KB

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