printers CHANGE provide error information from ly_out constructors

Provide specific LY_ERR value to inform what failed when creating
ly_out structure.
diff --git a/src/printer.c b/src/printer.c
index 9685fc5..e3dfd85 100644
--- a/src/printer.c
+++ b/src/printer.c
@@ -159,19 +159,19 @@
     return out->type;
 }
 
-API struct ly_out *
-ly_out_new_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg)
+API LY_ERR
+ly_out_new_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg, struct ly_out **out)
 {
-    struct ly_out *out;
+    LY_CHECK_ARG_RET(NULL, out, writeclb, LY_EINVAL);
 
-    out = calloc(1, sizeof *out);
-    LY_CHECK_ERR_RET(!out, LOGMEM(NULL), NULL);
+    *out = calloc(1, sizeof **out);
+    LY_CHECK_ERR_RET(!*out, LOGMEM(NULL), LY_EMEM);
 
-    out->type = LY_OUT_CALLBACK;
-    out->method.clb.func = writeclb;
-    out->method.clb.arg = arg;
+    (*out)->type = LY_OUT_CALLBACK;
+    (*out)->method.clb.func = writeclb;
+    (*out)->method.clb.arg = arg;
 
-    return out;
+    return LY_SUCCESS;
 }
 
 API ssize_t (*ly_out_clb(struct ly_out *out, ssize_t (*writeclb)(void *arg, const void *buf, size_t count)))(void *arg, const void *buf, size_t count)
@@ -205,41 +205,43 @@
     return prev_arg;
 }
 
-API struct ly_out *
-ly_out_new_fd(int fd)
+API LY_ERR
+ly_out_new_fd(int fd, struct ly_out **out)
 {
-    struct ly_out *out;
+    LY_CHECK_ARG_RET(NULL, out, fd != -1, LY_EINVAL);
 
-    out = calloc(1, sizeof *out);
-    LY_CHECK_ERR_RET(!out, LOGMEM(NULL), NULL);
+    *out = calloc(1, sizeof **out);
+    LY_CHECK_ERR_RET(!*out, LOGMEM(NULL), LY_EMEM);
 
 #ifdef HAVE_VDPRINTF
-    out->type = LY_OUT_FD;
-    out->method.fd = fd;
+    (*out)->type = LY_OUT_FD;
+    (*out)->method.fd = fd;
 #else
     /* Without vdfprintf(), change the printing method to printing to a FILE stream.
      * To preserve the original file descriptor, duplicate it and use it to open file stream. */
-    out->type = LY_OUT_FDSTREAM;
-    out->method.fdstream.fd = fd;
+    (*out)->type = LY_OUT_FDSTREAM;
+    (*out)->method.fdstream.fd = fd;
 
-    fd = dup(out->method.fdstream.fd);
+    fd = dup((*out)->method.fdstream.fd);
     if (fd < 0) {
         LOGERR(NULL, LY_ESYS, "Unable to duplicate provided file descriptor (%d) for printing the output (%s).",
-               out->method.fdstream.fd, strerror(errno));
-        free(out);
-        return NULL;
+               (*out)->method.fdstream.fd, strerror(errno));
+        free(*out);
+        *out = NULL;
+        return LY_ESYS;
     }
-    out->method.fdstream.f = fdopen(fd, "a");
-    if (!out->method.fdstream.f) {
+    (*out)->method.fdstream.f = fdopen(fd, "a");
+    if (!(*out)->method.fdstream.f) {
         LOGERR(NULL, LY_ESYS, "Unable to open provided file descriptor (%d) for printing the output (%s).",
-               out->method.fdstream.fd, strerror(errno));
-        free(out);
+               (*out)->method.fdstream.fd, strerror(errno));
+        free(*out);
+        *out = NULL;
         fclose(fd);
-        return NULL;
+        return LY_ESYS;
     }
 #endif
 
