23 #include <config/netconf.h>
24 #include <netcomm/fawkes/client.h>
25 #include <plugins/openprs/mod_utils.h>
28 #include <oprs_f-pub.h>
32 extern "C" void finalize();
39 action_config_load(TermList terms)
42 ACTION_ASSERT_ARG_LENGTH(
"config-load", terms, 1);
43 ACTION_SET_AND_ASSERT_ARG_TYPE(
"config-load", prefix, terms, 1, STRING);
45 #if __cplusplus >= 201103L
46 std::unique_ptr<Configuration::ValueIterator> v(g_config->
search(prefix->u.string));
48 std::auto_ptr<Configuration::ValueIterator> v(g_config->
search(prefix->u.string));
51 TermList tl = sl_make_slist();
52 tl = build_term_list(tl, build_string(v->path()));
55 tl = build_term_list(tl, build_id(declare_atom(
"UINT")));
57 TermList ll = sl_make_slist();
58 std::vector<unsigned int> uints = v->get_uints();
59 for (
size_t i = 0; i < uints.size(); ++i) {
60 ll = build_term_list(ll, build_long_long(uints[i]));
62 tl = build_term_list(tl, build_term_l_list_from_c_list(ll));
64 tl = build_term_list(tl, build_long_long(v->get_uint()));
66 }
else if (v->is_int()) {
67 tl = build_term_list(tl, build_id(declare_atom(
"INT")));
69 TermList ll = sl_make_slist();
70 std::vector<int> ints = v->get_ints();
71 for (
size_t i = 0; i < ints.size(); ++i) {
72 ll = build_term_list(ll, build_integer(ints[i]));
74 tl = build_term_list(tl, build_term_l_list_from_c_list(ll));
76 tl = build_term_list(tl, build_integer(v->get_int()));
78 }
else if (v->is_float()) {
79 tl = build_term_list(tl, build_id(declare_atom(
"FLOAT")));
81 TermList ll = sl_make_slist();
82 std::vector<float> floats = v->get_floats();
83 for (
size_t i = 0; i < floats.size(); ++i) {
84 ll = build_term_list(ll, build_float(floats[i]));
86 tl = build_term_list(tl, build_term_l_list_from_c_list(ll));
88 tl = build_term_list(tl, build_float(v->get_float()));
90 }
else if (v->is_bool()) {
91 tl = build_term_list(tl, build_id(declare_atom(
"BOOL")));
93 TermList ll = sl_make_slist();
94 std::vector<bool> bools = v->get_bools();
95 for (
size_t i = 0; i < bools.size(); ++i) {
96 ll = build_term_list(ll, bools[i] ? build_t() : build_nil());
98 tl = build_term_list(tl, build_term_l_list_from_c_list(ll));
100 tl = build_term_list(tl, v->get_bool() ? build_t() : build_nil());
102 }
else if (v->is_string()) {
103 tl = build_term_list(tl, build_id(declare_atom(
"STRING")));
105 TermList ll = sl_make_slist();
106 std::vector<std::string> strings = v->get_strings();
107 for (
size_t i = 0; i < strings.size(); ++i) {
108 ll = build_term_list(ll, build_string(strings[i].c_str()));
110 tl = build_term_list(tl, build_term_l_list_from_c_list(ll));
112 tl = build_term_list(tl, build_string(v->get_string().c_str()));
116 "Warn[config-load]: value at '%s' of unknown type '%s'",
121 add_external_fact((
char *)
"confval", tl);
124 TermList tl = sl_make_slist();
125 tl = build_term_list(tl, build_string(prefix->u.string));
126 add_external_fact((
char *)
"config-loaded", tl);
132 pred_string_prefix_p(TermList terms)
135 ACTION_ASSERT_B_ARG_LENGTH(
"string-prefix-p", terms, 2);
136 ACTION_SET_AND_ASSERT_B_ARG_TYPE(
"string-prefix-p", str, terms, 1, STRING);
137 ACTION_SET_AND_ASSERT_B_ARG_TYPE(
"string-prefix-p", prefix, terms, 2, STRING);
139 return (strncmp(str->u.string, prefix->u.string, strlen(prefix->u.string)) == 0);
143 func_string_remove_prefix(TermList terms)
146 ACTION_ASSERT_ARG_LENGTH(
"string-remove-prefix", terms, 2);
147 ACTION_SET_AND_ASSERT_ARG_TYPE(
"string-remove-prefix", str, terms, 1, STRING);
148 ACTION_SET_AND_ASSERT_ARG_TYPE(
"string-remove-prefix", prefix, terms, 2, STRING);
150 if (!pred_string_prefix_p(terms))
151 return build_string(str->u.string);
153 if (strlen(prefix->u.string) >= strlen(str->u.string))
154 return build_string(
"");
156 return build_string(&str->u.string[strlen(prefix->u.string)]);
163 printf(
"*** LOADING mod_config\n");
165 std::string fawkes_host;
166 unsigned short fawkes_port = 0;
167 get_fawkes_host_port(fawkes_host, fawkes_port);
169 printf(
"Connecting to Fawkes at %s:%u\n", fawkes_host.c_str(), fawkes_port);
176 fprintf(stderr,
"Error: cannot establish network connection: %s\n", e.
what_no_backtrace());
179 make_and_declare_action(
"config-load", action_config_load, 1);
180 make_and_declare_eval_pred(
"string-prefix-p", pred_string_prefix_p, 2, FALSE);
181 make_and_declare_eval_funct(
"string-remove-prefix", func_string_remove_prefix, 2);
182 add_user_end_kernel_hook(finalize);
189 printf(
"*** DESTROYING mod_config\n");
192 delete g_fnet_client;
193 g_fnet_client = NULL;