1 /* This file is part of GNU tar.
2 Copyright 2006-2008, 2013-2014 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
);
383 add_char_segment (tf
, delim
);
392 add_literal_segment (tf
, buf
, buf
+ 2);
399 else if (*cur
== '&')
401 add_literal_segment (tf
, beg
, cur
);
402 add_backref_segment (tf
, 0);
408 add_literal_segment (tf
, beg
, cur
);
414 set_transform_expr (const char *expr
)
417 expr
= parse_transform_expr (expr
);
420 /* Run case conversion specified by CASE_CTL on array PTR of SIZE
421 characters. Returns pointer to statically allocated storage. */
423 run_case_conv (enum case_ctl_type case_ctl
, char *ptr
, size_t size
)
425 static char *case_ctl_buffer
;
426 static size_t case_ctl_bufsize
;
429 if (case_ctl_bufsize
< size
)
431 case_ctl_bufsize
= size
;
432 case_ctl_buffer
= xrealloc (case_ctl_buffer
, case_ctl_bufsize
);
434 memcpy (case_ctl_buffer
, ptr
, size
);
437 case ctl_upcase_next
:
438 case_ctl_buffer
[0] = toupper ((unsigned char) case_ctl_buffer
[0]);
441 case ctl_locase_next
:
442 case_ctl_buffer
[0] = tolower ((unsigned char) case_ctl_buffer
[0]);
446 for (p
= case_ctl_buffer
; p
< case_ctl_buffer
+ size
; p
++)
447 *p
= toupper ((unsigned char) *p
);
451 for (p
= case_ctl_buffer
; p
< case_ctl_buffer
+ size
; p
++)
452 *p
= tolower ((unsigned char) *p
);
458 return case_ctl_buffer
;
462 static struct obstack stk
;
463 static bool stk_init
;
466 _single_transform_name_to_obstack (struct transform
*tf
, char *input
)
471 enum case_ctl_type case_ctl
= ctl_stop
, /* Current case conversion op */
472 save_ctl
= ctl_stop
; /* Saved case_ctl for \u and \l */
474 /* Reset case conversion after a single-char operation */
475 #define CASE_CTL_RESET() if (case_ctl == ctl_upcase_next \
476 || case_ctl == ctl_locase_next) \
478 case_ctl = save_ctl; \
479 save_ctl = ctl_stop; \
482 rmp
= xmalloc ((tf
->regex
.re_nsub
+ 1) * sizeof (*rmp
));
489 rc
= regexec (&tf
->regex
, input
, tf
->regex
.re_nsub
+ 1, rmp
, 0);
493 struct replace_segm
*segm
;
498 obstack_grow (&stk
, input
, rmp
[0].rm_so
);
501 if (tf
->match_number
&& nmatches
< tf
->match_number
)
503 obstack_grow (&stk
, input
, disp
);
508 for (segm
= tf
->repl_head
; segm
; segm
= segm
->next
)
512 case segm_literal
: /* Literal segment */
513 if (case_ctl
== ctl_stop
)
514 ptr
= segm
->v
.literal
.ptr
;
517 ptr
= run_case_conv (case_ctl
,
519 segm
->v
.literal
.size
);
522 obstack_grow (&stk
, ptr
, segm
->v
.literal
.size
);
525 case segm_backref
: /* Back-reference segment */
526 if (rmp
[segm
->v
.ref
].rm_so
!= -1
527 && rmp
[segm
->v
.ref
].rm_eo
!= -1)
529 size_t size
= rmp
[segm
->v
.ref
].rm_eo
530 - rmp
[segm
->v
.ref
].rm_so
;
531 ptr
= input
+ rmp
[segm
->v
.ref
].rm_so
;
532 if (case_ctl
!= ctl_stop
)
534 ptr
= run_case_conv (case_ctl
, ptr
, size
);
538 obstack_grow (&stk
, ptr
, size
);
545 case ctl_upcase_next
:
546 case ctl_locase_next
:
561 case_ctl
= segm
->v
.ctl
;
568 disp
= strlen (input
);
569 obstack_grow (&stk
, input
, disp
);
574 if (tf
->transform_type
== transform_first
)
576 obstack_grow (&stk
, input
, strlen (input
));
581 obstack_1grow (&stk
, 0);
586 _transform_name_to_obstack (int flags
, char *input
, char **output
)
588 struct transform
*tf
;
589 bool alloced
= false;
597 for (tf
= transform_head
; tf
; tf
= tf
->next
)
599 if (tf
->flags
& flags
)
601 _single_transform_name_to_obstack (tf
, input
);
602 input
= obstack_finish (&stk
);
611 transform_name_fp (char **pinput
, int flags
,
612 char *(*fun
)(char *, void *), void *dat
)
615 bool ret
= _transform_name_to_obstack (flags
, *pinput
, &str
);
618 assign_string (pinput
, fun
? fun (str
, dat
) : str
);
619 obstack_free (&stk
, str
);
624 assign_string (pinput
, fun (str
, dat
));
632 transform_name (char **pinput
, int type
)
634 return transform_name_fp (pinput
, type
, NULL
, NULL
);
638 transform_program_p (void)
640 return transform_head
!= NULL
;