Skip to content

Commit

Permalink
Add env builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
nicowilliams committed Jun 13, 2014
1 parent d14fca0 commit e9a1de4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
21 changes: 20 additions & 1 deletion builtin.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "builtin.h"
#include "compile.h"
#include "jq_parser.h"
Expand Down Expand Up @@ -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},

Expand Down Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions docs/content/3.manual/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down

0 comments on commit e9a1de4

Please sign in to comment.