~ubuntu-branches/ubuntu/feisty/elvis/feisty

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
/* regsub.c */

/* This file contains the regsub() function, which performs substitutions
 * after a regexp match has been found.
 */

#include "elvis.h"
#ifdef FEATURE_RCSID
char id_regsub[] = "$Id: regsub.c,v 2.22 2003/10/17 17:41:23 steve Exp $";
#endif

/* Allocate a new copy of the replacement string, with all ~'s replaced by
 * the previous replacement string.
 *
 * NOTE: The value returned by this function should never be freed from outside
 * this function, because this function maintains an internal pointer to the
 * same memory so it knows what value to substitute for ~ on next invocation.
 */
CHAR *regtilde(newp)
	CHAR	*newp;	/* new text as supplied by user */
{
	static CHAR *prev;	/* previous replacement text */
	static CHAR *willfree;	/* previous replacement text if nosaveregex */
	CHAR	*ret;		/* returned string */
	CHAR	*scan;		/* used for stepping through chars of "prev" */

	/* If "willfree" isn't NULL, then free it now.  This is used when
	 * the saveregexp option is turned off, so we can leave prev unchanged.
	 */
	if (willfree)
	{
		safefree(willfree);
		willfree = NULL;
	}

	/* copy new into ret, replacing the ~s by the previous text */
	for (ret = NULL; *newp; )
	{
		if (o_magic && *newp == '~')
		{
			if (!prev) goto Fail;
			for (scan = prev; *scan; scan++)
				buildCHAR(&ret, *scan);
			newp++;
		}
		else if (!o_magic && *newp == '\\' && *(newp + 1) == '~')
		{
			if (!prev) goto Fail;
			for (scan = prev; *scan; scan++)
				buildCHAR(&ret, *scan);
			newp += 2;
		}
		else
		{
			if (*newp == '\\' && *(newp + 1))
			{
				buildCHAR(&ret, *newp++);
			}
			buildCHAR(&ret, *newp++);
		}
	}

	/* if empty string, then allocate a single '\0' character */
	if (!ret)
		ret = (CHAR *)safealloc(1, sizeof(CHAR));

	/* remember this as the "previous" for next time */
	if (o_saveregexp)
	{
		if (prev)
			safefree(prev);
		prev = ret;
	}
	else
	{
		/* leave "prev" unchanged, but remember it somewhere else so
		 * we can free the text when regtilde() is called next time.
		 */
		willfree = ret;
	}

	return ret;

Fail:
	msg(MSG_ERROR, "no previous text to substitute for ~");
	if (ret)
		safefree(ret);
	return NULL;
}

/* Perform substitutions after a regexp match.  "re" is the compiled regular
 * expression which has been matched to a text string.  "new" is a pointer to
 * the replacement text string.  Return the actual replacement text (after all
 * metacharacters have been processed) if successful, or NULL if error.  The
 * calling function is responsible for calling safefree() on the returned
 * string.
 */
