[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[augeas-devel] augeas: master - * src/builtin.c: new builtins Sys.getenv and Sys.read_file



Gitweb:        http://git.fedorahosted.org/git/augeas.git?p=augeas.git;a=commitdiff;h=7aada7dd1dd14a623cf73b4043eea6adc454db79
Commit:        7aada7dd1dd14a623cf73b4043eea6adc454db79
Parent:        44afcf37bd5f2b7ed293e78149a6fc3931153eaa
Author:        David Lutterkort <lutter redhat com>
AuthorDate:    Mon Mar 9 15:00:15 2009 -0700
Committer:     David Lutterkort <lutter redhat com>
CommitterDate: Mon Mar 9 15:38:13 2009 -0700

* src/builtin.c: new builtins Sys.getenv and Sys.read_file

---
 src/builtin.c                    |   33 +++++++++++++++++++++++++++++++++
 tests/modules/pass_read_file.aug |   15 +++++++++++++++
 2 files changed, 48 insertions(+), 0 deletions(-)

diff --git a/src/builtin.c b/src/builtin.c
index 59bc844..f0f5442 100644
--- a/src/builtin.c
+++ b/src/builtin.c
@@ -23,6 +23,8 @@
 #include <config.h>
 #include <stdio.h>
 #include <stdarg.h>
+#include <errno.h>
+#include <stdlib.h>
 
 #include "syntax.h"
 #include "memory.h"
@@ -355,6 +357,32 @@ static struct value *xform_transform(struct info *info, struct value *l,
     return v;
 }
 
+static struct value *sys_getenv(struct info *info, struct value *n) {
+    assert(n->tag == V_STRING);
+    struct value *v = make_value(V_STRING, ref(info));
+    v->string = dup_string(getenv(n->string->str));
+    return v;
+}
+
+static struct value *sys_read_file(struct info *info, struct value *n) {
+    assert(n->tag == V_STRING);
+    char *str = NULL;
+
+    str = read_file(n->string->str);
+    if (str == NULL) {
+        char error_buf[1024];
+        const char *errmsg;
+        errmsg = xstrerror(errno, error_buf, sizeof(error_buf));
+        struct value *exn = make_exn_value(ref(info),
+             "reading file %s failed:", n->string->str);
+        exn_printf_line(exn, "%s", errmsg);
+        return exn;
+    }
+    struct value *v = make_value(V_STRING, ref(info));
+    v->string = make_string(str);
+    return v;
+}
+
 struct module *builtin_init(void) {
     struct module *modl = module_create("Builtin");
     define_native(modl, "gensym", 1, gensym, T_STRING, T_STRING);
@@ -382,6 +410,11 @@ struct module *builtin_init(void) {
     define_native(modl, "excl", 1, xform_excl, T_STRING, T_FILTER);
     define_native(modl, "transform", 2, xform_transform, T_LENS, T_FILTER,
                                                          T_TRANSFORM);
+    /* System functions */
+    struct module *sys = module_create("Sys");
+    modl->next = sys;
+    define_native(sys, "getenv", 1, sys_getenv, T_STRING, T_STRING);
+    define_native(sys, "read_file", 1, sys_read_file, T_STRING, T_STRING);
     return modl;
 }
 
diff --git a/tests/modules/pass_read_file.aug b/tests/modules/pass_read_file.aug
new file mode 100644
index 0000000..f07250d
--- /dev/null
+++ b/tests/modules/pass_read_file.aug
@@ -0,0 +1,15 @@
+module Pass_read_file =
+
+(* This is a roundabout way to test that Sys.getenv and Sys.read_file *)
+(* work. Since we don't have a generic unit testing facility, we need *)
+(* to phrase things in terms of a lens test.                          *)
+
+let fname = (Sys.getenv "abs_top_srcdir") . "/tests/root/pairs.txt"
+let str = (Sys.read_file fname)
+let lns = [ key /[a-z0-9]*/ . del /[ \t]*=[ \t]*/ "="
+    . store /[^ \t\n]*/ . del /\n/ "\n" ] *
+
+test lns get str =
+  { "key1" = "value1" }
+  { "key2" = "value2" }
+  { "key3" = "value3" }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]