Mercurial > hg > toybox
annotate toys/pending/sed.c @ 1556:e7944f8fdcbb draft
Debugging pass on sed: make the existing test suite pass.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 15 Nov 2014 16:16:29 -0600 |
parents | 2423dd884182 |
children | 6e720e1e833c |
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 |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
7 USE_SED(NEWTOY(sed, "(version)e*f*inr", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_LOCALE)) |
234
163498bf547b
Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents:
233
diff
changeset
|
8 |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
231
diff
changeset
|
9 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
|
10 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
|
11 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
|
12 help |
1520
dfd6b3404c16
Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents:
1235
diff
changeset
|
13 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
|
14 |
1537
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
15 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
|
16 (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
|
17 |
1520
dfd6b3404c16
Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents:
1235
diff
changeset
|
18 -e add SCRIPT to list |
dfd6b3404c16
Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents:
1235
diff
changeset
|
19 -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
|
20 -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
|
21 -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
|
22 -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
|
23 -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
|
24 |
dfd6b3404c16
Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents:
1235
diff
changeset
|
25 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
|
26 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
|
27 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
|
28 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
|
29 |
dfd6b3404c16
Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents:
1235
diff
changeset
|
30 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
|
31 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
|
32 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
|
33 |
1537
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
34 [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
|
35 |
1520
dfd6b3404c16
Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents:
1235
diff
changeset
|
36 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
|
37 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
|
38 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
|
39 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
|
40 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
|
41 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
|
42 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
|
43 each file can match. |
1520
dfd6b3404c16
Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents:
1235
diff
changeset
|
44 |
dfd6b3404c16
Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents:
1235
diff
changeset
|
45 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
|
46 (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
|
47 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
|
48 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
|
49 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
|
50 (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
|
51 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
|
52 |
1537
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
53 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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
60 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
|
61 no arguments: |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
62 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
63 { 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
|
64 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
|
65 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
|
66 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
67 } 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
|
68 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
69 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
|
70 (ignores remaining COMMANDs) |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
71 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
72 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
|
73 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
|
74 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
75 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
|
76 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
77 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
|
78 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
79 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
|
80 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
81 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
|
82 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
83 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
|
84 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
|
85 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
|
86 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
87 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
|
88 (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
|
89 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
90 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
|
91 (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
|
92 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
|
93 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
94 p Print this line |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
95 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
96 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
|
97 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
98 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
|
99 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
100 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
|
101 |
1537
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
102 = 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
|
103 |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
104 The following commands (may) take an argument. The "text" arguments (to |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
105 the "a", "b", and "c" commands) may end with an unescaped "\" to append |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
106 the next line (for which leading whitespace is not skipped), and also |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
107 treat ";" as a literal character (use "\;" instead). |
1537
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
108 |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
109 a [text] Append text to output before attempting to read next line |
1537
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
110 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
111 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
|
112 |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
113 c [text] Delete line, output text at end of matching address range |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
114 (ignores remaining COMMANDs) |
1537
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
115 |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
116 i [text] Print text |
1537
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 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
|
119 next 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 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
|
122 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
|
123 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
|
124 |
1537
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
125 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
|
126 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
|
127 \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
|
128 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
|
129 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
|
130 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
131 The flags are: |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
132 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
133 [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
|
134 g Global, substitute all occurrences of pattern |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
135 i Ignore case when matching |
1537
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
136 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
|
137 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
|
138 |
1537
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
139 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
|
140 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
|
141 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
142 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
|
143 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
144 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
|
145 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
146 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
|
147 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
|
148 any repeated character except \ or \n) |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
149 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
150 : [label] Labeled target for jump commands |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
151 |
a6ef79b31829
Fill out rest of help text for sed.
Rob Landley <rob@landley.net>
parents:
1532
diff
changeset
|
152 # 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
|
153 |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
154 Deviations from posix: 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
|
155 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
|
156 continuations, semicolons after all commands, 2-address anywhere an |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
157 address is allowed, "T" command, multiline continuations for [abc], |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
158 \; to end [abc] argument before end of line. |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
231
diff
changeset
|
159 */ |
231
31dc682c18ad
Very early stub of sed, does nothing yet.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
160 |
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
|
161 #define FOR_sed |
231
31dc682c18ad
Very early stub of sed, does nothing yet.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
162 #include "toys.h" |
31dc682c18ad
Very early stub of sed, does nothing yet.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
163 |
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
|
164 GLOBALS( |
1520
dfd6b3404c16
Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents:
1235
diff
changeset
|
165 struct arg_list *f; |
dfd6b3404c16
Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents:
1235
diff
changeset
|
166 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
|
167 |
1530
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
168 // processed pattern list |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
169 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
|
170 |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
171 char *nextline, *remember; |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
172 void *restart; |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
173 long nextlen, rememberlen, count; |
1532
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
174 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
|
175 ) |
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
176 |
1530
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
177 struct step { |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
178 struct step *next, *prev; |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
179 |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
180 // 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
|
181 long lmatch[2]; |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
182 int rmatch[2], arg1, arg2, w; // offsets because remalloc() |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
183 unsigned not, hit, sflags; |
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
184 char c; // action |
1530
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
185 }; |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
186 |
1532
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
187 // 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
|
188 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
|
189 { |
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
190 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
|
191 if (eol) line[len++] = '\n'; |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
192 TT.noeol = len && !eol; |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
193 if (len && len != writeall(TT.fdout, line, len)) { |
1532
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
194 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
|
195 |
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
196 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
|
197 } |
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
198 |
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
199 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
|
200 } |
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
201 |
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
|
202 // 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
|
203 // 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
|
204 // (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
|
205 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
|
206 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
|
207 { |
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
|
208 /* |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
209 while (len && !*string) { |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
210 string++; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
211 len--; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
212 int l = strlen(string); |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
213 if (len != strlen(string)) |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
214 |
1532
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
215 // 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
|
216 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
|
217 |
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 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
|
219 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
|
220 |
8bc715741481
Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents:
1537
diff
changeset
|
221 // 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
|
222 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
|
223 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
|
224 } |
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 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
|
227 */ |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
228 |
1532
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
229 return regexec(preg, string, nmatch, pmatch, eflags); |
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
230 } |
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
231 |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
232 // Extend allocation to include new string, with newline between if newlen<0 |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
233 |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
234 static char *extend_string(char **old, char *new, int oldlen, int newlen) |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
235 { |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
236 int newline = newlen < 0; |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
237 char *s; |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
238 |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
239 if (newline) newlen = -newlen; |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
240 s = *old = xrealloc(*old, oldlen+newlen+newline+1); |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
241 if (newline) s[oldlen++] = '\n'; |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
242 memcpy(s+oldlen, new, newlen); |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
243 s[oldlen+newlen] = 0; |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
244 |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
245 return s+oldlen+newlen; |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
246 } |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
247 |
1530
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
248 // 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
|
249 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
|
250 { |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
251 struct append { |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
252 struct append *next, *prev; |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
253 int file; |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
254 char *str; |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
255 } *append = 0; |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
256 char *line = TT.nextline; |
1532
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
257 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
|
258 struct step *logrus; |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
259 int eol = 0, tea = 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
|
260 |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
261 // Grab next line for deferred processing (EOF detection: we get a NULL |
1532
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
262 // 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
|
263 // 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
|
264 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
|
265 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
|
266 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
|
267 *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
|
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 |
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 (!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
|
271 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
|
272 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
|
273 |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
274 logrus = TT.restart ? TT.restart : (void *)TT.pattern; |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
275 TT.restart = 0; |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
276 |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
277 while (logrus) { |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
278 char *str, c = logrus->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
|
279 |
1556
e7944f8fdcbb
Debugging pass on sed: make the existing test suite pass.
Rob Landley <rob@landley.net>
parents:
1553
diff
changeset
|
280 // Have we got a line or regex 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
|
281 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
|
282 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
|
283 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
|
284 |
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
285 // 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
|
286 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
|
287 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
|
288 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
|
289 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
|
290 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
|
291 |
1532
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
292 // 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
|
293 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
|
294 } |
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
295 } 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
|
296 |
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
297 // 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
|
298 } else { |
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
299 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
|
300 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
|
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 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
|
303 } 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
|
304 } |
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
305 |
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
|
306 // 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
|
307 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
|
308 |
8bc715741481
Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents:
1537
diff
changeset
|
309 // 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
|
310 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
|
311 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
|
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 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
|
314 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
|
315 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
|
316 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
|
317 } |
8bc715741481
Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents:
1537
diff
changeset
|
318 } |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
319 logrus = logrus->next; |
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
|
320 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
|
321 } |
8bc715741481
Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents:
1537
diff
changeset
|
322 // 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
|
323 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
|
324 } |
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
325 |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
326 // Process command |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
327 |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
328 if (c=='a' || c=='r') { |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
329 struct append *a = xzalloc(sizeof(struct append)); |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
330 a->str = logrus->arg1+(char *)logrus; |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
331 a->file = c== 'r'; |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
332 dlist_add_nomalloc((void *)&append, (void *)a); |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
333 } else if (c=='b' || c=='t' || c=='T') { |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
334 int t = tea; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
335 |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
336 if (c != 'b') tea = 0; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
337 if (c=='b' || t^(c=='T')) { |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
338 str = logrus->arg1+(char *)logrus; |
1532
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
339 |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
340 |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
341 if (!*str) break; |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
342 for (logrus = (void *)TT.pattern; logrus; logrus = logrus->next) |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
343 if (logrus->c == ':' && !strcmp(logrus->arg1+(char *)logrus, str)) |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
344 break; |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
345 if (!logrus) error_exit("no :%s", str); |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
346 } |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
347 } else if (c=='c') { |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
348 str = logrus->arg1+(char *)logrus; |
1556
e7944f8fdcbb
Debugging pass on sed: make the existing test suite pass.
Rob Landley <rob@landley.net>
parents:
1553
diff
changeset
|
349 if (!logrus->hit || (!logrus->lmatch[1] && !logrus->rmatch[1])) |
e7944f8fdcbb
Debugging pass on sed: make the existing test suite pass.
Rob Landley <rob@landley.net>
parents:
1553
diff
changeset
|
350 emit(str, strlen(str), 1); |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
351 goto done; |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
352 } else if (c=='d') goto done; |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
353 else if (c=='D') { |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
354 // Delete up to \n or end of buffer |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
355 for (str = line; !*str || *str=='\n'; str++); |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
356 len -= str - line; |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
357 memmove(line, str, len); |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
358 line[len] = 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
|
359 |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
360 // restart script |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
361 logrus = (void *)TT.pattern; |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
362 continue; |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
363 } else if (c=='g') { |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
364 free(line); |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
365 line = xstrdup(TT.remember); |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
366 len = TT.rememberlen; |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
367 } else if (c=='G') { |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
368 line = xrealloc(line, len+TT.rememberlen+2); |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
369 line[len++] = '\n'; |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
370 memcpy(line+len, TT.remember, TT.rememberlen); |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
371 line[len += TT.rememberlen] = 0; |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
372 } else if (c=='h') { |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
373 free(TT.remember); |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
374 TT.remember = xstrdup(line); |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
375 TT.rememberlen = len; |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
376 } else if (c=='H') { |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
377 TT.remember = xrealloc(TT.remember, TT.rememberlen+len+2); |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
378 TT.remember[TT.rememberlen++] = '\n'; |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
379 memcpy(TT.remember+TT.rememberlen, line, len); |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
380 TT.remember[TT.rememberlen += len] = 0; |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
381 } else if (c=='i') { |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
382 str = logrus->arg1+(char *)logrus; |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
383 emit(str, strlen(str), 1); |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
384 // } else if (c=='l') { |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
385 // error_exit("todo: l"); |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
386 } else if (c=='n') { |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
387 TT.restart = logrus->next; |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
388 |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
389 break; |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
390 } else if (c=='N') { |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
391 if (pline) { |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
392 TT.restart = logrus->next; |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
393 extend_string(&line, TT.nextline, plen, -TT.nextlen); |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
394 free(TT.nextline); |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
395 TT.nextline = line; |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
396 line = 0; |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
397 } |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
398 |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
399 goto done; |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
400 } else if (c=='p') { |
1532
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
401 if (emit(line, len, eol)) break; |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
402 } else if (c=='q') break; |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
403 else if (c=='s') { |
1553
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
404 char *rline = line, *new = logrus->arg2 + (char *)logrus, *swap, *rswap; |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
405 regmatch_t *match = (void *)toybuf; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
406 regex_t *reg = (void *)(logrus->arg1+(char *)logrus); |
1556
e7944f8fdcbb
Debugging pass on sed: make the existing test suite pass.
Rob Landley <rob@landley.net>
parents:
1553
diff
changeset
|
407 int mflags = 0, count = 0, zmatch = 1, rlen = len, mlen, off, newlen; |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
408 |
1553
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
409 // Find match in remaining line (up to remaining len) |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
410 while (!ghostwheel(reg, rline, rlen, 10, match, mflags)) { |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
411 mflags = REG_NOTBOL; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
412 |
1553
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
413 // Zero length matches don't count immediately after a previous match |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
414 mlen = match[0].rm_eo-match[0].rm_so; |
1556
e7944f8fdcbb
Debugging pass on sed: make the existing test suite pass.
Rob Landley <rob@landley.net>
parents:
1553
diff
changeset
|
415 if (!mlen && !zmatch) { |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
416 if (!rlen--) break; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
417 rline++; |
1556
e7944f8fdcbb
Debugging pass on sed: make the existing test suite pass.
Rob Landley <rob@landley.net>
parents:
1553
diff
changeset
|
418 zmatch++; |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
419 continue; |
1556
e7944f8fdcbb
Debugging pass on sed: make the existing test suite pass.
Rob Landley <rob@landley.net>
parents:
1553
diff
changeset
|
420 } else zmatch = 0; |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
421 |
1553
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
422 // If we're replacing only a specific match, skip if this isn't it |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
423 off = logrus->sflags>>3; |
1553
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
424 if (off && off != ++count) { |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
425 rline += match[0].rm_eo; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
426 rlen -= match[0].rm_eo; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
427 |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
428 continue; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
429 } |
1556
e7944f8fdcbb
Debugging pass on sed: make the existing test suite pass.
Rob Landley <rob@landley.net>
parents:
1553
diff
changeset
|
430 // The fact getline() can allocate unbounded amounts of memory is |
e7944f8fdcbb
Debugging pass on sed: make the existing test suite pass.
Rob Landley <rob@landley.net>
parents:
1553
diff
changeset
|
431 // a bigger issue, but while we're here check for integer overflow |
e7944f8fdcbb
Debugging pass on sed: make the existing test suite pass.
Rob Landley <rob@landley.net>
parents:
1553
diff
changeset
|
432 if (match[0].rm_eo > INT_MAX) perror_exit(0); |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
433 |
1553
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
434 // newlen = strlen(new) but with \1 and & and printf escapes |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
435 for (off = newlen = 0; new[off]; off++) { |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
436 int cc = 0; |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
437 |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
438 if (new[off] == '\\') { |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
439 cc = new[++off] - '0'; |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
440 if (cc < 0 || cc > 9) { |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
441 newlen += 1+!unescape(new[off]); |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
442 continue; |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
443 } |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
444 } else if (new[off] != '&') { |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
445 newlen++; |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
446 continue; |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
447 } |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
448 newlen += match[cc].rm_eo-match[cc].rm_so; |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
449 } |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
450 |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
451 // Allocate new size, copy start/end around match. (Can't extend in |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
452 // place because backrefs may refer to text after it's overwritten.) |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
453 len += newlen-mlen; |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
454 swap = xmalloc(len+1); |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
455 rswap = swap+(rline-line); |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
456 memcpy(swap, line, (rline-line)+match[0].rm_so); |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
457 memcpy(rswap+match[0].rm_so+newlen, rline+match[0].rm_eo, |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
458 (rlen -= match[0].rm_eo)+1); |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
459 |
1553
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
460 // copy in new replacement text |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
461 rswap += match[0].rm_so; |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
462 for (off = mlen = 0; new[off]; off++) { |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
463 int cc = 0, ll; |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
464 |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
465 if ((rswap[mlen++] = new[off]) == '\\') { |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
466 cc = new[++off] - '0'; |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
467 if (cc<0 || cc>9) { |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
468 cc = unescape(new[off]); |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
469 if (cc) rswap[mlen-1] = cc; |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
470 else rswap[mlen++] = new[off]; |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
471 |
1553
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
472 continue; |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
473 } else if (match[cc].rm_so == -1) error_exit("no s//\\%d/", cc); |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
474 } else if (new[off] != '&') continue; |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
475 |
1553
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
476 ll = match[cc].rm_eo-match[cc].rm_so; |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
477 memcpy(rswap+(--mlen), rline+match[cc].rm_so, ll); |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
478 mlen += ll; |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
479 } |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
480 |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
481 rline = rswap+newlen; |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
482 free(line); |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
483 line = swap; |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
484 len = rlen+(rline-line); |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
485 |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
486 // Stop after first substitution unless we have flag g |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
487 if (!(logrus->sflags & 2)) break; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
488 } |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
489 |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
490 if (mflags) { |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
491 // flag p |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
492 if (logrus->sflags & 4) emit(line, len, eol); |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
493 |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
494 tea = 1; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
495 if (logrus->w) goto writenow; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
496 } |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
497 } else if (c=='w') { |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
498 int fd, noeol; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
499 char *name; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
500 |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
501 writenow: |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
502 // Swap out emit() context |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
503 fd = TT.fdout; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
504 noeol = TT.noeol; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
505 |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
506 // We save filehandle and newline status before filename |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
507 name = logrus->w + (char *)logrus; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
508 memcpy(&TT.fdout, name, 4); |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
509 name += 4; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
510 TT.noeol = *(name++); |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
511 |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
512 // write, then save/restore context |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
513 if (emit(line, len, eol)) |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
514 perror_exit("w '%s'", logrus->arg1+(char *)logrus); |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
515 *(--name) = TT.noeol; |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
516 TT.noeol = noeol; |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
517 TT.fdout = fd; |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
518 } else if (c=='x') { |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
519 long swap = TT.rememberlen; |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
520 |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
521 str = TT.remember; |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
522 TT.remember = line; |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
523 line = str; |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
524 TT.rememberlen = len; |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
525 len = swap; |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
526 } else if (c=='y') { |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
527 char *from, *to = (char *)logrus; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
528 int i, j; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
529 |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
530 from = to+logrus->arg1; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
531 to += logrus->arg2; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
532 |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
533 for (i = 0; i < len; i++) { |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
534 j = stridx(from, line[i]); |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
535 if (j != -1) line[i] = to[j]; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
536 } |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
537 } else if (c=='=') xprintf("%ld\n", TT.count); |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
538 else if (c!=':') error_exit("todo: %c", c); |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
539 |
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
540 logrus = logrus->next; |
1532
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
541 } |
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
542 |
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
543 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
|
544 |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
545 done: |
1532
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
546 free(line); |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
547 |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
548 if (dlist_terminate(append)) while (append) { |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
549 struct append *a = append->next; |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
550 |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
551 if (append->file) { |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
552 int fd = xopen(append->str, O_RDONLY); |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
553 |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
554 // Force newline if noeol pending |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
555 emit(0, 0, 0); |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
556 xsendfile(fd, TT.fdout); |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
557 close(fd); |
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
558 } else emit(append->str, strlen(append->str), 1); |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
559 free(append); |
1550
2997847aa299
Implement another largeish chunk of sed. Untested, unfinished, do not use yet.
Rob Landley <rob@landley.net>
parents:
1545
diff
changeset
|
560 append = a; |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
561 } |
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
|
562 } |
231
31dc682c18ad
Very early stub of sed, does nothing yet.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
563 |
1530
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
564 // 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
|
565 |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
566 // 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
|
567 // 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
|
568 // 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
|
569 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
|
570 { |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
571 FILE *fp = fd ? xfdopen(fd, "r") : stdin; |
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
|
572 |
1520
dfd6b3404c16
Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents:
1235
diff
changeset
|
573 for (;;) { |
dfd6b3404c16
Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents:
1235
diff
changeset
|
574 char *line = 0; |
dfd6b3404c16
Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents:
1235
diff
changeset
|
575 ssize_t len; |
231
31dc682c18ad
Very early stub of sed, does nothing yet.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
576 |
1520
dfd6b3404c16
Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents:
1235
diff
changeset
|
577 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
|
578 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
|
579 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
|
580 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
|
581 } else break; |
1520
dfd6b3404c16
Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents:
1235
diff
changeset
|
582 } |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
583 |
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
584 if (fd) fclose(fp); |
1530
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
585 } |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
586 |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
587 // 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
|
588 // 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
|
589 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
|
590 { |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
591 long ll; |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
592 |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
593 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
|
594 if (data[ll] == '\n') { |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
595 char *c = data; |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
596 |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
597 data[ll] = 0; |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
598 call(&c, len); |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
599 data[ll++] = '\n'; |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
600 data += ll; |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
601 len -= ll; |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
602 ll = -1; |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
603 } |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
604 } |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
605 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
|
606 } |
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
|
607 |
1520
dfd6b3404c16
Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents:
1235
diff
changeset
|
608 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
|
609 { |
1532
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
610 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
|
611 |
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
612 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
|
613 // 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
|
614 } |
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
|
615 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
|
616 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
|
617 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
|
618 |
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
619 // 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
|
620 } |
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
|
621 } |
231
31dc682c18ad
Very early stub of sed, does nothing yet.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
622 |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
623 // Note: removing backslash escapes and null terminating edits the source |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
624 // string, which could be from the environment space via -e, which could |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
625 // screw up what "ps" sees, and I'm ok with that. (Modifying the environment |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
626 // space like that means sed is very, very not reentrant.) |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
627 |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
628 // Ok, what happens if we xexec() sed with constant arguments then? |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
629 // TODO: ^^^ that |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
630 |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
631 // returns length of processed string, *pstr advances to next unused char, |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
632 // if delim (or *delim) is 0 uses starting char as delimiter, otherwise |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
633 // parses and saves delimiter from first character(s) |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
634 static int unescape_delimited_string(char **pstr, char *delim) |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
635 { |
1556
e7944f8fdcbb
Debugging pass on sed: make the existing test suite pass.
Rob Landley <rob@landley.net>
parents:
1553
diff
changeset
|
636 char *to, *from, d; |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
637 int rc; |
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
638 |
1556
e7944f8fdcbb
Debugging pass on sed: make the existing test suite pass.
Rob Landley <rob@landley.net>
parents:
1553
diff
changeset
|
639 to = from = *pstr; |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
640 if (!delim || !*delim) { |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
641 if (!(d = *(from++))) return -1; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
642 if (d == '\\') d = *(from++); |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
643 if (!d || d == '\\') return -1; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
644 if (delim) *delim = d; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
645 } else d = *delim; |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
646 |
1556
e7944f8fdcbb
Debugging pass on sed: make the existing test suite pass.
Rob Landley <rob@landley.net>
parents:
1553
diff
changeset
|
647 while (*from != d) { |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
648 if (!*from) return -1; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
649 if (*from == '\\') { |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
650 if (!from[1]) return -1; |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
651 |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
652 // Check escaped end delimiter before printf style escapes. |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
653 if (from[1] == d) from++; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
654 else { |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
655 char c = unescape(from[1]); |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
656 |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
657 if (c) { |
1556
e7944f8fdcbb
Debugging pass on sed: make the existing test suite pass.
Rob Landley <rob@landley.net>
parents:
1553
diff
changeset
|
658 *(to++) = c; |
e7944f8fdcbb
Debugging pass on sed: make the existing test suite pass.
Rob Landley <rob@landley.net>
parents:
1553
diff
changeset
|
659 from+=2; |
e7944f8fdcbb
Debugging pass on sed: make the existing test suite pass.
Rob Landley <rob@landley.net>
parents:
1553
diff
changeset
|
660 continue; |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
661 } |
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
662 } |
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
663 } |
1556
e7944f8fdcbb
Debugging pass on sed: make the existing test suite pass.
Rob Landley <rob@landley.net>
parents:
1553
diff
changeset
|
664 *(to++) = *(from++); |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
665 } |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
666 rc = to-*pstr; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
667 *to = 0; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
668 *pstr = from+1; |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
669 |
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
670 return rc; |
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
671 } |
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
672 |
1530
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
673 // 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
|
674 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
|
675 { |
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
|
676 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
|
677 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
|
678 int i; |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
679 |
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
|
680 // 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
|
681 if (corwin && corwin->prev->hit) { |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
682 // Remove half-finished entry from list so remalloc() doesn't confuse it |
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
683 TT.pattern = TT.pattern->prev; |
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
684 corwin = dlist_pop(&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
|
685 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
|
686 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
|
687 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
|
688 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
|
689 |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
690 // Resume parsing |
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
|
691 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
|
692 } |
8bc715741481
Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents:
1537
diff
changeset
|
693 |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
694 // Loop through commands in line |
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
695 |
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
696 corwin = 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
|
697 for (;;) { |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
698 if (corwin) dlist_add_nomalloc(&TT.pattern, (void *)corwin); |
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
699 |
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
|
700 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
|
701 if (!*line || *line == '#') return; |
1530
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
702 |
1532
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
703 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
|
704 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
|
705 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
|
706 |
1532
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
707 // 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
|
708 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
|
709 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
|
710 else if (i) break; |
1530
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
711 |
1532
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
712 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
|
713 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
|
714 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
|
715 line++; |
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
716 } else if (*line == '/' || *line == '\\') { |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
717 char *s = line; |
1530
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
718 |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
719 if (-1 == unescape_delimited_string(&line, 0)) goto brand; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
720 xregcomp((void *)reg, s, |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
721 ((toys.optflags & FLAG_r)*REG_EXTENDED)|REG_NOSUB); |
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
|
722 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
|
723 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
|
724 } 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
|
725 } |
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
726 |
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
727 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
|
728 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
|
729 |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
730 while (*line == '!') { |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
731 corwin->not = 1; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
732 line++; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
733 } |
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
|
734 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
|
735 |
8bc715741481
Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents:
1537
diff
changeset
|
736 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
|
737 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
|
738 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
|
739 |
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
740 // 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
|
741 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
|
742 memcpy(corwin, toybuf, reg-toybuf); |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
743 reg = (reg-toybuf) + (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
|
744 |
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
|
745 // 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
|
746 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
|
747 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
|
748 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
|
749 } else if (c == 's') { |
1553
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
750 char *merlin, *fiona, delim = 0; |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
751 |
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
752 // s/pattern/replacement/flags |
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
753 |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
754 // get pattern (just record, we parse it later) |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
755 corwin->arg1 = reg - (char *)corwin; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
756 merlin = line; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
757 if (-1 == unescape_delimited_string(&line, &delim)) goto brand; |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
758 |
1553
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
759 // get replacement - don't replace escapes because \1 and \& need |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
760 // processing later, after we replace \\ with \ we can't tell \\1 from \1 |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
761 fiona = line; |
1553
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
762 while (*line != delim) { |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
763 if (!*line) goto brand; |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
764 if (*line == '\\') { |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
765 if (!line[1]) goto brand; |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
766 line += 2; |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
767 } else line++; |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
768 } |
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
769 |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
770 corwin->arg2 = corwin->arg1 + sizeof(regex_t); |
1553
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
771 reg = extend_string((void *)&corwin, fiona, corwin->arg2, line-fiona)+1; |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
772 |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
773 // get flags |
1556
e7944f8fdcbb
Debugging pass on sed: make the existing test suite pass.
Rob Landley <rob@landley.net>
parents:
1553
diff
changeset
|
774 for (line++; *line && *line != ';' && *line != '#'; line++) { |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
775 long l; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
776 |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
777 if (isspace(*line)) continue; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
778 |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
779 if (0 <= (l = stridx("igp", *line))) corwin->sflags |= 1<<l; |
1553
2423dd884182
sed: implement s & and \1 backrefs.
Rob Landley <rob@landley.net>
parents:
1552
diff
changeset
|
780 else if (!corwin->sflags >> 3 && 0<(l = strtol(line, &line, 10))) { |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
781 corwin->sflags |= l << 3; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
782 line--; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
783 } else if (*line == 'w') break; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
784 else goto brand; |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
785 } |
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
786 |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
787 // We deferred actually parsing the rexex until we had the s///i flag |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
788 // allocating the space was done by extend_string() above |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
789 xregcomp((void *)(corwin->arg1 + (char *)corwin), merlin, |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
790 ((toys.optflags & FLAG_r)*REG_EXTENDED)|((corwin->sflags&1)*REG_ICASE)); |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
791 if (*line == 'w') { |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
792 line++; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
793 goto writenow; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
794 } |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
795 } else if (c == 'w') { |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
796 int fd, delim; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
797 char *cc; |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
798 |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
799 // Since s/// uses arg1 and arg2, and w needs a persistent filehandle and |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
800 // eol status, and to retain the filename for error messages, we'd need |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
801 // to go up to arg5 just for this. Compromise: dynamically allocate the |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
802 // filehandle and eol status. |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
803 |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
804 writenow: |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
805 while (isspace(*line)) line++; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
806 if (!*line) goto brand; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
807 for (cc = line; *cc; cc++) if (*cc == '\\' && cc[1] == ';') break; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
808 delim = *cc; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
809 *cc = 0; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
810 fd = xcreate(line, O_WRONLY|O_CREAT|O_TRUNC, 0544); |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
811 *cc = delim; |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
812 |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
813 delim = cc-line; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
814 corwin->w = reg - (char *)corwin; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
815 corwin = xrealloc(corwin, corwin->w + delim + 6); |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
816 reg = corwin->w + (char *)corwin; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
817 |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
818 memcpy(reg, &fd, 4); |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
819 reg += 4; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
820 *(reg++) = 0; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
821 memcpy(reg, line, delim); |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
822 reg += delim; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
823 *(reg++) = 0; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
824 |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
825 line = cc; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
826 if (delim) line += 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
|
827 } else if (c == 'y') { |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
828 char *s = line, delim = 0; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
829 int len1, len2; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
830 |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
831 if (-1 == (len1 = unescape_delimited_string(&line, &delim))) goto brand; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
832 corwin->arg1 = reg-(char *)corwin; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
833 reg = extend_string((void *)&corwin, s, reg-(char *)corwin, len1); |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
834 s = line; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
835 corwin->arg2 = reg-(char *)corwin; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
836 if (-1 == (len2 = unescape_delimited_string(&line, &delim))) goto brand; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
837 if (len1 != len2) goto brand; |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
838 reg = extend_string((void *)&corwin, s, reg-(char*)corwin, len2); |
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
|
839 } else if (strchr("abcirtTw:", c)) { |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
840 int end, class; |
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
|
841 |
8bc715741481
Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents:
1537
diff
changeset
|
842 // 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
|
843 |
8bc715741481
Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents:
1537
diff
changeset
|
844 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
|
845 append: |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
846 class = !strchr("btT:", c); |
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
847 end = strcspn(line, class ? "" : "; \t\r\n\v\f"); |
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
|
848 |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
849 if (!end) { |
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
|
850 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
|
851 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
|
852 } |
8bc715741481
Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents:
1537
diff
changeset
|
853 |
8bc715741481
Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents:
1537
diff
changeset
|
854 // 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
|
855 // 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
|
856 // here instead of toybuf so there's no maximum size. |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
857 if (!corwin->arg1) corwin->arg1 = reg - (char*)corwin; |
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
858 reg = extend_string((void *)&corwin, line, reg - (char *)corwin, end); |
1556
e7944f8fdcbb
Debugging pass on sed: make the existing test suite pass.
Rob Landley <rob@landley.net>
parents:
1553
diff
changeset
|
859 line += end; |
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
|
860 |
8bc715741481
Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents:
1537
diff
changeset
|
861 // Line continuation? |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
862 if (class && reg[-1] == '\\') { |
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
863 reg[-1] = 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
|
864 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
|
865 } |
8bc715741481
Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents:
1537
diff
changeset
|
866 |
8bc715741481
Next drop of sed infrastructure, mostly argument parsing, doesn't do anything interesting yet.
Rob Landley <rob@landley.net>
parents:
1537
diff
changeset
|
867 // 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
|
868 } 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
|
869 } |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
870 |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
871 brand: |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
872 // Reminisce about chestnut trees. |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
873 error_exit("bad pattern '%s'@%ld (%c)", *pline, line-*pline+1, *line); |
1530
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
874 } |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
875 |
231
31dc682c18ad
Very early stub of sed, does nothing yet.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
876 void sed_main(void) |
31dc682c18ad
Very early stub of sed, does nothing yet.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
877 { |
1530
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
878 struct arg_list *dworkin; |
1520
dfd6b3404c16
Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents:
1235
diff
changeset
|
879 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
|
880 |
1530
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
881 // 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
|
882 // 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
|
883 // 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
|
884 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
|
885 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
|
886 return; |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
887 } |
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
888 |
1532
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
889 // 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
|
890 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
|
891 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
|
892 (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
|
893 } |
1543
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
894 |
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
895 // Option parsing infrastructure can't interlace "-e blah -f blah -e blah" |
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
896 // so handle all -e, then all -f. (At least the behavior's consistent.) |
ad6b2f0e566b
Next round of sed infrastructure, parses most commands now, doesn't implement them yet.
Rob Landley <rob@landley.net>
parents:
1538
diff
changeset
|
897 |
1532
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
898 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
|
899 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
|
900 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
|
901 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
|
902 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
|
903 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
|
904 |
1532
bf2c5216d726
Basic sed range support, enough for "sed -n 9,11p README" to work.
Rob Landley <rob@landley.net>
parents:
1530
diff
changeset
|
905 TT.fdout = 1; |
1545
b6ff5dc17763
Implement a few sed commands. Not done, and not tested yet.
Rob Landley <rob@landley.net>
parents:
1543
diff
changeset
|
906 TT.remember = xstrdup(""); |
1530
3eafa445c1a6
Random in-progress snapshot of sed, not finished yet.
Rob Landley <rob@landley.net>
parents:
1520
diff
changeset
|
907 |
1520
dfd6b3404c16
Started over on sed (by reading the posix spec).
Rob Landley <rob@landley.net>
parents:
1235
diff
changeset
|
908 // 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
|
909 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
|
910 |
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
|
911 if (!(toys.optflags & FLAG_i)) walk_pattern(0, 0); |
1552
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
912 |
57ee7711a0c9
Implement more sed plumbing, including s/// (without \1 or & yet).
Rob Landley <rob@landley.net>
parents:
1550
diff
changeset
|
913 // todo: need to close fd when done for TOYBOX_FREE? |
231
31dc682c18ad
Very early stub of sed, does nothing yet.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
914 } |