@port-labs/jq-node-bindings 0.0.1

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 (131) hide show
  1. package/.editorconfig +5 -0
  2. package/.jshintignore +1 -0
  3. package/.jshintrc +23 -0
  4. package/binding.gyp +56 -0
  5. package/configure +26 -0
  6. package/deps/jq/.gitattributes +2 -0
  7. package/deps/jq/.travis.yml +53 -0
  8. package/deps/jq/AUTHORS +73 -0
  9. package/deps/jq/COPYING +70 -0
  10. package/deps/jq/ChangeLog +1349 -0
  11. package/deps/jq/Makefile.am +198 -0
  12. package/deps/jq/NEWS +88 -0
  13. package/deps/jq/README.md +64 -0
  14. package/deps/jq/builtin.c +1684 -0
  15. package/deps/jq/builtin.h +10 -0
  16. package/deps/jq/bytecode.c +161 -0
  17. package/deps/jq/bytecode.h +92 -0
  18. package/deps/jq/compile-ios.sh +102 -0
  19. package/deps/jq/compile.c +1210 -0
  20. package/deps/jq/compile.h +101 -0
  21. package/deps/jq/config/m4/check-math-func.m4 +4 -0
  22. package/deps/jq/config/m4/find-func-no-libs.m4 +8 -0
  23. package/deps/jq/config/m4/find-func-no-libs2.m4 +62 -0
  24. package/deps/jq/config/m4/find-func.m4 +9 -0
  25. package/deps/jq/config/m4/misc.m4 +3 -0
  26. package/deps/jq/configure.ac +221 -0
  27. package/deps/jq/docs/Gemfile +7 -0
  28. package/deps/jq/docs/Gemfile.lock +63 -0
  29. package/deps/jq/docs/README.md +25 -0
  30. package/deps/jq/docs/Rakefile +145 -0
  31. package/deps/jq/docs/content/1.tutorial/default.yml +327 -0
  32. package/deps/jq/docs/content/2.download/default.yml +117 -0
  33. package/deps/jq/docs/content/3.manual/manual.yml +2878 -0
  34. package/deps/jq/docs/content/3.manual/v1.3/manual.yml +1270 -0
  35. package/deps/jq/docs/content/3.manual/v1.4/manual.yml +1672 -0
  36. package/deps/jq/docs/content/index/index.yml +51 -0
  37. package/deps/jq/docs/default_manpage.md +22 -0
  38. package/deps/jq/docs/public/.htaccess +28 -0
  39. package/deps/jq/docs/public/bootstrap/css/bootstrap-responsive.css +1058 -0
  40. package/deps/jq/docs/public/bootstrap/css/bootstrap-responsive.min.css +9 -0
  41. package/deps/jq/docs/public/bootstrap/css/bootstrap.css +5224 -0
  42. package/deps/jq/docs/public/bootstrap/css/bootstrap.min.css +9 -0
  43. package/deps/jq/docs/public/bootstrap/img/glyphicons-halflings-white.png +0 -0
  44. package/deps/jq/docs/public/bootstrap/img/glyphicons-halflings.png +0 -0
  45. package/deps/jq/docs/public/bootstrap/js/bootstrap.js +2027 -0
  46. package/deps/jq/docs/public/bootstrap/js/bootstrap.min.js +6 -0
  47. package/deps/jq/docs/public/css/base.scss +99 -0
  48. package/deps/jq/docs/public/jq.png +0 -0
  49. package/deps/jq/docs/public/robots.txt +2 -0
  50. package/deps/jq/docs/site.yml +18 -0
  51. package/deps/jq/docs/templates/default.liquid +34 -0
  52. package/deps/jq/docs/templates/index.liquid +60 -0
  53. package/deps/jq/docs/templates/manual.liquid +122 -0
  54. package/deps/jq/docs/templates/shared/_footer.liquid +5 -0
  55. package/deps/jq/docs/templates/shared/_head.liquid +12 -0
  56. package/deps/jq/docs/templates/shared/_header.liquid +26 -0
  57. package/deps/jq/exec_stack.h +112 -0
  58. package/deps/jq/execute.c +1155 -0
  59. package/deps/jq/inject_errors.c +112 -0
  60. package/deps/jq/jq.1.default +39 -0
  61. package/deps/jq/jq.1.prebuilt +3075 -0
  62. package/deps/jq/jq.h +60 -0
  63. package/deps/jq/jq.spec +70 -0
  64. package/deps/jq/jq_parser.h +9 -0
  65. package/deps/jq/jq_test.c +346 -0
  66. package/deps/jq/jv.c +1333 -0
  67. package/deps/jq/jv.h +240 -0
  68. package/deps/jq/jv_alloc.c +179 -0
  69. package/deps/jq/jv_alloc.h +27 -0
  70. package/deps/jq/jv_aux.c +619 -0
  71. package/deps/jq/jv_dtoa.c +4275 -0
  72. package/deps/jq/jv_dtoa.h +22 -0
  73. package/deps/jq/jv_file.c +49 -0
  74. package/deps/jq/jv_parse.c +852 -0
  75. package/deps/jq/jv_print.c +348 -0
  76. package/deps/jq/jv_unicode.c +96 -0
  77. package/deps/jq/jv_unicode.h +11 -0
  78. package/deps/jq/jv_utf8_tables.h +37 -0
  79. package/deps/jq/lexer.c +2442 -0
  80. package/deps/jq/lexer.h +362 -0
  81. package/deps/jq/lexer.l +184 -0
  82. package/deps/jq/libm.h +160 -0
  83. package/deps/jq/linker.c +393 -0
  84. package/deps/jq/linker.h +7 -0
  85. package/deps/jq/locfile.c +91 -0
  86. package/deps/jq/locfile.h +29 -0
  87. package/deps/jq/m4/ax_compare_version.m4 +177 -0
  88. package/deps/jq/m4/ax_prog_bison_version.m4 +68 -0
  89. package/deps/jq/main.c +566 -0
  90. package/deps/jq/opcode_list.h +44 -0
  91. package/deps/jq/parser.c +3914 -0
  92. package/deps/jq/parser.h +193 -0
  93. package/deps/jq/parser.y +923 -0
  94. package/deps/jq/scripts/crosscompile +42 -0
  95. package/deps/jq/scripts/gen_utf8_tables.py +32 -0
  96. package/deps/jq/scripts/version +5 -0
  97. package/deps/jq/setup.sh +33 -0
  98. package/deps/jq/tests/jq.test +1235 -0
  99. package/deps/jq/tests/jqtest +5 -0
  100. package/deps/jq/tests/mantest +7 -0
  101. package/deps/jq/tests/modules/.jq +5 -0
  102. package/deps/jq/tests/modules/a.jq +2 -0
  103. package/deps/jq/tests/modules/b/b.jq +2 -0
  104. package/deps/jq/tests/modules/c/c.jq +16 -0
  105. package/deps/jq/tests/modules/c/d.jq +1 -0
  106. package/deps/jq/tests/modules/data.json +4 -0
  107. package/deps/jq/tests/modules/lib/jq/e/e.jq +1 -0
  108. package/deps/jq/tests/modules/lib/jq/f.jq +1 -0
  109. package/deps/jq/tests/modules/syntaxerror/syntaxerror.jq +1 -0
  110. package/deps/jq/tests/modules/test_bind_order.jq +4 -0
  111. package/deps/jq/tests/modules/test_bind_order0.jq +1 -0
  112. package/deps/jq/tests/modules/test_bind_order1.jq +2 -0
  113. package/deps/jq/tests/modules/test_bind_order2.jq +2 -0
  114. package/deps/jq/tests/onig.supp +21 -0
  115. package/deps/jq/tests/onig.test +85 -0
  116. package/deps/jq/tests/onigtest +5 -0
  117. package/deps/jq/tests/setup +36 -0
  118. package/deps/jq/tests/shtest +205 -0
  119. package/deps/jq/tests/torture/input0.json +7 -0
  120. package/deps/jq/util.c +462 -0
  121. package/deps/jq/util.h +64 -0
  122. package/deps/jq.gyp +35 -0
  123. package/index.d.ts +3 -0
  124. package/jest.config.js +10 -0
  125. package/lib/index.js +14 -0
  126. package/package.json +48 -0
  127. package/reports/jest-port-api.xml +35 -0
  128. package/src/binding.cc +177 -0
  129. package/src/binding.h +13 -0
  130. package/test/santiy.test.js +122 -0
  131. package/util/configure.js +27 -0
@@ -0,0 +1,68 @@
1
+ # ===========================================================================
2
+ # Modified from
3
+ # http://www.gnu.org/software/autoconf-archive/ax_prog_perl_version.html
4
+ # ===========================================================================
5
+ #
6
+ # SYNOPSIS
7
+ #
8
+ # AX_PROG_BISON_VERSION([VERSION],[ACTION-IF-TRUE],[ACTION-IF-FALSE])
9
+ #
10
+ # DESCRIPTION
11
+ #
12
+ # Makes sure that bison supports the version indicated. If true the shell
13
+ # commands in ACTION-IF-TRUE are executed. If not the shell commands in
14
+ # ACTION-IF-FALSE are run. Note if $PERL is not set the macro will fail.
15
+ #
16
+ # Example:
17
+ #
18
+ # AC_PROG_YACC
19
+ # AX_PROG_BISON_VERSION([3.0.0],[ ... ],[ ... ])
20
+ #
21
+ # This will check to make sure that the bison you have supports at least
22
+ # version 3.0.0.
23
+ #
24
+ # NOTE: This macro uses the $YACC variable to perform the check.
25
+ # AX_WITH_YACC can be used to set that variable prior to running this
26
+ # macro. The $BISON_VERSION variable will be set with the detected
27
+ # version.
28
+ #
29
+ # LICENSE
30
+ #
31
+ # Copyright (c) 2009 Francesco Salvestrini <salvestrini@users.sourceforge.net>
32
+ #
33
+ # Copying and distribution of this file, with or without modification, are
34
+ # permitted in any medium without royalty provided the copyright notice
35
+ # and this notice are preserved. This file is offered as-is, without any
36
+ # warranty.
37
+
38
+ #serial 12
39
+
40
+ AC_DEFUN([AX_PROG_BISON_VERSION],[
41
+ AC_REQUIRE([AC_PROG_SED])
42
+ AC_REQUIRE([AC_PROG_GREP])
43
+
44
+ AS_IF([test -n "$YACC"],[
45
+ ax_bison_version="$1"
46
+
47
+ AC_MSG_CHECKING([for bison version])
48
+ changequote(<<,>>)
49
+ bison_version=`$YACC --version 2>&1 \
50
+ | $GREP bison \
51
+ | $SED -e 's/^[^0-9]*//' -e 's/[^0-9]*$//'`
52
+ changequote([,])
53
+ AC_MSG_RESULT($bison_version)
54
+
55
+ AC_SUBST([BISON_VERSION],[$bison_version])
56
+
57
+ AX_COMPARE_VERSION([$ax_bison_version],[le],[$bison_version],[
58
+ :
59
+ $2
60
+ ],[
61
+ :
62
+ $3
63
+ ])
64
+ ],[
65
+ AC_MSG_WARN([could not find bison])
66
+ $3
67
+ ])
68
+ ])
package/deps/jq/main.c ADDED
@@ -0,0 +1,566 @@
1
+ #include <assert.h>
2
+ #include <ctype.h>
3
+ #include <errno.h>
4
+ #include <libgen.h>
5
+ #include <stdlib.h>
6
+ #include <stdio.h>
7
+ #include <string.h>
8
+ #include <unistd.h>
9
+
10
+ #ifdef WIN32
11
+ #include <windows.h>
12
+ #include <io.h>
13
+ #include <fcntl.h>
14
+ #include <processenv.h>
15
+ #include <shellapi.h>
16
+ #include <wchar.h>
17
+ #include <wtypes.h>
18
+ #endif
19
+
20
+ #include "compile.h"
21
+ #include "jv.h"
22
+ #include "jq.h"
23
+ #include "jv_alloc.h"
24
+ #include "util.h"
25
+ #include "version.h"
26
+
27
+ int jq_testsuite(jv lib_dirs, int verbose, int argc, char* argv[]);
28
+
29
+ static const char* progname;
30
+
31
+ /*
32
+ * For a longer help message we could use a better option parsing
33
+ * strategy, one that lets stack options.
34
+ */
35
+ static void usage(int code) {
36
+ FILE *f = stderr;
37
+
38
+ if (code == 0)
39
+ f = stdout;
40
+
41
+ int ret = fprintf(f,
42
+ "jq - commandline JSON processor [version %s]\n"
43
+ "Usage: %s [options] <jq filter> [file...]\n\n"
44
+ "\tjq is a tool for processing JSON inputs, applying the\n"
45
+ "\tgiven filter to its JSON text inputs and producing the\n"
46
+ "\tfilter's results as JSON on standard output.\n"
47
+ "\tThe simplest filter is ., which is the identity filter,\n"
48
+ "\tcopying jq's input to its output unmodified (except for\n"
49
+ "\tformatting).\n"
50
+ "\tFor more advanced filters see the jq(1) manpage (\"man jq\")\n"
51
+ "\tand/or https://stedolan.github.io/jq\n\n"
52
+ "\tSome of the options include:\n"
53
+ "\t -c\t\tcompact instead of pretty-printed output;\n"
54
+ "\t -n\t\tuse `null` as the single input value;\n"
55
+ "\t -e\t\tset the exit status code based on the output;\n"
56
+ "\t -s\t\tread (slurp) all inputs into an array; apply filter to it;\n"
57
+ "\t -r\t\toutput raw strings, not JSON texts;\n"
58
+ "\t -R\t\tread raw strings, not JSON texts;\n"
59
+ "\t -C\t\tcolorize JSON;\n"
60
+ "\t -M\t\tmonochrome (don't colorize JSON);\n"
61
+ "\t -S\t\tsort keys of objects on output;\n"
62
+ "\t --tab\tuse tabs for indentation;\n"
63
+ "\t --arg a v\tset variable $a to value <v>;\n"
64
+ "\t --argjson a v\tset variable $a to JSON value <v>;\n"
65
+ "\t --slurpfile a f\tset variable $a to an array of JSON texts read from <f>;\n"
66
+ "\tSee the manpage for more options.\n", JQ_VERSION, progname);
67
+ exit((ret < 0 && code == 0) ? 2 : code);
68
+ }
69
+
70
+ static void die() {
71
+ fprintf(stderr, "Use %s --help for help with command-line options,\n", progname);
72
+ fprintf(stderr, "or see the jq manpage, or online docs at https://stedolan.github.io/jq\n");
73
+ exit(2);
74
+ }
75
+
76
+
77
+
78
+
79
+ static int isoptish(const char* text) {
80
+ return text[0] == '-' && (text[1] == '-' || isalpha(text[1]));
81
+ }
82
+
83
+ static int isoption(const char* text, char shortopt, const char* longopt, size_t *short_opts) {
84
+ if (text[0] != '-' || text[1] == '-')
85
+ *short_opts = 0;
86
+ if (text[0] != '-') return 0;
87
+
88
+ // check long option
89
+ if (text[1] == '-' && !strcmp(text+2, longopt)) return 1;
90
+ else if (text[1] == '-') return 0;
91
+
92
+ // must be short option; check it and...
93
+ if (!shortopt) return 0;
94
+ if (strchr(text, shortopt) != NULL) {
95
+ (*short_opts)++; // ...count it (for option stacking)
96
+ return 1;
97
+ }
98
+ return 0;
99
+ }
100
+
101
+ enum {
102
+ SLURP = 1,
103
+ RAW_INPUT = 2,
104
+ PROVIDE_NULL = 4,
105
+ RAW_OUTPUT = 8,
106
+ ASCII_OUTPUT = 32,
107
+ COLOUR_OUTPUT = 64,
108
+ NO_COLOUR_OUTPUT = 128,
109
+ SORTED_OUTPUT = 256,
110
+ FROM_FILE = 512,
111
+ RAW_NO_LF = 1024,
112
+ UNBUFFERED_OUTPUT = 2048,
113
+ EXIT_STATUS = 4096,
114
+ SEQ = 8192,
115
+ RUN_TESTS = 16384,
116
+ /* debugging only */
117
+ DUMP_DISASM = 32768,
118
+ };
119
+ static int options = 0;
120
+
121
+ static int process(jq_state *jq, jv value, int flags, int dumpopts) {
122
+ int ret = 14; // No valid results && -e -> exit(4)
123
+ jq_start(jq, value, flags);
124
+ jv result;
125
+ while (jv_is_valid(result = jq_next(jq))) {
126
+ if ((options & RAW_OUTPUT) && jv_get_kind(result) == JV_KIND_STRING) {
127
+ fwrite(jv_string_value(result), 1, jv_string_length_bytes(jv_copy(result)), stdout);
128
+ ret = 0;
129
+ jv_free(result);
130
+ } else {
131
+ if (jv_get_kind(result) == JV_KIND_FALSE || jv_get_kind(result) == JV_KIND_NULL)
132
+ ret = 11;
133
+ else
134
+ ret = 0;
135
+ if (options & SEQ)
136
+ priv_fwrite("\036", 1, stdout, dumpopts & JV_PRINT_ISATTY);
137
+ jv_dump(result, dumpopts);
138
+ }
139
+ if (!(options & RAW_NO_LF))
140
+ priv_fwrite("\n", 1, stdout, dumpopts & JV_PRINT_ISATTY);
141
+ if (options & UNBUFFERED_OUTPUT)
142
+ fflush(stdout);
143
+ }
144
+ if (jv_invalid_has_msg(jv_copy(result))) {
145
+ // Uncaught jq exception
146
+ jv msg = jv_invalid_get_msg(jv_copy(result));
147
+ jv input_pos = jq_util_input_get_position(jq);
148
+ if (jv_get_kind(msg) == JV_KIND_STRING) {
149
+ fprintf(stderr, "jq: error (at %s): %s\n",
150
+ jv_string_value(input_pos), jv_string_value(msg));
151
+ } else {
152
+ msg = jv_dump_string(msg, 0);
153
+ fprintf(stderr, "jq: error (at %s) (not a string): %s\n",
154
+ jv_string_value(input_pos), jv_string_value(msg));
155
+ }
156
+ ret = 5;
157
+ jv_free(input_pos);
158
+ jv_free(msg);
159
+ }
160
+ jv_free(result);
161
+ return ret;
162
+ }
163
+
164
+ static void debug_cb(void *data, jv input) {
165
+ int dumpopts = *(int *)data;
166
+ jv_dumpf(JV_ARRAY(jv_string("DEBUG:"), input), stderr, dumpopts & ~(JV_PRINT_PRETTY));
167
+ fprintf(stderr, "\n");
168
+ }
169
+
170
+ int main(int argc, char* argv[]) {
171
+ jq_state *jq = NULL;
172
+ int ret = 0;
173
+ int compiled = 0;
174
+ int parser_flags = 0;
175
+ int nfiles = 0;
176
+ int badwrite;
177
+ jv program_arguments = jv_array();
178
+
179
+ #ifdef WIN32
180
+ SetConsoleOutputCP(CP_UTF8);
181
+ fflush(stdout);
182
+ fflush(stderr);
183
+ _setmode(fileno(stdout), _O_TEXT | _O_U8TEXT);
184
+ _setmode(fileno(stderr), _O_TEXT | _O_U8TEXT);
185
+ int wargc;
186
+ wchar_t **wargv = CommandLineToArgvW(GetCommandLineW(), &wargc);
187
+ assert(wargc == argc);
188
+ size_t arg_sz;
189
+ for (int i = 0; i < argc; i++) {
190
+ argv[i] = alloca((arg_sz = WideCharToMultiByte(CP_UTF8,
191
+ 0,
192
+ wargv[i],
193
+ -1, 0, 0, 0, 0)));
194
+ WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, argv[i], arg_sz, 0, 0);
195
+ }
196
+ #endif
197
+
198
+ if (argc) progname = argv[0];
199
+
200
+ jq = jq_init();
201
+ if (jq == NULL) {
202
+ perror("malloc");
203
+ ret = 2;
204
+ goto out;
205
+ }
206
+
207
+ int dumpopts = JV_PRINT_INDENT_FLAGS(2);
208
+ const char* program = 0;
209
+
210
+ jq_util_input_state *input_state = jq_util_input_init(NULL, NULL); // XXX add err_cb
211
+
212
+ int further_args_are_files = 0;
213
+ int jq_flags = 0;
214
+ size_t short_opts = 0;
215
+ jv lib_search_paths = jv_null();
216
+ for (int i=1; i<argc; i++, short_opts = 0) {
217
+ if (further_args_are_files) {
218
+ jq_util_input_add_input(input_state, argv[i]);
219
+ nfiles++;
220
+ } else if (!strcmp(argv[i], "--")) {
221
+ if (!program) usage(2);
222
+ further_args_are_files = 1;
223
+ } else if (!isoptish(argv[i])) {
224
+ if (program) {
225
+ jq_util_input_add_input(input_state, argv[i]);
226
+ nfiles++;
227
+ } else {
228
+ program = argv[i];
229
+ }
230
+ } else {
231
+ if (argv[i][1] == 'L') {
232
+ if (jv_get_kind(lib_search_paths) == JV_KIND_NULL)
233
+ lib_search_paths = jv_array();
234
+ if (argv[i][2] != 0) { // -Lname (faster check than strlen)
235
+ lib_search_paths = jv_array_append(lib_search_paths, jq_realpath(jv_string(argv[i]+2)));
236
+ } else if (i >= argc - 1) {
237
+ fprintf(stderr, "-L takes a parameter: (e.g. -L /search/path or -L/search/path)\n");
238
+ die();
239
+ } else {
240
+ lib_search_paths = jv_array_append(lib_search_paths, jq_realpath(jv_string(argv[i+1])));
241
+ i++;
242
+ }
243
+ continue;
244
+ }
245
+
246
+ if (isoption(argv[i], 's', "slurp", &short_opts)) {
247
+ options |= SLURP;
248
+ if (!short_opts) continue;
249
+ }
250
+ if (isoption(argv[i], 'r', "raw-output", &short_opts)) {
251
+ options |= RAW_OUTPUT;
252
+ if (!short_opts) continue;
253
+ }
254
+ if (isoption(argv[i], 'c', "compact-output", &short_opts)) {
255
+ dumpopts &= ~(JV_PRINT_TAB | JV_PRINT_INDENT_FLAGS(7));
256
+ if (!short_opts) continue;
257
+ }
258
+ if (isoption(argv[i], 'C', "color-output", &short_opts)) {
259
+ options |= COLOUR_OUTPUT;
260
+ if (!short_opts) continue;
261
+ }
262
+ if (isoption(argv[i], 'M', "monochrome-output", &short_opts)) {
263
+ options |= NO_COLOUR_OUTPUT;
264
+ if (!short_opts) continue;
265
+ }
266
+ if (isoption(argv[i], 'a', "ascii-output", &short_opts)) {
267
+ options |= ASCII_OUTPUT;
268
+ if (!short_opts) continue;
269
+ }
270
+ if (isoption(argv[i], 0, "unbuffered", &short_opts)) {
271
+ options |= UNBUFFERED_OUTPUT;
272
+ if (!short_opts) continue;
273
+ }
274
+ if (isoption(argv[i], 'S', "sort-keys", &short_opts)) {
275
+ options |= SORTED_OUTPUT;
276
+ if (!short_opts) continue;
277
+ }
278
+ if (isoption(argv[i], 'R', "raw-input", &short_opts)) {
279
+ options |= RAW_INPUT;
280
+ if (!short_opts) continue;
281
+ }
282
+ if (isoption(argv[i], 'n', "null-input", &short_opts)) {
283
+ options |= PROVIDE_NULL;
284
+ if (!short_opts) continue;
285
+ }
286
+ if (isoption(argv[i], 'f', "from-file", &short_opts)) {
287
+ options |= FROM_FILE;
288
+ if (!short_opts) continue;
289
+ }
290
+ if (isoption(argv[i], 'j', "join-output", &short_opts)) {
291
+ options |= RAW_OUTPUT | RAW_NO_LF;
292
+ if (!short_opts) continue;
293
+ }
294
+ if (isoption(argv[i], 0, "tab", &short_opts)) {
295
+ dumpopts &= ~JV_PRINT_INDENT_FLAGS(7);
296
+ dumpopts |= JV_PRINT_TAB | JV_PRINT_PRETTY;
297
+ if (!short_opts) continue;
298
+ }
299
+ if (isoption(argv[i], 0, "indent", &short_opts)) {
300
+ if (i >= argc - 1) {
301
+ fprintf(stderr, "%s: --indent takes one parameter\n", progname);
302
+ die();
303
+ }
304
+ dumpopts &= ~(JV_PRINT_TAB | JV_PRINT_INDENT_FLAGS(7));
305
+ int indent = atoi(argv[i+1]);
306
+ if (indent < -1 || indent > 7) {
307
+ fprintf(stderr, "%s: --indent takes a number between -1 and 7\n", progname);
308
+ die();
309
+ }
310
+ dumpopts |= JV_PRINT_INDENT_FLAGS(indent);
311
+ i++;
312
+ if (!short_opts) continue;
313
+ }
314
+ if (isoption(argv[i], 0, "seq", &short_opts)) {
315
+ options |= SEQ;
316
+ if (!short_opts) continue;
317
+ }
318
+ if (isoption(argv[i], 0, "stream", &short_opts)) {
319
+ parser_flags |= JV_PARSE_STREAMING;
320
+ if (!short_opts) continue;
321
+ }
322
+ if (isoption(argv[i], 0, "stream-errors", &short_opts)) {
323
+ parser_flags |= JV_PARSE_STREAM_ERRORS;
324
+ if (!short_opts) continue;
325
+ }
326
+ if (isoption(argv[i], 'e', "exit-status", &short_opts)) {
327
+ options |= EXIT_STATUS;
328
+ if (!short_opts) continue;
329
+ }
330
+ // FIXME: For --arg* we should check that the varname is acceptable
331
+ if (isoption(argv[i], 0, "arg", &short_opts)) {
332
+ if (i >= argc - 2) {
333
+ fprintf(stderr, "%s: --arg takes two parameters (e.g. --arg varname value)\n", progname);
334
+ die();
335
+ }
336
+ jv arg = jv_object();
337
+ arg = jv_object_set(arg, jv_string("name"), jv_string(argv[i+1]));
338
+ arg = jv_object_set(arg, jv_string("value"), jv_string(argv[i+2]));
339
+ program_arguments = jv_array_append(program_arguments, arg);
340
+ i += 2; // skip the next two arguments
341
+ if (!short_opts) continue;
342
+ }
343
+ if (isoption(argv[i], 0, "argjson", &short_opts)) {
344
+ if (i >= argc - 2) {
345
+ fprintf(stderr, "%s: --argjson takes two parameters (e.g. --argjson varname text)\n", progname);
346
+ die();
347
+ }
348
+ jv v = jv_parse(argv[i+2]);
349
+ if (!jv_is_valid(v)) {
350
+ fprintf(stderr, "%s: invalid JSON text passed to --argjson\n", progname);
351
+ die();
352
+ }
353
+ jv arg = jv_object();
354
+ arg = jv_object_set(arg, jv_string("name"), jv_string(argv[i+1]));
355
+ arg = jv_object_set(arg, jv_string("value"), v);
356
+ program_arguments = jv_array_append(program_arguments, arg);
357
+ i += 2; // skip the next two arguments
358
+ if (!short_opts) continue;
359
+ }
360
+ if (isoption(argv[i], 0, "argfile", &short_opts) ||
361
+ isoption(argv[i], 0, "slurpfile", &short_opts)) {
362
+ const char *which;
363
+ if (isoption(argv[i], 0, "argfile", &short_opts))
364
+ which = "argfile";
365
+ else
366
+ which = "slurpfile";
367
+ if (i >= argc - 2) {
368
+ fprintf(stderr, "%s: --%s takes two parameters (e.g. --%s varname filename)\n", progname, which, which);
369
+ die();
370
+ }
371
+ jv arg = jv_object();
372
+ arg = jv_object_set(arg, jv_string("name"), jv_string(argv[i+1]));
373
+ jv data = jv_load_file(argv[i+2], 0);
374
+ if (!jv_is_valid(data)) {
375
+ data = jv_invalid_get_msg(data);
376
+ fprintf(stderr, "%s: Bad JSON in --%s %s %s: %s\n", progname, which,
377
+ argv[i+1], argv[i+2], jv_string_value(data));
378
+ jv_free(data);
379
+ jv_free(arg);
380
+ ret = 2;
381
+ goto out;
382
+ }
383
+ if (isoption(argv[i], 0, "argfile", &short_opts) &&
384
+ jv_get_kind(data) == JV_KIND_ARRAY && jv_array_length(jv_copy(data)) == 1)
385
+ data = jv_array_get(data, 0);
386
+ arg = jv_object_set(arg, jv_string("value"), data);
387
+ program_arguments = jv_array_append(program_arguments, arg);
388
+ i += 2; // skip the next two arguments
389
+ if (!short_opts) continue;
390
+ }
391
+ if (isoption(argv[i], 0, "debug-dump-disasm", &short_opts)) {
392
+ options |= DUMP_DISASM;
393
+ if (!short_opts) continue;
394
+ }
395
+ if (isoption(argv[i], 0, "debug-trace", &short_opts)) {
396
+ jq_flags |= JQ_DEBUG_TRACE;
397
+ if (!short_opts) continue;
398
+ }
399
+ if (isoption(argv[i], 'h', "help", &short_opts)) {
400
+ usage(0);
401
+ if (!short_opts) continue;
402
+ }
403
+ if (isoption(argv[i], 'V', "version", &short_opts)) {
404
+ printf("jq-%s\n", JQ_VERSION);
405
+ ret = 0;
406
+ goto out;
407
+ }
408
+ if (isoption(argv[i], 0, "run-tests", &short_opts)) {
409
+ i++;
410
+ // XXX Pass program_arguments, even a whole jq_state *, through;
411
+ // could be useful for testing
412
+ ret = jq_testsuite(lib_search_paths,
413
+ (options & DUMP_DISASM) || (jq_flags & JQ_DEBUG_TRACE),
414
+ argc - i, argv + i);
415
+ goto out;
416
+ }
417
+
418
+ // check for unknown options... if this argument was a short option
419
+ if (strlen(argv[i]) != short_opts + 1) {
420
+ fprintf(stderr, "%s: Unknown option %s\n", progname, argv[i]);
421
+ die();
422
+ }
423
+ }
424
+ }
425
+
426
+ if (isatty(fileno(stdout))) {
427
+ dumpopts |= JV_PRINT_ISATTY;
428
+ #ifndef WIN32
429
+ /* Disable colour by default on Windows builds as Windows
430
+ terminals tend not to display it correctly */
431
+ dumpopts |= JV_PRINT_COLOUR;
432
+ #endif
433
+ }
434
+ if (options & SORTED_OUTPUT) dumpopts |= JV_PRINT_SORTED;
435
+ if (options & ASCII_OUTPUT) dumpopts |= JV_PRINT_ASCII;
436
+ if (options & COLOUR_OUTPUT) dumpopts |= JV_PRINT_COLOUR;
437
+ if (options & NO_COLOUR_OUTPUT) dumpopts &= ~JV_PRINT_COLOUR;
438
+
439
+ if (jv_get_kind(lib_search_paths) == JV_KIND_NULL) {
440
+ // Default search path list
441
+ lib_search_paths = JV_ARRAY(jv_string("~/.jq"),
442
+ jv_string("$ORIGIN/../lib/jq"),
443
+ jv_string("$ORIGIN/lib"));
444
+ }
445
+ jq_set_attr(jq, jv_string("JQ_LIBRARY_PATH"), lib_search_paths);
446
+
447
+ char *origin = strdup(argv[0]);
448
+ if (origin == NULL) {
449
+ fprintf(stderr, "Error: out of memory\n");
450
+ exit(1);
451
+ }
452
+ jq_set_attr(jq, jv_string("JQ_ORIGIN"), jv_string(dirname(origin)));
453
+ free(origin);
454
+
455
+ if (strchr(JQ_VERSION, '-') == NULL)
456
+ jq_set_attr(jq, jv_string("VERSION_DIR"), jv_string(JQ_VERSION));
457
+ else
458
+ jq_set_attr(jq, jv_string("VERSION_DIR"), jv_string_fmt("%.*s-master", (int)(strchr(JQ_VERSION, '-') - JQ_VERSION), JQ_VERSION));
459
+
460
+ #if (!defined(WIN32) && defined(HAVE_ISATTY)) || defined(HAVE__ISATTY)
461
+
462
+ #if defined(HAVE__ISATTY) && defined(isatty)
463
+ #undef isatty
464
+ #define isatty _isatty
465
+ #endif
466
+
467
+ if (!program && isatty(STDOUT_FILENO) && !isatty(STDIN_FILENO))
468
+ program = ".";
469
+ #endif
470
+
471
+ if (!program) usage(2);
472
+
473
+ if (options & FROM_FILE) {
474
+ char *program_origin = strdup(program);
475
+ if (program_origin == NULL) {
476
+ perror("malloc");
477
+ exit(2);
478
+ }
479
+
480
+ jv data = jv_load_file(program, 1);
481
+ if (!jv_is_valid(data)) {
482
+ data = jv_invalid_get_msg(data);
483
+ fprintf(stderr, "%s: %s\n", progname, jv_string_value(data));
484
+ jv_free(data);
485
+ ret = 2;
486
+ goto out;
487
+ }
488
+ jq_set_attr(jq, jv_string("PROGRAM_ORIGIN"), jq_realpath(jv_string(dirname(program_origin))));
489
+ compiled = jq_compile_args(jq, jv_string_value(data), jv_copy(program_arguments));
490
+ free(program_origin);
491
+ jv_free(data);
492
+ } else {
493
+ jq_set_attr(jq, jv_string("PROGRAM_ORIGIN"), jq_realpath(jv_string("."))); // XXX is this good?
494
+ compiled = jq_compile_args(jq, program, jv_copy(program_arguments));
495
+ }
496
+ if (!compiled){
497
+ ret = 3;
498
+ goto out;
499
+ }
500
+
501
+ if (options & DUMP_DISASM) {
502
+ jq_dump_disassembly(jq, 0);
503
+ printf("\n");
504
+ }
505
+
506
+ if ((options & SEQ))
507
+ parser_flags |= JV_PARSE_SEQ;
508
+
509
+ if ((options & RAW_INPUT))
510
+ jq_util_input_set_parser(input_state, NULL, (options & SLURP) ? 1 : 0);
511
+ else
512
+ jq_util_input_set_parser(input_state, jv_parser_new(parser_flags), (options & SLURP) ? 1 : 0);
513
+
514
+ // Let jq program read from inputs
515
+ jq_set_input_cb(jq, jq_util_input_next_input_cb, input_state);
516
+
517
+ // Let jq program call `debug` builtin and have that go somewhere
518
+ jq_set_debug_cb(jq, debug_cb, &dumpopts);
519
+
520
+ if (nfiles == 0)
521
+ jq_util_input_add_input(input_state, "-");
522
+
523
+ if (options & PROVIDE_NULL) {
524
+ ret = process(jq, jv_null(), jq_flags, dumpopts);
525
+ } else {
526
+ jv value;
527
+ while (jq_util_input_errors(input_state) == 0 &&
528
+ (jv_is_valid((value = jq_util_input_next_input(input_state))) || jv_invalid_has_msg(jv_copy(value)))) {
529
+ if (jv_is_valid(value)) {
530
+ ret = process(jq, value, jq_flags, dumpopts);
531
+ continue;
532
+ }
533
+
534
+ // Parse error
535
+ jv msg = jv_invalid_get_msg(value);
536
+ if (!(options & SEQ)) {
537
+ // --seq -> errors are not fatal
538
+ ret = 4;
539
+ fprintf(stderr, "parse error: %s\n", jv_string_value(msg));
540
+ jv_free(msg);
541
+ break;
542
+ }
543
+ fprintf(stderr, "ignoring parse error: %s\n", jv_string_value(msg));
544
+ jv_free(msg);
545
+ }
546
+ }
547
+
548
+ if (jq_util_input_errors(input_state) != 0)
549
+ ret = 2;
550
+
551
+ out:
552
+ badwrite = ferror(stdout);
553
+ if (fclose(stdout)!=0 || badwrite) {
554
+ fprintf(stderr,"Error: writing output failed: %s\n", strerror(errno));
555
+ ret = 2;
556
+ }
557
+
558
+ jv_free(program_arguments);
559
+ jq_util_input_free(&input_state);
560
+ jq_teardown(&jq);
561
+ if (ret >= 10 && (options & EXIT_STATUS))
562
+ return ret - 10;
563
+ if (ret >= 10)
564
+ return 0;
565
+ return ret;
566
+ }
@@ -0,0 +1,44 @@
1
+ OP(LOADK, CONSTANT, 1, 1)
2
+ OP(DUP, NONE, 1, 2)
3
+ OP(DUPN, NONE, 1, 2)
4
+ OP(DUP2, NONE, 2, 3)
5
+ OP(POP, NONE, 1, 0)
6
+ OP(LOADV, VARIABLE, 1, 1)
7
+ OP(LOADVN, VARIABLE, 1, 1)
8
+ OP(STOREV, VARIABLE, 1, 0)
9
+ OP(STORE_GLOBAL, GLOBAL, 0, 0)
10
+ OP(INDEX, NONE, 2, 1)
11
+ OP(INDEX_OPT, NONE, 2, 1)
12
+ OP(EACH, NONE, 1, 1)
13
+ OP(EACH_OPT, NONE, 1, 1)
14
+ OP(FORK, BRANCH, 0, 0)
15
+ OP(FORK_OPT, BRANCH, 0, 0)
16
+ OP(JUMP, BRANCH, 0, 0)
17
+ OP(JUMP_F,BRANCH, 1, 0)
18
+ OP(BACKTRACK, NONE, 0, 0)
19
+ OP(APPEND, VARIABLE,1, 0)
20
+ OP(INSERT, NONE, 4, 2)
21
+ OP(RANGE, VARIABLE, 1, 1)
22
+
23
+ OP(SUBEXP_BEGIN, NONE, 1, 2)
24
+ OP(SUBEXP_END, NONE, 2, 2)
25
+
26
+ OP(PATH_BEGIN, NONE, 1, 2)
27
+ OP(PATH_END, NONE, 2, 1)
28
+
29
+ OP(CALL_BUILTIN, CFUNC, -1, 1)
30
+
31
+ OP(CALL_JQ, UFUNC, 1, 1)
32
+ OP(RET, NONE, 1, 1)
33
+ OP(TAIL_CALL_JQ, UFUNC, 1, 1)
34
+
35
+ OP(CLOSURE_PARAM, DEFINITION, 0, 0)
36
+ OP(CLOSURE_REF, CLOSURE_REF_IMM, 0, 0)
37
+ OP(CLOSURE_CREATE, DEFINITION, 0, 0)
38
+ OP(CLOSURE_CREATE_C, DEFINITION, 0, 0)
39
+
40
+ OP(TOP, NONE, 0, 0)
41
+ OP(CLOSURE_PARAM_REGULAR, DEFINITION, 0, 0)
42
+ OP(DEPS, CONSTANT, 0, 0)
43
+ OP(MODULEMETA, CONSTANT, 0, 0)
44
+ OP(GENLABEL, NONE, 0, 1)