regex.h 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754
  1. #if !defined(_RX_H) || defined(RX_WANT_SE_DEFS)
  2. #define _RX_H
  3. /* Copyright (C) 1992, 1993 Free Software Foundation, Inc.
  4. This file is part of the librx library.
  5. Librx is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU Library General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. Librx is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this software; see the file COPYING.LIB. If not,
  15. write to the Free Software Foundation, 675 Mass Ave, Cambridge, MA
  16. 02139, USA. */
  17. /* t. lord Wed Sep 23 18:20:57 1992 */
  18. #include <features.h>
  19. #define __need_size_t
  20. #include <stddef.h>
  21. #include <string.h>
  22. #if RX_WANT_SE_DEFS != 1
  23. __BEGIN_DECLS
  24. #endif
  25. #ifndef RX_WANT_SE_DEFS
  26. /* This page: Bitsets */
  27. #ifndef RX_subset
  28. typedef unsigned int RX_subset;
  29. #define RX_subset_bits (32)
  30. #define RX_subset_mask (RX_subset_bits - 1)
  31. #endif
  32. typedef RX_subset * rx_Bitset;
  33. #ifdef __STDC__
  34. typedef void (*rx_bitset_iterator) (void *, int member_index);
  35. #else
  36. typedef void (*rx_bitset_iterator) ();
  37. #endif
  38. #define rx_bitset_subset(N) ((N) / RX_subset_bits)
  39. #define rx_bitset_subset_val(B,N) ((B)[rx_bitset_subset(N)])
  40. #define RX_bitset_access(B,N,OP) \
  41. ((B)[rx_bitset_subset(N)] OP rx_subset_singletons[(N) & RX_subset_mask])
  42. #define RX_bitset_member(B,N) RX_bitset_access(B, N, &)
  43. #define RX_bitset_enjoin(B,N) RX_bitset_access(B, N, |=)
  44. #define RX_bitset_remove(B,N) RX_bitset_access(B, N, &= ~)
  45. #define RX_bitset_toggle(B,N) RX_bitset_access(B, N, ^= )
  46. #define rx_bitset_numb_subsets(N) (((N) + RX_subset_bits - 1) / RX_subset_bits)
  47. #define rx_sizeof_bitset(N) (rx_bitset_numb_subsets(N) * sizeof(RX_subset))
  48. /* This page: Splay trees. */
  49. #ifdef __STDC__
  50. typedef int (*rx_sp_comparer) (void * a, void * b);
  51. #else
  52. typedef int (*rx_sp_comparer) ();
  53. #endif
  54. struct rx_sp_node
  55. {
  56. void * key;
  57. void * data;
  58. struct rx_sp_node * kids[2];
  59. };
  60. #ifdef __STDC__
  61. typedef void (*rx_sp_key_data_freer) (struct rx_sp_node *);
  62. #else
  63. typedef void (*rx_sp_key_data_freer) ();
  64. #endif
  65. /* giant inflatable hash trees */
  66. struct rx_hash_item
  67. {
  68. struct rx_hash_item * next_same_hash;
  69. struct rx_hash * table;
  70. unsigned long hash;
  71. void * data;
  72. void * binding;
  73. };
  74. struct rx_hash
  75. {
  76. struct rx_hash * parent;
  77. int refs;
  78. struct rx_hash * children[13];
  79. struct rx_hash_item * buckets [13];
  80. int bucket_size [13];
  81. };
  82. struct rx_hash_rules;
  83. #ifdef __STDC__
  84. /* should return like == */
  85. typedef int (*rx_hash_eq)(void *, void *);
  86. typedef struct rx_hash * (*rx_alloc_hash)(struct rx_hash_rules *);
  87. typedef void (*rx_free_hash)(struct rx_hash *,
  88. struct rx_hash_rules *);
  89. typedef struct rx_hash_item * (*rx_alloc_hash_item)(struct rx_hash_rules *,
  90. void *);
  91. typedef void (*rx_free_hash_item)(struct rx_hash_item *,
  92. struct rx_hash_rules *);
  93. #else
  94. typedef int (*rx_hash_eq)();
  95. typedef struct rx_hash * (*rx_alloc_hash)();
  96. typedef void (*rx_free_hash)();
  97. typedef struct rx_hash_item * (*rx_alloc_hash_item)();
  98. typedef void (*rx_free_hash_item)();
  99. #endif
  100. struct rx_hash_rules
  101. {
  102. rx_hash_eq eq;
  103. rx_alloc_hash hash_alloc;
  104. rx_free_hash free_hash;
  105. rx_alloc_hash_item hash_item_alloc;
  106. rx_free_hash_item free_hash_item;
  107. };
  108. /* Forward declarations */
  109. struct rx_cache;
  110. struct rx_superset;
  111. struct rx;
  112. struct rx_se_list;
  113. /*
  114. * GLOSSARY
  115. *
  116. * regexp
  117. * regular expression
  118. * expression
  119. * pattern - a `regular' expression. The expression
  120. * need not be formally regular -- it can contain
  121. * constructs that don't correspond to purely regular
  122. * expressions.
  123. *
  124. * buffer
  125. * string - the string (or strings) being searched or matched.
  126. *
  127. * pattern buffer - a structure of type `struct re_pattern_buffer'
  128. * This in turn contains a `struct rx', which holds the
  129. * NFA compiled from a pattern, as well as some of the state
  130. * of a matcher using the pattern.
  131. *
  132. * NFA - nondeterministic finite automata. Some people
  133. * use this term to a member of the class of
  134. * regular automata (those corresponding to a regular
  135. * language). However, in this code, the meaning is
  136. * more general. The automata used by Rx are comperable
  137. * in power to what are usually called `push down automata'.
  138. *
  139. * Two NFA are built by rx for every pattern. One is built
  140. * by the compiler. The other is built from the first, on
  141. * the fly, by the matcher. The latter is called the `superstate
  142. * NFA' because its states correspond to sets of states from
  143. * the first NFA. (Joe Keane gets credit for the name
  144. * `superstate NFA').
  145. *
  146. * NFA edges
  147. * epsilon edges
  148. * side-effect edges - The NFA compiled from a pattern can have three
  149. * kinds of edges. Epsilon edges can be taken freely anytime
  150. * their source state is reached. Character set edges can be
  151. * taken when their source state is reached and when the next
  152. * character in the buffer is a member of the set. Side effect
  153. * edges imply a transition that can only be taken after the
  154. * indicated side effect has been successfully accomplished.
  155. * Some examples of side effects are:
  156. *
  157. * Storing the current match position to record the
  158. * location of a parentesized subexpression.
  159. *
  160. * Advancing the matcher over N characters if they
  161. * match the N characters previously matched by a
  162. * parentesized subexpression.
  163. *
  164. * Both of those kinds of edges occur in the NFA generated
  165. * by the pattern: \(.\)\1
  166. *
  167. * Epsilon and side effect edges are similar. Unfortunately,
  168. * some of the code uses the name `epsilon edge' to mean
  169. * both epsilon and side effect edges. For example, the
  170. * function has_non_idempotent_epsilon_path computes the existance
  171. * of a non-trivial path containing only a mix of epsilon and
  172. * side effect edges. In that case `nonidempotent epsilon' is being
  173. * used to mean `side effect'.
  174. */
  175. /* LOW LEVEL PATTERN BUFFERS */
  176. /* Suppose that from some NFA state, more than one path through
  177. * side-effect edges is possible. In what order should the paths
  178. * be tried? A function of type rx_se_list_order answers that
  179. * question. It compares two lists of side effects, and says
  180. * which list comes first.
  181. */
  182. #ifdef __STDC__
  183. typedef int (*rx_se_list_order) (struct rx *,
  184. struct rx_se_list *,
  185. struct rx_se_list *);
  186. #else
  187. typedef int (*rx_se_list_order) ();
  188. #endif
  189. /* Struct RX holds a compiled regular expression - that is, an nfa
  190. * ready to be converted on demand to a more efficient superstate nfa.
  191. * This is for the low level interface. The high-level interfaces enclose
  192. * this in a `struct re_pattern_buffer'.
  193. */
  194. struct rx
  195. {
  196. /* The compiler assigns a unique id to every pattern.
  197. * Like sequence numbers in X, there is a subtle bug here
  198. * if you use Rx in a system that runs for a long time.
  199. * But, because of the way the caches work out, it is almost
  200. * impossible to trigger the Rx version of this bug.
  201. *
  202. * The id is used to validate superstates found in a cache
  203. * of superstates. It isn't sufficient to let a superstate
  204. * point back to the rx for which it was compiled -- the caller
  205. * may be re-using a `struct rx' in which case the superstate
  206. * is not really valid. So instead, superstates are validated
  207. * by checking the sequence number of the pattern for which
  208. * they were built.
  209. */
  210. int rx_id;
  211. /* This is memory mgt. state for superstates. This may be
  212. * shared by more than one struct rx.
  213. */
  214. struct rx_cache * cache;
  215. /* Every regex defines the size of its own character set.
  216. * A superstate has an array of this size, with each element
  217. * a `struct rx_inx'. So, don't make this number too large.
  218. * In particular, don't make it 2^16.
  219. */
  220. int local_cset_size;
  221. /* After the NFA is built, it is copied into a contiguous region
  222. * of memory (mostly for compatability with GNU regex).
  223. * Here is that region, and it's size:
  224. */
  225. void * buffer;
  226. unsigned long allocated;
  227. /* Clients of RX can ask for some extra storage in the space pointed
  228. * to by BUFFER. The field RESERVED is an input parameter to the
  229. * compiler. After compilation, this much space will be available
  230. * at (buffer + allocated - reserved)
  231. */
  232. unsigned long reserved;
  233. /* --------- The remaining fields are for internal use only. --------- */
  234. /* --------- But! they must be initialized to 0. --------- */
  235. /* NODEC is the number of nodes in the NFA with non-epsilon
  236. * transitions.
  237. */
  238. int nodec;
  239. /* EPSNODEC is the number of nodes with only epsilon transitions. */
  240. int epsnodec;
  241. /* The sum (NODEC + EPSNODEC) is the total number of states in the
  242. * compiled NFA.
  243. */
  244. /* Lists of side effects as stored in the NFA are `hash consed'..meaning
  245. * that lists with the same elements are ==. During compilation,
  246. * this table facilitates hash-consing.
  247. */
  248. struct rx_hash se_list_memo;
  249. /* Lists of NFA states are also hashed.
  250. */
  251. struct rx_hash set_list_memo;
  252. /* The compiler and matcher must build a number of instruction frames.
  253. * The format of these frames is fixed (c.f. struct rx_inx). The values
  254. * of the instructions is not fixed.
  255. *
  256. * An enumerated type (enum rx_opcode) defines the set of instructions
  257. * that the compiler or matcher might generate. When filling an instruction
  258. * frame, the INX field is found by indexing this instruction table
  259. * with an opcode:
  260. */
  261. void ** instruction_table;
  262. /* The list of all states in an NFA.
  263. * During compilation, the NEXT field of NFA states links this list.
  264. * After compilation, all the states are compacted into an array,
  265. * ordered by state id numbers. At that time, this points to the base
  266. * of that array.
  267. */
  268. struct rx_nfa_state *nfa_states;
  269. /* Every nfa begins with one distinguished starting state:
  270. */
  271. struct rx_nfa_state *start;
  272. /* This orders the search through super-nfa paths.
  273. * See the comment near the typedef of rx_se_list_order.
  274. */
  275. rx_se_list_order se_list_cmp;
  276. struct rx_superset * start_set;
  277. };
  278. /* SYNTAX TREES */
  279. /* Compilation is in stages.
  280. *
  281. * In the first stage, a pattern specified by a string is
  282. * translated into a syntax tree. Later stages will convert
  283. * the syntax tree into an NFA optimized for conversion to a
  284. * superstate-NFA.
  285. *
  286. * This page is about syntax trees.
  287. */
  288. enum rexp_node_type
  289. {
  290. r_cset, /* Match from a character set. `a' or `[a-z]'*/
  291. r_concat, /* Concat two subexpressions. `ab' */
  292. r_alternate, /* Choose one of two subexpressions. `a\|b' */
  293. r_opt, /* Optional subexpression. `a?' */
  294. r_star, /* Repeated subexpression. `a*' */
  295. /* A 2phase-star is a variation on a repeated subexpression.
  296. * In this case, there are two subexpressions. The first, if matched,
  297. * begins a repitition (otherwise, the whole expression is matches the
  298. * empth string).
  299. *
  300. * After matching the first subexpression, a 2phase star either finishes,
  301. * or matches the second subexpression. If the second subexpression is
  302. * matched, then the whole construct repeats.
  303. *
  304. * 2phase stars are used in two circumstances. First, they
  305. * are used as part of the implementation of POSIX intervals (counted
  306. * repititions). Second, they are used to implement proper star
  307. * semantics when the repeated subexpression contains paths of
  308. * only side effects. See rx_compile for more information.
  309. */
  310. r_2phase_star,
  311. /* c.f. "typedef void * rx_side_effect" */
  312. r_side_effect,
  313. /* This is an extension type: It is for transient use in source->source
  314. * transformations (implemented over syntax trees).
  315. */
  316. r_data
  317. };
  318. /* A side effect is a matcher-specific action associated with
  319. * transitions in the NFA. The details of side effects are up
  320. * to the matcher. To the compiler and superstate constructors
  321. * side effects are opaque:
  322. */
  323. typedef void * rx_side_effect;
  324. /* Nodes in a syntax tree are of this type:
  325. */
  326. struct rexp_node
  327. {
  328. enum rexp_node_type type;
  329. union
  330. {
  331. rx_Bitset cset;
  332. rx_side_effect side_effect;
  333. struct
  334. {
  335. struct rexp_node *left;
  336. struct rexp_node *right;
  337. } pair;
  338. void * data;
  339. } params;
  340. };
  341. /* NFA
  342. *
  343. * A syntax tree is compiled into an NFA. This page defines the structure
  344. * of that NFA.
  345. */
  346. struct rx_nfa_state
  347. {
  348. /* These are kept in a list as the NFA is being built. */
  349. struct rx_nfa_state *next;
  350. /* After the NFA is built, states are given integer id's.
  351. * States whose outgoing transitions are all either epsilon or
  352. * side effect edges are given ids less than 0. Other states
  353. * are given successive non-negative ids starting from 0.
  354. */
  355. int id;
  356. /* The list of NFA edges that go from this state to some other. */
  357. struct rx_nfa_edge *edges;
  358. /* If you land in this state, then you implicitly land
  359. * in all other states reachable by only epsilon translations.
  360. * Call the set of maximal paths to such states the epsilon closure
  361. * of this state.
  362. *
  363. * There may be other states that are reachable by a mixture of
  364. * epsilon and side effect edges. Consider the set of maximal paths
  365. * of that sort from this state. Call it the epsilon-side-effect
  366. * closure of the state.
  367. *
  368. * The epsilon closure of the state is a subset of the epsilon-side-
  369. * effect closure. It consists of all the paths that contain
  370. * no side effects -- only epsilon edges.
  371. *
  372. * The paths in the epsilon-side-effect closure can be partitioned
  373. * into equivalance sets. Two paths are equivalant if they have the
  374. * same set of side effects, in the same order. The epsilon-closure
  375. * is one of these equivalance sets. Let's call these equivalance
  376. * sets: observably equivalant path sets. That name is chosen
  377. * because equivalance of two paths means they cause the same side
  378. * effects -- so they lead to the same subsequent observations other
  379. * than that they may wind up in different target states.
  380. *
  381. * The superstate nfa, which is derived from this nfa, is based on
  382. * the observation that all of the paths in an observably equivalant
  383. * path set can be explored at the same time, provided that the
  384. * matcher keeps track not of a single nfa state, but of a set of
  385. * states. In particular, after following all the paths in an
  386. * observably equivalant set, you wind up at a set of target states.
  387. * That set of target states corresponds to one state in the
  388. * superstate NFA.
  389. *
  390. * Staticly, before matching begins, it is convenient to analyze the
  391. * nfa. Each state is labeled with a list of the observably
  392. * equivalant path sets who's union covers all the
  393. * epsilon-side-effect paths beginning in this state. This list is
  394. * called the possible futures of the state.
  395. *
  396. * A trivial example is this NFA:
  397. * s1
  398. * A ---> B
  399. *
  400. * s2
  401. * ---> C
  402. *
  403. * epsilon s1
  404. * ---------> D ------> E
  405. *
  406. *
  407. * In this example, A has two possible futures.
  408. * One invokes the side effect `s1' and contains two paths,
  409. * one ending in state B, the other in state E.
  410. * The other invokes the side effect `s2' and contains only
  411. * one path, landing in state C.
  412. */
  413. struct rx_possible_future *futures;
  414. /* There are exactly two distinguished states in every NFA: */
  415. unsigned int is_final:1;
  416. unsigned int is_start:1;
  417. /* These are used during NFA construction... */
  418. unsigned int eclosure_needed:1;
  419. unsigned int mark:1;
  420. };
  421. /* An edge in an NFA is typed: */
  422. enum rx_nfa_etype
  423. {
  424. /* A cset edge is labled with a set of characters one of which
  425. * must be matched for the edge to be taken.
  426. */
  427. ne_cset,
  428. /* An epsilon edge is taken whenever its starting state is
  429. * reached.
  430. */
  431. ne_epsilon,
  432. /* A side effect edge is taken whenever its starting state is
  433. * reached. Side effects may cause the match to fail or the
  434. * position of the matcher to advance.
  435. */
  436. ne_side_effect /* A special kind of epsilon. */
  437. };
  438. struct rx_nfa_edge
  439. {
  440. struct rx_nfa_edge *next;
  441. enum rx_nfa_etype type;
  442. struct rx_nfa_state *dest;
  443. union
  444. {
  445. rx_Bitset cset;
  446. rx_side_effect side_effect;
  447. } params;
  448. };
  449. /* A possible future consists of a list of side effects
  450. * and a set of destination states. Below are their
  451. * representations. These structures are hash-consed which
  452. * means that lists with the same elements share a representation
  453. * (their addresses are ==).
  454. */
  455. struct rx_nfa_state_set
  456. {
  457. struct rx_nfa_state * car;
  458. struct rx_nfa_state_set * cdr;
  459. };
  460. struct rx_se_list
  461. {
  462. rx_side_effect car;
  463. struct rx_se_list * cdr;
  464. };
  465. struct rx_possible_future
  466. {
  467. struct rx_possible_future *next;
  468. struct rx_se_list * effects;
  469. struct rx_nfa_state_set * destset;
  470. };
  471. /* This begins the description of the superstate NFA.
  472. *
  473. * The superstate NFA corresponds to the NFA in these ways:
  474. *
  475. * Every superstate NFA states SUPER correspond to sets of NFA states,
  476. * nfa_states(SUPER).
  477. *
  478. * Superstate edges correspond to NFA paths.
  479. *
  480. * The superstate has no epsilon transitions;
  481. * every edge has a character label, and a (possibly empty) side
  482. * effect label. The side effect label corresponds to a list of
  483. * side effects that occur in the NFA. These parts are referred
  484. * to as: superedge_character(EDGE) and superedge_sides(EDGE).
  485. *
  486. * For a superstate edge EDGE starting in some superstate SUPER,
  487. * the following is true (in pseudo-notation :-):
  488. *
  489. * exists DEST in nfa_states s.t.
  490. * exists nfaEDGE in nfa_edges s.t.
  491. * origin (nfaEDGE) == DEST
  492. * && origin (nfaEDGE) is a member of nfa_states(SUPER)
  493. * && exists PF in possible_futures(dest(nfaEDGE)) s.t.
  494. * sides_of_possible_future (PF) == superedge_sides (EDGE)
  495. *
  496. * also:
  497. *
  498. * let SUPER2 := superedge_destination(EDGE)
  499. * nfa_states(SUPER2)
  500. * == union of all nfa state sets S s.t.
  501. * exists PF in possible_futures(dest(nfaEDGE)) s.t.
  502. * sides_of_possible_future (PF) == superedge_sides (EDGE)
  503. * && S == dests_of_possible_future (PF) }
  504. *
  505. * Or in english, every superstate is a set of nfa states. A given
  506. * character and a superstate implies many transitions in the NFA --
  507. * those that begin with an edge labeled with that character from a
  508. * state in the set corresponding to the superstate.
  509. *
  510. * The destinations of those transitions each have a set of possible
  511. * futures. A possible future is a list of side effects and a set of
  512. * destination NFA states. Two sets of possible futures can be
  513. * `merged' by combining all pairs of possible futures that have the
  514. * same side effects. A pair is combined by creating a new future
  515. * with the same side effect but the union of the two destination sets.
  516. * In this way, all the possible futures suggested by a superstate
  517. * and a character can be merged into a set of possible futures where
  518. * no two elements of the set have the same set of side effects.
  519. *
  520. * The destination of a possible future, being a set of NFA states,
  521. * corresponds to a supernfa state. So, the merged set of possible
  522. * futures we just created can serve as a set of edges in the
  523. * supernfa.
  524. *
  525. * The representation of the superstate nfa and the nfa is critical.
  526. * The nfa has to be compact, but has to facilitate the rapid
  527. * computation of missing superstates. The superstate nfa has to
  528. * be fast to interpret, lazilly constructed, and bounded in space.
  529. *
  530. * To facilitate interpretation, the superstate data structures are
  531. * peppered with `instruction frames'. There is an instruction set
  532. * defined below which matchers using the supernfa must be able to
  533. * interpret.
  534. *
  535. * We'd like to make it possible but not mandatory to use code
  536. * addresses to represent instructions (c.f. gcc's computed goto).
  537. * Therefore, we define an enumerated type of opcodes, and when
  538. * writing one of these instructions into a data structure, use
  539. * the opcode as an index into a table of instruction values.
  540. *
  541. * Here are the opcodes that occur in the superstate nfa:
  542. */
  543. /* Every superstate contains a table of instruction frames indexed
  544. * by characters. A normal `move' in a matcher is to fetch the next
  545. * character and use it as an index into a superstates transition
  546. * table.
  547. *
  548. * In the fasted case, only one edge follows from that character.
  549. * In other cases there is more work to do.
  550. *
  551. * The descriptions of the opcodes refer to data structures that are
  552. * described further below.
  553. */
  554. enum rx_opcode
  555. {
  556. /*
  557. * BACKTRACK_POINT is invoked when a character transition in
  558. * a superstate leads to more than one edge. In that case,
  559. * the edges have to be explored independently using a backtracking
  560. * strategy.
  561. *
  562. * A BACKTRACK_POINT instruction is stored in a superstate's
  563. * transition table for some character when it is known that that
  564. * character crosses more than one edge. On encountering this
  565. * instruction, the matcher saves enough state to backtrack to this
  566. * point in the match later.
  567. */
  568. rx_backtrack_point = 0, /* data is (struct transition_class *) */
  569. /*
  570. * RX_DO_SIDE_EFFECTS evaluates the side effects of an epsilon path.
  571. * There is one occurence of this instruction per rx_distinct_future.
  572. * This instruction is skipped if a rx_distinct_future has no side effects.
  573. */
  574. rx_do_side_effects = rx_backtrack_point + 1,
  575. /* data is (struct rx_distinct_future *) */
  576. /*
  577. * RX_CACHE_MISS instructions are stored in rx_distinct_futures whose
  578. * destination superstate has been reclaimed (or was never built).
  579. * It recomputes the destination superstate.
  580. * RX_CACHE_MISS is also stored in a superstate transition table before
  581. * any of its edges have been built.
  582. */
  583. rx_cache_miss = rx_do_side_effects + 1,
  584. /* data is (struct rx_distinct_future *) */
  585. /*
  586. * RX_NEXT_CHAR is called to consume the next character and take the
  587. * corresponding transition. This is the only instruction that uses
  588. * the DATA field of the instruction frame instead of DATA_2.
  589. * (see EXPLORE_FUTURE in regex.c).
  590. */
  591. rx_next_char = rx_cache_miss + 1, /* data is (struct superstate *) */
  592. /* RX_BACKTRACK indicates that a transition fails.
  593. */
  594. rx_backtrack = rx_next_char + 1, /* no data */
  595. /*
  596. * RX_ERROR_INX is stored only in places that should never be executed.
  597. */
  598. rx_error_inx = rx_backtrack + 1, /* Not supposed to occur. */
  599. rx_num_instructions = rx_error_inx + 1
  600. };
  601. /* An id_instruction_table holds the values stored in instruction
  602. * frames. The table is indexed by the enums declared above.
  603. */
  604. extern void * rx_id_instruction_table[rx_num_instructions];
  605. /* The heart of the matcher is a `word-code-interpreter'
  606. * (like a byte-code interpreter, except that instructions
  607. * are a full word wide).
  608. *
  609. * Instructions are not stored in a vector of code, instead,
  610. * they are scattered throughout the data structures built
  611. * by the regexp compiler and the matcher. One word-code instruction,
  612. * together with the arguments to that instruction, constitute
  613. * an instruction frame (struct rx_inx).
  614. *
  615. * This structure type is padded by hand to a power of 2 because
  616. * in one of the dominant cases, we dispatch by indexing a table
  617. * of instruction frames. If that indexing can be accomplished
  618. * by just a shift of the index, we're happy.
  619. *
  620. * Instructions take at most one argument, but there are two
  621. * slots in an instruction frame that might hold that argument.
  622. * These are called data and data_2. The data slot is only
  623. * used for one instruction (RX_NEXT_CHAR). For all other
  624. * instructions, data should be set to 0.
  625. *
  626. * RX_NEXT_CHAR is the most important instruction by far.
  627. * By reserving the data field for its exclusive use,
  628. * instruction dispatch is sped up in that case. There is
  629. * no need to fetch both the instruction and the data,
  630. * only the data is needed. In other words, a `cycle' begins
  631. * by fetching the field data. If that is non-0, then it must
  632. * be the destination state of a next_char transition, so
  633. * make that value the current state, advance the match position
  634. * by one character, and start a new cycle. On the other hand,
  635. * if data is 0, fetch the instruction and do a more complicated
  636. * dispatch on that.
  637. */
  638. struct rx_inx
  639. {
  640. void * data;
  641. void * data_2;
  642. void * inx;
  643. void * fnord;
  644. };
  645. #ifndef RX_TAIL_ARRAY
  646. #define RX_TAIL_ARRAY 1
  647. #endif
  648. /* A superstate corresponds to a set of nfa states. Those sets are
  649. * represented by STRUCT RX_SUPERSET. The constructors
  650. * guarantee that only one (shared) structure is created for a given set.
  651. */
  652. struct rx_superset
  653. {
  654. int refs; /* This is a reference counted structure. */
  655. /* We keep these sets in a cache because (in an unpredictable way),
  656. * the same set is often created again and again. But that is also
  657. * problematic -- compatibility with POSIX and GNU regex requires
  658. * that we not be able to tell when a program discards a particular
  659. * NFA (thus invalidating the supersets created from it).
  660. *
  661. * But when a cache hit appears to occur, we will have in hand the
  662. * nfa for which it may have happened. That is why every nfa is given
  663. * its own sequence number. On a cache hit, the cache is validated
  664. * by comparing the nfa sequence number to this field:
  665. */
  666. int id;
  667. struct rx_nfa_state * car; /* May or may not be a valid addr. */
  668. struct rx_superset * cdr;
  669. /* If the corresponding superstate exists: */
  670. struct rx_superstate * superstate;
  671. /* There is another bookkeeping problem. It is expensive to
  672. * compute the starting nfa state set for an nfa. So, once computed,
  673. * it is cached in the `struct rx'.
  674. *
  675. * But, the state set can be flushed from the superstate cache.
  676. * When that happens, we can't know if the corresponding `struct rx'
  677. * is still alive or if it has been freed or re-used by the program.
  678. * So, the cached pointer to this set in a struct rx might be invalid
  679. * and we need a way to validate it.
  680. *
  681. * Fortunately, even if this set is flushed from the cache, it is
  682. * not freed. It just goes on the free-list of supersets.
  683. * So we can still examine it.
  684. *
  685. * So to validate a starting set memo, check to see if the
  686. * starts_for field still points back to the struct rx in question,
  687. * and if the ID matches the rx sequence number.
  688. */
  689. struct rx * starts_for;
  690. /* This is used to link into a hash bucket so these objects can
  691. * be `hash-consed'.
  692. */
  693. struct rx_hash_item hash_item;
  694. };
  695. #define rx_protect_superset(RX,CON) (++(CON)->refs)
  696. /* The terminology may be confusing (rename this structure?).
  697. * Every character occurs in at most one rx_super_edge per super-state.
  698. * But, that structure might have more than one option, indicating a point
  699. * of non-determinism.
  700. *
  701. * In other words, this structure holds a list of superstate edges
  702. * sharing a common starting state and character label. The edges
  703. * are in the field OPTIONS. All superstate edges sharing the same
  704. * starting state and character are in this list.
  705. */
  706. struct rx_super_edge
  707. {
  708. struct rx_super_edge *next;
  709. struct rx_inx rx_backtrack_frame;
  710. int cset_size;
  711. rx_Bitset cset;
  712. struct rx_distinct_future *options;
  713. };
  714. /* A superstate is a set of nfa states (RX_SUPERSET) along
  715. * with a transition table. Superstates are built on demand and reclaimed
  716. * without warning. To protect a superstate from this ghastly fate,
  717. * use LOCK_SUPERSTATE.
  718. */
  719. struct rx_superstate
  720. {
  721. int rx_id; /* c.f. the id field of rx_superset */
  722. int locks; /* protection from reclamation */
  723. /* Within a superstate cache, all the superstates are kept in a big
  724. * queue. The tail of the queue is the state most likely to be
  725. * reclaimed. The *recyclable fields hold the queue position of
  726. * this state.
  727. */
  728. struct rx_superstate * next_recyclable;
  729. struct rx_superstate * prev_recyclable;
  730. /* The supernfa edges that exist in the cache and that have
  731. * this state as their destination are kept in this list:
  732. */
  733. struct rx_distinct_future * transition_refs;
  734. /* The list of nfa states corresponding to this superstate: */
  735. struct rx_superset * contents;
  736. /* The list of edges in the cache beginning from this state. */
  737. struct rx_super_edge * edges;
  738. /* A tail of the recyclable queue is marked as semifree. A semifree
  739. * state has no incoming next_char transitions -- any transition
  740. * into a semifree state causes a complex dispatch with the side
  741. * effect of rescuing the state from its semifree state.
  742. *
  743. * An alternative to this might be to make next_char more expensive,
  744. * and to move a state to the head of the recyclable queue whenever
  745. * it is entered. That way, popular states would never be recycled.
  746. *
  747. * But unilaterally making next_char more expensive actually loses.
  748. * So, incoming transitions are only made expensive for states near
  749. * the tail of the recyclable queue. The more cache contention
  750. * there is, the more frequently a state will have to prove itself
  751. * and be moved back to the front of the queue. If there is less
  752. * contention, then popular states just aggregate in the front of
  753. * the queue and stay there.
  754. */
  755. int is_semifree;
  756. /* This keeps track of the size of the transition table for this
  757. * state. There is a half-hearted attempt to support variable sized
  758. * superstates.
  759. */
  760. int trans_size;
  761. /* Indexed by characters... */
  762. struct rx_inx transitions[RX_TAIL_ARRAY];
  763. };
  764. /* A list of distinct futures define the edges that leave from a
  765. * given superstate on a given character. c.f. rx_super_edge.
  766. */
  767. struct rx_distinct_future
  768. {
  769. struct rx_distinct_future * next_same_super_edge[2];
  770. struct rx_distinct_future * next_same_dest;
  771. struct rx_distinct_future * prev_same_dest;
  772. struct rx_superstate * present; /* source state */
  773. struct rx_superstate * future; /* destination state */
  774. struct rx_super_edge * edge;
  775. /* The future_frame holds the instruction that should be executed
  776. * after all the side effects are done, when it is time to complete
  777. * the transition to the next state.
  778. *
  779. * Normally this is a next_char instruction, but it may be a
  780. * cache_miss instruction as well, depending on whether or not
  781. * the superstate is in the cache and semifree.
  782. *
  783. * If this is the only future for a given superstate/char, and
  784. * if there are no side effects to be performed, this frame is
  785. * not used (directly) at all. Instead, its contents are copied
  786. * into the transition table of the starting state of this dist. future.
  787. */
  788. struct rx_inx future_frame;
  789. struct rx_inx side_effects_frame;
  790. struct rx_se_list * effects;
  791. };
  792. #define rx_lock_superstate(R,S) ((S)->locks++)
  793. #define rx_unlock_superstate(R,S) (--(S)->locks)
  794. /* This page destined for rx.h */
  795. struct rx_blocklist
  796. {
  797. struct rx_blocklist * next;
  798. int bytes;
  799. };
  800. struct rx_freelist
  801. {
  802. struct rx_freelist * next;
  803. };
  804. struct rx_cache;
  805. #ifdef __STDC__
  806. typedef void (*rx_morecore_fn)(struct rx_cache *);
  807. #else
  808. typedef void (*rx_morecore_fn)();
  809. #endif
  810. /* You use this to control the allocation of superstate data
  811. * during matching. Most of it should be initialized to 0.
  812. *
  813. * A MORECORE function is necessary. It should allocate
  814. * a new block of memory or return 0.
  815. * A default that uses malloc is called `rx_morecore'.
  816. *
  817. * The number of SUPERSTATES_ALLOWED indirectly limits how much memory
  818. * the system will try to allocate. The default is 128. Batch style
  819. * applications that are very regexp intensive should use as high a number
  820. * as possible without thrashing.
  821. *
  822. * The LOCAL_CSET_SIZE is the number of characters in a character set.
  823. * It is therefore the number of entries in a superstate transition table.
  824. * Generally, it should be 256. If your character set has 16 bits,
  825. * it is better to translate your regexps into equivalent 8 bit patterns.
  826. */
  827. struct rx_cache
  828. {
  829. struct rx_hash_rules superset_hash_rules;
  830. /* Objects are allocated by incrementing a pointer that
  831. * scans across rx_blocklists.
  832. */
  833. struct rx_blocklist * memory;
  834. struct rx_blocklist * memory_pos;
  835. int bytes_left;
  836. char * memory_addr;
  837. rx_morecore_fn morecore;
  838. /* Freelists. */
  839. struct rx_freelist * free_superstates;
  840. struct rx_freelist * free_transition_classes;
  841. struct rx_freelist * free_discernable_futures;
  842. struct rx_freelist * free_supersets;
  843. struct rx_freelist * free_hash;
  844. /* Two sets of superstates -- those that are semifreed, and those
  845. * that are being used.
  846. */
  847. struct rx_superstate * lru_superstate;
  848. struct rx_superstate * semifree_superstate;
  849. struct rx_superset * empty_superset;
  850. int superstates;
  851. int semifree_superstates;
  852. int hits;
  853. int misses;
  854. int superstates_allowed;
  855. int local_cset_size;
  856. void ** instruction_table;
  857. struct rx_hash superset_table;
  858. };
  859. /* The lowest-level search function supports arbitrarily fragmented
  860. * strings and (optionally) suspendable/resumable searches.
  861. *
  862. * Callers have to provide a few hooks.
  863. */
  864. #ifndef __GNUC__
  865. #ifdef __STDC__
  866. #define __const__ const
  867. #else
  868. #define __const__
  869. #endif
  870. #endif
  871. /* This holds a matcher position */
  872. struct rx_string_position
  873. {
  874. __const__ unsigned char * pos; /* The current pos. */
  875. __const__ unsigned char * string; /* The current string burst. */
  876. __const__ unsigned char * end; /* First invalid position >= POS. */
  877. int offset; /* Integer address of the current burst. */
  878. int size; /* Current string's size. */
  879. int search_direction; /* 1 or -1 */
  880. int search_end; /* First position to not try. */
  881. };
  882. enum rx_get_burst_return
  883. {
  884. rx_get_burst_continuation,
  885. rx_get_burst_error,
  886. rx_get_burst_ok,
  887. rx_get_burst_no_more
  888. };
  889. /* A call to get burst should make POS valid. It might be invalid
  890. * if the STRING field doesn't point to a burst that actually
  891. * contains POS.
  892. *
  893. * GET_BURST should take a clue from SEARCH_DIRECTION (1 or -1) as to
  894. * whether or not to pad to the left. Padding to the right is always
  895. * appropriate, but need not go past the point indicated by STOP.
  896. *
  897. * If a continuation is returned, then the reentering call to
  898. * a search function will retry the get_burst.
  899. */
  900. #ifdef __STDC__
  901. typedef enum rx_get_burst_return
  902. (*rx_get_burst_fn) (struct rx_string_position * pos,
  903. void * app_closure,
  904. int stop);
  905. #else
  906. typedef enum rx_get_burst_return (*rx_get_burst_fn) ();
  907. #endif
  908. enum rx_back_check_return
  909. {
  910. rx_back_check_continuation,
  911. rx_back_check_error,
  912. rx_back_check_pass,
  913. rx_back_check_fail
  914. };
  915. /* Back_check should advance the position it is passed
  916. * over rparen - lparen characters and return pass iff
  917. * the characters starting at POS match those indexed
  918. * by [LPAREN..RPAREN].
  919. *
  920. * If a continuation is returned, then the reentering call to
  921. * a search function will retry the back_check.
  922. */
  923. #ifdef __STDC__
  924. typedef enum rx_back_check_return
  925. (*rx_back_check_fn) (struct rx_string_position * pos,
  926. int lparen,
  927. int rparen,
  928. unsigned char * translate,
  929. void * app_closure,
  930. int stop);
  931. #else
  932. typedef enum rx_back_check_return (*rx_back_check_fn) ();
  933. #endif
  934. /* A call to fetch_char should return the character at POS or POS + 1.
  935. * Returning continuations here isn't supported. OFFSET is either 0 or 1
  936. * and indicates which characters is desired.
  937. */
  938. #ifdef __STDC__
  939. typedef int (*rx_fetch_char_fn) (struct rx_string_position * pos,
  940. int offset,
  941. void * app_closure,
  942. int stop);
  943. #else
  944. typedef int (*rx_fetch_char_fn) ();
  945. #endif
  946. enum rx_search_return
  947. {
  948. rx_search_continuation = -4,
  949. rx_search_error = -3,
  950. rx_search_soft_fail = -2, /* failed by running out of string */
  951. rx_search_fail = -1 /* failed only by reaching failure states */
  952. /* return values >= 0 indicate the position of a successful match */
  953. };
  954. /* regex.h
  955. *
  956. * The remaining declarations replace regex.h.
  957. */
  958. /* This is an array of error messages corresponding to the error codes.
  959. */
  960. extern __const__ char *re_error_msg[];
  961. /* If any error codes are removed, changed, or added, update the
  962. `re_error_msg' table in regex.c. */
  963. typedef enum
  964. {
  965. REG_NOERROR = 0, /* Success. */
  966. REG_NOMATCH, /* Didn't find a match (for regexec). */
  967. /* POSIX regcomp return error codes. (In the order listed in the
  968. standard.) */
  969. REG_BADPAT, /* Invalid pattern. */
  970. REG_ECOLLATE, /* Not implemented. */
  971. REG_ECTYPE, /* Invalid character class name. */
  972. REG_EESCAPE, /* Trailing backslash. */
  973. REG_ESUBREG, /* Invalid back reference. */
  974. REG_EBRACK, /* Unmatched left bracket. */
  975. REG_EPAREN, /* Parenthesis imbalance. */
  976. REG_EBRACE, /* Unmatched \{. */
  977. REG_BADBR, /* Invalid contents of \{\}. */
  978. REG_ERANGE, /* Invalid range end. */
  979. REG_ESPACE, /* Ran out of memory. */
  980. REG_BADRPT, /* No preceding re for repetition op. */
  981. /* Error codes we've added. */
  982. REG_EEND, /* Premature end. */
  983. REG_ESIZE, /* Compiled pattern bigger than 2^16 bytes. */
  984. REG_ERPAREN /* Unmatched ) or \); not returned from regcomp. */
  985. } reg_errcode_t;
  986. /* The regex.c support, as a client of rx, defines a set of possible
  987. * side effects that can be added to the edge lables of nfa edges.
  988. * Here is the list of sidef effects in use.
  989. */
  990. enum re_side_effects
  991. {
  992. #define RX_WANT_SE_DEFS 1
  993. #undef RX_DEF_SE
  994. #undef RX_DEF_CPLX_SE
  995. #define RX_DEF_SE(IDEM, NAME, VALUE) NAME VALUE,
  996. #define RX_DEF_CPLX_SE(IDEM, NAME, VALUE) NAME VALUE,
  997. #include <regex.h>
  998. #undef RX_DEF_SE
  999. #undef RX_DEF_CPLX_SE
  1000. #undef RX_WANT_SE_DEFS
  1001. re_floogle_flap = 65533
  1002. };
  1003. /* These hold paramaters for the kinds of side effects that are possible
  1004. * in the supported pattern languages. These include things like the
  1005. * numeric bounds of {} operators and the index of paren registers for
  1006. * subexpression measurement or backreferencing.
  1007. */
  1008. struct re_se_params
  1009. {
  1010. enum re_side_effects se;
  1011. int op1;
  1012. int op2;
  1013. };
  1014. typedef unsigned reg_syntax_t;
  1015. struct re_pattern_buffer
  1016. {
  1017. struct rx rx;
  1018. reg_syntax_t syntax; /* See below for syntax bit definitions. */
  1019. unsigned int no_sub:1; /* If set, don't return register offsets. */
  1020. unsigned int not_bol:1; /* If set, the anchors ('^' and '$') don't */
  1021. unsigned int not_eol:1; /* match at the ends of the string. */
  1022. unsigned int newline_anchor:1;/* If true, an anchor at a newline matches.*/
  1023. unsigned int least_subs:1; /* If set, and returning registers, return
  1024. * as few values as possible. Only
  1025. * backreferenced groups and group 0 (the whole
  1026. * match) will be returned.
  1027. */
  1028. /* If true, this says that the matcher should keep registers on its
  1029. * backtracking stack. For many patterns, we can easily determine that
  1030. * this isn't necessary.
  1031. */
  1032. unsigned int match_regs_on_stack:1;
  1033. unsigned int search_regs_on_stack:1;
  1034. /* is_anchored and begbuf_only are filled in by rx_compile. */
  1035. unsigned int is_anchored:1; /* Anchorded by ^? */
  1036. unsigned int begbuf_only:1; /* Anchored to char position 0? */
  1037. /* If REGS_UNALLOCATED, allocate space in the `regs' structure
  1038. * for `max (RE_NREGS, re_nsub + 1)' groups.
  1039. * If REGS_REALLOCATE, reallocate space if necessary.
  1040. * If REGS_FIXED, use what's there.
  1041. */
  1042. #define REGS_UNALLOCATED 0
  1043. #define REGS_REALLOCATE 1
  1044. #define REGS_FIXED 2
  1045. unsigned int regs_allocated:2;
  1046. /* Either a translate table to apply to all characters before
  1047. * comparing them, or zero for no translation. The translation
  1048. * is applied to a pattern when it is compiled and to a string
  1049. * when it is matched.
  1050. */
  1051. unsigned char * translate;
  1052. /* If this is a valid pointer, it tells rx not to store the extents of
  1053. * certain subexpressions (those corresponding to non-zero entries).
  1054. * Passing 0x1 is the same as passing an array of all ones. Passing 0x0
  1055. * is the same as passing an array of all zeros.
  1056. * The array should contain as many entries as their are subexps in the
  1057. * regexp.
  1058. *
  1059. * For POSIX compatability, when using regcomp and regexec this field
  1060. * is zeroed and ignored.
  1061. */
  1062. char * syntax_parens;
  1063. /* Number of subexpressions found by the compiler. */
  1064. size_t re_nsub;
  1065. void * buffer; /* Malloced memory for the nfa. */
  1066. unsigned long allocated; /* Size of that memory. */
  1067. /* Pointer to a fastmap, if any, otherwise zero. re_search uses
  1068. * the fastmap, if there is one, to skip over impossible
  1069. * starting points for matches. */
  1070. char *fastmap;
  1071. unsigned int fastmap_accurate:1; /* These three are internal. */
  1072. unsigned int can_match_empty:1;
  1073. struct rx_nfa_state * start; /* The nfa starting state. */
  1074. /* This is the list of iterator bounds for {lo,hi} constructs.
  1075. * The memory pointed to is part of the rx->buffer.
  1076. */
  1077. struct re_se_params *se_params;
  1078. /* This is a bitset representation of the fastmap.
  1079. * This is a true fastmap that already takes the translate
  1080. * table into account.
  1081. */
  1082. rx_Bitset fastset;
  1083. };
  1084. /* Type for byte offsets within the string. POSIX mandates this. */
  1085. typedef int regoff_t;
  1086. /* This is the structure we store register match data in. See
  1087. regex.texinfo for a full description of what registers match. */
  1088. struct re_registers
  1089. {
  1090. unsigned num_regs;
  1091. regoff_t *start;
  1092. regoff_t *end;
  1093. };
  1094. typedef struct re_pattern_buffer regex_t;
  1095. /* POSIX specification for registers. Aside from the different names than
  1096. `re_registers', POSIX uses an array of structures, instead of a
  1097. structure of arrays. */
  1098. typedef struct
  1099. {
  1100. regoff_t rm_so; /* Byte offset from string's start to substring's start. */
  1101. regoff_t rm_eo; /* Byte offset from string's start to substring's end. */
  1102. } regmatch_t;
  1103. /* The following bits are used to determine the regexp syntax we
  1104. recognize. The set/not-set meanings are chosen so that Emacs syntax
  1105. remains the value 0. The bits are given in alphabetical order, and
  1106. the definitions shifted by one from the previous bit; thus, when we
  1107. add or remove a bit, only one other definition need change. */
  1108. /* If this bit is not set, then \ inside a bracket expression is literal.
  1109. If set, then such a \ quotes the following character. */
  1110. #define RE_BACKSLASH_ESCAPE_IN_LISTS (1)
  1111. /* If this bit is not set, then + and ? are operators, and \+ and \? are
  1112. literals.
  1113. If set, then \+ and \? are operators and + and ? are literals. */
  1114. #define RE_BK_PLUS_QM (RE_BACKSLASH_ESCAPE_IN_LISTS << 1)
  1115. /* If this bit is set, then character classes are supported. They are:
  1116. [:alpha:], [:upper:], [:lower:], [:digit:], [:alnum:], [:xdigit:],
  1117. [:space:], [:print:], [:punct:], [:graph:], and [:cntrl:].
  1118. If not set, then character classes are not supported. */
  1119. #define RE_CHAR_CLASSES (RE_BK_PLUS_QM << 1)
  1120. /* If this bit is set, then ^ and $ are always anchors (outside bracket
  1121. expressions, of course).
  1122. If this bit is not set, then it depends:
  1123. ^ is an anchor if it is at the beginning of a regular
  1124. expression or after an open-group or an alternation operator;
  1125. $ is an anchor if it is at the end of a regular expression, or
  1126. before a close-group or an alternation operator.
  1127. This bit could be (re)combined with RE_CONTEXT_INDEP_OPS, because
  1128. POSIX draft 11.2 says that * etc. in leading positions is undefined.
  1129. We already implemented a previous draft which made those constructs
  1130. invalid, though, so we haven't changed the code back. */
  1131. #define RE_CONTEXT_INDEP_ANCHORS (RE_CHAR_CLASSES << 1)
  1132. /* If this bit is set, then special characters are always special
  1133. regardless of where they are in the pattern.
  1134. If this bit is not set, then special characters are special only in
  1135. some contexts; otherwise they are ordinary. Specifically,
  1136. * + ? and intervals are only special when not after the beginning,
  1137. open-group, or alternation operator. */
  1138. #define RE_CONTEXT_INDEP_OPS (RE_CONTEXT_INDEP_ANCHORS << 1)
  1139. /* If this bit is set, then *, +, ?, and { cannot be first in an re or
  1140. immediately after an alternation or begin-group operator. */
  1141. #define RE_CONTEXT_INVALID_OPS (RE_CONTEXT_INDEP_OPS << 1)
  1142. /* If this bit is set, then . matches newline.
  1143. If not set, then it doesn't. */
  1144. #define RE_DOT_NEWLINE (RE_CONTEXT_INVALID_OPS << 1)
  1145. /* If this bit is set, then . doesn't match NUL.
  1146. If not set, then it does. */
  1147. #define RE_DOT_NOT_NULL (RE_DOT_NEWLINE << 1)
  1148. /* If this bit is set, nonmatching lists [^...] do not match newline.
  1149. If not set, they do. */
  1150. #define RE_HAT_LISTS_NOT_NEWLINE (RE_DOT_NOT_NULL << 1)
  1151. /* If this bit is set, either \{...\} or {...} defines an
  1152. interval, depending on RE_NO_BK_BRACES.
  1153. If not set, \{, \}, {, and } are literals. */
  1154. #define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1)
  1155. /* If this bit is set, +, ? and | aren't recognized as operators.
  1156. If not set, they are. */
  1157. #define RE_LIMITED_OPS (RE_INTERVALS << 1)
  1158. /* If this bit is set, newline is an alternation operator.
  1159. If not set, newline is literal. */
  1160. #define RE_NEWLINE_ALT (RE_LIMITED_OPS << 1)
  1161. /* If this bit is set, then `{...}' defines an interval, and \{ and \}
  1162. are literals.
  1163. If not set, then `\{...\}' defines an interval. */
  1164. #define RE_NO_BK_BRACES (RE_NEWLINE_ALT << 1)
  1165. /* If this bit is set, (...) defines a group, and \( and \) are literals.
  1166. If not set, \(...\) defines a group, and ( and ) are literals. */
  1167. #define RE_NO_BK_PARENS (RE_NO_BK_BRACES << 1)
  1168. /* If this bit is set, then \<digit> matches <digit>.
  1169. If not set, then \<digit> is a back-reference. */
  1170. #define RE_NO_BK_REFS (RE_NO_BK_PARENS << 1)
  1171. /* If this bit is set, then | is an alternation operator, and \| is literal.
  1172. If not set, then \| is an alternation operator, and | is literal. */
  1173. #define RE_NO_BK_VBAR (RE_NO_BK_REFS << 1)
  1174. /* If this bit is set, then an ending range point collating higher
  1175. than the starting range point, as in [z-a], is invalid.
  1176. If not set, then when ending range point collates higher than the
  1177. starting range point, the range is ignored. */
  1178. #define RE_NO_EMPTY_RANGES (RE_NO_BK_VBAR << 1)
  1179. /* If this bit is set, then an unmatched ) is ordinary.
  1180. If not set, then an unmatched ) is invalid. */
  1181. #define RE_UNMATCHED_RIGHT_PAREN_ORD (RE_NO_EMPTY_RANGES << 1)
  1182. /* If this bit is set, do not process the GNU regex operators.
  1183. IF not set, then the GNU regex operators are recognized. */
  1184. #define RE_NO_GNU_OPS (RE_UNMATCHED_RIGHT_PAREN_ORD << 1)
  1185. /* This global variable defines the particular regexp syntax to use (for
  1186. some interfaces). When a regexp is compiled, the syntax used is
  1187. stored in the pattern buffer, so changing this does not affect
  1188. already-compiled regexps. */
  1189. extern reg_syntax_t re_syntax_options;
  1190. /* Define combinations of the above bits for the standard possibilities.
  1191. (The [[[ comments delimit what gets put into the Texinfo file, so
  1192. don't delete them!) */
  1193. /* [[[begin syntaxes]]] */
  1194. #define RE_SYNTAX_EMACS 0
  1195. #define RE_SYNTAX_AWK \
  1196. (RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DOT_NOT_NULL \
  1197. | RE_NO_BK_PARENS | RE_NO_BK_REFS \
  1198. | RE_NO_BK_VBAR | RE_NO_EMPTY_RANGES \
  1199. | RE_DOT_NEWLINE \
  1200. | RE_UNMATCHED_RIGHT_PAREN_ORD | RE_NO_GNU_OPS)
  1201. #define RE_SYNTAX_GNU_AWK \
  1202. ((RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS) \
  1203. & ~(RE_DOT_NOT_NULL|RE_INTERVALS))
  1204. #define RE_SYNTAX_POSIX_AWK \
  1205. (RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS | RE_NO_GNU_OPS)
  1206. #define RE_SYNTAX_GREP \
  1207. (RE_BK_PLUS_QM | RE_CHAR_CLASSES \
  1208. | RE_HAT_LISTS_NOT_NEWLINE | RE_INTERVALS \
  1209. | RE_NEWLINE_ALT)
  1210. #define RE_SYNTAX_EGREP \
  1211. (RE_CHAR_CLASSES | RE_CONTEXT_INDEP_ANCHORS \
  1212. | RE_CONTEXT_INDEP_OPS | RE_HAT_LISTS_NOT_NEWLINE \
  1213. | RE_NEWLINE_ALT | RE_NO_BK_PARENS \
  1214. | RE_NO_BK_VBAR)
  1215. #define RE_SYNTAX_POSIX_EGREP \
  1216. (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES)
  1217. /* P1003.2/D11.2, section 4.20.7.1, lines 5078ff. */
  1218. #define RE_SYNTAX_ED RE_SYNTAX_POSIX_BASIC
  1219. #define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC
  1220. /* Syntax bits common to both basic and extended POSIX regex syntax. */
  1221. #define _RE_SYNTAX_POSIX_COMMON \
  1222. (RE_CHAR_CLASSES | RE_DOT_NEWLINE | RE_DOT_NOT_NULL \
  1223. | RE_INTERVALS | RE_NO_EMPTY_RANGES)
  1224. #define RE_SYNTAX_POSIX_BASIC \
  1225. (_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM)
  1226. /* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes
  1227. RE_LIMITED_OPS, i.e., \? \+ \| are not recognized. Actually, this
  1228. isn't minimal, since other operators, such as \`, aren't disabled. */
  1229. #define RE_SYNTAX_POSIX_MINIMAL_BASIC \
  1230. (_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS)
  1231. #define RE_SYNTAX_POSIX_EXTENDED \
  1232. (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
  1233. | RE_CONTEXT_INDEP_OPS | RE_NO_BK_BRACES \
  1234. | RE_NO_BK_PARENS | RE_NO_BK_VBAR \
  1235. | RE_UNMATCHED_RIGHT_PAREN_ORD)
  1236. /* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INVALID_OPS
  1237. replaces RE_CONTEXT_INDEP_OPS and RE_NO_BK_REFS is added. */
  1238. #define RE_SYNTAX_POSIX_MINIMAL_EXTENDED \
  1239. (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
  1240. | RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES \
  1241. | RE_NO_BK_PARENS | RE_NO_BK_REFS \
  1242. | RE_NO_BK_VBAR | RE_UNMATCHED_RIGHT_PAREN_ORD)
  1243. /* [[[end syntaxes]]] */
  1244. /* Maximum number of duplicates an interval can allow. Some systems
  1245. (erroneously) define this in other header files, but we want our
  1246. value, so remove any previous define. */
  1247. #ifdef RE_DUP_MAX
  1248. #undef RE_DUP_MAX
  1249. #endif
  1250. /* if sizeof(int) == 2, then ((1 << 15) - 1) overflows */
  1251. #define RE_DUP_MAX (0x7fff)
  1252. /* POSIX `cflags' bits (i.e., information for `regcomp'). */
  1253. /* If this bit is set, then use extended regular expression syntax.
  1254. If not set, then use basic regular expression syntax. */
  1255. #define REG_EXTENDED 1
  1256. /* If this bit is set, then ignore case when matching.
  1257. If not set, then case is significant. */
  1258. #define REG_ICASE (REG_EXTENDED << 1)
  1259. /* If this bit is set, then anchors do not match at newline
  1260. characters in the string.
  1261. If not set, then anchors do match at newlines. */
  1262. #define REG_NEWLINE (REG_ICASE << 1)
  1263. /* If this bit is set, then report only success or fail in regexec.
  1264. If not set, then returns differ between not matching and errors. */
  1265. #define REG_NOSUB (REG_NEWLINE << 1)
  1266. /* POSIX `eflags' bits (i.e., information for regexec). */
  1267. /* If this bit is set, then the beginning-of-line operator doesn't match
  1268. the beginning of the string (presumably because it's not the
  1269. beginning of a line).
  1270. If not set, then the beginning-of-line operator does match the
  1271. beginning of the string. */
  1272. #define REG_NOTBOL 1
  1273. /* Like REG_NOTBOL, except for the end-of-line. */
  1274. #define REG_NOTEOL (1 << 1)
  1275. /* If `regs_allocated' is REGS_UNALLOCATED in the pattern buffer,
  1276. * `re_match_2' returns information about at least this many registers
  1277. * the first time a `regs' structure is passed.
  1278. *
  1279. * Also, this is the greatest number of backreferenced subexpressions
  1280. * allowed in a pattern being matched without caller-supplied registers.
  1281. */
  1282. #ifndef RE_NREGS
  1283. #define RE_NREGS 30
  1284. #endif
  1285. extern int rx_cache_bound;
  1286. extern char rx_version_string[];
  1287. #ifdef RX_WANT_RX_DEFS
  1288. /* This is decls to the interesting subsystems and lower layers
  1289. * of rx. Everything which doesn't have a public counterpart in
  1290. * regex.c is declared here.
  1291. */
  1292. #ifdef __STDC__
  1293. typedef void (*rx_hash_freefn) (struct rx_hash_item * it);
  1294. #else /* ndef __STDC__ */
  1295. typedef void (*rx_hash_freefn) ();
  1296. #endif /* ndef __STDC__ */
  1297. #ifdef __STDC__
  1298. RX_DECL int rx_bitset_is_equal (int size, rx_Bitset a, rx_Bitset b);
  1299. RX_DECL int rx_bitset_is_subset (int size, rx_Bitset a, rx_Bitset b);
  1300. RX_DECL int rx_bitset_empty (int size, rx_Bitset set);
  1301. RX_DECL void rx_bitset_null (int size, rx_Bitset b);
  1302. RX_DECL void rx_bitset_universe (int size, rx_Bitset b);
  1303. RX_DECL void rx_bitset_complement (int size, rx_Bitset b);
  1304. RX_DECL void rx_bitset_assign (int size, rx_Bitset a, rx_Bitset b);
  1305. RX_DECL void rx_bitset_union (int size, rx_Bitset a, rx_Bitset b);
  1306. RX_DECL void rx_bitset_intersection (int size,
  1307. rx_Bitset a, rx_Bitset b);
  1308. RX_DECL void rx_bitset_difference (int size, rx_Bitset a, rx_Bitset b);
  1309. RX_DECL void rx_bitset_revdifference (int size,
  1310. rx_Bitset a, rx_Bitset b);
  1311. RX_DECL void rx_bitset_xor (int size, rx_Bitset a, rx_Bitset b);
  1312. RX_DECL unsigned long rx_bitset_hash (int size, rx_Bitset b);
  1313. RX_DECL struct rx_hash_item * rx_hash_find (struct rx_hash * table,
  1314. unsigned long hash,
  1315. void * value,
  1316. struct rx_hash_rules * rules);
  1317. RX_DECL struct rx_hash_item * rx_hash_store (struct rx_hash * table,
  1318. unsigned long hash,
  1319. void * value,
  1320. struct rx_hash_rules * rules);
  1321. RX_DECL void rx_hash_free (struct rx_hash_item * it, struct rx_hash_rules * rules);
  1322. RX_DECL void rx_free_hash_table (struct rx_hash * tab, rx_hash_freefn freefn,
  1323. struct rx_hash_rules * rules);
  1324. RX_DECL rx_Bitset rx_cset (struct rx *rx);
  1325. RX_DECL rx_Bitset rx_copy_cset (struct rx *rx, rx_Bitset a);
  1326. RX_DECL void rx_free_cset (struct rx * rx, rx_Bitset c);
  1327. RX_DECL struct rexp_node * rexp_node (struct rx *rx,
  1328. enum rexp_node_type type);
  1329. RX_DECL struct rexp_node * rx_mk_r_cset (struct rx * rx,
  1330. rx_Bitset b);
  1331. RX_DECL struct rexp_node * rx_mk_r_concat (struct rx * rx,
  1332. struct rexp_node * a,
  1333. struct rexp_node * b);
  1334. RX_DECL struct rexp_node * rx_mk_r_alternate (struct rx * rx,
  1335. struct rexp_node * a,
  1336. struct rexp_node * b);
  1337. RX_DECL struct rexp_node * rx_mk_r_opt (struct rx * rx,
  1338. struct rexp_node * a);
  1339. RX_DECL struct rexp_node * rx_mk_r_star (struct rx * rx,
  1340. struct rexp_node * a);
  1341. RX_DECL struct rexp_node * rx_mk_r_2phase_star (struct rx * rx,
  1342. struct rexp_node * a,
  1343. struct rexp_node * b);
  1344. RX_DECL struct rexp_node * rx_mk_r_side_effect (struct rx * rx,
  1345. rx_side_effect a);
  1346. RX_DECL struct rexp_node * rx_mk_r_data (struct rx * rx,
  1347. void * a);
  1348. RX_DECL void rx_free_rexp (struct rx * rx, struct rexp_node * node);
  1349. RX_DECL struct rexp_node * rx_copy_rexp (struct rx *rx,
  1350. struct rexp_node *node);
  1351. RX_DECL struct rx_nfa_state * rx_nfa_state (struct rx *rx);
  1352. RX_DECL void rx_free_nfa_state (struct rx_nfa_state * n);
  1353. RX_DECL struct rx_nfa_state * rx_id_to_nfa_state (struct rx * rx,
  1354. int id);
  1355. RX_DECL struct rx_nfa_edge * rx_nfa_edge (struct rx *rx,
  1356. enum rx_nfa_etype type,
  1357. struct rx_nfa_state *start,
  1358. struct rx_nfa_state *dest);
  1359. RX_DECL void rx_free_nfa_edge (struct rx_nfa_edge * e);
  1360. RX_DECL void rx_free_nfa (struct rx *rx);
  1361. RX_DECL int rx_build_nfa (struct rx *rx,
  1362. struct rexp_node *rexp,
  1363. struct rx_nfa_state **start,
  1364. struct rx_nfa_state **end);
  1365. RX_DECL void rx_name_nfa_states (struct rx *rx);
  1366. RX_DECL int rx_eclose_nfa (struct rx *rx);
  1367. RX_DECL void rx_delete_epsilon_transitions (struct rx *rx);
  1368. RX_DECL int rx_compactify_nfa (struct rx *rx,
  1369. void **mem, unsigned long *size);
  1370. RX_DECL void rx_release_superset (struct rx *rx,
  1371. struct rx_superset *set);
  1372. RX_DECL struct rx_superset * rx_superset_cons (struct rx * rx,
  1373. struct rx_nfa_state *car, struct rx_superset *cdr);
  1374. RX_DECL struct rx_superset * rx_superstate_eclosure_union
  1375. (struct rx * rx, struct rx_superset *set, struct rx_nfa_state_set *ecl);
  1376. RX_DECL struct rx_superstate * rx_superstate (struct rx *rx,
  1377. struct rx_superset *set);
  1378. RX_DECL struct rx_inx * rx_handle_cache_miss
  1379. (struct rx *rx, struct rx_superstate *super, unsigned char chr, void *data);
  1380. RX_DECL reg_errcode_t rx_compile (__const__ char *pattern, int size,
  1381. reg_syntax_t syntax,
  1382. struct re_pattern_buffer * rxb);
  1383. RX_DECL void rx_blow_up_fastmap (struct re_pattern_buffer * rxb);
  1384. #else /* STDC */
  1385. RX_DECL int rx_bitset_is_equal ();
  1386. RX_DECL int rx_bitset_is_subset ();
  1387. RX_DECL int rx_bitset_empty ();
  1388. RX_DECL void rx_bitset_null ();
  1389. RX_DECL void rx_bitset_universe ();
  1390. RX_DECL void rx_bitset_complement ();
  1391. RX_DECL void rx_bitset_assign ();
  1392. RX_DECL void rx_bitset_union ();
  1393. RX_DECL void rx_bitset_intersection ();
  1394. RX_DECL void rx_bitset_difference ();
  1395. RX_DECL void rx_bitset_revdifference ();
  1396. RX_DECL void rx_bitset_xor ();
  1397. RX_DECL unsigned long rx_bitset_hash ();
  1398. RX_DECL struct rx_hash_item * rx_hash_find ();
  1399. RX_DECL struct rx_hash_item * rx_hash_store ();
  1400. RX_DECL void rx_hash_free ();
  1401. RX_DECL void rx_free_hash_table ();
  1402. RX_DECL rx_Bitset rx_cset ();
  1403. RX_DECL rx_Bitset rx_copy_cset ();
  1404. RX_DECL void rx_free_cset ();
  1405. RX_DECL struct rexp_node * rexp_node ();
  1406. RX_DECL struct rexp_node * rx_mk_r_cset ();
  1407. RX_DECL struct rexp_node * rx_mk_r_concat ();
  1408. RX_DECL struct rexp_node * rx_mk_r_alternate ();
  1409. RX_DECL struct rexp_node * rx_mk_r_opt ();
  1410. RX_DECL struct rexp_node * rx_mk_r_star ();
  1411. RX_DECL struct rexp_node * rx_mk_r_2phase_star ();
  1412. RX_DECL struct rexp_node * rx_mk_r_side_effect ();
  1413. RX_DECL struct rexp_node * rx_mk_r_data ();
  1414. RX_DECL void rx_free_rexp ();
  1415. RX_DECL struct rexp_node * rx_copy_rexp ();
  1416. RX_DECL struct rx_nfa_state * rx_nfa_state ();
  1417. RX_DECL void rx_free_nfa_state ();
  1418. RX_DECL struct rx_nfa_state * rx_id_to_nfa_state ();
  1419. RX_DECL struct rx_nfa_edge * rx_nfa_edge ();
  1420. RX_DECL void rx_free_nfa_edge ();
  1421. RX_DECL void rx_free_nfa ();
  1422. RX_DECL int rx_build_nfa ();
  1423. RX_DECL void rx_name_nfa_states ();
  1424. RX_DECL int rx_eclose_nfa ();
  1425. RX_DECL void rx_delete_epsilon_transitions ();
  1426. RX_DECL int rx_compactify_nfa ();
  1427. RX_DECL void rx_release_superset ();
  1428. RX_DECL struct rx_superset * rx_superset_cons ();
  1429. RX_DECL struct rx_superset * rx_superstate_eclosure_union ();
  1430. RX_DECL struct rx_superstate * rx_superstate ();
  1431. RX_DECL struct rx_inx * rx_handle_cache_miss ();
  1432. RX_DECL reg_errcode_t rx_compile ();
  1433. RX_DECL void rx_blow_up_fastmap ();
  1434. #endif /* STDC */
  1435. #endif /* RX_WANT_RX_DEFS */
  1436. #ifdef __STDC__
  1437. extern int re_search_2 (struct re_pattern_buffer *rxb,
  1438. __const__ char * string1, int size1,
  1439. __const__ char * string2, int size2,
  1440. int startpos, int range,
  1441. struct re_registers *regs,
  1442. int stop);
  1443. extern int re_search (struct re_pattern_buffer * rxb, __const__ char *string,
  1444. int size, int startpos, int range,
  1445. struct re_registers *regs);
  1446. extern int re_match_2 (struct re_pattern_buffer * rxb,
  1447. __const__ char * string1, int size1,
  1448. __const__ char * string2, int size2,
  1449. int pos, struct re_registers *regs, int stop);
  1450. extern int re_match (struct re_pattern_buffer * rxb,
  1451. __const__ char * string,
  1452. int size, int pos,
  1453. struct re_registers *regs);
  1454. extern reg_syntax_t re_set_syntax (reg_syntax_t syntax);
  1455. extern void re_set_registers (struct re_pattern_buffer *bufp,
  1456. struct re_registers *regs,
  1457. unsigned num_regs,
  1458. regoff_t * starts, regoff_t * ends);
  1459. extern __const__ char * re_compile_pattern (__const__ char *pattern,
  1460. int length,
  1461. struct re_pattern_buffer * rxb);
  1462. extern int re_compile_fastmap (struct re_pattern_buffer * rxb);
  1463. extern char * re_comp (__const__ char *s);
  1464. extern int re_exec (__const__ char *s);
  1465. extern int regcomp (regex_t * preg, __const__ char * pattern, int cflags);
  1466. extern int regexec (__const__ regex_t *preg, __const__ char *string,
  1467. size_t nmatch, regmatch_t pmatch[],
  1468. int eflags);
  1469. extern size_t regerror (int errcode, __const__ regex_t *preg,
  1470. char *errbuf, size_t errbuf_size);
  1471. extern void regfree (regex_t *preg);
  1472. #else /* STDC */
  1473. extern int re_search_2 ();
  1474. extern int re_search ();
  1475. extern int re_match_2 ();
  1476. extern int re_match ();
  1477. extern reg_syntax_t re_set_syntax ();
  1478. extern void re_set_registers ();
  1479. extern __const__ char * re_compile_pattern ();
  1480. extern int re_compile_fastmap ();
  1481. extern char * re_comp ();
  1482. extern int re_exec ();
  1483. extern int regcomp ();
  1484. extern int regexec ();
  1485. extern size_t regerror ();
  1486. extern void regfree ();
  1487. #endif /* STDC */
  1488. #ifdef RX_WANT_RX_DEFS
  1489. struct rx_counter_frame
  1490. {
  1491. int tag;
  1492. int val;
  1493. struct rx_counter_frame * inherited_from; /* If this is a copy. */
  1494. struct rx_counter_frame * cdr;
  1495. };
  1496. struct rx_backtrack_frame
  1497. {
  1498. char * counter_stack_sp;
  1499. /* A frame is used to save the matchers state when it crosses a
  1500. * backtracking point. The `stk_' fields correspond to variables
  1501. * in re_search_2 (just strip off thes `stk_'). They are documented
  1502. * tere.
  1503. */
  1504. struct rx_superstate * stk_super;
  1505. unsigned int stk_c;
  1506. struct rx_string_position stk_test_pos;
  1507. int stk_last_l;
  1508. int stk_last_r;
  1509. int stk_test_ret;
  1510. /* This is the list of options left to explore at the backtrack
  1511. * point for which this frame was created.
  1512. */
  1513. struct rx_distinct_future * df;
  1514. struct rx_distinct_future * first_df;
  1515. #ifdef RX_DEBUG
  1516. int stk_line_no;
  1517. #endif
  1518. };
  1519. struct rx_stack_chunk
  1520. {
  1521. struct rx_stack_chunk * next_chunk;
  1522. int bytes_left;
  1523. char * sp;
  1524. };
  1525. enum rx_outer_entry
  1526. {
  1527. rx_outer_start,
  1528. rx_outer_fastmap,
  1529. rx_outer_test,
  1530. rx_outer_restore_pos
  1531. };
  1532. enum rx_fastmap_return
  1533. {
  1534. rx_fastmap_continuation,
  1535. rx_fastmap_error,
  1536. rx_fastmap_ok,
  1537. rx_fastmap_fail
  1538. };
  1539. enum rx_fastmap_entry
  1540. {
  1541. rx_fastmap_start,
  1542. rx_fastmap_string_break
  1543. };
  1544. enum rx_test_return
  1545. {
  1546. rx_test_continuation,
  1547. rx_test_error,
  1548. rx_test_fail,
  1549. rx_test_ok
  1550. };
  1551. enum rx_test_internal_return
  1552. {
  1553. rx_test_internal_error,
  1554. rx_test_found_first,
  1555. rx_test_line_finished
  1556. };
  1557. enum rx_test_match_entry
  1558. {
  1559. rx_test_start,
  1560. rx_test_cache_hit_loop,
  1561. rx_test_backreference_check,
  1562. rx_test_backtrack_return
  1563. };
  1564. struct rx_search_state
  1565. {
  1566. /* Two groups of registers are kept. The group with the register state
  1567. * of the current test match, and the group that holds the state at the end
  1568. * of the best known match, if any.
  1569. *
  1570. * For some patterns, there may also be registers saved on the stack.
  1571. */
  1572. unsigned num_regs; /* Includes an element for register zero. */
  1573. regoff_t * lparen; /* scratch space for register returns */
  1574. regoff_t * rparen;
  1575. regoff_t * best_lpspace; /* in case the user doesn't want these */
  1576. regoff_t * best_rpspace; /* values, we still need space to store
  1577. * them. Normally, this memoryis unused
  1578. * and the space pointed to by REGS is
  1579. * used instead.
  1580. */
  1581. int last_l; /* Highest index of a valid lparen. */
  1582. int last_r; /* It's dual. */
  1583. int * best_lparen; /* This contains the best known register */
  1584. int * best_rparen; /* assignments.
  1585. * This may point to the same mem as
  1586. * best_lpspace, or it might point to memory
  1587. * passed by the caller.
  1588. */
  1589. int best_last_l; /* best_last_l:best_lparen::last_l:lparen */
  1590. int best_last_r;
  1591. unsigned char * translate;
  1592. struct rx_string_position outer_pos;
  1593. struct rx_superstate * start_super;
  1594. int nfa_choice;
  1595. int first_found; /* If true, return after finding any match. */
  1596. int ret_val;
  1597. /* For continuations... */
  1598. enum rx_outer_entry outer_search_resume_pt;
  1599. struct re_pattern_buffer * saved_rxb;
  1600. int saved_startpos;
  1601. int saved_range;
  1602. int saved_stop;
  1603. int saved_total_size;
  1604. rx_get_burst_fn saved_get_burst;
  1605. rx_back_check_fn saved_back_check;
  1606. struct re_registers * saved_regs;
  1607. /**
  1608. ** state for fastmap
  1609. **/
  1610. char * fastmap;
  1611. int fastmap_chr;
  1612. int fastmap_val;
  1613. /* for continuations in the fastmap procedure: */
  1614. enum rx_fastmap_entry fastmap_resume_pt;
  1615. /**
  1616. ** state for test_match
  1617. **/
  1618. /* The current superNFA position of the matcher. */
  1619. struct rx_superstate * super;
  1620. /* The matcher interprets a series of instruction frames.
  1621. * This is the `instruction counter' for the interpretation.
  1622. */
  1623. struct rx_inx * ifr;
  1624. /* We insert a ghost character in the string to prime
  1625. * the nfa. test_pos.pos, test_pos.str_half, and test_pos.end_half
  1626. * keep track of the test-match position and string-half.
  1627. */
  1628. unsigned char c;
  1629. /* Position within the string. */
  1630. struct rx_string_position test_pos;
  1631. struct rx_stack_chunk * counter_stack;
  1632. struct rx_stack_chunk * backtrack_stack;
  1633. int backtrack_frame_bytes;
  1634. int chunk_bytes;
  1635. struct rx_stack_chunk * free_chunks;
  1636. /* To return from this function, set test_ret and
  1637. * `goto test_do_return'.
  1638. *
  1639. * Possible return values are:
  1640. * 1 --- end of string while the superNFA is still going
  1641. * 0 --- internal error (out of memory)
  1642. * -1 --- search completed by reaching the superNFA fail state
  1643. * -2 --- a match was found, maybe not the longest.
  1644. *
  1645. * When the search is complete (-1), best_last_r indicates whether
  1646. * a match was found.
  1647. *
  1648. * -2 is return only if search_state.first_found is non-zero.
  1649. *
  1650. * if search_state.first_found is non-zero, a return of -1 indicates no match,
  1651. * otherwise, best_last_r has to be checked.
  1652. */
  1653. int test_ret;
  1654. int could_have_continued;
  1655. #ifdef RX_DEBUG
  1656. int backtrack_depth;
  1657. /* There is a search tree with every node as set of deterministic
  1658. * transitions in the super nfa. For every branch of a
  1659. * backtrack point is an edge in the tree.
  1660. * This counts up a pre-order of nodes in that tree.
  1661. * It's saved on the search stack and printed when debugging.
  1662. */
  1663. int line_no;
  1664. int lines_found;
  1665. #endif
  1666. /* For continuations within the match tester */
  1667. enum rx_test_match_entry test_match_resume_pt;
  1668. struct rx_inx * saved_next_tr_table;
  1669. struct rx_inx * saved_this_tr_table;
  1670. int saved_reg;
  1671. struct rx_backtrack_frame * saved_bf;
  1672. };
  1673. static __inline__ void init_fastmap( struct re_pattern_buffer *,
  1674. struct rx_search_state * );
  1675. extern char rx_slowmap[];
  1676. extern unsigned char rx_id_translation[];
  1677. static __inline__ void
  1678. init_fastmap( struct re_pattern_buffer * rxb,
  1679. struct rx_search_state * search_state )
  1680. {
  1681. search_state->fastmap = (rxb->fastmap
  1682. ? (char *)rxb->fastmap
  1683. : (char *)rx_slowmap);
  1684. /* Update the fastmap now if not correct already.
  1685. * When the regexp was compiled, the fastmap was computed
  1686. * and stored in a bitset. This expands the bitset into a
  1687. * character array containing 1s and 0s.
  1688. */
  1689. if ((search_state->fastmap == rxb->fastmap) && !rxb->fastmap_accurate)
  1690. rx_blow_up_fastmap (rxb);
  1691. search_state->fastmap_chr = -1;
  1692. search_state->fastmap_val = 0;
  1693. search_state->fastmap_resume_pt = rx_fastmap_start;
  1694. }
  1695. static __inline__ void
  1696. uninit_fastmap ( struct re_pattern_buffer * rxb,
  1697. struct rx_search_state * search_state )
  1698. {
  1699. /* Unset the fastmap sentinel */
  1700. if (search_state->fastmap_chr >= 0)
  1701. search_state->fastmap[search_state->fastmap_chr]
  1702. = search_state->fastmap_val;
  1703. }
  1704. static __inline__ int
  1705. fastmap_search ( struct re_pattern_buffer * rxb, int stop,
  1706. rx_get_burst_fn get_burst, void * app_closure,
  1707. struct rx_search_state * search_state )
  1708. {
  1709. enum rx_fastmap_entry pc;
  1710. if (0)
  1711. {
  1712. return_continuation:
  1713. search_state->fastmap_resume_pt = pc;
  1714. return rx_fastmap_continuation;
  1715. }
  1716. pc = search_state->fastmap_resume_pt;
  1717. switch (pc)
  1718. {
  1719. default:
  1720. return rx_fastmap_error;
  1721. case rx_fastmap_start:
  1722. init_fastmap_sentinal:
  1723. /* For the sake of fast fastmapping, set a sentinal in the fastmap.
  1724. * This sentinal will trap the fastmap loop when it reaches the last
  1725. * valid character in a string half.
  1726. *
  1727. * This must be reset when the fastmap/search loop crosses a string
  1728. * boundry, and before returning to the caller. So sometimes,
  1729. * the fastmap loop is restarted with `continue', othertimes by
  1730. * `goto init_fastmap_sentinal'.
  1731. */
  1732. if (search_state->outer_pos.size)
  1733. {
  1734. search_state->fastmap_chr = ((search_state->outer_pos.search_direction == 1)
  1735. ? *(search_state->outer_pos.end - 1)
  1736. : *search_state->outer_pos.string);
  1737. search_state->fastmap_val
  1738. = search_state->fastmap[search_state->fastmap_chr];
  1739. search_state->fastmap[search_state->fastmap_chr] = 1;
  1740. }
  1741. else
  1742. {
  1743. search_state->fastmap_chr = -1;
  1744. search_state->fastmap_val = 0;
  1745. }
  1746. if (search_state->outer_pos.pos >= search_state->outer_pos.end)
  1747. goto fastmap_hit_bound;
  1748. else
  1749. {
  1750. if (search_state->outer_pos.search_direction == 1)
  1751. {
  1752. if (search_state->fastmap_val)
  1753. {
  1754. for (;;)
  1755. {
  1756. while (!search_state->fastmap[*search_state->outer_pos.pos])
  1757. ++search_state->outer_pos.pos;
  1758. return rx_fastmap_ok;
  1759. }
  1760. }
  1761. else
  1762. {
  1763. for (;;)
  1764. {
  1765. while (!search_state->fastmap[*search_state->outer_pos.pos])
  1766. ++search_state->outer_pos.pos;
  1767. if (*search_state->outer_pos.pos != search_state->fastmap_chr)
  1768. return rx_fastmap_ok;
  1769. else
  1770. {
  1771. ++search_state->outer_pos.pos;
  1772. if (search_state->outer_pos.pos == search_state->outer_pos.end)
  1773. goto fastmap_hit_bound;
  1774. }
  1775. }
  1776. }
  1777. }
  1778. else
  1779. {
  1780. __const__ unsigned char * bound;
  1781. bound = search_state->outer_pos.string - 1;
  1782. if (search_state->fastmap_val)
  1783. {
  1784. for (;;)
  1785. {
  1786. while (!search_state->fastmap[*search_state->outer_pos.pos])
  1787. --search_state->outer_pos.pos;
  1788. return rx_fastmap_ok;
  1789. }
  1790. }
  1791. else
  1792. {
  1793. for (;;)
  1794. {
  1795. while (!search_state->fastmap[*search_state->outer_pos.pos])
  1796. --search_state->outer_pos.pos;
  1797. if ((*search_state->outer_pos.pos != search_state->fastmap_chr) || search_state->fastmap_val)
  1798. return rx_fastmap_ok;
  1799. else
  1800. {
  1801. --search_state->outer_pos.pos;
  1802. if (search_state->outer_pos.pos == bound)
  1803. goto fastmap_hit_bound;
  1804. }
  1805. }
  1806. }
  1807. }
  1808. }
  1809. case rx_fastmap_string_break:
  1810. fastmap_hit_bound:
  1811. {
  1812. /* If we hit a bound, it may be time to fetch another burst
  1813. * of string, or it may be time to return a continuation to
  1814. * the caller, or it might be time to fail.
  1815. */
  1816. int burst_state;
  1817. burst_state = get_burst (&search_state->outer_pos, app_closure, stop);
  1818. switch (burst_state)
  1819. {
  1820. default:
  1821. case rx_get_burst_error:
  1822. return rx_fastmap_error;
  1823. case rx_get_burst_continuation:
  1824. {
  1825. pc = rx_fastmap_string_break;
  1826. goto return_continuation;
  1827. }
  1828. case rx_get_burst_ok:
  1829. goto init_fastmap_sentinal;
  1830. case rx_get_burst_no_more:
  1831. /* ...not a string split, simply no more string.
  1832. *
  1833. * When searching backward, running out of string
  1834. * is reason to quit.
  1835. *
  1836. * When searching forward, we allow the possibility
  1837. * of an (empty) match after the last character in the
  1838. * virtual string. So, fall through to the matcher
  1839. */
  1840. return ( (search_state->outer_pos.search_direction == 1)
  1841. ? rx_fastmap_ok
  1842. : rx_fastmap_fail);
  1843. }
  1844. }
  1845. }
  1846. }
  1847. #ifdef emacs
  1848. /* The `emacs' switch turns on certain matching commands
  1849. * that make sense only in Emacs.
  1850. */
  1851. #include "config.h"
  1852. #include "lisp.h"
  1853. #include "buffer.h"
  1854. #include "syntax.h"
  1855. #endif /* emacs */
  1856. /* Setting RX_MEMDBUG is useful if you have dbmalloc. Maybe with similar
  1857. * packages too.
  1858. */
  1859. #ifdef RX_MEMDBUG
  1860. #include <malloc.h>
  1861. #endif /* RX_RX_MEMDBUG */
  1862. /* We used to test for `BSTRING' here, but only GCC and Emacs define
  1863. * `BSTRING', as far as I know, and neither of them use this code.
  1864. */
  1865. #if HAVE_STRING_H || __STDC__
  1866. #include <string.h>
  1867. #ifndef bcmp
  1868. #define bcmp(s1, s2, n) memcmp ((s1), (s2), (n))
  1869. #endif
  1870. #ifndef bcopy
  1871. #define bcopy(s, d, n) memcpy ((d), (s), (n))
  1872. #endif
  1873. #ifndef bzero
  1874. #define bzero(s, n) memset ((s), 0, (n))
  1875. #endif
  1876. #else /* HAVE_STRING_H || __STDC__ */
  1877. #include <strings.h>
  1878. #endif /* not (HAVE_STRING_H || __STDC__) */
  1879. #ifdef __STDC__
  1880. #include <stdlib.h>
  1881. #else /* not __STDC__ */
  1882. char *malloc ();
  1883. char *realloc ();
  1884. #endif /* not __STDC__ */
  1885. /* How many characters in the character set. */
  1886. #define CHAR_SET_SIZE (1 << CHARBITS)
  1887. #ifndef emacs
  1888. /* Define the syntax basics for \<, \>, etc.
  1889. * This must be nonzero for the wordchar and notwordchar pattern
  1890. * commands in re_match_2.
  1891. */
  1892. #ifndef Sword
  1893. #define Sword 1
  1894. #endif
  1895. #define SYNTAX(c) re_syntax_table[c]
  1896. RX_DECL char re_syntax_table[CHAR_SET_SIZE];
  1897. #endif /* not emacs */
  1898. /* Test if at very beginning or at very end of the virtual concatenation
  1899. * of `string1' and `string2'. If only one string, it's `string2'.
  1900. */
  1901. #define AT_STRINGS_BEG() \
  1902. ( -1 \
  1903. == ((search_state.test_pos.pos - search_state.test_pos.string) \
  1904. + search_state.test_pos.offset))
  1905. #define AT_STRINGS_END() \
  1906. ( (total_size - 1) \
  1907. == ((search_state.test_pos.pos - search_state.test_pos.string) \
  1908. + search_state.test_pos.offset))
  1909. /* Test if POS + 1 points to a character which is word-constituent. We have
  1910. * two special cases to check for: if past the end of string1, look at
  1911. * the first character in string2; and if before the beginning of
  1912. * string2, look at the last character in string1.
  1913. *
  1914. * Assumes `string1' exists, so use in conjunction with AT_STRINGS_BEG ().
  1915. */
  1916. #define LETTER_P(POS,OFF) \
  1917. ( SYNTAX (fetch_char(POS, OFF, app_closure, stop)) \
  1918. == Sword)
  1919. /* Test if the character at D and the one after D differ with respect
  1920. * to being word-constituent.
  1921. */
  1922. #define AT_WORD_BOUNDARY(d) \
  1923. (AT_STRINGS_BEG () || AT_STRINGS_END () || LETTER_P (d,0) != LETTER_P (d, 1))
  1924. #ifdef RX_SUPPORT_CONTINUATIONS
  1925. #define RX_STACK_ALLOC(BYTES) malloc(BYTES)
  1926. #define RX_STACK_FREE(MEM) free(MEM)
  1927. #else
  1928. #define RX_STACK_ALLOC(BYTES) alloca(BYTES)
  1929. #define RX_STACK_FREE(MEM) \
  1930. ((struct rx_stack_chunk *)MEM)->next_chunk = search_state.free_chunks; \
  1931. search_state.free_chunks = ((struct rx_stack_chunk *)MEM);
  1932. #endif
  1933. #define PUSH(CHUNK_VAR,BYTES) \
  1934. if (!CHUNK_VAR || (CHUNK_VAR->bytes_left < (BYTES))) \
  1935. { \
  1936. struct rx_stack_chunk * new_chunk; \
  1937. if (search_state.free_chunks) \
  1938. { \
  1939. new_chunk = search_state.free_chunks; \
  1940. search_state.free_chunks = search_state.free_chunks->next_chunk; \
  1941. } \
  1942. else \
  1943. { \
  1944. new_chunk = (struct rx_stack_chunk *)RX_STACK_ALLOC(search_state.chunk_bytes); \
  1945. if (!new_chunk) \
  1946. { \
  1947. search_state.ret_val = 0; \
  1948. goto test_do_return; \
  1949. } \
  1950. } \
  1951. new_chunk->sp = (char *)new_chunk + sizeof (struct rx_stack_chunk); \
  1952. new_chunk->bytes_left = (search_state.chunk_bytes \
  1953. - (BYTES) \
  1954. - sizeof (struct rx_stack_chunk)); \
  1955. new_chunk->next_chunk = CHUNK_VAR; \
  1956. CHUNK_VAR = new_chunk; \
  1957. } \
  1958. else \
  1959. (CHUNK_VAR->sp += (BYTES)), (CHUNK_VAR->bytes_left -= (BYTES))
  1960. #define POP(CHUNK_VAR,BYTES) \
  1961. if (CHUNK_VAR->sp == ((char *)CHUNK_VAR + sizeof(*CHUNK_VAR))) \
  1962. { \
  1963. struct rx_stack_chunk * new_chunk = CHUNK_VAR->next_chunk; \
  1964. RX_STACK_FREE(CHUNK_VAR); \
  1965. CHUNK_VAR = new_chunk; \
  1966. } \
  1967. else \
  1968. (CHUNK_VAR->sp -= BYTES), (CHUNK_VAR->bytes_left += BYTES)
  1969. #define SRCH_TRANSLATE(C) search_state.translate[(unsigned char) (C)]
  1970. #ifdef __STDC__
  1971. RX_DECL __inline__ int
  1972. rx_search (struct re_pattern_buffer * rxb,
  1973. int startpos,
  1974. int range,
  1975. int stop,
  1976. int total_size,
  1977. rx_get_burst_fn get_burst,
  1978. rx_back_check_fn back_check,
  1979. rx_fetch_char_fn fetch_char,
  1980. void * app_closure,
  1981. struct re_registers * regs,
  1982. struct rx_search_state * resume_state,
  1983. struct rx_search_state * save_state)
  1984. #else
  1985. RX_DECL __inline__ int
  1986. rx_search (rxb, startpos, range, stop, total_size,
  1987. get_burst, back_check, fetch_char,
  1988. app_closure, regs, resume_state, save_state)
  1989. struct re_pattern_buffer * rxb;
  1990. int startpos;
  1991. int range;
  1992. int stop;
  1993. int total_size;
  1994. rx_get_burst_fn get_burst;
  1995. rx_back_check_fn back_check;
  1996. rx_fetch_char_fn fetch_char;
  1997. void * app_closure;
  1998. struct re_registers * regs;
  1999. struct rx_search_state * resume_state;
  2000. struct rx_search_state * save_state;
  2001. #endif
  2002. {
  2003. int pc;
  2004. int test_state;
  2005. struct rx_search_state search_state;
  2006. search_state.free_chunks = 0;
  2007. if (!resume_state)
  2008. pc = rx_outer_start;
  2009. else
  2010. {
  2011. search_state = *resume_state;
  2012. regs = search_state.saved_regs;
  2013. rxb = search_state.saved_rxb;
  2014. startpos = search_state.saved_startpos;
  2015. range = search_state.saved_range;
  2016. stop = search_state.saved_stop;
  2017. total_size = search_state.saved_total_size;
  2018. get_burst = search_state.saved_get_burst;
  2019. back_check = search_state.saved_back_check;
  2020. pc = search_state.outer_search_resume_pt;
  2021. if (0)
  2022. {
  2023. return_continuation:
  2024. if (save_state)
  2025. {
  2026. *save_state = search_state;
  2027. save_state->saved_regs = regs;
  2028. save_state->saved_rxb = rxb;
  2029. save_state->saved_startpos = startpos;
  2030. save_state->saved_range = range;
  2031. save_state->saved_stop = stop;
  2032. save_state->saved_total_size = total_size;
  2033. save_state->saved_get_burst = get_burst;
  2034. save_state->saved_back_check = back_check;
  2035. save_state->outer_search_resume_pt = pc;
  2036. }
  2037. return rx_search_continuation;
  2038. }
  2039. }
  2040. switch (pc)
  2041. {
  2042. case rx_outer_start:
  2043. search_state.ret_val = rx_search_fail;
  2044. ( search_state.lparen
  2045. = search_state.rparen
  2046. = search_state.best_lpspace
  2047. = search_state.best_rpspace
  2048. = 0);
  2049. /* figure the number of registers we may need for use in backreferences.
  2050. * the number here includes an element for register zero.
  2051. */
  2052. search_state.num_regs = rxb->re_nsub + 1;
  2053. /* check for out-of-range startpos. */
  2054. if ((startpos < 0) || (startpos > total_size))
  2055. return rx_search_fail;
  2056. /* fix up range if it might eventually take us outside the string. */
  2057. {
  2058. int endpos;
  2059. endpos = startpos + range;
  2060. if (endpos < -1)
  2061. range = (-1 - startpos);
  2062. else if (endpos > (total_size + 1))
  2063. range = total_size - startpos;
  2064. }
  2065. /* if the search isn't to be a backwards one, don't waste time in a
  2066. * long search for a pattern that says it is anchored.
  2067. */
  2068. if (rxb->begbuf_only && (range > 0))
  2069. {
  2070. if (startpos > 0)
  2071. return rx_search_fail;
  2072. else
  2073. range = 1;
  2074. }
  2075. /* decide whether to use internal or user-provided reg buffers. */
  2076. if (!regs || rxb->no_sub)
  2077. {
  2078. search_state.best_lpspace =
  2079. (regoff_t *)REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t));
  2080. search_state.best_rpspace =
  2081. (regoff_t *)REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t));
  2082. search_state.best_lparen = search_state.best_lpspace;
  2083. search_state.best_rparen = search_state.best_rpspace;
  2084. }
  2085. else
  2086. {
  2087. /* have the register data arrays been allocated? */
  2088. if (rxb->regs_allocated == REGS_UNALLOCATED)
  2089. { /* no. so allocate them with malloc. we need one
  2090. extra element beyond `search_state.num_regs' for the `-1' marker
  2091. gnu code uses. */
  2092. regs->num_regs = MAX (RE_NREGS, rxb->re_nsub + 1);
  2093. regs->start = ((regoff_t *)
  2094. malloc (regs->num_regs * sizeof ( regoff_t)));
  2095. regs->end = ((regoff_t *)
  2096. malloc (regs->num_regs * sizeof ( regoff_t)));
  2097. if (regs->start == 0 || regs->end == 0)
  2098. return rx_search_error;
  2099. rxb->regs_allocated = REGS_REALLOCATE;
  2100. }
  2101. else if (rxb->regs_allocated == REGS_REALLOCATE)
  2102. { /* yes. if we need more elements than were already
  2103. allocated, reallocate them. if we need fewer, just
  2104. leave it alone. */
  2105. if (regs->num_regs < search_state.num_regs + 1)
  2106. {
  2107. regs->num_regs = search_state.num_regs + 1;
  2108. regs->start = ((regoff_t *)
  2109. realloc (regs->start,
  2110. regs->num_regs * sizeof (regoff_t)));
  2111. regs->end = ((regoff_t *)
  2112. realloc (regs->end,
  2113. regs->num_regs * sizeof ( regoff_t)));
  2114. if (regs->start == 0 || regs->end == 0)
  2115. return rx_search_error;
  2116. }
  2117. }
  2118. else if (rxb->regs_allocated != REGS_FIXED)
  2119. return rx_search_error;
  2120. if (regs->num_regs < search_state.num_regs + 1)
  2121. {
  2122. search_state.best_lpspace =
  2123. ((regoff_t *)
  2124. REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t)));
  2125. search_state.best_rpspace =
  2126. ((regoff_t *)
  2127. REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t)));
  2128. search_state.best_lparen = search_state.best_lpspace;
  2129. search_state.best_rparen = search_state.best_rpspace;
  2130. }
  2131. else
  2132. {
  2133. search_state.best_lparen = regs->start;
  2134. search_state.best_rparen = regs->end;
  2135. }
  2136. }
  2137. search_state.lparen =
  2138. (regoff_t *) REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t));
  2139. search_state.rparen =
  2140. (regoff_t *) REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t));
  2141. if (! ( search_state.best_rparen
  2142. && search_state.best_lparen
  2143. && search_state.lparen && search_state.rparen))
  2144. return rx_search_error;
  2145. search_state.best_last_l = search_state.best_last_r = -1;
  2146. search_state.translate = (rxb->translate
  2147. ? rxb->translate
  2148. : rx_id_translation);
  2149. /*
  2150. * two nfa's were compiled.
  2151. * `0' is complete.
  2152. * `1' faster but gets registers wrong and ends too soon.
  2153. */
  2154. search_state.nfa_choice = (regs && !rxb->least_subs) ? '\0' : '\1';
  2155. /* we have the option to look for the best match or the first
  2156. * one we can find. if the user isn't asking for register information,
  2157. * we don't need to find the best match.
  2158. */
  2159. search_state.first_found = !regs;
  2160. if (range >= 0)
  2161. {
  2162. search_state.outer_pos.search_end = startpos + range;
  2163. search_state.outer_pos.search_direction = 1;
  2164. }
  2165. else
  2166. {
  2167. search_state.outer_pos.search_end = startpos + range;
  2168. search_state.outer_pos.search_direction = -1;
  2169. }
  2170. /* the vacuous search always turns up nothing. */
  2171. if ((search_state.outer_pos.search_direction == 1)
  2172. ? (startpos > search_state.outer_pos.search_end)
  2173. : (startpos < search_state.outer_pos.search_end))
  2174. return rx_search_fail;
  2175. /* now we build the starting state of the supernfa. */
  2176. {
  2177. struct rx_superset * start_contents;
  2178. struct rx_nfa_state_set * start_nfa_set;
  2179. /* we presume here that the nfa start state has only one
  2180. * possible future with no side effects.
  2181. */
  2182. start_nfa_set = rxb->start->futures->destset;
  2183. if ( rxb->rx.start_set
  2184. && (rxb->rx.start_set->starts_for == &rxb->rx))
  2185. start_contents = rxb->rx.start_set;
  2186. else
  2187. {
  2188. start_contents =
  2189. rx_superstate_eclosure_union (&rxb->rx,
  2190. rx_superset_cons (&rxb->rx, 0, 0),
  2191. start_nfa_set);
  2192. if (!start_contents)
  2193. return rx_search_fail;
  2194. start_contents->starts_for = &rxb->rx;
  2195. rxb->rx.start_set = start_contents;
  2196. }
  2197. if ( start_contents->superstate
  2198. && (start_contents->superstate->rx_id == rxb->rx.rx_id))
  2199. {
  2200. search_state.start_super = start_contents->superstate;
  2201. rx_lock_superstate (&rxb->rx, search_state.start_super);
  2202. }
  2203. else
  2204. {
  2205. rx_protect_superset (&rxb->rx, start_contents);
  2206. search_state.start_super = rx_superstate (&rxb->rx, start_contents);
  2207. if (!search_state.start_super)
  2208. return rx_search_fail;
  2209. rx_lock_superstate (&rxb->rx, search_state.start_super);
  2210. rx_release_superset (&rxb->rx, start_contents);
  2211. }
  2212. }
  2213. /* The outer_pos tracks the position within the strings
  2214. * as seen by loop that calls fastmap_search.
  2215. *
  2216. * The caller supplied get_burst function actually
  2217. * gives us pointers to chars.
  2218. *
  2219. * Communication with the get_burst function is through an
  2220. * rx_string_position structure. Here, the structure for
  2221. * outer_pos is initialized. It is set to point to the
  2222. * NULL string, at an offset of STARTPOS. STARTPOS is out
  2223. * of range of the NULL string, so the first call to
  2224. * getburst will patch up the rx_string_position to point
  2225. * to valid characters.
  2226. */
  2227. ( search_state.outer_pos.string
  2228. = search_state.outer_pos.end
  2229. = 0);
  2230. search_state.outer_pos.offset = 0;
  2231. search_state.outer_pos.size = 0;
  2232. search_state.outer_pos.pos = (unsigned char *)startpos;
  2233. init_fastmap (rxb, &search_state);
  2234. search_state.fastmap_resume_pt = rx_fastmap_start;
  2235. case rx_outer_fastmap:
  2236. /* do { */
  2237. pseudo_do:
  2238. {
  2239. {
  2240. int fastmap_state;
  2241. fastmap_state = fastmap_search (rxb, stop, get_burst, app_closure,
  2242. &search_state);
  2243. switch (fastmap_state)
  2244. {
  2245. case rx_fastmap_continuation:
  2246. pc = rx_outer_fastmap;
  2247. goto return_continuation;
  2248. case rx_fastmap_fail:
  2249. goto finish;
  2250. case rx_fastmap_ok:
  2251. break;
  2252. }
  2253. }
  2254. /* now the fastmap loop has brought us to a plausible
  2255. * starting point for a match. so, it's time to run the
  2256. * nfa and see if a match occured.
  2257. */
  2258. startpos = ( search_state.outer_pos.pos
  2259. - search_state.outer_pos.string
  2260. + search_state.outer_pos.offset);
  2261. #if 0
  2262. /*|*/ if ((range > 0) && (startpos == search_state.outer_pos.search_end))
  2263. /*|*/ goto finish;
  2264. #endif
  2265. }
  2266. search_state.test_match_resume_pt = rx_test_start;
  2267. /* do interrupted for entry point... */
  2268. case rx_outer_test:
  2269. /* ...do continued */
  2270. {
  2271. goto test_match;
  2272. test_returns_to_search:
  2273. switch (test_state)
  2274. {
  2275. case rx_test_continuation:
  2276. pc = rx_outer_test;
  2277. goto return_continuation;
  2278. case rx_test_error:
  2279. search_state.ret_val = rx_search_error;
  2280. goto finish;
  2281. case rx_test_fail:
  2282. break;
  2283. case rx_test_ok:
  2284. goto finish;
  2285. }
  2286. search_state.outer_pos.pos += search_state.outer_pos.search_direction;
  2287. startpos += search_state.outer_pos.search_direction;
  2288. #if 0
  2289. /*|*/ if (search_state.test_pos.pos < search_state.test_pos.end)
  2290. /*|*/ break;
  2291. #endif
  2292. }
  2293. /* do interrupted for entry point... */
  2294. case rx_outer_restore_pos:
  2295. {
  2296. int x;
  2297. x = get_burst (&search_state.outer_pos, app_closure, stop);
  2298. switch (x)
  2299. {
  2300. case rx_get_burst_continuation:
  2301. pc = rx_outer_restore_pos;
  2302. goto return_continuation;
  2303. case rx_get_burst_error:
  2304. search_state.ret_val = rx_search_error;
  2305. goto finish;
  2306. case rx_get_burst_no_more:
  2307. if (rxb->can_match_empty)
  2308. break;
  2309. goto finish;
  2310. case rx_get_burst_ok:
  2311. break;
  2312. }
  2313. } /* } while (...see below...) */
  2314. if ((search_state.outer_pos.search_direction == 1)
  2315. ? (startpos <= search_state.outer_pos.search_end)
  2316. : (startpos > search_state.outer_pos.search_end))
  2317. goto pseudo_do;
  2318. finish:
  2319. uninit_fastmap (rxb, &search_state);
  2320. if (search_state.start_super)
  2321. rx_unlock_superstate (&rxb->rx, search_state.start_super);
  2322. #ifdef regex_malloc
  2323. if (search_state.lparen) free (search_state.lparen);
  2324. if (search_state.rparen) free (search_state.rparen);
  2325. if (search_state.best_lpspace) free (search_state.best_lpspace);
  2326. if (search_state.best_rpspace) free (search_state.best_rpspace);
  2327. #endif
  2328. return search_state.ret_val;
  2329. }
  2330. test_match:
  2331. {
  2332. enum rx_test_match_entry test_pc;
  2333. int inx;
  2334. test_pc = search_state.test_match_resume_pt;
  2335. if (test_pc == rx_test_start)
  2336. {
  2337. #ifdef RX_DEBUG
  2338. search_state.backtrack_depth = 0;
  2339. #endif
  2340. search_state.last_l = search_state.last_r = 0;
  2341. search_state.lparen[0] = startpos;
  2342. search_state.super = search_state.start_super;
  2343. search_state.c = search_state.nfa_choice;
  2344. search_state.test_pos.pos = search_state.outer_pos.pos - 1;
  2345. search_state.test_pos.string = search_state.outer_pos.string;
  2346. search_state.test_pos.end = search_state.outer_pos.end;
  2347. search_state.test_pos.offset = search_state.outer_pos.offset;
  2348. search_state.test_pos.size = search_state.outer_pos.size;
  2349. search_state.test_pos.search_direction = 1;
  2350. search_state.counter_stack = 0;
  2351. search_state.backtrack_stack = 0;
  2352. search_state.backtrack_frame_bytes =
  2353. (sizeof (struct rx_backtrack_frame)
  2354. + (rxb->match_regs_on_stack
  2355. ? sizeof (regoff_t) * (search_state.num_regs + 1) * 2
  2356. : 0));
  2357. search_state.chunk_bytes = search_state.backtrack_frame_bytes * 64;
  2358. search_state.test_ret = rx_test_line_finished;
  2359. search_state.could_have_continued = 0;
  2360. }
  2361. /* This is while (1)...except that the body of the loop is interrupted
  2362. * by some alternative entry points.
  2363. */
  2364. pseudo_while_1:
  2365. switch (test_pc)
  2366. {
  2367. case rx_test_cache_hit_loop:
  2368. goto resume_continuation_1;
  2369. case rx_test_backreference_check:
  2370. goto resume_continuation_2;
  2371. case rx_test_backtrack_return:
  2372. goto resume_continuation_3;
  2373. case rx_test_start:
  2374. #ifdef RX_DEBUG
  2375. /* There is a search tree with every node as set of deterministic
  2376. * transitions in the super nfa. For every branch of a
  2377. * backtrack point is an edge in the tree.
  2378. * This counts up a pre-order of nodes in that tree.
  2379. * It's saved on the search stack and printed when debugging.
  2380. */
  2381. search_state.line_no = 0;
  2382. search_state.lines_found = 0;
  2383. #endif
  2384. top_of_cycle:
  2385. /* A superstate is basicly a transition table, indexed by
  2386. * characters from the string being tested, and containing
  2387. * RX_INX (`instruction frame') structures.
  2388. */
  2389. search_state.ifr = &search_state.super->transitions [search_state.c];
  2390. recurse_test_match:
  2391. /* This is the point to which control is sent when the
  2392. * test matcher `recurses'. Before jumping here, some variables
  2393. * need to be saved on the stack and the next instruction frame
  2394. * has to be computed.
  2395. */
  2396. restart:
  2397. /* Some instructions don't advance the matcher, but just
  2398. * carry out some side effects and fetch a new instruction.
  2399. * To dispatch that new instruction, `goto restart'.
  2400. */
  2401. {
  2402. struct rx_inx * next_tr_table;
  2403. struct rx_inx * this_tr_table;
  2404. /* The fastest route through the loop is when the instruction
  2405. * is RX_NEXT_CHAR. This case is detected when SEARCH_STATE.IFR->DATA
  2406. * is non-zero. In that case, it points to the next
  2407. * superstate.
  2408. *
  2409. * This allows us to not bother fetching the bytecode.
  2410. */
  2411. next_tr_table = (struct rx_inx *)search_state.ifr->data;
  2412. this_tr_table = search_state.super->transitions;
  2413. while (next_tr_table)
  2414. {
  2415. #ifdef RX_DEBUG_0
  2416. if (rx_debug_trace)
  2417. {
  2418. struct rx_superset * setp;
  2419. fprintf (stderr, "%d %d>> re_next_char @ %d (%d)",
  2420. search_state.line_no,
  2421. search_state.backtrack_depth,
  2422. (search_state.test_pos.pos - search_state.test_pos.string
  2423. + search_state.test_pos.offset), search_state.c);
  2424. search_state.super =
  2425. ((struct rx_superstate *)
  2426. ((char *)this_tr_table
  2427. - ((unsigned long)
  2428. ((struct rx_superstate *)0)->transitions)));
  2429. setp = search_state.super->contents;
  2430. fprintf (stderr, " superstet (rx=%d, &=%x: ",
  2431. rxb->rx.rx_id, setp);
  2432. while (setp)
  2433. {
  2434. fprintf (stderr, "%d ", setp->id);
  2435. setp = setp->cdr;
  2436. }
  2437. fprintf (stderr, "\n");
  2438. }
  2439. #endif
  2440. this_tr_table = next_tr_table;
  2441. ++search_state.test_pos.pos;
  2442. if (search_state.test_pos.pos == search_state.test_pos.end)
  2443. {
  2444. int burst_state;
  2445. try_burst_1:
  2446. burst_state = get_burst (&search_state.test_pos,
  2447. app_closure, stop);
  2448. switch (burst_state)
  2449. {
  2450. case rx_get_burst_continuation:
  2451. search_state.saved_this_tr_table = this_tr_table;
  2452. search_state.saved_next_tr_table = next_tr_table;
  2453. test_pc = rx_test_cache_hit_loop;
  2454. goto test_return_continuation;
  2455. resume_continuation_1:
  2456. /* Continuation one jumps here to do its work: */
  2457. search_state.saved_this_tr_table = this_tr_table;
  2458. search_state.saved_next_tr_table = next_tr_table;
  2459. goto try_burst_1;
  2460. case rx_get_burst_ok:
  2461. /* get_burst succeeded...keep going */
  2462. break;
  2463. case rx_get_burst_no_more:
  2464. search_state.test_ret = rx_test_line_finished;
  2465. search_state.could_have_continued = 1;
  2466. goto test_do_return;
  2467. case rx_get_burst_error:
  2468. /* An error... */
  2469. search_state.test_ret = rx_test_internal_error;
  2470. goto test_do_return;
  2471. }
  2472. }
  2473. search_state.c = *search_state.test_pos.pos;
  2474. search_state.ifr = this_tr_table + search_state.c;
  2475. next_tr_table = (struct rx_inx *)search_state.ifr->data;
  2476. } /* Fast loop through cached transition tables */
  2477. /* Here when we ran out of cached next-char transitions.
  2478. * So, it will be necessary to do a more expensive
  2479. * dispatch on the current instruction. The superstate
  2480. * pointer is allowed to become invalid during next-char
  2481. * transitions -- now we must bring it up to date.
  2482. */
  2483. search_state.super =
  2484. ((struct rx_superstate *)
  2485. ((char *)this_tr_table
  2486. - ((unsigned long)
  2487. ((struct rx_superstate *)0)->transitions)));
  2488. }
  2489. /* We've encountered an instruction other than next-char.
  2490. * Dispatch that instruction:
  2491. */
  2492. inx = (int)search_state.ifr->inx;
  2493. #ifdef RX_DEBUG_0
  2494. if (rx_debug_trace)
  2495. {
  2496. struct rx_superset * setp = search_state.super->contents;
  2497. fprintf (stderr, "%d %d>> %s @ %d (%d)", search_state.line_no,
  2498. search_state.backtrack_depth,
  2499. inx_names[inx],
  2500. (search_state.test_pos.pos - search_state.test_pos.string
  2501. + (test_pos.half == 0 ? 0 : size1)), search_state.c);
  2502. fprintf (stderr, " superstet (rx=%d, &=%x: ",
  2503. rxb->rx.rx_id, setp);
  2504. while (setp)
  2505. {
  2506. fprintf (stderr, "%d ", setp->id);
  2507. setp = setp->cdr;
  2508. }
  2509. fprintf (stderr, "\n");
  2510. }
  2511. #endif
  2512. switch ((enum rx_opcode)inx)
  2513. {
  2514. case rx_do_side_effects:
  2515. /* RX_DO_SIDE_EFFECTS occurs when we cross epsilon
  2516. * edges associated with parentheses, backreferencing, etc.
  2517. */
  2518. {
  2519. struct rx_distinct_future * df =
  2520. (struct rx_distinct_future *)search_state.ifr->data_2;
  2521. struct rx_se_list * el = df->effects;
  2522. /* Side effects come in lists. This walks down
  2523. * a list, dispatching.
  2524. */
  2525. while (el)
  2526. {
  2527. long effect;
  2528. effect = (long)el->car;
  2529. if (effect < 0)
  2530. {
  2531. #ifdef RX_DEBUG_0
  2532. if (rx_debug_trace)
  2533. {
  2534. struct rx_superset * setp = search_state.super->contents;
  2535. fprintf (stderr, "....%d %d>> %s\n", search_state.line_no,
  2536. search_state.backtrack_depth,
  2537. efnames[-effect]);
  2538. }
  2539. #endif
  2540. switch ((enum re_side_effects) effect)
  2541. {
  2542. case re_se_pushback:
  2543. search_state.ifr = &df->future_frame;
  2544. if (!search_state.ifr->data)
  2545. {
  2546. struct rx_superstate * sup;
  2547. sup = search_state.super;
  2548. rx_lock_superstate (rx, sup);
  2549. if (!rx_handle_cache_miss (&rxb->rx,
  2550. search_state.super,
  2551. search_state.c,
  2552. (search_state.ifr
  2553. ->data_2)))
  2554. {
  2555. rx_unlock_superstate (rx, sup);
  2556. search_state.test_ret = rx_test_internal_error;
  2557. goto test_do_return;
  2558. }
  2559. rx_unlock_superstate (rx, sup);
  2560. }
  2561. /* --search_state.test_pos.pos; */
  2562. search_state.c = 't';
  2563. search_state.super
  2564. = ((struct rx_superstate *)
  2565. ((char *)search_state.ifr->data
  2566. - (long)(((struct rx_superstate *)0)
  2567. ->transitions)));
  2568. goto top_of_cycle;
  2569. break;
  2570. case re_se_push0:
  2571. {
  2572. struct rx_counter_frame * old_cf
  2573. = (search_state.counter_stack
  2574. ? ((struct rx_counter_frame *)
  2575. search_state.counter_stack->sp)
  2576. : 0);
  2577. struct rx_counter_frame * cf;
  2578. PUSH (search_state.counter_stack,
  2579. sizeof (struct rx_counter_frame));
  2580. cf = ((struct rx_counter_frame *)
  2581. search_state.counter_stack->sp);
  2582. cf->tag = re_se_iter;
  2583. cf->val = 0;
  2584. cf->inherited_from = 0;
  2585. cf->cdr = old_cf;
  2586. break;
  2587. }
  2588. case re_se_fail:
  2589. goto test_do_return;
  2590. case re_se_begbuf:
  2591. if (!AT_STRINGS_BEG ())
  2592. goto test_do_return;
  2593. break;
  2594. case re_se_endbuf:
  2595. if (!AT_STRINGS_END ())
  2596. goto test_do_return;
  2597. break;
  2598. case re_se_wordbeg:
  2599. if ( LETTER_P (&search_state.test_pos, 1)
  2600. && ( AT_STRINGS_BEG()
  2601. || !LETTER_P (&search_state.test_pos, 0)))
  2602. break;
  2603. else
  2604. goto test_do_return;
  2605. case re_se_wordend:
  2606. if ( !AT_STRINGS_BEG ()
  2607. && LETTER_P (&search_state.test_pos, 0)
  2608. && (AT_STRINGS_END ()
  2609. || !LETTER_P (&search_state.test_pos, 1)))
  2610. break;
  2611. else
  2612. goto test_do_return;
  2613. case re_se_wordbound:
  2614. if (AT_WORD_BOUNDARY (&search_state.test_pos))
  2615. break;
  2616. else
  2617. goto test_do_return;
  2618. case re_se_notwordbound:
  2619. if (!AT_WORD_BOUNDARY (&search_state.test_pos))
  2620. break;
  2621. else
  2622. goto test_do_return;
  2623. case re_se_hat:
  2624. if (AT_STRINGS_BEG ())
  2625. {
  2626. if (rxb->not_bol)
  2627. goto test_do_return;
  2628. else
  2629. break;
  2630. }
  2631. else
  2632. {
  2633. char pos_c = *search_state.test_pos.pos;
  2634. if ( (SRCH_TRANSLATE (pos_c)
  2635. == SRCH_TRANSLATE('\n'))
  2636. && rxb->newline_anchor)
  2637. break;
  2638. else
  2639. goto test_do_return;
  2640. }
  2641. case re_se_dollar:
  2642. if (AT_STRINGS_END ())
  2643. {
  2644. if (rxb->not_eol)
  2645. goto test_do_return;
  2646. else
  2647. break;
  2648. }
  2649. else
  2650. {
  2651. if ( ( SRCH_TRANSLATE (fetch_char
  2652. (&search_state.test_pos, 1,
  2653. app_closure, stop))
  2654. == SRCH_TRANSLATE ('\n'))
  2655. && rxb->newline_anchor)
  2656. break;
  2657. else
  2658. goto test_do_return;
  2659. }
  2660. case re_se_try:
  2661. /* This is the first side effect in every
  2662. * expression.
  2663. *
  2664. * FOR NO GOOD REASON...get rid of it...
  2665. */
  2666. break;
  2667. case re_se_pushpos:
  2668. {
  2669. int urhere =
  2670. ((int)(search_state.test_pos.pos
  2671. - search_state.test_pos.string)
  2672. + search_state.test_pos.offset);
  2673. struct rx_counter_frame * old_cf
  2674. = (search_state.counter_stack
  2675. ? ((struct rx_counter_frame *)
  2676. search_state.counter_stack->sp)
  2677. : 0);
  2678. struct rx_counter_frame * cf;
  2679. PUSH(search_state.counter_stack,
  2680. sizeof (struct rx_counter_frame));
  2681. cf = ((struct rx_counter_frame *)
  2682. search_state.counter_stack->sp);
  2683. cf->tag = re_se_pushpos;
  2684. cf->val = urhere;
  2685. cf->inherited_from = 0;
  2686. cf->cdr = old_cf;
  2687. break;
  2688. }
  2689. case re_se_chkpos:
  2690. {
  2691. int urhere =
  2692. ((int)(search_state.test_pos.pos
  2693. - search_state.test_pos.string)
  2694. + search_state.test_pos.offset);
  2695. struct rx_counter_frame * cf
  2696. = ((struct rx_counter_frame *)
  2697. search_state.counter_stack->sp);
  2698. if (cf->val == urhere)
  2699. goto test_do_return;
  2700. cf->val = urhere;
  2701. break;
  2702. }
  2703. break;
  2704. case re_se_poppos:
  2705. POP(search_state.counter_stack,
  2706. sizeof (struct rx_counter_frame));
  2707. break;
  2708. case re_se_at_dot:
  2709. case re_se_syntax:
  2710. case re_se_not_syntax:
  2711. #ifdef emacs
  2712. /*
  2713. * this release lacks emacs support
  2714. */
  2715. #endif
  2716. break;
  2717. case re_se_win:
  2718. case re_se_lparen:
  2719. case re_se_rparen:
  2720. case re_se_backref:
  2721. case re_se_iter:
  2722. case re_se_end_iter:
  2723. case re_se_tv:
  2724. case re_floogle_flap:
  2725. search_state.ret_val = 0;
  2726. goto test_do_return;
  2727. }
  2728. }
  2729. else
  2730. {
  2731. #ifdef RX_DEBUG_0
  2732. if (rx_debug_trace)
  2733. fprintf (stderr, "....%d %d>> %s %d %d\n", search_state.line_no,
  2734. search_state.backtrack_depth,
  2735. efnames2[rxb->se_params [effect].se],
  2736. rxb->se_params [effect].op1,
  2737. rxb->se_params [effect].op2);
  2738. #endif
  2739. switch (rxb->se_params [effect].se)
  2740. {
  2741. case re_se_win:
  2742. /* This side effect indicates that we've
  2743. * found a match, though not necessarily the
  2744. * best match. This is a fancy assignment to
  2745. * register 0 unless the caller didn't
  2746. * care about registers. In which case,
  2747. * this stops the match.
  2748. */
  2749. {
  2750. int urhere =
  2751. ((int)(search_state.test_pos.pos
  2752. - search_state.test_pos.string)
  2753. + search_state.test_pos.offset);
  2754. if ( (search_state.best_last_r < 0)
  2755. || (urhere + 1 > search_state.best_rparen[0]))
  2756. {
  2757. /* Record the best known and keep
  2758. * looking.
  2759. */
  2760. int x;
  2761. for (x = 0; x <= search_state.last_l; ++x)
  2762. search_state.best_lparen[x] = search_state.lparen[x];
  2763. search_state.best_last_l = search_state.last_l;
  2764. for (x = 0; x <= search_state.last_r; ++x)
  2765. search_state.best_rparen[x] = search_state.rparen[x];
  2766. search_state.best_rparen[0] = urhere + 1;
  2767. search_state.best_last_r = search_state.last_r;
  2768. }
  2769. /* If we're not reporting the match-length
  2770. * or other register info, we need look no
  2771. * further.
  2772. */
  2773. if (search_state.first_found)
  2774. {
  2775. search_state.test_ret = rx_test_found_first;
  2776. goto test_do_return;
  2777. }
  2778. }
  2779. break;
  2780. case re_se_lparen:
  2781. {
  2782. int urhere =
  2783. ((int)(search_state.test_pos.pos
  2784. - search_state.test_pos.string)
  2785. + search_state.test_pos.offset);
  2786. int reg = rxb->se_params [effect].op1;
  2787. #if 0
  2788. if (reg > search_state.last_l)
  2789. #endif
  2790. {
  2791. search_state.lparen[reg] = urhere + 1;
  2792. /* In addition to making this assignment,
  2793. * we now know that lower numbered regs
  2794. * that haven't already been assigned,
  2795. * won't be. We make sure they're
  2796. * filled with -1, so they can be
  2797. * recognized as unassigned.
  2798. */
  2799. if (search_state.last_l < reg)
  2800. while (++search_state.last_l < reg)
  2801. search_state.lparen[search_state.last_l] = -1;
  2802. }
  2803. break;
  2804. }
  2805. case re_se_rparen:
  2806. {
  2807. int urhere =
  2808. ((int)(search_state.test_pos.pos
  2809. - search_state.test_pos.string)
  2810. + search_state.test_pos.offset);
  2811. int reg = rxb->se_params [effect].op1;
  2812. search_state.rparen[reg] = urhere + 1;
  2813. if (search_state.last_r < reg)
  2814. {
  2815. while (++search_state.last_r < reg)
  2816. search_state.rparen[search_state.last_r]
  2817. = -1;
  2818. }
  2819. break;
  2820. }
  2821. case re_se_backref:
  2822. {
  2823. int reg = rxb->se_params [effect].op1;
  2824. if ( reg > search_state.last_r
  2825. || search_state.rparen[reg] < 0)
  2826. goto test_do_return;
  2827. {
  2828. int backref_status;
  2829. check_backreference:
  2830. backref_status
  2831. = back_check (&search_state.test_pos,
  2832. search_state.lparen[reg],
  2833. search_state.rparen[reg],
  2834. search_state.translate,
  2835. app_closure,
  2836. stop);
  2837. switch (backref_status)
  2838. {
  2839. case rx_back_check_continuation:
  2840. search_state.saved_reg = reg;
  2841. test_pc = rx_test_backreference_check;
  2842. goto test_return_continuation;
  2843. resume_continuation_2:
  2844. reg = search_state.saved_reg;
  2845. goto check_backreference;
  2846. case rx_back_check_fail:
  2847. /* Fail */
  2848. goto test_do_return;
  2849. case rx_back_check_pass:
  2850. /* pass --
  2851. * test_pos now advanced to last
  2852. * char matched by backref
  2853. */
  2854. break;
  2855. }
  2856. }
  2857. break;
  2858. }
  2859. case re_se_iter:
  2860. {
  2861. struct rx_counter_frame * csp
  2862. = ((struct rx_counter_frame *)
  2863. search_state.counter_stack->sp);
  2864. if (csp->val == rxb->se_params[effect].op2)
  2865. goto test_do_return;
  2866. else
  2867. ++csp->val;
  2868. break;
  2869. }
  2870. case re_se_end_iter:
  2871. {
  2872. struct rx_counter_frame * csp
  2873. = ((struct rx_counter_frame *)
  2874. search_state.counter_stack->sp);
  2875. if (csp->val < rxb->se_params[effect].op1)
  2876. goto test_do_return;
  2877. else
  2878. {
  2879. struct rx_counter_frame * source = csp;
  2880. while (source->inherited_from)
  2881. source = source->inherited_from;
  2882. if (!source || !source->cdr)
  2883. {
  2884. POP(search_state.counter_stack,
  2885. sizeof(struct rx_counter_frame));
  2886. }
  2887. else
  2888. {
  2889. source = source->cdr;
  2890. csp->val = source->val;
  2891. csp->tag = source->tag;
  2892. csp->cdr = 0;
  2893. csp->inherited_from = source;
  2894. }
  2895. }
  2896. break;
  2897. }
  2898. case re_se_tv:
  2899. /* is a noop */
  2900. break;
  2901. case re_se_try:
  2902. case re_se_pushback:
  2903. case re_se_push0:
  2904. case re_se_pushpos:
  2905. case re_se_chkpos:
  2906. case re_se_poppos:
  2907. case re_se_at_dot:
  2908. case re_se_syntax:
  2909. case re_se_not_syntax:
  2910. case re_se_begbuf:
  2911. case re_se_hat:
  2912. case re_se_wordbeg:
  2913. case re_se_wordbound:
  2914. case re_se_notwordbound:
  2915. case re_se_wordend:
  2916. case re_se_endbuf:
  2917. case re_se_dollar:
  2918. case re_se_fail:
  2919. case re_floogle_flap:
  2920. search_state.ret_val = 0;
  2921. goto test_do_return;
  2922. }
  2923. }
  2924. el = el->cdr;
  2925. }
  2926. /* Now the side effects are done,
  2927. * so get the next instruction.
  2928. * and move on.
  2929. */
  2930. search_state.ifr = &df->future_frame;
  2931. goto restart;
  2932. }
  2933. case rx_backtrack_point:
  2934. {
  2935. /* A backtrack point indicates that we've reached a
  2936. * non-determinism in the superstate NFA. This is a
  2937. * loop that exhaustively searches the possibilities.
  2938. *
  2939. * A backtracking strategy is used. We keep track of what
  2940. * registers are valid so we can erase side effects.
  2941. *
  2942. * First, make sure there is some stack space to hold
  2943. * our state.
  2944. */
  2945. struct rx_backtrack_frame * bf;
  2946. PUSH(search_state.backtrack_stack,
  2947. search_state.backtrack_frame_bytes);
  2948. #ifdef RX_DEBUG_0
  2949. ++search_state.backtrack_depth;
  2950. #endif
  2951. bf = ((struct rx_backtrack_frame *)
  2952. search_state.backtrack_stack->sp);
  2953. {
  2954. bf->stk_super = search_state.super;
  2955. /* We prevent the current superstate from being
  2956. * deleted from the superstate cache.
  2957. */
  2958. rx_lock_superstate (&rxb->rx, search_state.super);
  2959. #ifdef RX_DEBUG_0
  2960. bf->stk_search_state.line_no = search_state.line_no;
  2961. #endif
  2962. bf->stk_c = search_state.c;
  2963. bf->stk_test_pos = search_state.test_pos;
  2964. bf->stk_last_l = search_state.last_l;
  2965. bf->stk_last_r = search_state.last_r;
  2966. bf->df = ((struct rx_super_edge *)
  2967. search_state.ifr->data_2)->options;
  2968. bf->first_df = bf->df;
  2969. bf->counter_stack_sp = (search_state.counter_stack
  2970. ? search_state.counter_stack->sp
  2971. : 0);
  2972. bf->stk_test_ret = search_state.test_ret;
  2973. if (rxb->match_regs_on_stack)
  2974. {
  2975. int x;
  2976. regoff_t * stk =
  2977. (regoff_t *)((char *)bf + sizeof (*bf));
  2978. for (x = 0; x <= search_state.last_l; ++x)
  2979. stk[x] = search_state.lparen[x];
  2980. stk += x;
  2981. for (x = 0; x <= search_state.last_r; ++x)
  2982. stk[x] = search_state.rparen[x];
  2983. }
  2984. }
  2985. /* Here is a while loop whose body is mainly a function
  2986. * call and some code to handle a return from that
  2987. * function.
  2988. *
  2989. * From here on for the rest of `case backtrack_point' it
  2990. * is unsafe to assume that the search_state copies of
  2991. * variables saved on the backtracking stack are valid
  2992. * -- so read their values from the backtracking stack.
  2993. *
  2994. * This lets us use one generation fewer stack saves in
  2995. * the call-graph of a search.
  2996. */
  2997. while_non_det_options:
  2998. #ifdef RX_DEBUG_0
  2999. ++search_state.lines_found;
  3000. if (rx_debug_trace)
  3001. fprintf (stderr, "@@@ %d calls %d @@@\n",
  3002. search_state.line_no, search_state.lines_found);
  3003. search_state.line_no = search_state.lines_found;
  3004. #endif
  3005. if (bf->df->next_same_super_edge[0] == bf->first_df)
  3006. {
  3007. /* This is a tail-call optimization -- we don't recurse
  3008. * for the last of the possible futures.
  3009. */
  3010. search_state.ifr = (bf->df->effects
  3011. ? &bf->df->side_effects_frame
  3012. : &bf->df->future_frame);
  3013. rx_unlock_superstate (&rxb->rx, search_state.super);
  3014. POP(search_state.backtrack_stack,
  3015. search_state.backtrack_frame_bytes);
  3016. #ifdef RX_DEBUG
  3017. --search_state.backtrack_depth;
  3018. #endif
  3019. goto restart;
  3020. }
  3021. else
  3022. {
  3023. if (search_state.counter_stack)
  3024. {
  3025. struct rx_counter_frame * old_cf
  3026. = ((struct rx_counter_frame *)search_state.counter_stack->sp);
  3027. struct rx_counter_frame * cf;
  3028. PUSH(search_state.counter_stack, sizeof (struct rx_counter_frame));
  3029. cf = ((struct rx_counter_frame *)search_state.counter_stack->sp);
  3030. cf->tag = old_cf->tag;
  3031. cf->val = old_cf->val;
  3032. cf->inherited_from = old_cf;
  3033. cf->cdr = 0;
  3034. }
  3035. /* `Call' this test-match block */
  3036. search_state.ifr = (bf->df->effects
  3037. ? &bf->df->side_effects_frame
  3038. : &bf->df->future_frame);
  3039. goto recurse_test_match;
  3040. }
  3041. /* Returns in this block are accomplished by
  3042. * goto test_do_return. There are two cases.
  3043. * If there is some search-stack left,
  3044. * then it is a return from a `recursive' call.
  3045. * If there is no search-stack left, then
  3046. * we should return to the fastmap/search loop.
  3047. */
  3048. test_do_return:
  3049. if (!search_state.backtrack_stack)
  3050. {
  3051. #ifdef RX_DEBUG_0
  3052. if (rx_debug_trace)
  3053. fprintf (stderr, "!!! %d bails returning %d !!!\n",
  3054. search_state.line_no, search_state.test_ret);
  3055. #endif
  3056. /* No more search-stack -- this test is done. */
  3057. if (search_state.test_ret != rx_test_internal_error)
  3058. goto return_from_test_match;
  3059. else
  3060. goto error_in_testing_match;
  3061. }
  3062. /* Returning from a recursive call to
  3063. * the test match block:
  3064. */
  3065. bf = ((struct rx_backtrack_frame *)
  3066. search_state.backtrack_stack->sp);
  3067. #ifdef RX_DEBUG_0
  3068. if (rx_debug_trace)
  3069. fprintf (stderr, "+++ %d returns %d (to %d)+++\n",
  3070. search_state.line_no,
  3071. search_state.test_ret,
  3072. bf->stk_search_state.line_no);
  3073. #endif
  3074. while (search_state.counter_stack
  3075. && (!bf->counter_stack_sp
  3076. || (bf->counter_stack_sp
  3077. != search_state.counter_stack->sp)))
  3078. {
  3079. POP(search_state.counter_stack,
  3080. sizeof (struct rx_counter_frame));
  3081. }
  3082. if (search_state.test_ret == rx_test_internal_error)
  3083. {
  3084. POP (search_state.backtrack_stack,
  3085. search_state.backtrack_frame_bytes);
  3086. goto test_do_return;
  3087. }
  3088. /* If a non-longest match was found and that is good
  3089. * enough, return immediately.
  3090. */
  3091. if ( (search_state.test_ret == rx_test_found_first)
  3092. && search_state.first_found)
  3093. {
  3094. rx_unlock_superstate (&rxb->rx, bf->stk_super);
  3095. POP (search_state.backtrack_stack,
  3096. search_state.backtrack_frame_bytes);
  3097. goto test_do_return;
  3098. }
  3099. search_state.test_ret = bf->stk_test_ret;
  3100. search_state.last_l = bf->stk_last_l;
  3101. search_state.last_r = bf->stk_last_r;
  3102. bf->df = bf->df->next_same_super_edge[0];
  3103. search_state.super = bf->stk_super;
  3104. search_state.c = bf->stk_c;
  3105. #ifdef RX_DEBUG_0
  3106. search_state.line_no = bf->stk_search_state.line_no;
  3107. #endif
  3108. if (rxb->match_regs_on_stack)
  3109. {
  3110. int x;
  3111. regoff_t * stk =
  3112. (regoff_t *)((char *)bf + sizeof (*bf));
  3113. for (x = 0; x <= search_state.last_l; ++x)
  3114. search_state.lparen[x] = stk[x];
  3115. stk += x;
  3116. for (x = 0; x <= search_state.last_r; ++x)
  3117. search_state.rparen[x] = stk[x];
  3118. }
  3119. if ((search_state.test_ret != rx_test_line_finished) &&
  3120. (search_state.test_ret != rx_test_internal_error))
  3121. {
  3122. int x;
  3123. try_burst_2:
  3124. x = get_burst (&bf->stk_test_pos, app_closure, stop);
  3125. switch (x)
  3126. {
  3127. case rx_get_burst_continuation:
  3128. search_state.saved_bf = bf;
  3129. test_pc = rx_test_backtrack_return;
  3130. goto test_return_continuation;
  3131. resume_continuation_3:
  3132. bf = search_state.saved_bf;
  3133. goto try_burst_2;
  3134. case rx_get_burst_no_more:
  3135. /* Since we've been here before, it is some kind of
  3136. * error that we can't return.
  3137. */
  3138. case rx_get_burst_error:
  3139. search_state.test_ret = rx_test_internal_error;
  3140. goto test_do_return;
  3141. case rx_get_burst_ok:
  3142. break;
  3143. }
  3144. }
  3145. search_state.test_pos = bf->stk_test_pos;
  3146. goto while_non_det_options;
  3147. }
  3148. case rx_cache_miss:
  3149. /* Because the superstate NFA is lazily constructed,
  3150. * and in fact may erode from underneath us, we sometimes
  3151. * have to construct the next instruction from the hard way.
  3152. * This invokes one step in the lazy-conversion.
  3153. */
  3154. search_state.ifr = rx_handle_cache_miss (&rxb->rx,
  3155. search_state.super,
  3156. search_state.c,
  3157. search_state.ifr->data_2);
  3158. if (!search_state.ifr)
  3159. {
  3160. search_state.test_ret = rx_test_internal_error;
  3161. goto test_do_return;
  3162. }
  3163. goto restart;
  3164. case rx_backtrack:
  3165. /* RX_BACKTRACK means that we've reached the empty
  3166. * superstate, indicating that match can't succeed
  3167. * from this point.
  3168. */
  3169. goto test_do_return;
  3170. case rx_next_char:
  3171. case rx_error_inx:
  3172. case rx_num_instructions:
  3173. search_state.ret_val = 0;
  3174. goto test_do_return;
  3175. }
  3176. goto pseudo_while_1;
  3177. }
  3178. /* Healthy exits from the test-match loop do a
  3179. * `goto return_from_test_match' On the other hand,
  3180. * we might end up here.
  3181. */
  3182. error_in_testing_match:
  3183. test_state = rx_test_error;
  3184. goto test_returns_to_search;
  3185. /***** fastmap/search loop body
  3186. * considering the results testing for a match
  3187. */
  3188. return_from_test_match:
  3189. if (search_state.best_last_l >= 0)
  3190. {
  3191. if (regs && (regs->start != search_state.best_lparen))
  3192. {
  3193. bcopy (search_state.best_lparen, regs->start,
  3194. regs->num_regs * sizeof (int));
  3195. bcopy (search_state.best_rparen, regs->end,
  3196. regs->num_regs * sizeof (int));
  3197. }
  3198. if (regs && !rxb->no_sub)
  3199. {
  3200. int q;
  3201. int bound = (regs->num_regs < search_state.num_regs
  3202. ? regs->num_regs
  3203. : search_state.num_regs);
  3204. regoff_t * s = regs->start;
  3205. regoff_t * e = regs->end;
  3206. for (q = search_state.best_last_l + 1; q < bound; ++q)
  3207. s[q] = e[q] = -1;
  3208. }
  3209. search_state.ret_val = search_state.best_lparen[0];
  3210. test_state = rx_test_ok;
  3211. goto test_returns_to_search;
  3212. }
  3213. else
  3214. {
  3215. test_state = rx_test_fail;
  3216. goto test_returns_to_search;
  3217. }
  3218. test_return_continuation:
  3219. search_state.test_match_resume_pt = test_pc;
  3220. test_state = rx_test_continuation;
  3221. goto test_returns_to_search;
  3222. }
  3223. }
  3224. #endif /* RX_WANT_RX_DEFS */
  3225. #else /* RX_WANT_SE_DEFS */
  3226. /* Integers are used to represent side effects.
  3227. *
  3228. * Simple side effects are given negative integer names by these enums.
  3229. *
  3230. * Non-negative names are reserved for complex effects.
  3231. *
  3232. * Complex effects are those that take arguments. For example,
  3233. * a register assignment associated with a group is complex because
  3234. * it requires an argument to tell which group is being matched.
  3235. *
  3236. * The integer name of a complex effect is an index into rxb->se_params.
  3237. */
  3238. RX_DEF_SE(1, re_se_try, = -1) /* Epsilon from start state */
  3239. RX_DEF_SE(0, re_se_pushback, = re_se_try - 1)
  3240. RX_DEF_SE(0, re_se_push0, = re_se_pushback -1)
  3241. RX_DEF_SE(0, re_se_pushpos, = re_se_push0 - 1)
  3242. RX_DEF_SE(0, re_se_chkpos, = re_se_pushpos -1)
  3243. RX_DEF_SE(0, re_se_poppos, = re_se_chkpos - 1)
  3244. RX_DEF_SE(1, re_se_at_dot, = re_se_poppos - 1) /* Emacs only */
  3245. RX_DEF_SE(0, re_se_syntax, = re_se_at_dot - 1) /* Emacs only */
  3246. RX_DEF_SE(0, re_se_not_syntax, = re_se_syntax - 1) /* Emacs only */
  3247. RX_DEF_SE(1, re_se_begbuf, = re_se_not_syntax - 1) /* match beginning of buffer */
  3248. RX_DEF_SE(1, re_se_hat, = re_se_begbuf - 1) /* match beginning of line */
  3249. RX_DEF_SE(1, re_se_wordbeg, = re_se_hat - 1)
  3250. RX_DEF_SE(1, re_se_wordbound, = re_se_wordbeg - 1)
  3251. RX_DEF_SE(1, re_se_notwordbound, = re_se_wordbound - 1)
  3252. RX_DEF_SE(1, re_se_wordend, = re_se_notwordbound - 1)
  3253. RX_DEF_SE(1, re_se_endbuf, = re_se_wordend - 1)
  3254. /* This fails except at the end of a line.
  3255. * It deserves to go here since it is typicly one of the last steps
  3256. * in a match.
  3257. */
  3258. RX_DEF_SE(1, re_se_dollar, = re_se_endbuf - 1)
  3259. /* Simple effects: */
  3260. RX_DEF_SE(1, re_se_fail, = re_se_dollar - 1)
  3261. /* Complex effects. These are used in the 'se' field of
  3262. * a struct re_se_params. Indexes into the se array
  3263. * are stored as instructions on nfa edges.
  3264. */
  3265. RX_DEF_CPLX_SE(1, re_se_win, = 0)
  3266. RX_DEF_CPLX_SE(1, re_se_lparen, = re_se_win + 1)
  3267. RX_DEF_CPLX_SE(1, re_se_rparen, = re_se_lparen + 1)
  3268. RX_DEF_CPLX_SE(0, re_se_backref, = re_se_rparen + 1)
  3269. RX_DEF_CPLX_SE(0, re_se_iter, = re_se_backref + 1)
  3270. RX_DEF_CPLX_SE(0, re_se_end_iter, = re_se_iter + 1)
  3271. RX_DEF_CPLX_SE(0, re_se_tv, = re_se_end_iter + 1)
  3272. #endif
  3273. #if RX_WANT_SE_DEFS != 1
  3274. __END_DECLS
  3275. #endif
  3276. #endif