[augeas-devel] augeas: master - Make struct error available in struct info

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


Gitweb:        http://git.fedorahosted.org/git/augeas.git?p=augeas.git;a=commitdiff;h=c2d4b5e4dd28b36f5f68ef7462fbd854e9a66b42
Commit:        c2d4b5e4dd28b36f5f68ef7462fbd854e9a66b42
Parent:        ddc14c2d2d7694411483abc44ab986d55ee15eab
Author:        David Lutterkort <lutter at redhat.com>
AuthorDate:    Wed Sep 30 15:48:41 2009 -0700
Committer:     David Lutterkort <lutter at redhat.com>
CommitterDate: Wed Sep 30 17:20:38 2009 -0700

Make struct error available in struct info

This makes it much easier to report errors into the central struct error
instance hanging off the Augeas instance.
---
 src/builtin.c   |    4 ++--
 src/get.c       |    1 +
 src/info.h      |    2 ++
 src/lexer.l     |   22 ++++++++++++----------
 src/parser.y    |   31 ++++++++++++++++++-------------
 src/syntax.c    |    9 ++++++---
 src/syntax.h    |    8 +++++---
 src/transform.c |    1 +
 8 files changed, 47 insertions(+), 31 deletions(-)

diff --git a/src/builtin.c b/src/builtin.c
index c9698b0..f42d45f 100644
--- a/src/builtin.c
+++ b/src/builtin.c
@@ -427,12 +427,12 @@ static struct value *sys_read_file(struct info *info, struct value *n) {
     return v;
 }
 