-    return out;
+    return LY_SUCCESS;
 }
 
 API int
@@ -284,18 +286,18 @@
     return prev_fd;
 }
 
-API struct ly_out *
-ly_out_new_file(FILE *f)
+API LY_ERR
+ly_out_new_file(FILE *f, struct ly_out **out)
 {
-    struct ly_out *out;
+    LY_CHECK_ARG_RET(NULL, out, f, LY_EINVAL);
 
-    out = calloc(1, sizeof *out);
-    LY_CHECK_ERR_RET(!out, LOGMEM(NULL), NULL);
+    *out = calloc(1, sizeof **out);
+    LY_CHECK_ERR_RET(!*out, LOGMEM(NULL), LY_EMEM);
 
-    out->type = LY_OUT_FILE;
-    out->method.f = f;
+    (*out)->type = LY_OUT_FILE;
+    (*out)->method.f = f;
 
-    return out;
+    return LY_SUCCESS;
 }
 
 API FILE *
@@ -314,25 +316,25 @@
     return prev_f;
 }
 
-API struct ly_out *
-ly_out_new_memory(char **strp, size_t size)
+API LY_ERR
+ly_out_new_memory(char **strp, size_t size, struct ly_out **out)
 {
-    struct ly_out *out;
+    LY_CHECK_ARG_RET(NULL, out, strp, LY_EINVAL);
 
-    out = calloc(1, sizeof *out);
-    LY_CHECK_ERR_RET(!out, LOGMEM(NULL), NULL);
+    *out = calloc(1, sizeof **out);
+    LY_CHECK_ERR_RET(!*out, LOGMEM(NULL), LY_EMEM);
 
-    out->type = LY_OUT_MEMORY;
-    out->method.mem.buf = strp;
+    (*out)->type = LY_OUT_MEMORY;
+    (*out)->method.mem.buf = strp;
     if (!size) {
         /* buffer is supposed to be allocated */
         *strp = NULL;
     } else if (*strp) {
         /* there is already buffer to use */
-        out->method.mem.size = size;
+        (*out)->method.mem.size = size;
     }
 
-    return out;
+    return LY_SUCCESS;
 }
 
 char *
@@ -406,22 +408,22 @@
     return LY_SUCCESS;
 }
 
-API struct ly_out *
-ly_out_new_filepath(const char *filepath)
+API LY_ERR
+ly_out_new_filepath(const char *filepath, struct ly_out **out)
 {
-    struct ly_out *out;
+    LY_CHECK_ARG_RET(NULL, out, filepath, LY_EINVAL);
 
-    out = calloc(1, sizeof *out);
-    LY_CHECK_ERR_RET(!out, LOGMEM(NULL), NULL);
+    *out = calloc(1, sizeof **out);
+    LY_CHECK_ERR_RET(!*out, LOGMEM(NULL), LY_EMEM);
 
-    out->type = LY_OUT_FILEPATH;
-    out->method.fpath.f = fopen(filepath, "w");
-    if (!out->method.fpath.f) {
+    (*out)->type = LY_OUT_FILEPATH;
+    (*out)->method.fpath.f = fopen(filepath, "w");
+    if (!(*out)->method.fpath.f) {
         LOGERR(NULL, LY_ESYS, "Failed to open file \"%s\" (%s).", filepath, strerror(errno));
-        return NULL;
+        return LY_ESYS;
     }
-    out->method.fpath.filepath = strdup(filepath);
-    return out;
+    (*out)->method.fpath.filepath = strdup(filepath);
+    return LY_SUCCESS;
 }
 
 API const char *
diff --git a/src/printer.h b/src/printer.h
index 4c15f7f..8929678 100644
--- a/src/printer.h
+++ b/src/printer.h
@@ -70,10 +70,11 @@
  *
  * @param[in] writeclb Pointer to the printer callback function writing the data (see write(2)).
  * @param[in] arg Optional caller-specific argument to be passed to the @p writeclb callback.
- * @return NULL in case of memory allocation error.
- * @return Created printer handler supposed to be passed to different ly*_print_*() functions.
+ * @param[out] out Created printer handler supposed to be passed to different ly*_print() functions.
+ * @return LY_SUCCESS in case of success
+ * @return LY_EMEM in case allocating the @p out handler fails.
  */
-struct ly_out *ly_out_new_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg);
+LY_ERR ly_out_new_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg, struct ly_out **out);
 
 /**
  * @brief Get or reset callback function associated with a callback printer handler.
@@ -98,10 +99,11 @@
  * @brief Create printer handler using file descriptor.
  *
  * @param[in] fd File descriptor to use.
- * @return NULL in case of error.
- * @return Created printer handler supposed to be passed to different ly*_print_*() functions.
+ * @param[out] out Created printer handler supposed to be passed to different ly*_print() functions.
+ * @return LY_SUCCESS in case of success
+ * @return LY_ERR value in case of failure.
  */
