rugged 0.16.0 → 0.17.0b1

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