mirror of
https://github.com/stedolan/jq.git
synced 2024-05-11 05:55:39 +00:00
Load library from ~/.jq
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,5 +1,7 @@
|
|||||||
*.o
|
*.o
|
||||||
*~
|
*~
|
||||||
|
.*.sw[a-p]
|
||||||
|
tags
|
||||||
|
|
||||||
jq
|
jq
|
||||||
jq.1
|
jq.1
|
||||||
|
52
builtin.c
52
builtin.c
@@ -1,3 +1,4 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "builtin.h"
|
#include "builtin.h"
|
||||||
#include "compile.h"
|
#include "compile.h"
|
||||||
@@ -5,6 +6,7 @@
|
|||||||
#include "locfile.h"
|
#include "locfile.h"
|
||||||
#include "jv_aux.h"
|
#include "jv_aux.h"
|
||||||
#include "jv_unicode.h"
|
#include "jv_unicode.h"
|
||||||
|
#include "main.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -566,16 +568,44 @@ static const char* const jq_builtins[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
block builtins_bind(block b) {
|
int builtins_bind_one(block* bb, const char* code) {
|
||||||
for (int i=(int)(sizeof(jq_builtins)/sizeof(jq_builtins[0]))-1; i>=0; i--) {
|
struct locfile src;
|
||||||
struct locfile src;
|
locfile_init(&src, code, strlen(code));
|
||||||
locfile_init(&src, jq_builtins[i], strlen(jq_builtins[i]));
|
block funcs;
|
||||||
block funcs;
|
int nerrors = jq_parse_library(&src, &funcs);
|
||||||
int nerrors = jq_parse_library(&src, &funcs);
|
if (nerrors == 0) {
|
||||||
assert(!nerrors);
|
*bb = block_bind_referenced(funcs, *bb, OP_IS_CALL_PSEUDO);
|
||||||
b = block_bind_referenced(funcs, b, OP_IS_CALL_PSEUDO);
|
|
||||||
locfile_free(&src);
|
|
||||||
}
|
}
|
||||||
b = bind_bytecoded_builtins(b);
|
locfile_free(&src);
|
||||||
return gen_cbinding(function_list, sizeof(function_list)/sizeof(function_list[0]), b);
|
return nerrors;
|
||||||
|
}
|
||||||
|
|
||||||
|
int slurp_lib(block* bb) {
|
||||||
|
int nerrors = 0;
|
||||||
|
char* home = getenv("HOME");
|
||||||
|
if (home) { // silently ignore no $HOME
|
||||||
|
jv filename = jv_string_append_str(jv_string(home), "/.jq");
|
||||||
|
jv data = slurp_file(jv_string_value(filename), 1);
|
||||||
|
if (jv_is_valid(data)) {
|
||||||
|
nerrors = builtins_bind_one(bb, jv_string_value(data) );
|
||||||
|
}
|
||||||
|
jv_free(filename);
|
||||||
|
jv_free(data);
|
||||||
|
}
|
||||||
|
return nerrors;
|
||||||
|
}
|
||||||
|
|
||||||
|
int builtins_bind(block* bb) {
|
||||||
|
int nerrors = slurp_lib(bb);
|
||||||
|
if (nerrors) {
|
||||||
|
block_free(*bb);
|
||||||
|
return nerrors;
|
||||||
|
}
|
||||||
|
for (int i=(int)(sizeof(jq_builtins)/sizeof(jq_builtins[0]))-1; i>=0; i--) {
|
||||||
|
nerrors = builtins_bind_one(bb, jq_builtins[i]);
|
||||||
|
assert(!nerrors);
|
||||||
|
}
|
||||||
|
*bb = bind_bytecoded_builtins(*bb);
|
||||||
|
*bb = gen_cbinding(function_list, sizeof(function_list)/sizeof(function_list[0]), *bb);
|
||||||
|
return nerrors;
|
||||||
}
|
}
|
||||||
|
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#include "compile.h"
|
#include "compile.h"
|
||||||
|
|
||||||
block builtins_bind(block);
|
int builtins_bind(block*);
|
||||||
|
|
||||||
|
|
||||||
typedef void (*cfunction_ptr)(void);
|
typedef void (*cfunction_ptr)(void);
|
||||||
|
@@ -611,8 +611,10 @@ struct bytecode* jq_compile_args(const char* str, jv args) {
|
|||||||
jv_free(name);
|
jv_free(name);
|
||||||
}
|
}
|
||||||
jv_free(args);
|
jv_free(args);
|
||||||
program = builtins_bind(program);
|
nerrors = builtins_bind(&program);
|
||||||
nerrors = block_compile(program, &locations, &bc);
|
if (nerrors == 0) {
|
||||||
|
nerrors = block_compile(program, &locations, &bc);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (nerrors) {
|
if (nerrors) {
|
||||||
fprintf(stderr, "%d compile %s\n", nerrors, nerrors > 1 ? "errors" : "error");
|
fprintf(stderr, "%d compile %s\n", nerrors, nerrors > 1 ? "errors" : "error");
|
||||||
|
2
main.c
2
main.c
@@ -93,7 +93,7 @@ static void process(jv value, int flags) {
|
|||||||
jq_teardown(&jq);
|
jq_teardown(&jq);
|
||||||
}
|
}
|
||||||
|
|
||||||
static jv slurp_file(const char* filename, int raw) {
|
jv slurp_file(const char* filename, int raw) {
|
||||||
FILE* file = fopen(filename, "r");
|
FILE* file = fopen(filename, "r");
|
||||||
struct jv_parser parser;
|
struct jv_parser parser;
|
||||||
jv data;
|
jv data;
|
||||||
|
Reference in New Issue
Block a user