1*1258a77dSderaadt# $OpenBSD: POSIX,v 1.2 1996/06/26 05:39:04 deraadt Exp $ 2df930be7Sderaadt# from: @(#)POSIX 8.1 (Berkeley) 6/6/93 3df930be7Sderaadt 4df930be7SderaadtComments on the IEEE P1003.2 Draft 12 5df930be7Sderaadt Part 2: Shell and Utilities 6df930be7Sderaadt Section 4.55: sed - Stream editor 7df930be7Sderaadt 8df930be7SderaadtDiomidis Spinellis <dds@doc.ic.ac.uk> 9df930be7SderaadtKeith Bostic <bostic@cs.berkeley.edu> 10df930be7Sderaadt 11df930be7SderaadtIn the following paragraphs, "wrong" usually means "inconsistent with 12df930be7Sderaadthistoric practice", as most of the following comments refer to 13df930be7Sderaadtundocumented inconsistencies between the historical versions of sed and 14df930be7Sderaadtthe POSIX 1003.2 standard. All the comments are notes taken while 15df930be7Sderaadtimplementing a POSIX-compatible version of sed, and should not be 16df930be7Sderaadtinterpreted as official opinions or criticism towards the POSIX committee. 17df930be7SderaadtAll uses of "POSIX" refer to section 4.55, Draft 12 of POSIX 1003.2. 18df930be7Sderaadt 19df930be7Sderaadt 1. 32V and BSD derived implementations of sed strip the text 20df930be7Sderaadt arguments of the a, c and i commands of their initial blanks, 21df930be7Sderaadt i.e. 22df930be7Sderaadt 23df930be7Sderaadt #!/bin/sed -f 24df930be7Sderaadt a\ 25df930be7Sderaadt foo\ 26df930be7Sderaadt \ indent\ 27df930be7Sderaadt bar 28df930be7Sderaadt 29df930be7Sderaadt produces: 30df930be7Sderaadt 31df930be7Sderaadt foo 32df930be7Sderaadt indent 33df930be7Sderaadt bar 34df930be7Sderaadt 35df930be7Sderaadt POSIX does not specify this behavior as the System V versions of 36df930be7Sderaadt sed do not do this stripping. The argument against stripping is 37df930be7Sderaadt that it is difficult to write sed scripts that have leading blanks 38df930be7Sderaadt if they are stripped. The argument for stripping is that it is 39df930be7Sderaadt difficult to write readable sed scripts unless indentation is allowed 40df930be7Sderaadt and ignored, and leading whitespace is obtainable by entering a 41df930be7Sderaadt backslash in front of it. This implementation follows the BSD 42df930be7Sderaadt historic practice. 43df930be7Sderaadt 44df930be7Sderaadt 2. Historical versions of sed required that the w flag be the last 45df930be7Sderaadt flag to an s command as it takes an additional argument. This 46df930be7Sderaadt is obvious, but not specified in POSIX. 47df930be7Sderaadt 48df930be7Sderaadt 3. Historical versions of sed required that whitespace follow a w 49df930be7Sderaadt flag to an s command. This is not specified in POSIX. This 50df930be7Sderaadt implementation permits whitespace but does not require it. 51df930be7Sderaadt 52df930be7Sderaadt 4. Historical versions of sed permitted any number of whitespace 53df930be7Sderaadt characters to follow the w command. This is not specified in 54df930be7Sderaadt POSIX. This implementation permits whitespace but does not 55df930be7Sderaadt require it. 56df930be7Sderaadt 57df930be7Sderaadt 5. The rule for the l command differs from historic practice. Table 58df930be7Sderaadt 2-15 includes the various ANSI C escape sequences, including \\ 59df930be7Sderaadt for backslash. Some historical versions of sed displayed two 60df930be7Sderaadt digit octal numbers, too, not three as specified by POSIX. POSIX 61df930be7Sderaadt is a cleanup, and is followed by this implementation. 62df930be7Sderaadt 63df930be7Sderaadt 6. The POSIX specification for ! does not specify that for a single 64df930be7Sderaadt command the command must not contain an address specification 65df930be7Sderaadt whereas the command list can contain address specifications. The 66df930be7Sderaadt specification for ! implies that "3!/hello/p" works, and it never 67df930be7Sderaadt has, historically. Note, 68df930be7Sderaadt 69df930be7Sderaadt 3!{ 70df930be7Sderaadt /hello/p 71df930be7Sderaadt } 72df930be7Sderaadt 73df930be7Sderaadt does work. 74df930be7Sderaadt 75df930be7Sderaadt 7. POSIX does not specify what happens with consecutive ! commands 76df930be7Sderaadt (e.g. /foo/!!!p). Historic implementations allow any number of 77df930be7Sderaadt !'s without changing the behaviour. (It seems logical that each 78df930be7Sderaadt one might reverse the behaviour.) This implementation follows 79df930be7Sderaadt historic practice. 80df930be7Sderaadt 81df930be7Sderaadt 8. Historic versions of sed permitted commands to be separated 82df930be7Sderaadt by semi-colons, e.g. 'sed -ne '1p;2p;3q' printed the first 83df930be7Sderaadt three lines of a file. This is not specified by POSIX. 84df930be7Sderaadt Note, the ; command separator is not allowed for the commands 85df930be7Sderaadt a, c, i, w, r, :, b, t, # and at the end of a w flag in the s 86df930be7Sderaadt command. This implementation follows historic practice and 87df930be7Sderaadt implements the ; separator. 88df930be7Sderaadt 89df930be7Sderaadt 9. Historic versions of sed terminated the script if EOF was reached 90df930be7Sderaadt during the execution of the 'n' command, i.e.: 91df930be7Sderaadt 92df930be7Sderaadt sed -e ' 93df930be7Sderaadt n 94df930be7Sderaadt i\ 95df930be7Sderaadt hello 96df930be7Sderaadt ' </dev/null 97df930be7Sderaadt 98df930be7Sderaadt did not produce any output. POSIX does not specify this behavior. 99df930be7Sderaadt This implementation follows historic practice. 100df930be7Sderaadt 101df930be7Sderaadt10. Deleted. 102df930be7Sderaadt 103df930be7Sderaadt11. Historical implementations do not output the change text of a c 104df930be7Sderaadt command in the case of an address range whose first line number 105df930be7Sderaadt is greater than the second (e.g. 3,1). POSIX requires that the 106df930be7Sderaadt text be output. Since the historic behavior doesn't seem to have 107df930be7Sderaadt any particular purpose, this implementation follows the POSIX 108df930be7Sderaadt behavior. 109df930be7Sderaadt 110df930be7Sderaadt12. POSIX does not specify whether address ranges are checked and 111df930be7Sderaadt reset if a command is not executed due to a jump. The following 112df930be7Sderaadt program will behave in different ways depending on whether the 113df930be7Sderaadt 'c' command is triggered at the third line, i.e. will the text 114df930be7Sderaadt be output even though line 3 of the input will never logically 115df930be7Sderaadt encounter that command. 116df930be7Sderaadt 117df930be7Sderaadt 2,4b 118df930be7Sderaadt 1,3c\ 119df930be7Sderaadt text 120df930be7Sderaadt 121df930be7Sderaadt Historic implementations, and this implementation, do not output 122df930be7Sderaadt the text in the above example. The general rule, therefore, 123df930be7Sderaadt is that a range whose second address is never matched extends to 124df930be7Sderaadt the end of the input. 125df930be7Sderaadt 126df930be7Sderaadt13. Historical implementations allow an output suppressing #n at the 127df930be7Sderaadt beginning of -e arguments as well as in a script file. POSIX 128df930be7Sderaadt does not specify this. This implementation follows historical 129df930be7Sderaadt practice. 130df930be7Sderaadt 131df930be7Sderaadt14. POSIX does not explicitly specify how sed behaves if no script is 132df930be7Sderaadt specified. Since the sed Synopsis permits this form of the command, 133df930be7Sderaadt and the language in the Description section states that the input 134df930be7Sderaadt is output, it seems reasonable that it behave like the cat(1) 135df930be7Sderaadt command. Historic sed implementations behave differently for "ls | 136df930be7Sderaadt sed", where they produce no output, and "ls | sed -e#", where they 137df930be7Sderaadt behave like cat. This implementation behaves like cat in both cases. 138df930be7Sderaadt 139df930be7Sderaadt15. The POSIX requirement to open all w files at the beginning makes 140df930be7Sderaadt sed behave nonintuitively when the w commands are preceded by 141df930be7Sderaadt addresses or are within conditional blocks. This implementation 142df930be7Sderaadt follows historic practice and POSIX, by default, and provides the 143df930be7Sderaadt -a option which opens the files only when they are needed. 144df930be7Sderaadt 145df930be7Sderaadt16. POSIX does not specify how escape sequences other than \n and \D 146df930be7Sderaadt (where D is the delimiter character) are to be treated. This is 147df930be7Sderaadt reasonable, however, it also doesn't state that the backslash is 148df930be7Sderaadt to be discarded from the output regardless. A strict reading of 149df930be7Sderaadt POSIX would be that "echo xyz | sed s/./\a" would display "\ayz". 150df930be7Sderaadt As historic sed implementations always discarded the backslash, 151df930be7Sderaadt this implementation does as well. 152df930be7Sderaadt 153df930be7Sderaadt17. POSIX specifies that an address can be "empty". This implies 154df930be7Sderaadt that constructs like ",d" or "1,d" and ",5d" are allowed. This 155df930be7Sderaadt is not true for historic implementations or this implementation 156df930be7Sderaadt of sed. 157df930be7Sderaadt 158df930be7Sderaadt18. The b t and : commands are documented in POSIX to ignore leading 159df930be7Sderaadt white space, but no mention is made of trailing white space. 160df930be7Sderaadt Historic implementations of sed assigned different locations to 161df930be7Sderaadt the labels "x" and "x ". This is not useful, and leads to subtle 162df930be7Sderaadt programming errors, but it is historic practice and changing it 163df930be7Sderaadt could theoretically break working scripts. This implementation 164df930be7Sderaadt follows historic practice. 165df930be7Sderaadt 166df930be7Sderaadt19. Although POSIX specifies that reading from files that do not exist 167df930be7Sderaadt from within the script must not terminate the script, it does not 168df930be7Sderaadt specify what happens if a write command fails. Historic practice 169df930be7Sderaadt is to fail immediately if the file cannot be opened or written. 170df930be7Sderaadt This implementation follows historic practice. 171df930be7Sderaadt 172df930be7Sderaadt20. Historic practice is that the \n construct can be used for either 173df930be7Sderaadt string1 or string2 of the y command. This is not specified by 174df930be7Sderaadt POSIX. This implementation follows historic practice. 175df930be7Sderaadt 176df930be7Sderaadt21. Deleted. 177df930be7Sderaadt 178df930be7Sderaadt22. Historic implementations of sed ignore the RE delimiter characters 179df930be7Sderaadt within character classes. This is not specified in POSIX. This 180df930be7Sderaadt implementation follows historic practice. 181df930be7Sderaadt 182df930be7Sderaadt23. Historic implementations handle empty RE's in a special way: the 183df930be7Sderaadt empty RE is interpreted as if it were the last RE encountered, 184df930be7Sderaadt whether in an address or elsewhere. POSIX does not document this 185df930be7Sderaadt behavior. For example the command: 186df930be7Sderaadt 187df930be7Sderaadt sed -e /abc/s//XXX/ 188df930be7Sderaadt 189df930be7Sderaadt substitutes XXX for the pattern abc. The semantics of "the last 190df930be7Sderaadt RE" can be defined in two different ways: 191df930be7Sderaadt 192df930be7Sderaadt 1. The last RE encountered when compiling (lexical/static scope). 193df930be7Sderaadt 2. The last RE encountered while running (dynamic scope). 194df930be7Sderaadt 195df930be7Sderaadt While many historical implementations fail on programs depending 196df930be7Sderaadt on scope differences, the SunOS version exhibited dynamic scope 197df930be7Sderaadt behaviour. This implementation does dynamic scoping, as this seems 198df930be7Sderaadt the most useful and in order to remain consistent with historical 199df930be7Sderaadt practice. 200