-
Notifications
You must be signed in to change notification settings - Fork 7
/
sed0.cpp
257 lines (234 loc) · 4.42 KB
/
sed0.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
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
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "sed.h"
void readscript(Text*, char*);
void copyscript(Text*, uchar*);
void initinput(int, char **);
FILE *aopen(char*);
#define ustrncmp(a,b,c) strncmp((const char*)(a), (const char*)(b), c)
int recno; /* current record number */
int nflag; /* nonprint option */
int qflag; /* command q executed */
int sflag; /* substitution has occurred */
int bflag; /* strip leading blanks from c,a,i <text> */
int options; /* conjunction, negation */
int
main(int argc, char **argv)
{
static Text script;
static Text data;
for(;;) {
switch(getopt(argc, argv, "bnf:e:")) {
case 'b':
bflag++;
continue;
case 'e':
copyscript(&data, (uchar*)optarg);
continue;
case 'f':
readscript(&data, optarg);
continue;
case 'n':
nflag++;
continue;
case '?':
quit("usage: sed [-n] script [files]\n"
" sed [-n] [-f scriptfile] "
"[-e script] [files]");
case -1:
break;
}
break;
}
if(data.s == 0) {
if(optind >= argc)
quit("no script");
copyscript(&data, (uchar*)argv[optind++]);
}
if(ustrncmp(data.s, "#n", 2) == 0)
nflag = 1;
copyscript(&data, (uchar*)"\n\n"); /* e.g. s/a/\ */
compile(&script, &data);
// printscript(&script); // debugging
initinput(argc-optind, argv+optind);
for(;;) {
data.w = data.s;
if(!readline(&data))
break;
execute(&script, &data);
}
if(fclose(stdout) == EOF)
quit(stdouterr);
return 0;
}
void
grow(Text *t, int n)
{
int w = t->w - t->s;
int e = t->e - t->s + (n/BUFSIZ+1)*BUFSIZ;
t->s = (uchar*)realloc(t->s, e);
if(t->s == 0)
quit("out of space");
t->w = t->s + w;
t->e = t->s + e;
}
/* BUG: a segment that ends with a comment whose
last character is \ causes a diagnostic */
void
safescript(Text *t)
{
if(t->w > t->s+1 && t->w[-2] == '\\')
warn("script segment ends with \\");
}
void
readscript(Text *t, char *s)
{
int n;
FILE *f = aopen(s);
for(;;) {
assure(t, 4);
n = fread(t->w, 1, t->e - t->w - 3, f);
if(n <= 0)
break;
t->w += n;
}
fclose(f);
if(t->w > t->s && t->w[-1] != '\n') {
*t->w++ = '\n';
warn("newline appended to script segment");
}
*t->w = 0;
safescript(t);
}
void
copyscript(Text *t, uchar *s)
{
do {
assure(t, 2);
} while((*t->w++ = *s++) != 0);
if(--t->w > t->s && t->w[-1] != '\n') {
*t->w++ = '\n';
*t->w = 0;
}
safescript(t);
}
/* DATA INPUT */
struct input {
int iargc; /* # of files not fully read */
char **iargv; /* current file */
FILE *ifile; /* current input file */
} input;
/* getch fetches char from current file
returns EOF at final end of file
leaves iargc==0 after line $
*/
#define getch(cp) if((*(cp)=getc(input.ifile))==EOF) \
*(cp)=gopen(); else
int gopen(void); /* called only by getch() */
int
readline(Text *t)
{
int c;
int len = t->w - t->s;
coda();
if(qflag || ateof())
return 0;
for(;;) {
assure(t, 2);
getch(&c);
if(c == '\n')
break;
else if(c != EOF)
*t->w++ = c;
else if(t->w - t->s == len)
return 0;
else {
warn("newline appended");
break;
}
}
*t->w = 0; /* for safety */
getch(&c); /* to identify line $ */
if(c != EOF)
ungetc(c, input.ifile);
recno++;
sflag = 0;
return 1;
}
int
gopen(void)
{
int c = EOF;
while(c==EOF && --input.iargc > 0) {
fclose(input.ifile);
input.ifile = aopen(*++input.iargv);
c = getc(input.ifile);
}
return c;
}
int
ateof(void)
{
return input.iargc <= 0;
}
void
initinput(int argc, char **argv)
{
input.iargc = argc;
input.iargv = argv;
if(input.iargc == 0) {
input.iargc = 1; /* for ateof() */
input.ifile = stdin;
} else
input.ifile = aopen(*input.iargv);
}
FILE *
aopen(char *s)
{
FILE *f = fopen(s, "r");
if(f == 0)
quit("cannot open %s", s);
return f;
}
void
warn(const char *format, ...)
{
va_list args;
fprintf(stderr,"sed warning: ");
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
fprintf(stderr,"\n");
}
void
quit(const char *format, ...)
{
va_list args;
fprintf(stderr,"sed error: ");
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
fprintf(stderr,"\n");
exit(1);
}
/* debugging code 1; compile and execute stubs.
simply prints the already collected script and
prints numbered input lines
void
compile(Text *script, Text *t)
{
uchar *s = t->s;
assure(script, 1);
*script->w++ = 0;
while(*s) putchar(*s++);
}
void
execute(Text *x, Text *y)
{
x = x;
printf("%d: %s", recno, y->s);
}
*/