CHAR *regsub(re, newp, doit)
	regexp		*re;	/* a regular expression that has been matched */
	REG CHAR	*newp;	/* the replacement text */
	ELVBOOL		doit;	/* perform the substitution? (else just return string) */
{
	MARKBUF		cpy;	/* start of text to copy */
	long		end;	/* length of text to copy */
	REG CHAR	c;	/* a character from "new" text */
	long		cval;	/* numeric value of 'c', if 'c' is digit */
	CHAR		*inst;	/* the new next, after processing escapes */
	int		mod = 0;/* used to track \U, \L, \u, \l, and \E */
	int		len;	/* used to calculate length of subst string */
	MARKBUF		tmp;	/* end of replacement region */
	CHAR		*scan;	/* used for scanning a segment of orig text */
	char		lnum[12];/* line number */

	/* initialize "cval" just to silence a compiler warning */
	cval = 0;

	/* for each character of the new text... */
	for (inst = NULL, len = 0; (c = *newp++) != '\0'; )
	{
		/* recognize any meta characters */
		if (c == '&' && o_magic)
		{
			(void)marktmp(cpy, re->buffer, re->startp[0]);
			end = re->endp[0] - re->startp[0];
		}
		else if (c == '\\')
		{
			c = *newp++;
			switch (c)
			{
			  case '0':
				/* Traditionally \0 has been a synonym for &,
				 * but we need a way to insert NUL so...
				 */
				len = buildCHAR(&inst, '\0');
				continue;

			  case '1':
			  case '2':
			  case '3':
			  case '4':
			  case '5':
			  case '6':
			  case '7':
			  case '8':
			  case '9':
				/* \0 thru \9 mean "copy subexpression" */
				cval = c - '0';
				(void)marktmp(cpy, re->buffer, re->startp[cval]);
				end = re->endp[cval] - re->startp[cval];
				break;

			  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;

			  case '&':
				/* "\&" means "original text" */
				if (o_magic)
				{
					len = buildCHAR(&inst, c);
					continue;
				}
				(void)marktmp(cpy, re->buffer, re->startp[0]);
				end = re->endp[0] - re->startp[0];
				break;

			  case '#':	/* "\#" means "line number" */
				sprintf(lnum, "%ld", markline(marktmp(tmp, re->buffer, re->startp[0])));
				len = buildstr(&inst, lnum);
				continue;

			  case 'a':	/* \a => ^G, <BEL>	*/
				len = buildCHAR(&inst, '\007');
				continue;

			  case 'b':	/* \b => ^H, <BS>	*/
				len = buildCHAR(&inst, '\b');
				continue;

 			  case 'f':	/* \f => ^L, <FF>	*/
				len = buildCHAR(&inst, '\f');
				continue;

 			  case 'n':	/* \n => ^J, <NL>	*/
				len = buildCHAR(&inst, '\n');
				continue;

 			  case 'r':	/* \r => ^M, <CR>	*/
				len = buildCHAR(&inst, '\r');
				continue;

			  case 't':	/* \t => ^I, <TAB>	*/
				len = buildCHAR(&inst, '\t');
				continue;

#if 0
				/* \e is already taken for ending of \U and \L.
				 * Though it's a shame both \E and \e are well
				 * documented to ex/vi here we' like \E to
				 * suffice, so we would use \e for <Esc>.
				 */
			  case 'e':	/* \e => ^[, <ESC>	*/
				len = buildCHAR(&inst, '\033');
				continue;
#endif

			  default:
				/* ordinary char preceded by backslash */
				len = buildCHAR(&inst, c);
				continue;
			}
		}
# if OSK
		else if (c == '\l')
# else
		else if (c == '\r')
# endif
		{
			/* transliterate ^M into newline */
			len = buildCHAR(&inst, '\n');
			continue;
		}
		else
		{
			/* ordinary character, so just copy it */
			if (!mod)
				len = buildCHAR(&inst, c);
			else if (elvtolower(mod) == 'l')
				len = buildCHAR(&inst, elvtolower(c));
			else
				len = buildCHAR(&inst, elvtoupper(c));
			if (elvlower(mod))
				mod = 0;
			continue;
		}

		/* Note: to reach this point in the code, we 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 (markoffset(&cpy) < 0)
		{
			msg(MSG_ERROR, "[d]too few \\\\\\(\\\\\\)s to use \\\\$1", cval);
			if (inst)
				safefree(inst);
			return NULL;
		}

		/* copy over a portion of the original */
		for (scanalloc(&scan, &cpy);
		     scan && end > 0;
		     scannext(&scan), end--)
		{
			switch (mod)
			{
			  case 'U':
			  case 'u':
				/* convert to uppercase */
				len = buildCHAR(&inst, (_CHAR_)elvtoupper(*scan));
				break;

			  case 'L':
			  case 'l':
				/* convert to lowercase */
				len = buildCHAR(&inst, (_CHAR_)elvtolower(*scan));
				break;

			  default:
				/* copy without any conversion */
				len = buildCHAR(&inst, *scan);
			}

			/* \u and \l end automatically after the first char */
			if (mod == 'u' || mod == 'l')
			{
				mod = 0;
			}
		}
		scanfree(&scan);
	}

	/* if we're supposed to perform the substitution, then do it */
	if (doit)
	{
		/* replace the old text with the new text in the buffer */
		bufreplace(marktmp(cpy, re->buffer, re->startp[0]),
			marktmp(tmp, re->buffer, re->endp[0]), inst, len);
	
		/* Adjust the offset of the end of the whole expression
		 * to compensate for the change in the length of text.
		 * Also, if this regexp could conceivably match a
		 * zero-length string, then require at least 1 unmatched
		 * character between matches.
		 */
		re->endp[0] = re->startp[0] + len;
		if (re->minlen == 0
			&& re->endp[0] < o_bufchars(re->buffer)
			&& scanchar(marktmp(tmp, re->buffer, re->endp[0])) != '\n')
		{
			re->endp[0]++;
		}
	}

	/* At this point, we know we were successful but the "inst" pointer
	 * will be NULL if the replacement text is 0 characters long.  We don't
	 * want to return NULL for a successful substitution, so allocate
	 * a string which contains only a '\0' character and return that.
	 */
	if (!inst)
	{
		assert(len == 0);
		buildCHAR(&inst, (_CHAR_)'\0');
		assert(inst != NULL);
	}

	return inst;
}