1 /* This file is part of GNU tar.
2 Copyright (C) 2006, 2007, 2008 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, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
28 enum replace_segm_type
30 segm_literal
, /* Literal segment */
31 segm_backref
, /* Back-reference segment */
32 segm_case_ctl
/* Case control segment (GNU extension) */
37 ctl_stop
, /* Stop case conversion */
38 ctl_upcase_next
,/* Turn the next character to uppercase */
39 ctl_locase_next
,/* Turn the next character to lowercase */
40 ctl_upcase
, /* Turn the replacement to uppercase until ctl_stop */
41 ctl_locase
/* Turn the replacement to lowercase until ctl_stop */
46 struct replace_segm
*next
;
47 enum replace_segm_type type
;
54 } literal
; /* type == segm_literal */
55 size_t ref
; /* type == segm_backref */
56 enum case_ctl_type ctl
; /* type == segm_case_ctl */
62 struct transform
*next
;
63 enum transform_type transform_type
;
65 unsigned match_number
;
67 /* Compiled replacement expression */
68 struct replace_segm
*repl_head
, *repl_tail
;
69 size_t segm_count
; /* Number of elements in the above list */
74 int transform_flags
= XFORM_ALL
;
75 static struct transform
*transform_head
, *transform_tail
;
77 static struct transform
*
80 struct transform
*p
= xzalloc (sizeof *p
);
82 transform_tail
->next
= p
;
89 static struct replace_segm
*
90 add_segment (struct transform
*tf
)
92 struct replace_segm
*segm
= xmalloc (sizeof *segm
);
95 tf
->repl_tail
->next
= segm
;
104 add_literal_segment (struct transform
*tf
, char *str
, char *end
)
106 size_t len
= end
- str
;
109 struct replace_segm
*segm
= add_segment (tf
);
110 segm
->type
= segm_literal
;
111 segm
->v
.literal
.ptr
= xmalloc (len
+ 1);
112 memcpy (segm
->v
.literal
.ptr
, str
, len
);
113 segm
->v
.literal
.ptr
[len
] = 0;
114 segm
->v
.literal
.size
= len
;
119 add_char_segment (struct transform
*tf
, int chr
)
121 struct replace_segm
*segm
= add_segment (tf
);
122 segm
->type
= segm_literal
;
123 segm
->v
.literal
.ptr
= xmalloc (2);
124 segm
->v
.literal
.ptr
[0] = chr
;
125 segm
->v
.literal
.ptr
[1] = 0;
126 segm
->v
.literal
.size
= 1;
130 add_backref_segment (struct transform
*tf
, size_t ref
)
132 struct replace_segm
*segm
= add_segment (tf
);
133 segm
->type
= segm_backref
;
138 parse_xform_flags (int *pflags
, int c
)
143 *pflags
|= XFORM_REGFILE
;
147 *pflags
&= ~XFORM_REGFILE
;
151 *pflags
|= XFORM_LINK
;
155 *pflags
&= ~XFORM_LINK
;
159 *pflags
|= XFORM_SYMLINK
;
163 *pflags
&= ~XFORM_SYMLINK
;
173 add_case_ctl_segment (struct transform
*tf
, enum case_ctl_type ctl
)
175 struct replace_segm
*segm
= add_segment (tf
);
176 segm
->type
= segm_case_ctl
;
181 parse_transform_expr (const char *expr
)
185 char *str
, *beg
, *cur
;
188 struct transform
*tf
= new_transform ();
192 if (strncmp (expr
, "flags=", 6) == 0)
195 for (expr
+= 6; *expr
; expr
++)
202 if (parse_xform_flags (&transform_flags
, *expr
))
203 USAGE_ERROR ((0, 0, _("Unknown transform flag: %c"),
208 USAGE_ERROR ((0, 0, _("Invalid transform expression")));
213 /* Scan regular expression */
214 for (i
= 2; expr
[i
] && expr
[i
] != delim
; i
++)
215 if (expr
[i
] == '\\' && expr
[i
+1])
218 if (expr
[i
] != delim
)
219 USAGE_ERROR ((0, 0, _("Invalid transform expression")));
221 /* Scan replacement expression */
222 for (j
= i
+ 1; expr
[j
] && expr
[j
] != delim
; j
++)
223 if (expr
[j
] == '\\' && expr
[j
+1])
226 if (expr
[j
] != delim
)
227 USAGE_ERROR ((0, 0, _("Invalid transform expression")));
230 tf
->transform_type
= transform_first
;
231 tf
->flags
= transform_flags
;
232 for (p
= expr
+ j
+ 1; *p
&& *p
!= ';'; p
++)
236 tf
->transform_type
= transform_global
;
244 cflags
|= REG_EXTENDED
;
247 case '0': case '1': case '2': case '3': case '4':
248 case '5': case '6': case '7': case '8': case '9':
249 tf
->match_number
= strtoul (p
, (char**) &p
, 0);
254 if (parse_xform_flags (&tf
->flags
, *p
))
255 USAGE_ERROR ((0, 0, _("Unknown flag in transform expression: %c"),
262 /* Extract and compile regex */
263 str
= xmalloc (i
- 1);
264 memcpy (str
, expr
+ 2, i
- 2);
267 rc
= regcomp (&tf
->regex
, str
, cflags
);
272 regerror (rc
, &tf
->regex
, errbuf
, sizeof (errbuf
));
273 USAGE_ERROR ((0, 0, _("Invalid transform expression: %s"), errbuf
));
276 if (str
[0] == '^' || str
[strlen (str
) - 1] == '$')
277 tf
->transform_type
= transform_first
;
281 /* Extract and compile replacement expr */
283 str
= xmalloc (j
- i
+ 1);
284 memcpy (str
, expr
+ i
, j
- i
);
287 for (cur
= beg
= str
; *cur
;)
293 add_literal_segment (tf
, beg
, cur
);
296 case '0': case '1': case '2': case '3': case '4':
297 case '5': case '6': case '7': case '8': case '9':
298 n
= strtoul (cur
, &cur
, 10);
299 if (n
> tf
->regex
.re_nsub
)
300 USAGE_ERROR ((0, 0, _("Invalid transform replacement: back reference out of range")));
301 add_backref_segment (tf
, n
);
305 add_char_segment (tf
, '\\');
310 add_char_segment (tf
, '\a');
315 add_char_segment (tf
, '\b');
320 add_char_segment (tf
, '\f');
325 add_char_segment (tf
, '\n');
330 add_char_segment (tf
, '\r');
335 add_char_segment (tf
, '\t');
340 add_char_segment (tf
, '\v');
345 add_char_segment (tf
, '&');
350 /* Turn the replacement to lowercase until a `\U' or `\E'
352 add_case_ctl_segment (tf
, ctl_locase
);
357 /* Turn the next character to lowercase, */
358 add_case_ctl_segment (tf
, ctl_locase_next
);
363 /* Turn the replacement to uppercase until a `\L' or `\E'
365 add_case_ctl_segment (tf
, ctl_upcase
);
370 /* Turn the next character to uppercase, */
371 add_case_ctl_segment (tf
, ctl_upcase_next
);
376 /* Stop case conversion started by `\L' or `\U'. */
377 add_case_ctl_segment (tf
, ctl_stop
);
387 add_literal_segment (tf
, buf
, buf
+ 2);
394 else if (*cur
== '&')
396 add_literal_segment (tf
, beg
, cur
);
397 add_backref_segment (tf
, 0);
403 add_literal_segment (tf
, beg
, cur
);
409 set_transform_expr (const char *expr
)
412 expr
= parse_transform_expr (expr
);
415 /* Run case conversion specified by CASE_CTL on array PTR of SIZE
416 characters. Returns pointer to statically allocated storage. */
418 run_case_conv (enum case_ctl_type case_ctl
, char *ptr
, size_t size
)
420 static char *case_ctl_buffer
;
421 static size_t case_ctl_bufsize
;
424 if (case_ctl_bufsize
< size
)
426 case_ctl_bufsize
= size
;
427 case_ctl_buffer
= xrealloc (case_ctl_buffer
, case_ctl_bufsize
);
429 memcpy (case_ctl_buffer
, ptr
, size
);
432 case ctl_upcase_next
:
433 case_ctl_buffer
[0] = toupper ((unsigned char) case_ctl_buffer
[0]);
436 case ctl_locase_next
:
437 case_ctl_buffer
[0] = tolower ((unsigned char) case_ctl_buffer
[0]);
441 for (p
= case_ctl_buffer
; p
< case_ctl_buffer
+ size
; p
++)
442 *p
= toupper ((unsigned char) *p
);
446 for (p
= case_ctl_buffer
; p
< case_ctl_buffer
+ size
; p
++)
447 *p
= tolower ((unsigned char) *p
);
453 return case_ctl_buffer
;
457 static struct obstack stk
;
458 static bool stk_init
;
461 _single_transform_name_to_obstack (struct transform
*tf
, char *input
)
466 enum case_ctl_type case_ctl
= ctl_stop
, /* Current case conversion op */
467 save_ctl
= ctl_stop
; /* Saved case_ctl for \u and \l */
469 /* Reset case conversion after a single-char operation */
470 #define CASE_CTL_RESET() if (case_ctl == ctl_upcase_next \
471 || case_ctl == ctl_locase_next) \
473 case_ctl = save_ctl; \
474 save_ctl = ctl_stop; \
477 rmp
= xmalloc ((tf
->regex
.re_nsub
+ 1) * sizeof (*rmp
));
484 rc
= regexec (&tf
->regex
, input
, tf
->regex
.re_nsub
+ 1, rmp
, 0);
488 struct replace_segm
*segm
;
493 obstack_grow (&stk
, input
, rmp
[0].rm_so
);
496 if (tf
->match_number
&& nmatches
< tf
->match_number
)
498 obstack_grow (&stk
, input
, disp
);
503 for (segm
= tf
->repl_head
; segm
; segm
= segm
->next
)
507 case segm_literal
: /* Literal segment */
508 if (case_ctl
== ctl_stop
)
509 ptr
= segm
->v
.literal
.ptr
;
512 ptr
= run_case_conv (case_ctl
,
514 segm
->v
.literal
.size
);
517 obstack_grow (&stk
, ptr
, segm
->v
.literal
.size
);
520 case segm_backref
: /* Back-reference segment */
521 if (rmp
[segm
->v
.ref
].rm_so
!= -1
522 && rmp
[segm
->v
.ref
].rm_eo
!= -1)
524 size_t size
= rmp
[segm
->v
.ref
].rm_eo
525 - rmp
[segm
->v
.ref
].rm_so
;
526 ptr
= input
+ rmp
[segm
->v
.ref
].rm_so
;
527 if (case_ctl
!= ctl_stop
)
529 ptr
= run_case_conv (case_ctl
, ptr
, size
);
533 obstack_grow (&stk
, ptr
, size
);
540 case ctl_upcase_next
:
541 case ctl_locase_next
:
556 case_ctl
= segm
->v
.ctl
;
563 disp
= strlen (input
);
564 obstack_grow (&stk
, input
, disp
);
569 if (tf
->transform_type
== transform_first
)
571 obstack_grow (&stk
, input
, strlen (input
));
576 obstack_1grow (&stk
, 0);
581 _transform_name_to_obstack (int flags
, char *input
, char **output
)
583 struct transform
*tf
;
584 bool alloced
= false;
592 for (tf
= transform_head
; tf
; tf
= tf
->next
)
594 if (tf
->flags
& flags
)
596 _single_transform_name_to_obstack (tf
, input
);
597 input
= obstack_finish (&stk
);
606 transform_name_fp (char **pinput
, int flags
,
607 char *(*fun
)(char *, void *), void *dat
)
610 bool ret
= _transform_name_to_obstack (flags
, *pinput
, &str
);
613 assign_string (pinput
, fun
? fun (str
, dat
) : str
);
614 obstack_free (&stk
, str
);
619 assign_string (pinput
, fun (str
, dat
));
627 transform_name (char **pinput
, int type
)
629 return transform_name_fp (pinput
, type
, NULL
, NULL
);