Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
regsub.c
1 /*
2  * regsub
3  *
4  * Copyright (c) 1986 by University of Toronto.
5  * Written by Henry Spencer. Not derived from licensed software.
6  *
7  * Permission is granted to anyone to use this software for any
8  * purpose on any computer system, and to redistribute it freely,
9  * subject to the following restrictions:
10  *
11  * 1. The author is not responsible for the consequences of use of
12  * this software, no matter how awful, even if they arise
13  * from defects in it.
14  *
15  * 2. The origin of this software must not be misrepresented, either
16  * by explicit claim or by omission.
17  *
18  * 3. Altered versions must be plainly marked as such, and must not
19  * be misrepresented as being the original software.
20  */
21 #include <stdio.h>
22 #include <string.h>
23 #include "regexp.h"
24 #include "regmagic.h"
25 
26 #ifndef CHARBITS
27 #define UCHARAT(p) ((int)*(unsigned char *)(p))
28 #else
29 #define UCHARAT(p) ((int)*(p)&CHARBITS)
30 #endif
31 
32 /*
33  - regsub - perform substitutions after a regexp match
34  */
35 void
36 hs_regsub(prog, source, dest)
37 const hs_regexp *prog;
38 const char *source;
39 char *dest;
40 {
41  register char *src;
42  register char *dst;
43  register char c;
44  register int no;
45  register int len;
46 
47  if (prog == NULL || source == NULL || dest == NULL) {
48  hs_regerror("NULL parm to regsub");
49  return;
50  }
51  if (UCHARAT(prog->program) != MAGIC) {
52  hs_regerror("damaged regexp fed to regsub");
53  return;
54  }
55 
56  src = (char *)source;
57  dst = dest;
58  while ((c = *src++) != '\0') {
59  if (c == '&')
60  no = 0;
61  else if (c == '\\' && '0' <= *src && *src <= '9')
62  no = *src++ - '0';
63  else
64  no = -1;
65  if (no < 0) { /* Ordinary character. */
66  if (c == '\\' && (*src == '\\' || *src == '&'))
67  c = *src++;
68  *dst++ = c;
69  } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
70  len = prog->endp[no] - prog->startp[no];
71  (void) strncpy(dst, prog->startp[no], len);
72  dst += len;
73  if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
74  hs_regerror("damaged match string");
75  return;
76  }
77  }
78  }
79  *dst++ = '\0';
80 }