Imported Upstream version 3.2.0
[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 /* Match STR against POSIX regular expression REGEX by calling regexec.  This uses
39  * the REG_NEWLINE flag, meaning that . does not match a newline and $ and ^ are
40  * relative to lines as well as the beginning and end of STR. */
41 int     match(const char *regex, const char *str);
42
43 /* Like match(), but without REG_NEWLINE, so a newline is treated like any other
44  * character */
45 int     match_no_newline(const char *regex, const char *str);
46
47 /* quote any non-alphanumeric characters in str, so that the result will only
48  * match the original string.  If anchor is true, then add ^ and $ to make sure
49  * that substrings will not match.  */
50 char *  clean_regex(const char *str, gboolean anchor);
51
52 /*
53  * Globs
54  */
55
56 /*
57  * A "glob expression" is similar to shell globs; it supports metacharacters
58  * "*" and "?", as well as character classes like "[...]" and "[!...]"
59  * (negated).  The "*" and "?" do not match filename separators ("/").  The
60  * entire expression is anchored, so it must match the string, not just a single
61  * filename component.
62  */
63
64 /* Validate that GLOB is a legal GLOB expression.  Returns a statically
65  * allocated error message on failure, or NULL on success. */
66 char *  validate_glob(const char *glob);
67
68 /* Convert a GLOB expression into a dynamically allocated regular expression */
69 char *  glob_to_regex(const char *glob);
70
71 /* Like match(), but with a glob expression */
72 int     match_glob(const char *glob, const char *str);
73
74 /*
75  * Tar Patterns
76  */
77
78 /* A "tar expression" is almost the same as a glob, except that "*" can match a
79  * filename separator ("?" cannot).  It is used by calcsize to emulate tar's exclude
80  * list patterns, which are actually significantly more complicated than this.
81  */
82
83 /* Like match(), but with a tar expression */
84 int     match_tar(const char *glob, const char *str);
85
86 /*
87  * Host expressions
88  */
89
90 /* Host expressions are described in amanda(8). */
91
92 /* Make an Amanda host expression that will match the given string exactly.
93  * There is a little bit of fuzz here involving leading and trailing "."
94  * chararacters, (so "host.org", "host.org.", and ".host.org" will all match
95  * the same expressions) but DNS considers them equivalent, too. */
96 char *  make_exact_host_expression(const char *host);
97
98 /* Like match(), but using a host expression */
99 int     match_host(const char *glob, const char *host);
100
101 /*
102  * Disk expressions
103  */
104
105 /* Disk expressions are described in amanda(8) */
106
107 /* Make an Amanda disk expression that will match the given string exactly. */
108 char *  make_exact_disk_expression(const char *disk);
109
110 /* Like match(), but using a disk expression */
111 int     match_disk(const char *glob, const char *disk);
112
113 /*
114  * Datestamp expressions
115  */
116
117 /* Datestamp expressions are described in amanda(8) */
118
119 int     match_datestamp(const char *dateexp, const char *datestamp);
120
121 /*
122  * Level expressions
123  */
124
125 /* Level expressions are either prefix matches e.g., "1", which matches "1", "10", and "123",
126  * absolute matches e.g., "3$" which only matches "3", or a range e.g., "3-5" which only
127  * matches levels 3, 4, and 5. */
128
129 /* Like match(), but using a level expression */
130 int     match_level(const char *levelexp, const char *level);
131
132 #endif /* MATCH_H */
133