1 /* $OpenBSD: regsub.c,v 1.4 2019/11/27 20:54:30 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 21 #include <regex.h> 22 #include <string.h> 23 24 #include "tmux.h" 25 26 static void 27 regsub_copy(char **buf, size_t *len, const char *text, size_t start, 28 size_t end) 29 { 30 size_t add = end - start; 31 32 *buf = xrealloc(*buf, (*len) + add + 1); 33 memcpy((*buf) + *len, text + start, add); 34 (*len) += add; 35 } 36 37 static void 38 regsub_expand(char **buf, size_t *len, const char *with, const char *text, 39 regmatch_t *m, u_int n) 40 { 41 const char *cp; 42 u_int i; 43 44 for (cp = with; *cp != '\0'; cp++) { 45 if (*cp == '\\') { 46 cp++; 47 if (*cp >= '0' && *cp <= '9') { 48 i = *cp - '0'; 49 if (i < n && m[i].rm_so != m[i].rm_eo) { 50 regsub_copy(buf, len, text, m[i].rm_so, 51 m[i].rm_eo); 52 continue; 53 } 54 } 55 } 56 *buf = xrealloc(*buf, (*len) + 2); 57 (*buf)[(*len)++] = *cp; 58 } 59 } 60 61 char * 62 regsub(const char *pattern, const char *with, const char *text, int flags) 63 { 64 regex_t r; 65 regmatch_t m[10]; 66 ssize_t start, end, last, len = 0; 67 int empty = 0; 68 char *buf = NULL; 69 70 if (*text == '\0') 71 return (xstrdup("")); 72 if (regcomp(&r, pattern, flags) != 0) 73 return (NULL); 74 75 start = 0; 76 last = 0; 77 end = strlen(text); 78 79 while (start <= end) { 80 if (regexec(&r, text + start, nitems(m), m, 0) != 0) { 81 regsub_copy(&buf, &len, text, start, end); 82 break; 83 } 84 85 /* 86 * Append any text not part of this match (from the end of the 87 * last match). 88 */ 89 regsub_copy(&buf, &len, text, last, m[0].rm_so + start); 90 91 /* 92 * If the last match was empty and this one isn't (it is either 93 * later or has matched text), expand this match. If it is 94 * empty, move on one character and try again from there. 95 */ 96 if (empty || 97 start + m[0].rm_so != last || 98 m[0].rm_so != m[0].rm_eo) { 99 regsub_expand(&buf, &len, with, text + start, m, 100 nitems(m)); 101 102 last = start + m[0].rm_eo; 103 start += m[0].rm_eo; 104 empty = 0; 105 } else { 106 last = start + m[0].rm_eo; 107 start += m[0].rm_eo + 1; 108 empty = 1; 109 } 110 111 /* Stop now if anchored to start. */ 112 if (*pattern == '^') { 113 regsub_copy(&buf, &len, text, start, end); 114 break; 115 } 116 } 117 buf[len] = '\0'; 118 119 regfree(&r); 120 return (buf); 121 } 122