[augeas-devel] augeas: master - Put struct error and report_error into its own file

David Lutterkort lutter at fedoraproject.org
Thu Oct 1 00:23:41 UTC 2009


Gitweb:        http://git.fedorahosted.org/git/augeas.git?p=augeas.git;a=commitdiff;h=ddc14c2d2d7694411483abc44ab986d55ee15eab
Commit:        ddc14c2d2d7694411483abc44ab986d55ee15eab
Parent:        9db81ed0a5e5b52c01f90d8531eda48b1373fbb2
Author:        David Lutterkort <lutter at redhat.com>
AuthorDate:    Wed Sep 30 14:47:35 2009 -0700
Committer:     David Lutterkort <lutter at redhat.com>
CommitterDate: Wed Sep 30 17:20:33 2009 -0700

Put struct error and report_error into its own file

Also, make the struct error in struct augeas a pointer
---
 src/Makefile.am |    2 +-
 src/augeas.c    |   22 ++++++++++-------
 src/errcode.c   |   57 +++++++++++++++++++++++++++++++++++++++++++
 src/errcode.h   |   73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/internal.c  |   22 ----------------
 src/internal.h  |   37 +--------------------------
 src/parser.y    |    1 +
 src/pathx.c     |    1 +
 src/syntax.c    |    1 +
 9 files changed, 149 insertions(+), 67 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index 09d88ae..ab5e661 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -23,7 +23,7 @@ libaugeas_la_SOURCES = augeas.h augeas.c pathx.c \
 	memory.h memory.c ref.h ref.c \
     syntax.c syntax.h parser.y builtin.c lens.c lens.h regexp.c regexp.h \
 	transform.h transform.c ast.c get.c put.c list.h \
-    info.c info.h
+    info.c info.h errcode.c errcode.h
 
 if USE_VERSION_SCRIPT
   AUGEAS_VERSION_SCRIPT = $(VERSION_SCRIPT_FLAGS)$(srcdir)/augeas_sym.version
diff --git a/src/augeas.c b/src/augeas.c
index 793ff31..3ad2eac 100644
--- a/src/augeas.c
+++ b/src/augeas.c
@@ -26,6 +26,7 @@
 #include "memory.h"
 #include "syntax.h"
 #include "transform.h"
+#include "errcode.h"
 
 #include <fnmatch.h>
 #include <argz.h>
