1 /* $NetBSD: regsub.c,v 1.3 2016/02/29 22:10:13 aymeric Exp $ */ 2 3 /*- 4 * Copyright (c) 2015 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 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 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 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 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 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 #include <sys/cdefs.h> 32 #if defined(LIBC_SCCS) && !defined(lint) 33 __RCSID("$NetBSD: regsub.c,v 1.3 2016/02/29 22:10:13 aymeric Exp $"); 34 #endif 35 36 #include <sys/param.h> 37 #include <ctype.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <regex.h> 41 42 struct str { 43 char *s_ptr; 44 size_t s_max; 45 size_t s_len; 46 int s_fixed; 47 }; 48 49 #define REINCR 64 50 51 static int 52 addspace(struct str *s, size_t len) 53 { 54 void *v; 55 56 if (s->s_max - s->s_len > len) 57 return 0; 58 59 if (s->s_fixed) 60 return -1; 61 62 s->s_max += len + REINCR; 63 64 v = realloc(s->s_ptr, s->s_max); 65 if (v == NULL) 66 return -1; 67 s->s_ptr = v; 68 69 return 0; 70 } 71 72 static void 73 addchar(struct str *s, int c) 74 { 75 if (addspace(s, 1) == -1) 76 s->s_len++; 77 else 78 s->s_ptr[s->s_len++] = c; 79 if (c == 0) { 80 --s->s_len; 81 s->s_ptr[s->s_max - 1] = c; 82 } 83 } 84 85 static void 86 addnstr(struct str *s, const char *buf, size_t len) 87 { 88 if (addspace(s, len) != -1) 89 memcpy(s->s_ptr + s->s_len, buf, len); 90 s->s_len += len; 91 } 92 93 static int 94 initstr(struct str *s, char *buf, size_t len) 95 { 96 s->s_max = len; 97 s->s_ptr = buf == NULL ? malloc(len) : buf; 98 s->s_fixed = buf != NULL; 99 s->s_len = 0; 100 return s->s_ptr == NULL ? -1 : 0; 101 } 102 103 static ssize_t 104 regsub1(char **buf, size_t len, const char *sub, 105 const regmatch_t *rm, const char *str) 106 { 107 ssize_t i; 108 char c; 109 struct str s; 110 111 if (initstr(&s, *buf, len) == -1) 112 return -1; 113 114 while ((c = *sub++) != '\0') { 115 116 switch (c) { 117 case '&': 118 i = 0; 119 break; 120 case '\\': 121 if (isdigit((unsigned char)*sub)) 122 i = *sub++ - '0'; 123 else 124 i = -1; 125 break; 126 default: 127 i = -1; 128 break; 129 } 130 131 if (i == -1) { 132 if (c == '\\' && (*sub == '\\' || *sub == '&')) 133 c = *sub++; 134 addchar(&s, c); 135 } else if (rm[i].rm_so != -1 && rm[i].rm_eo != -1) { 136 size_t l = (size_t)(rm[i].rm_eo - rm[i].rm_so); 137 addnstr(&s, str + rm[i].rm_so, l); 138 } 139 } 140 141 addchar(&s, '\0'); 142 if (!s.s_fixed) { 143 if (s.s_len >= s.s_max) { 144 free(s.s_ptr); 145 return -1; 146 } 147 *buf = s.s_ptr; 148 } 149 return s.s_len; 150 } 151 152 ssize_t 153 regnsub(char *buf, size_t len, const char *sub, const regmatch_t *rm, 154 const char *str) 155 { 156 return regsub1(&buf, len, sub, rm, str); 157 } 158 159 ssize_t 160 regasub(char **buf, const char *sub, const regmatch_t *rm, const char *str) 161 { 162 *buf = NULL; 163 return regsub1(buf, REINCR, sub, rm, str); 164 } 165