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
;
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 */
72 static struct transform
*transform_head
, *transform_tail
;
74 static struct transform
*
77 struct transform
*p
= xzalloc (sizeof *p
);
79 transform_tail
->next
= p
;
86 static struct replace_segm
*
87 add_segment (struct transform
*tf
)
89 struct replace_segm
*segm
= xmalloc (sizeof *segm
);
92 tf
->repl_tail
->next
= segm
;
101 add_literal_segment (struct transform
*tf
, char *str
, char *end
)
103 size_t len
= end
- str
;
106 struct replace_segm
*segm
= add_segment (tf
);
107 segm
->type
= segm_literal
;
108 segm
->v
.literal
.ptr
= xmalloc (len
+ 1);
109 memcpy (segm
->v
.literal
.ptr
, str
, len
);
110 segm
->v
.literal
.ptr
[len
] = 0;
111 segm
->v
.literal
.size
= len
;
116 add_char_segment (struct transform
*tf
, int chr
)
118 struct replace_segm
*segm
= add_segment (tf
);
119 segm
->type
= segm_literal
;
120 segm
->v
.literal
.ptr
= xmalloc (2);
121 segm
->v
.literal
.ptr
[0] = chr
;
122 segm
->v
.literal
.ptr
[1] = 0;
123 segm
->v
.literal
.size
= 1;
127 add_backref_segment (struct transform
*tf
, size_t ref
)
129 struct replace_segm
*segm
= add_segment (tf
);
130 segm
->type
= segm_backref
;
135 add_case_ctl_segment (struct transform
*tf
, enum case_ctl_type ctl
)
137 struct replace_segm
*segm
= add_segment (tf
);
138 segm
->type
= segm_case_ctl
;
143 parse_transform_expr (const char *expr
)
147 char *str
, *beg
, *cur
;
150 struct transform
*tf
= new_transform ();
153 USAGE_ERROR ((0, 0, _("Invalid transform expression")));
157 /* Scan regular expression */
158 for (i
= 2; expr
[i
] && expr
[i
] != delim
; i
++)
159 if (expr
[i
] == '\\' && expr
[i
+1])
162 if (expr
[i
] != delim
)
163 USAGE_ERROR ((0, 0, _("Invalid transform expression")));
165 /* Scan replacement expression */
166 for (j
= i
+ 1; expr
[j
] && expr
[j
] != delim
; j
++)
167 if (expr
[j
] == '\\' && expr
[j
+1])
170 if (expr
[j
] != delim
)
171 USAGE_ERROR ((0, 0, _("Invalid transform expression")));
174 tf
->transform_type
= transform_first
;
175 for (p
= expr
+ j
+ 1; *p
&& *p
!= ';'; p
++)
179 tf
->transform_type
= transform_global
;
187 cflags
|= REG_EXTENDED
;
190 case '0': case '1': case '2': case '3': case '4':
191 case '5': case '6': case '7': case '8': case '9':
192 tf
->match_number
= strtoul (p
, (char**) &p
, 0);
197 USAGE_ERROR ((0, 0, _("Unknown flag in transform expression: %c"),
204 /* Extract and compile regex */
205 str
= xmalloc (i
- 1);
206 memcpy (str
, expr
+ 2, i
- 2);
209 rc
= regcomp (&tf
->regex
, str
, cflags
);
214 regerror (rc
, &tf
->regex
, errbuf
, sizeof (errbuf
));
215 USAGE_ERROR ((0, 0, _("Invalid transform expression: %s"), errbuf
));
218 if (str
[0] == '^' || str
[strlen (str
) - 1] == '$')
219 tf
->transform_type
= transform_first
;
223 /* Extract and compile replacement expr */
225 str
= xmalloc (j
- i
+ 1);
226 memcpy (str
, expr
+ i
, j
- i
);
229 for (cur
= beg
= str
; *cur
;)
235 add_literal_segment (tf
, beg
, cur
);
238 case '0': case '1': case '2': case '3': case '4':
239 case '5': case '6': case '7': case '8': case '9':
240 n
= strtoul (cur
, &cur
, 10);
241 if (n
> tf
->regex
.re_nsub
)
242 USAGE_ERROR ((0, 0, _("Invalid transform replacement: back reference out of range")));
243 add_backref_segment (tf
, n
);
247 add_char_segment (tf
, '\\');
252 add_char_segment (tf
, '\a');
257 add_char_segment (tf
, '\b');
262 add_char_segment (tf
, '\f');
267 add_char_segment (tf
, '\n');
272 add_char_segment (tf
, '\r');
277 add_char_segment (tf
, '\t');
282 add_char_segment (tf
, '\v');
287 add_char_segment (tf
, '&');
292 /* Turn the replacement to lowercase until a `\U' or `\E'
294 add_case_ctl_segment (tf
, ctl_locase
);
299 /* Turn the next character to lowercase, */
300 add_case_ctl_segment (tf
, ctl_locase_next
);
305 /* Turn the replacement to uppercase until a `\L' or `\E'
307 add_case_ctl_segment (tf
, ctl_upcase
);
312 /* Turn the next character to uppercase, */
313 add_case_ctl_segment (tf
, ctl_upcase_next
);
318 /* Stop case conversion started by `\L' or `\U'. */
319 add_case_ctl_segment (tf
, ctl_stop
);
329 add_literal_segment (tf
, buf
, buf
+ 2);
336 else if (*cur
== '&')
338 add_literal_segment (tf
, beg
, cur
);
339 add_backref_segment (tf
, 0);
345 add_literal_segment (tf
, beg
, cur
);
351 set_transform_expr (const char *expr
)
354 expr
= parse_transform_expr (expr
);
357 /* Run case conversion specified by CASE_CTL on array PTR of SIZE
358 characters. Returns pointer to statically allocated storage. */
360 run_case_conv (enum case_ctl_type case_ctl
, char *ptr
, size_t size
)
362 static char *case_ctl_buffer
;
363 static size_t case_ctl_bufsize
;
366 if (case_ctl_bufsize
< size
)
368 case_ctl_bufsize
= size
;
369 case_ctl_buffer
= xrealloc (case_ctl_buffer
, case_ctl_bufsize
);
371 memcpy (case_ctl_buffer
, ptr
, size
);
374 case ctl_upcase_next
:
375 case_ctl_buffer
[0] = toupper (case_ctl_buffer
[0]);
378 case ctl_locase_next
:
379 case_ctl_buffer
[0] = tolower (case_ctl_buffer
[0]);
383 for (p
= case_ctl_buffer
; p
< case_ctl_buffer
+ size
; p
++)
388 for (p
= case_ctl_buffer
; p
< case_ctl_buffer
+ size
; p
++)
395 return case_ctl_buffer
;
399 static struct obstack stk
;
400 static bool stk_init
;
403 _single_transform_name_to_obstack (struct transform
*tf
, char *input
)
408 enum case_ctl_type case_ctl
= ctl_stop
, /* Current case conversion op */
409 save_ctl
= ctl_stop
; /* Saved case_ctl for \u and \l */
411 /* Reset case conversion after a single-char operation */
412 #define CASE_CTL_RESET() if (case_ctl == ctl_upcase_next \
413 || case_ctl == ctl_locase_next) \
415 case_ctl = save_ctl; \
416 save_ctl = ctl_stop; \
419 rmp
= xmalloc ((tf
->regex
.re_nsub
+ 1) * sizeof (*rmp
));
426 rc
= regexec (&tf
->regex
, input
, tf
->regex
.re_nsub
+ 1, rmp
, 0);
430 struct replace_segm
*segm
;
435 obstack_grow (&stk
, input
, rmp
[0].rm_so
);
438 if (tf
->match_number
&& nmatches
< tf
->match_number
)
440 obstack_grow (&stk
, input
, disp
);
445 for (segm
= tf
->repl_head
; segm
; segm
= segm
->next
)
449 case segm_literal
: /* Literal segment */
450 if (case_ctl
== ctl_stop
)
451 ptr
= segm
->v
.literal
.ptr
;
454 ptr
= run_case_conv (case_ctl
,
456 segm
->v
.literal
.size
);
459 obstack_grow (&stk
, ptr
, segm
->v
.literal
.size
);
462 case segm_backref
: /* Back-reference segment */
463 if (rmp
[segm
->v
.ref
].rm_so
!= -1
464 && rmp
[segm
->v
.ref
].rm_eo
!= -1)
466 size_t size
= rmp
[segm
->v
.ref
].rm_eo
467 - rmp
[segm
->v
.ref
].rm_so
;
468 ptr
= input
+ rmp
[segm
->v
.ref
].rm_so
;
469 if (case_ctl
!= ctl_stop
)
471 ptr
= run_case_conv (case_ctl
, ptr
, size
);
475 obstack_grow (&stk
, ptr
, size
);
482 case ctl_upcase_next
:
483 case ctl_locase_next
:
498 case_ctl
= segm
->v
.ctl
;
505 disp
= strlen (input
);
506 obstack_grow (&stk
, input
, disp
);
511 if (tf
->transform_type
== transform_first
)
513 obstack_grow (&stk
, input
, strlen (input
));
518 obstack_1grow (&stk
, 0);
523 _transform_name_to_obstack (char *input
, char **output
)
525 struct transform
*tf
;
533 for (tf
= transform_head
; tf
; tf
= tf
->next
)
535 _single_transform_name_to_obstack (tf
, input
);
536 input
= obstack_finish (&stk
);
539 return transform_head
!= NULL
;
543 transform_name_fp (char **pinput
, char *(*fun
)(char *, void *), void *dat
)
546 bool ret
= _transform_name_to_obstack (*pinput
, &str
);
549 assign_string (pinput
, fun
? fun (str
, dat
) : str
);
550 obstack_free (&stk
, str
);
555 assign_string (pinput
, fun (str
, dat
));
563 transform_name (char **pinput
)
565 return transform_name_fp (pinput
, NULL
, NULL
);