static ni_bool_t ni_dhcp4_tester_req_init(ni_dhcp4_request_t *req, const char *request) { /* Apply some defaults */ req->dry_run = NI_DHCP4_RUN_OFFER; req->update = ~0; req->acquire_timeout = 10; if (!ni_string_empty(request)) { xml_document_t *doc; if (!(doc = xml_document_read(request))) { ni_error("Cannot parse dhcp4 request xml '%s'", request); return FALSE; } if (!ni_dhcp4_tester_req_xml_init(req, doc)) { xml_document_free(doc); return FALSE; } xml_document_free(doc); } /* Always enter dry run mode */ if (ni_uuid_is_null(&req->uuid)) ni_uuid_generate(&req->uuid); return TRUE; }
/** * Tries to parse a document containing multiple tags */ static void test_xml_parse_document_1() { SOURCE(source, "" "<Parent>\n" "\t<Child>\n" "\t\tFirst content\n" "\t</Child>\n" "\t<Child>\n" "\t\tSecond content\n" "\t</Child>\n" "</Parent>\n" ); struct xml_document* document = xml_parse_document(source, strlen(source)); assert_that(document, "Could not parse document"); struct xml_node* root = xml_document_root(document); // assert_that(string_equals(xml_node_name(root), "Parent"), "root node name must be `Parent'"); assert_that(2 == xml_node_children(root), "root must have two children"); struct xml_node* first_child = xml_node_child(root, 0); struct xml_node* second_child = xml_node_child(root, 1); assert_that(first_child && second_child, "Failed retrieving the children of root"); struct xml_node* third_child = xml_node_child(root, 2); assert_that(!third_child, "root has a third child where non should be"); // assert_that(string_equals(xml_node_name(first_child), "Child"), "first_child node name must be `Child'"); // assert_that(string_equals(xml_node_content(first_child), "First content"), "first_child node content must be `First content'"); // assert_that(string_equals(xml_node_name(second_child), "Child"), "second_child node name must be `Child'"); // assert_that(string_equals(xml_node_content(second_child), "Second content"), "second_child node content must be `tSecond content'"); xml_document_free(document, true); }
ni_bool_t ni_objectmodel_save_state(const char *filename) { xml_document_t *doc; ni_bool_t rv = FALSE; FILE *fp = NULL; ni_debug_objectmodel("saving server state to %s", filename); doc = xml_document_new(); if (!ni_objectmodel_save_state_xml(doc->root, __ni_objectmodel_server)) goto done; fp = ni_file_open(filename, "w", 0600); if (xml_document_print(doc, fp) < 0) { ni_error("%s: unable to write server state to %s", __func__, filename); goto done; } rv = TRUE; done: if (fp) fclose(fp); xml_document_free(doc); return rv; }
/** * GetLocalization.com Toolkit * =========================== * * Downloads translations from GetLocalization.com and converts them into * gettext sources * * @param argv[1] GetLocalization.com project name * @param argv[2] Working directory * * Currently this toolkit does several actions at once and is optimized for the * Violetland project. A future version might be better generalized and runtime * configurable: * * 0. Validate arguments * 1. Fetch all available languages and write them to LINGUAS * 2. All translations and write po translation file */ int main(int argc, char** argv) { /* 0. Validate arguments */ if (3 != argc) { fprintf(stderr, "Usage: gltoolkit <project> <working-directory>\n"); return EXIT_FAILURE; } uint8_t const* project = argv[1]; uint8_t const* working_directory = argv[2]; /* 1. Fetch all available languages and write them to LINGUAS */ struct gl_languages* languages = gl_get_languages(project); print_linguas(languages, working_directory, "LINGUAS"); /* 2. For every language do */ size_t i = 0; for (; i < gl_get_languages_count(languages); ++i) { struct gl_language* language = gl_get_language(languages, i); uint8_t const* language_code = gl_get_language_code(language); fprintf(stdout, "Fetching %s/%s\n", project, language_code); /* 2.a Fetch all translations */ struct gl_translations* translations = gl_get_translations( project, language_code ); /* 2.b Open translation configuration */ struct xml_document* configuration = open_configuration( language, working_directory ); struct xml_node* config = configuration ? xml_document_root(configuration) : 0 ; /* 2.c Write po file */ print_po(project, language, translations, config, working_directory); /* Free resources */ if (configuration) { xml_document_free(configuration, true); } gl_free_translations(translations); } /* Free resources and exit */ gl_free_languages(languages); return EXIT_SUCCESS; }
void xml_document_array_destroy(xml_document_array_t *array) { unsigned int i; for (i = 0; i < array->count; ++i) xml_document_free(array->data[i]); if (array->data) free(array->data); memset(array, 0, sizeof(*array)); }
void ni_iaid_map_free(ni_iaid_map_t *map) { if (map) { if (map->fd >= 0) { ni_iaid_map_unlock(map); close(map->fd); map->fd = -1; } xml_document_free(map->doc); ni_string_free(&map->file); free(map); } }
ni_bool_t ni_objectmodel_recover_state(const char *filename, const char **prefix_list) { xml_document_t *doc; ni_bool_t rv; if (!(doc = xml_document_read(filename))) { ni_error("unable to read server state from %s", filename); return FALSE; } rv = ni_objectmodel_recover_state_xml(doc->root, prefix_list); xml_document_free(doc); return rv; }
xml_document_t * xml_document_scan(FILE *fp, const char *location) { xml_reader_t reader; xml_document_t *doc; if (xml_reader_init_file(&reader, fp, location) < 0) return NULL; doc = xml_process_document(&reader); if (xml_reader_destroy(&reader) < 0) { xml_document_free(doc); return NULL; } return doc; }
xml_document_t * xml_document_from_buffer(ni_buffer_t *in_buffer, const char *location) { xml_reader_t reader; xml_document_t *doc; if (xml_reader_init_buffer(&reader, in_buffer, location) < 0) return NULL; doc = xml_process_document(&reader); if (xml_reader_destroy(&reader) < 0) { xml_document_free(doc); return NULL; } return doc; }
/** * Tries to parse a simple document containing only one tag */ static void test_xml_parse_document_0() { SOURCE(source, "<Hello>World</Hello>"); // uint8_t* source = malloc((1 + strlen("<Hello>World</Hello>")) * sizeof(uint8_t)); // { char const* content_string = "<Hello>World</Hello>"; // memcpy(source, content_string, strlen("<Hello>World</Hello>") + 1); // } struct xml_document* document = xml_parse_document(source, strlen(source)); assert_that(document, "Could not parse document"); struct xml_node* root = xml_document_root(document); // assert_that(string_equals(xml_node_name(root), "Hello"), "root node name must be `Hello'"); // assert_that(string_equals(xml_node_content(root), "World"), "root node content must be `World'"); xml_document_free(document, true); }
/** * Tests the xml_open_document functionality */ static void test_xml_parse_document_3() { #define FILE_NAME "test.xml" FILE* handle = fopen(FILE_NAME, "rb"); assert_that(handle, "Cannot open " FILE_NAME); struct xml_document* document = xml_open_document(handle); assert_that(document, "Cannot parse " FILE_NAME); struct xml_node* element = xml_easy_child( xml_document_root(document), "Element", "With", 0 ); assert_that(element, "Cannot find Document/Element/With"); // assert_that(string_equals(xml_node_content(element), "Child"), "Content of Document/Element/With must be `Child'"); xml_document_free(document, true); #undef FILE_NAME }
/** * Tests the eas functionality */ static void test_xml_parse_document_2() { SOURCE(source, "" "<Parent>\n" "\t<Child>\n" "\t\tFirst content\n" "\t</Child>\n" "\t<This><Is>\n" "<A><Test>Content A</Test></A>\n" "<B><Test>Content B</Test></B>\n" "\t</Is></This>\n" "\t<Child>\n" "\t\tSecond content\n" "\t</Child>\n" "</Parent>\n" ); struct xml_document* document = xml_parse_document(source, strlen(source)); assert_that(document, "Could not parse document"); struct xml_node* root = xml_document_root(document); // assert_that(string_equals(xml_node_name(root), "Parent"), "root node name must be `Parent'"); assert_that(3 == xml_node_children(root), "root must have two children"); struct xml_node* test_a = xml_easy_child(root, "This", "Is", "A", "Test", 0); assert_that(test_a, "Cannot find Parent/This/Is/A/Test"); // assert_that(string_equals(xml_node_content(test_a), "Content A"), "Content of Parent/This/Is/A/Test must be `Content A'"); struct xml_node* test_b = xml_easy_child(root, "This", "Is", "B", "Test", 0); assert_that(test_b, "Cannot find Parent/This/Is/B/Test"); // assert_that(string_equals(xml_node_content(test_b), "Content B"), "Content of Parent/This/Is/B/Test must be `Content B'"); struct xml_node* test_c = xml_easy_child(root, "This", "Is", "C", "Test", 0); assert_that(!test_c, "Must not find Parent/This/Is/C/Test because no such path exists"); struct xml_node* must_be_null = xml_easy_child(root, "Child"); assert_that(!must_be_null, "Parent/Child cannot be a valid expression, because there are two children named `Child' in `Parent'"); // uint8_t* name_is = xml_easy_name(xml_easy_child(root, "This", "Is", 0)); // assert_that(!strcmp(name_is, "Is"), "Name of Parent/This/Is must be `Is'"); // free(name_is); // uint8_t* content_a = xml_easy_content(test_a); // assert_that(!strcmp(content_a, "Content A"), "Content of Parent/This/Is/A/Test must be `Content A'"); // free(content_a); xml_document_free(document, true); }
xml_document_t * xml_process_document(xml_reader_t *xr) { xml_document_t *doc; xml_node_t *root; doc = xml_document_new(); root = xml_document_root(doc); /* Note! We do not deal with properly formatted XML documents here. * Specifically, we do not expect them to have a document header. */ if (!xml_process_element_nested(xr, root, 0)) { xml_document_free(doc); return NULL; } return doc; }
/* * Document reader implementation */ xml_document_t * xml_document_read(const char *filename) { xml_reader_t reader; xml_document_t *doc; if (!strcmp(filename, "-")) { if (xml_reader_init_file(&reader, stdin, NULL) < 0) return NULL; } else if (xml_reader_open(&reader, filename) < 0) return NULL; doc = xml_process_document(&reader); if (xml_reader_destroy(&reader) < 0) { xml_document_free(doc); return NULL; } return doc; }
ni_bool_t __ni_config_parse(ni_config_t *conf, const char *filename, ni_init_appdata_callback_t *cb, void *appdata) { xml_document_t *doc; xml_node_t *node, *child; ni_debug_wicked("Reading config file %s", filename); doc = xml_document_read(filename); if (!doc) { ni_error("%s: error parsing configuration file", filename); goto failed; } node = xml_node_get_child(doc->root, "config"); if (!node) { ni_error("%s: no <config> element", filename); goto failed; } /* Loop over all elements in the config file */ for (child = node->children; child; child = child->next) { if (strcmp(child->name, "include") == 0) { const char *attrval, *path; if ((attrval = xml_node_get_attr(child, "name")) == NULL) { ni_error("%s: <include> element lacks filename", xml_node_location(child)); goto failed; } if (!(path = ni_config_build_include(filename, attrval))) goto failed; if (!__ni_config_parse(conf, path, cb, appdata)) goto failed; } else if (strcmp(child->name, "use-nanny") == 0) { if (ni_parse_boolean(child->cdata, &conf->use_nanny)) { ni_error("%s: invalid <%s>%s</%s> element value", filename, child->name, child->name, child->cdata); goto failed; } } else if (strcmp(child->name, "piddir") == 0) { ni_config_parse_fslocation(&conf->piddir, child); } else if (strcmp(child->name, "statedir") == 0) { ni_config_parse_fslocation(&conf->statedir, child); } else if (strcmp(child->name, "storedir") == 0) { ni_config_parse_fslocation(&conf->storedir, child); } else if (strcmp(child->name, "dbus") == 0) { const char *attrval; if ((attrval = xml_node_get_attr(child, "name")) != NULL) ni_string_dup(&conf->dbus_name, attrval); if ((attrval = xml_node_get_attr(child, "type")) != NULL) ni_string_dup(&conf->dbus_type, attrval); } else if (strcmp(child->name, "schema") == 0) { const char *attrval; if ((attrval = xml_node_get_attr(child, "name")) != NULL) ni_string_dup(&conf->dbus_xml_schema_file, attrval); } else if (strcmp(child->name, "addrconf") == 0) { xml_node_t *gchild; for (gchild = child->children; gchild; gchild = gchild->next) { if (!strcmp(gchild->name, "default-allow-update")) ni_config_parse_update_targets(&conf->addrconf.default_allow_update, gchild); if (!strcmp(gchild->name, "dhcp4") && !ni_config_parse_addrconf_dhcp4(&conf->addrconf.dhcp4, gchild)) goto failed; if (!strcmp(gchild->name, "dhcp6") && !ni_config_parse_addrconf_dhcp6(&conf->addrconf.dhcp6, gchild)) goto failed; } } else if (strcmp(child->name, "sources") == 0) { if (!ni_config_parse_sources(conf, child)) goto failed; } else if (strcmp(child->name, "extension") == 0 || strcmp(child->name, "dbus-service") == 0) { if (!ni_config_parse_objectmodel_extension(&conf->dbus_extensions, child)) goto failed; } else if (strcmp(child->name, "netif-naming-services") == 0) { if (!ni_config_parse_objectmodel_netif_ns(&conf->ns_extensions, child)) goto failed; } else if (strcmp(child->name, "netif-firmware-discovery") == 0) { if (!ni_config_parse_objectmodel_firmware_discovery(&conf->fw_extensions, child)) goto failed; } else if (strcmp(child->name, "system-updater") == 0) { if (!ni_config_parse_system_updater(&conf->updater_extensions, child)) goto failed; } else if (cb != NULL) { if (!cb(appdata, child)) goto failed; } } if (conf->backupdir.path == NULL) { char pathname[PATH_MAX]; snprintf(pathname, sizeof(pathname), "%s/backup", conf->statedir.path); ni_config_fslocation_init(&conf->backupdir, pathname, 0700); } xml_document_free(doc); return TRUE; failed: if (doc) xml_document_free(doc); return FALSE; }