void prepare_gsl_file (char *filename) { xml_root = xml_new (NULL, NULL, NULL); xml_source = xml_new (xml_root, "root", NULL); xml_put_attr (xml_source, "script", filename); xml_put_attr (xml_switches, "template", "0"); }
/* * stm is top of template. Create a new parse-tree and copy to it. */ static int traverse(struct xml_node *st, struct xml_node *xt, struct xml_node *rtop) { struct xml_node *rt; int i; if (st->xn_namespace && strcmp(st->xn_namespace, "xsl") == 0){ /* do intelligent stuff */ if (strcmp(st->xn_name, "value-of")==0) value_of(st, xt, rtop); else if (strcmp(st->xn_name, "for-each")==0) for_each(st, xt, rtop); else if (strcmp(st->xn_name, "if")==0) ifelement(st, xt, rtop); else if (strcmp(st->xn_name, "choose")==0) choose(st, xt, rtop); } else{ /* just copy */ if ((rt = xml_new(st->xn_name, rtop)) == NULL) return -1; xml_cp1(st, rt); for (i=0; i<st->xn_nrchildren; i++) if (traverse(st->xn_children[i], xt, rt) < 0) return -1; } return 0; }
MODULE initialise_program_data (void) { xml_root = NULL; xml_source = NULL; xml_switches = xml_new (NULL, "switches", NULL); }
static PyObject* py_xml_new(PyObject *self, PyObject *args) { char* nm; if (!PyArg_ParseTuple(args, "s:xml_new", &nm)) return NULL; int n = xml_new(nm); PyObject* pn = Py_BuildValue("i",n); return pn; }
/*! Get database configuration * Same as clicon_proto_change just with a cvec instead of lvec * @param[in] h CLICON handle * @param[in] db Name of database * @param[in] xpath XPath (or "") * @param[out] xt XML tree. Free with xml_free. * Either <config> or <rpc-error>. * @retval 0 OK * @retval -1 Error, fatal or xml * @code * cxobj *xt = NULL; * if (clicon_rpc_get_config(h, "running", "/", &xt) < 0) * err; * if ((xerr = xpath_first(xt, "/rpc-error")) != NULL){ * clicon_rpc_generate_error("", xerr); * err; * } * if (xt) * xml_free(xt); * @endcode * @see clicon_rpc_generate_error */ int clicon_rpc_get_config(clicon_handle h, char *db, char *xpath, cxobj **xt) { int retval = -1; struct clicon_msg *msg = NULL; cbuf *cb = NULL; cxobj *xret = NULL; cxobj *xd; char *username; if ((cb = cbuf_new()) == NULL) goto done; cprintf(cb, "<rpc"); if ((username = clicon_username_get(h)) != NULL) cprintf(cb, " username=\"%s\"", username); cprintf(cb, "><get-config><source><%s/></source>", db); if (xpath && strlen(xpath)) cprintf(cb, "<filter type=\"xpath\" select=\"%s\"/>", xpath); cprintf(cb, "</get-config></rpc>"); if ((msg = clicon_msg_encode("%s", cbuf_get(cb))) == NULL) goto done; if (clicon_rpc_msg(h, msg, &xret, NULL) < 0) goto done; /* Send xml error back: first check error, then ok */ if ((xd = xpath_first(xret, "/rpc-reply/rpc-error")) != NULL) xd = xml_parent(xd); /* point to rpc-reply */ else if ((xd = xpath_first(xret, "/rpc-reply/data")) == NULL) if ((xd = xml_new("data", NULL, NULL)) == NULL) goto done; if (xt){ if (xml_rm(xd) < 0) goto done; *xt = xd; } retval = 0; done: if (cb) cbuf_free(cb); if (xret) xml_free(xret); if (msg) free(msg); return retval; }
int main (int argc, char *argv []) { XML_ITEM *root; root = xml_new (NULL, "root", ""); if (xml_load_file (&root, ".", argv[1], FALSE) == 0) { xml_save_string (root); xml_save_file (root, "testxml.txt"); xml_free (root); } else printf ("Load error: %s\n", xml_error ()); return (EXIT_SUCCESS); }
/* * <xsl:value-of select="expr"/> */ static int value_of(struct xml_node *st, struct xml_node *xt, struct xml_node *rt) { char *select; struct xml_node *x; struct xml_node *rb; char *v; if ((select = xml_get(st, "select")) == NULL) return 0; if ((x = xml_xpath(xt, select)) == NULL) return 0; if ((v = xml_get_body(x)) == NULL) return 0; if ((rb = xml_new("body", rt)) == NULL) return 0; rb->xn_type = XML_BODY; rb->xn_value = strdup(v); return 0; }
bool_t saveenv(char * file) { struct environ_t * environ = &(runtime_get()->__environ); struct environ_t * p; struct xml * root, * env; char * str; int fd; root = xml_new("environment"); if(!root) return FALSE; for(p = environ->next; p != environ; p = p->next) { env = xml_add_child(root, "env", 0); xml_set_txt(env, p->content); } str = xml_toxml(root); if(!str) { xml_free(root); return FALSE; } fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)); if(fd < 0) { free(str); xml_free(root); return FALSE; } write(fd, str, strlen(str)); close(fd); free(str); xml_free(root); return TRUE; }
/* * save environment variable */ bool_t saveenv(char * file) { struct xml * root, * env; char * str; char ** ep; int fd; root = xml_new("environment"); if(!root) return FALSE; for(ep = environ; *ep; ep++) { env = xml_add_child(root, "env", 0); xml_set_txt(env, *ep); } str = xml_toxml(root); if(!str) { xml_free(root); return FALSE; } fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)); if(fd < 0) { free(str); xml_free(root); return FALSE; } write(fd, str, strlen(str)); close(fd); free(str); xml_free(root); return TRUE; }
char * xml_tree( char *p, xml *onode ) { newstr tag; xml_attrib *attrib; int type, is_style = 0; newstr_init( &tag ); while ( *p ) { /* retain white space for <style> tags in endnote xml */ if ( onode->tag && onode->tag->data && !strcasecmp(onode->tag->data,"style") ) is_style=1; while ( *p && *p!='<' ) { if ( onode->value->len>0 || is_style || !is_ws( *p ) ) newstr_addchar( onode->value, *p ); p++; } if ( *p=='<' ) { newstr_empty( &tag ); p = xml_processtag( p, &tag, &attrib, &type ); if ( type==XML_OPEN || type==XML_OPENCLOSE || type==XML_DESCRIPTOR ) { xml *nnode = xml_new(); newstr_newstrcpy( nnode->tag, &tag ); nnode->a = attrib; xml_appendnode( onode, nnode ); if ( type==XML_OPEN ) p = xml_tree( p, nnode ); } else if ( type==XML_CLOSE ) { /*check to see if it's closing for this one*/ goto out; /* assume it's right for now */ } } } out: newstr_free( &tag ); return p; }
/* The big decision maker, is it a regular node, or a text node. * If it's a node, it will check if it should have children, and if so * will recurse over them. * Text nodes don't have children, so no recursing. */ static XmlNode* xml_parse(struct XMLBUF *xml) { int offset; int toff; char **tmp; char *stmp; XmlNode **this, *ret = NULL; this = &ret; xml_skip(xml, XML_SPACE); // skip whitespace offset=0; while( (xml->read_index < xml->len) || !xml->eof ) { switch(is_special(xml_peek(xml))) { case XML_OPEN: xml_read_byte(xml); if(xml_peek(xml) == '/') return ret; // parents close tag // read the tag name feed_mask = XML_SPACE | XML_SLASH | XML_CLOSE; *this = xml_new( xml_feed(xml, test_mask)); if(xml->error)goto xml_parse_malloc; xml_skip(xml, XML_SPACE); // skip any whitespace xml_read_attr(xml, *this); // read attributes // how does this tag finish? switch(is_special(xml_peek(xml))) { case XML_CLOSE: // child-nodes ahead xml_read_byte(xml); (*this)->child = xml_parse(xml); xml_skip(xml, XML_ALL ^ XML_CLOSE); xml_read_byte(xml); break; case XML_SLASH: // self closing tag xml_read_byte(xml); xml_read_byte(xml); break; } break; default: // text node *this = xml_new(0); xml_skip(xml, XML_SPACE); // skip any whitespace feed_mask = XML_OPEN; (*this)->nattrib=1; tmp = malloc(sizeof(char*)*2); if(!tmp)goto xml_parse_malloc; (*this)->attrib = tmp; (*this)->attrib[1] = NULL; stmp = (*this)->attrib[0] = xml_feed(xml, test_mask); /* trim the whitespace off the end of text nodes, * by overwriting the spaces will null termination. */ toff = strlen(stmp)-1; while( ( is_special(stmp[toff]) & XML_SPACE ) ) { stmp[toff] = 0; toff --; } break; } this = &(*this)->next; xml_skip(xml, XML_SPACE); // skip whitespace } return ret; xml_parse_malloc: xml_end_file(xml); if(ret)xml_free(ret); return 0; }
void xmlmethods(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) { int j, m, iok = 0; char* file, *key, *val, *nm; int job = getInt(prhs[1]); int i = getInt(prhs[2]); // Check for proper number of arguments if (!nargs_ok(job,nrhs-1)) { mexErrMsgTxt("Wrong number of inputs."); return; } else if (nlhs > 1) { mexErrMsgTxt("Too many output arguments"); } // options that do not return a value if (job < 20) { switch (job) { case 0: nm = getString(prhs[3]); iok = xml_new(nm); break; case 1: iok = xml_del(i); break; case 2: iok = xml_copy(i); break; case 4: file = getString(prhs[3]); iok = xml_build(i, file); break; case 5: key = getString(prhs[3]); val = getString(prhs[4]); iok = xml_addAttrib(i, key, val); break; case 6: key = getString(prhs[3]); iok = xml_child(i, key); break; case 7: m = getInt(prhs[3]); iok = xml_child_bynumber(i, m); break; case 8: key = getString(prhs[3]); iok = xml_findID(i, key); break; case 9: key = getString(prhs[3]); iok = xml_findByName(i, key); break; case 10: iok = xml_nChildren(i); break; case 11: key = getString(prhs[3]); val = getString(prhs[4]); iok = xml_addChild(i, key, val); break; case 12: key = getString(prhs[3]); j = getInt(prhs[4]); iok = xml_addChildNode(i, j); break; case 13: file = getString(prhs[3]); iok = xml_write(i, file); break; case 14: j = getInt(prhs[3]); iok = xml_removeChild(i, j); break; case 15: file = getString(prhs[3]); iok = xml_get_XML_File(file, 0); break; default: mexErrMsgTxt("unknown job parameter"); } plhs[0] = mxCreateNumericMatrix(1,1,mxDOUBLE_CLASS,mxREAL); double* h = mxGetPr(plhs[0]); *h = double(iok); if (iok < 0) { reportError(); } return; } // options that return strings char* v = (char*)mxCalloc(80, sizeof(char)); switch (job) { case 20: // return an attribute key = getString(prhs[3]); iok = xml_attrib(i, key, v); break; case 21: // return the value of the node iok = xml_value(i, v); break; case 22: iok = xml_tag(i, v); break; default: mexErrMsgTxt("unknown job parameter"); } if (iok < 0) { plhs[0] = mxCreateNumericMatrix(1,1,mxDOUBLE_CLASS,mxREAL); double* h = mxGetPr(plhs[0]); *h = double(iok); if (iok < 0) { reportError(); } } else { plhs[0] = mxCreateString(v); } }
/*! Modify xml datastore from a callback using xml key format strings * @param[in] h Clicon handle * @param[in] cvv Vector of cli string and instantiated variables * @param[in] argv Vector. First element xml key format string, eg "/aaa/%s" * @param[in] op Operation to perform on database * Cvv will contain first the complete cli string, and then a set of optional * instantiated variables. * Example: * cvv[0] = "set interfaces interface eth0 type bgp" * cvv[1] = "eth0" * cvv[2] = "bgp" * argv[0] = "/interfaces/interface/%s/type" * op: OP_MERGE * @see cli_callback_generate where arg is generated */ static int cli_dbxml(clicon_handle h, cvec *cvv, cvec *argv, enum operation_type op) { int retval = -1; char *str = NULL; char *api_path_fmt; /* xml key format */ char *api_path = NULL; /* xml key */ cg_var *cval; int len; cg_var *arg; cbuf *cb = NULL; yang_stmt *yspec; cxobj *xbot = NULL; /* xpath, NULL if datastore */ yang_stmt *y = NULL; /* yang spec of xpath */ cxobj *xtop = NULL; /* xpath root */ cxobj *xa; /* attribute */ cxobj *xb; /* body */ if (cvec_len(argv) != 1){ clicon_err(OE_PLUGIN, 0, "Requires one element to be xml key format string"); goto done; } if ((yspec = clicon_dbspec_yang(h)) == NULL){ clicon_err(OE_FATAL, 0, "No DB_SPEC"); goto done; } arg = cvec_i(argv, 0); api_path_fmt = cv_string_get(arg); if (api_path_fmt2api_path(api_path_fmt, cvv, &api_path) < 0) goto done; /* Create config top-of-tree */ if ((xtop = xml_new("config", NULL, NULL)) == NULL) goto done; xbot = xtop; if (api_path && api_path2xml(api_path, yspec, xtop, YC_DATANODE, 1, &xbot, &y) < 1) goto done; if ((xa = xml_new("operation", xbot, NULL)) == NULL) goto done; xml_type_set(xa, CX_ATTR); if (xml_value_set(xa, xml_operation2str(op)) < 0) goto done; if (yang_keyword_get(y) != Y_LIST && yang_keyword_get(y) != Y_LEAF_LIST){ len = cvec_len(cvv); if (len > 1){ cval = cvec_i(cvv, len-1); if ((str = cv2str_dup(cval)) == NULL){ clicon_err(OE_UNIX, errno, "cv2str_dup"); goto done; } if ((xb = xml_new("body", xbot, NULL)) == NULL) goto done; xml_type_set(xb, CX_BODY); if (xml_value_set(xb, str) < 0) goto done; } } if ((cb = cbuf_new()) == NULL){ clicon_err(OE_XML, errno, "cbuf_new"); goto done; } if (clicon_xml2cbuf(cb, xtop, 0, 0) < 0) goto done; if (clicon_rpc_edit_config(h, "candidate", OP_NONE, cbuf_get(cb)) < 0) goto done; if (clicon_autocommit(h)) { if (clicon_rpc_commit(h) < 0) goto done; } retval = 0; done: if (cb) cbuf_free(cb); if (str) free(str); if (api_path) free(api_path); if (xtop) xml_free(xtop); return retval; }
/*! Copy one configuration object to antother * * Works for objects that are items ina yang list with a keyname, eg as: * list sender{ * key name; * leaf name{... * * @param[in] h CLICON handle * @param[in] cvv Vector of variables from CLIgen command-line * @param[in] argv Vector: <db>, <xpath>, <field>, <fromvar>, <tovar> * Explanation of argv fields: * db: Database name, eg candidate|tmp|startup * xpath: XPATH expression with exactly two %s pointing to field and from name * field: Name of list key, eg name * fromvar:Name of variable containing name of object to copy from (given by xpath) * tovar: Name of variable containing name of object to copy to. * @code * cli spec: * copy snd <n1:string> to <n2:string>, cli_copy_config("candidate", "/sender[%s='%s']", "from", "n1", "n2"); * cli command: * copy snd from to to * @endcode */ int cli_copy_config(clicon_handle h, cvec *cvv, cvec *argv) { int retval = -1; char *db; cxobj *x1 = NULL; cxobj *x2 = NULL; cxobj *x; char *xpath; int i; int j; cbuf *cb = NULL; char *keyname; char *fromvar; cg_var *fromcv; char *fromname = NULL; char *tovar; cg_var *tocv; char *toname; cxobj *xerr; if (cvec_len(argv) != 5){ clicon_err(OE_PLUGIN, 0, "Requires four elements: <db> <xpath> <keyname> <from> <to>"); goto done; } /* First argv argument: Database */ db = cv_string_get(cvec_i(argv, 0)); /* Second argv argument: xpath */ xpath = cv_string_get(cvec_i(argv, 1)); /* Third argv argument: name of keyname */ keyname = cv_string_get(cvec_i(argv, 2)); /* Fourth argv argument: from variable */ fromvar = cv_string_get(cvec_i(argv, 3)); /* Fifth argv argument: to variable */ tovar = cv_string_get(cvec_i(argv, 4)); /* Get from variable -> cv -> from name */ if ((fromcv = cvec_find(cvv, fromvar)) == NULL){ clicon_err(OE_PLUGIN, 0, "fromvar '%s' not found in cligen var list", fromvar); goto done; } /* Get from name from cv */ fromname = cv_string_get(fromcv); /* Create xpath */ if ((cb = cbuf_new()) == NULL){ clicon_err(OE_PLUGIN, errno, "cbuf_new"); goto done; } /* Sanity check that xpath contains exactly two %s, ie [%s='%s'] */ j = 0; for (i=0; i<strlen(xpath); i++){ if (xpath[i] == '%') j++; } if (j != 2){ clicon_err(OE_PLUGIN, 0, "xpath '%s' does not have two '%%'", xpath); goto done; } cprintf(cb, xpath, keyname, fromname); /* Get from object configuration and store in x1 */ if (clicon_rpc_get_config(h, db, cbuf_get(cb), &x1) < 0) goto done; if ((xerr = xpath_first(x1, "/rpc-error")) != NULL){ clicon_rpc_generate_error("Get configuration", xerr); goto done; } /* Get to variable -> cv -> to name */ if ((tocv = cvec_find(cvv, tovar)) == NULL){ clicon_err(OE_PLUGIN, 0, "tovar '%s' not found in cligen var list", tovar); goto done; } toname = cv_string_get(tocv); /* Create copy xml tree x2 */ if ((x2 = xml_new("new", NULL, NULL)) == NULL) goto done; if (xml_copy(x1, x2) < 0) goto done; xml_name_set(x2, "config"); cprintf(cb, "/%s", keyname); if ((x = xpath_first(x2, "%s", cbuf_get(cb))) == NULL){ clicon_err(OE_PLUGIN, 0, "Field %s not found in copy tree", keyname); goto done; } x = xml_find(x, "body"); xml_value_set(x, toname); /* resuse cb */ cbuf_reset(cb); /* create xml copy tree and merge it with database configuration */ clicon_xml2cbuf(cb, x2, 0, 0); if (clicon_rpc_edit_config(h, db, OP_MERGE, cbuf_get(cb)) < 0) goto done; retval = 0; done: if (cb) cbuf_free(cb); if (x1 != NULL) xml_free(x1); if (x2 != NULL) xml_free(x2); return retval; }
bool_t console_stdio_save(char * file) { struct xml * root; struct xml * in, * out, * err; char * str; s32_t fd; root = xml_new("console"); if(!root) return FALSE; in = xml_add_child(root, "stdin", 0); if(!in) { xml_free(root); return FALSE; } if(console_stdin && console_stdin->name) xml_set_attr(in, "name", console_stdin->name); out = xml_add_child(root, "stdout", 1); if(!out) { xml_free(root); return FALSE; } if(console_stdout && console_stdout->name) xml_set_attr(out, "name", console_stdout->name); err = xml_add_child(root, "stderr", 1); if(!err) { xml_free(root); return FALSE; } if(console_stderr && console_stderr->name) xml_set_attr(err, "name", console_stderr->name); str = xml_toxml(root); if(!str) { xml_free(root); return FALSE; } fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)); if(fd < 0) { free(str); xml_free(root); return FALSE; } write(fd, str, strlen((const char *)str)); close(fd); free(str); xml_free(root); return TRUE; }
MODULE use_standard_input (void) { xml_root = xml_new (NULL, NULL, NULL); xml_source = xml_new (xml_root, "root", NULL); xml_put_attr (xml_switches, "template", "0"); }