zsv 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (240) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +36 -0
  3. data/LICENSE +21 -0
  4. data/README.md +311 -0
  5. data/ext/zsv/common.h +34 -0
  6. data/ext/zsv/extconf.rb +137 -0
  7. data/ext/zsv/options.c +126 -0
  8. data/ext/zsv/options.h +31 -0
  9. data/ext/zsv/options_internal.h +8 -0
  10. data/ext/zsv/parser.c +300 -0
  11. data/ext/zsv/parser.h +62 -0
  12. data/ext/zsv/row.c +122 -0
  13. data/ext/zsv/row.h +39 -0
  14. data/ext/zsv/vendor/zsv-1.3.0/app/2db.c +756 -0
  15. data/ext/zsv/vendor/zsv-1.3.0/app/2json.c +381 -0
  16. data/ext/zsv/vendor/zsv-1.3.0/app/2tsv.c +228 -0
  17. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/help.c +123 -0
  18. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/license.c +39 -0
  19. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/register.c +104 -0
  20. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/thirdparty.c +41 -0
  21. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/unregister.c +1 -0
  22. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/version.c +14 -0
  23. data/ext/zsv/vendor/zsv-1.3.0/app/check/simdutf_wrapper.h +19 -0
  24. data/ext/zsv/vendor/zsv-1.3.0/app/check/utf8.c +116 -0
  25. data/ext/zsv/vendor/zsv-1.3.0/app/check.c +194 -0
  26. data/ext/zsv/vendor/zsv-1.3.0/app/cli.c +796 -0
  27. data/ext/zsv/vendor/zsv-1.3.0/app/cli_const.h +41 -0
  28. data/ext/zsv/vendor/zsv-1.3.0/app/cli_export.h +16 -0
  29. data/ext/zsv/vendor/zsv-1.3.0/app/cli_ini.c +280 -0
  30. data/ext/zsv/vendor/zsv-1.3.0/app/cli_internal.h +36 -0
  31. data/ext/zsv/vendor/zsv-1.3.0/app/compare.c +913 -0
  32. data/ext/zsv/vendor/zsv-1.3.0/app/compare.h +23 -0
  33. data/ext/zsv/vendor/zsv-1.3.0/app/compare_added_column.c +20 -0
  34. data/ext/zsv/vendor/zsv-1.3.0/app/compare_internal.h +140 -0
  35. data/ext/zsv/vendor/zsv-1.3.0/app/compare_sort.c +91 -0
  36. data/ext/zsv/vendor/zsv-1.3.0/app/compare_unique_colname.c +81 -0
  37. data/ext/zsv/vendor/zsv-1.3.0/app/count-pull.c +82 -0
  38. data/ext/zsv/vendor/zsv-1.3.0/app/count.c +404 -0
  39. data/ext/zsv/vendor/zsv-1.3.0/app/desc.c +569 -0
  40. data/ext/zsv/vendor/zsv-1.3.0/app/echo.c +365 -0
  41. data/ext/zsv/vendor/zsv-1.3.0/app/ext_example/my_extension.c +366 -0
  42. data/ext/zsv/vendor/zsv-1.3.0/app/ext_example/mysheet_extension.c +341 -0
  43. data/ext/zsv/vendor/zsv-1.3.0/app/ext_template/YOUR_EXTENSION_zsvext.c +263 -0
  44. data/ext/zsv/vendor/zsv-1.3.0/app/external/inih/ini.c +298 -0
  45. data/ext/zsv/vendor/zsv-1.3.0/app/external/inih/ini.h +157 -0
  46. data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/json_numeric.c +177 -0
  47. data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/jsonwriter.c +444 -0
  48. data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/jsonwriter.h +145 -0
  49. data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/utils.c +110 -0
  50. data/ext/zsv/vendor/zsv-1.3.0/app/external/memfile-1.0/include/memfile.h +15 -0
  51. data/ext/zsv/vendor/zsv-1.3.0/app/external/memfile-1.0/src/memfile.c +64 -0
  52. data/ext/zsv/vendor/zsv-1.3.0/app/external/sglib/sglib.h +1955 -0
  53. data/ext/zsv/vendor/zsv-1.3.0/app/external/simdutf/simdutf.h +6802 -0
  54. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3.c +230517 -0
  55. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3.h +12174 -0
  56. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_and_csv_vtab.c +2 -0
  57. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab-mem.c +142 -0
  58. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab-mem.h +49 -0
  59. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab-zsv.c +485 -0
  60. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab.c +1015 -0
  61. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3ext.h +663 -0
  62. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/vtab_helper.c +85 -0
  63. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_common.h +75 -0
  64. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_gen.h +167 -0
  65. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_parse.h +228 -0
  66. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_tree.h +186 -0
  67. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_version.h +23 -0
  68. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_common.h +76 -0
  69. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_gen.h +167 -0
  70. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_parse.h +238 -0
  71. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_tree.h +186 -0
  72. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl.c +184 -0
  73. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_alloc.c +52 -0
  74. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_alloc.h +34 -0
  75. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_buf.c +103 -0
  76. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_buf.h +57 -0
  77. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_bytestack.h +69 -0
  78. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_encode.c +220 -0
  79. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_encode.h +34 -0
  80. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_gen.c +362 -0
  81. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_lex.c +764 -0
  82. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_lex.h +117 -0
  83. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_parser.c +508 -0
  84. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_parser.h +78 -0
  85. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_tree.c +505 -0
  86. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_version.c +7 -0
  87. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper/json_value.h +59 -0
  88. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper/yajl_helper.h +208 -0
  89. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper.c +795 -0
  90. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper_internal.h +28 -0
  91. data/ext/zsv/vendor/zsv-1.3.0/app/flatten.c +851 -0
  92. data/ext/zsv/vendor/zsv-1.3.0/app/jq.c +106 -0
  93. data/ext/zsv/vendor/zsv-1.3.0/app/jq.h +6 -0
  94. data/ext/zsv/vendor/zsv-1.3.0/app/mv.c +113 -0
  95. data/ext/zsv/vendor/zsv-1.3.0/app/noop.c +90 -0
  96. data/ext/zsv/vendor/zsv-1.3.0/app/overwrite.c +295 -0
  97. data/ext/zsv/vendor/zsv-1.3.0/app/paste.c +175 -0
  98. data/ext/zsv/vendor/zsv-1.3.0/app/pretty.c +693 -0
  99. data/ext/zsv/vendor/zsv-1.3.0/app/prop.c +980 -0
  100. data/ext/zsv/vendor/zsv-1.3.0/app/rm.c +131 -0
  101. data/ext/zsv/vendor/zsv-1.3.0/app/select/fixed.c +130 -0
  102. data/ext/zsv/vendor/zsv-1.3.0/app/select/internal.h +118 -0
  103. data/ext/zsv/vendor/zsv-1.3.0/app/select/parallel.c +45 -0
  104. data/ext/zsv/vendor/zsv-1.3.0/app/select/parallel.h +41 -0
  105. data/ext/zsv/vendor/zsv-1.3.0/app/select/processing.c +107 -0
  106. data/ext/zsv/vendor/zsv-1.3.0/app/select/rand.c +20 -0
  107. data/ext/zsv/vendor/zsv-1.3.0/app/select/regex.c +61 -0
  108. data/ext/zsv/vendor/zsv-1.3.0/app/select/search.c +14 -0
  109. data/ext/zsv/vendor/zsv-1.3.0/app/select/selection.c +192 -0
  110. data/ext/zsv/vendor/zsv-1.3.0/app/select/usage.c +72 -0
  111. data/ext/zsv/vendor/zsv-1.3.0/app/select-pull.c +812 -0
  112. data/ext/zsv/vendor/zsv-1.3.0/app/select.c +753 -0
  113. data/ext/zsv/vendor/zsv-1.3.0/app/serialize.c +372 -0
  114. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/curses.h +15 -0
  115. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/cursor.c +119 -0
  116. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/errors.c +45 -0
  117. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/file.c +63 -0
  118. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/file.h +12 -0
  119. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/filter.c +166 -0
  120. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/handlers.c +214 -0
  121. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/handlers_internal.h +128 -0
  122. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/help.c +43 -0
  123. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/index.c +81 -0
  124. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/index.h +25 -0
  125. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/key-bindings.c +325 -0
  126. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/key-bindings.h +73 -0
  127. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/lexer.c +203 -0
  128. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/newline_handler.c +7 -0
  129. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/pivot.c +318 -0
  130. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/procedure.c +134 -0
  131. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/procedure.h +119 -0
  132. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/read-data.c +322 -0
  133. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/screen_buffer.c +203 -0
  134. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/screen_buffer.h +36 -0
  135. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/sheet-sql.c +167 -0
  136. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/sheet_internal.h +36 -0
  137. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/sqlfilter.c +153 -0
  138. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/terminfo.c +32 -0
  139. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/transformation.c +312 -0
  140. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/transformation.h +29 -0
  141. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/ui_buffer.c +266 -0
  142. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/usage.c +9 -0
  143. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/utf8-width.c +60 -0
  144. data/ext/zsv/vendor/zsv-1.3.0/app/sheet.c +1007 -0
  145. data/ext/zsv/vendor/zsv-1.3.0/app/sql.c +453 -0
  146. data/ext/zsv/vendor/zsv-1.3.0/app/sql_internal.c +101 -0
  147. data/ext/zsv/vendor/zsv-1.3.0/app/sql_internal.h +49 -0
  148. data/ext/zsv/vendor/zsv-1.3.0/app/stack.c +393 -0
  149. data/ext/zsv/vendor/zsv-1.3.0/app/utils/arg.c +322 -0
  150. data/ext/zsv/vendor/zsv-1.3.0/app/utils/cache.c +228 -0
  151. data/ext/zsv/vendor/zsv-1.3.0/app/utils/cat.c +91 -0
  152. data/ext/zsv/vendor/zsv-1.3.0/app/utils/chunk.c +240 -0
  153. data/ext/zsv/vendor/zsv-1.3.0/app/utils/chunk.h +63 -0
  154. data/ext/zsv/vendor/zsv-1.3.0/app/utils/clock.c +57 -0
  155. data/ext/zsv/vendor/zsv-1.3.0/app/utils/db.c +148 -0
  156. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs-no-jq.c +2 -0
  157. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs.c +427 -0
  158. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs_from_json.c +253 -0
  159. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs_to_json.c +121 -0
  160. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dl.c +20 -0
  161. data/ext/zsv/vendor/zsv-1.3.0/app/utils/emcc/fs_api.c +159 -0
  162. data/ext/zsv/vendor/zsv-1.3.0/app/utils/err.c +24 -0
  163. data/ext/zsv/vendor/zsv-1.3.0/app/utils/file-mem.c +180 -0
  164. data/ext/zsv/vendor/zsv-1.3.0/app/utils/file.c +256 -0
  165. data/ext/zsv/vendor/zsv-1.3.0/app/utils/index.c +197 -0
  166. data/ext/zsv/vendor/zsv-1.3.0/app/utils/index.h +49 -0
  167. data/ext/zsv/vendor/zsv-1.3.0/app/utils/jq.c +400 -0
  168. data/ext/zsv/vendor/zsv-1.3.0/app/utils/json.c +120 -0
  169. data/ext/zsv/vendor/zsv-1.3.0/app/utils/mem.c +18 -0
  170. data/ext/zsv/vendor/zsv-1.3.0/app/utils/memmem.c +132 -0
  171. data/ext/zsv/vendor/zsv-1.3.0/app/utils/os.c +178 -0
  172. data/ext/zsv/vendor/zsv-1.3.0/app/utils/overwrite.c +258 -0
  173. data/ext/zsv/vendor/zsv-1.3.0/app/utils/overwrite_writer.c +246 -0
  174. data/ext/zsv/vendor/zsv-1.3.0/app/utils/pcre2-8/pcre2-8-test.c +123 -0
  175. data/ext/zsv/vendor/zsv-1.3.0/app/utils/pcre2-8/pcre2-8.c +153 -0
  176. data/ext/zsv/vendor/zsv-1.3.0/app/utils/pcre2-8/pcre2-8.h +54 -0
  177. data/ext/zsv/vendor/zsv-1.3.0/app/utils/prop.c +267 -0
  178. data/ext/zsv/vendor/zsv-1.3.0/app/utils/signal.c +53 -0
  179. data/ext/zsv/vendor/zsv-1.3.0/app/utils/string.c +357 -0
  180. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/dir_exists_longpath.c +83 -0
  181. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/dl.c +33 -0
  182. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/fopen_longpath.c +184 -0
  183. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/foreach_dirent_longpath.c +292 -0
  184. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/io.c +259 -0
  185. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/io.h +13 -0
  186. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/mkdir_longpath.c +255 -0
  187. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/remove_longpath.c +96 -0
  188. data/ext/zsv/vendor/zsv-1.3.0/app/utils/writer.c +361 -0
  189. data/ext/zsv/vendor/zsv-1.3.0/app/zsv_command.h +40 -0
  190. data/ext/zsv/vendor/zsv-1.3.0/app/zsv_command_standalone.c +16 -0
  191. data/ext/zsv/vendor/zsv-1.3.0/app/zsv_main.h +44 -0
  192. data/ext/zsv/vendor/zsv-1.3.0/examples/js/zsv_parser_api_dummy.c +3 -0
  193. data/ext/zsv/vendor/zsv-1.3.0/examples/lib/parse_by_chunk.c +100 -0
  194. data/ext/zsv/vendor/zsv-1.3.0/examples/lib/print_my_column.c +143 -0
  195. data/ext/zsv/vendor/zsv-1.3.0/examples/lib/pull.c +89 -0
  196. data/ext/zsv/vendor/zsv-1.3.0/examples/lib/simple.c +123 -0
  197. data/ext/zsv/vendor/zsv-1.3.0/fuzz/fuzz.c +16 -0
  198. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/api.h +336 -0
  199. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/common.h +361 -0
  200. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/ext/implementation.h +62 -0
  201. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/ext/implementation_private.h +113 -0
  202. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/ext/sheet.h +73 -0
  203. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/ext.h +329 -0
  204. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/arg.h +90 -0
  205. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/cache.h +49 -0
  206. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/clock.h +36 -0
  207. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/compiler.h +58 -0
  208. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/db.h +19 -0
  209. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/dirs.h +147 -0
  210. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/dl.h +22 -0
  211. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/emcc/fs_api.h +28 -0
  212. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/err.h +22 -0
  213. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/file-mem.h +17 -0
  214. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/file.h +99 -0
  215. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/jq.h +65 -0
  216. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/json.h +19 -0
  217. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/mem.h +19 -0
  218. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/memmem.h +13 -0
  219. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/os.h +54 -0
  220. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/overwrite.h +71 -0
  221. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/overwrite_writer.h +53 -0
  222. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/prop.h +107 -0
  223. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/signal.h +18 -0
  224. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/sql.h +11 -0
  225. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/string.h +148 -0
  226. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/utf8.h +41 -0
  227. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/win/dl.h +25 -0
  228. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/writer.h +101 -0
  229. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/zsv_export.h +33 -0
  230. data/ext/zsv/vendor/zsv-1.3.0/include/zsv.h +20 -0
  231. data/ext/zsv/vendor/zsv-1.3.0/src/vector_delim.c +60 -0
  232. data/ext/zsv/vendor/zsv-1.3.0/src/zsv.c +484 -0
  233. data/ext/zsv/vendor/zsv-1.3.0/src/zsv_internal.c +731 -0
  234. data/ext/zsv/vendor/zsv-1.3.0/src/zsv_scan_delim.c +285 -0
  235. data/ext/zsv/vendor/zsv-1.3.0/src/zsv_scan_fixed.c +88 -0
  236. data/ext/zsv/vendor/zsv-1.3.0/src/zsv_strencode.c +51 -0
  237. data/ext/zsv/zsv_ext.c +343 -0
  238. data/lib/zsv/version.rb +5 -0
  239. data/lib/zsv.rb +81 -0
  240. metadata +340 -0
