[augeas-devel] augeas: master - Split regexps, info and strings out of syntax.[ch]

David Lutterkort lutter at fedoraproject.org
Thu Sep 24 19:09:02 UTC 2009


Gitweb:        http://git.fedorahosted.org/git/augeas.git?p=augeas.git;a=commitdiff;h=68a2c6f0e755f5f19b5176aff614d1eaef7ca3d0
Commit:        68a2c6f0e755f5f19b5176aff614d1eaef7ca3d0
Parent:        d92bf7da008ee94e17f9da0b2ed380fa35a392d2
Author:        David Lutterkort <lutter at redhat.com>
AuthorDate:    Thu Sep 17 16:04:06 2009 -0700
Committer:     David Lutterkort <lutter at redhat.com>
CommitterDate: Mon Sep 21 17:39:57 2009 -0700

Split regexps, info and strings out of syntax.[ch]

  * Move struct string and struct info and related functions into info.[ch]
  * Move headers for regexp.c into new file regexp.h

This makes it easier to reuse these outside of syntax.[ch] - including
syntax.h in get.c and put.c was way too much anyway.
---
 src/Makefile.am |    5 +-
 src/get.c       |    4 +-
 src/info.c      |  117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/info.h      |   69 ++++++++++++++++++++++++++++++++
 src/put.c       |    3 +-
 src/regexp.c    |    2 +
 src/regexp.h    |  116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/syntax.c    |   76 -----------------------------------
 src/syntax.h    |  107 +-------------------------------------------------
 9 files changed, 314 insertions(+), 185 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index a78fd39..e835e31 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -22,8 +22,9 @@ include_HEADERS = augeas.h fa.h
 libaugeas_la_SOURCES = augeas.h augeas.c pathx.c \
 	internal.h internal.c \
 	memory.h memory.c ref.h \
-    syntax.c syntax.h parser.y builtin.c lens.c lens.h regexp.c \
-	transform.h transform.c ast.c get.c put.c list.h
+    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
 
 if USE_VERSION_SCRIPT
   AUGEAS_VERSION_SCRIPT = $(VERSION_SCRIPT_FLAGS)$(srcdir)/augeas_sym.version
diff --git a/src/get.c b/src/get.c
index 7ab8bad..057b6b3 100644
--- a/src/get.c
+++ b/src/get.c
@@ -25,10 +25,12 @@
 #include <regex.h>
 #include <stdarg.h>
 
-#include "syntax.h"
+#include "regexp.h"
 #include "list.h"
 #include "internal.h"
 #include "memory.h"
+#include "info.h"
+#include "lens.h"
 
 /* Our favorite error message */
 static const char *const short_iteration =