-struct module *builtin_init(void) {
+struct module *builtin_init(struct error *error) {
     struct module *modl = module_create("Builtin");
     int r;
 
 #define DEFINE_NATIVE(modl, name, nargs, impl, types ...)               \
-    r = define_native(modl, name, nargs, impl, ##types);                \
+    r = define_native(error, modl, name, nargs, impl, ##types);         \
     if (r < 0) goto error;
 
     DEFINE_NATIVE(modl, "gensym", 1, gensym, T_STRING, T_STRING);
diff --git a/src/get.c b/src/get.c
index 057b6b3..05e91ba 100644
--- a/src/get.c
+++ b/src/get.c
@@ -805,6 +805,7 @@ struct skel *lns_parse(struct lens *lens, const char *text, struct dict **dict,
 
     MEMZERO(&state, 1);
     state.info.ref = UINT_MAX;
+    state.info.error = lens->info->error;
     state.text = text;
 
     state.text = text;
diff --git a/src/info.h b/src/info.h
index c2ca2d1..52ff4ef 100644
--- a/src/info.h
+++ b/src/info.h
@@ -43,6 +43,8 @@ void free_string(struct string *string);
 
 /* File information */
 struct info {
+    /* There is only one struct error for each Augeas instance */
+    struct error  *error;
     struct string *filename;
     uint16_t first_line;
     uint16_t first_column;
diff --git a/src/lexer.l b/src/lexer.l
index 12c5bb9..500240d 100644
--- a/src/lexer.l
+++ b/src/lexer.l
@@ -30,19 +30,20 @@ typedef struct info YYLTYPE;
   (Loc).first_line = (Loc).last_line;
 
 /* The lack of reference counting for filename is intentional */
-#define YY_USER_ACTION                                  \
-   do {                                                 \
-     yylloc->last_column += yyleng;                     \
-     yylloc->filename = augl_get_extra(yyscanner);      \
+#define YY_USER_ACTION                                            \
+  do {                                                            \
+     yylloc->last_column += yyleng;                               \
+     yylloc->filename = augl_get_extra(yyscanner)->filename;      \
+     yylloc->error = augl_get_extra(yyscanner)->error;            \
    } while(0);
 
 #define YY_USER_INIT LOCATION_STEP(*yylloc)
 
-#define YY_EXTRA_TYPE struct string *
+#define YY_EXTRA_TYPE struct info *
 
 int augl_get_column  (yyscan_t yyscanner);
 static void augl_set_column (int  column_no , yyscan_t yyscanner);
-int augl_init_lexer(struct string *name, yyscan_t * scanner);
+int augl_init_lexer(struct info *info, yyscan_t * scanner);
 
 static int to_int(const char *str) {
   int v;
@@ -131,7 +132,7 @@ ARROW  ->
                    BEGIN(COMMENT);
                 }
   .             {
-    fprintf(stderr, "%s:%d:%d: Unexpected character %c\n", augl_get_extra(yyscanner)->str, yylineno, yylloc->first_column, yytext[0]);
+    fprintf(stderr, "%s:%d:%d: Unexpected character %c\n", augl_get_extra(yyscanner)->filename->str, yylineno, yylloc->first_column, yytext[0]);
                 }
 
   <<EOF>>       {
@@ -153,14 +154,15 @@ ARROW  ->
                 }
   .             /* Skip */;
   <<EOF>>       {
-                  fprintf(stderr, "%s:%d:%d: Missing *)\n", augl_get_extra(yyscanner)->str, yylineno, yylloc->first_column);
+                  fprintf(stderr, "%s:%d:%d: Missing *)\n", augl_get_extra(yyscanner)->filename->str, yylineno, yylloc->first_column);
                   yyterminate();
                 }
 }
 %%
 
-int augl_init_lexer(struct string *name, yyscan_t *scanner) {
+int augl_init_lexer(struct info *info, yyscan_t *scanner) {
   FILE *f;
+  struct string *name = info->filename;
 
   f = fopen(name->str, "r");
   if (f == NULL)
@@ -170,7 +172,7 @@ int augl_init_lexer(struct string *name, yyscan_t *scanner) {
     fprintf(stderr, "Failed to init scanner\n");
     return -1;
   }
-  augl_set_extra(name, *scanner);
+  augl_set_extra(info, *scanner);
   augl_set_in(f, *scanner);
   return 0;
 }
diff --git a/src/parser.y b/src/parser.y
index df5e3df..6fee08d 100644
--- a/src/parser.y
+++ b/src/parser.y
@@ -26,19 +26,20 @@ typedef struct info YYLTYPE;
 #define YYLTYPE_IS_DECLARED 1
 #define YYLTYPE_IS_TRIVIAL 1
 /* The lack of reference counting on filename is intentional */
-# define YYLLOC_DEFAULT(Current, Rhs, N)				\
-  do {									\
-    (Current).filename = augl_get_extra(scanner);                       \
+# define YYLLOC_DEFAULT(Current, Rhs, N)                                \
+  do {                                                                  \
+    (Current).filename = augl_get_extra(scanner)->filename;             \
+    (Current).error = augl_get_extra(scanner)->error;                   \
     if (YYID (N)) {                                                     \
         (Current).first_line   = YYRHSLOC (Rhs, 1).first_line;          \
-        (Current).first_column = YYRHSLOC (Rhs, 1).first_column;	\
-        (Current).last_line    = YYRHSLOC (Rhs, N).last_line;		\
+        (Current).first_column = YYRHSLOC (Rhs, 1).first_column;        \
+        (Current).last_line    = YYRHSLOC (Rhs, N).last_line;           \
         (Current).last_column  = YYRHSLOC (Rhs, N).last_column;         \
     } else {                                                            \
-        (Current).first_line   = (Current).last_line   =		\
-	    YYRHSLOC (Rhs, 0).last_line;				\
-	  (Current).first_column = (Current).last_column =		\
-	    YYRHSLOC (Rhs, 0).last_column;				\
+      (Current).first_line   = (Current).last_line   =                  \
+	    YYRHSLOC (Rhs, 0).last_line;                                    \
+	  (Current).first_column = (Current).last_column =                  \
+	    YYRHSLOC (Rhs, 0).last_column;                                  \
     }                                                                   \
   } while (0)
 
@@ -90,11 +91,11 @@ typedef struct info YYLTYPE;
 %{
 /* Lexer */
 extern int augl_lex (YYSTYPE * yylval_param,struct info * yylloc_param ,yyscan_t yyscanner);
- int augl_init_lexer(struct string *name, yyscan_t * scanner);
+ int augl_init_lexer(struct info *info, yyscan_t * scanner);
 int augl_lex_destroy (yyscan_t yyscanner );
 int augl_get_lineno (yyscan_t yyscanner );
 int augl_get_column  (yyscan_t yyscanner);
-struct string *augl_get_extra (yyscan_t yyscanner );
+struct info *augl_get_extra (yyscan_t yyscanner );
 char *augl_get_text (yyscan_t yyscanner );
 
 static void augl_error(struct info *locp, struct term **term,
@@ -140,6 +141,7 @@ static void augl_error(struct info *locp, struct term **term,
    (a).first_column = (b).first_column;                                 \
    (a).last_line    = (c).last_line;                                    \
    (a).last_column  = (c).last_column;                                  \
+   (a).error        = (b).error;                                        \
  } while(0);
 
 %}
@@ -324,7 +326,8 @@ int augl_parse_file(struct augeas *aug, const char *name,
   MEMZERO(&info, 1);
   info.ref = UINT_MAX;
   info.filename = sname;
-  if (augl_init_lexer(sname, &scanner) != 0) {
+  info.error = aug->error;
+  if (augl_init_lexer(&info, &scanner) != 0) {
     fprintf(stderr, "file name: %s [%s]\n", sname->str, name);
     augl_error(&info, term, NULL, "file not found");
     goto error;
@@ -357,6 +360,7 @@ static struct info *clone_info(struct info *locp) {
   info->first_column = locp->first_column;
   info->last_line    = locp->last_line;
   info->last_column  = locp->last_column;
+  info->error        = locp->error;
   return info;
 }
 
@@ -509,6 +513,7 @@ void augl_error(struct info *locp,
   MEMZERO(&info, 1);
   info.ref = string.ref = UINT_MAX;
   info.filename = &string;
+  info.error = locp->error;
 
   if (locp != NULL) {
     info.first_line   = locp->first_line;
@@ -521,7 +526,7 @@ void augl_error(struct info *locp,
     info.first_column = augl_get_column(scanner);
     info.last_line    = augl_get_lineno(scanner);
     info.last_column  = augl_get_column(scanner);
-    info.filename     = augl_get_extra(scanner);
+    info.filename     = augl_get_extra(scanner)->filename;
   } else if (*term != NULL && (*term)->info != NULL) {
     memcpy(&info, (*term)->info, sizeof(info));
   } else {
diff --git a/src/syntax.c b/src/syntax.c
index f255f86..fb2a59f 100644
--- a/src/syntax.c
+++ b/src/syntax.c
@@ -1723,12 +1723,14 @@ static struct module *compile(struct term *term, struct augeas *aug) {
 /*
  * Defining native functions
  */
-static struct info *make_native_info(const char *fname, int line) {
+static struct info *
+make_native_info(struct error *error, const char *fname, int line) {
     struct info *info;
     if (make_ref(info) < 0)
         goto error;
     info->first_line = info->last_line = line;
     info->first_column = info->last_column = 0;
+    info->error = error;
     if (make_ref(info->filename) < 0)
         goto error;
     info->filename->str = strdup(fname);
@@ -1739,6 +1741,7 @@ static struct info *make_native_info(const char *fname, int line) {
 }
 
 int define_native_intl(const char *file, int line,
+                       struct error *error,
                        struct module *module, const char *name,
                        int argc, void *impl, ...) {
     assert(argc > 0);  /* We have no unit type */
@@ -1751,7 +1754,7 @@ int define_native_intl(const char *file, int line,
     struct info *info = NULL;
     struct ctx ctx;
 
-    info = make_native_info(file, line);
+    info = make_native_info(error, file, line);
     if (info == NULL)
         goto error;
 
@@ -1906,7 +1909,7 @@ static int load_module(struct augeas *aug, const char *name) {
 int interpreter_init(struct augeas *aug) {
     int r;
 
-    aug->modules = builtin_init();
+    aug->modules = builtin_init(aug->error);
 
     if (aug->flags & AUG_NO_MODL_AUTOLOAD)
         return 0;
diff --git a/src/syntax.h b/src/syntax.h
index 4d805ae..cb8be01 100644
--- a/src/syntax.h
+++ b/src/syntax.h
@@ -252,15 +252,17 @@ struct term *build_func(struct term *params, struct term *exp);
 
 struct module *module_create(const char *name);
 
-#define define_native(module, name, argc, impl, types ...)       \
-    define_native_intl(__FILE__, __LINE__, module, name, argc, impl, ## types)
+#define define_native(error, module, name, argc, impl, types ...)       \
+    define_native_intl(__FILE__, __LINE__, error, module, name,         \
+                       argc, impl, ## types)
 
 ATTRIBUTE_RETURN_CHECK
 int define_native_intl(const char *fname, int line,
+                       struct error *error,
                        struct module *module, const char *name,
                        int argc, void *impl, ...);
 
-struct module *builtin_init(void);
+struct module *builtin_init(struct error *);
 
 /* Used by augparse for some testing */
 int __aug_load_module_file(struct augeas *aug, const char *filename);
diff --git a/src/transform.c b/src/transform.c
index 397ad0d..e2ea7db 100644
--- a/src/transform.c
+++ b/src/transform.c
@@ -416,6 +416,7 @@ static int load_file(struct augeas *aug, struct lens *lens, char *filename) {
     make_ref(info);
     make_ref(info->filename);
     info->filename->str = filename;
+    info->error = aug->error;
     info->first_line = 1;
 
     tree = lns_get(info, lens, text, &err);




More information about the augeas-devel mailing list