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