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
|
/* sed.c - stream editor
*
* Copyright 2014 Rob Landley <rob@landley.net>
*
* See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html
*
* todo "-e blah -f blah -e blah" what order?
* What happens when first address matched, then EOF? How about ",42" or "1,"
* Does $ match last line of file or last line of input
* If file doesn't end with newline
* command preceded by whitespace. whitespace before rw or s///w file
* space before address
* numerical addresses that cross, select one line
* test backslash escapes in regex; share code with printf?
USE_SED(NEWTOY(sed, "e*f*inr", TOYFLAG_USR|TOYFLAG_BIN))
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 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.
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
run only on the specified lines:
[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. 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).
Each COMMAND starts with a single character, which may be followed by
additional data depending on the COMMAND:
rwbrstwy:{
s search and replace
The search and replace syntax
Deviations from posix: we allow extended regular expressions with -r,
editing in place with -i, printf escapes in text, semicolons after.
*/
#define FOR_sed
#include "toys.h"
GLOBALS(
struct arg_list *f;
struct arg_list *e;
void *pattern;
)
static void do_line(char **pline, long len)
{
printf("len=%ld line=%s\n", len, *pline);
}
static void do_lines(int fd, char *name, void (*call)(char **pline, long len))
{
FILE *fp = fdopen(fd, "r");
for (;;) {
char *line = 0;
ssize_t len;
len = getline(&line, (void *)&len, fp);
do_line(&line, len);
free(line);
if (len < 1) break;
}
}
static void do_sed(int fd, char *name)
{
do_lines(fd, name, do_line);
}
void sed_main(void)
{
char **args = toys.optargs;
// Need a pattern
if (!TT.e) {
if (!*toys.optargs) error_exit("no pattern");
(TT.e = xzalloc(sizeof(struct arg_list)))->arg = *(args++);
}
// Inflict pattern upon input files
loopfiles(args, do_sed);
}
|