-struct ly_out *ly_out_new_fd(int fd);
+LY_ERR ly_out_new_fd(int fd, struct ly_out **out);
 
 /**
  * @brief Get or reset file descriptor printer handler.
@@ -117,10 +119,11 @@
  * @brief Create printer handler using file stream.
  *
  * @param[in] f File stream to use.
- * @return NULL in case of error.
- * @return Created printer handler supposed to be passed to different ly*_print_*() functions.
+ * @param[out] out Created printer handler supposed to be passed to different ly*_print() functions.
+ * @return LY_SUCCESS in case of success
+ * @return LY_ERR value in case of failure.
  */
-struct ly_out *ly_out_new_file(FILE *f);
+LY_ERR ly_out_new_file(FILE *f, struct ly_out **out);
 
 /**
  * @brief Get or reset file stream printer handler.
@@ -138,10 +141,11 @@
  * @p size of the buffer is set, the buffer is used (and extended if needed) to store the printed data.
  * @param[in] size Size of the buffer provided via @p strp. In case it is 0, the buffer for the printed data
  * is newly allocated even if @p strp points to a pointer to an existing buffer.
- * @return NULL in case of error.
- * @return Created printer handler supposed to be passed to different ly*_print_*() functions.
+ * @param[out] out Created printer handler supposed to be passed to different ly*_print() functions.
+ * @return LY_SUCCESS in case of success
+ * @return LY_ERR value in case of failure.
  */
-struct ly_out *ly_out_new_memory(char **strp, size_t size);
+LY_ERR ly_out_new_memory(char **strp, size_t size, struct ly_out **out);
 
 /**
  * @brief Get or change memory where the data are dumped.
@@ -162,7 +166,7 @@
  * @return NULL in case of error.
  * @return Created printer handler supposed to be passed to different ly*_print_*() functions.
  */
-struct ly_out *ly_out_new_filepath(const char *filepath);
+LY_ERR ly_out_new_filepath(const char *filepath, struct ly_out **out);
 
 /**
  * @brief Get or change the filepath of the file where the printer prints the data.
diff --git a/src/printer_data.c b/src/printer_data.c
index 1f67b76..1f29523 100644
--- a/src/printer_data.c
+++ b/src/printer_data.c
@@ -23,15 +23,6 @@
 #include "printer_internal.h"
 #include "tree_data.h"
 
-/**
- * @brief Common YANG data printer.
- *
- * @param[in] out Prepared structure defining the type and details of the printer output.
- * @param[in] root The root element of the (sub)tree to print.
- * @param[in] format Output format.
- * @param[in] options [Data printer flags](@ref dataprinterflags). With \p format LYD_LYB, only #LYDP_WITHSIBLINGS option is accepted.
- * @return LY_ERR value.
- */
 API ssize_t
 lyd_print(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, int options)
 {
@@ -97,7 +88,7 @@
     /* init */
     *strp = NULL;
 
-    out = ly_out_new_memory(strp, 0);
+    LY_CHECK_RET(ly_out_new_memory(strp, 0, &out));
     return lyd_print_(out, root, format, options);
 }
 
