-
Notifications
You must be signed in to change notification settings - Fork 7
/
sed3.cpp
143 lines (132 loc) · 2.81 KB
/
sed3.cpp
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
#include <string.h>
#include "sed.h"
void docopy(uchar *where, int n);
int dosub(uchar *where, uchar *rp);
Text retemp; /* holds a rewritten regex, without delimiter */
int
recomp(Text *rebuf, Text *t, int delim)
{
static int lastre;
uchar *w;
vacate(&retemp);
for(w=t->w; ; retemp.w++,w++) {
assure(&retemp, 2)
*retemp.w = *w;
if(*w == delim)
break;
else if(*w==0 || *w=='\n')
syntax("unterminated address");
else if(*w != '\\')
continue;
else if(*++w==delim)
*retemp.w = delim;
else if(*w == 'n')
*retemp.w = '\n';
else if(*w==0 || *w=='\n')
syntax("unterminated regular expression");
else {
assure(&retemp, 2);
*++retemp.w = *w;
}
}
*retemp.w = 0;
assure(rebuf, sizeof(regex_t));
if(*retemp.s != 0) {
if(regcomp((regex_t*)rebuf->w,(char*)retemp.s,options) != 0)
syntax("bad regular expression");
lastre = rebuf->w - rebuf->s;
rebuf->w += sizeof(regex_t);
} else if(rebuf->w == rebuf->s)
syntax("no previous regular expression");
t->w = w + 1;
return lastre;
}
Text gendata;
#define NMATCH 10
regmatch_t matches[NMATCH];
#define so matches[0].rm_so
#define eo matches[0].rm_eo
int
substitute(regex_t *re, Text* data, uchar *rhs, int n)
{
Text t;
uchar *where = data->s;
if(regexec(re, (char*)data->s, NMATCH, matches, 0))
return 0;
vacate(&gendata);
if(n == 0)
do {
docopy(where, so);
if(!dosub(where, rhs))
return 0;
where += eo;
if(eo == so) {
if(where < data->w)
docopy(where++, 1);
else
goto done;
}
} while(regexec(re, (char*)where, NMATCH, matches, REG_NOTBOL) == 0);
else {
while(--n > 0) {
where += eo;
if(eo == so) {
if(where < data->w)
where++;
else
return 0;
}
if(regexec(re, (char*)where, NMATCH, matches, REG_NOTBOL))
return 0;
}
docopy(data->s, where-data->s+so);
if(!dosub(where, rhs))
return 0;
where += eo;
}
eo = so = data->w - where;
docopy(where, so);
done:
exch(gendata, *data, t);
return 1;
}
void
docopy(uchar *where, int n)
{
assure(&gendata, n+1);
memmove(gendata.w, where, n);
gendata.w += n;
*gendata.w = 0;
}
/* interpretation problem: if there is no match for \1, say,
does the substitition occur? dosub uses a null string.
a change where indicated will abort the substitution */
int
dosub(uchar *where, uchar *rp)
{
int c, n;
regmatch_t *m;
while((c = *rp++) != 0) {
if(c == '\\') {
c = *rp++;
if (c >= '1' && c <= '9') {
m = matches + c - '0';
if(m->rm_eo == -1)
continue; /* or return 0 */
n = m->rm_eo - m->rm_so;
assure(&gendata, n);
memmove(gendata.w,where+m->rm_so,n);
gendata.w += n;
continue;
}
} else if(c == '&') {
assure(&gendata, eo-so);
memmove(gendata.w,where+so,eo-so);
gendata.w += eo-so;
continue;
}
assure(&gendata, 1);
*gendata.w++ = c;
}
return 1;
}