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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
|
/* sed.c - stream editor. Thing that does s/// and other stuff.
*
* Copyright 2014 Rob Landley <rob@landley.net>
*
* See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html
USE_SED(NEWTOY(sed, "(version)e*f*inr", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_LOCALE))
config SED
bool "sed"
default n
help
usage: sed [-inr] [-e SCRIPT]...|SCRIPT [-f SCRIPT_FILE]... [FILE...]
Stream editor. Apply one or more editing SCRIPTs to each line of input
(from FILE or stdin) producing output (by default to stdout).
-e add SCRIPT to list
-f add contents of SCRIPT_FILE to list
-i Edit each file in place.
-n No default output. (Use the p command to output matched lines.)
-r Use extended regular expression syntax.
-s Treat input files separately (implied by -i)
A SCRIPT is a series of one or more COMMANDs separated by newlines or
semicolons. All -e SCRIPTs are concatenated together as if separated
by newlines, followed by all lines from -f SCRIPT_FILEs, in order.
If no -e or -f SCRIPTs are specified, the first argument is the SCRIPT.
Each COMMAND may be preceded by an address which limits the command to
apply only to the specified line(s). Commands without an address apply to
every line. Addresses are of the form:
[ADDRESS[,ADDRESS]]COMMAND
The ADDRESS may be a decimal line number (starting at 1), a /regular
expression/ within a pair of forward slashes, or the character "$" which
matches the last line of input. (In -s or -i mode this matches the last
line of each file, otherwise just the last line of the last file.) A single
address matches one line, a pair of comma separated addresses match
everything from the first address to the second address (inclusive). If
both addresses are regular expressions, more than one range of lines in
each file can match.
REGULAR EXPRESSIONS in sed are started and ended by the same character
(traditionally / but anything except a backslash or a newline works).
Backslashes may be used to escape the delimiter if it occurs in the
regex, and for the usual printf escapes (\abcefnrtv and octal, hex,
and unicode). An empty regex repeats the previous one. ADDRESS regexes
(above) require the first delimeter to be escaped with a backslash when
it isn't a forward slash (to distinguish it from the COMMANDs below).
Sed mostly operates on individual lines one at a time. It reads each line,
processes it, and either writes it to the output or discards it before
reading the next line. Sed can remember one additional line in a separate
buffer (using the h, H, g, G, and x commands), and can read the next line
of input early (using the n and N command), but other than that command
scripts operate on individual lines of text.
Each COMMAND starts with a single character. The following commands take
no arguments:
{ Start a new command block, continuing until a corresponding "}".
Command blocks may nest. If the block has an address, commands within
the block are only run for lines within the block's address range.
} End command block (this command cannot have an address)
d Delete this line and move on to the next one
(ignores remaining COMMANDs)
D Delete one line of input and restart command SCRIPT (same as "d"
unless you've glued lines together with "N" or similar)
g Get remembered line (overwriting current line)
G Get remembered line (appending to current line)
h Remember this line (overwriting remembered line)
H Remember this line (appending to remembered line, if any)
l Print this line, escaping \abfrtv (but leaving \n as a newline),
using octal escapes for other nonprintable characters, and
wrapping lines to terminal width with a backslash and newline
n Print default output and read next line, replacing current line
(If no next line available, quit processing script)
N Append next line of input to this line, separated by a newline
(This advances the line counter for address matching and "=", if no
next line available quit processing script without default output)
p Print this line
P Print this line up to first newline (from "N")
q Quit (print default output, no more commands processed or lines read)
x Exchange this line with remembered line (overwrite in both directions)
= Print the current line number (followed by a newline)
The following commands (may) take an argument. The "text" arguments (to
the "a", "b", and "c" commands) may end with an unescaped "\" to append
the next line (for which leading whitespace is not skipped), and also
treat ";" as a literal character (use "\;" instead).
a [text] Append text to output before attempting to read next line
b [label] Branch, jumps to :label (or with no label, to end of SCRIPT)
c [text] Delete line, output text at end of matching address range
(ignores remaining COMMANDs)
i [text] Print text
r [file] Append contents of file to output before attempting to read
next line.
s/S/R/F Search for regex S, replace matched text with R using flags F.
The first character after the "s" (anything but newline or
backslash) is the delimiter, escape with \ to use normally.
The replacement text may contain "&" to substitute the matched
text (escape it with backslash for a literal &), or \1 through
\9 to substitute a parenthetical subexpression in the regex.
You can also use the normal backslash escapes such as \n and
a backslash at the end of the line appends the next line.
The flags are:
[0-9] A number, substitute only that occurrence of pattern
g Global, substitute all occurrences of pattern
i Ignore case when matching
p Print the line if match was found and replaced
w [file] Write (append) line to file if match replaced
t [label] Test, jump to :label only if an "s" command found a match in
this line since last test (replacing with same text counts)
T [label] Test false, jump only if "s" hasn't found a match.
w [file] Write (append) line to file
y/old/new/ Change each character in 'old' to corresponding character
in 'new' (with standard backslash escapes, delimiter can be
any repeated character except \ or \n)
: [label] Labeled target for jump commands
# Comment, ignore rest of this line of SCRIPT
Deviations from posix: allow extended regular expressions with -r,
editing in place with -i, separate with -s, printf escapes in text, line
continuations, semicolons after all commands, 2-address anywhere an
address is allowed, "T" command, multiline continuations for [abc],
\; to end [abc] argument before end of line.
*/
#define FOR_sed
#include "toys.h"
GLOBALS(
struct arg_list *f;
struct arg_list *e;
// processed pattern list
struct double_list *pattern;
char *nextline, *remember;
void *restart;
long nextlen, rememberlen, count;
int fdout, noeol;
)
struct step {
struct step *next, *prev;
// Begin and end of each match
long lmatch[2];
int rmatch[2]; // offset to regex_t, because realloc() would confuse pointer
unsigned not, hit, sflags;
int arg1, arg2, arg3; // offset start of to argument (string or regex_t)
char c; // action
};
// Write out line with potential embedded NUL, handling eol/noeol
static int emit(char *line, long len, int eol)
{
if (TT.noeol && !writeall(TT.fdout, "\n", 1)) return 1;
if (eol) line[len++] = '\n';
TT.noeol = len && !eol;
if (len && len != writeall(TT.fdout, line, len)) {
perror_msg("short write");
return 1;
}
return 0;
}
// Do regex matching handling embedded NUL bytes in string. Note that
// neither the pattern nor the match can currently include NUL bytes
// (even with wildcards) and string must be nul terminated.
static int ghostwheel(regex_t *preg, char *string, long len, int nmatch,
regmatch_t pmatch[], int eflags)
{
/*
// todo: this
long start = 0, rc = 0, matches = 0;
for (;;) {
long new = strlen(string+start);
// eflags nobegin noend
rc |= regexec(preg, string+start, nmatch-matches, pmatch+matches, eflags);
if ((start += end + 1) >= len) break;
}
return rc;
*/
return regexec(preg, string, nmatch, pmatch, eflags);
}
// Extend allocation to include new string, with newline between if newlen<0
static char *extend_string(char **old, char *new, int oldlen, int newlen)
{
int newline = newlen < 0;
char *s;
if (newline) newlen = -newlen;
s = *old = xrealloc(*old, oldlen+newlen+newline+1);
if (newline) s[oldlen++] = '\n';
memcpy(s+oldlen, new, newlen);
s[oldlen+newlen] = 0;
return s+oldlen+newlen;
}
// Apply pattern to line from input file
static void walk_pattern(char **pline, long plen)
{
struct append {
struct append *next, *prev;
int file;
char *str;
} *append;
char *line = TT.nextline;
long len = TT.nextlen;
struct step *logrus;
int eol = 0, tea = 0;
// Grab next line for deferred processing (EOF detection: we get a NULL
// pline at EOF to flush last line). Note that only end of _last_ input
// file matches $ (unless we're doing -i).
if (pline) {
TT.nextline = *pline;
TT.nextlen = plen;
*pline = 0;
}
if (!line || !len) return;
if (line[len-1] == '\n') line[--len] = eol++;
TT.count++;
logrus = TT.restart ? TT.restart : (void *)TT.pattern;
TT.restart = 0;
while (logrus) {
char *str, c = logrus->c;
// Have we got a matching range for this rule?
if (*logrus->lmatch || *logrus->rmatch) {
int miss = 0;
long lm;
// In a match that might end?
if (logrus->hit) {
if (!(lm = logrus->lmatch[1])) {
if (!logrus->rmatch[1]) logrus->hit = 0;
else {
void *rm = logrus->rmatch[1] + (char *)logrus;
// regex match end includes matching line, so defer deactivation
if (!ghostwheel(rm, line, len, 0, 0, 0)) miss = 1;
}
} else if (lm > 0 && lm < TT.count) logrus->hit = 0;
// Start a new match?
} else {
if (!(lm = *logrus->lmatch)) {
void *rm = *logrus->rmatch + (char *)logrus;
if (!ghostwheel(rm, line, len, 0, 0, 0)) logrus->hit++;
} else if (lm == TT.count || (lm == -1 && !pline)) logrus->hit++;
}
// Didn't match?
if (!(logrus->hit ^ logrus->not)) {
// Handle skipping curly bracket command group
if (c == '{') {
int curly = 1;
while (curly) {
logrus = logrus->next;
if (logrus->c == '{') curly++;
if (logrus->c == '}') curly--;
}
}
continue;
}
// Deferred disable from regex end match
if (miss) logrus->hit = 0;
}
// Process command
if (c=='a' || c=='r') {
struct append *a = xzalloc(sizeof(struct append));
a->str = logrus->arg1+(char *)logrus;
a->file = c== 'r';
dlist_add_nomalloc((void *)&append, (void *)a);
} else if (c=='b' || c=='t' || c=='T') {
if (c=='b' || tea^(c=='T')) {
str = logrus->arg1+(char *)logrus;
if (!*str) break;
for (logrus = (void *)TT.pattern; logrus; logrus = logrus->next)
if (logrus->c == ':' && !strcmp(logrus->arg1+(char *)logrus, str))
break;
if (!logrus) error_exit("no :%s", str);
}
} else if (c=='c') {
str = logrus->arg1+(char *)logrus;
if (!logrus->hit) emit(str, strlen(str), 1);
goto done;
} else if (c=='d') goto done;
else if (c=='D') {
// Delete up to \n or end of buffer
for (str = line; !*str || *str=='\n'; str++);
len -= str - line;
memmove(line, str, len);
line[len] = 0;
// restart script
logrus = (void *)TT.pattern;
continue;
} else if (c=='g') {
free(line);
line = xstrdup(TT.remember);
len = TT.rememberlen;
} else if (c=='G') {
line = xrealloc(line, len+TT.rememberlen+2);
line[len++] = '\n';
memcpy(line+len, TT.remember, TT.rememberlen);
line[len += TT.rememberlen] = 0;
} else if (c=='h') {
free(TT.remember);
TT.remember = xstrdup(line);
TT.rememberlen = len;
} else if (c=='H') {
TT.remember = xrealloc(TT.remember, TT.rememberlen+len+2);
TT.remember[TT.rememberlen++] = '\n';
memcpy(TT.remember+TT.rememberlen, line, len);
TT.remember[TT.rememberlen += len] = 0;
} else if (c=='i') {
str = logrus->arg1+(char *)logrus;
emit(str, strlen(str), 1);
// } else if (c=='l') {
// error_exit("todo: l");
} else if (c=='n') {
TT.restart = logrus->next;
break;
} else if (c=='N') {
if (pline) {
TT.restart = logrus->next;
extend_string(&line, TT.nextline, plen, -TT.nextlen);
free(TT.nextline);
TT.nextline = line;
line = 0;
}
goto done;
} else if (c=='p') {
if (emit(line, len, eol)) break;
} else if (c=='q') break;
// else if (c=='s') {
// tea = 1;
// }
else if (c=='w') {
int fd = TT.fdout, noeol = TT.noeol;
TT.fdout = logrus->arg2;
TT.noeol = logrus->arg3;
if (emit(line, len, eol))
perror_exit("w '%s'", logrus->arg1+(char *)logrus);
logrus->arg3 = TT.noeol;
TT.noeol = noeol;
TT.fdout = fd;
} else if (c=='x') {
long swap = TT.rememberlen;
str = TT.remember;
TT.remember = line;
line = str;
TT.rememberlen = len;
len = swap;
// } else if (c=='y') {
} else if (c=='=') xprintf("%ld\n", TT.count);
// labcirstTwy
else if (c!=':') error_exit("todo: %c", c);
logrus = logrus->next;
}
if (!(toys.optflags & FLAG_n)) emit(line, len, eol);
done:
free(line);
if (dlist_terminate(append)) while (append) {
struct append *a = append->next;
if (append->file) {
int fd = xopen(append->str, O_RDONLY);
// Force newline if noeol pending
emit(0, 0, 0);
xsendfile(fd, TT.fdout);
close(fd);
} else emit(append->str, strlen(append->str), 1);
free(append);
append = a;
}
}
// Genericish function, can probably get moved to lib.c
// Iterate over lines in file, calling function. Function can write NULL to
// the line pointer if they want to keep it, otherwise line is freed.
// Passed file descriptor is closed at the end of processing.
static void do_lines(int fd, char *name, void (*call)(char **pline, long len))
{
FILE *fp = fd ? xfdopen(fd, "r") : stdin;
for (;;) {
char *line = 0;
ssize_t len;
len = getline(&line, (void *)&len, fp);
if (len > 0) {
call(&line, len);
free(line);
} else break;
}
if (fd) fclose(fp);
}
// Iterate over newline delimited data blob (potentially with embedded NUL),
// call function on each line.
static void chop_lines(char *data, long len, void (*call)(char **p, long l))
{
long ll;
for (ll = 0; ll < len; ll++) {
if (data[ll] == '\n') {
char *c = data;
data[ll] = 0;
call(&c, len);
data[ll++] = '\n';
data += ll;
len -= ll;
ll = -1;
}
}
if (len) call(&data, len);
}
static void do_sed(int fd, char *name)
{
int i = toys.optflags & FLAG_i;
if (i) {
// todo: rename dance
}
do_lines(fd, name, walk_pattern);
if (i) {
walk_pattern(0, 0);
// todo: rename dance
}
}
// Note: removing backslash escapes edits the source string, which could
// be from the environment space via -e, which could screw up what
// "ps" sees, and I'm ok with that.
// extract regex up to delimeter, converting \escapes
// You can't use \ as delimiter because how would you escape anything?
static int parse_regex(regex_t *reg, char **pstr, char delim)
{
char *to, *from = *pstr;
int rc;
if (delim == '\\') rc = 0;
else {
for (to = from; *from != delim; *(to++) = *(from++)) {
if (!*from) break;
if (*from == '\\') {
if (!from[1]) break;
// Check escaped end delimiter before printf style escapes.
if (from[1] == delim) from++;
else {
char c = unescape(from[1]);
if (c) {
*to = c;
from++;
}
}
}
}
rc = (*from == delim);
}
if (rc) {
delim = *to;
*to = 0;
xregcomp(reg, *pstr, ((toys.optflags & FLAG_r)*REG_EXTENDED)|REG_NOSUB);
*to = delim;
}
*pstr = from + rc;
return rc;
}
// Translate primal pattern into walkable form.
static void jewel_of_judgement(char **pline, long len)
{
struct step *corwin = (void *)TT.pattern;
char *line = *pline, *reg, c;
int i;
// Append additional line to pattern argument string?
if (corwin && corwin->prev->hit) {
// Remove half-finished entry from list so remalloc() doesn't confuse it
TT.pattern = TT.pattern->prev;
corwin = dlist_pop(&TT.pattern);
corwin->hit = 0;
c = corwin->c;
reg = (char *)corwin;
reg += corwin->arg1 + strlen(reg + corwin->arg1);
// Resume parsing
goto append;
}
// Loop through commands in line
corwin = 0;
for (;;) {
if (corwin) dlist_add_nomalloc(&TT.pattern, (void *)corwin);
while (isspace(*line) || *line == ';') line++;
if (!*line || *line == '#') return;
memset(toybuf, 0, sizeof(struct step));
corwin = (void *)toybuf;
reg = toybuf + sizeof(struct step);
// Parse address range (if any)
for (i = 0; i < 2; i++) {
if (*line == ',') line++;
else if (i) break;
if (isdigit(*line)) corwin->lmatch[i] = strtol(line, &line, 0);
else if (*line == '$') {
corwin->lmatch[i] = -1;
line++;
} else if (*line == '/' || *line == '\\') {
char delim = *(line++);
if (delim == '\\') {
if (!*line) goto brand;
delim = *(line++);
}
if (!parse_regex((void *)reg, &line, delim)) goto brand;
corwin->rmatch[i] = reg-toybuf;
reg += sizeof(regex_t);
} else break;
}
while (isspace(*line)) line++;
if (!*line) break;
while (*line == '!') corwin->not = 1;
while (isspace(*line)) line++;
c = corwin->c = *(line++);
if (strchr("}:", c) && i) break;
if (strchr("aiqr=", c) && i>1) break;
// Add step to pattern
corwin = xmalloc(reg-toybuf);
memcpy(corwin, toybuf, reg-toybuf);
reg = (reg-toybuf) + (char *)corwin;
// Parse arguments by command type
if (c == '{') TT.nextlen++;
else if (c == '}') {
if (!TT.nextlen--) break;
} else if (c == 's') {
char delim = *line;
int end;
// s/pattern/replacement/flags
if (delim) line++;
else break;
// get pattern
end = reg - (char *)corwin;
corwin = xrealloc(corwin, end+sizeof(regex_t));
reg = end + (char *)corwin;
if (!parse_regex((void *)reg, &line, delim)) break;
corwin->arg1 = reg-toybuf;
reg += sizeof(regex_t);
// get replacement
for (end = 0; line[end] != delim; end++) {
if (line[end] == '\\') end++;
if (!line[end]) goto brand;
}
corwin->arg2 = reg - (char*)corwin;
reg = extend_string((void *)&corwin, line, corwin->arg2, end);
line += end+1;
// parse s///flags
for (;;) {
long l;
line++;
if (!*line || *line == ';') break;
if (isspace(*line)) continue;
if (0 <= (l = stridx("gpi", *line))) corwin->sflags |= 1<<l;
else if (!corwin->sflags >> 4 && 0<(l = strtol(line+end, &line, 10))) {
corwin->sflags |= l << 4;
line--;
} else if (*line == 'w') {
while (isspace(*++line));
if (!*line) goto brand;
corwin->arg3 = reg - (char *)corwin;
reg = extend_string((void *)&corwin, line, corwin->arg3,
strlen(line));
} else goto brand;
}
} else if (c == 'y') {
// y/old/new/
} else if (strchr("abcirtTw:", c)) {
int end, class;
// Trim whitespace from "b ;" and ": blah " but only first space in "w x "
while (isspace(*line)) line++;
append:
class = !strchr("btT:", c);
end = strcspn(line, class ? "" : "; \t\r\n\v\f");
if (!end) {
if (!strchr("btT", c)) break;
continue;
}
// Extend allocation to include new string. We use offsets instead of
// pointers so realloc() moving stuff doesn't break things. Do it
// here instead of toybuf so there's no maximum size.
if (!corwin->arg1) corwin->arg1 = reg - (char*)corwin;
reg = extend_string((void *)&corwin, line, reg - (char *)corwin, end);
// Line continuation?
if (class && reg[-1] == '\\') {
reg[-1] = 0;
corwin->hit++;
}
// Commands that take no arguments
} else if (!strchr("{dDgGhHlnNpPqx=", *line)) break;
}
brand:
// Reminisce about chestnut trees.
error_exit("bad pattern '%s'@%ld (%c)", *pline, line-*pline+1, *line);
}
void sed_main(void)
{
struct arg_list *dworkin;
char **args = toys.optargs;
// Lie to autoconf when it asks stupid questions, so configure regexes
// that look for "GNU sed version %f" greater than some old buggy number
// don't fail us for not matching their narrow expectations.
if (toys.optflags & FLAG_version) {
xprintf("This is not GNU sed version 9.0\n");
return;
}
// Need a pattern. If no unicorns about, fight serpent and take its eye.
if (!TT.e && !TT.f) {
if (!*toys.optargs) error_exit("no pattern");
(TT.e = xzalloc(sizeof(struct arg_list)))->arg = *(args++);
}
// Option parsing infrastructure can't interlace "-e blah -f blah -e blah"
// so handle all -e, then all -f. (At least the behavior's consistent.)
for (dworkin = TT.e; dworkin; dworkin = dworkin->next)
chop_lines(dworkin->arg, strlen(dworkin->arg), jewel_of_judgement);
for (dworkin = TT.f; dworkin; dworkin = dworkin->next)
do_lines(xopen(dworkin->arg, O_RDONLY), dworkin->arg, jewel_of_judgement);
dlist_terminate(TT.pattern);
if (TT.nextlen) error_exit("no }");
TT.fdout = 1;
TT.remember = xstrdup("");
// Inflict pattern upon input files
loopfiles_rw(args, O_RDONLY, 0, 0, do_sed);
if (!(toys.optflags & FLAG_i)) walk_pattern(0, 0);
}
|