diff options
author | Lennart Poettering <lennart@poettering.net> | 2014-11-19 16:42:21 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2014-11-20 00:01:05 +0100 |
commit | a1ad376761af16da46c9ad90fd8df41c8c5c0976 (patch) | |
tree | f1693ce95f0b338f6f10f40ca044e97f9e69c9e7 /src/libsystemd/sd-bus/busctl.c | |
parent | a4962513c555fe3ac4b5bebf97a71701361a45b0 (diff) |
busctl: split out introspection parser from tree logic so that we can reuse it for a future "busctl introspect" command
Diffstat (limited to 'src/libsystemd/sd-bus/busctl.c')
-rw-r--r-- | src/libsystemd/sd-bus/busctl.c | 582 |
1 files changed, 15 insertions, 567 deletions
diff --git a/src/libsystemd/sd-bus/busctl.c b/src/libsystemd/sd-bus/busctl.c index 6d03692c7f..c58a97bc14 100644 --- a/src/libsystemd/sd-bus/busctl.c +++ b/src/libsystemd/sd-bus/busctl.c @@ -35,6 +35,7 @@ #include "bus-util.h" #include "bus-dump.h" #include "bus-signature.h" +#include "busctl-introspect.h" static bool arg_no_pager = false; static bool arg_legend = true; @@ -304,550 +305,27 @@ static void print_tree(const char *prefix, char **l) { print_subtree(prefix, "/", l); } -static int parse_xml_annotation( - const char **p, - void **xml_state) { - - - enum { - STATE_ANNOTATION, - STATE_NAME, - STATE_VALUE - } state = STATE_ANNOTATION; - - for (;;) { - _cleanup_free_ char *name = NULL; - - int t; - - t = xml_tokenize(p, &name, xml_state, NULL); - if (t < 0) { - log_error("XML parse error."); - return t; - } - - if (t == XML_END) { - log_error("Premature end of XML data."); - return -EBADMSG; - } - - switch (state) { - - case STATE_ANNOTATION: - - if (t == XML_ATTRIBUTE_NAME) { - - if (streq_ptr(name, "name")) - state = STATE_NAME; - - else if (streq_ptr(name, "value")) - state = STATE_VALUE; - - else { - log_error("Unexpected <annotation> attribute %s.", name); - return -EBADMSG; - } - - } else if (t == XML_TAG_CLOSE_EMPTY || - (t == XML_TAG_CLOSE && streq_ptr(name, "annotation"))) - - return 0; - - else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) { - log_error("Unexpected token in <annotation>. (1)"); - return -EINVAL; - } - - break; - - case STATE_NAME: - - if (t == XML_ATTRIBUTE_VALUE) - state = STATE_ANNOTATION; - else { - log_error("Unexpected token in <annotation>. (2)"); - return -EINVAL; - } - - break; - - case STATE_VALUE: - - if (t == XML_ATTRIBUTE_VALUE) - state = STATE_ANNOTATION; - else { - log_error("Unexpected token in <annotation>. (3)"); - return -EINVAL; - } - - break; - - default: - assert_not_reached("Bad state"); - } - } -} - -static int parse_xml_node( - const char *prefix, - Set *paths, - const char **p, - void **xml_state) { - - enum { - STATE_NODE, - STATE_NODE_NAME, - STATE_INTERFACE, - STATE_INTERFACE_NAME, - STATE_METHOD, - STATE_METHOD_NAME, - STATE_METHOD_ARG, - STATE_METHOD_ARG_NAME, - STATE_METHOD_ARG_TYPE, - STATE_METHOD_ARG_DIRECTION, - STATE_SIGNAL, - STATE_SIGNAL_NAME, - STATE_SIGNAL_ARG, - STATE_SIGNAL_ARG_NAME, - STATE_SIGNAL_ARG_TYPE, - STATE_PROPERTY, - STATE_PROPERTY_NAME, - STATE_PROPERTY_TYPE, - STATE_PROPERTY_ACCESS, - } state = STATE_NODE; - - _cleanup_free_ char *node_path = NULL; - const char *np = prefix; +static int on_path(const char *path, void *userdata) { + Set *paths = userdata; int r; - for (;;) { - _cleanup_free_ char *name = NULL; - int t; - - t = xml_tokenize(p, &name, xml_state, NULL); - if (t < 0) { - log_error("XML parse error."); - return t; - } - - if (t == XML_END) { - log_error("Premature end of XML data."); - return -EBADMSG; - } - - switch (state) { - - case STATE_NODE: - if (t == XML_ATTRIBUTE_NAME) { - - if (streq_ptr(name, "name")) - state = STATE_NODE_NAME; - else { - log_error("Unexpected <node> attribute %s.", name); - return -EBADMSG; - } - - } else if (t == XML_TAG_OPEN) { - - if (streq_ptr(name, "interface")) - state = STATE_INTERFACE; - - else if (streq_ptr(name, "node")) { - - r = parse_xml_node(np, paths, p, xml_state); - if (r < 0) - return r; - } else { - log_error("Unexpected <node> tag %s.", name); - return -EBADMSG; - } - } else if (t == XML_TAG_CLOSE_EMPTY || - (t == XML_TAG_CLOSE && streq_ptr(name, "node"))) { - - if (paths) { - if (!node_path) { - node_path = strdup(np); - if (!node_path) - return log_oom(); - } - - r = set_put(paths, node_path); - if (r < 0) - return log_oom(); - else if (r > 0) - node_path = NULL; - } - - return 0; - - } else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) { - log_error("Unexpected token in <node>. (1)"); - return -EINVAL; - } - - break; - - case STATE_NODE_NAME: - - if (t == XML_ATTRIBUTE_VALUE) { - - free(node_path); - - if (name[0] == '/') { - node_path = name; - name = NULL; - } else { - - if (endswith(prefix, "/")) - node_path = strappend(prefix, name); - else - node_path = strjoin(prefix, "/", name, NULL); - if (!node_path) - return log_oom(); - } - - np = node_path; - state = STATE_NODE; - } else { - log_error("Unexpected token in <node>. (2)"); - return -EINVAL; - } - - break; - - case STATE_INTERFACE: - - if (t == XML_ATTRIBUTE_NAME) { - if (streq_ptr(name, "name")) - state = STATE_INTERFACE_NAME; - else { - log_error("Unexpected <interface> attribute %s.", name); - return -EBADMSG; - } - - } else if (t == XML_TAG_OPEN) { - if (streq_ptr(name, "method")) - state = STATE_METHOD; - else if (streq_ptr(name, "signal")) - state = STATE_SIGNAL; - else if (streq_ptr(name, "property")) - state = STATE_PROPERTY; - else if (streq_ptr(name, "annotation")) { - r = parse_xml_annotation(p, xml_state); - if (r < 0) - return r; - } else { - log_error("Unexpected <interface> tag %s.", name); - return -EINVAL; - } - } else if (t == XML_TAG_CLOSE_EMPTY || - (t == XML_TAG_CLOSE && streq_ptr(name, "interface"))) - - state = STATE_NODE; + assert(paths); - else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) { - log_error("Unexpected token in <interface>. (1)"); - return -EINVAL; - } - - break; - - case STATE_INTERFACE_NAME: - - if (t == XML_ATTRIBUTE_VALUE) - state = STATE_INTERFACE; - else { - log_error("Unexpected token in <interface>. (2)"); - return -EINVAL; - } - - break; - - case STATE_METHOD: - - if (t == XML_ATTRIBUTE_NAME) { - if (streq_ptr(name, "name")) - state = STATE_METHOD_NAME; - else { - log_error("Unexpected <method> attribute %s", name); - return -EBADMSG; - } - } else if (t == XML_TAG_OPEN) { - if (streq_ptr(name, "arg")) - state = STATE_METHOD_ARG; - else if (streq_ptr(name, "annotation")) { - r = parse_xml_annotation(p, xml_state); - if (r < 0) - return r; - } else { - log_error("Unexpected <method> tag %s.", name); - return -EINVAL; - } - } else if (t == XML_TAG_CLOSE_EMPTY || - (t == XML_TAG_CLOSE && streq_ptr(name, "method"))) - - state = STATE_INTERFACE; - - else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) { - log_error("Unexpected token in <method> (1)."); - return -EINVAL; - } - - break; - - case STATE_METHOD_NAME: - - if (t == XML_ATTRIBUTE_VALUE) - state = STATE_METHOD; - else { - log_error("Unexpected token in <method> (2)."); - return -EINVAL; - } - - break; - - case STATE_METHOD_ARG: - - if (t == XML_ATTRIBUTE_NAME) { - if (streq_ptr(name, "name")) - state = STATE_METHOD_ARG_NAME; - else if (streq_ptr(name, "type")) - state = STATE_METHOD_ARG_TYPE; - else if (streq_ptr(name, "direction")) - state = STATE_METHOD_ARG_DIRECTION; - else { - log_error("Unexpected method <arg> attribute %s.", name); - return -EBADMSG; - } - } else if (t == XML_TAG_OPEN) { - if (streq_ptr(name, "annotation")) { - r = parse_xml_annotation(p, xml_state); - if (r < 0) - return r; - } else { - log_error("Unexpected method <arg> tag %s.", name); - return -EINVAL; - } - } else if (t == XML_TAG_CLOSE_EMPTY || - (t == XML_TAG_CLOSE && streq_ptr(name, "arg"))) - - state = STATE_METHOD; - - else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) { - log_error("Unexpected token in method <arg>. (1)"); - return -EINVAL; - } - - break; - - case STATE_METHOD_ARG_NAME: - - if (t == XML_ATTRIBUTE_VALUE) - state = STATE_METHOD_ARG; - else { - log_error("Unexpected token in method <arg>. (2)"); - return -EINVAL; - } - - break; - - case STATE_METHOD_ARG_TYPE: - - if (t == XML_ATTRIBUTE_VALUE) - state = STATE_METHOD_ARG; - else { - log_error("Unexpected token in method <arg>. (3)"); - return -EINVAL; - } - - break; - - case STATE_METHOD_ARG_DIRECTION: - - if (t == XML_ATTRIBUTE_VALUE) - state = STATE_METHOD_ARG; - else { - log_error("Unexpected token in method <arg>. (4)"); - return -EINVAL; - } - - break; - - case STATE_SIGNAL: - - if (t == XML_ATTRIBUTE_NAME) { - if (streq_ptr(name, "name")) - state = STATE_SIGNAL_NAME; - else { - log_error("Unexpected <signal> attribute %s.", name); - return -EBADMSG; - } - } else if (t == XML_TAG_OPEN) { - if (streq_ptr(name, "arg")) - state = STATE_SIGNAL_ARG; - else if (streq_ptr(name, "annotation")) { - r = parse_xml_annotation(p, xml_state); - if (r < 0) - return r; - } else { - log_error("Unexpected <signal> tag %s.", name); - return -EINVAL; - } - } else if (t == XML_TAG_CLOSE_EMPTY || - (t == XML_TAG_CLOSE && streq_ptr(name, "signal"))) - - state = STATE_INTERFACE; - - else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) { - log_error("Unexpected token in <signal>. (1)"); - return -EINVAL; - } - - break; - - case STATE_SIGNAL_NAME: - - if (t == XML_ATTRIBUTE_VALUE) - state = STATE_SIGNAL; - else { - log_error("Unexpected token in <signal>. (2)"); - return -EINVAL; - } - - break; - - - case STATE_SIGNAL_ARG: - - if (t == XML_ATTRIBUTE_NAME) { - if (streq_ptr(name, "name")) - state = STATE_SIGNAL_ARG_NAME; - else if (streq_ptr(name, "type")) - state = STATE_SIGNAL_ARG_TYPE; - else { - log_error("Unexpected signal <arg> attribute %s.", name); - return -EBADMSG; - } - } else if (t == XML_TAG_OPEN) { - if (streq_ptr(name, "annotation")) { - r = parse_xml_annotation(p, xml_state); - if (r < 0) - return r; - } else { - log_error("Unexpected signal <arg> tag %s.", name); - return -EINVAL; - } - } else if (t == XML_TAG_CLOSE_EMPTY || - (t == XML_TAG_CLOSE && streq_ptr(name, "arg"))) - - state = STATE_SIGNAL; - - else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) { - log_error("Unexpected token in signal <arg> (1)."); - return -EINVAL; - } - - break; - - case STATE_SIGNAL_ARG_NAME: - - if (t == XML_ATTRIBUTE_VALUE) - state = STATE_SIGNAL_ARG; - else { - log_error("Unexpected token in signal <arg> (2)."); - return -EINVAL; - } - - break; - - case STATE_SIGNAL_ARG_TYPE: - - if (t == XML_ATTRIBUTE_VALUE) - state = STATE_SIGNAL_ARG; - else { - log_error("Unexpected token in signal <arg> (3)."); - return -EINVAL; - } - - break; - - case STATE_PROPERTY: - - if (t == XML_ATTRIBUTE_NAME) { - if (streq_ptr(name, "name")) - state = STATE_PROPERTY_NAME; - else if (streq_ptr(name, "type")) - state = STATE_PROPERTY_TYPE; - else if (streq_ptr(name, "access")) - state = STATE_PROPERTY_ACCESS; - else { - log_error("Unexpected <property> attribute %s.", name); - return -EBADMSG; - } - } else if (t == XML_TAG_OPEN) { - - if (streq_ptr(name, "annotation")) { - r = parse_xml_annotation(p, xml_state); - if (r < 0) - return r; - } else { - log_error("Unexpected <property> tag %s.", name); - return -EINVAL; - } - - } else if (t == XML_TAG_CLOSE_EMPTY || - (t == XML_TAG_CLOSE && streq_ptr(name, "property"))) - - state = STATE_INTERFACE; - - else if (t != XML_TEXT || !in_charset(name, WHITESPACE)) { - log_error("Unexpected token in <property>. (1)"); - return -EINVAL; - } - - break; - - case STATE_PROPERTY_NAME: - - if (t == XML_ATTRIBUTE_VALUE) - state = STATE_PROPERTY; - else { - log_error("Unexpected token in <property>. (2)"); - return -EINVAL; - } - - break; - - case STATE_PROPERTY_TYPE: - - if (t == XML_ATTRIBUTE_VALUE) - state = STATE_PROPERTY; - else { - log_error("Unexpected token in <property>. (3)"); - return -EINVAL; - } - - break; - - case STATE_PROPERTY_ACCESS: - - if (t == XML_ATTRIBUTE_VALUE) - state = STATE_PROPERTY; - else { - log_error("Unexpected token in <property>. (4)"); - return -EINVAL; - } + r = set_put_strdup(paths, path); + if (r < 0) + return log_oom(); - break; - } - } + return 0; } static int find_nodes(sd_bus *bus, const char *service, const char *path, Set *paths) { + const XMLIntrospectOps ops = { + .on_path = on_path, + }; + _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; - _cleanup_free_ sd_bus_error error = SD_BUS_ERROR_NULL; - const char *xml, *p; - void *xml_state = NULL; + _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; + const char *xml; int r; r = sd_bus_call_method(bus, service, path, "org.freedesktop.DBus.Introspectable", "Introspect", &error, &reply, ""); @@ -861,37 +339,7 @@ static int find_nodes(sd_bus *bus, const char *service, const char *path, Set *p return bus_log_parse_error(r); /* fputs(xml, stdout); */ - - p = xml; - for (;;) { - _cleanup_free_ char *name = NULL; - - r = xml_tokenize(&p, &name, &xml_state, NULL); - if (r < 0) { - log_error("XML parse error"); - return r; - } - - if (r == XML_END) - break; - - if (r == XML_TAG_OPEN) { - - if (streq(name, "node")) { - r = parse_xml_node(path, paths, &p, &xml_state); - if (r < 0) - return r; - } else { - log_error("Unexpected tag '%s' in introspection data.", name); - return -EBADMSG; - } - } else if (r != XML_TEXT || !in_charset(name, WHITESPACE)) { - log_error("Unexpected token."); - return -EINVAL; - } - } - - return 0; + return parse_xml_introspect(path, xml, &ops, paths); } static int tree_one(sd_bus *bus, const char *service, const char *prefix) { |