regcomp.c 108 KB

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