regcomp.c 108 KB

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