annotate toys/pending/sed.c @ 1538:8bc715741481 draft

Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
author Rob Landley <rob@landley.net>
date Wed, 29 Oct 2014 14:49:47 -0500
parents a6ef79b31829
children ad6b2f0e566b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1537
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
1 /* sed.c - stream editor. Thing that does s/// and other stuff.
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 231
diff changeset
2 *
1520
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
3 * Copyright 2014 Rob Landley <rob@landley.net>
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
4 *
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 231
diff changeset
6 *
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
7 * todo "-e blah -f blah -e blah" what order? (All -e, then all -f.)
1520
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
8 * What happens when first address matched, then EOF? How about ",42" or "1,"
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
9 * Does $ match last line of file or last line of input
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
10 * If file doesn't end with newline
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
11 * command preceded by whitespace. whitespace before rw or s///w file
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
12 * space before address
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
13 * numerical addresses that cross, select one line
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
14 * test backslash escapes in regex; share code with printf?
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
15 * address counts lines cumulatively across files
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
16 * Why can't I start an address with \\ (posix says no, but _why_?)
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
17 * Fun with \nblah\nn vs \tblah\tt
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
18 *
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
19 * echo -e "one\ntwo\nthree" | sed -n '$,$p'
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 231
diff changeset
20
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
21 USE_SED(NEWTOY(sed, "(version)e*f*inr", TOYFLAG_USR|TOYFLAG_BIN))
234
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 233
diff changeset
22
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 231
diff changeset
23 config SED
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
24 bool "sed"
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
25 default n
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
26 help
1520
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
27 usage: sed [-inr] [-e SCRIPT]...|SCRIPT [-f SCRIPT_FILE]... [FILE...]
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
28
1537
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
29 Stream editor. Apply one or more editing SCRIPTs to each line of input
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
30 (from FILE or stdin) producing output (by default to stdout).
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 231
diff changeset
31
1520
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
32 -e add SCRIPT to list
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
33 -f add contents of SCRIPT_FILE to list
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
34 -i Edit each file in place.
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
35 -n No default output. (Use the p command to output matched lines.)
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
36 -r Use extended regular expression syntax.
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
37 -s Treat input files separately (implied by -i)
1520
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
38
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
39 A SCRIPT is a series of one or more COMMANDs separated by newlines or
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
40 semicolons. All -e SCRIPTs are concatenated together as if separated
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
41 by newlines, followed by all lines from -f SCRIPT_FILEs, in order.
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
42 If no -e or -f SCRIPTs are specified, the first argument is the SCRIPT.
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
43
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
44 Each COMMAND may be preceded by an address which limits the command to
1537
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
45 apply only to the specified line(s). Commands without an address apply to
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
46 every line. Addresses are of the form:
1520
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
47
1537
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
48 [ADDRESS[,ADDRESS]]COMMAND
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
49
1520
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
50 The ADDRESS may be a decimal line number (starting at 1), a /regular
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
51 expression/ within a pair of forward slashes, or the character "$" which
1537
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
52 matches the last line of input. (In -s or -i mode this matches the last
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
53 line of each file, otherwise just the last line of the last file.) A single
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
54 address matches one line, a pair of comma separated addresses match
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
55 everything from the first address to the second address (inclusive). If
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
56 both addresses are regular expressions, more than one range of lines in
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
57 each file can match.
1520
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
58
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
59 REGULAR EXPRESSIONS in sed are started and ended by the same character
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
60 (traditionally / but anything except a backslash or a newline works).
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
61 Backslashes may be used to escape the delimiter if it occurs in the
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
62 regex, and for the usual printf escapes (\abcefnrtv and octal, hex,
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
63 and unicode). An empty regex repeats the previous one. ADDRESS regexes
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
64 (above) require the first delimeter to be escaped with a backslash when
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
65 it isn't a forward slash (to distinguish it from the COMMANDs below).
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
66
1537
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
67 Sed mostly operates on individual lines one at a time. It reads each line,
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
68 processes it, and either writes it to the output or discards it before
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
69 reading the next line. Sed can remember one additional line in a separate
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
70 buffer (using the h, H, g, G, and x commands), and can read the next line
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
71 of input early (using the n and N command), but other than that command
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
72 scripts operate on individual lines of text.
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
73
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
74 Each COMMAND starts with a single character. The following commands take
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
75 no arguments:
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
76
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
77 { Start a new command block, continuing until a corresponding "}".
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
78 Command blocks may nest. If the block has an address, commands within
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
79 the block are only run for lines within the block's address range.
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
80
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
81 } End command block (this command cannot have an address)
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
82
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
83 d Delete this line and move on to the next one
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
84 (ignores remaining COMMANDs)
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
85
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
86 D Delete one line of input and restart command SCRIPT (same as "d"
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
87 unless you've glued lines together with "N" or similar)
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
88
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
89 g Get remembered line (overwriting current line)
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
90
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
91 G Get remembered line (appending to current line)
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
92
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
93 h Remember this line (overwriting remembered line)
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
94
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
95 H Remember this line (appending to remembered line, if any)
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
96
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
97 l Print this line, escaping \abfrtv (but leaving \n as a newline),
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
98 using octal escapes for other nonprintable characters, and
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
99 wrapping lines to terminal width with a backslash and newline
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
100
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
101 n Print default output and read next line, replacing current line
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
102 (If no next line available, quit processing script)
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
103
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
104 N Append next line of input to this line, separated by a newline
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
105 (This advances the line counter for address matching and "=", if no
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
106 next line available quit processing script without default output)
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
107
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
108 p Print this line
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
109
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
110 P Print this line up to first newline (from "N")
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
111
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
112 q Quit (print default output, no more commands processed or lines read)
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
113
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
114 x Exchange this line with remembered line (overwrite in both directions)
1520
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
115
1537
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
116 = Print the current line number (followed by a newline)
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
117
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
118 The following commands (may) take an argument. ("b", "s", "t", "T", "y"
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
119 and ":" may be ended with semicolons, the rest eat at least one line.)
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
120
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
121 a [text] Append text to output before attempting to read next line,
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
122 if text ends with unescaped "\" append next line of script
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
123
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
124 b [label] Branch, jumps to :label (or with no label, to end of SCRIPT)
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
125
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
126 c [text] Delete current address range and print text instead,
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
127 if text ends with unescaped "\" append next line of script
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
128
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
129 i [text] Print text, if text ends with unescaped "\" append next
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
130 line of script
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
131
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
132 r [file] Append contents of file to output before attempting to read
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
133 next line.
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
134
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
135 s/S/R/F Search for regex S, replace matched text with R using flags F.
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
136 The first character after the "s" (anything but newline or
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
137 backslash) is the delimiter, escape with \ to use normally.
1520
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
138
1537
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
139 The replacement text may contain "&" to substitute the matched
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
140 text (escape it with backslash for a literal &), or \1 through
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
141 \9 to substitute a parenthetical subexpression in the regex.
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
142 You can also use the normal backslash escapes such as \n and
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
143 a backslash at the end of the line appends the next line.
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
144
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
145 The flags are:
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
146
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
147 [0-9] A number, substitute only that occurrence of pattern
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
148 g Global, substitute all occurrences of pattern
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
149 p Print the line if match was found and replaced
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
150 w [file] Write (append) line to file if match replaced
1520
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
151
1537
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
152 t [label] Test, jump to :label only if an "s" command found a match in
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
153 this line since last test (replacing with same text counts)
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
154
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
155 T [label] Test false, jump only if "s" hasn't found a match.
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
156
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
157 w [file] Write (append) line to file
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
158
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
159 y/old/new/ Change each character in 'old' to corresponding character
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
160 in 'new' (with standard backslash escapes, delimiter can be
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
161 any repeated character except \ or \n)
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
162
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
163 : [label] Labeled target for jump commands
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
164
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
165 # Comment, ignore rest of this line of SCRIPT
1520
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
166
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
167 Deviations from posix: we allow extended regular expressions with -r,
1537
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
168 editing in place with -i, separate with -s, printf escapes in text, line
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
169 continuations, semicolons after all commands, 2-address anywhere an
a6ef79b31829 Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents: 1532
diff changeset
170 address is allowed, "T" command.
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 231
diff changeset
171 */
231
31dc682c18ad Very early stub of sed, does nothing yet.
Rob Landley <rob@landley.net>
parents:
diff changeset
172
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
173 #define FOR_sed
231
31dc682c18ad Very early stub of sed, does nothing yet.
Rob Landley <rob@landley.net>
parents:
diff changeset
174 #include "toys.h"
31dc682c18ad Very early stub of sed, does nothing yet.
Rob Landley <rob@landley.net>
parents:
diff changeset
175
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
176 GLOBALS(
1520
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
177 struct arg_list *f;
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
178 struct arg_list *e;
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
179
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
180 // processed pattern list
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
181 struct double_list *pattern;
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
182
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
183 char *nextline;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
184 long nextlen, count;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
185 int fdout, noeol;
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
186 )
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
187
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
188 struct step {
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
189 struct step *next, *prev;
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
190
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
191 // Begin and end of each match
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
192 long lmatch[2];
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
193 int rmatch[2]; // offset to regex_t, because realloc() would confuse pointer
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
194 int not, hit;
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
195
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
196 // action
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
197 char c;
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
198 int arg1, arg2; // offset to argument
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
199 };
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
200
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
201 // Write out line with potential embedded NUL, handling eol/noeol
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
202 static int emit(char *line, long len, int eol)
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
203 {
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
204 if (TT.noeol && !writeall(TT.fdout, "\n", 1)) return 1;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
205 if (eol) line[len++] = '\n';
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
206 TT.noeol = !eol;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
207 if (len != writeall(TT.fdout, line, len)) {
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
208 perror_msg("short write");
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
209
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
210 return 1;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
211 }
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
212
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
213 return 0;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
214 }
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
215
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
216 // Do regex matching handling embedded NUL bytes in string. Note that
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
217 // neither the pattern nor the match can currently include NUL bytes
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
218 // (even with wildcards) and string must be nul terminated.
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
219 static int ghostwheel(regex_t *preg, char *string, long len, int nmatch,
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
220 regmatch_t pmatch[], int eflags)
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
221 {
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
222 /*
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
223 // todo: this
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
224 long start = 0, rc = 0, matches = 0;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
225
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
226 for (;;) {
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
227 long new = strlen(string+start);
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
228
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
229 // eflags nobegin noend
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
230 rc |= regexec(preg, string+start, nmatch-matches, pmatch+matches, eflags);
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
231 if ((start += end + 1) >= len) break;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
232 }
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
233
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
234 return rc;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
235 */
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
236 return regexec(preg, string, nmatch, pmatch, eflags);
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
237
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
238 }
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
239
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
240 // Apply pattern to line from input file
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
241 static void walk_pattern(char **pline, long plen)
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
242 {
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
243 char *line = TT.nextline;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
244 long len = TT.nextlen;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
245 struct step *logrus;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
246 int eol = 0;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
247
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
248 // Grab next line for deferred processing (EOF detection, we get a NULL
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
249 // pline at EOF to flush last line). Note that only end of _last_ input
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
250 // file matches $ (unless we're doing -i).
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
251 if (pline) {
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
252 TT.nextline = *pline;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
253 TT.nextlen = plen;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
254 *pline = 0;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
255 }
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
256
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
257 if (!line || !len) return;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
258 if (line[len-1] == '\n') line[--len] = eol++;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
259 TT.count++;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
260
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
261 for (logrus = (void *)TT.pattern; logrus; logrus = logrus->next) {
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
262 char c = logrus->c;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
263
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
264 // Have we got a matching range for this rule?
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
265 if (*logrus->lmatch || *logrus->rmatch) {
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
266 int miss = 0;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
267 long lm;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
268
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
269 // In a match that might end?
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
270 if (logrus->hit) {
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
271 if (!(lm = logrus->lmatch[1])) {
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
272 if (!logrus->rmatch[1]) logrus->hit = 0;
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
273 else {
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
274 void *rm = logrus->rmatch[1] + (char *)logrus;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
275
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
276 // regex match end includes matching line, so defer deactivation
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
277 if (!ghostwheel(rm, line, len, 0, 0, 0)) miss = 1;
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
278 }
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
279 } else if (lm > 0 && lm < TT.count) logrus->hit = 0;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
280
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
281 // Start a new match?
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
282 } else {
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
283 if (!(lm = *logrus->lmatch)) {
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
284 void *rm = *logrus->rmatch + (char *)logrus;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
285
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
286 if (!ghostwheel(rm, line, len, 0, 0, 0)) logrus->hit++;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
287 } else if (lm == TT.count || (lm == -1 && !pline)) logrus->hit++;
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
288 }
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
289
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
290 // Didn't match?
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
291 if (!(logrus->hit ^ logrus->not)) {
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
292
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
293 // Handle skipping curly bracket command group
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
294 if (c == '{') {
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
295 int curly = 1;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
296
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
297 while (curly) {
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
298 logrus = logrus->next;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
299 if (logrus->c == '{') curly++;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
300 if (logrus->c == '}') curly--;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
301 }
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
302 }
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
303 continue;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
304 }
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
305 // Deferred disable from regex end match
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
306 if (miss) logrus->hit = 0;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
307 }
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
308
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
309 // Process like the wind, bullseye!
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
310
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
311 // if (strchr("dDgGhHlnNpPqx=", c)) {
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
312
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
313
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
314 // if (strchr("abcirstTwy:", c)
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
315 if (c == 'p') {
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
316 if (emit(line, len, eol)) break;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
317 } else error_exit("what?");
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
318 }
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
319
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
320 if (!(toys.optflags & FLAG_n)) emit(line, len, eol);
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
321
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
322 free(line);
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
323 }
231
31dc682c18ad Very early stub of sed, does nothing yet.
Rob Landley <rob@landley.net>
parents:
diff changeset
324
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
325 // Genericish function, can probably get moved to lib.c
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
326
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
327 // Iterate over lines in file, calling function. Function can write NULL to
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
328 // the line pointer if they want to keep it, otherwise line is freed.
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
329 // Passed file descriptor is closed at the end of processing.
1520
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
330 static void do_lines(int fd, char *name, void (*call)(char **pline, long len))
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
331 {
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
332 FILE *fp = xfdopen(fd, "r");
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
333
1520
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
334 for (;;) {
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
335 char *line = 0;
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
336 ssize_t len;
231
31dc682c18ad Very early stub of sed, does nothing yet.
Rob Landley <rob@landley.net>
parents:
diff changeset
337
1520
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
338 len = getline(&line, (void *)&len, fp);
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
339 if (len > 0) {
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
340 call(&line, len);
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
341 free(line);
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
342 } else break;
1520
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
343 }
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
344 fclose(fp);
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
345 }
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
346
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
347 // Iterate over newline delimited data blob (potentially with embedded NUL),
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
348 // call function on each line.
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
349 static void chop_lines(char *data, long len, void (*call)(char **p, long l))
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
350 {
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
351 long ll;
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
352
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
353 for (ll = 0; ll < len; ll++) {
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
354 if (data[ll] == '\n') {
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
355 char *c = data;
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
356
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
357 data[ll] = 0;
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
358 call(&c, len);
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
359 data[ll++] = '\n';
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
360 data += ll;
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
361 len -= ll;
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
362 ll = -1;
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
363 }
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
364 }
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
365 if (len) call(&data, len);
1520
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
366 }
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
367
1520
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
368 static void do_sed(int fd, char *name)
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
369 {
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
370 int i = toys.optflags & FLAG_i;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
371
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
372 if (i) {
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
373 // todo: rename dance
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
374 }
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
375 do_lines(fd, name, walk_pattern);
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
376 if (i) {
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
377 walk_pattern(0, 0);
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
378
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
379 // todo: rename dance
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
380 }
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
381 }
231
31dc682c18ad Very early stub of sed, does nothing yet.
Rob Landley <rob@landley.net>
parents:
diff changeset
382
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
383 // Translate primal pattern into walkable form.
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
384 static void jewel_of_judgement(char **pline, long len)
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
385 {
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
386 struct step *corwin = (void *)TT.pattern;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
387 char *line = *pline, *reg, c;
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
388 int i;
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
389
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
390 // Append additional line to pattern argument string?
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
391 if (corwin && corwin->prev->hit) {
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
392 corwin = corwin->prev;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
393 corwin->hit = 0;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
394 c = corwin->c;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
395 reg = (char *)corwin;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
396 reg += corwin->arg1 + strlen(reg + corwin->arg1);
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
397
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
398 goto append;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
399 }
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
400
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
401 // Loop through characters in line
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
402 for (;;) {
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
403 while (isspace(*line) || *line == ';') line++;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
404 if (!*line || *line == '#') return;
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
405
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
406 memset(toybuf, 0, sizeof(struct step));
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
407 corwin = (void *)toybuf;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
408 reg = toybuf + sizeof(struct step);
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
409
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
410 // Parse address range (if any)
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
411 for (i = 0; i < 2; i++) {
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
412 if (*line == ',') line++;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
413 else if (i) break;
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
414
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
415 if (isdigit(*line)) corwin->lmatch[i] = strtol(line, &line, 0);
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
416 else if (*line == '$') {
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
417 corwin->lmatch[i] = -1;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
418 line++;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
419 } else if (*line == '/' || *line == '\\') {
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
420 char delim = *(line++), slash = 0, *to, *from;
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
421
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
422 if (delim == '\\') {
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
423 if (!*line) goto brand;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
424 slash = delim = *(line++);
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
425 }
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
426
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
427 // Removing backslash escapes edits the source string, which could
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
428 // be from the environment space via -e, which could screw up what
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
429 // "ps" sees, and I'm ok with that.
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
430 for (to = from = line; *from != delim; *(to++) = *(from++)) {
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
431 if (!*from) goto brand;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
432 if (*from == '\\') {
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
433 if (!from[1]) goto brand;
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
434
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
435 // Check escaped end delimiter before printf style escapes.
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
436 if (from[1] == slash) from++;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
437 else {
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
438 char c = unescape(from[1]);
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
439
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
440 if (c) {
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
441 *to = c;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
442 from++;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
443 }
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
444 }
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
445 }
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
446 }
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
447 slash = *to;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
448 *to = 0;
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
449 xregcomp((void *)reg, line,
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
450 ((toys.optflags & FLAG_r)*REG_EXTENDED)|REG_NOSUB);
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
451 *to = slash;
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
452 corwin->rmatch[i] = reg-toybuf;
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
453 reg += sizeof(regex_t);
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
454 line = from + 1;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
455 } else break;
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
456 }
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
457
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
458 while (isspace(*line)) line++;
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
459 if (!*line) break;
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
460
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
461 while (*line == '!') corwin->not = 1;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
462 while (isspace(*line)) line++;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
463
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
464 c = corwin->c = *(line++);
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
465 if (strchr("}:", c) && i) break;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
466 if (strchr("aiqr=", c) && i>1) break;
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
467
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
468 // Add step to pattern
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
469 corwin = xmalloc(reg-toybuf);
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
470 memcpy(corwin, toybuf, reg-toybuf);
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
471 dlist_add_nomalloc(&TT.pattern, (void *)corwin);
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
472 reg = (toybuf-reg) + (char *)corwin;
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
473
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
474 // Parse arguments by command type
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
475 if (c == '{') TT.nextlen++;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
476 else if (c == '}') {
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
477 if (!TT.nextlen--) break;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
478 } else if (c == 's') {
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
479 // s/S/R/F
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
480 // [0-9] A number, substitute only that occurrence of pattern
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
481 // g Global, substitute all occurrences of pattern
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
482 // p Print the line if match was found and replaced
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
483 // w [file] Write (append) line to file if match replaced
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
484
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
485 } else if (c == 'y') {
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
486 // y/old/new/
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
487 } else if (strchr("abcirtTw:", c)) {
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
488 char *end, *class;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
489 int len;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
490
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
491 // Trim whitespace from "b ;" and ": blah " but only first space in "w x "
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
492
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
493 while (isspace(*line)) line++;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
494 append:
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
495 class = strchr("btT:", c);
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
496 end = line + strcspn(line, class ? "; \t\r\n\v\f" : "");
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
497
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
498 if (end == line) {
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
499 if (!strchr("btT", c)) break;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
500 continue;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
501 }
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
502
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
503 // Extend allocation to include new string. We use offsets instead of
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
504 // pointers so realloc() moving stuff doesn't break things. Do it
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
505 // here instead of toybuf so there's no maximum size.
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
506 len = reg - (char *)corwin;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
507 corwin = xrealloc(corwin, len+(end-line)+1);
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
508 reg = len + (char *)corwin;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
509 if (!corwin->arg1) corwin->arg1 = len;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
510 len = end-line;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
511 memcpy(reg, line, len);
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
512 reg[len] = 0;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
513
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
514 // Line continuation?
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
515 if (!class && reg[--len] == '\\') {
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
516 reg[len] = 0;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
517 corwin->hit++;
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
518 }
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
519
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
520 // Commands that take no arguments
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
521 } else if (!strchr("{dDgGhHlnNpPqx=", *line)) break;
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
522 }
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
523
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
524 brand:
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
525 // Reminisce about chestnut trees.
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
526 error_exit("bad pattern '%s'@%ld (%c)", *pline, line-*pline, *line);
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
527 }
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
528
231
31dc682c18ad Very early stub of sed, does nothing yet.
Rob Landley <rob@landley.net>
parents:
diff changeset
529 void sed_main(void)
31dc682c18ad Very early stub of sed, does nothing yet.
Rob Landley <rob@landley.net>
parents:
diff changeset
530 {
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
531 struct arg_list *dworkin;
1520
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
532 char **args = toys.optargs;
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
533
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
534 // Lie to autoconf when it asks stupid questions, so configure regexes
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
535 // that look for "GNU sed version %f" greater than some old buggy number
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
536 // don't fail us for not matching their narrow expectations.
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
537 if (toys.optflags & FLAG_version) {
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
538 xprintf("This is not GNU sed version 9.0\n");
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
539 return;
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
540 }
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
541
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
542 // Need a pattern. If no unicorns about, fight serpent and take its eye.
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
543 if (!TT.e && !TT.f) {
1520
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
544 if (!*toys.optargs) error_exit("no pattern");
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
545 (TT.e = xzalloc(sizeof(struct arg_list)))->arg = *(args++);
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
546 }
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
547 for (dworkin = TT.e; dworkin; dworkin = dworkin->next)
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
548 chop_lines(dworkin->arg, strlen(dworkin->arg), jewel_of_judgement);
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
549 for (dworkin = TT.f; dworkin; dworkin = dworkin->next)
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
550 do_lines(xopen(dworkin->arg, O_RDONLY), dworkin->arg, jewel_of_judgement);
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
551 dlist_terminate(TT.pattern);
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
552 if (TT.nextlen) error_exit("no }");
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
553
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
554 TT.fdout = 1;
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
555
1520
dfd6b3404c16 Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents: 1235
diff changeset
556 // Inflict pattern upon input files
1530
3eafa445c1a6 Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents: 1520
diff changeset
557 loopfiles_rw(args, O_RDONLY, 0, 0, do_sed);
1532
bf2c5216d726 Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents: 1530
diff changeset
558
1538
8bc715741481 Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents: 1537
diff changeset
559 if (!(toys.optflags & FLAG_i)) walk_pattern(0, 0);
231
31dc682c18ad Very early stub of sed, does nothing yet.
Rob Landley <rob@landley.net>
parents:
diff changeset
560 }