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