moved detect_endtag to clixon_proto
This commit is contained in:
parent
7880b5d498
commit
05edace630
5 changed files with 67 additions and 43 deletions
|
|
@ -147,30 +147,6 @@ add_error_postamble(cbuf *xf)
|
||||||
return 0;
|
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
|
/*! Get "target" attribute, return actual database given candidate or running
|
||||||
* Caller must do error handling
|
* Caller must do error handling
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,6 @@ extern int cc_closed;
|
||||||
int add_preamble(cbuf *xf);
|
int add_preamble(cbuf *xf);
|
||||||
int add_postamble(cbuf *xf);
|
int add_postamble(cbuf *xf);
|
||||||
int add_error_preamble(cbuf *xf, char *reason);
|
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);
|
char *netconf_get_target(cxobj *xn, char *path);
|
||||||
int add_error_postamble(cbuf *xf);
|
int add_error_postamble(cbuf *xf);
|
||||||
int netconf_output(int s, cbuf *xf, char *msg);
|
int netconf_output(int s, cbuf *xf, char *msg);
|
||||||
|
|
|
||||||
|
|
@ -175,20 +175,18 @@ static int
|
||||||
netconf_input_cb(int s,
|
netconf_input_cb(int s,
|
||||||
void *arg)
|
void *arg)
|
||||||
{
|
{
|
||||||
|
int retval = -1;
|
||||||
clicon_handle h = arg;
|
clicon_handle h = arg;
|
||||||
unsigned char buf[BUFSIZ];
|
unsigned char buf[BUFSIZ];
|
||||||
int i;
|
int i;
|
||||||
int len;
|
int len;
|
||||||
// static cbuf *cb; /* XXX: should use ce state? */
|
cbuf *cb=NULL;
|
||||||
cbuf *cb=NULL; /* XXX: should use ce state? */
|
|
||||||
int xml_state = 0;
|
int xml_state = 0;
|
||||||
int retval = -1;
|
|
||||||
|
|
||||||
if (cb == NULL)
|
if ((cb = cbuf_new()) == NULL){
|
||||||
if ((cb = cbuf_new()) == NULL){
|
clicon_err(OE_XML, errno, "%s: cbuf_new", __FUNCTION__);
|
||||||
clicon_err(OE_XML, errno, "%s: cbuf_new", __FUNCTION__);
|
return retval;
|
||||||
return retval;
|
}
|
||||||
}
|
|
||||||
memset(buf, 0, sizeof(buf));
|
memset(buf, 0, sizeof(buf));
|
||||||
if ((len = read(s, buf, sizeof(buf))) < 0){
|
if ((len = read(s, buf, sizeof(buf))) < 0){
|
||||||
if (errno == ECONNRESET)
|
if (errno == ECONNRESET)
|
||||||
|
|
|
||||||
|
|
@ -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 send_msg_reply(int s, char *data, uint16_t datalen);
|
||||||
|
|
||||||
|
int detect_endtag(char *tag, char ch, int *state);
|
||||||
|
|
||||||
#endif /* _CLIXON_PROTO_H_ */
|
#endif /* _CLIXON_PROTO_H_ */
|
||||||
|
|
|
||||||
|
|
@ -90,7 +90,9 @@ static struct formatvec _FORMATS[] = {
|
||||||
{NULL, -1}
|
{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 *
|
char *
|
||||||
format_int2str(enum format_enum showas)
|
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";
|
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
|
enum format_enum
|
||||||
format_str2int(char *str)
|
format_str2int(char *str)
|
||||||
{
|
{
|
||||||
|
|
@ -115,8 +121,8 @@ format_str2int(char *str)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*! Encode a clicon netconf message
|
/*! Encode a clicon netconf message
|
||||||
* @param[in] param Variable agrument list format an XML netconf string
|
* @param[in] format Variable agrument list format an XML netconf string
|
||||||
* @retval msg Clicon message to send to eg clicon_msg_send()
|
* @retval msg Clicon message to send to eg clicon_msg_send()
|
||||||
*/
|
*/
|
||||||
struct clicon_msg *
|
struct clicon_msg *
|
||||||
clicon_msg_encode(char *format, ...)
|
clicon_msg_encode(char *format, ...)
|
||||||
|
|
@ -149,10 +155,12 @@ clicon_msg_encode(char *format, ...)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*! Decode a clicon netconf message
|
/*! Decode a clicon netconf message
|
||||||
|
* @param[in] msg CLICON msg
|
||||||
|
* @param[out] xml XML parse tree
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
clicon_msg_decode(struct clicon_msg *msg,
|
clicon_msg_decode(struct clicon_msg *msg,
|
||||||
cxobj **xml)
|
cxobj **xml)
|
||||||
{
|
{
|
||||||
int retval = -1;
|
int retval = -1;
|
||||||
char *xmlstr;
|
char *xmlstr;
|
||||||
|
|
@ -168,6 +176,9 @@ clicon_msg_decode(struct clicon_msg *msg,
|
||||||
}
|
}
|
||||||
|
|
||||||
/*! Open local connection using unix domain sockets
|
/*! Open local connection using unix domain sockets
|
||||||
|
* @param[in] sockpath Unix domain file path
|
||||||
|
* @retval s socket
|
||||||
|
* @retval -1 error
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
clicon_connect_unix(char *sockpath)
|
clicon_connect_unix(char *sockpath)
|
||||||
|
|
@ -206,13 +217,19 @@ atomicio_sig_handler(int arg)
|
||||||
_atomicio_sig++;
|
_atomicio_sig++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*! Ensure all of data on socket comes through. fn is either read or write
|
/*! 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
|
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;
|
ssize_t res, pos = 0;
|
||||||
|
|
||||||
while (n > pos) {
|
while (n > pos) {
|
||||||
|
|
@ -236,6 +253,9 @@ atomicio(ssize_t (*fn) (int, void *, size_t), int fd, void *_s, size_t n)
|
||||||
return (pos);
|
return (pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*! Print message on debug. Log if syslog, stderr if not
|
||||||
|
* @param[in] msg CLICON msg
|
||||||
|
*/
|
||||||
static int
|
static int
|
||||||
msg_dump(struct clicon_msg *msg)
|
msg_dump(struct clicon_msg *msg)
|
||||||
{
|
{
|
||||||
|
|
@ -261,6 +281,10 @@ msg_dump(struct clicon_msg *msg)
|
||||||
return 0;
|
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
|
int
|
||||||
clicon_msg_send(int s,
|
clicon_msg_send(int s,
|
||||||
struct clicon_msg *msg)
|
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?
|
* XXX: timeout? and signals?
|
||||||
* There is rudimentary code for turning on signals and handling them
|
* There is rudimentary code for turning on signals and handling them
|
||||||
|
|
@ -292,7 +316,7 @@ clicon_msg_send(int s,
|
||||||
* behaviour.
|
* behaviour.
|
||||||
* Now, ^C will interrupt the whole process, and this may not be what you want.
|
* 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] msg CLICON msg data reply structure. Free with free()
|
||||||
* @param[out] eof Set if eof encountered
|
* @param[out] eof Set if eof encountered
|
||||||
* Note: caller must ensure that s is closed if eof is set after call.
|
* 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;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*! Connect to server, send a clicon_msg message and wait for result using unix socket
|
/*! 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.
|
* @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);
|
free(msg);
|
||||||
return retval;
|
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;
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue