Repository navigation
Expand file tree
/
Copy pathregsub.c
More file actions
243 lines (223 loc) · 4.67 KB
/
Copy pathregsub.c
File metadata and controls
243 lines (223 loc) · 4.67 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* regsub.c */
/* This file contains the regsub() function, which performs substitutions
* after a regexp match has been found.
*/
#include "config.h"
#include "ctype.h"
#include "vi.h"
#include "regexp.h"
/* perform substitutions after a regexp match */
void regsub(re, src, dst)
regexp *re; /* the regexp with pointers into matched text */
REG char *src; /* the replacement string */
REG char *dst; /* where to put the result of the subst */
{
REG char *cpy; /* pointer to start of text to copy */
REG char *end; /* pointer to end of text to copy */
REG char c;
char *start;
#ifndef CRUNCH
int mod = 0;/* used to track \U, \L, \u, \l, and \E */
int len; /* used to calculate length of subst string */
static char *prev; /* a copy of the text from the previous subst */
/* replace \~ (or maybe ~) by previous substitution text */
/* step 1: calculate the length of the new substitution text */
for (len = strlen(src), c = '\0', cpy = src; *cpy; cpy++)
{
# ifdef NO_MAGIC
if (c == '\\' && *cpy == '~')
# else
if (c == (*o_magic ? '\0' : '\\') && *cpy == '~')
# endif
{
if (!prev)
{
regerror("No prev text to substitute for ~");
return;
}
len += strlen(prev) - 1;
# ifndef NO_MAGIC
if (!*o_magic)
# endif
len -= 1; /* because we lose the \ too */
}
/* watch backslash quoting */
if (c != '\\' && *cpy == '\\')
c = '\\';
else
c = '\0';
}
/* allocate memory for the ~ed version of src */
start = cpy = (char *)malloc((unsigned)(len + 1));
if (!cpy)
{
regerror("Not enough memory for ~ expansion");
return;
}
/* copy src into start, replacing the ~s by the previous text */
while (*src)
{
# ifndef NO_MAGIC
if (*o_magic && *src == '~')
{
strcpy(cpy, prev);
cpy += strlen(prev);
src++;
}
else if (!*o_magic && *src == '\\' && *(src + 1) == '~')
# else /* NO_MAGIC */
if (*src == '\\' && *(src + 1) == '~')
# endif /* NO_MAGIC */
{
strcpy(cpy, prev);
cpy += strlen(prev);
src += 2;
}
else
{
*cpy++ = *src++;
}
}
*cpy = '\0';
#ifdef DEBUG
if ((int)(cpy - start) != len)
{
msg("Bug in regsub.c! Predicted length = %d, Actual length = %d", len, (int)(cpy - start));
}
#endif
/* remember this as the "previous" for next time */
if (prev)
free(prev);
prev = src = start;
#endif /* undef CRUNCH */
start = src;
while ((c = *src++) != '\0')
{
#ifndef NO_MAGIC
/* recognize any meta characters */
if (c == '&' && *o_magic)
{
cpy = re->startp[0];
end = re->endp[0];
}
else
#endif /* not NO_MAGIC */
if (c == '\\')
{
c = *src++;
switch (c)
{
#ifndef NO_MAGIC
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
/* \0 thru \9 mean "copy subexpression" */
c -= '0';
cpy = re->startp[c];
end = re->endp[c];
break;
# ifndef CRUNCH
case 'U':
case 'u':
case 'L':
case 'l':
/* \U and \L mean "convert to upper/lowercase" */
mod = c;
continue;
case 'E':
case 'e':
/* \E ends the \U or \L */
mod = 0;
continue;
# endif /* not CRUNCH */
case '&':
/* "\&" means "original text" */
if (*o_magic)
{
*dst++ = c;
continue;
}
cpy = re->startp[0];
end = re->endp[0];
break;
#else /* NO_MAGIC */
case '&':
/* "\&" means "original text" */
cpy = re->startp[0];
end = re->endp[0];
break;
#endif /* NO_MAGIC */
default:
/* ordinary char preceded by backslash */
*dst++ = c;
continue;
}
}
#ifndef CRUNCH
# if OSK
else if (c == '\l')
# else
else if (c == '\r')
# endif
{
/* transliterate ^M into newline */
*dst++ = '\n';
continue;
}
#endif /* !CRUNCH */
else
{
/* ordinary character, so just copy it */
*dst++ = c;
continue;
}
/* Note: to reach this point in the code, we must have evaded
* all "continue" statements. To do that, we must have hit
* a metacharacter that involves copying.
*/
/* if there is nothing to copy, loop */
if (!cpy)
continue;
/* copy over a portion of the original */
while (cpy < end)
{
#ifndef NO_MAGIC
# ifndef CRUNCH
switch (mod)
{
case 'U':
case 'u':
/* convert to uppercase */
*dst++ = toupper(*cpy++);
break;
case 'L':
case 'l':
/* convert to lowercase */
*dst++ = tolower(*cpy++);
break;
default:
/* copy without any conversion */
*dst++ = *cpy++;
}
/* \u and \l end automatically after the first char */
if (mod && (mod == 'u' || mod == 'l'))
{
mod = 0;
}
# else /* CRUNCH */
*dst++ = *cpy++;
# endif /* CRUNCH */
#else /* NO_MAGIC */
*dst++ = *cpy++;
#endif /* NO_MAGIC */
}
}
*dst = '\0';
}