@@ -0,0 +1,796 @@
1
+ /*
2
+ * Copyright (C) 2021 Liquidaty and the zsv/lib contributors
3
+ * All rights reserved
4
+ *
5
+ * This file is part of zsv/lib, distributed under the license defined at
6
+ * https://opensource.org/licenses/MIT
7
+ */
8
+
9
+ #include <stdlib.h>
10
+ #include <stdio.h>
11
+ #include <string.h>
12
+ #include <zsv/utils/arg.h>
13
+ #include <zsv/utils/dl.h>
14
+ #include <zsv/utils/prop.h>
15
+ #include <zsv/utils/string.h>
16
+ #include <zsv/utils/dirs.h>
17
+ #include <zsv/utils/signal.h>
18
+ #ifdef ZSV_EXTRAS
19
+ #include <zsv/utils/overwrite.h>
20
+ #endif
21
+ #include <zsv.h>
22
+ #include <zsv/ext.h>
23
+ #include "cli_internal.h"
24
+ #include "cli_const.h"
25
+ #include "cli_export.h"
26
+ #ifdef ZSVSHEET_BUILD
27
+ #include "sheet/sheet_internal.h"
28
+ #include "sheet/handlers_internal.h"
29
+ #include "sheet/transformation.h"
30
+ #endif
31
+ #include "sheet/procedure.h"
32
+ #include "sheet/key-bindings.h"
33
+ #include "sql_internal.h"
34
+
35
+ struct cli_config {
36
+ struct zsv_ext *extensions;
37
+ char err_if_not_found;
38
+ char filepath[FILENAME_MAX];
39
+ char home_filepath[FILENAME_MAX];
40
+ const char *current_filepath; // path being currently parsed
41
+ char verbose;
42
+ };
43
+
44
+ static struct zsv_ext *zsv_ext_new(const char *dl_name, const char *id, char verbose);
45
+
46
+ zsvsheet_status zsvsheet_ext_prompt(struct zsvsheet_proc_context *ctx, char *buffer, size_t bufsz, const char *fmt,
47
+ ...);
48
+
49
+ #include "cli_ini.c"
50
+
51
+ typedef int(cmd_main)(int argc, const char *argv[]);
52
+ typedef int(zsv_cmd)(int argc, const char *argv[], struct zsv_opts *opts, struct zsv_prop_handler *custom_prop_handler);
53
+ typedef int (*cmd_reserved)(void);
54
+
55
+ struct builtin_cmd {
56
+ const char *name;
57
+ cmd_main *main;
58
+ zsv_cmd *cmd;
59
+ };
60
+
61
+ static int config_init(struct cli_config *c, char err_if_dl_not_found, char do_init, char verbose);
62
+ static int config_init_2(struct cli_config *c, char err_if_dl_not_found, char do_init, char verbose, char global_only,
63
+ char home_only);
64
+ #include "zsv_main.h"
65
+
66
+ #define CLI_BUILTIN_DECL(x) int main_##x(int argc, const char *argv[])
67
+
68
+ #define CLI_BUILTIN_DECL_STATIC(x) static int main_##x(int argc, const char *argv[])
69
+
70
+ CLI_BUILTIN_DECL_STATIC(version);
71
+ CLI_BUILTIN_DECL_STATIC(help);
72
+ CLI_BUILTIN_DECL_STATIC(thirdparty);
73
+
74
+ #ifndef __EMSCRIPTEN__
75
+ CLI_BUILTIN_DECL_STATIC(register);
76
+ CLI_BUILTIN_DECL_STATIC(unregister);
77
+ CLI_BUILTIN_DECL_STATIC(license);
78
+ #endif
79
+
80
+ ZSV_MAIN_DECL(select);
81
+ ZSV_MAIN_DECL(count);
82
+ ZSV_MAIN_DECL(paste);
83
+ ZSV_MAIN_DECL(check);
84
+ ZSV_MAIN_DECL(2json);
85
+ ZSV_MAIN_DECL(2tsv);
86
+ ZSV_MAIN_DECL(serialize);
87
+ ZSV_MAIN_DECL(flatten);
88
+ ZSV_MAIN_DECL(pretty);
89
+ ZSV_MAIN_DECL(stack);
90
+ ZSV_MAIN_DECL(desc);
91
+ ZSV_MAIN_DECL(sql);
92
+ ZSV_MAIN_DECL(2db);
93
+ ZSV_MAIN_DECL(compare);
94
+ #ifdef ZSVSHEET_BUILD
95
+ ZSV_MAIN_DECL(sheet);
96
+ #endif
97
+ ZSV_MAIN_DECL(echo);
98
+ #ifdef ZSV_EXTRAS
99
+ ZSV_MAIN_DECL(overwrite);
100
+ #endif
101
+ ZSV_MAIN_NO_OPTIONS_DECL(prop);
102
+ ZSV_MAIN_NO_OPTIONS_DECL(rm);
103
+ ZSV_MAIN_NO_OPTIONS_DECL(mv);
104
+
105
+ #ifdef USE_JQ
106
+ ZSV_MAIN_NO_OPTIONS_DECL(jq);
107
+ #endif
108
+
109
+ // clang-format off
110
+
111
+ #define CLI_BUILTIN_CMD(x) {.name = #x, .main = main_##x, .cmd = NULL}
112
+ #define CLI_BUILTIN_COMMAND(x) {.name = #x, .main = NULL, .cmd = ZSV_MAIN_FUNC(x)}
113
+ #define CLI_BUILTIN_COMMANDEXT(x) {.name = #x, .main = NULL, .cmd = ZSV_MAINEXT_FUNC(x)}
114
+ #define CLI_BUILTIN_NO_OPTIONS_COMMAND(x) {.name = #x, .main = ZSV_MAIN_NO_OPTIONS_FUNC(x), .cmd = NULL}
115
+
116
+ #ifdef ZSVSHEET_BUILD
117
+ ZSV_MAINEXT_FUNC_DEFINE(sheet);
118
+ #endif
119
+
120
+ struct builtin_cmd builtin_cmds[] = {
121
+ CLI_BUILTIN_CMD(version),
122
+ CLI_BUILTIN_CMD(help),
123
+ CLI_BUILTIN_CMD(thirdparty),
124
+ #ifndef __EMSCRIPTEN__
125
+ CLI_BUILTIN_CMD(register),
126
+ CLI_BUILTIN_CMD(unregister),
127
+ CLI_BUILTIN_CMD(license),
128
+ #endif
129
+ CLI_BUILTIN_COMMAND(select),
130
+ CLI_BUILTIN_COMMAND(count),
131
+ CLI_BUILTIN_COMMAND(paste),
132
+ CLI_BUILTIN_COMMAND(check),
133
+ CLI_BUILTIN_COMMAND(2json),
134
+ CLI_BUILTIN_COMMAND(2tsv),
135
+ CLI_BUILTIN_COMMAND(serialize),
136
+ CLI_BUILTIN_COMMAND(flatten),
137
+ CLI_BUILTIN_COMMAND(pretty),
138
+ CLI_BUILTIN_COMMAND(stack),
139
+ CLI_BUILTIN_COMMAND(desc),
140
+ CLI_BUILTIN_COMMAND(sql),
141
+ CLI_BUILTIN_COMMAND(2db),
142
+ CLI_BUILTIN_COMMAND(compare),
143
+ #ifdef ZSVSHEET_BUILD
144
+ CLI_BUILTIN_COMMANDEXT(sheet),
145
+ #endif
146
+ CLI_BUILTIN_COMMAND(echo),
147
+ #ifdef ZSV_EXTRAS
148
+ CLI_BUILTIN_COMMAND(overwrite),
149
+ #endif
150
+ CLI_BUILTIN_NO_OPTIONS_COMMAND(prop),
151
+ CLI_BUILTIN_NO_OPTIONS_COMMAND(rm),
152
+ CLI_BUILTIN_NO_OPTIONS_COMMAND(mv),
153
+ #ifdef USE_JQ
154
+ CLI_BUILTIN_NO_OPTIONS_COMMAND(jq),
155
+ #endif
156
+ };
157
+
158
+ // clang-format on
159
+
160
+ struct zsv_execution_data {
161
+ struct zsv_ext *ext;
162
+ zsv_parser parser;
163
+
164
+ int argc;
165
+ const char **argv;
166
+ void *custom_context; // user-defined
167
+ void *state; // program state relevant to the handler, e.g. cursor
168
+ // position in zsv sheet.
169
+ };
170
+
171
+ static struct zsv_opts ext_parser_opts(zsv_execution_context ctx) {
172
+ (void)(ctx);
173
+ return zsv_get_default_opts();
174
+ }
175
+
176
+ char **custom_cmd_names = NULL;
177
+
178
+ static enum zsv_ext_status ext_init(struct zsv_ext *ext);
179
+
180
+ static int config_init_2(struct cli_config *c, char err_if_dl_not_found, char do_init, char verbose, char global_only,
181
+ char home_only) {
182
+ memset(c, 0, sizeof(*c));
183
+ if (!global_only) { // find local (home dir) zsv.ini
184
+ if (!get_ini_file(c->home_filepath, FILENAME_MAX, 0))
185
+ *c->home_filepath = '\0';
186
+ }
187
+ if (!home_only) {
188
+ if (!get_ini_file(c->filepath, FILENAME_MAX, 1))
189
+ *c->filepath = '\0';
190
+ }
191
+ if (*c->home_filepath == '\0' && *c->filepath == '\0') {
192
+ fprintf(stderr, "Unable to get config filepath!\n");
193
+ return 1;
194
+ }
195
+
196
+ int rc = parse_extensions_ini(c, err_if_dl_not_found, verbose);
197
+ if (rc == 0 && do_init) {
198
+ for (struct zsv_ext *ext = c->extensions; ext; ext = ext->next) {
199
+ if (ext_init(ext) != zsv_ext_status_ok)
200
+ fprintf(stderr, "Error: unable to initialize extension %s\n", ext->id);
201
+ }
202
+ }
203
+ return rc;
204
+ }
205
+
206
+ static int config_init(struct cli_config *c, char err_if_dl_not_found, char do_init, char verbose) {
207
+ return config_init_2(c, err_if_dl_not_found, do_init, verbose, 0, 0);
208
+ }
209
+
210
+ static int config_free(struct cli_config *c) {
211
+ return zsv_unload_custom_cmds(c->extensions);
212
+ }
213
+
214
+ static void ext_set_context(zsv_execution_context ctx, void *custom_context) {
215
+ struct zsv_execution_data *d = ctx;
216
+ d->custom_context = custom_context;
217
+ }
218
+
219
+ static void *ext_get_context(zsv_execution_context ctx) {
220
+ struct zsv_execution_data *d = ctx;
221
+ return d->custom_context;
222
+ }
223
+
224
+ static void ext_set_state(zsv_execution_context ctx, void *state) {
225
+ struct zsv_execution_data *d = ctx;
226
+ d->state = state;
227
+ }
228
+
229
+ static void *ext_get_state(zsv_execution_context ctx) {
230
+ struct zsv_execution_data *d = ctx;
231
+ return d->state;
232
+ }
233
+
234
+ static void ext_set_parser(zsv_execution_context ctx, zsv_parser parser) {
235
+ struct zsv_execution_data *d = ctx;
236
+ d->parser = parser;
237
+ }
238
+
239
+ static zsv_parser ext_get_parser(zsv_execution_context ctx) {
240
+ struct zsv_execution_data *d = ctx;
241
+ return d->parser;
242
+ }
243
+
244
+ static void execution_context_free(struct zsv_execution_data *d) {
245
+ (void)(d);
246
+ }
247
+
248
+ static enum zsv_ext_status execution_context_init(struct zsv_execution_data *d, int argc, const char *argv[]) {
249
+ memset(d, 0, sizeof(*d));
250
+ d->argv = argv;
251
+ d->argc = argc;
252
+ return zsv_ext_status_ok;
253
+ }
254
+
255
+ static char *zsv_ext_errmsg(enum zsv_ext_status stat, zsv_execution_context ctx) {
256
+ switch (stat) {
257
+ case zsv_ext_status_ok:
258
+ return strdup("No error");
259
+ case zsv_ext_status_memory:
260
+ return strdup("Out of memory");
261
+ case zsv_ext_status_unrecognized_cmd:
262
+ if (!(ctx && ((struct zsv_execution_data *)ctx)->argc > 0))
263
+ return strdup("Unrecognized command");
264
+ else {
265
+ char *s;
266
+ struct zsv_execution_data *d = ctx;
267
+ asprintf(&s, "Unrecognized command %s", d->argv[1]);
268
+ return s;
269
+ }
270
+ // use zsv_ext_status_other for silent errors. will not attempt to call errcode() or errstr()
271
+ case zsv_ext_status_not_permitted:
272
+ return strdup("Not permitted");
273
+ case zsv_ext_status_other:
274
+ // use zsv_ext_status_err for custom errors. will attempt to call errcode() and errstr()
275
+ // for custom error code and message (if not errcode or errstr not provided, will be silent)
276
+ case zsv_ext_status_error:
277
+ return NULL;
278
+ }
279
+ return NULL;
280
+ }
281
+
282
+ // handle_ext_err(): return 1 if handled via ext callbacks, 0 otherwise
283
+ static int handle_ext_err(struct zsv_ext *ext, zsv_execution_context ctx, enum zsv_ext_status stat) {
284
+ int rc = stat;
285
+ char *msg = zsv_ext_errmsg(stat, ctx);
286
+ if (msg) {
287
+ fprintf(stderr, "Error in extension %s: %s\n", ext->id, msg);
288
+ free(msg);
289
+ } else if (ext->module.errcode) {
290
+ int ext_err = rc = ext->module.errcode(ctx);
291
+ char *errstr = ext->module.errstr ? ext->module.errstr(ctx, ext_err) : NULL;
292
+ if (errstr) {
293
+ fprintf(stderr, "Error (%s): %s\n", ext->id, errstr);
294
+ if (ext->module.errfree)
295
+ ext->module.errfree(errstr);
296
+ }
297
+ }
298
+ return rc;
299
+ }
300
+
301
+ static void ext_set_help(zsv_execution_context ctx, const char *help) {
302
+ struct zsv_execution_data *data = ctx;
303
+ if (data && data->ext) {
304
+ free(data->ext->help);
305
+ data->ext->help = help ? strdup(help) : NULL;
306
+ }
307
+ }
308
+
309
+ static void ext_set_license(zsv_execution_context ctx, const char *license) {
310
+ struct zsv_execution_data *data = ctx;
311
+ if (data && data->ext) {
312
+ free(data->ext->license);
313
+ data->ext->license = license ? strdup(license) : NULL;
314
+ }
315
+ }
316
+
317
+ static char *dup_str_array(const char *ss[]) {
318
+ size_t len = 0;
319
+ for (int i = 0; ss && ss[i]; i++)
320
+ len += strlen(ss[i]);
321
+
322
+ if (!len)
323
+ return NULL;
324
+ char *mem = malloc(len + 2 * sizeof(*mem));
325
+ if (mem) {
326
+ char *tmp = mem;
327
+ for (int i = 0; ss && ss[i]; i++) {
328
+ size_t n = strlen(ss[i]);
329
+ if (n) {
330
+ memcpy(tmp, ss[i], n);
331
+ tmp += n;
332
+ }
333
+ }
334
+ mem[len] = mem[len + 1] = '\0';
335
+ }
336
+ return mem;
337
+ }
338
+
339
+ static void ext_set_thirdparty(zsv_execution_context ctx, const char *thirdparty[]) {
340
+ struct zsv_execution_data *data = ctx;
341
+ if (data && data->ext) {
342
+ free(data->ext->thirdparty);
343
+ data->ext->thirdparty = dup_str_array(thirdparty);
344
+ }
345
+ }
346
+
347
+ static enum zsv_ext_status ext_add_command(zsv_execution_context ctx, const char *id, const char *help,
348
+ zsv_ext_main extmain) {
349
+ struct zsv_execution_data *data = ctx;
350
+ if (data && data->ext && data->ext->commands_next && id && *id && extmain) {
351
+ struct zsv_ext_command *cmd = ext_command_new(id, help, extmain);
352
+ if (cmd) {
353
+ *data->ext->commands_next = cmd;
354
+ data->ext->commands_next = &cmd->next;
355
+ return zsv_ext_status_ok;
356
+ }
357
+ }
358
+ return zsv_ext_status_error;
359
+ }
360
+
361
+ static enum zsv_ext_status ext_parse_all(zsv_execution_context ctx, void *user_context, void (*row_handler)(void *ctx),
362
+ struct zsv_opts *const custom) {
363
+ struct zsv_opts opts = custom ? *custom : ext_parser_opts(ctx);
364
+
365
+ if (row_handler)
366
+ opts.row_handler = row_handler;
367
+ zsv_parser parser = zsv_new(&opts);
368
+ if (!parser)
369
+ return zsv_ext_status_memory;
370
+
371
+ ext_set_parser(ctx, parser);
372
+ ext_set_context(ctx, user_context);
373
+ zsv_set_context(parser, ctx);
374
+
375
+ zsv_handle_ctrl_c_signal();
376
+ enum zsv_status stat = zsv_status_ok;
377
+ while (!zsv_signal_interrupted && (stat = zsv_parse_more(parser)) == zsv_status_ok)
378
+ ;
379
+ if (stat == zsv_status_no_more_input || (zsv_signal_interrupted && stat == zsv_status_ok))
380
+ stat = zsv_finish(parser);
381
+ zsv_delete(parser);
382
+
383
+ return stat ? zsv_ext_status_error : zsv_ext_status_ok;
384
+ }
385
+
386
+ static struct zsv_ext_callbacks *zsv_ext_callbacks_init(struct zsv_ext_callbacks *e) {
387
+ if (e) {
388
+ memset(e, 0, sizeof(*e));
389
+ e->set_row_handler = zsv_set_row_handler;
390
+ e->set_context = zsv_set_context;
391
+ e->parse_more = zsv_parse_more;
392
+ e->abort = zsv_abort;
393
+ e->cell_count = zsv_cell_count;
394
+ e->get_cell = zsv_get_cell;
395
+ e->finish = zsv_finish;
396
+ e->delete = zsv_delete;
397
+
398
+ e->ext_set_context = ext_set_context;
399
+ e->ext_get_context = ext_get_context;
400
+ e->ext_get_parser = ext_get_parser;
401
+ e->ext_add_command = ext_add_command;
402
+
403
+ e->ext_set_help = ext_set_help;
404
+ e->ext_set_license = ext_set_license;
405
+ e->ext_set_thirdparty = ext_set_thirdparty;
406
+
407
+ e->ext_parse_all = ext_parse_all;
408
+ e->ext_parser_opts = ext_parser_opts;
409
+
410
+ e->ext_sqlite3_add_csv = zsv_sqlite3_add_csv;
411
+ e->ext_sqlite3_db_delete = zsv_sqlite3_db_delete;
412
+ e->ext_sqlite3_db_new = zsv_sqlite3_db_new;
413
+ #ifdef ZSVSHEET_BUILD
414
+ e->ext_sheet_keypress = zsvsheet_ext_keypress;
415
+ e->ext_sheet_prompt = zsvsheet_ext_prompt;
416
+ e->ext_sheet_buffer_set_ctx = zsvsheet_buffer_set_ctx;
417
+ e->ext_sheet_buffer_get_ctx = zsvsheet_buffer_get_ctx;
418
+ e->ext_sheet_buffer_set_cell_attrs = zsvsheet_buffer_set_cell_attrs;
419
+ e->ext_sheet_cell_profile_attrs = zsvsheet_cell_profile_attrs;
420
+ e->ext_sheet_buffer_get_zsv_opts = zsvsheet_buffer_get_zsv_opts;
421
+ e->ext_sheet_buffer_on_newline = zsvsheet_buffer_on_newline;
422
+ e->ext_sheet_buffer_get_selected_cell = zsvsheet_buffer_get_selected_cell;
423
+ e->ext_sheet_set_status = zsvsheet_set_status;
424
+ e->ext_sheet_buffer_current = zsvsheet_buffer_current;
425
+ e->ext_sheet_buffer_prior = zsvsheet_buffer_prior;
426
+ e->ext_sheet_buffer_info = zsvsheet_buffer_info;
427
+ e->ext_sheet_buffer_filename = zsvsheet_buffer_filename;
428
+ e->ext_sheet_buffer_data_filename = zsvsheet_buffer_data_filename;
429
+ e->ext_sheet_open_file = zsvsheet_open_file;
430
+ e->ext_sheet_register_proc = zsvsheet_register_proc;
431
+ e->ext_sheet_register_proc_key_binding = zsvsheet_register_proc_key_binding;
432
+ e->ext_sheet_push_transformation = zsvsheet_push_transformation;
433
+ e->ext_sheet_transformation_writer = zsvsheet_transformation_writer;
434
+ e->ext_sheet_transformation_parser = zsvsheet_transformation_parser;
435
+ e->ext_sheet_transformation_filename = zsvsheet_transformation_filename;
436
+ e->ext_sheet_transformation_user_context = zsvsheet_transformation_user_context;
437
+ #endif
438
+ }
439
+ return e;
440
+ }
441
+
442
+ static enum zsv_ext_status ext_init(struct zsv_ext *ext) {
443
+ enum zsv_ext_status stat = zsv_ext_status_ok;
444
+ if (!ext->inited) {
445
+ if (!ext->ok)
446
+ return zsv_ext_status_error;
447
+
448
+ ext->inited = zsv_init_started;
449
+ struct zsv_ext_callbacks cb;
450
+ zsv_ext_callbacks_init(&cb);
451
+ ext->commands_next = &ext->commands;
452
+
453
+ struct zsv_execution_data d;
454
+ memset(&d, 0, sizeof(d));
455
+ d.ext = ext;
456
+ if ((stat = ext->module.init(&cb, &d)) != zsv_ext_status_ok) {
457
+ handle_ext_err(ext, NULL, stat);
458
+ return stat;
459
+ }
460
+ ext->inited = zsv_init_ok; // init ok
461
+ }
462
+ return stat;
463
+ }
464
+
465
+ static int zsv_unload_custom_cmds(struct zsv_ext *ext) {
466
+ int err = 0;
467
+ for (struct zsv_ext *next; ext; ext = next) {
468
+ next = ext->next;
469
+ if (zsv_ext_delete(ext))
470
+ err = 1;
471
+ }
472
+ return err;
473
+ }
474
+
475
+ struct zsv_ext_command *find_ext_cmd(struct zsv_ext *ext, const char *id) {
476
+ for (struct zsv_ext_command *cmd = ext->commands; cmd; cmd = cmd->next)
477
+ if (!strcmp(cmd->id, id))
478
+ return cmd;
479
+ return NULL;
480
+ }
481
+
482
+ static enum zsv_ext_status run_extension(int argc, const char *argv[], struct zsv_ext *ext) {
483
+ enum zsv_ext_status stat = zsv_ext_status_error;
484
+ if (ext) {
485
+ if ((stat = ext_init(ext)) != zsv_ext_status_ok)
486
+ return stat;
487
+
488
+ struct zsv_ext_command *cmd = find_ext_cmd(ext, argv[1] + 3);
489
+ if (!cmd) {
490
+ fprintf(stderr, "Unrecognized command for extension %s: %s\n", ext->id, argv[1] + 3);
491
+ return zsv_ext_status_unrecognized_cmd;
492
+ }
493
+
494
+ struct zsv_execution_data ctx = {0};
495
+ if ((stat = execution_context_init(&ctx, argc, argv)) == zsv_ext_status_ok) {
496
+ struct zsv_opts opts;
497
+ zsv_args_to_opts(argc, argv, &ctx.argc, ctx.argv, &opts);
498
+ zsv_set_default_opts(opts);
499
+ // need a corresponding zsv_set_default_custom_prop_handler?
500
+
501
+ stat = cmd->main(&ctx, ctx.argc - 1, &ctx.argv[1], &opts);
502
+ }
503
+
504
+ if (stat != zsv_ext_status_ok)
505
+ stat = handle_ext_err(ext, &ctx, stat);
506
+ execution_context_free(&ctx);
507
+ }
508
+ return stat;
509
+ }
510
+
511
+ // havearg(): case-insensitive partial arg matching
512
+ char havearg(const char *arg, const char *form1, size_t min_len1, const char *form2, size_t min_len2) {
513
+ size_t len = strlen(arg);
514
+ if (!min_len1)
515
+ min_len1 = strlen(form1);
516
+ if (len > min_len1)
517
+ min_len1 = len;
518
+
519
+ if (!zsv_strincmp_ascii((const unsigned char *)arg, min_len1, (const unsigned char *)form1, min_len1))
520
+ return 1;
521
+
522
+ if (form2) {
523
+ if (!min_len2)
524
+ min_len2 = strlen(form2);
525
+ if (len > min_len2)
526
+ min_len2 = len;
527
+ if (!zsv_strincmp_ascii((const unsigned char *)arg, min_len2, (const unsigned char *)form2, min_len2))
528
+ return 1;
529
+ }
530
+ return 0;
531
+ }
532
+
533
+ static struct builtin_cmd *find_builtin(const char *cmd_name) {
534
+ int builtin_cmd_count = sizeof(builtin_cmds) / sizeof(*builtin_cmds);
535
+ if (!strcmp(cmd_name, "-h") || !strcmp(cmd_name, "--help"))
536
+ cmd_name = "help";
537
+ for (int i = 0; i < builtin_cmd_count; i++)
538
+ if (havearg(cmd_name, builtin_cmds[i].name, 0, 0, 0))
539
+ return &builtin_cmds[i];
540
+ return NULL;
541
+ }
542
+
543
+ #include "builtin/license.c"
544
+ #include "builtin/thirdparty.c"
545
+ #include "builtin/help.c"
546
+ #include "builtin/version.c"
547
+ #include "builtin/register.c"
548
+
549
+ static const char *extension_cmd_from_arg(const char *arg) {
550
+ const char *dash = strchr(arg, '-');
551
+ if (dash && dash < arg + ZSV_EXTENSION_ID_MAX_LEN && dash[1] != '\0')
552
+ return dash + 1;
553
+ return NULL;
554
+ }
555
+
556
+ #ifndef ZSV_CLI_MAIN
557
+ #define ZSV_CLI_MAIN main
558
+ #endif
559
+
560
+ ZSV_CLI_EXPORT
561
+ int ZSV_CLI_MAIN(int argc, const char *argv[]) {
562
+ const char **alt_argv = NULL;
563
+ struct builtin_cmd *builtin = find_builtin(argc > 1 ? argv[1] : "help");
564
+ if (builtin) {
565
+ // help is different from other commands: zsv help <arg> is treated as
566
+ // if it was zsv <arg> --help
567
+ if (builtin->main == main_help && argc > 2) {
568
+ struct builtin_cmd *help_builtin = find_builtin(argv[2]);
569
+ if (help_builtin) {
570
+ const char *argv_tmp[2] = {argv[2], "--help"};
571
+ if (help_builtin->main)
572
+ return help_builtin->main(2, argv_tmp);
573
+ else if (help_builtin->cmd) {
574
+ struct zsv_opts opts = {0};
575
+ return help_builtin->cmd(2, argv_tmp, &opts, NULL);
576
+ } else
577
+ return fprintf(stderr, "Unexpected syntax!\n");
578
+ } else {
579
+ const char *ext_cmd = extension_cmd_from_arg(argv[2]);
580
+ if (ext_cmd) {
581
+ alt_argv = calloc(3, sizeof(*alt_argv));
582
+ alt_argv[0] = argv[0];
583
+ alt_argv[1] = argv[2];
584
+ alt_argv[2] = "--help";
585
+ argc = 3;
586
+ argv = alt_argv;
587
+ } else {
588
+ fprintf(stderr, "Unrecognized command %s\n", argv[2]);
589
+ free(alt_argv);
590
+ return 1;
591
+ }
592
+ }
593
+ } else {
594
+ if (builtin->main)
595
+ return builtin->main(argc - 1, argc > 1 ? &argv[1] : NULL);
596
+
597
+ struct zsv_opts opts;
598
+ enum zsv_status stat = zsv_args_to_opts(argc, argv, &argc, argv, &opts);
599
+ if (stat == zsv_status_ok)
600
+ return builtin->cmd(argc - 1, argc > 1 ? &argv[1] : NULL, &opts, NULL);
601
+ return stat;
602
+ }
603
+ }
604
+
605
+ int err = 1;
606
+ if (extension_cmd_from_arg(argv[1]) != NULL) { // this is an extension command
607
+ struct cli_config config;
608
+ memset(&config, 0, sizeof(config));
609
+ if (!(err = add_extension(argv[1], &config.extensions, 0, 0))) {
610
+ if (config.extensions)
611
+ err = run_extension(argc, argv, config.extensions);
612
+ else {
613
+ fprintf(stderr, "Unrecognized command or extension %s\n", argv[1]);
614
+ ;
615
+ err = 1;
616
+ }
617
+ }
618
+ if (config_free(&config) && !err)
619
+ err = 1;
620
+ } else
621
+ fprintf(stderr, "Unrecognized command %s\n", argv[1]), err = 1;
622
+
623
+ free(alt_argv);
624
+ return err;
625
+ }
626
+
627
+ // extensions
628
+ static enum zsv_ext_status zsv_ext_delete(struct zsv_ext *ext) {
629
+ enum zsv_ext_status stat = zsv_ext_status_ok;
630
+ if (ext) {
631
+ if (ext->dl) {
632
+ if (ext->module.exit && ext->inited) {
633
+ stat = ext->module.exit();
634
+ if (stat != zsv_ext_status_ok)
635
+ handle_ext_err(ext, NULL, stat);
636
+ }
637
+ dlclose(ext->dl);
638
+ }
639
+ for (struct zsv_ext_command *next, *cmd = ext->commands; cmd; cmd = next) {
640
+ next = cmd->next;
641
+ ext_command_delete(cmd);
642
+ }
643
+ free(ext->id);
644
+ free(ext->help);
645
+ free(ext->license);
646
+ free(ext->thirdparty);
647
+ free(ext);
648
+ }
649
+ return stat;
650
+ }
651
+
652
+ /**
653
+ * zsv_ext_init(): return error
654
+ */
655
+ static int zsv_ext_init(void *dl, const char *dl_name, struct zsv_ext *ext) {
656
+ memset(ext, 0, sizeof(*ext));
657
+ if (dl) {
658
+ #define zsv_ext_func_assign(sig, x) ext->module.x = sig zsv_dlsym(dl, "zsv_ext_" #x)
659
+ #include "cli_internal.c.in"
660
+
661
+ /* check if required functions are present */
662
+ if (ext->module.id)
663
+ return 0;
664
+ fprintf(stderr, "Dynamic library %s missing required function zsv_ext_id()\n", dl_name);
665
+ }
666
+ return 1;
667
+ }
668
+
669
+ #ifdef __APPLE__
670
+ #include <dlfcn.h>
671
+ #endif
672
+
673
+ #ifdef _WIN32
674
+ char *get_module_name(HMODULE handle) {
675
+ wchar_t *pth16 = (wchar_t *)malloc(32768); // max long path length
676
+ DWORD n16 = GetModuleFileNameW(handle, pth16, 32768);
677
+ if (n16 <= 0) {
678
+ free(pth16);
679
+ return NULL;
680
+ }
681
+ pth16[n16] = L'\0';
682
+ DWORD n8 = WideCharToMultiByte(CP_UTF8, 0, pth16, -1, NULL, 0, NULL, NULL);
683
+ if (n8 == 0) {
684
+ free(pth16);
685
+ return NULL;
686
+ }
687
+ char *filepath = (char *)malloc(++n8);
688
+ if (!WideCharToMultiByte(CP_UTF8, 0, pth16, -1, filepath, n8, NULL, NULL)) {
689
+ free(pth16);
690
+ free(filepath);
691
+ return NULL;
692
+ }
693
+ free(pth16);
694
+ return filepath;
695
+ }
696
+ #endif
697
+
698
+ static char *dl_name_from_func(const void *func) {
699
+ #ifdef _WIN32
700
+ HMODULE hModule = NULL;
701
+ if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
702
+ (LPCTSTR)func, &hModule))
703
+ return get_module_name(hModule);
704
+ #elif defined __APPLE__
705
+ Dl_info info;
706
+ if (dladdr(func, &info))
707
+ return strdup(info.dli_fname);
708
+ #else
709
+ (void)(func);
710
+ #endif
711
+ return NULL;
712
+ }
713
+
714
+ #ifdef __EMSCRIPTEN__
715
+ static void *zsv_dlopen(const char *path, int mode) {
716
+ fprintf(stderr, "Emscripten dlopen() does not work for shared libs > 4kb!"
717
+ "fix this when it does."
718
+ "See https://github.com/emscripten-core/emscripten/issues/15795\n");
719
+ return dlopen(path, mode);
720
+ }
721
+ #else
722
+ #define zsv_dlopen dlopen
723
+ #endif
724
+
725
+ static struct zsv_ext *zsv_ext_new(const char *dl_name, const char *id, char verbose) {
726
+ struct zsv_ext *dl = NULL;
727
+ struct zsv_ext tmp;
728
+ #ifdef __EMSCRIPTEN__
729
+ void *h = NULL;
730
+ #else
731
+ void *h = zsv_dlopen(dl_name, RTLD_LAZY);
732
+ #endif
733
+ if (!h) {
734
+ // not in system search path. try again in the same path as our executable (or in /ext if in emcc)
735
+ char exe_path[FILENAME_MAX];
736
+ char have_path = 0;
737
+ #ifdef __EMSCRIPTEN__
738
+ size_t n = snprintf(exe_path, sizeof(exe_path), "/tmp/zsvext%s.so", id);
739
+ if (n > 0 && n < sizeof(exe_path)) {
740
+ fprintf(stderr, "Opening %s\n", exe_path);
741
+ h = zsv_dlopen(exe_path, RTLD_LAZY);
742
+ } else
743
+ fprintf(stderr, "Opening what???\n");
744
+ #else
745
+ size_t n = zsv_get_executable_path(exe_path, sizeof(exe_path));
746
+ if (n > 0 && n < sizeof(exe_path)) {
747
+ char *end = strrchr(exe_path, FILESLASH);
748
+ if (end) {
749
+ end[1] = '\0';
750
+ n = strlen(exe_path);
751
+ size_t n2 = snprintf(exe_path + n, sizeof(exe_path) - n, "%s", dl_name);
752
+ if (n2 > 0 && n + n2 < sizeof(exe_path)) {
753
+ have_path = 1;
754
+ h = zsv_dlopen(exe_path, RTLD_LAZY);
755
+ }
756
+ }
757
+ }
758
+ #endif
759
+ if (verbose) {
760
+ if (have_path)
761
+ fprintf(stderr, "Library %s not found in path; trying exe dir %s\n", dl_name, exe_path);
762
+ else
763
+ fprintf(stderr, "Library %s not found in path; cannot determine exe dir\n", dl_name);
764
+ }
765
+ }
766
+ if (!h)
767
+ #if defined(WIN32) || defined(_WIN32)
768
+ fprintf(stderr, "Library %s not found\n", dl_name);
769
+ #else
770
+ fprintf(stderr, "Library %s failed to load: %s\n", dl_name, dlerror());
771
+ #endif
772
+
773
+ // run zsv_ext_init to add to our extension list, even if it's invalid
774
+ tmp.ok = !zsv_ext_init(h, dl_name, &tmp);
775
+ const char *m_id = tmp.ok && tmp.module.id ? tmp.module.id() : NULL;
776
+ if (h && (!m_id || strcmp(m_id, (const char *)id))) {
777
+ fprintf(stderr,
778
+ "Library %s: unexpected result from zsv_ext_id()\n"
779
+ "(got %s, expected %s)\n",
780
+ id, m_id ? m_id : "(null)", id);
781
+ tmp.ok = 0;
782
+ } else if (h && tmp.ok) {
783
+ if (verbose) {
784
+ char *image_name = dl_name_from_func((const void *)tmp.module.id);
785
+ fprintf(stderr, "Loaded %s from %s\n", dl_name, image_name ? image_name : "unknown location");
786
+ free(image_name);
787
+ }
788
+ }
789
+ tmp.dl = h;
790
+ tmp.id = strdup((const char *)id);
791
+ if (!(dl = calloc(1, sizeof(*dl))))
792
+ fprintf(stderr, "Out of memory\n");
793
+ else
794
+ *dl = tmp;
795
+ return dl;
796
+ }