From e9a1de47b42499ebb7ca3e666a9fbf316d1bb704 Mon Sep 17 00:00:00 2001 From: Nicolas Williams Date: Fri, 13 Jun 2014 17:51:41 -0500 Subject: Add `env` builtin --- builtin.c | 21 ++++++++++++++++++++- docs/content/3.manual/manual.yml | 10 ++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/builtin.c b/builtin.c index 274576bc..f50cbca8 100644 --- a/builtin.c +++ b/builtin.c @@ -1,7 +1,8 @@ +#include +#include #include #include #include -#include #include "builtin.h" #include "compile.h" #include "jq_parser.h" @@ -575,6 +576,23 @@ static jv f_error(jv input, jv msg) { return jv_invalid_with_msg(msg); } +extern const char **environ; + +static jv f_env(jv input) { + jv_free(input); + jv env = jv_object(); + const char *var, *val; + for (const char **e = environ; *e != NULL; e++) { + var = e[0]; + val = strchr(e[0], '='); + if (val == NULL) + env = jv_object_set(env, jv_string(var), jv_null()); + else if (var - val < INT_MAX) + env = jv_object_set(env, jv_string_sized(var, val - var), jv_string(val + 1)); + } + return env; +} + #define LIBM_DD(name) \ {(cfunction_ptr)f_ ## name, "_" #name, 1}, @@ -620,6 +638,7 @@ static const struct cfunction function_list[] = { {(cfunction_ptr)f_max_by_impl, "_max_by_impl", 2}, {(cfunction_ptr)f_error, "error", 2}, {(cfunction_ptr)f_format, "format", 2}, + {(cfunction_ptr)f_env, "env", 1}, }; #undef LIBM_DD diff --git a/docs/content/3.manual/manual.yml b/docs/content/3.manual/manual.yml index caeb58a1..5bcce3ff 100644 --- a/docs/content/3.manual/manual.yml +++ b/docs/content/3.manual/manual.yml @@ -1172,6 +1172,16 @@ sections: input: '[[{"a":1}]]' output: ['1'] + - title: "`env`" + body: | + + Outputs an object representing jq's environment. + + examples: + - program: 'env.PAGER' + input: 'null' + output: ['"less"'] + - title: "String interpolation - `\\(foo)`" body: | -- cgit v1.2.3