diff --git a/src/info.c b/src/info.c
new file mode 100644
index 0000000..429e2a2
--- /dev/null
+++ b/src/info.c
@@ -0,0 +1,117 @@
+/*
+ * info.c: filename/linenumber information for parser/interpreter
+ *
+ * Copyright (C) 2007-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 "info.h"
+#include "internal.h"
+#include "ref.h"
+
+/*
+ * struct string
+ */
+struct string *make_string(char *str) {
+    struct string *string;
+    make_ref(string);
+    string->str = str;
+    return string;
+}
+
+struct string *dup_string(const char *str) {
+    struct string *string;
+    make_ref(string);
+    if (str == NULL)
+        string->str = strdup("");
+    else
+        string->str = strdup(str);
+    if (string->str == NULL)
+        unref(string, string);
+    return string;
+}
+
+void free_string(struct string *string) {
+    if (string == NULL)
+        return;
+    assert(string->ref == 0);
+    free(string->str);
+    free(string);
+}
+
+/*
+ * struct info
+ */
+char *format_info(struct info *info) {
+    const char *fname;
+    char *result;
+    int r = 0;
+    int fl = info->first_line, ll = info->last_line;
+    int fc = info->first_column, lc = info->last_column;
+    fname = (info->filename != NULL) ? info->filename->str : "(unknown file)";
+
+    if (fl > 0) {
+        if (fl == ll) {
+            if (fc == lc) {
+                r = asprintf(&result, "%s:%d.%d", fname, fl, fc);
+            } else {
+                r = asprintf(&result, "%s:%d.%d-.%d", fname, fl, fc, lc);
+            }
+        } else {
+            r = asprintf(&result, "%s:%d.%d-%d.%d", fname, fl, fc, ll, lc);
+        }
+    }
+    return (r == -1) ? NULL : result;
+}
+
+void print_info(FILE *out, struct info *info) {
+    fprintf(out, "%s:",
+            info->filename != NULL ? info->filename->str : "(unknown file)");
+    if (info->first_line > 0) {
+        if (info->first_line == info->last_line) {
+            if (info->first_column == info->last_column) {
+                fprintf(out, "%d.%d:", info->first_line, info->first_column);
+            } else {
+                fprintf(out, "%d.%d-.%d:", info->first_line,
+                        info->first_column, info->last_column);
+            }
+        } else {
+            fprintf(out, "%d.%d-%d.%d:",
+                    info->first_line, info->first_column,
+                    info->last_line, info->last_column);
+        }
+    }
+}
+
+void free_info(struct info *info) {
+    if (info == NULL)
+        return;
+    assert(info->ref == 0);
+    unref(info->filename, string);
+    free(info);
+}
+
+/*
+ * Local variables:
+ *  indent-tabs-mode: nil
+ *  c-indent-level: 4
+ *  c-basic-offset: 4
+ *  tab-width: 4
+ * End:
+ */
diff --git a/src/info.h b/src/info.h
new file mode 100644
index 0000000..09f62d3
--- /dev/null
+++ b/src/info.h
@@ -0,0 +1,69 @@
+/*
+ * info.h: filename/linenumber information for parser/interpreter
+ *
+ * Copyright (C) 2007-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 INFO_H_
+#define INFO_H_
+
+#include <stdio.h>
+
+/* Reference-counted strings */
+struct string {
+    unsigned int   ref;
+    char          *str;
+};
+
+struct string *make_string(char *str);
+
+/* Duplicate a string; if STR is NULL, use the empty string "" */
+struct string *dup_string(const char *str);
+
+/* Do not call directly, use UNREF instead */
+void free_string(struct string *string);
+
+/* File information */
+struct info {
+    unsigned int ref;
+    struct string *filename;
+    unsigned int first_line;
+    unsigned int first_column;
+    unsigned int last_line;
+    unsigned int last_column;
+};
+
+char *format_info(struct info *info);
+
+void print_info(FILE *out, struct info *info);
+
+/* Do not call directly, use UNREF instead */
+void free_info(struct info *info);
+
+#endif
+
+
+/*
+ * Local variables:
+ *  indent-tabs-mode: nil
+ *  c-indent-level: 4
+ *  c-basic-offset: 4
+ *  tab-width: 4
+ * End:
+ */
diff --git a/src/put.c b/src/put.c
index fe3df39..2b4e60f 100644
--- a/src/put.c
+++ b/src/put.c
@@ -23,8 +23,9 @@
 #include <config.h>
 
 #include <stdarg.h>
-#include "syntax.h"
+#include "regexp.h"
 #include "memory.h"
+#include "lens.h"
 
 /* Data structure to keep track of where we are in the tree. The split
  * describes a sublist of the list of siblings in the current tree. The
diff --git a/src/regexp.c b/src/regexp.c
index df99656..f6a851f 100644
--- a/src/regexp.c
+++ b/src/regexp.c
@@ -21,7 +21,9 @@
  */
 
 #include <config.h>
+#include <regex.h>
 
+#include "internal.h"
 #include "syntax.h"
 #include "memory.h"
 
diff --git a/src/regexp.h b/src/regexp.h
new file mode 100644
index 0000000..ef601d8
--- /dev/null
+++ b/src/regexp.h
@@ -0,0 +1,116 @@
+/*
+ * regexp.h: wrappers for regexp handling
+ *
+ * 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 REGEXP_H_
+#define REGEXP_H_
+
+#include <stdio.h>
+#include <regex.h>
+
+struct regexp {
+    unsigned int              ref;
+    struct info              *info;
+    struct string            *pattern;
+    struct re_pattern_buffer *re;
+};
+
+void print_regexp(FILE *out, struct regexp *regexp);
+
+/* Make a regexp with pattern PAT, which is not copied. Ownership
+ * of INFO is taken.
+ */
+struct regexp *make_regexp(struct info *info, char *pat);
+
+/* Return 1 if R is an empty pattern, i.e. one consisting of nothing but
+   '(' and ')' characters, 0 otherwise */
+int regexp_is_empty_pattern(struct regexp *r);
+
+/* Make a regexp that matches TEXT literally; the string TEXT
+ * is not used by the returned rgexp and must be freed by the caller
+ */
+struct regexp *make_regexp_literal(struct info *info, const char *text);
+
+/* Do not call directly, use UNREF instead */
+void free_regexp(struct regexp *regexp);
+
+/* Compile R->PATTERN into R->RE; return -1 and print an error
+ * if compilation fails. Return 0 otherwise
+ */
+int regexp_compile(struct regexp *r);
+
+/* Call RE_MATCH on R->RE and return its result; if R hasn't been compiled
+ * yet, compile it. Return -3 if compilation fails
+ */
+int regexp_match(struct regexp *r, const char *string, const int size,
+                 const int start, struct re_registers *regs);
+
+/* Return 1 if R matches the empty string, 0 otherwise */
+int regexp_matches_empty(struct regexp *r);
+
+/* Return the number of subexpressions (parentheses) inside R. May cause
+ * compilation of R; return -1 if compilation fails.
+ */
+int regexp_nsub(struct regexp *r);
+
+struct regexp *
+regexp_union(struct info *, struct regexp *r1, struct regexp *r2);
+
+struct regexp *
+regexp_concat(struct info *, struct regexp *r1, struct regexp *r2);
+
+struct regexp *
+regexp_union_n(struct info *, int n, struct regexp **r);
+
+struct regexp *
+regexp_concat_n(struct info *, int n, struct regexp **r);
+
+struct regexp *
+regexp_iter(struct info *info, struct regexp *r, int min, int max);
+
+/* Return a new REGEXP that matches all the words matched by R1 but
+ * not by R2
+ */
+struct regexp *
+regexp_minus(struct info *info, struct regexp *r1, struct regexp *r2);
+
+struct regexp *
+regexp_maybe(struct info *info, struct regexp *r);
+
+struct regexp *regexp_make_empty(struct info *);
+
+/* Free up temporary data structures, most importantly compiled
+   regular expressions */
+void regexp_release(struct regexp *regexp);
+
+/* Produce a printable representation of R */
+char *regexp_escape(const struct regexp *r);
+#endif
+
+
+/*
+ * Local variables:
+ *  indent-tabs-mode: nil
+ *  c-indent-level: 4
+ *  c-basic-offset: 4
+ *  tab-width: 4
+ * End:
+ */
diff --git a/src/syntax.c b/src/syntax.c
index 014bfb6..3f06c05 100644
--- a/src/syntax.c
+++ b/src/syntax.c
@@ -75,47 +75,6 @@ struct ctx {
     struct binding *local;
 };
 
