regcomp.c 107 KB

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