-
Notifications
You must be signed in to change notification settings - Fork 7
/
sed1.cpp
660 lines (604 loc) · 12.8 KB
/
sed1.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include "sed.h"
#define ustrlen(p) strlen((char*)(p))
#define ustrcmp(p, q) strcmp((const char*)(p), (const char*)(q))
#define ustrcpy(p, q) (uchar*)strcpy((char*)(p), (const char*)(q))
#define ustrchr(p, c) (uchar*)strchr((const char*)(p), c)
int blank(Text*);
void fixlabels(Text*);
void fixbrack(Text*);
void ckludge(Text*, int, int, int, Text*);
int addr(Text*, Text*);
int pack(int, int, int);
int* instr(uchar*);
uchar *succi(uchar*);
extern void jprint(regex_t*); /* secret entry into regex pkg */
int semicolon;
Text rebuf;
uchar adrs[256] = { /* max no. of addrs, 3 is illegal */
0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3, /* <nl> */
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 2, 3, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* !# */
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 3, 1, 3, 3, /* := */
3, 3, 3, 3, 2, 3, 3, 2, 2, 3, 3, 3, 3, 3, 2, 3, /* DGHN */
2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* P */
3, 1, 2, 2, 2, 3, 3, 2, 2, 1, 3, 3, 2, 3, 2, 3, /* a-n */
2, 1, 2, 2, 2, 3, 3, 2, 2, 2, 3, 2, 3, 0, 3, 3, /* p-y{} */
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3
};
#define Ec Tc /* commands that have same compilation method */
#define Dc Tc
#define Gc Tc
#define Hc Tc
#define Nc Tc
#define Pc Tc
#define dc Tc
#define gc Tc
#define hc Tc
#define lc Tc
#define nc Tc
#define pc Tc
#define qc Tc
#define xc Tc
#define tc bc
#define ic ac
#define cc ac
typedef void cmdf(Text*, Text*);
cmdf Xc, Cc, Ec; /* comment #, colon, equal */
cmdf Lc, Rc; /* left {, right }, */
cmdf Ic, Tc, xx; /* ignore, trivial, error */
cmdf Dc, Gc, Hc, Nc, Pc;
cmdf ac, bc, cc, dc, gc, hc, ic, nc;
cmdf pc, qc, rc, sc, tc, wc, xc, yc;
static cmdf *docom[128] = {
xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,Ic,xx,xx,xx,xx,xx, /* <nl> */
xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,
xx,Ic,xx,Xc,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx, /* !# */
xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,Cc,Ic,xx,Ec,xx,xx, /* :;= */
xx,xx,xx,xx,Dc,xx,xx,Gc,Hc,xx,xx,xx,xx,xx,Nc,xx, /* DGHN */
Pc,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx, /* P */
xx,ac,bc,cc,dc,xx,xx,gc,hc,ic,xx,xx,lc,xx,nc,xx, /* a-n */
pc,qc,rc,sc,tc,xx,xx,wc,xc,yc,xx,Lc,xx,Rc,xx,xx /* p-y{} */
};
uchar *synl; /* current line pointer for syntax errors */
void
compile(Text *script, Text *t)
{
int loc; /* progam counter */
int neg; /* ! in effect */
int cmd;
int naddr;
int *q; /* address of instruction word */
t->w = t->s; /* here w is a read pointer */
while(*t->w) {
assure(script, 4*sizeof(int));
loc = script->w - script->s;
synl = t->w;
naddr = 0;
while(blank(t)) ;
naddr += addr(script, t);
if(naddr && *t->w ==',') {
t->w++;
naddr += addr(script, t);
if(naddr < 2)
syntax("missing address");
}
q = (int*)script->w;
if(naddr == 2)
*q++ = INACT;
script->w = (uchar*)(q+1);
neg = 0;
for(;;) {
while(blank(t));
cmd = *t->w++;
if(neg && docom[cmd&0xff]==Ic)
syntax("improper !");
if(cmd != '!')
break;
neg = NEG;
}
if(!neg) {
switch(adrs[cmd]) {
case 1:
if(naddr <= 1)
break;
case 0:
if(naddr == 0)
break;
syntax("too many addresses");
}
}
docom[cmd&0xff](script, t);
switch(*t->w) {
case 0:
script->w = script->s + loc;
break;
default:
if(cmd == '{')
break;
syntax("junk after command");
case ';':
if(!semicolon++)
synwarn("semicolon separators");
case '\n':
t->w++;
}
*q = pack(neg,cmd,script->w-script->s-loc);
}
fixbrack(script);
fixlabels(script);
}
/* COMMAND LAYOUT */
int
blank(Text *t)
{
if(*t->w==' ' || *t->w=='\t') {
t->w++;
return 1;
} else
return 0;
}
int *
instr(uchar *p) /* get address of command word */
{
int *q = (int*)p;
while((*q & IMASK) != IMASK)
q++;
return q;
}
uchar *
succi(uchar *p)
{
int *q = instr(p);
if(code(*q) == '{')
return (uchar*)(q+1);
else
return p + (*q & LMASK);
}
int
pack(int neg, int cmd, int length)
{
int l = length & LMASK;
if(length != l)
syntax("<command-list> or <text> too long");
return IMASK | neg | cmd << 2*BYTE | l;
}
void
putint(Text *s, int n)
{
assure(s, sizeof(int));
*(int*)s->w = n;
s->w += sizeof(int);
}
int
number(Text *t)
{
unsigned n = 0;
while(isdigit(*t->w)) {
if(n > (INT_MAX-9)/10)
syntax("number too big");
n = n*10 + *t->w++ - '0';
}
return n;
}
int
addr(Text *script, Text *t)
{
int n;
switch(*t->w) {
default:
return 0;
case '$':
t->w++;
n = DOLLAR;
break;
case '\\':
t->w++;
if(*t->w=='\n' ||*t->w=='\\')
syntax("bad regexp delimiter");
case '/':
n = recomp(&rebuf, t, *t->w++) | REGADR;
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
n = number(t);
if(n == 0)
syntax("address is zero");
}
putint(script, n);
return 1;
}
regex_t *
readdr(int x)
{
return (regex_t*)(rebuf.s + (x&AMASK));
}
/* LABEL HANDLING */
/* the labels array consists of intptr_t values followed by strings.
value -1 means unassigned; other values are relative to the
beginning of the script.
the files array, which is also accessed using lablook,
stores FILE* pointers into the intptr_t slots.
value -1 still means unassigned.
on the first pass, every script ref to a label becomes the
integer offset of that label in the labels array, or -1 if
it is a branch to the end of script
on the second pass (fixlabels), the script ref is replaced
by the value from the labels array. */
Text labels;
intptr_t *
lablook(uchar *l, Text *labels)
{
uchar *p, *q;
int n;
assure(labels, 1);
for(p = labels->s; p < labels->w; ) {
q = p + sizeof(intptr_t);
if(ustrcmp(q, l) == 0)
return (intptr_t*)p;
q += ustrlen(q) + 1;
p = (uchar*)intptrp(q);
}
n = ustrlen(l);
assure(labels, sizeof(intptr_t)+n+1+sizeof(intptr_t));
*(intptr_t*)p = -1;
q = p + sizeof(intptr_t);
ustrcpy(q, l);
q += ustrlen(q) + 1;
labels->w = (uchar*)intptrp(q);
return (intptr_t*)p;
}
/* find pos in label list; assign value i to label if i>=0 */
int
getlab(Text *t, int i)
{
intptr_t *p;
uchar *u;
while(blank(t)); /* not exactly posix */
for(u=t->w; *t->w!='\n'; t->w++)
if(!isprint(*t->w) || *t->w==' ')
synwarn("invisible character in name");
if(u == t->w)
return -1;
*t->w = 0;
p = lablook(u, &labels);
if(*p == -1)
*p = i;
else if(i != -1)
syntax("duplicate label");
*t->w = '\n';
return (uchar*)p - labels.s;
}
void
Cc(Text *script, Text *t) /* colon */
{
if(getlab(t, script->w - sizeof(int) - script->s) == -1)
syntax("missing label");
}
void
bc(Text *script, Text *t)
{
int g;
g = getlab(t, -1); /* relative pointer to label list */
putint(script, g);
}
void
fixlabels(Text *script)
{
uchar *p;
int *q;
for(p=script->s; p<script->w; p=succi(p)) {
q = instr(p);
switch(code(*q)) {
case 't':
case 'b':
if(q[1] == -1)
q[1] = script->w - script->s;
else if(*(intptr_t*)(labels.s+q[1]) != -1)
q[1] = *(intptr_t*)(labels.s+q[1]);
else
quit("undefined label: ",
labels.s+q[1]+sizeof(intptr_t));
}
}
free(labels.s);
}
/* FILES */
Text files;
void
rc(Text *script, Text *t)
{
uchar *u;
if(!blank(t))
synwarn("no space before file name");
while(blank(t)) ;
for(u=t->w; *t->w!='\n'; t->w++) ;
if(u == t->w)
syntax("missing file name");
*t->w = 0;
putint(script, (uchar*)lablook(u, &files) - files.s);
*t->w = '\n';
}
void
wc(Text *script, Text *t)
{
intptr_t *p;
rc(script, t);
p = (intptr_t*)(files.s + ((int*)script->w)[-1]);
if(*p != -1)
return;
*(FILE**)p = fopen((char*)(p+1), "w");
if(*p == 0)
syntax("can't open file for writing");
}
/* BRACKETS */
Text brack;
/* Lc() stacks (in brack) the location of the { command word.
Rc() stuffs into that word the offset of the } sequel
relative to the command word.
fixbrack() modifies the offset to be relative to the
beginning of the instruction, including addresses. */
void /* { */
Lc(Text *script, Text *t)
{
while(blank(t));
putint(&brack, script->w - sizeof(int) - script->s);
}
void /* } */
Rc(Text *script, Text *t)
{
int l;
int *p;
t = t;
if(brack.w == 0 || (brack.w-=sizeof(int)) < brack.s)
syntax("unmatched }");
l = *(int*)brack.w;
p = (int*)(script->s + l);
l = script->w - script->s - l;
if(l >= LMASK - 3*sizeof(int)) /* fixbrack could add 3 */
syntax("{command-list} too long)");
*p = (*p&~LMASK) | l;
}
void
fixbrack(Text *script)
{
uchar *p;
int *q;
if(brack.w == 0)
return;
if(brack.w > brack.s)
syntax("unmatched {");
for(p=script->s; p<script->w; p=succi(p)) {
q = instr(p);
if(code(*q) == '{')
*q += (uchar*)q - p;
}
free(brack.s);
}
/* EASY COMMANDS */
void
Xc(Text *script, Text *t) /* # */
{
script = script; /* avoid use/set diagnostics */
if(t->s[1]=='n')
nflag = 1;
while(*t->w != '\n')
t->w++;
}
void
Ic(Text *script, Text *t) /* ignore */
{
script = script;
t->w--;
}
void
Tc(Text *script, Text *t) /* trivial to compile */
{
script = script;
t = t;
}
void
xx(Text *script, Text *t)
{
script = script;
t = t;
syntax("unknown command");
}
/* MISCELLANY */
void
ac(Text *script, Text *t)
{
if(*t->w++ != '\\' || *t->w++ != '\n')
syntax("\\<newline> missing after command");
for(;;) {
while(bflag && blank(t)) ;
assure(script, 2 + sizeof(int));
switch(*t->w) {
case 0:
quit("bug: missed end of <text>");
case '\n':
*script->w++ = *t->w;
*script->w++ = 0;
script->w = (uchar*)intp(script->w);
return;
case '\\':
t->w++;
default:
*script->w++ = *t->w++;
}
}
}
void
yc(Text *script, Text *t)
{
int i;
int delim = *t->w++;
uchar *s = script->w;
uchar *p, *q;
uchar c, d;
if(delim == '\n' || delim=='\\')
syntax("missing delimiter");
assure(script, 256);
for(i=0; i<256; i++)
s[i] = 0;
for(q=t->w; *q!=delim; q++)
if(*q == '\n')
syntax("missing delimiter");
else if(*q=='\\' && q[1]==delim)
q++;
for(p=t->w, q++; *p != delim; p++, q++) {
if(*p=='\\' && p[1]==delim)
p++;
if(*q == '\n')
syntax("missing delimiter");
if(*q == delim)
syntax("string lengths differ");
if(*q=='\\' && q[1]==delim)
q++;
if(s[*p] && s[*p]!=*q)
syntax("ambiguous map");
if(s[*p])
synwarn("redundant map");
s[*p] = *q;
}
if(*q++ != delim)
syntax("string lengths differ");
for(i=0; i<256; i++)
if(s[i] == 0)
s[i] = i;
t->w = q;
script->w += 256;
}
void
sc(Text *script, Text *t)
{
int c, flags, re;
int *q;
int n = -1;
int nsub;
int delim = *t->w++;
switch(delim) {
case '\n':
case '\\':
syntax("improper delimiter");
}
re = recomp(&rebuf, t, delim);
putint(script, re);
nsub = readdr(re)->re_nsub;
flags = script->w - script->s;
putint(script, 0); /* space for flags */
while((c=*t->w++) != delim) {
assure(script, 3+sizeof(int*));
if(c == '\n')
syntax("unterminated command");
else if(c == '\\') {
int d = *t->w;
if(d==delim)
;
else if(d=='&' || d=='\\')
*script->w++ = c;
else if(d>='0' && d<='9') {
if(d > '0'+nsub)
syntax("improper backreference");
*script->w++ = c;
}
c = *t->w++;
}
*script->w++ = c;
}
*script->w++ = 0;
script->w = (uchar*)intp(script->w);
q = (int*)(script->s + flags);
*q = 0;
for(;;) {
switch(*t->w) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
if(n != -1)
syntax("extra flags");
n = number(t);
if(n == 0 || (n&(PFLAG|WFLAG)) != 0)
syntax("count out of range");
continue;
case 'p':
if(*q & PFLAG)
syntax("extra flags");
t->w++;
*q |= PFLAG;
continue;
case 'g':
t->w++;
if(n != -1)
syntax("extra flags");
n = 0;
continue;
case 'w':
t->w++;
*q |= WFLAG;
wc(script, t);
}
break;
}
*q |= n==-1? 1: n;
}
void
synwarn(const char *s)
{
uchar *t = ustrchr(synl, '\n');
warn("%s: %.*s", s, t-synl, synl);
}
void
syntax(const char *s)
{
uchar *t = ustrchr(synl, '\n');
quit("%s: %.*s", s, t-synl, synl);
}
void
printscript(Text *script)
{
/*
uchar *s;
int *q;
for(s=script->s; s<script->w; s = succi(s)) {
q = (int*)s;
if((*q&IMASK) != IMASK) {
if((*q®ADR) == 0)
printf("%d", *q);
else
jprint((regex_t*)(*q & AMASK));
q++;
}
if((*q&IMASK) != IMASK) {
if((*q®ADR) == 0)
printf(",%d", *q);
else
jprint((regex_t*)(*q & AMASK));
q += 2;
}
if(code(*q) == '\n')
continue;
printf("%s%c\n", *q&NEG?"!":"", code(*q));
}
*/
}
/* debugging code 2; execute stub.
prints the compiled script (without arguments)
then each input line with line numbers
void
execute(Text *script, Text *y)
{
if(recno == 1)
printscript(script);
printf("%d:%s",recno,y->s);
}
*/