]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - usr.bin/tar/subst.c
MFC r217226:
[FreeBSD/releng/8.2.git] / usr.bin / tar / subst.c
1 /*-
2  * Copyright (c) 2008 Joerg Sonnenberger
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
24  */
25
26 #include "bsdtar_platform.h"
27 __FBSDID("$FreeBSD$");
28
29 #if HAVE_REGEX_H
30 #include "bsdtar.h"
31 #include "err.h"
32
33 #include <errno.h>
34 #include <regex.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #ifndef REG_BASIC
39 #define REG_BASIC 0
40 #endif
41
42 struct subst_rule {
43         struct subst_rule *next;
44         regex_t re;
45         char *result;
46         unsigned int global:1, print:1, symlink:1;
47 };
48
49 struct substitution {
50         struct subst_rule *first_rule, *last_rule;
51 };
52
53 static void
54 init_substitution(struct bsdtar *bsdtar)
55 {
56         struct substitution *subst;
57
58         bsdtar->substitution = subst = malloc(sizeof(*subst));
59         if (subst == NULL)
60                 bsdtar_errc(1, errno, "Out of memory");
61         subst->first_rule = subst->last_rule = NULL;
62 }
63
64 void
65 add_substitution(struct bsdtar *bsdtar, const char *rule_text)
66 {
67         struct subst_rule *rule;
68         struct substitution *subst;
69         const char *end_pattern, *start_subst;
70         char *pattern;
71         int r;
72
73         if ((subst = bsdtar->substitution) == NULL) {
74                 init_substitution(bsdtar);
75                 subst = bsdtar->substitution;
76         }
77
78         rule = malloc(sizeof(*rule));
79         if (rule == NULL)
80                 bsdtar_errc(1, errno, "Out of memory");
81         rule->next = NULL;
82
83         if (subst->last_rule == NULL)
84                 subst->first_rule = rule;
85         else
86                 subst->last_rule->next = rule;
87         subst->last_rule = rule;
88
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");
94
95         pattern = malloc(end_pattern - rule_text);
96         if (pattern == NULL)
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';
100
101         if ((r = regcomp(&rule->re, pattern, REG_BASIC)) != 0) {
102                 char buf[80];
103                 regerror(r, &rule->re, buf, sizeof(buf));
104                 bsdtar_errc(1, 0, "Invalid regular expression: %s", buf);
105         }
106         free(pattern);
107
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");
112
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';
118
119         rule->global = 0;
120         rule->print = 0;
121         rule->symlink = 0;
122
123         while (*++end_pattern) {
124                 switch (*end_pattern) {
125                 case 'g':
126                 case 'G':
127                         rule->global = 1;
128                         break;
129                 case 'p':
130                 case 'P':
131                         rule->print = 1;
132                         break;
133                 case 's':
134                 case 'S':
135                         rule->symlink = 1;
136                         break;
137                 default:
138                         bsdtar_errc(1, 0, "Invalid replacement flag %c", *end_pattern);
139                 }
140         }
141 }
142
143 static void
144 realloc_strncat(char **str, const char *append, size_t len)
145 {
146         char *new_str;
147         size_t old_len;
148
149         if (*str == NULL)
150                 old_len = 0;
151         else
152                 old_len = strlen(*str);
153
154         new_str = malloc(old_len + len + 1);
155         if (new_str == NULL)
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';
160         free(*str);
161         *str = new_str;
162 }
163
164 static void
165 realloc_strcat(char **str, const char *append)
166 {
167         char *new_str;
168         size_t old_len;
169
170         if (*str == NULL)
171                 old_len = 0;
172         else
173                 old_len = strlen(*str);
174
175         new_str = malloc(old_len + strlen(append) + 1);
176         if (new_str == NULL)
177                 bsdtar_errc(1, errno, "Out of memory");
178         memcpy(new_str, *str, old_len);
179         strcpy(new_str + old_len, append);
180         free(*str);
181         *str = new_str;
182 }
183
184 int
185 apply_substitution(struct bsdtar *bsdtar, const char *name, char **result, int symlink_only)
186 {
187         const char *path = name;
188         regmatch_t matches[10];
189         size_t i, j;
190         struct subst_rule *rule;
191         struct substitution *subst;
192         int c, got_match, print_match;
193
194         *result = NULL;
195
196         if ((subst = bsdtar->substitution) == NULL)
197                 return 0;
198
199         got_match = 0;
200         print_match = 0;
201
202         for (rule = subst->first_rule; rule != NULL; rule = rule->next) {
203                 if (symlink_only && !rule->symlink)
204                         continue;
205                 if (regexec(&rule->re, name, 10, matches, 0))
206                         continue;
207
208                 got_match = 1;
209                 print_match |= rule->print;
210                 realloc_strncat(result, name, matches[0].rm_so);
211
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);
216                                 j = i + 1;
217                                 continue;
218                         }
219                         if (rule->result[i] != '\\')
220                                 continue;
221
222                         ++i;
223                         c = rule->result[i];
224                         switch (c) {
225                         case '~':
226                         case '\\':
227                                 realloc_strncat(result, rule->result + j, i - j - 1);
228                                 j = i;
229                                 break;
230                         case '1':
231                         case '2':
232                         case '3':
233                         case '4':
234                         case '5':
235                         case '6':
236                         case '7':
237                         case '8':
238                         case '9':
239                                 realloc_strncat(result, rule->result + j, i - j - 1);
240                                 if ((size_t)(c - '0') > (size_t)(rule->re.re_nsub)) {
241                                         free(*result);
242                                         *result = NULL;
243                                         return -1;
244                                 }
245                                 realloc_strncat(result, name + matches[c - '0'].rm_so, matches[c - '0'].rm_eo - matches[c - '0'].rm_so);
246                                 j = i + 1;
247                                 break;
248                         default:
249                                 /* Just continue; */
250                                 break;
251                         }
252
253                 }
254
255                 realloc_strcat(result, rule->result + j);
256
257                 name += matches[0].rm_eo;
258
259                 if (!rule->global)
260                         break;
261         }
262
263         if (got_match)
264                 realloc_strcat(result, name);
265
266         if (print_match)
267                 fprintf(stderr, "%s >> %s\n", path, *result);
268
269         return got_match;
270 }
271
272 void
273 cleanup_substitution(struct bsdtar *bsdtar)
274 {
275         struct subst_rule *rule;
276         struct substitution *subst;
277
278         if ((subst = bsdtar->substitution) == NULL)
279                 return;
280
281         while ((rule = subst->first_rule) != NULL) {
282                 subst->first_rule = rule->next;
283                 free(rule->result);
284                 free(rule);
285         }
286         free(subst);
287 }
288 #endif /* HAVE_REGEX_H */