| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: regsub.c,v 1.2 2016/01/16 19:28:36 christos Exp $ */ | | 1 | /* $NetBSD: regsub.c,v 1.3 2016/01/20 15:13:33 christos Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2015 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2015 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation | | 7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Christos Zoulas. | | 8 | * by Christos Zoulas. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
| @@ -18,57 +18,53 @@ | | | @@ -18,57 +18,53 @@ |
18 | * | | 18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | | 19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. | | 29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ | | 30 | */ |
31 | #include <sys/cdefs.h> | | | |
32 | #ifdef __RCSID | | | |
33 | __RCSID("$NetBSD: regsub.c,v 1.2 2016/01/16 19:28:36 christos Exp $"); | | | |
34 | #endif | | | |
35 | | | | |
36 | #include <sys/param.h> | | 31 | #include <sys/param.h> |
37 | | | | |
38 | #include <ctype.h> | | 32 | #include <ctype.h> |
39 | #include <stdlib.h> | | 33 | #include <stdlib.h> |
40 | #include <string.h> | | 34 | #include <string.h> |
41 | #include <regex.h> | | 35 | #include <regex.h> |
42 | | | 36 | |
43 | struct str { | | 37 | struct str { |
44 | char *s_ptr; | | 38 | char *s_ptr; |
45 | size_t s_max; | | 39 | size_t s_max; |
46 | size_t s_len; | | 40 | size_t s_len; |
47 | int s_fixed; | | 41 | int s_fixed; |
48 | }; | | 42 | }; |
49 | | | 43 | |
| | | 44 | #define REINCR 64 |
| | | 45 | |
50 | static int | | 46 | static int |
51 | addspace(struct str *s, size_t len) | | 47 | addspace(struct str *s, size_t len) |
52 | { | | 48 | { |
53 | void *v; | | 49 | void *v; |
54 | | | 50 | |
55 | if (s->s_max - s->s_len > len) | | 51 | if (s->s_max - s->s_len > len) |
56 | return 0; | | 52 | return 0; |
57 | | | 53 | |
58 | if (s->s_fixed) | | 54 | if (s->s_fixed) |
59 | return -1; | | 55 | return -1; |
60 | | | 56 | |
61 | s->s_max += MAX(len, 64); | | 57 | s->s_max += len + REINCR; |
62 | | | 58 | |
63 | v = realloc(s->s_ptr, s->s_max); | | 59 | v = realloc(s->s_ptr, s->s_max); |
64 | if (v == NULL) | | 60 | if (v == NULL) |
65 | return -1; | | 61 | return -1; |
66 | s->s_ptr = (char *)v; | | 62 | s->s_ptr = (char *)v; |
67 | | | 63 | |
68 | return 0; | | 64 | return 0; |
69 | } | | 65 | } |
70 | | | 66 | |
71 | static void | | 67 | static void |
72 | addchar(struct str *s, int c) | | 68 | addchar(struct str *s, int c) |
73 | { | | 69 | { |
74 | if (addspace(s, 1) == -1) | | 70 | if (addspace(s, 1) == -1) |
| @@ -83,37 +79,37 @@ addchar(struct str *s, int c) | | | @@ -83,37 +79,37 @@ addchar(struct str *s, int c) |
83 | | | 79 | |
84 | static void | | 80 | static void |
85 | addnstr(struct str *s, const char *buf, size_t len) | | 81 | addnstr(struct str *s, const char *buf, size_t len) |
86 | { | | 82 | { |
87 | if (addspace(s, len) != -1) | | 83 | if (addspace(s, len) != -1) |
88 | memcpy(s->s_ptr + s->s_len, buf, len); | | 84 | memcpy(s->s_ptr + s->s_len, buf, len); |
89 | s->s_len += len; | | 85 | s->s_len += len; |
90 | } | | 86 | } |
91 | | | 87 | |
92 | static int | | 88 | static int |
93 | initstr(struct str *s, char *buf, size_t len) | | 89 | initstr(struct str *s, char *buf, size_t len) |
94 | { | | 90 | { |
95 | s->s_max = len; | | 91 | s->s_max = len; |
96 | s->s_ptr = buf == NULL ? (char *)malloc(len) : buf; | | 92 | s->s_ptr = (char *)(buf == NULL ? malloc(len) : buf); |
97 | s->s_fixed = buf != NULL; | | 93 | s->s_fixed = buf != NULL; |
98 | s->s_len = 0; | | 94 | s->s_len = 0; |
99 | return s->s_ptr == NULL ? -1 : 0; | | 95 | return s->s_ptr == NULL ? -1 : 0; |
100 | } | | 96 | } |
101 | | | 97 | |
102 | static ssize_t | | 98 | static ssize_t |
103 | regsub1(char **buf, size_t len, const char *sub, | | 99 | regsub1(char **buf, size_t len, const char *sub, |
104 | const regmatch_t *rm, const char *str) | | 100 | const regmatch_t *rm, const char *str) |
105 | { | | 101 | { |
106 | ssize_t i; | | 102 | ssize_t i; |
107 | char c; | | 103 | char c; |
108 | struct str s; | | 104 | struct str s; |
109 | | | 105 | |
110 | if (initstr(&s, *buf, len) == -1) | | 106 | if (initstr(&s, *buf, len) == -1) |
111 | return -1; | | 107 | return -1; |
112 | | | 108 | |
113 | while ((c = *sub++) != '\0') { | | 109 | while ((c = *sub++) != '\0') { |
114 | | | 110 | |
115 | switch (c) { | | 111 | switch (c) { |
116 | case '&': | | 112 | case '&': |
117 | i = 0; | | 113 | i = 0; |
118 | break; | | 114 | break; |
119 | case '\\': | | 115 | case '\\': |
| @@ -139,25 +135,25 @@ regsub1(char **buf, size_t len, const ch | | | @@ -139,25 +135,25 @@ regsub1(char **buf, size_t len, const ch |
139 | | | 135 | |
140 | addchar(&s, '\0'); | | 136 | addchar(&s, '\0'); |
141 | if (!s.s_fixed) { | | 137 | if (!s.s_fixed) { |
142 | if (s.s_len >= s.s_max) { | | 138 | if (s.s_len >= s.s_max) { |
143 | free(s.s_ptr); | | 139 | free(s.s_ptr); |
144 | return -1; | | 140 | return -1; |
145 | } | | 141 | } |
146 | *buf = s.s_ptr; | | 142 | *buf = s.s_ptr; |
147 | } | | 143 | } |
148 | return s.s_len; | | 144 | return s.s_len; |
149 | } | | 145 | } |
150 | | | 146 | |
151 | ssize_t | | 147 | ssize_t |
152 | regsub(char *buf, size_t len, const char *sub, const regmatch_t *rm, | | 148 | regnsub(char *buf, size_t len, const char *sub, const regmatch_t *rm, |
153 | const char *str) | | 149 | const char *str) |
154 | { | | 150 | { |
155 | return regsub1(&buf, len, sub, rm, str); | | 151 | return regsub1(&buf, len, sub, rm, str); |
156 | } | | 152 | } |
157 | | | 153 | |
158 | ssize_t | | 154 | ssize_t |
159 | aregsub(char **buf, const char *sub, const regmatch_t *rm, const char *str) | | 155 | regasub(char **buf, const char *sub, const regmatch_t *rm, const char *str) |
160 | { | | 156 | { |
161 | *buf = NULL; | | 157 | *buf = NULL; |
162 | return regsub1(buf, 64, sub, rm, str); | | 158 | return regsub1(buf, REINCR, sub, rm, str); |
163 | } | | 159 | } |