regcomp.c 108 KB

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