@@ -133,19 +134,19 @@ static int tree_set_value(struct tree *tree, const char *value) {
 
 /* Report pathx errors in /augeas/pathx/error */
 static void store_pathx_error(const struct augeas *aug) {
-    if (aug->error.code != AUG_EPATHX)
+    if (aug->error->code != AUG_EPATHX)
         return;
 
     struct tree *error =
         tree_path_cr(aug->origin, 3, s_augeas, s_pathx, s_error);
     if (error == NULL)
         return;
-    tree_set_value(error, aug->error.minor_details);
+    tree_set_value(error, aug->error->minor_details);
 
     struct tree *tpos = tree_child_cr(error, s_pos);
     if (tpos == NULL)
         return;
-    tree_set_value(tpos, aug->error.details);
+    tree_set_value(tpos, aug->error->details);
 }
 
 static struct pathx *parse_user_pathx(const struct augeas *aug,
@@ -239,6 +240,8 @@ struct augeas *aug_init(const char *root, const char *loadpath,
 
     if (ALLOC(result) < 0)
         goto error;
+    if (ALLOC(result->error) < 0)
+        goto error;
     result->origin = make_tree_origin(tree_root);
     if (result->origin == NULL) {
         free_tree(tree_root);
@@ -347,7 +350,7 @@ static void tree_unlink_children(struct augeas *aug, struct tree *tree) {
  * work within a matching pair of api_entry/api_exit calls.
  */
 static void api_entry(const struct augeas *aug) {
-    struct error *err = &((struct augeas *) aug)->error;
+    struct error *err = ((struct augeas *) aug)->error;
 
     ((struct augeas *) aug)->api_entries += 1;
 
@@ -1097,7 +1100,8 @@ void aug_close(struct augeas *aug) {
     free((void *) aug->root);
     free(aug->modpathz);
     free_symtab(aug->symtab);
-    free(aug->error.details);
+    free(aug->error->details);
+    free(aug->error);
     free(aug);
 }
 
@@ -1119,11 +1123,11 @@ int tree_equal(const struct tree *t1, const struct tree *t2) {
  * Error reporting API
  */
 int aug_error(struct augeas *aug) {
-    return aug->error.code;
+    return aug->error->code;
 }
 
 const char *aug_error_message(struct augeas *aug) {
-    aug_errcode_t errcode = aug->error.code;
+    aug_errcode_t errcode = aug->error->code;
 
     if (errcode > ARRAY_CARDINALITY(errcodes))
         errcode = AUG_EINTERNAL;
@@ -1131,11 +1135,11 @@ const char *aug_error_message(struct augeas *aug) {
 }
 
 const char *aug_error_minor_message(struct augeas *aug) {
-    return aug->error.minor_details;
+    return aug->error->minor_details;
 }
 
 const char *aug_error_details(struct augeas *aug) {
-    return aug->error.details;
+    return aug->error->details;
 }
 
 /*
diff --git a/src/errcode.c b/src/errcode.c
new file mode 100644
index 0000000..71bace0
--- /dev/null
+++ b/src/errcode.c
@@ -0,0 +1,57 @@
+/*
+ * errcode.c: internal interface for error reporting
+ *
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Author: David Lutterkort <lutter at redhat.com>
+ */
+
+#include <config.h>
+
+#include "errcode.h"
+
+static void vreport_error(struct error *err, aug_errcode_t errcode,
+                   const char *format, va_list ap) {
+    /* We only remember the first error */
+    if (err->code != AUG_NOERROR)
+        return;
+    assert(err->details == NULL);
+
+    err->code = errcode;
+    if (format != NULL) {
+        if (vasprintf(&err->details, format, ap) < 0)
+            err->details = NULL;
+    }
+}
+
+void report_error(struct error *err, aug_errcode_t errcode,
+                  const char *format, ...) {
+    va_list ap;
+
+    va_start(ap, format);
+    vreport_error(err, errcode, format, ap);
+    va_end(ap);
+}
+
+/*
+ * Local variables:
+ *  indent-tabs-mode: nil
+ *  c-indent-level: 4
+ *  c-basic-offset: 4
+ *  tab-width: 4
+ * End:
+ */
diff --git a/src/errcode.h b/src/errcode.h
new file mode 100644
index 0000000..8913403
--- /dev/null
+++ b/src/errcode.h
@@ -0,0 +1,73 @@
+/*
+ * errcode.h: internal interface for error reporting
+ *
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Author: David Lutterkort <lutter at redhat.com>
+ */
+
+#ifndef ERRCODE_H_
+#define ERRCODE_H_
+
+#include "internal.h"
+/* Include augeas.h for the error codes */
+#include "augeas.h"
+
+/*
+ * Error details in a separate struct that we can pass around
+ */
+struct error {
+    aug_errcode_t  code;
+    int            minor;
+    char          *details;       /* Human readable explanation */
+    const char    *minor_details; /* Human readable version of MINOR */
+};
+
+void report_error(struct error *err, aug_errcode_t errcode,
+                  const char *format, ...)
+    ATTRIBUTE_FORMAT(printf, 3, 4);
+
+#define ERR_BAIL(obj) if ((obj)->error->code != AUG_NOERROR) goto error;
+
+#define ERR_NOMEM(cond, obj)                             \
+    if (cond) {                                          \
+        report_error((obj)->error, AUG_ENOMEM, NULL);    \
+        goto error;                                      \
+    }
+
+#define ERR_REPORT(obj, code, fmt ...)          \
+    report_error((obj)->error, code, ## fmt)
+
+#define ERR_THROW(cond, obj, code, fmt ...)             \
+    do {                                                \
+        if (cond) {                                     \
+            report_error((obj)->error, code, ## fmt);   \
+            goto error;                                 \
+        }                                               \
+    } while(0)
+
+#endif
+
+
+/*
+ * Local variables:
+ *  indent-tabs-mode: nil
+ *  c-indent-level: 4
+ *  c-basic-offset: 4
+ *  tab-width: 4
+ * End:
+ */
diff --git a/src/internal.c b/src/internal.c
index 9d508d3..718caf8 100644
--- a/src/internal.c
+++ b/src/internal.c
@@ -390,28 +390,6 @@ int xasprintf(char **strp, const char *format, ...) {
   return result;
 }
 
-static void vreport_error(struct error *err, aug_errcode_t errcode,
-                   const char *format, va_list ap) {
-    /* We only remember the first error */
-    if (err->code != AUG_NOERROR)
-        return;
-    assert(err->details == NULL);
-
-    err->code = errcode;
-    if (format != NULL) {
-        if (vasprintf(&err->details, format, ap) < 0)
-            err->details = NULL;
-    }
-}
-
-void report_error(struct error *err, aug_errcode_t errcode,
-                  const char *format, ...) {
-    va_list ap;
-
-    va_start(ap, format);
-    vreport_error(err, errcode, format, ap);
-    va_end(ap);
-}
 /*
  * Local variables:
  *  indent-tabs-mode: nil
diff --git a/src/internal.h b/src/internal.h
index 66c1bf7..e564f89 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -267,16 +267,6 @@ const char *xstrerror(int errnum, char *buf, size_t len);
 /* Like asprintf, but set *STRP to NULL on error */
 int xasprintf(char **strp, const char *format, ...);
 
-/*
- * Error details in a separate struct that we can pass around
- */
-struct error {
-    aug_errcode_t  code;
-    int            minor;
-    char          *details;       /* Human readable explanation */
-    const char    *minor_details; /* Human readable version of MINOR */
-};
-
 /* Struct: augeas
  * The data structure representing a connection to Augeas. */
 struct augeas {
@@ -289,38 +279,15 @@ struct augeas {
     char             *modpathz;   /* The search path for modules as a
                                      glibc argz vector */
     struct pathx_symtab *symtab;
-    struct error        error;
+    struct error        *error;
     uint                api_entries;  /* Number of entries through a public
                                        * API, 0 when called from outside */
 };
 
-void report_error(struct error *err, aug_errcode_t errcode,
-                  const char *format, ...)
-    ATTRIBUTE_FORMAT(printf, 3, 4);
-
 static inline struct error *err_of_aug(const struct augeas *aug) {
-    return &((struct augeas *) aug)->error;
+    return ((struct augeas *) aug)->error;
 }
 
-#define ERR_BAIL(aug) if ((aug)->error.code != AUG_NOERROR) goto error;
-
-#define ERR_NOMEM(cond, aug)                             \
-    if (cond) {                                          \
-        report_error(&((aug)->error), AUG_ENOMEM, NULL); \
-        goto error;                                      \
-    }
-
-#define ERR_REPORT(aug, code, fmt ...)          \
-    report_error(&((aug)->error), code, ## fmt)
-
-#define ERR_THROW(cond, aug, code, fmt ...)             \
-    do {                                                \
-        if (cond) {                                     \
-            report_error(&(aug)->error, code, ## fmt);  \
-            goto error;                                 \
-        }                                               \
-    } while(0)
-
 /* Struct: tree
  * An entry in the global config tree. The data structure allows associating
  * values with interior nodes, but the API currently marks that as an error.
diff --git a/src/parser.y b/src/parser.y
index 3d13f43..df5e3df 100644
--- a/src/parser.y
+++ b/src/parser.y
@@ -5,6 +5,7 @@
 #include "internal.h"
 #include "syntax.h"
 #include "list.h"
+#include "errcode.h"
 #include <stdio.h>
 
 /* Work around a problem on FreeBSD where Bison looks for _STDLIB_H
diff --git a/src/pathx.c b/src/pathx.c
index 15d1c30..75f1aa6 100644
--- a/src/pathx.c
+++ b/src/pathx.c
@@ -29,6 +29,7 @@
 
 #include "ref.h"
 #include "regexp.h"
+#include "errcode.h"
 
 static const char *const errcodes[] = {
     "no error",
diff --git a/src/syntax.c b/src/syntax.c
index 9d3dccb..f255f86 100644
--- a/src/syntax.c
+++ b/src/syntax.c
@@ -36,6 +36,7 @@
 #include "syntax.h"
 #include "augeas.h"
 #include "transform.h"
+#include "errcode.h"
 
 /* Extension of source files */
 #define AUG_EXT ".aug"




More information about the augeas-devel mailing list