regcomp.c 106 KB

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