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$");
43 struct subst_rule *next;
46 unsigned int global:1, print:1, symlink:1;
50 struct subst_rule *first_rule, *last_rule;
54 init_substitution(struct bsdtar *bsdtar)
56 struct substitution *subst;
58 bsdtar->substitution = subst = malloc(sizeof(*subst));
60 bsdtar_errc(1, errno, "Out of memory");
61 subst->first_rule = subst->last_rule = NULL;
65 add_substitution(struct bsdtar *bsdtar, const char *rule_text)
67 struct subst_rule *rule;
68 struct substitution *subst;
69 const char *end_pattern, *start_subst;
73 if ((subst = bsdtar->substitution) == NULL) {
74 init_substitution(bsdtar);
75 subst = bsdtar->substitution;
78 rule = malloc(sizeof(*rule));
80 bsdtar_errc(1, errno, "Out of memory");
83 if (subst->last_rule == NULL)
84 subst->first_rule = rule;
86 subst->last_rule->next = rule;
87 subst->last_rule = rule;
89 if (*rule_text == '\0')
90 bsdtar_errc(1, 0, "Empty replacement string");
91 end_pattern = strchr(rule_text + 1, *rule_text);
92 if (end_pattern == NULL)
93 bsdtar_errc(1, 0, "Invalid replacement string");
95 pattern = malloc(end_pattern - rule_text);
97 bsdtar_errc(1, errno, "Out of memory");
98 memcpy(pattern, rule_text + 1, end_pattern - rule_text - 1);
99 pattern[end_pattern - rule_text - 1] = '\0';
101 if ((r = regcomp(&rule->re, pattern, REG_BASIC)) != 0) {
103 regerror(r, &rule->re, buf, sizeof(buf));
104 bsdtar_errc(1, 0, "Invalid regular expression: %s", buf);
108 start_subst = end_pattern + 1;
109 end_pattern = strchr(start_subst, *rule_text);
110 if (end_pattern == NULL)
111 bsdtar_errc(1, 0, "Invalid replacement string");
113 rule->result = malloc(end_pattern - start_subst + 1);
114 if (rule->result == NULL)
115 bsdtar_errc(1, errno, "Out of memory");
116 memcpy(rule->result, start_subst, end_pattern - start_subst);
117 rule->result[end_pattern - start_subst] = '\0';
123 while (*++end_pattern) {
124 switch (*end_pattern) {
138 bsdtar_errc(1, 0, "Invalid replacement flag %c", *end_pattern);
144 realloc_strncat(char **str, const char *append, size_t len)
152 old_len = strlen(*str);
154 new_str = malloc(old_len + len + 1);
156 bsdtar_errc(1, errno, "Out of memory");
157 memcpy(new_str, *str, old_len);
158 memcpy(new_str + old_len, append, len);
159 new_str[old_len + len] = '\0';
165 realloc_strcat(char **str, const char *append)
173 old_len = strlen(*str);
175 new_str = malloc(old_len + strlen(append) + 1);
177 bsdtar_errc(1, errno, "Out of memory");
178 memcpy(new_str, *str, old_len);
179 strcpy(new_str + old_len, append);
185 apply_substitution(struct bsdtar *bsdtar, const char *name, char **result, int symlink_only)
187 const char *path = name;
188 regmatch_t matches[10];
190 struct subst_rule *rule;
191 struct substitution *subst;
192 int c, got_match, print_match;
196 if ((subst = bsdtar->substitution) == NULL)
202 for (rule = subst->first_rule; rule != NULL; rule = rule->next) {
203 if (symlink_only && !rule->symlink)
205 if (regexec(&rule->re, name, 10, matches, 0))
209 print_match |= rule->print;
210 realloc_strncat(result, name, matches[0].rm_so);
212 for (i = 0, j = 0; rule->result[i] != '\0'; ++i) {
213 if (rule->result[i] == '~') {
214 realloc_strncat(result, rule->result + j, i - j);
215 realloc_strncat(result, name, matches[0].rm_eo);
219 if (rule->result[i] != '\\')
227 realloc_strncat(result, rule->result + j, i - j - 1);
239 realloc_strncat(result, rule->result + j, i - j - 1);
240 if ((size_t)(c - '0') > (size_t)(rule->re.re_nsub)) {
245 realloc_strncat(result, name + matches[c - '0'].rm_so, matches[c - '0'].rm_eo - matches[c - '0'].rm_so);
255 realloc_strcat(result, rule->result + j);
257 name += matches[0].rm_eo;
264 realloc_strcat(result, name);
267 fprintf(stderr, "%s >> %s\n", path, *result);
273 cleanup_substitution(struct bsdtar *bsdtar)
275 struct subst_rule *rule;
276 struct substitution *subst;
278 if ((subst = bsdtar->substitution) == NULL)
281 while ((rule = subst->first_rule) != NULL) {
282 subst->first_rule = rule->next;
288 #endif /* HAVE_REGEX_H */