xref: /openbsd/bin/ed/POSIX (revision b1a6effd)
1*b1a6effdSdaniel$OpenBSD: POSIX,v 1.8 2014/05/24 01:35:55 daniel Exp $
2df930be7Sderaadt$NetBSD: POSIX,v 1.9 1995/03/21 09:04:32 cgd Exp $
3df930be7Sderaadt
4df930be7SderaadtThis version of ed(1) is not strictly POSIX compliant, as described in
5df930be7Sderaadtthe POSIX 1003.2 document.  The following is a summary of the omissions,
6df930be7Sderaadtextensions and possible deviations from POSIX 1003.2.
7df930be7Sderaadt
8df930be7SderaadtOMISSIONS
9df930be7Sderaadt---------
10df930be7Sderaadt1) Locale(3) is not supported yet.
11df930be7Sderaadt
12df930be7Sderaadt2) For backwards compatibility, the POSIX rule that says a range of
13df930be7Sderaadt   addresses cannot be used where only a single address is expected has
14df930be7Sderaadt   been relaxed.
15df930be7Sderaadt
16df930be7Sderaadt3) To support the BSD `s' command (see extension [1] below),
17df930be7Sderaadt   substitution patterns cannot be delimited by numbers or the characters
187da5ee4bStobias   `r', `g' and `p'.  In contrast, POSIX specifies any character except
197da5ee4bStobias   space or newline can be used as a delimiter.
20df930be7Sderaadt
21df930be7SderaadtEXTENSIONS
22df930be7Sderaadt----------
23df930be7Sderaadt1) BSD commands have been implemented wherever they do not conflict with
24df930be7Sderaadt   the POSIX standard.  The BSD-ism's included are:
25df930be7Sderaadt	i) `s' (i.e., s[n][rgp]*) to repeat a previous substitution,
26df930be7Sderaadt	ii) `W' for appending text to an existing file,
27df930be7Sderaadt	iii) `wq' for exiting after a write,
28df930be7Sderaadt	iv) `z' for scrolling through the buffer, and
29df930be7Sderaadt	v) BSD line addressing syntax (i.e., `^' and `%') is recognized.
30df930be7Sderaadt
31a23f2669Sderaadt2) The POSIX interactive global commands `G' and `V' are extended to
32df930be7Sderaadt   support multiple commands, including `a', `i' and `c'.  The command
33df930be7Sderaadt   format is the same as for the global commands `g' and `v', i.e., one
34df930be7Sderaadt   command per line with each line, except for the last, ending in a
35df930be7Sderaadt   backslash (\).
36df930be7Sderaadt
37a23f2669Sderaadt3) An extension to the POSIX file commands `E', `e', `r', `W' and `w' is
38df930be7Sderaadt   that <file> arguments are processed for backslash escapes, i.e.,  any
39df930be7Sderaadt   character preceded by a backslash is interpreted literally.  If the
40df930be7Sderaadt   first unescaped character of a <file> argument is a bang (!), then the
41df930be7Sderaadt   rest of the line is interpreted as a shell command, and no escape
42df930be7Sderaadt   processing is performed by ed.
43df930be7Sderaadt
44df930be7SderaadtDEVIATIONS
45df930be7Sderaadt----------
46df930be7Sderaadt1) Though ed is not a stream editor, it can be used to edit binary files.
47df930be7Sderaadt   To assist in binary editing, when a file containing at least one ASCII
48df930be7Sderaadt   NUL character is written, a newline is not appended if it did not
49df930be7Sderaadt   already contain one upon reading.  In particular, reading /dev/null
50df930be7Sderaadt   prior to writing prevents appending a newline to a binary file.
51df930be7Sderaadt
52df930be7Sderaadt   For example, to create a file with ed containing a single NUL character:
53df930be7Sderaadt      $ ed file
54df930be7Sderaadt      a
55df930be7Sderaadt      ^@
56df930be7Sderaadt      .
57df930be7Sderaadt      r /dev/null
58df930be7Sderaadt      wq
59df930be7Sderaadt
60df930be7Sderaadt    Similarly, to remove a newline from the end of binary `file':
61df930be7Sderaadt      $ ed file
62df930be7Sderaadt      r /dev/null
63df930be7Sderaadt      wq
64df930be7Sderaadt
65df930be7Sderaadt2) Since the behavior of `u' (undo) within a `g' (global) command list is
66df930be7Sderaadt   not specified by POSIX, it follows the behavior of the SunOS ed:
67df930be7Sderaadt   undo forces a global command list to be executed only once, rather than
6858ec2aaeSjmc   for each line matching a global pattern.  In addition, each instance of
69df930be7Sderaadt   `u' within a global command undoes all previous commands (including
70df930be7Sderaadt   undo's) in the command list.  This seems the best way, since the
71df930be7Sderaadt   alternatives are either too complicated to implement or too confusing
72df930be7Sderaadt   to use.
73df930be7Sderaadt
74df930be7Sderaadt   The global/undo combination is useful for masking errors that
75df930be7Sderaadt   would otherwise cause a script to fail.  For instance, an ed script
7675beb280Spjanzen   to remove any occurrences of either `censor1' or `censor2' might be
77df930be7Sderaadt   written as:
78df930be7Sderaadt   	ed - file <<EOF
79df930be7Sderaadt	1g/.*/u\
80df930be7Sderaadt	,s/censor1//g\
81df930be7Sderaadt	,s/censor2//g
82df930be7Sderaadt	...
83df930be7Sderaadt
84df930be7Sderaadt3) The `m' (move) command within a `g' command list also follows the SunOS
85df930be7Sderaadt   ed implementation: any moved lines are removed from the global command's
86df930be7Sderaadt   `active' list.
87df930be7Sderaadt
88df930be7Sderaadt4) If ed is invoked with a name argument prefixed by a bang (!), then the
89df930be7Sderaadt   remainder of the argument is interpreted as a shell command.  To invoke
90df930be7Sderaadt   ed on a file whose name starts with bang, prefix the name with a
91df930be7Sderaadt   backslash.
92