zsv 1.3.0 → 1.4.3

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 (232) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -0
  3. data/README.md +41 -3
  4. data/ext/zsv/extconf.rb +66 -49
  5. data/ext/zsv/options.c +1 -2
  6. data/ext/zsv/parser.c +14 -0
  7. data/ext/zsv/zsv_ext.c +3 -0
  8. data/lib/zsv/version.rb +1 -1
  9. metadata +6 -226
  10. data/ext/zsv/vendor/zsv-1.3.0/app/2db.c +0 -756
  11. data/ext/zsv/vendor/zsv-1.3.0/app/2json.c +0 -381
  12. data/ext/zsv/vendor/zsv-1.3.0/app/2tsv.c +0 -228
  13. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/help.c +0 -123
  14. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/license.c +0 -39
  15. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/register.c +0 -104
  16. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/thirdparty.c +0 -41
  17. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/unregister.c +0 -1
  18. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/version.c +0 -14
  19. data/ext/zsv/vendor/zsv-1.3.0/app/check/simdutf_wrapper.h +0 -19
  20. data/ext/zsv/vendor/zsv-1.3.0/app/check/utf8.c +0 -116
  21. data/ext/zsv/vendor/zsv-1.3.0/app/check.c +0 -194
  22. data/ext/zsv/vendor/zsv-1.3.0/app/cli.c +0 -796
  23. data/ext/zsv/vendor/zsv-1.3.0/app/cli_const.h +0 -41
  24. data/ext/zsv/vendor/zsv-1.3.0/app/cli_export.h +0 -16
  25. data/ext/zsv/vendor/zsv-1.3.0/app/cli_ini.c +0 -280
  26. data/ext/zsv/vendor/zsv-1.3.0/app/cli_internal.h +0 -36
  27. data/ext/zsv/vendor/zsv-1.3.0/app/compare.c +0 -913
  28. data/ext/zsv/vendor/zsv-1.3.0/app/compare.h +0 -23
  29. data/ext/zsv/vendor/zsv-1.3.0/app/compare_added_column.c +0 -20
  30. data/ext/zsv/vendor/zsv-1.3.0/app/compare_internal.h +0 -140
  31. data/ext/zsv/vendor/zsv-1.3.0/app/compare_sort.c +0 -91
  32. data/ext/zsv/vendor/zsv-1.3.0/app/compare_unique_colname.c +0 -81
  33. data/ext/zsv/vendor/zsv-1.3.0/app/count-pull.c +0 -82
  34. data/ext/zsv/vendor/zsv-1.3.0/app/count.c +0 -404
  35. data/ext/zsv/vendor/zsv-1.3.0/app/desc.c +0 -569
  36. data/ext/zsv/vendor/zsv-1.3.0/app/echo.c +0 -365
  37. data/ext/zsv/vendor/zsv-1.3.0/app/ext_example/my_extension.c +0 -366
  38. data/ext/zsv/vendor/zsv-1.3.0/app/ext_example/mysheet_extension.c +0 -341
  39. data/ext/zsv/vendor/zsv-1.3.0/app/ext_template/YOUR_EXTENSION_zsvext.c +0 -263
  40. data/ext/zsv/vendor/zsv-1.3.0/app/external/inih/ini.c +0 -298
  41. data/ext/zsv/vendor/zsv-1.3.0/app/external/inih/ini.h +0 -157
  42. data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/json_numeric.c +0 -177
  43. data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/jsonwriter.c +0 -444
  44. data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/jsonwriter.h +0 -145
  45. data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/utils.c +0 -110
  46. data/ext/zsv/vendor/zsv-1.3.0/app/external/memfile-1.0/include/memfile.h +0 -15
  47. data/ext/zsv/vendor/zsv-1.3.0/app/external/memfile-1.0/src/memfile.c +0 -64
  48. data/ext/zsv/vendor/zsv-1.3.0/app/external/sglib/sglib.h +0 -1955
  49. data/ext/zsv/vendor/zsv-1.3.0/app/external/simdutf/simdutf.h +0 -6802
  50. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3.c +0 -230517
  51. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3.h +0 -12174
  52. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_and_csv_vtab.c +0 -2
  53. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab-mem.c +0 -142
  54. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab-mem.h +0 -49
  55. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab-zsv.c +0 -485
  56. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab.c +0 -1015
  57. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3ext.h +0 -663
  58. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/vtab_helper.c +0 -85
  59. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_common.h +0 -75
  60. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_gen.h +0 -167
  61. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_parse.h +0 -228
  62. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_tree.h +0 -186
  63. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_version.h +0 -23
  64. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_common.h +0 -76
  65. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_gen.h +0 -167
  66. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_parse.h +0 -238
  67. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_tree.h +0 -186
  68. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl.c +0 -184
  69. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_alloc.c +0 -52
  70. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_alloc.h +0 -34
  71. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_buf.c +0 -103
  72. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_buf.h +0 -57
  73. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_bytestack.h +0 -69
  74. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_encode.c +0 -220
  75. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_encode.h +0 -34
  76. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_gen.c +0 -362
  77. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_lex.c +0 -764
  78. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_lex.h +0 -117
  79. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_parser.c +0 -508
  80. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_parser.h +0 -78
  81. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_tree.c +0 -505
  82. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_version.c +0 -7
  83. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper/json_value.h +0 -59
  84. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper/yajl_helper.h +0 -208
  85. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper.c +0 -795
  86. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper_internal.h +0 -28
  87. data/ext/zsv/vendor/zsv-1.3.0/app/flatten.c +0 -851
  88. data/ext/zsv/vendor/zsv-1.3.0/app/jq.c +0 -106
  89. data/ext/zsv/vendor/zsv-1.3.0/app/jq.h +0 -6
  90. data/ext/zsv/vendor/zsv-1.3.0/app/mv.c +0 -113
  91. data/ext/zsv/vendor/zsv-1.3.0/app/noop.c +0 -90
  92. data/ext/zsv/vendor/zsv-1.3.0/app/overwrite.c +0 -295
  93. data/ext/zsv/vendor/zsv-1.3.0/app/paste.c +0 -175
  94. data/ext/zsv/vendor/zsv-1.3.0/app/pretty.c +0 -693
  95. data/ext/zsv/vendor/zsv-1.3.0/app/prop.c +0 -980
  96. data/ext/zsv/vendor/zsv-1.3.0/app/rm.c +0 -131
  97. data/ext/zsv/vendor/zsv-1.3.0/app/select/fixed.c +0 -130
  98. data/ext/zsv/vendor/zsv-1.3.0/app/select/internal.h +0 -118
  99. data/ext/zsv/vendor/zsv-1.3.0/app/select/parallel.c +0 -45
  100. data/ext/zsv/vendor/zsv-1.3.0/app/select/parallel.h +0 -41
  101. data/ext/zsv/vendor/zsv-1.3.0/app/select/processing.c +0 -107
  102. data/ext/zsv/vendor/zsv-1.3.0/app/select/rand.c +0 -20
  103. data/ext/zsv/vendor/zsv-1.3.0/app/select/regex.c +0 -61
  104. data/ext/zsv/vendor/zsv-1.3.0/app/select/search.c +0 -14
  105. data/ext/zsv/vendor/zsv-1.3.0/app/select/selection.c +0 -192
  106. data/ext/zsv/vendor/zsv-1.3.0/app/select/usage.c +0 -72
  107. data/ext/zsv/vendor/zsv-1.3.0/app/select-pull.c +0 -812
  108. data/ext/zsv/vendor/zsv-1.3.0/app/select.c +0 -753
  109. data/ext/zsv/vendor/zsv-1.3.0/app/serialize.c +0 -372
  110. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/curses.h +0 -15
  111. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/cursor.c +0 -119
  112. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/errors.c +0 -45
  113. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/file.c +0 -63
  114. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/file.h +0 -12
  115. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/filter.c +0 -166
  116. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/handlers.c +0 -214
  117. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/handlers_internal.h +0 -128
  118. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/help.c +0 -43
  119. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/index.c +0 -81
  120. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/index.h +0 -25
  121. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/key-bindings.c +0 -325
  122. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/key-bindings.h +0 -73
  123. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/lexer.c +0 -203
  124. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/newline_handler.c +0 -7
  125. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/pivot.c +0 -318
  126. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/procedure.c +0 -134
  127. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/procedure.h +0 -119
  128. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/read-data.c +0 -322
  129. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/screen_buffer.c +0 -203
  130. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/screen_buffer.h +0 -36
  131. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/sheet-sql.c +0 -167
  132. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/sheet_internal.h +0 -36
  133. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/sqlfilter.c +0 -153
  134. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/terminfo.c +0 -32
  135. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/transformation.c +0 -312
  136. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/transformation.h +0 -29
  137. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/ui_buffer.c +0 -266
  138. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/usage.c +0 -9
  139. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/utf8-width.c +0 -60
  140. data/ext/zsv/vendor/zsv-1.3.0/app/sheet.c +0 -1007
  141. data/ext/zsv/vendor/zsv-1.3.0/app/sql.c +0 -453
  142. data/ext/zsv/vendor/zsv-1.3.0/app/sql_internal.c +0 -101
  143. data/ext/zsv/vendor/zsv-1.3.0/app/sql_internal.h +0 -49
  144. data/ext/zsv/vendor/zsv-1.3.0/app/stack.c +0 -393
  145. data/ext/zsv/vendor/zsv-1.3.0/app/utils/arg.c +0 -322
  146. data/ext/zsv/vendor/zsv-1.3.0/app/utils/cache.c +0 -228
  147. data/ext/zsv/vendor/zsv-1.3.0/app/utils/cat.c +0 -91
  148. data/ext/zsv/vendor/zsv-1.3.0/app/utils/chunk.c +0 -240
  149. data/ext/zsv/vendor/zsv-1.3.0/app/utils/chunk.h +0 -63
  150. data/ext/zsv/vendor/zsv-1.3.0/app/utils/clock.c +0 -57
  151. data/ext/zsv/vendor/zsv-1.3.0/app/utils/db.c +0 -148
  152. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs-no-jq.c +0 -2
  153. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs.c +0 -427
  154. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs_from_json.c +0 -253
  155. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs_to_json.c +0 -121
  156. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dl.c +0 -20
  157. data/ext/zsv/vendor/zsv-1.3.0/app/utils/emcc/fs_api.c +0 -159
  158. data/ext/zsv/vendor/zsv-1.3.0/app/utils/err.c +0 -24
  159. data/ext/zsv/vendor/zsv-1.3.0/app/utils/file-mem.c +0 -180
  160. data/ext/zsv/vendor/zsv-1.3.0/app/utils/file.c +0 -256
  161. data/ext/zsv/vendor/zsv-1.3.0/app/utils/index.c +0 -197
  162. data/ext/zsv/vendor/zsv-1.3.0/app/utils/index.h +0 -49
  163. data/ext/zsv/vendor/zsv-1.3.0/app/utils/jq.c +0 -400
  164. data/ext/zsv/vendor/zsv-1.3.0/app/utils/json.c +0 -120
  165. data/ext/zsv/vendor/zsv-1.3.0/app/utils/mem.c +0 -18
  166. data/ext/zsv/vendor/zsv-1.3.0/app/utils/memmem.c +0 -132
  167. data/ext/zsv/vendor/zsv-1.3.0/app/utils/os.c +0 -178
  168. data/ext/zsv/vendor/zsv-1.3.0/app/utils/overwrite.c +0 -258
  169. data/ext/zsv/vendor/zsv-1.3.0/app/utils/overwrite_writer.c +0 -246
  170. data/ext/zsv/vendor/zsv-1.3.0/app/utils/pcre2-8/pcre2-8-test.c +0 -123
  171. data/ext/zsv/vendor/zsv-1.3.0/app/utils/pcre2-8/pcre2-8.c +0 -153
  172. data/ext/zsv/vendor/zsv-1.3.0/app/utils/pcre2-8/pcre2-8.h +0 -54
  173. data/ext/zsv/vendor/zsv-1.3.0/app/utils/prop.c +0 -267
  174. data/ext/zsv/vendor/zsv-1.3.0/app/utils/signal.c +0 -53
  175. data/ext/zsv/vendor/zsv-1.3.0/app/utils/string.c +0 -357
  176. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/dir_exists_longpath.c +0 -83
  177. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/dl.c +0 -33
  178. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/fopen_longpath.c +0 -184
  179. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/foreach_dirent_longpath.c +0 -292
  180. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/io.c +0 -259
  181. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/io.h +0 -13
  182. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/mkdir_longpath.c +0 -255
  183. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/remove_longpath.c +0 -96
  184. data/ext/zsv/vendor/zsv-1.3.0/app/utils/writer.c +0 -361
  185. data/ext/zsv/vendor/zsv-1.3.0/app/zsv_command.h +0 -40
  186. data/ext/zsv/vendor/zsv-1.3.0/app/zsv_command_standalone.c +0 -16
  187. data/ext/zsv/vendor/zsv-1.3.0/app/zsv_main.h +0 -44
  188. data/ext/zsv/vendor/zsv-1.3.0/examples/js/zsv_parser_api_dummy.c +0 -3
  189. data/ext/zsv/vendor/zsv-1.3.0/examples/lib/parse_by_chunk.c +0 -100
  190. data/ext/zsv/vendor/zsv-1.3.0/examples/lib/print_my_column.c +0 -143
  191. data/ext/zsv/vendor/zsv-1.3.0/examples/lib/pull.c +0 -89
  192. data/ext/zsv/vendor/zsv-1.3.0/examples/lib/simple.c +0 -123
  193. data/ext/zsv/vendor/zsv-1.3.0/fuzz/fuzz.c +0 -16
  194. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/api.h +0 -336
  195. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/common.h +0 -361
  196. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/ext/implementation.h +0 -62
  197. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/ext/implementation_private.h +0 -113
  198. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/ext/sheet.h +0 -73
  199. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/ext.h +0 -329
  200. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/arg.h +0 -90
  201. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/cache.h +0 -49
  202. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/clock.h +0 -36
  203. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/compiler.h +0 -58
  204. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/db.h +0 -19
  205. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/dirs.h +0 -147
  206. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/dl.h +0 -22
  207. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/emcc/fs_api.h +0 -28
  208. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/err.h +0 -22
  209. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/file-mem.h +0 -17
  210. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/file.h +0 -99
  211. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/jq.h +0 -65
  212. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/json.h +0 -19
  213. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/mem.h +0 -19
  214. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/memmem.h +0 -13
  215. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/os.h +0 -54
  216. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/overwrite.h +0 -71
  217. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/overwrite_writer.h +0 -53
  218. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/prop.h +0 -107
  219. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/signal.h +0 -18
  220. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/sql.h +0 -11
  221. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/string.h +0 -148
  222. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/utf8.h +0 -41
  223. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/win/dl.h +0 -25
  224. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/writer.h +0 -101
  225. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/zsv_export.h +0 -33
  226. data/ext/zsv/vendor/zsv-1.3.0/include/zsv.h +0 -20
  227. data/ext/zsv/vendor/zsv-1.3.0/src/vector_delim.c +0 -60
  228. data/ext/zsv/vendor/zsv-1.3.0/src/zsv.c +0 -484
  229. data/ext/zsv/vendor/zsv-1.3.0/src/zsv_internal.c +0 -731
  230. data/ext/zsv/vendor/zsv-1.3.0/src/zsv_scan_delim.c +0 -285
  231. data/ext/zsv/vendor/zsv-1.3.0/src/zsv_scan_fixed.c +0 -88
  232. data/ext/zsv/vendor/zsv-1.3.0/src/zsv_strencode.c +0 -51
@@ -1,796 +0,0 @@
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
- }