]>
Dogcows Code - chaz/tar/blob - src/transform.c
1 /* This file is part of GNU tar.
2 Copyright (C) 2006 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 2, 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 transform_type
= transform_none
;
30 static struct obstack stk
;
32 enum replace_segm_type
34 segm_literal
, /* Literal segment */
35 segm_backref
, /* Back-reference segment */
36 segm_case_ctl
/* Case control segment (GNU extension) */
41 ctl_stop
, /* Stop case conversion */
42 ctl_upcase_next
,/* Turn the next character to uppercase */
43 ctl_locase_next
,/* Turn the next character to lowercase */
44 ctl_upcase
, /* Turn the replacement to uppercase until ctl_stop */
45 ctl_locase
/* Turn the replacement to lowercase until ctl_stop */
50 struct replace_segm
*next
;
51 enum replace_segm_type type
;
58 } literal
; /* type == segm_literal */
59 size_t ref
; /* type == segm_backref */
60 enum case_ctl_type ctl
; /* type == segm_case_ctl */
64 /* Compiled replacement expression */
65 static struct replace_segm
*repl_head
, *repl_tail
;
66 static segm_count
; /* Number of elements in the above list */
68 static struct replace_segm
*
71 struct replace_segm
*segm
= xmalloc (sizeof *segm
);
74 repl_tail
->next
= segm
;
83 add_literal_segment (char *str
, char *end
)
85 size_t len
= end
- str
;
88 struct replace_segm
*segm
= add_segment ();
89 segm
->type
= segm_literal
;
90 segm
->v
.literal
.ptr
= xmalloc (len
+ 1);
91 memcpy (segm
->v
.literal
.ptr
, str
, len
);
92 segm
->v
.literal
.ptr
[len
] = 0;
93 segm
->v
.literal
.size
= len
;
98 add_char_segment (int chr
)
100 struct replace_segm
*segm
= add_segment ();
101 segm
->type
= segm_literal
;
102 segm
->v
.literal
.ptr
= xmalloc (2);
103 segm
->v
.literal
.ptr
[0] = chr
;
104 segm
->v
.literal
.ptr
[1] = 0;
105 segm
->v
.literal
.size
= 2;
109 add_backref_segment (size_t ref
)
111 struct replace_segm
*segm
= add_segment ();
112 segm
->type
= segm_backref
;
117 add_case_ctl_segment (enum case_ctl_type ctl
)
119 struct replace_segm
*segm
= add_segment ();
120 segm
->type
= segm_case_ctl
;
125 set_transform_expr (const char *expr
)
129 char *str
, *beg
, *cur
;
133 if (transform_type
== transform_none
)
137 /* Redefinition of the transform expression */
142 USAGE_ERROR ((0, 0, _("Invalid transform expression")));
146 /* Scan regular expression */
147 for (i
= 2; expr
[i
] && expr
[i
] != delim
; i
++)
148 if (expr
[i
] == '\\' && expr
[i
+1])
151 if (expr
[i
] != delim
)
152 USAGE_ERROR ((0, 0, _("Invalid transform expression")));
154 /* Scan replacement expression */
155 for (j
= i
+ 1; expr
[j
] && expr
[j
] != delim
; j
++)
156 if (expr
[j
] == '\\' && expr
[j
+1])
159 if (expr
[j
] != delim
)
160 USAGE_ERROR ((0, 0, _("Invalid transform expression")));
163 transform_type
= transform_first
;
164 for (p
= expr
+ j
+ 1; *p
; p
++)
168 transform_type
= transform_global
;
176 cflags
|= REG_EXTENDED
;
180 USAGE_ERROR ((0, 0, _("Unknown flag in transform expression")));
183 /* Extract and compile regex */
184 str
= xmalloc (i
- 1);
185 memcpy (str
, expr
+ 2, i
- 2);
188 rc
= regcomp (®ex
, str
, cflags
);
193 regerror (rc
, ®ex
, errbuf
, sizeof (errbuf
));
194 USAGE_ERROR ((0, 0, _("Invalid transform expression: %s"), errbuf
));
197 if (str
[0] == '^' || str
[strlen (str
) - 1] == '$')
198 transform_type
= transform_first
;
202 /* Extract and compile replacement expr */
204 str
= xmalloc (j
- i
+ 1);
205 memcpy (str
, expr
+ i
, j
- i
);
208 for (cur
= beg
= str
; *cur
;)
214 add_literal_segment (beg
, cur
);
217 case '0': case '1': case '2': case '3': case '4':
218 case '5': case '6': case '7': case '8': case '9':
219 n
= strtoul (cur
, &cur
, 10);
220 if (n
> regex
.re_nsub
)
221 USAGE_ERROR ((0, 0, _("Invalid transform replacement: back reference out of range")));
222 add_backref_segment (n
);
226 add_char_segment ('\\');
231 add_char_segment ('\a');
236 add_char_segment ('\b');
241 add_char_segment ('\f');
246 add_char_segment ('\n');
251 add_char_segment ('\r');
256 add_char_segment ('\t');
261 add_char_segment ('\v');
266 add_char_segment ('&');
271 /* Turn the replacement to lowercase until a `\U' or `\E'
273 add_case_ctl_segment (ctl_locase
);
278 /* Turn the next character to lowercase, */
279 add_case_ctl_segment (ctl_locase_next
);
284 /* Turn the replacement to uppercase until a `\L' or `\E'
286 add_case_ctl_segment (ctl_upcase
);
291 /* Turn the next character to uppercase, */
292 add_case_ctl_segment (ctl_upcase_next
);
297 /* Stop case conversion started by `\L' or `\U'. */
298 add_case_ctl_segment (ctl_stop
);
308 add_literal_segment (buf
, buf
+ 2);
315 else if (*cur
== '&')
317 add_literal_segment (beg
, cur
);
318 add_backref_segment (0);
324 add_literal_segment (beg
, cur
);
328 /* Run case conversion specified by CASE_CTL on array PTR of SIZE
329 characters. Returns pointer to statically allocated storage. */
331 run_case_conv (enum case_ctl_type case_ctl
, char *ptr
, size_t size
)
333 static char *case_ctl_buffer
;
334 static size_t case_ctl_bufsize
;
337 if (case_ctl_bufsize
< size
)
339 case_ctl_bufsize
= size
;
340 case_ctl_buffer
= xrealloc (case_ctl_buffer
, case_ctl_bufsize
);
342 memcpy (case_ctl_buffer
, ptr
, size
);
345 case ctl_upcase_next
:
346 case_ctl_buffer
[0] = toupper (case_ctl_buffer
[0]);
349 case ctl_locase_next
:
350 case_ctl_buffer
[0] = tolower (case_ctl_buffer
[0]);
354 for (p
= case_ctl_buffer
; p
< case_ctl_buffer
+ size
; p
++)
359 for (p
= case_ctl_buffer
; p
< case_ctl_buffer
+ size
; p
++)
366 return case_ctl_buffer
;
370 _transform_name_to_obstack (char *input
)
375 enum case_ctl_type case_ctl
= ctl_stop
, /* Current case conversion op */
376 save_ctl
= ctl_stop
; /* Saved case_ctl for \u and \l */
378 /* Reset case conversion after a single-char operation */
379 #define CASE_CTL_RESET() if (case_ctl == ctl_upcase_next \
380 || case_ctl == ctl_locase_next) \
382 case_ctl = save_ctl; \
383 save_ctl = ctl_stop; \
386 if (transform_type
== transform_none
)
389 rmp
= xmalloc ((regex
.re_nsub
+ 1) * sizeof (*rmp
));
396 rc
= regexec (®ex
, input
, regex
.re_nsub
+ 1, rmp
, 0);
400 struct replace_segm
*segm
;
405 obstack_grow (&stk
, input
, rmp
[0].rm_so
);
407 for (segm
= repl_head
; segm
; segm
= segm
->next
)
411 case segm_literal
: /* Literal segment */
412 if (case_ctl
== ctl_stop
)
413 ptr
= segm
->v
.literal
.ptr
;
416 ptr
= run_case_conv (case_ctl
,
418 segm
->v
.literal
.size
);
421 obstack_grow (&stk
, ptr
, segm
->v
.literal
.size
);
424 case segm_backref
: /* Back-reference segment */
425 if (rmp
[segm
->v
.ref
].rm_so
!= -1
426 && rmp
[segm
->v
.ref
].rm_eo
!= -1)
428 size_t size
= rmp
[segm
->v
.ref
].rm_eo
429 - rmp
[segm
->v
.ref
].rm_so
;
430 ptr
= input
+ rmp
[segm
->v
.ref
].rm_so
;
431 if (case_ctl
!= ctl_stop
)
433 ptr
= run_case_conv (case_ctl
, ptr
, size
);
437 obstack_grow (&stk
, ptr
, size
);
444 case ctl_upcase_next
:
445 case ctl_locase_next
:
460 case_ctl
= segm
->v
.ctl
;
467 disp
= strlen (input
);
468 obstack_grow (&stk
, input
, disp
);
473 if (transform_type
== transform_first
)
475 obstack_grow (&stk
, input
, strlen (input
));
480 obstack_1grow (&stk
, 0);
486 transform_name_fp (char **pinput
, char *(*fun
)(char *))
489 bool ret
= _transform_name_to_obstack (*pinput
);
492 str
= obstack_finish (&stk
);
493 assign_string (pinput
, fun
? fun (str
) : str
);
494 obstack_free (&stk
, str
);
500 assign_string (pinput
, fun (str
));
508 transform_name (char **pinput
)
510 return transform_name_fp (pinput
, NULL
);
515 read_and_transform_loop ()
518 while (fgets (buf
, sizeof buf
, stdin
))
520 char *p
= buf
+ strlen (buf
);
523 if (transform_name (buf
, &p
))
524 printf ("=> %s\n", p
);
This page took 0.059855 seconds and 5 git commands to generate.