-
Notifications
You must be signed in to change notification settings - Fork 0
/
evar.c
192 lines (180 loc) · 4.27 KB
/
evar.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Copyright (C) 1984-2024 Mark Nudelman
*
* You may distribute under the terms of either the GNU General Public
* License or the Less License, as specified in the README file.
*
* For more information, see the README file.
*/
/*
* Code to support expanding environment variables in text.
*/
#include "less.h"
#include "xbuf.h"
struct replace {
struct replace *r_next;
char *r_fm;
char *r_to;
};
/*
* Skip to the next unescaped slash or right curly bracket in a string.
*/
static size_t skipsl(constant char *buf, size_t len, size_t e)
{
lbool esc = FALSE;
while (e < len && buf[e] != '\0' && (esc || (buf[e] != '/' && buf[e] != '}')))
{
esc = (!esc && buf[e] == '\\');
++e;
}
return e;
}
/*
* Parse a replacement string: one or more instances of
* (slash, pattern, slash, replacement), followed by right curly bracket.
* Replacement may be empty in which case the second slash is optional.
*/
static struct replace * make_replaces(mutable char *buf, size_t len, size_t *pe, char term)
{
size_t e = *pe;
struct replace *replaces = NULL;
while (term == '/')
{
struct replace *repl;
size_t to;
size_t fm = e;
e = skipsl(buf, len, e);
if (e >= len) break;
if (e == fm) /* missing fm string; we're done */
{
while (e < len)
if (buf[e++] == '}') break;
break;
}
term = buf[e];
buf[e++] = '\0'; /* terminate the fm string */
if (term != '/') /* missing to string */
{
to = e-1;
} else
{
to = e;
e = skipsl(buf, len, e);
if (e >= len) break;
term = buf[e];
buf[e++] = '\0'; /* terminate the to string */
}
repl = ecalloc(1, sizeof(struct replace));
repl->r_fm = &buf[fm];
repl->r_to = &buf[to];
repl->r_next = replaces;
replaces = repl;
}
*pe = e;
return replaces;
}
/*
* Free a list of replace structs.
*/
static void free_replaces(struct replace *replaces)
{
while (replaces != NULL)
{
struct replace *r = replaces;
replaces = r->r_next;
free(r);
}
}
/*
* See if the initial substring of a string matches a pattern.
* Backslash escapes in the pattern are ignored.
* Return the length of the matched substring, or 0 if no match.
*/
static size_t evar_match(constant char *str, constant char *pat)
{
size_t len = 0;
while (*pat != '\0')
{
if (*pat == '\\') ++pat;
if (*str++ != *pat++) return 0;
++len;
}
return len;
}
/*
* Find the replacement for a string (&evar[*pv]),
* given a list of replace structs.
*/
static constant char * find_replace(constant struct replace *repl, constant char *evar, size_t *pv)
{
for (; repl != NULL; repl = repl->r_next)
{
size_t len = evar_match(&evar[*pv], repl->r_fm);
if (len > 0)
{
*pv += len;
return repl->r_to;
}
}
return NULL;
}
/*
* With buf[e] positioned just after NAME in "${NAME" and
* term containing the character at that point, parse the rest
* of the environment var string (including the final right curly bracket).
* Write evar to xbuf, performing any specified text replacements.
* Return the new value of e to point just after the final right curly bracket.
*/
static size_t add_evar(struct xbuffer *xbuf, mutable char *buf, size_t len, size_t e, constant char *evar, char term)
{
struct replace *replaces = make_replaces(buf, len, &e, term);
size_t v;
for (v = 0; evar[v] != '\0'; )
{
constant char *repl = find_replace(replaces, evar, &v);
if (repl == NULL)
xbuf_add_char(xbuf, evar[v++]);
else
{
size_t r;
for (r = 0; repl[r] != '\0'; r++)
{
if (repl[r] == '\\') ++r;
xbuf_add_char(xbuf, repl[r]);
}
}
}
free_replaces(replaces);
return e;
}
/*
* Expand env variables in a string.
* Writes expanded output to xbuf. Corrupts buf.
*/
public void expand_evars(mutable char *buf, size_t len, struct xbuffer *xbuf)
{
size_t i;
for (i = 0; i < len; )
{
if (i+1 < len && buf[i] == '$' && buf[i+1] == '{')
{
constant char *evar;
char term;
size_t e;
i += 2; /* skip "${" */
for (e = i; e < len; e++)
if (buf[e] == '\0' || buf[e] == '}' || buf[e] == '/')
break;
if (e >= len || buf[e] == '\0')
break; /* missing right curly bracket; ignore var */
term = buf[e];
buf[e++] = '\0';
evar = lgetenv_ext(&buf[i], xbuf->data, xbuf->end);
if (evar == NULL) evar = "";
i = add_evar(xbuf, buf, len, e, evar, term);
} else
{
xbuf_add_char(xbuf, buf[i++]);
}
}
}