* Changed first parameter from int fd to FILE *f in the following functions:

* clixon_xml_parse_file(), clixon_json_parse_file(), yang_parse_file()
  * See [Bytewise read() of files is slow #146](https://github.com/clicon/clixon/issues/146)
This commit is contained in:
Olof hagsand 2020-11-04 22:21:42 +01:00
parent 7a0838da3a
commit c31b1c471b
21 changed files with 113 additions and 99 deletions

View file

@ -44,8 +44,17 @@ Users may have to change how they access the system
* New clixon-config@2020-11-03.yang revision * New clixon-config@2020-11-03.yang revision
* Added option: `CLICON_RESTCONF_CONFIG` for reading restconf daemon config frm datastore * Added option: `CLICON_RESTCONF_CONFIG` for reading restconf daemon config frm datastore
### C/CLI-API changes on existing features
Developers may need to change their code
* Changed first parameter from `int fd` to `FILE *f` in the following functions:
* clixon_xml_parse_file(), clixon_json_parse_file(), yang_parse_file()
* See [Bytewise read() of files is slow #146](https://github.com/clicon/clixon/issues/146)
### Minor changes ### Minor changes
* Improved performance of parsing files as described in [Bytewise read() of files is slow #146](https://github.com/clicon/clixon/issues/146), thanks: @hjelmeland
* Added new backend plugin: ca_pre-demon called if backend is daemonized just prior to forking. * Added new backend plugin: ca_pre-demon called if backend is daemonized just prior to forking.
* Added XPATH functions `position` * Added XPATH functions `position`

View file

@ -186,7 +186,6 @@ nacm_load_external(clicon_handle h)
cxobj *xt = NULL; cxobj *xt = NULL;
struct stat st; struct stat st;
FILE *f = NULL; FILE *f = NULL;
int fd;
filename = clicon_option_str(h, "CLICON_NACM_FILE"); filename = clicon_option_str(h, "CLICON_NACM_FILE");
if (filename == NULL || strlen(filename)==0){ if (filename == NULL || strlen(filename)==0){
@ -209,9 +208,8 @@ nacm_load_external(clicon_handle h)
goto done; goto done;
if (yang_spec_parse_module(h, "ietf-netconf-acm", NULL, yspec) < 0) if (yang_spec_parse_module(h, "ietf-netconf-acm", NULL, yspec) < 0)
goto done; goto done;
fd = fileno(f);
/* Read configfile */ /* Read configfile */
if (clixon_xml_parse_file(fd, YB_MODULE, yspec, NULL, &xt, NULL) < 0) if (clixon_xml_parse_file(f, YB_MODULE, yspec, NULL, &xt, NULL) < 0)
goto done; goto done;
if (xt == NULL){ if (xt == NULL){
clicon_err(OE_XML, 0, "No xml tree in %s", filename); clicon_err(OE_XML, 0, "No xml tree in %s", filename);

View file

@ -168,17 +168,17 @@ load_extraxml(clicon_handle h,
{ {
int retval = -1; int retval = -1;
cxobj *xt = NULL; cxobj *xt = NULL;
int fd = -1; FILE *fp = NULL;
yang_stmt *yspec = NULL; yang_stmt *yspec = NULL;
if (filename == NULL) if (filename == NULL)
return 1; return 1;
if ((fd = open(filename, O_RDONLY)) < 0){ if ((fp = fopen(filename, "r")) < 0){
clicon_err(OE_UNIX, errno, "open(%s)", filename); clicon_err(OE_UNIX, errno, "open(%s)", filename);
goto done; goto done;
} }
yspec = clicon_dbspec_yang(h); yspec = clicon_dbspec_yang(h);
if (clixon_xml_parse_file(fd, YB_MODULE, yspec, NULL, &xt, NULL) < 0) if (clixon_xml_parse_file(fp, YB_MODULE, yspec, NULL, &xt, NULL) < 0)
goto done; goto done;
/* Replace parent w first child */ /* Replace parent w first child */
if (xml_rootchild(xt, 0, &xt) < 0) if (xml_rootchild(xt, 0, &xt) < 0)
@ -186,8 +186,8 @@ load_extraxml(clicon_handle h,
/* Merge user reset state */ /* Merge user reset state */
retval = xmldb_put(h, (char*)db, OP_MERGE, xt, clicon_username_get(h), cbret); retval = xmldb_put(h, (char*)db, OP_MERGE, xt, clicon_username_get(h), cbret);
done: done:
if (fd != -1) if (fp)
close(fd); fclose(fp);
if (xt) if (xt)
xml_free(xt); xml_free(xt);
return retval; return retval;

View file

@ -771,7 +771,7 @@ load_config_file(clicon_handle h,
cg_var *cv; cg_var *cv;
char *opstr; char *opstr;
char *varstr; char *varstr;
int fd = -1; FILE *fp = NULL;
cxobj *xt = NULL; cxobj *xt = NULL;
cxobj *x; cxobj *x;
cbuf *cbxml; cbuf *cbxml;
@ -804,11 +804,11 @@ load_config_file(clicon_handle h,
goto done; goto done;
} }
/* Open and parse local file into xml */ /* Open and parse local file into xml */
if ((fd = open(filename, O_RDONLY)) < 0){ if ((fp = fopen(filename, "r")) < 0){
clicon_err(OE_UNIX, errno, "open(%s)", filename); clicon_err(OE_UNIX, errno, "open(%s)", filename);
goto done; goto done;
} }
if (clixon_xml_parse_file(fd, YB_NONE, NULL, NULL, &xt, NULL) < 0) if (clixon_xml_parse_file(fp, YB_NONE, NULL, NULL, &xt, NULL) < 0)
goto done; goto done;
if (xt == NULL) if (xt == NULL)
goto done; goto done;
@ -831,8 +831,8 @@ load_config_file(clicon_handle h,
done: done:
if (xt) if (xt)
xml_free(xt); xml_free(xt);
if (fd != -1) if (fp)
close(fd); fclose(fp);
return ret; return ret;
} }

View file

@ -370,7 +370,7 @@ example_statedata(clicon_handle h,
cvec *nsc1 = NULL; cvec *nsc1 = NULL;
cvec *nsc2 = NULL; cvec *nsc2 = NULL;
yang_stmt *yspec = NULL; yang_stmt *yspec = NULL;
int fd; FILE *fp = NULL;
if (!_state) if (!_state)
goto ok; goto ok;
@ -395,15 +395,14 @@ example_statedata(clicon_handle h,
} }
else{ else{
cxobj *x1; cxobj *x1;
if ((fd = open(_state_file, O_RDONLY)) < 0){ if ((fp = fopen(_state_file, "r")) < 0){
clicon_err(OE_UNIX, errno, "open(%s)", _state_file); clicon_err(OE_UNIX, errno, "open(%s)", _state_file);
goto done; goto done;
} }
if ((xt = xml_new("config", NULL, CX_ELMNT)) == NULL) if ((xt = xml_new("config", NULL, CX_ELMNT)) == NULL)
goto done; goto done;
if (clixon_xml_parse_file(fd, YB_MODULE, yspec, NULL, &xt, NULL) < 0) if (clixon_xml_parse_file(fp, YB_MODULE, yspec, NULL, &xt, NULL) < 0)
goto done; goto done;
close(fd);
if (xpath_vec(xt, nsc, "%s", &xvec, &xlen, xpath) < 0) if (xpath_vec(xt, nsc, "%s", &xvec, &xlen, xpath) < 0)
goto done; goto done;
for (i=0; i<xlen; i++){ for (i=0; i<xlen; i++){
@ -477,6 +476,8 @@ example_statedata(clicon_handle h,
ok: ok:
retval = 0; retval = 0;
done: done:
if (fp)
fclose(fp);
if (nsc1) if (nsc1)
xml_nsctx_free(nsc1); xml_nsctx_free(nsc1);
if (nsc2) if (nsc2)
@ -968,21 +969,20 @@ example_start(clicon_handle h)
int int
example_daemon(clicon_handle h) example_daemon(clicon_handle h)
{ {
int retval = -1; int retval = -1;
int ret; int ret;
FILE *fp = NULL;
yang_stmt *yspec;
/* Read state file (or should this be in init/start?) */ /* Read state file (or should this be in init/start?) */
if (_state && _state_file && _state_file_init){ if (_state && _state_file && _state_file_init){
int fd; yspec = clicon_dbspec_yang(h);
yang_stmt *yspec = clicon_dbspec_yang(h); if ((fp = fopen(_state_file, "r")) < 0){
if ((fd = open(_state_file, O_RDONLY)) < 0){
clicon_err(OE_UNIX, errno, "open(%s)", _state_file); clicon_err(OE_UNIX, errno, "open(%s)", _state_file);
goto done; goto done;
} }
if ((ret = clixon_xml_parse_file(fd, YB_MODULE, yspec, NULL, &_state_xstate, NULL)) < 0) if ((ret = clixon_xml_parse_file(fp, YB_MODULE, yspec, NULL, &_state_xstate, NULL)) < 0)
goto done; goto done;
close(fd);
if (ret == 0){ if (ret == 0){
fprintf(stderr, "%s error\n", __FUNCTION__); fprintf(stderr, "%s error\n", __FUNCTION__);
goto done; goto done;
@ -991,6 +991,8 @@ example_daemon(clicon_handle h)
} }
retval = 0; retval = 0;
done: done:
if (fp)
fclose(fp);
return retval; return retval;
} }

View file

@ -51,6 +51,6 @@ int xml2json_cb(FILE *f, cxobj *x, int pretty, clicon_output_cb *fn);
int json_print(FILE *f, cxobj *x); int json_print(FILE *f, cxobj *x);
int xml2json_vec(FILE *f, cxobj **vec, size_t veclen, int pretty); int xml2json_vec(FILE *f, cxobj **vec, size_t veclen, int pretty);
int clixon_json_parse_string(char *str, yang_bind yb, yang_stmt *yspec, cxobj **xt, cxobj **xret); int clixon_json_parse_string(char *str, yang_bind yb, yang_stmt *yspec, cxobj **xt, cxobj **xret);
int clixon_json_parse_file(int fd, yang_bind yb, yang_stmt *yspec, cxobj **xt, cxobj **xret); int clixon_json_parse_file(FILE *fp, yang_bind yb, yang_stmt *yspec, cxobj **xt, cxobj **xret);
#endif /* _CLIXON_JSON_H */ #endif /* _CLIXON_JSON_H */

View file

@ -50,7 +50,7 @@ int clicon_xml2cbuf(cbuf *cb, cxobj *x, int level, int prettyprint, int32_t dept
char *clicon_xml2str(cxobj *x); char *clicon_xml2str(cxobj *x);
int xmltree2cbuf(cbuf *cb, cxobj *x, int level); int xmltree2cbuf(cbuf *cb, cxobj *x, int level);
int clixon_xml_parse_file(int fd, yang_bind yb, yang_stmt *yspec, char *endtag, cxobj **xt, cxobj **xerr); int clixon_xml_parse_file(FILE *f, yang_bind yb, yang_stmt *yspec, char *endtag, cxobj **xt, cxobj **xerr);
int clixon_xml_parse_string(const char *str, yang_bind yb, yang_stmt *yspec, cxobj **xt, cxobj **xerr); int clixon_xml_parse_string(const char *str, yang_bind yb, yang_stmt *yspec, cxobj **xt, cxobj **xerr);
#if defined(__GNUC__) && __GNUC__ >= 3 #if defined(__GNUC__) && __GNUC__ >= 3

View file

@ -51,7 +51,7 @@
/* /*
* Prototypes * Prototypes
*/ */
yang_stmt *yang_parse_file(int fd, const char *name, yang_stmt *ysp); yang_stmt *yang_parse_file(FILE *fp, const char *name, yang_stmt *ysp);
yang_stmt *yang_parse_filename(const char *filename, yang_stmt *ysp); yang_stmt *yang_parse_filename(const char *filename, yang_stmt *ysp);
int yang_spec_parse_module(clicon_handle h, const char *module, int yang_spec_parse_module(clicon_handle h, const char *module,
const char *revision, yang_stmt *yspec); const char *revision, yang_stmt *yspec);

View file

@ -438,7 +438,7 @@ xmldb_readfile(clicon_handle h,
int retval = -1; int retval = -1;
cxobj *x0 = NULL; cxobj *x0 = NULL;
char *dbfile = NULL; char *dbfile = NULL;
int fd = -1; FILE *fp = NULL;
char *format; char *format;
int ret; int ret;
@ -453,15 +453,15 @@ xmldb_readfile(clicon_handle h,
goto done; goto done;
} }
/* Parse file into internal XML tree from different formats */ /* Parse file into internal XML tree from different formats */
if ((fd = open(dbfile, O_RDONLY)) < 0) { if ((fp = fopen(dbfile, "r")) < 0) {
clicon_err(OE_UNIX, errno, "open(%s)", dbfile); clicon_err(OE_UNIX, errno, "open(%s)", dbfile);
goto done; goto done;
} }
if (strcmp(format, "json")==0){ if (strcmp(format, "json")==0){
if ((ret = clixon_json_parse_file(fd, yb, yspec, &x0, NULL)) < 0) /* XXX: ret == 0*/ if ((ret = clixon_json_parse_file(fp, yb, yspec, &x0, NULL)) < 0) /* XXX: ret == 0*/
goto done; goto done;
} }
else if ((ret = clixon_xml_parse_file(fd, yb, yspec, "</config>", &x0, NULL)) < 0) else if ((ret = clixon_xml_parse_file(fp, yb, yspec, "</config>", &x0, NULL)) < 0)
goto done; goto done;
#ifdef XMLDB_READFILE_FAIL /* The functions calling this function cannot handle a failed parse yet */ #ifdef XMLDB_READFILE_FAIL /* The functions calling this function cannot handle a failed parse yet */
if (ret == 0) if (ret == 0)
@ -495,8 +495,8 @@ xmldb_readfile(clicon_handle h,
} }
retval = 1; retval = 1;
done: done:
if (fd != -1) if (fp)
close(fd); fclose(fp);
if (dbfile) if (dbfile)
free(dbfile); free(dbfile);
if (x0) if (x0)

View file

@ -1367,14 +1367,14 @@ clixon_json_parse_string(char *str,
* eg: name="a:b" -> prefix="a", name="b" * eg: name="a:b" -> prefix="a", name="b"
* But this is not done if yspec=NULL, and is not part of the JSON spec * But this is not done if yspec=NULL, and is not part of the JSON spec
* *
* @param[in] fd File descriptor to the JSON file (ASCII string) * @param[in] fp File descriptor to the JSON file (ASCII string)
* @param[in] yspec Yang specification, or NULL * @param[in] yspec Yang specification, or NULL
* @param[in,out] xt Pointer to (XML) parse tree. If empty, create. * @param[in,out] xt Pointer to (XML) parse tree. If empty, create.
* @param[out] xerr Reason for invalid returned as netconf err msg * @param[out] xerr Reason for invalid returned as netconf err msg
* *
* @code * @code
* cxobj *xt = NULL; * cxobj *xt = NULL;
* if (clixon_json_parse_file(0, YB_MODULE, yspec, &xt) < 0) * if (clixon_json_parse_file(stdin, YB_MODULE, yspec, &xt) < 0)
* err; * err;
* xml_free(xt); * xml_free(xt);
* @endcode * @endcode
@ -1390,7 +1390,7 @@ clixon_json_parse_string(char *str,
* @see RFC7951 * @see RFC7951
*/ */
int int
clixon_json_parse_file(int fd, clixon_json_parse_file(FILE *fp,
yang_bind yb, yang_bind yb,
yang_stmt *yspec, yang_stmt *yspec,
cxobj **xt, cxobj **xt,
@ -1404,7 +1404,7 @@ clixon_json_parse_file(int fd,
char *ptr; char *ptr;
char ch; char ch;
int len = 0; int len = 0;
if (xt==NULL){ if (xt==NULL){
clicon_err(OE_XML, EINVAL, "xt is NULL"); clicon_err(OE_XML, EINVAL, "xt is NULL");
return -1; return -1;
@ -1416,7 +1416,7 @@ clixon_json_parse_file(int fd,
memset(jsonbuf, 0, jsonbuflen); memset(jsonbuf, 0, jsonbuflen);
ptr = jsonbuf; ptr = jsonbuf;
while (1){ while (1){
if ((ret = read(fd, &ch, 1)) < 0){ if ((ret = fread(&ch, 1, 1, fp)) < 0){
clicon_err(OE_XML, errno, "read"); clicon_err(OE_XML, errno, "read");
break; break;
} }

View file

@ -204,19 +204,19 @@ parse_configfile_one(const char *filename,
cxobj **xconfig) cxobj **xconfig)
{ {
int retval = -1; int retval = -1;
int fd = -1; FILE *fp = NULL;
cxobj *xt = NULL; cxobj *xt = NULL;
cxobj *xerr = NULL; cxobj *xerr = NULL;
cxobj *xa; cxobj *xa;
cbuf *cbret = NULL; cbuf *cbret = NULL;
int ret; int ret;
if ((fd = open(filename, O_RDONLY)) < 0){ if ((fp = fopen(filename, "r")) < 0){
clicon_err(OE_UNIX, errno, "open configure file: %s", filename); clicon_err(OE_UNIX, errno, "open configure file: %s", filename);
return -1; return -1;
} }
clicon_debug(2, "%s: Reading config file %s", __FUNCTION__, filename); clicon_debug(2, "%s: Reading config file %s", __FUNCTION__, filename);
if ((ret = clixon_xml_parse_file(fd, yspec?YB_MODULE:YB_NONE, yspec, NULL, &xt, &xerr)) < 0) if ((ret = clixon_xml_parse_file(fp, yspec?YB_MODULE:YB_NONE, yspec, NULL, &xt, &xerr)) < 0)
goto done; goto done;
if (ret == 0){ if (ret == 0){
if ((cbret = cbuf_new()) ==NULL){ if ((cbret = cbuf_new()) ==NULL){
@ -249,8 +249,8 @@ parse_configfile_one(const char *filename,
done: done:
if (xt) if (xt)
xml_free(xt); xml_free(xt);
if (fd != -1) if (fp)
close(fd); fclose(fp);
if (cbret) if (cbret)
cbuf_free(cbret); cbuf_free(cbret);
if (xerr) if (xerr)

View file

@ -436,7 +436,7 @@ clixon_xml_changelog_init(clicon_handle h)
{ {
int retval = -1; int retval = -1;
char *filename; char *filename;
int fd = -1; FILE *fp = NULL;
cxobj *xt = NULL; cxobj *xt = NULL;
yang_stmt *yspec; yang_stmt *yspec;
int ret; int ret;
@ -445,11 +445,11 @@ clixon_xml_changelog_init(clicon_handle h)
yspec = clicon_dbspec_yang(h); yspec = clicon_dbspec_yang(h);
if ((filename = clicon_option_str(h, "CLICON_XML_CHANGELOG_FILE")) != NULL){ if ((filename = clicon_option_str(h, "CLICON_XML_CHANGELOG_FILE")) != NULL){
if ((fd = open(filename, O_RDONLY)) < 0){ if ((fp = fopen(filename, "r")) < 0){
clicon_err(OE_UNIX, errno, "open(%s)", filename); clicon_err(OE_UNIX, errno, "fopen(%s)", filename);
goto done; goto done;
} }
if (clixon_xml_parse_file(fd, YB_MODULE, yspec, NULL, &xt, NULL) < 0) if (clixon_xml_parse_file(fp, YB_MODULE, yspec, NULL, &xt, NULL) < 0)
goto done; goto done;
if (xml_rootchild(xt, 0, &xt) < 0) if (xml_rootchild(xt, 0, &xt) < 0)
goto done; goto done;
@ -477,8 +477,8 @@ clixon_xml_changelog_init(clicon_handle h)
cbuf_free(cbret); cbuf_free(cbret);
if (xret) if (xret)
xml_free(xret); xml_free(xret);
if (fd != -1) if (fp)
close(fd); fclose(fp);
if (xt) if (xt)
xml_free(xt); xml_free(xt);
return retval; return retval;

View file

@ -578,9 +578,9 @@ FSM(char *tag,
* @code * @code
* cxobj *xt = NULL; * cxobj *xt = NULL;
* cxobj *xerr = NULL; * cxobj *xerr = NULL;
* int fd; * FILE *f;
* fd = open(filename, O_RDONLY); * f = fopen(filename, "r");
* if ((ret = clixon_xml_parse_file(fd, YB_MODULE, yspec, "</config>", &xt, &xerr)) < 0) * if ((ret = clixon_xml_parse_file(f, YB_MODULE, yspec, "</config>", &xt, &xerr)) < 0)
* err; * err;
* xml_free(xt); * xml_free(xt);
* @endcode * @endcode
@ -590,7 +590,7 @@ FSM(char *tag,
* @note May block on file I/O * @note May block on file I/O
*/ */
int int
clixon_xml_parse_file(int fd, clixon_xml_parse_file(FILE *fp,
yang_bind yb, yang_bind yb,
yang_stmt *yspec, yang_stmt *yspec,
char *endtag, char *endtag,
@ -626,9 +626,8 @@ clixon_xml_parse_file(int fd,
memset(xmlbuf, 0, xmlbuflen); memset(xmlbuf, 0, xmlbuflen);
ptr = xmlbuf; ptr = xmlbuf;
while (1){ while (1){
if ((ret = read(fd, &ch, 1)) < 0){ if ((ret = fread(&ch, 1, 1, fp)) < 0){
clicon_err(OE_XML, errno, "read: [pid:%d]", clicon_err(OE_XML, errno, "read");
(int)getpid());
break; break;
} }
if (ret != 0){ if (ret != 0){

View file

@ -704,7 +704,7 @@ yang_parse_str(char *str,
* @note this function simply parse a yang spec, no dependencies or checks * @note this function simply parse a yang spec, no dependencies or checks
*/ */
yang_stmt * yang_stmt *
yang_parse_file(int fd, yang_parse_file(FILE *fp,
const char *name, const char *name,
yang_stmt *yspec) yang_stmt *yspec)
{ {
@ -723,7 +723,7 @@ yang_parse_file(int fd,
memset(buf, 0, len); memset(buf, 0, len);
i = 0; /* position in buf */ i = 0; /* position in buf */
while (1){ /* read the whole file */ while (1){ /* read the whole file */
if ((ret = read(fd, &c, 1)) < 0){ if ((ret = fread(&c, 1, 1, fp)) < 0){
clicon_err(OE_XML, errno, "read"); clicon_err(OE_XML, errno, "read");
break; break;
} }
@ -872,7 +872,7 @@ yang_parse_filename(const char *filename,
yang_stmt *yspec) yang_stmt *yspec)
{ {
yang_stmt *ymod = NULL; yang_stmt *ymod = NULL;
int fd = -1; FILE *fp = NULL;
struct stat st; struct stat st;
clicon_debug(1, "%s %s", __FUNCTION__, filename); clicon_debug(1, "%s %s", __FUNCTION__, filename);
@ -880,15 +880,15 @@ yang_parse_filename(const char *filename,
clicon_err(OE_YANG, errno, "%s not found", filename); clicon_err(OE_YANG, errno, "%s not found", filename);
goto done; goto done;
} }
if ((fd = open(filename, O_RDONLY)) < 0){ if ((fp = fopen(filename, "r")) == NULL){
clicon_err(OE_YANG, errno, "open(%s)", filename); clicon_err(OE_YANG, errno, "fopen(%s)", filename);
goto done; goto done;
} }
if ((ymod = yang_parse_file(fd, filename, yspec)) < 0) if ((ymod = yang_parse_file(fp, filename, yspec)) < 0)
goto done; goto done;
done: done:
if (fd != -1) if (fp)
close(fd); fclose(fp);
return ymod; /* top-level (sub)module */ return ymod; /* top-level (sub)module */
} }

View file

@ -250,14 +250,14 @@ main(int argc, char **argv)
usage(argv0); usage(argv0);
} }
if (argc == 2){ if (argc == 2){
int fd; FILE *fp;
if ((fd = open(xmlfilename, O_RDONLY)) < 0){ if ((fp = fopen(xmlfilename, "r")) < 0){
clicon_err(OE_UNIX, errno, "open(%s)", xmlfilename); clicon_err(OE_UNIX, errno, "fopen(%s)", xmlfilename);
goto done; goto done;
} }
if (clixon_xml_parse_file(fd, YB_MODULE, yspec, NULL, &xt, NULL) < 0) if (clixon_xml_parse_file(fp, YB_MODULE, yspec, NULL, &xt, NULL) < 0)
goto done; goto done;
close(fd); fclose(fp);
} }
else else
if (clixon_xml_parse_string(argv[2], YB_MODULE, yspec, &xt, NULL) < 0) if (clixon_xml_parse_string(argv[2], YB_MODULE, yspec, &xt, NULL) < 0)

View file

@ -137,7 +137,7 @@ main(int argc,
return -1; return -1;
} }
} }
if ((ret = clixon_json_parse_file(0, yspec?YB_MODULE:YB_NONE, yspec, &xt, &xerr)) < 0) if ((ret = clixon_json_parse_file(stdin, yspec?YB_MODULE:YB_NONE, yspec, &xt, &xerr)) < 0)
goto done; goto done;
if (ret == 0){ if (ret == 0){
xml_print(stderr, xerr); xml_print(stderr, xerr);

View file

@ -97,7 +97,7 @@ main(int argc,
int len; int len;
char *buf = NULL; char *buf = NULL;
int ret; int ret;
int fd = 0; /* unless overriden by argv[1] */ FILE *fp = stdin; /* unless overriden by argv[1] */
char *yang_file_dir = NULL; char *yang_file_dir = NULL;
yang_stmt *yspec = NULL; yang_stmt *yspec = NULL;
char *path = NULL; char *path = NULL;
@ -135,8 +135,8 @@ main(int argc,
break; break;
case 'f': /* XML file */ case 'f': /* XML file */
filename = optarg; filename = optarg;
if ((fd = open(filename, O_RDONLY)) < 0){ if ((fp = fopen(filename, "r")) < 0){
clicon_err(OE_UNIX, errno, "open(%s)", optarg); clicon_err(OE_UNIX, errno, "fopen(%s)", optarg);
goto done; goto done;
} }
break; break;
@ -212,10 +212,10 @@ main(int argc,
} }
/* /*
* If fd=0, then continue reading from stdin (after CR) * If fp=stdin, then continue reading from stdin (after CR)
* If fd>0, reading from file opened as argv[1] * XXX Note 0 above, stdin here
*/ */
if (clixon_xml_parse_file(fd, YB_NONE, NULL, NULL, &x, NULL) < 0){ if (clixon_xml_parse_file(fp, YB_NONE, NULL, NULL, &x, NULL) < 0){
fprintf(stderr, "Error: parsing: %s\n", clicon_err_reason); fprintf(stderr, "Error: parsing: %s\n", clicon_err_reason);
return -1; return -1;
} }
@ -297,8 +297,8 @@ main(int argc,
xml_free(x); xml_free(x);
if (xcfg) if (xcfg)
xml_free(xcfg); xml_free(xcfg);
if (fd > 0) if (fp)
close(fd); fclose(fp);
if (h) if (h)
clicon_handle_exit(h); clicon_handle_exit(h);
return retval; return retval;

View file

@ -87,7 +87,7 @@ main(int argc,
char *retdata = NULL; char *retdata = NULL;
int jsonin = 0; int jsonin = 0;
char *input_filename = NULL; char *input_filename = NULL;
int fd = 0; /* stdin */ FILE *fp = stdin;
cxobj *xt = NULL; cxobj *xt = NULL;
cxobj *xc; cxobj *xc;
cxobj *xerr = NULL; cxobj *xerr = NULL;
@ -138,14 +138,14 @@ main(int argc,
usage(argv[0]); usage(argv[0]);
} }
if (input_filename){ if (input_filename){
if ((fd = open(input_filename, O_RDONLY)) < 0){ if ((fp = fopen(input_filename, "r")) < 0){
clicon_err(OE_YANG, errno, "open(%s)", input_filename); clicon_err(OE_YANG, errno, "open(%s)", input_filename);
goto done; goto done;
} }
} }
/* 2. Parse data (xml/json) */ /* 2. Parse data (xml/json) */
if (jsonin){ if (jsonin){
if ((ret = clixon_json_parse_file(fd, YB_NONE, NULL, &xt, &xerr)) < 0) if ((ret = clixon_json_parse_file(fp, YB_NONE, NULL, &xt, &xerr)) < 0)
goto done; goto done;
if (ret == 0){ if (ret == 0){
fprintf(stderr, "Invalid JSON\n"); fprintf(stderr, "Invalid JSON\n");
@ -153,7 +153,7 @@ main(int argc,
} }
} }
else{ else{
if (clixon_xml_parse_file(fd, YB_NONE, NULL, NULL, &xt, NULL) < 0){ if (clixon_xml_parse_file(fp, YB_NONE, NULL, NULL, &xt, NULL) < 0){
fprintf(stderr, "xml parse error: %s\n", clicon_err_reason); fprintf(stderr, "xml parse error: %s\n", clicon_err_reason);
goto done; goto done;
} }
@ -176,6 +176,8 @@ main(int argc,
fprintf(stdout, "%s\n", retdata); fprintf(stdout, "%s\n", retdata);
retval = 0; retval = 0;
done: done:
if (fp)
fclose(fp);
if (xerr) if (xerr)
xml_free(xerr); xml_free(xerr);
if (xt) if (xt)

View file

@ -154,8 +154,8 @@ main(int argc,
int output = 0; int output = 0;
clicon_handle h; clicon_handle h;
struct stat st; struct stat st;
int fd = 0; /* base file, stdin */ FILE *fp = stdin; /* base file, stdin */
int tfd = -1; /* top file */ FILE *tfp = NULL; /* top file */
cxobj *xcfg = NULL; cxobj *xcfg = NULL;
cbuf *cbret = NULL; cbuf *cbret = NULL;
cxobj *xtop = NULL; /* Top tree if any */ cxobj *xtop = NULL; /* Top tree if any */
@ -263,11 +263,11 @@ main(int argc,
* Always validated * Always validated
*/ */
if (top_input_filename){ if (top_input_filename){
if ((tfd = open(top_input_filename, O_RDONLY)) < 0){ if ((tfp = fopen(top_input_filename, "r")) < 0){
clicon_err(OE_YANG, errno, "open(%s)", top_input_filename); clicon_err(OE_YANG, errno, "fopen(%s)", top_input_filename);
goto done; goto done;
} }
if ((ret = clixon_xml_parse_file(tfd, YB_MODULE, yspec, NULL, &xtop, &xerr)) < 0){ if ((ret = clixon_xml_parse_file(tfp, YB_MODULE, yspec, NULL, &xtop, &xerr)) < 0){
fprintf(stderr, "xml parse error: %s\n", clicon_err_reason); fprintf(stderr, "xml parse error: %s\n", clicon_err_reason);
goto done; goto done;
} }
@ -288,14 +288,14 @@ main(int argc,
xt = xbot; xt = xbot;
} }
if (input_filename){ if (input_filename){
if ((fd = open(input_filename, O_RDONLY)) < 0){ if ((fp = fopen(input_filename, "r")) < 0){
clicon_err(OE_YANG, errno, "open(%s)", input_filename); clicon_err(OE_YANG, errno, "open(%s)", input_filename);
goto done; goto done;
} }
} }
/* 2. Parse data (xml/json) */ /* 2. Parse data (xml/json) */
if (jsonin){ if (jsonin){
if ((ret = clixon_json_parse_file(fd, top_input_filename?YB_PARENT:YB_MODULE, yspec, &xt, &xerr)) < 0) if ((ret = clixon_json_parse_file(fp, top_input_filename?YB_PARENT:YB_MODULE, yspec, &xt, &xerr)) < 0)
goto done; goto done;
if (ret == 0){ if (ret == 0){
clixon_netconf_error(xerr, "util_xml", NULL); clixon_netconf_error(xerr, "util_xml", NULL);
@ -309,7 +309,7 @@ main(int argc,
yb = YB_MODULE; yb = YB_MODULE;
else else
yb = YB_PARENT; yb = YB_PARENT;
if ((ret = clixon_xml_parse_file(fd, yb, yspec, NULL, &xt, &xerr)) < 0){ if ((ret = clixon_xml_parse_file(fp, yb, yspec, NULL, &xt, &xerr)) < 0){
fprintf(stderr, "xml parse error: %s\n", clicon_err_reason); fprintf(stderr, "xml parse error: %s\n", clicon_err_reason);
goto done; goto done;
} }
@ -345,6 +345,10 @@ main(int argc,
} }
retval = 0; retval = 0;
done: done:
if (tfp)
fclose(tfp);
if (fp)
fclose(fp);
if (nsc) if (nsc)
cvec_free(nsc); cvec_free(nsc);
if (cbret) if (cbret)

View file

@ -126,7 +126,7 @@ main(int argc,
int len; int len;
char *buf = NULL; char *buf = NULL;
int ret; int ret;
int fd = 0; /* unless overriden by argv[1] */ FILE *fp = stdin; /* unless overriden by argv[1] */
char *yang_file_dir = NULL; char *yang_file_dir = NULL;
yang_stmt *yspec = NULL; yang_stmt *yspec = NULL;
char *xpath = NULL; char *xpath = NULL;
@ -168,7 +168,7 @@ main(int argc,
break; break;
case 'f': /* XML file */ case 'f': /* XML file */
filename = optarg; filename = optarg;
if ((fd = open(filename, O_RDONLY)) < 0){ if ((fp = fopen(filename, "r")) < 0){
clicon_err(OE_UNIX, errno, "open(%s)", argv[1]); clicon_err(OE_UNIX, errno, "open(%s)", argv[1]);
goto done; goto done;
} }
@ -295,10 +295,10 @@ main(int argc,
} }
/* /*
* If fd=0, then continue reading from stdin (after CR) * If fp=stdin, then continue reading from stdin (after CR)
* If fd>0, reading from file opened as argv[1] * XXX Note 0 above, stdin here
*/ */
if (clixon_xml_parse_file(fd, YB_NONE, NULL, NULL, &x0, NULL) < 0){ if (clixon_xml_parse_file(fp, YB_NONE, NULL, NULL, &x0, NULL) < 0){
fprintf(stderr, "Error: parsing: %s\n", clicon_err_reason); fprintf(stderr, "Error: parsing: %s\n", clicon_err_reason);
return -1; return -1;
} }
@ -375,7 +375,7 @@ main(int argc,
free(buf); free(buf);
if (x0) if (x0)
xml_free(x0); xml_free(x0);
if (fd > 0) if (fp)
close(fd); fclose(fp);
return retval; return retval;
} }

View file

@ -106,7 +106,7 @@ main(int argc, char **argv)
clicon_debug_init(dbg, NULL); clicon_debug_init(dbg, NULL);
if ((yspec = yspec_new()) == NULL) if ((yspec = yspec_new()) == NULL)
goto done; goto done;
if (yang_parse_file(0, "yang test", yspec) == NULL){ if (yang_parse_file(stdin, "yang test", yspec) == NULL){
fprintf(stderr, "yang parse error %s\n", clicon_err_reason); fprintf(stderr, "yang parse error %s\n", clicon_err_reason);
return -1; return -1;
} }