nicm | 3e72e98 | 2019-06-13 19:46:00 +0000 | [diff] [blame] | 1 | /* $OpenBSD$ */ |
| 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 |
nicm | e9e5fac | 2020-04-09 14:23:34 +0000 | [diff] [blame] | 27 | regsub_copy(char **buf, size_t *len, const char *text, size_t start, size_t end) |
nicm | 3e72e98 | 2019-06-13 19:46:00 +0000 | [diff] [blame] | 28 | { |
| 29 | size_t add = end - start; |
| 30 | |
| 31 | *buf = xrealloc(*buf, (*len) + add + 1); |
| 32 | memcpy((*buf) + *len, text + start, add); |
| 33 | (*len) += add; |
| 34 | } |
| 35 | |
| 36 | static void |
| 37 | regsub_expand(char **buf, size_t *len, const char *with, const char *text, |
| 38 | regmatch_t *m, u_int n) |
| 39 | { |
| 40 | const char *cp; |
| 41 | u_int i; |
| 42 | |
| 43 | for (cp = with; *cp != '\0'; cp++) { |
| 44 | if (*cp == '\\') { |
| 45 | cp++; |
| 46 | if (*cp >= '0' && *cp <= '9') { |
| 47 | i = *cp - '0'; |
| 48 | if (i < n && m[i].rm_so != m[i].rm_eo) { |
| 49 | regsub_copy(buf, len, text, m[i].rm_so, |
| 50 | m[i].rm_eo); |
| 51 | continue; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | *buf = xrealloc(*buf, (*len) + 2); |
| 56 | (*buf)[(*len)++] = *cp; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | char * |
| 61 | regsub(const char *pattern, const char *with, const char *text, int flags) |
| 62 | { |
| 63 | regex_t r; |
| 64 | regmatch_t m[10]; |
nicm | f4e8357 | 2019-06-20 15:40:14 +0000 | [diff] [blame] | 65 | ssize_t start, end, last, len = 0; |
| 66 | int empty = 0; |
nicm | 3e72e98 | 2019-06-13 19:46:00 +0000 | [diff] [blame] | 67 | char *buf = NULL; |
| 68 | |
| 69 | if (*text == '\0') |
| 70 | return (xstrdup("")); |
| 71 | if (regcomp(&r, pattern, flags) != 0) |
| 72 | return (NULL); |
| 73 | |
| 74 | start = 0; |
nicm | f4e8357 | 2019-06-20 15:40:14 +0000 | [diff] [blame] | 75 | last = 0; |
nicm | 3e72e98 | 2019-06-13 19:46:00 +0000 | [diff] [blame] | 76 | end = strlen(text); |
| 77 | |
nicm | f4e8357 | 2019-06-20 15:40:14 +0000 | [diff] [blame] | 78 | while (start <= end) { |
nicm | eb4d60b | 2019-11-27 20:54:30 +0000 | [diff] [blame] | 79 | if (regexec(&r, text + start, nitems(m), m, 0) != 0) { |
nicm | 3e72e98 | 2019-06-13 19:46:00 +0000 | [diff] [blame] | 80 | regsub_copy(&buf, &len, text, start, end); |
| 81 | break; |
| 82 | } |
nicm | 3e72e98 | 2019-06-13 19:46:00 +0000 | [diff] [blame] | 83 | |
nicm | f4e8357 | 2019-06-20 15:40:14 +0000 | [diff] [blame] | 84 | /* |
| 85 | * Append any text not part of this match (from the end of the |
| 86 | * last match). |
| 87 | */ |
nicm | eb4d60b | 2019-11-27 20:54:30 +0000 | [diff] [blame] | 88 | regsub_copy(&buf, &len, text, last, m[0].rm_so + start); |
nicm | f4e8357 | 2019-06-20 15:40:14 +0000 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * If the last match was empty and this one isn't (it is either |
| 92 | * later or has matched text), expand this match. If it is |
| 93 | * empty, move on one character and try again from there. |
| 94 | */ |
nicm | eb4d60b | 2019-11-27 20:54:30 +0000 | [diff] [blame] | 95 | if (empty || |
| 96 | start + m[0].rm_so != last || |
| 97 | m[0].rm_so != m[0].rm_eo) { |
| 98 | regsub_expand(&buf, &len, with, text + start, m, |
| 99 | nitems(m)); |
nicm | f4e8357 | 2019-06-20 15:40:14 +0000 | [diff] [blame] | 100 | |
nicm | eb4d60b | 2019-11-27 20:54:30 +0000 | [diff] [blame] | 101 | last = start + m[0].rm_eo; |
| 102 | start += m[0].rm_eo; |
nicm | f4e8357 | 2019-06-20 15:40:14 +0000 | [diff] [blame] | 103 | empty = 0; |
| 104 | } else { |
nicm | eb4d60b | 2019-11-27 20:54:30 +0000 | [diff] [blame] | 105 | last = start + m[0].rm_eo; |
| 106 | start += m[0].rm_eo + 1; |
nicm | f4e8357 | 2019-06-20 15:40:14 +0000 | [diff] [blame] | 107 | empty = 1; |
| 108 | } |
nicm | 20c1f1a | 2019-11-24 18:37:23 +0000 | [diff] [blame] | 109 | |
| 110 | /* Stop now if anchored to start. */ |
| 111 | if (*pattern == '^') { |
| 112 | regsub_copy(&buf, &len, text, start, end); |
| 113 | break; |
| 114 | } |
nicm | 3e72e98 | 2019-06-13 19:46:00 +0000 | [diff] [blame] | 115 | } |
| 116 | buf[len] = '\0'; |
| 117 | |
| 118 | regfree(&r); |
| 119 | return (buf); |
| 120 | } |