printers CHANGE detect a failure and stop printing as soon as an error occurred
diff --git a/src/printer.c b/src/printer.c
index 0c34995..e681104 100644
--- a/src/printer.c
+++ b/src/printer.c
@@ -69,6 +69,8 @@
FILE *stream;
#endif
+ LYOUT_CHECK(out, out->status);
+
va_start(ap, format);
switch (out->type) {
@@ -86,6 +88,7 @@
LOGERR(NULL, LY_ESYS, "Unable to duplicate provided file descriptor (%d) for printing the output (%s).",
out->method.fd, strerror(errno));
va_end(ap);
+ out->status = LY_ESYS;
return LY_ESYS;
}
stream = fdopen(fd, "a");
@@ -93,6 +96,7 @@
LOGERR(NULL, LY_ESYS, "Unable to open provided file descriptor (%d) for printing the output (%s).",
out->method.fd, strerror(errno));
va_end(ap);
+ out->status = LY_ESYS;
return LY_ESYS;
}
out->method.f = stream;
@@ -138,6 +142,7 @@
if (count < 0) {
LOGERR(out->ctx, LY_ESYS, "%s: writing data failed (%s).", __func__, strerror(errno));
+ out->status = LY_ESYS;
return LY_ESYS;
} else {
out->printed += count;
@@ -166,6 +171,8 @@
{
int written = 0;
+ LYOUT_CHECK(out, out->status);
+
if (out->hole_count) {
/* we are buffering data after a hole */
if (out->buf_len + count > out->buf_size) {
@@ -218,9 +225,11 @@
goto repeat;
}
LOGERR(out->ctx, LY_ESYS, "%s: writing data failed (%s).", __func__, strerror(errno));
+ out->status = LY_ESYS;
return LY_ESYS;
} else if ((size_t)written != count) {
LOGERR(out->ctx, LY_ESYS, "%s: writing data failed (unable to write %u from %u data).", __func__, count - (size_t)written, count);
+ out->status = LY_ESYS;
return LY_ESYS;
} else {
out->printed += written;
@@ -231,6 +240,8 @@
LY_ERR
ly_write_skip(struct lyout *out, size_t count, size_t *position)
{
+ LYOUT_CHECK(out, out->status);
+
switch (out->type) {
case LYOUT_MEMORY:
if (out->method.mem.len + count > out->method.mem.size) {
@@ -238,6 +249,7 @@
if (!out->method.mem.buf) {
out->method.mem.len = 0;
out->method.mem.size = 0;
+ out->status = LY_ESYS;
LOGMEM_RET(NULL);
}
out->method.mem.size = out->method.mem.len + count;
@@ -262,6 +274,7 @@
if (!out->buffered) {
out->buf_len = 0;
out->buf_size = 0;
+ out->status = LY_ESYS;
LOGMEM_RET(NULL);
}
out->buf_size = out->buf_len + count;
@@ -285,6 +298,8 @@
{
LY_ERR ret = LY_SUCCESS;
+ LYOUT_CHECK(out, out->status);
+
switch (out->type) {
case LYOUT_MEMORY:
/* write */
@@ -295,6 +310,7 @@
case LYOUT_STREAM:
case LYOUT_CALLBACK:
if (out->buf_len < position + count) {
+ out->status = LY_ESYS;
LOGMEM_RET(NULL);
}
diff --git a/src/printer_internal.h b/src/printer_internal.h
index 09a081a..2fb582e 100644
--- a/src/printer_internal.h
+++ b/src/printer_internal.h
@@ -54,8 +54,11 @@
/* libyang context for error logging */
struct ly_ctx *ctx;
+ LY_ERR status;
};
+#define LYOUT_CHECK(LYOUT, ...) if (LYOUT->status) {return __VA_ARGS__;}
+
struct ext_substmt_info_s {
const char *name;
const char *arg;
diff --git a/src/printer_yang.c b/src/printer_yang.c
index 9d7921d..015f60a 100755
--- a/src/printer_yang.c
+++ b/src/printer_yang.c
@@ -290,6 +290,7 @@
if (asprintf(&str, "%u", attr_value) == -1) {
LOGMEM(ctx->module->ctx);
+ ctx->out->status = LY_EMEM;
return;
}
ypr_open(ctx->out, flag);
@@ -304,6 +305,7 @@
if (asprintf(&str, "%d", attr_value) == -1) {
LOGMEM(ctx->module->ctx);
+ ctx->out->status = LY_EMEM;
return;
}
ypr_open(ctx->out, flag);
@@ -972,6 +974,7 @@
}
default:
LOGINT(ctx->module->ctx);
+ ctx->out->status = LY_EINT;
}
LEVEL--;
@@ -981,6 +984,8 @@
static void
yprp_typedef(struct ypr_ctx *ctx, const struct lysp_tpdf *tpdf)
{
+ LYOUT_CHECK(ctx->out);
+
ly_print(ctx->out, "\n%*stypedef %s {\n", INDENT, tpdf->name);
LEVEL++;
@@ -1014,6 +1019,8 @@
int flag = 0;
struct lysp_node *data;
+ LYOUT_CHECK(ctx->out);
+
ly_print(ctx->out, "\n%*sgrouping %s", INDENT, grp->name);
LEVEL++;
@@ -1112,6 +1119,8 @@
int flag = 0;
struct lysp_node *data;
+ LYOUT_CHECK(ctx->out);
+
ly_print(ctx->out, "%*snotification %s", INDENT, notif->name);
LEVEL++;
@@ -1151,6 +1160,8 @@
int flag = 0;
struct lysc_node *data;
+ LYOUT_CHECK(ctx->out);
+
ly_print(ctx->out, "%*snotification %s", INDENT, notif->name);
LEVEL++;
@@ -1179,6 +1190,8 @@
unsigned int u;
int flag = 0;
+ LYOUT_CHECK(ctx->out);
+
ly_print(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
LEVEL++;
@@ -1210,6 +1223,8 @@
{
int flag = 0;
+ LYOUT_CHECK(ctx->out);
+
ly_print(ctx->out, "%*s%s %s", INDENT, action->parent ? "action" : "rpc", action->name);
LEVEL++;
@@ -1857,6 +1872,8 @@
static void
yprp_node(struct ypr_ctx *ctx, const struct lysp_node *node)
{
+ LYOUT_CHECK(ctx->out);
+
switch (node->nodetype) {
case LYS_CONTAINER:
yprp_container(ctx, node);
@@ -1891,6 +1908,8 @@
static void
yprc_node(struct ypr_ctx *ctx, const struct lysc_node *node)
{
+ LYOUT_CHECK(ctx->out);
+
switch (node->nodetype) {
case LYS_CONTAINER:
yprc_container(ctx, node);