2012-08-16 01:00:30 +01:00
|
|
|
#include <stdio.h>
|
2012-09-11 12:24:54 +01:00
|
|
|
#include <string.h>
|
2012-08-16 01:00:30 +01:00
|
|
|
#include "compile.h"
|
|
|
|
#include "builtin.h"
|
2012-09-02 16:31:59 +01:00
|
|
|
#include "jv.h"
|
2012-09-11 14:52:10 +01:00
|
|
|
#include "jv_parse.h"
|
2012-09-11 12:24:54 +01:00
|
|
|
#include "locfile.h"
|
2012-09-18 10:17:38 +01:00
|
|
|
#include "parser.h"
|
2012-09-18 22:17:13 +01:00
|
|
|
#include "execute.h"
|
2012-08-16 01:00:30 +01:00
|
|
|
|
2012-09-18 23:37:46 +01:00
|
|
|
static const char* progname;
|
|
|
|
|
|
|
|
static void usage() {
|
|
|
|
fprintf(stderr, "\njq - commandline JSON processor\n");
|
|
|
|
fprintf(stderr, "Usage: %s <jq filter>\n\n", progname);
|
|
|
|
fprintf(stderr, "For a description of how to write jq filters and\n");
|
|
|
|
fprintf(stderr, "why you might want to, see the jq documentation at\n");
|
|
|
|
fprintf(stderr, "http://stedolan.github.com/jq\n\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-08-16 01:00:30 +01:00
|
|
|
int main(int argc, char* argv[]) {
|
2012-09-18 23:37:46 +01:00
|
|
|
if (argc) progname = argv[0];
|
|
|
|
if (argc != 2 || !strcmp(argv[1], "--help") || !strcmp(argv[1], "--version")) usage();
|
2012-09-11 12:24:54 +01:00
|
|
|
struct bytecode* bc = jq_compile(argv[1]);
|
|
|
|
if (!bc) return 1;
|
2012-09-11 14:52:10 +01:00
|
|
|
|
|
|
|
#if JQ_DEBUG
|
|
|
|
dump_disassembly(0, bc);
|
|
|
|
printf("\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct jv_parser parser;
|
|
|
|
jv_parser_init(&parser);
|
|
|
|
while (!feof(stdin)) {
|
|
|
|
char buf[4096];
|
|
|
|
if (!fgets(buf, sizeof(buf), stdin)) buf[0] = 0;
|
|
|
|
jv_parser_set_buf(&parser, buf, strlen(buf), !feof(stdin));
|
|
|
|
jv value;
|
|
|
|
while (jv_is_valid((value = jv_parser_next(&parser)))) {
|
|
|
|
jq_init(bc, value);
|
|
|
|
jv result;
|
|
|
|
while (jv_is_valid(result = jq_next())) {
|
2012-09-11 15:51:12 +01:00
|
|
|
jv_dump(result, JV_PRINT_PRETTY);
|
2012-09-11 14:52:10 +01:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
jv_free(result);
|
|
|
|
jq_teardown();
|
|
|
|
}
|
|
|
|
if (jv_invalid_has_msg(jv_copy(value))) {
|
|
|
|
jv msg = jv_invalid_get_msg(value);
|
|
|
|
fprintf(stderr, "parse error: %s\n", jv_string_value(msg));
|
|
|
|
jv_free(msg);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
jv_free(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
jv_parser_free(&parser);
|
|
|
|
|
2012-09-02 22:08:36 +01:00
|
|
|
bytecode_free(bc);
|
2012-09-11 00:04:47 +01:00
|
|
|
return 0;
|
2012-08-16 01:00:30 +01:00
|
|
|
}
|