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$");
29 #if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H)
33 #ifdef HAVE_PCREPOSIX_H
34 #include <pcreposix.h>
48 struct subst_rule *next;
51 unsigned int global:1, print:1, regular:1, symlink:1, hardlink:1;
55 struct subst_rule *first_rule, *last_rule;
59 init_substitution(struct bsdtar *bsdtar)
61 struct substitution *subst;
63 bsdtar->substitution = subst = malloc(sizeof(*subst));
65 lafe_errc(1, errno, "Out of memory");
66 subst->first_rule = subst->last_rule = NULL;
70 add_substitution(struct bsdtar *bsdtar, const char *rule_text)
72 struct subst_rule *rule;
73 struct substitution *subst;
74 const char *end_pattern, *start_subst;
78 if ((subst = bsdtar->substitution) == NULL) {
79 init_substitution(bsdtar);
80 subst = bsdtar->substitution;
83 rule = malloc(sizeof(*rule));
85 lafe_errc(1, errno, "Out of memory");
89 if (subst->last_rule == NULL)
90 subst->first_rule = rule;
92 subst->last_rule->next = rule;
93 subst->last_rule = rule;
95 if (*rule_text == '\0')
96 lafe_errc(1, 0, "Empty replacement string");
97 end_pattern = strchr(rule_text + 1, *rule_text);
98 if (end_pattern == NULL)
99 lafe_errc(1, 0, "Invalid replacement string");
101 pattern = malloc(end_pattern - rule_text);
103 lafe_errc(1, errno, "Out of memory");
104 memcpy(pattern, rule_text + 1, end_pattern - rule_text - 1);
105 pattern[end_pattern - rule_text - 1] = '\0';
107 if ((r = regcomp(&rule->re, pattern, REG_BASIC)) != 0) {
109 regerror(r, &rule->re, buf, sizeof(buf));
110 lafe_errc(1, 0, "Invalid regular expression: %s", buf);
114 start_subst = end_pattern + 1;
115 end_pattern = strchr(start_subst, *rule_text);
116 if (end_pattern == NULL)
117 lafe_errc(1, 0, "Invalid replacement string");
119 rule->result = malloc(end_pattern - start_subst + 1);
120 if (rule->result == NULL)
121 lafe_errc(1, errno, "Out of memory");
122 memcpy(rule->result, start_subst, end_pattern - start_subst);
123 rule->result[end_pattern - start_subst] = '\0';
126 rule->global = 0; /* Don't do multiple replacements. */
127 rule->print = 0; /* Don't print. */
128 rule->regular = 1; /* Rewrite regular filenames. */
129 rule->symlink = 1; /* Rewrite symlink targets. */
130 rule->hardlink = 1; /* Rewrite hardlink targets. */
132 while (*++end_pattern) {
133 switch (*end_pattern) {
161 lafe_errc(1, 0, "Invalid replacement flag %c", *end_pattern);
167 realloc_strncat(char **str, const char *append, size_t len)
175 old_len = strlen(*str);
177 new_str = malloc(old_len + len + 1);
179 lafe_errc(1, errno, "Out of memory");
181 memcpy(new_str, *str, old_len);
182 memcpy(new_str + old_len, append, len);
183 new_str[old_len + len] = '\0';
189 realloc_strcat(char **str, const char *append)
197 old_len = strlen(*str);
199 new_str = malloc(old_len + strlen(append) + 1);
201 lafe_errc(1, errno, "Out of memory");
203 memcpy(new_str, *str, old_len);
204 strcpy(new_str + old_len, append);
210 apply_substitution(struct bsdtar *bsdtar, const char *name, char **result,
211 int symlink_target, int hardlink_target)
213 const char *path = name;
214 regmatch_t matches[10];
216 struct subst_rule *rule;
217 struct substitution *subst;
218 int c, got_match, print_match;
222 if ((subst = bsdtar->substitution) == NULL)
228 for (rule = subst->first_rule; rule != NULL; rule = rule->next) {
229 if (symlink_target) {
232 } else if (hardlink_target) {
235 } else { /* Regular filename. */
241 if (regexec(&rule->re, name, 10, matches, 0))
245 print_match |= rule->print;
246 realloc_strncat(result, name, matches[0].rm_so);
248 for (i = 0, j = 0; rule->result[i] != '\0'; ++i) {
249 if (rule->result[i] == '~') {
250 realloc_strncat(result, rule->result + j, i - j);
251 realloc_strncat(result,
252 name + matches[0].rm_so,
253 matches[0].rm_eo - matches[0].rm_so);
257 if (rule->result[i] != '\\')
265 realloc_strncat(result, rule->result + j, i - j - 1);
277 realloc_strncat(result, rule->result + j, i - j - 1);
278 if ((size_t)(c - '0') > (size_t)(rule->re.re_nsub)) {
283 realloc_strncat(result, name + matches[c - '0'].rm_so, matches[c - '0'].rm_eo - matches[c - '0'].rm_so);
293 realloc_strcat(result, rule->result + j);
295 name += matches[0].rm_eo;
303 realloc_strcat(result, name);
306 fprintf(stderr, "%s >> %s\n", path, *result);
312 cleanup_substitution(struct bsdtar *bsdtar)
314 struct subst_rule *rule;
315 struct substitution *subst;
317 if ((subst = bsdtar->substitution) == NULL)
320 while ((rule = subst->first_rule) != NULL) {
321 subst->first_rule = rule->next;
327 #endif /* defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H) */