Imported Upstream version 1.6.0
[platform/upstream/augeas.git] / src / regexp.h
1 /*
2  * regexp.h: wrappers for regexp handling
3  *
4  * Copyright (C) 2009-2015 David Lutterkort
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
19  *
20  * Author: David Lutterkort <lutter@redhat.com>
21  */
22
23 #ifndef REGEXP_H_
24 #define REGEXP_H_
25
26 #include <stdio.h>
27 #include <regex.h>
28
29 struct regexp {
30     unsigned int              ref;
31     struct info              *info;
32     struct string            *pattern;
33     struct re_pattern_buffer *re;
34     unsigned int              nocase : 1;
35 };
36
37 void print_regexp(FILE *out, struct regexp *regexp);
38
39 /* Make a regexp with pattern PAT, which is not copied. Ownership
40  * of INFO is taken.
41  */
42 struct regexp *make_regexp(struct info *info, char *pat, int nocase);
43
44 /* Make a regexp with pattern PAT, which is copied. Ownership of INFO is
45  * taken. Escape sequences like \n in PAT are interpreted.
46  */
47 struct regexp *make_regexp_unescape(struct info *info, const char *pat,
48                                     int nocase);
49
50 /* Return 1 if R is an empty pattern, i.e. one consisting of nothing but
51    '(' and ')' characters, 0 otherwise */
52 int regexp_is_empty_pattern(struct regexp *r);
53
54 /* Make a regexp that matches TEXT literally; the string TEXT
55  * is not used by the returned rgexp and must be freed by the caller
56  */
57 struct regexp *make_regexp_literal(struct info *info, const char *text);
58
59 /* Make a regexp from a glob pattern */
60 struct regexp *make_regexp_from_glob(struct info *info, const char *glob);
61
62 /* Do not call directly, use UNREF instead */
63 void free_regexp(struct regexp *regexp);
64
65 /* Compile R->PATTERN into R->RE; return -1 and print an error
66  * if compilation fails. Return 0 otherwise
67  */
68 int regexp_compile(struct regexp *r);
69
70 /* Check the syntax of R->PATTERN; return -1 if the pattern has a syntax
71  * error, and a string indicating the error in *C. Return 0 if the pattern
72  * is a valid regular expression.
73  */
74 int regexp_check(struct regexp *r, const char **msg);
75
76 /* Call RE_MATCH on R->RE and return its result; if R hasn't been compiled
77  * yet, compile it. Return -3 if compilation fails
78  */
79 int regexp_match(struct regexp *r, const char *string, const int size,
80                  const int start, struct re_registers *regs);
81
82 /* Return 1 if R matches the empty string, 0 otherwise */
83 int regexp_matches_empty(struct regexp *r);
84
85 /* Return the number of subexpressions (parentheses) inside R. May cause
86  * compilation of R; return -1 if compilation fails.
87  */
88 int regexp_nsub(struct regexp *r);
89
90 struct regexp *
91 regexp_union(struct info *, struct regexp *r1, struct regexp *r2);
92
93 struct regexp *
94 regexp_concat(struct info *, struct regexp *r1, struct regexp *r2);
95
96 struct regexp *
97 regexp_union_n(struct info *, int n, struct regexp **r);
98
99 struct regexp *
100 regexp_concat_n(struct info *, int n, struct regexp **r);
101
102 struct regexp *
103 regexp_iter(struct info *info, struct regexp *r, int min, int max);
104
105 /* Return a new REGEXP that matches all the words matched by R1 but
106  * not by R2
107  */
108 struct regexp *
109 regexp_minus(struct info *info, struct regexp *r1, struct regexp *r2);
110
111 struct regexp *
112 regexp_maybe(struct info *info, struct regexp *r);
113
114 struct regexp *regexp_make_empty(struct info *);
115
116 /* Free up temporary data structures, most importantly compiled
117    regular expressions */
118 void regexp_release(struct regexp *regexp);
119
120 /* Produce a printable representation of R */
121 char *regexp_escape(const struct regexp *r);
122
123 /* If R is case-insensitive, expand its pattern so that it matches the same
124  * string even when used in a case-sensitive match. */
125 char *regexp_expand_nocase(struct regexp *r);
126 #endif
127
128
129 /*
130  * Local variables:
131  *  indent-tabs-mode: nil
132  *  c-indent-level: 4
133  *  c-basic-offset: 4
134  *  tab-width: 4
135  * End:
136  */