From 05edace630f295bc610a8ec9570c16d19114897c Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Fri, 7 Apr 2017 12:34:07 +0200 Subject: [PATCH] moved detect_endtag to clixon_proto --- apps/netconf/netconf_lib.c | 24 ------------- apps/netconf/netconf_lib.h | 1 - apps/netconf/netconf_main.c | 14 ++++---- lib/clixon/clixon_proto.h | 2 ++ lib/src/clixon_proto.c | 69 +++++++++++++++++++++++++++++++------ 5 files changed, 67 insertions(+), 43 deletions(-) diff --git a/apps/netconf/netconf_lib.c b/apps/netconf/netconf_lib.c index 524be705..e8854708 100644 --- a/apps/netconf/netconf_lib.c +++ b/apps/netconf/netconf_lib.c @@ -147,30 +147,6 @@ add_error_postamble(cbuf *xf) return 0; } -/*! Look for a text pattern in an input string, one char at a time - * @param[in] tag What to look for - * @param[in] ch New input character - * @param[in,out] state A state integer holding how far we have parsed. - * @retval 0 No, we havent detected end tag - * @retval 1 Yes, we have detected end tag! - * XXX: move to clicon_xml? - */ -int -detect_endtag(char *tag, char ch, int *state) -{ - int retval = 0; - - if (tag[*state] == ch){ - (*state)++; - if (*state == strlen(tag)){ - *state = 0; - retval = 1; - } - } - else - *state = 0; - return retval; -} /*! Get "target" attribute, return actual database given candidate or running * Caller must do error handling diff --git a/apps/netconf/netconf_lib.h b/apps/netconf/netconf_lib.h index 70077c1f..d47f7d7b 100644 --- a/apps/netconf/netconf_lib.h +++ b/apps/netconf/netconf_lib.h @@ -72,7 +72,6 @@ extern int cc_closed; int add_preamble(cbuf *xf); int add_postamble(cbuf *xf); int add_error_preamble(cbuf *xf, char *reason); -int detect_endtag(char *tag, char ch, int *state); char *netconf_get_target(cxobj *xn, char *path); int add_error_postamble(cbuf *xf); int netconf_output(int s, cbuf *xf, char *msg); diff --git a/apps/netconf/netconf_main.c b/apps/netconf/netconf_main.c index de90a128..5826393a 100644 --- a/apps/netconf/netconf_main.c +++ b/apps/netconf/netconf_main.c @@ -175,20 +175,18 @@ static int netconf_input_cb(int s, void *arg) { + int retval = -1; clicon_handle h = arg; unsigned char buf[BUFSIZ]; int i; int len; - // static cbuf *cb; /* XXX: should use ce state? */ - cbuf *cb=NULL; /* XXX: should use ce state? */ + cbuf *cb=NULL; int xml_state = 0; - int retval = -1; - if (cb == NULL) - if ((cb = cbuf_new()) == NULL){ - clicon_err(OE_XML, errno, "%s: cbuf_new", __FUNCTION__); - return retval; - } + if ((cb = cbuf_new()) == NULL){ + clicon_err(OE_XML, errno, "%s: cbuf_new", __FUNCTION__); + return retval; + } memset(buf, 0, sizeof(buf)); if ((len = read(s, buf, sizeof(buf))) < 0){ if (errno == ECONNRESET) diff --git a/lib/clixon/clixon_proto.h b/lib/clixon/clixon_proto.h index 85342170..711f835a 100644 --- a/lib/clixon/clixon_proto.h +++ b/lib/clixon/clixon_proto.h @@ -87,4 +87,6 @@ int send_msg_notify(int s, int level, char *event); int send_msg_reply(int s, char *data, uint16_t datalen); +int detect_endtag(char *tag, char ch, int *state); + #endif /* _CLIXON_PROTO_H_ */ diff --git a/lib/src/clixon_proto.c b/lib/src/clixon_proto.c index 21c2b0ed..0e14eee2 100644 --- a/lib/src/clixon_proto.c +++ b/lib/src/clixon_proto.c @@ -90,7 +90,9 @@ static struct formatvec _FORMATS[] = { {NULL, -1} }; -/*! Translate from numeric error to string representation +/*! Translate from numeric format to string representation + * @param[in] showas Format value (see enum format_enum) + * @retval str String value */ char * format_int2str(enum format_enum showas) @@ -103,6 +105,10 @@ format_int2str(enum format_enum showas) return fv?(fv->fv_str?fv->fv_str:"unknown"):"unknown"; } +/*! Translate from string to numeric format representation + * @param[in] str String value + * @retval enum Format value (see enum format_enum) + */ enum format_enum format_str2int(char *str) { @@ -115,8 +121,8 @@ format_str2int(char *str) } /*! Encode a clicon netconf message - * @param[in] param Variable agrument list format an XML netconf string - * @retval msg Clicon message to send to eg clicon_msg_send() + * @param[in] format Variable agrument list format an XML netconf string + * @retval msg Clicon message to send to eg clicon_msg_send() */ struct clicon_msg * clicon_msg_encode(char *format, ...) @@ -149,10 +155,12 @@ clicon_msg_encode(char *format, ...) } /*! Decode a clicon netconf message + * @param[in] msg CLICON msg + * @param[out] xml XML parse tree */ int clicon_msg_decode(struct clicon_msg *msg, - cxobj **xml) + cxobj **xml) { int retval = -1; char *xmlstr; @@ -168,6 +176,9 @@ clicon_msg_decode(struct clicon_msg *msg, } /*! Open local connection using unix domain sockets + * @param[in] sockpath Unix domain file path + * @retval s socket + * @retval -1 error */ int clicon_connect_unix(char *sockpath) @@ -206,13 +217,19 @@ atomicio_sig_handler(int arg) _atomicio_sig++; } - /*! Ensure all of data on socket comes through. fn is either read or write + * @param[in] fn I/O function, ie read/write + * @param[in] fd File descriptor, eg socket + * @param[in] s0 Buffer to read to or write from + * @param[in] n Number of bytes to read/write, loop until done */ static ssize_t -atomicio(ssize_t (*fn) (int, void *, size_t), int fd, void *_s, size_t n) +atomicio(ssize_t (*fn) (int, void *, size_t), + int fd, + void *s0, + size_t n) { - char *s = _s; + char *s = s0; ssize_t res, pos = 0; while (n > pos) { @@ -236,6 +253,9 @@ atomicio(ssize_t (*fn) (int, void *, size_t), int fd, void *_s, size_t n) return (pos); } +/*! Print message on debug. Log if syslog, stderr if not + * @param[in] msg CLICON msg + */ static int msg_dump(struct clicon_msg *msg) { @@ -261,6 +281,10 @@ msg_dump(struct clicon_msg *msg) return 0; } +/*! Send a CLICON netconf message + * @param[in] s socket (unix or inet) to communicate with backend + * @param[out] msg CLICON msg data reply structure. Free with free() + */ int clicon_msg_send(int s, struct clicon_msg *msg) @@ -282,7 +306,7 @@ clicon_msg_send(int s, } -/*! Receive a CLICON message on a UNIX domain socket +/*! Receive a CLICON message * * XXX: timeout? and signals? * There is rudimentary code for turning on signals and handling them @@ -292,7 +316,7 @@ clicon_msg_send(int s, * behaviour. * Now, ^C will interrupt the whole process, and this may not be what you want. * - * @param[in] s UNIX domain socket to communicate with backend + * @param[in] s socket (unix or inet) to communicate with backend * @param[out] msg CLICON msg data reply structure. Free with free() * @param[out] eof Set if eof encountered * Note: caller must ensure that s is closed if eof is set after call. @@ -351,7 +375,6 @@ clicon_msg_rcv(int s, return retval; } - /*! Connect to server, send a clicon_msg message and wait for result using unix socket * * @param[in] msg CLICON msg data structure. It has fixed header and variable body. @@ -553,3 +576,29 @@ send_msg_notify(int s, free(msg); return retval; } + +/*! Look for a text pattern in an input string, one char at a time + * @param[in] tag What to look for + * @param[in] ch New input character + * @param[in,out] state A state integer holding how far we have parsed. + * @retval 0 No, we havent detected end tag + * @retval 1 Yes, we have detected end tag! + */ +int +detect_endtag(char *tag, + char ch, + int *state) +{ + int retval = 0; + + if (tag[*state] == ch){ + (*state)++; + if (*state == strlen(tag)){ + *state = 0; + retval = 1; + } + } + else + *state = 0; + return retval; +}