2 * Copyright (c) 2008 Joerg Sonnenberger
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "bsdtar_platform.h"
27 __FBSDID("$FreeBSD$");
38 struct subst_rule *next;
41 int global:1, print:1, symlink:1;
45 struct subst_rule *first_rule, *last_rule;
49 init_substitution(struct bsdtar *bsdtar)
51 struct substitution *subst;
53 bsdtar->substitution = subst = malloc(sizeof(*subst));
55 bsdtar_errc(bsdtar, 1, errno, "Out of memory");
56 subst->first_rule = subst->last_rule = NULL;
60 add_substitution(struct bsdtar *bsdtar, const char *rule_text)
62 struct subst_rule *rule;
63 struct substitution *subst;
64 const char *end_pattern, *start_subst;
68 if ((subst = bsdtar->substitution) == NULL) {
69 init_substitution(bsdtar);
70 subst = bsdtar->substitution;
73 rule = malloc(sizeof(*rule));
75 bsdtar_errc(bsdtar, 1, errno, "Out of memory");
78 if (subst->last_rule == NULL)
79 subst->first_rule = rule;
81 subst->last_rule->next = rule;
82 subst->last_rule = rule;
84 if (*rule_text == '\0')
85 bsdtar_errc(bsdtar, 1, 0, "Empty replacement string");
86 end_pattern = strchr(rule_text + 1, *rule_text);
87 if (end_pattern == NULL)
88 bsdtar_errc(bsdtar, 1, 0, "Invalid replacement string");
90 pattern = malloc(end_pattern - rule_text);
92 bsdtar_errc(bsdtar, 1, errno, "Out of memory");
93 memcpy(pattern, rule_text + 1, end_pattern - rule_text - 1);
94 pattern[end_pattern - rule_text - 1] = '\0';
96 if ((r = regcomp(&rule->re, pattern, REG_BASIC)) != 0) {
98 regerror(r, &rule->re, buf, sizeof(buf));
99 bsdtar_errc(bsdtar, 1, 0, "Invalid regular expression: %s", buf);
103 start_subst = end_pattern + 1;
104 end_pattern = strchr(start_subst, *rule_text);
105 if (end_pattern == NULL)
106 bsdtar_errc(bsdtar, 1, 0, "Invalid replacement string");
108 rule->result = malloc(end_pattern - start_subst + 1);
109 if (rule->result == NULL)
110 bsdtar_errc(bsdtar, 1, errno, "Out of memory");
111 memcpy(rule->result, start_subst, end_pattern - start_subst);
112 rule->result[end_pattern - start_subst] = '\0';
118 while (*++end_pattern) {
119 switch (*end_pattern) {
133 bsdtar_errc(bsdtar, 1, 0, "Invalid replacement flag %c", *end_pattern);
139 realloc_strncat(struct bsdtar *bsdtar, char **str, const char *append, size_t len)
147 old_len = strlen(*str);
149 new_str = malloc(old_len + len + 1);
151 bsdtar_errc(bsdtar, 1, errno, "Out of memory");
152 memcpy(new_str, *str, old_len);
153 memcpy(new_str + old_len, append, len);
154 new_str[old_len + len] = '\0';
160 realloc_strcat(struct bsdtar *bsdtar, char **str, const char *append)
168 old_len = strlen(*str);
170 new_str = malloc(old_len + strlen(append) + 1);
172 bsdtar_errc(bsdtar, 1, errno, "Out of memory");
173 memcpy(new_str, *str, old_len);
174 strcpy(new_str + old_len, append);
180 apply_substitution(struct bsdtar *bsdtar, const char *name, char **result, int symlink_only)
182 const char *path = name;
183 regmatch_t matches[10];
185 struct subst_rule *rule;
186 struct substitution *subst;
187 int c, got_match, print_match;
191 if ((subst = bsdtar->substitution) == NULL)
197 for (rule = subst->first_rule; rule != NULL; rule = rule->next) {
198 if (symlink_only && !rule->symlink)
200 if (regexec(&rule->re, name, 10, matches, 0))
204 print_match |= rule->print;
205 realloc_strncat(bsdtar, result, name, matches[0].rm_so);
207 for (i = 0, j = 0; rule->result[i] != '\0'; ++i) {
208 if (rule->result[i] == '~') {
209 realloc_strncat(bsdtar, result, rule->result + j, i - j);
210 realloc_strncat(bsdtar, result, name, matches[0].rm_eo);
214 if (rule->result[i] != '\\')
222 realloc_strncat(bsdtar, result, rule->result + j, i - j - 1);
226 realloc_strncat(bsdtar, result, rule->result + j, i - j - 1);
227 if ((size_t)(c - '0') > (size_t)(rule->re.re_nsub)) {
232 realloc_strncat(bsdtar, result, name + matches[c - '0'].rm_so, matches[c - '0'].rm_eo - matches[c - '0'].rm_so);
242 realloc_strcat(bsdtar, result, rule->result + j);
244 name += matches[0].rm_eo;
251 realloc_strcat(bsdtar, result, name);
254 fprintf(stderr, "%s >> %s\n", path, *result);
260 cleanup_substitution(struct bsdtar *bsdtar)
262 struct subst_rule *rule;
263 struct substitution *subst;
265 if ((subst = bsdtar->substitution) == NULL)
268 while ((rule = subst->first_rule) != NULL) {
269 subst->first_rule = rule->next;
275 #endif /* HAVE_REGEX_H */