Imported Upstream version 1.12.0
[platform/upstream/augeas.git] / src / lens.h
index 902197d..d1a37cd 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * lens.h: Repreentation of lenses
  *
- * Copyright (C) 2007-2011 David Lutterkort
+ * Copyright (C) 2007-2016 David Lutterkort
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -79,6 +79,7 @@ struct lens {
     struct regexp            *atype;
     struct regexp            *ktype;
     struct regexp            *vtype;
+    struct jmt               *jmt;    /* When recursive == 1, might have jmt */
     unsigned int              value : 1;
     unsigned int              key : 1;
     unsigned int              recursive : 1;
@@ -89,9 +90,6 @@ struct lens {
     union {
         /* Primitive lenses */
         struct {                   /* L_DEL uses both */
-            /* L_DEL string set to NULL means it belongs to parent L_SQUARE lens
-             * and the put and create copy the current key
-             */
             struct regexp *regexp; /* L_STORE, L_KEY */
             struct string *string; /* L_VALUE, L_LABEL, L_SEQ, L_COUNTER */
         };
@@ -119,7 +117,6 @@ struct lens {
              * lens.
              */
             struct lens *alias;
-            struct jmt  *jmt;
         };
     };
 };
@@ -143,8 +140,9 @@ struct value *lns_make_plus(struct info *, struct lens *,
                             int check);
 struct value *lns_make_maybe(struct info *, struct lens *,
                              int check);
-struct value *lns_make_square(struct info *, struct regexp *, struct lens *,
-                              int check);
+struct value *lns_make_square(struct info *, struct lens *, struct lens *,
+                              struct lens *lens, int check);
+
 
 /* Pretty-print a lens */
 char *format_lens(struct lens *l);
@@ -165,13 +163,15 @@ struct skel {
     enum lens_tag tag;
     union {
         char        *text;    /* L_DEL */
-        struct skel *skels;   /* L_CONCAT, L_STAR */
+        struct skel *skels;   /* L_CONCAT, L_STAR, L_SQUARE */
     };
     /* Also tag == L_SUBTREE, with no data in the union */
 };
 
 struct lns_error {
     struct lens  *lens;
+    struct lens  *last;       /* The last lens that matched */
+    struct lens  *next;       /* The next lens that should match but doesn't */
     int           pos;        /* Errors from get/parse */
     char         *path;       /* Errors from put, pos will be -1 */
     char         *message;
@@ -185,21 +185,30 @@ void free_skel(struct skel *skel);
 void free_dict(struct dict *dict);
 void free_lns_error(struct lns_error *err);
 
-/* Parse text TEXT with LENS. INFO indicats where TEXT was read from.
+/* Parse text TEXT with LENS. INFO indicates where TEXT was read from.
  *
  * If ERR is non-NULL, *ERR is set to NULL on success, and to an error
  * message on failure; the constructed tree is always returned. If ERR is
  * NULL, return the tree on success, and NULL on failure.
  *
- * FLAGS controls what is printed and should be a set of flags from enum
- * parse_flags
+ * ENABLE_SPAN indicates whether span information should be collected or not
  */
 struct tree *lns_get(struct info *info, struct lens *lens, const char *text,
-                     struct lns_error **err);
+                     int enable_span, struct lns_error **err);
 struct skel *lns_parse(struct lens *lens, const char *text,
                        struct dict **dict, struct lns_error **err);
-void lns_put(FILE *out, struct lens *lens, struct tree *tree,
-             const char *text, struct lns_error **err);
+
+/* Write tree TREE that was initially read from TEXT (but might have been
+ * modified) into file OUT using LENS.
+ *
+ * If ERR is non-NULL, *ERR is set to NULL on success, and to an error
+ * message on failure.
+ *
+ * INFO indicates where we are writing to, and its flags indicate whether
+ * to update spans or not.
+ */
+void lns_put(struct info *info, FILE *out, struct lens *lens, struct tree *tree,
+             const char *text, int enable_span, struct lns_error **err);
 
 /* Free up temporary data structures, most importantly compiled
    regular expressions */
@@ -234,17 +243,33 @@ void free_lens(struct lens *lens);
 
    This range must include the ENC_* characters
 */
-#define RESERVED_FROM '\001'
-#define RESERVED_TO   ENC_SLASH_CH
+#define RESERVED_FROM "\001"
+#define RESERVED_TO   ENC_SLASH
+#define RESERVED_FROM_CH (RESERVED_FROM[0])
+#define RESERVED_TO_CH   ENC_SLASH_CH
+/* The range of reserved chars as it appears in a regex */
+#define RESERVED_RANGE_RX RESERVED_FROM "-" RESERVED_TO
+/* The equivalent of "." in a regexp for display */
+#define RESERVED_DOT_RX "[^" RESERVED_RANGE_RX "\n]"
 
 /* The length of the string S encoded */
 #define ENCLEN(s) ((s) == NULL ? strlen(ENC_NULL) : strlen(s))
 #define ENCSTR(s) ((s) == NULL ? ENC_NULL : s)
 
+/* helper to access first and last child */
+#define child_first(l) (l)->children[0]
+#define child_last(l) (l)->children[(l)->nchildren - 1]
+
 /* Format an encoded level as
  *    { key1 = value1 } { key2 = value2 } .. { keyN = valueN }
  */
 char *enc_format(const char *e, size_t len);
+/* Format an encoded level similar to ENC_FORMAT, but put each tree node
+ * on a new line indented by INDENT spaces. If INDENT is negative, produce the
+ * same output as ENC_FORMAT
+ *    { key1 = value1 } { key2 = value2 } .. { keyN = valueN }
+ */
+char *enc_format_indent(const char *e, size_t len, int indent);
 
 #if ENABLE_DEBUG
 void dump_lens_tree(struct lens *lens);