@@ -108,7 +99,7 @@
 
     LY_CHECK_ARG_RET(NULL, fd != -1, root, LY_EINVAL);
 
-    out = ly_out_new_fd(fd);
+    LY_CHECK_RET(ly_out_new_fd(fd, &out));
     return lyd_print_(out, root, format, options);
 }
 
@@ -119,7 +110,7 @@
 
     LY_CHECK_ARG_RET(NULL, f, root, LY_EINVAL);
 
-    out = ly_out_new_file(f);
+    LY_CHECK_RET(ly_out_new_file(f, &out));
     return lyd_print_(out, root, format, options);
 }
 
@@ -130,7 +121,7 @@
 
     LY_CHECK_ARG_RET(NULL, path, root, LY_EINVAL);
 
-    out = ly_out_new_filepath(path);
+    LY_CHECK_RET(ly_out_new_filepath(path, &out));
     return lyd_print_(out, root, format, options);
 }
 
@@ -142,6 +133,6 @@
 
     LY_CHECK_ARG_RET(NULL, writeclb, root, LY_EINVAL);
 
-    out = ly_out_new_clb(writeclb, arg);
+    LY_CHECK_RET(ly_out_new_clb(writeclb, arg, &out));
     return lyd_print_(out, root, format, options);
 }
diff --git a/src/printer_schema.c b/src/printer_schema.c
index df14f21..acd00a2 100644
--- a/src/printer_schema.c
+++ b/src/printer_schema.c
@@ -95,7 +95,7 @@
     /* init */
     *strp = NULL;
 
-    out = ly_out_new_memory(strp, 0);
+    LY_CHECK_RET(ly_out_new_memory(strp, 0, &out));
     return lys_print_(out, module, format, line_length, options);
 }
 
@@ -106,7 +106,7 @@
 
     LY_CHECK_ARG_RET(NULL, fd != -1, module, LY_EINVAL);
 
-    out = ly_out_new_fd(fd);
+    LY_CHECK_RET(ly_out_new_fd(fd, &out));
     return lys_print_(out, module, format, line_length, options);
 }
 
@@ -117,7 +117,7 @@
 
     LY_CHECK_ARG_RET(NULL, f, module, LY_EINVAL);
 
-    out = ly_out_new_file(f);
+    LY_CHECK_RET(ly_out_new_file(f, &out));
     return lys_print_(out, module, format, line_length, options);
 }
 
@@ -128,7 +128,7 @@
 
     LY_CHECK_ARG_RET(NULL, path, module, LY_EINVAL);
 
-    out = ly_out_new_filepath(path);
+    LY_CHECK_RET(ly_out_new_filepath(path, &out));
     return lys_print_(out, module, format, line_length, options);
 }
 
@@ -140,7 +140,7 @@
 
     LY_CHECK_ARG_RET(NULL, writeclb, module, LY_EINVAL);
 
-    out = ly_out_new_clb(writeclb, arg);
+    LY_CHECK_RET(ly_out_new_clb(writeclb, arg, &out));
     return lys_print_(out, module, format, line_length, options);
 }
 
diff --git a/src/xpath.c b/src/xpath.c
index f73988b..459c78e 100644
--- a/src/xpath.c
+++ b/src/xpath.c
@@ -416,7 +416,7 @@
                 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
                 break;
             case LYD_ANYDATA_DATATREE:
-                out = ly_out_new_memory(&buf, 0);
+                LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
                 rc = lyd_print(out, any->value.tree, LYD_XML, LYDP_WITHSIBLINGS);
                 ly_out_free(out, NULL, 0);
                 LY_CHECK_RET(rc < 0, -rc);