Line | Count | Source |
1 | | /* $OpenBSD: regsub.c,v 1.10 2026/07/08 11:04:51 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 | 703 | { |
30 | 703 | size_t add = end - start; |
31 | | |
32 | 703 | *buf = xrealloc(*buf, (*len) + add + 1); |
33 | 703 | memcpy((*buf) + *len, text + start, add); |
34 | 703 | (*len) += add; |
35 | 703 | } |
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 = 0; |
68 | 8.71k | char *buf = NULL; |
69 | | |
70 | 8.71k | if (*text == '\0') |
71 | 935 | return (xstrdup("")); |
72 | 7.77k | if (*pattern == '\0') |
73 | 7.07k | return (xstrdup(text)); |
74 | 703 | if (regcomp(&r, pattern, flags) != 0) |
75 | 0 | return (NULL); |
76 | | |
77 | 703 | start = 0; |
78 | 703 | last = 0; |
79 | 703 | end = strlen(text); |
80 | | |
81 | 703 | while (start <= end) { |
82 | 703 | if (regexec(&r, text + start, nitems(m), m, 0) != 0) { |
83 | 703 | regsub_copy(&buf, &len, text, start, end); |
84 | 703 | break; |
85 | 703 | } |
86 | | |
87 | | /* |
88 | | * Append any text not part of this match (from the end of the |
89 | | * last match). |
90 | | */ |
91 | 0 | regsub_copy(&buf, &len, text, last, m[0].rm_so + start); |
92 | | |
93 | | /* For anchored patterns, replace the first match only. */ |
94 | 0 | if (*pattern == '^') { |
95 | 0 | regsub_expand(&buf, &len, with, text + start, m, |
96 | 0 | nitems(m)); |
97 | 0 | last = start + m[0].rm_eo; |
98 | 0 | regsub_copy(&buf, &len, text, last, end); |
99 | 0 | break; |
100 | 0 | } |
101 | | |
102 | | /* |
103 | | * If the last match was empty and this one isn't (it is either |
104 | | * later or has matched text), expand this match. If it is |
105 | | * empty, move on one character and try again from there. |
106 | | */ |
107 | 0 | if (empty || |
108 | 0 | start + m[0].rm_so != last || |
109 | 0 | m[0].rm_so != m[0].rm_eo) { |
110 | 0 | regsub_expand(&buf, &len, with, text + start, m, |
111 | 0 | nitems(m)); |
112 | |
|
113 | 0 | last = start + m[0].rm_eo; |
114 | 0 | start += m[0].rm_eo; |
115 | 0 | empty = 0; |
116 | 0 | } else { |
117 | 0 | last = start + m[0].rm_eo; |
118 | 0 | start += m[0].rm_eo + 1; |
119 | 0 | empty = 1; |
120 | 0 | } |
121 | 0 | } |
122 | 703 | buf[len] = '\0'; |
123 | | |
124 | 703 | regfree(&r); |
125 | 703 | return (buf); |
126 | 703 | } |