output:
- '[{"a":{"b":2}}]'
- - title: "`env`"
+ - title: "`$ENV`, `env`"
body: |
- Outputs an object representing jq's environment.
+ `$ENV` is an object representing the environment variables as
+ set when the jq program started.
+
+ `env` outputs an object representing jq's current environment.
+
+ At the moment there is no builtin for setting environment
+ variables.
examples:
+ - program: '$ENV.PAGER'
+ input: 'null'
+ output: ['"less"']
+
- program: 'env.PAGER'
input: 'null'
output: ['"less"']
.
.IP "" 0
.
-.SS "env"
-Outputs an object representing jq\'s environment\.
+.SS "$ENV, env"
+\fB$ENV\fR is an object representing the environment variables as set when the jq program started\.
+.
+.P
+\fBenv\fR outputs an object representing jq\'s current environment\.
+.
+.P
+At the moment there is no builtin for setting environment variables\.
.
.IP "" 4
.
.nf
+jq \'$ENV\.PAGER\'
+ null
+=> "less"
+
jq \'env\.PAGER\'
null
=> "less"
#include <math.h>
#include <string.h>
#include <stdlib.h>
+#include <unistd.h>
#include "compile.h"
#include "bytecode.h"
#include "locfile.h"
return n;
}
+#ifdef WIN32
+extern const char **environ;
+#endif
+
+static jv env = {JV_KIND_INVALID, 0, 0, 0, {0}};
+
+static void
+free_env(void)
+{
+ jv_free(env);
+}
+
+static jv
+make_env(void)
+{
+ if (jv_is_valid(env))
+ return jv_copy(env);
+ jv r = jv_object();
+ if (environ == NULL)
+ return r;
+ for (size_t i = 0; environ[i] != NULL; i++) {
+ const char *eq;
+
+ if ((eq = strchr(environ[i], '=')) == NULL)
+ r = jv_object_delete(r, jv_string(environ[i]));
+ else
+ r = jv_object_set(r, jv_string_sized(environ[i], eq - environ[i]), jv_string(eq + 1));
+ }
+ atexit(free_env);
+ return (env = jv_copy(r));
+}
// Expands call instructions into a calling sequence
static int expand_call_arglist(block* b) {
block ret = gen_noop();
for (inst* curr; (curr = block_take(b));) {
if (opcode_describe(curr->op)->flags & OP_HAS_BINDING) {
- if (!curr->bound_by) {
+ if (!curr->bound_by && curr->op == LOADV && strcmp(curr->symbol, "ENV") == 0) {
+ curr->op = LOADK;
+ curr->imm.constant = make_env();
+ } else if (!curr->bound_by) {
if (curr->symbol[0] == '*' && curr->symbol[1] >= '1' && curr->symbol[1] <= '3' && curr->symbol[2] == '\0')
locfile_locate(curr->locfile, curr->source, "jq: error: break used outside labeled control structure");
else if (curr->op == LOADV)