-
Notifications
You must be signed in to change notification settings - Fork 142
/
gres.c
195 lines (178 loc) · 3.16 KB
/
gres.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
193
194
195
/* gres - grep and substitute Author: Martin C. Atkins */
/*
* globally search, and replace
*
*<-xtx-*>cc -o gres gres.c -lregexp
*/
/*
* This program was written by:
* Martin C. Atkins,
* University of York,
* Heslington,
* York. Y01 5DD
* England
* and is released into the public domain, on the condition
* that this comment is always included without alteration.
*/
#include "stdio.h"
#include "regexp.h"
#define MAXLINE (1024)
int status = 1;
char *usagemsg = "Usage: gres [-g] search replace [file ...]\n";
char *progname;
int gflag = 0; /* != 0 => only do first substitution on line */
extern char *index();
FILE *fopen();
main(argc,argv)
int argc;
char *argv[];
{
regexp *exp;
char *repstr;
char **argp = &argv[1];
progname = argv[0];
if(*argp != 0 && argp[0][0] == '-' && argp[0][1] == 'g') {
gflag = 1;
argp++,argc--;
}
if(argc < 3) {
std_err(usagemsg);
done(2);
}
if(argp[0][0] == '\0') {
std_err("gres: null match string is silly\n");
done(2);
}
if((exp = regcomp(*argp++)) == NULL) {
std_err("gres: regcomp failed\n");
done(2);
}
repstr = *argp++;
if(*argp == 0)
process(stdin,exp,repstr);
else
while(*argp) {
FILE *inf;
if(strcmp(*argp,"-") == 0)
process(stdin,exp,repstr);
else {
if((inf = fopen(*argp,"r")) == NULL) {
std_err("gres: Can't open ");
std_err(*argp);
std_err("\n");
status = 2;
} else {
process(inf,exp,repstr);
fclose(inf);
}
}
argp++;
}
done(status);
}
/*
* This routine does the processing
*/
process(inf, exp, repstr)
FILE *inf;
regexp *exp;
char *repstr;
{
char ibuf[MAXLINE];
while(fgets(ibuf,MAXLINE,inf) != NULL) {
char *cr = index(ibuf,'\n');
if(cr == 0)
std_err("gres: Line broken\n");
else
*cr = '\0';
if(regexec(exp,ibuf,1)) {
pline(exp,ibuf,repstr);
if(status != 2)
status = 0;
} else
printf("%s\n",ibuf);
}
}
void
regerror(s)
char *s;
{
std_err("gres: ");
std_err(s);
std_err("\n");
done(2);
}
char *
getbuf(exp, repstr)
regexp *exp;
char *repstr;
{
char *malloc();
void free();
static bufsize = 0;
static char *buf = 0;
int guess = 10;
int ch;
while(*repstr) {
switch(*repstr) {
case '&':
guess += exp->endp[0] - exp->startp[0];
break;
case '\\':
if((ch = *++repstr) < '0' || ch > '9')
guess += 2;
else {
ch -= '0';
guess += exp->endp[ch] - exp->startp[ch];
}
break;
default:
guess++;
}
repstr++;
}
if(bufsize < guess) {
if(buf != 0)
free((char *)buf);
buf = malloc(guess);
}
return buf;
}
pline(exp, ibuf, repstr)
regexp *exp;
char ibuf[];
char *repstr;
{
do {
dosub(exp,ibuf,repstr);
ibuf = exp->endp[0];
if(*ibuf == '\0')
break;
if(ibuf == exp->startp[0])
putchar(*ibuf++);
} while(!gflag && regexec(exp,ibuf,0));
printf("%s\n",ibuf);
}
/*
* print one subsitution
*/
dosub(exp,ibuf,repstr)
regexp *exp;
char ibuf[];
char *repstr;
{
char *buf = getbuf(exp, repstr);
char *end = exp->startp[0];
int ch = *end;
*end = '\0';
fputs(ibuf,stdout); /* output the initial part of line */
*end = ch;
regsub(exp, repstr, buf);
fputs(buf,stdout);
}
done(n)
int n;
{
_cleanup(); /* flush stdio's internal buffers */
exit(n);
}