-char *format_info(struct info *info) {
-    const char *fname;
-    char *result;
-    int r = 0;
-    int fl = info->first_line, ll = info->last_line;
-    int fc = info->first_column, lc = info->last_column;
-    fname = (info->filename != NULL) ? info->filename->str : "(unknown file)";
-
-    if (fl > 0) {
-        if (fl == ll) {
-            if (fc == lc) {
-                r = asprintf(&result, "%s:%d.%d", fname, fl, fc);
-            } else {
-                r = asprintf(&result, "%s:%d.%d-.%d", fname, fl, fc, lc);
-            }
-        } else {
-            r = asprintf(&result, "%s:%d.%d-%d.%d", fname, fl, fc, ll, lc);
-        }
-    }
-    return (r == -1) ? NULL : result;
-}
-
-void print_info(FILE *out, struct info *info) {
-    fprintf(out, "%s:",
-            info->filename != NULL ? info->filename->str : "(unknown file)");
-    if (info->first_line > 0) {
-        if (info->first_line == info->last_line) {
-            if (info->first_column == info->last_column) {
-                fprintf(out, "%d.%d:", info->first_line, info->first_column);
-            } else {
-                fprintf(out, "%d.%d-.%d:", info->first_line,
-                        info->first_column, info->last_column);
-            }
-        } else {
-            fprintf(out, "%d.%d-%d.%d:",
-                    info->first_line, info->first_column,
-                    info->last_line, info->last_column);
-        }
-    }
-}
-
 void syntax_error(struct info *info, const char *format, ...) {
     va_list ap;
 
@@ -151,22 +110,6 @@ void assert_error_at(const char *srcfile, int srclineno, struct info *info,
     fprintf(stderr, "\n");
 }
 
-void free_string(struct string *string) {
-    if (string == NULL)
-        return;
-    assert(string->ref == 0);
-    free(string->str);
-    free(string);
-}
-
-void free_info(struct info *info) {
-    if (info == NULL)
-        return;
-    assert(info->ref == 0);
-    unref(info->filename, string);
-    free(info);
-}
-
 static void free_param(struct param *param) {
     if (param == NULL)
         return;
@@ -352,25 +295,6 @@ struct value *make_value(enum value_tag tag, struct info *info) {
     return value;
 }
 
-struct string *make_string(char *str) {
-    struct string *string;
-    make_ref(string);
-    string->str = str;
-    return string;
-}
-
-struct string *dup_string(const char *str) {
-    struct string *string;
-    make_ref(string);
-    if (str == NULL)
-        string->str = strdup("");
-    else
-        string->str = strdup(str);
-    if (string->str == NULL)
-        unref(string, string);
-    return string;
-}
-
 struct term *make_app_term(struct term *lambda, struct term *arg,
                            struct info *info) {
   struct term *app = make_term(A_APP, info);
diff --git a/src/syntax.h b/src/syntax.h
index 7453f19..f0c21a2 100644
--- a/src/syntax.h
+++ b/src/syntax.h
@@ -23,25 +23,13 @@
 #ifndef SYNTAX_H_
 #define SYNTAX_H_
 
-#include <regex.h>
 #include <limits.h>
 #include "internal.h"
 #include "lens.h"
 #include "ref.h"
 #include "fa.h"
-
-struct info {
-    unsigned int ref;
-    struct string *filename;
-    unsigned int first_line;
-    unsigned int first_column;
-    unsigned int last_line;
-    unsigned int last_column;
-};
-
-/* syntax.c */
-char *format_info(struct info *info);
-void print_info(FILE *out, struct info *info);
+#include "regexp.h"
+#include "info.h"
 
 void syntax_error(struct info *info, const char *format, ...)
     ATTRIBUTE_FORMAT(printf, 2, 3);
@@ -128,91 +116,6 @@ struct param {
     struct type   *type;
 };
 
-struct string {
-    unsigned int   ref;
-    char          *str;
-};
-
-struct regexp {
-    unsigned int              ref;
-    struct info              *info;
-    struct string            *pattern;
-    struct re_pattern_buffer *re;
-};
-
-/* Defined in regexp.c */
-
-void print_regexp(FILE *out, struct regexp *regexp);
-
-/* Make a regexp with pattern PAT, which is not copied. Ownership
- * of INFO is taken.
- */
-struct regexp *make_regexp(struct info *info, char *pat);
-
-/* Return 1 if R is an empty pattern, i.e. one consisting of nothing but
-   '(' and ')' characters, 0 otherwise */
-int regexp_is_empty_pattern(struct regexp *r);
-
-/* Make a regexp that matches TEXT literally; the string TEXT
- * is not used by the returned rgexp and must be freed by the caller
- */
-struct regexp *make_regexp_literal(struct info *info, const char *text);
-
-/* Do not call directly, use UNREF instead */
-void free_regexp(struct regexp *regexp);
-
-/* Compile R->PATTERN into R->RE; return -1 and print an error
- * if compilation fails. Return 0 otherwise
- */
-int regexp_compile(struct regexp *r);
-
-/* Call RE_MATCH on R->RE and return its result; if R hasn't been compiled
- * yet, compile it. Return -3 if compilation fails
- */
-int regexp_match(struct regexp *r, const char *string, const int size,
-                 const int start, struct re_registers *regs);
-
-/* Return 1 if R matches the empty string, 0 otherwise */
-int regexp_matches_empty(struct regexp *r);
-
-/* Return the number of subexpressions (parentheses) inside R. May cause
- * compilation of R; return -1 if compilation fails.
- */
-int regexp_nsub(struct regexp *r);
-
-struct regexp *
-regexp_union(struct info *, struct regexp *r1, struct regexp *r2);
-
-struct regexp *
-regexp_concat(struct info *, struct regexp *r1, struct regexp *r2);
-
-struct regexp *
-regexp_union_n(struct info *, int n, struct regexp **r);
-
-struct regexp *
-regexp_concat_n(struct info *, int n, struct regexp **r);
-
-struct regexp *
-regexp_iter(struct info *info, struct regexp *r, int min, int max);
-
-/* Return a new REGEXP that matches all the words matched by R1 but
- * not by R2
- */
-struct regexp *
-regexp_minus(struct info *info, struct regexp *r1, struct regexp *r2);
-
-struct regexp *
-regexp_maybe(struct info *info, struct regexp *r);
-
-struct regexp *regexp_make_empty(struct info *);
-
-/* Free up temporary data structures, most importantly compiled
-   regular expressions */
-void regexp_release(struct regexp *regexp);
-
-/* Produce a printable representation of R */
-char *regexp_escape(const struct regexp *r);
-
 struct native {
     unsigned int argc;
     struct type *type;
@@ -315,14 +218,10 @@ void free_type(struct type *type);
 struct term *make_term(enum term_tag tag, struct info *info);
 struct term *make_param(char *name, struct type *type, struct info *info);
 struct value *make_value(enum value_tag tag, struct info *info);
-struct string *make_string(char *str);
 struct term *make_app_term(struct term *func, struct term *arg,
                            struct info *info);
 struct term *make_app_ident(char *id, struct term *func, struct info *info);
 
-/* Duplicate a string; if STR is NULL, use the empty string "" */
-struct string *dup_string(const char *str);
-
 /* Make an EXN value
  * Receive ownership of INFO
  *
@@ -343,8 +242,6 @@ void exn_printf_line(struct value *exn, const char *format, ...)
     ATTRIBUTE_FORMAT(printf, 2, 3);
 
 /* Do not call these directly, use UNREF instead */
-void free_info(struct info *info);
-void free_string(struct string *string);
 void free_value(struct value *v);
 void free_module(struct module *module);
 




More information about the augeas-devel mailing list