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