967c852b854c880738bd8df54aeba104fb681bae
[debian/amanda] / common-src / match.h
1 /*
2  * Copyright (c) 2010 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published
6  * by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16  *
17  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19  */
20
21 #ifndef MATCH_H
22 #define MATCH_H
23
24 #include <glib.h>
25
26 /*
27  * Regular expressions
28  */
29
30 /* The regular expressions used here are POSIX extended regular expressions;
31  * see http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap09.html
32  */
33
34 /* validate that REGEX is a valid POSIX regular expression by calling regcomp.
35  * Returns a statically allocated error message on failure or NULL on success. */
36 char *  validate_regexp(const char *regex);
37
38 /*
39  * Match the string "str" against POSIX regex "regex" with regexec(), with
40  * REG_NEWLINE set (match_newline == TRUE) or not.
41  *
42  * REG_NEWLINE means two things:
43  * - the dot won't match a newline;
44  * - ^ and $ will match around \n in the input string (as well as the beginning
45  *   and end of the input).
46  */
47
48 int do_match(const char *regex, const char *str, gboolean match_newline);
49
50 #define match(regex, str) do_match(regex, str, TRUE)
51 #define match_no_newline(regex, str) do_match(regex, str, FALSE)
52
53 /* quote any non-alphanumeric characters in str, so that the result will only
54  * match the original string.  If anchor is true, then add ^ and $ to make sure
55  * that substrings will not match.  */
56 char *  clean_regex(const char *str, gboolean anchor);
57
58 /*
59  * Globs
60  */
61
62 /*
63  * A "glob expression" is similar to shell globs; it supports metacharacters
64  * "*" and "?", as well as character classes like "[...]" and "[!...]"
65  * (negated).  The "*" and "?" do not match filename separators ("/").  The
66  * entire expression is anchored, so it must match the string, not just a single
67  * filename component.
68  */
69
70 /* Validate that GLOB is a legal GLOB expression.  Returns a statically
71  * allocated error message on failure, or NULL on success. */
72 char *  validate_glob(const char *glob);
73
74 /* Convert a GLOB expression into a dynamically allocated regular expression */
75 char *  glob_to_regex(const char *glob);
76
77 /* Like match(), but with a glob expression */
78 int     match_glob(const char *glob, const char *str);
79
80 /*
81  * Tar Patterns
82  */
83
84 /* A "tar expression" is almost the same as a glob, except that "*" can match a
85  * filename separator ("?" cannot).  It is used by calcsize to emulate tar's exclude
86  * list patterns, which are actually significantly more complicated than this.
87  */
88
89 /* Like match(), but with a tar expression */
90 int     match_tar(const char *glob, const char *str);
91
92 /*
93  * Host expressions
94  */
95
96 /* Host expressions are described in amanda(8). */
97
98 /* Make an Amanda host expression that will match the given string exactly.
99  * There is a little bit of fuzz here involving leading and trailing "."
100  * chararacters, (so "host.org", "host.org.", and ".host.org" will all match
101  * the same expressions) but DNS considers them equivalent, too. */
102 char *  make_exact_host_expression(const char *host);
103
104 /* Like match(), but using a host expression */
105 int     match_host(const char *glob, const char *host);
106
107 /*
108  * Disk expressions
109  */
110
111 /* Disk expressions are described in amanda(8) */
112
113 /* Make an Amanda disk expression that will match the given string exactly. */
114 char *  make_exact_disk_expression(const char *disk);
115
116 /* Like match(), but using a disk expression */
117 int     match_disk(const char *glob, const char *disk);
118
119 /*
120  * Datestamp expressions
121  */
122
123 /* Datestamp expressions are described in amanda(8) */
124
125 int     match_datestamp(const char *dateexp, const char *datestamp);
126
127 /*
128  * Level expressions
129  */
130
131 /* Level expressions are either prefix matches e.g., "1", which matches "1", "10", and "123",
132  * absolute matches e.g., "3$" which only matches "3", or a range e.g., "3-5" which only
133  * matches levels 3, 4, and 5. */
134
135 /* Like match(), but using a level expression */
136 int     match_level(const char *levelexp, const char *level);
137
138 #endif /* MATCH_H */
139