clixon/util/clixon_util_insert.c
Olof hagsand 09a2e09848 * New XML parsing API:
* `clixon_xml_parse_string()`
   * `clixon_xml_parse_file()`
* New JSON parsing API, with same signature as XML parsing:
   * `clixon_json_parse_string()`
   * `clixon_xml_parse_file()`
* XML YANG binding API have been rearranged as follows:
   * `xml_bind_yang_rpc()`
   * `xml_bind_yang_rpc_reply()`
   * `xml_bind_yang()`
   * `xml_bind_yang0()`
2020-03-19 21:32:27 +01:00

202 lines
5.5 KiB
C

/*
*
***** BEGIN LICENSE BLOCK *****
Copyright (C) 2009-2016 Olof Hagsand and Benny Holmgren
Copyright (C) 2017-2019 Olof Hagsand
Copyright (C) 2020 Olof Hagsand and Rubicon Communications, LLC
This file is part of CLIXON.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Alternatively, the contents of this file may be used under the terms of
the GNU General Public License Version 3 or later (the "GPL"),
in which case the provisions of the GPL are applicable instead
of those above. If you wish to allow use of your version of this file only
under the terms of the GPL, and not to allow others to
use your version of this file under the terms of Apache License version 2,
indicate your decision by deleting the provisions above and replace them with
the notice and other provisions required by the GPL. If you do not delete
the provisions above, a recipient may use your version of this file under
the terms of any one of the Apache License version 2 or the GPL.
***** END LICENSE BLOCK *****
* Utility for Inserting element in list
*/
#ifdef HAVE_CONFIG_H
#include "clixon_config.h" /* generated by config & autoconf */
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <stdint.h>
#include <syslog.h>
#include <fcntl.h>
/* cligen */
#include <cligen/cligen.h>
/* clixon */
#include "clixon/clixon.h"
static int
usage(char *argv0)
{
fprintf(stderr, "usage:%s [options]\n"
"where options are\n"
"\t-h \t\tHelp\n"
"\t-D <level>\tDebug\n"
"\t-y <file> \tYANG spec file (or stdin)\n"
"\t-b <base> \tXML base expression\n"
"\t-x <xml> \tXML to insert\n"
"\t-p <xpath>\tXpath to where in base and XML\n"
"\t-s \tSort output after insert\n"
"Assume insert xml is first child of xpath. Ie if xml=<a><x>23 and xpath=a, then inserted element is <x>23\n",
argv0
);
exit(0);
}
int
main(int argc, char **argv)
{
int retval = -1;
char *argv0 = argv[0];
int c;
char *filename = NULL;
int fd = 0; /* unless overriden by argv[1] */
char *x0str = NULL;
char *xistr = NULL;
char *xpath = NULL;
yang_stmt *yspec = NULL;
cxobj *x0 = NULL;
cxobj *xb;
cxobj *xi = NULL;
int sort = 0;
clicon_handle h;
clicon_log_init("clixon_insert", LOG_DEBUG, CLICON_LOG_STDERR);
if ((h = clicon_handle_init()) == NULL)
goto done;
optind = 1;
opterr = 0;
while ((c = getopt(argc, argv, "hD:y:b:x:p:s")) != -1)
switch (c) {
case 'h':
usage(argv0);
break;
case 'D':
if (sscanf(optarg, "%d", &debug) != 1)
usage(argv0);
break;
case 'y': /* YANG spec file */
filename = optarg;
if (0 && (fd = open(filename, O_RDONLY)) < 0){
clicon_err(OE_UNIX, errno, "open(%s)", argv[1]);
goto done;
}
break;
case 'b': /* Base XML expression */
x0str = optarg;
break;
case 'x': /* XML to insert */
xistr = optarg;
break;
case 'p': /* XPATH base */
xpath = optarg;
break;
case 's': /* sort output after insert */
sort++;
break;
default:
usage(argv[0]);
break;
}
if (xistr == NULL || x0str == NULL)
usage(argv0);
if (xpath == NULL)
usage(argv0);
if (filename == NULL)
usage(argv0);
clicon_debug(1, "xistr:%s", xistr);
clicon_debug(1, "x0str:%s", x0str);
clicon_debug(1, "xpath:%s", xpath);
if ((yspec = yspec_new()) == NULL)
goto done;
if (yang_spec_parse_file(h, filename, yspec) < 0)
goto done;
/* Parse base XML */
if (clixon_xml_parse_string(x0str, YB_MODULE, yspec, &x0, NULL) < 0){
clicon_err(OE_XML, 0, "Parsing base xml: %s", x0str);
goto done;
}
if (xml_bind_yang(x0, YB_MODULE, yspec, NULL) < 0)
goto done;
if ((xb = xpath_first(x0, NULL, "%s", xpath)) == NULL){
clicon_err(OE_XML, 0, "xpath: %s not found in x0", xpath);
goto done;
}
if (debug){
clicon_debug(1, "xb:");
xml_print(stderr, xb);
}
/* Parse insert XML */
if (clixon_xml_parse_string(xistr, YB_MODULE, yspec, &xi, NULL) < 0){
clicon_err(OE_XML, 0, "Parsing insert xml: %s", xistr);
goto done;
}
if (xml_bind_yang(xi, YB_MODULE, yspec, NULL) < 0)
goto done;
if ((xi = xpath_first(xi, NULL, "%s", xpath)) == NULL){
clicon_err(OE_XML, 0, "xpath: %s not found in xi", xpath);
goto done;
}
/* Find first element child */
if ((xi = xml_child_i_type(xi, 0, CX_ELMNT)) == NULL){
clicon_err(OE_XML, 0, "xi has no element child");
goto done;
}
/* Remove it from parent */
if (xml_rm(xi) < 0)
goto done;
if (debug){
clicon_debug(1, "xi:");
xml_print(stderr, xi);
}
if (xml_insert(xb, xi, INS_LAST, NULL, NULL) < 0)
goto done;
if (debug){
clicon_debug(1, "x0:");
xml_print(stderr, x0);
}
if (sort)
xml_sort(xb, h);
clicon_xml2file(stdout, xb, 0, 0);
retval = 0;
done:
if (x0)
xml_free(x0);
if (yspec)
yspec_free(yspec);
if (fd > 0)
close(fd);
return retval;
}