From edc79098656fc01483b40ef4a7455d63c612779e Mon Sep 17 00:00:00 2001 From: Florents Tselai Date: Sun, 3 Mar 2024 01:23:45 +0200 Subject: [PATCH 1/3] Add functions to work with paths: `expand_path` , `get_home`, `get_abspath` --- src/builtin.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/builtin.c b/src/builtin.c index 393fac0ded..e2d3d50bf5 100644 --- a/src/builtin.c +++ b/src/builtin.c @@ -1149,6 +1149,28 @@ static jv f_env(jq_state *jq, jv input) { return env; } +static jv f_expand_path(jq_state *jq, jv input) { + if (jv_get_kind(input) != JV_KIND_STRING) { + jv_free(input); + return type_error(input, "expand_path/1: string required"); + } + return expand_path(input); +} + +static jv f_get_home(jq_state *jq, jv input) { + jv_free(input); + return get_home(); +} + +static jv f_get_abspath(jq_state *jq, jv input) { + if (jv_get_kind(input) != JV_KIND_STRING) { + jv_free(input); + return type_error(input, "get_abspath/1: string required"); + } + return jq_realpath(input); +} + + static jv f_halt(jq_state *jq, jv input) { jv_free(input); jq_halt(jq, jv_invalid(), jv_invalid()); @@ -1738,6 +1760,9 @@ BINOPS {f_error, "error", 1}, {f_format, "format", 2}, {f_env, "env", 1}, + {f_expand_path, "expand_path", 1}, + {f_get_home, "get_home", 1}, + {f_get_abspath, "jq_realpath", 1}, {f_halt, "halt", 1}, {f_halt_error, "halt_error", 2}, {f_get_search_list, "get_search_list", 1}, From 6cead3b8a22af5b561981f1376c26c5c3da3d378 Mon Sep 17 00:00:00 2001 From: Florents Tselai Date: Sun, 3 Mar 2024 01:33:34 +0200 Subject: [PATCH 2/3] Rename to get_abspath --- src/builtin.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/builtin.c b/src/builtin.c index e2d3d50bf5..aeb9727c7c 100644 --- a/src/builtin.c +++ b/src/builtin.c @@ -1762,7 +1762,7 @@ BINOPS {f_env, "env", 1}, {f_expand_path, "expand_path", 1}, {f_get_home, "get_home", 1}, - {f_get_abspath, "jq_realpath", 1}, + {f_get_abspath, "get_abspath", 1}, {f_halt, "halt", 1}, {f_halt_error, "halt_error", 2}, {f_get_search_list, "get_search_list", 1}, From 828ab317c6ce0b89738ed1e40bda1a4ad6958d69 Mon Sep 17 00:00:00 2001 From: Florents Tselai Date: Sun, 3 Mar 2024 11:37:37 +0200 Subject: [PATCH 3/3] Remove unnecessary jv_free and correct error message --- src/builtin.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/builtin.c b/src/builtin.c index aeb9727c7c..255abf717d 100644 --- a/src/builtin.c +++ b/src/builtin.c @@ -1151,8 +1151,7 @@ static jv f_env(jq_state *jq, jv input) { static jv f_expand_path(jq_state *jq, jv input) { if (jv_get_kind(input) != JV_KIND_STRING) { - jv_free(input); - return type_error(input, "expand_path/1: string required"); + return type_error(input, "expand_path/0: string required"); } return expand_path(input); } @@ -1164,8 +1163,7 @@ static jv f_get_home(jq_state *jq, jv input) { static jv f_get_abspath(jq_state *jq, jv input) { if (jv_get_kind(input) != JV_KIND_STRING) { - jv_free(input); - return type_error(input, "get_abspath/1: string required"); + return type_error(input, "get_abspath/0: string required"); } return jq_realpath(input); }