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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
/* $OpenBSD: regsub.c,v 1.1 1996/09/07 21:40:25 downsj Exp $ */
/* vi:set ts=4 sw=4:
* NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE
*
* This is NOT the original regular expression code as written by
* Henry Spencer. This code has been modified specifically for use
* with the VIM editor, and should not be used apart from compiling
* VIM. If you want a good regular expression library, get the
* original code. The copyright notice that follows is from the
* original.
*
* NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE
*
* vim_regsub
*
* Copyright (c) 1986 by University of Toronto.
* Written by Henry Spencer. Not derived from licensed software.
*
* Permission is granted to anyone to use this software for any
* purpose on any computer system, and to redistribute it freely,
* subject to the following restrictions:
*
* 1. The author is not responsible for the consequences of use of
* this software, no matter how awful, even if they arise
* from defects in it.
*
* 2. The origin of this software must not be misrepresented, either
* by explicit claim or by omission.
*
* 3. Altered versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* $Log: regsub.c,v $
* Revision 1.1 1996/09/07 21:40:25 downsj
* Initial revision
*
* Revision 1.2 88/04/28 08:11:25 tony
* First modification of the regexp library. Added an external variable
* 'reg_ic' which can be set to indicate that case should be ignored.
* Added a new parameter to vim_regexec() to indicate that the given string
* comes from the beginning of a line and is thus eligible to match
* 'beginning-of-line'.
*
* Revisions by Olaf 'Rhialto' Seibert, rhialto@mbfys.kun.nl:
* Changes for vi: (the semantics of several things were rather different)
* - Added lexical analyzer, because in vi magicness of characters
* is rather difficult, and may change over time.
* - Added support for \< \> \1-\9 and ~
* - Left some magic stuff in, but only backslashed: \| \+
* - * and \+ still work after \) even though they shouldn't.
*/
#include "vim.h"
#include "globals.h"
#include "proto.h"
#ifndef __ARGS
# define __ARGS(a) a
#endif
#include <stdio.h>
#include "regexp.h"
#ifdef LATTICE
# include <sys/types.h> /* for size_t */
#endif
#ifndef CHARBITS
#define UCHARAT(p) ((int)*(char_u *)(p))
#else
#define UCHARAT(p) ((int)*(p)&CHARBITS)
#endif
extern char_u *reg_prev_sub;
/* This stuff below really confuses cc on an SGI -- webb */
#ifdef __sgi
# undef __ARGS
# define __ARGS(x) ()
#endif
/*
* We should define ftpr as a pointer to a function returning a pointer to
* a function returning a pointer to a function ...
* This is impossible, so we declare a pointer to a function returning a
* pointer to a function returning void. This should work for all compilers.
*/
typedef void (*(*fptr) __ARGS((char_u *, int)))();
static fptr do_upper __ARGS((char_u *, int));
static fptr do_Upper __ARGS((char_u *, int));
static fptr do_lower __ARGS((char_u *, int));
static fptr do_Lower __ARGS((char_u *, int));
static fptr
do_upper(d, c)
char_u *d;
int c;
{
*d = TO_UPPER(c);
return (fptr)NULL;
}
static fptr
do_Upper(d, c)
char_u *d;
int c;
{
*d = TO_UPPER(c);
return (fptr)do_Upper;
}
static fptr
do_lower(d, c)
char_u *d;
int c;
{
*d = TO_LOWER(c);
return (fptr)NULL;
}
static fptr
do_Lower(d, c)
char_u *d;
int c;
{
*d = TO_LOWER(c);
return (fptr)do_Lower;
}
/*
* regtilde: replace tildes in the pattern by the old pattern
*
* Short explanation of the tilde: it stands for the previous replacement
* pattern. If that previous pattern also contains a ~ we should go back
* a step further... but we insert the previous pattern into the current one
* and remember that.
* This still does not handle the case where "magic" changes. TODO?
*
* New solution: The tilde's are parsed once before the first call to
* vim_regsub(). In the old solution (tilde handled in regsub()) is was
* possible to get an endless loop.
*/
char_u *
regtilde(source, magic)
char_u *source;
int magic;
{
char_u *newsub = NULL;
char_u *tmpsub;
char_u *p;
int len;
int prevlen;
for (p = source; *p; ++p)
{
if ((*p == '~' && magic) || (*p == '\\' && *(p + 1) == '~' && !magic))
{
if (reg_prev_sub)
{
/* length = len(current) - 1 + len(previous) + 1 */
prevlen = STRLEN(reg_prev_sub);
tmpsub = alloc((unsigned)(STRLEN(source) + prevlen));
if (tmpsub)
{
/* copy prefix */
len = (int)(p - source); /* not including ~ */
STRNCPY(tmpsub, source, len);
/* interpretate tilde */
STRCPY(tmpsub + len, reg_prev_sub);
/* copy postfix */
if (!magic)
++p; /* back off \ */
STRCAT(tmpsub + len, p + 1);
vim_free(newsub);
newsub = tmpsub;
p = newsub + len + prevlen;
}
}
else if (magic)
STRCPY(p, p + 1); /* remove '~' */
else
STRCPY(p, p + 2); /* remove '\~' */
}
else if (*p == '\\' && p[1]) /* skip escaped characters */
++p;
}
vim_free(reg_prev_sub);
if (newsub)
{
source = newsub;
reg_prev_sub = newsub;
}
else
reg_prev_sub = strsave(source);
return source;
}
/*
- vim_regsub - perform substitutions after a regexp match
*
* If copy is TRUE really copy into dest, otherwise dest is not written to.
*
* Returns the size of the replacement, including terminating \0.
*/
int
vim_regsub(prog, source, dest, copy, magic)
regexp *prog;
char_u *source;
char_u *dest;
int copy;
int magic;
{
register char_u *src;
register char_u *dst;
register char_u *s;
register int c;
register int no;
fptr func = (fptr)NULL;
if (prog == NULL || source == NULL || dest == NULL)
{
emsg(e_null);
return 0;
}
if (UCHARAT(prog->program) != MAGIC)
{
emsg(e_re_corr);
return 0;
}
src = source;
dst = dest;
while ((c = *src++) != '\0')
{
no = -1;
if (c == '&' && magic)
no = 0;
else if (c == '\\' && *src != NUL)
{
if (*src == '&' && !magic)
{
++src;
no = 0;
}
else if ('0' <= *src && *src <= '9')
{
no = *src++ - '0';
}
else if (vim_strchr((char_u *)"uUlLeE", *src))
{
switch (*src++)
{
case 'u': func = (fptr)do_upper;
continue;
case 'U': func = (fptr)do_Upper;
continue;
case 'l': func = (fptr)do_lower;
continue;
case 'L': func = (fptr)do_Lower;
continue;
case 'e':
case 'E': func = (fptr)NULL;
continue;
}
}
}
if (no < 0) /* Ordinary character. */
{
if (c == '\\' && *src != NUL)
{
/* Check for abbreviations -- webb */
switch (*src)
{
case 'r': c = CR; break;
case 'n': c = NL; break;
case 't': c = TAB; break;
/* Oh no! \e already has meaning in subst pat :-( */
/* case 'e': c = ESC; break; */
case 'b': c = Ctrl('H'); break;
default:
/* Normal character, not abbreviation */
c = *src;
break;
}
src++;
}
if (copy)
{
if (func == (fptr)NULL) /* just copy */
*dst = c;
else /* change case */
func = (fptr)(func(dst, c));
/* Turbo C complains without the typecast */
}
dst++;
}
else if (prog->startp[no] != NULL && prog->endp[no] != NULL)
{
for (s = prog->startp[no]; s < prog->endp[no]; ++s)
{
if (copy && *s == '\0') /* we hit NUL. */
{
emsg(e_re_damg);
goto exit;
}
/*
* Insert a CTRL-V in front of a CR, otherwise
* it will be replaced by a line break.
*/
if (*s == CR)
{
if (copy)
{
dst[0] = Ctrl('V');
dst[1] = CR;
}
dst += 2;
}
else
{
if (copy)
{
if (func == (fptr)NULL) /* just copy */
*dst = *s;
else /* change case */
func = (fptr)(func(dst, *s));
/* Turbo C complains without the typecast */
}
++dst;
}
}
}
}
if (copy)
*dst = '\0';
exit:
return (int)((dst - dest) + 1);
}
|