@@ -48,7 +48,7 @@ static int path_is_relative(jv p) {
4848// in the following order:
4949// 1. lib_path
5050// 2. -L paths passed in on the command line (from jq_state*) or builtin list
51- static jv build_lib_search_chain (jq_state * jq , jv search_path , jv xdg_config_home , jv jq_origin , jv lib_origin ) {
51+ static jv build_lib_search_chain (jq_state * jq , jv search_path , jv config_home , jv jq_origin , jv lib_origin ) {
5252 assert (jv_get_kind (search_path ) == JV_KIND_ARRAY );
5353 jv expanded = jv_array ();
5454 jv expanded_elt ;
@@ -66,14 +66,15 @@ static jv build_lib_search_chain(jq_state *jq, jv search_path, jv xdg_config_hom
6666 }
6767 if (strcmp ("." ,jv_string_value (path )) == 0 ) {
6868 expanded_elt = jv_copy (path );
69- } else if (strncmp ("$XDG_CONFIG_HOME/" ,jv_string_value (path ),sizeof ("$XDG_CONFIG_HOME/" ) - 1 ) == 0 ) {
70- if (jv_is_valid (xdg_config_home )) {
69+ } else if (strcmp ("$JQ_CONFIG_HOME" ,jv_string_value (path )) == 0 ) {
70+ expanded_elt = jv_copy (config_home );
71+ } else if (strncmp ("$JQ_CONFIG_HOME/" ,jv_string_value (path ),sizeof ("$JQ_CONFIG_HOME/" ) - 1 ) == 0 ) {
72+ if (jv_is_valid (config_home )) {
7173 expanded_elt = jv_string_fmt ("%s/%s" ,
72- jv_string_value (xdg_config_home ),
73- jv_string_value (path ) + sizeof ("$XDG_CONFIG_HOME /" ) - 1 );
74+ jv_string_value (config_home ),
75+ jv_string_value (path ) + sizeof ("$JQ_CONFIG_HOME /" ) - 1 );
7476 } else {
75- // Remove $XDG_CONFIG_HOME/* from the search path if $XDG_CONFIG_HOME is not defined.
76- expanded_elt = jv_null ();
77+ expanded_elt = jv_invalid ();
7778 }
7879 } else if (strncmp ("$ORIGIN/" ,jv_string_value (path ),sizeof ("$ORIGIN/" ) - 1 ) == 0 ) {
7980 expanded_elt = jv_string_fmt ("%s/%s" ,
@@ -88,10 +89,14 @@ static jv build_lib_search_chain(jq_state *jq, jv search_path, jv xdg_config_hom
8889 expanded_elt = path ;
8990 path = jv_invalid ();
9091 }
91- expanded = jv_array_append (expanded , expanded_elt );
92+ if (jv_is_valid (expanded_elt )) {
93+ expanded = jv_array_append (expanded , expanded_elt );
94+ } else {
95+ jv_free (expanded_elt );
96+ }
9297 jv_free (path );
9398 }
94- jv_free (xdg_config_home );
99+ jv_free (config_home );
95100 jv_free (jq_origin );
96101 jv_free (lib_origin );
97102 jv_free (search_path );
@@ -143,26 +148,26 @@ static jv jv_basename(jv name) {
143148}
144149
145150// Asummes validated relative path to module
146- static jv find_lib (jq_state * jq , jv rel_path , jv search , const char * suffix , jv xdg_config_home , jv jq_origin , jv lib_origin ) {
151+ static jv find_lib (jq_state * jq , jv rel_path , jv search , const char * suffix , jv config_home , jv jq_origin , jv lib_origin ) {
147152 if (!jv_is_valid (rel_path )) {
148153 jv_free (search );
149- jv_free (xdg_config_home );
154+ jv_free (config_home );
150155 jv_free (jq_origin );
151156 jv_free (lib_origin );
152157 return rel_path ;
153158 }
154159 if (jv_get_kind (rel_path ) != JV_KIND_STRING ) {
155160 jv_free (rel_path );
156161 jv_free (search );
157- jv_free (xdg_config_home );
162+ jv_free (config_home );
158163 jv_free (jq_origin );
159164 jv_free (lib_origin );
160165 return jv_invalid_with_msg (jv_string_fmt ("Module path must be a string" ));
161166 }
162167 if (jv_get_kind (search ) != JV_KIND_ARRAY ) {
163168 jv_free (rel_path );
164169 jv_free (search );
165- jv_free (xdg_config_home );
170+ jv_free (config_home );
166171 jv_free (jq_origin );
167172 jv_free (lib_origin );
168173 return jv_invalid_with_msg (jv_string_fmt ("Module search path must be an array" ));
@@ -172,7 +177,7 @@ static jv find_lib(jq_state *jq, jv rel_path, jv search, const char *suffix, jv
172177 int ret ;
173178
174179 // Ideally we should cache this somewhere
175- search = build_lib_search_chain (jq , search , xdg_config_home , jq_origin , lib_origin );
180+ search = build_lib_search_chain (jq , search , config_home , jq_origin , lib_origin );
176181 jv err = jv_array_get (jv_copy (search ), 1 );
177182 search = jv_array_get (search , 0 );
178183
@@ -254,7 +259,7 @@ static jv default_search(jq_state *jq, jv value) {
254259}
255260
256261// XXX Split this into a util that takes a callback, and then...
257- static int process_dependencies (jq_state * jq , jv xdg_config_home , jv jq_origin , jv lib_origin , block * src_block , struct lib_loading_state * lib_state ) {
262+ static int process_dependencies (jq_state * jq , jv config_home , jv jq_origin , jv lib_origin , block * src_block , struct lib_loading_state * lib_state ) {
258263 jv deps = block_take_imports (src_block );
259264 block bk = * src_block ;
260265 int nerrors = 0 ;
@@ -283,7 +288,7 @@ static int process_dependencies(jq_state *jq, jv xdg_config_home, jv jq_origin,
283288 // dep is now freed; do not reuse
284289
285290 // find_lib does a lot of work that could be cached...
286- jv resolved = find_lib (jq , relpath , search , is_data ? ".json" : ".jq" , jv_copy (xdg_config_home ), jv_copy (jq_origin ), jv_copy (lib_origin ));
291+ jv resolved = find_lib (jq , relpath , search , is_data ? ".json" : ".jq" , jv_copy (config_home ), jv_copy (jq_origin ), jv_copy (lib_origin ));
287292 // XXX ...move the rest of this into a callback.
288293 if (!jv_is_valid (resolved )) {
289294 jv_free (as );
@@ -295,7 +300,7 @@ static int process_dependencies(jq_state *jq, jv xdg_config_home, jv jq_origin,
295300 jq_report_error (jq , jv_string_fmt ("jq: error: %s\n" ,jv_string_value (emsg )));
296301 jv_free (emsg );
297302 jv_free (deps );
298- jv_free (xdg_config_home );
303+ jv_free (config_home );
299304 jv_free (jq_origin );
300305 jv_free (lib_origin );
301306 return 1 ;
@@ -335,7 +340,7 @@ static int process_dependencies(jq_state *jq, jv xdg_config_home, jv jq_origin,
335340 jv_free (as );
336341 }
337342 jv_free (lib_origin );
338- jv_free (xdg_config_home );
343+ jv_free (config_home );
339344 jv_free (jq_origin );
340345 jv_free (deps );
341346 return nerrors ;
@@ -374,7 +379,7 @@ static int load_library(jq_state *jq, jv lib_path, int is_data, int raw, int opt
374379 locfile_free (src );
375380 if (nerrors == 0 ) {
376381 char * lib_origin = strdup (jv_string_value (lib_path ));
377- nerrors += process_dependencies (jq , get_xdg_config_home (),
382+ nerrors += process_dependencies (jq , get_config_home (),
378383 jq_get_jq_origin (jq ),
379384 jv_string (dirname (lib_origin )),
380385 & program , lib_state );
@@ -398,7 +403,7 @@ static int load_library(jq_state *jq, jv lib_path, int is_data, int raw, int opt
398403// as we do in process_dependencies.
399404jv load_module_meta (jq_state * jq , jv mod_relpath ) {
400405 // We can't know the caller's origin; we could though, if it was passed in
401- jv lib_path = find_lib (jq , validate_relpath (mod_relpath ), jq_get_lib_dirs (jq ), ".jq" , get_xdg_config_home (), jq_get_jq_origin (jq ), jv_null ());
406+ jv lib_path = find_lib (jq , validate_relpath (mod_relpath ), jq_get_lib_dirs (jq ), ".jq" , get_config_home (), jq_get_jq_origin (jq ), jv_null ());
402407 if (!jv_is_valid (lib_path ))
403408 return lib_path ;
404409 jv meta = jv_null ();
@@ -448,7 +453,7 @@ int load_program(jq_state *jq, struct locfile* src, block *out_block) {
448453 jv_free (home );
449454 }
450455
451- nerrors = process_dependencies (jq , get_xdg_config_home (), jq_get_jq_origin (jq ), jq_get_prog_origin (jq ), & program , & lib_state );
456+ nerrors = process_dependencies (jq , get_config_home (), jq_get_jq_origin (jq ), jq_get_prog_origin (jq ), & program , & lib_state );
452457 block libs = gen_noop ();
453458 for (uint64_t i = 0 ; i < lib_state .ct ; ++ i ) {
454459 free (lib_state .names [i ]);
0 commit comments