regcomp.c 106 KB

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