/** * \brief Prepare basic information needed to dynamically load storage plugins * specified to be the output plugins of the collectingProcess. This information * is get from the user configuration (given as parameter) and from internal * configuration of the ipfixcol. * * @param[in] collector_node XML node with parameters for the particular * collectingProcess. This is part of user configuration file. * @param[in] config User XML configuration. * @param[in] internal_cfg internalcfg.xml file. * @return List of information about storage plugin for the specified collector, * NULL in case of error. */ struct plugin_xml_conf_list* get_storage_plugins(xmlNodePtr collector_node, xmlDocPtr config, char *internal_cfg) { int i, j, k, l; xmlDocPtr collector_doc = NULL, exporter_doc = NULL; xmlNodePtr aux_node = NULL, node_filewriter = NULL; xmlXPathContextPtr internal_ctxt = NULL, collector_ctxt = NULL, config_ctxt = NULL, exporter_ctxt = NULL; xmlXPathObjectPtr xpath_obj_expprocnames = NULL, xpath_obj_expproc = NULL, xpath_obj_destinations = NULL, xpath_obj_plugin_desc = NULL; xmlChar *file_format = (xmlChar *) "", *file_format_inter, *plugin_file, *odid, *thread_name, *single_mgr_txt; struct plugin_xml_conf_list* plugins = NULL, *aux_plugin = NULL; char *odidptr; bool single_mgr; /* initiate internal config - open xml file, get xmlDoc and prepare xpath context for it */ if ((internal_ctxt = ic_init(BAD_CAST "cesnet-ipfixcol-int", internal_cfg)) == NULL) { goto cleanup; } /* get the list of supported storage plugins description (including supported file formats) */ xpath_obj_plugin_desc = xmlXPathEvalExpression(BAD_CAST "/cesnet-ipfixcol-int:ipfixcol/cesnet-ipfixcol-int:storagePlugin", internal_ctxt); if (xpath_obj_plugin_desc == NULL || xmlXPathNodeSetIsEmpty(xpath_obj_plugin_desc->nodesetval)) { MSG_ERROR(msg_module, "No list of supported storage formats found in internal configuration"); goto cleanup; } /* get all <exportingProcess>s from the collector node */ collector_doc = xmlNewDoc (BAD_CAST "1.0"); xmlDocSetRootElement (collector_doc, xmlCopyNode(collector_node, 1)); /* create xpath evaluation context of collector node */ if ((collector_ctxt = xmlXPathNewContext(collector_doc)) == NULL) { MSG_ERROR(msg_module, "Unable to create XPath context for collectingProcess (%s:%d)", __FILE__, __LINE__); goto cleanup; } /* register namespace for the context of internal configuration file */ if (xmlXPathRegisterNs(collector_ctxt, BAD_CAST "ietf-ipfix", BAD_CAST "urn:ietf:params:xml:ns:yang:ietf-ipfix-psamp") != 0) { MSG_ERROR(msg_module, "Unable to register namespace for collectingProcess (%s:%d)", __FILE__, __LINE__); return (NULL); } /* search for <exportingProcess>s nodes defining exporters (including fileWriters) */ xpath_obj_expprocnames = xmlXPathEvalExpression(BAD_CAST "/ietf-ipfix:collectingProcess/ietf-ipfix:exportingProcess", collector_ctxt); if (xpath_obj_expprocnames == NULL || xmlXPathNodeSetIsEmpty(xpath_obj_expprocnames->nodesetval)) { MSG_ERROR(msg_module, "No exportingProcess defined for collectingProcess"); goto cleanup; } /* create xpath evaluation context of user configuration */ if ((config_ctxt = xmlXPathNewContext(config)) == NULL) { MSG_ERROR(msg_module, "Unable to create XPath context for user configuration (%s:%d)", __FILE__, __LINE__); goto cleanup; } /* register namespace for the context of internal configuration file */ if (xmlXPathRegisterNs(config_ctxt, BAD_CAST "ietf-ipfix", BAD_CAST "urn:ietf:params:xml:ns:yang:ietf-ipfix-psamp") != 0) { MSG_ERROR(msg_module, "Unable to register namespace for user configuration (%s:%d)", __FILE__, __LINE__); goto cleanup; } /* now look for <exportingProcess> definition with name(s) specified in <collectingProcess> */ /* first, get all <exportingProcess>es */ xpath_obj_expproc = xmlXPathEvalExpression(BAD_CAST "/ietf-ipfix:ipfix/ietf-ipfix:exportingProcess", config_ctxt); if (xpath_obj_expproc == NULL || xmlXPathNodeSetIsEmpty(xpath_obj_expproc->nodesetval)) { MSG_ERROR(msg_module, "No exportingProcess defined in user configuration"); goto cleanup; } /* and then check them for searching names */ for (i = 0; i < xpath_obj_expprocnames->nodesetval->nodeNr; i++) { for (j = 0; j < xpath_obj_expproc->nodesetval->nodeNr; j++) { aux_node = xpath_obj_expproc->nodesetval->nodeTab[j]->children; while (aux_node) { /* get the <name> element */ if (!xmlStrncmp(aux_node->name, BAD_CAST "name", strlen ("name") + 1)) { /* compare the content of <name> in user config and <name> in the internal config */ if (!xmlStrncmp(xpath_obj_expprocnames->nodesetval->nodeTab[i]->children->content, aux_node->children->content, xmlStrlen(aux_node->children->content) + 1)) { /* we got it! maybe :) */ /* now check if it is a <fileWriter> because all storage plugins ARE <fileWriter>s */ exporter_doc = xmlNewDoc(BAD_CAST "1.0"); xmlDocSetRootElement(exporter_doc, xmlCopyNode (xpath_obj_expproc->nodesetval->nodeTab[j], 1)); /* check if single output data manager is required */ single_mgr = false; single_mgr_txt = get_children_content(xpath_obj_expproc->nodesetval->nodeTab[j], BAD_CAST "singleManager"); if (single_mgr_txt) { single_mgr = !xmlStrcmp(single_mgr_txt, BAD_CAST "yes"); } /* create xpath evaluation context of <exportingProcess> node */ exporter_ctxt = xmlXPathNewContext(exporter_doc); if (exporter_ctxt == NULL) { MSG_ERROR(msg_module, "Unable to create XPath context for exportingProcess (%s:%d)", __FILE__, __LINE__); goto cleanup; } /* register namespace for the context of <exportingProcess> in user config file */ if (xmlXPathRegisterNs(exporter_ctxt, BAD_CAST "ietf-ipfix", BAD_CAST "urn:ietf:params:xml:ns:yang:ietf-ipfix-psamp") != 0) { MSG_ERROR(msg_module, "Unable to register namespace for exportingProcess (%s:%d)", __FILE__, __LINE__); goto cleanup; } /* search for <destination> nodes defining ipfixcol's storage plugins */ xpath_obj_destinations = xmlXPathEvalExpression(BAD_CAST "/ietf-ipfix:exportingProcess/ietf-ipfix:destination", exporter_ctxt); if (xpath_obj_destinations == NULL || xmlXPathNodeSetIsEmpty(xpath_obj_destinations->nodesetval)) { /* no fileWriter found, continue with another exportingProcess */ goto loop_cleanup; } /* now we have a <fileWriter> node with description of storage plugin */ /* but first we have to check that we support this storage plugin type (according to fileFormat) */ for (k = 0; k < xpath_obj_destinations->nodesetval->nodeNr; k++) { plugin_file = NULL; node_filewriter = get_children(xpath_obj_destinations->nodesetval->nodeTab[k], BAD_CAST "fileWriter"); if (node_filewriter == NULL) { /* try next <destination> node */ continue; } for (l = 0; l < xpath_obj_plugin_desc->nodesetval->nodeNr; l++) { file_format_inter = get_children_content(xpath_obj_plugin_desc->nodesetval->nodeTab[l], BAD_CAST "fileFormat"); if (file_format_inter == NULL) { /* this plugin description node is invalid, there is no fileFormat element */ MSG_WARNING(msg_module, "Storage plugin with missing fileFormat detected"); continue; } file_format = get_children_content(node_filewriter, BAD_CAST "fileFormat"); if (file_format == NULL) { /* this fileWriter has no fileFormat element - use default format */ MSG_WARNING(msg_module, "User configuration contains fileWriter without format specification; using %s", DEFAULT_STORAGE_PLUGIN); /* do not allocate memory since we always use strings allocated at other places or static strings */ file_format = BAD_CAST DEFAULT_STORAGE_PLUGIN; } if (!xmlStrncmp(file_format_inter, file_format, xmlStrlen (file_format) + 1)) { /* now we are almost done - prepare an item of the plugin list for return */ plugin_file = get_children_content(xpath_obj_plugin_desc->nodesetval->nodeTab[l], BAD_CAST "file"); if (plugin_file == NULL) { MSG_WARNING(msg_module, "Unable to detect path to storage plugin file for format '%s' in internal configuration", file_format_inter); break; } /* load thread name from internalcfg.xml */ thread_name = get_children_content(xpath_obj_plugin_desc->nodesetval->nodeTab[l], BAD_CAST "threadName"); odid = get_children_content(xpath_obj_destinations->nodesetval->nodeTab[k], BAD_CAST "observationDomainId"); /* prepare plugin info structure for return list */ aux_plugin = (struct plugin_xml_conf_list*) calloc (1, sizeof(struct plugin_xml_conf_list)); if (odid != NULL) { strtol((char*) odid, &odidptr, 10); if (*odidptr == '\0') { aux_plugin->config.observation_domain_id = (char *) malloc (sizeof(char) *(xmlStrlen (odid) + 1)); strncpy_safe(aux_plugin->config.observation_domain_id, (char*) odid, xmlStrlen (odid) + 1); } else { MSG_WARNING(msg_module, "observationDomainId element '%s' not valid; ignoring...", (char*) odid); } } aux_plugin->config.file = (char *) malloc (sizeof(char) *(xmlStrlen (plugin_file) + 1)); strncpy_safe(aux_plugin->config.file, (char *) plugin_file, xmlStrlen (plugin_file) + 1); /* copy thread name to prepared string */ if (thread_name != NULL) { strncpy_safe(aux_plugin->config.name, (char *) thread_name, 16); } aux_plugin->config.xmldata = xmlNewDoc (BAD_CAST "1.0"); xmlDocSetRootElement(aux_plugin->config.xmldata, xmlCopyNode(node_filewriter, 1)); aux_plugin->config.require_single_manager = single_mgr; /* link new plugin item into the return list */ aux_plugin->next = plugins; plugins = aux_plugin; } } if (plugin_file == NULL) { MSG_ERROR(msg_module, "Unable to load storage plugin; specification for fileFormat '%s' could not be found", (char *) file_format); } } /* break while loop to get to another exportingProcess */ break; } } /* go to the next exportingProcess element */ aux_node = aux_node->next; } loop_cleanup: /* free data from previous iteration in case of multiple exportingProcesses (also erased in cleanup) */ xmlXPathFreeObject(xpath_obj_destinations); xpath_obj_destinations = NULL; xmlXPathFreeContext(exporter_ctxt); exporter_ctxt = NULL; xmlFreeDoc(exporter_doc); exporter_doc = NULL; } } /* inform that everything was done but no valid plugin has been found */ if (plugins == NULL) { MSG_WARNING(msg_module, "No valid storage plugin specification for collector found"); } cleanup: /* Cleanup of XPath data */ if (xpath_obj_expproc) { xmlXPathFreeObject(xpath_obj_expproc); } if (xpath_obj_expprocnames) { xmlXPathFreeObject(xpath_obj_expprocnames); } if (xpath_obj_plugin_desc) { xmlXPathFreeObject(xpath_obj_plugin_desc); } if (collector_ctxt) { xmlXPathFreeContext(collector_ctxt); } if (config_ctxt) { xmlXPathFreeContext(config_ctxt); } if (exporter_ctxt) { xmlXPathFreeContext(exporter_ctxt); } if (internal_ctxt) { xmlFreeDoc(internal_ctxt->doc); xmlXPathFreeContext(internal_ctxt); } if (exporter_doc) { xmlFreeDoc(exporter_doc); } if (collector_doc) { xmlFreeDoc(collector_doc); } return (plugins); }
static int noit_console_watch_check(noit_console_closure_t ncct, int argc, char **argv, noit_console_state_t *state, void *closure) { int i, cnt; int adding = (int)(vpsized_int)closure; int period = 0; char xpath[1024]; xmlXPathObjectPtr pobj = NULL; xmlXPathContextPtr xpath_ctxt = NULL; noit_conf_xml_xpath(NULL, &xpath_ctxt); if(argc < 1 || argc > 2) { nc_printf(ncct, "requires one or two arguments\n"); return -1; } /* An alternate period */ if(argc == 2) period = atoi(argv[1]); if(noit_console_mkcheck_xpath(xpath, sizeof(xpath), NULL, argc ? argv[0] : NULL)) { nc_printf(ncct, "ERROR: could not find check '%s'\n", argv[0]); return -1; } pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt); if(!pobj || pobj->type != XPATH_NODESET || xmlXPathNodeSetIsEmpty(pobj->nodesetval)) { nc_printf(ncct, "no checks found\n"); goto out; } cnt = xmlXPathNodeSetGetLength(pobj->nodesetval); for(i=0; i<cnt; i++) { uuid_t checkid; noit_check_t *check; xmlNodePtr node; char *uuid_conf; node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i); uuid_conf = (char *)xmlGetProp(node, (xmlChar *)"uuid"); if(!uuid_conf || uuid_parse(uuid_conf, checkid)) { nc_printf(ncct, "%s has invalid or missing UUID!\n", (char *)xmlGetNodePath(node) + strlen("/noit")); continue; } if(period == 0) { check = noit_poller_lookup(checkid); if(!check) continue; if(adding) noit_check_transient_add_feed(check, ncct->feed_path); else noit_check_transient_remove_feed(check, ncct->feed_path); } else { if(adding) { check = noit_check_watch(checkid, period); /* This check must be watched from the console */ noit_check_transient_add_feed(check, ncct->feed_path); /* Note the check */ noit_check_log_check(check); /* kick it off, if it isn't running already */ if(!NOIT_CHECK_LIVE(check)) noit_check_activate(check); } else { check = noit_check_get_watch(checkid, period); if(check) noit_check_transient_remove_feed(check, ncct->feed_path); } } } out: if(pobj) xmlXPathFreeObject(pobj); return 0; }
static int noit_console_config_nocheck(noit_console_closure_t ncct, int argc, char **argv, noit_console_state_t *state, void *closure) { int i, cnt; const char *err = "internal error"; noit_conf_t_userdata_t *info; xmlXPathObjectPtr pobj = NULL; xmlXPathContextPtr xpath_ctxt = NULL; char xpath[1024]; uuid_t checkid; noit_conf_xml_xpath(NULL, &xpath_ctxt); if(argc < 1) { nc_printf(ncct, "requires one argument\n"); return -1; } info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA); if(noit_console_mkcheck_xpath(xpath, sizeof(xpath), info, argv[0])) { nc_printf(ncct, "could not find check '%s'\n", argv[0]); return -1; } pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt); if(!pobj || pobj->type != XPATH_NODESET || xmlXPathNodeSetIsEmpty(pobj->nodesetval)) { err = "no checks found"; goto bad; } cnt = xmlXPathNodeSetGetLength(pobj->nodesetval); for(i=0; i<cnt; i++) { xmlNodePtr node; char *uuid_conf; node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i); uuid_conf = (char *)xmlGetProp(node, (xmlChar *)"uuid"); if(!uuid_conf || uuid_parse(uuid_conf, checkid)) { nc_printf(ncct, "%s has invalid or missing UUID!\n", (char *)xmlGetNodePath(node) + strlen("/noit")); } else { if(argc > 1) { int j; for(j=1;j<argc;j++) xmlUnsetProp(node, (xmlChar *)argv[j]); } else { nc_printf(ncct, "descheduling %s\n", uuid_conf); noit_poller_deschedule(checkid); xmlUnlinkNode(node); } noit_conf_mark_changed(); } } if(argc > 1) { noit_poller_process_checks(xpath); noit_poller_reload(xpath); } nc_printf(ncct, "rebuilding causal map...\n"); noit_poller_make_causal_map(); if(pobj) xmlXPathFreeObject(pobj); return 0; bad: if(pobj) xmlXPathFreeObject(pobj); nc_printf(ncct, "%s\n", err); return -1; }
ReturnCode checkElement(const xmlDocPtr xmlDocument, const char* elementPathDirty, xmlNodePtr* element, xmlXPathObjectPtr* xpathObject) { xmlXPathContextPtr xpathContext = NULL; xmlNodeSetPtr nodes = NULL; char elementPath[1024]; *xpathObject = NULL; /* remove trailing slash */ strncpy(elementPath, elementPathDirty, 1024); if(strlen(elementPath) > 1 && elementPath[strlen(elementPath)-1] == '/') { elementPath[strlen(elementPath)-1] = '\0'; } /* Create xpath evaluation context */ xpathContext = xmlXPathNewContext(xmlDocument); if (!xpathContext) { printMsg(MESSAGETYPE_ERROR, "Error: Unable to create new XPath context.\n"); return FAILED; } /* Evaluate Expression */ *xpathObject = xmlXPathEvalExpression((xmlChar*) elementPath, xpathContext); if (!(*xpathObject)) { printMsg(MESSAGETYPE_ERROR, "Error: Invalid XPath expression \"%s\"\n", elementPath); xmlXPathFreeContext(xpathContext); return INVALID_XPATH; } if (xmlXPathNodeSetIsEmpty((*xpathObject)->nodesetval)) { xmlXPathFreeContext(xpathContext); xmlXPathFreeObject(*xpathObject); *xpathObject = NULL; return ELEMENT_NOT_FOUND; } nodes = (*xpathObject)->nodesetval; assert(nodes); if (nodes->nodeNr > 1) { printMsg(MESSAGETYPE_ERROR, "Error: Element chosen by XPath \"%s\" expression is not unique. \n", elementPath); xmlXPathFreeContext(xpathContext); xmlXPathFreeObject(*xpathObject); *xpathObject = NULL; return ELEMENT_PATH_NOT_UNIQUE; } assert(nodes->nodeTab[0]); if (nodes->nodeTab[0]->type == XML_ELEMENT_NODE || nodes->nodeTab[0]->type == XML_DOCUMENT_NODE) { *element = nodes->nodeTab[0]; xmlXPathFreeContext(xpathContext); return SUCCESS; } else { printMsg(MESSAGETYPE_ERROR, "Error: XPath expression \"%s\"does not point to an element node.\n", elementPath); xmlXPathFreeContext(xpathContext); xmlXPathFreeObject(*xpathObject); *xpathObject = NULL; return NOT_AN_ELEMENT; } }
static int rest_show_check(mtev_http_rest_closure_t *restc, int npats, char **pats) { mtev_http_session_ctx *ctx = restc->http_ctx; xmlXPathObjectPtr pobj = NULL; xmlXPathContextPtr xpath_ctxt = NULL; xmlDocPtr doc = NULL; xmlNodePtr node, root, attr, config, state, tmp, anode; uuid_t checkid; noit_check_t *check; char xpath[1024], *uuid_conf, *module = NULL, *value = NULL; int rv, mod, mod_cnt, cnt, error_code = 500; mtev_hash_iter iter = MTEV_HASH_ITER_ZERO; const char *k; int klen; void *data; mtev_hash_table *configh; if(npats != 2 && npats != 3) goto error; rv = noit_check_xpath(xpath, sizeof(xpath), pats[0], pats[1]); if(rv == 0) goto not_found; if(rv < 0) goto error; mtev_conf_xml_xpath(NULL, &xpath_ctxt); pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt); if(!pobj || pobj->type != XPATH_NODESET || xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto not_found; cnt = xmlXPathNodeSetGetLength(pobj->nodesetval); if(cnt != 1) goto error; node = (mtev_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0); uuid_conf = (char *)xmlGetProp(node, (xmlChar *)"uuid"); if(!uuid_conf || uuid_parse(uuid_conf, checkid)) goto error; if(npats == 3 && !strcmp(pats[2], ".json")) { return rest_show_check_json(restc, checkid); } doc = xmlNewDoc((xmlChar *)"1.0"); root = xmlNewDocNode(doc, NULL, (xmlChar *)"check", NULL); xmlDocSetRootElement(doc, root); #define MYATTR(node,a,n,b) _mtev_conf_get_string(node, &(n), "@" #a, &(b)) #define INHERIT(node,a,n,b) \ _mtev_conf_get_string(node, &(n), "ancestor-or-self::node()/@" #a, &(b)) #define SHOW_ATTR(parent, node, a) do { \ char *_value = NULL; \ INHERIT(node, a, anode, _value); \ if(_value != NULL) { \ int clen, plen;\ char *_cpath, *_apath; \ xmlNodePtr child; \ _cpath = node ? (char *)xmlGetNodePath(node) : strdup(""); \ _apath = anode ? (char *)xmlGetNodePath(anode) : strdup(""); \ clen = strlen(_cpath); \ plen = strlen("/noit/checks"); \ child = xmlNewNode(NULL, (xmlChar *)#a); \ xmlNodeAddContent(child, (xmlChar *)_value); \ if(!strncmp(_cpath, _apath, clen) && _apath[clen] == '/') { \ } \ else { \ xmlSetProp(child, (xmlChar *)"inherited", (xmlChar *)_apath+plen); \ } \ xmlAddChild(parent, child); \ free(_cpath); \ free(_apath); \ free(_value); \ } \ } while(0) attr = xmlNewNode(NULL, (xmlChar *)"attributes"); xmlAddChild(root, attr); SHOW_ATTR(attr,node,uuid); SHOW_ATTR(attr,node,seq); /* Name is odd, it falls back transparently to module */ if(!INHERIT(node, module, tmp, module)) module = NULL; xmlAddChild(attr, (tmp = xmlNewNode(NULL, (xmlChar *)"name"))); if(MYATTR(node, name, anode, value)) xmlNodeAddContent(tmp, (xmlChar *)value); else if(module) xmlNodeAddContent(tmp, (xmlChar *)module); if(value) free(value); if(module) free(module); SHOW_ATTR(attr,node,module); SHOW_ATTR(attr,node,target); SHOW_ATTR(attr,node,resolve_rtype); SHOW_ATTR(attr,node,seq); SHOW_ATTR(attr,node,period); SHOW_ATTR(attr,node,timeout); SHOW_ATTR(attr,node,oncheck); SHOW_ATTR(attr,node,filterset); SHOW_ATTR(attr,node,disable); /* Add the config */ config = xmlNewNode(NULL, (xmlChar *)"config"); configh = mtev_conf_get_hash(node, "config"); while(mtev_hash_next(configh, &iter, &k, &klen, &data)) NODE_CONTENT(config, k, data); mtev_hash_destroy(configh, free, free); free(configh); mod_cnt = noit_check_registered_module_cnt(); for(mod=0; mod<mod_cnt; mod++) { xmlNsPtr ns; const char *nsname; char buff[256]; nsname = noit_check_registered_module(mod); snprintf(buff, sizeof(buff), "noit://module/%s", nsname); ns = xmlSearchNs(root->doc, root, (xmlChar *)nsname); if(!ns) ns = xmlNewNs(root, (xmlChar *)buff, (xmlChar *)nsname); if(ns) { configh = mtev_conf_get_namespaced_hash(node, "config", nsname); if(configh) { memset(&iter, 0, sizeof(iter)); while(mtev_hash_next(configh, &iter, &k, &klen, &data)) { NS_NODE_CONTENT(config, ns, "value", data, xmlSetProp(tmp, (xmlChar *)"name", (xmlChar *)k); ); } mtev_hash_destroy(configh, free, free); free(configh); } } }
char *xmlrpc_parse(char *xml) { xmlParserCtxtPtr xp; xmlDocPtr doc; xmlXPathContextPtr context; xmlXPathObjectPtr result; xmlNodeSetPtr nodeset; char *methodName = NULL; char *ret = NULL; int i; _xmlrpc_vars = NULL; _xmlrpc_vars_num = 0; _xlastElementNames = NULL; _xlastElementNames_num = 0; _xIdParent = 0; /* We need to strip the new line characters as it's making issues */ while (strstr(xml, "\n") != NULL) xml = replace(xml, "\n", ""); DPRINTF("Parsing XML:\n%s\n", xml); xp = xmlCreateDocParserCtxt( (xmlChar *)xml ); if (!xp) { DPRINTF("Cannot create DocParserCtxt\n"); return NULL; } doc = xmlCtxtReadDoc(xp, (xmlChar *)xml, NULL, NULL, 0); if (!doc) { DPRINTF("Cannot get xmlDocPtr\n"); return NULL; } context = xmlXPathNewContext(doc); if (!context) { DPRINTF("Cannot get new XPath context\n"); return NULL; } result = xmlXPathEvalExpression( (xmlChar *)"//methodCall/methodName", context); if (!result) { xmlXPathFreeContext(context); DPRINTF("Cannot evaluate expression\n"); goto cleanup; } if(xmlXPathNodeSetIsEmpty(result->nodesetval)){ xmlXPathFreeObject(result); xmlXPathFreeContext(context); DPRINTF("Nothing found\n"); goto cleanup; } nodeset = result->nodesetval; if (nodeset->nodeNr != 1) { xmlXPathFreeObject(result); xmlXPathFreeContext(context); DPRINTF("Invalid number of methodName elements\n"); goto cleanup; } methodName = (char *)xmlNodeListGetString(doc, nodeset->nodeTab[0]->xmlChildrenNode, 1); xmlXPathFreeObject(result); result = xmlXPathEvalExpression( (xmlChar *)"//methodCall/params/param", context); nodeset = result->nodesetval; for (i = 0; i < nodeset->nodeNr; i++) { xmlrpc_process_param(doc, nodeset->nodeTab[i], 0, 1); } ret = strdup(methodName); cleanup: xmlXPathFreeObject(result); xmlFreeDoc(doc); xmlCleanupParser(); return ret; }
int getdatabasecfg(void* lpxmlDoc,dbCfg* lpCfg) { xmlDocPtr pxmlDoc; xmlNodePtr cur; xmlNodePtr curpath; xmlXPathContextPtr context = NULL; xmlXPathObjectPtr result = NULL; xmlNodeSetPtr nodeset; xmlAttrPtr attrPtr; xmlChar *key; xmlChar *xpath = BAD_CAST("/application/database"); pxmlDoc = (xmlDocPtr)lpxmlDoc; cur = xmlDocGetRootElement(pxmlDoc); if (cur == NULL) { printf("Can't get the root element\n"); return 0; } context = xmlXPathNewContext(pxmlDoc); result = xmlXPathEvalExpression(xpath, context); xmlXPathFreeContext(context); if (result != NULL) { if (xmlXPathNodeSetIsEmpty(result->nodesetval) == 0) { nodeset = result->nodesetval; if(nodeset->nodeNr > 0) { curpath = nodeset->nodeTab[0]; while (curpath != NULL) { cur = curpath->xmlChildrenNode; while(cur != NULL) { if (xmlStrcmp(cur->name, (const xmlChar *)"address") == 0) { key = xmlNodeListGetString(static_cast<xmlDocPtr>(lpxmlDoc), cur->xmlChildrenNode, 1); printf("db address: %s\n", key); strcpy(lpCfg->szaddr,(char*)key); xmlFree(key); } else if(xmlStrcmp(cur->name, (const xmlChar *)"port") == 0) { key = xmlNodeListGetString(static_cast<xmlDocPtr>(lpxmlDoc), cur->xmlChildrenNode, 1); printf("db port: %s\n", key); lpCfg->nport = atoi((char*)key); xmlFree(key); } else if(xmlStrcmp(cur->name, (const xmlChar *)"dbinst") == 0) { key = xmlNodeListGetString(static_cast<xmlDocPtr>(lpxmlDoc), cur->xmlChildrenNode, 1); printf("db dbinst: %s\n", key); strcpy(lpCfg->szinstname,(char*)key); xmlFree(key); } else if(xmlStrcmp(cur->name, (const xmlChar *)"account") == 0) { attrPtr = cur->properties; while (attrPtr != NULL) { if (!xmlStrcmp(attrPtr->name, BAD_CAST "name")) { key = xmlGetProp(cur,(const xmlChar *)"name"); printf("db accout name: %s\n", key); strcpy(lpCfg->szaccname,(char*)key); xmlFree(key); } else if (!xmlStrcmp(attrPtr->name, BAD_CAST "password")) { key = xmlGetProp(cur,(const xmlChar *)"password"); printf("db account password: %s\n", key); strcpy(lpCfg->szaccpwd,(char*)key); xmlFree(key); } attrPtr = attrPtr->next; } } cur = cur->next; } curpath = curpath->next; } } } xmlXPathFreeObject(result); } return 0; }
int _tmain(int argc, _TCHAR* argv[]) { char message[100] = "[URL]https://www.youtube.com/watch?v=wCRStRWMdWM#t=39s[/URL]\0"; CURLcode curlCode; const char *curlMessage = ""; struct MemoryStruct chunk; htmlDocPtr doc; xmlXPathContextPtr context; xmlXPathObjectPtr result; char * keyword; int i; char newMessage[1024]; char errorMessage[128]; const char* url; //printf("Enter your message: "); //fgets(message, 100, stdin); printf("Your Message is: %s\n", message); system("pause"); url = GetURLFromMessage(message); chunk.memory = (char *) malloc(1); /* will be grown as needed by the realloc above */ chunk.size = 0; /* no data at this point */ if (url == NULL) { printf("URL is null, exiting...\n"); system("pause"); free(chunk.memory); return 0; } printf("URL: "); printf(url); printf("\n"); //ts3Functions.logMessage("Opening URL: ", LogLevel_INFO, "Plugin", serverConnectionHandlerID); //ts3Functions.logMessage(url, LogLevel_INFO, "Plugin", serverConnectionHandlerID); GetHTML(url, &chunk, &curlCode, curlMessage); printf("curlMessage: "); printf(curlMessage); printf("\n"); //if (curlCode != 0) { // ts3Functions.logMessage("cURL Error: ", LogLevel_ERROR, "Plugin", serverConnectionHandlerID); // ts3Functions.logMessage(curlMessage, LogLevel_ERROR, "Plugin", serverConnectionHandlerID); //} #if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64) sprintf_s(errorMessage, 128, "Reading HTML file that is the following bytes long: %d", chunk.size); #else sprintf(errorMessage, "Reading HTML file that is the following bytes long: %d", chunk.size); #endif //ts3Functions.logMessage(errorMessage, LogLevel_INFO, "Plugin", serverConnectionHandlerID); doc = htmlReadMemory(chunk.memory, chunk.size, url, NULL, HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING); if (!doc) { printf("Could not read HTML document from memory\n"); system("pause"); //ts3Functions.logMessage("Could not read HTML document from memory", LogLevel_ERROR, "Plugin", serverConnectionHandlerID); free(chunk.memory); return 0; } context = xmlXPathNewContext(doc); result = xmlXPathEvalExpression("/html/head/title", context); if (xmlXPathNodeSetIsEmpty(result->nodesetval)) { printf("Could not read HTML node set from memory\n"); system("pause"); //ts3Functions.logMessage("Could not read HTML node set from memory", LogLevel_ERROR, "Plugin", serverConnectionHandlerID); xmlXPathFreeObject(result); free(chunk.memory); return 0; } for (i=0; i < result->nodesetval->nodeNr; i++) { keyword = (char *) xmlNodeListGetString(doc, result->nodesetval->nodeTab[i]->xmlChildrenNode, 1); continue; //printf("keyword: %s\n", keyword); } #if defined(_WIN32) || defined(WIN32) || defined(WIN64) || defined(_WIN64) strcpy_s(newMessage, 1024, "\""); strcat_s(newMessage, 1024, (const char *) keyword); strcat_s(newMessage, 1024, "\" <[URL]"); strcat_s(newMessage, 1024, url); strcat_s(newMessage, 1024, "[/URL]>"); #else strcpy(newMessage, "\""); strcat(newMessage, (const char *) keyword); strcat(newMessage, "\" <"); strcat(newMessage, message); strcat(newMessage, ">"); #endif //xmlFree(keyword); xmlXPathFreeObject(result); xmlFreeDoc(doc); free(chunk.memory); printf("New message: "); printf(newMessage); printf("\n"); printf("End\n"); system("pause"); return 1; }
//! Checks whether the nodeset is empty. //----------------------------------------------------------------------- static bool empty(nodeset_type const ns) { return xmlXPathNodeSetIsEmpty(ns); }
gint parse_and_write_xml_data(const gchar *station_id, htmlDocPtr doc, const gchar *result_file){ gchar buff[256], buffer[buff_size], current_temperature[20], current_icon[10], current_title[1024], current_pressure[15], current_humidity[15], current_wind_direction[15], current_wind_speed[15]; gchar temp_buffer[buff_size]; GSList *forecast = NULL; GSList *tmp = NULL; GHashTable *day = NULL; gboolean flag; gboolean night_flag; gint size; gint i, j; GHashTable *hash_for_translate; GHashTable *hash_for_icons; xmlXPathContextPtr xpathCtx; xmlXPathObjectPtr xpathObj = NULL; xmlXPathObjectPtr xpathObj2 = NULL; xmlXPathObjectPtr xpathObj3 = NULL; xmlXPathObjectPtr xpathObj4 = NULL; xmlXPathObjectPtr xpathObj5 = NULL; xmlXPathObjectPtr xpathObj6 = NULL; xmlXPathObjectPtr xpathObj7 = NULL; xmlXPathObjectPtr xpathObj8 = NULL; xmlXPathObjectPtr xpathObj9 = NULL; xmlNodeSetPtr nodes; gchar *temp_char; gchar *temp_char2; gint pressure; gint speed; gint ppcp; gchar *image = NULL; double time_diff = 0; time_t loc_time; time_t utc_time; gint location_timezone = 0; gboolean timezone_flag = FALSE; gboolean sunrise_flag = FALSE; struct tm tmp_tm_loc = {0}; struct tm tmp_tm = {0}; struct tm current_tm = {0}; struct tm tm_l = {0}; struct tm tmp_tm2 = {0}; struct tm *tm; time_t t_start = 0, t_end = 0, t_sunrise = 0, t_sunset = 0, current_time = 0; FILE *file_out; file_out = fopen(result_file, "w"); if (!file_out) return -1; fprintf(file_out,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<station name=\"Station name\" id=\"%s\" xmlns=\"http://omweather.garage.maemo.org/schemas\">\n", station_id); fprintf(file_out," <units>\n <t>C</t>\n <ws>m/s</ws>\n <wg>m/s</wg>\n <d>km</d>\n"); fprintf(file_out," <h>%%</h> \n <p>mmHg</p>\n </units>\n"); hash_for_icons = hash_icons_forecacom_table_create(); /* Create xpath evaluation context */ xpathCtx = xmlXPathNewContext(doc); if(xpathCtx == NULL) { fprintf(stderr,"Error: unable to create new XPath context\n"); return(-1); } /* Register namespaces from list (if any) */ xmlXPathRegisterNs(xpathCtx, (const xmlChar*)"html", (const xmlChar*)"http://www.w3.org/1999/xhtml"); /* Day weather forecast */ /* Evaluate xpath expression */ xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div/div/table//tr/th[@colspan='3']", xpathCtx); if(xpathObj == NULL) { fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", "/html/body/div/div/table//tr/th[@colspan='3']/text()"); xmlXPathFreeContext(xpathCtx); return(-1); } nodes = xpathObj->nodesetval; size = (nodes) ? nodes->nodeNr : 0; if (size > 10) size = 10; /* fprintf(stderr, "SIZE!!!!!!!!!!!!!!: %i\n", size); */ xpathObj2 = xmlXPathEvalExpression("/html/body/div/div/table//tr/td[@class='in']//span[1]/text()", xpathCtx); xpathObj3 = xmlXPathEvalExpression("/html/body/div/div/table//tr/td[@class='in']//span[2]/text()", xpathCtx); xpathObj4 = xmlXPathEvalExpression("/html/body/div/div/table//tr/td[2]/img/@alt", xpathCtx); xpathObj5 = xmlXPathEvalExpression("/html/body/div/div/table//tr/td[2]/text()", xpathCtx); xpathObj6 = xmlXPathEvalExpression("/html/body/div/div/table//tr/td[3]/img/@src", xpathCtx); xpathObj7 = xmlXPathEvalExpression("/html/body/div/div/table//tr/td[@class='in2']/text()[1]", xpathCtx); xpathObj8 = xmlXPathEvalExpression("/html/body/div/div/table//tr/td[@class='in2']/text()[2]", xpathCtx); /* fprintf(stderr, "Result (%d nodes):\n", size); */ for(i = 0; i < size; ++i) { day = NULL; /* Take time: */ if (!nodes->nodeTab[i]->children->content) continue; temp_char = strstr(nodes->nodeTab[i]->children->content, " "); int j = 0; if (temp_char != NULL){ for (j=0; j<strlen(temp_char)-1; j++){ if (temp_char[j] == ' ' || temp_char[j] == '\n') continue; else{ temp_char = temp_char + j; break; } } } if (temp_char == NULL) continue; current_time = time(NULL); tm = localtime(¤t_time); setlocale(LC_TIME, "POSIX"); strptime((const char*)temp_char, "%b %d", &tmp_tm); setlocale(LC_TIME, ""); /* set begin of day in localtime */ tmp_tm.tm_year = tm->tm_year; tmp_tm.tm_hour = 0; tmp_tm.tm_min = 0; tmp_tm.tm_sec = 0; t_start = mktime(&tmp_tm); fprintf(file_out," <period start=\"%li\"", (t_start +1)); /* set end of day in localtime */ t_end = t_start + 3600*24 - 1; fprintf(file_out," end=\"%li\">\n", t_end); /* added hi temperature */ if (xpathObj2 && !xmlXPathNodeSetIsEmpty(xpathObj2->nodesetval) && xpathObj2->nodesetval->nodeTab[i] && xpathObj2->nodesetval->nodeTab[i]->content){ /* fprintf (stderr, "temperature %s\n", xpathObj2->nodesetval->nodeTab[i]->content); */ snprintf(buffer, sizeof(buffer)-1,"%s", xpathObj2->nodesetval->nodeTab[i]->content); memset(temp_buffer, 0, sizeof(temp_buffer)); for (j = 0 ; (j<(strlen(buffer)) && j < buff_size); j++ ){ if (buffer[j] == '&') break; if ((uint)buffer[j] == 226 || buffer[j] == '-' || (buffer[j]>='0' && buffer[j]<='9')){ if ((uint)buffer[j] == 226) sprintf(temp_buffer,"%s-",temp_buffer); else sprintf(temp_buffer,"%s%c",temp_buffer, buffer[j]); } } /* fprintf(stderr, " <temperature>%s</temperature>\n", temp_buffer); */ fprintf(file_out," <temperature_hi>%s</temperature_hi>\n", temp_buffer); } /* added lo temperature */ if (xpathObj3 && !xmlXPathNodeSetIsEmpty(xpathObj3->nodesetval) && xpathObj3->nodesetval->nodeTab[i] && xpathObj3->nodesetval->nodeTab[i]->content){ /* fprintf (stderr, "temperature %s\n", xpathObj3->nodesetval->nodeTab[i]->content); */ snprintf(buffer, sizeof(buffer)-1,"%s", xpathObj3->nodesetval->nodeTab[i]->content); memset(temp_buffer, 0, sizeof(temp_buffer)); for (j = 0 ; (j<(strlen(buffer)) && j < buff_size); j++ ){ if (buffer[j] == '&') break; if ((uint)buffer[j] == 226 || buffer[j] == '-' || (buffer[j]>='0' && buffer[j]<='9')){ if ((uint)buffer[j] == 226) sprintf(temp_buffer,"%s-",temp_buffer); else sprintf(temp_buffer,"%s%c",temp_buffer, buffer[j]); } } /* fprintf(stderr, " <temperature>%s</temperature>\n", temp_buffer); */ fprintf(file_out," <temperature_low>%s</temperature_low>\n", temp_buffer); } /* added wind direction */ if (xpathObj4 && !xmlXPathNodeSetIsEmpty(xpathObj4->nodesetval) && xpathObj4->nodesetval->nodeTab[i] && xpathObj4->nodesetval->nodeTab[i]->children->content){ /* fprintf(stderr, "Wind direction %s \n", xpathObj4->nodesetval->nodeTab[i]->children->content); */ fprintf(file_out," <wind_direction>%s</wind_direction>\n", xpathObj4->nodesetval->nodeTab[i]->children->content); } /* added wind speed */ if (xpathObj5 && !xmlXPathNodeSetIsEmpty(xpathObj5->nodesetval) && xpathObj5->nodesetval->nodeTab[i] && xpathObj5->nodesetval->nodeTab[i]->content){ /* fprintf(stderr, "Wind direction %s \n", xpathObj4->nodesetval->nodeTab[i]->children->content); */ fprintf(file_out," <wind_speed>%s</wind_speed>\n", xpathObj5->nodesetval->nodeTab[i]->content); } /* added icon */ if (xpathObj6 && !xmlXPathNodeSetIsEmpty(xpathObj6->nodesetval) && xpathObj6->nodesetval->nodeTab[i] && xpathObj6->nodesetval->nodeTab[i]->children->content){ temp_char = strrchr((char*)xpathObj6->nodesetval->nodeTab[i]->children->content, '/'); temp_char ++; /* fprintf (stderr, "icon %s %s \n", xpathObj6->nodesetval->nodeTab[i]->children->content, choose_hour_weather_icon(hash_for_icons, temp_char)); */ fprintf(file_out," <icon>%s</icon>\n", choose_hour_weather_icon(hash_for_icons, temp_char)); } /* added text */ if (xpathObj7 && !xmlXPathNodeSetIsEmpty(xpathObj7->nodesetval) && xpathObj7->nodesetval->nodeTab[i] && xpathObj7->nodesetval->nodeTab[i]->content){ snprintf(buffer, sizeof(buffer)-1,"%s", xpathObj7->nodesetval->nodeTab[i]->content); memset(temp_buffer, 0, sizeof(temp_buffer)); for (j = 0 ; (j<(strlen(buffer)) && j < buff_size); j++ ){ if (buffer[j] == 13 || buffer[j] == 10) continue; sprintf(temp_buffer,"%s%c",temp_buffer, buffer[j]); } fprintf(file_out," <description>%s</description>\n", temp_buffer); } /* added ppcp */ if (xpathObj8 && !xmlXPathNodeSetIsEmpty(xpathObj8->nodesetval) && xpathObj8->nodesetval->nodeTab[i] && xpathObj8->nodesetval->nodeTab[i]->content){ snprintf(buffer, sizeof(buffer) - 1, "%s", xpathObj8->nodesetval->nodeTab[i]->content); memset(temp_buffer, 0, sizeof(temp_buffer)); for (j = 0 ; (j<(strlen(buffer)) && j < strlen(buffer)); j++ ){ if (buffer[j] == '%') break; if ( (buffer[j]>='0' && buffer[j]<='9')){ sprintf(temp_buffer,"%s%c",temp_buffer, buffer[j]); } } fprintf(file_out," <ppcp>%s</ppcp>\n", temp_buffer); } fprintf(file_out," </period>\n"); } /* Cleanup */ if (xpathObj) xmlXPathFreeObject(xpathObj); if (xpathObj2) xmlXPathFreeObject(xpathObj2); if (xpathObj3) xmlXPathFreeObject(xpathObj3); if (xpathObj4) xmlXPathFreeObject(xpathObj4); if (xpathObj5) xmlXPathFreeObject(xpathObj5); if (xpathObj6) xmlXPathFreeObject(xpathObj6); if (xpathObj7) xmlXPathFreeObject(xpathObj7); if (xpathObj8) xmlXPathFreeObject(xpathObj8); /* Clean */ g_hash_table_destroy(hash_for_icons); fclose(file_out); return size; }
gint parse_and_write_detail_data(const gchar *station_id, htmlDocPtr doc, const gchar *result_file){ gchar buff[256], buffer[buff_size], current_temperature[20], current_icon[10], current_title[1024], current_pressure[15], current_humidity[15], current_wind_direction[15], current_wind_speed[15]; gchar temp_buffer[buff_size]; GSList *forecast = NULL; GSList *tmp = NULL; GHashTable *day = NULL; gboolean night_flag; gint size; gint i, j, k, l; GHashTable *hash_for_translate; GHashTable *hash_for_icons; xmlXPathContextPtr xpathCtx; xmlXPathObjectPtr xpathObj = NULL; xmlXPathObjectPtr xpathObj2 = NULL; xmlXPathObjectPtr xpathObj3 = NULL; xmlXPathObjectPtr xpathObj4 = NULL; xmlXPathObjectPtr xpathObj5 = NULL; xmlXPathObjectPtr xpathObj6 = NULL; xmlXPathObjectPtr xpathObj7 = NULL; xmlXPathObjectPtr xpathObj8 = NULL; xmlXPathObjectPtr xpathObj9 = NULL; xmlNodeSetPtr nodes; gchar *temp_char; gchar *temp_char2; gint pressure; gint speed; gint ppcp; gchar *image = NULL; double time_diff = 0; time_t loc_time; time_t utc_time; gint location_timezone = 0; gboolean timezone_flag = FALSE; gboolean sunrise_flag = FALSE; gboolean flag; struct tm tmp_tm_loc = {0}; struct tm tmp_tm = {0}; struct tm current_tm = {0}; struct tm tm_l = {0}; struct tm tmp_tm2 = {0}; struct tm *tm; time_t t_start = 0, t_end = 0, t_sunrise = 0, t_sunset = 0, current_time = 0; FILE *file_out; int index = 1; file_out = fopen(result_file, "a"); if (!file_out) return -1; hash_for_icons = hash_icons_forecacom_table_create(); /* Create xpath evaluation context */ xpathCtx = xmlXPathNewContext(doc); if(xpathCtx == NULL) { fprintf(stderr,"Error: unable to create new XPath context\n"); return(-1); } /* Register namespaces from list (if any) */ xmlXPathRegisterNs(xpathCtx, (const xmlChar*)"html", (const xmlChar*)"http://www.w3.org/1999/xhtml"); /* Current weather forecast */ /* Evaluate xpath expression */ xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div/div/table//tr[1]/th[@colspan='4']/text()", xpathCtx); if(xpathObj == NULL) { fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", " /html/body/div/div/table//tr[1]/th[@colspan='4']/text()"); xmlXPathFreeContext(xpathCtx); return(-1); } temp_char = strchr(xpathObj->nodesetval->nodeTab[0]->content, ' '); if (temp_char == NULL || strlen(temp_char)<2) return -1; temp_char = temp_char + 1; temp_char = strchr(temp_char, ' '); if (temp_char != NULL){ for (j=0; j<strlen(temp_char)-1; j++){ if (temp_char[j] == ' ' || temp_char[j] == '\n') continue; else{ temp_char = temp_char + j; break; } } } xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div/div/table//tr[3]/td[@class='in']/text()", xpathCtx); if (xpathObj && xpathObj->nodesetval->nodeTab[0]->content){ snprintf(buffer, sizeof(buffer)-1,"%s %s", temp_char, xpathObj->nodesetval->nodeTab[0]->content); current_time = time(NULL); tm = localtime(¤t_time); setlocale(LC_TIME, "POSIX"); strptime((const char*)buffer, "%d/%m %H:%M", &tmp_tm); setlocale(LC_TIME, ""); /* set begin of day in localtime */ tmp_tm.tm_year = tm->tm_year; t_start = mktime(&tmp_tm); fprintf(file_out," <period start=\"%li\"", (t_start + 1 - 2*3600)); /* set end of current time in localtime */ t_end = t_start + 3600*4 - 1; fprintf(file_out," end=\"%li\" current=\"true\" >\n", t_end); } if (xpathObj) xmlXPathFreeObject(xpathObj); xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div/div/table//tr[3]/td[2]/span/text()", xpathCtx); /* added temperature */ if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) && xpathObj->nodesetval->nodeTab[0] && xpathObj->nodesetval->nodeTab[0]->content){ /* fprintf (stderr, "temperature %s\n", xpathObj->nodesetval->nodeTab[0]->content); */ snprintf(buffer, sizeof(buffer)-1,"%s", xpathObj->nodesetval->nodeTab[0]->content); memset(temp_buffer, 0, sizeof(temp_buffer)); for (j = 0 ; (j<(strlen(buffer)) && j < buff_size); j++ ){ if (buffer[j] == '&') break; if ((uint)buffer[j] == 226 || buffer[j] == '-' || (buffer[j]>='0' && buffer[j]<='9')){ if ((uint)buffer[j] == 226) sprintf(temp_buffer,"%s-",temp_buffer); else sprintf(temp_buffer,"%s%c",temp_buffer, buffer[j]); } } /* fprintf(stderr, " <temperature>%s</temperature>\n", temp_buffer); */ fprintf(file_out," <temperature>%s</temperature>\n", temp_buffer); } if (xpathObj) xmlXPathFreeObject(xpathObj); xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div/div/table//tr[3]/td[3]/img/@alt", xpathCtx); /* added wind direction */ if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) && xpathObj->nodesetval->nodeTab[0] && xpathObj->nodesetval->nodeTab[0]->children->content){ /* fprintf(stderr, "Wind direction .%s. \n", xpathObj->nodesetval->nodeTab[0]->children->content); */ if (strlen(xpathObj->nodesetval->nodeTab[0]->children->content)>0) fprintf(file_out," <wind_direction>%s</wind_direction>\n", xpathObj->nodesetval->nodeTab[0]->children->content); } if (xpathObj) xmlXPathFreeObject(xpathObj); xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div/div/table//tr[3]/td[3]/text()", xpathCtx); /* added wind speed */ if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) && xpathObj->nodesetval->nodeTab[0] && xpathObj->nodesetval->nodeTab[0]->content){ /* fprintf(stderr, "Wind direction %s \n", xpathObj4->nodesetval->nodeTab[i]->children->content); */ fprintf(file_out," <wind_speed>%s</wind_speed>\n", xpathObj->nodesetval->nodeTab[0]->content); } if (xpathObj) xmlXPathFreeObject(xpathObj); xpathObj = xmlXPathEvalExpression((const xmlChar*)"/html/body/div/div/table//tr[3]/td[4]/img/@src", xpathCtx); /* added icon */ if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) && xpathObj->nodesetval->nodeTab[0] && xpathObj->nodesetval->nodeTab[0]->children->content){ temp_char = strrchr((char*)xpathObj->nodesetval->nodeTab[0]->children->content, '/'); temp_char ++; /* fprintf (stderr, "icon %s %s \n", xpathObj6->nodesetval->nodeTab[i]->children->content, choose_hour_weather_icon(hash_for_icons, temp_char)); */ fprintf(file_out," <icon>%s</icon>\n", choose_hour_weather_icon(hash_for_icons, temp_char)); } if (xpathObj) xmlXPathFreeObject(xpathObj); index = 1; snprintf(buffer, sizeof(buffer)-1,"/html/body/div/div/table//tr[4]/td/text()[%i]", index); xpathObj = xmlXPathEvalExpression((const xmlChar*)buffer, xpathCtx); /* added text */ if (xpathObj && !xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) && xpathObj->nodesetval->nodeTab[0] && xpathObj->nodesetval->nodeTab[0]->content){ if (strstr(xpathObj->nodesetval->nodeTab[0]->content, "Feels Like:") == NULL){ /* fprintf (stderr, "description %s\n", xpathObj7->nodesetval->nodeTab[i]->content); */ snprintf(buffer, sizeof(buffer)-1,"%s", xpathObj->nodesetval->nodeTab[0]->content); memset(temp_buffer, 0, sizeof(temp_buffer)); for (j = 0 ; (j<(strlen(buffer)) && j < buff_size); j++ ){ if (buffer[j] == 13 || buffer[j] == 10) continue; sprintf(temp_buffer,"%s%c",temp_buffer, buffer[j]); } index ++; fprintf(file_out," <description>%s</description>\n", temp_buffer); } } if (xpathObj) xmlXPathFreeObject(xpathObj); snprintf(buffer, sizeof(buffer)-1,"/html/body/div/div/table//tr[4]/td/text()[%i]", index); xpathObj = xmlXPathEvalExpression((const xmlChar*)buffer, xpathCtx); temp_char = strstr(xpathObj->nodesetval->nodeTab[0]->content, "Feels Like:"); if (temp_char != NULL){ temp_char = strchr(temp_char, ':'); temp_char = temp_char + 2; snprintf(buffer, sizeof(buffer)-1,"%s", temp_char); memset(temp_buffer, 0, sizeof(temp_buffer)); for (j = 0 ; (j<(strlen(buffer)) && j < buff_size); j++ ){ if (buffer[j] == '&') break; if ((uint)buffer[j] == 226 || buffer[j] == '-' || (buffer[j]>='0' && buffer[j]<='9')){ if ((uint)buffer[j] == 226) sprintf(temp_buffer,"%s-",temp_buffer); else sprintf(temp_buffer,"%s%c",temp_buffer, buffer[j]); } } /* fprintf(stderr, " <temperature>%s</temperature>\n", temp_buffer); */ fprintf(file_out," <flike>%s</flike>\n", temp_buffer); } fprintf(file_out," </period>\n"); /* To DO sunrise and sunset */ xpathObj = xmlXPathEvalExpression("/html/body/div/div/table//tr[th]/*/text()", xpathCtx); /* Day weather forecast */ nodes = xpathObj->nodesetval; size = (nodes) ? nodes->nodeNr : 0; snprintf(buffer, sizeof(buffer)-1,"(/html/body/div/div/table//tr/th/text() | /html/body/div/div/table//tr/td[@class='in']/text() | /html/body/div/div/table//tr/td/span/text() | /html/body/div/div/table//tr/td/img/@alt | /html/body/div/div/table//tr/td/text() | /html/body/div/div/table//tr/td/img/@src )"); xpathObj2 = xmlXPathEvalExpression(buffer, xpathCtx); k = 0; for(i = 1; i < (size) ; ++i) { nodes = xpathObj2->nodesetval; flag = FALSE; for (j = 0; j <(nodes->nodeNr); ++j){ if (!flag && xpathObj2->nodesetval->nodeTab[j]->content && xpathObj->nodesetval->nodeTab[i]->content && !strcmp(xpathObj->nodesetval->nodeTab[i]->content, xpathObj2->nodesetval->nodeTab[j]->content)){ flag = TRUE; k = 0; } if (flag && i+1 < size && xpathObj2->nodesetval->nodeTab[j]->content && xpathObj->nodesetval->nodeTab[i+1]->content && !strcmp(xpathObj->nodesetval->nodeTab[i+1]->content, xpathObj2->nodesetval->nodeTab[j]->content)){ flag = FALSE; } if (flag){ switch (k){ case 1: { current_time = time(NULL); tm = localtime(¤t_time); snprintf(buffer, sizeof(buffer)-1,"%s %s", xpathObj->nodesetval->nodeTab[i]->content, xpathObj2->nodesetval->nodeTab[j]->content); fprintf(stderr," Buffer %s\n", buffer); setlocale(LC_TIME, "POSIX"); strptime((const char*)buffer, "%A %b %d %H:%M", &tmp_tm); setlocale(LC_TIME, ""); /* set begin of day in localtime */ tmp_tm.tm_year = tm->tm_year; t_start = mktime(&tmp_tm); fprintf(file_out," <period start=\"%li\"", (t_start)); /* set end of current time in localtime */ t_end = t_start + 3600*6 - 1; fprintf(file_out," end=\"%li\" hour=\"true\">\n", t_end); break; } case 2: { /* added temperature */ if (xpathObj2->nodesetval->nodeTab[j]->content){ /* fprintf (stderr, "temperature %s\n", xpathObj->nodesetval->nodeTab[0]->content); */ snprintf(buffer, sizeof(buffer)-1,"%s", xpathObj2->nodesetval->nodeTab[j]->content); memset(temp_buffer, 0, sizeof(temp_buffer)); for (l = 0 ; (l<(strlen(buffer)) && l < buff_size); l++ ){ if (buffer[l] == '&') break; if ((uint)buffer[l] == 226 || buffer[l] == '-' || (buffer[l]>='0' && buffer[l]<='9')){ if ((uint)buffer[l] == 226) sprintf(temp_buffer,"%s-",temp_buffer); else sprintf(temp_buffer,"%s%c",temp_buffer, buffer[l]); } } /* fprintf(stderr, " <temperature>%s</temperature>\n", temp_buffer); */ fprintf(file_out," <temperature>%s</temperature>\n", temp_buffer); } break; } case 4: { if (strlen(xpathObj2->nodesetval->nodeTab[j]->children->content)>0) fprintf(file_out," <wind_direction>%s</wind_direction>\n", xpathObj2->nodesetval->nodeTab[j]->children->content); break; } case 5: { if (xpathObj2->nodesetval->nodeTab[j] && xpathObj2->nodesetval->nodeTab[j]->content){ /* fprintf(stderr, "Wind direction %s \n", xpathObj4->nodesetval->nodeTab[i]->children->content); */ fprintf(file_out," <wind_speed>%s</wind_speed>\n", xpathObj2->nodesetval->nodeTab[j]->content); } break; } case 6: { if (strlen(xpathObj2->nodesetval->nodeTab[j]->children->content)>0){ temp_char = strrchr((char*)xpathObj2->nodesetval->nodeTab[j]->children->content, '/'); temp_char ++; fprintf(file_out," <icon>%s</icon>\n", choose_hour_weather_icon(hash_for_icons, temp_char)); } break; } case 7: { if (strlen(xpathObj2->nodesetval->nodeTab[j]->children->content)>0){ fprintf(file_out," <description>%s</description>\n", xpathObj2->nodesetval->nodeTab[j]->children->content); } k = 0; fprintf(file_out," </period>\n"); } } k++; } } } fclose(file_out); return 1; }
bool Project::parse(const std::string& filename) { xmlInitParser(); xmlDocPtr doc = xmlParseFile(filename.c_str()); if ( doc == NULL ) { std::cerr << "unable to parse file(" << filename << ")" << std::endl; return false; } /* Create xpath evaluation context */ xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc); if ( xpathCtx == NULL ) { std::cerr << "unable to create new XPath context" << std::endl; xmlFreeDoc(doc); return false; } /* Evaluate xpath expression */ xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(BAD_CAST "/grxui/mode/item", xpathCtx); if ( xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) ) { std::cerr << "unable to find <mode>" << std::endl; } int size; size = xpathObj->nodesetval->nodeNr; for (int i = 0; i < size; i++ ) { xmlNodePtr node = xpathObj->nodesetval->nodeTab[i]; //std::cerr << i << " class:" << xmlGetProp(node, (xmlChar *)"class") << std::endl; if ( xmlStrEqual( xmlGetProp(node, (xmlChar *)"class"), (xmlChar *)"com.generalrobotix.ui.item.GrxSimulationItem") ) { xmlNodePtr cur_node = node->children; while ( cur_node ) { if ( cur_node->type == XML_ELEMENT_NODE ) { if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"name"),(xmlChar *)"integrate") ) { m_kinematicsOnly = std::string((char *)(xmlGetProp(cur_node, (xmlChar *)"value"))) == "false"; } else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"name"),(xmlChar *)"viewsimulate") ) { } else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"name"),(xmlChar *)"totalTime") ) { m_totalTime = atof((char *)(xmlGetProp(cur_node, (xmlChar *)"value"))); } else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"name"),(xmlChar *)"timeStep") ) { m_timeStep = atof((char *)(xmlGetProp(cur_node, (xmlChar *)"value"))); } else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"name"),(xmlChar *)"realTime") ) { } else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"name"),(xmlChar *)"gravity") ) { m_gravity = atof((char *)(xmlGetProp(cur_node, (xmlChar *)"value"))); } else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"name"),(xmlChar *)"method") ) { m_isEuler = std::string((char *)(xmlGetProp(cur_node, (xmlChar *)"value"))) == std::string("EULER"); } else { #if 0 std::cerr << "Unknown tag : " << cur_node->name << " " << "name=" << xmlGetProp(cur_node, (xmlChar *)"name") << "value=" << xmlGetProp(cur_node, (xmlChar *)"value") << std::endl; #endif } } cur_node = cur_node->next; } } else if ( xmlStrEqual( xmlGetProp(node, (xmlChar *)"class"), (xmlChar *)"com.generalrobotix.ui.item.GrxRTSItem") ) { xmlNodePtr cur_node = node->children; while ( cur_node ) { if ( cur_node->type == XML_ELEMENT_NODE ) { std::string name = (char *)xmlGetProp(cur_node, (xmlChar *)"name"); std::string value = (char *)xmlGetProp(cur_node, (xmlChar *)"value"); //std::cout << name << "," << value << std::endl; int pos = name.find('.'); if (pos < 0){ std::cerr << "unknown property name:" << name << std::endl; }else{ std::string comp = name.substr(0,pos); std::string cat = name.substr(pos+1); if (cat == "factory"){ m_rts.components[comp].path = value; int pos = value.find_last_of("/"); m_rts.components[comp].name = value.substr(pos+1); }else if (cat == "period") { m_rts.components[comp].period = atof(value.c_str()); }else{ m_rts.connections.push_back(std::make_pair(name, value)); } } } cur_node = cur_node->next; } } else if ( xmlStrEqual( xmlGetProp(node, (xmlChar *)"class"), (xmlChar *)"com.generalrobotix.ui.item.GrxModelItem") ) { //std::cerr << "GrxModelItem name:" << xmlGetProp(node, (xmlChar *)"name") << ", url:" << xmlGetProp(node, (xmlChar *)"url") << std::endl; std::string path = "file://"; path += (char *)xmlGetProp(node, (xmlChar *)"url"); if ( path.find("$(CURRENT_DIR)") != std::string::npos ) { path.replace(path.find("$(CURRENT_DIR)"),14, filename.substr(0, filename.find_last_of("/"))); } if ( path.find("$(PROJECT_DIR)") != std::string::npos ) { std::string shdir = OPENHRP_SHARE_DIR; std::string pjdir = shdir + "/sample/project"; path.replace(path.find("$(PROJECT_DIR)"),14, pjdir); } ModelItem m; m.url = path; xmlNodePtr cur_node = node->children; while ( cur_node ) { if ( cur_node->type == XML_ELEMENT_NODE ) { if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"name"),(xmlChar *)"isRobot") ) { if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"value"),(xmlChar *)"true")) { //isRobot = true; } } else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"name"),(xmlChar *)"controlTime") ) { //controlTimeStep = atof((char *)(xmlGetProp(cur_node, (xmlChar *)"value"))); } else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"name"),(xmlChar *)"rtcName") ) { m.rtcName = (char *)xmlGetProp(cur_node, (xmlChar *)"value"); } else if ( std::string((char *)xmlGetProp(cur_node, (xmlChar *)"name")).rfind(".angle") != std::string::npos ) { std::string name = std::string((char *)xmlGetProp(cur_node, (xmlChar *)"name")); name.erase(name.rfind(".angle")); m.joint[name].angle = atof((char *)xmlGetProp(cur_node, (xmlChar *)"value")); } else if ( std::string((char *)xmlGetProp(cur_node, (xmlChar *)"name")).rfind(".mode") != std::string::npos ) { std::string name = std::string((char *)xmlGetProp(cur_node, (xmlChar *)"name")); name.erase(name.rfind(".mode")); m.joint[name].isHighGain = xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"value"), (xmlChar *)"HighGain"); } else if ( std::string((char *)xmlGetProp(cur_node, (xmlChar *)"name")).rfind(".translation") != std::string::npos ) { std::string name = std::string((char *)xmlGetProp(cur_node, (xmlChar *)"name")); name.erase(name.rfind(".translation")); float x, y, z; sscanf(((char *)xmlGetProp(cur_node, (xmlChar *)"value")),"%f %f %f", &x, &y, &z); m.joint[name].translation[0] = x; m.joint[name].translation[1] = y; m.joint[name].translation[2] = z; } else if ( std::string((char *)xmlGetProp(cur_node, (xmlChar *)"name")).rfind(".rotation") != std::string::npos ) { std::string name = std::string((char *)xmlGetProp(cur_node, (xmlChar *)"name")); name.erase(name.rfind(".rotation")); float x, y, z, w; sscanf(((char *)xmlGetProp(cur_node, (xmlChar *)"value")),"%f %f %f %f", &x, &y, &z, &w); hrp::calcRodrigues(m.joint[name].rotation, hrp::Vector3(x, y, z), w); } else { #if 0 std::cerr << "Unknown tag : " << cur_node->name << " " << "name=" << xmlGetProp(cur_node, (xmlChar *)"name") << " " << "value=" << xmlGetProp(cur_node, (xmlChar *)"value") << std::endl; #endif } } cur_node = cur_node->next; } std::string n = std::string((char *)xmlGetProp(node, (xmlChar *)"name")); m_models[n] = m; } else if ( xmlStrEqual( xmlGetProp(node, (xmlChar *)"class"), (xmlChar *)"com.generalrobotix.ui.item.GrxWorldStateItem") ) { xmlNodePtr cur_node = node->children; while ( cur_node ) { if ( cur_node->type == XML_ELEMENT_NODE ) { if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"name"),(xmlChar *)"logTimeStep") ) { //logTimeStep = atof((char *)(xmlGetProp(cur_node, (xmlChar *)"value"))); } else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"name"),(xmlChar *)"timeStep") ) { m_timeStep = atof((char *)(xmlGetProp(cur_node, (xmlChar *)"value"))); } else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"name"),(xmlChar *)"totalTime") ) { m_totalTime = atof((char *)(xmlGetProp(cur_node, (xmlChar *)"value"))); } else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"name"),(xmlChar *)"method") ) { m_isEuler = std::string((char *)(xmlGetProp(cur_node, (xmlChar *)"value"))) == std::string("EULER"); } } cur_node = cur_node->next; } } else if ( xmlStrEqual ( xmlGetProp(node, (xmlChar *)"class"), (xmlChar *)"com.generalrobotix.ui.item.GrxCollisionPairItem") ) { CollisionPairItem c; xmlNodePtr cur_node = node->children; while ( cur_node ) { if ( cur_node->type == XML_ELEMENT_NODE ) { if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"name"),(xmlChar *)"objectName1") ) { c.objectName1 = (char *)(xmlGetProp(cur_node, (xmlChar *)"value")); } else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"name"),(xmlChar *)"objectName2") ) { c.objectName2 = (char *)(xmlGetProp(cur_node, (xmlChar *)"value")); } else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"name"),(xmlChar *)"jointName1") ) { c.jointName1 = (char *)(xmlGetProp(cur_node, (xmlChar *)"value")); } else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"name"),(xmlChar *)"jointName2") ) { c.jointName2 = (char *)(xmlGetProp(cur_node, (xmlChar *)"value")); } else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"name"),(xmlChar *)"slidingFriction") ) { c.slidingFriction = atof((char *)(xmlGetProp(cur_node, (xmlChar *)"value"))); } else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"name"),(xmlChar *)"staticFriction") ) { c.staticFriction = atof((char *)(xmlGetProp(cur_node, (xmlChar *)"value"))); } else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"name"),(xmlChar *)"cullingThresh") ) { c.cullingThresh = atof((char *)(xmlGetProp(cur_node, (xmlChar *)"value"))); } else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)"name"),(xmlChar *)"sprintDamperModel") ) { c.sprintDamperModel = (char *)(xmlGetProp(cur_node, (xmlChar *)"value")); } else { #if 0 std::cerr << "Unknown tag : " << cur_node->name << " " << ", name=" << xmlGetProp(cur_node, (xmlChar *)"name") << ", value=" << xmlGetProp(cur_node, (xmlChar *)"value") << std::endl; #endif } } cur_node = cur_node->next; } m_collisionPairs.push_back(c); } } /* Cleanup Xpath Data */ xmlXPathFreeObject(xpathObj); xmlXPathFreeContext(xpathCtx); /* free the document */ xmlFreeDoc(doc); xmlCleanupParser(); return true; }
/** * \brief Prepare basic information needed to dynamically load intermediate plugins. * This information is get from the user configuration (given as parameter) and from * internal configuration of the ipfixmed. * * @param[in] config User XML configuration. * @param[in] internal_cfg internalcfg.xml file. * @return List of information about intermediate plugins, * NULL in case of error. */ struct plugin_xml_conf_list* get_intermediate_plugins(xmlDocPtr config, char *internal_cfg) { int i; xmlXPathContextPtr internal_ctxt = NULL; xmlXPathContextPtr config_ctxt = NULL; xmlXPathObjectPtr xpath_obj_ipinter = NULL; xmlXPathObjectPtr xpath_obj_core = NULL; struct plugin_xml_conf_list *plugins = NULL; struct plugin_xml_conf_list *aux_plugin = NULL; struct plugin_xml_conf_list *last_plugin = NULL; xmlNodePtr node; xmlNodePtr plugin_config_internal; xmlChar *plugin_file = NULL, *thread_name = NULL; xmlDocPtr xmldata = NULL; uint8_t hit = 0; /* initiate internal config - open xml file, get xmlDoc and prepare xpath context for it */ if ((internal_ctxt = ic_init(BAD_CAST "cesnet-ipfixcol-int", internal_cfg)) == NULL) { goto cleanup; } /* get the list of supported intermediate plugins description from internal config */ xpath_obj_ipinter = xmlXPathEvalExpression(BAD_CAST "/cesnet-ipfixcol-int:ipfixcol/cesnet-ipfixcol-int:intermediatePlugin", internal_ctxt); if (xpath_obj_ipinter == NULL || xmlXPathNodeSetIsEmpty(xpath_obj_ipinter->nodesetval)) { MSG_ERROR(msg_module, "No list of supported intermediate formats found in internal configuration"); goto cleanup; } /* create xpath evaluation context of user configuration */ if ((config_ctxt = xmlXPathNewContext(config)) == NULL) { MSG_ERROR(msg_module, "Unable to create XPath context for user configuration (%s:%d)", __FILE__, __LINE__); goto cleanup; } /* register namespace for the context of internal configuration file */ if (xmlXPathRegisterNs(config_ctxt, BAD_CAST "ietf-ipfix", BAD_CAST "urn:ietf:params:xml:ns:yang:ietf-ipfix-psamp") != 0) { MSG_ERROR(msg_module, "Unable to register namespace for user configuration (%s:%d)", __FILE__, __LINE__); goto cleanup; } /* look for <ipfixmedCore> */ xpath_obj_core = xmlXPathEvalExpression(BAD_CAST "/ietf-ipfix:ipfix/ietf-ipfix:intermediatePlugins", config_ctxt); if (xpath_obj_core == NULL || xmlXPathNodeSetIsEmpty(xpath_obj_core->nodesetval)) { MSG_INFO(msg_module, "No intermediate plugin configured in user configuration"); goto cleanup; } node = xpath_obj_core->nodesetval->nodeTab[0]->children; /* Loop over all nodes and skip comments */ while (node != NULL) { /* Skip processing this node in case it's a comment */ if (node->type == XML_COMMENT_NODE) { node = node->next; continue; } plugin_file = NULL; xmldata = NULL; thread_name = NULL; hit = 0; /* find internal configuration for this Intermediate plugin */ for (i = 0; i < xpath_obj_ipinter->nodesetval->nodeNr; i++) { plugin_config_internal = xpath_obj_ipinter->nodesetval->nodeTab[i]->children; while (plugin_config_internal) { if ((!xmlStrncmp(plugin_config_internal->name, BAD_CAST "name", strlen("name") + 1)) && (!xmlStrncmp(plugin_config_internal->children->content, node->name, xmlStrlen(node->name)))) { hit = 1; } if (!xmlStrncmp(plugin_config_internal->name, BAD_CAST "file", strlen("file") + 1)) { plugin_file = xmlNodeListGetString(plugin_config_internal->doc, plugin_config_internal->children, 1); } if (!xmlStrncmp(plugin_config_internal->name, BAD_CAST "threadName", strlen("threadName") + 1)) { thread_name = xmlNodeListGetString(plugin_config_internal->doc, plugin_config_internal->children, 1); } plugin_config_internal = plugin_config_internal->next; } if (!hit) { xmlFree(plugin_file); xmlFree(thread_name); plugin_file = NULL; continue; } break; } if (!plugin_file) { MSG_ERROR(msg_module, "Unable to load intermediate plugin; specification for '%s' could not be found", (char *) node->name); node = node->next; continue; } xmldata = xmlNewDoc(BAD_CAST "1.0"); xmlDocSetRootElement(xmldata, xmlCopyNode(node, 1)); if (!xmldata) { xmlFree(plugin_file); xmlFree(thread_name); node = node->next; continue; } aux_plugin = (struct plugin_xml_conf_list *) malloc(sizeof(*aux_plugin)); if (!aux_plugin) { MSG_ERROR(msg_module, "Memory allocation failed (%s:%d)", __FILE__, __LINE__); goto cleanup; } memset(aux_plugin, 0, sizeof(*aux_plugin)); aux_plugin->config.file = (char *) plugin_file; if (thread_name) { strncpy_safe(aux_plugin->config.name, (char*) thread_name, 16); xmlFree(thread_name); } else { strncpy_safe(aux_plugin->config.name, (char *) node->name, 16); } aux_plugin->config.xmldata = xmldata; if (plugins) { last_plugin->next = aux_plugin; } else { plugins = aux_plugin; } last_plugin = aux_plugin; node = node->next; } /* inform that everything was done but no valid plugin has been found */ if (plugins == NULL) { MSG_WARNING(msg_module, "No valid intermediate plugin specification found for mediator"); } cleanup: /* Cleanup of XPath data */ if (xpath_obj_core) { xmlXPathFreeObject(xpath_obj_core); } if (xpath_obj_ipinter) { xmlXPathFreeObject(xpath_obj_ipinter); } if (config_ctxt) { xmlXPathFreeContext(config_ctxt); } if (internal_ctxt) { xmlFreeDoc(internal_ctxt->doc); xmlXPathFreeContext(internal_ctxt); } return (plugins); }
/** * \brief Prepare basic information needed to dynamically load input plugin * specified to be the input of the specified \<collectingProcess\>. This * information is get from the user configuration (given as parameter) and from * internal configuration of the ipfixcol. * * @param[in] collector_node XML node with parameters for the particular * collectingProcess. This is part of user configuration file. * @param[in] internal_cfg internalcfg.xml file. * @return Information about first input plugin for the specified collector, * NULL in case of error. */ struct plugin_xml_conf_list* get_input_plugins(xmlNodePtr collector_node, char *internal_cfg) { int i, j; xmlChar *collector_name; xmlNodePtr auxNode = NULL, children1 = NULL, children2 = NULL, children3 = NULL; xmlXPathContextPtr internal_ctxt = NULL; xmlXPathObjectPtr xpath_obj_suppcolls = NULL, xpath_obj_file = NULL; struct plugin_xml_conf_list *retval = NULL; /* prepare return structure */ retval = (struct plugin_xml_conf_list *) malloc(sizeof(struct plugin_xml_conf_list)); if (retval == NULL) { MSG_ERROR(msg_module, "Memory allocation failed (%s:%d)", __FILE__, __LINE__); return (NULL); } retval->next = NULL; retval->config.xmldata = NULL; retval->config.file = NULL; /* initiate internal config - open xml file, get xmlDoc and prepare xpath context for it */ internal_ctxt = ic_init(BAD_CAST "cesnet-ipfixcol-int", internal_cfg); if (internal_ctxt == NULL) { free(retval); return (NULL); } /* * get the list of supported Collector types which will be used to get * collector information from the user configuration file */ xpath_obj_suppcolls = xmlXPathEvalExpression(BAD_CAST "/cesnet-ipfixcol-int:ipfixcol/cesnet-ipfixcol-int:supportedCollectors/cesnet-ipfixcol-int:name", internal_ctxt); if (xpath_obj_suppcolls == NULL || xmlXPathNodeSetIsEmpty(xpath_obj_suppcolls->nodesetval)) { MSG_ERROR(msg_module, "No list of supported collectors found in internal configuration"); free(retval); retval = NULL; goto cleanup; } /* get paths to libraries implementing plugins from internal configuration */ for (j = 0; j < xpath_obj_suppcolls->nodesetval->nodeNr; j++) { auxNode = get_children(collector_node, xpath_obj_suppcolls->nodesetval->nodeTab[j]->children->content); /* finnish the loop if the collector description found */ if (auxNode) { break; } } /* if we didn't found any valid collector description, we have to quit */ if (!auxNode) { MSG_ERROR(msg_module, "No valid collecting process description found"); free(retval); retval = NULL; goto cleanup; } /* remember node with input plugin parameters */ retval->config.xmldata = xmlNewDoc(BAD_CAST "1.0"); xmlDocSetRootElement(retval->config.xmldata, xmlCopyNode(auxNode, 1)); /* * remember filename of input plugin implementation */ /* we are looking for the node with this name node */ collector_name = xpath_obj_suppcolls->nodesetval->nodeTab[j]->children->content; /* first get list of inputPlugin nodes in internal configuration file */ xpath_obj_file = xmlXPathEvalExpression(BAD_CAST "/cesnet-ipfixcol-int:ipfixcol/cesnet-ipfixcol-int:inputPlugin", internal_ctxt); if (xpath_obj_file == NULL || xmlXPathNodeSetIsEmpty(xpath_obj_file->nodesetval)) { MSG_ERROR(msg_module, "No input plugin definition found in internal configuration"); free(retval); retval = NULL; goto cleanup; } /* and now select the one with required name element */ for (i = 0; i < xpath_obj_file->nodesetval->nodeNr; i++) { children1 = children2 = children3 = xpath_obj_file->nodesetval->nodeTab[i]->children; while (children1) { if ((!strncmp((char *) children1->name, "name", strlen ("name") + 1)) && (!xmlStrncmp(children1->children->content, collector_name, xmlStrlen (collector_name) + 1))) { /* find the processName of specified inputPlugin in internalcfg.xml */ while (children3) { if (!xmlStrncmp(children3->name, BAD_CAST "processName", strlen ("processName") + 1)) { strncpy_safe(retval->config.name, (char *) children3->children->content, 16); } children3 = children3->next; } /* find the file of specified inputPLugin in internalcfg.xml */ while (children2) { if (!xmlStrncmp(children2->name, BAD_CAST "file", strlen("file") + 1)) { retval->config.file = (char *) malloc(sizeof(char) * (strlen((char*) children2->children->content) + 1)); strncpy_safe(retval->config.file, (char *) children2->children->content, strlen((char *) children2->children->content) + 1); goto found_input_plugin_file; } children2 = children2->next; } } children1 = children1->next; } } found_input_plugin_file: if (retval->config.file == NULL) { MSG_ERROR(msg_module, "Unable to load input plugin; specification for '%s' could not be found", collector_name); free(retval); retval = NULL; } cleanup: /* Cleanup of XPath data */ if (xpath_obj_file) { xmlXPathFreeObject(xpath_obj_file); } if (xpath_obj_suppcolls) { xmlXPathFreeObject(xpath_obj_suppcolls); } if (internal_ctxt) { xmlFreeDoc(internal_ctxt->doc); xmlXPathFreeContext(internal_ctxt); } return (retval); }
static int noit_console_show_check(mtev_console_closure_t ncct, int argc, char **argv, mtev_console_state_t *state, void *closure) { int i, cnt; mtev_conf_t_userdata_t *info; char xpath[1024]; xmlXPathObjectPtr pobj = NULL; xmlXPathContextPtr xpath_ctxt = NULL; mtev_conf_xml_xpath(NULL, &xpath_ctxt); if(argc > 1) { nc_printf(ncct, "requires zero or one arguments\n"); return -1; } info = mtev_console_userdata_get(ncct, MTEV_CONF_T_USERDATA); /* We many not be in conf-t mode -- that's fine */ if(noit_console_mkcheck_xpath(xpath, sizeof(xpath), info, argc ? argv[0] : NULL)) { nc_printf(ncct, "could not find check '%s'\n", argv[0]); return -1; } pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt); if(!pobj || pobj->type != XPATH_NODESET || xmlXPathNodeSetIsEmpty(pobj->nodesetval)) { nc_printf(ncct, "no checks found\n"); goto out; } cnt = xmlXPathNodeSetGetLength(pobj->nodesetval); if(info && cnt != 1) { nc_printf(ncct, "Ambiguous check specified\n"); goto out; } for(i=0; i<cnt; i++) { mtev_hash_iter iter = MTEV_HASH_ITER_ZERO; const char *k; int klen; void *data; uuid_t checkid; noit_check_t *check; mtev_hash_table *config; xmlNodePtr node, anode, mnode = NULL; char *uuid_conf; char *module, *value; node = (mtev_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i); uuid_conf = (char *)xmlGetProp(node, (xmlChar *)"uuid"); if(!uuid_conf || uuid_parse(uuid_conf, checkid)) { nc_printf(ncct, "%s has invalid or missing UUID!\n", (char *)xmlGetNodePath(node) + strlen("/noit")); continue; } nc_printf(ncct, "==== %s ====\n", uuid_conf); xmlFree(uuid_conf); #define MYATTR(a,n,b) _mtev_conf_get_string(node, &(n), "@" #a, &(b)) #define INHERIT(a,n,b) \ _mtev_conf_get_string(node, &(n), "ancestor-or-self::node()/@" #a, &(b)) #define SHOW_ATTR(a) do { \ anode = NULL; \ value = NULL; \ INHERIT(a, anode, value); \ nc_attr_show(ncct, #a, node, anode, value); \ if(value != NULL) free(value); \ } while(0) if(!INHERIT(module, mnode, module)) module = NULL; if(MYATTR(name, anode, value)) { nc_printf(ncct, " name: %s\n", value); free(value); } else nc_printf(ncct, " name: %s [from module]\n", module ? module : "[undef]"); nc_attr_show(ncct, "module", node, mnode, module); if(module) free(module); SHOW_ATTR(target); SHOW_ATTR(seq); SHOW_ATTR(resolve_rtype); SHOW_ATTR(period); SHOW_ATTR(timeout); SHOW_ATTR(oncheck); SHOW_ATTR(filterset); SHOW_ATTR(disable); /* Print out all the config settings */ config = mtev_conf_get_hash(node, "config"); while(mtev_hash_next(config, &iter, &k, &klen, &data)) { nc_printf(ncct, " config::%s: %s\n", k, (const char *)data); } mtev_hash_destroy(config, free, free); free(config); check = noit_poller_lookup(checkid); if(!check) { nc_printf(ncct, " ERROR: not in running system\n"); } else { int idx = 0; stats_t *c; struct timeval *whence; mtev_hash_table *metrics; nc_printf(ncct, " target_ip: %s\n", check->target_ip); nc_printf(ncct, " currently: %08x ", check->flags); if(NOIT_CHECK_RUNNING(check)) { nc_printf(ncct, "running"); idx++; } if(NOIT_CHECK_KILLED(check)) nc_printf(ncct, "%skilled", idx++?",":""); if(!NOIT_CHECK_CONFIGURED(check)) nc_printf(ncct, "%sunconfig", idx++?",":""); if(NOIT_CHECK_DISABLED(check)) nc_printf(ncct, "%sdisabled", idx++?",":""); if(!idx) nc_printf(ncct, "idle"); nc_write(ncct, "\n", 1); if (check->fire_event != NULL) { struct timeval now, diff; mtev_gettimeofday(&now, NULL); sub_timeval(check->fire_event->whence, now, &diff); nc_printf(ncct, " next run: %0.3f seconds\n", diff.tv_sec + (diff.tv_usec / 1000000.0)); } else { nc_printf(ncct, " next run: unscheduled\n"); } c = noit_check_get_stats_current(check); whence = noit_check_stats_whence(c, NULL); if(whence->tv_sec == 0) { nc_printf(ncct, " last run: never\n"); } else { const char *status; struct timeval now, *then, diff; mtev_gettimeofday(&now, NULL); then = noit_check_stats_whence(c, NULL); sub_timeval(now, *then, &diff); nc_printf(ncct, " last run: %0.3f seconds ago\n", diff.tv_sec + (diff.tv_usec / 1000000.0)); nc_printf(ncct, " availability/state: %s/%s\n", noit_check_available_string(noit_check_stats_available(c, NULL)), noit_check_state_string(noit_check_stats_state(c, NULL))); status = noit_check_stats_status(c, NULL); nc_printf(ncct, " status: %s\n", status); nc_printf(ncct, " feeds: %d\n", check->feeds ? check->feeds->size : 0); } c = noit_check_get_stats_inprogress(check); metrics = noit_check_stats_metrics(c); if(mtev_hash_size(metrics) > 0) { nc_printf(ncct, " metrics (inprogress):\n"); nc_print_stat_metrics(ncct, check, c); } c = noit_check_get_stats_current(check); metrics = noit_check_stats_metrics(c); if(mtev_hash_size(metrics)) { nc_printf(ncct, " metrics (current):\n"); nc_print_stat_metrics(ncct, check, c); } c = noit_check_get_stats_previous(check); metrics = noit_check_stats_metrics(c); if(mtev_hash_size(metrics) > 0) { nc_printf(ncct, " metrics (previous):\n"); nc_print_stat_metrics(ncct, check, c); } } } out: if(pobj) xmlXPathFreeObject(pobj); return 0; }
trans_global_vec macFlowJoWorkspace::getGlobalTrans(){ trans_global_vec tgVec; string path="/Workspace/CalibrationTables/Table"; xmlXPathContextPtr context = xmlXPathNewContext(doc); xmlXPathObjectPtr result = xmlXPathEval((xmlChar *)path.c_str(), context); if(xmlXPathNodeSetIsEmpty(result->nodesetval)) { COUT<<"no calibration Tables found!"<<endl; return(tgVec); } /* * during the traversing of the calibration table list, * we try to split these tables into groups by their prefix names(which should match the compensation names defined in CompensationMatrices node) */ for(int i=0;i<result->nodesetval->nodeNr;i++) { wsNode calTblNode(result->nodesetval->nodeTab[i]); transformation *curTran=new transformation(); calibrationTable caltbl("flowJo",2); string tname=calTblNode.getProperty("name"); if(tname.empty()) throw(domain_error("empty name for calibration table")); if(g_loglevel>=GATING_SET_LEVEL) COUT<<"parsing calibrationTable:"<<tname<<endl; /* * parse the string from tname to extract channel name */ size_t nPrefix=tname.find("<"); size_t nsuffix=tname.find(">"); bool isGeneric=(nPrefix==string::npos)|(nsuffix==string::npos); string transGroupName; if(isGeneric) { /* * generic cal table (non-channel-specific) */ curTran->setName(tname); curTran->setChannel("*"); transGroupName="Generic"; } else { /* * channel-specific cal table */ curTran->setName(boost::trim_copy((tname.substr(0,nPrefix)))); curTran->setChannel(tname.substr(nPrefix,tname.length()-nPrefix)); transGroupName=curTran->getName(); } string sTbl=calTblNode.getContent(); /* * parse the stream to x,y double arrays */ valarray<double> tbl(toArray(sTbl)); unsigned nX=tbl.size()/2; caltbl.init(nX); caltbl.setY(tbl[slice(0,nX,2)]); caltbl.setX(tbl[slice(1,nX,2)]); curTran->setCalTbl(caltbl); /*since it is base class of transformation,which means the caltbl is already given by workspace * no need to compute later on. so set this flag to be true to assure the subsequent interpolation can be performed */ curTran->setComputeFlag(true); /*Find the respective reference(iterator) by name from the trans_global_vec * If not found,push back a new entry in the vector and return its reference */ trans_global_vec::iterator tRes=findTransGroup(tgVec,curTran->getName()); if(tRes==tgVec.end())//if not exsit yet, then push back the new instance { if(g_loglevel>=GATING_SET_LEVEL) COUT<<"creating new transformation group:"<<transGroupName<<endl; trans_global newTg; newTg.setGroupName(transGroupName); tgVec.push_back(newTg); tgVec.back().addTrans(curTran->getChannel(),curTran); } else //if already exists, then save the current transformation into the respective transGroup tRes->addTrans(curTran->getChannel(),curTran); } xmlXPathFreeObject(result); xmlXPathFreeContext(context); return tgVec; }
bool XPathNodeIterator::MoveNext() { if (!m_p || xmlXPathNodeSetIsEmpty(m_p->nodesetval) || m_i>=m_p->nodesetval->nodeNr-1) return false; ++m_i; return true; }
static gboolean fetch_tweets(gpointer GOL_UNUSED_ARG(data)) { if (!enable) return FALSE; CURL* curl = NULL; CURLcode res = CURLE_OK; long http_status = 0; MEMFILE* mbody = NULL; char* body = NULL; xmlDocPtr doc = NULL; xmlNodeSetPtr nodes = NULL; xmlXPathContextPtr ctx = NULL; xmlXPathObjectPtr path = NULL; mbody = memfopen(); curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); curl_easy_setopt(curl, CURLOPT_URL, "https://api.twitter.com/1/statuses/public_timeline.xml"); curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, REQUEST_TIMEOUT); curl_easy_setopt(curl, CURLOPT_TIMEOUT, REQUEST_TIMEOUT); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, memfwrite); curl_easy_setopt(curl, CURLOPT_WRITEDATA, mbody); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); res = curl_easy_perform(curl); if (res == CURLE_OK) curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &http_status); curl_easy_cleanup(curl); body = memfstrdup(mbody); memfclose(mbody); if (res != CURLE_OK || http_status != 200) goto leave; doc = body ? xmlParseDoc((xmlChar*) body) : NULL; if (!doc) goto leave; ctx = xmlXPathNewContext(doc); if (!ctx) goto leave; path = xmlXPathEvalExpression((xmlChar*)"/statuses/status", ctx); if (!path || xmlXPathNodeSetIsEmpty(path->nodesetval)) goto leave; nodes = path->nodesetval; const size_t length = xmlXPathNodeSetGetLength(nodes); gchar* first_id = NULL; for (size_t n = 0; n < length; n++) { char* id = NULL; char* user_id = NULL; char* icon = NULL; char* user_name = NULL; char* text = NULL; xmlNodePtr status = nodes->nodeTab[n]; if (status->type != XML_ATTRIBUTE_NODE && status->type != XML_ELEMENT_NODE && status->type != XML_CDATA_SECTION_NODE) continue; status = status->children; while(status) { if (!strcmp("id", (char*) status->name)) id = (char*) status->children->content; if (!strcmp("text", (char*) status->name)) { if (status->children) text = (char*) status->children->content; } /* user nodes */ if (!strcmp("user", (char*) status->name)) { xmlNodePtr user = status->children; while(user) { if (!strcmp("id", (char*) user->name)) user_id = XML_CONTENT(user); if (!strcmp("screen_name", (char*) user->name)) user_name = XML_CONTENT(user); if (!strcmp("profile_image_url", (char*) user->name)) { icon = (char*) g_strchug(g_strchomp((gchar*) XML_CONTENT(user))); } user = user->next; } } status = status->next; } if (!first_id) first_id = id; if (id && last_id && !strcmp(id, last_id)) break; if (text && user_id) { NOTIFICATION_INFO* ni = g_new0(NOTIFICATION_INFO, 1); ni->title = g_strdup(user_name); ni->text = g_strdup(text); ni->icon = g_strdup(icon); g_timeout_add(1000 * (n+1), delay_show, ni); } } if (last_id) g_free(last_id); if (first_id) last_id = g_strdup(first_id); leave: free(body); if (path) xmlXPathFreeObject(path); if (ctx) xmlXPathFreeContext(ctx); if (doc) xmlFreeDoc(doc); g_timeout_add(1000 * length, fetch_tweets, NULL); return FALSE; }
static void apedax_get_channels_data(unzFile uFile, guchar *scanXmlContent, gsize contentSize, const gchar *filename, GwyContainer *container, GwyContainer *meta, const APEScanSize *scanSize, GError **error) { xmlDocPtr doc = NULL; xmlNodePtr cur = NULL; xmlXPathContextPtr context; xmlXPathObjectPtr pathObj; xmlNodeSetPtr nodeset; xmlChar *buffer = NULL; gchar key[256]; gint i; gint power10 = 0; gdouble scaleFactor = 1.0; GwySIUnit *zUnit; gchar *zUnitString = NULL; gchar *binFileName = NULL; GwyDataField *dfield; GwyContainer *tmp; if (scanXmlContent == NULL || contentSize == 0) return; gwy_clear(key, sizeof(key)); doc = xmlReadMemory(scanXmlContent, contentSize, "scan.xml", NULL, 0); if (doc == NULL) goto fail; context = xmlXPathNewContext(doc); if (context == NULL) goto fail; pathObj = xmlXPathEvalExpression("/Scan/Channels/Channel", context); if (pathObj == NULL) { xmlXPathFreeContext(context); goto fail; } /*There must be at least one channel*/ if (xmlXPathNodeSetIsEmpty(pathObj->nodesetval)) { xmlXPathFreeObject(pathObj); xmlXPathFreeContext(context); err_NO_DATA(error); return; } nodeset = pathObj->nodesetval; if (nodeset->nodeNr <= 0) goto fail; for (i = 0; i < nodeset->nodeNr; i++) { cur = nodeset->nodeTab[i]->xmlChildrenNode; while (cur) { /*Label*/ if (gwy_strequal((gchar*)cur->name, "Label")) { buffer = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); g_snprintf(key, sizeof(key), "/%d/data/title", i); gwy_container_set_string_by_name(container, key, g_strdup(buffer)); xmlFree(buffer); } /*Factor*/ if (gwy_strequal((gchar*)cur->name, "ConversionFactor")) { buffer = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); scaleFactor = g_ascii_strtod((gchar*)buffer, NULL); xmlFree(buffer); } /*Unit*/ if (gwy_strequal((gchar*)cur->name, "DataUnit")) { buffer = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); zUnitString = g_strdup((gchar*)buffer); zUnit = gwy_si_unit_new_parse(zUnitString, &power10); xmlFree(buffer); g_object_unref(zUnit); } /*Binary file name*/ if (gwy_strequal((gchar*)cur->name, "BINFile")) { buffer = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); binFileName = g_strdup((gchar*)buffer); xmlFree(buffer); } cur = cur->next; } scaleFactor *= pow(10.0, power10); dfield = apedax_get_data_field(uFile, binFileName, scanSize, zUnitString, scaleFactor, error); if (dfield) { g_snprintf(key, sizeof(key), "/%d/data", i); gwy_container_set_object_by_name(container, key, dfield); g_object_unref(dfield); gwy_file_channel_import_log_add(container, i, NULL, filename); tmp = gwy_container_duplicate(meta); g_snprintf(key, sizeof(key), "/%d/meta", i); gwy_container_set_object_by_name(container, key, tmp); g_object_unref(tmp); } } xmlXPathFreeObject(pathObj); xmlXPathFreeContext(context); xmlFreeDoc(doc); return; fail: err_FILE_TYPE(error, FILE_TYPE); if (doc) xmlFreeDoc(doc); return; }
char * read_scheda(char *baseUrl, xmlChar *url, char *epgdb_root) { static char buf[2048]; int i; FILE *fd; char cachefile[strlen(epgdb_root) + strlen((char*)url) + 2]; htmlDocPtr docScheda = NULL; xmlXPathContextPtr contextScheda = NULL; xmlXPathObjectPtr par = NULL; xmlChar *urlScheda = NULL; /* build cache filename */ buf[0]='\0'; strcpy(cachefile, epgdb_root); cachefile[strlen(epgdb_root)] = '/'; for (i=0; i<strlen((char*)url); i++) if (url[i] == '/' || url[i] == '\\' || url[i] == '?' || url[i] == '&' || url[i] == '=') cachefile[i+strlen(epgdb_root)+1] = '_'; else cachefile[i+strlen(epgdb_root)+1] = url[i]; cachefile[i+strlen(epgdb_root)+1] = '\0'; /* try to read from cache */ fd = fopen(cachefile, "r"); if (fd) { fread(buf, 2048, 1, fd); fclose(fd); return buf; } /* ok... no cache... download it! */ urlScheda = xmlBuildURI(url, (xmlChar *)baseUrl); if (urlScheda != NULL ) { docScheda = htmlReadFile((char *)urlScheda, NULL, HTML_PARSE_RECOVER|HTML_PARSE_NOERROR|HTML_PARSE_NOWARNING); if (docScheda != NULL ) { contextScheda = xmlXPathNewContext(docScheda); if (contextScheda != NULL) { // Prende il primo paragrafo sotto la div con id="box" par = xmlXPathEvalExpression((const xmlChar *)"//div[@id='box']/p[1]", contextScheda); if (par != NULL && !xmlXPathNodeSetIsEmpty(par->nodesetval)) { append_scheda(buf, par->nodesetval->nodeTab[0]->children, 0, 2048); xmlXPathFreeObject(par); } xmlXPathFreeContext(contextScheda); } xmlFreeDoc(docScheda); } xmlFree(urlScheda); } /* save the cache */ if (strlen(buf) > 0) { fd = fopen(cachefile, "w"); if (fd) { fwrite(buf, strlen(buf)+1, 1, fd); fclose(fd); } } return buf; }
int getservercfg(void* lpxmlDoc,NetCfg* lpcfg) { xmlDocPtr pxmlDoc; xmlNodePtr cur; xmlNodePtr curpath; xmlXPathContextPtr context = NULL; xmlXPathObjectPtr result = NULL; xmlNodeSetPtr nodeset; xmlAttrPtr attrPtr; xmlChar *key; xmlChar *xpath = BAD_CAST("/application/server"); pxmlDoc = (xmlDocPtr)lpxmlDoc; cur = xmlDocGetRootElement(pxmlDoc); if (cur == NULL) { printf("Can't get the root element\n"); return 0; } context = xmlXPathNewContext(pxmlDoc); result = xmlXPathEvalExpression(xpath, context); xmlXPathFreeContext(context); if (result != NULL) { if (xmlXPathNodeSetIsEmpty(result->nodesetval) == 0) { nodeset = result->nodesetval; if(nodeset->nodeNr > 0) { curpath = nodeset->nodeTab[0]; if(curpath != NULL) { attrPtr = curpath->properties; while (attrPtr != NULL) { if (!xmlStrcmp(attrPtr->name, BAD_CAST "ip")) { key = xmlGetProp(curpath,(const xmlChar *)"ip"); printf("ip in xpath: %s\n", key); strcpy(lpcfg->szServeraddress,(char*)key); xmlFree(key); } else if(xmlStrcmp(attrPtr->name,BAD_CAST"port") == 0) { key = xmlGetProp(curpath,(const xmlChar *)"port"); printf("port in xpath: %s\n", key); lpcfg->nport = atoi((char*)key); xmlFree(key); } attrPtr = attrPtr->next; } } } } xmlXPathFreeObject(result); } return 1; }
// Add other such functions for other repository types. int Repository::get_index(string server_url, PACKAGE_LIST *packages, unsigned int type) { if (actionBus._abortActions) { actionBus._abortComplete=true; actionBus.setActionState(ACTIONID_DBUPDATE, ITEMSTATE_ABORTED); return MPKGERROR_ABORTED; } currentStatus = "Updating data from "+ server_url+"..."; mDebug("get_index!"); // First: detecting repository type // Trying to download in this order (if successful, we have detected a repository type): // 1. packages.xml.gz (Native MOPSLinux) // 2. PACKAGES.TXT (Legacy Slackware) // 3. Packages.gz (Debian) // (and something else for RPM, in future) string index_filename = get_tmp_file(); string md5sums_filename = get_tmp_file(); if (!dialogMode) say("[%s] ...\r",server_url.c_str()); string cm = "gunzip -f "+index_filename+".gz 2>/dev/null"; if (type == TYPE_MPKG || type == TYPE_AUTO) { actionBus.getActionState(0); mDebug("trying MPKG, type = "+ IntToStr(type)); if (CommonGetFile(server_url + "packages.xml.gz", index_filename+".gz")==DOWNLOAD_OK) { actionBus.getActionState(0); mDebug("download ok, validating contents..."); if (system(cm.c_str())==0 && \ ReadFile(index_filename).find("<?xml version=\"1.0\"")!=std::string::npos && ReadFile(index_filename).find("<repository")!=std::string::npos) { currentStatus = "Detected native MPKG repository"; type = TYPE_MPKG; } } } if (actionBus._abortActions) { actionBus._abortComplete=true; actionBus.setActionState(ACTIONID_DBUPDATE, ITEMSTATE_ABORTED); return MPKGERROR_ABORTED; } if (type == TYPE_SLACK || type == TYPE_AUTO) { mDebug("trying SLACK, type = "+ IntToStr(type)); if (CommonGetFile(server_url + "PACKAGES.TXT", index_filename)==DOWNLOAD_OK) { mDebug("download ok, validating contents..."); if (ReadFile(index_filename).find("PACKAGE NAME: ")!=std::string::npos) { currentStatus = _("Detected legacy Slackware repository"); if (CommonGetFile(server_url + "CHECKSUMS.md5", md5sums_filename) == DOWNLOAD_OK) { type = TYPE_SLACK; } else { mError(_("Error downloading checksums")); return -1; // Download failed: no checksums or checksums download error } } } } if (actionBus._abortActions) { actionBus._abortComplete=true; actionBus.setActionState(ACTIONID_DBUPDATE, ITEMSTATE_ABORTED); return MPKGERROR_ABORTED; } if (type == TYPE_DEBIAN || type == TYPE_AUTO) { if(CommonGetFile(server_url + "Packages.gz", index_filename+".gz")==DOWNLOAD_OK) { type = TYPE_DEBIAN; } } if (type != TYPE_MPKG && type != TYPE_SLACK && type!=TYPE_DEBIAN) { currentStatus = _("Error updating data from ") +server_url+_(": download error or unsupported type"); mError(_("Error downloading package index: download error, or unsupported repository type")); return -1; } mDebug("Starting to parse index"); PACKAGE *pkg = new PACKAGE; string xml_name=index_filename; // XMLNode *repository_root = new XMLNode; xmlDocPtr indexDoc; xmlNodePtr indexRootNode; // int pkg_count; int ret=0; currentStatus = "["+server_url+"] Importing data..."; if (actionBus._abortActions) { actionBus._abortComplete=true; actionBus.setActionState(ACTIONID_DBUPDATE, ITEMSTATE_ABORTED); return MPKGERROR_ABORTED; } string *pList = new string; string *mList = new string; //XMLNode *tmp = new XMLNode; //xmlDocPtr indexDoc; //xmlNodePtr indexRootNode; switch(type) { case TYPE_MPKG: indexDoc = xmlReadFile(xml_name.c_str(), "UTF-8", 0); if (indexDoc == NULL) { xmlFreeDoc(indexDoc); mError("ппц..."); return -1; } else mDebug("indexDoc read successfully"); indexRootNode = xmlDocGetRootElement(indexDoc); if (indexRootNode == NULL) { mError(_("Failed to get index")); xmlFreeDoc(indexDoc); } else mDebug("indexRootNode read successfully"); if (xmlStrcmp(indexRootNode->name, (const xmlChar *) "repository") ) { mError(_("Invalid index file")); xmlFreeDoc(indexDoc); } else mDebug("Found valid repository index"); xmlXPathContextPtr xContext; xmlXPathObjectPtr xResult; xContext = xmlXPathNewContext(indexDoc); if (xContext == NULL) { mError("ппц"); } xResult = xmlXPathEvalExpression((const xmlChar *)"/repository/package", xContext); if (xResult == NULL) { mError("XPath expression error"); } if (xmlXPathNodeSetIsEmpty(xResult->nodesetval)) { xmlXPathFreeObject(xResult); printf(_("[%s] ... Nothing found\n"), server_url.c_str()); //mError("No packages found"); return 0; } xmlNodeSetPtr xNodeSet; int xi; actionBus.setActionProgress(ACTIONID_DBUPDATE, 0); xNodeSet = xResult->nodesetval; xmlXPathFreeContext(xContext); actionBus.setActionProgressMaximum(ACTIONID_DBUPDATE, xNodeSet->nodeNr); if (xNodeSet->nodeNr==0) printf("[%s] ... Nothing found", server_url.c_str()); for (xi = 0; xi < xNodeSet->nodeNr; xi++) { printf("[%s] ... Importing received data: %d/%d\r",server_url.c_str(), xi+1, xNodeSet->nodeNr); actionBus.setActionProgress(ACTIONID_DBUPDATE, xi); mDebug("Processing " + IntToStr(xi) + " node"); if (actionBus._abortActions) { actionBus._abortComplete = true; actionBus.setActionState(ACTIONID_DBUPDATE, ITEMSTATE_ABORTED); return MPKGERROR_ABORTED; } actionBus.setActionProgress(ACTIONID_DBUPDATE, xi); pkg->clear(); mDebug("Calling xml2Package"); if (xml2package(xNodeSet->nodeTab[xi], pkg)<0) { mError("Failed to parse"); abort(); } else mDebug("xml2package OK"); // Adding location data pkg->get_locations()->at(0).set_server_url(&server_url); packages->add(pkg); } printf("\n"); xmlCleanupMemory(); xmlCleanupParser(); /* *repository_root=XMLNode::openFileHelper(xml_name.c_str(), "repository"); pkg_count=repository_root->nChildNode("package"); if (pkg_count==0) { mError("Repository has no packages\n"); delete pkg; return 0; } actionBus.setActionProgress(ACTIONID_DBUPDATE, 0); for (int i=0; i<pkg_count; i++) { if (actionBus._abortActions) { actionBus._abortComplete=true; actionBus.setActionState(ACTIONID_DBUPDATE, ITEMSTATE_ABORTED); return MPKGERROR_ABORTED; } actionBus.setActionProgress(ACTIONID_DBUPDATE, i); pkg->clear(); *tmp = repository_root->getChildNode("package", i); xml2package(tmp, pkg); // Adding location data pkg->get_locations()->at(0).set_server_url(&server_url); packages->add(pkg); }*/ //delete tmp; break; case TYPE_SLACK: *pList = ReadFile(index_filename); *mList = ReadFile(md5sums_filename); ret = slackpackages2list(pList, mList, packages, server_url); if (pList!=NULL) delete pList; if (mList!=NULL) delete mList; break; case TYPE_DEBIAN: break; default: break; } delete pkg; return ret; }
char *xml_query_by_id(char *xmlFile, char *xPath, int id) { xmlDocPtr doc; xmlParserCtxtPtr pctxt; xmlXPathContextPtr context; xmlXPathObjectPtr op; xmlNodeSetPtr nodeset; char *data = NULL; int num; if ((xmlFile == NULL) || (strlen(xmlFile) < 1)) return NULL; if (xmlFile[0] != '<') { if (access(xmlFile, R_OK) != 0) { fprintf(stderr, "Error: File %s doesn't exist or is not accessible for reading.\n", xmlFile); return NULL; } doc = xmlParseFile(xmlFile); } else { pctxt = xmlCreateDocParserCtxt((xmlChar *)xmlFile); doc = xmlCtxtReadDoc(pctxt, (xmlChar *)xmlFile, NULL, NULL, XML_PARSE_NOWARNING | XML_PARSE_NOERROR); /* A little hack, but working */ do { char tmp[1024] = { 0 }; char *dtmp = get_datetime(); snprintf(tmp, sizeof(tmp), "[%s ", dtmp); free(dtmp); dtmp=NULL; } while (0); } context = xmlXPathNewContext(doc); if (context == NULL) { DPRINTF("Error in xmlXPathNewContext\n"); return NULL; } DPRINTF("Trying to access xPath node %s (pos %d)\n", xPath, id); op = xmlXPathEvalExpression( (xmlChar *)xPath, context); xmlXPathFreeContext(context); if (op == NULL) { DPRINTF("Error in xmlXPathEvalExpression\n"); return NULL; } if(xmlXPathNodeSetIsEmpty(op->nodesetval)){ xmlXPathFreeObject(op); DPRINTF("No result\n"); return NULL; } nodeset = op->nodesetval; num = nodeset->nodeNr; #if 0 for (i = 0; i < num; i++) { data = (char *)xmlNodeListGetString(doc, (nodeset->nodeTab[i])->xmlChildrenNode, 1); DPRINTF("%d. >>> %s\n", i, data); } #endif DPRINTF("Current num value is %d, id value is %d\n", num, id); if (num > id) { char *tmp = (char *)xmlNodeListGetString(doc, (nodeset->nodeTab[id])->xmlChildrenNode, 1); data = strdup(tmp); DPRINTF("Got data element of '%s'\n", tmp); } else DPRINTF("Trying to access element out of bounds (id > num)\n"); xmlXPathFreeObject(op); xmlFreeDoc(doc); xmlCleanupParser(); return data; }
gboolean loadConfig(const gchar *filename, CfgFile *lcfg) { /* Parse hosts.xml document. */ xmlDocPtr xcfg = xmlParseFile(filename); if(xcfg == NULL) return(FALSE); /* Handle Xincludes. */ xmlSetGenericErrorFunc(NULL, xmlErrIgnoreHandler); xmlXIncludeProcess(xcfg); handleXMLError( xmlGetLastError() ); xmlSetGenericErrorFunc(NULL, NULL); /* Validate against DTD. */ xmlValidCtxtPtr xval = xmlNewValidCtxt(); if(xmlValidateDocument(xval, xcfg) == 0) { xmlFreeValidCtxt(xval); return(FALSE); } xmlFreeValidCtxt(xval); /* Allocate XPath context. */ xmlXPathContextPtr xctx = xmlXPathNewContext(xcfg); if(!xctx) { g_error("%s: xmlXPathNewContext failed!\n", filename); return(FALSE); } xmlNodePtr s_ssh[2] = {getXNode(xctx, "/apt-dater/ssh"), NULL}; xmlNodePtr s_path[2] = {getXNode(xctx, "/apt-dater/paths"), NULL}; #ifdef FEAT_TMUX xmlNodePtr s_tmux[2] = {getXNode(xctx, "/apt-dater/tmux"), NULL}; #else xmlNodePtr s_screen[2] = {getXNode(xctx, "/apt-dater/screen"), NULL}; #endif xmlNodePtr s_appearance[2] = {getXNode(xctx, "/apt-dater/appearance"), NULL}; xmlNodePtr s_notify[2] = {getXNode(xctx, "/apt-dater/notify"), NULL}; xmlNodePtr s_hooks[2] = {getXNode(xctx, "/apt-dater/hooks"), NULL}; #ifdef FEAT_AUTOREF xmlNodePtr s_autoref[2] = {getXNode(xctx, "/apt-dater/auto-ref"), NULL}; #endif #ifdef FEAT_HISTORY xmlNodePtr s_history[2] = {getXNode(xctx, "/apt-dater/history"), NULL}; #endif #ifdef FEAT_TCLFILTER xmlNodePtr s_tclfilter[2] = {getXNode(xctx, "/apt-dater/tcl-filter"), NULL}; #endif lcfg->ssh_optflags = getXPropStr(s_ssh, "opt-cmd-flags", "-t"); lcfg->ssh_cmd = getXPropStr(s_ssh, "cmd", "/usr/bin/ssh"); lcfg->sftp_cmd = getXPropStr(s_ssh, "sftp-cmd", "/usr/bin/sftp"); lcfg->umask = getXPropInt(s_path, "umask", S_IRWXG | S_IRWXO); umask(lcfg->umask); lcfg->hostsfile = getXPropStr(s_path, "hosts-file", g_strdup_printf("%s/%s/%s", g_get_user_config_dir(), PROG_NAME, "hosts.xml")); lcfg->statsdir = getXPropStr(s_path, "stats-dir", g_strdup_printf("%s/%s/%s", g_get_user_cache_dir(), PROG_NAME, "stats")); if(g_mkdir_with_parents(lcfg->statsdir, S_IRWXU | S_IRWXG) == -1) { g_warning("Failed to create %s: %s", lcfg->statsdir, g_strerror(errno)); exit(1); } #ifdef FEAT_TMUX lcfg->tmuxconffile = getXPropStr(s_tmux, "conf-file", g_strdup_printf("%s/%s/%s", g_get_user_config_dir(), PROG_NAME, "tmux.conf")); lcfg->tmuxsockpath = getXPropStr(s_tmux, "socket-path", g_strdup_printf("%s/%s/%s", g_get_user_cache_dir(), PROG_NAME, "tmux")); if(g_mkdir_with_parents(lcfg->tmuxsockpath, S_IRWXU | S_IRWXG) == -1) { g_warning("Failed to create %s: %s", lcfg->tmuxsockpath, g_strerror(errno)); exit(1); } #else lcfg->screenrcfile = getXPropStr(s_screen, "rc-file", g_strdup_printf("%s/%s/%s", g_get_user_config_dir(), PROG_NAME, "screenrc")); lcfg->screentitle = getXPropStr(s_screen, "title", g_strdup("%m # %U%H")); #endif lcfg->ssh_agent = getXPropBool(s_ssh, "spawn-agent", FALSE); xmlNodeSetPtr s_addkeys = getXNodes(xctx, "/apt-dater/ssh/add-key"); if(!xmlXPathNodeSetIsEmpty(s_addkeys)) { lcfg->ssh_add = g_new0(char*, s_addkeys->nodeNr + 1); int i; for(i = 0; i < s_addkeys->nodeNr; i++) { lcfg->ssh_add[i] = g_strdup((gchar *)xmlGetProp(s_addkeys->nodeTab[i], BAD_CAST("name"))); xmlChar *c = xmlGetProp(s_addkeys->nodeTab[i], BAD_CAST("fn")); if(!c) { g_printerr(_("Empty SSH key filename (%s/@fn) in configuration."), xmlGetNodePath(s_addkeys->nodeTab[i])); exit(1); } lcfg->ssh_add[i] = g_strdup((gchar *)c); } lcfg->ssh_numadd = s_addkeys->nodeNr; }
static int noit_console_check(noit_console_closure_t ncct, int argc, char **argv, noit_console_state_t *state, void *closure) { int cnt; noit_conf_t_userdata_t *info; char xpath[1024], newuuid_str[37]; char *uuid_conf, *wanted; uuid_t checkid; xmlXPathContextPtr xpath_ctxt = NULL; xmlXPathObjectPtr pobj = NULL; xmlNodePtr node = NULL; noit_boolean creating_new = noit_false; if(closure) { char *fake_argv[1] = { ".." }; noit_console_state_pop(ncct, 0, argv, NULL, NULL); noit_console_config_cd(ncct, 1, fake_argv, NULL, NULL); } noit_conf_xml_xpath(NULL, &xpath_ctxt); if(argc < 1) { nc_printf(ncct, "requires at least one argument\n"); return -1; } if(argc % 2 == 0) { nc_printf(ncct, "wrong number of arguments\n"); return -1; } info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA); wanted = strcmp(argv[0], "new") ? argv[0] : NULL; if(info && !wanted) { /* We are creating a new node */ uuid_t out; creating_new = noit_true; if(strncmp(info->path, "/checks/", strlen("/checks/")) && strcmp(info->path, "/checks")) { nc_printf(ncct, "New checks must be under /checks/\n"); return -1; } if(noit_conf_mkcheck_under(info->path, argc - 1, argv + 1, out)) { nc_printf(ncct, "Error creating new check\n"); return -1; } newuuid_str[0] = '\0'; uuid_unparse_lower(out, newuuid_str); wanted = newuuid_str; } /* We many not be in conf-t mode -- that's fine */ if(noit_console_mkcheck_xpath(xpath, sizeof(xpath), info, wanted)) { nc_printf(ncct, "could not find check '%s'\n", wanted); return -1; } pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt); if(!pobj || pobj->type != XPATH_NODESET || xmlXPathNodeSetIsEmpty(pobj->nodesetval)) { nc_printf(ncct, "no checks found for '%s'\n", wanted); goto out; } cnt = xmlXPathNodeSetGetLength(pobj->nodesetval); if(info && cnt != 1) { nc_printf(ncct, "Ambiguous check specified\n"); goto out; } node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0); uuid_conf = (char *)xmlGetProp(node, (xmlChar *)"uuid"); if(!uuid_conf || uuid_parse(uuid_conf, checkid)) { nc_printf(ncct, "%s has invalid or missing UUID!\n", (char *)xmlGetNodePath(node) + strlen("/noit")); goto out; } if(argc > 1 && !creating_new) if(noit_config_check_update_attrs(node, argc - 1, argv + 1)) nc_printf(ncct, "Partially successful, error setting some attributes\n"); if(info) { if(info->path) free(info->path); info->path = strdup((char *)xmlGetNodePath(node) + strlen("/noit")); uuid_copy(info->current_check, checkid); if(argc > 1) refresh_subchecks(ncct, info); if(state) { noit_console_state_push_state(ncct, state); noit_console_state_init(ncct); } goto out; } out: if(pobj) xmlXPathFreeObject(pobj); return 0; }
static int rest_show_check(noit_http_rest_closure_t *restc, int npats, char **pats) { noit_http_session_ctx *ctx = restc->http_ctx; xmlXPathObjectPtr pobj = NULL; xmlXPathContextPtr xpath_ctxt = NULL; xmlDocPtr doc = NULL; xmlNodePtr node, root, attr, config, state, tmp, anode; uuid_t checkid; noit_check_t *check; char xpath[1024], *uuid_conf, *module, *value; int rv, cnt, error_code = 500; noit_hash_iter iter = NOIT_HASH_ITER_ZERO; const char *k; int klen; void *data; noit_hash_table *configh; if(npats != 2) goto error; rv = noit_check_xpath(xpath, sizeof(xpath), pats[0], pats[1]); if(rv == 0) goto not_found; if(rv < 0) goto error; noit_conf_xml_xpath(NULL, &xpath_ctxt); pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt); if(!pobj || pobj->type != XPATH_NODESET || xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto not_found; cnt = xmlXPathNodeSetGetLength(pobj->nodesetval); if(cnt != 1) goto error; node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0); uuid_conf = (char *)xmlGetProp(node, (xmlChar *)"uuid"); if(!uuid_conf || uuid_parse(uuid_conf, checkid)) goto error; doc = xmlNewDoc((xmlChar *)"1.0"); root = xmlNewDocNode(doc, NULL, (xmlChar *)"check", NULL); xmlDocSetRootElement(doc, root); #define MYATTR(node,a,n,b) _noit_conf_get_string(node, &(n), "@" #a, &(b)) #define INHERIT(node,a,n,b) \ _noit_conf_get_string(node, &(n), "ancestor-or-self::node()/@" #a, &(b)) #define SHOW_ATTR(parent, node, a) do { \ xmlNodePtr anode = NULL; \ char *value = NULL; \ INHERIT(node, a, anode, value); \ if(value != NULL) { \ int clen, plen;\ const char *cpath, *apath; \ xmlNodePtr child; \ cpath = node ? (char *)xmlGetNodePath(node) : ""; \ apath = anode ? (char *)xmlGetNodePath(anode) : ""; \ clen = strlen(cpath); \ plen = strlen("/noit/checks"); \ child = xmlNewNode(NULL, (xmlChar *)#a); \ xmlNodeAddContent(child, (xmlChar *)value); \ if(!strncmp(cpath, apath, clen) && apath[clen] == '/') { \ } \ else { \ xmlSetProp(child, (xmlChar *)"inherited", (xmlChar *)apath+plen); \ } \ xmlAddChild(parent, child); \ } \ } while(0) attr = xmlNewNode(NULL, (xmlChar *)"attributes"); xmlAddChild(root, attr); SHOW_ATTR(attr,node,uuid); /* Name is odd, it falls back transparently to module */ if(!INHERIT(node, module, tmp, module)) module = NULL; xmlAddChild(attr, (tmp = xmlNewNode(NULL, (xmlChar *)"name"))); if(MYATTR(node, name, anode, value)) xmlNodeAddContent(tmp, (xmlChar *)value); else if(module) xmlNodeAddContent(tmp, (xmlChar *)module); SHOW_ATTR(attr,node,module); SHOW_ATTR(attr,node,target); SHOW_ATTR(attr,node,period); SHOW_ATTR(attr,node,timeout); SHOW_ATTR(attr,node,oncheck); SHOW_ATTR(attr,node,filterset); SHOW_ATTR(attr,node,disable); /* Add the config */ config = xmlNewNode(NULL, (xmlChar *)"config"); configh = noit_conf_get_hash(node, "config"); while(noit_hash_next(configh, &iter, &k, &klen, &data)) NODE_CONTENT(config, k, data); noit_hash_destroy(configh, free, free); free(configh); xmlAddChild(root, config); /* Add the state */ check = noit_poller_lookup(checkid); if(!check) { state = xmlNewNode(NULL, (xmlChar *)"state"); xmlSetProp(state, (xmlChar *)"error", (xmlChar *)"true"); } else state = noit_check_state_as_xml(check); xmlAddChild(root, state); noit_http_response_ok(ctx, "text/xml"); noit_http_response_xml(ctx, doc); noit_http_response_end(ctx); goto cleanup; not_found: noit_http_response_not_found(ctx, "text/html"); noit_http_response_end(ctx); goto cleanup; error: noit_http_response_standard(ctx, error_code, "ERROR", "text/html"); noit_http_response_end(ctx); goto cleanup; cleanup: if(pobj) xmlXPathFreeObject(pobj); if(doc) xmlFreeDoc(doc); return 0; }
static int noit_console_show_check(noit_console_closure_t ncct, int argc, char **argv, noit_console_state_t *state, void *closure) { int i, cnt; noit_conf_t_userdata_t *info; char xpath[1024]; xmlXPathObjectPtr pobj = NULL; xmlXPathContextPtr xpath_ctxt = NULL; noit_conf_xml_xpath(NULL, &xpath_ctxt); if(argc > 1) { nc_printf(ncct, "requires zero or one arguments\n"); return -1; } info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA); /* We many not be in conf-t mode -- that's fine */ if(noit_console_mkcheck_xpath(xpath, sizeof(xpath), info, argc ? argv[0] : NULL)) { nc_printf(ncct, "could not find check '%s'\n", argv[0]); return -1; } pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt); if(!pobj || pobj->type != XPATH_NODESET || xmlXPathNodeSetIsEmpty(pobj->nodesetval)) { nc_printf(ncct, "no checks found\n"); goto out; } cnt = xmlXPathNodeSetGetLength(pobj->nodesetval); if(info && cnt != 1) { nc_printf(ncct, "Ambiguous check specified\n"); goto out; } for(i=0; i<cnt; i++) { noit_hash_iter iter = NOIT_HASH_ITER_ZERO; const char *k; int klen; void *data; uuid_t checkid; noit_check_t *check; noit_hash_table *config; xmlNodePtr node, anode, mnode = NULL; char *uuid_conf; char *module, *value; node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i); uuid_conf = (char *)xmlGetProp(node, (xmlChar *)"uuid"); if(!uuid_conf || uuid_parse(uuid_conf, checkid)) { nc_printf(ncct, "%s has invalid or missing UUID!\n", (char *)xmlGetNodePath(node) + strlen("/noit")); continue; } nc_printf(ncct, "==== %s ====\n", uuid_conf); #define MYATTR(a,n,b) _noit_conf_get_string(node, &(n), "@" #a, &(b)) #define INHERIT(a,n,b) \ _noit_conf_get_string(node, &(n), "ancestor-or-self::node()/@" #a, &(b)) #define SHOW_ATTR(a) do { \ anode = NULL; \ value = NULL; \ INHERIT(a, anode, value); \ nc_attr_show(ncct, #a, node, anode, value); \ } while(0) if(!INHERIT(module, mnode, module)) module = NULL; if(MYATTR(name, anode, value)) nc_printf(ncct, " name: %s\n", value); else nc_printf(ncct, " name: %s [from module]\n", module ? module : "[undef]"); nc_attr_show(ncct, "module", node, mnode, module); SHOW_ATTR(target); SHOW_ATTR(period); SHOW_ATTR(timeout); SHOW_ATTR(oncheck); SHOW_ATTR(filterset); SHOW_ATTR(disable); /* Print out all the config settings */ config = noit_conf_get_hash(node, "config"); while(noit_hash_next(config, &iter, &k, &klen, &data)) { nc_printf(ncct, " config::%s: %s\n", k, (const char *)data); } noit_hash_destroy(config, free, free); free(config); check = noit_poller_lookup(checkid); if(!check) { nc_printf(ncct, " ERROR: not in running system\n"); } else { int idx = 0; nc_printf(ncct, " currently: "); if(NOIT_CHECK_RUNNING(check)) nc_printf(ncct, "%srunning", idx++?",":""); if(NOIT_CHECK_KILLED(check)) nc_printf(ncct, "%skilled", idx++?",":""); if(!NOIT_CHECK_CONFIGURED(check)) nc_printf(ncct, "%sunconfig", idx++?",":""); if(NOIT_CHECK_DISABLED(check)) nc_printf(ncct, "%sdisabled", idx++?",":""); if(!idx) nc_printf(ncct, "idle"); nc_write(ncct, "\n", 1); if(check->stats.current.whence.tv_sec == 0) { nc_printf(ncct, " last run: never\n"); } else { stats_t *c = &check->stats.current; struct timeval now, diff; gettimeofday(&now, NULL); sub_timeval(now, c->whence, &diff); nc_printf(ncct, " last run: %0.3f seconds ago\n", diff.tv_sec + (diff.tv_usec / 1000000.0)); nc_printf(ncct, " availability/state: %s/%s\n", noit_check_available_string(c->available), noit_check_state_string(c->state)); nc_printf(ncct, " status: %s\n", c->status ? c->status : "[[null]]"); nc_printf(ncct, " metrics:\n"); memset(&iter, 0, sizeof(iter)); while(noit_hash_next(&c->metrics, &iter, &k, &klen, &data)) { char buff[256]; noit_boolean filtered; noit_stats_snprint_metric(buff, sizeof(buff), (metric_t *)data); filtered = !noit_apply_filterset(check->filterset, check, (metric_t *)data); nc_printf(ncct, " %c%s\n", filtered ? '*' : ' ', buff); } } } } out: if(pobj) xmlXPathFreeObject(pobj); return 0; }
void htmlp_get_link(spider_t *sp, const char *fname, const char *encoding) { htmlDocPtr docp; xmlXPathContextPtr context; int i=0, j=0; xmlXPathObjectPtr result; xmlNodeSetPtr nodeset; struct _xmlAttr *attrs; char *url; char complete_url[1024] = {0}; url_queue_t *uqueue = sp->urlq; char *pos = NULL; char *cur_chr = NULL; char *url_tmp_buf = NULL; /* printf("*******************************\n"); printf("%s\n", sp->cur_url); printf("*******************************\n"); */ docp = htmlParseFile(fname, encoding); context = xmlXPathNewContext(docp); result = xmlXPathEvalExpression("//a", context); xmlXPathFreeContext(context); if(!xmlXPathNodeSetIsEmpty(result->nodesetval)) { nodeset = result->nodesetval; for(i=0; i < nodeset->nodeNr; i++) { attrs = nodeset->nodeTab[i]->properties; while(attrs) { if (strncasecmp(attrs->name, "href", 4) == 0) { url = attrs->children->content; url_tmp_buf = calloc(1, strlen(url) + 1); struct struct_parts uobj; memcpy(url_tmp_buf, url, strlen(url)); printf("===================\n"); printf("url:%s\n", url); urlp_parse(url_tmp_buf, &uobj); printf("access: %s\n", uobj.access); printf("host:%s\n", uobj.host); printf("absolute:%s\n", uobj.absolute); printf("relative:%s\n", uobj.relative); printf("search:%s\n", uobj.search); printf("anchor:%s\n", uobj.anchor); printf("===================\n"); if (uobj.access != NULL) { if (strcasecmp(uobj.access, "javascript") != 0) { if (uobj.host != NULL) { if (memcmp(uobj.host, (sp->root_url_obj).host, strlen(uobj.host)) == 0) { url_queue_uniq_add(uqueue, url); } } else { url_queue_uniq_add(uqueue, url); } } } else { if (uobj.access == NULL) uobj.access = (sp->root_url_obj).access; if (uobj.host == NULL) uobj.host = (sp->root_url_obj).host; if (uobj.relative || uobj.absolute) { memset(complete_url, 0, 1024); if (uobj.relative) { pos = strrchr(sp->cur_url, '/'); if (*(pos-1) == '/' && *(pos-2) == ':') { snprintf(complete_url, 1023, "%s://%s", uobj.access, uobj.host); complete_url[strlen(complete_url)] = '/'; strncat(complete_url, uobj.relative, 1023); } else { for (j=0, cur_chr = sp->cur_url; cur_chr < pos + 1; cur_chr++, j++) { complete_url[j] = *cur_chr; } strncat(complete_url, uobj.relative, 1023); } } if (uobj.absolute) { snprintf(complete_url, 1023, "%s://%s", uobj.access, uobj.host); complete_url[strlen(complete_url)] = '/'; strncat(complete_url, uobj.absolute, 1023); } } else { memset(complete_url, 0, 1024); snprintf(complete_url, 1023, "%s://%s", uobj.access, uobj.host); complete_url[strlen(complete_url)] = '/'; } if (uobj.search) { complete_url[strlen(complete_url)] = '?'; strncat(complete_url, uobj.search, 1023); } if (uobj.anchor) { complete_url[strlen(complete_url)] = '#'; strncat(complete_url, uobj.anchor, 1023); } printf("******preurl: %s\n", complete_url); urlp_strip_dot(complete_url); printf("******passurl: %s\n", complete_url); url_queue_uniq_add(uqueue, complete_url); } free(url_tmp_buf); break; } attrs = attrs->next; } } } xmlXPathFreeObject (result); xmlCleanupParser(); }
static void ga_gabuddy_parse_info_cb(HttpHandler* handler, gchar* response, gsize len, gpointer userdata) { htmlDocPtr doc; xmlXPathContextPtr xpathCtx; xmlXPathObjectPtr xpathObj; GayAttitudeAccount *gaa = handler->data; GayAttitudeBuddyInfoRequest *request = userdata; purple_debug(PURPLE_DEBUG_INFO, "gayattitude", "ga_buddy: Fetching info for '%s'.\n", request->gabuddy->buddy->name); doc = htmlReadMemory(response, len, "gayattitude.xml", NULL, 0); if (doc == NULL) { purple_debug(PURPLE_DEBUG_ERROR, "gayattitude", "ga_buddy: Unable to parse response (XML Parsing).\n"); return; } /* Create xpath evaluation context */ xpathCtx = xmlXPathNewContext(doc); if(xpathCtx == NULL) { purple_debug(PURPLE_DEBUG_ERROR, "gayattitude", "ga_buddy: Unable to parse response (XPath context init).\n"); xmlFreeDoc(doc); return; } xmlNode *info_node; /* Search internal Ref ID */ if (!request->gabuddy->ref_id) { purple_debug(PURPLE_DEBUG_INFO, "gayattitude", "ga_buddy: Fetching missing ref_id for '%s'.\n", request->gabuddy->buddy->name); xpathObj = xmlXPathEvalExpression((xmlChar*) "//input[@type='hidden' and @name='ref_id']", xpathCtx); if(xpathObj == NULL) { purple_debug(PURPLE_DEBUG_ERROR, "gayattitude", "ga_buddy: Unable to parse response (XPath evaluation).\n"); xmlXPathFreeContext(xpathCtx); xmlFreeDoc(doc); return; } if (!xmlXPathNodeSetIsEmpty(xpathObj->nodesetval)) { gchar *ref_id; info_node = xpathObj->nodesetval->nodeTab[0]; ref_id = (gchar*) xmlGetProp(info_node, (xmlChar*) "value"); if (request->gabuddy->real_gabuddy) request->gabuddy->real_gabuddy->ref_id = ref_id; else request->gabuddy->ref_id = ref_id; purple_debug(PURPLE_DEBUG_INFO, "gayattitude", "ga_buddy: Found ref_id for '%s': %s.\n", request->gabuddy->buddy->name, request->gabuddy->ref_id); } xmlXPathFreeObject(xpathObj); } if (request->advertise) { PurpleNotifyUserInfo *user_info = purple_notify_user_info_new(); int i; GString *str = NULL; /* Search short description */ xpathCtx->node = doc->parent; xpathObj = xmlXPathEvalExpression((xmlChar*) "//div[@id='PORTRAITHEADER2']/p/text()", xpathCtx); if(xpathObj == NULL) { purple_debug(PURPLE_DEBUG_ERROR, "gayattitude", "ga_buddy: Unable to parse response (XPath evaluation).\n"); xmlXPathFreeContext(xpathCtx); xmlFreeDoc(doc); return; } if (!xmlXPathNodeSetIsEmpty(xpathObj->nodesetval)) { info_node = xpathObj->nodesetval->nodeTab[0]; purple_notify_user_info_add_pair(user_info, "Short Description", (gchar*) info_node->content); } xmlXPathFreeObject(xpathObj); /* Search user research */ xpathCtx->node = doc->parent; xpathObj = xmlXPathEvalExpression((xmlChar*) "//div[@id='bloc_recherche']/p/text()", xpathCtx); if(xpathObj == NULL) { purple_debug(PURPLE_DEBUG_ERROR, "gayattitude", "ga_buddy: Unable to parse response (XPath evaluation).\n"); xmlXPathFreeContext(xpathCtx); xmlFreeDoc(doc); return; } if (!xmlXPathNodeSetIsEmpty(xpathObj->nodesetval)) { for(i = 0; i < xpathObj->nodesetval->nodeNr; i++) { info_node = xpathObj->nodesetval->nodeTab[i]; if (i == 0) str = g_string_new((gchar*) info_node->content); else g_string_append_printf(str, " -- %s", info_node->content); } purple_notify_user_info_add_pair(user_info, "Research", str->str); g_string_free(str, TRUE); } xmlXPathFreeObject(xpathObj); purple_notify_userinfo(gaa->pc, request->gabuddy->buddy->name, user_info, NULL, NULL); purple_notify_user_info_destroy(user_info); } /* Cleanup */ xmlXPathFreeContext(xpathCtx); xmlFreeDoc(doc); /* Chained Callback */ if (request->callback) { purple_debug(PURPLE_DEBUG_INFO, "gayattitude", "ga_buddy: Calling callback after info for '%s' was retrieved\n", request->gabuddy->buddy->name); request->callback(gaa, request->callback_data); } }
jobject buildXPathObject(JNIEnv *env, jobject xpathContext, xmlXPathObject *result) { jobject resultObject = (*env)->NewObject(env, classXPathObject, methodXPathObjectNew, (jlong)result, xmlXPathNodeSetIsEmpty(result->nodesetval)); jobject jdoc; switch(result->type) { case XPATH_NODESET: jdoc = (*env)->GetObjectField(env, xpathContext, fieldXPathContextDocument); (*env)->SetObjectField(env, resultObject, fieldXPathObjectSetNodeset, buildNodeSet(env, result->nodesetval, jdoc)); (*env)->DeleteLocalRef(env, jdoc); break; case XPATH_BOOLEAN: (*env)->SetBooleanField(env, resultObject, fieldXPathObjectSetBool, result->boolval==0 ? JNI_FALSE : JNI_TRUE); break; case XPATH_NUMBER: (*env)->SetDoubleField(env, resultObject, fieldXPathObjectSetFloat, result->floatval); break; case XPATH_STRING: (*env)->SetObjectField(env, resultObject, fieldXPathObjectSetString, (*env)->NewStringUTF(env, (char*)result->stringval)); break; case XPATH_UNDEFINED: assert(0 && "XPATH_UNDEFIEND not implemented"); break; case XPATH_POINT: assert(0 && "XPATH_POINT not implemented"); break; case XPATH_RANGE: assert(0 && "XPATH_RANGE not implemented"); break; case XPATH_LOCATIONSET: assert(0 && "XPATH_LOCATIONSET not implemented"); break; case XPATH_USERS: assert(0 && "XPATH_USERS not implemented"); break; case XPATH_XSLT_TREE: assert(0 && "XPATH_XSLT_TREE not implemented"); break; } return resultObject; }