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