From 4c5a08b9e01ebfce5c8914dd82c1722737bbecab Mon Sep 17 00:00:00 2001 From: Muh Muhten Date: Tue, 19 Feb 2019 00:39:34 -0500 Subject: [PATCH 8/9] Load ~/.jq as a library instead of with builtins Remove the special code which loads ~/.jq in builtin.c, and instead glue an optional include which points to the same file onto the main program in linker.c. Fixes a minor bug where errors in ~/.jq would be labelled . --- src/builtin.c | 44 +++++++------------------------------------- src/linker.c | 10 ++++++++++ 2 files changed, 17 insertions(+), 37 deletions(-) --- a/src/builtin.c +++ b/src/builtin.c @@ -1706,9 +1706,7 @@ static block bind_bytecoded_builtins(blo return block_bind(builtins, b, OP_IS_CALL_PSEUDO); } - - -static const char* const jq_builtins = +static const char jq_builtins[] = /* Include jq-coded builtins */ #include "src/builtin.inc" @@ -1744,45 +1742,17 @@ static block gen_builtin_list(block buil return BLOCK(builtins, gen_function("builtins", gen_noop(), gen_const(list))); } -static int builtins_bind_one(jq_state *jq, block* bb, const char* code) { - struct locfile* src; - src = locfile_init(jq, "", code, strlen(code)); - block funcs; - int nerrors = jq_parse_library(src, &funcs); - if (nerrors == 0) { - *bb = block_bind(funcs, *bb, OP_IS_CALL_PSEUDO); - } - locfile_free(src); - return nerrors; -} - -static int slurp_lib(jq_state *jq, 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 = jv_load_file(jv_string_value(filename), 1); - if (jv_is_valid(data)) { - nerrors = builtins_bind_one(jq, bb, jv_string_value(data) ); - } - jv_free(filename); - jv_free(data); - } - return nerrors; -} - int builtins_bind(jq_state *jq, block* bb) { - block builtins = gen_noop(); - int nerrors = slurp_lib(jq, bb); - if (nerrors) { - block_free(*bb); - return nerrors; - } - nerrors = builtins_bind_one(jq, &builtins, jq_builtins); + block builtins; + struct locfile* src = locfile_init(jq, "", jq_builtins, sizeof(jq_builtins)-1); + int nerrors = jq_parse_library(src, &builtins); assert(!nerrors); + locfile_free(src); + builtins = bind_bytecoded_builtins(builtins); builtins = gen_cbinding(function_list, sizeof(function_list)/sizeof(function_list[0]), builtins); builtins = gen_builtin_list(builtins); + *bb = block_bind(builtins, *bb, OP_IS_CALL_PSEUDO); *bb = block_drop_unreferenced(*bb); return nerrors; --- a/src/linker.c +++ b/src/linker.c @@ -387,6 +387,16 @@ int load_program(jq_state *jq, struct lo if (nerrors) return nerrors; + char* home = getenv("HOME"); + if (home) { // silently ignore no $HOME + /* Import ~/.jq as a library named "" found in $HOME */ + block import = gen_import_meta(gen_import("", NULL, 0), + gen_const(JV_OBJECT( + jv_string("optional"), jv_true(), + jv_string("search"), jv_string(home)))); + program = BLOCK(import, program); + } + nerrors = process_dependencies(jq, jq_get_jq_origin(jq), jq_get_prog_origin(jq), &program, &lib_state); block libs = gen_noop(); for (uint64_t i = 0; i < lib_state.ct; ++i) {