1 /* This file is part of GNU tar.
2 Copyright 2006-2008, 2013 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 3, or (at your option) any later
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
12 Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program. If not, see <http://www.gnu.org/licenses/>. */
27 enum replace_segm_type
29 segm_literal
, /* Literal segment */
30 segm_backref
, /* Back-reference segment */
31 segm_case_ctl
/* Case control segment (GNU extension) */
36 ctl_stop
, /* Stop case conversion */
37 ctl_upcase_next
,/* Turn the next character to uppercase */
38 ctl_locase_next
,/* Turn the next character to lowercase */
39 ctl_upcase
, /* Turn the replacement to uppercase until ctl_stop */
40 ctl_locase
/* Turn the replacement to lowercase until ctl_stop */
45 struct replace_segm
*next
;
46 enum replace_segm_type type
;
53 } literal
; /* type == segm_literal */
54 size_t ref
; /* type == segm_backref */
55 enum case_ctl_type ctl
; /* type == segm_case_ctl */
61 struct transform
*next
;
62 enum transform_type transform_type
;
64 unsigned match_number
;
66 /* Compiled replacement expression */
67 struct replace_segm
*repl_head
, *repl_tail
;
68 size_t segm_count
; /* Number of elements in the above list */
73 static int transform_flags
= XFORM_ALL
;
74 static struct transform
*transform_head
, *transform_tail
;
76 static struct transform
*
79 struct transform
*p
= xzalloc (sizeof *p
);
81 transform_tail
->next
= p
;
88 static struct replace_segm
*
89 add_segment (struct transform
*tf
)
91 struct replace_segm
*segm
= xmalloc (sizeof *segm
);
94 tf
->repl_tail
->next
= segm
;
103 add_literal_segment (struct transform
*tf
, char *str
, char *end
)
105 size_t len
= end
- str
;
108 struct replace_segm
*segm
= add_segment (tf
);
109 segm
->type
= segm_literal
;
110 segm
->v
.literal
.ptr
= xmalloc (len
+ 1);
111 memcpy (segm
->v
.literal
.ptr
, str
, len
);
112 segm
->v
.literal
.ptr
[len
] = 0;
113 segm
->v
.literal
.size
= len
;
118 add_char_segment (struct transform
*tf
, int chr
)
120 struct replace_segm
*segm
= add_segment (tf
);
121 segm
->type
= segm_literal
;
122 segm
->v
.literal
.ptr
= xmalloc (2);
123 segm
->v
.literal
.ptr
[0] = chr
;
124 segm
->v
.literal
.ptr
[1] = 0;
125 segm
->v
.literal
.size
= 1;
129 add_backref_segment (struct transform
*tf
, size_t ref
)
131 struct replace_segm
*segm
= add_segment (tf
);
132 segm
->type
= segm_backref
;
137 parse_xform_flags (int *pflags
, int c
)
142 *pflags
|= XFORM_REGFILE
;
146 *pflags
&= ~XFORM_REGFILE
;
150 *pflags
|= XFORM_LINK
;
154 *pflags
&= ~XFORM_LINK
;
158 *pflags
|= XFORM_SYMLINK
;
162 *pflags
&= ~XFORM_SYMLINK
;
172 add_case_ctl_segment (struct transform
*tf
, enum case_ctl_type ctl
)
174 struct replace_segm
*segm
= add_segment (tf
);
175 segm
->type
= segm_case_ctl
;
180 parse_transform_expr (const char *expr
)
184 char *str
, *beg
, *cur
;
187 struct transform
*tf
= new_transform ();
191 if (strncmp (expr
, "flags=", 6) == 0)
194 for (expr
+= 6; *expr
; expr
++)
201 if (parse_xform_flags (&transform_flags
, *expr
))
202 USAGE_ERROR ((0, 0, _("Unknown transform flag: %c"),
207 USAGE_ERROR ((0, 0, _("Invalid transform expression")));
212 /* Scan regular expression */
213 for (i
= 2; expr
[i
] && expr
[i
] != delim
; i
++)
214 if (expr
[i
] == '\\' && expr
[i
+1])
217 if (expr
[i
] != delim
)
218 USAGE_ERROR ((0, 0, _("Invalid transform expression")));
220 /* Scan replacement expression */
221 for (j
= i
+ 1; expr
[j
] && expr
[j
] != delim
; j
++)
222 if (expr
[j
] == '\\' && expr
[j
+1])
225 if (expr
[j
] != delim
)
226 USAGE_ERROR ((0, 0, _("Invalid transform expression")));
229 tf
->transform_type
= transform_first
;
230 tf
->flags
= transform_flags
;
231 for (p
= expr
+ j
+ 1; *p
&& *p
!= ';'; p
++)
235 tf
->transform_type
= transform_global
;
243 cflags
|= REG_EXTENDED
;
246 case '0': case '1': case '2': case '3': case '4':
247 case '5': case '6': case '7': case '8': case '9':
248 tf
->match_number
= strtoul (p
, (char**) &p
, 0);
253 if (parse_xform_flags (&tf
->flags
, *p
))
254 USAGE_ERROR ((0, 0, _("Unknown flag in transform expression: %c"),
261 /* Extract and compile regex */
262 str
= xmalloc (i
- 1);
263 memcpy (str
, expr
+ 2, i
- 2);
266 rc
= regcomp (&tf
->regex
, str
, cflags
);
271 regerror (rc
, &tf
->regex
, errbuf
, sizeof (errbuf
));
272 USAGE_ERROR ((0, 0, _("Invalid transform expression: %s"), errbuf
));
275 if (str
[0] == '^' || str
[strlen (str
) - 1] == '$')
276 tf
->transform_type
= transform_first
;
280 /* Extract and compile replacement expr */
282 str
= xmalloc (j
- i
+ 1);
283 memcpy (str
, expr
+ i
, j
- i
);
286 for (cur
= beg
= str
; *cur
;)
292 add_literal_segment (tf
, beg
, cur
);
295 case '0': case '1': case '2': case '3': case '4':
296 case '5': case '6': case '7': case '8': case '9':
297 n
= strtoul (cur
, &cur
, 10);
298 if (n
> tf
->regex
.re_nsub
)
299 USAGE_ERROR ((0, 0, _("Invalid transform replacement: back reference out of range")));
300 add_backref_segment (tf
, n
);
304 add_char_segment (tf
, '\\');
309 add_char_segment (tf
, '\a');
314 add_char_segment (tf
, '\b');
319 add_char_segment (tf
, '\f');
324 add_char_segment (tf
, '\n');
329 add_char_segment (tf
, '\r');
334 add_char_segment (tf
, '\t');
339 add_char_segment (tf
, '\v');
344 add_char_segment (tf
, '&');
349 /* Turn the replacement to lowercase until a '\U' or '\E'
351 add_case_ctl_segment (tf
, ctl_locase
);
356 /* Turn the next character to lowercase, */
357 add_case_ctl_segment (tf
, ctl_locase_next
);
362 /* Turn the replacement to uppercase until a '\L' or '\E'
364 add_case_ctl_segment (tf
, ctl_upcase
);
369 /* Turn the next character to uppercase, */
370 add_case_ctl_segment (tf
, ctl_upcase_next
);
375 /* Stop case conversion started by '\L' or '\U'. */
376 add_case_ctl_segment (tf
, ctl_stop
);
386 add_literal_segment (tf
, buf
, buf
+ 2);
393 else if (*cur
== '&')
395 add_literal_segment (tf
, beg
, cur
);
396 add_backref_segment (tf
, 0);
402 add_literal_segment (tf
, beg
, cur
);
408 set_transform_expr (const char *expr
)
411 expr
= parse_transform_expr (expr
);
414 /* Run case conversion specified by CASE_CTL on array PTR of SIZE
415 characters. Returns pointer to statically allocated storage. */
417 run_case_conv (enum case_ctl_type case_ctl
, char *ptr
, size_t size
)
419 static char *case_ctl_buffer
;
420 static size_t case_ctl_bufsize
;
423 if (case_ctl_bufsize
< size
)
425 case_ctl_bufsize
= size
;
426 case_ctl_buffer
= xrealloc (case_ctl_buffer
, case_ctl_bufsize
);
428 memcpy (case_ctl_buffer
, ptr
, size
);
431 case ctl_upcase_next
:
432 case_ctl_buffer
[0] = toupper ((unsigned char) case_ctl_buffer
[0]);
435 case ctl_locase_next
:
436 case_ctl_buffer
[0] = tolower ((unsigned char) case_ctl_buffer
[0]);
440 for (p
= case_ctl_buffer
; p
< case_ctl_buffer
+ size
; p
++)
441 *p
= toupper ((unsigned char) *p
);
445 for (p
= case_ctl_buffer
; p
< case_ctl_buffer
+ size
; p
++)
446 *p
= tolower ((unsigned char) *p
);
452 return case_ctl_buffer
;
456 static struct obstack stk
;
457 static bool stk_init
;
460 _single_transform_name_to_obstack (struct transform
*tf
, char *input
)
465 enum case_ctl_type case_ctl
= ctl_stop
, /* Current case conversion op */
466 save_ctl
= ctl_stop
; /* Saved case_ctl for \u and \l */
468 /* Reset case conversion after a single-char operation */
469 #define CASE_CTL_RESET() if (case_ctl == ctl_upcase_next \
470 || case_ctl == ctl_locase_next) \
472 case_ctl = save_ctl; \
473 save_ctl = ctl_stop; \
476 rmp
= xmalloc ((tf
->regex
.re_nsub
+ 1) * sizeof (*rmp
));
483 rc
= regexec (&tf
->regex
, input
, tf
->regex
.re_nsub
+ 1, rmp
, 0);
487 struct replace_segm
*segm
;
492 obstack_grow (&stk
, input
, rmp
[0].rm_so
);
495 if (tf
->match_number
&& nmatches
< tf
->match_number
)
497 obstack_grow (&stk
, input
, disp
);
502 for (segm
= tf
->repl_head
; segm
; segm
= segm
->next
)
506 case segm_literal
: /* Literal segment */
507 if (case_ctl
== ctl_stop
)
508 ptr
= segm
->v
.literal
.ptr
;
511 ptr
= run_case_conv (case_ctl
,
513 segm
->v
.literal
.size
);
516 obstack_grow (&stk
, ptr
, segm
->v
.literal
.size
);
519 case segm_backref
: /* Back-reference segment */
520 if (rmp
[segm
->v
.ref
].rm_so
!= -1
521 && rmp
[segm
->v
.ref
].rm_eo
!= -1)
523 size_t size
= rmp
[segm
->v
.ref
].rm_eo
524 - rmp
[segm
->v
.ref
].rm_so
;
525 ptr
= input
+ rmp
[segm
->v
.ref
].rm_so
;
526 if (case_ctl
!= ctl_stop
)
528 ptr
= run_case_conv (case_ctl
, ptr
, size
);
532 obstack_grow (&stk
, ptr
, size
);
539 case ctl_upcase_next
:
540 case ctl_locase_next
:
555 case_ctl
= segm
->v
.ctl
;
562 disp
= strlen (input
);
563 obstack_grow (&stk
, input
, disp
);
568 if (tf
->transform_type
== transform_first
)
570 obstack_grow (&stk
, input
, strlen (input
));
575 obstack_1grow (&stk
, 0);
580 _transform_name_to_obstack (int flags
, char *input
, char **output
)
582 struct transform
*tf
;
583 bool alloced
= false;
591 for (tf
= transform_head
; tf
; tf
= tf
->next
)
593 if (tf
->flags
& flags
)
595 _single_transform_name_to_obstack (tf
, input
);
596 input
= obstack_finish (&stk
);
605 transform_name_fp (char **pinput
, int flags
,
606 char *(*fun
)(char *, void *), void *dat
)
609 bool ret
= _transform_name_to_obstack (flags
, *pinput
, &str
);
612 assign_string (pinput
, fun
? fun (str
, dat
) : str
);
613 obstack_free (&stk
, str
);
618 assign_string (pinput
, fun (str
, dat
));
626 transform_name (char **pinput
, int type
)
628 return transform_name_fp (pinput
, type
, NULL
, NULL
);
632 transform_program_p (void)
634 return transform_head
!= NULL
;