* Added PRETTYPRINT_INDENT compile-time option controlling indentation level for XML,JSON and TEXT

* Default value is `3`
This commit is contained in:
Olof hagsand 2022-10-31 14:20:14 +01:00
parent 1830381249
commit 56f4bb45cc
11 changed files with 73 additions and 1152 deletions

View file

@ -57,6 +57,8 @@ Expected: End of 2022
Users may have to change how they access the system Users may have to change how they access the system
* Added `PRETTYPRINT_INDENT` compile-time option controlling indentation level for XML,JSON and TEXT
* Default value is `3`
* NETCONF: Removed `message-id` from hello protocol following RFC 6241 * NETCONF: Removed `message-id` from hello protocol following RFC 6241
* See [message-id present on netconf app "hello"](https://github.com/clicon/clixon/issues/369) * See [message-id present on netconf app "hello"](https://github.com/clicon/clixon/issues/369)
@ -64,6 +66,8 @@ Users may have to change how they access the system
Developers may need to change their code Developers may need to change their code
* [Code formatting: Change indentation style to space](https://github.com/clicon/clixon/issues/379)
* Applies to all c/h/y/l/sh files and .editorconfig
* C API changes * C API changes
* Added `defaults` parameter to `clicon_rpc_get_pageable_list()` * Added `defaults` parameter to `clicon_rpc_get_pageable_list()`
* `clicon_rpc_commit()` and `cli_commit` * `clicon_rpc_commit()` and `cli_commit`
@ -75,8 +79,6 @@ Developers may need to change their code
### Minor features ### Minor features
* [Code formatting: Change indentation style to space](https://github.com/clicon/clixon/issues/379)
* Applies to all c/h/y/l/sh files and .editorconfig
* Added warning if modstate is not present in datastore if `CLICON_XMLDB_MODSTATE` is set. * Added warning if modstate is not present in datastore if `CLICON_XMLDB_MODSTATE` is set.
### Corrected Bugs ### Corrected Bugs

View file

@ -411,7 +411,7 @@ show_yang(clicon_handle h,
* @param[in] state * @param[in] state
* @param[in] withdefault RFC 6243 with-default modes * @param[in] withdefault RFC 6243 with-default modes
* @param[in] extdefault with-defaults with propriatary extensions * @param[in] extdefault with-defaults with propriatary extensions
* @param[in] prefix CLI prefix to prepend cli syntax, eg "set " * @param[in] prepend CLI prefix to prepend cli syntax, eg "set "
* @param[in] xpath XPath * @param[in] xpath XPath
* @param[in] nsc Namespace mapping for xpath * @param[in] nsc Namespace mapping for xpath
* @param[in] skiptop If set, do not show object itself, only its children * @param[in] skiptop If set, do not show object itself, only its children
@ -424,7 +424,7 @@ cli_show_common(clicon_handle h,
int state, int state,
char *withdefault, char *withdefault,
char *extdefault, char *extdefault,
char *prefix, char *prepend,
char *xpath, char *xpath,
cvec *nsc, cvec *nsc,
int skiptop int skiptop
@ -496,7 +496,7 @@ cli_show_common(clicon_handle h,
goto done; goto done;
break; break;
case FORMAT_CLI: case FORMAT_CLI:
if (clixon_cli2file(h, stdout, xp, prefix, cligen_output, skiptop) < 0) /* cli syntax */ if (clixon_cli2file(h, stdout, xp, prepend, cligen_output, skiptop) < 0) /* cli syntax */
goto done; goto done;
break; break;
case FORMAT_NETCONF: case FORMAT_NETCONF:
@ -648,7 +648,7 @@ cli_show_option_withdefault(cvec *argv,
* <state> true|false: also print state * <state> true|false: also print state
* <default> Retrieval mode: report-all, trim, explicit, report-all-tagged, * <default> Retrieval mode: report-all, trim, explicit, report-all-tagged,
* NULL, report-all-tagged-default, report-all-tagged-strip (extended) * NULL, report-all-tagged-default, report-all-tagged-strip (extended)
* <prefix> CLI prefix: prepend before cli syntax output * <prepend> CLI prefix: prepend before cli syntax output
* @code * @code
* clispec: * clispec:
* show config, cli_show_config("running","xml"); * show config, cli_show_config("running","xml");
@ -675,7 +675,7 @@ cli_show_config(clicon_handle h,
enum format_enum format = FORMAT_XML; enum format_enum format = FORMAT_XML;
cvec *nsc = NULL; cvec *nsc = NULL;
int pretty = 1; int pretty = 1;
char *prefix = NULL; char *prepend = NULL;
int state = 0; int state = 0;
char *withdefault = NULL; /* RFC 6243 modes */ char *withdefault = NULL; /* RFC 6243 modes */
char *extdefault = NULL; /* with extended tagged modes */ char *extdefault = NULL; /* with extended tagged modes */
@ -684,7 +684,7 @@ cli_show_config(clicon_handle h,
char *namespace = NULL; char *namespace = NULL;
if (cvec_len(argv) < 2 || cvec_len(argv) > 8){ if (cvec_len(argv) < 2 || cvec_len(argv) > 8){
clicon_err(OE_PLUGIN, EINVAL, "Received %d arguments. Expected: <dbname> [<format><xpath> <namespace> <pretty> <state> <default> <prefix>]", cvec_len(argv)); clicon_err(OE_PLUGIN, EINVAL, "Received %d arguments. Expected: <dbname> [<format><xpath> <namespace> <pretty> <state> <default> <prepend>]", cvec_len(argv));
goto done; goto done;
} }
dbname = cv_string_get(cvec_i(argv, argc++)); dbname = cv_string_get(cvec_i(argv, argc++));
@ -715,11 +715,11 @@ cli_show_config(clicon_handle h,
goto done; goto done;
} }
if (cvec_len(argv) > argc){ if (cvec_len(argv) > argc){
prefix = cv_string_get(cvec_i(argv, argc++)); prepend = cv_string_get(cvec_i(argv, argc++));
} }
if (cli_show_common(h, dbname, format, pretty, state, if (cli_show_common(h, dbname, format, pretty, state,
withdefault, extdefault, withdefault, extdefault,
prefix, xpath, nsc, 0) < 0) prepend, xpath, nsc, 0) < 0)
goto done; goto done;
retval = 0; retval = 0;
done: done:
@ -811,7 +811,7 @@ int cli_show_version(clicon_handle h,
* @param[in] h Clixon handle * @param[in] h Clixon handle
* @param[in] cvv Vector of variables from CLIgen command-line * @param[in] cvv Vector of variables from CLIgen command-line
* @param[in] argv String vector of show options, format: * @param[in] argv String vector of show options, format:
* <api_path_fmt> Generated API PATH * <api_path_fmt> Generated API PATH (this is added implicitly, not actually given in the cvv)
* <dbname> Name of datastore, such as "running" * <dbname> Name of datastore, such as "running"
* -- from here optional: * -- from here optional:
* <format> "text"|"xml"|"json"|"cli"|"netconf" (see format_enum), default: xml * <format> "text"|"xml"|"json"|"cli"|"netconf" (see format_enum), default: xml
@ -819,7 +819,7 @@ int cli_show_version(clicon_handle h,
* <state> true|false: also print state * <state> true|false: also print state
* <default> Retrieval mode: report-all, trim, explicit, report-all-tagged, * <default> Retrieval mode: report-all, trim, explicit, report-all-tagged,
* NULL, report-all-tagged-default, report-all-tagged-strip (extended) * NULL, report-all-tagged-default, report-all-tagged-strip (extended)
* <prefix> CLI prefix: prepend before cli syntax output * <prepend> CLI prefix: prepend before cli syntax output
* @code * @code
* clispec: * clispec:
* show config @datamodelshow, cli_show_auto("candidate", "xml"); * show config @datamodelshow, cli_show_auto("candidate", "xml");
@ -846,7 +846,7 @@ cli_show_auto(clicon_handle h,
enum format_enum format = FORMAT_XML; enum format_enum format = FORMAT_XML;
cvec *nsc = NULL; cvec *nsc = NULL;
int pretty = 1; int pretty = 1;
char *prefix = NULL; char *prepend = NULL;
int state = 0; int state = 0;
char *withdefault = NULL; /* RFC 6243 modes */ char *withdefault = NULL; /* RFC 6243 modes */
char *extdefault = NULL; /* with extended tagged modes */ char *extdefault = NULL; /* with extended tagged modes */
@ -858,7 +858,7 @@ cli_show_auto(clicon_handle h,
char *api_path_fmt; /* xml key format */ char *api_path_fmt; /* xml key format */
if (cvec_len(argv) < 2 || cvec_len(argv) > 7){ if (cvec_len(argv) < 2 || cvec_len(argv) > 7){
clicon_err(OE_PLUGIN, EINVAL, "Received %d arguments. Expected:: <api-path-fmt>* <database> [<format> <pretty> <state> <default> <cli-prefix>]", cvec_len(argv)); clicon_err(OE_PLUGIN, EINVAL, "Received %d arguments. Expected:: <api-path-fmt>* <database> [<format> <pretty> <state> <default> <prepend>]", cvec_len(argv));
goto done; goto done;
} }
api_path_fmt = cv_string_get(cvec_i(argv, argc++)); api_path_fmt = cv_string_get(cvec_i(argv, argc++));
@ -881,7 +881,7 @@ cli_show_auto(clicon_handle h,
goto done; goto done;
} }
if (cvec_len(argv) > argc){ if (cvec_len(argv) > argc){
prefix = cv_string_get(cvec_i(argv, argc++)); prepend = cv_string_get(cvec_i(argv, argc++));
} }
if ((yspec = clicon_dbspec_yang(h)) == NULL){ if ((yspec = clicon_dbspec_yang(h)) == NULL){
clicon_err(OE_FATAL, 0, "No DB_SPEC"); clicon_err(OE_FATAL, 0, "No DB_SPEC");
@ -897,7 +897,7 @@ cli_show_auto(clicon_handle h,
} }
if (cli_show_common(h, dbname, format, pretty, state, if (cli_show_common(h, dbname, format, pretty, state,
withdefault, extdefault, withdefault, extdefault,
prefix, xpath, nsc, 0) < 0) prepend, xpath, nsc, 0) < 0)
goto done; goto done;
retval = 0; retval = 0;
done: done:
@ -925,7 +925,7 @@ cli_show_auto(clicon_handle h,
* <state> true|false: also print state * <state> true|false: also print state
* <default> Retrieval mode: report-all, trim, explicit, report-all-tagged, * <default> Retrieval mode: report-all, trim, explicit, report-all-tagged,
* NULL, report-all-tagged-default, report-all-tagged-strip (extended) * NULL, report-all-tagged-default, report-all-tagged-strip (extended)
* <prefix> CLI prefix: prepend before cli syntax output * <prepend> CLI prefix: prepend before cli syntax output
* @code * @code
* clispec: * clispec:
* show config, cli_show_auto_mode("candidate"); * show config, cli_show_auto_mode("candidate");
@ -953,7 +953,7 @@ cli_show_auto_mode(clicon_handle h,
enum format_enum format = FORMAT_XML; enum format_enum format = FORMAT_XML;
cvec *nsc = NULL; cvec *nsc = NULL;
int pretty = 1; int pretty = 1;
char *prefix = NULL; char *prepend = NULL;
int state = 0; int state = 0;
char *withdefault = NULL; /* RFC 6243 modes */ char *withdefault = NULL; /* RFC 6243 modes */
char *extdefault = NULL; /* with extended tagged modes */ char *extdefault = NULL; /* with extended tagged modes */
@ -986,7 +986,7 @@ cli_show_auto_mode(clicon_handle h,
goto done; goto done;
} }
if (cvec_len(argv) > argc){ if (cvec_len(argv) > argc){
prefix = cv_string_get(cvec_i(argv, argc++)); prepend = cv_string_get(cvec_i(argv, argc++));
} }
/* Store this as edit-mode */ /* Store this as edit-mode */
if (clicon_data_get(h, "cli-edit-mode", &api_path) == 0 && strlen(api_path)) if (clicon_data_get(h, "cli-edit-mode", &api_path) == 0 && strlen(api_path))
@ -1006,7 +1006,7 @@ cli_show_auto_mode(clicon_handle h,
skiptop = (strcmp(xpath,"/") != 0); skiptop = (strcmp(xpath,"/") != 0);
if (cli_show_common(h, dbname, format, pretty, state, if (cli_show_common(h, dbname, format, pretty, state,
withdefault, extdefault, withdefault, extdefault,
prefix, xpath, nsc, skiptop) < 0) prepend, xpath, nsc, skiptop) < 0)
goto done; goto done;
retval = 0; retval = 0;
done: done:
@ -1017,6 +1017,7 @@ cli_show_auto_mode(clicon_handle h,
return retval; return retval;
} }
#if 1 // OBSOLETE
/*! Obsolete Show configuration callback for autocli edit modes using tree working point /*! Obsolete Show configuration callback for autocli edit modes using tree working point
* *
* @note Please use cli_show_auto_mode instead, * @note Please use cli_show_auto_mode instead,
@ -1050,6 +1051,7 @@ cli_auto_show(clicon_handle h,
cvec_free(argv1); cvec_free(argv1);
return retval; return retval;
} }
#endif
/*! Show clixon configuration options as loaded /*! Show clixon configuration options as loaded
*/ */

View file

@ -179,3 +179,8 @@
* // Here "s" is still open and you can reply on the non-ssl underlying socket * // Here "s" is still open and you can reply on the non-ssl underlying socket
*/ */
#define HTTP_ON_HTTPS_REPLY #define HTTP_ON_HTTPS_REPLY
/*! Indentation number of spaces for XML, JSON and TEXT pretty-printed output.
* Consider moving to configure.ac(compile-time) or to clixon-config.yang(run-time)
*/
#define PRETTYPRINT_INDENT 3

View file

@ -77,8 +77,6 @@
#include "clixon_json.h" #include "clixon_json.h"
#include "clixon_json_parse.h" #include "clixon_json_parse.h"
#define JSON_INDENT 2 /* maybe we should set this programmatically? */
/* Let xml2json_cbuf_vec() return json array: [a,b]. /* Let xml2json_cbuf_vec() return json array: [a,b].
ALternative is to create a pseudo-object and return that: {top:{a,b}} ALternative is to create a pseudo-object and return that: {top:{a,b}}
*/ */
@ -731,11 +729,11 @@ json_metadata_encoding(cbuf *cb,
cprintf(cb, "%s\":", name); cprintf(cb, "%s\":", name);
if (list) if (list)
cprintf(cb, "["); cprintf(cb, "[");
cprintf(cb, "%*s", pretty?((level+1)*JSON_INDENT):0, "{"); cprintf(cb, "%*s", pretty?((level+1)*PRETTYPRINT_INDENT):0, "{");
cprintf(cb, "\"%s:%s\":%s", modname2, name2, val); cprintf(cb, "\"%s:%s\":%s", modname2, name2, val);
cprintf(cb, "%*s", pretty?((level+1)*JSON_INDENT):0, "}"); cprintf(cb, "%*s", pretty?((level+1)*PRETTYPRINT_INDENT):0, "}");
if (list) if (list)
cprintf(cb, "%*s", pretty?(level*JSON_INDENT):0, "]"); cprintf(cb, "%*s", pretty?(level*PRETTYPRINT_INDENT):0, "]");
return 0; return 0;
} }
@ -896,7 +894,7 @@ xml2json1_cbuf(cbuf *cb,
break; break;
case NO_ARRAY: case NO_ARRAY:
if (!flat){ if (!flat){
cprintf(cb, "%*s\"", pretty?(level*JSON_INDENT):0, ""); cprintf(cb, "%*s\"", pretty?(level*PRETTYPRINT_INDENT):0, "");
if (modname) if (modname)
cprintf(cb, "%s:", modname); cprintf(cb, "%s:", modname);
cprintf(cb, "%s\":%s", xml_name(x), pretty?" ":""); cprintf(cb, "%s\":%s", xml_name(x), pretty?" ":"");
@ -917,14 +915,14 @@ xml2json1_cbuf(cbuf *cb,
break; break;
case FIRST_ARRAY: case FIRST_ARRAY:
case SINGLE_ARRAY: case SINGLE_ARRAY:
cprintf(cb, "%*s\"", pretty?(level*JSON_INDENT):0, ""); cprintf(cb, "%*s\"", pretty?(level*PRETTYPRINT_INDENT):0, "");
if (modname) if (modname)
cprintf(cb, "%s:", modname); cprintf(cb, "%s:", modname);
cprintf(cb, "%s\":%s", xml_name(x), pretty?" ":""); cprintf(cb, "%s\":%s", xml_name(x), pretty?" ":"");
level++; level++;
cprintf(cb, "[%s%*s", cprintf(cb, "[%s%*s",
pretty?"\n":"", pretty?"\n":"",
pretty?(level*JSON_INDENT):0, ""); pretty?(level*PRETTYPRINT_INDENT):0, "");
switch (childt){ switch (childt){
case NULL_CHILD: case NULL_CHILD:
if (nullchild(cb, x, ys) < 0) if (nullchild(cb, x, ys) < 0)
@ -943,7 +941,7 @@ xml2json1_cbuf(cbuf *cb,
case LAST_ARRAY: case LAST_ARRAY:
level++; level++;
cprintf(cb, "%*s", cprintf(cb, "%*s",
pretty?(level*JSON_INDENT):0, ""); pretty?(level*PRETTYPRINT_INDENT):0, "");
switch (childt){ switch (childt){
case NULL_CHILD: case NULL_CHILD:
if (nullchild(cb, x, ys) < 0) if (nullchild(cb, x, ys) < 0)
@ -1007,7 +1005,7 @@ xml2json1_cbuf(cbuf *cb,
case ANY_CHILD: case ANY_CHILD:
cprintf(cb, "%s%*s}", cprintf(cb, "%s%*s}",
pretty?"\n":"", pretty?"\n":"",
pretty?(level*JSON_INDENT):0, ""); pretty?(level*PRETTYPRINT_INDENT):0, "");
break; break;
default: default:
break; break;
@ -1023,7 +1021,7 @@ xml2json1_cbuf(cbuf *cb,
case ANY_CHILD: case ANY_CHILD:
cprintf(cb, "%s%*s}", cprintf(cb, "%s%*s}",
pretty?"\n":"", pretty?"\n":"",
pretty?(level*JSON_INDENT):0, ""); pretty?(level*PRETTYPRINT_INDENT):0, "");
level--; level--;
break; break;
default: default:
@ -1040,7 +1038,7 @@ xml2json1_cbuf(cbuf *cb,
case ANY_CHILD: case ANY_CHILD:
cprintf(cb, "%s%*s}", cprintf(cb, "%s%*s}",
pretty?"\n":"", pretty?"\n":"",
pretty?(level*JSON_INDENT):0, ""); pretty?(level*PRETTYPRINT_INDENT):0, "");
cprintf(cb, "%s",pretty?"\n":""); cprintf(cb, "%s",pretty?"\n":"");
level--; level--;
break; break;
@ -1048,7 +1046,7 @@ xml2json1_cbuf(cbuf *cb,
break; break;
} }
cprintf(cb, "%*s]", cprintf(cb, "%*s]",
pretty?(level*JSON_INDENT):0,""); pretty?(level*PRETTYPRINT_INDENT):0,"");
break; break;
default: default:
break; break;
@ -1095,7 +1093,7 @@ xml2json_cbuf1(cbuf *cb,
goto ok; goto ok;
} }
cprintf(cb, "%*s{%s", cprintf(cb, "%*s{%s",
pretty?level*JSON_INDENT:0,"", pretty?level*PRETTYPRINT_INDENT:0,"",
pretty?"\n":""); pretty?"\n":"");
if (y != NULL){ if (y != NULL){
@ -1120,7 +1118,7 @@ xml2json_cbuf1(cbuf *cb,
goto done; goto done;
cprintf(cb, "%s%*s}%s", cprintf(cb, "%s%*s}%s",
pretty?"\n":"", pretty?"\n":"",
pretty?level*JSON_INDENT:0,"", pretty?level*PRETTYPRINT_INDENT:0,"",
pretty?"\n":""); pretty?"\n":"");
ok: ok:
retval = 0; retval = 0;
@ -1239,11 +1237,7 @@ xml2json_cbuf_vec(cbuf *cb,
if (xml2json1_cbuf(cb, if (xml2json1_cbuf(cb,
xp, xp,
NO_ARRAY, NO_ARRAY,
#if 1
level, level,
#else
level+1,
#endif
pretty, pretty,
1, NULL, NULL) < 0) 1, NULL, NULL) < 0)
goto done; goto done;

View file

@ -71,8 +71,11 @@
/* Size of json read buffer when reading from file*/ /* Size of json read buffer when reading from file*/
#define BUFLEN 1024 #define BUFLEN 1024
/* Name of xml top object created by parse functions */ /* Name of xml top object created by parse functions
#define TOP_SYMBOL "top" * See also DATASTORE_TOP_SYMBOL which is the clixon datastore top symbol. By default also config
*/
#define TEXT_TOP_SYMBOL "top"
/*! x is element and has eactly one child which in turn has none /*! x is element and has eactly one child which in turn has none
* @see child_type in clixon_json.c * @see child_type in clixon_json.c
@ -100,7 +103,7 @@ tleaf(cxobj *x)
* @param[in] xn XML object to print * @param[in] xn XML object to print
* @param[in] fn Callback to make print function * @param[in] fn Callback to make print function
* @param[in] f File to print to * @param[in] f File to print to
* @param[in] level Print 4 spaces per level in front of each line * @param[in] level Print PRETTYPRINT_INDENT spaces per level in front of each line
* @param[in] autocliext How to handle autocli extensions: 0: ignore 1: follow * @param[in] autocliext How to handle autocli extensions: 0: ignore 1: follow
* @param[in,out] leafl Leaflist state for keeping track of when [] ends * @param[in,out] leafl Leaflist state for keeping track of when [] ends
* @param[in,out] leaflname Leaflist state for [] * @param[in,out] leaflname Leaflist state for []
@ -171,7 +174,7 @@ xml2txt1(cxobj *xn,
else{ else{
*leafl = 0; *leafl = 0;
*leaflname = NULL; *leaflname = NULL;
(*fn)(f, "%*s\n", 4*(level), "]"); (*fn)(f, "%*s\n", PRETTYPRINT_INDENT*(level), "]");
} }
} }
xc = NULL; /* count children (elements and bodies, not attributes) */ xc = NULL; /* count children (elements and bodies, not attributes) */
@ -191,13 +194,13 @@ xml2txt1(cxobj *xn,
else else
cprintf(cb, "%s", value); cprintf(cb, "%s", value);
if (*leafl) /* Skip keyword if leaflist */ if (*leafl) /* Skip keyword if leaflist */
(*fn)(f, "%*s%s\n", 4*level, "", cbuf_get(cb)); (*fn)(f, "%*s%s\n", PRETTYPRINT_INDENT*level, "", cbuf_get(cb));
else else
(*fn)(f, "%s;\n", cbuf_get(cb)); (*fn)(f, "%s;\n", cbuf_get(cb));
break; break;
} }
case CX_ELMNT: case CX_ELMNT:
(*fn)(f, "%*s%s", 4*level, "", xml_name(xn)); (*fn)(f, "%*s%s", PRETTYPRINT_INDENT*level, "", xml_name(xn));
cvi = NULL; /* Lists only */ cvi = NULL; /* Lists only */
while ((cvi = cvec_each(cvk, cvi)) != NULL) { while ((cvi = cvec_each(cvk, cvi)) != NULL) {
if ((xc = xml_find_type(xn, NULL, cv_string_get(cvi), CX_ELMNT)) != NULL) if ((xc = xml_find_type(xn, NULL, cv_string_get(cvi), CX_ELMNT)) != NULL)
@ -211,7 +214,7 @@ xml2txt1(cxobj *xn,
goto ok; goto ok;
} }
if (*leafl == 0){ if (*leafl == 0){
(*fn)(f, "%*s", 4*level, ""); (*fn)(f, "%*s", PRETTYPRINT_INDENT*level, "");
#ifndef TEXT_SYNTAX_NOPREFIX #ifndef TEXT_SYNTAX_NOPREFIX
if (prefix) if (prefix)
(*fn)(f, "%s:", prefix); (*fn)(f, "%s:", prefix);
@ -247,10 +250,10 @@ xml2txt1(cxobj *xn,
/* Stop leaf-list printing (ie []) if no longer leaflist and same name */ /* Stop leaf-list printing (ie []) if no longer leaflist and same name */
if (yn && yang_keyword_get(yn) != Y_LEAF_LIST && *leafl != 0){ if (yn && yang_keyword_get(yn) != Y_LEAF_LIST && *leafl != 0){
*leafl = 0; *leafl = 0;
(*fn)(f, "%*s\n", 4*(level+1), "]"); (*fn)(f, "%*s\n", PRETTYPRINT_INDENT*(level+1), "]");
} }
if (!tleaf(xn)) if (!tleaf(xn))
(*fn)(f, "%*s}\n", 4*level, ""); (*fn)(f, "%*s}\n", PRETTYPRINT_INDENT*level, "");
ok: ok:
retval = 0; retval = 0;
done: done:
@ -263,7 +266,7 @@ xml2txt1(cxobj *xn,
* *
* @param[in] f File to print to * @param[in] f File to print to
* @param[in] xn XML object to print * @param[in] xn XML object to print
* @param[in] level Print 4 spaces per level in front of each line * @param[in] level Print PRETTYPRINT_INDENT spaces per level in front of each line
* @param[in] fn File print function (if NULL, use fprintf) * @param[in] fn File print function (if NULL, use fprintf)
* @param[in] skiptop 0: Include top object 1: Skip top-object, only children, * @param[in] skiptop 0: Include top object 1: Skip top-object, only children,
* @param[in] autocliext How to handle autocli extensions: 0: ignore 1: follow * @param[in] autocliext How to handle autocli extensions: 0: ignore 1: follow
@ -563,7 +566,7 @@ clixon_text_syntax_parse_file(FILE *fp,
textbuf[len++] = ch; textbuf[len++] = ch;
if (ret == 0){ if (ret == 0){
if (*xt == NULL) if (*xt == NULL)
if ((*xt = xml_new(TOP_SYMBOL, NULL, CX_ELMNT)) == NULL) if ((*xt = xml_new(TEXT_TOP_SYMBOL, NULL, CX_ELMNT)) == NULL)
goto done; goto done;
if (len){ if (len){
if ((ret = _text_syntax_parse(ptr, yb, yspec, *xt, xerr)) < 0) if ((ret = _text_syntax_parse(ptr, yb, yspec, *xt, xerr)) < 0)

View file

@ -80,8 +80,6 @@
*/ */
/* Size of xml read buffer */ /* Size of xml read buffer */
#define BUFLEN 1024 #define BUFLEN 1024
/* Indentation for xml pretty-print. Consider option? */
#define XML_INDENT 3
/*------------------------------------------------------------------------ /*------------------------------------------------------------------------
* XML printing functions. Output a parse tree to file, string cligen buf * XML printing functions. Output a parse tree to file, string cligen buf
@ -147,7 +145,7 @@ xml2file_recurse(FILE *f,
(*fn)(f, "%s=\"%s\"", name, xml_value(x)); (*fn)(f, "%s=\"%s\"", name, xml_value(x));
break; break;
case CX_ELMNT: case CX_ELMNT:
(*fn)(f, "%*s<", pretty?(level*XML_INDENT):0, ""); (*fn)(f, "%*s<", pretty?(level*PRETTYPRINT_INDENT):0, "");
if (namespace) if (namespace)
(*fn)(f, "%s:", namespace); (*fn)(f, "%s:", namespace);
(*fn)(f, "%s", name); (*fn)(f, "%s", name);
@ -187,7 +185,7 @@ xml2file_recurse(FILE *f,
goto done; goto done;
} }
if (pretty && hasbody==0) if (pretty && hasbody==0)
(*fn)(f, "%*s", level*XML_INDENT, ""); (*fn)(f, "%*s", level*PRETTYPRINT_INDENT, "");
(*fn)(f, "</"); (*fn)(f, "</");
if (namespace) if (namespace)
(*fn)(f, "%s:", namespace); (*fn)(f, "%s:", namespace);
@ -249,7 +247,6 @@ clixon_xml2file(FILE *f,
retval = 0; retval = 0;
done: done:
return retval; return retval;
} }
/*! Print an XML tree structure to an output stream /*! Print an XML tree structure to an output stream
@ -356,7 +353,7 @@ clixon_xml2cbuf1(cbuf *cb,
break; break;
case CX_ELMNT: case CX_ELMNT:
if (pretty) if (pretty)
cprintf(cb, "%*s<", level*XML_INDENT, ""); cprintf(cb, "%*s<", level*PRETTYPRINT_INDENT, "");
else else
cbuf_append_str(cb, "<"); cbuf_append_str(cb, "<");
if (namespace){ if (namespace){
@ -396,7 +393,7 @@ clixon_xml2cbuf1(cbuf *cb,
if (clixon_xml2cbuf1(cb, xc, level+1, pretty, depth-1) < 0) if (clixon_xml2cbuf1(cb, xc, level+1, pretty, depth-1) < 0)
goto done; goto done;
if (pretty && hasbody == 0) if (pretty && hasbody == 0)
cprintf(cb, "%*s", level*XML_INDENT, ""); cprintf(cb, "%*s", level*PRETTYPRINT_INDENT, "");
cbuf_append_str(cb, "</"); cbuf_append_str(cb, "</");
if (namespace){ if (namespace){
cbuf_append_str(cb, namespace); cbuf_append_str(cb, namespace);
@ -476,7 +473,7 @@ xmltree2cbuf(cbuf *cb,
cxobj *xc; cxobj *xc;
int i; int i;
for (i=0; i<level*XML_INDENT; i++) for (i=0; i<level*PRETTYPRINT_INDENT; i++)
cprintf(cb, " "); cprintf(cb, " ");
if (xml_type(x) != CX_BODY) if (xml_type(x) != CX_BODY)
cprintf(cb, "%s", xml_type2str(xml_type(x))); cprintf(cb, "%s", xml_type2str(xml_type(x)));
@ -495,7 +492,7 @@ xmltree2cbuf(cbuf *cb,
while ((xc = xml_child_each(x, xc, -1)) != NULL) while ((xc = xml_child_each(x, xc, -1)) != NULL)
xmltree2cbuf(cb, xc, level+1); xmltree2cbuf(cb, xc, level+1);
if (xml_child_nr(x)){ if (xml_child_nr(x)){
for (i=0; i<level*XML_INDENT; i++) for (i=0; i<level*PRETTYPRINT_INDENT; i++)
cprintf(cb, " "); cprintf(cb, " ");
cprintf(cb, "}\n"); cprintf(cb, "}\n");
} }

View file

@ -2,8 +2,8 @@
# Autocli show tests # Autocli show tests
# Go through all formats and show for all formats # Go through all formats and show for all formats
# Formats: XML, JSON, TEXT, CLI, NETCONF # Formats: XML, JSON, TEXT, CLI, NETCONF
# Pretty-print: false, true XXXX: align spaces # Pretty-print: false, true, indentation-level is 3 (see PRETTYPRINT_INDENT)
# API: cli_show_auto_mode(), cli_show_auto(), cli_show_config() XXX # API: cli_show_auto_mode(), cli_show_auto(), cli_show_config()
# Magic line must be first in script (see README.md) # Magic line must be first in script (see README.md)
s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi

File diff suppressed because it is too large Load diff